#!/bin/sh #----------------------------------------------------------------------------- # /var/install/bin/openvz-create-vm.cui - create VM from template # # Last Update: $Id:$ # # Copyright (c) 2006-2009 the eisfair team # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. #----------------------------------------------------------------------------- template="$3" . /var/install/include/cuilib . /var/install/include/cuivmlib #============================================================================ # global constants #============================================================================ IDC_VMDLG_BUTOK='10' # dlg OK button ID IDC_VMDLG_BUTCANCEL='11' # dlg Cancel button ID IDC_VMDLG_LABEL1='12' # dlg label ID IDC_VMDLG_LABEL2='13' # dlg label ID IDC_VMDLG_LABEL3='14' # dlg label ID IDC_VMDLG_LABEL4='15' # dlg label ID IDC_VMDLG_LABEL5='16' # dlg label ID IDC_VMDLG_LABEL6='17' # dlg label ID IDC_VMDLG_LABEL7='18' # dlg label ID IDC_VMDLG_EDHOSTNAME='19' # dlg edit ID IDC_VMDLG_EDDOMAIN='20' # dlg edit ID IDC_VMDLG_EDPASSWD1='21' # dlg edit ID IDC_VMDLG_EDPASSWD2='22' # dlg edit ID IDC_VMDLG_EDQUOTA='23' # dlg edit ID IDC_VMDLG_EDIP='24' # dlg edit ID IDC_VMDLG_EDDNS='25' # dlg edit ID IDC_VMDLG_CHKBOOTRUN='26' # dlg edit ID #============================================================================ # VM edit/create dialog #============================================================================ #---------------------------------------------------------------------------- # vmdlg_ok_clicked # Ok button clicked hook # expects: $1 : window handle of dialog window # $2 : button control id # returns: 1 : event handled #---------------------------------------------------------------------------- function vmdlg_ok_clicked() { local win="$p2" local ctrl cui_window_getctrl "$win" "$IDC_VMDLG_EDHOSTNAME" && ctrl="$p2" if cuivm_valid_handle $ctrl then cui_edit_gettext "$ctrl" vmdlg_hostname="$p2" fi cui_window_getctrl "$win" "$IDC_VMDLG_EDDOMAIN" && ctrl="$p2" if cuivm_valid_handle $ctrl then cui_edit_gettext "$ctrl" vmdlg_domain="$p2" fi cui_window_getctrl "$win" "$IDC_VMDLG_EDPASSWD1" && ctrl="$p2" if cuivm_valid_handle $ctrl then cui_edit_gettext "$ctrl" vmdlg_passwd1="$p2" fi cui_window_getctrl "$win" "$IDC_VMDLG_EDPASSWD2" && ctrl="$p2" if cuivm_valid_handle $ctrl then cui_edit_gettext "$ctrl" vmdlg_passwd2="$p2" fi cui_window_getctrl "$win" "$IDC_VMDLG_EDQUOTA" && ctrl="$p2" if cuivm_valid_handle $ctrl then cui_edit_gettext "$ctrl" vmdlg_quota="$p2" fi cui_window_getctrl "$win" "$IDC_VMDLG_EDIP" && ctrl="$p2" if cuivm_valid_handle $ctrl then cui_edit_gettext "$ctrl" vmdlg_ip="$p2" fi cui_window_getctrl "$win" "$IDC_VMDLG_EDDNS" && ctrl="$p2" if cuivm_valid_handle $ctrl then cui_edit_gettext "$ctrl" vmdlg_dns="$p2" fi cui_window_getctrl "$win" "$IDC_VMDLG_CHKBOOTRUN" && ctrl="$p2" if cuivm_valid_handle $ctrl then cui_checkbox_getcheck "$ctrl" && vmdlg_bootrun="$p2" fi if [ -z "${vmdlg_hostname}" ] then cui_message "$win" "No host name entered! Please enter a valid host name" \ "Missing data" "$MB_ERROR" cui_return 1 return fi if [ -z "${vmdlg_domain}" ] then cui_message "$win" "No domain name entered! Please enter a valid domain" \ "Missing data" "$MB_ERROR" cui_return 1 return fi if [ -z "${vmdlg_quota}" ] then cui_message "$win" "Soft/hardquota: missing entry! Please enter: 18G:20G " \ "Missing data" "$MB_ERROR" cui_return 1 return elif ! cuivm_check_is_quota "${vmdlg_quota}" then cui_message "$win" "Soft/hardquota: value not correct! Please enter: 18G:20G " \ "Invalid data" "$MB_ERROR" cui_return 1 return fi if [ -z "${vmdlg_ip}" ] then cui_message "$win" "IP address: no IP address specified!" \ "Missing data" "$MB_ERROR" cui_return 1 return elif ! cuivm_check_is_ipaddress "${vmdlg_ip}" then cui_message "$win" "IP address: no valid ip address!" \ "Invalid data" "$MB_ERROR" cui_return 1 return fi if [ -z "${vmdlg_dns}" ] then cui_message "$win" "DNS server: no IP address specified!" \ "Missing data" "$MB_ERROR" cui_return 1 return elif ! cuivm_check_is_ipaddress "${vmdlg_dns}" then cui_message "$win" "DNS server: no valid ip address!" \ "Invalid data" "$MB_ERROR" cui_return 1 return fi if [ "${vmdlg_passwd1}" != "${vmdlg_passwd2}" ] then cui_message "$win" "Passwords do not match. Please reenter passwords." \ "Missing data" "$MB_ERROR" cui_return 1 return fi cui_window_close "$win" "$IDOK" cui_return 1 } #---------------------------------------------------------------------------- # vmdlg_cancel_clicked # Cancel button clicked hook # expects: $1 : window handle of dialog window # $2 : button control id # returns: 1 : event handled #---------------------------------------------------------------------------- function vmdlg_cancel_clicked() { cui_window_close "$p2" "$IDCANCEL" cui_return 1 } #---------------------------------------------------------------------------- # vmdlg_create_hook # Dialog create hook - create dialog controls # expects: $1 : window handle of dialog window # returns: 1 : event handled #---------------------------------------------------------------------------- function vmdlg_create_hook() { local dlg="$p2" local ctrl if cui_label_new "$dlg" "Host name:" 2 1 14 1 "$IDC_VMDLG_LABEL1" "$CWS_NONE" "$CWS_NONE" then cui_window_create "$p2" fi if cui_label_new "$dlg" "Domain:" 2 3 14 1 "$IDC_VMDLG_LABEL2" "$CWS_NONE" "$CWS_NONE" then cui_window_create "$p2" fi if cui_label_new "$dlg" "Root password:" 2 5 14 1 "$IDC_VMDLG_LABEL3" "$CWS_NONE" "$CWS_NONE" then cui_window_create "$p2" fi if cui_label_new "$dlg" "Retype passwd.:" 2 7 14 1 "$IDC_VMDLG_LABEL4" "$CWS_NONE" "$CWS_NONE" then cui_window_create "$p2" fi if cui_label_new "$dlg" "Soft/hardquota:" 2 9 14 1 "$IDC_VMDLG_LABEL5" "$CWS_NONE" "$CWS_NONE" then cui_window_create "$p2" fi if cui_label_new "$dlg" "IP address:" 2 11 14 1 "$IDC_VMDLG_LABEL6" "$CWS_NONE" "$CWS_NONE" then cui_window_create "$p2" fi if cui_label_new "$dlg" "DNS server:" 2 13 14 1 "$IDC_VMDLG_LABEL7" "$CWS_NONE" "$CWS_NONE" then cui_window_create "$p2" fi cui_edit_new "$dlg" "" 17 1 25 1 255 "$IDC_VMDLG_EDHOSTNAME" "$CWS_NONE" "$CWS_NONE" && ctrl="$p2" if cuivm_valid_handle "$ctrl" then cui_window_create "$ctrl" cui_edit_settext "$ctrl" "${vmdlg_hostname}" fi cui_edit_new "$dlg" "" 17 3 25 1 255 "$IDC_VMDLG_EDDOMAIN" "$CWS_NONE" "$CWS_NONE" && ctrl="$p2" if cuivm_valid_handle "$ctrl" then cui_window_create "$ctrl" cui_edit_settext "$ctrl" "${vmdlg_domain}" fi cui_edit_new "$dlg" "" 17 5 25 1 255 "$IDC_VMDLG_EDPASSWD1" "$EF_PASSWORD" "$CWS_NONE" && ctrl="$p2" if cuivm_valid_handle "$ctrl" then cui_window_create "$ctrl" cui_edit_settext "$ctrl" "${vmdlg_passwd1}" fi cui_edit_new "$dlg" "" 17 7 25 1 255 "$IDC_VMDLG_EDPASSWD2" "$EF_PASSWORD" "$CWS_NONE" && ctrl="$p2" if cuivm_valid_handle "$ctrl" then cui_window_create "$ctrl" cui_edit_settext "$ctrl" "${vmdlg_passwd2}" fi cui_edit_new "$dlg" "" 17 9 25 1 255 "$IDC_VMDLG_EDQUOTA" "$CWS_NONE" "$CWS_NONE" && ctrl="$p2" if cuivm_valid_handle "$ctrl" then cui_window_create "$ctrl" cui_edit_settext "$ctrl" "${vmdlg_quota}" fi cui_edit_new "$dlg" "" 17 11 25 1 255 "$IDC_VMDLG_EDIP" "$CWS_NONE" "$CWS_NONE" && ctrl="$p2" if cuivm_valid_handle "$ctrl" then cui_window_create "$ctrl" cui_edit_settext "$ctrl" "${vmdlg_ip}" fi cui_edit_new "$dlg" "" 17 13 25 1 255 "$IDC_VMDLG_EDDNS" "$CWS_NONE" "$CWS_NONE" && ctrl="$p2" if cuivm_valid_handle "$ctrl" then cui_window_create "$ctrl" cui_edit_settext "$ctrl" "${vmdlg_dns}" fi cui_checkbox_new "$dlg" "boots after a reboot" 17 15 24 1 "$IDC_VMDLG_CHKBOOTRUN" "$CWS_NONE" "$CWS_NONE" && ctrl="$p2" if cuivm_valid_handle "$ctrl" then cui_window_create "$ctrl" cui_checkbox_setcheck "$ctrl" "${vmdlg_bootrun}" fi cui_button_new "$dlg" "&OK" 11 17 10 1 $IDC_VMDLG_BUTOK $CWS_DEFOK $CWS_NONE && ctrl="$p2" if cuivm_valid_handle "$ctrl" then cui_button_callback "$ctrl" "$BUTTON_CLICKED" "$dlg" vmdlg_ok_clicked cui_window_create "$ctrl" fi cui_button_new "$dlg" "&Cancel" 22 17 10 1 $IDC_VMDLG_BUTCANCEL $CWS_DEFCANCEL $CWS_NONE && ctrl="$p2" if cuivm_valid_handle "$ctrl" then cui_button_callback "$ctrl" "$BUTTON_CLICKED" "$dlg" vmdlg_cancel_clicked cui_window_create "$ctrl" fi cui_return 1 } #============================================================================ # invoke VM dialog due to key or menu selection #============================================================================ #---------------------------------------------------------------------------- # vm_create_dialog # Create a new VM # returns: 0 : created (reload data) # 1 : not modified (don't reload data) #---------------------------------------------------------------------------- function vm_create_dialog() { local win="$1" local result="$IDCANCEL" local dlg local lastnum local vznum local scriptfile . /etc/config.d/base template=`echo $template | sed -e 's|\.tar\.gz||' -e 's|\.tar\.bz2||'` vmdlg_hostname="$template" vmdlg_domain="$DOMAIN_NAME" vmdlg_passwd1="" vmdlg_passwd2="" vmdlg_quota="9G:10G" vmdlg_ip="$IP_NET_1_IPADDR" vmdlg_dns="`echo $DNS_SERVER | awk '{print $1}'`" vmdlg_bootrun="1" cui_window_new "$win" 0 0 46 20 $[$CWS_POPUP + $CWS_BORDER + $CWS_CENTERED] && dlg="$p2" if cuivm_valid_handle $dlg then cui_window_setcolors "$dlg" "DIALOG" cui_window_settext "$dlg" "Create new VE" cui_window_sethook "$dlg" "$HOOK_CREATE" vmdlg_create_hook cui_window_create "$dlg" cui_window_modal "$dlg" && result="$p2" if [ "$result" == "$IDOK" ] then cui_window_destroy "$dlg" lastnum=`vzlist -a -H 2>/dev/null | awk '{print $1}' | tail -n1` if [ -z "$lastnum" ] then lastnum=100 fi vznum=$(($lastnum + 1)) scriptfile="/var/lib/vz/create-${vznum}.sh" cat > ${scriptfile} << EOF #!/bin/sh echo "" vzctl create $vznum --ostemplate ${template} echo -n '* Hostname ' && vzctl set $vznum --hostname ${vmdlg_hostname} --save echo -n '* Domain ' && vzctl set $vznum --searchdomain ${vmdlg_domain} --save echo -n '* Ipaddress ' && vzctl set $vznum --ipadd ${vmdlg_ip} --save echo -n '* Nameserver ' && vzctl set $vznum --nameserver ${vmdlg_dns} --save echo -n '* Quota ' && vzctl set $vznum --diskspace ${vmdlg_quota} --save EOF if [ "$vmdlg_bootrun" = "1" ] then echo "echo -n '* Onboot yes ' && vzctl set $vznum --onboot yes --save " >> ${scriptfile} else echo "echo -n '* Onboot no ' && vzctl set $vznum --onboot no --save " >> ${scriptfile} fi if [ -n "$vmdlg_passwd2" ] then echo "echo -n '* Password ' && vzctl set $vznum --userpasswd root:$vmdlg_passwd2 --save" >> ${scriptfile} fi echo "sleep 2" >> ${scriptfile} echo "rm -f ${scriptfile}" >> ${scriptfile} chmod 0750 ${scriptfile} cuivm_create_terminal "$mainwin" "${scriptfile}" "Create new VE from template" else cui_window_destroy "$dlg" fi fi [ "$result" == "$IDOK" ] return "$?" } #============================================================================ # main window hooks #============================================================================ #---------------------------------------------------------------------------- # init routine (entry point of all shellrun.cui based programs) # $p2 --> desktop window handle #---------------------------------------------------------------------------- function init() { local win="$p2" # setup main window cui_window_new "$win" 0 0 0 0 $[$CWS_POPUP + $CWS_CAPTION + $CWS_STATUSBAR + $CWS_MAXIMIZED] && mainwin="$p2" if cuivm_valid_handle $mainwin then cui_window_setcolors "$mainwin" "DESKTOP" cui_window_settext "$mainwin" "OpenVZ VE administration " cui_window_setrstatustext "$mainwin" "V1.0.0" cui_window_create "$mainwin" vm_create_dialog $mainwin cui_window_quit 0 fi cui_return 0 } #---------------------------------------------------------------------------- # main routines (always at the bottom of the file) #---------------------------------------------------------------------------- cui_init cui_run exit 0