How to resize a Root Partition in Linux

How to resize a Root Partition in Linux

Image for postPhoto by Will Francis on Unsplash

Resizing a root partition is tricky. In Linux, there isn?t a way to actually resize an existing partition. One should delete the partition and re-create a new partition again with the required size in the same position.

I have come across this process when I had to use a custom Debian image and boot up a VM on Azure. The size of the image is around 3GB and the size of the root device (/dev/sda) would be configured to be about 10GB. I preferred extending the existing partition to make use of 10GB on the root device.

Resizing a Linux Root File System partition involves three main steps

Recreating the Partition with the desired size:

Understanding the fdisk Commands: The below snippet is the output of the help text that fdisk prints out when m is typed in the prompt. It also includes the mapping of the steps with the commands to be used.

Welcome to fdisk (util-linux 2.33.1).Changes will remain in memory only, until you decide to write them.Be careful before using the write command.Command (m for help): mHelp:DOS (MBR)a toggle a bootable flag # This is required for Rootfsb edit nested BSD disklabelc toggle the dos compatibility flagGenericd delete a partition # Step 2 – Delete Existing partitionF list free unpartitioned spacel list known partition typesn add a new partition # Step 3 – Create a new partition with the same sector number as the start valuep print the partition table # Step 1(and 4)- Print the existing partition table. Note the sector number in the start column and if the boot bit is set (*)t change a partition typev verify the partition tablei print information about a partitionMiscm print this menuu change display/entry unitsx extra functionality (experts only)ScriptI load disk layout from sfdisk script fileO dump disk layout to sfdisk script fileSave & Exitw write table to disk and exit # Step 5: Write The partition tableq quit without saving changesCreate a new labelg create a new empty GPT partition tableG create a new empty SGI (IRIX) partition tableo create a new empty DOS partition tables create a new empty Sun partition table

The process involves these 5 steps as seen below

  • Print the existing partition table ( Make a note of boot bit and the sector number of the start column)
  • Delete the existing partition
  • Create a new partition with the same first sector number
  • Specify the last sector if needed. It defaults to creating the partition till the end. If you only want to have a single partition on the disk, choosing the default option is the best idea
  • Print the newly created partition table
  • Write the partition table

sudo fdisk /dev/sdaCommand (m for help): p Device Boot Start End Blocks Id System/dev/sda1 * 2048 9437183 4717568 83 LinuxCommand (m for help): dSelected partition 1Command (m for help): p Device Boot Start End Blocks Id SystemCommand (m for help): nCommand action e extended p primary partition (1-4)pPartition number (1-4, default 1): 1First sector (2048-10485759, default 2048):Using default value 2048Last sector, +sectors or +size{K,M,G} (2048-10485759, default 10485759):Using default value 10485759Command (m for help): p Device Boot Start End Blocks Id System/dev/sda1 2048 10485759 5241856 83 LinuxCommand (m for help): wThe partition table has been altered!Calling ioctl() to re-read partition table.WARNING: Re-reading the partition table failed with error 16: Device or resource busy.The kernel still uses the old table. The new table will be used atthe next reboot or after you run partprobe(8) or kpartx(8)Syncing disks.

The Kernel should be informed of the updated Partition table:

After writing the partition table there would be a warning, which mentions that the kernel is still using the old partition table. To update the kernel with the new partition table, we could either use partprobe or kpartx. I have personally used partprobe and prefer that. It can be installed using the below command.

apt install -y partprobe

After installing run partprobe on the command line. partprobe utility is used to make sure the kernel is aware of the new partition. If everything about re-creating the partition is correct, it shouldn?t error out anything. If the below error occurs, it’s possible that you could have specified the wrong start sector, and the next time the system boots, it will not boot correctly since the start sector is not correct. partprobe doesn?t give a constructive message in this situation, but when you get an error like below, it’s worth checking if the start sector is the same as the previously existing partition that was deleted.

partprobe /dev/sdaWarning: WARNING: the kernel failed to re-read the partition table on /dev/sda (Device or resource busy). As a result, it may not reflect all of your changes until after reboot

Resizing the File System

After the partprobe run is successful, the final step is to resize the root file system. This is the step to write in the file system to the newly added blocks into the partition. You can first start with checking the filesystem on the partition using the e2fsck command and then resize it.

e2fsck -f /dev/sda1resize2fs /dev/sda1

These steps would let you resize the root file system as per your requirement without requiring to reboot the system.


No Responses

Write a response