source: drbl-virt/sbin/drbl_PXE_PV-VM_create @ 223

Last change on this file since 223 was 221, checked in by rock, 14 years ago

Bugfix: fix copy sequence

  • Property svn:executable set to *
File size: 4.4 KB
Line 
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]
12source /opt/drbl-virt/conf/drbl-virt.conf
13source $Work_Path/functions_drbl_virt
14#source ./functions_drbl_PXE_PV-VM_create
15
16
17# [Declation]
18# = 1. Varibales declation =
19vm_name=""
20vm_cpu="1"
21vm_ram="128"
22#vm_host=""
23vm_mac=""
24
25vm_dir="/home/domains"
26# = 2. Functions declation =
27Usage(){
28echo "Usage: drbl_PXE_PV-VM_create [options]"
29echo "Options: "
30echo "-v|--vm      vm name"
31echo "-c|--cpu     cpu number (default: 1)"
32echo "-r|--ram     ram size(M) (default: 128)"
33echo "Examples: "
34echo "drbl_PXE_PV-VM_create -v drbl131 -c 2 -r 512"
35}
36
37
38# [Main]
39check_root
40# = 1. Parse parameters =
41while [ $# -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
101done
102
103# check null value
104[ -z $vm_name ] && echo "[Error] no VM name" && Usage && exit 2
105# check vm_name exist
106IP_VM_files=$(ls $Work_Home/etc | grep 'IP_VM_eth[0-9]$')
107vm_exist_jude="no"
108for vm_IP_file in $IP_VM_files
109do
110    if [ -n "$(grep "$vm_name" $Work_Home/etc/$vm_IP_file)" ]; then
111        vm_exist_jude="yes"
112    fi
113done
114
115if [ $vm_exist_jude == "no" ]; then
116    echo "[Error] This VM name isn't DRBL PXE VM"
117    exit 2
118fi
119
120
121# = 2. Get VM MAC address =
122#IP_MAC_files=$(ls $Work_Home/etc | grep 'macadr-VM-eth[0-9]*.txt')
123IP_VM_right_file=""
124IP_VM_right_line=""
125IP_VM_eth=""
126for IP_VM_file in $IP_VM_files
127do
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
133done
134
135# if no value, exit and check IP_VM_ethX
136if [ -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
141fi
142
143# start get mac address
144IP_VM_eth=$(echo $IP_VM_right_file | sed 's/IP_VM_//g' )
145vm_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  =
148if [ ! -e $vm_dir ]; then
149    mkdir $vm_dir
150fi
151
152vm_cfg=${vm_dir}/${vm_name}_PXE_PV-VM.cfg
153
154if [ -e $vm_cfg ]; then
155rm $vm_cfg
156fi
157
158# start to generate
159echo "kernel      = '/home/domains/vmlinuz-pxe'" > $vm_cfg
160echo "ramdisk     = '/home/domains/initrd-pxe.img'" >> $vm_cfg
161echo "memory      = '$vm_ram'" >> $vm_cfg
162echo "name        = '$vm_name'" >> $vm_cfg
163echo "vcpus        = '$vm_cpu'" >> $vm_cfg
164echo "dhcp        = 'dhcp'" >> $vm_cfg
165echo "vif         = [ 'mac=$vm_mac']" >> $vm_cfg
166echo "on_poweroff = 'destroy'" >> $vm_cfg
167echo "on_reboot   = 'restart'" >> $vm_cfg
168echo "on_crash    = 'restart'" >> $vm_cfg
169echo "vfb = [ 'type=vnc,vnclisten=0.0.0.0,vncunused=1' ]" >> $vm_cfg
170echo "extra = 'console=hvc0 xencons=tty'" >> $vm_cfg
171
172#
173echo "$vm_cfg is completion"
174echo "Use this command drbl_PXE_PV-VM_deploy to deploy vm"
Note: See TracBrowser for help on using the repository browser.