From ba9eed114a57e9361fb101507c19ebc2c2f4eeac Mon Sep 17 00:00:00 2001 From: primardj Date: Sat, 13 Jan 2024 16:50:37 +0000 Subject: [PATCH] a simple vm launcher --- vm-start.sh | 165 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 165 insertions(+) create mode 100755 vm-start.sh diff --git a/vm-start.sh b/vm-start.sh new file mode 100755 index 0000000..3dc4c1d --- /dev/null +++ b/vm-start.sh @@ -0,0 +1,165 @@ +#!/bin/bash +# + +#RESULT_POSSIBLE=("init","start","start-backup","create","backup","restore") + +VERSION=0.5 +MAIN_LOCATION="$HOME/virtual-machine/" + + +INIT() { + echo "INIT for vm $1" + NAME=$1 + echo "How many proccessor will you use ? " + read NPROC + echo "How much memory will you need ? " + read MEMORY + + echo "Do you want to forward ssh port (y/n) " + read entry + + if [[ $entry =~ ^[yY]$ ]] + then + FORWARD=",hostfwd=tcp::10022-:22" # forward VM 22 port to Host 10022 + else + FORWARD="" + fi + + mkdir -p conf + echo " +NAME=\"$NAME\" +VMHOSTNAME=\"\$NAME-vm\" +NPROC=$NPROC +MEMORY=\"$MEMORY\" +FORWARD=\"$FORWARD\" +LOCATION=\"\$NAME-sandbox/\" +" > conf/$NAME-vm_var.sh + chmod +x conf/$NAME-vm_var.sh + +} + +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 \ + -daemonize + + sleep 5 + + ssh -o "StrictHostKeyChecking no" -o "UserKnownHostsFile=/dev/null" primardj@localhost -p 10022 +} + +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 \ + -daemonize + + sleep 5 + + #ssh -o "StrictHostKeyChecking no" -o "UserKnownHostsFile=/dev/null" primardj@localhost -p 10022 +} + +CREATE() { + + if [[ ! -f ${MAIN_LOCATION}${LOCATION}${VMHOSTNAME}.cow ]] + then + echo "ceate directory ${MAIN_LOCATION}${LOCATION}" + mkdir -p ${MAIN_LOCATION}${LOCATION} + qemu-img create -f qcow2 ${MAIN_LOCATION}${LOCATION}${VMHOSTNAME}.cow 40G + 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 \ + -boot d \ + -drive file="$1",media=cdrom +} + +RESTORE() { + echo "Are you sure you want to restore the rescue? (y/N)" + read entry + if [[ $entry =~ ^[yY]$ ]] + then + cp ${MAIN_LOCATION}${LOCATION}${VMHOSTNAME}.cow.backup ${MAIN_LOCATION}${LOCATION}${VMHOSTNAME}.cow + echo "Done" + fi +} + + +BACKUP() { + echo "Are you sure you want to backup the volume? If you continue the previous backup will be lost (y/N)" + read entry + if [[ $entry =~ ^[yY]$ ]] + then + cp ${MAIN_LOCATION}${LOCATION}${VMHOSTNAME}.cow ${MAIN_LOCATION}${LOCATION}${VMHOSTNAME}.cow.backup + echo "Done" + fi +} + + +if [ $# -lt 2 ] +then + echo "Error, not enough args" + exit 1 +fi + +cd "$( dirname "$( readlink -f "$0" )" )" + +if [[ ! -f conf/$1-vm_var.sh ]] +then + INIT $1 +fi + +source conf/$1-vm_var.sh + + +if [ "$2" == "start" ] +then + echo "starting VM $1" + START +elif [ "$2" == "start-backup" ] +then + echo "starting VM rescue" + START-BACKUP +elif [ "$2" == "backup" ] +then + echo "backup VM" + BACKUP +elif [ "$2" == "restore" ] +then + echo "restore VM" + RESTORE +elif [ "$2" == "create" ] +then + echo "creating VM" + if [[ $# == 3 ]] + then + echo "using image disk $3" + CREATE $3 + else + echo "Error, no image defined." + fi +elif [[ "$2" == "version" ]] +then + echo "script lancement vm version v.$VERSION" +else + echo "commande $2 inconnue" +fi +