diff --git a/nlvmi b/nlvmi new file mode 100755 index 0000000..28a99d2 --- /dev/null +++ b/nlvmi @@ -0,0 +1,163 @@ +#!/bin/bash + +#getting the config +source /etc/nlvmi/nlvmi.conf +if [ $LOGLEVEL -gt "1" ]; then echo "configuration loaded"; fi + +#checking directories +if [ ! -d $VMDIRECTORY ]; then echo "VMDIRECTORY wrong, $VMDIRECTORY does not exist"; exit 1; fi +if [ ! -d $RUNDIRECTORY ]; then if [ $LOGLEVEL -gt "1" ]; then echo "creating $RUNDIRECTORY"; fi; mkdir -p $RUNDIRECTORY; fi + + +#checking database +function checkdb { + if [ $DATABASETYPE == "sqlite" ]; then + if [ ! -e $SQLITEFILE ]; then echo "sqlite configured, but db-file not present! create the db first!"; exit 1; fi + SQCHECK=`sqlite3 $SQLITEFILE "SELECT name FROM sqlite_master WHERE type='table' AND name='vms'"` + if [ $SQCHECK == "vms" ]; then + if [ $LOGLEVEL -gt "1" ]; then echo "DB ready to use"; fi + else + echo "error accessing sqlite!" + exit 1; + fi +echo "sqlite ready to use"; + elif [ $DATABASETYPE == "mariadb" ]; then + DBCON="mysql --disable-pager -u $MARIAUSER -p$MARIAPASS -h $MARIAHOST $MARIADB" + declare -a DBC=`echo "SHOW TABLES FROM $MARIADB; " | $DBCON | sed 1d` + if [ -z $DBC ]; then + echo "mariadb connection failed"; exit 1 + fi + fi + return 0 +} + + + + + +#creating database +function createdb { + if [ $LOGLEVEL -gt "1" ]; then echo "going to create the db"; fi + if [ $DATABASETYPE == "sqlite" ]; then + sqlite3 $SQLITEFILE "CREATE TABLE vms ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + vmname VARCHAR(50) NOT NULL, + server VARCHAR(50), + user VARCHAR(50) NOT NULL, + autostart INTEGER, + bootoption VARCHAR(2) NOT NULL, + drive1 VARCHAR(50), + drive2 VARCHAR(50), + drive3 VARCHAR(50), + cdrom VARCHAR(50), + arch VARCHAR(50) NOT NULL, + mac VARCHAR(50), + brdev1 VARCHAR(50), + brdev2 VARCHAR(50), + brdev3 VARCHAR(50), + tapdev1 VARCHAR(50), + tapdev2 VARCHAR(50), + tapdev3 VARCHAR(50), + vncport VARCHAR(50), + websocket INTEGER, + vncpasswort VARCHAR(50), + usbdev1 VARCHAR(50), + usbdev2 VARCHAR(50), + usbdev3 VARCHAR(50), + kblang VARCHAR(50) + );" + fi + checkdb && echo "DB creation successful!" || (echo "Failed to create the DB"; exit 1) +} + +#autostart VMs +function vmautostart { + if [ $DATABASETYPE == "sqlite" ]; then + sqlite3 $SQLITEFILE "SELECT id FROM vms WHERE autostart=1" | while read line; do + IFS='|' read -r -a array <<< "$line" + vmstart ${array[0]} + done + + fi +} + +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 + sqlite3 $SQLITEFILE "SELECT * FROM vms WHERE id=$1" | while read line; do + IFS='|' read -r -a array <<< "$line" + if [ ${array[6]}=="" ]; then + DRIVE1=""; fi + if [ ${array[6]}!="" ]; then + DRIVE1="-drive file=${array[6]},cache=none,format=raw,if=virtio,aio=native"; fi + if [ ${array[7]}=="" ]; then + DRIVE2=""; fi + if [ ${array[7]}!="" ]; then + DRIVE2="-drive file=${array[7]},cache=none,format=raw,if=virtio,aio=native"; fi + if [ ${array[8]}=="" ]; then + DRIVE3=""; fi + if [ ${array[8]}!="" ]; then + DRIVE3="-drive file=${array[8]},cache=none,format=raw,if=virtio,aio=native"; fi + if [ ${array[9]}=="" ]; then + CDROM=""; fi + if [ ${array[9]}!="" ]; then + CDROM="-cdrom file=${array[9]}"; fi + if [ ${array[11]}=="" ]; then + NETDEV1=""; fi + if [ ${array[11]}!="" ]; then + NETDEV1="-net nic,macaddr=${array[11]},model=virtio,netdev=net0"; fi + 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 + echo "su ${array[3]} -c \"${array[10]} -enable-kvm -cpu host -m 1024 -smp 2 -boot ${array[5]} $USBDEV1 $USBDEV2 $USBDEV3 -k ${array[24]} -daemonize $DRIVE1 $DRIVE2 $DRIVE3 $CDROM $NETDEV1 $TAPDEV1 $TAPDEV2 $TAPDEV3 $VNCPORT\"" + done + export VMFOUND="yes" +} + +#mainloop +if [ $# -gt 0 ]; then + if [ $1 == "createdb" ]; then + createdb + elif [ $1 == "autostart" ]; then + vmautostart + elif [ $1 == "start" ]; then + if [ -z $2 ]; then echo "start 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" + vmstart $arrays + done + if [[ $VMFOUND == "no" ]]; then echo "no VM with that name found!"; exit 1; fi) + + fi +else + checkdb +fi