服务器端安装配置

//安装iscsi服务
#yum -y install targetcli

//创建目录以便于设置共享
#mkdir /iscsi_iso
 
//进入进入管理控制台
#targetcli
/> cd backstores/fileio
/backstores/fileio> create iso /iscsi_iso/iso.img 2G    
Created fileio iso with size 2147483648 
 
//创建目标
/backstores/fileio> cd /iscsi
注意:命名在同一子网内确保是唯一的,命名格式为:iqn.yyyy-mm.<主机名反写>:自定义名称(自定义名称内不能有下划线)
创建一个客户的iqn. 这里的iqn是客户端的名字可以自己自定义
/iscsi> create iqn.2018-07.icee.cn:iso
Created target iqn.2018-07.icee.cn:iso.
Created TPG 1.
Global pref auto_add_default_portal=true
Created default portal listening on all IPs (0.0.0.0), port 3260.

//设定luns,也就是绑定访问的磁盘,也就是第一步创建的磁盘镜像的名称

/iscsi> cd iqn.2018-07.icee.cn:iso/tpg1/luns 
/iscsi/iqn.20...iso/tpg1/luns> create /backstores/fileio/iso
Created LUN 0.
 
//创建acls,允许访问的iqn或者说允许访问的客户端
/iscsi/iqn.20...iso/tpg1/luns> cd ../acls 
/iscsi/iqn.20...iso/tpg1/acls> create iqn.2018-07.icee.cn:iso
Created Node ACL for iqn.2018-07.icee.cn:iso
Created mapped LUN 0.

//在这里有一个步骤设置可以多台客户端共同访问:
添加一条新的acls 也是另外一台客户端的iqn  
/iscsi/iqn.20...iso/tpg1/acls>create iqn.1998-01.com.vmware:5b16a947-1b34-c8ce-b854-b82a72dc11f2-4679e709 
//我这里添加的是自己将要准备共享给esxi的共享iscsi,当然你也可以给其他的iqn同理


 
//设置ACL(客户端连接的IQN)这里可以不设置验证密码。
/iscsi/iqn.20...iso/tpg1/acls> cd iqn.2018-07.icee.cn:iso
/iscsi/iqn.20...7.icee.cn:iso> set auth userid=username
Parameter userid is now 'username'.
/iscsi/iqn.20...7.icee.cn:iso> set auth password=password
Parameter password is now 'password'.
/iscsi/iqn.20...7.icee.cn:iso> exit
Global pref auto_save_on_exit=true
Last 10 configs saved in /etc/target/backup/.
Configuration saved to /etc/target/saveconfig.json
 
//查看防火墙设置,或者关闭防火墙。
# ss -napt | grep 3260  查看端口
LISTEN     1      256          *:3260 
#firewall-cmd --add-port 3260/tcp --permanent  //在防火墙上开启相应的端口
//开启target服务
systemctl start target
//设置开机自行启动
systemctl enable target






//接下来我们设置客户端配置。
//安装客户端的服务
yum -y install iscsi-initiator-utils 

修改iqn改成自定义的,当然你先安装客户端的时候可以使用客户端给的
vi /etc/iscsi/initiatorname.iscsi
InitiatorName=iqn.2018-07.icee.cn:iso

vi /etc/iscsi/iscsid.conf
//取消以下注释:
node.session.auth.authmethod = CHAP
node.session.auth.username = username
node.session.auth.password = password
systemctl restart iscsi
systemctl enable iscsi
//发现target
[root@localhost ~]# iscsiadm -m discovery -t sendtargets -p 172.18.0.109
172.18.0.109:3260,1 iqn.2018-07.icee.cn:iso

