Clone Linux Drive – How to Clone Linux Hard Drive to Smaller Flash Drive

cloneclonezillalinuxpartitionusb-flash-drive

Currently, I'm trying to clone a hard drive that has Linux installed to a 16GB flash drive. The hard drive is 233GB, but df -h shows that only 3.5G is in use. Can I use Clonezilla to create an image using just the allocated amount of space and copy it to a flash drive, where I can deploy the image to other Linux laptops?

Edit: I tried using Clonzilla with Parted Magic, and tried to do device-image and the following error is thrown:

split /home/partimag/lubuntu.img/lubuntu-vg-root.ext4.ptcl-img.gz.aa: Input/output error
Checking the disk space...
Failed to save partition /dev/lubuntu-vg/root.
Press Enter to continue

Best Answer

You haven't specified much about your disk layout so I am going to make some assumptions. Please modify for your application, or provide the additional details and I can edit this.

Assumptions:

  • 1 physical disk
  • 2 partitions (boot + LVM PV)
  • 2 LV (root + swap)

This is pretty much a default install of CentOS/Red Hat and is very, very common.

You can use GParted as your boot media, but let's try this without the actual GParted tool:

Start from the bottom of the stack. Shrink your LVM Logical Volume and its filesystem.

lvreduce -rL 8G /dev/mapper/centos-root

I specified 8G here because it's smaller than your 16G available and you may still have a small swap volume.

Replace centos-root with your LV name. Run lvs/lvdisplay/ls /dev/mapper if you are unsure.)

Then, you should be able to shrink the LVM PV.

pvresize --setphysicalvolumesize 14G /dev/vda2

Replace /dev/vda2 with your disk.

I chose 14G because it's just under your 16G limit and we won't have to do any exact math this way. If your swap was allocated before your root volume, things should work. This command may fail if swap was allocated behind the root volume that you just resized. If that is the case, there's now a gap between root and swap and you need to slide swap into that free area so that the PV can shrink. I ran into this but overcame it with the next steps.

/dev/vda2: cannot resize to *n* extents as later ones are allocated.

First, find what extents need to move.

pvs -v --segments /dev/vda2

You'll see the LV with a range of extents occurring after the target boundary n above.

pvmove --alloc anywhere /dev/vda2:n-n

This command will rearrange the layout such that the volume occupies the free space you just created from shrinking root. You can then attempt to pvresize again and it should work now.

Lastly, shrink your LVM partition.

fdisk /dev/vda2

First, to print (p) your partition table. (You'll need this later.) The one you want to delete (d) is your LVM partition and hopefully it's at the end of the disk. Then, recreate it with the new smaller size. (n) Be absolutely sure that it begins in the same sector as it did before. (Scroll up to where you printed the original table.) Make it 15G, again, because math, and we know it will fit on the 16G flash drive that's not quite really 16G. Finish this operation with (w) to write your changes.

Verify everything is < 16G and retry your CloneZilla process.

shrinking disk layout