Posts Tagged ‘iscsi’

Installing Debian with root on iSCSI

Saturday, October 25th, 2008

You know, if a normal person wanted a diskless Debian install, they’d use NFS – a widely supported way to have a root filesystem on a network share and therefore reasonably easy to set-up. I however, wanted to have the root filesystem on iSCSI since it’s a nice, fast way of achieving the same thing. I’m now going to document how I achieved this for those who wish to do the same, but first: if you want an easy solution, use NFS. Seriously. You need to have some experience as a Linux sysadmin, developer and Debian user to make this work.

HERE BE DRAGONS

You’re still reading? OK then, here goes.

The first step is to configure your iSCSI target, which I won’t go into here. You may find my previous post, Playing with iSCSI, helpful. Now grab a copy of the Debian netinst CD image, burn it to CD and boot from it on the machine you want to install on. Note: it’s essential that you have another machine already running the version of Debian that you’re installing.

Using another machine, you now need to get a copy of the open-iscsi sources. You should do this on a Debian system running the same version of Debian, with the same kernel on the same architecture as you are installing. Simply run:

apt-get source open-iscsi

You now need to make some modifications to the open-iscsi source to make it work in the Debian Installer environment (and in initramfs, which we’ll come to later). The changes are minimal, but for convenience I’ve produced a patch:

open-iscsi-initrdfix.patch

Change into the directory containing the open-iscsi source, and apply the patch as follows:

patch -p1 < /path/to/open-iscsi-initrdfix.patch

Now simply type make to build open-iscsi. This will build the open-iscsi userspace applications and the necessary kernel modules. If you find that open-iscsi refuses to build kernel modules with a complaint like the following:

make[1]: *** No rule to make target `linux_2_6_26′, needed by `kernel_check’. Stop.

Then don’t worry – this simply means that you can avoid compiling the modules, and will need to copy them from the running kernel instead.

You now need to copy some files, plus a few modules from the running kernel, to a USB pen-drive. From the open-iscsi directory you need the following (ignore the .ko files if the kernel modules didn’t build):

usr/iscsid
usr/iscsiadm
kernel/libiscsi.ko
kernel/iscsi_tcp.ko
kernel/scsi_transport_iscsi.ko

Plus the following files from your running kernel:

/lib/modules/`uname -r`/kernel/crypto/crc32c.ko
/lib/modules/`uname -r`/kernel/lib/libcrc32c.ko

If the open-iscsi kernel modules didn’t build before, copy the following too:

/lib/modules/`uname -r`/kernel/kernel/drivers/scsi/iscsi_tcp.ko
/lib/modules/`uname -r`/kernel/drivers/scsi/libiscsi.ko
/lib/modules/`uname -r`/kernel/drivers/scsi/scsi_transport_iscsi.ko

Now on the machine you’re installing on, begin the Debian installation and continue normally until after the stage where the network is configured. Now switch to a console (Alt+F2), insert your USB pen-drive and mount it. Copy the files off it and place them in /tmp.

You now need to create /etc/iscsi/initiatorname.iscsi with content like the following:

InitiatorName=iqn.1993-08.org.debian:01.665f5fd03076

The actual name isn’t important, just make something up (as long as it’s in the above format).
Now remove your USB pen-drive (important!) and run the following commands to get iSCSI going:

insmod /tmp/libiscsi.ko
insmod /tmp/scsi_transport_iscsi.ko
insmod /tmp/iscsi-tcp.ko
insmod /tmp/libcrc32c.ko
insmod /tmp/crc32c.ko
/tmp/iscsid
/tmp/iscsiadm -m discovery -t sendtargets -p <iscsi target ip address>:<port>
/tmp/iscsiadm -m node -L all

That will load all the required modules, discover your target and then connect to it. All being well, it will appear as a SCSI disk (check dmesg). Now you can continue the install normally. At the final stage of the install DON’T allow the system to reboot – the installed system can’t boot yet.

I decided that I wanted to boot using BOOTP and TFTP. You might want to boot from USB, a flash card or something else, in which case you’re on your own – I’m going to document how to boot using BOOP and TFTP. On your iSCSI target machine (or any machine, actually) configure the required daemons and drop in the Debian netboot files – you may wish to use Preparing Files for TFTP Net Booting as a guide.

Now that your installation is finished, you need to copy some files off the installed system to your pen drive. You need the initramfs and the kernel from /mnt/target/boot/ directory, named initrd.img-<kernelversion> and ‘vmlinuz-<kernelversion>. You also need the entire /etc/iscsi directory.

Now copy the files onto the machine you configured as your TFTP server, and put the kernel image into your tftp directory (probably /tftpboot). You now need to modify the initramfs image to add the iSCSI bits.

First, decompress the initramfs image into an empty directory:

gzip -dc <initramfs image> | cpio -id

Now, you need to copy in the contents of the pen drive to the right places. Copy the /etc/iscsi directory from the pen drive to etc/ in the directory containing the decompressed initramfs image. Also copy the iscsid binary to sbin/, and the iscsiadm binary to bin/.
Now you need to add the following to the end of scripts/init-premount/udev (we need to add it to one of the scripts run at the start of the boot process, and this is as good a place as any):

/bin/ipconfig -c dhcp -d eth0
/sbin/iscsid -f &
/bin/iscsiadm -m node -L all

You also need to copy in the kernel modules, into the correct paths:

