Ubuntu – How to clone to a smaller harddisk

cloneclonezilladdsoftware-recommendation

How to clone a harddisk to a smaller sized one.

  1. Clonezilla is great but it doesn't support such functionality.
  2. Also dd and PartImage don't support this demand.
  3. rsync will not copy the MBR since an MBR is not a file.

I need to clone a HDD not to take a backup, so rsync is not an acceptable answer.

Any suggestions?

Best Answer

You clearly cannot clone a larger partition to a smaller partition (using dd and the like) since there is simply not enough space.

However, if the files that are on the larger partition would also fit on the smaller partition, you could use rsync to copy those files. The exact options to use depend on your particular use case, but to simply copy all the files the following should do:

rsync -av /mount/point/of/large/partition/ /mount/point/of/small/partition

Edit: Once again: You cannot clone a larger partition onto a smaller partition. (But do read on, your problem can be solved yet.)

The reason is simple: your source partition is bigger than your target partition. What do you expect? Should some blocks just be dropped? Which ones? And how should dd know? Of course, you could use dd's bs= and count= options to only copy the first so-and-so-many blocks of your source partition such that it fits onto your target partition, but you will end up with a broken partition. That is certainly not what you want.

So, since you cannot clone a larger partition onto a smaller partition, the only thing you could do is to first reduce the size of your source partition to a size smaller or equal to that of your target partition with something like gparted which is aware of the filesystem specifics, such that you do not lose data. And only then could you use dd to clone the partition. Ideally, the new size of your source partition should be equal to the size of your target partition (and not just smaller or equal), or else you will end up with some unallocated space on your target partition after the cloning.

Please also note that you should not simply copy an MBR of a larger drive onto the MBR of a smaller drive (or vice versa, for that matter). The MBR, which has a size of 512 bytes and is the first section of your hard drive, contains information on the layout of the harddrive:

  • 446 bytes - Bootstrap.
  • 64 bytes - Partition table.
  • 2 bytes - Signature.

(Note that 446+64+2=512.)

If you insist on cloning the MBR, then only clone the first 446 bytes like so:

dd if=/dev/source of=/tmp/mbr.bak bs=512 count=1
dd if=/tmp/mbr.bak of=/dev/target bs=446 count=1

...replacing /dev/source and /dev/target with the device names of the source and target harddrives, e.g., /dev/sda and /dev/sdb, respectively. (More information is available here.)

However, the proper way to do it would be to do a clean Grub reinstall (or whatever you have on your MBR) on the new harddrive.

Summing up, if you want to clone a larger drive onto a smaller drive, proceed as follows:

  1. Lay out a partition table on the target drive with as many partitions as on the source drive. There should be a one-to-one correspondence between the partitions on your source drive and the partitions on your target drive, except that (some of) the partitions on the target drive can be smaller than their corresponding partitions on the source drive. Use a tool such as fdisk or cfdisk for that.

  2. For each partition on the target drive which is smaller than its corresponding partition on the source drive, reduce the size of this corresponding partition on the source drive to match the size of the partition on the target drive. Use a tool such as gparted for that.

  3. For each partition on the source drive, issue the command

    dd if=/dev/sdaX of=/dev/sdbY
    

    ... to clone the partition /dev/sdaX from the source drive to the corresponding partition /dev/sdbY on the target drive (replace the device names appropriately, of course.)

  4. If you insist on also cloning the MBR, use the two dd commands written further above in this post (those with the /tmp/mbr.bak stuff). However, keep in mind that a clean Grub re-install would be better.

Related Question