← Documents Documentation/target/target-export-device GitHub 원문 ↗

Linux 6.18.37 · Target

간단한 LIO iSCSI target export script

block device 또는 file backstore로 단일 LUN LIO iSCSI target을 만드는 configfs shell script의 인자, object 생성 순서와 접근 설정을 설명합니다.

Source pathDocumentation/target/target-export-device
Source versionLinux v6.18.37
TranslationDUJINLABS 전문 번역 + 해설

요약·해설과 원문, 전문 번역을 서로 분리했습니다. API 이름, symbol, source path는 원문 표기를 사용합니다.

1. 요약·해설

원문의 핵심 논리와 kernel programming 관점의 보충 설명입니다. 아래의 전문 번역과는 별도로 작성했습니다.

요약·해설

target-export-device:1-80

block device 또는 file backstore로 단일 LUN LIO iSCSI target을 만드는 configfs shell script의 인자, object 생성 순서와 접근 설정을 설명합니다.

2. 영어 원문 전체

번역 기준이 된 Linux v6.18.37 원문입니다. 줄 번호는 이 버전의 파일 좌표입니다.

원문 전체 펼치기
1 #!/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 <cvubrugier@fastmail.fm>
8 #
9
10 print_usage() {
11 cat <<EOF
12 Usage: $(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 die() {
18 echo $1
19 exit 1
20 }
21
22 while getopts "hp:" arg; do
23 case $arg in
24 h) print_usage; exit 0;;
25 p) PORTAL=${OPTARG};;
26 esac
27 done
28 shift $(($OPTIND - 1))
29
30 DEVICE=$1
31 [ -n "$DEVICE" ] || die "Missing device or file argument"
32 [ -b $DEVICE -o -f $DEVICE ] || die "Invalid device or file: ${DEVICE}"
33 IQN="iqn.2003-01.org.linux-iscsi.$(hostname):$(basename $DEVICE)"
34 [ -n "$PORTAL" ] || PORTAL="0.0.0.0:3260"
35
36 CONFIGFS=/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 | grep -q configfs || modprobe configfs
42 lsmod | grep -q target_core_mod || modprobe target_core_mod
43 mount | grep -q ^configfs || mount -t configfs none $CONFIGFS
44 mkdir -p $ISCSI_DIR
45
46 # Create a backstore
47 if [ -b $DEVICE ]; then
48 BACKSTORE_DIR=$CORE_DIR/iblock_0/data
49 mkdir -p $BACKSTORE_DIR
50 echo "udev_path=${DEVICE}" > $BACKSTORE_DIR/control
51 else
52 BACKSTORE_DIR=$CORE_DIR/fileio_0/data
53 mkdir -p $BACKSTORE_DIR
54 DEVICE_SIZE=$(du -b $DEVICE | cut -f1)
55 echo "fd_dev_name=${DEVICE}" > $BACKSTORE_DIR/control
56 echo "fd_dev_size=${DEVICE_SIZE}" > $BACKSTORE_DIR/control
57 echo 1 > $BACKSTORE_DIR/attrib/emulate_write_cache
58 fi
59 echo 1 > $BACKSTORE_DIR/enable
60
61 # Create an iSCSI target and a target portal group (TPG)
62 mkdir $ISCSI_DIR/$IQN
63 mkdir $ISCSI_DIR/$IQN/tpgt_1/
64
65 # Create a LUN
66 mkdir $ISCSI_DIR/$IQN/tpgt_1/lun/lun_0
67 ln -s $BACKSTORE_DIR $ISCSI_DIR/$IQN/tpgt_1/lun/lun_0/data
68 echo 1 > $ISCSI_DIR/$IQN/tpgt_1/enable
69
70 # Create a network portal
71 mkdir $ISCSI_DIR/$IQN/tpgt_1/np/$PORTAL
72
73 # Disable authentication
74 echo 0 > $ISCSI_DIR/$IQN/tpgt_1/attrib/authentication
75 echo 1 > $ISCSI_DIR/$IQN/tpgt_1/attrib/generate_node_acls
76
77 # Allow write access for non authenticated initiators
78 echo 0 > $ISCSI_DIR/$IQN/tpgt_1/attrib/demo_mode_write_protect
79
80 echo "Target ${IQN}, portal ${PORTAL} has been created"
81

