forked from jirutka/qemu-openrc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqemu.initd
364 lines (301 loc) · 8.87 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
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
#!/sbin/openrc-run
# vim: set ft=sh: ts=4:
# source: https://github.com/jirutka/qemu-openrc
VERSION='0.10.0'
VM_NAME="${RC_SVCNAME#qemu.}"
: ${user:=qemu}
: ${group:=qemu}
: ${pidfile:=/run/qemu/${VM_NAME}/qemu.pid}
: ${logfile:=/var/log/qemu/${VM_NAME}.log}
: ${shutdown_timeout:=40}
: ${system_type:=x86_64}
: ${enable_kvm:=yes}
: ${enable_usb_tablet:=no}
: ${enable_guest_agent:=no}
: ${enable_qmp:=no}
: ${enable_spice:=no}
: ${cpu_model:=host}
: ${smp_cpus:=1}
: ${smp_cpus_max:=$smp_cpus}
: ${memory:=1G}
: ${memory_max:=$memory}
: ${memory_slots:=2}
: ${memory_hugepages:=no}
: ${rtc_base:=utc}
: ${vga:=std}
: ${spice_listen:=0.0.0.0}
: ${spice_port:=5800}
: ${vnc_listen:=0.0.0.0}
: ${hugepages_path:=/dev/hugepages}
: ${monitor_socket:=/run/qemu/${VM_NAME}/monitor.sock}
: ${guest_agent_socket:=/run/qemu/${VM_NAME}/guest_agent.sock}
: ${qmp_socket:=/run/qemu/${VM_NAME}/qmp.sock}
: ${extra_args:=}
name="VM $VM_NAME"
description="QEMU virtual machine \"$VM_NAME\""
extra_commands='forcestop version'
description_forcestop='Force stop the system'
description_version='Show version of this script'
extra_started_commands='reset resume suspend vmstatus'
description_reset='Reset the system'
description_resume='Resume suspended VM'
description_suspend='Suspend running VM'
description_vmstatus='Show status reported by QEMU'
command="/usr/bin/qemu-system-$system_type"
command_args="
-name $VM_NAME,process=$VM_NAME
-nodefaults
-no-user-config
-cpu $cpu_model
-overcommit mem-lock=off
-rtc base=$rtc_base
-smp cpus=$smp_cpus,maxcpus=$smp_cpus_max
-device virtio-balloon
-vga $vga
-device virtio-rng-pci
-device virtio-scsi-pci,id=scsi
-monitor unix:$monitor_socket,server,nowait"
command_background='yes'
start_stop_daemon_args="
--user=$user
--wait=100
--stdout=$logfile
--stderr=$logfile"
required_files="$command"
depend() {
need net
after iptables ip6tables ebtables
}
start_pre() {
if [ "$RC_SVCNAME" = 'qemu' ]; then
eerror ''
eerror 'You are not supposed to run this runscript directly. Instead, you should'
eerror 'create a symlink for the VM you want to run as well as a copy of the'
eerror 'configuration file and modify it appropriately, like so:'
eerror ''
eerror ' ln -s qemu /etc/init.d/qemu.example'
eerror ' cp /etc/conf.d/qemu /etc/conf.d/qemu.example'
return 1
fi
if yesno "$enable_kvm"; then
command_args_push '-enable-kvm'
fi
if yesno "$enable_usb_tablet"; then
command_args_push '-usb -device usb-tablet'
fi
if yesno "$enable_guest_agent"; then
command_args_push '-chardev socket,path=$guest_agent_socket,server,nowait,id=qga0 -device virtio-serial -device virtserialport,chardev=qga0,name=org.qemu.guest_agent.0'
fi
if yesno "$enable_qmp"; then
command_args_push '-qmp unix:$qmp_socket,server,nowait'
fi
if yesno "$enable_spice"; then
if [ -n "$spice_password" ]; then
command_args_push '-spice port=$spice_port,addr=$spice_listen,password=$spice_password'
else
command_args_push '-spice port=$spice_port,addr=$spice_listen,disable-ticketing'
fi
command_args_push '-device virtio-serial -chardev spicevmc,id=spicechannel0,name=vdagent
-device virtserialport,chardev=spicechannel0,name=com.redhat.spice.0
-chardev spiceport,name=org.spice-space.webdav.0,id=charchannel1
-device virtserialport,chardev=charchannel1,id=channel1,name=org.spice-space.webdav.0'
fi
if [ "$memory" = "$memory_max" ]; then
memory_slots="0"
fi
command_args_push "-m size=$memory,slots=$memory_slots,maxmem=$memory_max"
if yesno "$memory_hugepages"; then
command_args_push "-mem-path $hugepages_path"
fi
if [ -n "$vnc_display" ]; then
command_args_push "-display vnc=${vnc_listen}:${vnc_display}${vnc_password:+",password"}"
fi
command_args_push "$(net_args) $(disk_args) $(cdrom_args) $extra_args"
if yesno "$EINFO_VERBOSE"; then
einfo "Command: $command $(printf '%s ' $command_args)"
fi
local path; for path in "$pidfile" "$monitor_socket" "$guest_agent_socket" "$logfile" "$qmp_socket"; do
# checkpath doesn't create intermediate directories
mkdir -p "$(dirname "$path")"
checkpath -d -m 0750 -o $user:$group "$(dirname "$path")"
done
return 0
}
start_post() {
ewaitfile 5 "$monitor_socket" || {
eerror 'Monitor socket has not been created!'; return 1
}
if yesno "$enable_guest_agent"; then
ewaitfile 5 "$guest_agent_socket" || {
eerror 'Guest Agent socket has not been created!'; return 1
}
fi
if yesno "$enable_qmp"; then
ewaitfile 5 "$qmp_socket" || {
eerror 'QMP socket has not been created!'; return 1
}
fi
if [ -n "$vnc_password" ]; then
qemush "set_password vnc $vnc_password" || eerror 'Failed to set VNC password!'
fi
}
stop() {
local count=0
local retval=0
if is_running && yesno "$enable_guest_agent"; then
ebegin "Stopping $name with guest agent"
count="$shutdown_timeout"
echo -e '{"execute": "guest-shutdown"}' | socat - "unix-connect:$guest_agent_socket"
printf " Waiting $count seconds for VM shutdown "
while is_running && [ $count -gt 0 ]; do
sleep 1
printf '.'
count=$(( count - 1 ))
done
printf '\n'
elif is_running && qemush 'system_powerdown'; then
ebegin "Stopping $name with qemu monitor (ACPI)"
count="$shutdown_timeout"
printf " Waiting $count seconds for VM shutdown "
while is_running && [ $count -gt 0 ]; do
sleep 1
printf '.'
count=$(( count - 1 ))
done
printf '\n'
fi
if [ $count -eq 0 ]; then
if yesno "$enable_guest_agent"; then
ewarn 'Failed to shutdown VM gracefully using guest agent, stopping it with force'
else
ewarn 'Failed to shutdown VM gracefully using qemu monitor (ACPI), stopping it with force'
fi
start-stop-daemon --stop \
--quiet --retry 'SIGKILL/5' \
--pidfile "$pidfile" --exec "$command"
retval="$?"
fi
eend $retval
}
stop_post() {
[ -S "$monitor_socket" ] && rm -f "$monitor_socket"
if yesno "$enable_guest_agent"; then
[ -S "$guest_agent_socket" ] && rm -f "$guest_agent_socket"
fi
if yesno "$enable_qmp"; then
[ -S "$qmp_socket" ] && rm -f "$qmp_socket"
fi
[ -f "$pidfile" ] && rm -f "$pidfile"
return 0
}
forcestop() {
ebegin "Force stopping $name"
start-stop-daemon --stop \
--quiet --retry 'SIGKILL/3' \
--pidfile "$pidfile" --exec "$command"
local retval="$?"
if [ $retval -eq 0 ]; then
if service_started "$RC_SVCNAME"; then
mark_service_stopped "$RC_SVCNAME"
fi
stop_post
fi
eend $retval
}
reset() {
ebegin "Resetting $name"
qemush 'system_reset'
eend $?
}
resume() {
ebegin "Resuming suspended $name"
qemush 'cont'
eend $?
}
suspend() {
ebegin "Suspending $name"
qemush 'stop'
eend $?
}
vmstatus() {
qemush_show 'info status' | tr -d '\r' | xargs einfo
}
version() {
echo "qemu-openrc $VERSION"
}
#-------------------------------- Helpers -------------------------------
is_running() {
[ -e "$pidfile" ] && kill -0 "$(cat "$pidfile")" 2>/dev/null
}
command_args_push() {
command_args="$command_args $@"
}
disk_args() {
local idx opts
for idx in $(seq 0 9); do
# comma-separated key=value pairs; contains trailing comma if not empty
opts=$(set | sed -En "s/^disk${idx}_(.*)/\1/p" | tr $'\n' ',')
if [ -n "$opts" ]; then
echo "-drive id=hd${idx},${opts}media=disk,if=none"
echo "-device scsi-hd,drive=hd${idx},scsi-id=${idx}"
fi
done
}
cdrom_args() {
local idx file
for idx in $(seq 0 9); do
file=$(getval "cdrom${idx}_file")
if [ -n "$file" ]; then
echo "-drive id=cdr${idx},media=cdrom,if=none,file='$file',readonly,cache=none"
echo "-device ide-cd,drive=cdr${idx}"
fi
done
}
net_args() {
local idx net_id net_type opts mac dev
for idx in $(seq 0 9); do
net_id="net${idx}"
net_type=$(getval "$net_id")
if [ "$net_type" = 'bridge' ]; then
check_bridge "$(getval ${net_id}_br br0)"
fi
if [ -n "$net_type" ]; then
# comma-separated key=value pairs; contains trailing comma if not empty
opts=$(set | sed -En "s/^net${idx}_(.*)/\1/p" \
| grep -Ev '^(mac|device)=.*$' \
| tr $'\n' ',')
mac=$(getval ${net_id}_mac "$(gen_macaddr ${VM_NAME}#${idx})")
dev=$(getval ${net_id}_device virtio-net-pci)
echo "-netdev ${net_type},${opts}id=hostnet${idx}"
echo "-device ${dev},id=${net_id},netdev=hostnet${idx},mac=${mac}"
fi
done
}
check_bridge() {
local name="$1"
if [ ! -e "/sys/class/net/$name" ]; then
ewarn "WARNING: Bridge $name does not exist"
return 1
fi
if [ "$user" != 'root' ] \
&& ! grep -q "^allow\s*$name\W*" /etc/qemu/bridge.conf 2>/dev/null; then
ewarn "WARNING: Bridge $name must be allowed in /etc/qemu/bridge.conf"
return 1
fi
}
qemush() {
local IFS=$'\n'
printf "%b\n" "$*" | socat - "UNIX-CONNECT:${monitor_socket}" 1>/dev/null
}
qemush_show() {
local IFS=$'\n'
printf "%b\n" "$*" | socat - "UNIX-CONNECT:${monitor_socket}" | tail -n +3 | head -n -1
}
gen_macaddr() {
printf "$1" | md5sum | sed -E 's/^(..)(..)(..)(..).*$/52:54:\1:\2:\3:\4/'
}
getval() {
local var_name="$1"
local default="${2:-}"
eval "printf '%s\n' \"\${$var_name:-$default}\""
}