Bash Script for Automation Xen Management
- Details
- Category: Virtualization
- Written by Mahdi Bahmani Ciahmard
- Hits: 42
mbahmani@it-27:~$ cat lvm
[root@xen02 ~]# pvcreate /dev/sda1
Physical volume "/dev/sda1" successfully created
[root@xen02 ~]# vgcreate VGNetstorage01 /dev/sda1
Volume group "VGNetstorage01" successfully created
[root@xen02 ~]# vgchange -a y
3 logical volume(s) in volume group "VGOS" now active
0 logical volume(s) in volume group "VGNetstorage01" now active
[root@xen02 ~]#
[root@xen02 ~]# pvcreate /dev/cciss/c0d1
Physical volume "/dev/cciss/c0d1" successfully created
[root@xen02 ~]# vgcreate VGVMlocal /dev/cciss/c0d1
Volume group "VGVMlocal" successfully created
[root@xen02 ~]# vgchange -a y
0 logical volume(s) in volume group "VGVMlocal" now active
3 logical volume(s) in volume group "VGOS" now active
0 logical volume(s) in volume group "VGNetstorage01" now active
[root@xen02 ~]#
mbahmani@it-27:~$
[root@xen02 ~]# lvs |grep nodel
nodelwin2k364_001 VGVMlocal -wi-a- 50.00G
nodelwin2k3r2sp2_001 VGVMlocal -wi-a- 50.00G
nodelwin2k864_001 VGVMlocal -wi-a- 51.00G
nodelwinxpsp3 VGVMlocal -wi-a- 50.00G
[root@xen02 ~]#
[root@xen02 ~]# ls /etc/xen/vmlist/
DevelopTest fedora13 nodel OlapTest64 olther tdevmehregan2008
[root@xen02 ~]#
[root@xen02 ~]# cat /usr/local/bin/bahxen
#!/bin/bash
# newvm2.sh - Create New Xen Guest
set +x
OUTPUT="/tmp/input.txt"<br />INPUT=/tmp/menu.sh.$$<br />LOG="/var/bahxen.log"<br /><br />echo "baxenlog\n" >"${LOG}"
STORAGE0NAME=VGVMlocal
STORAGE0DES="Local Disk Storage"<br /> <br />STORAGE1NAME=VGNetstorage01<br />STORAGE1DES="iSCSI MSA2312i"<br /><br /><br />debian=""<br />centos=""<br />winxp="/dev/VGVMlocal/nodelwinxpsp3"<br />win2k332="/dev/VGVMlocal/nodelwin2k3r2sp2_001"<br />win2k364="/dev/VGVMlocal/nodelwin2k364_001"<br />win2k832=""<br />win2k864="/dev/VGVMlocal/nodelwin2k864_001"<br /><br />ISODIR=/home/mbahmani/updateiso<br /> <br /># create empty file<br />>$OUTPUT<br /> <br /># <br /><br /><br />function display_output(){<br /> local h=${1-10} # box height default 10<br /> local w=${2-41} # box width default 41<br /> local t=${3-Output} # box title <br /> dialog --backtitle "Bahmani Shell Script to Provid Xen Guest" --title "${t}" --clear --msgbox "$(<$OUTPUT)" ${h} ${w}
}
function CLEAR(){
/usr/bin/clear
}
#####################################################################
function Select_OS_Type(){
dialog --clear --help-button --backtitle "Linux Shell Script" \
--title "[ Select OS Type ]" \
--menu "You can use the UP/DOWN arrow keys, the first \n\
letter of the choice as a hot key, or the \n\
number keys 1-9 to choose an option.\n\
Choose the TASK" 18 60 8 \
0 "linux Debian squeeze amd64bit (Base ISO)" \
1 "linux Centos 5.5 64bit (Base ISO)" \
2 "winxp (Base ISO)" \
3 "win2003 r2sp2 32bit (Base ISO)" \
4 "win2003 r2sp2 64bit (Base ISO)" \
5 "win2008 r2sp2 32bit (Base ISO)" \
6 "win2008 r2sp2 64bit (Base ISO)" \
7 "Clear Installation" 2>"${INPUT}"
Select_OS_Type_respose=$?
echo $Select_OS_Type_respose
vmtype=$(<"${INPUT}")
}
#####################################################################
function Select_Storage(){
dialog --clear --help-button --backtitle "Linux Shell Script" \
--title "[ Select Storage ]" \
--menu "Important Current Sotrage Status \n
\n
StorageName Usage Free \n
------------------------------------------------------------ \n
`pvs|grep $STORAGE0NAME|awk '{printf $2" "$5" "$6}'` \n
------------------------------------------------------------ \n
`pvs|grep $STORAGE1NAME|awk '{printf $2" "$5" "$6}'` \n
------------------------------------------------------------ \n
Choose the Storage \n\
" 18 100 3 \
0 "`echo $STORAGE0NAME "($STORAGE0DES) Dedicated for BaseISO"`" \
1 "`echo $STORAGE1NAME "($STORAGE1DES)"`" 2>"${OUTPUT}"
Select_Storage_respose=$?
selectstorage=$(<$OUTPUT)
case $selectstorage in
0)ST=$STORAGE0NAME;;
1)ST=$STORAGE1NAME;;
esac
# if [ $selectstorage=0 ]
# then
# ST=$STORAGE0NAME
# else
# ST=$STORAGE1NAME
# fi
}
#####################################################################
# cleanup - add a trap that will remove $OUTPUT
# if any of the signals - SIGHUP SIGINT SIGTERM it received.
#trap "rm $OUTPUT; exit" SIGHUP SIGINT SIGTERM
function Get_OS_Name(){
dialog --title "VM Name" \
--backtitle "Bahmani Shell Script to Provid Xen Guest" \
--inputbox "Enter your OS name " 8 60 2>$OUTPUT
# get respose
Get_OS_Name_respose=$?
# get data stored in $OUPUT using input redirection
vmname=$(<$OUTPUT)
}
#########################################################################
function Get_VM_Size(){
dialog --title "VM Size" \
--backtitle "Bahmani Shell Script to Provid Xen Guest" \
--inputbox "Enter your OS HDD Size it must be great than 53000MB : MB " 8 60 2>$OUTPUT
Get_VM_Size_respose=$?
vmhddsize=$(<$OUTPUT)
}
##########################################################################
function Get_VM_MEMORY_Size(){
dialog --title "Memory Allocation" \
--backtitle "Bahmani Shell Script to Provid Xen Guest" \
--inputbox "Please enter the memory configuration for this virtual machine.
\n
Current Memory Information: MB \n
`xm info |grep total_memory`\n
`xm info |grep free_memory`\n
\n
" 15 80 2>$OUTPUT
vmmemorysize=$(<$OUTPUT)
Get_VM_MEMORY_Size_respose=$?
}
##########################################################################
function Get_VM_Network(){
dialog --title "[ Select Network Interface ]" \
--backtitle "Bahmani Shell Script to Provid Xen Guest" \
--inputbox "Type Interface Name: \n
------------------------------------------------------------\n
`/sbin/ifconfig |grep xen |awk '{print $1 " |"}'` \n
------------------------------------------------------------\n
" 10 70 2>$OUTPUT
vmnetname=$(<$OUTPUT)
Get_VM_Network_respose=$?
}
##########################################################################
function Create_VM_CONFIG_FILE(){
echo "builder='hvm'" |tee -a "${LOG}" >>/etc/xen/vmlist/$vmname<br /> vmmaxmem=$(echo "scale=9; $vmmemorysize+100" | bc) <br /> echo "maxmem = '$vmmaxmem' " |tee -a "${LOG}" >>/etc/xen/vmlist/$vmname
echo "memory = '$vmmemorysize'" |tee -a "${LOG}" >>/etc/xen/vmlist/$vmname<br /> echo "vcpus = 1" |tee -a "${LOG}">>/etc/xen/vmlist/$vmname <br /> echo "name = '$vmname'" |tee -a "${LOG}" >>/etc/xen/vmlist/$vmname<br /> echo "device_model ='/usr/lib/xen-3.2-1/bin/qemu-dm'" |tee -a "${LOG}" >>/etc/xen/vmlist/$vmname<br /> echo "kernel = '/usr/lib/xen-3.2-1/boot/hvmloader'" |tee -a "${LOG}" >>/etc/xen/vmlist/$vmname<br /> echo "pae=1" |tee -a "${LOG}" >>/etc/xen/vmlist/$vmname<br /> echo "acpi=1" |tee -a "${LOG}" >>/etc/xen/vmlist/$vmname<br /> echo "apic=1" |tee -a "${LOG}" >>/etc/xen/vmlist/$vmname<br /> echo "localtime = 0" |tee -a "${LOG}" >>/etc/xen/vmlist/$vmname<br /> echo "vif = [ 'bridge=$vmnetname,rate=50Kb/s' ]" |tee -a "${LOG}" >>/etc/xen/vmlist/$vmname
# echo "disk = [ 'phy:/dev/VGVMlocal/$vmname,ioemu:hda,w']">>/etc/xen/vmlist/$vmname
# echo "device_model = '/usr/lib64/xen/bin/qemu-dm'">>/etc/xen/vmlist/$vmname <br /> echo "boot='c'" |tee -a "${LOG}" >>/etc/xen/vmlist/$vmname<br /> echo "#boot='d'" |tee -a "${LOG}" >>/etc/xen/vmlist/$vmname<br /> echo "sdl = 1" |tee -a "${LOG}" >>/etc/xen/vmlist/$vmname<br /> echo "vnc = 1" |tee -a "${LOG}" >>/etc/xen/vmlist/$vmname<br /> echo "vncunused = 1" |tee -a "${LOG}" >>/etc/xen/vmlist/$vmname<br /> echo "vncviewer=1" |tee -a "${LOG}" >>/etc/xen/vmlist/$vmname<br /> echo "serial = 'pty'" |tee -a "${LOG}" >>/etc/xen/vmlist/$vmname<br /> echo "on_poweroff = 'destroy'" |tee -a "${LOG}" >>/etc/xen/vmlist/$vmname<br /> echo "on_reboot = 'restart'" |tee -a "${LOG}" >>/etc/xen/vmlist/$vmname<br /> echo "on_crash = 'restart'" |tee -a "${LOG}" >>/etc/xen/vmlist/$vmname<br /> echo "disk = [ 'phy:/dev/$ST/$vmname,ioemu:hda,w']" |tee -a "${LOG}" >>/etc/xen/vmlist/$vmname<br /><br />}<br /><br />##########################################################################<br />function Cancel_exit(){<br /><br />if [ $1 = "1" ] <br />then<br /> echo $1;echo "Cancel pressed."; exit 0<br />fi<br />}<br /><br />##########################################################################<br /><br />function Show_Summery(){<br /> echo "Summery" >$OUTPUT<br /> echo " " |tee -a "${LOG}">>$OUTPUT<br /> echo "--------------------------------------" |tee -a "${LOG}">>$OUTPUT<br /> echo "Virtual Machine Name: $vmname " |tee -a "${LOG}">>$OUTPUT<br /> echo "--------------------------------------" |tee -a "${LOG}">>$OUTPUT<br /> echo "Memory: $vmmemorysize" |tee -a "${LOG}">>$OUTPUT<br /> echo "--------------------------------------" |tee -a "${LOG}">>$OUTPUT<br /> echo "Storage: /dev/$ST/$vmname" |tee -a "${LOG}">>$OUTPUT<br /> echo "--------------------------------------" |tee -a "${LOG}">>$OUTPUT<br /> echo "Disk Size: $vmhddsize" |tee -a "${LOG}">>$OUTPUT<br /> echo "--------------------------------------" |tee -a "${LOG}">>$OUTPUT<br /> echo "Network: $vmnetname" |tee -a "${LOG}">>$OUTPUT<br /> echo "--------------------------------------" |tee -a "${LOG}">>$OUTPUT<br /> echo |tee -a "${LOG}">>$OUTPUT<br /><br /> display_output 25 60 "Finish Virtual Machine Creation"<br />}<br /><br />#########################################################################<br />function Create_Storage(){<br /><br />#lvcreate -L$vmhddsize"G" -n $vmname /dev/$ST<br />lvcreate -L51G -n $vmname /dev/$ST<br /><br />}<br /><br />##########################################################################<br />function RUN_VM(){<br />whiptail --title "Run Virtual Machine" --yesno "Do You Want Run This Vm yes/no ." 8 78<br /> <br />exitstatus=$?<br />if [ $exitstatus = 0 ]; then<br /> /usr/sbin/xm create /etc/xen/vmlist/$vmname;<br /> /usr/bin/convirt &<br />else<br /> exit 0;<br />fi<br /> <br />#echo "(Exit status was $exitstatus)"<br /><br />}<br /><br /><br />################################## main ###################################<br /><br /><br />Get_OS_Name<br /> Cancel_exit $Get_OS_Name_respose<br /> if [ -f /etc/xen/vmlist/$vmname ]<br /> then echo "This name in use for another VM Guest";<br /> dialog --title "Error" --msgbox 'This name in use for another VM Guest' 6 40
ls /etc/xen/vmlist/$vmname; exit 0 ;
else
Select_OS_Type
Cancel_exit $Select_OS_Type_respose
fi
Select_Storage
Cancel_exit $Select_Storage_respose
Get_VM_Size
Cancel_exit $Get_VM_Size_respose
Get_VM_MEMORY_Size
Cancel_exit $Get_VM_MEMORY_Size_respose
Get_VM_Network
Cancel_exit $Get_VM_Network_respose
Show_Summery
########################################################################
# case $respose in
# 0)
# sayhello ${name}
# ;;
# 1)
# echo "Cancel pressed."
# ;;
# 255)
# echo "[ESC] key pressed."
# esac
########################################################################
# letter of the choice as a hot key, or the \n\
# number keys 1-9 to choose an option.\n\
# ------------------------------------------------------------
# PV VG Fmt Attr PSize PFree \n\
# ------------------------------------------------------------\n\
# ------------------------------------------------------------ \n\
# pvs|grep VGNetstorage01 \n\
# pvs|grep VGVMlocal \n\
# ----------------------------------------------------------- \n\
echo $OF |tee -a "${LOG}"
OF="/dev/$ST/$vmname"<br /><br />case $vmtype in<br /> 0);;<br /> 1);;<br /> 2) <br /> CLEAR;Create_Storage;Create_VM_CONFIG_FILE;echo;echo "Task in Progress Please Waite...";echo;<br /> ( `dd if=$winxp conv=sync,noerror bs=8192 | pv -s 51G |dd bs=8192 of=$OF `);<br /> RUN_VM ;<br /> ;;<br /> 3) <br /> CLEAR;Create_Storage;Create_VM_CONFIG_FILE;echo;echo "Task in Progress Please Waite...";echo;<br /> ( `dd if=$win2k332 conv=sync,noerror bs=8192 | pv -s 50G |dd bs=8192 of=$OF `) ;<br /> RUN_VM ;<br /> ;;<br /> 4) <br /> CLEAR;Create_Storage;Create_VM_CONFIG_FILE;echo;echo "Task in Progress Please Waite...";echo;<br /> #( (`dd if=$win2k364 conv=sync,noerror bs=8192 | pv -n -s 50.00G |gzip -c > $OF `) 2>&1 | dialog --gauge 'Task in Progress Please Waite...' 7 70 );<br /> ( `dd if=$win2k364 conv=sync,noerror bs=8192 | pv -s 50G |dd bs=8192 of=$OF `);<br /> RUN_VM ;<br /> ;;<br /> 5) <br /> CLEAR;Create_Storage;Create_VM_CONFIG_FILE;echo;echo "Task in Progress Please Waite...";echo;<br /> ( `dd if=$win2k832 conv=sync,noerror bs=8192 | pv -s 50G |dd bs=8192 of=$OF `); <br /> RUN_VM ;<br /> ;;<br /> 6) <br /> CLEAR;Create_Storage;Create_VM_CONFIG_FILE;echo;echo "Task in Progress Please Waite...";echo;<br /> ( `dd if=$win2k864 conv=sync,noerror bs=8192 | pv -s 51G |dd bs=8192 of=$OF `); <br /> RUN_VM ; <br /> ;;<br /> 7) <br /> ISOFILENAME=$(dialog --title "Select iso File" --stdout --title "Please choose a iso file " --fselect $ISODIR 14 48 );
Create_VM_CONFIG_FILE;
`sed -i "/disk/d" /etc/xen/vmlist/$vmname` ;
`echo "disk = [ 'phy:/dev/$ST/$vmname,ioemu:hda,w','file:$ISOFILENAME,hdd:cdrom,r']">>/etc/xen/vmlist/$vmname` ;
`sed -i '/boot/ s/c/d/' /etc/xen/vmlist/$vmname` ;
`/usr/sbin/lvcreate -L$vmhddsize"M" -n $vmname /dev/$ST`;
RUN_VM ;
exit 0;
;;
esac
echo "vmtype:$vmtype\n" |tee -a "${LOG}"
#(dd if=/dev/VGNetstorage01/tes conv=sync,noerror bs=1k | pv -s 5000000k | gzip -c > /dev/VGNetstorage01/tes1 ) 2>&1 | dialog --gauge 'Task in Progress Please Waite...' 7 70
##ssh xen02 'lvcreate -L25G -n /dev/VGNetstorage01/DevelopTest'
#for i in `cat /etc/xen/vmlist/currentvm`; do xm create /etc/xen/vmlist/$i; done
#dd if=/dev/VGVMlocal/DevelopTest |pv -s 25G |ssh root@xen02 "dd of=/dev/VGNetstorage01/DevelopTest"
#/sbin/ip link set dev eth0 mtu 9000
#/sbin/ip link set dev eth1 mtu 9000
#/sbin/ip link set dev eth2 mtu 9000
#/sbin/ip link set dev eth3 mtu 9000
#ethtool -K eth0 tx off
#ethtool -K eth1 tx off
#ethtool -K eth2 tx off
#ethtool -K eth0 tso off
#ethtool -K eth1 tso off
#ethtool -K eth2 tso off
# remove $OUTPUT file
#rm $OUTPUT
set +x
[root@xen02 ~]#

