dotfiles/scripts/qemu-helper
Sonny 8ae037cd30 Qemu script deletion & container script update
- container script checks for sudo & adds optionally adds entry to
users' ssh config
- initialize script checks for sudo priveliges like the container script
- Removal of qemu scripts
2017-07-12 13:25:26 +02:00

85 lines
3 KiB
Bash
Executable file

#!/bin/bash
## Script to create and or edit a qemu script
create_script() {
read -p "Create VM image? [y/n]: " IMAGE
# Image creation/selection
if [ "$IMAGE" == "y" ] ;then
read -e -p "Enter the the path of the image: " DISK
read -p "Enter the size of the image [10G/5G]: " SIZE
qemu-img create -f raw "$DISK" "$SIZE"
elif [ "$IMAGE" == "n" ] ;then
read -e -p "Enter the Disk path and press [ENTER]: " DISK
else
exit 1
fi
# VM configuration
read -p "Enter the VM name and press [ENTER]: " NAME
read -p "Enter the port number and press [ENTER]: " PORT
read -p "Enter qemu script's name and press [ENTER]: " FILE
read -p "Enter the amount of RAM [1G/4G]: " RAM
read -p "Enter the number of cores the guest can use [1/4]: " CORES
read -p "Boot disk needed? [y/n]: " ANSWER
if [ "$ANSWER" == "y" ] ;then
read -e -p "Enter the Boot disk path and press [ENTER]: " BOOT_DISK
else
echo "Okay, dropping boot disk"
fi
# Creation of script
touch "$HOME/.bin/qemu/$FILE"
chmod +x "$HOME/.bin/qemu/$FILE"
if [ "$ANSWER" == "y" ] ;then
echo -e '#!/bin/bash \n' \
'qemu-system-x86_64 -enable-kvm --daemonize \' '\n' \
'\t -cpu host \' '\n' \
'\t -smp '$CORES' \' '\n' \
'\t -drive format=raw,file='$DISK',if=virtio \' '\n' \
'\t -boot d -cdrom '$BOOT_DISK' \' '\n' \
'\t -m '$RAM' \' '\n' \
'\t -name '$NAME' \' '\n' \
'\t -vga qxl \' '\n' \
'\t -spice port='$PORT',disable-ticketing \' '\n' \
'\t -device virtio-serial \' '\n' \
'\t -chardev spicevmc,id=vdagent,name=vdagent \' '\n' \
'\t -device virtserialport,chardev=vdagent,name=com.redhat.spice.0' \
> $HOME/.bin/qemu/$FILE
# Remove leading whitespace
sed -i 's/^ *//g' $HOME/.bin/qemu/$FILE
elif [ "$ANSWER" == "n" ] ;then
echo -e '#!/bin/bash \n' \
'qemu-system-x86_64 -enable-kvm --daemonize \' '\n' \
'\t -cpu host \' '\n' \
'\t -smp '$CORES' \' '\n' \
'\t -drive format=raw,file='$DISK',if=virtio \' '\n' \
'\t -m '$RAM' \' '\n' \
'\t -name '$NAME' \' '\n' \
'\t -vga qxl \' '\n' \
'\t -spice port='$PORT',disable-ticketing \' '\n' \
'\t -device virtio-serial \' '\n' \
'\t -chardev spicevmc,id=vdagent,name=vdagent \' '\n' \
'\t -device virtserialport,chardev=vdagent,name=com.redhat.spice.0' \
> $HOME/.bin/qemu/$FILE
sed -i 's/^ *//g' $HOME/.bin/qemu/$FILE
fi
# Remove trailing whitespaces
sed -i 's/\s*$//g' $HOME/.bin/qemu/$FILE
}
# Remove GUI options for headless VM
remove_gui() {
sed -i "/-vga\|-spice\|virtio-serial\|-chardev\|exec/d" "$FILENAME"
}
read -p "Do you want to create a qemu script or remove the gui from a script? [create/remove]: " FUNCTION
if [ "$FUNCTION" == "create" ] ;then
create_script
elif [ "$FUNCTION" == "remove" ] ;then
read -e -p "Enter script's file path and press [Enter]: " FILENAME
remove_gui
fi