#!/bin/zsh
export vmname=$1
export noshutdownsnap=1
export nostartupsnap=1
export noweb=1
export nowin=1
if [ -z $vmname ];then
echo usage: $0 \<vm name\> \[vm init params\]
exit 1
fi
cd /home/vmuser
if [ -e $vmname/$vmname.vars ];then
sleep .01
else
if [ -e /home/vmuser/$vmname.qcow2 ];then
if [ -d $vmname ];then
sleep .01
else
mkdir -p /home/vmuser/$vmname
chown -R vmuser:vmuser /home/vmuser/$vmname
fi
echo existing system disk detected
ln -s /home/vmuser/$vmname.qcow2 /home/vmuser/$vmname/$vmname.qcow2
chown -R vmuser:vmuser /home/vmuser/$vmname/$vmname.qcow2
fi
echo creating vm
sudo --preserve-env=noweb,nowin,noshutdownsnap,nostartupsnap -u vmuser -s /bin/vm init $@
sed -i "s|-spice tls-port=\$port,x509-dir=crypto,streaming-video=all,password-secret=sec0|-spice addr=127.0.0.1,port=\$port,streaming-video=filter,password-secret=sec0|g;s|sec0 \\\|sec0\&|g;/monitorc/d;/monitors/d;s|if=none|if=none,snapshot=on|g;s|if=pflash|if=pflash,snapshot=on|g;/usbredir/d;/webdav/d;/vdagent/d;s|-device usb-ccid|-vga none|g;/smartcard/d;s|hda-duplex|hda-output|g;/chrtpm/d;/tpm-tis/d;/swtpm/d;/tpm\/pid/d" /home/vmuser/$vmname/$vmname.vars
fi
echo setting vmuser ownership
export tempport=`cat /home/vmuser/$vmname/$vmname.vars|grep -w port|head -n 1|cut -f 2 -d =`
export vmport=$(($tempport+5900))
echo copying template
cp -rf /usr/share/webapps/vmlab/vmtemplate /usr/share/webapps/vmlab/$vmname
if [ -e /home/vmuser/$vmname.html ];then
cp /home/vmuser/$vmname.html /usr/share/webapps/vmlab/$vmname/ui.html
fi
echo editing template files
sed -i "s|vmtemplate|$vmname|g" /usr/share/webapps/vmlab/$vmname/index.php
sed -i "s|vmdefaultport|$vmport|g" /usr/share/webapps/vmlab/$vmname/index.php
sed "s|vmtemplate|$vmname|g" /usr/share/webapps/vmlab/$vmname/vmtemplate.apache.conf >> /etc/httpd/conf/extra/$vmname.conf
if [ -e /home/vmuser/$vmname.title ];then
export title=`cat /home/vmuser/$vmname.title`
sed -i "s|\$title=\$vmname\;|\$title=\"$title\"\;|g" /usr/share/webapps/vmlab/$vmname/index.php
fi
echo setting http ownership for $vmname
chown http:http /usr/share/webapps/vmlab
chown -R http:http /usr/share/webapps/vmlab/$vmname
echo starting and enabling monitor for $vmname
systemctl start vmmon@$vmname
systemctl enable vmmon@$vmname
if grep -qw conf/extra/$vmname.conf /etc/httpd/conf/httpd.conf;then
sleep .01
else
echo adding path for $vmname to your apache
echo Include conf/extra/$vmname.conf >> /etc/httpd/conf/httpd.conf
if pgrep -af httpd > /dev/null 2>/dev/null;then
systemctl reload httpd
fi
fi
