Back to home page

OSCL-LXR

 
 

    


0001 #!/bin/sh
0002 #
0003 # This script illustrates the sequence of operations in configfs to
0004 # create a very simple LIO iSCSI target with a file or block device
0005 # backstore.
0006 #
0007 # (C) Copyright 2014 Christophe Vu-Brugier <cvubrugier@fastmail.fm>
0008 #
0009 
0010 print_usage() {
0011     cat <<EOF
0012 Usage: $(basename $0) [-p PORTAL] DEVICE|FILE
0013 Export a block device or a file as an iSCSI target with a single LUN
0014 EOF
0015 }
0016 
0017 die() {
0018     echo $1
0019     exit 1
0020 }
0021 
0022 while getopts "hp:" arg; do
0023     case $arg in
0024         h) print_usage; exit 0;;
0025         p) PORTAL=${OPTARG};;
0026     esac
0027 done
0028 shift $(($OPTIND - 1))
0029 
0030 DEVICE=$1
0031 [ -n "$DEVICE" ] || die "Missing device or file argument"
0032 [ -b $DEVICE -o -f $DEVICE ] || die "Invalid device or file: ${DEVICE}"
0033 IQN="iqn.2003-01.org.linux-iscsi.$(hostname):$(basename $DEVICE)"
0034 [ -n "$PORTAL" ] || PORTAL="0.0.0.0:3260"
0035 
0036 CONFIGFS=/sys/kernel/config
0037 CORE_DIR=$CONFIGFS/target/core
0038 ISCSI_DIR=$CONFIGFS/target/iscsi
0039 
0040 # Load the target modules and mount the config file system
0041 lsmod | grep -q configfs || modprobe configfs
0042 lsmod | grep -q target_core_mod || modprobe target_core_mod
0043 mount | grep -q ^configfs || mount -t configfs none $CONFIGFS
0044 mkdir -p $ISCSI_DIR
0045 
0046 # Create a backstore
0047 if [ -b $DEVICE ]; then
0048     BACKSTORE_DIR=$CORE_DIR/iblock_0/data
0049     mkdir -p $BACKSTORE_DIR
0050     echo "udev_path=${DEVICE}" > $BACKSTORE_DIR/control
0051 else
0052     BACKSTORE_DIR=$CORE_DIR/fileio_0/data
0053     mkdir -p $BACKSTORE_DIR
0054     DEVICE_SIZE=$(du -b $DEVICE | cut -f1)
0055     echo "fd_dev_name=${DEVICE}" > $BACKSTORE_DIR/control
0056     echo "fd_dev_size=${DEVICE_SIZE}" > $BACKSTORE_DIR/control
0057     echo 1 > $BACKSTORE_DIR/attrib/emulate_write_cache
0058 fi
0059 echo 1 > $BACKSTORE_DIR/enable
0060 
0061 # Create an iSCSI target and a target portal group (TPG)
0062 mkdir $ISCSI_DIR/$IQN
0063 mkdir $ISCSI_DIR/$IQN/tpgt_1/
0064 
0065 # Create a LUN
0066 mkdir $ISCSI_DIR/$IQN/tpgt_1/lun/lun_0
0067 ln -s $BACKSTORE_DIR $ISCSI_DIR/$IQN/tpgt_1/lun/lun_0/data
0068 echo 1 > $ISCSI_DIR/$IQN/tpgt_1/enable
0069 
0070 # Create a network portal
0071 mkdir $ISCSI_DIR/$IQN/tpgt_1/np/$PORTAL
0072 
0073 # Disable authentication
0074 echo 0 > $ISCSI_DIR/$IQN/tpgt_1/attrib/authentication
0075 echo 1 > $ISCSI_DIR/$IQN/tpgt_1/attrib/generate_node_acls
0076 
0077 # Allow write access for non authenticated initiators
0078 echo 0 > $ISCSI_DIR/$IQN/tpgt_1/attrib/demo_mode_write_protect
0079 
0080 echo "Target ${IQN}, portal ${PORTAL} has been created"