How to Extend Linux Partitions with Resize2fs?

Posted in

How to Extend Linux Partitions with Resize2fs?

Maaz Bin Asad
Last updated on June 10, 2022

    Continue reading to know how to extend Linux partitions with Resize2fs, the command-line utility that lets a user resize ext2, ext3, or ext4 file systems.

    While working with any operating system - be it Linux, Windows, or macOS - we need to partition a hard drive to be able to store and manage data in a more organized way.

    The most simplistic partition level is a simple logical separation of drive, for instance, we got a drive, and we split it into different drives. This approach is very efficient and helps us to separate the system information and data from the user information and data.

    Most admins perform multiple partitions. One partition might have just the operating system files, and we generally do not change many things associated with that partition. There might be a different partition for user files where lots of changes and updates can be performed during regular workdays.

    How to Extend Linux Partitions with Resize2fs?

    Another reason for partition is isolation, where we might want to partition out things separately just in case to prevent the scenario where the individual file system happens to become corrupted. If there is a bad part of the drive and it happens to have in it one particular partition, it may not affect any of the other partitions on our system.

    Many times a requirement arises when we want to resize the partition itself. In other operating systems, it's straightforward to resize any partition, but in Linux, it could be tricky. It has been seen many times that when we perform resizing, we lose some data.

    In this article, we have mentioned how you can resize any ext4 partition using the terminal and resize2fs command.

    <Note:> Before you perform resizing your Linux partitioning, check these points:

    • There must be enough disk space available on the drive or specific partition.
    • The partition cannot be extended if there is no free space available.
    • It is suggested to back up your data because data might be lost during partitioning.

    Before we start discussing how to extend Linux partitions with resize2fs, let's learn a little about the command-line utility.

    What is the resize2fs Command?

    The resize2fs [ -fFpPM ] [ -d debug-flags ] [ -S RAID-stride ] device [ size ] command can resize ext2, ext3, or ext4 file systems. It is often used to increase or decrease the size of the unmounted file system. It can also be performed on the unmounted file system to expand its size.

    The size attribute of the command specifies the new size of the file system. However, it can also be defined using a suffix, such as 'S,' 'K,' 'M,' or 'G,' where they signify 512-byte sectors, kilobytes, megabytes, or gigabytes, respectively.

    Using the resize2fs program , we cannot manipulate the overall size of the partition. To do so, we need to delete the partition(s) first and create a new one with a larger size.

    Resize a Live Filesystem on Linux

    Let us see how to make a partition and then resize it by using up all the free space present on the drive:

    1. List all the devices

    Before resizing the partition, let's have a look at all the devices we have and what partition we are going to resize. This is how to do so:

    $ lsblk
    
    NAME      MAJ:MIN   RM      SIZE    RO   TYPE  MOUNTPOINT
    …
    ………..
    sda         8:0      0      10G     0    disk         /
    ---sd1      8:1      0      5.9     0    part
    ….
    ……….

    Here, we are going to resize the "sda" device, which is a virtual drive of 10 GB, and it has one partition that is "sd1," which is about 5.9 GB. Here the "sda" device is in the root directory of the virtual drive, so here we are resizing the root directory.

    2. Get more information about the device

    Before resizing the partition "sd1" we need more information about it. Using the fdisk command, we can get more details on the complete device" sda." This is how:

    $ sudo fdisk /dev/sda
    [sudo] password for user:
    Welcome to fdisk (util-linux 2.32.1).
    changes will remain in the memory only until you decide to write them.
    Be careful before using the write command.
    
    Command (m for help): p
    Disk /dev/sda: 10GiB, 10737418240 bytes, 20972520 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
    Disklable type: dos
    
    Disk identifier: 0xb58f3284
    
    Device            Boot Start        End    Sectors  Size     Id    Type
    /dev/sda1               2048   12374015   12371968  5.9G     83    Linux
    Command (m for help):

    Some Important information: Start sector 2048, make sure you know this number before resizing the partition. Here the end sector number is 12374015, and that's what we need to change to resize the partition sd1.

    3. Delete the partition

    For deleting the partition "sd1" we use the "d" command. It will not remove the data. The data will be left exactly on the storage drive, but we are going to remove the entry for the partition. This is how to do so:

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

    4. Make a new partition

    Now we will create a new partition using the "n" command:

    Command (m for help): n
    Partition type
    p      primary (0 primary, 0 extended, 4 free)
    e       extended (container for logical partitions)
    Select (default p):

    Your setup may vary, but for this demo, we are going to use the primary type, as demonstrated follows:

    Select (default p): p
    Partition number (1-4, default 1): 1
    First Sector (2048-20971519, default 2048): 2048
    Last sector, + sectors or +size{K, M, G, T, P} (2048-20971519, default 20971519): 
    Created a new partition 1 of type 'Linux' and of size 10GiB
    Partition #1 contain a ext4 signature
    Do you want to remove the signature? [Y]es/[N]o: N
    Command (m for help):

    Here the sector number comes into play, the previous partition "sd1" starts with 2048. The start sector is 2048 here as well because there is no partition on the device. Previous "sd1" partition last sector was 12374015, but here we decided to give up all the space so here the last sector would be the default one which fills the partition with the size of the device itself.

    5. Write the changes

    Before you write the changes, make sure that you have done everything right. Do the following to check the detail:

    Command (m for help): p
    Disk /dev/sda: 10GiB, 10737418240 bytes, 20972520 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
    Disklable type: dos
    Disk identifier: 0xb58f3284
    Device            Boot Start       End    Sectors  Size   Id    Type
    /dev/sda1               2048  20971519   20969472   10G   83    Linux
    Command (m for help): w
    The partition table has been altered.
    Syncing disk.

    Now we have resized the partition with all the space present on the device sda.

    6. Expand the file system with resize2fs

    One more step we need to perform. Now, we need to tell the file system to use all the space present in the partition. Initially, the partition was 5.9GiB, then we resize it to 10 GiB. Now we need to tell the file system to expand it to 10GB:

    $ sudo resize2fs /dev/sda1
    resize2fs 1.44.1 (23-Feb-2020)
    Filesystem at dev/sda1 is mounted on /; on-line resizing required
    old_desc_blocks = 1, new_desc_blocks =2
    The filesystem on /dev/sda1 is now 2621184 (4k) blocks long.

    7. Reboot

    Now the partition has been resized. Just to make sure everything is ok, reboot the system. Use the reboot command to do so:

    $ reboot

    8. Verify

    Now verify if the changes have been made properly or not by using the following command:

    $  df -h
    
    Filesystem            Size  Used Avail  Use%  Mounted on
    /dev/sda1             9.8G  3.9G  5.6G   41%      /

    Conclusion

    Here we go through what is partition and how to extend Linux partitions with resizef2s. The simple approach is to remove the old partition and resize it with the new one. The data will remain intact on the device itself.

    We suggest you backup your data before you resize a partition because there is always a risk, although it's low. You could lose your data if some exception or error occurred during the process. So, it's always a good practice to back up the data before beginning with it.

    If you like this article or have any suggestions, please let us know by commenting down below.

    People are also reading:

    Leave a Comment on this Post

    0 Comments