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] |
---|
12 | source /opt/drbl-virt/conf/drbl-virt.conf |
---|
13 | source $Work_Path/functions_drbl_virt |
---|
14 | #source ./functions_drbl_PXE_PV-VM_create |
---|
15 | |
---|
16 | |
---|
17 | # [Declation] |
---|
18 | # = 1. Varibales declation = |
---|
19 | vm_name="" |
---|
20 | vm_cpu="1" |
---|
21 | vm_ram="128" |
---|
22 | #vm_host="" |
---|
23 | vm_mac="" |
---|
24 | |
---|
25 | vm_dir="/home/domains" |
---|
26 | # = 2. Functions declation = |
---|
27 | Usage(){ |
---|
28 | echo "Usage: drbl_PXE_PV-VM_create.sh options" |
---|
29 | echo "Options: " |
---|
30 | echo "-v|--vm vm name" |
---|
31 | echo "-c|--cpu cpu number (default: 1)" |
---|
32 | echo "-r|--ram ram size(M) (default: 128)" |
---|
33 | echo "Examples: " |
---|
34 | echo "drbl_PXE_PV-VM_create.sh options -v drbl131 -c 2 -r 512" |
---|
35 | } |
---|
36 | |
---|
37 | |
---|
38 | # [Main] |
---|
39 | check_root |
---|
40 | # = 1. Parse parameters = |
---|
41 | if [ $# -eq 0 ]; then |
---|
42 | Usage && exit |
---|
43 | fi |
---|
44 | |
---|
45 | while [ $# -gt 0 ]; do |
---|
46 | case "$1" in |
---|
47 | -v|--vm) |
---|
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 | ;; |
---|
59 | -c|--cpu) |
---|
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 | ;; |
---|
71 | -r|--ram) |
---|
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 | ;; |
---|
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 | # ;; |
---|
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 |
---|
105 | done |
---|
106 | |
---|
107 | # check null value |
---|
108 | [ -z $vm_name ] && echo "[Error] no VM name" && Usage && exit 2 |
---|
109 | # check vm_name exist |
---|
110 | vm_IP_files=$(ls $Work_Home/etc/IP_VM_eth*) |
---|
111 | vm_exist_jude="no" |
---|
112 | for vm_IP_file in $vm_IP_files |
---|
113 | do |
---|
114 | if [ -n "$(grep "$vm_name" $vm_IP_file)" ]; then |
---|
115 | vm_exist_jude="yes" |
---|
116 | fi |
---|
117 | |
---|
118 | done |
---|
119 | |
---|
120 | if [ $vm_exist_jude == "no" ]; then |
---|
121 | echo "[Error] This VM name isn't DRBL PXE VM" |
---|
122 | exit 2 |
---|
123 | fi |
---|
124 | |
---|
125 | |
---|
126 | # = 2. Get VM MAC address = |
---|
127 | #IP_MAC_files=$(ls $Work_Home/etc | grep 'macadr-VM-eth[0-9]*.txt') |
---|
128 | IP_VM_files=$(ls $Work_Home/etc | grep 'IP_VM_eth[0-9]*') |
---|
129 | IP_VM_right_file="" |
---|
130 | IP_VM_right_line="" |
---|
131 | IP_VM_eth="" |
---|
132 | for IP_VM_file in $IP_VM_files |
---|
133 | do |
---|
134 | IP_VM_line=$(cat -n $Work_Home/etc/$IP_VM_file | grep "$vm_name" | awk '{print $1}') |
---|
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 |
---|
139 | done |
---|
140 | |
---|
141 | # if no value, exit and check IP_VM_ethX |
---|
142 | if [ -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 |
---|
147 | fi |
---|
148 | |
---|
149 | # start get mac address |
---|
150 | IP_VM_eth=$(echo $IP_VM_right_file | sed 's/IP_VM_//g' ) |
---|
151 | vm_mac=$(cat $Work_Home/etc/macadr-VM-${IP_VM_eth}.txt | sed -n "${IP_VM_right_line}p") |
---|
152 | |
---|
153 | # = 3. Generate PXE PV-VM configuration = |
---|
154 | if [ ! -e $vm_dir ]; then |
---|
155 | mkdir $vm_dir |
---|
156 | fi |
---|
157 | |
---|
158 | vm_cfg=${vm_dir}/${vm_name}_PXE_PV-VM.cfg |
---|
159 | |
---|
160 | if [ -e $vm_cfg ]; then |
---|
161 | rm $vm_cfg |
---|
162 | fi |
---|
163 | |
---|
164 | # start to generate |
---|
165 | echo "kernel = '/home/domains/vmlinuz-pxe'" > $vm_cfg |
---|
166 | echo "ramdisk = '/home/domains/initrd-pxe.img'" >> $vm_cfg |
---|
167 | echo "memory = '$vm_ram'" >> $vm_cfg |
---|
168 | echo "name = '$vm_name'" >> $vm_cfg |
---|
169 | echo "vcpus = '$vm_cpu'" >> $vm_cfg |
---|
170 | echo "dhcp = 'dhcp'" >> $vm_cfg |
---|
171 | echo "vif = [ 'mac=$vm_mac']" >> $vm_cfg |
---|
172 | echo "on_poweroff = 'destroy'" >> $vm_cfg |
---|
173 | echo "on_reboot = 'restart'" >> $vm_cfg |
---|
174 | echo "on_crash = 'restart'" >> $vm_cfg |
---|
175 | echo "vfb = [ 'type=vnc,vnclisten=0.0.0.0,vncunused=1' ]" >> $vm_cfg |
---|
176 | echo "extra = 'console=hvc0 xencons=tty'" >> $vm_cfg |
---|
177 | |
---|