Resize Linux partition without losing data

Before performing the resize procedure is recommended to backup the data if possible.

Resize procedure will be performed using the tools listed below, so is better having a little knowledge about them before continue.

  • umount - is a command-line utility that detaches the file system mentioned from the file hierarchy.
  • fdisk - is a command-line utility that provides disk partitioning functions. We will use fdisk to delete and recreate our disk partition on required new size.
  • e2fsck - is a command-line utility used to check the ext2/ext3/ext4 family of file systems.
  • resize2fs - is a program used to resize ext2, ext3, or ext4 file systems.
  • mount - the opposite of unmount, it attaches the file system found on some device to the file hierarchy (the big file tree)
In our example here we will use a 4GB flash drive. The logic is the same for hard drive (Physical or virtual) and is very simple, resize the partition (by deleting and recreating it using fdisk) then resize the file system using resize2fs. Below is the detailed procedure to resize (shrink or grow) a Linux partition.

Unmount

First you have to unmount the partition using umount command. There are many methods to check if a partition is mounted:

  • df - displays the file system disk space usage.
  • mount - without any switch it displays all mounted file systems.
  • cat /proc/mounts - show all mounted file systems.

[[email protected] /]# df -h

Filesystem Size Used Avail Use% Mounted on
/dev/sda3 68G 55G 12G 83% /
/dev/sda1 485M 72M 388M 16% /boot
/dev/sdf1 2.8G 69M 2.6G 3% /my-mount-point

Our partition /dev/sdf1 is mounted to /my-mount-point and needs to unmounted.

[[email protected] /]# umount /dev/sdf1

Resize partition

[[email protected] /]# fdisk /dev/sdf

Welcome to fdisk (util-linux 2.21.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help):


Print the partition table using option "p" to check the partition you want to resize.



Disk /dev/sdf: 4037 MB, 4037017600 bytes
42 heads, 8 sectors/track, 23466 cylinders, total 7884800 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: 0xcbf56e62

Device Boot Start End Blocks Id System
/dev/sdf1 2048 5884799 2941376 83 Linux

Command (m for help):


In our case there is one partition, /dev/sdf1 . Use the option "d" to delete that partition.



Command (m for help): d
Selected partition 1
Partition 1 is deleted

Command (m for help):


Now you have to create a new partition using the option "n" . It is possible to specify a set, human-readable size instead of using sectors.

If you are recreating a bigger partition, ensure you create it with the same starting disk sector as before. Otherwise, resize operation will not work and the entire file system may be lost.



Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p):
Using default response p
Partition number (1-4, default 1):
Using default value 1
First sector (2048-7884799, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-7884799, default 7884799):
Using default value 7884799
Partition 1 of type Linux and of size 3.8 GiB is set


By default, fdisk offer the maximum available size to be used. In our example above we have created a bigger partition using all the space available (all sectors). Again, let's print the partition table for confirmation using option "p".



Disk /dev/sdf: 4037 MB, 4037017600 bytes
42 heads, 8 sectors/track, 23466 cylinders, total 7884800 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: 0xcbf56e62

Device Boot Start End Blocks Id System
/dev/sdf1 2048 7884799 3941376 83 Linux

Use the option "w" to write the changes if everything is correct.

The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[[email protected] /]#


Check the partition using command e2fsck:



[[email protected] /]# e2fsck -f /dev/sdf1
e2fsck 1.42 (29-Nov-2011)
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/sdf1: 11/184000 files (0.0% non-contiguous), 29022/735344 blocks
[[email protected] /]#


At this point you may increase or decrease the file system using resize2fs. In our example we will increase the size.



[[email protected] /]# resize2fs /dev/sdf1
resize2fs 1.42 (29-Nov-2011)
Resizing the filesystem on /dev/sdf1 to 985344 (4k) blocks.
The filesystem on /dev/sdf1 is now 985344 blocks long.


Finally, you can mount the partition using mount command without losing data. Then check the file size using df command.



[[email protected] /]# mount /dev/sdf1 /my-mount-point/

[[email protected] /]# df -h

Filesystem Size Used Avail Use% Mounted on
/dev/sda3 68G 55G 12G 83% /
/dev/sda1 485M 72M 388M 16% /boot
/dev/sdf1 3.7G 70M 3.5G 2% /my-mount-point


As you see, the mounted file system has been increased.