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

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

Add: KVM PXE VM deployment

  • Property svn:executable set to *
File size: 3.8 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=""
22
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 "-r|--ram       vm ram size(M) (default: 256)"
32echo "Example:"
33echo "drbl_PXE_KVM-VM_deploy -h drbl101 -v drbl131 -r 512"
34}
35
36
37# [Main]
38check_root
39# = 1. Parse parameters =
40if [ $# -eq 0 ]; then
41Usage && exit
42fi
43
44while [ $# -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
92done
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
102cat $Work_Home/etc/IP_HOST_TABLE | grep $client_name
103if [ $? -ne 0 ]; then
104    echo "[Error] This host is not DRBL client"
105    exit 2
106fi
107# check vm name exist
108IP_VM_files=$(ls $Work_Home/etc | grep 'IP_VM_eth[0-9]$')
109vm_exist_jude="no"
110for vm_IP_file in $IP_VM_files
111do
112    if [ -n "$(grep "$vm_name" $Work_Home/etc/$vm_IP_file)" ]; then
113        vm_exist_jude="yes"
114    fi
115done
116
117if [ $vm_exist_jude == "no" ]; then
118    echo "[Error] This VM name isn't DRBL PXE VM"
119    exit 2
120fi
121
122# 4. Get Mac Address
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# = 4. ssh host to deploy VM  =
148# local varibales
149host_IP=$(cat $Work_Home/etc/IP_HOST_TABLE | grep $client_name | awk '{print $1}')
150
151# start ssh
152vnc_nus=$(ssh -o StrictHostKeyChecking=no $host_IP "netstat -tunlp | grep kvm | wc -l")
153vnc_nus=$((vnc_nus+1))
154ssh -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
Note: See TracBrowser for help on using the repository browser.