2024-01-23 21:05:06 +01:00
#!/usr/bin/bash
2024-01-13 17:50:37 +01:00
#
2024-01-31 20:52:46 +01:00
#RESULT_POSSIBLE=("init","list","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-03-21 22:39:01 +01:00
Version = "0.7.1"
2024-01-31 20:52:46 +01:00
USAGE = " vm-start [PARAMETTER] [ NAME COMMAND [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.
2024-01-31 20:52:46 +01:00
PARAMETTER available
-h, --help Print this help and exit
-v, --version Print the version of this program and quit
2024-01-31 21:45:08 +01:00
-l, --list Print the list of all availables machines and quit
2024-01-31 20:52:46 +01:00
2024-01-21 03:50:10 +01:00
COMMAND available
2024-01-24 02:00:49 +01:00
init Create the conf file in conf/NAME_var.sh
2024-03-03 19:26:33 +01:00
init_cdrom init a RO image/cdrom. The network will have to be set later in the conf file. NO EUFI
2024-01-24 02:00:49 +01:00
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.
2024-02-01 12:51:40 +01:00
suppr-all Suppr all data related to the vm even the conf file
2024-01-21 03:50:10 +01:00
OPTION available
2024-02-01 15:36:55 +01:00
--nproc= <nb proc> Def the number of proc of the vm
--memory= <mem> Def the qty of memory allocated to the vm
2024-02-01 16:41:55 +01:00
--size= <size> Def the size that will be allocated by create. Have no effect on other commands.
2024-02-01 15:36:55 +01:00
--uefi Select if there will be an UEFI by default.
2024-01-24 02:00:49 +01:00
can be either set or unset. By default is unset.
2024-02-01 15:36:55 +01:00
--options= <option> additinal options to give to qemu. By default is
2024-01-24 02:00:49 +01:00
-display gtk -vga qxl
2024-02-01 15:36:55 +01:00
--create_disk= <loc_disk> Def a create disk to boot on. Is necessary for using create, or need the variable INSTALLATION_DISK_LOCATION to be set.
--net= nat:id Add a nat connection to the vm. By default there is already one.
--net= br:<br-id> Connect the vm to the bridge <br-id>. Please use the script set-tap.sh as root to create the bridge. in the conf file, a mac field have to be set. for two vm to connect togethers, their mac addresses on the same network have to be differents.
--net= none The vm will have no network interface
2024-01-21 03:50:10 +01:00
"
2024-01-31 21:15:28 +01:00
2024-01-23 21:05:06 +01:00
MAIN_LOCATION = " $HOME /virtual_machine "
2024-01-31 21:15:28 +01:00
# if MAIN_LOCATION doesn't exists, create the folder.
if [ -f " $MAIN_LOCATION " ]
then
mkdir " $MAIN_LOCATION " || exit 0 #Exit in case the folder can't be created.
echo " $MAIN_LOCATION created to contain all virtual machines. "
fi
2024-02-21 21:43:31 +01:00
OPTION = " -display gtk,gl=on,show-cursor=on,show-menubar=off \
-device virtio-gpu-gl \
2024-02-14 00:04:11 +01:00
-usbdevice tablet \
-daemonize"
2024-01-21 03:50:10 +01:00
2024-03-21 22:39:01 +01:00
option_graphical = " -display sdl,gl=on,show-cursor=on \
-device virtio-vga-gl \
-usbdevice tablet \
-daemonize"
2024-01-21 03:50:10 +01:00
# other options choices
#
2024-02-21 21:43:31 +01:00
#-display gtk,show-menubar=off,gl=on,show-cursor=on,zoom-to-fit=on \
2024-01-21 03:50:10 +01:00
#-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
2024-01-31 21:45:08 +01:00
list( ) {
echo "this is the list of all vm actually configured."
for vm in $( ls conf) ; do
echo " ${ vm %-vm_var.sh } "
done
exit 0
}
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
2024-01-13 17:50:37 +01:00
2024-02-01 12:51:40 +01:00
# from now on, ssh port forward to choose while exec the script.
#read -p "Do you want to forward ssh port (y/n) " -r entry
#if echo "$entry" | grep -q "^[yY]$"
#then
# FORWARD=",hostfwd=tcp::10022-:22" # forward VM 22 port to Host 10022
#fi
2024-01-13 17:50:37 +01:00
mkdir -p conf
2024-01-22 01:32:12 +01:00
cat << EOF > "conf/$NAME -vm_var.sh"
2024-03-21 22:48:24 +01:00
vmstart_version = ${ Version }
2024-01-24 00:31:58 +01:00
NAME = " ${ NAME } "
2024-01-24 00:23:58 +01:00
VMHOSTNAME = "\${NAME}-vm"
2024-01-23 21:05:06 +01:00
VMDRIVE = "\${VMHOSTNAME}.cow"
2024-01-24 00:31:58 +01:00
NPROC = " ${ NPROC } "
MEMORY = " ${ MEMORY } "
2024-01-24 00:23:58 +01:00
LOCATION = "\${NAME}-sandbox"
2024-03-21 22:39:01 +01:00
UEFI = "no"
FORWARD = " ${ FORWARD } "
2024-02-01 03:45:49 +01:00
mac = "00:00:00:00:00:00"
2024-03-21 22:39:01 +01:00
net = ""
driver = "virtio-net"
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-03-03 19:26:33 +01:00
INIT_cdrom( ) {
echo "Init for a cdrom."
echo "Init was already called."
# necessite au moins l'addresse de l'image en tant que $1
VMROM = " $( basename $1 ) "
if [ ! -f " ${ MAIN_LOCATION } / ${ LOCATION } / ${ VMROM } " ]
then
echo " create directory ${ MAIN_LOCATION } / ${ LOCATION } / "
mkdir -p " ${ MAIN_LOCATION } / ${ LOCATION } / "
cp $1 " ${ MAIN_LOCATION } / ${ LOCATION } / ${ VMROM } "
fi
echo " CDROM_DISK=\" ${ VMROM } \" " >> " conf/ $NAME -vm_var.sh "
echo "Done"
}
2024-01-23 21:05:06 +01:00
start_the_vm( ) {
2024-02-01 00:53:00 +01:00
if [ " ${ network } " = = "" ]
then
network = " -net nic \
-net user${ FORWARD } ,hostname= ${ VMHOSTNAME } "
fi
2024-03-03 19:26:33 +01:00
if [ " ${ CDROM_DISK } " != "" ]
then
DRIVE = " -drive file= ${ MAIN_LOCATION } / ${ LOCATION } / ${ CDROM_DISK } ,index=2,id=maindrive,media=cdrom "
else
DRIVE = " -drive file= ${ MAIN_LOCATION } / ${ LOCATION } / ${ VMDRIVE } ,index=2,id=maindrive,media=disk "
fi
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-02-01 00:53:00 +01:00
$network \
2024-01-23 21:05:06 +01:00
-m " $MEMORY " \
2024-03-03 19:26:33 +01:00
$DRIVE \
2024-01-23 21:05:06 +01:00
$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-24 02:00:49 +01:00
-drive file = $1 ,if= none,media= cdrom,id= drive-cd1 \
-device ide-cd,drive= drive-cd1,id= cd1,bootindex= 1"
2024-01-23 21:05:06 +01:00
start_the_vm
2024-01-13 17:50:37 +01:00
}
RESTORE( ) {
2024-01-24 18:23:00 +01:00
read -p "Are you sure you want to restore the rescue? (y/N) " -r entry
2024-01-24 00:26:37 +01:00
if echo " $entry " | grep -q " ^[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-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-24 00:26:37 +01:00
if echo " $entry " | grep -q " ^[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-24 00:26:37 +01:00
if echo " $entry " | grep -q " ^[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-02-01 12:51:40 +01:00
suppress_all( ) {
read -p " Are you sure you want to delete all data relative to the vm $1 even conf file? (y/N) " -r entry
if echo " $entry " | grep -q " ^[yY] $"
then
rm -i conf/$1 -vm_var.sh
rm -Ri " ${ MAIN_LOCATION } / ${ LOCATION } "
fi
}
2024-01-24 00:31:58 +01:00
set_uefi( ) {
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"
}
2024-02-01 00:53:00 +01:00
set_net( ) {
case " ${ 1 % : * } " in
"nat" )
network = " ${ network } \
-netdev user,id= ${ 1 #* : } \
-device virtio-net,netdev= ${ 1 #* : } "
; ;
2024-02-01 04:26:14 +01:00
"br" )
2024-02-01 03:24:27 +01:00
if [ " ${ mac } " = = "" ]
then
echo "error, no mac set"
exit 1
fi
2024-03-03 19:26:33 +01:00
if [ " ${ driver } " = = "" ]
then
driver = "virtio-net"
fi
2024-02-01 03:24:27 +01:00
network = " ${ network } \
2024-02-19 18:38:08 +01:00
-netdev bridge,id= ${ 1 #* : } ,br= ${ 1 #* : } \
2024-03-03 19:26:33 +01:00
-device ${ driver } ,netdev= ${ 1 #* : } ,mac= ${ mac } "
2024-02-01 00:53:00 +01:00
; ;
"none" )
echo "no interface will be connected."
network = "-nic none"
; ;
*)
2024-02-01 04:26:14 +01:00
echo " type ${ 1 % : * } not known, it should be either nat, br or none " >& 2
2024-02-01 00:53:00 +01:00
exit 1
; ;
esac
}
2024-01-21 20:11:24 +01:00
2024-01-31 21:45:08 +01:00
# change active directory to became the one is stored vm-start. In case it is impossible, quit.
cd " $( dirname " $( readlink -f " $0 " ) " ) " || exit
2024-01-21 20:11:24 +01:00
2024-01-31 21:45:08 +01:00
# gère les paramètres
2024-01-23 21:05:06 +01:00
for i in " $@ " ; do
2024-01-31 21:45:08 +01:00
case " $i " in
"-h" | "--help" )
HELP
; ;
"-v" | "--version" )
echo " vm-start ${ Version } "
exit 0
; ;
"-l" | "--list" )
list
; ;
esac
2024-01-21 20:11:24 +01:00
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-13 17:50:37 +01:00
2024-01-31 21:45:08 +01:00
# Vérifie 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.
2024-01-21 20:11:24 +01:00
# possibilitée de générer à nouveau le fichier de config avec vm-start nom-vm init
2024-01-24 02:00:49 +01:00
if [ ! -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-24 02:00:49 +01:00
if [ " $2 " = = "init" ]
then
# Exit, it is the end of the program.
exit 0
fi
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
2024-02-05 01:22:52 +01:00
for i in ${ net } ; do
set_net " ${ i } "
done
2024-01-21 20:11:24 +01:00
for ( ( i = 3; i<= $# ; i++) ) ; do
2024-01-23 21:26:08 +01:00
case " ${ !i%=* } " in
2024-02-01 15:36:55 +01:00
"--nproc" )
2024-01-23 21:26:08 +01:00
NPROC = " ${ !i#*= } "
; ;
2024-02-01 15:36:55 +01:00
"--memory" )
2024-01-23 21:26:08 +01:00
MEMORY = " ${ !i#*= } "
; ;
2024-02-01 15:36:55 +01:00
"--size" )
2024-02-01 12:54:27 +01:00
SIZE = " ${ !i#*= } "
; ;
2024-02-01 15:36:55 +01:00
"--uefi" )
2024-01-24 00:31:58 +01:00
set_uefi
2024-01-23 21:26:08 +01:00
; ;
2024-02-01 15:36:55 +01:00
"--create_disk" )
2024-01-24 02:00:49 +01:00
INSTALLATION_DISK_LOCATION = " ${ !i#*= } "
; ;
2024-02-01 15:36:55 +01:00
"--net" )
2024-02-01 00:53:00 +01:00
param = " ${ !i#*= } "
set_net " ${ param } "
; ;
2024-01-23 21:26:08 +01:00
esac
2024-01-21 20:11:24 +01:00
done
2024-01-22 01:32:12 +01:00
if [ " ${ UEFI } " = = "yes" ]
2024-01-21 03:50:10 +01:00
then
2024-01-24 00:31:58 +01:00
set_uefi
2024-01-21 03:50:10 +01:00
fi
2024-01-13 17:50:37 +01:00
2024-01-23 22:19:36 +01:00
case " $2 " in
"start" )
echo " starting VM $1 "
START
; ;
"start-backup" )
echo "starting VM rescue"
START_BACKUP
; ;
"backup" )
echo "backup VM"
BACKUP
; ;
"restore" )
echo "restore backup"
RESTORE
; ;
"create" )
echo "creating VM"
2024-01-24 02:00:49 +01:00
if [ ! " ${ INSTALLATION_DISK_LOCATION } " = = "" ]
2024-01-23 22:19:36 +01:00
then
2024-01-24 02:00:49 +01:00
echo " using image disk ${ INSTALLATION_DISK_LOCATION } "
CREATE " ${ INSTALLATION_DISK_LOCATION } "
2024-01-23 22:19:36 +01:00
else
echo "Error, no image defined." >& 2
2024-02-05 01:36:57 +01:00
echo "use --create_disk=<loc_disk>" >& 2
2024-01-23 22:19:36 +01:00
fi
; ;
"suppr" )
echo " suppressing all data of the vm $1 "
SUPPRESS " $1 "
; ;
2024-02-01 12:51:40 +01:00
"suppr-all" )
echo " suppressing all data and conf file of the vm $1 "
suppress_all " $1 "
; ;
2024-01-24 02:00:49 +01:00
"init" )
echo " Init volume $1 "
2024-03-03 19:26:33 +01:00
#INIT "$1"
echo "Error, Normally, already called before."
; ;
"init_cdrom" )
echo " Init cdrom $1 "
if [ ! " ${ INSTALLATION_DISK_LOCATION } " = = "" ]
then
echo " using image disk ${ INSTALLATION_DISK_LOCATION } "
INIT_cdrom " ${ INSTALLATION_DISK_LOCATION } "
else
echo "Error, no image defined." >& 2
echo "use --create_disk=<loc_disk>" >& 2
fi
2024-01-24 02:00:49 +01:00
; ;
*)
2024-01-23 22:19:36 +01:00
echo " commande $2 inconnue " >& 2
HELP
esac
2024-01-13 17:50:37 +01:00