use shellckeck and other Hippolyte correction

This commit is contained in:
primardj 2024-01-23 20:05:06 +00:00
parent 66d877d267
commit 4a7ab2b52e

View file

@ -1,4 +1,4 @@
#!/bin/bash #!/usr/bin/bash
# #
#RESULT_POSSIBLE=("init","start","start_backup","create","backup","restore","suppr") #RESULT_POSSIBLE=("init","start","start_backup","create","backup","restore","suppr")
@ -36,13 +36,14 @@ OPTION available
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 -display gtk -vga qxl
" "
MAIN_LOCATION="$HOME/virtual_machine/" MAIN_LOCATION="$HOME/virtual_machine"
OPTION="-display gtk \ OPTION="-display gtk \
-vga qxl" -usbdevice tablet"
# other options choices # other options choices
# #
#-display sdl #-display sdl
#-vga qxl
#-usbdevice tablet \ #-usbdevice tablet \
@ -54,12 +55,12 @@ HELP() {
INIT() { INIT() {
echo "INIT for vm $1" echo "INIT for vm $1"
NAME=$1 NAME="$1"
read -p "How many proccessor will you use ? " -r NPROC read -p "How many proccessor will you use ? " -r NPROC
read -p "How much memory will you need ? " -r MEMORY read -p "How much memory will you need ? " -r MEMORY
read -p "Do you want to forward ssh port (y/n) " -r entry read -p "Do you want to forward ssh port (y/n) " -r entry
if [[ $entry =~ ^[yY]$ ]] if [[ "$entry" =~ ^[yY]$ ]]
then then
FORWARD=",hostfwd=tcp::10022-:22" # forward VM 22 port to Host 10022 FORWARD=",hostfwd=tcp::10022-:22" # forward VM 22 port to Host 10022
else else
@ -70,108 +71,102 @@ INIT() {
cat << EOF > "conf/$NAME-vm_var.sh" cat << EOF > "conf/$NAME-vm_var.sh"
NAME="$NAME" NAME="$NAME"
VMHOSTNAME="\$NAME-vm" VMHOSTNAME="\$NAME-vm"
VMDRIVE="\${VMHOSTNAME}.cow"
NPROC="$NPROC" NPROC="$NPROC"
MEMORY="$MEMORY" MEMORY="$MEMORY"
FORWARD="$FORWARD" FORWARD="$FORWARD"
LOCATION="\$NAME-sandbox/" LOCATION="\$NAME-sandbox"
EOF EOF
chmod +x conf/$NAME-vm_var.sh chmod +x "conf/$NAME-vm_var.sh"
}
start_the_vm() {
echo "${MAIN_LOCATION}/${LOCATION}/${VMDRIVE}"
echo "$OPTION"
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}" \
$OPTION
} }
START() { START() {
qemu-system-x86_64 \ start_the_vm
-enable-kvm \
-cpu host \
-smp $NPROC \
-net nic \
-net user$FORWARD,hostname=$VMHOSTNAME \
-m $MEMORY \
-drive file=${MAIN_LOCATION}${LOCATION}${VMHOSTNAME}.cow \
$OPTION \
-daemonize
} }
START_BACKUP() { START_BACKUP() {
qemu-system-x86_64 \ VMDRIVE="${VMDRIVE}.backup"
-enable-kvm \ start_the_vm
-cpu host \
-smp $NPROC \
-net nic \
-net user$FORWARD,hostname=$VMHOSTNAME \
-m $MEMORY \
-drive file=${MAIN_LOCATION}${LOCATION}${VMHOSTNAME}.cow.backup \
$OPTION \
-daemonize
} }
CREATE() { CREATE() {
if [ -z ${SIZE+x} ] if [ -z "${SIZE+x}" ]
then then
SIZE="40G" SIZE="40G"
fi fi
if [ ! -f "${MAIN_LOCATION}${LOCATION}${VMHOSTNAME}.cow" ] if [ ! -f "${MAIN_LOCATION}/${LOCATION}/${VMHOSTNAME}.cow" ]
then then
echo "create directory ${MAIN_LOCATION}${LOCATION}" echo "create directory ${MAIN_LOCATION}/${LOCATION}/"
mkdir -p ${MAIN_LOCATION}${LOCATION} mkdir -p "${MAIN_LOCATION}/${LOCATION}/"
qemu-img create -f qcow2 ${MAIN_LOCATION}${LOCATION}${VMHOSTNAME}.cow $SIZE qemu-img create -f qcow2 "${MAIN_LOCATION}/${LOCATION}/${VMHOSTNAME}.cow" "$SIZE"
fi fi
if [ "${UEFI}" == "set" ] if [ "${UEFI}" == "set" ]
then then
echo "create UEFIVAR" echo "create UEFIVAR"
if [ ! -f ${MAIN_LOCATION}${LOCATION}OVMF_VARS.4m.fd ] if [ ! -f "${MAIN_LOCATION}/${LOCATION}/OVMF_VARS.4m.fd" ]
then then
cp /usr/share/edk2/x64/OVMF_VARS.4m.fd ${MAIN_LOCATION}${LOCATION} cp /usr/share/edk2/x64/OVMF_VARS.4m.fd "${MAIN_LOCATION}/${LOCATION}"
else else
echo "INFO, UEFIVAR already existing" echo "INFO, UEFIVAR already existing"
fi fi
fi fi
OPTION="$OPTION \
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 \ -boot d \
-drive file="$1",media=cdrom \ -drive file=$1,media=cdrom"
$OPTION
start_the_vm
} }
RESTORE() { RESTORE() {
echo "Are you sure you want to restore the rescue? (y/N)" echo "Are you sure you want to restore the rescue? (y/N)"
read -r entry read -r entry
if [[ $entry =~ ^[yY]$ ]] if [[ "$entry" =~ ^[yY]$ ]]
then then
cp -i ${MAIN_LOCATION}${LOCATION}${VMHOSTNAME}.cow.backup ${MAIN_LOCATION}${LOCATION}${VMHOSTNAME}.cow cp -i "${MAIN_LOCATION}/${LOCATION}/${VMHOSTNAME}.cow.backup" "${MAIN_LOCATION}/${LOCATION}/${VMHOSTNAME}.cow"
echo "Done" echo "Done"
fi fi
} }
BACKUP() { BACKUP() {
# Should test if the folder ${MAIN_LOCATION}${LOCATION}${VMHOSTNAME}.cow exist or else it will arrive an error. # Should test if the folder ${MAIN_LOCATION}/${LOCATION}/${VMHOSTNAME}.cow exist or else it will arrive an error.
read -p "Are you sure you want to backup the volume? If you continue the previous backup will be lost (y/N) " -r entry 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 [[ $entry =~ ^[yY]$ ]] if [[ "$entry" =~ ^[yY]$ ]]
then then
cp -i ${MAIN_LOCATION}${LOCATION}${VMHOSTNAME}.cow ${MAIN_LOCATION}${LOCATION}${VMHOSTNAME}.cow.backup cp -i "${MAIN_LOCATION}/${LOCATION}/${VMHOSTNAME}.cow" "${MAIN_LOCATION}/${LOCATION}/${VMHOSTNAME}.cow.backup"
echo "Done" echo "Done"
fi fi
} }
SUPPRESS() { SUPPRESS() {
read -p "Are you sure you want to delete all data relative to the vm $1? (y/N)" -r entry read -p "Are you sure you want to delete all data relative to the vm $1? (y/N)" -r entry
if [[ $entry =~ ^[yY]$ ]] if [[ "$entry" =~ ^[yY]$ ]]
then then
rm -Ri ${MAIN_LOCATION}${LOCATION} rm -Ri "${MAIN_LOCATION}/${LOCATION}"
fi fi
} }
@ -179,8 +174,7 @@ SUPPRESS() {
# gère les paramètres # gère les paramètres
for i in $@; for i in "$@"; do
do
if [ "$i" == "-h" ] || [ "$i" == "--help" ] if [ "$i" == "-h" ] || [ "$i" == "--help" ]
then then
HELP HELP
@ -202,7 +196,7 @@ fi
# vérifie que le dossier pour stocker les vms existe & change le dossier actif pour qu'il soit cellui du lanceur. # 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 mkdir -p "$MAIN_LOCATION"
cd "$( dirname "$( readlink -f "$0" )" )" || exit 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. # 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.
@ -210,16 +204,16 @@ cd "$( dirname "$( readlink -f "$0" )" )" || exit
if [ "$2" == "init" ] if [ "$2" == "init" ]
then then
INIT $1 INIT "$1"
elif [ ! -f "conf/$1-vm_var.sh" ] elif [ ! -f "conf/$1-vm_var.sh" ]
then then
echo "no config file found. creating one in conf/${1}-vm_var.sh" echo "no config file found. creating one in conf/${1}-vm_var.sh"
INIT $1 INIT "$1"
fi fi
#Vérifie si l'option UEFI est set dans le fichier de config. Si oui l'ajoutte aux options. #Vérifie si l'option UEFI est set dans le fichier de config. Si oui l'ajoutte aux options.
. conf/$1-vm_var.sh . "conf/$1-vm_var.sh"
for (( i=3; i<=$#; i++)); do for (( i=3; i<=$#; i++)); do
if [ "${!i%=*}" == "nproc" ] if [ "${!i%=*}" == "nproc" ]
@ -233,7 +227,7 @@ for (( i=3; i<=$#; i++)); do
UEFI="set" UEFI="set"
OPTION="$OPTION \ OPTION="$OPTION \
-drive if=pflash,format=raw,readonly=on,file=/usr/share/edk2/x64/OVMF_CODE.4m.fd \ -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" -drive if=pflash,format=raw,file=${MAIN_LOCATION}/${LOCATION}/OVMF_VARS.4m.fd"
fi fi
done done
@ -243,7 +237,7 @@ then
UEFI="set" UEFI="set"
OPTION="$OPTION \ OPTION="$OPTION \
-drive if=pflash,format=raw,readonly=on,file=/usr/share/edk2/x64/OVMF_CODE.4m.fd \ -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" -drive if=pflash,format=raw,file=${MAIN_LOCATION}/${LOCATION}/OVMF_VARS.4m.fd"
fi fi
@ -269,7 +263,7 @@ then
if [ $# -ge 3 ] if [ $# -ge 3 ]
then then
echo "using image disk $3" # Error here, please don't think that there will be only three devices, should be improve like with an install-device parametter. echo "using image disk $3" # Error here, please don't think that there will be only three devices, should be improve like with an install-device parametter.
CREATE $3 # TODO CREATE "$3" # TODO
else else
echo "$#" echo "$#"
echo "Error, no image defined." >&2 echo "Error, no image defined." >&2
@ -277,7 +271,7 @@ then
elif [ "$2" == "suppr" ] elif [ "$2" == "suppr" ]
then then
echo "suppressing all data of the vm $1" echo "suppressing all data of the vm $1"
SUPPRESS $1 SUPPRESS "$1"
else else
echo "commande $2 inconnue" >&2 echo "commande $2 inconnue" >&2
HELP HELP