summaryrefslogtreecommitdiffstats
path: root/kvm-wrapper.sh
blob: b1727bf40c0dccb1282e0ba12840ad540d93ddb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
#!/bin/sh
#
# KVM Wrapper Script
# -- bencoh, 2009/06
#


#######################################################################
# Draft 
#######################################################################
#
# kvm -net nic,model=virtio,macaddr=00:11:22:33:44:55 -net tap
# 	  -hda /path/to/hda -hdb /path/to/hdb -cdrom /path/to/cdrom
#	  -boot a|c|d|n
#	  -k en-us
#	  -vnc :1 -nographics -curses
#	  -pidfile myfile.pid
#
#######################################################################

PATH=/usr/sbin:/usr/bin:/sbin:/bin

function canonpath ()
{
	echo $(cd $(dirname $1); pwd -P)/$(basename $1)
}

# Exit on fail and print a nice message
function fail_exit ()
{
	echo -e "$1"
	echo "Exiting."
	exit 1
}

# File/socket/directory tester
function test_dir ()
{
	DIR="$1"
	[[ -d "$DIR" && -r "$DIR" ]]
}

function test_dir_rw ()
{
	DIR="$1"
	[[ -d "$DIR" && -r "$DIR" && -w "$DIR" ]]
}

function test_file ()
{
	FILE="$1"
	[[ -f "$FILE" && -r "$FILE" ]]
}

function test_file_rw ()
{
	FILE="$1"
	[[ -f "$FILE" && -r "$FILE" && -w "$FILE" ]]
}

function test_socket ()
{
	FILE="$1"
	[[ -S "$FILE" && -r "$FILE" ]]
}

function test_socket_rw ()
{
	FILE="$1"
	[[ -S "$FILE" && -r "$FILE" && -w "$FILE" ]]
}

function check_create_dir ()
{
	DIR="$1"
	test_dir_rw "$DIR" || mkdir -p "$DIR"
	test_dir_rw "$DIR" || fail_exit "Couldn't read/write VM PID directory :\n$DIR"
}

function random_mac ()
{
# Macaddress : 52:54:00:ff:34:56
RANGE=99
STR=""
for blah in 0 1
do
	number=$RANDOM
	let "number %= $RANGE"
	STR="$STR"":""$number"
done
MACADDRESS="52:54:00:ff""$STR"
echo -ne $MACADDRESS
}

function bs_copy_from_host()
{
	FILE="$1"
	cp -rf "$FILE" "$MNTDIR/$FILE"
}

# Update (if exists) descriptor setting and keep a backup, create otherwise
function desc_update_backup_setting ()
{
	KEY="$1"
	VALUE="$2"
	IDENT=$RANDOM

	#sed -i "s/^$KEY.*/#\0 ###AUTO$IDENT\n$KEY=$(escape_sed "\"$VALUE\"") ###AUTO$IDENT/g" "$VM_DESCRIPTOR"
	sed -i "s/^$KEY.*/#\0 ###AUTO$IDENT/g" "$VM_DESCRIPTOR"
	echo "$KEY=\"$VALUE\" ###AUTO$IDENT" >> "$VM_DESCRIPTOR"

	echo $IDENT
}

# Overwrite (or create) descriptor setting
function desc_update_setting ()
{
	KEY="$1"
	VALUE="$2"

	#sed -i "s/^$KEY.*/$KEY=$(escape_sed "\"$VALUE\"") ###AUTO/g" "$VM_DESCRIPTOR"
	sed -i "/^$KEY.*/d" "$VM_DESCRIPTOR"
	echo "$KEY=\"$VALUE\" ###AUTO" >> "$VM_DESCRIPTOR"
}

# Revert descriptor setting modified by this script
function desc_revert_setting()
{
	IDENT=$1
	sed -i "/^[^#].*###AUTO$IDENT$/d" "$VM_DESCRIPTOR"
	sed -ie "s/^#\(.*\)###AUTO$IDENT$/\1/g" "$VM_DESCRIPTOR"
}

# VM Status
function kvm_status_vm ()
{
	VM_NAME="$1"
	PID_FILE="$PID_DIR/$VM_NAME-vm.pid"
	test_file "$PID_FILE" || fail_exit "Error : $VM_NAME doesn't seem to be running."
	status_from_pid_file "$PID_FILE"
}

function status_from_pid_file ()
{
	VM_PID=`cat "$1"`
	ps wwp "$VM_PID"
}