[root@localhost ~]# iscsiadm -m node -o show
# BEGIN RECORD 6.2.0.874-7
node.name = iqn.2018-07.icee.cn:iso
node.tpgt = 1
node.startup = automatic
node.leading_login = No
iface.hwaddress = <empty>
iface.ipaddress = <empty>
iface.iscsi_ifacename = default
iface.net_ifacename = <empty>
iface.gateway = <empty>
iface.subnet_mask = <empty>
iface.transport_name = tcp
iface.initiatorname = <empty>
iface.state = <empty>
iface.vlan_id = 0
iface.vlan_priority = 0
iface.vlan_state = <empty>
iface.iface_num = 0
iface.mtu = 0
iface.port = 0
iface.bootproto = <empty>
iface.dhcp_alt_client_id_state = <empty>
iface.dhcp_alt_client_id = <empty>
iface.dhcp_dns = <empty>
iface.dhcp_learn_iqn = <empty>
iface.dhcp_req_vendor_id_state = <empty>
iface.dhcp_vendor_id_state = <empty>
iface.dhcp_vendor_id = <empty>
iface.dhcp_slp_da = <empty>
iface.fragmentation = <empty>
iface.gratuitous_arp = <empty>
iface.incoming_forwarding = <empty>
iface.tos_state = <empty>
iface.tos = 0
iface.ttl = 0
iface.delayed_ack = <empty>
iface.tcp_nagle = <empty>
iface.tcp_wsf_state = <empty>
iface.tcp_wsf = 0
iface.tcp_timer_scale = 0
iface.tcp_timestamp = <empty>
iface.redirect = <empty>
iface.def_task_mgmt_timeout = 0
iface.header_digest = <empty>
iface.data_digest = <empty>
iface.immediate_data = <empty>
iface.initial_r2t = <empty>
iface.data_seq_inorder = <empty>
iface.data_pdu_inorder = <empty>
iface.erl = 0
iface.max_receive_data_len = 0
iface.first_burst_len = 0
iface.max_outstanding_r2t = 0
iface.max_burst_len = 0
iface.chap_auth = <empty>
iface.bidi_chap = <empty>
iface.strict_login_compliance = <empty>
iface.discovery_auth = <empty>
iface.discovery_logout = <empty>
node.discovery_address = 172.18.0.109
node.discovery_port = 3260
node.discovery_type = send_targets
node.session.initial_cmdsn = 0
node.session.initial_login_retry_max = 8
node.session.xmit_thread_priority = -20
node.session.cmds_max = 128
node.session.queue_depth = 32
node.session.nr_sessions = 1
node.session.auth.authmethod = CHAP
node.session.auth.username = <empty>
node.session.auth.password = <empty>
node.session.auth.username_in = <empty>
node.session.auth.password_in = <empty>
node.session.timeo.replacement_timeout = 120
node.session.err_timeo.abort_timeout = 15
node.session.err_timeo.lu_reset_timeout = 30
node.session.err_timeo.tgt_reset_timeout = 30
node.session.err_timeo.host_reset_timeout = 60
node.session.iscsi.FastAbort = Yes
node.session.iscsi.InitialR2T = No
node.session.iscsi.ImmediateData = Yes
node.session.iscsi.FirstBurstLength = 262144
node.session.iscsi.MaxBurstLength = 16776192
node.session.iscsi.DefaultTime2Retain = 0
node.session.iscsi.DefaultTime2Wait = 2
node.session.iscsi.MaxConnections = 1
node.session.iscsi.MaxOutstandingR2T = 1
node.session.iscsi.ERL = 0
node.session.scan = auto
node.conn[0].address = 172.18.0.109
node.conn[0].port = 3260
node.conn[0].startup = manual
node.conn[0].tcp.window_size = 524288
node.conn[0].tcp.type_of_service = 0
node.conn[0].timeo.logout_timeout = 15
node.conn[0].timeo.login_timeout = 15
node.conn[0].timeo.auth_timeout = 45
node.conn[0].timeo.noop_out_interval = 5
node.conn[0].timeo.noop_out_timeout = 5
node.conn[0].iscsi.MaxXmitDataSegmentLength = 0
node.conn[0].iscsi.MaxRecvDataSegmentLength = 262144
node.conn[0].iscsi.HeaderDigest = None
node.conn[0].iscsi.IFMarker = No
node.conn[0].iscsi.OFMarker = No
# END RECORD

