Monday, February 4, 2013

Using Clonezilla to Clone From a Larger Hard Drive to a Smaller Drive

Officially, Clonezilla does not support cloning from a larger hard drive to a smaller one. While this may seem counter-intuitive there are some situations where you may want or need to do this. Such as with my job when images have been created from systems with 500GB drives and the new systems come with 250GB drives, or when going from a large traditional drive to a smaller SSD.If you are in this situation, I have found a way to get this done. We will have to do some prep work on the image (and re-create it) and then some additional prep on the destination drive before final imaging.

To prep what will be the new image we will be creating a new one (if its an already created image) and we will need to shrink the main partition first. You may need to restore your original image on the larger, original disk first and go from there. I use Parted Magic (which also has Clonezilla built into it) to boot the system and shrink the partition using GParted, making sure to give myself a good amount of flex room - we can always grow the partition on the new disk later. In my test I shrunk the main partition of a two partition Windows 7 drive down to about 100GB (160GB total drive capacity) to be able to fit it on a 120GB SSD. You will also want to note sizes of all partitions on the drive, in this case there was the 100MB system/boot partition and the larger main partition. After the partition has been shrunk and everything is smaller than the new destination drive, reboot the system and let the  operating system boot and run its file system/drive check - if you skip this test CloneZilla will fail when we get to making an image from the newly shrunk drive. Once the checks are finished, reboot the system again and boot into the Clonezilla environment. Once we start up Clonezilla, set the options desired after choosing to work with the device-image option. When asked what mode to use, we will be choosing "saveparts - Save_local_partitions_as_an_image" and then go through the rest of the option sets, making sure to check all the partitions on the disk.

Once the new image is saved and after installing the new, smaller disk we will again boot into Parted Magic (or GParted Live, or a similar tool), load up gParted and edit the partitions on the new drive. First (if it is a new drive) we will need to create a partition table (I did an msdos table for this restoration) and re-create the partition theme from the old drive. To create an aligned partition (critical for an SSD) create a new partition with 2MiB of space preceding it (in my case I created the 100MB system/boot partition for the Windows 7 dive I was moving over to a smaller drive) apply the change then resize/move the partition and change the 2MiB preceding space to 1mb. After that, create the next partition - again starting with 2MiB preceding space and then resizing/moving with 0 preceding space (always choose align to MiB). Repeat these steps for each additional partition. You may have to mark the appropriate partition with a boot flag either in this step or after running Clonezilla.

Now that the new drive is prepared, we will again boot Clonezilla and this time we will be restoring the recently created image. When we are offed the mode option, we will be choosing "restoreparts - Restore_an_image_to_local_partitions." Set our options of source/destination when prompted and away we go. Once finished reboot and you should now be all good to go. Your operating system may run through its file system checks again and you will want to expand the partition to fill any empty space.

There you have it, a tested procedure to clone a larger drive to a smaller drive using Clonezilla.

Blessings,

Jesse

