1
0
Fork 0
mirror of https://github.com/foxlet/macOS-Simple-KVM.git synced 2024-11-27 05:29:42 +01:00
macOS-Simple-KVM/make.sh

94 lines
2.7 KiB
Bash
Raw Normal View History

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
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(){
NAME="macOS"
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
if [[ -e version ]]; then
NAME="$NAME $(cat version)"
fi
UUID=$( cat /proc/sys/kernel/random/uuid )
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
read -p "How much storage? [$DEFAULT_STORAGE]: " STORAGE
## Use default 60G storage if no storage provided
if [[ -z $STORAGE ]]; then STORAGE=$DEFAULT_STORAGE; fi
## TODO do some input validation
echo Creating system disk $BOXES_HOME/macOS.qcow2 of size $STORAGE
if [[ -e $BOXES_HOME/macOS.qcow2 ]]; then
echo "macOS.qcow2 already exist, skipping"
else
qemu-img create -f qcow2 $BOXES_HOME/macOS.qcow2 $STORAGE
fi
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/
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/
cp -Zfu firmware/OVMF_VARS-1024x768.fd $QEMU_HOME/nvram/
2020-05-03 22:05:36 +02:00
echo Copy $OUT to $QEMU_HOME
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
}
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