iscsi_persistent.patch

SZTAKI LPDS, 04/25/2012 09:28 AM

Download (10.6 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
TID=`$SSH $DST_HOST $SUDO $(tgtadm_get_next_free_tid)`
78 78

  
79 79
REGISTER_CMD=$(cat <<EOF
80 80
    set -e
......
82 82
    $SUDO $(tgtadm_target_new "$TID" "$IQN")
83 83
    $SUDO $(tgtadm_target_bind_all "$TID")
84 84
    $SUDO $(tgtadm_logicalunit_new "$TID" "$DEV")
85
    $SUDO $(tgt-admin_dump_config "/etc/tgt/targets.conf")
85 86
EOF
86 87
)
87 88

  
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
TID=`$SSH $DST_HOST $SUDO $(tgtadm_get_next_free_tid)`
79 79

  
80
REGISTER_CMD=$(cat <<EOF
80
# ------------ Image to save_as disk, no need to create a FS ------------
81

  
82
if [ "$FSTYPE" = "save_as" ]; then
83
    REGISTER_CMD=$(cat <<EOF
81 84
    set -e
82 85
    $SUDO $LVCREATE -L${SIZE}M ${VG_NAME} -n ${LV_NAME}
83 86
    $SUDO $(tgtadm_target_new "$TID" "$IQN")
84 87
    $SUDO $(tgtadm_target_bind_all "$TID")
85 88
    $SUDO $(tgtadm_logicalunit_new "$TID" "$DEV")
86
    $SUDO $(mkfs_command "$DEV" "$FSTYPE")
89
    $SUDO $(tgt-admin_dump_config "/etc/tgt/targets.conf")
87 90
EOF
88 91
)
92
    ssh_exec_and_log "$DST_HOST" "$REGISTER_CMD" \
93
    "Error registering $DST_HOST:$DEV"
89 94

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

  
92
if [ "$FSTYPE" = "save_as" ]; then
93
    echo "$DST $SIZE"
95
    echo "$IQN $SIZE"
94 96
    exit 0
95 97
fi
96 98

  
97 99
# ------------ Create the image to the repository ------------
98 100

  
101
REGISTER_CMD=$(cat <<EOF
102
    set -e
103
    $SUDO $LVCREATE -L${SIZE}M ${VG_NAME} -n ${LV_NAME}
104
    $SUDO $(tgtadm_target_new "$TID" "$IQN")
105
    $SUDO $(tgtadm_target_bind_all "$TID")
106
    $SUDO $(tgtadm_logicalunit_new "$TID" "$DEV")
107
    $SUDO $(tgt-admin_dump_config "/etc/tgt/targets.conf")
108
    $SUDO $(mkfs_command "$DEV" "$FSTYPE")
109
EOF
110
)
111

  
99 112
ssh_exec_and_log "$DST_HOST" "$REGISTER_CMD" \
100 113
    "Error registering $DST_HOST:$DEV"
101 114

  
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_next_free_tid {
299
    echo "$TGTADM --lld iscsi --op show --mode target | grep \"Target\" | tail -n 1 | awk '{split(\$2,tmp,\":\"); print tmp[1]+1;}'"
300
}
301

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

  
307
function tgt-admin_dump_config {
308
    FILE_PATH="$1"
309
    echo "$TGTADMIN --dump > $FILE_PATH"
310
}
311

  
297 312
###
298 313

  
299 314
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
TID=`$SSH $TARGET_HOST $SUDO $(tgtadm_get_next_free_tid)`
54

  
55
SIZE=`$SSH $TARGET_HOST $SUDO lvdisplay $TARGET_DEV | /bin/awk '/Current LE/ {print $3}'`
56

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

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

  
36
log_error "CLONE not supported for TM_ISCSI. Use persistent images"
63
log "Creating new iSCSI Target, TID: $TID IQN: $NEW_IQN"
64
$SSH $TARGET_HOST $SUDO $(tgtadm_target_new "$TID" "$NEW_IQN")
65
$SSH $TARGET_HOST $SUDO $(tgtadm_target_bind_all "$TID")
66
$SSH $TARGET_HOST $SUDO $(tgtadm_logicalunit_new "$TID" "/dev/$VG_NAME/$TARGET_LV_NAME")
67
$SSH $TARGET_HOST $SUDO $(tgt-admin_dump_config "/etc/tgt/targets.conf")
68
DISCOVER_CMD=$(cat <<EOF
69
    set -e
70
    mkdir -p $DST_DIR
71
    $SUDO $(iscsiadm_discovery "$TARGET_HOST")
72
    $SUDO $(iscsiadm_login "$NEW_IQN" "$TARGET_HOST")
73
    sleep 1
74
    DISK_BY_PATH=\$(ls /dev/disk/by-path/*$NEW_IQN-lun-1)
75
    ln -s "\$DISK_BY_PATH" "$DST_PATH"
76
EOF
77
)
78
ssh_exec_and_log "$DST_HOST" "$DISCOVER_CMD" \
79
        "Error registering $DST_HOST:$DEV"
37 80

  
38
exit 1
81
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/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