aboutsummaryrefslogtreecommitdiff
path: root/virt/virt.init
blob: 6ccbd9e03486b34fa5846bb95c42b0844b29cd53 (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
#!/sbin/openrc-run
# vim: ft=sh
VM_NAME="${RC_SVCNAME#virt.}"

depend() {
	need networking
	after iptables ip6tables
	use logger
}

pid_file="/run/virt/$VM_NAME/pid"

status() {
	if [ -f "$pid_file" ]; then
		if kill -0 "$(cat "$pid_file")" >/dev/null 2>&1; then
			einfo "Virtual $VM_NAME is running"
		else
			eerror "Virtual $VM_NAME isn't running but some garbage was left"
		fi
	else
		ewarn "Virtual $VM_NAME isn't running"
	fi
}

start() {
	if [ "$RC_SVCNAME" = 'virt' ]; then
		eerror 'You are not supposed to run this runscript directly. Instead, you should create a symlink for the VM you want to run.'
		return 1
	fi

	mkdir -p /run/virt/$VM_NAME
	chown :qemu /run/virt/$VM_NAME
	chmod 775 /run/virt/$VM_NAME

	qemu-system-x86_64 $ARGS -daemonize -runas qemu \
		-pidfile "$pid_file" -D /var/log/virt.$VM_NAME.log \
		-monitor unix:/run/virt/$VM_NAME/monitor.sock,server,nowait \
		-serial unix:/run/virt/$VM_NAME/serial.sock,server,nowait
}

stop() {
	if [ -f "$pid_file" ] && kill -0 $(cat "$pid_file") >/dev/null 2>&1; then
		PID=$(cat "$pid_file")
		kill $PID
		# TODO if there are problems we should implement also timeout
		while kill -0 $PID >/dev/null 2>&1; do sleep 1; done
	else
		ewarn "Virtual $VM_NAME is not running"
	fi
}