Partition Resizing with GNU Parted

You've installed Linux on a computer and everything's coming up roses -- X is working, your servers are running, and so on. Suddenly, you run into a problem! When writing a new file, you receive an ominous error message: "No space left on device." Your disk -- or at least one of your partitions -- is full.

You’ve installed Linux on a computer and everything’s coming up roses — X is working, your servers are running, and so on. Suddenly, you run into a problem! When writing a new file, you receive an ominous error message: “No space left on device.” Your disk — or at least one of your partitions — is full.

One way to deal with this problem, of course, is to delete unnecessary files. Depending upon the partition that’s affected, you might search for and delete core files, remove unused but large packages, delete temporary files, delete large but unnecessary user files, or otherwise remove data you don’t need. This approach can be effective, but sometimes it’s not enough. Another approach is to add disk space by purchasing and installing a new hard disk. And sometimes, what’s needed is a wholesale restructuring of the layout of your disk. Perhaps you gave too much space to /home and not enough space to /usr. Perhaps you’ve created too many partitions and need to consolidate them. Whatever the case, you need to deal with the problem by using tools that can repartition your hard disk.

Unfortunately, the simplest disk partitioning tools tend to be destructive: to create a new partition layout you must destroy the old one, including any data on the disk. The standard Linux fdisk utility, like the DOS/Windows tool of the same name, is an example of such a program. Another class of tools is less destructive, though. These tools are known generically as dynamic partition resizers. They enable you to change partition sizes without destroying the data they contain.

One of the earliest and simplest of these tools is the First Nondestructive Interactive Partition Sizer (FIPS), which can split a single File Allocation Table (FAT) primary partition in two. FIPS comes with many Linux distrubitions, but because it works only on primary FAT partitions, it’s not very helpful if you need to resize a Linux partition.

