This is a read-only archive. Find the latest Linux articles, documentation, and answers at the new!

Feature: Linux

Resizing and defragmenting Linux filesystems

By Roderick W. Smith on October 13, 2003 (8:00:00 AM)

Share    Print    Comments   

Last time we talked about a few ways to optimize Linux filesystems. Now we'll talk about resizing and defragmenting, two other filesystem operations.
This article is excerpted from the recently published book Linux Power Tools.

Resizing filesystems

All too frequently, you discover only after installing Linux that your partitions aren't the optimum size. For instance, you might have too much room in /usr and not enough room in /home. Traditional fixes for this problem include using symbolic links to store some directories that are nominally on one partition on another partition; and backing up, repartitioning, and restoring data. In many cases, a simpler approach is to use a dynamic partition resizer. Fortunately, partition resizers exist for the most popular Linux filesystems, as well, so you can use these tools to manage your Linux installation.

Dynamic partition resizers are inherently dangerous. In the event of a power outage, system crash, or bug, they can do serious damage to a partition. You should always back up the data on any partition you resize. Also, you should never run a dynamic partition resizer on a partition that's currently mounted. If necessary, boot a Linux emergency system to resize your partitions.

Resizing ext2fs and ext3fs

Several tools exist to resize ext2 and ext3 filesystems:

resize2fs This program ships with the e2fsprogs package included with most distributions. The resize2fs program is fairly basic in terms of options. At a minimum, you pass it the device file associated with the partition, as in resize2fs /dev/hda4. This command resizes the filesystem on /dev/hda4 to match the size of the partition. You can also pass the partition size in allocation blocks, as in resize2fs /dev/hda4 256000 to resize a filesystem to 256,000 blocks. The resize2fs program doesn't resize partitions, just the filesystems they contain. Therefore, you must use resize2fs in conjunction with fdisk to resize a partition and its filesystem. If you want to shrink a filesystem, you should do so first and then use fdisk to shrink the partition to match. If you want to grow a partition, you use fdisk first and then resize2fs. Because getting filesystem and partition sizes to match is tricky, it's usually best to forgo resize2fs in favor of GNU Parted or PartitionMagic.

GNU Parted This program provides both filesystem and partition resizing at once, so it's easier to use than resize2fs. It's described in more detail shortly, in "Using GNU Parted."

