source: drbl-virt/sbin/drbl_PXE_KVM-VM_deploy @ 220

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

Modify: KVM smp support

  • Property svn:executable set to *
File size: 4.2 KB
Line 
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]
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_ram="256"
21vm_mac=""
22vm_cpu="1"
23vm_dir="/home/domains"
24
25# = 2. Functions declation =
26Usage(){
27echo "Usage: drbl_PXE_PV-VM_deploy [options]"
28echo "Options:"
29echo "-h|--host      deploy vm to which host"
30echo "-v|--vm        vm name"
31echo "-c|--cpu       vm cpu (default: 1)"
32echo "-r|--ram       vm ram size(M) (default: 256)"
33echo "Example:"
34echo "drbl_PXE_KVM-VM_deploy -h drbl101 -v drbl131 -r 512"
35}
36
37
38# [Main]
39check_root
40# = 1. Parse parameters =
41if [ $# -eq 0 ]; then
42Usage && exit
43fi
44
45while [ $# -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
103done
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
113cat $Work_Home/etc/IP_HOST_TABLE | grep $client_name
114if [ $? -ne 0 ]; then
115    echo "[Error] This host is not DRBL client"
116    exit 2
117fi
118# check vm name exist
119IP_VM_files=$(ls $Work_Home/etc | grep 'IP_VM_eth[0-9]$')
120vm_exist_jude="no"
121for vm_IP_file in $IP_VM_files
122do
123    if [ -n "$(grep "$vm_name" $Work_Home/etc/$vm_IP_file)" ]; then
124        vm_exist_jude="yes"
125    fi
126done
127
128if [ $vm_exist_jude == "no" ]; then
129    echo "[Error] This VM name isn't DRBL PXE VM"
130    exit 2
131fi
132
133# 4. Get Mac Address
134IP_VM_right_file=""
135IP_VM_right_line=""
136IP_VM_eth=""
137for IP_VM_file in $IP_VM_files
138do
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
144done
145
146# if no value, exit and check IP_VM_ethX
147if [ -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
152fi
153
154# start get mac address
155IP_VM_eth=$(echo $IP_VM_right_file | sed 's/IP_VM_//g' )
156vm_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
160host_IP=$(cat $Work_Home/etc/IP_HOST_TABLE | grep $client_name | awk '{print $1}')
161
162# start ssh
163vnc_nus=$(ssh -o StrictHostKeyChecking=no $host_IP "netstat -tunlp | grep kvm | wc -l")
164vnc_nus=$((vnc_nus+1))
165ssh -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
Note: See TracBrowser for help on using the repository browser.