source: drbl-virt/sbin/drbl_PXE_PV-VM_create.sh @ 200

Last change on this file since 200 was 200, checked in by rock, 14 years ago

Bugfix: var error

  • Property svn:executable set to *
File size: 4.4 KB
RevLine 
[191]1#!/bin/bash
2# Program:
3#   Create PXE PV VM
4# Author:
5#   Jazz, Rock {jazz, rock}@nchc.org.tw
6# Version:
7#    1.0
8# History:                                                                                         
9#   2010/08/27  Rock    First release (1.0)
10
11# [Source]
12source /opt/drbl-virt/conf/drbl-virt.conf
[195]13source $Work_Path/functions_drbl_virt
14#source ./functions_drbl_PXE_PV-VM_create
[191]15
16
17# [Declation]
18# = 1. Varibales declation =
19vm_name=""
[199]20vm_cpu="1"
21vm_ram="128"
22#vm_host=""
[191]23vm_mac=""
24
25vm_dir="/home/domains"
26# = 2. Functions declation =
27Usage(){
28echo "Usage: drbl_PXE_PV-VM_create.sh options"
29echo "Options: "
[195]30echo "-v|--vm      vm name"
[199]31echo "-c|--cpu     cpu number (default: 1)"
32echo "-r|--ram     ram size(M) (default: 128)"
[191]33echo "Examples: "
[199]34echo "drbl_PXE_PV-VM_create.sh options -v drbl131 -c 2 -r 512"
[191]35}
36
37
38# [Main]
39check_root
40# = 1. Parse parameters =
41if [ $# -eq 0 ]; then
[192]42Usage && exit
[191]43fi
44
45while [ $# -gt 0 ]; do
46    case "$1" in
[195]47        -v|--vm)
[191]48            shift
49            if [ -z "$(echo $1 |grep ^-.)" ]; then
50                if [ -n "$(echo $1)" ]; then
51                    vm_name=$1
52                else
53                    echo "-v vm_name is null"
54                    Usage && exit 2
55                fi
56            shift
57            fi
58            ;;
[195]59        -c|--cpu)
[191]60            shift
61            if [ -z "$(echo $1 |grep ^-.)" ]; then
62                if [ -n "$(echo $1)" ]; then 
63                    vm_cpu=$1
64                else
65                    echo "-c cpu_value is null"
66                    Usage && exit 2
67                fi
68            shift
69            fi
70            ;;
[195]71        -r|--ram)
[191]72            shift 
73            if [ -z "$(echo $1 |grep ^-.)" ]; then
74                if [ -n "$(echo $1)" ]; then
75                    vm_ram=$1
76                else
77                    echo "-r ram_value is null"
78                    Usage && exit 2
79                fi
80            shift 
81            fi 
82            ;; 
[199]83#        -h|--host)
84#            shift
85#            if [ -z "$(echo $1 |grep ^-.)" ]; then
86#                if [ -n "$(echo $1)" ]; then
87#                vm_host=$1
88#                else
89#                    echo "-h host_value is null"
90#                    Usage && exit 2
91#                fi
92#            shift
93#            fi
94#            ;;
[191]95        -*)
96            echo "$0 $1 invalid option" >&2
97            echo ""
98            Usage >&2
99            exit 2
100            ;;
101        *)  Usage >&2
102            exit 2
103            ;;
104    esac
105done
106
[199]107# check null value
108[ -z $vm_name ] && echo "[Error] no VM name" && Usage && exit 2
[200]109# check vm_name exist
110vm_IP_files=$(ls $Work_Home/etc/IP_VM_eth*)
111vm_exist_jude="no"
112for vm_IP_file in $vm_IP_files
113do
114    if [ -n "$(grep "$vm_name" $vm_IP_file)" ]; then
115        vm_exist_jude="yes"
116    fi
117   
118done
[199]119
[200]120if [ $vm_exist_jude == "no" ]; then
121    echo "[Error] This VM name isn't DRBL PXE VM"
122    exit 2
123fi
124
125
[191]126# = 2. Get VM MAC address =
127#IP_MAC_files=$(ls $Work_Home/etc | grep 'macadr-VM-eth[0-9]*.txt')
128IP_VM_files=$(ls $Work_Home/etc | grep 'IP_VM_eth[0-9]*')
129IP_VM_right_file=""
130IP_VM_right_line=""
131IP_VM_eth=""
132for IP_VM_file in $IP_VM_files
133do
[192]134    IP_VM_line=$(cat -n $Work_Home/etc/$IP_VM_file | grep "$vm_name" | awk '{print $1}') 
[191]135    if [ -n $IP_VM_line ]; then
136        IP_VM_right_file=$IP_VM_file
137        IP_VM_right_line=$IP_VM_line
138    fi
139done
140
141# if no value, exit and check IP_VM_ethX
142if [ -z $IP_VM_right_file ]; then
143    echo ""
144    echo "Error: don't fine ${vm_name}'s IP address"
145    echo "Please check $Wokr_Home/etc/"
146    exit 2
147fi
148
149# start get mac address
150IP_VM_eth=$(echo $IP_VM_right_file | sed 's/IP_VM_//g' )
[192]151vm_mac=$(cat $Work_Home/etc/macadr-VM-${IP_VM_eth}.txt | sed -n "${IP_VM_right_line}p")
[191]152
153# = 3. Generate PXE PV-VM configuration  =
154if [ ! -e $vm_dir ]; then
155    mkdir $vm_dir
156fi
157
[195]158vm_cfg=${vm_dir}/${vm_name}_PXE_PV-VM.cfg
[192]159
160if [ -e $vm_cfg ]; then
[191]161rm $vm_cfg
[192]162fi
[191]163
164# start to generate
165echo "kernel      = '/home/domains/vmlinuz-pxe'" > $vm_cfg
166echo "ramdisk     = '/home/domains/initrd-pxe.img'" >> $vm_cfg
[200]167echo "memory      = '$vm_ram'" >> $vm_cfg
[193]168echo "name        = '$vm_name'" >> $vm_cfg
169echo "vcpus        = '$vm_cpu'" >> $vm_cfg
[191]170echo "dhcp        = 'dhcp'" >> $vm_cfg
[193]171echo "vif         = [ 'mac=$vm_mac']" >> $vm_cfg
[191]172echo "on_poweroff = 'destroy'" >> $vm_cfg
173echo "on_reboot   = 'restart'" >> $vm_cfg
174echo "on_crash    = 'restart'" >> $vm_cfg
175echo "vfb = [ 'type=vnc,vnclisten=0.0.0.0,vncunused=1' ]" >> $vm_cfg
176echo "extra = 'console=hvc0 xencons=tty'" >> $vm_cfg
177
Note: See TracBrowser for help on using the repository browser.