Version 1.0,
31 January 1998
How to copy a Linux system from one hard disk to another.
1. Install both disks on your system
2. Unmount non-Linux partitions
3. Partition the new disk
4. Format the new disk
5. Mount the new disk
6. Copy the files from the old disk to the new disk
7. Modify /etc/fstab as appropriate
8. Prepare LILO to boot the new disk
9. Remove the old disk
10. Reboot the system, install LILO on the new disk
Recently, I replaced my small 249 Mb hard disk with a larger disk. I wanted to transfer my entire Linux system, including LILO, from the old disk to the new disk. This is how I did it.
In the following explanation, I use "/dev/hda" to denote the "old" disk, and "/dev/hda1" means the old Linux partition. "/dev/hdb" means the "new" disk, and "/dev/hdb1" means the new Linux partition.
Therefore, I'm assuming that Linux is on the first partition of the first disk. Modify this as appropriate for your set-up.
This document is based on my own system, running Red Hat 4.2, and I've tested all the commands that follow with that distribution. I've also tested them under Debian 1.3.1 and Slackware 3.3, and I indicate a few differences to note if you're using those distributions.
If the commands don't work properly on your system, please let me know, telling me what version of Linux you're using.
Modern systems can accept four "EIDE" devices on the hard disk controller, so there shouldn't be any problem installing both disks on your system at the same time, even if you also have other EIDE devices. Hard disks and CD-ROM drives are typical EIDE devices. Floppy drives and tape drives are usually connected to the floppy drive controller rather than to the hard disk controller.
SCSI adapters are even more flexible and can accept seven devices. If you're lucky (and rich) enough to have a SCSI adapter, you probably already know this, and you probably know which of your devices are SCSI devices! For more information, see the SCSI How-To.
Even the oldest systems can accept two devices on the hard disk controller, so you can still install both hard disks at the same time. However, if you already have another device installed in addition to your hard disk (for example, if you have both a hard disk and a CD-ROM drive), you'll have to remove the other device to be able to install the old hard disk and the new hard disk at the same time.
You must configure the disks as "master" or "slave" by installing the disks' jumpers as appropriate. You'll often find configuration information on the disks themselves; if not, consult the manuals or the disks' manufacturers.
You must also inform the BIOS of the disks' presence and of their "geometry". Usually, you enter the BIOS setup programme by pressing a key during the system boot-up. Here's what to do for some common BIOSes:
American Megatrends (AMI): Del key during Power-On Self-Test (POST)
Award: Ctrl-Alt-Esc
Compaq: F10 key after the square appears in the top right corner of the screen during boot-up
Dell: Ctrl-Alt-Enter
DTK: Esc key during Power-On Self-Test
IBM PS/2: Ctrl-Alt-Del, then Ctrl-Alt-Ins when the cursor is in the top right corner
Phoenix: Ctrl-Alt-Esc, or Ctrl-Alt-S, or Ctrl-Alt-Enter
Many older systems require an Installation or Reference Disk.
(I'm interested in receiving information on other BIOSes to add them to this list.)
Reboot the system and login as root.
Some people like to mount partitions from other operating systems (DOS, Windows, OS/2, etc.) so they can use them under Linux. These partitions must be created and copied under their own operating system, and you should unmount them before copying your Linux partition. For example, if you have a DOS partition mounted at /dos, you must unmount it with this command:
umount /dos
Note that the command is "umount", with the first letter "n" missing from the word "unmount".
Use this command to partition the new disk:
fdisk /dev/hdb
For more information on partitioning, see the Installation How-To and the Partitioning Mini How-To.
If your new disk has over 1024 cylinders, see the Large Disk Mini How-To. In brief, you should install all files required to boot Linux within the first 1024 cylinders. One way to do this is to create a small partition (1 Mb or 2 Mb) just for the /boot directory at the beginning of the disk. (Slackware only: The kernel is at /vmlinuz rather than /boot/vmlinuz, so you should put both the / directory and the /boot directory in this partition.)
Partitions for systems other than Linux should be created using their own fdisk or equivalent command rather than with Linux's fdisk.
Use the following command to format the new disk:
mkfs.ext2 /dev/hdb1
To check the disk for bad blocks (physical defects), add the -c option just before "/dev/hdb1".
(Note: Contrary to what the man page states, the command "mkfs -t ext2 -c /dev/hdb1" doesn't check for bad blocks under any of Red Hat, Debian or Slackware.)
Create a directory where you'll mount the new disk, for example /new-disk, and mount it there:
mkdir /new-disk
mount -t ext2 /dev/hdb1 /new-disk
You want to completely reproduce the disk structure, including links.
However, you don't want to copy the directory /new-disk, since this would copy the new disk to itself!
Furthermore, you want to create the /proc directory on the new disk, but you don't want to copy its contents: /proc is a "virtual" file system and doesn't have any actual files, but rather contains information on the processes running on the system.
Here are four different ways to copy the old disk to the new one. This may take quite a while, especially if you have a large disk or little memory. You can expect to be able to copy 10 Mb per minute, and possibly much more.
You can follow the copy's progress by using the command "df" from another terminal. If you're as easily amused as I am, try "watch df" or "watch ls -l /new-disk" to see a report updated every two seconds; press Ctrl-C to end the display. Be aware that running the "watch" programme itself will slow down the copying.
This is the simplest method, but will only work if your original Linux system is on a single disk partition. The -a option preserves the original system as much as possible. The -x option limits cp to a single file system; this is necessary to avoid copying the /new-disk and /proc directories.
(Write this all on one line.)
This goes to the root directory and then copies all files and directories except /new-disk and /proc to /new-disk. Note that the first option after ls is the number 1, not the letter L!
This command should work in all circumstances.
(Write this all on one line.)
This goes to the root directory, "tars" everything except /new-disk and /proc, switches to /new-disk and "untars" everything there. Note that there must not be a slash before or after the names of the directories in the --exclude options.
(Note: The option -l doesn't work here, since tar will still re-create the directories /new-disk and /proc even though it doesn't copy their contents. Therefore, tar's -l option doesn't have the same behaviour as cp's -x option.)
This method is somewhat slower than the others.
(Write this all on one line.)
The last directory, /new-disk, is the destination for the cp command. All the other directories are the sources. Therefore, here I'm copying all the directories I'm listing to /new-disk.
With this method, you simply list yourself the directories you want to copy. Here I listed all my directories except /new-disk and /proc. If you can't use the other methods for any reason, you can always use this command to manually specify the directories you want to copy.
With this method only, if there are any files in the root directory itself, you need another command to copy them. In particular, this is required with Debian and Slackware, since these distributions put files in the root directory:
cp -dp /* /.* /new-disk
After using any of these four methods, you must also create the /proc directory on the new disk:
mkdir /new-disk/proc
At this point, you may verify the file structure on the new disk, if you wish:
umount /new-disk
fsck.ext2 -f /dev/hdb1
mount -t ext2 /dev/hdb1 /new-disk
You may also use the following script to compare the two disks, to ensure that the files were copied properly:
#!/bin/sh cd / for file in `/bin/ls -1A | egrep -v '^new-disk$|^proc$'` do find $file -xtype f -exec cmp \{\} /new-disk/\{\} \; done
(Slackware only: A basic Slackware installation doesn't include the "cmp" or "diff" commands, so you won't be able to run this script if you have only installed the basic files.)
This will only compare regular files, not character or block special files (in the /dev directory), sockets, etc., since the "cmp" command doesn't work properly with these. I would welcome suggestions on how to verify these "special" files.
If your new disk doesn't have the same partitions or organization as the old disk, modify the file /etc/fstab on the new disk as appropriate. Remember that this file is currently located at /new-disk/etc/fstab.
Make sure that the disk partitions in the first column correspond to the organization you'll have with the new disk, once the old disk has been removed, and that you're only mounting one partition at "/" as shown in the second column.
This is the most complicated step. I'm assuming that LILO is installed on the hard disk's Master Boot Record (MBR); this seems to be the most common configuration.
You want to install LILO on what's presently the second hard disk. It's clear that LILO can't run from the second hard disk; however, LILO's documentation does anticipate that you might want to install LILO on the second hard disk, for example if the first hard disk will be removed:
LILO can't be stored at any of the following locations:
- on the second hard disk. (Unless for backup purposes, if the current first disk will be removed or disabled, or if some other boot loader is used, that is capable of loading boot sectors from other drives.)
However, the documentation doesn't explain the proper way to install LILO on the second hard disk if the first hard disk will be removed, and I've concluded after many attempts that it isn't possible to install LILO directly onto the MBR of the second hard disk and have it work correctly the first time.
Instead, I suggest the use of a boot diskette to boot the new hard disk the first time.
Insert an empty diskette, format it, create a file system on it and mount it:
fdformat /dev/fd0H1440
mkfs.ext2 /dev/fd0
mount -t ext2 /dev/fd0 /mnt
(Debian only: The command "fdformat" is not included in a basic installation with Debian. If you don't have this command, you may omit it if the floppy is already formatted. In this case, you should check the diskette for bad blocks by adding "-c" after the "mkfs.ext2" command.
(Debian and Slackware only: Use the command "fdformat /dev/fd0h1440", with a lower case "h".)
Copy all files in /boot to the diskette:
cp -dp /boot/* /mnt
(Slackware only: Copy the file /vmlinuz to the boot diskette; use the command "cp /vmlinuz /mnt".)
Create a new file /mnt/lilo.conf as follows:
boot=/dev/fd0 # Install LILO on floppy disk. map=/mnt/map # Location of "map file". install=/mnt/boot.b # File to copy to floppy's boot sector. prompt # Have LILO show "LILO boot:" prompt. timeout=50 # Boot default system after 5 seconds. # (Value is in tenths of seconds.) image=/mnt/vmlinuz # Location of Linux kernel on floppy. label=linux # Label for Linux system. root=/dev/hda1 # Location of root partition on new hard # disk. Modify this as appropriate for # your system. # Note that you must use the name of the # future location, once the old disk has # been removed.
(Debian only: In the "image" line, use the actual name of the Linux kernel. For example, with Debian 1.3.1, use "/mnt/vmlinuz-2.0.29".)
Install LILO on the boot diskette:
/sbin/lilo -C /mnt/lilo.conf
The -C option tells /sbin/lilo what configuration file to use.
Unmount the diskette:
umount /mnt
and shut down the system.
After removing the old disk, remember to modify the disk jumpers and the BIOS information to reflect the changes.
Reboot the system from the boot diskette you just made. To do so, you may have to modify your BIOS's boot-up sequence to "A:, C:".
Make any necessary changes to the /etc/lilo.conf file, and run /sbin/lilo to install LILO on the new disk. With Debian, make sure that the "boot" line says "/dev/hda" rather than "/dev/hda1" or similar if you want to install LILO on the Master Boot Record.
You can then try re-booting your system from your new hard disk to test if everything is working properly. If you run into any problems, you can still use the diskette you just made to boot your system.
Special thanks to Dr Konrad Hinsen of the Institut de biologie structurale, Grenoble, France, who has been kindly acting as my personal Linux guru. Thanks also to Frank Damgaard, Paul Koning and Josh Rabinowitz, and to Scott Christensen for alerting me to some particularities of the Slackware distribution.