lib/modules/<kernelversion>/kernel/crypto/crc32c.ko
lib/modules/<kernelversion>/kernel/lib/libcrc32c.ko
lib/modules/<kernelversion>/kernel/drivers/scsi/iscsi_tcp.ko
lib/modules/<kernelversion>/kernel/drivers/scsi/libiscsi.ko
lib/modules/<kernelversion>/kernel/drivers/scsi/scsi_transport_iscsi.ko

And finally, you need to edit the init script to reference the root partition, e.g:

export ROOT=/dev/sda1

Now re-create the initramfs image by running the following in the root of the directory in which you decompressed the image, and save it to your tftp directory:

find . | cpio -H newc -o > /tftpboot/initrd.img-<kernelversion>

That’s it. Ensure your TFTP server is fully configured, modify the PXE configuration to point to your kernel and initramfs image, then you can restart your system and hopefully you’ll get root on iSCSI…

Playing with iSCSI

Friday, March 2nd, 2007

I recently found myself needing more disk space on a system, but unfortunately the system concerned can only take one hard disk and I didn’t want to buy a bigger hard disk because I’ve got a load of smaller drives spare. So I figured I’d whack a hard disk into a USB enclosure and connect it to my server (rather than shut the server down to add the hard disk properly). As I’d been waiting for an excuse to play with iSCSI, I figured I’d give it a go rather than share the drive by NFS as I usually would.

There are basically two halves to iSCSI: a target, which is the device you want to share and an initiator, a system that wants to access the shared device. So on one system you need to setup and configure iSCSI target software and on the other initiator software.

I started by downloading, compiling and installing iSCSI Enterprise Target onto my server. Once installed, you need to set-up a configuration file (/etc/ietd.conf) to share the device which looks hideous, but actually it’s not too hard. This is mine:

Target iqn.2007-03.uk.co.demon.lug:myth
  Lun 0 Path=/dev/sdb,Type=fileio
  Alias myth
  MaxConnections 1

The first line has to be in a particular format:

iqn.yyyy-mm.<reversed domain name>[:identifier]

So it’s basically the date, the machine’s domain name reversed and a made-up identifier (I used ‘myth’ as the disk is for use by MythTV).
Then I give it the device node (sdb), give it an alias and specify that only one initiator may be connected at any one time. You can define security (a username and password, allowed/disallowed hosts etc.) but I’m on a secure network so I didn’t bother. That’s all the required configuration, now start the daemon:

/etc/init.d/iscsi-target start

On the initiator machine I downloaded, compiled and installed Open-iSCSI. The configuration of this is a bit more of a pain, but not too bad. Once it’s installed, you’ll need to run ‘depmod -a’ to allow the kernel to find the newly-installed modules.
You need to start by creating a configuration file called /etc/iscsi/initiatorname.iscsi. Mine is:

InitiatorName=mythtvbox
InitiatorAlias=mythtv

Basically you just have to make up a name and an alias, which the machine will use to identify itself to the target.
Now you need to start the iscsi daemon:

/etc/init.d/open-iscsi start

Now you need to configure the daemon to automatically connect to the target when started. First you need to ‘discover’ the target using the iscsi_discovery command:

iscsi_discovery <ip address/domain name>

In my case, this returns:

starting discovery to 10.0.0.254
Testing iser-login to target iqn.2007-03.uk.co.demon.lug:myth portal 10.0.0.254:3260
starting to test tcp-login to target iqn.2007-03.uk.co.demon.lug:myth portal 10.0.0.254:3260
discovered 1 targets at 10.0.0.254, connected to 0

That demonstrates that the connection is working. Now you can configure the daemon to connect automatically at start:

iscsiadm -m node -p <ip address/domain name> -T <target name as above> –op update -n node.conn[0].startup -v automatic

In my case, I did:

iscsiadm -m node -p 10.0.0.254 -T iqn.2007-03.uk.co.demon.lug:myth –op update -n node.conn[0].startup -v automatic

Now you need to restart the daemon. There’s a slight bug in the iscsi daemon which means that it doesn’t shut down properly (it isn’t really considered ’stable’ yet), so in addition to stopping it via the init script (/etc/init.d/open-iscsi stop) you’ll need to kill it off manually then restart it:

/etc/init.d/open-iscsi start

All being well, the hard disk should have appeared, so you can access it just like a physical disk connected by SCSI or USB – check in dmesg:

Loading iSCSI transport class v2.0-754.
iscsi: registered transport (tcp)
scsi0 : iSCSI Initiator over TCP/IP
scsi 0:0:0:0: Direct-Access IET VIRTUAL-DISK 0 PQ: 0 ANSI: 4
SCSI device sda: 160086528 512-byte hdwr sectors (81964 MB)
sda: Write Protect is off
sda: Mode Sense: 77 00 00 08
SCSI device sda: drive cache: write through
SCSI device sda: 160086528 512-byte hdwr sectors (81964 MB)
sda: Write Protect is off
sda: Mode Sense: 77 00 00 08
SCSI device sda: drive cache: write through
sda: sda1 sda2 < sda5 >
sd 0:0:0:0: Attached scsi disk sda
sd 0:0:0:0: Attached scsi generic sg0 type 0

Now you can enjoy the iSCSI-goodness. Sure sharing with NFS is easier, but apparently iSCSI is faster (actually, it probably doesn’t help very much at all when you’re talking over USB using emulated-SCSI to an IDE disk…).