Sunday, April 01, 2007

Non-Destructive Partitioning with GParted

"Grandpa, tell me a story."

"Okay, little Percival, let me tell you what partitioning was like in the old days. If you already had information on your hard drive, but wanted to re-partition it, all your data would be erased. There were programs available that would partition your drive without erasing all your stuff, but they cost money."

"Gee, Grandpa, that story sure did suck."

It's true. That story did suck. But it's also true that, unless you really, really knew what you were doing and were willing to spend a lot of time on it, repartitioning your hard drive would destroy your data. Commercial programs were (and still are) available to partition your drive without data loss, but they cost money. If repartitioning your hard drive is something you only do once in rare while (and for most of us it is) free tools may be a better option.

Let's back up a bit. What is partitioning and why would you want to repartition? In the simplest terms, partitions are separate areas on your hard drive. In Windows a single hard drive can be partitioned to look like separate drives, so that what appears to be drive C:, drive D:, and drive E: may all be separate areas of a single physical drive. In Linux they may appear as hda1, hda2, and so on. There are many reasons why a hard drive may be partitioned, but a very common one is to keep the operating system and user data separate. In this way if the operating system is to be upgraded or restored the user's data doesn't have to be touched, lessening the risk of data loss. Another reason is to control damage from runaway files. In Linux most log files are kept in the /var folder. Sometimes servers are set up to put /var in a separate partition. If a runaway process starts filling up a log file it can only fill up the partition /var is on, leaving the free space on the other partitions untouched.

GParted is a free utility that allows you to partition your hard drive without destroying your data. It is a Linux program, but can be used to partition filesystems for Windows (FAT16, FAT32, NTFS) and Mac OS X (HFS, HFS+) as well. A full listing of the filesystems and operations available can be found here.

GParted does many checks and is very safe, but keep in mind that the partitioning process is only non-destructive if everything goes well. I think GParted says it best with the message it gives you if you try to run as a regular user:

Always back up your data before major operations on your hard drive.

Even if you're not running Linux on your computer, you can still use GParted by booting from the LiveCD. The LiveCD is a small image (about 50 MB) that contains a stripped down version of the Gentoo Linux operating system, the Fluxbox window manager, and GParted. It's probably the easiest way to partition your hard drive, as you won't have to worry about unmounting partitions. It is dangerous to modify a mounted partition, and using the LiveCD ensures all partitions are unmounted unless you explicitly mount them.

Download the iso image and burn it to a CD. If you're running Windows your CD burning program should have an option to "Burn Image to Disc" or something similar. Don't just burn it as a data disk; it won't work. In Linux, if you're using Gnome, just right click the iso image and select "Write to Disc." With KDE right click the image and select "Actions," then "Write CD Image with K3B." Burn the CD, put it in the CD tray, and reboot.

You'll see a screen like this:

Just select the first option (GParted LiveCD) with the arrow keys and press Enter. You'll be prompted to hit Enter a couple more times to accept the default language and keyboard template. Once the boot process is complete you will see this:

A list of partitions on your hard drive will be listed. Mine looked like this:

I have four partitions on one hard drive: hda1, hda2, hda3, and hda4. Only four primary partitions are allowed on a single hard drive, so hda4 is made as an extended partition, with hda5 and hda6 under it. Don't worry too much about what extended partitions are. They are primary partitions that are partitionable, allowing you to fit more than four partitions on a single hard drive.

My problem was that the partition my operating system was on, hda2, was too small at about 5 GB. I wanted it to be twice that size. I needed to take some space from my data partition, hda3, and give it to my operating system. The first thing I needed to do was resize the data partition, hda3. Highlight the partition you want to shrink, and click Resize/Move. You will see a graph representing the partition, as well as some fields telling you how much space available. We have the option of freeing up space either before or after the partition. The partition we want to give the space to (hda2) is before, so grab the left arrow with the mouse and move it until enough space is freed.

We've moved the arrow until about 5 GB is freed in front. The new free space is represented by the gray area directly to the left of the the arrow. Rather than moving the arrow, you could also have entered the amount of space you wanted directly into the "Free Space Preceding" form.

Click the Resize/Move button.

Now let's select the partition we want to enlarge, hda2. Click Resize/Move, and there will be a window similar to what we've seen before. We want to use the freed space immediately following the partition, so grab the right arrow and pull it all the way to the right.

You will see the extended graph cover all the available space, with the new space being about twice as large as it was before.

Again, click Resize/Remove.

Now that we've told GParted what to do, we're going to tell it to go ahead and do it. Click on Apply, and you'll see a final warning:

If you're sure you've backed up all your data and that you want to do this, click Apply.

Using a graphical tool like GParted hides a lot of the complexity of partitioning. The first operation we asked it to do, freeing up space in front of the partition, actually involves resizing and moving the partition. A lot of data has to be checked and copied, so it takes some time. For this operation, resizing two partitions by 5 GB and moving one, it took a little over two hours. Operations involving moving more data and more partitions will take longer.

Now you can tell your grandchildren about how easy free software made it to repartition your hard drive. Your stories will still suck, but at least your data is safe.

1 comment:

Anonymous said...

Doesn't work for me. I have a .vdi file for my virtual machine that is 8gb in size and I want to make it 40gb so I create another vdi file and attach it. I then boot up in gparted live cd and I can see both hda and hdb.

After I copy hda to hdab I end up with /dev/hdb1 and /dev/hdb2. hdb1 is ext3 format boot partition and hdb2 is unknown. The resize button is greyed out for hdb2 so you cannot resize it no matter what you do. I can resize the /dev/hdb1 but that's the boot partition and I need to resize /dev/hdb2 but it is greyed out.

Any ideas?