Resize Linux File System
Overview
When working with a virtual machine sometimes it becomes necessary to resize a disk partition. With ext3/ext4 file systems in Linux there are several steps involved.
Find the File System
First you must find the file system to expand. Use the Linux 'df -h'
command to examine the list of file systems on the host. Note the drive which the file system is located on. For example if the volume is /dev/sdb1 then the file system is on /dev/sdb . Note that on a drive with multiple partitions it may only be possible to expand the last partition on the drive without extensive work.
Expand the Partition
Now that you have located the partition you can expand it. The partition must be unmounted during the process, so if it is required for the system to operate then you will have to boot up from a recovery or LiveCD to access the partition. If not then simply umount the partition with the Linux umount command. In our example that would be 'umount /dev/sdb1'
.
Once the partition is unmounted we can safely alter the partition table. To do this we use the fdisk command on the drive, which would be 'fdisk /dev/sdb'
in this example. Please note this is NOT sdb1 (the partition) but the entire drive. Use the 'p'
command to find the partition starting cylinder and type. You will then use the 'd'
command to delete the partition in question. Next you will need to use the 'n'
command to recreate the partition, making sure you use the same partition number, starting cylinder and type as the original. The ending cylinder or size should be greater than the original values to increase the size. If this was a bootable partition you must then use the 'a'
command to flag it as such again.
After you complete the changes use the 'w'
command to write out the partition table to the disk. Once this is complete you may wish to reboot if using a recovery or LiveCD to make sure the partition information is updated.
Expand the File System
Ensure the file system is still unmounted using the 'df -h'
command, as some Linux systems seem to remount the file systems after fdisk changes. Use the fsck command to force a check of the volume such as 'fsck -e /dev/sdb1'
to ensure it is clean prior to expansion. Once this is complete you can use the resize2fs command to grow the file system. By default it will grow to the size you made it in the partition table with the command 'resize2fs /dev/sdb1'
.