Jump to content
The Inquirer-Home

Upgrading your Linux system

Part Two -- Cloning a hard drive
Thursday, 24 April 2003, 10:02
IN THIS ARTICLE I'll walk through the steps needed to backup your running Linux system to a second hard drive and establish the clone as an alternate operating environment. This is one of the easiest ways to make sure that you don't trash your existing system during an upgrade to a new release of your chosen Linux distribution. I'll use Mandrake 9.0 simply because that's what I happen to be running, but this procedure should work on most any Linux system.

As with my earlier series The INQUIRER guide to installing Linux, readers who know more about Linux that I do are welcome to offer corrections and suggestions. However, please don't send me complex shell scripts involving grep, sed, awk and the like. I wouldn't want to be an unwitting party to anyone running rm -rf * on their root partition, or anything similarly disastrous, due to a transcription error in a sophisticated shell script.

We're going to use Mandrake's GUI hard drive partitioning utility HardDrake, a few simple Linux commands, and the vi editor to execute the steps needed to clone a running Linux system.

We'll assume that you have a normal Linux system on your first IDE hard drive, /dev/hda, and that you have a second IDE hard drive already installed as /dev/hdb. We won't go into hard drive installation issues because there are too many variables among all the various mainboards and hard drive models, etc., and many vendor and hardware websites and forums exist where one can find excellent help regarding hardware problems.

