feat: add hot plugging for usb devices

This commit is contained in:
Anton Livaja 2025-04-30 18:48:15 -07:00
parent 06010bea59
commit c608ad5ccd
Signed by: anton
GPG Key ID: 44A86CFF1FDF0E85
1 changed files with 20 additions and 2 deletions

View File

@ -4,6 +4,7 @@ set -eu
COMMAND=($@)
LOCKFILE=/var/run/netvm.pid
QGA_SOCK_PATH=/var/run/netvm_qga.sock
QMP_SOCK_PATH=/var/run/netvm_qmp.sock
QGA_SOCK_FDS=false
qga_connect() {
@ -107,9 +108,10 @@ function cmd_start(){
-net none \
-cdrom /guest.img \
-boot order=d \
-chardev socket,path=/var/run/netvm_qga.sock,server=on,wait=off,id=qga0 \
-chardev socket,path=${QGA_SOCK_PATH},server=on,wait=off,id=qga0 \
-qmp unix:${QMP_SOCK_PATH},server,nowait \
$net_args \
-device qemu-xhci \
-device qemu-xhci,id=usb \
-device virtio-serial \
-device virtserialport,chardev=qga0,name=org.qemu.guest_agent.0 \
>/dev/null 2>&1 &!
@ -237,6 +239,20 @@ function cmd_run(){
done
}
function cmd_attach() {
local id="${COMMAND[1]}"
IFS=":" read vendorid_hex productid_hex <<< "$id"
vendorid=$(printf "%d\n" "0x$vendorid_hex")
productid=$(printf "%d\n" "0x$productid_hex")
echo $productid
echo $vendorid
(
echo '{"execute":"qmp_capabilities"}'
echo '{"execute":"device_add","arguments":{"driver":"usb-host","bus":"usb.0","vendorid":'$vendorid',"productid":'$productid'}}'
) | socat - UNIX-CONNECT:/var/run/netvm_qmp.sock
}
cmd_usage() {
cat <<-_EOF
netvm
@ -252,6 +268,8 @@ cmd_usage() {
Start interactive network vm in the foreground
netvm status
Get hostname and uptime from running network vm
netvm attach <vendorid:deviceid>
Hot-plug a USB device to network VM
netvm push <local-path> <remote-path>
Push a local file to the network VM
netvm pull <remote-path> <local-path>