root-wrap, checkvm, stop, stopall and several fixes

This commit is contained in:
mad 2018-08-18 11:47:47 +02:00
parent b4b0a1032a
commit 66e950ea54
2 changed files with 61 additions and 22 deletions

64
nlvmi
View File

@ -35,6 +35,15 @@ echo "sqlite ready to use";
return 0
}
#check if we are root and wrap if we are not
U=`/usr/bin/whoami`
if [ $U != "root" ]; then
if [ ! -z $1 ]; then A=$1; else A="bla"; fi
if [ ! -z $2 ]; then B=$2; else B="bla"; fi
if [ ! -z $3 ]; then C=$3; else C="bla"; fi
/usr/bin/wrap-nlvmi $A $B $C && exit
fi
#creating database
function createdb {
if [ $LOGLEVEL -gt "1" ]; then echo "going to create the db"; fi
@ -95,6 +104,7 @@ function vmautostart {
fi
}
#start single VM
function vmstart {
if [ -z $1 ]; then echo "function start needs an id!"; exit 1; fi
if [ $LOGLEVEL -gt "1" ]; then echo "startvm function entered for vmid $1"; fi
@ -112,19 +122,17 @@ function vmstart {
if [ ! -z ${array[17]} ]; then DRIVE3="-drive file=${array[17]},format=raw,if=virtio,aio=native"; fi
if [ ! -z ${array[18]} ]; then FORMAT3=",format=${array[18]},if=virtio"; fi
if [ ! -z ${array[19]} ]; then CDROM="-cdrom ${array[19]}"; fi
if [ ! -z ${array[20]} ]; then TAPDEV1="-netdev tap,ifname=${array[20]},script=no,id=net0"; fi
if [ ! -z ${array[20]} ]; then TAPDEV1="-netdev tap,ifname=${array[20]},script=no,id=net0"; tunctl -t ${array[20]} -u ${array[2]} && ifconfig ${array[20]} up; fi
if [ ! -z ${array[21]} ]; then MACADDR1="-net nic,macaddr=${array[21]},model=virtio,netdev=net0"; fi
if [ ! -z ${array[22]} ]; then BRDEV1="${array[22]}"; fi
if [ ! -z ${array[23]} ]; then TAPDEV2="-netdev tap,ifname=${array[23]},script=no,id=net0"; fi
if [ ! -z ${array[22]} ]; then BRDEV1="${array[22]}"; brctl addif ${array[22]} ${array[20]}; fi
if [ ! -z ${array[23]} ]; then TAPDEV2="-netdev tap,ifname=${array[23]},script=no,id=net0"; tunctl -t ${array[23]} -u ${array[2]} && ifconfig ${array[23]} up; fi
if [ ! -z ${array[24]} ]; then MACADDR2="-net nic,macaddr=${array[24]},model=virtio,netdev=net0"; fi
if [ ! -z ${array[25]} ]; then BRDEV2="${array[25]}"; fi
if [ ! -z ${array[26]} ]; then TAPDEV3="-netdev tap,ifname=${array[26]},script=no,id=net0"; fi
if [ ! -z ${array[25]} ]; then BRDEV2="${array[25]}"; brctl addif ${array[25]} ${array[23]}; fi
if [ ! -z ${array[26]} ]; then TAPDEV3="-netdev tap,ifname=${array[26]},script=no,id=net0"; tunctl -t ${array[26]} -u ${array[2]} && ifconfig ${array[26]} up; fi
if [ ! -z ${array[27]} ]; then MACADDR3="-net nic,macaddr=${array[27]},model=virtio,netdev=net0"; fi
if [ ! -z ${array[28]} ]; then BRDEV3="${array[28]}"; fi
if [ ! -z ${array[29]} ]; then VNCPORT="-vnc :${array[29]},websocket=${array[30]},password"; fi
tunctl -t ${array[20]} -u ${array[2]}
brctl addif ${array[22]} ${array[20]}
ifconfig ${array[20]} up
if [ ! -z ${array[28]} ]; then BRDEV3="${array[28]}"; brctl addif ${array[28]} ${array[26]}; fi
if [ ! -z ${array[30]} ]; then WEBSOCK=",websocket=${array[30]}"; else WEBSOCK=""; fi
if [ ! -z ${array[29]} ]; then VNCPORT="-vnc :${array[29]}$WEBSOCK,password"; fi
COMMAND=(su ${array[2]} -c "${array[4]} -enable-kvm $CPUTYPE $MEMORY $SMP -boot ${array[11]} $USBDEV -k ${array[9]} -daemonize $DRIVE1$FORMAT1 $DRIVE2$FORMAT2 $DRIVE3$FORMAT3 $CDROM $MACADDR1 $TAPDEV1 $MACADDR2 $TAPDEV2 $MACADDR3 $TAPDEV3 $VNCPORT $CUSTOM -pidfile $RUNDIRECTORY/${array[1]}.pid -monitor unix:$RUNDIRECTORY/${array[1]}.mon,server,nowait")
if [ $LOGLEVEL -gt "1" ]; then echo ${COMMAND[@]}; fi
`"${COMMAND[@]}"`
@ -134,26 +142,53 @@ function vmstart {
export VMFOUND="yes"
}
#stop VM
function vmstop {
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
sqlite3 $SQLITEFILE "SELECT * FROM vms WHERE vmname='$1'" | while read line; do
sqlite3 $SQLITEFILE "SELECT id,vmname FROM vms WHERE vmname='$1'" | while read line; do
IFS='|' read -r -a array <<< "$line"
echo "system_powerdown" | socat - unix-connect:$RUNDIRECTORY/${array[1]}.mon >/dev/null
sleep 5s
STOPPED="no"
COUNTER=0
while [ $STOPPED = "no" ]; do
ps -ef | grep `cat $RUNDIRECTORY/${array[1]}.pid` | grep -v grep >/dev/null || STOPPED="yes"
if [ $COUNTER==100 ]; then
kill `cat $RUNDIRECTORY/${array[1]}.pid`
echo "${array[1]} forcefully killed!";
STOPPED="yes"
fi
((COUNTER++))
ps -ef | grep `cat $RUNDIRECTORY/${array[1]}.pid` | grep -v grep >/dev/null || STOPPED="yes"
sleep 1s
done
done
}
#stopall
function stopall {
sqlite3 $SQLITEFILE "SELECT id,vmname FROM vms" | while read line; do
IFS='|' read -r -a array <<< "$line"
ps -ef | grep `cat $RUNDIRECTORY/${array[1]}.pid` | grep -v grep >/dev/null && (vmstop ${array[1]} &)
done
}
#check if VM is running
function checkvm {
if [ -z $1 ]; then echo "checkvm needs one argument!"; exit 1; fi
ps -ef | grep `cat $RUNDIRECTORY/$1.pid` | grep -v grep >/dev/null && echo "VM is running" || echo "VM is not running"
}
#mainloop
if [ $# -gt 0 ]; then
if [ $1 == "createdb" ]; then
createdb
elif [ $1 == "autostart" ]; then
vmautostart
elif [ $1 == "checkvm" ]; then
if [ -z $2 ]; then echo "checkvm needs two arguments"; fi
checkvm $2
elif [ $1 == "start" ]; then
if [ -z $2 ]; then echo "start needs a vmname!"; exit 1; fi
VMFOUND="no"
@ -173,9 +208,12 @@ if [ $# -gt 0 ]; then
IFS='|' read -r -a arrays <<< "$line"
declare VMFOUND="yes"
export "$VMFOUND"
echo "nana"
vmstop $2
done
if [[ $VMFOUND == "no" ]]; then echo "no VM with that name found!"; exit 1; fi)
elif [ $1 == "stopall" ]; then
stopall
elif [ $1 == "checkvm" ]; then
if [ -z $2 ]; then echo "checkvm needs a vmname!"; exit 1; fi
if [ -e $RUNDIRECTORY/$2.mon ]; then echo $2 running; else echo $2 stopped; fi

View File

@ -16,11 +16,12 @@ if(isset($_REQUEST['logout'])){
$mode="";
function checkvm($vmname) {
$shellout = shell_exec("/usr/bin/wrap-nlvmi checkvm $vmname bla");//){
if (preg_match_all('/running/', $shellout)){
return 0;
$shellout = shell_exec("/usr/bin/nlvmi checkvm $vmname bla");//){
// echo $shellout;
if (preg_match_all('/not running/', $shellout)){
return 1;
}
return 1;
return 0;
}
//read config file
@ -165,7 +166,7 @@ if (isset($_SESSION['username'])){
}
}
//ajax queries are all don, time for the header
//ajax queries are all done, time for the header
include('header.php');
@ -216,7 +217,7 @@ if (isset($_SESSION['username'])){
$sql="SELECT * FROM vms WHERE vmname='$vmname' $sqllimit";
$res = $db_handle->query($sql);
while ($row = $res->fetchArray(SQLITE3_ASSOC)){
$shellout = shell_exec("/usr/bin/wrap-nlvmi start $vmname bla");
$shellout = shell_exec("/usr/bin/nlvmi start $vmname bla");
echo $shellout;
}
}
@ -229,8 +230,8 @@ if (isset($_SESSION['username'])){
$sql="SELECT * FROM vms WHERE vmname='$vmname' $sqllimit";
$res = $db_handle->query($sql);
while ($row = $res->fetchArray(SQLITE3_ASSOC)){
if(checkvm($vmname)){
$shellout = shell_exec("/usr/bin/wrap-nlvmi stop $vmname bla");
if(!checkvm($vmname)){
$shellout = shell_exec("/usr/bin/nlvmi stop $vmname bla");
echo $shellout;
}
}
@ -451,7 +452,7 @@ if (isset($_SESSION['username'])){
echo "<button class=\"btn btn-danger\" data-delete-text=\"Delete VM $row[vmname]!\" data-delete-vm=\"$row[id]\" data-delete-name=\"$row[vmname]\" data-toggle=\"modal\" data-target=\"#confirm-delete\">Delete VM</button> ";
$server = gethostname();
if (preg_match('/stop/', $button)) {
echo "<a target=_blank href=novnc/vnc.html?port=$row[websocket]&path=&host=$server class=\"btn btn-success\">VNC</a></div>";
echo "<a target=_blank href=novnc/vnc.html?port=$row[websocket]&path=&host=$server class=\"btn btn-success\">VNC</a>";
}
echo "</div>";
}