PREPARATIONS
(If you have a couple of boot diskettes that you're sure work properly and you already know how you want to allocate the new partitions on your second hard drive, you can skip this section.)

Before you dive in, you need to take a precaution. And you will need to gather some data about your existing Linux system and plan an optimum partition layout to clone it onto your second hard drive.

First thing, create boot diskettes! You are going to be changing your LILO configuration, so it's imperative that you establish a backup method to boot your existing system in the event of bootloader failure.

To create a boot diskette under Mandrake, you can use the Mandrake Control Center. Open your KDE or Gnome menu, click on "Configuration" then "Mandrake Control Center", enter your root password, and click on "Boot". Then click on the Boot Diskette icon and follow the instructions to create a boot diskette.

Or you can use the fdformat and mkbootdisk commands to create a boot diskette. You'll need to know what version of the Linux kernel is in your /boot directory, which you can find out using the command:

ls /boot

Note the kernel version string in the name of the vmlinuz... entry. In Mandrake 9.0 the kernel is vmlinuz-2.4.19-16mdk , so you would write down "2.4.19-16mdk" here. Then you can enter the commands:

fdformat /dev/fd0

mkbootdisk 2.4.19-16mdk

The fdformat command formats and verifies the diskette. It isn't strictly necessary to use it, but it's a good check to make sure that a particular diskette is actually a good one before proceeding.

The mkbootdisk utility assumes that its target is the first floppy drive, /dev/fd0 . Use the man mkbootdisk command to view its manual entry if you have unusual requirements.

(Most Linux commands have man command entries and --help parameters, and many have info and apropos entries as well.)

Whichever method you use to create a boot diskette, do it twice. Then shut down your system and test both of your new boot diskettes to make sure they work properly.

Next, use the df command to see the used, available, and total sizes of your existing disk partitions, in 1K blocks. It's a good idea to regard your existing partition sizes as minimum sizes for your cloned disk partitions. However, if an existing partition appears to be grossly overallocated, you can reduce its size on your second hard drive and make better use the space.

Unless your existing swap partition is cramped, you should allocate the same size swap partition on your second hard drive as you have on your first hard drive. You can see your current swap usage with the swapon -s command.

Fortunately, if you bought (or put together) your system more than a few months ago, the second hard drive you picked up will likely be much larger than your original hard drive. Or perhaps you bought a second hard drive of the same size as your existing one (but likely paid much less for it recently). In that case, you might simply allocate the same size partitions on your second hard drive as you have on your first hard drive. How you allocate space on your second hard drive will depend, in large measure, on how you use your system, but some common sense rules apply.

Your root (i.e., / ) partition doesn't need to be larger than a few hundred MB (mostly to leave room for growth in the /var directory) -- or less, if you prefer to use a separate /var partition. Your /home partition doesn't need to be larger than half a GB, unless you have multiple users or like to use that area for storing lots of large files. And more than about three GB or so for your /usr partition will just be wasted space unless you're a polymath developer or use a lot of large applications. Similarly, you shouldn't need more than twice the total swap space as you have memory in the system. But these are just guidelines for home desktop systems. If your situation is different, plan to allocate space on your second hard drive however you see fit. With Linux, you have flexibility and control.

Using the command output obtained above, write down a two column list of your existing /dev/hda and planned /dev/hdb partitions, like this:

device mount-pt disk-size device mount-pt disk-size
hda5 / 365MB hdb5 / 512MB
hda6 /home 749MB hdb6 /home 512MB
hda7 swap 512MB hdb7 swap 512MB
hda8 /usr 3.8GB hdb8 /usr 4GB
hda9 /data 2.7GB hdb9 /data 12.5GB

Be a little generous with your partition size estimates, because the Mandrake's disk partitioning utility may (and likely will) adjust your requested partition sizes downward somewhat to conform to convenient disk geometry boundaries. (The table above shows approximate partition sizes for cloning a nominal 9GB hard drive to a nominal 18GB hard drive.)

So, now that you have a good idea of how you want to partition your second hard drive, let's do it.

PARTITIONING
(If you've already partitioned your second hard drive, or if you're experienced with using Mandrake's HardDrake disk partitioning utility, you can skip this section.)

Perhaps your second hard drive is empty. In any case, the example uses logical disk partitions (partition 5 and higher), so you can have up to three other partitions at the front of your second hard drive containing other systems, and the drive numbers in the example won't change. If you have any high numbered partitions on the disk that you want to keep, you'll need to relocate those to the front of the hard drive first, but that's out of scope here. (Check out PartitionMagic or other capable disk partition management tools if you need to do this.) We'll assume that's not the case and proceed.

Before you begin defining new partitions on your second hard drive, you're going to need to define some new mount points. A mount point is simply an empty directory in your root ( / ) directory that new filesystems or partitions can be mounted to using the mount command either explicitly in a bash shell or implicitly by a utility.

So, open a bash shell in a console terminal like Eterm, Xterm, Konsole (KDE), or Gnome Terminal (Gnome), then enter the following commands:

su

(enter your root password at the prompt)

mkdir /disk1

mkdir /disk2

mkdir /disk3

mkdir /disk4

Define as many such /disk n mount points as the number of new partitions you will define on your second hard drive. You're going to need them.

To launch Mandrake's HardDrake disk partitioning utility under KDE or Gnome, click on "Configuration", "Hardware", then "HardDrake", and enter your root password. HardDrake will probe all the devices on your system, which can take a few minutes, so be patient.

HardDrake will display your device tree. The hard drive you want to partition should appear under the Disk node. If it doesn't, then the hard drive is not being detected, implying that you have a hardware problem to resolve before proceeding. But assuming it's there, just click on it. HardDrake will display what it knows about the device. And at the bottom of the right pane will be a wide button, "Run config tool". Click on that.

HardDrake will read the partition table of the hard drive (if it has one) and display a warning dialog to "Please make a backup of your data first." If you have any data on the hard drive that you have not backed up yet, this is your opportunity to think better about repartitioning the hard drive. Should you want to backup anything first, you can click on "Cancel" here, do the backup(s), and return later.

But let's assume the hard drive is empty or you've already backed up any data that you want to save, so you click "OK". HardDrake shows the message "Please click on a partition" in a white box on the left and displays the identity and size of the hard drive in the Details box on the right. At this point, you want to be absolutely certain that you've selected the specific hard drive that you want to partition. Check the drive letter twice to make sure. Here, we're partitioning /dev/hdb, so the Details box should contain "hdb".

HardDrake displays the hard drive layout in its top window and has several buttons below its display areas labeled "Clear all", "More", "Undo", "Toggle to expert mode", and "Done". The "Clear all" button does what its name suggests -- use this button if (and only if) you want to delete all of the existing partitions on the hard drive. The "More" button displays three partition table management functions: "Save partition table", "Restore partition table", and "Rescue partition table". If you have any partitions on the hard drive that you want to keep, you can use the "Save partition table" function to write a copy of the hard drive's partition table to a file on a mounted filesystem, and this gives you an option to later "Restore the partition table" from that file if you make a serious mistake in partitioning. The "Undo" button resets a previous action, if the partition table hasn't already been written to disk. The "Toggle to expert mode" button shows you more information and expands your control over partitioning. The "Done" button is self-explanatory.

Now let's use HardDrake to partition the second hard drive.

First, click on the "Toggle to expert mode" button. This adds "Type" and "Options" entries to the disk partitioning menu, and we're going to use one of those.

What you do next will depend upon whether the hard drive is empty (i.e., not partitioned yet) and, if not, whether you want to keep any existing partition(s) below the new ones you plan to define.

By "below" we mean with lower partition numbers and closer to the left end of the HardDrake disk layout display. The reason for this is that partition numbers are assigned in the order defined and it's a very good idea to keep the partition numbers in ascending sequence of disk cylinders on each hard drive, both to keep partition tables orderly and to avoid confusing the systems administrator, who would be you. Trust me, this is how you want to lay out your hard drives, unless of course you're the perverse sort of person who enjoys making things much more difficult for themselves than necessay.

If the hard drive is empty, HardDrake will display a wide band of featureless grey in its disk layout display window. If this is your situation, you can skip the next four paragraphs.

If the hard drive has disk partitions defined, HardDrake will display them as colored segments across its disk layout display window. Now you have some choices. You can either wipe the entire partition table, selectively delete some partitions you want to replace, or retain the existing partitions and add some new ones, assuming that unpartitioned free space exists at the end of the disk (at the right side of the disk layout display window).

If you wish to clear the whole drive, click the "Clear all" button and the partition table will be cleared.

If you want to delete some but not all existing partitions, click on each partition in descending sequence from right to left in the disk layout display window, and click on "Delete" for each one in turn. You will see an expanding band of grey moving from right to left across the disk layout display window as you delete the partitions you want to replace. Take care to stop before you inadvertantly delete any low numbered partition(s) that you want to keep. (If you do delete any such partition(s), you might be able to use the "Undo" button to recover... if the partition table hasn't yet been written to disk.)

If you just want to add several new partitions and have the necessary free space at the end of the hard drive (agan, the right side of the disk layout display window), then you're ready to define them.

Click on the grey band of free space in the HardDrake disk layout display window. If you don't have any logical partitions defined already, the Details box will contain "Device: hdb5" and other information about the free space area like its starting location and size. If you do have low numbered logical partitions already defined, the prospective partition number will be higher (and those logical partitions should be lower on the disk, i.e., to the left side of the disk layout display window -- see above).

1) Click on "Mount point", type "/disk1" in the dialogue box, then click on "OK".

2) Click on "Type", select "Journalized FS ext3" from the drop-down list, then click on "OK".

3) Click on "Resize", use the slider or type in the size you want (in MB) for the target partition (i.e., root ( / ) for the first partition), then click on "OK".

