vm-start/vm-start.sh

297 lines
7.5 KiB
Bash
Executable file

#!/usr/bin/bash
#
#RESULT_POSSIBLE=("init","list","start","start_backup","create","backup","restore","suppr")
# Constante d'éxecutions.
Version="0.7.0-developpement_day"
USAGE="vm-start [PARAMETTER] [ NAME COMMAND [OPTION] ]
Script to manage virtual machine easily.
NAME is the name of the virtual machine to use.
PARAMETTER available
-h, --help Print this help and exit
-v, --version Print the version of this program and quit
-l, --list Print the list of all availables machines and quit <TODO>
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
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 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
create_disk=<loc_disk> Def a create disk to boot on. Is necessary for using create, or need the variable INSTALLATION_DISK_LOCATION to be set.
"
MAIN_LOCATION="$HOME/virtual_machine"
# if MAIN_LOCATION doesn't exists, create the folder.
if [ -f "$MAIN_LOCATION" ]
then
mkdir "$MAIN_LOCATION" || exit 0 #Exit in case the folder can't be created.
echo "$MAIN_LOCATION created to contain all virtual machines."
fi
OPTION="-display gtk \
-usbdevice tablet"
# other options choices
#
#-display sdl
#-vga qxl
#-usbdevice tablet \
HELP() {
echo "$USAGE"
exit 0
}
INIT() {
echo "INIT for vm $1"
NAME="$1"
read -p "How many proccessor will you use ? " -r NPROC
read -p "How much memory will you need ? " -r MEMORY
read -p "Do you want to forward ssh port (y/n) " -r entry
if echo "$entry" | grep -q "^[yY]$"
then
FORWARD=",hostfwd=tcp::10022-:22" # forward VM 22 port to Host 10022
fi
mkdir -p conf
cat << EOF > "conf/$NAME-vm_var.sh"
NAME="${NAME}"
VMHOSTNAME="\${NAME}-vm"
VMDRIVE="\${VMHOSTNAME}.cow"
NPROC="${NPROC}"
MEMORY="${MEMORY}"
FORWARD="${FORWARD}"
LOCATION="\${NAME}-sandbox"
EOF
chmod +x "conf/$NAME-vm_var.sh"
}
start_the_vm() {
qemu-system-x86_64 \
-enable-kvm \
-cpu host \
-smp "$NPROC" \
-net nic \
-net user"$FORWARD",hostname="$VMHOSTNAME" \
-m "$MEMORY" \
-drive file="${MAIN_LOCATION}/${LOCATION}/${VMDRIVE}",index=2,id=maindrive,media=disk \
$OPTION
}
START() {
start_the_vm
}
START_BACKUP() {
VMDRIVE="${VMDRIVE}.backup"
start_the_vm
}
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 [ "${UEFI}" == "set" ]
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
OPTION="$OPTION \
-boot d \
-drive file=$1,if=none,media=cdrom,id=drive-cd1 \
-device ide-cd,drive=drive-cd1,id=cd1,bootindex=1"
start_the_vm
}
RESTORE() {
read -p "Are you sure you want to restore the rescue? (y/N) " -r entry
if echo "$entry" | grep -q "^[yY]$"
then
cp -i "${MAIN_LOCATION}/${LOCATION}/${VMHOSTNAME}.cow.backup" "${MAIN_LOCATION}/${LOCATION}/${VMHOSTNAME}.cow"
echo "Done"
fi
}
BACKUP() {
read -p "Are you sure you want to backup the volume? If you continue the previous backup will be lost (y/N) " -r entry
if echo "$entry" | grep -q "^[yY]$"
then
cp -i "${MAIN_LOCATION}/${LOCATION}/${VMHOSTNAME}.cow" "${MAIN_LOCATION}/${LOCATION}/${VMHOSTNAME}.cow.backup"
echo "Done"
fi
}
SUPPRESS() {
read -p "Are you sure you want to delete all data relative to the vm $1? (y/N)" -r entry
if echo "$entry" | grep -q "^[yY]$"
then
rm -Ri "${MAIN_LOCATION}/${LOCATION}"
fi
}
set_uefi() {
UEFI="set"
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"
}
# gère les paramètres
for i in "$@"; do
if [ "$i" == "-h" ] || [ "$i" == "--help" ]
then
HELP
elif [ "$i" == "-v" ] || [ "$i" == "--version" ]
then
echo "vm-start ${Version}"
exit 0
fi
done
# verifie au moins assez d'options pour utiliser le programme.
if [ $# -lt 2 ]
then
echo "Error, not enough args" >&2
HELP
fi
# vérifie que le dossier pour stocker les vms existe & change le dossier actif pour qu'il soit cellui du lanceur.
mkdir -p "$MAIN_LOCATION"
cd "$( dirname "$( readlink -f "$0" )" )" || exit
# Véréfie qu'il y ait bien un fichier de config pour la vm que l'on tente de démarer dans la cas contraire en génére un.
# possibilitée de générer à nouveau le fichier de config avec vm-start nom-vm init
if [ ! -f "conf/$1-vm_var.sh" ]
then
echo "no config file found. creating one in conf/${1}-vm_var.sh"
INIT "$1"
if [ "$2" == "init" ]
then
# Exit, it is the end of the program.
exit 0
fi
fi
#Vérifie si l'option UEFI est set dans le fichier de config. Si oui l'ajoutte aux options.
. "conf/$1-vm_var.sh"
for (( i=3; i<=$#; i++)); do
case "${!i%=*}" in
"nproc")
NPROC="${!i#*=}"
;;
"memory")
MEMORY="${!i#*=}"
;;
"uefi")
set_uefi
;;
"create_disk")
INSTALLATION_DISK_LOCATION="${!i#*=}"
;;
esac
done
if [ "${UEFI}" == "yes" ]
then
set_uefi
fi
case "$2" in
"start")
echo "starting VM $1"
START
;;
"start-backup")
echo "starting VM rescue"
START_BACKUP
;;
"backup")
echo "backup VM"
BACKUP
;;
"restore")
echo "restore backup"
RESTORE
;;
"create")
echo "creating VM"
if [ ! "${INSTALLATION_DISK_LOCATION}" == "" ]
then
echo "using image disk ${INSTALLATION_DISK_LOCATION}"
CREATE "${INSTALLATION_DISK_LOCATION}"
else
echo "Error, no image defined." >&2
fi
;;
"suppr")
echo "suppressing all data of the vm $1"
SUPPRESS "$1"
;;
"init")
echo "Init volume $1"
INIT "$1"
;;
*)
echo "commande $2 inconnue" >&2
HELP
esac