Skip to content

Instantly share code, notes, and snippets.

@marcov
Created September 28, 2019 08:31
Show Gist options
  • Save marcov/53ea3e42d47e58a88f95b79d5dddfc61 to your computer and use it in GitHub Desktop.
Save marcov/53ea3e42d47e58a88f95b79d5dddfc61 to your computer and use it in GitHub Desktop.
Generic QEMU on-the-fly VM startup from a disk image
#!/bin/bash
#########################################################
#
# Generic QEMU on-the-fly VM startup from a disk image.
#
# Adapted from: https://github.com/kholia/OSX-KVM
#
#########################################################
set -e
MY_OPTIONS="+aes,+xsave,+avx,+xsaveopt,avx2,+smep"
#Uncomment to enable DVD
#DVD_DRIVE="-drive id=MacDVD,if=none,snapshot=on,media=cdrom,file=./HighSierra.iso -device ide-drive,bus=ide.0,drive=MacDVD "
USE_SPICE="-spice port=5901,addr=127.0.0.1,disable-ticketing,image-compression=off,seamless-migration=on -device qxl-vga,id=video0,ram_size=67108864,vram_size=67108864,vram64_size_mb=0,vgamem_mb=16,max_outputs=1,bus=pcie.0,addr=0x2"
CPU_PENRYN="-cpu Penryn"
CPU_HOST="-cpu host"
CPU="$CPU_HOST,kvm=on,vendor=GenuineIntel,+invtsc,vmware-cpuid-freq=on,$MY_OPTIONS"
declare -a DEFAULT_HDD=( \
-device ide-drive,bus=ide.1,drive=MacHDD \
-drive id=GuestHDD,if=none,file=./guest-hdd.img,format=qcow2 \
)
declare -a STORAGE_OPTS=()
getUSBInfo() {
local deviceName="$1"
[ -z "$deviceName" ] && { echo "Need to specify a USB device name" >&2; exit -1; }
usbInfo=$(lsusb | grep "$deviceName")
[ -z "$usbInfo" ] && { echo "Could not find USB device with name '$deviceName'" >&2; exit -1; }
sed -E "s/Bus 0*([0-9]+) Device 0*([0-9]+):.+$/hostbus=\1,hostaddr=\2/" <<< $usbInfo
}
usage() {
cat << EOF
USAGE:
$(basename $0) OPTIONS COMMAND [COMMAND-ARGUMENTS...]
Options:
-h: Show this help
-k: Always include the default image file with any boot mode
--usb: Add a USB device, as specified by name
Commands:
block: Boot from the block device specified as an argument
E.g.:
$(basename $0) block /dev/sdb
default: Boot with the default mac HDD image file
nohd: Boot without an hard disk
usb2:
usb3: Boot from a USB2 or USB3 drive, using the specified USB
device name, as showed by 'lsusb'
E.g.:
$(basename $0) usb2 Seageate
NOTE: Comment out the "MY_OPTIONS" line in case you are having booting problems!
EOF
}
while [[ "$1" =~ ^- ]]; do
option="$1"
case "$option" in
-h)
usage
exit 0
;;
--usb)
shift
deviceName=$1
EXTRA_ARGS+=(-device usb-host,$(getUSBInfo $deviceName))
;;
-k)
STORAGE_OPTS+=(${DEFAULT_HDD[@]})
;;
*)
echo "Invalid option"
usage
exit 1
;;
esac
shift
done
cmd="$1"
case "$cmd" in
default)
STORAGE_OPTS=(${DEFAULT_HDD[@]})
;;
usb2)
STORAGE_OPTS+=(-device usb-host,$(getUSBInfo $2))
;;
usb3)
STORAGE_OPTS+=( \
-device qemu-xhci,id=xhci \
-device usb-host,bus=xhci.0,$(getUSBInfo $2) \
)
;;
nohd)
STORAGE_OPTS=()
;;
block)
blockDevice="$2"
[[ "$blockDevice" =~ /dev/sd ]] || { echo "Invalid block device specified"; exit -1; }
STORAGE_OPTS+=( \
-device ide-drive,bus=ide.1,drive=rawBlock \
-drive id=rawBlock,file=${blockDevice},format=raw,if=none \
)
;;
*)
if [ -n "$command" ]; then
echo "Invalid command"
else
echo "A command is required"
fi
usage
exit 1
esac
AUDIO_PA="-audiodev pa,id=pa1,server=/run/user/1000/pulse/native"
AUDIO_ALSA="-audiodev alsa,id=alsa1"
AUDIO=" -soundhw hda $AUDIO_PA"
AUDIO=""
( sleep .2; spicy -h localhost -p 5901 2>/dev/null; ) &
qemu-system-x86_64 -enable-kvm -m 8192 $CPU \
-machine pc-q35-2.9 \
-smp 8,cores=4 \
-usb -device usb-kbd -device usb-tablet \
-smbios type=2 \
$DVD_DRIVE \
$USE_SPICE \
$AUDIO \
${STORAGE_OPTS[@]} \
-netdev user,id=net0 -device e1000-82545em,netdev=net0,id=net0,mac=52:54:00:c9:18:27 \
${EXTRA_ARGS[@]} \
-monitor stdio
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment