18c2ecf20Sopenharmony_ci#!/bin/sh 28c2ecf20Sopenharmony_ci# 38c2ecf20Sopenharmony_ci# This script illustrates the sequence of operations in configfs to 48c2ecf20Sopenharmony_ci# create a very simple LIO iSCSI target with a file or block device 58c2ecf20Sopenharmony_ci# backstore. 68c2ecf20Sopenharmony_ci# 78c2ecf20Sopenharmony_ci# (C) Copyright 2014 Christophe Vu-Brugier <cvubrugier@fastmail.fm> 88c2ecf20Sopenharmony_ci# 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ciprint_usage() { 118c2ecf20Sopenharmony_ci cat <<EOF 128c2ecf20Sopenharmony_ciUsage: $(basename $0) [-p PORTAL] DEVICE|FILE 138c2ecf20Sopenharmony_ciExport a block device or a file as an iSCSI target with a single LUN 148c2ecf20Sopenharmony_ciEOF 158c2ecf20Sopenharmony_ci} 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_cidie() { 188c2ecf20Sopenharmony_ci echo $1 198c2ecf20Sopenharmony_ci exit 1 208c2ecf20Sopenharmony_ci} 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ciwhile getopts "hp:" arg; do 238c2ecf20Sopenharmony_ci case $arg in 248c2ecf20Sopenharmony_ci h) print_usage; exit 0;; 258c2ecf20Sopenharmony_ci p) PORTAL=${OPTARG};; 268c2ecf20Sopenharmony_ci esac 278c2ecf20Sopenharmony_cidone 288c2ecf20Sopenharmony_cishift $(($OPTIND - 1)) 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ciDEVICE=$1 318c2ecf20Sopenharmony_ci[ -n "$DEVICE" ] || die "Missing device or file argument" 328c2ecf20Sopenharmony_ci[ -b $DEVICE -o -f $DEVICE ] || die "Invalid device or file: ${DEVICE}" 338c2ecf20Sopenharmony_ciIQN="iqn.2003-01.org.linux-iscsi.$(hostname):$(basename $DEVICE)" 348c2ecf20Sopenharmony_ci[ -n "$PORTAL" ] || PORTAL="0.0.0.0:3260" 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ciCONFIGFS=/sys/kernel/config 378c2ecf20Sopenharmony_ciCORE_DIR=$CONFIGFS/target/core 388c2ecf20Sopenharmony_ciISCSI_DIR=$CONFIGFS/target/iscsi 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_ci# Load the target modules and mount the config file system 418c2ecf20Sopenharmony_cilsmod | grep -q configfs || modprobe configfs 428c2ecf20Sopenharmony_cilsmod | grep -q target_core_mod || modprobe target_core_mod 438c2ecf20Sopenharmony_cimount | grep -q ^configfs || mount -t configfs none $CONFIGFS 448c2ecf20Sopenharmony_cimkdir -p $ISCSI_DIR 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci# Create a backstore 478c2ecf20Sopenharmony_ciif [ -b $DEVICE ]; then 488c2ecf20Sopenharmony_ci BACKSTORE_DIR=$CORE_DIR/iblock_0/data 498c2ecf20Sopenharmony_ci mkdir -p $BACKSTORE_DIR 508c2ecf20Sopenharmony_ci echo "udev_path=${DEVICE}" > $BACKSTORE_DIR/control 518c2ecf20Sopenharmony_cielse 528c2ecf20Sopenharmony_ci BACKSTORE_DIR=$CORE_DIR/fileio_0/data 538c2ecf20Sopenharmony_ci mkdir -p $BACKSTORE_DIR 548c2ecf20Sopenharmony_ci DEVICE_SIZE=$(du -b $DEVICE | cut -f1) 558c2ecf20Sopenharmony_ci echo "fd_dev_name=${DEVICE}" > $BACKSTORE_DIR/control 568c2ecf20Sopenharmony_ci echo "fd_dev_size=${DEVICE_SIZE}" > $BACKSTORE_DIR/control 578c2ecf20Sopenharmony_ci echo 1 > $BACKSTORE_DIR/attrib/emulate_write_cache 588c2ecf20Sopenharmony_cifi 598c2ecf20Sopenharmony_ciecho 1 > $BACKSTORE_DIR/enable 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_ci# Create an iSCSI target and a target portal group (TPG) 628c2ecf20Sopenharmony_cimkdir $ISCSI_DIR/$IQN 638c2ecf20Sopenharmony_cimkdir $ISCSI_DIR/$IQN/tpgt_1/ 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci# Create a LUN 668c2ecf20Sopenharmony_cimkdir $ISCSI_DIR/$IQN/tpgt_1/lun/lun_0 678c2ecf20Sopenharmony_ciln -s $BACKSTORE_DIR $ISCSI_DIR/$IQN/tpgt_1/lun/lun_0/data 688c2ecf20Sopenharmony_ciecho 1 > $ISCSI_DIR/$IQN/tpgt_1/enable 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci# Create a network portal 718c2ecf20Sopenharmony_cimkdir $ISCSI_DIR/$IQN/tpgt_1/np/$PORTAL 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci# Disable authentication 748c2ecf20Sopenharmony_ciecho 0 > $ISCSI_DIR/$IQN/tpgt_1/attrib/authentication 758c2ecf20Sopenharmony_ciecho 1 > $ISCSI_DIR/$IQN/tpgt_1/attrib/generate_node_acls 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci# Allow write access for non authenticated initiators 788c2ecf20Sopenharmony_ciecho 0 > $ISCSI_DIR/$IQN/tpgt_1/attrib/demo_mode_write_protect 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_ciecho "Target ${IQN}, portal ${PORTAL} has been created" 81