add a script to connect to the vm by ssh, add uefi option and a few others to vm-start

This commit is contained in:
primardj 2024-01-21 02:50:10 +00:00
parent ba9eed114a
commit 37caf10ca4
2 changed files with 152 additions and 6 deletions

69
ssh-local.sh Executable file
View file

@ -0,0 +1,69 @@
#/bin/bash
#
# little class about bash extension parametters.
#
# ${myvar#pattern} delete shortest occurence from the begining
# ${myvar##pattern} delete longuest occurence from begining
# ${myvar%pattern} delete shortest occurence from the end
# ${myvar%%pattern} delete longuest occurence from the end
#
# conclusion, use of # will delete the pattern at begining
# and % will delete the parameter at the end.
USER="root"
PORT=10022
VERSION="1.0"
HELP_MESSAGE="
ssh-local [OPTION] [OPTION=VALUE]
script which permit to launch ssh to connect to the vm on localhost without letting any traces on this computer.
-u <user>,--user=<user> Select the user, by default root
-p <port>,--port=<port> Select the port, by default 10022
-v, --version print the version and exit
-h, --help print this help and exit
"
# looking for double parametters.
for (( arg=1; arg<$#; arg++)); do
value="$((arg+1))"
if [ "${!arg}" == "-u" ]
then
USER=${!value}
elif [ "${!arg}" == "-p" ]
then
PORT=${!value}
fi
done
# looking for all parametter alone
for i in "$@"; do
if [ "${i%=*}" == "--user" ]
then
USER="${i#*=}"
elif [ "${i%=*}" == "--port" ]
then
PORT="${i#*=}"
elif [ "${i}" == "--version" ] || [ "${i}" == "-v" ]
then
echo "Prog ssh-local v.${VERSION}"
exit 0
elif [ "${i}" == "--help" ] || [ "${i}" == "-h" ]
then
echo "$HELP_MESSAGE"
exit 0
fi
done
echo user=${USER}
echo port=${PORT}
ssh -o "StrictHostKeyChecking no" -o "UserKnownHostsFile=/dev/null" ${USER}@localhost -p ${PORT}

View file

@ -1,11 +1,50 @@
#!/bin/bash #!/bin/bash
# #
#RESULT_POSSIBLE=("init","start","start-backup","create","backup","restore") #RESULT_POSSIBLE=("init","start","start-backup","create","backup","restore","suppr")
VERSION=0.5 VERSION=0.6
MAIN_LOCATION="$HOME/virtual-machine/" USAGE="vm-start NAME COMMAND [PARAMETER] [OPTION]
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
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=<uefi> Select if there will be an UEFI by default.
can be either set or unset. By default is unset.
OPTIONS=<option> additinal options to give to qemu. By default is
-display gtk -vga qxl
"
MAIN_LOCATION="$HOME/virtual_machine/"
OPTION="-display gtk \
-vga qxl"
# other options choices
#
#-display sdl
#-usbdevice tablet \
mkdir -p $MAIN_LOCATION
INIT() { INIT() {
echo "INIT for vm $1" echo "INIT for vm $1"
@ -47,6 +86,7 @@ START() {
-net user$FORWARD,hostname=$VMHOSTNAME \ -net user$FORWARD,hostname=$VMHOSTNAME \
-m $MEMORY \ -m $MEMORY \
-drive file=${MAIN_LOCATION}${LOCATION}${VMHOSTNAME}.cow \ -drive file=${MAIN_LOCATION}${LOCATION}${VMHOSTNAME}.cow \
$OPTION \
-daemonize -daemonize
sleep 5 sleep 5
@ -63,6 +103,7 @@ START-BACKUP() {
-net user$FORWARD,hostname=$VMHOSTNAME \ -net user$FORWARD,hostname=$VMHOSTNAME \
-m $MEMORY \ -m $MEMORY \
-drive file=${MAIN_LOCATION}${LOCATION}${VMHOSTNAME}.cow.backup \ -drive file=${MAIN_LOCATION}${LOCATION}${VMHOSTNAME}.cow.backup \
$OPTION \
-daemonize -daemonize
sleep 5 sleep 5
@ -72,11 +113,27 @@ START-BACKUP() {
CREATE() { CREATE() {
if [ -z ${SIZE+x} ]
then
SIZE="40G"
fi
if [[ ! -f ${MAIN_LOCATION}${LOCATION}${VMHOSTNAME}.cow ]] if [[ ! -f ${MAIN_LOCATION}${LOCATION}${VMHOSTNAME}.cow ]]
then then
echo "ceate 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 40G qemu-img create -f qcow2 ${MAIN_LOCATION}${LOCATION}${VMHOSTNAME}.cow $SIZE
fi
if [ -n ${UEFI+x} ]
then
echo "create UEFIVAR"
if [ ! -f ${MAIN_LOCATION}${LOCATION}OVMF_VARS.4m.fd ]
then
cp /usr/share/edk2/x64/OVMF_VARS.4m.fd ${MAIN_LOCATION}${LOCATION}
else
echo "INFO, UEFIVAR already existing"
fi
fi fi
@ -89,7 +146,8 @@ CREATE() {
-m $MEMORY \ -m $MEMORY \
-drive file=${MAIN_LOCATION}${LOCATION}${VMHOSTNAME}.cow \ -drive file=${MAIN_LOCATION}${LOCATION}${VMHOSTNAME}.cow \
-boot d \ -boot d \
-drive file="$1",media=cdrom -drive file="$1",media=cdrom \
$OPTION
} }
RESTORE() { RESTORE() {
@ -113,10 +171,19 @@ BACKUP() {
fi fi
} }
SUPPRESS() {
echo "Are you sure you want to delete all data relative to the vm $1"
read entry
if [[ $entry =~ ^[yY]$ ]]
then
rm -Ri ${MAIN_LOCATION}${LOCATION}
fi
}
if [ $# -lt 2 ] if [ $# -lt 2 ]
then then
echo "Error, not enough args" echo "Error, not enough args"
echo "${USAGE}"
exit 1 exit 1
fi fi
@ -128,6 +195,12 @@ then
fi fi
source conf/$1-vm_var.sh source conf/$1-vm_var.sh
if [ -n "${UEFI+x}" ]
then
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"
fi
if [ "$2" == "start" ] if [ "$2" == "start" ]
@ -156,6 +229,10 @@ then
else else
echo "Error, no image defined." echo "Error, no image defined."
fi fi
elif [[ $2 == "suppr" ]]
then
echo "suppressing all data of the vm $1"
SUPPRESS
elif [[ "$2" == "version" ]] elif [[ "$2" == "version" ]]
then then
echo "script lancement vm version v.$VERSION" echo "script lancement vm version v.$VERSION"