//登录target
[root@localhost ~]# iscsiadm -m node --login
Logging in to [iface: default, target: iqn.2018-07.icee.cn:iso, portal: 172.18.0.109,3260] (multiple)
Login to [iface: default, target: iqn.2018-07.icee.cn:iso, portal: 172.18.0.109,3260] successful.

//确认连接状态
[root@localhost ~]# iscsiadm -m session -o show
tcp: [5] 172.18.0.109:3260,1 iqn.2018-07.icee.cn:iso (non-flash)

//确认分区状况
[root@localhost iso]# cat /proc/partitions
major minor  #blocks  name

   8        0  209715200 sda
   8        1    1048576 sda1
   8        2  152171520 sda2
   8        3   52428800 sda3
   8        4          1 sda4
   8        5    4063232 sda5
  11        0    1048575 sr0
   8       16    2097152 sdb


//新建分区
[root@localhost ~]# fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。

更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。

Device does not contain a recognized partition table
使用磁盘标识符 0xebbdff66 创建新的 DOS 磁盘标签。

命令(输入 m 获取帮助):n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
分区号 (1-4,默认 1):
起始 扇区 (8192-4194303,默认为 8192):
将使用默认值 8192
Last 扇区, +扇区 or +size{K,M,G} (8192-4194303,默认为 4194303):
将使用默认值 4194303
分区 1 已设置为 Linux 类型,大小设为 2 GiB

命令(输入 m 获取帮助):s
Building a new Sun disklabel.
分区 1 已设置为 Linux native 类型,大小设为 2 GiB
分区 2 已设置为 Linux swap 类型,大小设为 48.7 MiB
分区 3 已设置为 Whole disk 类型,大小设为 2 GiB

命令(输入 m 获取帮助):w
The partition table has been altered!

Calling ioctl() to re-read partition table.
正在同步磁盘。
[root@localhost ~]#


//格式化分区
[root@localhost ~]# mkfs.ext4 /dev/sdb1
mke2fs 1.42.9 (28-Dec-2013)
文件系统标签=
OS type: Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=1024 blocks
128000 inodes, 511461 blocks
25573 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=524288000
16 block groups
32768 blocks per group, 32768 fragments per group
8000 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912

Allocating group tables: 完成
正在写入inode表: 完成
Creating journal (8192 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

//挂载分区
mkdir /iso
mount /dev/sdb1 /iso

查看是否挂载上去了还有磁盘类型
[root@localhost ~]# df -hT
文件系统       类型      容量  已用  可用 已用% 挂载点
/dev/sda3      ext4       50G  1.1G   46G    3% /
devtmpfs       devtmpfs  1.9G     0  1.9G    0% /dev
tmpfs          tmpfs     1.9G     0  1.9G    0% /dev/shm
tmpfs          tmpfs     1.9G  8.9M  1.9G    1% /run
tmpfs          tmpfs     1.9G     0  1.9G    0% /sys/fs/cgroup
/dev/sda1      ext4      976M  104M  806M   12% /boot
/dev/sda2      ext4      143G   61M  136G    1% /home
tmpfs          tmpfs     380M     0  380M    0% /run/user/0
/dev/sdb1      ext4      1.9G  5.9M  1.8G    1% /iso

查看/dev/sdb1 的uuid
[root@localhost ~]# blkid /dev/sdb1
/dev/sdb1: UUID="f3072c49-b159-46e9-b0cd-83cae896c57c" TYPE="ext4"

在/etc/fstab中添加
UUID=f3072c49-b159-46e9-b0cd-83cae896c57c       /iso       ext4     _netdev   0   0

自动连接 iscsi
# iscsiadm -m node -T iqn.2017-05.com.mwdserver:iscsimwd1 -p192.168.13.30:3260 -o update -n node.startup -v automatic

命令tune2fs -l /dev/sdb可以查到文件系统的UUID

在/etc/fstab中添加  
UUID=xxxxxxx  /data ext4   _netdev 0  0  替换掉原来的那行


转载于:https://blog.51cto.com/11728495/2136388