Skip to content

Instantly share code, notes, and snippets.

@BETLOG
Last active September 1, 2023 09:04
Show Gist options
  • Save BETLOG/b916be7a2a9cebd6e2862f43059073dd to your computer and use it in GitHub Desktop.
Save BETLOG/b916be7a2a9cebd6e2862f43059073dd to your computer and use it in GitHub Desktop.
learning to use qemu
#!/bin/bash
# betlog - 2023-08-26--17-25-47
#
timeout=15
port=5930
vmImage="/home/user/qemu/kubuntu2304.qcow2"
auxMedia="/home/user/qemu/temporary30GB.img" # raw .img
# ----------------- AUTOMATIC SNAPSHOT SELECTION
test=$(find ${vmImage%/*} -maxdepth 1 -type f|grep -E "_snap[0-9]{2}.${vmImage##*.}"|sort|tail -n1)
[[ -f "$test" ]] && vmImage=$test
vmName=$(basename ${vmImage%.*})
# -----------------
cmd="/usr/bin/qemu-system-x86_64 "
cmd+="-name $vmName "
cmd+="-smp 2 "
cmd+="-cpu host "
cmd+="-M pc-i440fx-lunar "
cmd+="-machine accel=kvm "
cmd+="-m 4096 "
cmd+="-boot c,menu=off "
cmd+="-net nic -net user "
cmd+="-rtc base=localtime "
# ---------
# cmd+="-cdrom /home/user/downloads/ISOs/kubuntu-23.04-desktop-amd64.iso "
cmd+="-hda \"$vmImage\" "
[[ -f $auxMedia ]] && cmd+="-drive file=$auxMedia,format=raw,index=1,media=disk " # workaround 9p and host's fuse sshfs mounts intermittent permission fails in qbittorrent
cmd+="-virtfs local,mount_tag=shared0,security_model=none,path=/home/user/documents/scripts/vpn "
cmd+="-virtfs local,mount_tag=shared1,security_model=none,path=/home/user/video/torrents "
# ---------
cmd+="-monitor stdio -vga qxl -spice port=$port,disable-ticketing=on "
# ---------
cmd+="-device virtio-serial-pci "
cmd+="-chardev qemu-vdagent,id=charchannel0,name=vdagent,clipboard=on "
cmd+="-device virtserialport,chardev=charchannel0,id=channel0,name=com.redhat.spice.0 "
# ---------
# cmd+="-snapshot " # WARNING DISCARD ANY CHANGES ON VM EXIT
cmd+="$* "
# -----------------
echo -ne "$cmd\\n"|sed -E 's| (-)|\n\1|g'
# exit
eval "$cmd" &
while sleep 1s; do
nc -z localhost $port
[[ $? -eq 0 ]] && break
[[ $(( timeout-- )) -le 0 ]] && exit || echo -ne " fail in:$timeout\\r"
done
remote-viewer spice://localhost:$port 2>/dev/null
echo -ne "\\n----done----\\n"
# -----------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment