79 lines
2.6 KiB
Bash
Executable file
79 lines
2.6 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
|
|
|
|
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
|
|
|
|
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
|
|
|
|
# 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 -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
|
|
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
|
|
|
|
# Remove trailing whitespace
|
|
sed -i 's/\s*$//g' $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
|