tm_clone.sh

Marco Gomes, 21 Mar 2012 12:36

Download (2.85 KB)

 
1
#!/bin/bash
2

    
3
# -------------------------------------------------------------------------- #
4
# Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org)             #
5
#                                                                            #
6
# Licensed under the Apache License, Version 2.0 (the "License"); you may    #
7
# not use this file except in compliance with the License. You may obtain    #
8
# a copy of the License at                                                   #
9
#                                                                            #
10
# http://www.apache.org/licenses/LICENSE-2.0                                 #
11
#                                                                            #
12
# Unless required by applicable law or agreed to in writing, software        #
13
# distributed under the License is distributed on an "AS IS" BASIS,          #
14
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
15
# See the License for the specific language governing permissions and        #
16
# limitations under the License.                                             #
17
#--------------------------------------------------------------------------- #
18

    
19
SRC=$1
20
DST=$2
21
SIZE=$3
22

    
23
if [ -z "${ONE_LOCATION}" ]; then
24
    TMCOMMON=/usr/lib/one/mads/tm_common.sh
25
    ISCSIRC=/etc/one/tm_iscsi/tm_iscsirc
26
else
27
    TMCOMMON=$ONE_LOCATION/lib/mads/tm_common.sh
28
    ISCSIRC=$ONE_LOCATION/etc/tm_iscsi/tm_iscsirc
29
fi
30

    
31
. $TMCOMMON
32
. $ISCSIRC
33

    
34
SRC_PATH=`arg_path $SRC`
35
DST_PATH=`arg_path $DST`
36

    
37
SRC_HOST=`arg_host $SRC`
38
DST_HOST=`arg_host $DST`
39

    
40
if [ -z $SIZE ] ; then
41
    SIZE=$DEFAULT_LV_SIZE
42
fi
43

    
44
LV_NAME=`get_lv_name $DST_PATH`
45

    
46
log "$1 $2"
47
log "DST: $DST_PATH"
48

    
49
DST_DIR=$(dirname $DST_PATH)
50

    
51
log "Creating directory $DST_DIR"
52
    exec_and_log "$SSH $DST_HOST mkdir -p $DST_DIR"
53

    
54
    log "Cloning LV $LV_NAME"
55
    exec_and_log "$SSH $STORAGE_HOST $SUDO $LVCREATE -s -L$SIZE -n $LV_NAME $SRC_PATH"
56

    
57
    log "Creating and exporting iscsi target"
58
    exec_and_log "$SSH $STORAGE_HOST $SUDO $ISCSIADD $LV_NAME $VG_NAME"
59

    
60
    log "Initiating iscsi target"
61
    exec_and_log "$SSH $DST_HOST $SUDO $ISCSIADM -m discovery -t st -p $STORAGE_HOST"
62
    exec_and_log "$SSH $DST_HOST $SUDO $ISCSIADM -m node | tee /tmp/$LV_NAME"
63

    
64
    ISCSI_TARGET=$($SSH $DST_HOST $CAT /tmp/$LV_NAME | $GREP $LV_NAME | $CUT -d " " -f2)
65
    ISCSI_HOST=$($SSH $DST_HOST $CAT /tmp/$LV_NAME | $GREP $LV_NAME | $CUT -d " " -f1 | $CUT -d "," -f1)
66

    
67
    exec_and_log "$SSH $DST_HOST $SUDO $ISCSIADM -m node --targetname \"$ISCSI_TARGET\" --portal \"$ISCSI_HOST\" --login"
68
   
69
    exec_and_log "$SSH $DST_HOST rm /tmp/$LV_NAME"
70

    
71
    log "Linking $DST_PATH to the iscsi device $DEVICE"
72
    exec_and_log "$SSH $DST_HOST $LN -s /dev/disk/by-path/ip-$(echo $ISCSI_HOST | $CUT -d ":" -f1)\:$(echo $ISCSI_HOST | $CUT -d ":" -f2)-iscsi-$(echo $ISCSI_TARGET | $CUT -d ":" -f1)\:$(echo $ISCSI_TARGET | $CUT -d ":" -f2)-lun-0 $DST_PATH"