B-Con
A Simple Partitioning Tip
• Posted by Brad Conte on June 26, 2007
• Post Categories: Computers & Tech
There are already plenty of resources and articles available that offer advice on the fundamentals of partitioning your hard drives, and honestly it can be more of an art than a science sometimes. It depends exactly on what your needs are and how you plan to use your operating system. However, I I put forward a practical tip that might aid some in their experimentation/backup efforts, a tip that I’ve learned the value of a couple times over from experience.

Why? So you can backup, damage, and restore your operating system.

I assume my audience has minimal-ish *nix experience and has access to some form of a Unix-like system. Linux liveCDs will certainly suffice, as will (I believe) OSX. The *nix system will be used to perform the delicate initial partitioning and the partition clones thereafter.

The exact way each person might keep such a spare partition around will differ, depending on exactly how they have your file system set up. Personally, I always dual-boot Windows and Linux, keep a third partition around for experimenting with other operating systems, and have a fourth partition blank. All four of these partitions are the exact same size, down to the byte. This allows me to clone any one partition I wish to the empty partition, perform whatever perverse thing I’m experimenting with on the partition I just cloned, then restore it if something goes wrong. Or I can just boot straight to the new copy of the partition I cloned. Somewhat time consuming, but simple and error-proof.

I know this concept may strike some as inelegant and wasteful, but it’s made life extremely easy for me in many cases. It’s something that I don’t really care to have until I want it, and when I want it I really want it. There’ve been times where I wanted to experiment with new packages in Linux, mess with some set of modules, or wanted to go all willy-nilly with X11, but didn’t really want to because I didn’t care to spend an hour or so afterward trying to clean up whatever mess might be made. However, when I would know that I had a perfect clone of my operating system that I could fall back on, I was much more eager to experiment with the confidence that no permanent damage could be done to my computer.

The argument for why you would use to backup a *nix system is admittedly sparse. You can usually undo anything and simply backing up config files or source directories will solve most of your problems. However, this backup technique best aids Windows backups, since undoing something in Windows is much less clean and sure than on a *nix-based system. In face, some operations can’t be undone at all, like upgrading Direct X. Cloning Windows first will allow you to do whatever you want and restore back to its original state if something goes wrong, without having to worry about reactivation or anything.

There are two drawbacks to this scheme. The first is that you have to have the necessary disk space to keep unused. Given the cheap price of 80GB hard drives these days I find this an unlikely problem, especially if the user likes to keep their primary OS partition smaller than a full disk size. The second drawback is that there is relatively much time involved in cloning partitions for backup/restore. But realistically, I can copy 17 GB on my laptop in about 30 minutes and 19 GB on my desktop in about 20. If you initiate the copy before you go to lunch it won’t impact your work much.

To use this partitioning backup scheme, there are two simple steps.

  1. Ensure all your operating system partitions are the same size
    If your partitions aren’t the same size, “cloning” them isn’t really an option, because you can’t copy a larger partition to a smaller one. Ensure they’re the same size when you start, unless you like going back later to resize partitions (which is always fun). You have to ensure that your operating system partitions are the exact same size from the beginning.

    The problem, though, is that if you rely on a partition manager like the default Windows manager or GParted (which is the default partition manager in the Ubuntu installation process) to create your partitions for you, as odd as it may seem, you aren’t guaranteed to get partitions the exact size you specified. They’ll do minor rounding as they see fit. Thus when you go back to try and clone one partition to another you may discover that the destination partition is, say, 8MB smaller than the source partition, and there will be hell to pay.

    So to guarantee yourself of getting partitions of the exact size you want, instead, use fdisk (the *nix version).

    For those who aren’t comfortable partitioning with fdisk, the way I do it is I start by opening the disk I want to partition with fdisk
    # fdisk /dev/hda
    then use the “p” command to get a printout of my current table. I note the number of bytes per “unit” (which, by default, will be something like “cylinders of 16065″). I then take my desired partition size (in bytes) and divide it by the above number of bytes per unit to figure out how many “units” the partition needs to be in size.

    Then I create the necessary partitions, all with the same number of units. For example I may need 2038 units per partition, so I start the first partition on “1″ and end it on “2037″ (note that this is an inclusive range). I start the next partition on “2038″ and end it on 4075. And so on. Then I just write the changes to disk, reboot if necessary, and commence installing my operating system.

    An alternate, simpler, method is to just start your first partition on block “1″ and then use the size incremental notation to specify the size of the partition. For example, “+10000MB” will make the partition 10000MB.

    (For the fdisk-challenged, there’s a more user-friendly version called “cfdisk” that might be a bit more intuitive to use.)

    Note: For speed and simplicity, you will probably want to make sure that the overall size is an even division of something like 2 or 4 MB. This is explained in the next section.
  2. Clone your partition
    When the time eventually comes and you feel the need to be reckless without the fear of damaging your operating system, you can take advantage of your partitioning scheme and clone it. Or even if you just want to perform a backup of your OS you can do it.

    You can clone your operating system’s partition right out from underneath it, but since your computer is constantly performing file management I can’t officially recommend it, although I will say I’ve done it a few times and never had any problems. Your best bet is to boot to another ‘nix system (a quick-booting LiveCD, maybe) and use that to perform the copy.

    To perform the copy, use the old handy “dd” command.
    # dd if=/dev/hda2 of=/dev/hda4 bs=2M
    A key parameter here is the “bs” option, which tells dd how many bytes to read and write at a time. Obviously you want to make each read/write operation worth your computer’s time so don’t skimp on the size. Depending on your hard disk and computer capabilities, the optimal number will vary. If you fail to specify a value, the default of 512 bytes will be used, which is bloody slow.

    (I don’t know what kinds of tools exist for Windows that will clone partitions byte-for-byte. If no good freeware programs exist, you can just use a Linux LiveCD.)

    Note that if your input and output partitions are not both a size that is a perfect increment of the “bs” option, the dd command will complain and abort when it tries to read/write the last block of data and you will have to figure out how much data was left over and issue another dd command to start at an offset of where the first one left off in order to copy the rest. So making your partitions an increment size of the “bs” size you intend to use is nice, but if you don’t do so you can just calculate the two dd commands you’ll need to issue ahead of time and put them both in a small script.

    Once this is done, experiment away. Go nuts and see how many ways you can break your operating system, if you want to. It’s expendable, because you’ll be able to restore it perfectly with a dd command with just the reverse input and output commands.
This method won’t obviously won’t be of aid everyone, but hopefully some people will find it useful. Also, I realize I didn’t say anything about people who install their operating system across multiple partitions, but I’m pretty sure they’re smart enough to figure out how to adapt this for their situation. (Extended partitions for the win.)
Bloggers' Rights at EFF