Digital Developer Conference: Cloud Security 2021 – Build the skills to secure your cloud and data Register free

Resizing Linux partitions, Part 1: Basics

If you follow common partitioning advice, chances are your disk uses several partitions. If you set the sizes incorrectly or if your needs change, you may find yourself needing to resize your partitions. To do so, you must familiarize yourself with the requirements for resizing partitions and the tools that can do the job. In many cases, resizing operations can proceed smoothly; however, numerous stumbling blocks can cause problems for the unwary, or even for those who take adequate precautions.

This article first describes some of the underlying capabilities and limitations of resizing tools, as well as the preparatory steps you should take before you begin a resizing operation. It then covers the common GParted partition resizing tool (also known as Gnome Partition Editor).

Note: For brevity, I use the term resize to refer to several operations, including increasing the size of a partition, reducing the size of a partition, and moving a partition. Sometimes one of these operations is possible but others aren’t. Some of these operations are inherently riskier than others. When these differences are important, I note that fact.

Partition resizing basics

Before you fire up a partitioning tool, you should understand what these programs can do and when it’s appropriate to use them. You should also take notes on your current configuration and prepare a backup of your data.

When should you resize partitions?

Let’s review the distinction between partitions and file systems. Partitions are containers that consist of contiguous sectors on the disk; partitions are defined by simple data structures. File systems, on the other hand, are data structures that reside inside partitions. File systems enable the computer to locate individual files, and they’re much more complex than partitions. In practice, resizing a partition also usually requires resizing the file system it contains. For brevity, I refer to “resizing partitions” when I mean “resizing partitions and the file systems they contain.”

Partition resizing is inherently at least a little bit risky. Partition resizing software must alter low-level partition and file system data structures and possibly move significant amounts of data. Problems such as corrupt data on the disk, power failures, and system crashes can all cause catastrophic failure of the resizing process. For this reason, you should not undertake a partition resizing operation lightly, and, whenever possible, you should back up your data, as described in “Preparing a backup.”

As a general rule, I recommend against resizing unless the existing setup currently causes problems or will soon cause them. That is, one or more partitions must be nearly full or you must expect to add enough data to overfill a partition in the near future before you consider resizing.

You must also keep in mind the limitations of resizing. For instance, you can’t shrink certain file systems, and you may need to carefully plan the positioning of primary versus logical partitions in your new layout. In some cases, such problems can become brick walls in your resizing plans, forcing you to adopt alternatives, as described in Part 2 of this series.

Obtaining current partition use data

The first step in planning your partition resizing operation is to obtain information about how your disk is currently laid out and what partitions are running out of space. One convenient way to obtain such information is to use GParted, which presents a friendly GUI display, as shown in Figure 1. The display shows where partitions are located relative to one another and how much space each consumes. Unfortunately, GParted doesn’t always present complete information, so you may want to supplement its displays with the output of the text-mode df, fdisk, or other utilities.

Figure 1. GParted presents disk use summary information

GParted presents disk use summary information

Figure 1 shows a system that has a cramped /usr partition (/dev/sdb5) and plenty of free space in /home (/dev/sdb7), which can cause problems if you want to add more software to the computer. Given this partition layout, you might want to reduce the size of /home and increase the size of /usr. (Note that Figure 1 was obtained from an emergency boot disc, so the Mount Point column doesn’t reflect the running system’s mount points.)

File system-specific capabilities

Linux supports several native file systems, and dual-boot systems often share the disk with others too. As a general rule, ext2fs, ext3fs, ex4fs, ReiserFS, and Btrfs are the most flexible in terms of what you can do with them. These file systems can all be grown, shrunk, and moved by most implementations of GParted. XFS and JFS, unfortunately, cannot be shrunk, although they can be grown or moved.

Among common non-Linux file systems, FAT and NTFS can be grown, shrunk, and moved. HFS and HFS+, used on Macintoshes, can usually be shrunk and moved, but not grown. OS X’s Disk Utility can sometimes grow HFS+ partitions.

