How to Safely Shrink and Expand Linux Partitions

On
Safely Shrink and Expand Linux Partitions

Imagine this situation. Your root partition has reached 95% capacity, your package manager is stuck because it couldn’t download an update, and somewhere in the back of your mind, you’re convinced you’ve allocated too much space to the /home partition. The remedy is simple! Shrink one, and enlarge the other. But expanding and shrinking Linux partitions is not your cup of tea. One wrong move, and you may lose your important data. In this guide, we will learn about all the tools and best practices for resizing Linux partitions without any data loss. All you need is basic command-line familiarity. Let’s get started!

Safely Shrink and Expand Linux Partitions
📷 A Practical Guide to Safely Shrink and Expand Linux Partitions

Most of the tools discussed below work the same on all Linux distributions. So, no matter which distro you are using, this guide covers it. Most tools are preinstalled on all distros.

Read Also:
Create an Encrypted Virtual Hard Drive in Linux: Step-by-Step Guide

I've also covered partition resizing when you have a dual-boot system with Windows. Safety precautions are also covered to avoid inadvertent loss of information. Let's resize Linux partitions like a pro!

Why You’d Want to Resize a Partition in the First Place

Let's discuss some of the most common scenarios that may force you to go for partition resizing.

  • You have a dual-boot system (Windows and Linux), and the Linux partition is near capacity.
  • During installation, you gave too much space to one partition, and now you want that transferred to other important partitions.
  • You are migrating the current installation to a large disk and want to take advantage of larger disk capacity.
  • You are looking to add an LVM volume or want to create more partitions for a dual-boot setup.

A partition consists of two parts, viz., the partition table and the filesystem. While resizing a partition, you have to resize both these parts. The order in which you resize them depends on whether you are expanding or shrinking a partition.

The Golden Rule: Back Up Before You Touch Anything

Here's what you must remember when dealing with partition management of any type.

Never fiddle with a partition unless you have a verified backup of the same.

Although tools like gparted and parted are quite reliable, disk partitioning in itself is a risky process. A power loss in the middle of a read/write operation, a buggy filesystem, a bad disk sector, or a human error can literally transform the partitioning process into a data-loss disaster.

Therefore, to be on the safer side, always take a backup. Here are some good options for it:

  • To clone a full disk image, use dd or Clonezilla.
  • Use rsync if you just want to back up important files and directories on an external drive.
  • For sensitive and critical documents, use a secure cloud backup service.

Here's how you can create a disk image through the dd command:

sudo dd if=/dev/sda of=/path/to/backup/sda.img bs=4M status=progress

This process can be time-consuming, if the disk image is large. So be patient! If you just want to back up a few files under your /home directory, send them to another machine using the rsync command.

Understanding What’s Under the Hood

Let's understand the core parts a partition is made up of. It'll help us better understand the entire partitioning process.

Partition Table vs. Filesystem

Layer What It Does Common Tools
Partition table Dictates partition boundaries (start/end sectors) fdisk, parted, gdisk
Filesystem Governs how files and directories are stored in the partition resize2fs, xfs_growfs, ntfsresize
Volume manager (optional) Adds a flexible layer between partitions and filesystems lvextend, lvreduce, pvresize

If you are not using LVM, apparently you'll just deal with the first two layers discussed above. Some Linux distributions (e.g., Fedora or RHEL) use LVM inherently, which makes partitioning easier.

MBR vs. GPT

Every disk uses one of two partitioning schemes:

  • MBR (Master Boot Record): Legacy version. It limits primary partitions to 4 (or 3 primary + 1 extended partition with logical partitions inside), and supports a maximum disk size of 2TB.
  • GPT (GUID Partition Table): Modern and newer standard. It supports more partitions and disks that are larger than 2TB.

To check which partitioning scheme is in use on your system:

sudo parted -l

You'll either see Partition Table: gpt or Partition Table: msdos (msdos = MBR) in the command's output.

Filesystem Types and Resize Support

The XFS filesystem does not have shrink support. Except for that, filesystems like ext4, Btrfs, and NTFS have both shrinking and expansion support.

Shrinking a Partition: Step-by-Step

Shrinking a partition is risky when compared with an expansion operation. It's because you are reducing the space where your data already resides.

Note: Always first shrink the filesystem before shrinking the partition table entry.

Using GParted (Recommended for Most Users)

GParted partition manager
📷 GParted partition manager
  1. First of all, boot from the GParted Live USB. You can also boot through any Linux live USB that has GParted installed. The root partition can't be shrunk while it is in active use.
  2. Open GParted, and select the disk you want to work on from the dropdown menu in the top-right corner. Ensure that you have selected the correct disk and partition.
  3. Now, right-click on the partition you want to shrink, and select the Resize/Move option from the context menu.
  4. To specify the new shrunk partition size, either drag the slider or provide the value in the 'New size' text field.
  5. Click the Resize/Move option, followed by a click on the green checkmark to apply the changes.
  6. Wait for the process to finish.

GParted shrinks the filesystem and updates the partition table entry in the correct order automatically. That's why it is best for users who are not comfortable with the command-line environment.

Using the Command Line (ext4 Example)

In this example, let's say we will be working on disk /dev/sda2. Change this to whatever disk you are working on on your system.

Step 1: Unmount the partition. You can’t shrink a mounted ext4 filesystem.

sudo umount /dev/sda2

Step 2: First, check the filesystem for errors. Make it a habit before resizing any partition.

sudo e2fsck -f /dev/sda2

Step 3: Now, shrink the filesystem to the size you want (e.g., 40GB).

