linuxvm.sh 11 KB

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