In GParted, you can select View > File System Support to obtain a table of file systems and the operations that GParted supports. If you try this in your regular Linux system, however, you should be aware that what is supported varies from one distribution to another. If you use an emergency disc, as described shortly, it may support different options than your regular installation does.

Planning a new partition layout

With the existing partition data and information about what you can do with your current file systems in hand, you can begin planning your new layout. You should consider several important factors:

  • Speed and safety — Changing the start point of a partition is usually slower and riskier than changing the end point because moving the start point entails rewriting much of the data in the partition. This effect is greater for partitions with more data than for those with less data.
  • Non-Linux file systems — Although GParted and its text-mode cousin GNU Parted can resize NTFS, FAT, HFS, and HFS+ file systems, you may want to consider using the native operating system’s tools instead of Linux tools when dealing with them. This is especially true of Microsoft® Windows® boot partitions on dual-boot systems because changing the start point of a Windows boot partition is likely to render Windows unbootable.
  • Primary vs. logical partitions — The Master Boot Record (MBR) partitioning system, used on most Linux computers, supports only four primary partitions. One of these partitions may be an extended partition, which can hold any number of logical partitions. Some operating systems, such as Windows, must boot from a primary partition. All your logical partitions must reside within a single extended partition, and so must be contiguous. If your system uses a globally unique identifier (GUID) Partition Table (GPT) rather than the older MBR system, these distinctions become moot because GPT supports only one partition type.
  • Device IDs — Take note of the device IDs, such as /dev/sda3, used by your Linux partitions. If these IDs change, you may need to make changes to your /etc/fstab file or your boot loader configuration (typically /boot/grub/menu.lst, /boot/grub/grub.conf, or /boot/grub/grub.cfg). Many modern distributions use Universally Unique Identifier (UUID) numbers rather than device identifiers in these files, so such changes may not be necessary. They also won’t be required if your new partition layout doesn’t change any critical device IDs. Note that primary partitions are numbered from 1 to 4, and the numbering system may contain gaps, whereas logical partitions are numbered from 5 up and may not contain gaps. Thus, if you add a primary partition, it’s possible that no partition numbers will change; but if you add a logical partition, all your other logical partitions’ numbers may change.

Preparing a backup

Once your changes are planned, you may be tempted to rush to implement them; however, because of the risks involved in repartitioning, you should back up your most critical data, if at all practical. If a backup is not practical, you’re well advised to make it so by purchasing the necessary hardware. Even aside from the risks involved in partition resizing, hard disks sometimes fail, and software problems can render file systems useless. Thus, having a backup is a good idea.

You can back up to whatever medium is most convenient. You may be able to use tar to create a system backup that will fit on a recordable DVD, once compressed; however, most modern systems have enough user data that a single DVD won’t hold it all. Thus, you may need to resort to some other medium, such as an external hard disk, to back up your user data.

Even if a full backup is impossible, you should back up the contents of /etc. This directory holds critical system configuration files, so keeping a copy of it can speed up recovery if you need to reinstall your operating system.

Using GParted to resize partitions

The easiest way to resize partitions in Linux is to use the GUI GParted program. Although you can run GParted from your regular Linux installation, it refuses to operate on any currently mounted partition. Thus, you can’t resize root (/) or any other partition that’s critical for the system’s functioning. Therefore, I describe how to implement your changes using a Linux emergency disc. After this disc is booted, you can run GParted much as you would from a regular system, using point-and-click operations to tell the software what you want it to do.

Running an emergency disc

Many Linux emergency disc systems exist, such as PartedMagic and SystemRescueCd (see Related topics). You may also be able to use your Linux installation media. Many distributions include a rescue mode that provides access to GParted or a similar utility. For illustrative purposes, I describe the use of PartedMagic 4.11; however, other utilities are similar in broad strokes. You may need to launch GParted in a different way, though.

Most emergency discs come only in x86 (32-bit) form, but these discs work fine on x86-64 (64-bit) computers, even if the system runs a 64-bit version of Linux. The file system data structures are not affected by the central processing unit (CPU) architecture. If you run on something other than standard personal computer (PC) hardware, however, such as a Macintosh, you may need to check that your emergency disc works with your hardware.