PartitionMagic This commercial program from PowerQuest supports integrated filesystem and partition resizing operations of FAT, NTFS, ext2fs, ext3fs, and Linux swap partitions. PartitionMagic is easier to use than other ext2fs and ext3fs partition resizers, but it runs only from DOS or Windows. (The package ships with a DOS boot floppy image and a bootable CD ROM, so it's still useable on a Linux-only system.)

Resizing ReiserFS

Two tools are available for resizing ReiserFS:

resize_reiserfs This tool is ReiserFS's equivalent of the resize2fs program. Like resize2fs, resize_reiserfs resizes the filesystem, but not the partition in which it resides, so you must use this tool in conjunction with fdisk. If you only pass the program the partition identifier, it resizes the filesystem to fit the partition. If you pass an -s option and filesystem size, the program resizes the partition to the requested size, which you can specify in bytes, kilobytes, megabytes, or gigabytes (the last three options require K, M, or G suffixes, respectively). Alternatively, you can specify a change to the partition size by prefixing the size with a minus (-) or plus (+) sign. For instance, resize_reiserfs -s -500M /dev/sda5 reduces the size of the filesystem on /dev/sda5 by 500MB.

GNU Parted According to its web page, this program supports ReiserFS as well as other filesystems. Unfortunately, as of version 1.6.4, this support is more theoretical than real, because it relies on libraries that aren't present on most distributions, and that even a fresh build can't find when everything's installed according to directions. With luck, though, this support will improve in the future.

The ReiserFS resizing tools are not as mature as are those for resizing ext2 and ext3 filesystems. In fact, resize_reiserfs displays warnings about the software being beta.

Resizing XFS

XFS has long included a partition-resizing tool, xfs_growfs. As the name implies, this program is designed for increasing a filesystem's size, not decreasing it. Unlike most partition-resizing tools, xfs_growfs is designed to work only on a mounted filesystem. The safest way to use it is to unmount the filesystem, delete the partition using fdisk, create a new partition in its place, mount the filesystem, and then call xfs_growfs:

# xfs_growfs /mount/point

As you might guess, /mount/point is the partition's mount point. You may also add the -D size option to specify the filesystem size in allocation blocks. Various other options are also available, as described in the xfs_growfs man page.

Although GNU Parted's web page doesn't mention XFS support, the source code does include an XFS subdirectory. Parted refuses to work on XFS partitions, but this may change in the future.

Resizing JFS

JFS includes a rather unusual partition-resizing ability: It's built into the kernel's JFS driver. You can use this feature to increase, but not to decrease, the size of the filesystem. As with most other partition-resizing tools, you must modify the partition size first by using fdisk to delete the partition and then recreate it with a larger size. After you've done this, you should mount the partition as you normally do and then issue the following command:

# mount -o remount,resize /mount/point

This command resizes the filesystem mounted at /mount/point to occupy all the available space in its partition. No other partition-resizing tools are available for JFS, although there is a JFS subdirectory in the GNU Parted source code, suggesting that Parted may support JFS in the future.

Using GNU Parted

Because Parted is the most sophisticated open source partition resizer, it deserves more attention. You can pass it a series of commands directly or use it in an interactive mode. The latter is more likely to be helpful for normal one-time uses. Passing commands to Parted enables you to write scripts to help automate partition resizing. Typically, you launch Parted in interactive mode by typing the program's name followed by the device on which you want to operate. You can then type commands to resize, create, delete, and otherwise manipulate partitions:

# parted /dev/sda
(parted) print
Disk geometry for /dev/scsi/host0/bus0/target5/lun0/disc: 0.000-96.000 megabytes
Disk label type: msdos
Minor    Start       End     Type      Filesystem  Flags
1          0.023     48.000  primary   ext2       
2         48.000     96.000  primary   ext2
(parted) rm 2
(parted) resize 1 0.00 96.00
(parted) quit

This example deletes the second partition on the disk and resizes the first partition to fill all the available space. Unlike most Linux partition-management tools, Parted works in figures of megabytes. This fact can make translating Parted's partition start and end points to and from the cylinder boundaries upon which fdisk and other tools work tricky..

Resizing partitions

Most of the filesystem-resizing tools require that you modify the partition using fdisk. (GNU Parted and PartitionMagic are exceptions to this rule.) Precisely how you modify the filesystem's carrier partition depends on whether you'll be shrinking or growing the partition. The simplest case is growing a partition. When doing this, you should follow these steps:

1. Launch fdisk on the disk in question.

2. Type d in fdisk to delete the partition you want to grow. You'll be asked for the partition number.

3. Type n in fdisk to create a new partition in place of the old one. You'll be asked for the partition number and the start and end cylinders. The start cylinder must be the same as it was originally, and of course the end cylinder should be larger than the original.

4. Type w in fdisk to write your changes to disk and exit.

5. Follow the procedure for your filesystem-resizing tool to increase the filesystem size.

Of course, in order to grow a partition, there must be free space on the disk into which to expand the partition. This normally means that you'll have already deleted or shrunk a partition that follows the one you want to expand. If you want to expand a filesystem into space that's before it on the disk, your job is much harder. It's possible to expand the carrier partition as just described, but specifying an earlier starting point, and then use dd to copy a filesystem from later in the new partition to earlier in the partition. This task is tricky, though, because you must compute precisely how far into the newly expanded partition the existing filesystem begins. An error can easily wipe out all your data. Thus, I don't recommend attempting this task; instead, try creating a new filesystem in the earlier space and mount it at some convenient place in your directory tree. If the empty space is larger than the partition you want to move, you can create a new partition, move the original, verify that the copied partition is intact, delete the original partition, and expand the copied partition and the filesystem it contains.

In order to reduce the size of the filesystem, you must match the size of the partition to the filesystem, which can be a tricky task. Fortunately, there is a procedure that can make this task less error-prone:

1. Shrink the filesystem, using your filesystem-resizing tool, to a value that's smaller than you intend. For instance, if you want to shrink a 700MB partition to 500MB, shrink it to 400MB.

2. Use fdisk to resize the partition to the target size, such as 500MB. This target size should be larger than the filesystem by a wide enough margin to be comfortable.

3. Use the partition-resizing tool to expand the filesystem into the extra space on the partition, filling it exactly.

As with increasing the size of the filesystem, the start point of the filesystem must remain untouched. When moving space between filesystems, this requirement can create an awkward situation: You can shrink an earlier partition, but expanding the next partition into the freed space is risky.

Defragmenting a disk

Microsoft filesystems, such as the File Allocation Table (FAT) filesystem and the New Technology File System (NTFS), suffer greatly from disk fragmentation -- the tendency of files to be broken up into many noncontiguous segments. Disk fragmentation degrades performance because the OS may need to move the disk head more frequently and over greater distances to read a fragmented file than to read a nonfragmented file.

Fortunately, Linux's native filesystems are all far more resistant to fragmentation than are Windows filesystems. Therefore, most Linux users don't bother defragmenting their disks. In fact, defragmentation tools for Linux are hard to come by. One that does exist is called defrag, but this package doesn't ship with most distributions. Because it is an older tool, it won't work with most modern ext2fs partitions, much less any of the journaling filesystems.

If you think your system may be suffering from fragmentation problems, you can at least discover how fragmented your ext2 or ext3 filesystems are by performing an fsck on them. You may need to force a check by using the -f parameter. This action will produce, among other things, a report on the fragmentation on the disk:

/dev/hda5: 45/8032 files (2.2% non-contiguous), 4170/32098 blocks

This report indicates that 2.2 percent of the files are noncontiguous (that is, fragmented). Such a small amount of fragmentation isn't a problem. Unfortunately, the fsck tools for other journaling filesystems don't return this information, so you have no indicator of fragmentation on these filesystems. If you truly believe that fragmentation has become a problem, you may be able to improve matters by backing up the partition, creating a fresh filesystem, and then restoring the files. This procedure is likely to take far longer than the time saved in disk accesses over the next several months or years, though, so I only recommend doing it if you want to change filesystem types or have some other reason (such as replacing a hard disk) to engage in this activity.

As a general rule, fragmentation becomes a problem only if your disk is almost full. On a nearly full disk, Linux may have trouble locating a large enough block of free space to fit a file without fragmenting it. If you almost fill a disk and then delete files, the remaining files may or may not be fragmented, depending on which ones you deleted. For this reason, keeping your partitions from filling up is best. As a general rule, anything less than 80 to 90 percent full is fine from a fragmentation perspective.

Next time we'll finish this series by looking at how to recover files, either ones that were deleted or ones that were lost as a result of filesystem corruption.

Share    Print    Comments   


on Resizing and defragmenting Linux filesystems

Note: Comments are owned by the poster. We are not responsible for their content.


Posted by: Anonymous Coward on October 13, 2003 06:45 PM
I think it's also worth to note the open source Partition Magic "clone" <A HREF="" TITLE="">QTParted</a>.

It can do whatever Parted can and even more, e.g. safe, non-destructive NTFS resizing. Several distributions already includes it.



Posted by: Anonymous Coward on October 14, 2003 08:34 PM
That's because it's just a GUI to parted and a few other utilities. However, there is one thing that parted can do that QTParted can't: be run off a bootable floppy.



Posted by: Anonymous Coward on October 13, 2003 06:53 PM
...use LVM. If you have an ext2/ext3 partition on LVM, e2fsadm does all the dirty work for you. Great stuff.


LVM solves these problems.

Posted by: Anonymous Coward on October 13, 2003 09:03 PM
IN real UNIX we use LVM to do this stuff on the fly.

Linux has an LVM modeled after the best LVM in the world (HPUX). More people should use it. It's not hard to use at all once you get a hang for it.

Using LVM is 10000x easier than the old PeeCee way of using disk paritions etc.


Re:LVM solves these problems.

Posted by: Anonymous Coward on October 14, 2003 04:33 PM
Can you point to a good how-to ?



Re:LVM solves these problems.

Posted by: Anonymous Coward on October 16, 2003 12:33 AM
the official <A HREF="" TITLE="">LVM HOWTO</a> on the <A HREF="" TITLE="">linuxdoc project</a> is a start. with that, and hardcopies of the man pages, it should be possible to get a handle on converting an existing installation to LVM - though the easiest way is to set it up during installation; Red Hat at least has support for doing that, Mandrake might need a bit more cajoling.

(hardcopies of the man pages, you ask? well, install the LVM utilities - a single RPM package, in my case - then "man -t commandname >" and print out at your leisure.)


Re:LVM solves these problems.

Posted by: Anonymous Coward on November 20, 2003 10:08 PM
actually linux LVM wants to be even better than HpUX's. It considers (if I'm not wrong) a single partition as a PV, thus being more flexible. Regarding the real Unix World, Hp is going to optionally provide LINUX on their top edge Superdome Servers. Think about it.
Cheers from Rome.


