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")
@ -36,13 +36,14 @@ OPTION available
options=<option> additinal options to give to qemu. By default is
-display gtk -vga qxl
"
MAIN_LOCATION="$HOME/virtual_machine/"
MAIN_LOCATION="$HOME/virtual_machine"
OPTION="-display gtk \
-vga qxl"
-usbdevice tablet"
# other options choices
#
#-display sdl
#-vga qxl
#-usbdevice tablet \
@ -54,12 +55,12 @@ HELP() {
INIT() {
echo "INIT for vm $1"
NAME=$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 [[ $entry =~ ^[yY]$ ]]
if [[ "$entry" =~ ^[yY]$ ]]
then
FORWARD=",hostfwd=tcp::10022-:22" # forward VM 22 port to Host 10022
else
@ -70,108 +71,102 @@ INIT() {
cat << EOF > "conf/$NAME-vm_var.sh"
NAME="$NAME"
VMHOSTNAME="\$NAME-vm"
VMDRIVE="\${VMHOSTNAME}.cow"
NPROC="$NPROC"
MEMORY="$MEMORY"
FORWARD="$FORWARD"
LOCATION="\$NAME-sandbox/"
LOCATION="\$NAME-sandbox"
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() {
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
start_the_vm
}
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
VMDRIVE="${VMDRIVE}.backup"
start_the_vm
}
CREATE() {
if [ -z ${SIZE+x} ]
if [ -z "${SIZE+x}" ]
then
SIZE="40G"
fi
if [ ! -f "${MAIN_LOCATION}${LOCATION}${VMHOSTNAME}.cow" ]
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
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 ]
if [ ! -f "${MAIN_LOCATION}/${LOCATION}/OVMF_VARS.4m.fd" ]
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
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 \
OPTION="$OPTION \
-boot d \
-drive file="$1",media=cdrom \
$OPTION
-drive file=$1,media=cdrom"
start_the_vm
}
RESTORE() {
echo "Are you sure you want to restore the rescue? (y/N)"
read -r entry
if [[ $entry =~ ^[yY]$ ]]
if [[ "$entry" =~ ^[yY]$ ]]
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"
fi
}
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
if [[ $entry =~ ^[yY]$ ]]
if [[ "$entry" =~ ^[yY]$ ]]
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"
fi
}
SUPPRESS() {
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
rm -Ri ${MAIN_LOCATION}${LOCATION}
rm -Ri "${MAIN_LOCATION}/${LOCATION}"
fi
}
@ -179,8 +174,7 @@ SUPPRESS() {
# gère les paramètres
for i in $@;
do
for i in "$@"; do
if [ "$i" == "-h" ] || [ "$i" == "--help" ]
then
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.
mkdir -p $MAIN_LOCATION
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.
@ -210,16 +204,16 @@ cd "$( dirname "$( readlink -f "$0" )" )" || exit
if [ "$2" == "init" ]
then
INIT $1
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
INIT "$1"
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
. "conf/$1-vm_var.sh"
for (( i=3; i<=$#; i++)); do
if [ "${!i%=*}" == "nproc" ]
@ -233,7 +227,7 @@ for (( i=3; i<=$#; i++)); do
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"
-drive if=pflash,format=raw,file=${MAIN_LOCATION}/${LOCATION}/OVMF_VARS.4m.fd"
fi
done
@ -243,7 +237,7 @@ then
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"
-drive if=pflash,format=raw,file=${MAIN_LOCATION}/${LOCATION}/OVMF_VARS.4m.fd"
fi
@ -269,7 +263,7 @@ then
if [ $# -ge 3 ]
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.
CREATE $3 # TODO
CREATE "$3" # TODO
else
echo "$#"
echo "Error, no image defined." >&2
@ -277,7 +271,7 @@ then
elif [ "$2" == "suppr" ]
then
echo "suppressing all data of the vm $1"
SUPPRESS $1
SUPPRESS "$1"
else
echo "commande $2 inconnue" >&2
HELP