Repeat steps (1) through (3) above to define the rest of your new partitions on the second hard drive using the temporary mount points shown below for each target partition (the table below includes the root partition for completeness):

device target mount-pt fs-type disk-size
hdb5 / /disk1 ext3 512MB
hdb6 /home /disk2 ext3 512MB
hdb7 swap none swap 512MB
hdb8 /usr /disk3 ext3 4096MB
hdb9 /data /disk4 ext3 12800MB

Note that the swap partition doesn't require a temporary mount point and that the last partition you define can be the size of all remaining free space on the hard drive, if you want to allocate it completely.

When you have finished defining all of your new partitions on the second hard drive, go back through them in sequence, click on each partition, click on "Format", and click on "OK" to the warning message.

At some point -- probably at the first "Format" command -- HardDrake will issue a warning message that the partition table of device "hdb" is about to be written to disk. This will be your last chance to undo your changes to that point without getting into partition table recovery, so make sure you're satisfied with your new partition layout before clicking "OK" in response to this message.

That's about all there is to partitioning a second hard drive. Next we'll copy your existing system.

COPYING
Now you have a second hard drive partitioned, just sitting there waiting to become a working clone of your existing Linux system. So how do you make the transition? The process is outlined below.

If you enter the following commands in a bash shell, you'll see the directory structure of your Linux system (as well as any files that might be in your root directory):