3. 한국어 전문 번역

영어 원문의 문단 순서와 의미를 유지한 전체 번역입니다. 코드, 함수명, symbol과 URL은 원문 표기를 유지합니다.

목적과 사용법

1-15

이 `/bin/sh` script는 configfs 작업 순서를 통해 file 또는 block device backstore를 가진 매우 단순한 LIO iSCSI target을 만드는 예다. 2014년 Christophe Vu-Brugier가 저작권을 표시했다.

사용법은 `target-export-device [-p PORTAL] DEVICE|FILE`이다. block device 또는 file 하나를 단일 LUN의 iSCSI target으로 export하며 `-p`로 network portal을 지정할 수 있다.

명령 인자
인자의미
-h사용법 출력 후 종료
-p PORTALiSCSI portal; 기본 0.0.0.0:3260
DEVICE|FILEexport할 block device 또는 regular file

script가 받는 입력이다.

#!/bin/sh
#
# This script illustrates the sequence of operations in configfs to
# create a very simple LIO iSCSI target with a file or block device
# backstore.
#
# (C) Copyright 2014 Christophe Vu-Brugier <cvubrugier@fastmail.fm>
#

print_usage() {
    cat <<EOF
Usage: $(basename $0) [-p PORTAL] DEVICE|FILE
Export a block device or a file as an iSCSI target with a single LUN
EOF
}

인자 검증과 configfs 경로

16-38

`die()`는 error message를 출력하고 status 1로 종료한다. `getopts "hp:"`는 help와 portal option을 처리하고, `shift $(($OPTIND - 1))`로 option을 제거한 뒤 첫 positional argument를 `DEVICE`로 받는다.

인자가 비었으면 `Missing device or file argument`, block device나 regular file이 아니면 `Invalid device or file`로 종료한다.

IQN은 `iqn.2003-01.org.linux-iscsi.$(hostname):$(basename $DEVICE)` 형식으로 만든다. portal을 지정하지 않으면 `0.0.0.0:3260`을 사용한다. configfs root는 `/sys/kernel/config`, core backstore 경로는 `$CONFIGFS/target/core`, iSCSI 경로는 `$CONFIGFS/target/iscsi`다.

인자 처리
Parse -h and -pRead DEVICE or FILE
Validate block or regular fileBuild IQN
Default or explicit PORTALSet core and iscsi paths

입력을 검증하고 target identity와 configfs root를 정한다.

생성 변수
변수
IQNhostname과 basename을 조합한 iSCSI qualified name
CONFIGFS/sys/kernel/config
CORE_DIR/sys/kernel/config/target/core
ISCSI_DIR/sys/kernel/config/target/iscsi

script가 파생하는 주요 값이다.


die() {
    echo $1
    exit 1
}

while getopts "hp:" arg; do
    case $arg in
        h) print_usage; exit 0;;
        p) PORTAL=${OPTARG};;
    esac
done
shift $(($OPTIND - 1))

DEVICE=$1
[ -n "$DEVICE" ] || die "Missing device or file argument"
[ -b $DEVICE -o -f $DEVICE ] || die "Invalid device or file: ${DEVICE}"
IQN="iqn.2003-01.org.linux-iscsi.$(hostname):$(basename $DEVICE)"
[ -n "$PORTAL" ] || PORTAL="0.0.0.0:3260"

CONFIGFS=/sys/kernel/config
CORE_DIR=$CONFIGFS/target/core
ISCSI_DIR=$CONFIGFS/target/iscsi

Module 준비와 backstore 생성

39-59

먼저 `configfs`와 `target_core_mod`가 load되어 있는지 `lsmod`로 확인하고 없으면 `modprobe`한다. configfs가 mount되지 않았으면 `/sys/kernel/config`에 mount한 뒤 iSCSI directory를 만든다.

입력이 block device이면 backstore를 `$CORE_DIR/iblock_0/data`에 만들고 `control`에 `udev_path=$DEVICE`를 기록한다.

