start stop prototype

This commit is contained in:
Michael Keist 2018-08-12 18:21:58 +02:00
parent 23a5af48ae
commit e030458aa4
2 changed files with 43 additions and 109 deletions

118
nlvmi
View File

@ -5,7 +5,12 @@ source /etc/nlvmi/nlvmi.conf
if [ $LOGLEVEL -gt "1" ]; then echo "configuration loaded"; fi if [ $LOGLEVEL -gt "1" ]; then echo "configuration loaded"; fi
#checking directories #checking directories
if [ ! -d $VMDIRECTORY ]; then echo "VMDIRECTORY wrong, $VMDIRECTORY does not exist"; exit 1; fi echo $VMDIRECTORY | while read line; do IFS='|' read -r -a array
for dir in "${array[@]}"; do
if [ ! -d $dir ]; then echo "VMDIRECTORY wrong, $dir does not exist"; exit 1; fi
done
done <<< "$line"
if [ ! -d $RUNDIRECTORY ]; then if [ $LOGLEVEL -gt "1" ]; then echo "creating $RUNDIRECTORY"; fi; mkdir -p $RUNDIRECTORY; fi if [ ! -d $RUNDIRECTORY ]; then if [ $LOGLEVEL -gt "1" ]; then echo "creating $RUNDIRECTORY"; fi; mkdir -p $RUNDIRECTORY; fi
#checking database #checking database
@ -92,91 +97,42 @@ function vmstart {
if [ $LOGLEVEL -gt "1" ]; then echo "startvm function entered for vmid $1"; fi if [ $LOGLEVEL -gt "1" ]; then echo "startvm function entered for vmid $1"; fi
sqlite3 $SQLITEFILE "SELECT * FROM vms WHERE id=$1" | while read line; do sqlite3 $SQLITEFILE "SELECT * FROM vms WHERE id=$1" | while read line; do
IFS='|' read -r -a array <<< "$line" IFS='|' read -r -a array <<< "$line"
if [ ${array[6]}=="" ]; then if [ ! -z ${array[6]} ]; then DRIVE1="-drive file=${array[6]},cache=none,format=raw,if=virtio,aio=native"; fi
DRIVE1=""; fi if [ ! -z ${array[7]} ]; then DRIVE2="-drive file=${array[7]},cache=none,format=raw,if=virtio,aio=native"; fi
if [ ${array[6]}!="" ]; then if [ ! -z ${array[8]} ]; then DRIVE3="-drive file=${array[8]},cache=none,format=raw,if=virtio,aio=native"; fi
DRIVE1="-drive file=${array[6]},cache=none,format=raw,if=virtio,aio=native"; fi if [ ! -z ${array[9]} ]; then CDROM="-cdrom ${array[9]}"; fi
if [ ${array[7]}=="" ]; then if [ ! -z ${array[11]} ]; then NETDEV1="-net nic,macaddr=${array[11]},model=virtio,netdev=net0"; fi
DRIVE2=""; fi if [ ! -z ${array[15]} ]; then TAPDEV1="-netdev tap,ifname=${array[15]},script=no,id=net0"; fi
if [ ${array[7]}!="" ]; then if [ ! -z ${array[16]} ]; then TAPDEV2="-netdev tap,ifname=${array[16]},script=no,id=net0"; fi
DRIVE2="-drive file=${array[7]},cache=none,format=raw,if=virtio,aio=native"; fi if [ ! -z ${array[17]} ]; then TAPDEV3="-netdev tap,ifname=${array[17]},script=no,id=net0"; fi
if [ ${array[8]}=="" ]; then if [ ! -z ${array[18]} ]; then VNCPORT="-vnc ${array[18]},websocket=${array[19]},password"; fi
DRIVE3=""; fi if [ ! -z ${array[21]} ]; then USBDEV1="-usb -device usb-ehci,id=ehci -device usb-${array[21]},bus=usb-bus.0"; fi
if [ ${array[8]}!="" ]; then if [ ! -z ${array[28]} ]; then CPUTYPE="-cpu ${array[28]}"; fi
DRIVE3="-drive file=${array[8]},cache=none,format=raw,if=virtio,aio=native"; fi if [ ! -z ${array[26]} ]; then SMP="-smp ${array[26]}"; fi
if [ ${array[9]}=="" ]; then if [ ! -z ${array[25]} ]; then MEMORY="-m ${array[25]}"; fi
CDROM=""; fi if [ ! -z ${array[27]} ]; then CUSTOM="${array[27]}"; fi
if [ ${array[9]}!="" ]; then # tunctl -t $TAPDEV1 -u ${array[3]}
CDROM="-cdrom file=${array[9]}"; fi # brctl addif br0 $TAPDEV1
if [ ${array[11]}=="" ]; then # ifconfig $TAPDEV1 up
NETDEV1=""; fi COMMAND=(su ${array[3]} -c "${array[10]} -enable-kvm $CPUTYPE $MEMORY $SMP -boot ${array[5]} $USBDEV1 -k ${array[24]} -daemonize $DRIVE1 $DRIVE2 $DRIVE3 $CDROM $NETDEV1 $TAPDEV1 $TAPDEV2 $TAPDEV3 $VNCPORT $CUSTOM -pidfile $RUNDIRECTORY/${array[1]}.pid -monitor unix:$RUNDIRECTORY/${array[1]}.mon,server,nowait")
if [ ${array[11]}!="" ]; then if [ $LOGLEVEL -gt "1" ]; then echo ${COMMAND[@]}; fi
NETDEV1="-net nic,macaddr=${array[11]},model=virtio,netdev=net0"; fi `"${COMMAND[@]}"`
if [ ${array[15]}=="" ]; then
TAPDEV1=""; fi
if [ ${array[15]}!="" ]; then
TAPDEV1="-netdev tap,ifname=${array[15]},script=no,id=net0"; fi
if [ ${array[16]}=="" ]; then
TAPDEV2=""; fi
if [ ${array[16]}!="" ]; then
TAPDEV2="-netdev tap,ifname=${array[16]},script=no,id=net0"; fi
if [ ${array[17]}=="" ]; then
TAPDEV3=""; fi
if [ ${array[17]}!="" ]; then
TAPDEV3="-netdev tap,ifname=${array[17]},script=no,id=net0"; fi
if [ ${array[18]}=="" ]; then
VNCPORT=""; fi
if [ ${array[18]}!="" ]; then
VNCPORT="-vnc ${array[18]},websocket=${array[19]},password"; fi
if [ ${array[21]}=="" ]; then
USBDEV1=""; fi
if [ ${array[21]}!="" ]; then
USBDEV1="-usbdevice ${array[21]}"; fi
if [ ${array[22]}=="" ]; then
USBDEV2=""; fi
if [ ${array[22]}!="" ]; then
USBDEV2="-usbdevice ${array[22]}"; fi
if [ ${array[23]}=="" ]; then
USBDEV3=""; fi
if [ ${array[23]}!="" ]; then
USBDEV3="-usbdevice ${array[22]}"; fi
if [ ${array[28]}=="" ]; then
CPUTYPE=""; fi
if [ ${array[28]}!="" ]; then
CPUTYPE="-cpu ${array[28]}"; fi
if [ ${array[26]}=="" ]; then
SMP=""; fi
if [ ${array[26]}!="" ]; then
SMP="-smp ${array[26]}"; fi
if [ ${array[25]}=="" ]; then
MEMORY=""; fi
if [ ${array[25]}!="" ]; then
MEMORY="-m ${array[25]}"; fi
if [ ${array[27]}=="" ]; then
CUSTOM=""; fi
if [ ${array[27]}!="" ]; then
CUSTOM="${array[27]}"; fi
tunctl -t $TAPDEV1 -u ${array[3]}
brctl addif br0 $TAPDEV1
ifconfig $TAPDEV1 up
su ${array[3]} -c \"${array[10]} -enable-kvm $CPUTYPE $MEMORY $SMP -boot ${array[5]} $USBDEV1 $USBDEV2 $USBDEV3 -k ${array[24]} -daemonize $DRIVE1 $DRIVE2 $DRIVE3 $CDROM $NETDEV1 $TAPDEV1 $TAPDEV2 $TAPDEV3 $VNCPORT $CUSTOM -pidfile $RUNDIRECTORY/${array[1]}.pid -monitor unix:$RUNDIRECTORY/${array[1]}.mon,server,nowait\"
#Set VNC Password #Set VNC Password
echo "change vnc password ${array[20]}" | socat - unix-connect:$RUNDIRECTORY/${array[1]}.mon >/dev/null # echo "change vnc password ${array[20]}" | socat - unix-connect:$RUNDIRECTORY/${array[1]}.mon >/dev/null
done done
export VMFOUND="yes" export VMFOUND="yes"
} }
function vmstop { function vmstop {
if [ -e $RUNDIRDIRECTORY/$1.pid ]; then echo "pidfile not exist"; exit 1; fi if [ ! -e $RUNDIRECTORY/$1.pid ]; then echo "pidfile $RUNDIRECTORY/$1.pid does not exist"; exit 1; fi
if [ $LOGLEVEL -gt "1" ]; then echo "stopvm function entered for VM $1"; fi if [ $LOGLEVEL -gt "1" ]; then echo "stopvm function entered for VM $1"; fi
sqlite3 $SQLITEFILE "SELECT * FROM vms WHERE vmname=$1" | while read line; do sqlite3 $SQLITEFILE "SELECT * FROM vms WHERE vmname='$1'" | while read line; do
IFS='|' read -r -a array <<< "$line" IFS='|' read -r -a array <<< "$line"
echo "system_powerdown" | socat - unix-connect:$RUNDIRDIRECTORY/${array[1]}.mon >/dev/null echo "system_powerdown" | socat - unix-connect:$RUNDIRECTORY/${array[1]}.mon >/dev/null
STOPPED="no" STOPPED="no"
while [ $STOPPED = "no" ]; do while [ $STOPPED = "no" ]; do
ps -ef | grep `cat $RUNDIRDIRECTORY/${array[1]}.pid` | grep -v grep >/dev/null || STOPPED="yes" ps -ef | grep `cat $RUNDIRECTORY/${array[1]}.pid` | grep -v grep >/dev/null || STOPPED="yes"
done done
done done
} }
@ -198,6 +154,18 @@ if [ $# -gt 0 ]; then
vmstart $arrays vmstart $arrays
done done
if [[ $VMFOUND == "no" ]]; then echo "no VM with that name found!"; exit 1; fi) if [[ $VMFOUND == "no" ]]; then echo "no VM with that name found!"; exit 1; fi)
elif [ $1 == "stop" ]; then
echo $1 $2
if [ -z $2 ]; then echo "stop needs a vmname!"; exit 1; fi
VMFOUND="no"
if [ $LOGLEVEL -gt "1" ]; then echo "searching for vmname $2"; fi
sqlite3 $SQLITEFILE "SELECT id FROM vms WHERE vmname='$2'" | (while read line; do
IFS='|' read -r -a arrays <<< "$line"
declare VMFOUND="yes"
export "$VMFOUND"
vmstop $2
done
if [[ $VMFOUND == "no" ]]; then echo "no VM with that name found!"; exit 1; fi)
fi fi
else else

View File

@ -1,34 +0,0 @@
##########
# This is the configuration file for the
# NativenetLightweightVMInterface
# basic settings go in here
# GPLv3
#Loglovel 1-3, 3 is debug
LOGLEVEL="3"
#Where your images are stored like qcow2
VMDIRECTORY="/kvm"
#Where your iso images are stored
ISODIRECTORY="/kvm/iso"
#Where pids and monitor sockets are getting stored
RUNDIRECTORY="/run/nlvmi"
#which database mariadb or sqlite
DATABASETYPE="sqlite"
#only sqlite settings
SQLITEFILE="/etc/nlvmi/nlvmi.sqlite"
#The unix user that runs php, to grand permissions on the sqlite file
PHPUSER="nobody"
#only mariadb settings
MARIAHOST="localhost"
MARIADB="nlvmi"
MARIAUSER="root"
MARIAPASS=""