The Linux Page

Copying a partition

Copying an entire partition to another when both are not of the exact same size (in blocks) can be tricky, especially if that partition includes special files such as /dev/hda and FIFOs.1

Yet, there are several ways to do so:

cp can be used, but I find it tricky to make sure that it is correctly used and all Unix systems do not have a cp command that supports copying special devices. For sure, don't just use cp -r / /mnt/there since /mnt/there is part of /, it will break and you won't know exactly what you have copied. Under linux, at least use the -x flag. Since I do not use this tool, I'm not too sure how you copy special files with it. (like /dev/hda3)

I have seen info about tar being usable for that purpose. That is, if you tar tool recognizes input files as special files because otherwise it will read their content and you really won't get the right result. I suggest you do a few tests first. The Linux version accepts --one-file-system to make sure you don't go accross devices.

What I use are the dump and restore commands. This is what I find the easiest and there is no question that dump and restore know how to handle special files. The command goes like this (assuming you wnat to copy partition S to partition D):

 (make sure you reformat the destination from scratch)
 # mkfs -t ext3 /dev/hdaD
 # mount /dev/hdaD /mnt/D

 (assuming hdaS is mounted on /mnt/S, it could be /)
 # cd /mnt/S; dump -0 -f- . | (cd /mnt/D; restore -r -f-)
	

That's it. If you are copying an entire partition to another you should get a warning that the lost+found directory already exists. To avoid some errors with the saved info of the dump tool (i.e. the status of the dump), you may need to use the -D option and specify a directoyry on a separate partition.

  • 1. Note that was the case especially in the 90's. As of today, things have come a long way and there are command line options one can use with the cp and tar instructions to copy pretty much anything. You will still have problems with some files such as locks, though. But you should not have to copy them!

dd tool...

Note that it is also possible to use the dd tool when the source partition is larger than the destination and all the inode used are under the number of sectors available in the destination. However, the result will be read-only since creating new files could end up creating files after the end of the destination partition.

Copy between Identical Partitions

Note that if you use identical partitions, then you should use the cat from one device to the other. This is much faster and does not require the use of some temporary folder like the dump/restore pair.

For instance, to copy A on B:

cat /dev/hda >/dev/hdb

With SCSI drives, which is kind of all you get these days, use sda and sdb instead.