-
Notifications
You must be signed in to change notification settings - Fork 0
/
qemu.initd
executable file
·123 lines (106 loc) · 4.46 KB
/
qemu.initd
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
#!/sbin/openrc-run
# Copyright 1999-2017 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
ID=${SVCNAME#*.}
ARCH=${ARCH:-x86_64}
pidfile="/run/qemu/${ID}.pid"
case "${SVCNAME%.*}" in
kvm)
command=$(which "qemu-system-${ARCH}")
[ -z "$command" ] && eerror "Failed to find the binary for qemu-system-${ARCH}"
;;
qemu)
command=$(which "qemu-${ARCH}")
[ -z "$command" ] && eerror "Failed to find the binary for qemu-${ARCH}"
;;
*)
eerror "Failed to find the binary for ${SVCNAME%.*}"
;;
esac
command_args="-mon chardev=qmp,mode=control \
-pidfile ${pidfile} \
-name ${NAME:-test} \
-boot menu=on,strict=on,reboot-timeout=1000 \
-vga ${VGA:-qxl} \
-vnc ${DISPLAY:-:1} \
-cpu ${CPU:-host} \
-m ${RAM:-512M} \
-numa node,nodeid=${NUMA},memdev=${NUMAMEM} \
-k ${LANGUAGE:-en-us}
-smbios ${SMBIOS} ${OPTS}"
name="${command##*/} for ${ID} (${NAME})"
start_stop_daemon_args="-q"
description="Start the system"
extra_commands="reboot"
description_reboot="Reboot the system"
extra_started_commands="console query reset resume status suspend"
description_console="Access QEMU console"
description_query="Query supported commands"
description_reset="Reset the system"
description_resume="Resume suspended system"
description_suspend="Suspend running system"
description_status="Show system status"
start_pre() {
if [ "$SVCNAME" = 'qemu' ] || [ "$SVCNAME" = 'kvm' ] ; then eerror "Create a symlink for the VM you want to run"; return 1; fi
if yesno ${KVM}; then command_args="${command_args} -enable-kvm"; fi
if yesno ${DAEMON}; then command_args="${command_args} -daemonize"; fi
if yesno ${NODEFAULTS}; then command_args="${command_args} -nodefaults"; fi
for idx in "${!CHARDEV[@]}"; do command_args="${command_args} -chardev ${CHARDEVBACKEND[$idx]},id=${CHARDEV[$idx]}${CHARDEVPARAM[$idx]} "; done
for idx in "${!OBJECT[@]}" ; do command_args="${command_args} -object ${OBJECTTYPE[$idx]},id=${OBJECT[$idx]} "; done
for idx in "${!DEVICE[@]}" ; do command_args="${command_args} -device ${DEVICEDRIVER[$idx]},id=${DEVICE[$idx]},bus=${DEVICEBUS[$idx]},${DEVICEADDR[$idx]}${DEVICEPARAM[$idx]} "; done
for idx in "${!SMP[@]}" ; do command_args="${command_args} -smp sockets=$(expr ${idx} + 1),cores=${SMP[$idx]} "; done
for idx in "${!DRIVE[@]}" ; do command_args="${command_args} -drive if=${DRIVEIF[$idx]},id=${DRIVE[$idx]}${DRIVEPARAM[$idx]} "; done
for idx in "${!NET[@]}" ; do command_args="${command_args} -netdev type=${NETTYPE[$idx]},id=${NET[$idx]}${NETPARAM[$idx]} "; done
for idx in "${!FS[@]}" ; do command_args="${command_args} -fsdev ${FSDRIVER[$idx]},id=${FS[$idx]},path=${FSPATH[$idx]},security_model=${FSMODEL[$idx]} "; done
if yesno ${VERBOSE} ; then einfo "Command: $command $(printf '%s ' $command_args)" ; fi
}
depend() {
need net
}
console() {
ebegin "Accessing console of ${ID} (${NAME}):"
socat unix-connect:/run/qemu/${ID}.qmp readline
eend $?
}
query() {
ebegin "Querring ${ID} (${NAME}) for supported commands"
echo '{ "execute": "qmp_capabilities" }{"execute":"query-commands"}' | socat - unix-connect:/run/qemu/${ID}.qmp
eend $?
}
reset() {
ebegin "Resetting ${ID} (${NAME})"
echo '{ "execute": "qmp_capabilities" }{"execute":"system_reset"}' | socat - unix-connect:/run/qemu/${ID}.qmp 1>/dev/null
eend $?
}
resume() {
ebegin "Resuming ${ID} (${NAME})"
echo '{ "execute": "qmp_capabilities" }{"execute":"cont"}' | socat - unix-connect:/run/qemu/${ID}.qmp 1>/dev/null
eend $?
}
reboot() {
ebegin "Rebooting ${ID} (${NAME})"
#send-keys
eend $?
}
status() {
ebegin "Status of ${ID} (${NAME}):"
echo '{ "execute": "qmp_capabilities" }{ "execute": "query-status" }' | socat - unix-connect:/run/qemu/${ID}.qmp
eend $?
}
suspend() {
ebegin "Suspending ${ID} (${NAME})"
echo '{ "execute": "qmp_capabilities" }{"execute":"stop"}' | socat - unix-connect:/run/qemu/${ID}.qmp 1>/dev/null
eend $?
}
stop() {
ebegin "Stopping ${command##*/} for ${ID} (${NAME})"
echo '{ "execute": "qmp_capabilities" }{"execute":"system_powerdown"}' | socat - unix-connect:/run/qemu/${ID}.qmp >/dev/null 2>&1
printf " Waiting $TIMEOUT seconds for shutdown "
while kill -0 $(cat ${pidfile}) 2>/dev/null && [ -e ${pidfile} ] && [ ${TIMEOUT} -gt 0 ] ; do sleep 1; printf '.'; TIMEOUT=$(( TIMEOUT - 1 )); done
printf '\n'
if [ ${TIMEOUT} -eq 0 ] ; then
ewarn "Failed to shutdown ${ID} (${NAME}) gracefully using ACPI, stopping it with force"
start-stop-daemon --stop --quiet --exec ${command} --pidfile ${pidfile} --retry SIGKILL/5
fi
eend $?
}