next up previous contents
Next: Client Initiator 設定 Up: iSCSI Storage Previous: iSCSI 簡介   Contents

Server Target 架設

  1. 安裝套件
    [root@kvm5 ~]# yum -y install targetcli
    
  2. 設定開機啟動 target 服務。
    [root@kvm5 ~]# systemctl enable target.service 
    ln -s '/usr/lib/systemd/system/target.service' '/etc/systemd/system/multi-user.target.wants/target.service'
    
  3. 啟動 target 服務。
    [root@kvm5 ~]# systemctl start target.service
    
  4. 防火牆開啟 3260/tcp port。
    [root@kvm5 ~]# firewall-cmd --permanent --add-port=3260/tcp
    success
    
  5. 重新載入防火牆設定。
    [root@kvm5 ~]# firewall-cmd --reload
    success
    
  6. 列出永久的防火牆設定,已開啟 3260/tcp port。
    [root@kvm5 ~]# firewall-cmd --permanent --list-all
    public (default)
      interfaces: 
      sources: 
      services: dhcpv6-client http https kerberos mountd nfs rpc-bind ssh
      ports: 3260/tcp 464/tcp 749/tcp
      masquerade: no
      forward-ports: 
      icmp-blocks: 
      rich rules:
    
  7. 查看目前系統的硬碟為 /dev/vda。
    [root@kvm5 ~]# lsblk 
    NAME                MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    vda                 252:0    0    4G  0 disk 
    ├─vda1              252:1    0  200M  0 part /boot
    ├─vda2              252:2    0  3.4G  0 part 
    │ ├─vg_kvm5usb-swap 253:0    0  124M  0 lvm  [SWAP]
    │ └─vg_kvm5usb-root 253:1    0  3.1G  0 lvm  /
    └─vda3              252:3    0  130M  0 part 
      └─vg_kvm5home-vo  253:2    0   80M  0 lvm  /home
    
  8. 從硬碟 /dev/vda 中分割出一個 lvm 分割區。
    [root@kvm5 ~]# fdisk /dev/vda 
    Welcome to fdisk (util-linux 2.23.2).
    
    Changes will remain in memory only, until you decide to write them.
    Be careful before using the write command.
    
    
    Command (m for help): p
    
    Disk /dev/vda: 4294 MB, 4294967296 bytes, 8388608 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disk label type: dos
    Disk identifier: 0x000f2281
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/vda1   *        2048      411647      204800   83  Linux
    /dev/vda2          411648     7579647     3584000   8e  Linux LVM
    /dev/vda3         7579648     7845887      133120   8e  Linux LVM
    
    Command (m for help): n
    Partition type:
       p   primary (3 primary, 0 extended, 1 free)
       e   extended
    Select (default e): e
    Selected partition 4
    First sector (7845888-8388607, default 7845888): 
    Using default value 7845888
    Last sector, +sectors or +size{K,M,G} (7845888-8388607, default 8388607): 
    Using default value 8388607
    Partition 4 of type Extended and of size 265 MiB is set
    
    Command (m for help): n
    All primary partitions are in use
    Adding logical partition 5
    First sector (7847936-8388607, default 7847936): 
    Using default value 7847936
    Last sector, +sectors or +size{K,M,G} (7847936-8388607, default 8388607): +100M
    Partition 5 of type Linux and of size 100 MiB is set
    
    Command (m for help): p
    
    Disk /dev/vda: 4294 MB, 4294967296 bytes, 8388608 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disk label type: dos
    Disk identifier: 0x000f2281
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/vda1   *        2048      411647      204800   83  Linux
    /dev/vda2          411648     7579647     3584000   8e  Linux LVM
    /dev/vda3         7579648     7845887      133120   8e  Linux LVM
    /dev/vda4         7845888     8388607      271360    5  Extended
    /dev/vda5         7847936     8052735      102400   83  Linux
    
    Command (m for help): t
    Partition number (1-5, default 5): 
    Hex code (type L to list all codes): 8e
    Changed type of partition 'Linux' to 'Linux LVM'
    
    Command (m for help): w
    The partition table has been altered!
    
    Calling ioctl() to re-read partition table.
    
    WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
    The kernel still uses the old table. The new table will be used at
    the next reboot or after you run partprobe(8) or kpartx(8)
    Syncing disks.
    
  9. 執行 partprobe 偵測新的分割區。
    [root@kvm5 ~]# partprobe
    
  10. 建立一個 10M 的 logical volume,因預設 pe 大小為 4M,故真正的 lv 大小為 3 個 pe,也就是 12M。
    [root@kvm5 ~]# pvcreate /dev/vda5
      Physical volume "/dev/vda5" successfully created
    [root@kvm5 ~]# vgcreate iscsi_vg /dev/vda5
      Volume group "iscsi_vg" successfully created
    [root@kvm5 ~]# lvcreate -n iscsi_vol -L 10M iscsi_vg 
      Rounding up size to full physical extent 12.00 MiB
      Logical volume "iscsi_vol" created.
    
  11. 進入 targetcli 命令環境產生 iSCSI target。
    [root@kvm5 ~]# targetcli 
    targetcli shell version 2.1.fb37
    Copyright 2011-2013 by Datera, Inc and others.
    For help on commands, type 'help'.
    
    />
    
  12. 將之前建立的 lv iscsi_vol 設定名為 kvm5.disk1 的 block-type backing store。
    /backstores> /backstores/block create kvm5.disk1 /dev/iscsi_vg/iscsi_vol 
    Created block storage object kvm5.disk1 using /dev/iscsi_vg/iscsi_vol.
    
  13. 產生 target iqn.2015-08.wang.deyu:kvm5 的 iSCSI Qualified Name (IQN),名稱自動設為 tpg1。
    /backstores> /iscsi create iqn.2015-08.wang.deyu:kvm5
    Created target iqn.2015-08.wang.deyu:kvm5.
    Created TPG 1.
    Global pref auto_add_default_portal=true
    Created default portal listening on all IPs (0.0.0.0), port 3260.
    
  14. 產生可存取 tpg1 的用戶端 (initiator) 為 iqn.2015-08.wang.deyu:kvm7。
    /backstores> /iscsi/iqn.2015-08.wang.deyu:kvm5/tpg1/acls create iqn.2015-08.wang.deyu:kvm7
    Created Node ACL for iqn.2015-08.wang.deyu:kvm7
    
  15. 產生 LUN,指定其為先前定義名為 kvm5.disk1 的 device。
    /backstores> /iscsi/iqn.2015-08.wang.deyu:kvm5/tpg1/luns create /backstores/block/kvm5.disk1
    Created LUN 0.
    Created LUN 0->0 mapping in node ACL iqn.2015-08.wang.deyu:kvm7
    
  16. 產生 target 開放的門戶為 192.168.122.5 在 port 3260,若執行時出現錯誤,表示系統已自動產生 0.0.0.0:3260 的門戶。
    /> /iscsi/iqn.2015-08.wang.deyu:kvm5/tpg1/portals create 192.168.122.5
    Using default IP port 3260
    Could not create NetworkPortal in configFS.
    
  17. 先刪除 0.0.0.0:3260 的門戶。
    /> /iscsi/iqn.2015-08.wang.deyu:kvm5/tpg1/portals delete 0.0.0.0 3260
    Deleted network portal 0.0.0.0:3260
    
  18. 成功產生 target 開放的門戶為 192.168.122.5 在 port 3260。
    /> /iscsi/iqn.2015-08.wang.deyu:kvm5/tpg1/portals create 192.168.122.5
    Using default IP port 3260
    Created network portal 192.168.122.5:3260.
    />
    
  19. 查看整個設定。
    /> ls
    o- / ..................................................................... [...]
      o- backstores .......................................................... [...]
      | o- block .............................................. [Storage Objects: 1]
      | | o- kvm5.disk1 ... [/dev/iscsi_vg/iscsi_vol (12.0MiB) write-thru activated]
      | o- fileio ............................................. [Storage Objects: 0]
      | o- pscsi .............................................. [Storage Objects: 0]
      | o- ramdisk ............................................ [Storage Objects: 0]
      o- iscsi ........................................................ [Targets: 1]
      | o- iqn.2015-08.wang.deyu:kvm5 .................................... [TPGs: 1]
      |   o- tpg1 ........................................... [no-gen-acls, no-auth]
      |     o- acls ...................................................... [ACLs: 1]
      |     | o- iqn.2015-08.wang.deyu:kvm7 ....................... [Mapped LUNs: 1]
      |     |   o- mapped_lun0 ........................ [lun0 block/kvm5.disk1 (rw)]
      |     o- luns ...................................................... [LUNs: 1]
      |     | o- lun0 ................. [block/kvm5.disk1 (/dev/iscsi_vg/iscsi_vol)]
      |     o- portals ................................................ [Portals: 1]
      |       o- 192.168.122.5:3260 ........................................... [OK]
      o- loopback ..................................................... [Targets: 0]
    
  20. 退出 targetcli。
    /> exit
    Global pref auto_save_on_exit=true
    Last 10 configs saved in /etc/target/backup.
    Configuration saved to /etc/target/saveconfig.json
    



De-Yu Wang 2018-09-07