Typically, you must boot an emergency disc much as you boot a Linux installation disc. On some systems, inserting the disc and then rebooting the computer does the job. On some systems, you must press a function key to select the boot device or adjust a basic input/output system (BIOS) option to boot from the optical drive rather than from your hard disk. The details of how to do this are system-specific, so you may need to consult your manual for details.

When you boot PartedMagic, a boot menu appears. Select Default settings (Runs from RAM) to boot into the standard system. When the system boots, you see a desktop appear with icons for some common tools, including one called Partition Editor, which launches GParted. Double-click this icon to begin resizing your partitions.

Telling GParted about your changes

To resize a partition, right-click it and select Resize/Move from the pop-up menu. The result is a dialog box such as that shown in Figure 2. You can adjust the size and position of the partition using the graphical slider or the text-entry fields.

Figure 2. GParted provides a point-and-click interface for resizing partitions

GParted presents a point-and-click interface for resizing partitions

Figure 2 shows a resizing of /dev/sdb7 from Figure 1, moving its start point to the right to make room for an expansion of /dev/sdb5. To complete a resizing of Figure 1’s partitions, you have to also move the swap partition (/dev/sdb6) to the right prior to resizing /dev/sdb5. Alternatively, you can delete and recreate the swap partition; however, this might require altering UUID references to swap space in /etc/fstab. When the swap space is moved, you can expand /dev/sdb5.

Note the Align to field in Figure 2. This is a new feature with recent versions of GParted, and it enables you to align partitions to cylinders, to mebibyte (MiB) boundaries, or neither. Older disks are typically partitioned to align on cylinder boundaries, but MiB alignment is common on disks partitioned with Windows Vista or later. Linux is happy with either type of alignment. If your system dual-boots with Windows XP, cylinder alignment may be required. If the system dual-boots with Windows Vista or later, or if it uses a new disk with 4096-byte sectors, MiB alignment may be required.

If you want to take space from a logical partition to give it to a primary partition or vice versa, you must explicitly resize the extended partition that surrounds the logical partitions. You do so just like you resize any other partition; however, you’ll find it easier to click on its list entry than its entry in the diagram above the partition list.

GParted doesn’t allow you to resize partitions that are in use. Such partitions have a padlock icon next to their entries. You can right-click the partition and select Unmount to unmount it. Note that the extended partition is locked in this way if any of its contained logical partitions are in use, including swap space.

Implementing your changes in GParted

After you tell GParted about your changes, you must implement them by selecting the Edit > Apply All Operations menu item or by clicking the Apply icon. The result is a progress dialog box that summarizes what the program is doing. If you decide against an operation, you can use GParted’s undo feature or exit from the program without applying your changes. Your disk is then unchanged.

Partition resizing operations can take anywhere from a fraction of a second to several hours to complete, depending on the nature of the change and how much data must be moved. Under no circumstances should you interrupt an ongoing operation! Doing so can damage the file system, rendering your data irretrievable.

Altering disk references

If you add or delete partitions, you may need to alter your /etc/fstab file or your boot loader configuration, as noted earlier. To do so, you must mount the root (/) partition and edit the file. In Figure 1, the root partition is /dev/sdb1, and GParted can mount it at /media/sdb1, so you would then edit /media/sdb1/etc/fstab. If your system uses a separate /boot partition, you may need to mount it separately to edit the boot loader configuration in the boot partition’s grub/menu.lst, grub/boot.conf, or grub/boot.cfg file.

Examine these files and replace any references to the old partition ID numbers to the new ones. If you make any changes, back up the original configuration file before saving the new one.

Moving forward

At this point, you can reboot your system. In PartedMagic, you can click the icon in the lower left of the screen and select Logout, which then gives you the option of, among other things, rebooting. Be sure to remove the emergency disc, if you haven’t already done so. With any luck, your computer should reboot into your regular Linux operating system, but you’ll find that your disk space allocation has changed.

This article describes a basic partition resizing operation. Part 2 of this series, Advanced resizing describes more advanced and alternative options, including resizing Logical Volume Management (LVM), troubleshooting, and alternatives you may want to consider if resizing operations are inadequate.