function kvm_status ()
{
	if [[ ! "$1" == "all" ]];
	then
		kvm_status_vm "$1"
	else
		for file in "$PID_DIR"/*-vm.pid
		do
			VM_NAME=`basename ${file%"-vm.pid"}`
			echo $VM_NAME :
			status_from_pid_file "$file"
		done
	fi
}

# Main function : start a virtual machine
function kvm_start_vm ()
{
	VM_NAME="$1"
	VM_DESCRIPTOR="$VM_DIR/$VM_NAME-vm"
	PID_FILE="$PID_DIR/$VM_NAME-vm.pid"
	MONITOR_FILE="$MONITOR_DIR/$VM_NAME.unix"
	SERIAL_FILE="$SERIAL_DIR/$VM_NAME.unix"

	check_create_dir "$PID_DIR"
	check_create_dir "$MONITOR_DIR"
	check_create_dir "$SERIAL_DIR"

	test_file "$VM_DESCRIPTOR" || fail_exit "Couldn't open VM $VM_NAME descriptor :\n$VM_DESCRIPTOR"
	source "$VM_DESCRIPTOR"

	[[ -z "$KVM_BIN" ]] && KVM_BIN="/usr/bin/kvm"

	# Build KVM Drives (hdd, cdrom) parameters
	KVM_DRIVES=""
	[[ -n "$KVM_HDA" ]] && KVM_DRIVES="$KVM_DRIVES -hda $KVM_HDA"
	[[ -n "$KVM_HDB" ]] && KVM_DRIVES="$KVM_DRIVES -hdb $KVM_HDB"
	[[ -n "$KVM_HDC" ]] && KVM_DRIVES="$KVM_DRIVES -hdc $KVM_HDC"
	[[ -n "$KVM_HDD" ]] && KVM_DRIVES="$KVM_DRIVES -hdd $KVM_HDD"
	[[ -n "$KVM_CDROM" ]] && KVM_DRIVES="$KVM_DRIVES -cdrom $KVM_CDROM"
	[[ "$KVM_DRIVES" == "" ]] && fail_exit "Your VM $VM_NAME should at least use one cdrom or harddisk drive !\nPlease check your conf file :\n$VM_DESCRIPTOR"
	LINUXBOOT=""
	[[ -n "$KVM_KERNEL" ]] && LINUXBOOT="$LINUXBOOT -kernel $KVM_KERNEL"
	[[ -n "$KVM_INITRD" ]] && LINUXBOOT="$LINUXBOOT -initrd $KVM_INITRD"
	[[ -n "$KVM_APPEND" ]] && LINUXBOOT="$LINUXBOOT -append \"$KVM_APPEND\""

	# Are we bridged or nated ? (default nated)
	KVM_NET_TAP="tap"
	KVM_NET_SCRIPT="$ROOTDIR/net/kvm"
	[[ "$KVM_BRIDGE" == "br0" ]] && KVM_NET_SCRIPT="$ROOTDIR/net/kvm-br0"
	KVM_NET_TAP="tap,script=$KVM_NET_SCRIPT-ifup,downscript=$KVM_NET_SCRIPT-ifdown"

	# Monitor/serial devices
	KVM_MONITORDEV="-monitor unix:$MONITOR_FILE,server,nowait"
	KVM_SERIALDEV="-serial unix:$SERIAL_FILE,server,nowait"

	# Build kvm exec string
	EXEC_STRING="$KVM_BIN -m $KVM_MEM -smp $KVM_CPU_NUM -net nic,model=$KVM_NETWORK_MODEL,macaddr=$KVM_MACADDRESS -net $KVM_NET_TAP $KVM_DRIVES -boot $KVM_BOOTDEVICE -k $KVM_KEYMAP $KVM_OUTPUT $LINUXBOOT $KVM_MONITORDEV $KVM_SERIALDEV -pidfile $PID_FILE $KVM_ADDITIONNAL_PARAMS"

	# More sanity checks : VM running, monitor socket existing, etc.
	test_file "$PID_FILE" && fail_exit "VM $VM_NAME seems to be running already.\nPID file $PID_FILE exists"
	rm -rf "$MONITOR_FILE"
	rm -rf "$SERIAL_FILE"
	test_socket "$MONITOR_FILE" && fail_exit "Monitor socket $MONITOR_FILE already existing and couldn't be removed"
	test_socket "$SERIAL_FILE" && fail_exit "Serial socket $SERIAL_FILE already existing and couldn't be removed"


	# Now run kvm
	echo $EXEC_STRING
	echo ""
	echo ""
	eval $EXEC_STRING

	# Cleanup files
	rm -rf "$PID_FILE"
	rm -rf "$MONITOR_FILE"
	rm -rf "$SERIAL_FILE"

	# Exit
	return 0
}



function kvm_stop_vm ()
{
	VM_NAME="$1"
	PID_FILE="$PID_DIR/$VM_NAME-vm.pid"
	MONITOR_FILE="$MONITOR_DIR/$VM_NAME.unix"

	test_file "$PID_FILE" || fail_exit "VM $VM_NAME doesn't seem to be running.\nPID file $PID_FILE not found"
#	test_file_rw "$MONITOR_FILE" || fail_exit "Monitor socket $MONITOR_FILE not existing or not writable"

	TIMELIMIT=30

	# Send monitor command through unix socket
	echo "Trying to powerdown the VM $VM_NAME first, might take some time (up to $TIMELIMIT sec)"
	echo "system_powerdown" | socat - unix:"$MONITOR_FILE"
	echo -n "Waiting ..."

	# Now wait for it
	ELAPSED=0
	while [[ $ELAPSED -le $TIMELIMIT ]]
	do
		ELAPSED=$(($ELAPSED+1))
		! test_file "$PID_FILE" && PROPER=1;
		[[ $PROPER -eq 1 ]] && break
		sleep 1
	done
	echo " elapsed time : $ELAPSED sec"

	if [[ $PROPER -eq 1 ]];
	then
		echo "VM powerdown properly :)"
	else
	
		# kill - SIGTERM
		KVM_PID="`cat $PID_FILE`"
		echo "Now trying to terminate (SIGTERM) $VM_NAME, pid $KVM_PID"
		kill "$KVM_PID"
	fi

	rm -rf "$PID_FILE" || fail_exit "Couldn't remove pid file"
	
	return 0
}

function kvm_run_disk ()
{
	KVM_HDA="$1"
	test_file_rw "$KVM_HDA" || "Couldn't read/write image file :\n$KVM_HDA"

	# Build kvm exec string
	EXEC_STRING="kvm -net nic,model=$KVM_NETWORK_MODEL,macaddr=$KVM_MACADDRESS -net tap -hda $KVM_HDA -boot c -k $KVM_KEYMAP $KVM_OUTPUT $KVM_ADDITIONNAL_PARAMS"
	$EXEC_STRING

	return 0
}

function kvm_start_screen ()
{
	VM_NAME="$1"
	SCREEN_SESSION_NAME="kvm-$VM_NAME"
	screen -d -m -S "$SCREEN_SESSION_NAME" /bin/sh -c "\"$SCRIPT_PATH\" start \"$VM_NAME\""
	EXITNUM="$?"
	return $EXITNUM
}

function kvm_attach_screen ()
{
	VM_NAME="$1"
	PID_FILE="$PID_DIR/$VM_NAME-vm.pid"
	SCREEN_SESSION_NAME="kvm-$VM_NAME"
	! test_file "$PID_FILE" && fail_exit "Error : $VM_NAME doesn't seem to be running."
	screen -x "$SCREEN_SESSION_NAME"
}

function kvm_monitor ()
{
	VM_NAME="$1"
	PID_FILE="$PID_DIR/$VM_NAME-vm.pid"
	! test_file "$PID_FILE" && fail_exit "Error : $VM_NAME doesn't seem to be running."
	MONITOR_FILE="$MONITOR_DIR/$VM_NAME.unix"
	! test_socket_rw "$MONITOR_FILE" && fail_exit "Error : could not open monitor socket $MONITOR_FILE."
	echo "Attaching monitor unix socket (using socat). Press ^D (EOF) to exit"
	socat - unix:"$MONITOR_FILE"
	echo "Monitor exited"
}

function kvm_serial ()
{
	VM_NAME="$1"
	PID_FILE="$PID_DIR/$VM_NAME-vm.pid"
	! [[ -f "$PID_FILE" ]] && fail_exit "Error : $VM_NAME doesn't seem to be running."
	SERIAL_FILE="$SERIAL_DIR/$VM_NAME.unix"
	! test_socket_rw "$SERIAL_FILE" && fail_exit "Error : could not open serial socket $SERIAL_FILE."
	echo "Attaching serial console unix socket (using socat). Press ^] to exit"
	socat -,IGNBRK=0,BRKINT=0,PARMRK=0,ISTRIP=0,INLCR=0,IGNCR=0,ICRNL=0,IXON=0,OPOST=1,ECHO=0,ECHONL=0,ICANON=0,ISIG=0,IEXTEN=0,CSIZE=0,PARENB=0,CS8,escape=0x1d unix:"$SERIAL_FILE"
	[[ "xx$?" != "xx0" ]] && fail_exit "socat must be of version > 1.7.0 to work"
	stty sane
	echo "Serial console exited"
}

function kvm_list ()
{
	echo "Available VM descriptors :"
	for file in "$VM_DIR"/*-vm
	do
		VM_NAME=`basename "${file%"-vm"}"`
		VM_STATUS="Halted"
		PID_FILE="$PID_DIR/$VM_NAME-vm.pid"
		test_file "$PID_FILE" && VM_STATUS="Running"
		echo -e "$VM_STATUS\t\t$VM_NAME"
	done
}

function kvm_edit_descriptor ()
{
	VM_NAME="$1"
	VM_DESCRIPTOR="$VM_DIR/$VM_NAME-vm"
	test_file "$VM_DESCRIPTOR" && "$EDITOR" "$VM_DESCRIPTOR"
}

function kvm_create_descriptor ()
{
	if [[ $# -ge 2 ]]
	then
		[[ ! -x "$KVM_IMG_BIN" ]] && fail_exit "kvm-img not found or not executable"
		KVM_IMG_DISKNAME="`canonpath \"$2\"`"
	fi
	if [[ $# -eq 2 ]]
	then
		DISK_CREATED=1
	fi
	if [[ $# -eq 3 ]]
	then
		echo "Calling kvm-img to create disk image"
		KVM_IMG_DISKSIZE="$3"
		"$KVM_IMG_BIN" create -f "$KVM_IMG_FORMAT" "$KVM_IMG_DISKNAME" "$KVM_IMG_DISKSIZE"
		if [[ "xx$?" == "xx0" ]] 
		then
			DISK_CREATED=1
		else
			echo "Failed creating disk. Creating vm anyway"
		fi
	fi

	VM_NAME="$1"
	VM_DESCRIPTOR="$VM_DIR/$VM_NAME-vm"
	test_file "$VM_DESCRIPTOR" && fail_exit "Error : $VM_NAME exists already ($VM_DESCRIPTOR found)"

	touch "$VM_DESCRIPTOR"
	echo "# VM $VM_NAME file descriptor" 			>> "$VM_DESCRIPTOR"
	echo "# Created : `date` on $HOSTNAME by $USER" >> "$VM_DESCRIPTOR"
	echo "" 										>> "$VM_DESCRIPTOR"

	foo=`grep -n '#xxDEFAULTxx#' "$CONFFILE"`
	foo=${foo%:*}
	
	cat "$CONFFILE" | while read LINE
	do
		[[ $foo -lt 0 ]] && echo "#$LINE" >> "$VM_DESCRIPTOR"
		((foo--))
	done

	if [[ "xx$DISK_CREATED" == "xx1" ]]
	then
		HDA_LINE="KVM_HDA=\"$KVM_IMG_DISKNAME\""
		sed -i "s,##KVM_HDA,$HDA_LINE,g" "$VM_DESCRIPTOR"
	fi
	MAC_ADDR="`random_mac`"
	sed -i 's/`random_mac`/'"$MAC_ADDR/g" "$VM_DESCRIPTOR"
	sed -i 's/#KVM_MAC/KVM_MAC/g' "$VM_DESCRIPTOR"

	echo "VM $VM_NAME created. Descriptor : $VM_DESCRIPTOR"
}

function kvm_bootstrap_vm ()
{
	VM_NAME="$1"
	VM_DESCRIPTOR="$VM_DIR/$VM_NAME-vm"
	test_file_rw "$VM_DESCRIPTOR" || fail_exit "Couldn't read/write VM $VM_NAME descriptor :\n$VM_DESCRIPTOR"
	source "$VM_DESCRIPTOR"

	PID_FILE="$PID_DIR/$VM_NAME-vm.pid"
	test_file "$PID_FILE" && fail_exit "Error : $VM_NAME seems to be running. Please stop it before trying to bootstrap it."

	BOOTSTRAP_DISTRIB="$2"
	BOOTSTRAP_SCRIPT="$BOOTSTRAP_DIR/$BOOTSTRAP_DISTRIB/bootstrap.sh"
	test_file "$BOOTSTRAP_SCRIPT" || fail_exit "Couldn't read $BOOTSTRAP_SCRIPT to bootstrap $VM_NAME as $BOOTSTRAP_DISTRIB"
	source "$BOOTSTRAP_SCRIPT"
	
	# Start bootstrap
	echo "Starting to bootstrap $VM_NAME as $BOOTSTRAP_DISTRIB on disk $KVM_HDA"
	bootstrap_fs "$KVM_HDA"
	echo "Bootstrap ended."
	return 0

}

function kvm_remove ()
{
	VM_NAME="$1"
	VM_DESCRIPTOR="$VM_DIR/$VM_NAME-vm"
	test_file_rw "$VM_DESCRIPTOR" || fail_exit "Couldn't read/write VM $VM_NAME descriptor :\n$VM_DESCRIPTOR"
	source "$VM_DESCRIPTOR"
	DRIVES_LIST=""
	[[ -n "$KVM_HDA" ]] && DRIVES_LIST="$DRIVES_LIST$KVM_HDA\n"
	[[ -n "$KVM_HDB" ]] && DRIVES_LIST="$DRIVES_LIST$KVM_HDB\n"
	[[ -n "$KVM_HDC" ]] && DRIVES_LIST="$DRIVES_LIST$KVM_HDC\n"
	[[ -n "$KVM_HDD" ]] && DRIVES_LIST="$DRIVES_LIST$KVM_HDD\n"
	if [[ -n "$DRIVES_LIST" ]]; then
		echo "The VM $VM_NAME used the following disks (NOT removed by $SCRIPT_NAME :"
		echo -e "$DRIVES_LIST"
	fi
	rm -f "$VM_DESCRIPTOR"
	test_file "$VM_DESCRIPTOR" && fail_exit "Failed to remove descriptor $VM_DSCRIPTOR."
}

function print_help ()
{
	echo -e "Usage: $SCRIPT_NAME {start|screen|stop} virtual-machine"
	echo -e "       $SCRIPT_NAME {attach|monitor|serial} virtual-machine"
	echo -e "       $SCRIPT_NAME rundisk disk-image"
	echo -e ""
	echo -e "       $SCRIPT_NAME status [virtual-machine]"
	echo -e "       $SCRIPT_NAME list"
	echo -e ""
	echo -e "       $SCRIPT_NAME edit   virtual-machine"
	echo -e "       $SCRIPT_NAME create virtual-machine [diskimage] [size]"
	echo -e "       $SCRIPT_NAME remove virtual-machine"
	exit 2
}

SCRIPT_PATH="$0"
SCRIPT_NAME="`basename $SCRIPT_PATH`"
ROOTDIR="/usr/share/kvm-wrapper"
CONFFILE="$ROOTDIR/kvm-wrapper.conf"

test_dir "$ROOTDIR" || fail_exit "Couldn't open kvm-wrapper's root directory :\n$ROOTDIR"
test_file "$CONFFILE" || fail_exit "Couldn't open kvm-wrapper's configuration file :\n$CONFFILE"

# Load default configuration file
source "$CONFFILE"

# Check VM descriptor directory
test_dir "$VM_DIR" || fail_exit "Couldn't open VM descriptor directory :\n$VM_DIR"

# Check arguments number
if [[ $# -eq 1 ]]
then
	case "$1" in
		status)
		  kvm_status "all"
		  ;;
		list)
		  kvm_list
		  ;;
		*)
		  print_help
		  ;;
	esac
elif [[ $# -eq 2 ]]
then
	case "$1" in
		start)
		  kvm_start_vm "$2"
		  ;;
		screen)
		  kvm_start_screen "$2"
		  ;;
		attach)
		  kvm_attach_screen "$2"
		  ;;
		monitor)
		  kvm_monitor "$2"
		  ;;
		serial)
		  kvm_serial "$2"
		  ;;
		stop)
		  kvm_stop_vm "$2"
		  ;;
		rundisk)
		  kvm_run_disk "$2"
		  ;;
		status)
		  kvm_status "$2"
		  ;;
		edit)
		  kvm_edit_descriptor "$2"
		  ;;
		create)
		  kvm_create_descriptor "$2"
		  ;;
		remove)
		  kvm_remove "$2"
		  ;;
		*)
		  print_help
		  ;;
	esac
elif [[ $# -eq 3 ]]
then
	case "$1" in
		create)
		  kvm_create_descriptor "$2" "$3"
		  ;;
		bootstrap)
		  kvm_bootstrap_vm "$2" "$3"
		  ;;
		*)
		  print_help
		  ;;
	esac

elif [[ $# -eq 4 ]]
then
	case "$1" in
		create)
		  kvm_create_descriptor "$2" "$3" "$4"
		  ;;
		*)
		  print_help
		  ;;
	esac
else
	print_help
fi