Compare commits

..

2 Commits

Author SHA1 Message Date
nativemad 8498ada9bf Merge branch 'master' of https://git.nativenet.ch/nativenet.ch/nlvmi 2018-11-12 16:19:35 +01:00
nativemad 31f872e6f9 tabs 2018-11-12 16:19:19 +01:00
1 changed files with 60 additions and 60 deletions

120
nlvmi
View File

@ -44,43 +44,43 @@ function createdb {
if [ $DATABASETYPE == "sqlite" ]; then if [ $DATABASETYPE == "sqlite" ]; then
sqlite3 $SQLITEFILE "CREATE TABLE vms ( sqlite3 $SQLITEFILE "CREATE TABLE vms (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
vmname VARCHAR(50) NOT NULL, vmname VARCHAR(50) NOT NULL,
user VARCHAR(50) NOT NULL, user VARCHAR(50) NOT NULL,
server VARCHAR(50), server VARCHAR(50),
arch VARCHAR(50) NOT NULL, arch VARCHAR(50) NOT NULL,
cputype VARCHAR(50), cputype VARCHAR(50),
cpus INTEGER, cpus INTEGER,
memory INTEGER, memory INTEGER,
usbdev VARCHAR(50), usbdev VARCHAR(50),
kblang VARCHAR(50), kblang VARCHAR(50),
custom VARCHAR(50), custom VARCHAR(50),
bootoption VARCHAR(2) NOT NULL, bootoption VARCHAR(2) NOT NULL,
autostart INTEGER, autostart INTEGER,
drive1 VARCHAR(50), drive1 VARCHAR(50),
format1 VARCHAR(50), format1 VARCHAR(50),
drive2 VARCHAR(50), drive2 VARCHAR(50),
format2 VARCHAR(50), format2 VARCHAR(50),
drive3 VARCHAR(50), drive3 VARCHAR(50),
format3 VARCHAR(50), format3 VARCHAR(50),
cdrom VARCHAR(50), cdrom VARCHAR(50),
tapdev1 VARCHAR(50), tapdev1 VARCHAR(50),
macaddr1 VARCHAR(50), macaddr1 VARCHAR(50),
brdev1 VARCHAR(50), brdev1 VARCHAR(50),
tapdev2 VARCHAR(50), tapdev2 VARCHAR(50),
macaddr2 VARCHAR(50), macaddr2 VARCHAR(50),
brdev2 VARCHAR(50), brdev2 VARCHAR(50),
tapdev3 VARCHAR(50), tapdev3 VARCHAR(50),
macaddr3 VARCHAR(50), macaddr3 VARCHAR(50),
brdev3 VARCHAR(50), brdev3 VARCHAR(50),
vncport INTEGER, vncport INTEGER,
websocket INTEGER, websocket INTEGER,
vncpassword VARCHAR(50), vncpassword VARCHAR(50),
prio INTEGER prio INTEGER
);" );"
sqlite3 $SQLITEFILE "CREATE TABLE users ( sqlite3 $SQLITEFILE "CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
username VARCHAR(50) NOT NULL, username VARCHAR(50) NOT NULL,
password VARCHAR(255), password VARCHAR(255),
admin INTEGER admin INTEGER
);" );"
sqlite3 $SQLITEFILE "CREATE TABLE servers ( sqlite3 $SQLITEFILE "CREATE TABLE servers (
@ -148,32 +148,32 @@ function vmstart {
echo "if ! \`id -u ${array[2]} &>/dev/null\`; then useradd ${array[2]} -d /run/nlvmi -g nlvmi -M -s /bin/false -G kvm; fi" >>$TMPF echo "if ! \`id -u ${array[2]} &>/dev/null\`; then useradd ${array[2]} -d /run/nlvmi -g nlvmi -M -s /bin/false -G kvm; fi" >>$TMPF
#chown mon and pid files #chown mon and pid files
echo "if [ -e $RUNDIRECTORY/${array[1]}.mon ] || [ -e $RUNDIRECTORY/${array[1]}.pid ]; then chown ${array[2]}:nlvmi $RUNDIRECTORY/${array[1]}.*; fi" >>$TMPF echo "if [ -e $RUNDIRECTORY/${array[1]}.mon ] || [ -e $RUNDIRECTORY/${array[1]}.pid ]; then chown ${array[2]}:nlvmi $RUNDIRECTORY/${array[1]}.*; fi" >>$TMPF
if [ ! -z ${array[5]} ]; then CPUTYPE="-cpu ${array[5]}"; fi if [ ! -z ${array[5]} ]; then CPUTYPE="-cpu ${array[5]}"; fi
if [ ! -z ${array[6]} ]; then SMP="-smp ${array[6]}"; fi if [ ! -z ${array[6]} ]; then SMP="-smp ${array[6]}"; fi
if [ ! -z ${array[7]} ]; then MEMORY="-m ${array[7]}"; fi if [ ! -z ${array[7]} ]; then MEMORY="-m ${array[7]}"; fi
if [ ! -z ${array[8]} ]; then USBDEV="-usb -device usb-ehci,id=ehci -device usb-${array[8]},bus=usb-bus.0"; fi if [ ! -z ${array[8]} ]; then USBDEV="-usb -device usb-ehci,id=ehci -device usb-${array[8]},bus=usb-bus.0"; fi
if [ ! -z ${array[9]} ]; then KB="-k ${array[9]}"; fi if [ ! -z ${array[9]} ]; then KB="-k ${array[9]}"; fi
if [ ! -z ${array[10]} ]; then CUSTOM="${array[10]}"; fi if [ ! -z ${array[10]} ]; then CUSTOM="${array[10]}"; fi
if [ ! -z ${array[13]} ]; then DRIVE1="-drive file=${array[13]}"; echo "chown ${array[2]}:nlvmi ${array[13]}" >>$TMPF; fi if [ ! -z ${array[13]} ]; then DRIVE1="-drive file=${array[13]}"; echo "chown ${array[2]}:nlvmi ${array[13]}" >>$TMPF; fi
if [ ! -z ${array[14]} ]; then FORMAT1=",format=${array[14]},if=virtio"; fi if [ ! -z ${array[14]} ]; then FORMAT1=",format=${array[14]},if=virtio"; fi
if [ ! -z ${array[15]} ]; then DRIVE2="-drive file=${array[15]}"; echo "chown ${array[2]}:nlvmi ${array[15]}" >>$TMPF; fi if [ ! -z ${array[15]} ]; then DRIVE2="-drive file=${array[15]}"; echo "chown ${array[2]}:nlvmi ${array[15]}" >>$TMPF; fi
if [ ! -z ${array[16]} ]; then FORMAT2=",format=${array[16]},if=virtio"; fi if [ ! -z ${array[16]} ]; then FORMAT2=",format=${array[16]},if=virtio"; fi
if [ ! -z ${array[17]} ]; then DRIVE3="-drive file=${array[17]}"; echo "chown ${array[2]}:nlvmi ${array[17]}" >>$TMPF; fi if [ ! -z ${array[17]} ]; then DRIVE3="-drive file=${array[17]}"; echo "chown ${array[2]}:nlvmi ${array[17]}" >>$TMPF; fi
if [ ! -z ${array[18]} ]; then FORMAT3=",format=${array[18]},if=virtio"; 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[19]} ]; then CDROM="-cdrom ${array[19]}"; fi
if [ ! -z ${array[20]} ]; then TAPDEV1="-netdev tap,ifname=${array[20]},script=no,id=net0"; echo "tunctl -t ${array[20]} -u ${array[2]} && ifconfig ${array[20]} up" >>$TMPF; fi if [ ! -z ${array[20]} ]; then TAPDEV1="-netdev tap,ifname=${array[20]},script=no,id=net0"; echo "tunctl -t ${array[20]} -u ${array[2]} && ifconfig ${array[20]} up" >>$TMPF; fi
if [ ! -z ${array[21]} ]; then MACADDR1="-net nic,macaddr=${array[21]},model=virtio,netdev=net0"; 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]}"; echo "/sbin/brctl addif ${array[22]} ${array[20]}" >>$TMPF; fi if [ ! -z ${array[22]} ]; then BRDEV1="${array[22]}"; echo "/sbin/brctl addif ${array[22]} ${array[20]}" >>$TMPF; fi
if [ ! -z ${array[23]} ]; then TAPDEV2="-netdev tap,ifname=${array[23]},script=no,id=net1"; echo "tunctl -t ${array[23]} -u ${array[2]} && ifconfig ${array[23]} up" >>$TMPF; fi if [ ! -z ${array[23]} ]; then TAPDEV2="-netdev tap,ifname=${array[23]},script=no,id=net1"; echo "tunctl -t ${array[23]} -u ${array[2]} && ifconfig ${array[23]} up" >>$TMPF; fi
if [ ! -z ${array[24]} ]; then MACADDR2="-net nic,macaddr=${array[24]},model=virtio,netdev=net1"; fi if [ ! -z ${array[24]} ]; then MACADDR2="-net nic,macaddr=${array[24]},model=virtio,netdev=net1"; fi
if [ ! -z ${array[25]} ]; then BRDEV2="${array[25]}"; echo "/sbin/brctl addif ${array[25]} ${array[23]}" >>$TMPF; fi if [ ! -z ${array[25]} ]; then BRDEV2="${array[25]}"; echo "/sbin/brctl addif ${array[25]} ${array[23]}" >>$TMPF; fi
if [ ! -z ${array[26]} ]; then TAPDEV3="-netdev tap,ifname=${array[26]},script=no,id=net2"; echo "tunctl -t ${array[26]} -u ${array[2]} && ifconfig ${array[26]} up" >>$TMPF; fi if [ ! -z ${array[26]} ]; then TAPDEV3="-netdev tap,ifname=${array[26]},script=no,id=net2"; echo "tunctl -t ${array[26]} -u ${array[2]} && ifconfig ${array[26]} up" >>$TMPF; fi
if [ ! -z ${array[27]} ]; then MACADDR3="-net nic,macaddr=${array[27]},model=virtio,netdev=net2"; fi if [ ! -z ${array[27]} ]; then MACADDR3="-net nic,macaddr=${array[27]},model=virtio,netdev=net2"; fi
if [ ! -z ${array[28]} ]; then BRDEV3="${array[28]}"; echo "/sbin/brctl addif ${array[28]} ${array[26]}" >>$TMPF; fi if [ ! -z ${array[28]} ]; then BRDEV3="${array[28]}"; echo "/sbin/brctl addif ${array[28]} ${array[26]}" >>$TMPF; fi
if [ ! -z ${array[30]} ]; then WPORT=$((${array[30]} + 5700)); WEBSOCK=",websocket=$WPORT"; else WEBSOCK=""; fi if [ ! -z ${array[30]} ]; then WPORT=$((${array[30]} + 5700)); WEBSOCK=",websocket=$WPORT"; else WEBSOCK=""; fi
if [ ! -z ${array[31]} ]; then VNCPASS=",password"; fi if [ ! -z ${array[31]} ]; then VNCPASS=",password"; fi
if [ ! -z ${array[29]} ]; then VNCPORT="-vnc :${array[29]}$WEBSOCK$VNCPASS"; fi if [ ! -z ${array[29]} ]; then VNCPORT="-vnc :${array[29]}$WEBSOCK$VNCPASS"; fi
COMMAND=(su - ${array[2]} -s /bin/bash -c \"${array[4]} -name ${array[1]} -enable-kvm ${array[10]} $CPUTYPE $MEMORY $SMP -device virtio-rng-pci -boot ${array[11]} $USBDEV $KB -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\") COMMAND=(su - ${array[2]} -s /bin/bash -c \"${array[4]} -name ${array[1]} -enable-kvm ${array[10]} $CPUTYPE $MEMORY $SMP -device virtio-rng-pci -boot ${array[11]} $USBDEV $KB -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 if [ $LOGLEVEL -gt "1" ]; then echo ${COMMAND[@]}; fi
echo "${COMMAND[@]}" >>$TMPF echo "${COMMAND[@]}" >>$TMPF
#Set VNC Password #Set VNC Password
@ -215,19 +215,19 @@ function vmstart {
#stop VM #stop VM
function vmstop { function vmstop {
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 vms.id AS vid,vmname,connectstring FROM vms LEFT JOIN servers ON vms.server = servers.hostname WHERE vmname='$1'" | while read line; do sqlite3 $SQLITEFILE "SELECT vms.id AS vid,vmname,connectstring FROM vms LEFT JOIN servers ON vms.server = servers.hostname WHERE vmname='$1'" | while read line; do
IFS='|' read -r -a array <<< "$line" IFS='|' read -r -a array <<< "$line"
if [ -z ${array[2]} ]; then if [ -z ${array[2]} ]; then
if [ ! -e $RUNDIRECTORY/$1.pid ]; then echo "pidfile $RUNDIRECTORY/$1.pid does not exist"; exit 1; fi if [ ! -e $RUNDIRECTORY/$1.pid ]; then echo "pidfile $RUNDIRECTORY/$1.pid does not exist"; exit 1; fi
echo "system_powerdown" | socat - unix-connect:$RUNDIRECTORY/${array[1]}.mon >/dev/null echo "system_powerdown" | socat - unix-connect:$RUNDIRECTORY/${array[1]}.mon >/dev/null
else else
su nlvmi -c "ssh ${array[2]} \"echo \\\"system_powerdown\\\" | socat - unix-connect:$RUNDIRECTORY/${array[1]}.mon >/dev/null\"" su nlvmi -c "ssh ${array[2]} \"echo \\\"system_powerdown\\\" | socat - unix-connect:$RUNDIRECTORY/${array[1]}.mon >/dev/null\""
fi fi
sleep 5s sleep 5s
STOPPED="no" STOPPED="no"
COUNTER=0 COUNTER=0
while [ $STOPPED = "no" ]; do while [ $STOPPED = "no" ]; do
if [ $COUNTER == 10 ]; then if [ $COUNTER == 10 ]; then
if [ -z ${array[2]} ]; then if [ -z ${array[2]} ]; then
kill `cat $RUNDIRECTORY/${array[1]}.pid` kill `cat $RUNDIRECTORY/${array[1]}.pid`
@ -248,19 +248,19 @@ function vmstop {
((COUNTER++)) ((COUNTER++))
checkvm ${array[1]} | grep "is not running" >/dev/null && STOPPED="yes" checkvm ${array[1]} | grep "is not running" >/dev/null && STOPPED="yes"
sleep 1s sleep 1s
done done
done done
} }
#stop local vm #stop local vm
function vmlocalstop { function vmlocalstop {
if [ ! -e $RUNDIRECTORY/$1.pid ]; then echo "pidfile $RUNDIRECTORY/$1.pid does not exist"; exit 1; fi if [ ! -e $RUNDIRECTORY/$1.pid ]; then echo "pidfile $RUNDIRECTORY/$1.pid does not exist"; exit 1; fi
echo "system_powerdown" | socat - unix-connect:$RUNDIRECTORY/$1.mon >/dev/null echo "system_powerdown" | socat - unix-connect:$RUNDIRECTORY/$1.mon >/dev/null
sleep 5s sleep 5s
STOPPED="no" STOPPED="no"
COUNTER=0 COUNTER=0
while [ $STOPPED = "no" ]; do while [ $STOPPED = "no" ]; do
if [ $COUNTER == 10 ]; then if [ $COUNTER == 10 ]; then
kill `cat $RUNDIRECTORY/$1.pid` kill `cat $RUNDIRECTORY/$1.pid`
echo "$1 forcefully killed!"; echo "$1 forcefully killed!";
@ -297,9 +297,9 @@ function listdir {
#mainloop #mainloop
if [ $# -gt 0 ]; then if [ $# -gt 0 ]; then
if [ $1 == "createdb" ]; then if [ $1 == "createdb" ]; then
createdb createdb
elif [ $1 == "autostart" ]; then elif [ $1 == "autostart" ]; then
vmautostart vmautostart
elif [ $1 == "checkvm" ]; then elif [ $1 == "checkvm" ]; then
if [ -z $2 ]; then echo "checkvm needs two arguments"; fi if [ -z $2 ]; then echo "checkvm needs two arguments"; fi