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 | |
---|
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 "-r|--ram vm ram size(M) (default: 256)" |
---|
32 | echo "Example:" |
---|
33 | echo "drbl_PXE_KVM-VM_deploy -h drbl101 -v drbl131 -r 512" |
---|
34 | } |
---|
35 | |
---|
36 | |
---|
37 | # [Main] |
---|
38 | check_root |
---|
39 | # = 1. Parse parameters = |
---|
40 | if [ $# -eq 0 ]; then |
---|
41 | Usage && exit |
---|
42 | fi |
---|
43 | |
---|
44 | while [ $# -gt 0 ]; do |
---|
45 | case "$1" in |
---|
46 | -h|--host) |
---|
47 | shift |
---|
48 | if [ -z "$(echo $1 |grep ^-.)" ]; then |
---|
49 | if [ -n "$(echo $1)" ]; then |
---|
50 | client_name=$1 |
---|
51 | else |
---|
52 | echo "-h host value is null" |
---|
53 | Usage && exit 2 |
---|
54 | fi |
---|
55 | shift |
---|
56 | fi |
---|
57 | ;; |
---|
58 | -v|--vm) |
---|
59 | shift |
---|
60 | if [ -z "$(echo $1 |grep ^-.)" ]; then |
---|
61 | if [ -n "$(echo $1)" ]; then |
---|
62 | vm_name=$1 |
---|
63 | else |
---|
64 | echo "-v vm name is null" |
---|
65 | Usage && exit 2 |
---|
66 | fi |
---|
67 | shift |
---|
68 | fi |
---|
69 | ;; |
---|
70 | -r|--ram) |
---|
71 | shift |
---|
72 | if [ -z "$(echo $1 |grep ^-.)" ]; then |
---|
73 | if [ -n "$(echo $1)" ]; then |
---|
74 | vm_ram=$1 |
---|
75 | else |
---|
76 | echo "-r ram_value is null" |
---|
77 | Usage && exit 2 |
---|
78 | fi |
---|
79 | shift |
---|
80 | fi |
---|
81 | ;; |
---|
82 | -*) |
---|
83 | echo "$0 $1 invalid option" >&2 |
---|
84 | echo "" |
---|
85 | Usage >&2 |
---|
86 | exit 2 |
---|
87 | ;; |
---|
88 | *) Usage >&2 |
---|
89 | exit 2 |
---|
90 | ;; |
---|
91 | esac |
---|
92 | done |
---|
93 | |
---|
94 | |
---|
95 | # = 2. Check null value = |
---|
96 | [ -z $client_name ] && echo "[Error] no host" && Usage && exit 2 |
---|
97 | [ -z $vm_name ] && echo "[Error] no VM name" && Usage && exit 2 |
---|
98 | |
---|
99 | |
---|
100 | # = 3. Check exist |
---|
101 | # check drbl client exist |
---|
102 | cat $Work_Home/etc/IP_HOST_TABLE | grep $client_name |
---|
103 | if [ $? -ne 0 ]; then |
---|
104 | echo "[Error] This host is not DRBL client" |
---|
105 | exit 2 |
---|
106 | fi |
---|
107 | # check vm name exist |
---|
108 | IP_VM_files=$(ls $Work_Home/etc | grep 'IP_VM_eth[0-9]$') |
---|
109 | vm_exist_jude="no" |
---|
110 | for vm_IP_file in $IP_VM_files |
---|
111 | do |
---|
112 | if [ -n "$(grep "$vm_name" $Work_Home/etc/$vm_IP_file)" ]; then |
---|
113 | vm_exist_jude="yes" |
---|
114 | fi |
---|
115 | done |
---|
116 | |
---|
117 | if [ $vm_exist_jude == "no" ]; then |
---|
118 | echo "[Error] This VM name isn't DRBL PXE VM" |
---|
119 | exit 2 |
---|
120 | fi |
---|
121 | |
---|
122 | # 4. Get Mac Address |
---|
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 | # = 4. ssh host to deploy VM = |
---|
148 | # local varibales |
---|
149 | host_IP=$(cat $Work_Home/etc/IP_HOST_TABLE | grep $client_name | awk '{print $1}') |
---|
150 | |
---|
151 | # start ssh |
---|
152 | vnc_nus=$(ssh -o StrictHostKeyChecking=no $host_IP "netstat -tunlp | grep kvm | wc -l") |
---|
153 | vnc_nus=$((vnc_nus+1)) |
---|
154 | ssh -o StrictHostKeyChecking=no $host_IP "kvm -name $vm_name -m $vm_ram -net nic,macaddr=$vm_mac -net tap -vnc :$vnc_nus -boot n &" |
---|
155 | |
---|