2012. március 10., szombat

ext4 filesystem online resizing

After looking into how to resize an ext3/ext4 filesystem, I found that it works! :) The tool called resize2fs can be used to resize a filesystem online. Actually, when the fs is mounted - online - only extending possible, shrink is not. It did not work for me. Here is a practical application about a successful shrink or resize demonstrated on my 8Gb pendrive. I have got a 2Gb filesystem mounted on /dev/sdb2 as shown below.

root@debian:~# df -m -T /media/usb
Filesystem     Type 1M-blocks  Used Available Use% Mounted on
/dev/sdb2      ext4      2030    48      1880   3% /media/usb

List what data what we have, if any before we'll try to shrink it to 1Gb.

root@debian:~# ls -l /media/usb
total 20
-rw-r--r-- 1 root root    26 Mar 10 00:36 bela
drwx------ 2 root root 16384 Mar 10 00:32 lost+found 

We see that a file named 'bela' is contained with the size of 26 bytes. Remember, we want to retain data and be able to see it after we will have resized the filesystem. Now, let's expand the filesystem. The underlying partition has more space , actually the no.of blocks is 3145728 which means it is a 3Gb partition holding a 2Gb filesystem on /dev/sdb2.

root@debian:~# fdisk -l /dev/sdb

Disk /dev/sdb: 7751 MB, 7751073792 bytes
239 heads, 62 sectors/track, 1021 cylinders, total 15138816 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x8ef631df

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1   *        2048     8390655     4194304    b  W95 FAT32
/dev/sdb2         8390656    14682111     3145728   83  Linux


Let's expand now with the following command 'online', without unmounting the filesystem. Your kernel needs to support online resizing. If we don't give the size option to the command, the resize2fs command will expand the filesystem to the end of the partition.

root@debian:~# resize2fs /dev/sdb2
resize2fs 1.42.1 (17-Feb-2012)
Filesystem at /dev/sdb2 is mounted on /media/iomega; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/sdb2 to 786432 (4k) blocks.
The filesystem on /dev/sdb2 is now 786432 blocks long.

It's done! The filesystem's size now is 3Gb, the increased size. Let's check if the OS can see the expanded size with 'df' utility.

root@debian:~# df -m -T /dev/sdb2
Filesystem     Type 1M-blocks  Used Available Use% Mounted on
/dev/sdb2      ext4      3053    96      2804   4% /media/iomega
Voila! In megabytes, the size is 3053 in Mb which is roughly a 3Gb filesystem. Finally, the data remained intact just as it was before, as we did not have to unmount the filesystem and the filesystem resize operation did not alter the data. Note that if you use LVM, you can expand the logical volume's size without unmounting the filesystem and then apply the resize2fs command. In an another article, I will show you how to use LVM to do that.

Nincsenek megjegyzések: