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 [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 -v drbl131 -c 2 -r 512" |
---|
35 | } |
---|
36 | |
---|
37 | |
---|
38 | # [Main] |
---|
39 | check_root |
---|
40 | # = 1. Parse parameters = |
---|
41 | while [ $# -gt 0 ]; do |
---|
42 | case "$1" in |
---|
43 | -v|--vm) |
---|
44 | shift |
---|
45 | if [ -z "$(echo $1 |grep ^-.)" ]; then |
---|
46 | if [ -n "$(echo $1)" ]; then |
---|
47 | vm_name=$1 |
---|
48 | else |
---|
49 | echo "-v vm_name is null" |
---|
50 | Usage && exit 2 |
---|
51 | fi |
---|
52 | shift |
---|
53 | fi |
---|
54 | ;; |
---|
55 | -c|--cpu) |
---|
56 | shift |
---|
57 | if [ -z "$(echo $1 |grep ^-.)" ]; then |
---|
58 | if [ -n "$(echo $1)" ]; then |
---|
59 | vm_cpu=$1 |
---|
60 | else |
---|
61 | echo "-c cpu_value is null" |
---|
62 | Usage && exit 2 |
---|
63 | fi |
---|
64 | shift |
---|
65 | fi |
---|
66 | ;; |
---|
67 | -r|--ram) |
---|
68 | shift |
---|
69 | if [ -z "$(echo $1 |grep ^-.)" ]; then |
---|
70 | if [ -n "$(echo $1)" ]; then |
---|
71 | vm_ram=$1 |
---|
72 | else |
---|
73 | echo "-r ram_value is null" |
---|
74 | Usage && exit 2 |
---|
75 | fi |
---|
76 | shift |
---|
77 | fi |
---|
78 | ;; |
---|
79 | # -h|--host) |
---|
80 | # shift |
---|
81 | # if [ -z "$(echo $1 |grep ^-.)" ]; then |
---|
82 | # if [ -n "$(echo $1)" ]; then |
---|
83 | # vm_host=$1 |
---|
84 | # else |
---|
85 | # echo "-h host_value is null" |
---|
86 | # Usage && exit 2 |
---|
87 | # fi |
---|
88 | # shift |
---|
89 | # fi |
---|
90 | # ;; |
---|
91 | -*) |
---|
92 | echo "$0 $1 invalid option" >&2 |
---|
93 | echo "" |
---|
94 | Usage >&2 |
---|
95 | exit 2 |
---|
96 | ;; |
---|
97 | *) Usage >&2 |
---|
98 | exit 2 |
---|
99 | ;; |
---|
100 | esac |
---|
101 | done |
---|
102 | |
---|
103 | # check null value |
---|
104 | [ -z $vm_name ] && echo "[Error] no VM name" && Usage && exit 2 |
---|
105 | # check vm_name exist |
---|
106 | IP_VM_files=$(ls $Work_Home/etc | grep 'IP_VM_eth[0-9]$') |
---|
107 | vm_exist_jude="no" |
---|
108 | for vm_IP_file in $IP_VM_files |
---|
109 | do |
---|
110 | if [ -n "$(grep "$vm_name" $Work_Home/etc/$vm_IP_file)" ]; then |
---|
111 | vm_exist_jude="yes" |
---|
112 | fi |
---|
113 | done |
---|
114 | |
---|
115 | if [ $vm_exist_jude == "no" ]; then |
---|
116 | echo "[Error] This VM name isn't DRBL PXE VM" |
---|
117 | exit 2 |
---|
118 | fi |
---|
119 | |
---|
120 | |
---|
121 | # = 2. Get VM MAC address = |
---|
122 | #IP_MAC_files=$(ls $Work_Home/etc | grep 'macadr-VM-eth[0-9]*.txt') |
---|
123 | IP_VM_right_file="" |
---|
124 | IP_VM_right_line="" |
---|
125 | IP_VM_eth="" |
---|
126 | for IP_VM_file in $IP_VM_files |
---|
127 | do |
---|
128 | IP_VM_line=$(cat -n $Work_Home/etc/$IP_VM_file | grep "$vm_name" | awk '{print $1}') |
---|
129 | if [ -n $IP_VM_line ]; then |
---|
130 | IP_VM_right_file=$IP_VM_file |
---|
131 | IP_VM_right_line=$IP_VM_line |
---|
132 | fi |
---|
133 | done |
---|
134 | |
---|
135 | # if no value, exit and check IP_VM_ethX |
---|
136 | if [ -z $IP_VM_right_file ]; then |
---|
137 | echo "" |
---|
138 | echo "[Error] don't fine ${vm_name}'s IP address" |
---|
139 | echo "Please check $Wokr_Home/etc/" |
---|
140 | exit 2 |
---|
141 | fi |
---|
142 | |
---|
143 | # start get mac address |
---|
144 | IP_VM_eth=$(echo $IP_VM_right_file | sed 's/IP_VM_//g' ) |
---|
145 | vm_mac=$(cat $Work_Home/etc/macadr-VM-${IP_VM_eth}.txt | sed -n "${IP_VM_right_line}p") |
---|
146 | |
---|
147 | # = 3. Generate PXE PV-VM configuration = |
---|
148 | if [ ! -e $vm_dir ]; then |
---|
149 | mkdir $vm_dir |
---|
150 | fi |
---|
151 | |
---|
152 | vm_cfg=${vm_dir}/${vm_name}_PXE_PV-VM.cfg |
---|
153 | |
---|
154 | if [ -e $vm_cfg ]; then |
---|
155 | rm $vm_cfg |
---|
156 | fi |
---|
157 | |
---|
158 | # start to generate |
---|
159 | echo "kernel = '/home/domains/vmlinuz-pxe'" > $vm_cfg |
---|
160 | echo "ramdisk = '/home/domains/initrd-pxe.img'" >> $vm_cfg |
---|
161 | echo "memory = '$vm_ram'" >> $vm_cfg |
---|
162 | echo "name = '$vm_name'" >> $vm_cfg |
---|
163 | echo "vcpus = '$vm_cpu'" >> $vm_cfg |
---|
164 | echo "dhcp = 'dhcp'" >> $vm_cfg |
---|
165 | echo "vif = [ 'mac=$vm_mac']" >> $vm_cfg |
---|
166 | echo "on_poweroff = 'destroy'" >> $vm_cfg |
---|
167 | echo "on_reboot = 'restart'" >> $vm_cfg |
---|
168 | echo "on_crash = 'restart'" >> $vm_cfg |
---|
169 | echo "vfb = [ 'type=vnc,vnclisten=0.0.0.0,vncunused=1' ]" >> $vm_cfg |
---|
170 | echo "extra = 'console=hvc0 xencons=tty'" >> $vm_cfg |
---|
171 | |
---|
172 | # |
---|
173 | echo "$vm_cfg is completion" |
---|
174 | echo "Use this command drbl_PXE_PV-VM_deploy to deploy vm" |
---|