Using iSCSI On Ubuntu 9
Using iSCSI On Ubuntu 9
Using iSCSI On Ubuntu 9
1 Preliminary Note
I'm using two Ubuntu 9.04 servers here:
Because we will run all the steps from this tutorial with root privileges, we can either prepend all commands in this tutorial with the string sudo, or we become root right now by typing sudo su
ISCSITARGET_ENABLE=true
We can use unused logical volumes, image files, hard drives (e.g. /dev/sdb), hard drive partitions (e.g. /dev/sdb1) or RAID devices (e.g./dev/md0) for the storage. In this example I will create a logical volume of 20GB named storage_lun1 in the volume group vg0: lvcreate -L20G -n storage_lun1 vg0
(If you want to use an image file, you can create it as follows:
mkdir /storage dd if=/dev/zero of=/storage/lun1.img bs=1024k count=20000 This creates the image file /storage/lun1.img with a size of 20GB. ) Next we edit /etc/ietd.conf... vi /etc/ietd.conf
... and comment out everything in that file. At the end we add the following stanza:
[...] Target iqn.2001-04.com.example:storage.lun1 IncomingUser someuser secret OutgoingUser Lun 0 Path=/dev/vg0/storage_lun1,Type=fileio Alias LUN1 #MaxConnections 6
The target name must be a globally unique name, the iSCSI standard defines the "iSCSI Qualified Name" as follows: iqn.yyyy-mm.<reversed domain name>[:identifier]; yyyy-mm is the date at which the domain is valid; the identifier is freely selectable. TheIncomingUser line contains a username and a password so that only the initiators (clients) that provide this username and password can log in and use the storage device; if you don't need authentication, don't specify a username and password in the IncomingUserline. In the Lun line, we must specify the full path to the storage device (e.g. /dev/vg0/storage_lun1, /storage/lun1.img, /dev/sdb, etc.). Now we tell the target that we want to allow connections to the device iqn.200104.com.example:storage.lun1 from the IP address192.168.0.100 (server1.example.com)...
vi /etc/initiators.allow
/etc/init.d/iscsitarget start
/etc/init.d/open-iscsi restart
Now we connect to the target (server2) and check what storage devices it has to offer: iscsiadm -m discovery -t st -p 192.168.0.101
server1:~# iscsiadm -m node 192.168.0.101:3260,1 iqn.2001-04.com.example:storage.lun1 server1:~# The settings for the storage device iqn.200104.com.example:storage.lun1 on 192.168.0.101:3260,1 are stored in the file/etc/iscsi/nodes/iqn.200104.com.example:storage.lun1/192.168.0.101,3260,1/default. We need to set the username and password for the target in that file; instead of editing that file manually, we can use the iscsiadm command to do this for us: iscsiadm -m node --targetname "iqn.2001-04.com.example:storage.lun1" --portal "192.168.0.101:3260" --op=update --name node.session.auth.authmethod --value=CHAP
iscsiadm -m node --targetname "iqn.2001-04.com.example:storage.lun1" --portal "192.168.0.101:3260" --op=update --name node.session.auth.username --value=someuser iscsiadm -m node --targetname "iqn.2001-04.com.example:storage.lun1" --portal "192.168.0.101:3260" --op=update --name node.session.auth.password --value=secret Now we can log in, either by running...
server1:~# iscsiadm -m node --targetname "iqn.2001-04.com.example:storage.lun1" --portal "192.168.0.101:3260" --login Logging in to [iface: default, target: iqn.2001-04.com.example:storage.lun1, portal: 192.168.0.101,3260] Login to [iface: default, target: iqn.2001-04.com.example:storage.lun1, portal: 192.168.0.101,3260]: successful server1:~# ... or by restarting the initiator:
/etc/init.d/open-iscsi restart
) In the output of
fdisk -l
you should now find a new hard drive (/dev/sdb in this example); that's our iSCSI storage device: server1:~# fdisk -l Disk /dev/sda: 32.2 GB, 32212254720 bytes 255 heads, 63 sectors/track, 3916 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Disk identifier: 0x00031334
Id 83 5 82
Disk /dev/sdb: 21.4 GB, 21474836480 bytes 64 heads, 32 sectors/track, 20480 cylinders Units = cylinders of 2048 * 512 = 1048576 bytes Disk identifier: 0x00000000 Disk /dev/sdb doesn't contain a valid partition table server1:~# To use that device, we must format it:
fdisk /dev/sdb
server1:~# fdisk /dev/sdb Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabe l Building a new DOS disklabel with disk identifier 0x882944df. Changes will remain in memory only, until you decide to write them. After that, of course, the previous content won't be recoverable.
The number of cylinders for this disk is set to 20480. There is nothing wrong with that, but this is larger than 1024, and could in certain setups cause problems with: 1) software that runs at boot time (e.g., old versions of LILO) 2) booting and partitioning software from other OSs (e.g., DOS FDISK, OS/2 FDISK) Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite) Command (m for help): <-- m Command action a toggle a bootable flag b edit bsd disklabel c toggle the dos compatibility flag d delete a partition l list known partition types m print this menu n add a new partition o create a new empty DOS partition table p print the partition table q quit without saving changes s create a new empty Sun disklabel t change a partition's system id
u v w x
change display/entry units verify the partition table write table to disk and exit extra functionality (experts only)
Command (m for help): <-- n Command action e extended p primary partition (1-4) <-- p Partition number (1-4): <-- 1 First cylinder (1-20480, default 1): <-- ENTER Using default value 1 Last cylinder or +size or +sizeM or +sizeK (1-20480, default 20480): <-- ENTER Using default value 20480 Command (m for help): <-- t Selected partition 1 Hex code (type L to list codes): <-- L 0 1 2 3 4 5 6 7 8 9 a b c e f 10 11 12 14 16 17 18 1b 1c Hex Empty FAT12 XENIX root XENIX usr FAT16 <32M Extended FAT16 HPFS/NTFS AIX AIX bootable OS/2 Boot Manag W95 FAT32 W95 FAT32 (LBA) W95 FAT16 (LBA) W95 Ext'd (LBA) OPUS Hidden FAT12 Compaq diagnost Hidden FAT16 <3 Hidden FAT16 Hidden HPFS/NTF AST SmartSleep Hidden W95 FAT3 Hidden W95 FAT3 code (type L to 1e Hidden W95 FAT1 24 NEC DOS 39 Plan 9 3c PartitionMagic 40 Venix 80286 41 PPC PReP Boot 42 SFS 4d QNX4.x 4e QNX4.x 2nd part 4f QNX4.x 3rd part 50 OnTrack DM 51 OnTrack DM6 Aux 52 CP/M 53 OnTrack DM6 Aux 54 OnTrackDM6 55 EZ-Drive 56 Golden Bow 5c Priam Edisk 61 SpeedStor 63 GNU HURD or Sys 64 Novell Netware 65 Novell Netware 70 DiskSecure Mult 75 PC/IX list codes): <-- 83 80 Old Minix 81 Minix / old Lin 82 Linux swap / So 83 Linux 84 OS/2 hidden C: 85 Linux extended 86 NTFS volume set 87 NTFS volume set 88 Linux plaintext 8e Linux LVM 93 Amoeba 94 Amoeba BBT 9f BSD/OS a0 IBM Thinkpad hi a5 FreeBSD a6 OpenBSD a7 NeXTSTEP a8 Darwin UFS a9 NetBSD ab Darwin boot b7 BSDI fs b8 BSDI swap bb Boot Wizard hid be Solaris boot bf Solaris c1 DRDOS/sec (FATc4 DRDOS/sec (FATc6 DRDOS/sec (FATc7 Syrinx da Non-FS data db CP/M / CTOS / . de Dell Utility df BootIt e1 DOS access e3 DOS R/O e4 SpeedStor eb BeOS fs ee EFI GPT ef EFI (FAT-12/16/ f0 Linux/PA-RISC b f1 SpeedStor f4 SpeedStor f2 DOS secondary fd Linux raid auto fe LANstep ff BBT
Command (m for help): <-- w The partition table has been altered!
Calling ioctl() to re-read partition table. Syncing disks. server1:~# Afterwards, the output of
fdisk -l
should look as follows: server1:~# fdisk -l Disk /dev/sda: 32.2 GB, 32212254720 bytes 255 heads, 63 sectors/track, 3916 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Disk identifier: 0x00031334 Device Boot /dev/sda1 * /dev/sda2 /dev/sda5 Start 1 3750 3750 End 3749 3916 3916 Blocks 30113811 1341427+ 1341396 Id 83 5 82 System Linux Extended Linux swap / Solaris
Disk /dev/sdb: 21.4 GB, 21474836480 bytes 64 heads, 32 sectors/track, 20480 cylinders Units = cylinders of 2048 * 512 = 1048576 bytes Disk identifier: 0x882944df Device Boot Start End /dev/sdb1 1 20480 server1:~# Now we create a filesystem on /dev/sdb1... mkfs.ext3 /dev/sdb1 Blocks 20971504 Id 83 System Linux
You should now see the new device in the outputs of...
mount
tmpfs on /lib/init/rw type tmpfs (rw,nosuid,mode=0755) proc on /proc type proc (rw,noexec,nosuid,nodev) sysfs on /sys type sysfs (rw,noexec,nosuid,nodev) udev on /dev type tmpfs (rw,mode=0755) tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev) devpts on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=620) /dev/sdb1 on /mnt type ext3 (rw) server1:~# ... and
df -h
Used Avail Use% Mounted on 685M 27G 3% / 0 253M 0% /lib/init/rw 88K 10M 1% /dev 0 253M 0% /dev/shm 173M 19G 1% /mnt
umount /mnt
To have the device mounted automatically at boot time, e.g. in the directory /storage, we create that directory... mkdir /storage
ext3
reboot
mount
server1:~# mount /dev/sda1 on / type ext3 (rw,errors=remount-ro) tmpfs on /lib/init/rw type tmpfs (rw,nosuid,mode=0755) proc on /proc type proc (rw,noexec,nosuid,nodev) sysfs on /sys type sysfs (rw,noexec,nosuid,nodev) udev on /dev type tmpfs (rw,mode=0755) tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev) devpts on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=620) /dev/sdb1 on /storage type ext3 (rw,_netdev) server1:~# df -h
Used Avail Use% Mounted on 685M 27G 3% / 0 253M 0% /lib/init/rw 88K 10M 1% /dev 0 253M 0% /dev/shm 173M 19G 1% /storage
4 Links
Open-iSCSI: http://www.open-iscsi.org/ iSCSI Enterprise Target: http://iscsitarget.sourceforge.net/ Ubuntu: http://www.ubuntu.com/