Installing a New Hard Disk and Copying the Files

Formatting the new disk

Use the following command to format Linux partitions using ext2fs on the new disk:

To check the disk for bad blocks (physical defects), add the -c option just before /dev/hdb1.

If the new disk will have more than one Linux partition, format the other partitions with mkfs.ext2 /dev/hdb2, mkfs.ext2 /dev/hdb3, and so on. Add the -c option if desired.

Note: With older distributions, the command mkfs -t ext2 -c /dev/hdb1 didn’t check for bad blocks under any of Red Hat, Debian or Slackware, contrary to what the man page stated. This has now been fixed.

To format a swap partition, use this command:

Again, you can add the -c option before /dev/hdb1 to check for bad blocks.
Mounting the disk.
Create a directory where you’ll mount the new disk, for example, /new-disk, and mount it there:

If the new disk will have more than one Linux partition, mount them all under /new-disk with the same organization, they’ll have later.

Example. The new disk will have four Linux partitions, as follows:

Mount the four partitions under /new-disk as follows:

You must create the mount points for each level before you mount the partitions at that level.



If you’ve created a mount point at /new-disk/tmp, you’ll need to correct the directory’s permissions  to let all users access it:
chmod 1777 /new-disk/tmp

Copying the files from old to new hard disk.
You might want to go to single-user mode before starting to copy the disk, in order to shut down  the system daemons and preserve the state of the logs, and to prevent users from logging in:

When copying the hard disk, you want to copy all directories and files, 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 three 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. Try to 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 program itself will slow down the copying.
cp -ax / /new-disk
This is the simplest method, but will only work if your original Linux system is on a single disk partition.

The -an 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.

SuSE only. With this method only, you must also create the directory /dev/pts on the new disk. Use the command  mkdir /new-disk/dev/pts“.

Note: When using the -x option, recent versions of cp will create the directories /new-disk/new-disk and
/new-disk/proc, although the directories will be empty. If these directories are created, you should delete
/new-disk/new-disk, and keep /new-disk/proc.

(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 number 1, not the letter L!

This command should work in all circumstances.

(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, we’re copying all the directories we’re listing to /new-disk.

With this method, you simply list yourself the directories you want to copy. Here we listed all the directories
except /new-disk and 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:

You could also use tar to copy the disk, but this method was found to have a bug. There are of course many other
ways to copy the disks, but these three are the simplest, quickest, and most reliable.

After using any of these three methods, you must also create the /proc directory on the new disk, if it doesn’t
already exist:

At this point, you may verify the file structure on the new disk, if you wish:

If the new disk has more than one partition, you must unmount them from the bottom up before running fsck.ext2:
in the example mentioned above, you’d first unmount the 3rd level partitions, then the 2nd level partitions,
and then the 1st level partition.

You may also compare the two disks, to ensure that the files were copied properly:

I hope it has been a useful article.