Home
avatar

lnblxj

OpenWrt iSCSI-将路由器空闲存储挂载为本地硬盘

iSCSI 允许在块级别共享块设备(例如硬盘),本质上将分区和文件系统的完全控制权交给连接到该设备的客户端。

由于其设计限制,同一时间仅允许一个客户端连接。iSCSI 主要用于为缺乏高级存储功能(如 RAID)的商业应用和设备提供网络存储。

  • 映射为本地磁盘,可用于安装软件
  • 一次仅允许一个客户端连接
  • 传输速率取决于路由器硬盘性能和内网带宽

步骤

依赖安装

# 更新软件源
opkg update

# 安装tgt
opkg install tgt

# 安装kmod-scisi-core
opkg install kmod-scsi-core

# 安装cfdisk
opkg install cfdisk

配置iSCSI

配置要挂载的磁盘

# 查看分区信息
cfdisk

如需重新划分分区,查看分区创建教程

分区创建教程

配置完成后,分区信息如下: 分区信息

编辑tgt配置

例如要将设备地址为/dev/sda5,大小为174.6GB的磁盘配置为iSCSI设备,则需将设备地址/dev/sda5写入tgt配置文件中

# 编辑tgt配置
vim /etc/config/tgt

配置目标1

config target 1
        option name 'openwrtiscsit1' # 改为便于识别的名称即可
#       list allow_name 'iqn.1994-05.org.example:fedcba987654'
#       list allow_address '192.168.2.128/27'

配置目标磁盘

config lun 1_1                        
        option device /dev/sda5    # 将硬件地址填写为/dev/sda5
#config lun 2_2                                                                                    
#       option device /dev/sdc        

配置认证信息

config account                                            
        list target 1                             
#       list target 2                                      
        option user "openwrt"                             
        option password "123456"       

wq保存并退出

重启tgt服务

service tgt restart

完整配置文件如下:

config options 'tgt'
# iothreads limits number of worker threads per rdwr target, default is 16
# which seems to be too much for an avarage router
        option iothreads '2'
#       option nop_count '3'
#       option nop_interval '1'
#       option logging '0'
#       list portal '[::1]'
#       list portal '127.0.0.1:3261'
#       list portal '0.0.0.0:3262'
#       list portal '[::]:3263'

config target 1
        option name 'openwrtiscsit1'
#       list allow_name 'iqn.1994-05.org.example:fedcba987654'
#       list allow_address '192.168.2.128/27'

#config target 2
#       option name 'iqn.2012-06.org.openwrt:t2'

# all options are set to default, except for the device
# lun "name" is constructed as TGTID_LUN
#config lun                        1_1
#       option 'device' '/dev/sda'
# type of scsi device. available options: disk, cd, pt (sg passthrough)
#       option 'type' 'disk'
# backing store access method: rdwr (read-write), aio (async IO), sg (for pt type only, device must
#       option 'bstype' 'aio'
# set sync and/or direct flags when opening device, affect only rdwr
#       option 'sync' '0'
#       option 'direct' '0'
# block size for lun, default is 512
#       option 'blocksize' 4096
# override SCSI mode page, see tgtadm man page for details
#       option 'mode_page' 'string'
# vendor, product, revision, SCSI ID and SCSI Serial number
#       option 'vendor_id' 'string'
#       option 'product_id' 'string'
#       option 'product_rev' 'string'
#       option 'scsi_id' 'string'
#       option 'scsi_sn' 'string'                      
# refuse write attempts. applies only to disk type            
#       option 'readonly' '0'                
# Disk devices default to non-removable, cd - to removable 
#       option 'removable' '0'                                         
#  0 = Classic sense format, 1 = Support descriptor format.   
#       option 'sense_format' '0'                                                                  
# Rotaion rate: 0: not reported, 1: non-rotational medium (SSD), 2-1024: reserverd, 1025+: "Nominal
#       option 'rotation_rate' '0'                                  
# A thin-provisioned LUN is represented as a sparse file.                                          
# When initiators use the SCSI UNMAP command TGTD will release the affected areas back to the files
# This option applies to the disk type and rdwr bstype                 
#       option 'thin_provisioning' '1'                                                             
                                                                                                   
config lun 1_1                        
        option device /dev/sda5                                                                    
                                   
#config lun 2_2                                                                                    
#       option device /dev/sdc                                                                     
                                                                    
config account                                            
        list target 1                             
#       list target 2                                      
        option user "openwrt"                             
        option password "123456"                  
                                                           
#config account                                            
#       option target 2                                                                            
#       option user "user2"                       
#       option password "pwd2"                           
#       option outgoing 1

Windows端配置iSCSI

Win + S 搜索 iSCSI

搜索 iSCSI

输入连接目标

输入连接目标

配置用户名、密码

点击连接->高级->启用CHAP登录->名称处输入用户名、目标机密输入密码->确定

配置用户名、密码

使用磁盘管理器初始化磁盘,并新建简单卷即可

初始化磁盘

配置完成

OpenWrt iSCSI 网络硬盘