su

(enter your root password at the prompt)

cd /

ls

The su command acquires superuser or root level privileges. The cd / command changes your present working directory to the root directory ( / ). The ls command lists the contents of the present working directory. The output of the ls command will look something like this:

/bin /disk2 /lib /sbin
/boot /disk3 /mnt /tmp
/dev /disk4 /opt /user
/data /etc /proc /usr
/disk1 /lhome /root /var

Notice those mount points ( /disk1 through /disk4) we defined to partition the second hard drive. The root directory also contains some other mount points used by your running Linux system, and these are /home, /usr, and /data throughout this article.

There are also two pseudo filesystems, /proc and /tmp, that appear in Linux root partition directory listings but are maintained in memory by the kernel and don't actually exist on disk except in their vestigial mount point forms.

Another directory, /lost+found appears in all partition directories for filesystem types ext2 and ext3 but can be ignored for our cloning purposes here because they rightfully belong to each physical partition rather than the Linux directory structure per se.

Important -- Before starting to copy your existing Linux system to the second hard drive partitions, close all your graphical applications (web browser(s), email client, word processors, spreadsheets, instant messaging, multimedia, etc.) and go into single user mode to shut down almost all system daemons, preserve system logs, and avoid copying open files and files that are deleted when processes are shut down. There are a couple of ways to do this.

One way to get into single user mode is to shut down all your running applications except a command terminal (otherwise known as a console or bash shell) under KDE or Gnome, then enter the commands:

su

/sbin/telinit s

These commands will (a) acquire superuser privileges and (b) shut down your display manager (KDE or Gnome) and drop you into single user mode in run-level 3.

Another and perhaps easier way to get into the most minimal single user mode is to shut down your system, reboot the "Failsafe" system, and enter "Maintenance" mode using your root password.

Either way, you should enter the following commands for copying your existing system to the second hard drive from single user mode.

1) Mount the new partitions on your second hard drive:

mount -t ext3 /dev/hdb5 /disk1

mount -t ext3 /dev/hdb6 /disk2

mount -t ext3 /dev/hdb8 /disk3

mount -t ext3 /dev/hdb9 /disk4

2) Define the mount points in the new root ( / ) partition, which is presently mounted as "/disk1", that you are not going to copy from your existing root partition:

mkdir /disk1/proc /disk1/tmp /disk1/usr /disk1/home /disk1/data

3) Copy your existing root partition structure and data to the new root partition mounted as "/disk1":

cp -a /bin /boot /dev /etc /lib /mnt /opt /root /sbin /user /var /disk1

The last directory /disk1 is the destination, while all those preceeding it are the sources. The -a parameter stands for "archive" and invokes "no-dereference" (links are copied as-is, not followed), "preserve" of file ownerships and permissions, and "recursive" to descend into the directory structures copying everything below the high-level directories specified.

Should you happen to have any files in your root ( / ) directory (which is unusual and generally frowned upon, but sometimes happens), you can copy them individually using the following command:

cp -dp / filename /disk1

4) Copy your other existing partitions' structures and data to their new partitions mounted as "/disk2", "/disk3", and "/disk4" respectively.

