Skip to content

Commit

Permalink
arisu/qemu-openrc: add guest-agent socket patch
Browse files Browse the repository at this point in the history
  • Loading branch information
arisudesu committed Sep 3, 2024
1 parent bcb6ace commit beead96
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 2 deletions.
6 changes: 4 additions & 2 deletions arisu/qemu-openrc/APKBUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
# Maintainer:
pkgname=qemu-openrc
pkgver=0.11.1
pkgrel=0
pkgrel=1
pkgdesc="OpenRC init scripts for QEMU/KVM"
url="https://github.com/jirutka/qemu-openrc"
arch="noarch"
license="MIT"
depends="qemu socat"
install="$pkgname.pre-install $pkgname.pre-upgrade"
source="https://github.com/jirutka/$pkgname/archive/v$pkgver/$pkgname-$pkgver.tar.gz"
source="https://github.com/jirutka/$pkgname/archive/v$pkgver/$pkgname-$pkgver.tar.gz
add-guest-agent-socket.patch"
options="!check"

package() {
Expand All @@ -18,4 +19,5 @@ package() {

sha512sums="
aae6eb14a22f3a452e8bc523609e52b0d41af0e4ea5abf1faaf5f78a53dd4ca8905ded3a0cdbe8835a37c3b8218e20f407f80ef050d35cf2e6eb380ada26db67 qemu-openrc-0.11.1.tar.gz
c9d0165c0a54867af5b358fb094e833e9568cea3f6a621feb401b0937dc60d5ae84a13a6b8ec12223f629612b68777c873adc54729a5b697edabd4618b555629 add-guest-agent-socket.patch
"
97 changes: 97 additions & 0 deletions arisu/qemu-openrc/add-guest-agent-socket.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
From 1ae75e2e8c04257629e44ff0bd5ab70f5014bcec Mon Sep 17 00:00:00 2001
From: Arisu Desu <arisudesu@yandex.ru>
Date: Sat, 22 Jun 2024 23:08:54 +0300
Subject: [PATCH] Add guest-agent socket; make use of it to request guest
shutdown along with ACPI request

---
qemu.confd | 2 ++
qemu.initd | 20 +++++++++++++++-----
2 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/qemu.confd b/qemu.confd
index 9dfd1cf..6733b08 100644
--- a/qemu.confd
+++ b/qemu.confd
@@ -98,6 +98,8 @@
# Path of the QEMU monitor socket for this VM.
#monitor_socket="/run/qemu/${VM_NAME}/monitor.sock"

+# Path of the QEMU Guest Agent socket for this VM.
+#qemu_ga_socket="/run/qemu/${VM_NAME}/qemu_ga.sock"

##
# Network interfaces
diff --git a/qemu.initd b/qemu.initd
index 9cfb3c7..ee87243 100644
--- a/qemu.initd
+++ b/qemu.initd
@@ -26,6 +26,7 @@ VM_NAME="${RC_SVCNAME#qemu.}"
: ${vnc_listen:=0.0.0.0}
: ${hugepages_path:=/dev/hugepages}
: ${monitor_socket:=/run/qemu/${VM_NAME}/monitor.sock}
+: ${qemu_ga_socket:=/run/qemu/${VM_NAME}/qemu-ga.sock}
: ${extra_args:=}

name="VM $VM_NAME"
@@ -54,7 +55,10 @@ command_args="
-vga $vga
-device virtio-rng-pci
-device virtio-scsi-pci,id=scsi
- -monitor unix:$monitor_socket,server,nowait"
+ -monitor unix:$monitor_socket,server,nowait
+ -chardev socket,path=$qemu_ga_socket,server,nowait,id=qga0
+ -device virtio-serial
+ -device virtserialport,chardev=qga0,name=org.qemu.guest_agent.0"
command_background='yes'
command_user="$user"
command_group="$group"
@@ -102,7 +106,7 @@ start_pre() {
einfo "Command: $command $(printf '%s ' $command_args)"
fi

- local path; for path in "$pidfile" "$monitor_socket" "$logfile"; do
+ local path; for path in "$pidfile" "$monitor_socket" "$qemu_ga_socket" "$logfile"; do
# checkpath doesn't create intermediate directories
mkdir -p "$(dirname "$path")"
checkpath -d -m 0750 -o $user:$group "$(dirname "$path")"
@@ -112,8 +116,8 @@ start_pre() {
}

start_post() {
- ewaitfile 5 "$monitor_socket" || {
- eerror 'Monitor socket has not been created!'; return 1
+ ewaitfile 5 "$monitor_socket" "$qemu_ga_socket" || {
+ eerror 'Monitor socket or guest agent socket have not been created!'; return 1
}
if [ -n "$vnc_password" ]; then
qemush "set_password vnc $vnc_password" || eerror 'Failed to set VNC password!'
@@ -126,7 +130,7 @@ stop() {

ebegin "Stopping $name"

- if is_running && qemush 'system_powerdown'; then
+ if is_running && guestcmd '{"execute":"guest-shutdown"}' && qemush 'system_powerdown'; then
count="$shutdown_timeout"

printf " Waiting $count seconds for VM shutdown "
@@ -152,6 +156,7 @@ stop() {

stop_post() {
[ -S "$monitor_socket" ] && rm -f "$monitor_socket"
+ [ -S "$qemu_ga_socket" ] && rm -f "$qemu_ga_socket"
[ -f "$pidfile" ] && rm -f "$pidfile"
return 0
}
@@ -290,6 +295,11 @@ qemush_show() {
printf "%b\n" "$*" | socat - "UNIX-CONNECT:${monitor_socket}" | tail -n +3 | head -n -1
}

+guestcmd() {
+ local IFS=$'\n'
+ printf "%b\n" "$*" | socat - "UNIX-CONNECT:${qemu_ga_socket}" 1>/dev/null
+}
+
gen_macaddr() {
printf "$1" | md5sum | sed -E 's/^(..)(..)(..)(..).*$/52:54:\1:\2:\3:\4/'
}

0 comments on commit beead96

Please sign in to comment.