Friday, May 11, 2007

Clone a partition with cp

Today I had to move a linux root partition to another disk (in the same computer) because I needed more space. Instead of relying on programs such as partimage, I simply tried to use the command cp with option -a:

cp -a /mnt/source/* /mnt/dest/

where of course, /mnt/source is the mount point of the source partition and /mnt/dest/ is the (possibly empty) target partition.

Of course, I'm copying the root partition (/mnt/source) of another linux distribution, not the one where I'm executing this command. For instance, you can execute this command using a live distribution.

I then updated my /etc/fstabs (especially the version in the target partition) to use the new /dev device, and rebooted.

Everything worked fine :-)


如月影 said...

Oops...Although I haven't tried it before,I was told that the command dd was used to do such a thing,is it?

betto said...

Yes, that's another way... but I think dd acts at a "raw" level (i.e., recreate an exact partition with the same size), while with cp you can copy a partition into an existing and formatted partition with a different size... in my case I need to create the contents of an existing partition into a bigger partition (on to another disk).

But I'm not sure of what I said about dd...

如月影 said...

Oh you are write. I search for some articles about dd and found that if just for copying files, use cp will be easier

betto said...

yes, surely cp's syntax is easier and less "dangerous" than dd's syntax ;-)

Paul Koan said...


cp -apx

-a means "archive" which does recursion and a bunch of other stuff
-p means "preserve", referring to existing permissions

-x means "only one filesystem", which stops the copy following mounted partitions etc

kris said...

thank you so much for this brief, elegant answer to a problem i've been having the entire past day and a half... i'd used a gparted livecd to copy/paste my main linux partition to a new same-size partition on an external drive so that i could shrink my main linux partition and give more space to my shared data space (fat32 at end of disk for both debian and 'doze)... that was all fine and good, however gparted can't copy/paste different-size partitions so i was stuck with a perfect backup and no way to restore it... but just now i was able to boot the gparted livecd, launch a shell, and .. mkdir /mnt/h5 ; mkdir /mnt/s3 ; mount -t xfs /dev/hda5 /mnt/h5 ; mount -t xfs /dev/sda3 /mnt/s3 ; cp -a /mnt/s3 /mnt/h5 .. rebooted and i've got my linux back, w00h00! mr. koan, the -p in -apx is already covered by -a (which provides all three options -dpR in one convenient combined option). -x might be right to really ensure not getting caught up in loops but the dereferencing part of -d in -a seems to do the job (at least in my case). thanks again betto!

betto said...

Kris, I'm really glad this post was useful :-)

and by the way, you have a nice blog, I bookmarked it!


Anonymous said...


Thanks, you're post was really helpful. I had almost zeroed in on partimage/dd to backup archlinux partition, but felt sad that I wouldn't be able to restore the backup image to a bigger partition. Your post was a savior. It once again proves GNU/Linux's flexibility and simplicity.