Man, is this badly written.

Posted by: Anonymous Coward on October 13, 2003 09:38 PM
The safest way to use it is to unmount the filesystem, delete the partition using fdisk, create a new partition in its place, mount the filesystem, and then call xfs_growfs:

You're kidding right? This is the safest way? Perhaps you may want to point out the tiny detail that deleting the partition wipes out all of your data. Additionally, if you are re-creating the partition, you don't need xfs_growfs you just need to use mkfs and make the file system whatever size you want. You've already lost your data.

This is a terrible article. I hope your backups are good.


Re: Man, is this badly written.

Posted by: macemoneta on October 13, 2003 10:45 PM
> deleting the partition wipes out all of your data

No, it doesn't. It only manipulates the partition table on the drive; it does not write to the data pointed to by any partition table entry.

Certainly, if you incorrectly re-add the partition entry, the partition will not be mountable. As long as the start is the same, and the end is equal to or greater than the previous end, you won't have a problem.

It should be obvious, but any partition after the one you are growing needs to be relocated. You can't have two partitions overlapping.


Re:Man, is this badly written.

Posted by: Anonymous Coward on October 13, 2003 10:59 PM
mk2fs deletes all the data on a partition, not simply recreating the partition. For instance you can remove a partition, then recreate a partition at the exact same starting point on the disk and your data will be there... just don't reformat it!<nobr> <wbr></nobr>:) In some instances that has been the only way I could increase a partition size correctly for ext3.