cp -a /home/* /disk2

cp -a /usr/* /disk3

cp -a /data/* /disk4

Note the "/*" appended to each source directory above. If you don't add these two crucial little characters slash ( / ) and asterisk ( * ) to the above copy commands, you'll end up with /disk2 containing /disk2/usr, and so on, and this won't do at all when you mount /dev/hdb8 as /usr, for then you'll have /usr/usr, etc., and your cloned Linux system won't be able to navigate those mis-copied directory structures successfully. This is just the way the cp command works with the -a parameter.

Now you're done copying your existing system to the second hard drive. You can issue the command /sbin/telinit 5 to restart your display manager (KDE or Gnome or perhaps something else), or just reboot your system normally. We only have a couple of things left to do in order to have a working clone of a production Linux system. However, these last items are critical.

EDITING
Once you've gotten this far, you're almost done. I say "almost" because you still can't boot or run your cloned Linux system on /dev/hdb. Why not? Well, first you have no way as yet to boot the system on /dev/hdb, and second, even if you could boot your cloned Linux kernel on /dev/hdb, as soon as it got to mounting its root partition it would start using /dev/hda again. So you'll have to edit a couple of files on your /dev/hda and /dev/hdb root ( / ) partitions to get your cloned Linux system to run:

1) Edit /etc/lilo.conf in your existing root partition (I won't try to address the "grub" analogue to this here because I don't use it):

Use whatever text editor youi're comfortable with... Kedit, Kwrite, vi or even Emacs. I use vi (minimally and not well) because it has the advantage of beng available even in the most primitive situations such as booting into "Failsafe" mode to fix some critical problem.

In a console terminal (i.e., bash shell):

su

cp /etc/lilo.conf /etc/lilo.conf.sav

vi /etc/lilo.conf

The vi editor will open /etc/lilo.conf. Press the Esc key and the "i" key to get into "INSERT" mode. Now you can edit /etc/lilo.conf to add the boot entries you need for your cloned Linux system residing on /dev/hdb.

Copy the existing entries for /dev/hda5. Change the "label=" values in your new LILO sections to indicate the cloned system on /dev/hdb. Change the "root=" values in your new LILO sections to point to /dev/hdb5 instead of /dev/hda5. Just type these in below the existing Linux entries. You shouldn't need to change anything in your new LILO entries other than "label=" and "root=".

When you're satisfied that your edits are correct, press the Esc key once or twice until you see a "]" appear at the bottom of the vi screen. Then press the ":" key and the "w" key to write the /etc/lilo.conf file to disk. vi will display a message to that effect. Then press the Esc key again (look for the "]" hint at the bottom of the screen), followed by the ":" key and the "q" key to quit vi and return to the bash shell.

Then, still in superuser mode, run LILO to write its new configuration to disk:

lilo

LILO will report its results to your console session. If it encounters an error, it will tell you approximately where the error is located. If LILO fails, reopen vi and fix the problem, then rerun LILO.

2) Edit /etc/fstab on /dev/hdb5:

In same console terminal (bash shell) where you already have superuser privileges:

mount -t ext3 /dev/hdb5 /disk1

cp /disk1/etc/fstab /disk1/etc/fstab.hda

vi /disk1/etc/fstab

These edits are even easier. Using vi as outlined above, change all instances of /dev/hda to /dev/hdb , then save the /disk1/etc/fstab file and quit vi.

FINALLY
Shutdown and reboot, selecting your cloned Linux system on /dev/hdb instead of your production system on /dev/hda. But keep in mind which is your primary system for email, etc.

My next installment of "Upgrading your Linux system" will wrestle with an upgrade to Mandrake 9.1.ยต

L'INQS
Upgrading your Linux system

Share this:

Comments

There are no comments submitted yet. Do you have an interesting opinion? Then be the first to post a comment.

Advertisement
Subscribe to the INQ Newsletter
Sign-up for the INQBot weekly newsletter
Click here to sign up Existing user
Advertisement
INQ Poll

Christmas computer sales

Will you be buying a new computer this Christmas?