Shrink Disk Image

0

So I have a disk image created by running dd and copying a full hard-drive (~ 67GB). My end goal is to get this image from 70GB to 55GB. The image looks as follows in terms of partitions:

boot partition
volume group containing the following logical volumes:
    root.fs 4GB
    localhd.fs 62GB
    swap.fs 2GB

What I ended up doing was mounting this image and actually reducing the localhd logical volume by 15GB. Up until this point there were no issues.
My question is how do I actually reduce the image itself after reducing the size of the logical volume -- I'm assuming that the shrinkage by 15GB simply resulted in 15GB of free unallocated space. I want to get rid of that 15Gb all together.

Florin Stingaciu

Posted 2013-05-31T19:02:00.890

Reputation: 101

Answers

1

After reducing the logical volume you most likely have some free extents in the middle of the physical volume. If you really want to shrink the disk image, you must have free space at the end of disk, not in the middle. You can try do this in multiple steps:

  1. Use pvdisplay to find out how physical extents (PEs) are allocated on your volume:

    pvdisplay --maps /dev/sdXY
    
  2. If free extents are in the middle, use pvmove to move some PEs from the end of PV, so that all free space will be left at the end of PV — you need the following syntax for moving PEs with explicitly specified numbers:

    pvmove --alloc anywhere /dev/sdXY:2000-2999 /dev/sdXY:1000-1999
    

    Alternatively, if the offending volume is swap, it would be faster to delete and recreate it (but make sure that either nothing depends on its UUID, or you update it there appropriately).

  3. After you freed some space at the end of PV, use pvresize to reduce the physical volume:

    pvresize --setphysicalvolumesize 53G /dev/sdXY
    

    (adjust the volume size according to your numbers).

    Unfortunately, the pvresize attempt may fail if you have two metadata areas on that PV. In this case you can try the dangerous process described in this LVM mailing list thread (backup VG, redo pvcreate with UUID & restorefile, restore VG).

  4. Find exact PV size in sectors:

    pvdisplay --units s /dev/sdXY
    
  5. Deactivate the volume group, so that the partition will not be used:

    vgchange -an VolGroupName
    
  6. Resize the partition containing the PV to the size displayed in step 4. This is the most dangerous step, because parted does not support actual resizing of LVM PV partitions, therefore you will need to delete the partition and then recreate it with exactly the same first sector as before, and the last sector calculated from the first sector and the PV size in sectors (end = start + size - 1). You can use fdisk or parted (if you use the GPT partition table, you must use either parted or gdisk (GPT fdisk), because plain fdisk does not support GPT). Be careful to avoid setting the partition size too small.

  7. Reboot if requested (if you forgot to deactivate the LVM group, or still had anything from the same disk mounted).

  8. Check if LVM VG can be activated and used without any problems.

  9. Look at the fdisk -lu /dev/sdX output to find the last used sector number; then you can trim the image to the appropriate size (again, be careful to avoid making the image size too small).

Sergey Vlasov

Posted 2013-05-31T19:02:00.890

Reputation: 2 678