add better gestion of args

This commit is contained in:
primardj 2024-01-21 19:11:24 +00:00
parent 37caf10ca4
commit aa98ee4c37

View file

@ -3,8 +3,9 @@
#RESULT_POSSIBLE=("init","start","start-backup","create","backup","restore","suppr")
# Constante d'éxecutions.
VERSION=0.6
USAGE="vm-start NAME COMMAND [PARAMETER] [OPTION]
USAGE="vm-start [ NAME COMMAND [PARAMETER] ] [OPTION]
Script to manage virtual machine easily.
@ -27,12 +28,12 @@ PARAMETTER available
-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.
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
options=<option> additinal options to give to qemu. By default is
-display gtk -vga qxl
"
MAIN_LOCATION="$HOME/virtual_machine/"
@ -44,7 +45,12 @@ OPTION="-display gtk \
#-display sdl
#-usbdevice tablet \
mkdir -p $MAIN_LOCATION
HELP() {
echo "$USAGE"
exit 0
}
INIT() {
echo "INIT for vm $1"
@ -180,21 +186,70 @@ SUPPRESS() {
fi
}
# 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"
echo "${USAGE}"
exit 1
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" )" )"
if [[ ! -f conf/$1-vm_var.sh ]]
# 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 [ "$2" == "init" ]
then
INIT $1
elif [[ ! -f conf/$1-vm_var.sh ]]
then
echo "no config file found. creating one in conf/${1}-vm_var.sh"
INIT $1
fi
source conf/$1-vm_var.sh
#Vérifie si l'option UEFI est set dans le fichier de config. Si oui l'ajoutte aux options.
#TODO l'ajoutter au detecteur d'options.
. conf/$1-vm_var.sh
for (( i=3; i<=$#; i++)); do
if [ "${!i%=*}" == "nproc" ]
then
NPROC="${!i#*=}"
elif [ "${!i%=*}" == "memory" ]
then
MEMORY="${!i#*=}"
elif [ "${!i%=*}" == "uefi" ]
then
unset UEFI
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
done
if [ -n "${UEFI+x}" ]
then
OPTION="$OPTION \
@ -233,10 +288,8 @@ 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"
HELP
fi