81 lines · plain
1#!/bin/sh2#3# This script illustrates the sequence of operations in configfs to4# create a very simple LIO iSCSI target with a file or block device5# backstore.6#7# (C) Copyright 2014 Christophe Vu-Brugier <cvubrugier@fastmail.fm>8#9 10print_usage() {11 cat <<EOF12Usage: $(basename $0) [-p PORTAL] DEVICE|FILE13Export a block device or a file as an iSCSI target with a single LUN14EOF15}16 17die() {18 echo $119 exit 120}21 22while getopts "hp:" arg; do23 case $arg in24 h) print_usage; exit 0;;25 p) PORTAL=${OPTARG};;26 esac27done28shift $(($OPTIND - 1))29 30DEVICE=$131[ -n "$DEVICE" ] || die "Missing device or file argument"32[ -b $DEVICE -o -f $DEVICE ] || die "Invalid device or file: ${DEVICE}"33IQN="iqn.2003-01.org.linux-iscsi.$(hostname):$(basename $DEVICE)"34[ -n "$PORTAL" ] || PORTAL="0.0.0.0:3260"35 36CONFIGFS=/sys/kernel/config37CORE_DIR=$CONFIGFS/target/core38ISCSI_DIR=$CONFIGFS/target/iscsi39 40# Load the target modules and mount the config file system41lsmod | grep -q configfs || modprobe configfs42lsmod | grep -q target_core_mod || modprobe target_core_mod43mount | grep -q ^configfs || mount -t configfs none $CONFIGFS44mkdir -p $ISCSI_DIR45 46# Create a backstore47if [ -b $DEVICE ]; then48 BACKSTORE_DIR=$CORE_DIR/iblock_0/data49 mkdir -p $BACKSTORE_DIR50 echo "udev_path=${DEVICE}" > $BACKSTORE_DIR/control51else52 BACKSTORE_DIR=$CORE_DIR/fileio_0/data53 mkdir -p $BACKSTORE_DIR54 DEVICE_SIZE=$(du -b $DEVICE | cut -f1)55 echo "fd_dev_name=${DEVICE}" > $BACKSTORE_DIR/control56 echo "fd_dev_size=${DEVICE_SIZE}" > $BACKSTORE_DIR/control57 echo 1 > $BACKSTORE_DIR/attrib/emulate_write_cache58fi59echo 1 > $BACKSTORE_DIR/enable60 61# Create an iSCSI target and a target portal group (TPG)62mkdir $ISCSI_DIR/$IQN63mkdir $ISCSI_DIR/$IQN/tpgt_1/64 65# Create a LUN66mkdir $ISCSI_DIR/$IQN/tpgt_1/lun/lun_067ln -s $BACKSTORE_DIR $ISCSI_DIR/$IQN/tpgt_1/lun/lun_0/data68echo 1 > $ISCSI_DIR/$IQN/tpgt_1/enable69 70# Create a network portal71mkdir $ISCSI_DIR/$IQN/tpgt_1/np/$PORTAL72 73# Disable authentication74echo 0 > $ISCSI_DIR/$IQN/tpgt_1/attrib/authentication75echo 1 > $ISCSI_DIR/$IQN/tpgt_1/attrib/generate_node_acls76 77# Allow write access for non authenticated initiators78echo 0 > $ISCSI_DIR/$IQN/tpgt_1/attrib/demo_mode_write_protect79 80echo "Target ${IQN}, portal ${PORTAL} has been created"81