Saturday 21 August 2010

Grow ext2 filesytem on an iSCSI target

When you grow an iSCSI volume there are several steps you need to take before you can get some extra space on your ext2 or ext3 filesystem. You need to get the OS to notice that the target has grown, and then you need to grow the filesystem.

This is only possible if:
• You have your filesystem mounted directly on the raw device
• You are using a volume manager like LVM2

If you have you device partitioned then you probably can't grow them this way. You may be able to if the partition is the last on the device and you grow that partition.

Firstly let’s check the size of the filesystem, and find out which device it is mounted on:
  root@server:~# df -h /build/my_mnt
  Filesystem Size Used Avail Use% Mounted on
  /dev/sde 49G 40G 5.9G 88% /build/my_mnt

Now we'll check the size of the device:
  root@server:~# cat /proc/partitions | grep -E "name|sde"
  major minor #blocks name
  8 64 51200000 sde

It's currently 50G. At this point you'll need to log into your SAN storage appliance and extend your iSCSI target to say 100G.

Once it’s extended we need to get the OS to notice. To do this we'll need the HBTL.
  root@server:~# ls -l /sys/block/sde/device
  lrwxrwxrwx 1 root root 0 2010-08-19 09:39 /sys/block/sde/device -> ../../../6:0:0:0

Now we can instruct the kernel to rescan the device so it sees it has grown:
  root@server:~# echo 1 > /sys/class/scsi_device/6:0:0:0/device/rescan

Let’s check it again to make sure it worked:
  root@server:~# cat /proc/partitions | grep -E "name|sde"
  major minor #blocks name
  8 64 104857600 sde

You will also be able to see log messages from the kernel in on of the /var/log/ files:
  root@server:~# grep sde /var/log/kern.log
  Aug 19 09:40:50 server kernel: [5507931.974895] sd 6:0:0:0: [sde] 209715200 512-byte logical blocks: (107 GB/100 GiB)
  Aug 19 09:40:50 server kernel: [5507931.975697] sde: detected capacity change from 52428800000 to 107374182400
  Aug 19 09:40:50 server kernel: [5507932.223757] VFS: busy inodes on changed media or resized disk sde

If you are using a volume manger like LVM2 you will need to grow the size of the logical volume with lvresize to take up the rest of the device.

Now unless you're running a kernel with a bunch of special patches applied then you are going to have to unmount your filesystem. There are lots of things that can stop you from being able to unmount a partition and I'll discuss here the ones I am aware of. When this happens you will get this output:
  root@server:~# umount /build/my_mnt
  umount: /build/my_mnt: device is busy.
          (In some cases useful info about processes that use
           the device is found by lsof(8) or fuser(1))

In order to unmount the filesystem we have to firstly unmount any filesystems mounted within it.
  root@server:~# cat /proc/mounts
  grep /build/my_mnt
  /dev/sde /build/my_mnt ext3 rw,relatime,errors=continue,user_xattr,acl,data=writeback 0 0
  /dev/loop0 /build/my_mnt/mnt iso9660 ro,relatime 0 0

So here we need to unmount /build/my_mnt/mnt before unmounting /build/my_mnt. Before we do, we need to make sure nothing is using either of those mounts.We can check if there are any loopback devices set up like this:
  root@server:~# losetup -a
  /dev/loop0: [0840]:4769717 (/build/my_mnt/cdrom.iso)
  /dev/loop5: [0840]:3068274 (/build/my_mnt/initrd.ext3)

We can either unmount these or use losetup to remove them. If they were mounted the loopback will be cleaned up:
  root@server:~# umount /dev/loop0
  root@server:~# umount /dev/loop5
  umount: /dev/loop5: not mounted
  root@server:~# losetup -d /dev/loop5

We can now see the loopbacks have gone and the mount within our filesystem:
  root@server:~# losetup -a
  root@server:~# cat /proc/mounts
  grep /build/my_mnt
  /dev/sde /build/my_mnt ext3 rw,relatime,errors=continue,user_xattr,acl,data=writeback 0 0

Now lets check if there are any open, mmaped, or locked files on our filesystem:
  root@server:/build/mny_mnt# lsof
  grep /build/my_mnt
  bash 24654 root cwd DIR 8,64 4096 2 /build/my_mnt
  emacs 24796 root cwd DIR 8,64 4096 2 /build/my_mnt
  lsof 24799 root cwd DIR 8,64 4096 2 /build/my_mnt
  grep 24800 root cwd DIR 8,64 4096 2 /build/my_mnt
  lsof 24801 root cwd DIR 8,64 4096 2 /build/my_mnt

The lsof and grep entries are from the command we just ran with is nolonger running. The emacs process needs to be closed. The bash entry is becasue we are currently in the directory where our filesystem is mounted. After changing directory to / and exiting the emacs process the open files should nolonger be an issue.

Our filesystem may still be in use by a network share from another computer. For samba:
  root@server:~# /etc/init.d/samba stop
  root@server:~# umount /build/my_mnt
  root@server:~# /etc/init.d/samba start

For NFS:
  root@server:~# exportfs -f
  root@server:~# umount /build/my_mnt
  root@server:~# exportfs -a

Be aware that anyone using the network share will now be accessing the directory on the unlerlying filesystem until we remount. The filesystem should be free to unmount if you haven't already:
  root@server:~# umount /build/my_mnt

Now to resize the filesystem. This could take quite a while and you'll probably be probted to check the filesystem first:
  root@server:~# resize2fs /dev/sde
  resize2fs 1.41.9 (22-Aug-2009)
  Please run 'e2fsck -f /dev/sde' first.

  root@server:~# e2fsck -f /dev/sde
  e2fsck 1.41.9 (22-Aug-2009)
  Pass 1: Checking inodes, blocks, and sizes
  Pass 2: Checking directory structure
  Pass 3: Checking directory connectivity
  Pass 4: Checking reference counts
  Pass 5: Checking group summary information
  /dev/sde: 1213129/6406144 files (6.6% non-contiguous), 10618919/12800000 blocks

  root@server:~# resize2fs /dev/sde
  resize2fs 1.41.9 (22-Aug-2009)
  Resizing the filesystem on /dev/sde to 26214400 (4k) blocks.
  The filesystem on /dev/sde is now 26214400 blocks long.

Finally we can remount the filesystem. You should probably restart the network shares to make sure they are looking at your mounted filesystem rather than the directory where it is mounted:
  root@server:~# mount -a
  root@server:~# /etc/init.d/samba stop
  root@server:~# /etc/init.d/samba start
  root@server:~# exportfs -f
  root@server:~# exportfs -a

We can now check the size of the filesystem to make sure it did really grow:
  root@server:~# df /build/my_mnt
  Filesystem 1K-blocks Used Available Use% Mounted on
  /dev/sde 103212320 41671520 56297920 43% /build/my_mnt

No comments:

Post a Comment