Sharing DVD drive over iSCSI

The old SCSI target utils shipped with older Fedoras and RHEL-6 never really worked for one of my use-cases: sharing optical drives. I don't use them too extensively however sometimes I need to burn a CD/DVD or watch a DVD video. This was not really possible before LIO and it's pSCSI plugin (the old iSCSI target daemon required to know backend block device's size and didn't suport removable media). I didn't feel like equipping each of my computers with an optical drive. It was not even possible for all of them.

These days it's easy to set up. There is a good documentation on the LIO wiki. There are slight modifications required with Fedora 20.

Please note: The pSCSI backstore is generally unsafe and not recommended to use. There are features that won't work in all scenarions and things will break (the backend device does not implement all the SCSI commands that the initiator may issue).

Target configuration

On the box where the optical drive to be exported is physically present (the target) install the targetcli utility:

[root@localhost ~]$ yum install targetcli

Assuming the DVD drive is at /dev/sr0, here's the complete guide for "exporting" it as an iSCSI device to the network.

There is not much difference from the iSCSI guide on the LIO wiki. As root, start targetcli and then:

Create the pSCSI backend:

/> cd backstores
/backstores> pscsi/ create name=dvd_backend dev=/dev/sr0
Note: block backstore recommended for SCSI block devices
Created pscsi storage object dvd_backend using /dev/sr0

Now instantiate the iSCSI target:

/backstores> cd pscsi/dvd_backend
/backstores/pscsi/dvd_backend> /iscsi create
Created target iqn.2003-01.org.linux-iscsi.zaphod.x8664:sn.a5f5c643cc7e.
Created TPG 1.
Created default portal listening on all IPs (0.0.0.0), port 3260.

Note that the default portal has been created along with the iSCSI target. By default it listens on all the interfaces and port 3260. This is actually what I want (and check the iptables settings...).

Now I need to export the LUN:

/backstores/pscsi/dvd_backend> cd
/iscsi/iqn.2003-01.org.linux-iscsi.zaphod.x8664:sn.a5f5c643cc7e/tpg1/
/iscsi/iqn.20...c643cc7e/tpg1> luns/ create /backstores/pscsi/dvd_backend
Created LUN 0.

Configure authentication/authorization for the portal. I'm on a local network, trusting everyody, so I open the unrestricted access for everybody:

/iscsi/iqn.20...c643cc7e/tpg1> set attribute authentication=0 \
demo_mode_write_protect=0 generate_node_acls=1 cache_dynamic_acls=1
Parameter demo_mode_write_protect is now '0'.
Parameter authentication is now '0'.
Parameter generate_node_acls is now '1'.
Parameter cache_dynamic_acls is now '1'.

The resulting configfs tree should look like this:

/iscsi/iqn.20...c643cc7e/tpg1> cd /
/> ls
o- / ............................................................................. [...]
  o- backstores .................................................................. [...]
  | o- block ...................................................... [Storage Objects: 0]
  | o- fileio ..................................................... [Storage Objects: 0]
  | o- pscsi ...................................................... [Storage Objects: 1]
  | | o- dvd_backend .............................................. [/dev/sr0 activated]
  | o- ramdisk .................................................... [Storage Objects: 0]
  o- iscsi ................................................................ [Targets: 1]
  | o- iqn.2003-01.org.linux-iscsi.zaphod.x8664:sn.a5f5c643cc7e .............. [TPGs: 1]
  |   o- tpg1 .......................................................[gen-acls, no-auth]
  |     o- acls .............................................................. [ACLs: 0]
  |     o- luns .............................................................. [LUNs: 1]
  |     | o- lun0 ....................................... [pscsi/dvd_backend (/dev/sr0)]
  |     o- portals ........................................................ [Portals: 1]
  |       o- 0.0.0.0:3260 ......................................................... [OK]
  o- loopback ............................................................. [Targets: 0]
  o- sbp .................................................................. [Targets: 0]
  o- vhost ................................................................ [Targets: 0]

Now, save the configuration so it doesn't get lost.

/> saveconfig
Last 10 configs saved in /etc/target/backup.
Configuration saved to /etc/target/saveconfig.json

The machine now "exports" its /dev/sr0 DVD drive over iSCSI with complete SCSI command pass-through.

There is one catch however: On Fedora 20 there is no unit file that would ensure the configuration is reloaded automatically on boot. This means the iSCSI target will be inaccessible after reboot until you run:

[root@localhost ~]$ targetcli restoreconfig

So... TODO: create a systemd unit file that would run that on boot.

Initiator configuration

Now I have the DVD device accessible over iSCSI. Let's configure the initiator (the machine which wants to connect the remote drive). Again, install the necessary packages first:

[root@localhost ~]$ yum install iscsi-initiator-utils

Assuming the iSCSI target machine's IP address is 192.168.122.1, here's how to discover and login to the target:

[root@localhost ~]# iscsiadm --mode discoverydb --type sendtargets \
    --portal 192.168.122.1 --discover
iscsiadm: Could not scan /sys/class/iscsi_transport.
192.168.122.1:3260,1 iqn.2003-01.org.linux-iscsi.zaphod.x8664:sn.a5f5c643cc7e
[root@localhost ~]# iscsiadm --mode node --login
Logging in to [iface: default, target: iqn.2003-01.org.linux-iscsi.zaphod.x8664:sn.a5f5c643cc7e,
portal: 192.168.122.1,3260] (multiple)
Login to [iface: default, target: iqn.2003-01.org.linux-iscsi.zaphod.x8664:sn.a5f5c643cc7e,
portal: 192.168.122.1,3260] successful.

This created /dev/sr0 device node as well as /dev/cdrom symlink to it. Now I can play video DVDs and audio CDs remotely, even burn optical media and do whatever a "normal" optical drive would let me to.

Please note: the initiator configuration is actually persistent. That means the system will try to connect the iSCSI device on the next boot.

In the case one decides to remove the remote device, just logout of the session and remove the node. Again this is a persistent change:

[root@localhost ~]# iscsiadm -m session -u
Logging out of session [sid: 1, target: iqn.2003-01.org.linux-iscsi.zaphod.x8664:sn.a5f5c643cc7e,
portal: 192.168.122.1,3260]
Logout of [sid: 1, target: iqn.2003-01.org.linux-iscsi.zaphod.x8664:sn.a5f5c643cc7e,
portal: 192.168.122.1,3260] successful.
[root@localhost ~]# iscsiadm -m node --delete