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_dir="/home/domains" |
---|
20 | hypervisior=$(cat $Work_Home/etc/hypervisior) |
---|
21 | |
---|
22 | # = 2. Functions declation = |
---|
23 | Usage(){ |
---|
24 | echo "Usage: drbl_VM_migrate options" |
---|
25 | echo "Options:" |
---|
26 | echo "-l|--live live migration" |
---|
27 | echo "-v|--vm vm name" |
---|
28 | echo "-h|--host host name" |
---|
29 | echo "Example:" |
---|
30 | echo "drbl_VM_migrate -l -v pc131 -h pc102" |
---|
31 | } |
---|
32 | |
---|
33 | |
---|
34 | # [Main] |
---|
35 | check_root |
---|
36 | # = 1. Parse parameters = |
---|
37 | if [ $# -eq 0 ]; then |
---|
38 | Usage && exit |
---|
39 | fi |
---|
40 | |
---|
41 | while [ $# -gt 0 ]; do |
---|
42 | case "$1" in |
---|
43 | -h|--host) |
---|
44 | shift |
---|
45 | if [ -z "$(echo $1 |grep ^-.)" ]; then |
---|
46 | if [ -n "$(echo $1)" ]; then |
---|
47 | host="$1" |
---|
48 | else |
---|
49 | echo "-h host value is null" |
---|
50 | Usage && exit 2 |
---|
51 | fi |
---|
52 | shift |
---|
53 | fi |
---|
54 | ;; |
---|
55 | -v|--vm) |
---|
56 | shift |
---|
57 | if [ -z "$(echo $1 |grep ^-.)" ]; then |
---|
58 | if [ -n "$(echo $1)" ]; then |
---|
59 | vm="$1" |
---|
60 | else |
---|
61 | echo "-v vm value is null" |
---|
62 | Usage && exit 2 |
---|
63 | fi |
---|
64 | shift |
---|
65 | fi |
---|
66 | ;; |
---|
67 | -l|--live) |
---|
68 | shift |
---|
69 | xm_option="live" |
---|
70 | ;; |
---|
71 | -*) |
---|
72 | echo "$0 $1 invalid option" >&2 |
---|
73 | echo "" |
---|
74 | Usage >&2 |
---|
75 | exit 2 |
---|
76 | ;; |
---|
77 | *) Usage >&2 |
---|
78 | exit 2 |
---|
79 | ;; |
---|
80 | esac |
---|
81 | done |
---|
82 | |
---|
83 | |
---|
84 | # = 2. Check null value = |
---|
85 | [ -z "$host" ] && echo "[Error] no host" && Usage && exit 2 |
---|
86 | [ -z "$vm" ] && echo "[Error] no vm" && Usage && exit 2 |
---|
87 | |
---|
88 | |
---|
89 | # = 3. ssh client to execute command = |
---|
90 | # check network |
---|
91 | IP_status="online" |
---|
92 | ping -c1 -w1 $host > /dev/null 2>&1 || IP_status="offline" |
---|
93 | |
---|
94 | if [ $IP_status == "offline" ]; then |
---|
95 | echo "[Error] $host's network is offline !!!" |
---|
96 | else |
---|
97 | host_hostnames="$(grep -v "#" $Work_Home/etc/IP_HOST_TABLE | awk '{print $2}')" |
---|
98 | |
---|
99 | for host_hostname in $host_hostnames |
---|
100 | do |
---|
101 | ssh -o StrictHostKeyChecking=no $host_hostname "xm list | grep $vm" > /tmp/vm_in_host_file$$ |
---|
102 | vm_in_host=$(cat /tmp/vm_in_host_file$$) |
---|
103 | if [ -n "$vm_in_host" ]; then |
---|
104 | right_host=$host_hostname |
---|
105 | break |
---|
106 | fi |
---|
107 | echo "$host_name" |
---|
108 | done |
---|
109 | |
---|
110 | if [ -z $xm_option ]; then |
---|
111 | ssh -o StrictHostKeyChecking=no $right_host "xm migrate $vm $host" |
---|
112 | else |
---|
113 | ssh -o StrictHostKeyChecking=no $right_host "xm migrate --$xm_option $vm $host" |
---|
114 | fi |
---|
115 | fi |
---|
116 | |
---|
117 | rm /tmp/vm_in_host_file$$ |
---|
118 | # check |
---|
119 | check_drbl_vm |
---|
120 | |
---|