| 1 | #!/bin/shscip-shell filesystem . . `/bin/sh`. |
| 2 | # |
| 3 | # This script illustrates the sequence of operations in configfs to |
| 4 | # create a very simple LIO iSCSI target with a file or block device |
| 5 | # backstore. |
| 6 | # |
| 7 | # (C) Copyright 2014 Christophe Vu-Brugier <[email protected]> |
| 8 | # |
| 9 | |
| 10 | print_usagescip-shell shell linux 7.0.12-2 print_usage().() { |
| 11 | catscip-shell system . . cat. <<EOF |
| 12 | Usage: $(basenamescip-shell system . . basename. $0) [-p PORTAL] DEVICE|FILE |
| 13 | Export a block device or a file as an iSCSI target with a single LUN |
| 14 | EOF |
| 15 | } |
| 16 | |
| 17 | diescip-shell shell linux 7.0.12-2 die().() { |
| 18 | echoscip-shell system . . echo. $1 |
| 19 | exit 1 |
| 20 | } |
| 21 | |
| 22 | while getopts "hp:" arg; do |
| 23 | case $arg in |
| 24 | h) print_usagescip-shell shell linux 7.0.12-2 print_usage().; exit 0;; |
| 25 | p) PORTAL=${OPTARG};; |
| 26 | esac |
| 27 | done |
| 28 | shift $(($OPTIND - 1)) |
| 29 | |
| 30 | DEVICE=$1 |
| 31 | [scip-shell system . . `[`. -n "$DEVICE" ] || diescip-shell shell linux 7.0.12-2 die(). "Missing device or file argument" |
| 32 | [scip-shell system . . `[`. -b $DEVICE -o -f $DEVICE ] || diescip-shell shell linux 7.0.12-2 die(). "Invalid device or file: ${DEVICE}" |
| 33 | IQN="iqn.2003-01.org.linux-iscsi.$(hostnamescip-shell system . . hostname.):$(basenamescip-shell system . . basename. $DEVICE)" |
| 34 | [scip-shell system . . `[`. -n "$PORTAL" ] || PORTAL="0.0.0.0:3260" |
| 35 | |
| 36 | CONFIGFS=/sys/kernel/configscip-shell filesystem . . `/sys/kernel/config`. |
| 37 | CORE_DIR=$CONFIGFS/target/core |
| 38 | ISCSI_DIR=$CONFIGFS/target/iscsi |
| 39 | |
| 40 | # Load the target modules and mount the config file system |
| 41 | lsmod | grepscip-shell system . . grep. -q configfs || modprobe configfs |
| 42 | lsmod | grepscip-shell system . . grep. -q target_core_mod || modprobe target_core_mod |
| 43 | mountscip-shell system . . mount. | grepscip-shell system . . grep. -q ^configfs || mountscip-shell system . . mount. -t configfs none $CONFIGFS |
| 44 | mkdirscip-shell system . . mkdir. -p $ISCSI_DIR |
| 45 | |
| 46 | # Create a backstore |
| 47 | if [scip-shell system . . `[`. -b $DEVICE ]; then |
| 48 | BACKSTORE_DIR=$CORE_DIR/iblock_0/data |
| 49 | mkdirscip-shell system . . mkdir. -p $BACKSTORE_DIR |
| 50 | echoscip-shell system . . echo. "udev_path=${DEVICE}" > $BACKSTORE_DIR/control |
| 51 | else |
| 52 | BACKSTORE_DIR=$CORE_DIR/fileio_0/data |
| 53 | mkdirscip-shell system . . mkdir. -p $BACKSTORE_DIR |
| 54 | DEVICE_SIZE=$(duscip-shell system . . du. -b $DEVICE | cutscip-shell system . . cut. -f1) |
| 55 | echoscip-shell system . . echo. "fd_dev_name=${DEVICE}" > $BACKSTORE_DIR/control |
| 56 | echoscip-shell system . . echo. "fd_dev_size=${DEVICE_SIZE}" > $BACKSTORE_DIR/control |
| 57 | echoscip-shell system . . echo. 1 > $BACKSTORE_DIR/attrib/emulate_write_cache |
| 58 | fi |
| 59 | echoscip-shell system . . echo. 1 > $BACKSTORE_DIR/enable |
| 60 | |
| 61 | # Create an iSCSI target and a target portal group (TPG) |
| 62 | mkdirscip-shell system . . mkdir. $ISCSI_DIR/$IQN |
| 63 | mkdirscip-shell system . . mkdir. $ISCSI_DIR/$IQN/tpgt_1/ |
| 64 | |
| 65 | # Create a LUN |
| 66 | mkdirscip-shell system . . mkdir. $ISCSI_DIR/$IQN/tpgt_1/lun/lun_0 |
| 67 | lnscip-shell system . . ln. -s $BACKSTORE_DIR $ISCSI_DIR/$IQN/tpgt_1/lun/lun_0/data |
| 68 | echoscip-shell system . . echo. 1 > $ISCSI_DIR/$IQN/tpgt_1/enable |
| 69 | |
| 70 | # Create a network portal |
| 71 | mkdirscip-shell system . . mkdir. $ISCSI_DIR/$IQN/tpgt_1/np/$PORTAL |
| 72 | |
| 73 | # Disable authentication |
| 74 | echoscip-shell system . . echo. 0 > $ISCSI_DIR/$IQN/tpgt_1/attrib/authentication |
| 75 | echoscip-shell system . . echo. 1 > $ISCSI_DIR/$IQN/tpgt_1/attrib/generate_node_acls |
| 76 | |
| 77 | # Allow write access for non authenticated initiators |
| 78 | echoscip-shell system . . echo. 0 > $ISCSI_DIR/$IQN/tpgt_1/attrib/demo_mode_write_protect |
| 79 | |
| 80 | echoscip-shell system . . echo. "Target ${IQN}, portal ${PORTAL} has been created" |