Re:Man, is this badly written.

Posted by: Anonymous Coward on October 14, 2003 12:30 AM
You don't know what you are talking about and are spreading FUD (fear, Uncertainty, Doubt); the article is correct.

Deleting a partition table does not touch your data.
It only changes your ability to access that data.

If you recreate a partition at the same start with the same type you are fine.

Just be careful that you don't have the partition mounted!


Re:Man, is this badly written.

Posted by: Anonymous Coward on October 15, 2003 08:03 AM
One thing you do need to be aware of is that for 'logical' partitions, the partition tables are chained. If you remake the logical partition, but make it too small, then add a new logical partition after it, you will be destroying data, even if you fix the partition table later.


Re:Man, is this badly written.

Posted by: Anonymous Coward on October 14, 2003 12:46 AM
You're kidding right? This is the safest way? Perhaps you may want to point out the tiny detail that deleting the partition wipes out all of your data.

Sorry this isn't the way Linux tools work, with M$ tools (fdisk) you are correct but _not_ on Linux fdisk, that _only_ make changes to the partition table.

Used linux fdisk a lot of times to repair the mbr of a fat16 disk what was destroyed bij win95.

Gtx Jan.


What about ReiserFS ?

Posted by: Chris Spencer on October 14, 2003 12:24 AM
Up until the last batch of Linux distros came out, I was using ext(2/3), because that's what came with RedHat. However, the latest versions of SuSE and Slackware both ship ReiserFS. Just as he said, fcsk could perform most tasks, as well as using tune2fs to check the file system for errors. However, that doesn't work for ReiserFS. I was hoping he would mention the best way to do such things with that particular filesystem, but I see at the end he just mentioned defragmenting with ext3.


Re:What about ReiserFS ?

Posted by: Anonymous Coward on October 14, 2003 01:31 AM
ReiserFS is blazingly fast and doesn't have a fragmentation problem, due to its tree structure.

Whatever you do, don't use ext2/3.


ReiserFS in Redhat 8 and 9

Posted by: Anonymous Coward on October 14, 2003 07:05 AM
Redhat has support for formatting reiserfs since at least version 8.0 (I think) in the install if you type "linux reiserfs" at the boot prompt instead of enter.



Posted by: Anonymous Coward on October 14, 2003 05:05 AM
As others have pointed out, the Logical Volume Manager is the only way to go for resizing, expanding, changing filesystems. Mandrake provides its wonderful diskdrake tool that makes it very easy and intuitive to use.

This is much more sensical. Read a bit about LVM and you will be gladly surprised. Very intuitive and very useful.


dont forget partition commander

Posted by: Anonymous Coward on October 14, 2003 06:20 AM
VCOM Partition Commander 8 is cheaper than Partition Magic and has more features (FAT,FAT32,NTFS,ext2,ext3,ReiserFS,Linux Swap) and costs less. It also boots off of CD for the floppy free systems. I consider it an indispensable tool.


This story has been archived. Comments can no longer be posted.

Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya