====== ISCSI ====== ===== Overview ===== ===== Server ===== ==== Installation ==== The following kernel options are required: {{Kernel|2.6.29-gentoo-r1|0@@}} Now install the iSCSI Enterprise Target packages: * {{Package|direct=yes|sys-block/iscsitarget}} Load the necessary modules, and make sure they are automatically loaded on boot: {{Root| modprobe crc32c modprobe iscsi_trgt }} {{File|/etc/conf.d/modules| modules_2_6="${modules_2_6} crc32c iscsi_trgt" module_crc32c_2_6="" module_iscsi_trgt_2_6="" }} ==== Configuration ==== Create an LVM partition to share, for example, a 10Gb partition named data from the pool volume group. This partition will not be mounted on the local machine. {{Root| lvcreate -L10G -n data pool }} Edit ietd.conf to mark this partition as an iSCSI target. The target specification contains the date the domain was registered in YYYY-MM format, followed by the hostname of the server, and finally an identifier. In this case, we've used the convention //vgname//.//lvname// for the target identifier. {{File|/etc/ietd.conf| Target iqn.2006-06.net.sihnon.jellybean.badger:pool.saffron Lun 0 Path=/dev/mapper/pool-saffron }} Now start ietd and ensure it auto-starts on boot. {{Root| /etc/init.d/ietd start rc-update add ietd default }} ===== Client ===== ==== Installation ==== We will be using open-iscsi to attach the iscsi targets on the clients. This is fussy about certain iscsi kernel options being built as modules, so ensure that the following options are modules and not built in directly. {{Kernel|2.6.29-gentoo-r1|1@@}} Install the client software: * {{Package|direct=yes|sys-block/open-iscsi}} ==== Configuration ==== Configure the targets, and have them loaded automatically: {{File|/etc/iscsid/initiatorname.iscsi| InitiatorName=iqn.2006-06.net.sihnon.jellybean.badger:pool.saffron }} {{File|/etc/iscsid/iscsid.conf| - node.startup = manual node.startup = automatic }} Configure the networking for open-iscsi. Find the MAC address for the network interface you'd like to use using ifconfig or similar, and then run the following commands. {{Root| iscsiadm -m iface -I iface0 --op=new iscsiadm -m iface -I iface0 --op=update -n iface.hwaddress -v AA:BB:CC:DD:EE:FF }} Now start iscsid, and have it autostart on boot. {{Root| /etc/init.d/iscsid start rc-update add iscsid default }} When iscsi targets are attached, a corresponding block device will be created in /dev, for example /dev/sdb. If the device contains a partition table, the corresponding partition nodes will be created as well. As this is a new target, we will need to create the partition table now, and format the partitions with a filesystem. Create a single partition the full size of the device, and format it with an ext3 filesystem. {{Root| fdisk /dev/sdb mke2fs -j /dev/sdb1 }} === Renaming targets === The device node that's created for a particular iscsi target is volatile, and depends on the state of the system when the target is first attached. We can use udev to stabilise this situation, with a bit of black magic. Create the following script, and then configure udev to exectute the script to determine a suitable symlink for the newly attached target. {{File|/lib/udev/iscsi_tgt.sh| #!/bin/bash DEV=`echo $1 | /bin/awk -F":" '{print $1":"$2":"$3}'` LUN=`echo $1 | /bin/awk -F":" '{print $NF}'` for i in /sys/class/iscsi_session/session* ; do test -d "${i}/device/target${DEV}/${DEV}:${LUN}" && echo `/bin/cat ${i}/targetname`:lun${LUN}: && exit 0 done }} {{File|/etc/udev/rules.d/10-iscsi.rules| BUS=="scsi", SYSFS{vendor}=="IET", SYSFS{model}=="VIRTUAL-DISK", KERNEL=="sd*", NAME="%k", PROGRAM="/lib/udev/iscsi_tgt.sh $id", SYMLINK+="iscsi/%c%n" }} Now that devices nodes are accessible at known locations, we can add an entry to fstab to make the partition mountable. The entry //must// have the noauto option set, as the device will not exist at boot time, and cannot be mounted until after iscsid has started and the target has been attached. {{File|/etc/fstab| /dev/iscsi/iqn.2006-06.net.sihnon.jellybean.badger:pool.saffron:lun0:1 /mnt/badger/saffron ext3 acl,noatime,noauto 0 0 }} === Automatically mounting volumes === If we want to automatically mount these filesystems after iscsid has started, we will need to use a custom init script as there does not appear to be any exiting way to do this. Add the following scripts. The latest version of the init script is held in subversion, under [[https://dev.sihnon.net/svn/configs/iscsi/|configs:/iscsi/]]. {{File|/etc/conf.d/iscsi-volumes| - List of volumes to mount VOLUMES=( "/dev/iscsi/iqn.2006-06.net.sihnon.jellybean.badger:pool.saffron:lun0:1" ) - Amount of time to sleep before mounting volumes, so that iscsid has time to - create any device nodes after connecting. SLEEP="2" }} {{File|/etc/init.d/iscsi-volumes| #!/sbin/runscript - Copyright 1999-2009 Gentoo Foundation - Distributed under the terms of the GNU General Public License v2 - $Header: $ depend() { need iscsid } start() { STATUS=0 ebegin "Automounting iscsi volumes" eindent einfo "Sleeping for ${SLEEP} seconds to allow device nodes to be created" sleep ${SLEEP} for v in ${VOLUMES[[@]]}; do RESULT=`mount $v` if [[|$? ]]; then einfo "Mounted $v" else ewarn "Failed to mount $v: $RESULT" STATUS=1 fi done eoutdent eend $STATUS } stop() { ebegin "Unmounting automounted iscsi volumes" eindent for v in ${VOLUMES[[@]]}; do RESULT=`umount $v` if [[|$? ]]; then einfo "Unmounted $v" else ewarn "Failed to unmount $v: $RESULT" fi done eoutdent eend 0 } }} Finally, add the iscsi-volumes script to start on boot, then restart iscsid to have the volume properly mounted. {{Root| /etc/init.d/iscsid restart /etc/init.d/iscsi-volumes start rc-update add iscsi-volumes default }} ===== See Also ===== * [[ISCSI Enterprise Target]] - Guide to installing IET on ubuntu, and sharing a virtual disk to a windows machine. * http://en.gentoo-wiki.com/wiki/ISCSI - Original article on which this is based.