dotfiles/scripts/qemu/qemu-helper
2017-06-17 18:35:23 +02:00

94 lines
2.4 KiB
Bash
Executable file

#!/bin/bash
## Script to create and or edit a qemu script
function create_script {
echo -n "Create VM image? [y/n]: "
read IMAGE
## Optional image creation
if [ "$IMAGE" == "y" ] ;then
echo -n "Enter the the path of the image: "
read IMAGE_NAME
DISK=$IMAGE_NAME
echo -n "Enter the size of the image [10G/5G]: "
read SIZE
qemu-img create -f raw "$IMAGE_NAME" "$SIZE"
elif [ -z "$IMAGE" ] ;then
echo -n "Enter the Disk path and press [ENTER]: "
read DISK
fi
echo -n "Boot disk needed? [y/n]: "
read ANSWER
if [ "$ANSWER" == "y" ] ;then
echo -n "Enter the Boot disk path and press [ENTER]: "
read BOOT_DISK
else
echo "Okay, dropping boot disk"
fi
echo -n "Enter the VM name and press [ENTER]: "
read NAME
echo -n "Enter the port number and press [ENTER]: "
read PORT
echo -n "Enter qemu script's name and press [ENTER]: "
read FILE
# Creation of script
touch "$HOME/.bin/qemu/$FILE"
chmod +x "$HOME/.bin/qemu/$FILE"
if [ "$ANSWER" == "y" ] ;then
cat <<EOF > "$HOME/.bin/qemu/$FILE"
#!/bin/bash
qemu-system-x86_64 -enable-kvm --daemonize '\'
-cpu host '\'
-drive format=raw,file="$DISK",if=virtio '\'
-boot d -cdrom "$BOOT_DISK" '\'
-m 4G '\'
-name "$NAME" '\'
-vga qxl '\'
-spice port="$PORT",disable-ticketing '\'
-device virtio-serial '\'
-chardev spicevmc,id=vdagent,name=vdagent '\'
-device virtserialport,chardev=vdagent,name=com.redhat.spice.0 &&
exec spicy --title "$NAME" 127.0.0.1 -p "$PORT"
EOF
elif [ "$ANSWER" == "n" ] ;then
cat <<EOF > "$HOME/.bin/qemu/$FILE"
#!/bin/bash
qemu-system-x86_64 -enable-kvm --daemonize '\'
-cpu host '\'
-drive format=raw,file="$DISK",if=virtio '\'
-m 4G '\'
-name "$NAME" '\'
-vga qxl '\'
-spice port="$PORT",disable-ticketing '\'
-device virtio-serial '\'
-chardev spicevmc,id=vdagent,name=vdagent '\'
-device virtserialport,chardev=vdagent,name=com.redhat.spice.0 &&
exec spicy --title "$NAME" 127.0.0.1 -p "$PORT"
EOF
fi
# Fix
## Remove '' from \
sed -i "s/'//g" "$HOME/.bin/qemu/$FILE"
}
## Remove GUI options for headless VM
function remove_gui {
sed -i "/-vga\|-spice\|virtio-serial\|-chardev\|exec/d" "$HOME/.bin/qemu/$1"
}
echo -n "Do you want to create a qemu script or remove the gui from a script? [create/remove]: "
read FUNCTION
if [ "$FUNCTION" == "create" ] ;then
create_script
elif [ "$FUNCTION" == "remove" ] ;then
echo "Enter script's file path and press [Enter]: "
read FILENAME
remove_gui $FILENAME
fi