linuxvm.sh 10 KB

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