2024-01-23 21:05:06 +01:00
#!/usr/bin/bash
2024-01-13 17:50:37 +01:00
#
2024-01-22 01:32:12 +01:00
#RESULT_POSSIBLE=("init","start","start_backup","create","backup","restore","suppr")
2024-01-21 03:50:10 +01:00
2024-01-21 20:11:24 +01:00
# Constante d'éxecutions.
2024-01-22 01:44:36 +01:00
Version = "0.6.1"
2024-01-21 20:11:24 +01:00
USAGE = " vm-start [ NAME COMMAND [PARAMETER] ] [OPTION]
2024-01-21 03:50:10 +01:00
Script to manage virtual machine easily.
NAME is the name of the virtual machine to use.
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
PARAMETTER available
-h, --help Print this help and exit
-v, --version Print the version of this program and quit
OPTION available
2024-01-21 20:11:24 +01:00
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.
2024-01-21 03:50:10 +01:00
can be either set or unset. By default is unset.
2024-01-21 20:11:24 +01:00
options = <option> additinal options to give to qemu. By default is
2024-01-21 03:50:10 +01:00
-display gtk -vga qxl
"
2024-01-23 21:05:06 +01:00
MAIN_LOCATION = " $HOME /virtual_machine "
2024-01-21 03:50:10 +01:00
OPTION = " -display gtk \
2024-01-23 21:05:06 +01:00
-usbdevice tablet"
2024-01-21 03:50:10 +01:00
# other options choices
#
#-display sdl
2024-01-23 21:05:06 +01:00
#-vga qxl
2024-01-21 03:50:10 +01:00
#-usbdevice tablet \
2024-01-21 20:11:24 +01:00
HELP( ) {
echo " $USAGE "
exit 0
}
2024-01-13 17:50:37 +01:00
INIT( ) {
echo " INIT for vm $1 "
2024-01-23 21:05:06 +01:00
NAME = " $1 "
2024-01-22 01:32:12 +01:00
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
2024-01-13 17:50:37 +01:00
2024-01-23 21:05:06 +01:00
if [ [ " $entry " = ~ ^[ yY] $ ] ]
2024-01-13 17:50:37 +01:00
then
FORWARD = ",hostfwd=tcp::10022-:22" # forward VM 22 port to Host 10022
else
FORWARD = ""
fi
mkdir -p conf
2024-01-22 01:32:12 +01:00
cat << EOF > "conf/$NAME -vm_var.sh"
NAME = " $NAME "
VMHOSTNAME = "\$NAME-vm"
2024-01-23 21:05:06 +01:00
VMDRIVE = "\${VMHOSTNAME}.cow"
2024-01-22 01:32:12 +01:00
NPROC = " $NPROC "
MEMORY = " $MEMORY "
FORWARD = " $FORWARD "
2024-01-23 21:05:06 +01:00
LOCATION = "\$NAME-sandbox"
2024-01-22 01:32:12 +01:00
EOF
2024-01-23 21:05:06 +01:00
chmod +x " conf/ $NAME -vm_var.sh "
2024-01-13 17:50:37 +01:00
}
2024-01-23 21:05:06 +01:00
start_the_vm( ) {
echo " ${ MAIN_LOCATION } / ${ LOCATION } / ${ VMDRIVE } "
echo " $OPTION "
2024-01-13 17:50:37 +01:00
qemu-system-x86_64 \
-enable-kvm \
-cpu host \
2024-01-23 21:05:06 +01:00
-smp " $NPROC " \
2024-01-13 17:50:37 +01:00
-net nic \
2024-01-23 21:05:06 +01:00
-net user" $FORWARD " ,hostname= " $VMHOSTNAME " \
-m " $MEMORY " \
-drive file = " ${ MAIN_LOCATION } / ${ LOCATION } / ${ VMDRIVE } " \
$OPTION
}
START( ) {
start_the_vm
2024-01-13 17:50:37 +01:00
}
2024-01-22 01:32:12 +01:00
START_BACKUP( ) {
2024-01-23 21:05:06 +01:00
VMDRIVE = " ${ VMDRIVE } .backup "
start_the_vm
2024-01-13 17:50:37 +01:00
}
CREATE( ) {
2024-01-23 21:05:06 +01:00
if [ -z " ${ SIZE +x } " ]
2024-01-21 03:50:10 +01:00
then
SIZE = "40G"
fi
2024-01-23 21:05:06 +01:00
if [ ! -f " ${ MAIN_LOCATION } / ${ LOCATION } / ${ VMHOSTNAME } .cow " ]
2024-01-13 17:50:37 +01:00
then
2024-01-23 21:05:06 +01:00
echo " create directory ${ MAIN_LOCATION } / ${ LOCATION } / "
mkdir -p " ${ MAIN_LOCATION } / ${ LOCATION } / "
qemu-img create -f qcow2 " ${ MAIN_LOCATION } / ${ LOCATION } / ${ VMHOSTNAME } .cow " " $SIZE "
2024-01-21 03:50:10 +01:00
fi
2024-01-22 01:32:12 +01:00
if [ " ${ UEFI } " = = "set" ]
2024-01-21 03:50:10 +01:00
then
echo "create UEFIVAR"
2024-01-23 21:05:06 +01:00
if [ ! -f " ${ MAIN_LOCATION } / ${ LOCATION } /OVMF_VARS.4m.fd " ]
2024-01-21 03:50:10 +01:00
then
2024-01-23 21:05:06 +01:00
cp /usr/share/edk2/x64/OVMF_VARS.4m.fd " ${ MAIN_LOCATION } / ${ LOCATION } "
2024-01-21 03:50:10 +01:00
else
echo "INFO, UEFIVAR already existing"
fi
2024-01-13 17:50:37 +01:00
fi
2024-01-23 21:05:06 +01:00
OPTION = " $OPTION \
2024-01-13 17:50:37 +01:00
-boot d \
2024-01-23 21:05:06 +01:00
-drive file = $1 ,media= cdrom"
start_the_vm
2024-01-13 17:50:37 +01:00
}
RESTORE( ) {
echo "Are you sure you want to restore the rescue? (y/N)"
2024-01-22 01:32:12 +01:00
read -r entry
2024-01-23 21:05:06 +01:00
if [ [ " $entry " = ~ ^[ yY] $ ] ]
2024-01-13 17:50:37 +01:00
then
2024-01-23 21:05:06 +01:00
cp -i " ${ MAIN_LOCATION } / ${ LOCATION } / ${ VMHOSTNAME } .cow.backup " " ${ MAIN_LOCATION } / ${ LOCATION } / ${ VMHOSTNAME } .cow "
2024-01-13 17:50:37 +01:00
echo "Done"
fi
}
BACKUP( ) {
2024-01-23 21:05:06 +01:00
# Should test if the folder ${MAIN_LOCATION}/${LOCATION}/${VMHOSTNAME}.cow exist or else it will arrive an error.
2024-01-22 01:32:12 +01:00
read -p "Are you sure you want to backup the volume? If you continue the previous backup will be lost (y/N) " -r entry
2024-01-23 21:05:06 +01:00
if [ [ " $entry " = ~ ^[ yY] $ ] ]
2024-01-13 17:50:37 +01:00
then
2024-01-23 21:05:06 +01:00
cp -i " ${ MAIN_LOCATION } / ${ LOCATION } / ${ VMHOSTNAME } .cow " " ${ MAIN_LOCATION } / ${ LOCATION } / ${ VMHOSTNAME } .cow.backup "
2024-01-13 17:50:37 +01:00
echo "Done"
fi
}
2024-01-21 03:50:10 +01:00
SUPPRESS( ) {
2024-01-22 01:32:12 +01:00
read -p " Are you sure you want to delete all data relative to the vm $1 ? (y/N) " -r entry
2024-01-23 21:05:06 +01:00
if [ [ " $entry " = ~ ^[ yY] $ ] ]
2024-01-21 03:50:10 +01:00
then
2024-01-23 21:05:06 +01:00
rm -Ri " ${ MAIN_LOCATION } / ${ LOCATION } "
2024-01-21 03:50:10 +01:00
fi
}
2024-01-13 17:50:37 +01:00
2024-01-21 20:11:24 +01:00
# gère les paramètres
2024-01-23 21:05:06 +01:00
for i in " $@ " ; do
2024-01-21 20:11:24 +01:00
if [ " $i " = = "-h" ] || [ " $i " = = "--help" ]
then
HELP
elif [ " $i " = = "-v" ] || [ " $i " = = "--version" ]
then
2024-01-22 01:32:12 +01:00
echo " vm-start ${ Version } "
2024-01-21 20:11:24 +01:00
exit 0
fi
done
# verifie au moins assez d'options pour utiliser le programme.
2024-01-13 17:50:37 +01:00
if [ $# -lt 2 ]
then
2024-01-22 01:32:12 +01:00
echo "Error, not enough args" >& 2
2024-01-21 20:11:24 +01:00
HELP
2024-01-13 17:50:37 +01:00
fi
2024-01-21 20:11:24 +01:00
# vérifie que le dossier pour stocker les vms existe & change le dossier actif pour qu'il soit cellui du lanceur.
2024-01-23 21:05:06 +01:00
mkdir -p " $MAIN_LOCATION "
2024-01-22 01:32:12 +01:00
cd " $( dirname " $( readlink -f " $0 " ) " ) " || exit
2024-01-13 17:50:37 +01:00
2024-01-21 20:11:24 +01:00
# 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
2024-01-23 21:05:06 +01:00
INIT " $1 "
2024-01-22 01:32:12 +01:00
elif [ ! -f " conf/ $1 -vm_var.sh " ]
2024-01-13 17:50:37 +01:00
then
2024-01-21 20:11:24 +01:00
echo " no config file found. creating one in conf/ ${ 1 } -vm_var.sh "
2024-01-23 21:05:06 +01:00
INIT " $1 "
2024-01-13 17:50:37 +01:00
fi
2024-01-21 20:11:24 +01:00
#Vérifie si l'option UEFI est set dans le fichier de config. Si oui l'ajoutte aux options.
2024-01-23 21:05:06 +01:00
. " conf/ $1 -vm_var.sh "
2024-01-21 20:11:24 +01:00
for ( ( i = 3; i<= $# ; i++) ) ; do
if [ " ${ !i%=* } " = = "nproc" ]
then
NPROC = " ${ !i#*= } "
elif [ " ${ !i%=* } " = = "memory" ]
then
MEMORY = " ${ !i#*= } "
elif [ " ${ !i%=* } " = = "uefi" ]
then
2024-01-22 01:32:12 +01:00
UEFI = "set"
2024-01-21 20:11:24 +01:00
OPTION = " $OPTION \
-drive if = pflash,format= raw,readonly= on,file= /usr/share/edk2/x64/OVMF_CODE.4m.fd \
2024-01-23 21:05:06 +01:00
-drive if = pflash,format= raw,file= ${ MAIN_LOCATION } /${ LOCATION } /OVMF_VARS.4m.fd"
2024-01-21 20:11:24 +01:00
fi
done
2024-01-22 01:32:12 +01:00
if [ " ${ UEFI } " = = "yes" ]
2024-01-21 03:50:10 +01:00
then
2024-01-22 01:32:12 +01:00
UEFI = "set"
2024-01-21 03:50:10 +01:00
OPTION = " $OPTION \
-drive if = pflash,format= raw,readonly= on,file= /usr/share/edk2/x64/OVMF_CODE.4m.fd \
2024-01-23 21:05:06 +01:00
-drive if = pflash,format= raw,file= ${ MAIN_LOCATION } /${ LOCATION } /OVMF_VARS.4m.fd"
2024-01-21 03:50:10 +01:00
fi
2024-01-13 17:50:37 +01:00
if [ " $2 " = = "start" ]
then
echo " starting VM $1 "
START
elif [ " $2 " = = "start-backup" ]
then
echo "starting VM rescue"
2024-01-22 01:32:12 +01:00
START_BACKUP
2024-01-13 17:50:37 +01:00
elif [ " $2 " = = "backup" ]
then
echo "backup VM"
BACKUP
elif [ " $2 " = = "restore" ]
then
echo "restore VM"
RESTORE
elif [ " $2 " = = "create" ]
then
echo "creating VM"
2024-01-22 01:32:12 +01:00
if [ $# -ge 3 ]
2024-01-13 17:50:37 +01:00
then
2024-01-22 01:32:12 +01:00
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.
2024-01-23 21:05:06 +01:00
CREATE " $3 " # TODO
2024-01-13 17:50:37 +01:00
else
2024-01-22 01:32:12 +01:00
echo " $# "
echo "Error, no image defined." >& 2
2024-01-13 17:50:37 +01:00
fi
2024-01-22 01:32:12 +01:00
elif [ " $2 " = = "suppr" ]
2024-01-21 03:50:10 +01:00
then
echo " suppressing all data of the vm $1 "
2024-01-23 21:05:06 +01:00
SUPPRESS " $1 "
2024-01-13 17:50:37 +01:00
else
2024-01-22 01:32:12 +01:00
echo " commande $2 inconnue " >& 2
2024-01-21 20:11:24 +01:00
HELP
2024-01-13 17:50:37 +01:00
fi