linuxvm.sh 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #!/bin/bash
  2. # linuxvm.sh <command> <vm> - Linux desktop VMs (KVM) for what the container can't run: Flatpak (bwrap needs user
  3. # namespaces), a real KMS session of a desktop. A VM is a cloud image overlay set up by cloud-init with user "lab"
  4. # (uid 1000, password "lab", passwordless sudo), reached over ssh on a localhost port. The guest GPU is virtio-vga-gl
  5. # with QEMU's egl-headless display on the container's render node (virgl), so nothing ever shows on the desktop's
  6. # display; screenshots and pointer input go through QMP. No audio device unless started with `desktop`.
  7. # fetch downloads the cloud images (Fedora 44, Arch, Ubuntu 24.04, Debian 13, openSUSE Tumbleweed)
  8. # and the installer ISOs (Linux Mint Cinnamon, Manjaro KDE, Pop!_OS, Bazzite)
  9. # create <vm> <image> new VM from images/<image>.qcow2 (fedora-44, arch, ubuntu-24.04, debian-13,
  10. # opensuse-tumbleweed), 60G overlay
  11. # create-iso <vm> <iso> blank 60G disk that boots images/<iso>.iso (a distro installer, e.g. linuxmint-22.3-cinnamon);
  12. # remove <vm>/boot.iso after installing. Such a VM has no cloud-init, ssh needs setting up by hand
  13. # start <vm> boots it (LINUXVM_CPUS, LINUXVM_MEM, LINUXVM_SIZE=WxH for the display)
  14. # desktop <vm> boots it in a window on the container's display (GTK, GL, host PipeWire audio, shared
  15. # clipboard with spice-vdagent in the guest) to use it like a normal desktop; same as
  16. # LINUXVM_DISPLAY=window start. Stop a headless run of the VM first
  17. # ssh <vm> [cmd] runs a command as lab (or a shell)
  18. # scp <vm> <src> <dst> copies into the VM (dst relative to /home/lab)
  19. # shot <vm> <png> screenshot through the VNC socket (vncshot.py); pointer/click/keys/type/raw go to qmp.py
  20. # stop | kill <vm> ACPI power off | kill QEMU
  21. # QEMU's virtio-gpu and egl-headless modules are not in the image's qemu-system-x86-core: `fetch` unpacks the Fedora
  22. # RPMs (plus virglrenderer) into $LINUXVM/root and QEMU_MODULE_DIR points there.
  23. LINUXVM=${LINUXVM:-$HOME/.local/share/linuxvm}
  24. HERE=$(cd "$(dirname "$0")" && pwd)
  25. QMP="python3 $HERE/../wecompare/winvm/qmp.py"
  26. KEY=$LINUXVM/id_ed25519
  27. ulimit -c 0
  28. vmdir () { echo "$LINUXVM/vms/$1"; }
  29. port () { cat "$(vmdir "$1")/port"; }
  30. ssh_vm () {
  31. local vm=$1
  32. shift
  33. ssh -i "$KEY" -p "$(port "$vm")" -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=ERROR \
  34. -o ConnectTimeout=5 lab@127.0.0.1 "$@"
  35. }
  36. case $1 in
  37. fetch)
  38. mkdir -p "$LINUXVM/images" "$LINUXVM/rpms" "$LINUXVM/root"
  39. cd "$LINUXVM/images" || exit 1
  40. [ -f fedora-44.qcow2 ] || curl -fLo fedora-44.qcow2 \
  41. https://download.fedoraproject.org/pub/fedora/linux/releases/44/Cloud/x86_64/images/Fedora-Cloud-Base-Generic-44-1.7.x86_64.qcow2
  42. [ -f arch.qcow2 ] || curl -fLo arch.qcow2 https://geo.mirror.pkgbuild.com/images/latest/Arch-Linux-x86_64-cloudimg.qcow2
  43. [ -f ubuntu-24.04.qcow2 ] || curl -fLo ubuntu-24.04.qcow2 \
  44. https://cloud-images.ubuntu.com/releases/noble/release/ubuntu-24.04-server-cloudimg-amd64.img
  45. [ -f debian-13.qcow2 ] || curl -fLo debian-13.qcow2 \
  46. https://cloud.debian.org/images/cloud/trixie/latest/debian-13-genericcloud-amd64.qcow2
  47. [ -f opensuse-tumbleweed.qcow2 ] || curl -fLo opensuse-tumbleweed.qcow2 \
  48. https://download.opensuse.org/tumbleweed/appliances/openSUSE-Tumbleweed-Minimal-VM.x86_64-Cloud.qcow2
  49. [ -f linuxmint-22.3-cinnamon.iso ] || curl -fLo linuxmint-22.3-cinnamon.iso \
  50. https://mirrors.kernel.org/linuxmint/stable/22.3/linuxmint-22.3-cinnamon-64bit.iso
  51. [ -f manjaro-kde.iso ] || curl -fLo manjaro-kde.iso \
  52. https://download.manjaro.org/kde/26.1.0/manjaro-kde-26.1.0-260812-linux71.iso
  53. if [ ! -f pop-os.iso ]; then
  54. pop=$(curl -fsSL https://api.pop-os.org/builds/24.04/generic)
  55. curl -fLo pop-os.iso "$(echo "$pop" | python3 -c 'import json,sys; print(json.load(sys.stdin)["url"])')"
  56. [ "$(sha256sum pop-os.iso | cut -d' ' -f1)" = "$(echo "$pop" | python3 -c 'import json,sys; print(json.load(sys.stdin)["sha_sum"])')" ] || echo "pop-os.iso: sha256 mismatch"
  57. fi
  58. [ -f bazzite.iso ] || curl -fLo bazzite.iso https://download.bazzite.gg/bazzite-stable-amd64.iso
  59. cd "$LINUXVM/rpms" && dnf download --arch x86_64 qemu-device-display-virtio-gpu qemu-device-display-virtio-gpu-gl \
  60. qemu-device-display-virtio-gpu-pci qemu-device-display-virtio-gpu-pci-gl qemu-device-display-virtio-vga \
  61. qemu-device-display-virtio-vga-gl qemu-ui-egl-headless qemu-ui-opengl virglrenderer qemu-ui-gtk qemu-audio-pipewire
  62. cd "$LINUXVM/root" && for rpm in "$LINUXVM"/rpms/*.rpm; do rpm2cpio "$rpm" | cpio -idm 2>/dev/null; done
  63. [ -f "$KEY" ] || ssh-keygen -q -t ed25519 -N '' -f "$KEY"
  64. ;;
  65. create)
  66. vm=$2 image=$LINUXVM/images/$3.qcow2 dir=$(vmdir "$2")
  67. [ -f "$image" ] || { echo "no $image, run fetch"; exit 1; }
  68. [ -e "$dir" ] && { echo "$dir exists"; exit 1; }
  69. mkdir -p "$dir/seed"
  70. qemu-img create -q -f qcow2 -F qcow2 -b "$image" "$dir/disk.qcow2" 60G
  71. used=$(cat "$LINUXVM"/vms/*/port 2>/dev/null)
  72. p=2250
  73. while echo "$used" | grep -qx $p; do p=$((p + 1)); done
  74. echo $p > "$dir/port"
  75. admin=wheel
  76. case $3 in ubuntu* | debian*) admin=sudo ;; esac
  77. cat > "$dir/seed/meta-data" <<EOF
  78. instance-id: $vm
  79. local-hostname: $vm
  80. EOF
  81. cat > "$dir/seed/user-data" <<EOF
  82. #cloud-config
  83. users:
  84. - name: lab
  85. uid: "1000"
  86. groups: [$admin, video, render, input]
  87. shell: /bin/bash
  88. sudo: ALL=(ALL) NOPASSWD:ALL
  89. lock_passwd: false
  90. plain_text_passwd: lab
  91. ssh_authorized_keys:
  92. - $(cat "$KEY.pub")
  93. growpart:
  94. mode: auto
  95. devices: ['/']
  96. EOF
  97. xorriso -as mkisofs -quiet -J -r -V cidata -o "$dir/seed.iso" "$dir/seed"
  98. cp /usr/share/edk2/ovmf/OVMF_VARS.fd "$dir/vars.fd"
  99. echo "created $vm (ssh port $p)"
  100. ;;
  101. create-iso)
  102. vm=$2 iso=$LINUXVM/images/$3.iso dir=$(vmdir "$2")
  103. [ -f "$iso" ] || { echo "no $iso"; exit 1; }
  104. [ -e "$dir" ] && { echo "$dir exists"; exit 1; }
  105. mkdir -p "$dir"
  106. qemu-img create -q -f qcow2 "$dir/disk.qcow2" 60G
  107. ln -s "$iso" "$dir/boot.iso"
  108. used=$(cat "$LINUXVM"/vms/*/port 2>/dev/null)
  109. p=2250
  110. while echo "$used" | grep -qx $p; do p=$((p + 1)); done
  111. echo $p > "$dir/port"
  112. cp /usr/share/edk2/ovmf/OVMF_VARS.fd "$dir/vars.fd"
  113. echo "created $vm (ssh port $p)"
  114. ;;
  115. start)
  116. vm=$2 dir=$(vmdir "$2")
  117. [ -f "$dir/qemu.pid" ] && kill -0 "$(cat "$dir/qemu.pid")" 2>/dev/null && { echo running; exit 0; }
  118. size=${LINUXVM_SIZE:-1920x1080}
  119. display=(-display egl-headless,rendernode="${LINUXVM_RENDERNODE:-/dev/dri/renderD128}")
  120. extra=(-vnc unix:"$dir/vnc.sock")
  121. if [ "${LINUXVM_DISPLAY:-}" = window ]; then
  122. display=(-display gtk,gl=on,zoom-to-fit=off,show-menubar=off)
  123. extra=(-audiodev pipewire,id=snd0 -device intel-hda -device hda-duplex,audiodev=snd0
  124. -device virtio-serial-pci -chardev qemu-vdagent,id=vdagent,clipboard=on,mouse=off
  125. -device virtserialport,chardev=vdagent,name=com.redhat.spice.0)
  126. fi
  127. QEMU_MODULE_DIR=$LINUXVM/root/usr/lib64/qemu LD_LIBRARY_PATH=$LINUXVM/root/usr/lib64 \
  128. qemu-system-x86_64 -name "$vm" -machine q35 -accel kvm -cpu host -smp "${LINUXVM_CPUS:-8}" -m "${LINUXVM_MEM:-8G}" \
  129. -drive if=pflash,format=raw,readonly=on,file=/usr/share/edk2/ovmf/OVMF_CODE.fd \
  130. -drive if=pflash,format=raw,file="$dir/vars.fd" \
  131. -drive file="$dir/disk.qcow2",if=virtio,format=qcow2,cache=unsafe,discard=unmap \
  132. $([ -f "$dir/seed.iso" ] && echo -drive file="$dir/seed.iso",media=cdrom,if=none,id=seed,readonly=on -device ide-cd,drive=seed) \
  133. $([ -e "$dir/boot.iso" ] && echo -drive file="$dir/boot.iso",media=cdrom,if=none,id=boot,readonly=on -device ide-cd,drive=boot,bootindex=1) \
  134. -nic user,model=virtio-net-pci,hostfwd=tcp:127.0.0.1:"$(port "$vm")"-:22 \
  135. -device virtio-vga-gl,xres="${size%x*}",yres="${size#*x}" \
  136. "${display[@]}" "${extra[@]}" \
  137. -device qemu-xhci -device usb-tablet \
  138. -qmp unix:"$dir/qmp.sock",server=on,wait=off -serial file:"$dir/serial.log" \
  139. -daemonize -pidfile "$dir/qemu.pid"
  140. [ -f "$dir/seed.iso" ] || { echo "$vm started (no cloud-init, no ssh wait)"; exit 0; }
  141. for _ in $(seq 120); do ssh_vm "$vm" true 2>/dev/null && { echo "$vm is up"; exit 0; }; sleep 2; done
  142. echo "$vm: no ssh after 4 minutes, see $dir/serial.log"
  143. exit 1
  144. ;;
  145. desktop)
  146. LINUXVM_DISPLAY=window exec "$0" start "$2"
  147. ;;
  148. ssh)
  149. vm=$2
  150. shift 2
  151. ssh_vm "$vm" "$@"
  152. ;;
  153. scp)
  154. scp -i "$KEY" -P "$(port "$2")" -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=ERROR \
  155. -r "$3" "lab@127.0.0.1:$4"
  156. ;;
  157. stop)
  158. $QMP "$(vmdir "$2")/qmp.sock" raw '{"execute":"system_powerdown"}'
  159. ;;
  160. kill)
  161. dir=$(vmdir "$2")
  162. [ -f "$dir/qemu.pid" ] && kill "$(cat "$dir/qemu.pid")"
  163. ;;
  164. shot)
  165. python3 "$HERE/vncshot.py" "$(vmdir "$2")/vnc.sock" "$3"
  166. ;;
  167. pointer | click | keys | type | raw)
  168. op=$1 vm=$2
  169. shift 2
  170. $QMP "$(vmdir "$vm")/qmp.sock" "$op" "$@"
  171. ;;
  172. *)
  173. sed -n '2,24p' "$0"
  174. exit 1
  175. ;;
  176. esac