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