입력이 file이면 `$CORE_DIR/fileio_0/data`를 만들고 `du -b`로 byte size를 구한다. `control`에 `fd_dev_name`과 `fd_dev_size`를 기록하고 `attrib/emulate_write_cache`를 1로 설정한다.

두 경우 모두 마지막에 backstore의 `enable` file에 1을 기록해 활성화한다.

Backstore 선택
Block deviceiblock_0/dataudev_path
Regular filefileio_0/datafd_dev_name + fd_dev_size
Selected backstoreenable = 1

입력 file type에 따라 iblock 또는 fileio control을 구성한다.


# Load the target modules and mount the config file system
lsmod | grep -q configfs || modprobe configfs
lsmod | grep -q target_core_mod || modprobe target_core_mod
mount | grep -q ^configfs || mount -t configfs none $CONFIGFS
mkdir -p $ISCSI_DIR

# Create a backstore
if [ -b $DEVICE ]; then
    BACKSTORE_DIR=$CORE_DIR/iblock_0/data
    mkdir -p $BACKSTORE_DIR
    echo "udev_path=${DEVICE}" > $BACKSTORE_DIR/control
else
    BACKSTORE_DIR=$CORE_DIR/fileio_0/data
    mkdir -p $BACKSTORE_DIR
    DEVICE_SIZE=$(du -b $DEVICE | cut -f1)
    echo "fd_dev_name=${DEVICE}" > $BACKSTORE_DIR/control
    echo "fd_dev_size=${DEVICE_SIZE}" > $BACKSTORE_DIR/control
    echo 1 > $BACKSTORE_DIR/attrib/emulate_write_cache
fi
echo 1 > $BACKSTORE_DIR/enable

iSCSI target, TPG와 LUN

60-69

script는 `$ISCSI_DIR/$IQN`에 iSCSI target을 만들고 그 아래 `tpgt_1` target portal group을 생성한다.

`tpgt_1/lun/lun_0` directory를 만든 뒤 선택한 backstore directory를 `lun_0/data`에 symbolic link한다. 이어 `tpgt_1/enable`에 1을 써 TPG를 활성화한다.

Target object 관계
IQN targettpgt_1lun/lun_0
lun_0/data symlinkiblock or fileio backstore
tpgt_1 enableActive target portal group

configfs object와 backstore 연결 구조다.


# Create an iSCSI target and a target portal group (TPG)
mkdir $ISCSI_DIR/$IQN
mkdir $ISCSI_DIR/$IQN/tpgt_1/

# Create a LUN
mkdir $ISCSI_DIR/$IQN/tpgt_1/lun/lun_0
ln -s $BACKSTORE_DIR $ISCSI_DIR/$IQN/tpgt_1/lun/lun_0/data
echo 1 > $ISCSI_DIR/$IQN/tpgt_1/enable

Portal과 인증 설정

70-80

network portal은 `$ISCSI_DIR/$IQN/tpgt_1/np/$PORTAL` directory를 만들어 등록한다.

예제는 `authentication`을 0으로 꺼 두고 `generate_node_acls`를 1로 설정해 ACL을 자동 생성한다. 인증하지 않은 initiator에도 write access를 허용하도록 `demo_mode_write_protect`를 0으로 설정한다.

마지막으로 생성한 target IQN과 portal을 출력한다. 이 설정은 인증을 끄고 write를 허용하는 최소 예제이므로 실제 배포에서는 접근 제어와 network 노출 범위를 별도로 설계해야 한다.

TPG 속성
속성효과
authentication0인증 비활성
generate_node_acls1node ACL 자동 생성
demo_mode_write_protect0비인증 initiator write 허용

예제에서 설정하는 접근 제어 값이다.

# Create a network portal
mkdir $ISCSI_DIR/$IQN/tpgt_1/np/$PORTAL

# Disable authentication
echo 0 > $ISCSI_DIR/$IQN/tpgt_1/attrib/authentication
echo 1 > $ISCSI_DIR/$IQN/tpgt_1/attrib/generate_node_acls

# Allow write access for non authenticated initiators
echo 0 > $ISCSI_DIR/$IQN/tpgt_1/attrib/demo_mode_write_protect

echo "Target ${IQN}, portal ${PORTAL} has been created"