VM script simplified

This commit is contained in:
Sonny 2017-07-09 15:59:47 +02:00
parent 1a3264bf03
commit f00f9babee

View file

@ -1,94 +1,79 @@
#!/bin/bash
## Script to create and or edit a qemu script
function create_script {
echo -n "Create VM image? [y/n]: "
read IMAGE
create_script() {
read -p "Create VM image? [y/n]: " IMAGE
## Optional image creation
# Image creation/selection
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
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
echo -n "Boot disk needed? [y/n]: "
read ANSWER
read -p "Boot disk needed? [y/n]: " ANSWER
if [ "$ANSWER" == "y" ] ;then
echo -n "Enter the Boot disk path and press [ENTER]: "
read BOOT_DISK
read -e -p "Enter the Boot disk path and press [ENTER]: " BOOT_DISK
else
echo "Okay, dropping boot disk"
fi
echo -n "Enter the VM name and press [ENTER]: "
read NAME
read -p "Enter the VM name and press [ENTER]: " NAME
echo -n "Enter the port number and press [ENTER]: "
read PORT
read -p "Enter the port number and press [ENTER]: " PORT
echo -n "Enter qemu script's name and press [ENTER]: "
read FILE
read -p "Enter qemu script's name and press [ENTER]: " 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
echo -e '#!/bin/bash \n' \
'qemu-system-x86_64 -enable-kvm --daemonize \' '\n' \
'\t -cpu host \' '\n' \
'\t -drive format=raw,file='$DISK',if=virtio \' '\n' \
'\t -boot d -cdrom '$BOOT_DISK' \' '\n' \
'\t -m 4G \' '\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' \
| sed 's/^ *//g' > $FILE
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
echo -e '#!/bin/bash \n' \
'qemu-system-x86_64 -enable-kvm --daemonize \' '\n' \
'\t -cpu host \' '\n' \
'\t -drive format=raw,file='$DISK',if=virtio \' '\n' \
'\t -m 4G \' '\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' \
| sed 's/^ *//g' > $FILE
fi
# Fix
## Remove '' from \
sed -i "s/'//g" "$HOME/.bin/qemu/$FILE"
# Remove trailing whitespace
sed -i 's/\s*$//g' $FILE
}
## Remove GUI options for headless VM
function remove_gui {
sed -i "/-vga\|-spice\|virtio-serial\|-chardev\|exec/d" "$HOME/.bin/qemu/$1"
# Remove GUI options for headless VM
remove_gui() {
sed -i "/-vga\|-spice\|virtio-serial\|-chardev\|exec/d" "$FILENAME"
}
echo -n "Do you want to create a qemu script or remove the gui from a script? [create/remove]: "
read FUNCTION
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
echo "Enter script's file path and press [Enter]: "
read FILENAME
remove_gui $FILENAME
read -e -p "Enter script's file path and press [Enter]: " FILENAME
remove_gui
fi