Skip to content

Instantly share code, notes, and snippets.

@jsamr
Last active October 12, 2016 18:46
Show Gist options
  • Save jsamr/a84075f7681b30ffcd781481080713d1 to your computer and use it in GitHub Desktop.
Save jsamr/a84075f7681b30ffcd781481080713d1 to your computer and use it in GitHub Desktop.
KVM + SPice Windows VM daemon script
#!/bin/bash
# • Following packages must be installed :
# - qemu
# - samba
# - spice-vdagent
# - virt-viewer
# • SPICE driver needed in guest from https://www.spice-space.org/download/windows/spice-guest-tools/spice-guest-tools-0.100.exe
# • For libvirtd to work for user, add user to libvirtd group and reboot
# > usermod -a -G libvirtd USER
#
cli_set=false
qemu_monitor=false
launch_server=false
op=NONE
script_name='dwin10'
# Parse args
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-h|--help)
echo "Manager for windows 10 virtualization."
echo "Usage: $script_name [OPTION...] COMMAND"
echo -e "OPTIONS"
echo -e "\t -c, --spice-cli (for command start only) launch spice client"
echo -e "\t -m, --qemu-monitor (for command start only) launch qemu monitor"
echo -e "\nCOMMANDS"
echo -e "\t start start the VM"
echo -e "\t stop softly stops the VM"
echo -e "\t status show info"
echo -e "\t cli launch spice client"
echo -e "\t monitor launch qemu monitor through telnet"
exit 0
;;
-c|--spice-cli)
cli_set=true
;;
-m|--qemu-monitor)
qemu_monitor=true
;;
start)
op=START
break
;;
stop)
op=STOP
break
;;
status)
op=STATUS
break
;;
cli)
op=CLI
break
;;
monitor)
op=MONITOR
qemu_monitor=true
break
;;
*)
echo "Unknown option or command $key, exiting."
exit 1
;;
esac
shift # past argument or value
done
# Vars
installImage=$HOME/ISO/Win10_1607_English_x64.iso
systemImage=$HOME/win10.img
maxMemSize="1G"
sambaSharePath=$HOME/Public
spicePort="5900"
telport="1234"
qemuHost="127.0.0.1"
vmname=WINDOWS10
winTitle="windows10"
# Misc
lockfile=/var/tmp/win10-kvm-daemon
# Options
systemDriveOptions=",if=virtio"
spiceOption="-spice port=$spicePort,disable-ticketing \
-device virtio-serial-pci \
-device virtserialport,chardev=spicechannel0,name=com.redhat.spice.0 \
-chardev spicevmc,id=spicechannel0,name=vdagent"
balloonOption="--balloon virtio"
sambaShareOption="-net user,smb=$sambaSharePath"
options="-m $maxMemSize \
$balloonOption \
-mem-prealloc \
-rtc clock=host,base=localtime \
-enable-kvm \
-drive file=$systemImage,cache=writeback,format=raw,index=0$systemDriveOption \
-cpu host \
-smp 4,sockets=1,cores=2,threads=2 \
-usbdevice tablet \
-vga qxl \
-net nic,model=virtio \
-display none \
-machine type=q35,accel=kvm \
-name $vmname,process=$vmname \
-nographic -monitor telnet:$qemuHost:$telport,server,nowait \
$sambaShareOption \
$spiceOption"
function launchSpiceCLI {
echo "Launching client..."
remote-viewer --full-screen --title $winTitle spice://localhost:$spicePort &
}
function launchQemuMonitor {
echo "Launching monitor..."
telnet $qemuHost $telport
}
function launchSpiceServer {
trap 'rm -f "$lockfile"; exit $?' INT TERM EXIT
qemu-system-x86_64 $options > /dev/null 2>&1
# clean up after yourself, and release your trap
rm -f "$lockfile"
trap - INT TERM EXIT
}
function handleStartOp {
if ( set -o noclobber; echo "$$" > "$lockfile") 2> /dev/null ; then
launchSpiceServer &
sleep 1
else
echo "An instance of $vmname is already running, exiting."
exit 1
fi
if [ "$qemu_monitor" = true ] ; then
launchQemuMonitor
else
if [ "$cli_set" = true ] ; then
launchSpiceCLI
fi
fi
}
function handleStopOp {
echo "Handle stop to be implemented"
echo "system_powerdown" | telnet $qemuHost $telport
}
function handleStatusOp {
if [ -f "$lockfile" ] ; then
echo -e "\tVM : started with PID $(pgrep $vmname)"
else
echo -e "\tVM : offline"
fi
}
case $op in
START)
handleStartOp
;;
STOP)
handleStopOp
;;
STATUS)
handleStatusOp
;;
CLI)
launchSpiceCLI
;;
MONITOR)
launchQemuMonitor
;;
*)
echo "No operation arg given amongst 'start', 'stop', 'cli', 'monitor' and 'status', exiting."
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment