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