iscsi_persistent_v3.patch

SZTAKI LPDS, 05/15/2012 03:42 PM

Download (12.4 KB)

View differences:

src/datastore_mad/remotes/iscsi/cp
74 74
IQN="$BASE_IQN:$DST_HOST.$VG_NAME.$LV_NAME"
75 75
DEV="/dev/$VG_NAME/$LV_NAME"
76 76

  
77
let TID=ID+BASE_TID
77
NEWTARGET_CMD=$(cat <<EOF
78
    set -e
79
    $SUDO $LVCREATE -L${SIZE}M ${VG_NAME} -n ${LV_NAME}
80
    TID=\`$SUDO $TGTADM --lld iscsi --op show --mode target | grep "Target" | tail -n 1 | awk '{split(\$2,tmp,":"); print tmp[1]+1;}'\`
81
    if [ -z \$TID ]; then TID=1; fi
82
    $SUDO $(tgtadm_target_new \$TID "$IQN")
83
EOF
84
)
85
ssh_exec_and_log "$DST_HOST" "$NEWTARGET_CMD" \
86
"Error creating iscsi target $DST_HOST:$DEV"
87
unset NEWTARGET_CMD
88

  
89
TID=`$SSH $DST_HOST $SUDO $(tgtadm_get_tid_for_iqn $IQN)`
78 90

  
79 91
REGISTER_CMD=$(cat <<EOF
80 92
    set -e
81
    $SUDO $LVCREATE -L${SIZE}M ${VG_NAME} -n ${LV_NAME}
82
    $SUDO $(tgtadm_target_new "$TID" "$IQN")
83 93
    $SUDO $(tgtadm_target_bind_all "$TID")
84 94
    $SUDO $(tgtadm_logicalunit_new "$TID" "$DEV")
95
    $SUDO $(tgt-admin_dump_config /etc/tgt/targets.conf)
85 96
EOF
86 97
)
87 98

  
src/datastore_mad/remotes/iscsi/iscsi.conf
18 18
HOST=localhost
19 19

  
20 20
# Default IQN path
21
BASE_IQN=iqn.2012-02.org.opennebula
21
BASE_IQN=iqn.2012-04.org.opennebula
22 22

  
23 23
# Default volume group
24 24
VG_NAME=vg-one
25 25

  
26
# Starting TID for iSCSI targets
27
BASE_TID=1
28

  
29 26
# Lists of hosts (separated by spaces) for which no iscsiadm login or logout
30 27
# is performed.
31 28
NO_ISCSI="$HOSTNAME"
src/datastore_mad/remotes/iscsi/mkfs
75 75
IQN="$BASE_IQN:$DST_HOST.$VG_NAME.$LV_NAME"
76 76
DEV="/dev/$VG_NAME/$LV_NAME"
77 77

  
78
let TID=ID+BASE_TID
78
# ------------ Image to save_as disk, no need to create a FS ------------
79 79

  
80
REGISTER_CMD=$(cat <<EOF
80
if [ "$FSTYPE" = "save_as" ]; then
81
    NEWTARGET_CMD=$(cat <<EOF
81 82
    set -e
82 83
    $SUDO $LVCREATE -L${SIZE}M ${VG_NAME} -n ${LV_NAME}
83
    $SUDO $(tgtadm_target_new "$TID" "$IQN")
84
    TID=\`$SUDO $TGTADM --lld iscsi --op show --mode target | grep "Target" | tail -n 1 | awk '{split(\$2,tmp,":"); print tmp[1]+1;}'\`
85
    if [ -z \$TID ]; then TID=1; fi
86
    $SUDO $(tgtadm_target_new \$TID "$IQN")
87
EOF
88
)
89
    ssh_exec_and_log "$DST_HOST" "$NEWTARGET_CMD" \
90
    "Error creating iscsi target $DST_HOST:$DEV"
91

  
92
    TID=`$SSH $DST_HOST $SUDO $(tgtadm_get_tid_for_iqn $IQN)`
93

  
94
    REGISTER_CMD=$(cat <<EOF
95
    set -e
84 96
    $SUDO $(tgtadm_target_bind_all "$TID")
85 97
    $SUDO $(tgtadm_logicalunit_new "$TID" "$DEV")
86
    $SUDO $(mkfs_command "$DEV" "$FSTYPE")
98
    $SUDO $(tgt-admin_dump_config /etc/tgt/targets.conf)
87 99
EOF
88 100
)
101
    ssh_exec_and_log "$DST_HOST" "$REGISTER_CMD" \
102
    "Error setting up iscsi target $DST_HOST:$DEV"
89 103

  
90
# ------------ Image to save_as disk, no need to create a FS ------------
91

  
92
if [ "$FSTYPE" = "save_as" ]; then
93
    echo "$DST $SIZE"
104
    echo "$IQN $SIZE"
94 105
    exit 0
95 106
fi
96 107

  
97 108
# ------------ Create the image to the repository ------------
98 109

  
110
NEWTARGET_CMD=$(cat <<EOF
111
    set -e
112
    $SUDO $LVCREATE -L${SIZE}M ${VG_NAME} -n ${LV_NAME}
113
    TID=\`$SUDO $TGTADM --lld iscsi --op show --mode target | grep "Target" | tail -n 1 | awk '{split(\$2,tmp,":"); print tmp[1]+1;}'\`
114
    if [ -z \$TID ]; then TID=1; fi
115
    $SUDO $(tgtadm_target_new \$TID "$IQN")
116
EOF
117
)
118
ssh_exec_and_log "$DST_HOST" "$NEWTARGET_CMD" \
119
    "Error creating iscsi target $DST_HOST:$DEV"
120

  
121
TID=`$SSH $DST_HOST $SUDO $(tgtadm_get_tid_for_iqn $IQN)`
122

  
123
REGISTER_CMD=$(cat <<EOF
124
    set -e
125
    $SUDO $(tgtadm_target_bind_all "$TID")
126
    $SUDO $(tgtadm_logicalunit_new "$TID" "$DEV")
127
    $SUDO $(tgt-admin_dump_config /etc/tgt/targets.conf)
128
    $SUDO $(mkfs_command "$DEV" "$FSTYPE")
129
EOF
130
)
131

  
99 132
ssh_exec_and_log "$DST_HOST" "$REGISTER_CMD" \
100 133
    "Error registering $DST_HOST:$DEV"
101 134

  
src/datastore_mad/remotes/iscsi/rm
58 58
VG_NAME=`echo $TARGET|$AWK -F. '{print $(NF-1)}'`
59 59
DEV="/dev/$VG_NAME/$LV_NAME"
60 60

  
61
let TID=ID+BASE_TID
61
TID=`$SSH $DST_HOST $SUDO $(tgtadm_get_tid_for_iqn $SRC)`
62 62

  
63 63
RM_COMMAND=$(cat <<EOF
64 64
    $SUDO $(tgtadm_target_delete "$TID")
65
    $SUDO $(tgt-admin_dump_config "/etc/tgt/targets.conf")
65 66
    $SUDO $LVREMOVE -f $VG_NAME/$LV_NAME
66 67
EOF
67 68
)
src/mad/sh/scripts_common.sh
40 40
SUDO=sudo
41 41
TAR=tar
42 42
TGTADM=tgtadm
43
TGTADMIN=tgt-admin
43 44
VMKFSTOOLS=/usr/sbin/vmkfstools
44 45
WGET=wget
45 46

  
......
294 295
    echo "$TGTADM --lld iscsi --op delete --mode target --tid $ID"
295 296
}
296 297

  
298
function tgtadm_get_tid_for_iqn {
299
    IQN="$1"
300
    echo "$TGTADM --lld iscsi --op show --mode target | grep \"$IQN\" | awk '{split(\$2,tmp,\":\"); print(tmp[1]);}'"
301
}
302

  
303
function tgt-admin_dump_config {
304
    FILE_PATH="$1"
305
    echo "$TGTADMIN --dump > $FILE_PATH"
306
}
307

  
297 308
###
298 309

  
299 310
function iscsiadm_discovery {
src/tm_mad/iscsi/clone
31 31
    TMCOMMON=$ONE_LOCATION/var/remotes/tm/tm_common.sh
32 32
fi
33 33

  
34
DRIVER_PATH=$(dirname $0)
35

  
34 36
. $TMCOMMON
37
IQN=$SRC
38
TARGET=`arg_path $SRC`
39
DST_PATH=`arg_path $DST`
40
DST_HOST=`arg_host $DST`
41
DST_DIR=`dirname $DST_PATH`
42
VM_ID=`echo $DST_DIR | awk '{split($0,a,"/"); print a[length(a)];}'`
43
NEW_IQN=$IQN-$VM_ID
44

  
45
BASE_IQN=`echo $IQN|$CUT -d: -f1`
46
TARGET=`echo $IQN|$CUT -d: -f2`
47
LV_NAME=`echo $TARGET|$AWK -F. '{print $(NF)}'`
48
TARGET_LV_NAME=`echo $LV_NAME-$VM_ID`
49
VG_NAME=`echo $TARGET|$AWK -F. '{print $(NF-1)}'`
50
TARGET_DEV="/dev/$VG_NAME/$LV_NAME"
51
TARGET_HOST=${TARGET%.$VG_NAME.$LV_NAME}
52

  
53
SIZE=`$SSH $TARGET_HOST $SUDO lvdisplay $TARGET_DEV | awk '/Current LE/ {print $3}'`
54

  
55
log "Create LV"
56
$SSH $TARGET_HOST $SUDO $LVCREATE -l${SIZE} ${VG_NAME} -n ${TARGET_LV_NAME}
57

  
58
log "Cloning /dev/${VG_NAME}/${LV_NAME} to /dev/${VG_NAME}/${TARGET_LV_NAME}"
59
$SSH $TARGET_HOST $SUDO $DD if=/dev/${VG_NAME}/${LV_NAME} of=/dev/${VG_NAME}/${TARGET_LV_NAME} bs=64k
60

  
61
log "Creating new iSCSI Target IQN: $NEW_IQN"
62
NEWTARGET_CMD=$(cat <<EOF
63
    TID=\`$SUDO $TGTADM --lld iscsi --op show --mode target | grep "Target" | tail -n 1 | awk '{split(\$2,tmp,":"); print tmp[1]+1;}'\`
64
    if [ -z \$TID ]; then TID=1; fi
65
    $SUDO $(tgtadm_target_new \$TID "$NEW_IQN")
66
EOF
67
)
68

  
69
ssh_exec_and_log "$TARGET_HOST" "$NEWTARGET_CMD" \
70
        "Error creating new target $TARGET_HOST:$DEV"
71
unset NEWTARGET_CMD
72

  
73
TID=`$SSH $TARGET_HOST $SUDO $(tgtadm_get_tid_for_iqn $NEW_IQN)`
74
TARGETSETUP_CMD=$(cat <<EOF
75
    $SUDO $(tgtadm_target_bind_all "$TID")
76
    $SUDO $(tgtadm_logicalunit_new "$TID" "/dev/$VG_NAME/$TARGET_LV_NAME")
77
    $SUDO $(tgt-admin_dump_config /etc/tgt/targets.conf)
78
EOF
79
)
80

  
81
ssh_exec_and_log "$TARGET_HOST" "$TARGETSETUP_CMD" \
82
        "Error setting up target $TARGET_HOST:$DEV"
83
unset TARGETSETUP_CMD
84

  
85
DISCOVER_CMD=$(cat <<EOF
86
    set -e
87
    mkdir -p $DST_DIR
88
    $SUDO $(iscsiadm_discovery "$TARGET_HOST")
89
    $SUDO $(iscsiadm_login "$NEW_IQN" "$TARGET_HOST")
90
    sleep 2
91
    DISK_BY_PATH=\$(ls /dev/disk/by-path/*$NEW_IQN-lun-1)
92
    ln -s "\$DISK_BY_PATH" "$DST_PATH"
93
EOF
94
)
35 95

  
36
log_error "CLONE not supported for TM_ISCSI. Use persistent images"
96
ssh_exec_and_log "$DST_HOST" "$DISCOVER_CMD" \
97
        "Error registering $DST_HOST:$DEV"
37 98

  
38
exit 1
99
exit 0
src/tm_mad/iscsi/delete
35 35
# remote_system_ds directory for the VM (remotely)
36 36
#-------------------------------------------------------------------------------
37 37
DST_PATH=`arg_path $DST`
38
DST_DIR=`dirname $DST_PATH`
38 39
DST_HOST=`arg_host $DST`
39 40

  
40 41
if [ `is_disk $DST_PATH` -eq 1 ]; then
41 42
    # Disk
43
    IQN=`$SSH $DST_HOST readlink $DST_PATH |grep -o 'iqn.*$'`
44
    IQN=${IQN%-lun-1}
45
    TARGET=`echo $IQN|$CUT -d: -f2` 
46
    LEN=`echo $IQN | awk '{split($0,a,".") split(a[length(a)],b,"-"); print length(b);}'`
47
    VM_ID=`echo $DST_DIR | awk '{split($0,a,"/"); print a[length(a)];}'`
42 48
    LOGOUT_CMD=$(cat <<EOF
43 49
    set -e
44 50
    IQN=\$(readlink $DST_PATH |grep -o 'iqn.*$')
......
46 52
    $SUDO $(iscsiadm_logout "\$IQN")
47 53
EOF
48 54
)
49
    ssh_exec_and_log "$DST_HOST" "$LOGOUT_CMD" \
50
        "Error logging out $IQN"
55
    if [ $LEN -eq 4 ]; then #Non persistent
56
	log "Removing non persistent disk from iSCSI DS"
57
	BASE_IQN=`echo $IQN|$CUT -d: -f1`
58
	LV_NAME=`echo $TARGET|$AWK -F. '{print $(NF)}'` 
59
	TARGET_LV_NAME=`echo $LV_NAME-$VM_ID`
60
	VG_NAME=`echo $TARGET|$AWK -F. '{print $(NF-1)}'`
61
	TARGET_DEV="/dev/$VG_NAME/$LV_NAME"
62
	TARGET_HOST=${TARGET%.$VG_NAME.$LV_NAME} 
63
	TID=`$SSH $TARGET_HOST $SUDO tgtadm --lld iscsi --op show --mode target | grep $IQN | awk '{split($2,a,":"); print a[1];}'`
64
        ssh_exec_and_log "$DST_HOST" "$LOGOUT_CMD" \
65
            "Error logging out $IQN"
66
        ssh_exec_and_log "$TARGET_HOST" "$SUDO $TGTADM --lld iscsi --op delete --mode target --tid $TID" \
67
    	    "Error while deleting iSCSI target $TID"
68
	ssh_exec_and_log "$TARGET_HOST" "$SUDO $TGTADMIN --dump > /etc/tgt/targets.conf" \
69
	    "Failed to refresh the config file"
70
        ssh_exec_and_log "$TARGET_HOST" "$SUDO $LVREMOVE -f $VG_NAME/$LV_NAME" \
71
    	    "Error while removing LV $VG_NAME/$LV_NAME"
72
    else #Persistent
73
	log "Logging out from iSCSI target"
74
        ssh_exec_and_log "$DST_HOST" "$LOGOUT_CMD" \
75
            "Error logging out $IQN"
76
    fi
51 77
else
52 78
    # Directory
53 79
    log "Deleting $DST_PATH"
src/tm_mad/iscsi/ln
55 55
    mkdir -p $DST_DIR
56 56
    $SUDO $(iscsiadm_discovery "$TARGET_HOST")
57 57
    $SUDO $(iscsiadm_login "$IQN" "$TARGET_HOST")
58
    sleep 1
58
    sleep 2
59 59
    DISK_BY_PATH=\$(ls /dev/disk/by-path/*$IQN-lun-1)
60 60
    ln -s "\$DISK_BY_PATH" "$DST_PATH"
61 61
EOF
src/tm_mad/iscsi/mvds
34 34
. $TMCOMMON
35 35

  
36 36
SRC_HOST=`arg_host $SRC`
37
IQN=$DST
37
SRC_PATH=`arg_path $SRC`
38
IQN=`$SSH $SRC_HOST readlink $SRC_PATH |grep -o 'iqn.*$'`
39
IQN=${IQN%-lun-1}
40
TARGET_IQN=`echo $2`
41
TARGET=`echo $IQN|$CUT -d: -f2`
42
TARGET_TARGET=`echo $TARGET_IQN|$CUT -d: -f2`
38 43

  
39
log "Logging out $IQN"
44
LEN=`echo $IQN | awk '{split($0,a,".") split(a[length(a)],b,"-"); print length(b);}'`
45
VM_ID=`echo $DST_DIR | awk '{split($0,a,"/"); print a[length(a)];}'` 
46

  
47
BASE_IQN=`echo $IQN|$CUT -d: -f1`               
48
LV_NAME=`echo $TARGET|$AWK -F. '{print $(NF)}'`
49
VG_NAME=`echo $TARGET|$AWK -F. '{print $(NF-1)}'`
50
TARGET_LV_NAME=`echo $TARGET_TARGET|$AWK -F. '{print $(NF)}'`
51
TARGET_VG_NAME=`echo $TARGET_TARGET|$AWK -F. '{print $(NF-1)}'`
52
TARGET_HOST=${TARGET%.$VG_NAME.$LV_NAME}
53

  
54
if [ $LEN -eq 3 ]; then
40 55

  
56
log "Logging out $IQN"
41 57
LOGOUT_CMD=$(cat <<EOF
42 58
    set -e
43 59
    $SUDO $(iscsiadm_logout "$IQN")
......
48 64
    "Error logging out $IQN"
49 65

  
50 66
exit 0
67
else
68
    log "Logging out $IQN"
69
LOGOUT_CMD=$(cat <<EOF
70
set -e
71
$SUDO $(iscsiadm_logout "$IQN")
72
EOF
73
)
74
    ssh_exec_and_log "$SRC_HOST" "$LOGOUT_CMD" \
75
    "Error logging out $IQN"
76
    TID=`$SSH $TARGET_HOST $SUDO tgtadm --lld iscsi --op show --mode target | grep $IQN | awk '{split($2,a,":"); print a[1];}'`
77
    ssh_exec_and_log "$TARGET_HOST" "$SUDO $TGTADM --lld iscsi --op delete --mode target --tid $TID" \
78
    "Error while deleting iSCSI target $TID"
79
    ssh_exec_and_log "$TARGET_HOST" "$SUDO $TGTADMIN --dump > /etc/tgt/targets.conf" \
80
    "Error while saving the config file"
81
    ssh_exec_and_log "$TARGET_HOST" "$SUDO $DD if=/dev/$VG_NAME/$LV_NAME of=/dev/$TARGET_VG_NAME/$TARGET_LV_NAME bs=64k" \
82
    "Error while copying disk images from /dev/$VG_NAME/$LV_NAME to /dev/$TARGET_VG_NAME/$TARGET_LV_NAME"
83
    ssh_exec_and_log "$TARGET_HOST" "$SUDO $LVREMOVE -f $VG_NAME/$LV_NAME" \
84
    "Error while removing LV /dev/$VG_NAME/$LV_NAME"
85
    exit 0
86
fi