application of some corrections

This commit is contained in:
primardj 2024-01-22 00:32:12 +00:00
parent aa98ee4c37
commit abf7786020
2 changed files with 37 additions and 47 deletions

View file

@ -65,5 +65,5 @@ done
echo user=${USER} echo user=${USER}
echo port=${PORT} echo port=${PORT}
ssh -o "StrictHostKeyChecking no" -o "UserKnownHostsFile=/dev/null" ${USER}@localhost -p ${PORT} ssh -o "StrictHostKeyChecking no" -o "UserKnownHostsFile=/dev/null" -p ${PORT} ${USER}@localhost

View file

@ -1,10 +1,10 @@
#!/bin/bash #!/bin/bash
# #
#RESULT_POSSIBLE=("init","start","start-backup","create","backup","restore","suppr") #RESULT_POSSIBLE=("init","start","start_backup","create","backup","restore","suppr")
# Constante d'éxecutions. # Constante d'éxecutions.
VERSION=0.6 Version="0.6"
USAGE="vm-start [ NAME COMMAND [PARAMETER] ] [OPTION] USAGE="vm-start [ NAME COMMAND [PARAMETER] ] [OPTION]
Script to manage virtual machine easily. Script to manage virtual machine easily.
@ -55,13 +55,9 @@ HELP() {
INIT() { INIT() {
echo "INIT for vm $1" echo "INIT for vm $1"
NAME=$1 NAME=$1
echo "How many proccessor will you use ? " read -p "How many proccessor will you use ? " -r NPROC
read NPROC read -p "How much memory will you need ? " -r MEMORY
echo "How much memory will you need ? " read -p "Do you want to forward ssh port (y/n) " -r entry
read MEMORY
echo "Do you want to forward ssh port (y/n) "
read entry
if [[ $entry =~ ^[yY]$ ]] if [[ $entry =~ ^[yY]$ ]]
then then
@ -71,14 +67,14 @@ INIT() {
fi fi
mkdir -p conf mkdir -p conf
echo " cat << EOF > "conf/$NAME-vm_var.sh"
NAME=\"$NAME\" NAME="$NAME"
VMHOSTNAME=\"\$NAME-vm\" VMHOSTNAME="\$NAME-vm"
NPROC=$NPROC NPROC="$NPROC"
MEMORY=\"$MEMORY\" MEMORY="$MEMORY"
FORWARD=\"$FORWARD\" FORWARD="$FORWARD"
LOCATION=\"\$NAME-sandbox/\" LOCATION="\$NAME-sandbox/"
" > conf/$NAME-vm_var.sh EOF
chmod +x conf/$NAME-vm_var.sh chmod +x conf/$NAME-vm_var.sh
} }
@ -95,12 +91,9 @@ START() {
$OPTION \ $OPTION \
-daemonize -daemonize
sleep 5
ssh -o "StrictHostKeyChecking no" -o "UserKnownHostsFile=/dev/null" primardj@localhost -p 10022
} }
START-BACKUP() { START_BACKUP() {
qemu-system-x86_64 \ qemu-system-x86_64 \
-enable-kvm \ -enable-kvm \
-cpu host \ -cpu host \
@ -112,9 +105,6 @@ START-BACKUP() {
$OPTION \ $OPTION \
-daemonize -daemonize
sleep 5
#ssh -o "StrictHostKeyChecking no" -o "UserKnownHostsFile=/dev/null" primardj@localhost -p 10022
} }
CREATE() { CREATE() {
@ -124,14 +114,14 @@ CREATE() {
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 [ -n ${UEFI+x} ] 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 ]
@ -158,7 +148,7 @@ CREATE() {
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 entry read -r entry
if [[ $entry =~ ^[yY]$ ]] if [[ $entry =~ ^[yY]$ ]]
then then
cp ${MAIN_LOCATION}${LOCATION}${VMHOSTNAME}.cow.backup ${MAIN_LOCATION}${LOCATION}${VMHOSTNAME}.cow cp ${MAIN_LOCATION}${LOCATION}${VMHOSTNAME}.cow.backup ${MAIN_LOCATION}${LOCATION}${VMHOSTNAME}.cow
@ -168,8 +158,8 @@ RESTORE() {
BACKUP() { BACKUP() {
echo "Are you sure you want to backup the volume? If you continue the previous backup will be lost (y/N)" # Should test if the folder ${MAIN_LOCATION}${LOCATION}${VMHOSTNAME}.cow exist or else it will arrive an error.
read 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 ${MAIN_LOCATION}${LOCATION}${VMHOSTNAME}.cow ${MAIN_LOCATION}${LOCATION}${VMHOSTNAME}.cow.backup cp ${MAIN_LOCATION}${LOCATION}${VMHOSTNAME}.cow ${MAIN_LOCATION}${LOCATION}${VMHOSTNAME}.cow.backup
@ -178,8 +168,7 @@ BACKUP() {
} }
SUPPRESS() { SUPPRESS() {
echo "Are you sure you want to delete all data relative to the vm $1" read -p "Are you sure you want to delete all data relative to the vm $1? (y/N)" -r entry
read entry
if [[ $entry =~ ^[yY]$ ]] if [[ $entry =~ ^[yY]$ ]]
then then
rm -Ri ${MAIN_LOCATION}${LOCATION} rm -Ri ${MAIN_LOCATION}${LOCATION}
@ -197,7 +186,7 @@ do
HELP HELP
elif [ "$i" == "-v" ] || [ "$i" == "--version" ] elif [ "$i" == "-v" ] || [ "$i" == "--version" ]
then then
echo "vm-start ${VERSION}" echo "vm-start ${Version}"
exit 0 exit 0
fi fi
done done
@ -207,14 +196,14 @@ done
if [ $# -lt 2 ] if [ $# -lt 2 ]
then then
echo "Error, not enough args" echo "Error, not enough args" >&2
HELP HELP
fi 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" )" )" 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.
# possibilitée de générer à nouveau le fichier de config avec vm-start nom-vm init # possibilitée de générer à nouveau le fichier de config avec vm-start nom-vm init
@ -222,14 +211,13 @@ cd "$( dirname "$( readlink -f "$0" )" )"
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.
#TODO l'ajoutter au detecteur d'options.
. conf/$1-vm_var.sh . conf/$1-vm_var.sh
@ -242,7 +230,7 @@ for (( i=3; i<=$#; i++)); do
MEMORY="${!i#*=}" MEMORY="${!i#*=}"
elif [ "${!i%=*}" == "uefi" ] elif [ "${!i%=*}" == "uefi" ]
then then
unset UEFI 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"
@ -250,8 +238,9 @@ for (( i=3; i<=$#; i++)); do
done done
if [ -n "${UEFI+x}" ] if [ "${UEFI}" == "yes" ]
then then
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"
@ -265,7 +254,7 @@ then
elif [ "$2" == "start-backup" ] elif [ "$2" == "start-backup" ]
then then
echo "starting VM rescue" echo "starting VM rescue"
START-BACKUP START_BACKUP
elif [ "$2" == "backup" ] elif [ "$2" == "backup" ]
then then
echo "backup VM" echo "backup VM"
@ -277,19 +266,20 @@ then
elif [ "$2" == "create" ] elif [ "$2" == "create" ]
then then
echo "creating VM" echo "creating VM"
if [[ $# == 3 ]] if [ $# -ge 3 ]
then then
echo "using image disk $3" 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 CREATE $3 # TODO
else else
echo "Error, no image defined." echo "$#"
echo "Error, no image defined." >&2
fi fi
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 SUPPRESS $1
else else
echo "commande $2 inconnue" echo "commande $2 inconnue" >&2
HELP HELP
fi fi