#!/bin/bash # Matt McCarrell 2009 script_dir=/xen/generic/scripts conf_dir=/xen/confs disk_dir=/xen/images DIALOG=`which dialog` RM=`which rm` CLEAR=`which clear` CAT=`which cat` AWK=`which awk` XM=/usr/sbin/xm GREP=`which grep` ECHO=`which echo` NOHUP=`which nohup` freemem=`${XM} info | ${GREP} free_memory | ${AWK} '{print $3}'` totalcpus=`${XM} info | ${GREP} nr_cpus | ${AWK} '{print $3}'` function checkexit(){ if [ "$?" -ne "0" ] then ${RM} /tmp/*$$ ${CLEAR} exit 1 fi } function askname(){ ${DIALOG} --cancel-label "EXIT" --title "CSLabs VM Creator" --inputbox "What is the name of this VM?\n\nEx. dns" 0 0 2> /tmp/name$$ checkexit if [ ! -s /tmp/name$$ ] then ${DIALOG} --title "CSLabs VM Creator" --msgbox "Error: You must choose a name for this VM.\n\nPress enter to try again." 0 0 askname fi if [ -s ${conf_dir}/`${CAT} /tmp/name$$` ] || [ -s ${disk_dir}/`${CAT} /tmp/name$$`.disk ] then ${DIALOG} --title "CSLabs VM Creator" --msgbox "Error: A VM with that name already exists.\n\nPress enter to try again." 0 0 askname fi } function askcpus(){ ${DIALOG} --cancel-label "EXIT" --title "CSLabs VM Creator" --inputbox "How many CPUs should the VM have?\n\nEx. 1" 0 0 2> /tmp/cpus$$ checkexit if [ ! -s /tmp/cpus$$ ] then ${DIALOG} --title "CSLabs VM Creator" --msgbox "Error: You must specify at least 1 CPU.\n\nPress enter to try again." 0 0 askcpus fi if [ `${CAT} /tmp/cpus$$` -eq `${CAT} /tmp/cpus$$` 2> /dev/null ] then if [ `${CAT} /tmp/cpus$$` -gt ${totalcpus} ] then ${DIALOG} --title "CSLabs VM Creator" --msgbox "Error: You specified a greater number of CPUs then what is available on the system. Please specify something less than or equal to ${totalcpus} for CPUs.\n\nPress enter to try again." 0 0 askcpus fi else ${DIALOG} --title "CSLabs VM Creator" --msgbox "Error: You must specify the number of CPUs for the VM as a digit.\n\nPress enter to try again." 0 0 askcpus fi } function askmem(){ ${DIALOG} --cancel-label "EXIT" --title "CSLabs VM Creator" --inputbox "How much RAM (in MB) should the VM have?\n\nEx. 512" 0 0 2> /tmp/mem$$ checkexit if [ ! -s /tmp/mem$$ ] then ${DIALOG} --title "CSLabs VM Creator" --msgbox "Error: You must specify some amount of RAM.\n\nPress enter to try again." 0 0 askmem fi if [ `${CAT} /tmp/mem$$` -eq `${CAT} /tmp/mem$$` 2> /dev/null ] then if [ `${CAT} /tmp/mem$$` -gt ${freemem} ] then ${DIALOG} --title "CSLabs VM Creator" --msgbox "Error: There is not enough RAM available on this dom0 for your VM. Please specify a smaller amount of RAM that is equal to or less than ${freemem} MB or choose a different dom0.\n\nPress enter to try again." 0 0 askmem fi else ${DIALOG} --title "CSLabs VM Creator" --msgbox "Error: You must specify the amount of RAM with digits.\n\nPress enter to try again." 0 0 askmem fi } function askuser(){ ${DIALOG} --cancel-label "EXIT" --title "CSLabs VM Creator" --inputbox "What should the first user account be?\n\nEx. mccarrms" 0 0 2> /tmp/user$$ checkexit if [ ! -s /tmp/user$$ ] then ${DIALOG} --title "CSLabs VM Creator" --msgbox "Error: You must specify what the first user account should be.\n\nPress enter to try again." 0 0 askuser fi } function askemail(){ ${DIALOG} --cancel-label "EXIT" --title "CSLabs VM Creator" --inputbox "What is this user's email address?\n\nEx. mccarrms@gmail.com" 0 0 2> /tmp/email$$ checkexit if [ ! -s /tmp/email$$ ] then ${DIALOG} --title "CSLabs VM Creator" --msgbox "Error: You must specify the user's email address.\n\nPress enter to try again." 0 0 askemail fi ${DIALOG} --title "CSLabs VM Creator" --yesno "Are you sure this email address is correct?\n\nEmail: `${CAT} /tmp/email$$`" 0 0 if [ "$?" -ne "0" ] then askemail fi } function askip(){ ${DIALOG} --cancel-label "EXIT" --title "CSLabs VM Creator" --inputbox "What should the Clarkson IP address be?\n\nEx. 128.153.145.100" 0 0 2> /tmp/ip$$ checkexit if [[ "`${CAT} /tmp/ip$$`" =~ "128\.153\.145\.([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])$" ]] then ${ECHO} "Do nothing" > /dev/null else ${DIALOG} --title "CSLabs VM Creator" --msgbox "Error: You must specify an IP address in the following range: 128.153.145.0-255\n\nPress enter to try again." 0 0 askip fi } function askip2(){ ${DIALOG} --cancel-label "EXIT" --title "CSLabs VM Creator" --inputbox "What should the server room IP address be?\n\nEx. 10.0.1.100" 0 0 2> /tmp/ip2$$ checkexit if [[ "`${CAT} /tmp/ip2$$`" =~ "10\.0\.1\.([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])$" ]] then ${ECHO} -n " " >> /tmp/ip$$ ${CAT} /tmp/ip2$$ >> /tmp/ip$$ else ${DIALOG} --title "CSLabs VM Creator" --msgbox "Error: You must specify an IP address in the following range: 10.0.1.0-255\n\nPress enter to try again." 0 0 askip2 fi } function askip3(){ ${DIALOG} --cancel-label "EXIT" --title "CSLabs VM Creator" --inputbox "What should the internal IP address be?\n\nEx. 10.0.0.100" 0 0 2> /tmp/ip3$$ checkexit if [[ "`${CAT} /tmp/ip3$$`" =~ "10\.0\.0\.([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])$" ]] then ${ECHO} -n " " >> /tmp/ip$$ ${CAT} /tmp/ip3$$ >> /tmp/ip$$ else ${DIALOG} --title "CSLabs VM Creator" --msgbox "Error: You must specify an IP address in the following range: 10.0.0.0-255\n\nPress enter to try again." 0 0 askip3 fi } ${DIALOG} --title "CSLabs VM Creator" --msgbox "Welcome to the virtual machine creation wizard!\n\nPress enter to continue." 0 0 ${DIALOG} --cancel-label "EXIT" --title "CSLabs VM Creator" --menu "Which OS would you like?" 10 30 3 Ubuntu 8.04 CentOS 5 2> /tmp/os$$ checkexit if [[ "`${CAT} /tmp/os$$`" =~ "CentOS" ]] then ${DIALOG} --title "CSLabs VM Creator" --msgbox "A generic CentOS 5 VM isn't available yet.\n\nCheck back shortly." 0 0 ${RM} /tmp/*$$ ${CLEAR} exit 0 fi askname askcpus askmem askuser askemail askip ${DIALOG} --title "CSLabs VM Creator" --yesno "Should this VM be on the server room network?\n\nNote: Most VMs don't need to be." 0 0 if [ "$?" -ne "1" ] then askip2 fi ${DIALOG} --cancel-label "EXIT" --title "CSLabs VM Creator" --yesno "Should this VM be on the internal lab network?\n\nNote: Most VMs don't need to be." 0 0 if [ "$?" -ne "1" ] then askip3 fi script_vars="`${CAT} /tmp/name$$` `${CAT} /tmp/cpus$$` `${CAT} /tmp/mem$$` `${CAT} /tmp/user$$` `${CAT} /tmp/email$$` `${CAT} /tmp/ip$$`" if [[ "`${CAT} /tmp/os$$`" =~ "Ubuntu" ]] then ${NOHUP} ${script_dir}/create_ubuntu_vm.sh ${script_vars} > /dev/null & ${DIALOG} --title "CSLabs VM Creator" --msgbox "That's it! Your VM is now being created. The initial user will receive an email once it is done or if any errors are encountered in the process.\n\nPress enter to exit." 0 0 ${RM} /tmp/*$$ ${CLEAR} exit 0 elif [[ "`${CAT} /tmp/os$$`" =~ "CentOS" ]] then ${NOHUP} ${script_dir}/create_centos_vm.sh ${script_vars} > /dev/null & ${DIALOG} --title "CSLabs VM Creator" --msgbox "That's it! Your VM is now being created. The initial user will receive an email once it is done or if any errors are encountered in the process.\n\nPress enter to exit." 0 0 ${RM} /tmp/*$$ ${CLEAR} exit 0 else ${DIALOG} --title "CSLabs VM Creator" --msgbox "There was a unexpected issue with creating your VM. Please try again.\n\nPress enter to exit." 0 0 ${RM} /tmp/*$$ ${CLEAR} exit 1 fi