34 comments:

  1. This is the first guide I've found that actually works

    ReplyDelete
  2. What is the 2 MB for...an "aligned partition"? What's that...?

    ReplyDelete
  3. The 2MB of free space that is later removed is an extra step due to the way GParted (the partitioning tool I used) handles rounding. Doing this little dance will create a partition that is aligned for optimal performance with an SSD.

    ReplyDelete
  4. With an SSD, if you write to the device it has to read usually 128KB, 256KB, or 512KB, erase the block, and then rewrite whatever you changed. If you read, it probably reads and caches the whole 512KB block. If your partition doesn't start on a 512KB boundary (which traditionally it didn't..) then reads and writes end up involving two blocks instead of one much more often which slows things down quite a bit. Why 2MB instead of, say, 512KB? That I'm not sure of.

    ReplyDelete
  5. Also you need to make sure you flag the first partition as boot, lba

    ReplyDelete
  6. Tried resizing from 2MiB to 1MiB preceeding space but it fails after i try to apply.

    ReplyDelete
  7. Is it giving you a specific error? You will want to ensure you are using the round to MiB option and not round to cylinders.

    ReplyDelete
  8. Couldnt' get this to work until I found this blog, with instructions to do a system/drive check after shrinking the size of the hard drive with gparted. Many thanks to Jesse for posting the instructions :)

    ReplyDelete
  9. Hello,

    Great tutorial. It saved my hide. I wanted to offer two main suggestions to better ease the process for folks:

    1. Don't set Destination Partition size = Source Partition size. This cause Clonezilla to fail for me. Make sure to set Destination Partition size > Source Partition size. I thought that making the sizes equal wouldn't pose a harm but it did, so I made sure to set destination size greater than source.

    2. Identifying the small amount of unallocated space on the source is important to successful alignment of the two drives. For me this was 2MB of unallocated SSD space prior to my actual partition. You can easily determine it's size by locating main source partition, right clicking, information, and noting the value of the first sector. For me, my first sector was 2048kb, this meant that I needed to create a partition on my destination drive that started at the same sector. I didn't try setting it mis-aligned but I've read that grave things will happen.

    Thank you for this great contribution, you cleared up a lot of questions with this article!

    ReplyDelete
  10. This is a really useful post. Thanks for the easy step by step tutorial!

    ReplyDelete
  11. Your directions worked really well, just use local partition to local partition option and it worked. Windows hiccuped several times, and found the boot manager was missing and it installed it automatically during start-up repair. Thanks for the great tip!

    ReplyDelete
  12. clonezilla actually has the easiest solution on the live iso/zip. I'm not at work now but I was surprised to see that fsarchiver was indeed included even though cz don't use it in the menu system. I have registered fsarchiver a few years ago on sysresccd. I shrunk +300GB partition down to 20GB effective time about 10 minutes, but with reading etc first time it took mabe 30 minutes. I used a clonezilla stable usb stick.
    * fsarhiver savefs imgfile.fsa /dev/sda1
    * fsarchiver restfs imgfile.fsa id0,dest=/dev/sda1
    easy peasy. I had three partitions, sda1 EFI vfat, sda2 ubuntu and a sda3 swap. The ubuntu sda2 needed to shrink because I, well I wanted it to...and so;
    1. From cz I first took a normal savedisk operation so I had a good backup source.
    2. Then fsarchiver saveparts sda2.fsa /dev/sda2
    3.Then I deleted the swap partition sda3 and ubuntu sda2. Recreated them with parted but with the size on sda2 I wanted.
    4. Used cz to restore parts sda1 and sda3, and in expert mode so I selected "dont create partitions".
    5. And fsarchiver restfs sda2.fsa id0,dest/dev/sda2 restored sda2 to a much smaller partition.
    Mabe I could have used fsarchiver to the other two partitions, or something else, but this really fixed quickly this challenge. gparted resize takes lilke 100 years on big disks. So from now on I can take regular cz savedisk images and dont need to use fsarvchiver, but I'll sure test it some more and will recommend it.

    ReplyDelete
  13. This link also helps if you partition numbers get thrown off when recreating them on the new drive: http://drbl.org/faq/fine-print.php?path=./2_System/102_restore_image_to_different_partition.faq#102_restore_image_to_different_partition.faq

    Normally Clonezilla only allows you to restore the image to the same partition, i.e. if it's saved from /dev/sda5, you can only restore that to /dev/sda5. If you really want to restore it to different partition, e.g. /dev/sda6, you can:
    Create the partition /dev/sda6, and make sure the size is equal or larger than the /dev/sda5 which you save the source image.
    Make a copy of image dir, here we use the image "my-image" as an example, e.g.
    cp -a /home/partimag/my-image /home/partimage/my-image-new

    Now we have to change some files in /home/partimage/my-image-new,
    Rename all the files /home/partimag/my-image-new/sda5* as /home/partimag/my-image-new/sda6*, e.g.
    mv /home/partimag/my-image-new/sda5.ext4-ptcl-img.gz.aa /home/partimag/my-image-new/sda6.ext4-ptcl-img.gz.aa
    mv /home/partimag/my-image-new/sda5.ext4-ptcl-img.gz.ab /home/partimag/my-image-new/sda6.ext4-ptcl-img.gz.ab
    ...


    Modify the content of /home/partimage/my-image-new/parts, replace "sda5" with "sda6".

    //Hint// In the above steps, if you want to save time and disk space, you can create the dir /home/partimage/my-image-new, copy the small files from /home/partimag/my-image, and use symbolic link (ln -fs) to link the sda5* files as sda6*.
    Now you can use the menu "restoreparts" to restore the image of partition "sda6" from my-image-new to /dev/sda6, i.e. to use the new created image "my-image-new" instead of original "my-image".

    ReplyDelete
    Replies
    1. yes, it really works.

      Delete
  14. Great blog!!!! Its really helps you to solve your problem go through it and free from your problem
    BOOTMGR Missing in Windows 7
    Thanks
    Aalia lyon

    ReplyDelete
  15. Any possibility of getting a video of this? I messed up somewhere and it didn't work for me.

    ReplyDelete
  16. Unfortunately a video is probably not likely at this time. I figured this out at work and don't have any extra drives to play with or any Windows install discs to demo with in a Virtual Box setup at home. Do you know where it failed or what exactly didn't work?

    ReplyDelete
  17. Assuming you shrunk the partitions of the original disk to fit in your target, Clonezilla does support writing to a smaller disk. You do not need to do the "restore parts" procedure.

    Choose Advanced mode, and under "Restore Disk" select the "-icds" option. This tells Clonezilla to not check disk sizes when restoring.

    ReplyDelete
  18. I remember trying that but not having any success. Would make the process easier, but my process above was the only way I was able to get it to work.

    ReplyDelete
  19. Great post! Been reading a lot about cloning my hard drive. Thanks for the info here!

    ReplyDelete
  20. thanks again, the empty space between partitions tip worked. I'm not entirely sure why - still need to research that

    ReplyDelete
  21. Dan I tried the advanced mode restore disk with -icds option and it did not work. I should have resized the larger drive before making an image but it was failing. To move forward I picked the option on the next page to restore the partition table proportionally and it is at least now trying to restore to a smaller SSD. Will know in an hour if it worked. Thanks!

    I had tried to restore the original image to a larger drive and then resize that. When the resize was done it set something in the smaller image to do a windows check disk on next boot. But now I can't make an image of that until it boots and clears this! Pain. How can I make an image of this second, smaller disk with Clonezilla?

    ReplyDelete
    Replies
    1. Just to be clear I used both advanced options, -lcds and proportionally update the partition table. Worked! SSD's rock!

      Delete
  22. Why don't we just leave the partitions 2mb apart? Would save on making those writes to the SSD on the move operation, right?

    Also thanks!

    ReplyDelete
  23. Creating the partitions further apart and moving them back is how we get the partitions aligned properly for optimal SSD use. It all boils down to how the partitioning software rounds it's numbers, making a quick extra step to get the optimal alignment. Moving empty partitions a few MB shouldn't be anywhere near intensive enough to worry about any added writes to the SSD.

    ReplyDelete
  24. Is it possible you can rewrite this so it's not just a giant wall of text? At least break it down into steps via an un/ordered list or paragraphs.

    ReplyDelete
  25. I am just so glad to have found this tutorial, I can reformat myself into steps! Thank you Jesse!

    ReplyDelete
  26. Does anyone know if this will work when using clonezilla to clone a bootable OS from a 120 Gb external hard drive onto a 64 Gb Pendrive. The actual space used by OS in the external hard drive is just around 30 Gb, well within the 64 Gb Pendrive limit.

    ReplyDelete
  27. I don't see why it wouldn't. I think worse case you may have to re-install your bootloader after the clone if it doesn't come over properly.

    ReplyDelete
  28. Even in 2016 ur post is still a saviour... thanks Jesse

    ReplyDelete
  29. Glad my post is still helping people out! I wouldn't label it a saviour though, that title is reserved for Jesus Christ.

    ReplyDelete
  30. This solution is simpler - http://lifehacker.com/5837543/how-to-migrate-to-a-solid-state-drive-without-reinstalling-windows

    ReplyDelete