1 | #!/bin/bash |
---|
2 | # Program: |
---|
3 | # Deploy PXE PV VM to DRBL client |
---|
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_ram="256" |
---|
21 | vm_mac="" |
---|
22 | vm_cpu="1" |
---|
23 | vm_dir="/home/domains" |
---|
24 | |
---|
25 | # = 2. Functions declation = |
---|
26 | Usage(){ |
---|
27 | echo "Usage: drbl_PXE_PV-VM_deploy [options]" |
---|
28 | echo "Options:" |
---|
29 | echo "-h|--host deploy vm to which host" |
---|
30 | echo "-v|--vm vm name" |
---|
31 | echo "-c|--cpu vm cpu (default: 1)" |
---|
32 | echo "-r|--ram vm ram size(M) (default: 256)" |
---|
33 | echo "Example:" |
---|
34 | echo "drbl_PXE_KVM-VM_deploy -h drbl101 -v drbl131 -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 | -h|--host) |
---|
48 | shift |
---|
49 | if [ -z "$(echo $1 |grep ^-.)" ]; then |
---|
50 | if [ -n "$(echo $1)" ]; then |
---|
51 | client_name=$1 |
---|
52 | else |
---|
53 | echo "-h host value is null" |
---|
54 | Usage && exit 2 |
---|
55 | fi |
---|
56 | shift |
---|
57 | fi |
---|
58 | ;; |
---|
59 | -v|--vm) |
---|
60 | shift |
---|
61 | if [ -z "$(echo $1 |grep ^-.)" ]; then |
---|
62 | if [ -n "$(echo $1)" ]; then |
---|
63 | vm_name=$1 |
---|
64 | else |
---|
65 | echo "-v vm name is null" |
---|
66 | Usage && exit 2 |
---|
67 | fi |
---|
68 | shift |
---|
69 | fi |
---|
70 | ;; |
---|
71 | -c|--cpu) |
---|
72 | if [ -z "$(echo $1 |grep ^-.)" ]; then |
---|
73 | if [ -n "$(echo $1)" ]; then |
---|
74 | vm_cpu=$1 |
---|
75 | else |
---|
76 | echo "-c vm cpu is null" |
---|
77 | Usage && exit 2 |
---|
78 | fi |
---|
79 | shift |
---|
80 | fi |
---|
81 | -r|--ram) |
---|
82 | shift |
---|
83 | if [ -z "$(echo $1 |grep ^-.)" ]; then |
---|
84 | if [ -n "$(echo $1)" ]; then |
---|
85 | vm_ram=$1 |
---|
86 | else |
---|
87 | echo "-r ram_value is null" |
---|
88 | Usage && exit 2 |
---|
89 | fi |
---|
90 | shift |
---|
91 | fi |
---|
92 | ;; |
---|
93 | -*) |
---|
94 | echo "$0 $1 invalid option" >&2 |
---|
95 | echo "" |
---|
96 | Usage >&2 |
---|
97 | exit 2 |
---|
98 | ;; |
---|
99 | *) Usage >&2 |
---|
100 | exit 2 |
---|
101 | ;; |
---|
102 | esac |
---|
103 | done |
---|
104 | |
---|
105 | |
---|
106 | # = 2. Check null value = |
---|
107 | [ -z $client_name ] && echo "[Error] no host" && Usage && exit 2 |
---|
108 | [ -z $vm_name ] && echo "[Error] no VM name" && Usage && exit 2 |
---|
109 | |
---|
110 | |
---|
111 | # = 3. Check exist |
---|
112 | # check drbl client exist |
---|
113 | cat $Work_Home/etc/IP_HOST_TABLE | grep $client_name |
---|
114 | if [ $? -ne 0 ]; then |
---|
115 | echo "[Error] This host is not DRBL client" |
---|
116 | exit 2 |
---|
117 | fi |
---|
118 | # check vm name exist |
---|
119 | IP_VM_files=$(ls $Work_Home/etc | grep 'IP_VM_eth[0-9]$') |
---|
120 | vm_exist_jude="no" |
---|
121 | for vm_IP_file in $IP_VM_files |
---|
122 | do |
---|
123 | if [ -n "$(grep "$vm_name" $Work_Home/etc/$vm_IP_file)" ]; then |
---|
124 | vm_exist_jude="yes" |
---|
125 | fi |
---|
126 | done |
---|
127 | |
---|
128 | if [ $vm_exist_jude == "no" ]; then |
---|
129 | echo "[Error] This VM name isn't DRBL PXE VM" |
---|
130 | exit 2 |
---|
131 | fi |
---|
132 | |
---|
133 | # 4. Get Mac Address |
---|
134 | IP_VM_right_file="" |
---|
135 | IP_VM_right_line="" |
---|
136 | IP_VM_eth="" |
---|
137 | for IP_VM_file in $IP_VM_files |
---|
138 | do |
---|
139 | IP_VM_line=$(cat -n $Work_Home/etc/$IP_VM_file | grep "$vm_name" | awk '{print $1}') |
---|
140 | if [ -n $IP_VM_line ]; then |
---|
141 | IP_VM_right_file=$IP_VM_file |
---|
142 | IP_VM_right_line=$IP_VM_line |
---|
143 | fi |
---|
144 | done |
---|
145 | |
---|
146 | # if no value, exit and check IP_VM_ethX |
---|
147 | if [ -z $IP_VM_right_file ]; then |
---|
148 | echo "" |
---|
149 | echo "[Error] don't fine ${vm_name}'s IP address" |
---|
150 | echo "Please check $Wokr_Home/etc/" |
---|
151 | exit 2 |
---|
152 | fi |
---|
153 | |
---|
154 | # start get mac address |
---|
155 | IP_VM_eth=$(echo $IP_VM_right_file | sed 's/IP_VM_//g' ) |
---|
156 | vm_mac=$(cat $Work_Home/etc/macadr-VM-${IP_VM_eth}.txt | sed -n "${IP_VM_right_line}p") |
---|
157 | |
---|
158 | # = 4. ssh host to deploy VM = |
---|
159 | # local varibales |
---|
160 | host_IP=$(cat $Work_Home/etc/IP_HOST_TABLE | grep $client_name | awk '{print $1}') |
---|
161 | |
---|
162 | # start ssh |
---|
163 | vnc_nus=$(ssh -o StrictHostKeyChecking=no $host_IP "netstat -tunlp | grep kvm | wc -l") |
---|
164 | vnc_nus=$((vnc_nus+1)) |
---|
165 | ssh -o StrictHostKeyChecking=no $host_IP "kvm -name $vm_name -smp $vm_cpu -m $vm_ram -net nic,macaddr=$vm_mac -net tap -vnc :$vnc_nus -boot n &" |
---|
166 | |
---|