vm-start/vm-start.sh

242 lines
5.9 KiB
Bash
Executable file

#!/bin/bash
#
#RESULT_POSSIBLE=("init","start","start-backup","create","backup","restore","suppr")
VERSION=0.6
USAGE="vm-start NAME COMMAND [PARAMETER] [OPTION]
Script to manage virtual machine easily.
NAME is the name of the virtual machine to use.
COMMAND available
init Create the conf file in conf/NAME_var.sh
start Start the virtual machine using the variabes in conf/NAME_var.sh
start-backup Start the virtual machine using the backup volume if it exists.
create Create the virtual machine using the conf in conf/NAME_var.sh
backup Create a backup file for the vm
restore Restore the virtual machine using to backup file
suppr Suppr all data related to the virtual machine other than the conf file.
COMMAND TODO
suppr-all Suppr all data related to the vm even the conf file #TODO
PARAMETTER available
-h, --help Print this help and exit
-v, --version Print the version of this program and quit
OPTION available
NPROC=<nb proc> Def the number of proc of the vm
MEMORY=<mem> Def the qty of memory allocated to the vm
SIZE=<size> Def the size that should be allocated by create
UEFI=<uefi> Select if there will be an UEFI by default.
can be either set or unset. By default is unset.
OPTIONS=<option> additinal options to give to qemu. By default is
-display gtk -vga qxl
"
MAIN_LOCATION="$HOME/virtual_machine/"
OPTION="-display gtk \
-vga qxl"
# other options choices
#
#-display sdl
#-usbdevice tablet \
mkdir -p $MAIN_LOCATION
INIT() {
echo "INIT for vm $1"
NAME=$1
echo "How many proccessor will you use ? "
read NPROC
echo "How much memory will you need ? "
read MEMORY
echo "Do you want to forward ssh port (y/n) "
read entry
if [[ $entry =~ ^[yY]$ ]]
then
FORWARD=",hostfwd=tcp::10022-:22" # forward VM 22 port to Host 10022
else
FORWARD=""
fi
mkdir -p conf
echo "
NAME=\"$NAME\"
VMHOSTNAME=\"\$NAME-vm\"
NPROC=$NPROC
MEMORY=\"$MEMORY\"
FORWARD=\"$FORWARD\"
LOCATION=\"\$NAME-sandbox/\"
" > conf/$NAME-vm_var.sh
chmod +x conf/$NAME-vm_var.sh
}
START() {
qemu-system-x86_64 \
-enable-kvm \
-cpu host \
-smp $NPROC \
-net nic \
-net user$FORWARD,hostname=$VMHOSTNAME \
-m $MEMORY \
-drive file=${MAIN_LOCATION}${LOCATION}${VMHOSTNAME}.cow \
$OPTION \
-daemonize
sleep 5
ssh -o "StrictHostKeyChecking no" -o "UserKnownHostsFile=/dev/null" primardj@localhost -p 10022
}
START-BACKUP() {
qemu-system-x86_64 \
-enable-kvm \
-cpu host \
-smp $NPROC \
-net nic \
-net user$FORWARD,hostname=$VMHOSTNAME \
-m $MEMORY \
-drive file=${MAIN_LOCATION}${LOCATION}${VMHOSTNAME}.cow.backup \
$OPTION \
-daemonize
sleep 5
#ssh -o "StrictHostKeyChecking no" -o "UserKnownHostsFile=/dev/null" primardj@localhost -p 10022
}
CREATE() {
if [ -z ${SIZE+x} ]
then
SIZE="40G"
fi
if [[ ! -f ${MAIN_LOCATION}${LOCATION}${VMHOSTNAME}.cow ]]
then
echo "create directory ${MAIN_LOCATION}${LOCATION}"
mkdir -p ${MAIN_LOCATION}${LOCATION}
qemu-img create -f qcow2 ${MAIN_LOCATION}${LOCATION}${VMHOSTNAME}.cow $SIZE
fi
if [ -n ${UEFI+x} ]
then
echo "create UEFIVAR"
if [ ! -f ${MAIN_LOCATION}${LOCATION}OVMF_VARS.4m.fd ]
then
cp /usr/share/edk2/x64/OVMF_VARS.4m.fd ${MAIN_LOCATION}${LOCATION}
else
echo "INFO, UEFIVAR already existing"
fi
fi
qemu-system-x86_64 \
-enable-kvm \
-cpu host \
-smp $NPROC \
-net nic \
-net user$FORWARD,hostname=$VMHOSTNAME \
-m $MEMORY \
-drive file=${MAIN_LOCATION}${LOCATION}${VMHOSTNAME}.cow \
-boot d \
-drive file="$1",media=cdrom \
$OPTION
}
RESTORE() {
echo "Are you sure you want to restore the rescue? (y/N)"
read entry
if [[ $entry =~ ^[yY]$ ]]
then
cp ${MAIN_LOCATION}${LOCATION}${VMHOSTNAME}.cow.backup ${MAIN_LOCATION}${LOCATION}${VMHOSTNAME}.cow
echo "Done"
fi
}
BACKUP() {
echo "Are you sure you want to backup the volume? If you continue the previous backup will be lost (y/N)"
read entry
if [[ $entry =~ ^[yY]$ ]]
then
cp ${MAIN_LOCATION}${LOCATION}${VMHOSTNAME}.cow ${MAIN_LOCATION}${LOCATION}${VMHOSTNAME}.cow.backup
echo "Done"
fi
}
SUPPRESS() {
echo "Are you sure you want to delete all data relative to the vm $1"
read entry
if [[ $entry =~ ^[yY]$ ]]
then
rm -Ri ${MAIN_LOCATION}${LOCATION}
fi
}
if [ $# -lt 2 ]
then
echo "Error, not enough args"
echo "${USAGE}"
exit 1
fi
cd "$( dirname "$( readlink -f "$0" )" )"
if [[ ! -f conf/$1-vm_var.sh ]]
then
INIT $1
fi
source conf/$1-vm_var.sh
if [ -n "${UEFI+x}" ]
then
OPTION="$OPTION \
-drive if=pflash,format=raw,readonly=on,file=/usr/share/edk2/x64/OVMF_CODE.4m.fd \
-drive if=pflash,format=raw,file=${MAIN_LOCATION}${LOCATION}OVMF_VARS.4m.fd"
fi
if [ "$2" == "start" ]
then
echo "starting VM $1"
START
elif [ "$2" == "start-backup" ]
then
echo "starting VM rescue"
START-BACKUP
elif [ "$2" == "backup" ]
then
echo "backup VM"
BACKUP
elif [ "$2" == "restore" ]
then
echo "restore VM"
RESTORE
elif [ "$2" == "create" ]
then
echo "creating VM"
if [[ $# == 3 ]]
then
echo "using image disk $3"
CREATE $3
else
echo "Error, no image defined."
fi
elif [[ $2 == "suppr" ]]
then
echo "suppressing all data of the vm $1"
SUPPRESS
elif [[ "$2" == "version" ]]
then
echo "script lancement vm version v.$VERSION"
else
echo "commande $2 inconnue"
fi