sudo resize2fs /dev/sda2 40G

Step 4: Use parted to shrink the partition table entry.

sudo parted /dev/sda
(parted) print
(parted) resizepart 2 40GB
(parted) quit

Step 5: Recheck the filesystem for any errors.

sudo e2fsck -f /dev/sda2

Step 6: Remount the partition and verify if everything looks ok.

sudo mount /dev/sda2 /mnt
df -h /mnt

Pro Tip: Always keep the filesystem size slightly smaller than the partition size.

So, that's it! We've just learned how to shrink a partition using a GUI or through a command-line environment.

Expanding a Partition: Step-by-Step

Expanding a partition is comparatively safer than shrinking one. It's because you are giving more space for the data to grow. There are no chances of cutting off data. But, some precautions are still needed.

Prerequisite: Adjacent Free Space

A partition can only be expanded if there is unallocated space right after it on the disk. If that is not the case, you’ll either have to delete/move that partition or use LVM, which addresses this limitation.

Using GParted

Expand a partition using GParted
📷 Expand a partition using GParted
  1. Open GParted.
  2. Select the right disk, and right-click on the partition having the unallocated space right next to it. Now choose the Resize/Move option.
  3. Drag the slider to cover all the available free (unallocated) space.
  4. Apply the changes to finish the expansion process.

GParted manages both partition table entry update and filesystem resizing.

Using the Command Line (ext4 Example)

Hypothetically, let's assume we have some adjacent free space beside the /dev/sda2 partition.

Step 1: Grow the partition table entry

sudo parted /dev/sda
(parted) print free
(parted) resizepart 2 100%
(parted) quit

In case you do not want to use all the available free space, reduce the 100% value to the percentage value you want.

Step 2: Expand the filesystem to cover the new partition size

sudo resize2fs /dev/sda2

You can note that specifying the partition size is not required here. The resize2fs command automatically expands it to the correct size.

Step 3: Verify the changes

df -h

For expansion of ext4 filesystems, unmounting is not required. You can expand it while it is live and in working state. It applies to root partitions as well.

Expanding XFS

Expanding an XFS filesystem is easy and can be done in a single command.

sudo xfs_growfs /mount/point

XFS partition expansion can be done in a mounted state, and by default, it will take all the available free space. If you want to specify the size, use the -D size option.

Resizing Partitions with LVM (The Flexible Way)

If your Linux system is using LVM (Logical Volume Manager), partition resizing is much easier. LVM adds an abstraction layer that makes resizing a cakewalk.

Here’s how to use LVM to resize a logical volume:

# 1. Extend the physical volume if the underlying partition/disk grew
sudo pvresize /dev/sda3

# 2. Extend the logical volume to use the new space
sudo lvextend -L +20G /dev/mapper/vg-root

# 3. Resize the filesystem to match (ext4 example)
sudo resize2fs /dev/mapper/vg-root

# For XFS instead of resize2fs:
sudo xfs_growfs /

For shrinking the partition using LVM, all you have to do is reverse the process. But if you are working with the ext4 filesystem, shrink the filesystem first, before you shrink the logical volume.

sudo umount /dev/mapper/vg-data
sudo e2fsck -f /dev/mapper/vg-data
sudo resize2fs /dev/mapper/vg-data 15G
sudo lvreduce -L 15G /dev/mapper/vg-data
sudo e2fsck -f /dev/mapper/vg-data

I strongly recommend setting up a LVM for new Linux systems as it makes the partition resizing process more stable and easier.

Special Case: Resizing a Partition Next to Windows (Dual-Boot)

Let's say you have a Windows + Linux dual-boot system. Now, you want to shift some space of the Windows NTFS partition to the Linux system, or vice versa.

To do it, follow this procedure:

  1. Boot into Windows and run the chkdsk /f command to fix all the filesystem errors, if any.
  2. Disable Fast Startup in Windows. To do it:
    • Press the Win + R key combo. Type powercfg.cpl in the search box and hit the Enter key.
    • Click the 'Choose what the power buttons do' option in the left-side menu.
    • Uncheck the 'Turn on fast startup' option, followed by a click on the Save changes button.
  3. If it's a regular hard drive, defragment it. If it's an SSD, skip this step.
  4. Then boot again through the Linux live USB and use GParted to go ahead with the partition resizing process as explained in the previous sections.

Remember, do not skip the first step no matter how time-consuming it is.

Best Practices Checklist

Here's what you need to follow for a seamless partition resizing session:

  1. Always back up your important data first. Never skip this.
  2. Use the lsblk or parted -l command to correctly identify the disk and partition you want to resize.
  3. If you are working with a root or boot partition, always boot the system through Linux Live USB.
  4. It's recommended to run a filesystem check through the e2fsck -f command before starting the actual resizing process.
  5. Always shrink the filesystem before updating the partition table entry, and always expand the partition first before growing the filesystem.
  6. Make sure you have a power backup for the system running a resize operation.
  7. Always use the df -h and lsblk commands to verify that the resize operation completed successfully.
  8. Finally, reboot the system and see if things are working normally to ensure the resizing is done correctly without any errors.

Conclusion

Linux partition resizing isn't something you should fear. But, at the same time, it's not a cakewalk either. The tools available for resizing are stable and mature enough to do the job correctly, every single time.

All you need is to follow the right process and adhere to the best practices of Linux partition resizing.

The three important steps to correctly resize a partition without any issues are: 1. To take a backup. 2. To select the right disk and partition. 3. To execute the commands in the correct order.