A frequently-cited example of a flexible partition resizer is the commercial PartitionMagic from PowerQuest (http://www.powerquest.com). This tool provides an easy-to-use GUI and can resize FAT, NTFS, ext2fs, ext3fs, and Linux swap partitions. (Older versions could also handle OS/2 HPFS partitions.) PartitionMagic, though, is a commercial program that costs more than a new mid-sized hard disk, so buying it may not be worthwhile for a single-use operation.

But there is a program that fills the middle ground: GNU Parted (available from http://www.gnu.org/software/parted/parted.html). This program is an open source tool that aims to do more-or-less what PartitionMagic does. GNU Parted features a simpler, text-based user interface, though, and it’s not nearly as polished. Nonetheless, GNU Parted is extremely useful in helping to manage partitions as your needs for disk storage space change.

Before proceeding with any partition resizing tool, be aware that dynamic partition resizing is inherently dangerous! A power outage, bug, or other problem during dynamic partition resizing can wreak havoc with your partitions — possibly even with partitions you’re not modifying. You should always back up your data before using one of these tools!

Principles of Partition Management

The most common partitioning schemes on x86 computers today use three types of partitions:

Primary partitions. The original x86 partitioning scheme supports just four partitions, which today are known by the name primary partition. DOS and Windows must boot from a primary partition, but Linux doesn’t suffer from this limit. Most disks have at least one primary partition.

Extended partitions. In order to extend the original x86 partitioning system to support more than four partitions, one primary partition can be set aside as an extended partition. This partition serves as a placeholder for additional logical partitions.

Logical partitions. A disk can support an arbitrary number of logical partitions, which in some sense reside within a single extended partition. If you use more than one or two Linux partitions, chances are most of them are logical partitions.

Other platforms support different partitioning schemes, which usually don’t have the primary/extended/logical distinction. GNU Parted supports the x86 partitioning scheme as well as several others, such as those used by Macintoshes and the BSD partitioning scheme.

No matter what the partition type, a partition is defined by its start and end point on the disk. On x86 systems, these points are usually specified in terms of the cylinder, head, and sector of the endpoints — that is, in terms of the CHS geometry. Many partitioning tools, including GNU Parted, try to present partition data in terms that are more meaningful to humans, such as the partition size in megabytes or gigabytes. Other programs, such as Linux’s fdisk, use cylinder numbers for partition management.

To resize a partition, GNU Parted must resize not just the partition (that part’s easy), but also the data structures on the partition — that is, the filesystem on the partition. Unfortunately, resizing a filesystem is a tricky proposition. If a filesystem is shrunk, data stored beyond the new end point of the partition must be moved. If a filesystem is expanded, fixed data structures may need to be expanded, necessitating the movement of files or other data structures. Overall, expanding a filesystem is usually easier than shrinking one. It’s also usually easier to change the filesystem’s end point than to change its starting point. In fact, GNU Parted supports resizing ext2fs, ext3fs, and ReiserFS partitions only by changing the end point.

When you use GNU Parted, you should remember that you may need to change your partition entries in /etc/fstab. Although you may be able to use GNU Parted to change non-vital filesystems from your main Linux system, changes to critical partitions such as the root filesystem may necessitate booting an emergency Linux system.


       page 1 2 3   next >>


Linux Magazine /
Partition Resizing with GNU Parted

What Can Parted Do?

Effective use of GNU Parted depends on a clear understanding of its capabilities and limitations. GNU Parted supports a handful of partitioning systems, a limited number of file systems, and a rich, but fixed set of operations.

GNU Parted can theoretically handle x86 partitions (aka the MS-DOS partition table), Intel GUID Partition Table (GPT) format (used on IA-64 systems), MIPS partition tables, PC98 partition tables, Sun and BSD disk labels, and Macintosh partition tables. Unfortunately, [based on the author's practical experience,] Parted frequently misbehaves when using partition table types other than the x86 partition table, so this wide range of support is more theoretical than real.

In terms of filesystems, GNU Parted supports FAT (the 16- and 32-bit varieties), ext2fs, ext3fs, ReiserFS, and Linux swap. ReiserFS requires the presence of libreiserfs, available in the progsreiserfs package from http://reiserfs.osdn.org.ua. If these libraries aren’t present when the program is compiled, you won’t be able to manipulate ReiserFS partitions. Unfortunately, most precompiled GNU Parted binaries don’t work with ReiserFS. Version 1.6.5, compiled locally against the version of libreiserfs that ships with progsreiserfs 0.3.0, does work with ReiserFS.

As of version 1.6.5, GNU Parted can recognize, but not operate on, several other filesystems, including Windows NTFS, Macintosh HFS, BSD UFS, and Linux JFS and XFS. Future versions of GNU Parted might well support operating on at least some of these filesystems, but for now you can’t do anything with them.

GNU Parted supports several operations on supported filesystems: creation, resizing, copying, moving, checking, and converting a filesystem type (between FAT=16 and FAT-32 only).

When resizing partitions, one important caveat for ext2fs, ext3fs, and ReiserFS file systems is that the start point of the filesystem must remain fixed. Therefore, if you want to shrink a filesystem to clear space at the start, you may not be able to do it. If you want to shrink a partition enough, though, you may be able to shrink it, copy it to a later position on the disk, and then delete the original. This operation isn’t always practical, though. When increasing the size of a FAT partition, Parted can’t increase the cluster size. This fact means that FAT-16 may be automatically converted to FAT-32.

Parted can create most filesystem types, but it can’t create an ext3 filesystem. You can use Parted to create an ext2 filesystem and then convert it to ext3fs by using tune2fs. In practice, Parted sometimes has problems recognizing or operating on filesystems created with some tools. For instance, Parted 1.6.5 couldn’t modify an ext2fs partition created on a Mandrake 9.1 system, and the same version of Parted didn’t even recognize a FAT partition created with mkdosfs on the same OS. Parted worked fine creating ext2fs and FAT partitions. The program also handled FAT partitions created in Windows and older ext2fs partitions.

Table One: A summary of GNU Parted commands

Command / Description

check partn

Checks the validity of the filesystem.

cp from-partn to-partn

Copies a filesystem from one partition to another.

mkfs partn fs-type

Creates a filesystem on an existing partition.

mkpart part-type start end

Creates a new partition on empty space.

mkpartfs part-type fs_type start end

Creates a new partition and filesystem on empty space.

move part-type start end

Moves a partition


Displays information on the partition table


Exits from the program

resize partn start end

Resizes a partition

rm partn

Removes (deletes) a partition


<< prev   page 1 2 3   next >>


Linux Magazine /
Partition Resizing with GNU Parted

Basic Parted Operations

To use GNU Parted, you should first install the package. If you need to operate on your system’s root partition, this may mean using an emergency disk with a copy of GNU Parted, or you might be able to temporarily install an extra hard disk with a Linux installation of its own. If you need to operate on ReiserFS, you may need to recompile both libreiserfs and Parted yourself.

When you’re ready, launch the software by typing its name followed by the name of the device you want to modify, as in:

# parted /dev/sda
(parted) ?

GNU Parted responds with the (parted) prompt. You can type a single question mark (?) to see a list of commands. Some of the more common commands are shown in Table One.

Most of the commands shown in Table One require information from you, such as the partition number, file system type, partition start or end points, and so on. You can type this information after the commands in the specified order, or you can type the command alone and the system will prompt you for the data. Partition sizes are specified in megabytes.

For example, suppose you want to create a new extended partition and two logical partitions using available free space. You might type the following commands:

(parted) mkpart extended 400 9600
(parted) mkpartfs logical reiserfs 400 7000
(parted) mkpartfs logical ext2 7000 9600

For most operations, GNU Parted doesn’t report on the progress of its operations or display summaries as commands are completed. Thus, if you see nothing but a new (parted) prompt, chances are the operation completed successfully. If necessary, though, you can type print to see the partition table and verify it. Don’t be alarmed if the program changes your specified partition sizes slightly to fit the disk’s CHS geometry.

Partitions are numbered as they are in fdisk, but Parted refers to partition numbers as minor numbers. For x86 systems, partition numbers 1 through 4 are the primary partitions. One of these may be an extended partition, as well. Partitions numbered 5 and up are logical partitions. When you create a partition, you can’t specify a partition number, but Parted does, when appropriate, ask you to specify the partition type — primary, extended, or logical. The program then assigns a number that befits the partition type.

Resizing Partitions

You can resize a partition using the resize command:

(parted) resize 4 7000 12860

This example resizes partition number 4 to start at 7,000 MB and end at 12,860 MB. If the partition is an ext2fs, ext3fs, or ReiserFS partition, it must already begin at 7,000MB or the command will fail. FAT partitions can be resized from the start as well as from the end. A resize operation can take several minutes to complete, depending upon the filesystem type and how much data must be moved.

If you want to shrink an ext2fs, ext3fs, or ReiserFS partition and expand the partition that precedes it on the disk, the inability of Parted to shrink from the start of a partition can be a real drag. Figure One illustrates just such a situation. The original layout includes a small /usr partition and a larger /home partition, but /usr is nearly full and /home has plenty of space; you’d like to shrink /home and expand /usr.

There are two ways to proceed:

1. Shrink the end of the /home partition. You can then move or copy this partition to a point later on the disk, leaving free space into which you can expand /usr. This solution will only work if you want to clear up more space than the /home partition will ultimately occupy, though; free space after the shrink operation must be greater than the partition’s size in order to support the copy. Alternatively, you can copy the partition twice, once to a spare disk and again back to the original disk.

2. Shrink the end of the /home partition. You can then create a new partition in the freed space and mount it somewhere in your directory tree (such as /usr/local) so as to hold some of the files from the partition you want to expand. Outside of GNU Parted, you’ll need to mount the new partition at a temporary mount point, copy the files to this new partition, test them, delete the original files, and change /etc/fstab to permanently mount the new partition where it belongs.

The first solution can be tedious and time-consuming, and may not even be possible, depending upon the partition sizes. The second solution is more likely to work, but if you need to make repeated adjustments to your partitions, it can result in an awkward patchwork of partitions.

Figure One: Resizing an ext2fs, ext3fs, or ReiserFS partition from the start isn’t possible with GNU Parted, so you must use a workaround approach.

More Partitioning Tools

In 2003, GNU Parted is the most sophisticated of the Linux dynamic partition resizers. There are other tools, though, and some of these may be appropriate in specific situations:

* resize2fs, which is part of the standard ext2fs tool set, resizes an ext2fs partition. It’s awkward to use “raw,” though, because you must resize the partition separately from the filesystem, using fdisk in addition to resize2fs.

* resize_reiserfs is the ReiserFS equivalent to resize2fs. It requires separate partition resizing using fdisk, and so is harder to use than GNU Parted.

* xfs_growfs can increase the size of a mounted XFS partition. You should first increase the size of the partition itself using fdisk, then type xfs_growfs mount-point.

JFS supports growing a filesystem when mounting a partition. Resize the partition using fdisk when the filesystem isn’t mounted, then use the resize mount option when mounting the filesystem. The driver should increase the filesystem’s size to fit the new space.

The XFS and JFS tools can be handy for expanding these filesystems, but expanding one filesystem usually means shrinking another one, so their utility may be limited. The resize2fs and resize_reiserfs programs are both capable of shrinking a partition, but they’re both harder to use than GNU Parted.

Roderick W. Smith is the author or co-author of eleven books, including Advanced Linux Networking and Linux Power Tools. He can be reached at rodsmith@rodsbooks.com.

Comments are closed.

 Stick a Fork in Flock: Why it Failed
 CentOS 5.6 Finally Arrives: Is It Suitable for Business Use?
 Rooting a Nook Color: Is it Worth It?
System Administration
 Scripting, Part Two: Looping for Fun and Profit
 Command Line Magic: Scripting, Part One
 Making the Evolutionary Leap from Meerkat to Narwhal
 Extended File Attributes Rock!
 Checksumming Files to Find Bit-Rot
 What's an inode?
 Putting Text to Speech to Work
 Look Who's Talking: Android Edition
 Upgrading Android: A Guided Tour
 A Little (q)bit of Quantum Computing
 Emailing HPC
 Chasing The Number