3

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
AndreasKralj
  • 321
  • 1
  • 4
  • 15
  • Shrink the partition to some value greater than the used space and less than the flash drive capacity. – Aaron Copley Jun 21 '19 at 20:04
  • When I go into gparted, it says that the used amount is the full size of the disk, even though `df -h` says that only 3.5GB are in use. How can I resize the partition so that it is only the size of the allocated space? – AndreasKralj Jun 21 '19 at 20:11
  • Uh.. Is your filesystem XFS or something that doesn't shrink? I am not a regular user of GParted, but this should be pretty straightforward. – Aaron Copley Jun 21 '19 at 21:05
  • The filesystem is ext4. I'd think it would be pretty straightforward too, it's weird that GParted shows the entire disk as being allocated. – AndreasKralj Jun 21 '19 at 21:10
  • Ext4 on LVM? I just built a Virtual Machine (Ext4 on LVM) and booted GParted. It would not reduce the size. I opened a shell from the desktop of GParted and reduced the LV size. Then, GParted allowed me to resize the partition in the UI. Edit: Maybe not. It let me drag the slider smaller, but failed to apply the change. I think you should forget GParted and just do this in the shell. – Aaron Copley Jun 21 '19 at 21:14
  • Gotcha. How do you recommend I do it in the shell? Not sure how to do that unfortunately. – AndreasKralj Jun 21 '19 at 21:23

1 Answers1

3

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

AndreasKralj
  • 321
  • 1
  • 4
  • 15
Aaron Copley
  • 12,345
  • 5
  • 46
  • 67