linuxvm.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 and Leap 16)
  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, opensuse-leap-16), 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. # golden <vm> freezes a stopped VM's disk as golden.qcow2 (and its UEFI vars) and puts a throwaway overlay on top
  21. # fresh <vm> throws the overlay away: the next start begins from the golden state again (release_check.sh)
  22. # a file <vm>/qemu-extra holds extra QEMU arguments (SteamOS: NVMe disk, Valve SMBIOS, the recovery image as USB disk)
  23. # stop | kill <vm> ACPI power off | kill QEMU
  24. # QEMU's virtio-gpu and egl-headless modules are not in the image's qemu-system-x86-core: `fetch` unpacks the Fedora
  25. # RPMs (plus virglrenderer) into $LINUXVM/root and QEMU_MODULE_DIR points there.
  26. LINUXVM=${LINUXVM:-$HOME/.local/share/linuxvm}
  27. HERE=$(cd "$(dirname "$0")" && pwd)
  28. QMP="python3 $HERE/../wecompare/winvm/qmp.py"
  29. KEY=$LINUXVM/id_ed25519
  30. ulimit -c 0
  31. vmdir () { echo "$LINUXVM/vms/$1"; }
  32. port () { cat "$(vmdir "$1")/port"; }
  33. ssh_vm () {
  34. local vm=$1
  35. shift
  36. ssh -i "$KEY" -p "$(port "$vm")" -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=ERROR \
  37. -o ConnectTimeout=5 lab@127.0.0.1 "$@"
  38. }
  39. case $1 in
  40. fetch)
  41. mkdir -p "$LINUXVM/images" "$LINUXVM/rpms" "$LINUXVM/root"
  42. cd "$LINUXVM/images" || exit 1
  43. [ -f fedora-44.qcow2 ] || curl -fLo fedora-44.qcow2 \
  44. https://download.fedoraproject.org/pub/fedora/linux/releases/44/Cloud/x86_64/images/Fedora-Cloud-Base-Generic-44-1.7.x86_64.qcow2
  45. [ -f arch.qcow2 ] || curl -fLo arch.qcow2 https://geo.mirror.pkgbuild.com/images/latest/Arch-Linux-x86_64-cloudimg.qcow2
  46. [ -f ubuntu-24.04.qcow2 ] || curl -fLo ubuntu-24.04.qcow2 \
  47. https://cloud-images.ubuntu.com/releases/noble/release/ubuntu-24.04-server-cloudimg-amd64.img
  48. [ -f debian-13.qcow2 ] || curl -fLo debian-13.qcow2 \
  49. https://cloud.debian.org/images/cloud/trixie/latest/debian-13-genericcloud-amd64.qcow2
  50. [ -f opensuse-tumbleweed.qcow2 ] || curl -fLo opensuse-tumbleweed.qcow2 \
  51. https://download.opensuse.org/tumbleweed/appliances/openSUSE-Tumbleweed-Minimal-VM.x86_64-Cloud.qcow2
  52. [ -f opensuse-leap-16.qcow2 ] || curl -fLo opensuse-leap-16.qcow2 \
  53. https://download.opensuse.org/distribution/leap/16.0/appliances/Leap-16.0-Minimal-VM.x86_64-Cloud.qcow2
  54. [ -f linuxmint-22.3-cinnamon.iso ] || curl -fLo linuxmint-22.3-cinnamon.iso \
  55. https://mirrors.kernel.org/linuxmint/stable/22.3/linuxmint-22.3-cinnamon-64bit.iso
  56. [ -f manjaro-kde.iso ] || curl -fLo manjaro-kde.iso \
  57. https://download.manjaro.org/kde/26.1.0/manjaro-kde-26.1.0-260812-linux71.iso
  58. if [ ! -f pop-os.iso ]; then
  59. pop=$(curl -fsSL https://api.pop-os.org/builds/24.04/generic)
  60. curl -fLo pop-os.iso "$(echo "$pop" | python3 -c 'import json,sys; print(json.load(sys.stdin)["url"])')"
  61. [ "$(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"
  62. fi
  63. [ -f bazzite.iso ] || curl -fLo bazzite.iso https://download.bazzite.gg/bazzite-stable-amd64.iso
  64. cd "$LINUXVM/rpms" && dnf download --arch x86_64 qemu-device-display-virtio-gpu qemu-device-display-virtio-gpu-gl \
  65. qemu-device-display-virtio-gpu-pci qemu-device-display-virtio-gpu-pci-gl qemu-device-display-virtio-vga \
  66. qemu-device-display-virtio-vga-gl qemu-ui-egl-headless qemu-ui-opengl virglrenderer qemu-ui-gtk qemu-audio-pipewire
  67. cd "$LINUXVM/root" && for rpm in "$LINUXVM"/rpms/*.rpm; do rpm2cpio "$rpm" | cpio -idm 2>/dev/null; done
  68. [ -f "$KEY" ] || ssh-keygen -q -t ed25519 -N '' -f "$KEY"
  69. ;;
  70. create)
  71. vm=$2 image=$LINUXVM/images/$3.qcow2 dir=$(vmdir "$2")
  72. [ -f "$image" ] || { echo "no $image, run fetch"; exit 1; }
  73. [ -e "$dir" ] && { echo "$dir exists"; exit 1; }
  74. mkdir -p "$dir/seed"
  75. qemu-img create -q -f qcow2 -F qcow2 -b "$image" "$dir/disk.qcow2" 60G
  76. used=$(cat "$LINUXVM"/vms/*/port 2>/dev/null)
  77. p=2250
  78. while echo "$used" | grep -qx $p; do p=$((p + 1)); done
  79. echo $p > "$dir/port"
  80. admin=wheel
  81. case $3 in ubuntu* | debian*) admin=sudo ;; esac
  82. cat > "$dir/seed/meta-data" <<EOF
  83. instance-id: $vm
  84. local-hostname: $vm
  85. EOF
  86. cat > "$dir/seed/user-data" <<EOF
  87. #cloud-config
  88. users:
  89. - name: lab
  90. uid: "1000"
  91. groups: [$admin, video, render, input]
  92. shell: /bin/bash
  93. sudo: ALL=(ALL) NOPASSWD:ALL
  94. lock_passwd: false
  95. plain_text_passwd: lab
  96. ssh_authorized_keys:
  97. - $(cat "$KEY.pub")
  98. growpart:
  99. mode: auto
  100. devices: ['/']
  101. EOF
  102. xorriso -as mkisofs -quiet -J -r -V cidata -o "$dir/seed.iso" "$dir/seed"
  103. cp /usr/share/edk2/ovmf/OVMF_VARS.fd "$dir/vars.fd"
  104. echo "created $vm (ssh port $p)"
  105. ;;
  106. create-iso)
  107. vm=$2 iso=$LINUXVM/images/$3.iso dir=$(vmdir "$2")
  108. [ -f "$iso" ] || { echo "no $iso"; exit 1; }
  109. [ -e "$dir" ] && { echo "$dir exists"; exit 1; }
  110. mkdir -p "$dir"
  111. qemu-img create -q -f qcow2 "$dir/disk.qcow2" 60G
  112. ln -s "$iso" "$dir/boot.iso"
  113. used=$(cat "$LINUXVM"/vms/*/port 2>/dev/null)
  114. p=2250
  115. while echo "$used" | grep -qx $p; do p=$((p + 1)); done
  116. echo $p > "$dir/port"
  117. cp /usr/share/edk2/ovmf/OVMF_VARS.fd "$dir/vars.fd"
  118. echo "created $vm (ssh port $p)"
  119. ;;
  120. start)
  121. vm=$2 dir=$(vmdir "$2")
  122. [ -f "$dir/qemu.pid" ] && kill -0 "$(cat "$dir/qemu.pid")" 2>/dev/null && { echo running; exit 0; }
  123. size=${LINUXVM_SIZE:-1920x1080}
  124. display=(-display egl-headless,rendernode="${LINUXVM_RENDERNODE:-/dev/dri/renderD128}")
  125. extra=(-vnc unix:"$dir/vnc.sock")
  126. if [ "${LINUXVM_DISPLAY:-}" = window ]; then
  127. display=(-display gtk,gl=on,zoom-to-fit=off,show-menubar=off)
  128. extra=(-audiodev pipewire,id=snd0 -device intel-hda -device hda-duplex,audiodev=snd0
  129. -device virtio-serial-pci -chardev qemu-vdagent,id=vdagent,clipboard=on,mouse=off
  130. -device virtserialport,chardev=vdagent,name=com.redhat.spice.0)
  131. fi
  132. QEMU_MODULE_DIR=$LINUXVM/root/usr/lib64/qemu LD_LIBRARY_PATH=$LINUXVM/root/usr/lib64 \
  133. qemu-system-x86_64 -name "$vm" -machine q35 -accel kvm -cpu host -smp "${LINUXVM_CPUS:-8}" -m "${LINUXVM_MEM:-8G}" \
  134. -drive if=pflash,format=raw,readonly=on,file=/usr/share/edk2/ovmf/OVMF_CODE.fd \
  135. -drive if=pflash,format=raw,file="$dir/vars.fd" \
  136. -drive file="$dir/disk.qcow2",if=virtio,format=qcow2,cache=unsafe,discard=unmap \
  137. $([ -f "$dir/seed.iso" ] && echo -drive file="$dir/seed.iso",media=cdrom,if=none,id=seed,readonly=on -device ide-cd,drive=seed) \
  138. $([ -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) \
  139. -nic user,model=virtio-net-pci,hostfwd=tcp:127.0.0.1:"$(port "$vm")"-:22 \
  140. -device virtio-vga-gl,xres="${size%x*}",yres="${size#*x}" \
  141. "${display[@]}" "${extra[@]}" \
  142. -device qemu-xhci -device usb-tablet \
  143. $([ -f "$dir/qemu-extra" ] && cat "$dir/qemu-extra") \
  144. -qmp unix:"$dir/qmp.sock",server=on,wait=off -serial file:"$dir/serial.log" \
  145. -daemonize -pidfile "$dir/qemu.pid"
  146. [ -f "$dir/seed.iso" ] || { echo "$vm started (no cloud-init, no ssh wait)"; exit 0; }
  147. for _ in $(seq 120); do ssh_vm "$vm" true 2>/dev/null && { echo "$vm is up"; exit 0; }; sleep 2; done
  148. echo "$vm: no ssh after 4 minutes, see $dir/serial.log"
  149. exit 1
  150. ;;
  151. desktop)
  152. LINUXVM_DISPLAY=window exec "$0" start "$2"
  153. ;;
  154. ssh)
  155. vm=$2
  156. shift 2
  157. ssh_vm "$vm" "$@"
  158. ;;
  159. scp)
  160. scp -i "$KEY" -P "$(port "$2")" -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=ERROR \
  161. -r "$3" "lab@127.0.0.1:$4"
  162. ;;
  163. golden)
  164. dir=$(vmdir "$2")
  165. [ -f "$dir/qemu.pid" ] && kill -0 "$(cat "$dir/qemu.pid")" 2>/dev/null && { echo "$2 is running, stop it first"; exit 1; }
  166. [ -f "$dir/golden.qcow2" ] && { echo "$2 has a golden disk already (delete $dir/golden.qcow2 to redo)"; exit 1; }
  167. mv "$dir/disk.qcow2" "$dir/golden.qcow2" && cp "$dir/vars.fd" "$dir/golden-vars.fd" \
  168. && qemu-img create -q -f qcow2 -F qcow2 -b "$dir/golden.qcow2" "$dir/disk.qcow2" \
  169. && echo "$2: golden disk saved, the VM now runs on an overlay"
  170. ;;
  171. fresh)
  172. dir=$(vmdir "$2")
  173. [ -f "$dir/golden.qcow2" ] || { echo "$2 has no golden disk"; exit 1; }
  174. [ -f "$dir/qemu.pid" ] && kill -0 "$(cat "$dir/qemu.pid")" 2>/dev/null && { echo "$2 is running, stop it first"; exit 1; }
  175. rm -f "$dir/disk.qcow2" && qemu-img create -q -f qcow2 -F qcow2 -b "$dir/golden.qcow2" "$dir/disk.qcow2" \
  176. && cp "$dir/golden-vars.fd" "$dir/vars.fd" && echo "$2: back to the golden state"
  177. ;;
  178. stop)
  179. $QMP "$(vmdir "$2")/qmp.sock" raw '{"execute":"system_powerdown"}'
  180. ;;
  181. kill)
  182. dir=$(vmdir "$2")
  183. [ -f "$dir/qemu.pid" ] && kill "$(cat "$dir/qemu.pid")"
  184. ;;
  185. shot)
  186. python3 "$HERE/vncshot.py" "$(vmdir "$2")/vnc.sock" "$3"
  187. ;;
  188. pointer | click | keys | type | raw)
  189. op=$1 vm=$2
  190. shift 2
  191. $QMP "$(vmdir "$vm")/qmp.sock" "$op" "$@"
  192. ;;
  193. *)
  194. sed -n '2,24p' "$0"
  195. exit 1
  196. ;;
  197. esac