2019-05-06 11:40:00 +02:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
# make.sh: Generate customized libvirt XML.
|
|
|
|
# by Foxlet <foxlet@furcode.co>
|
|
|
|
|
|
|
|
VMDIR=$PWD
|
2020-05-03 18:40:53 +02:00
|
|
|
QEMU_HOME="$HOME/.config/libvirt/qemu"
|
|
|
|
BOXES_HOME="$HOME/.local/share/gnome-boxes/images"
|
2019-07-19 21:44:21 +02:00
|
|
|
MACHINE="$(qemu-system-x86_64 --machine help | grep q35 | cut -d" " -f1 | grep -Eoe ".*-[0-9.]+" | sort -rV | head -1)"
|
2019-05-06 11:40:00 +02:00
|
|
|
OUT="template.xml"
|
2020-05-03 22:05:36 +02:00
|
|
|
DOMAIN_NAME=macOS-Simple-KVM
|
2020-05-03 21:32:25 +02:00
|
|
|
DEFAULT_STORAGE=60G
|
|
|
|
DEFAULT_MEMORY=2
|
|
|
|
MEMORY_MULTIPLIER=1048576
|
2019-05-06 11:40:00 +02:00
|
|
|
|
|
|
|
print_usage() {
|
2019-06-07 01:36:52 +02:00
|
|
|
echo
|
|
|
|
echo "Usage: $0"
|
|
|
|
echo
|
2020-05-03 18:40:53 +02:00
|
|
|
echo " -a, --add Add XML to virsh (uses sudo)."
|
|
|
|
echo
|
|
|
|
echo " -i, --install Install virtual machine in GNOME Boxes."
|
2019-06-07 01:36:52 +02:00
|
|
|
echo
|
2019-05-06 11:40:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
error() {
|
2019-07-19 21:44:21 +02:00
|
|
|
local error_message="$*"
|
2019-06-07 01:36:52 +02:00
|
|
|
echo "${error_message}" 1>&2;
|
2019-05-06 11:40:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
generate(){
|
2020-05-03 19:08:20 +02:00
|
|
|
NAME="macOS"
|
2020-05-03 21:32:25 +02:00
|
|
|
|
|
|
|
read -p "How much RAM? [$DEFAULT_MEMORY]: " MEMORY
|
|
|
|
## Use default 2GB memory if no memory provided
|
|
|
|
if [[ -n $MEMORY ]]; then MEMORY=$(($MEMORY_MULTIPLIER*$MEMORY)); else MEMORY=$(($MEMORY_MULTIPLIER*$DEFAULT_MEMORY)); fi
|
|
|
|
|
|
|
|
## TODO do some input validation
|
|
|
|
|
2020-05-03 23:03:27 +02:00
|
|
|
if [[ -e version ]]; then
|
|
|
|
NAME="$NAME $(cat version)"
|
2020-05-03 19:08:20 +02:00
|
|
|
fi
|
2020-05-03 17:47:02 +02:00
|
|
|
UUID=$( cat /proc/sys/kernel/random/uuid )
|
2020-05-03 23:03:27 +02:00
|
|
|
sed -e "s|BOXESHOME|$BOXES_HOME|g" -e "s|MACOSNAME|$NAME|g" -e "s|BOXESHOME|$BOXES_HOME|g" -e "s|QEMUHOME|$QEMU_HOME|g" -e "s|UUID|$UUID|g" -e "s|MACHINE|$MACHINE|g" -e "s|MACHINE|$MACHINE|g" tools/template.xml.in > $OUT
|
|
|
|
echo "$OUT has been generated in $VMDIR"
|
2019-05-06 11:40:00 +02:00
|
|
|
}
|
|
|
|
|
2020-05-03 18:40:53 +02:00
|
|
|
install(){
|
|
|
|
echo Creating direcories $QEMU_HOME/firmware and $BOXES_HOME
|
|
|
|
mkdir -p $BOXES_HOME
|
|
|
|
mkdir -p $QEMU_HOME/firmware
|
2020-05-03 21:32:25 +02:00
|
|
|
|
|
|
|
read -p "How much storage? [$DEFAULT_STORAGE]: " STORAGE
|
2020-05-03 23:03:27 +02:00
|
|
|
## Use default 60G storage if no storage provided
|
2020-05-03 21:32:25 +02:00
|
|
|
if [[ -z $STORAGE ]]; then STORAGE=$DEFAULT_STORAGE; fi
|
|
|
|
|
|
|
|
## TODO do some input validation
|
|
|
|
|
|
|
|
echo Creating system disk $BOXES_HOME/macOS.qcow2 of size $STORAGE
|
2020-05-03 23:03:27 +02:00
|
|
|
qemu-img create -f qcow2 $BOXES_HOME/macOS.qcow2 $STORAGE
|
2020-05-03 18:40:53 +02:00
|
|
|
echo Coping BaseSystem.img and ESP.qcow2 in $BOXES_HOME
|
|
|
|
cp -Zfu BaseSystem.img $BOXES_HOME
|
|
|
|
cp -Zfu ESP.qcow2 $BOXES_HOME
|
|
|
|
echo Coping OVMF_CODE.fd in $QEMU_HOME/firmware/
|
2020-05-03 19:08:20 +02:00
|
|
|
cp -Zfu firmware/OVMF_CODE.fd $QEMU_HOME/firmware/
|
2020-05-03 18:40:53 +02:00
|
|
|
echo Coping OVMF_CODE.fd in $QEMU_HOME/nvram/
|
2020-05-03 19:08:20 +02:00
|
|
|
cp -Zfu firmware/OVMF_VARS-1024x768.fd $QEMU_HOME/nvram/
|
2020-05-03 22:05:36 +02:00
|
|
|
echo Copy $OUT to $QEMU_HOME
|
2020-05-03 23:03:27 +02:00
|
|
|
cp -Zfu $OUT $QEMU_HOME/$DOMAIN_NAME.xml
|
2020-05-03 22:05:36 +02:00
|
|
|
virsh -c qemu:///session define $QEMU_HOME/$DOMAIN_NAME.xml
|
2020-05-03 18:40:53 +02:00
|
|
|
}
|
|
|
|
|
2020-05-03 23:03:27 +02:00
|
|
|
if [[ ! -e BaseSystem.img ]]; then
|
|
|
|
echo "Can't find base image, please run ./jumpstart.sh to download it."
|
|
|
|
echo
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2019-05-06 11:40:00 +02:00
|
|
|
generate
|
|
|
|
|
|
|
|
argument="$1"
|
|
|
|
case $argument in
|
2020-05-03 18:40:53 +02:00
|
|
|
-i|--install)
|
|
|
|
install
|
2019-05-06 11:40:00 +02:00
|
|
|
;;
|
|
|
|
-h|--help)
|
|
|
|
print_usage
|
|
|
|
;;
|
|
|
|
esac
|