summaryrefslogtreecommitdiff
blob: cec0ed6dce23aea642a093e8bb6613cd8e21e2bc (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
#!/sbin/runscript
# Copyright 2009 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

VMNAME=${SVCNAME#*.}
PIDFILE=/var/run/vm/${VMNAME}.pid
MONITOR=/var/run/vm/${VMNAME}.monitor
QTAP_FILE=/var/run/vm/${VMNAME}.qtap

# modify this
VMSOFTWARE=${VMSOFTWARE:-qemu-kvm}
DROP_USER=${DROP_USER:-nobody}
MEMORY=${MEMORY:-512M}
TIMEOUT=${TIMEOUT:-300}
SMP=${SMP:-1}
export KVM_USER=${KVM_USER:-"root"}

extra_commands="reboot"

depend() {
	need net.br0
}

send_command() {
	local command="socat -u - UNIX-CONNECT:${MONITOR}"
	which nc6 2> /dev/null > /dev/null && command="nc6 -U ${MONITOR} --send-only"
	echo "$@" | ${command} >/dev/null 2>&1
}

sanity_check() {
	if [ "${VMNAME}" != "${SVCNAME}" ]; then
		eerror "You have to create an init script for each vm:"
		eerror " ln -s vm /etc/init.d/vm.vmname"
		return 1
	elif [ ! -f "${DISKIMAGE}" -a ! -b "${DISKIMAGE}" ]; then
		eerror "couldn't find \$DISKIMAGE '$DISKIMAGE'"
		return 1;
   	fi
}

start() {
	sanity_check

	img=$(readlink -f "${DISKIMAGE}")
	[ -z "$img" ] && {
		eerror "couldn't find ${DISKIMAGE}"
		return 1;
	}
	
	mkdir -p $(dirname ${PIDFILE}) $(dirname ${MONITOR})
	ebegin "creating qtap ${QTAP:-(auto allocating one)}"
	QTAP_RET=$(qtap-manipulate create ${QTAP});
	if [ 0 != $? ]; then
		eerror "failed to create qtap interface"
		return 1
	fi
	QTAP=${QTAP:-${QTAP_RET}}
	echo "${QTAP}" > ${QTAP_FILE}
	eend $?

	ebegin "Starting ${VMSOFTWARE-qemu} for ${VMNAME} at VNC port${VNC}"
	start-stop-daemon --start /usr/bin/${VMSOFTWARE-qemu} \
	--pidfile ${PIDFILE} \
	-- -daemonize -pidfile ${PIDFILE} -monitor unix:${MONITOR},server,nowait \
	-runas ${DROP_USER} -name ${VMNAME} \
	-drive file="$img",if=${DRIVE_MODEL:-virtio},cache=${DRIVE_CACHE:-none} \
	-net nic,model=${NIC_MODEL:-virtio},macaddr=${MACADDR} -net tap,ifname=${QTAP},script=no \
	${DISABLE_KVM:---enable-kvm}  \
	${MEMORY:+-m ${MEMORY}} ${SMP:+-smp ${SMP}} ${VNC:+-vnc ${VNC}} ${OTHER_ARGS}
	ret=$?
	if [ "0" != "${ret}" ]; then
		qtap-manipulate destroy ${QTAP}
	fi
	eend ${ret}
}

reboot() {
	if [ ${VMNAME} = ${SVCNAME} ]; then
	eerror "You have to create an init script for each vm:"
	eerror " ln -s vm /etc/init.d/vm.vmname"
	return 1
	fi
	
	ebegin "Rebooting ${VMNAME}"
	send_command system_reset
	eend $?
}

stop() {
	sanity_check

	ebegin "Powering off ${VMNAME}"
	send_command system_powerdown
	eend $?

	ebegin "waiting up to ${TIMEOUT:-60} seconds for it to die"
	ret=1
	for x in $(seq 0 ${TIMEOUT:-60}); do
		kill -0 $(cat $PIDFILE) 2>&1 > /dev/null || { ret=0; break; }
		sleep 1s
	done
	eend ${ret}

	ebegin "Stopping ${VMSOFTWARE-qemu} for ${VMNAME}"
	start-stop-daemon --stop /usr/bin/${VMSOFTWARE-qemu} \
	--user ${DROP_USER} \
	--pidfile ${PIDFILE} \
	--quiet
	eend $?
	QTAP=$(cat ${QTAP_FILE})
	ebegin "destroying qtap ${QTAP}"
	qtap-manipulate destroy ${QTAP}
	eend $?
}