VMware ESX: shrinking a Linux guest partition with LVM
Perform from a LiveCD.
Activate LVM
Check the current state
# lvm lvs
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
lv_root vg_v12 -wi-a----- 55.53g
lv_swap vg_v12 -wi-a----- 3.97g
# pvdisplay -m
--- Physical volume ---
PV Name /dev/sda2
VG Name vg_v12
PV Size 19.51 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 4994
Free PE 0
Allocated PE 4994
PV UUID 93cTkO-pU2H-ARpf-0Ya9-4gBs-vkDK-AUkybo
--- Physical Segments ---
Physical extent 0 to 3977:
Logical volume /dev/vg_v12/lv_root
Logical extents 0 to 3977
Physical extent 3978 to 4993:
Logical volume /dev/vg_v12/lv_swap
Logical extents 0 to 1015
--- Physical volume ---
PV Name /dev/sda3
VG Name vg_v12
PV Size 40.00 GiB / not usable 4.07 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 10238
Free PE 0
Allocated PE 10238
PV UUID njw34K-C0rg-xIqw-mMDV-d2h1-wITg-qO3kIa
--- Physical Segments ---
Physical extent 0 to 10237:
Logical volume /dev/vg_v12/lv_root
Logical extents 3978 to 14215
Check the filesystem
Output:
Shrink the filesystem. This takes a long time.
Output:
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/vg_v12/lv_root to 3670016 (4k) blocks.
The filesystem on /dev/vg_v12/lv_root is now 3670016 blocks long.
Shrink the logical volume. This completes quickly.
# lvm lvreduce -L 15G /dev/vg_v12/lv_root
WARNING: Reducing active logical volume to 15.00 GiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv_root? [y/n]: y
Reducing logical volume lv_root to 15.00 GiB
Logical volume lv_root successfully resized
Review the result
# lvm lvs
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
lv_root vg_v12 -wi-a----- 15.00g
lv_swap vg_v12 -wi-a----- 3.97g
Check the disk before expanding
# e2fsck -f /dev/vg_v12/lv_root
e2fsck 1.41.12 (17-May-2010)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/vg_v12/lv_root: 236193/913920 files (0.2% non-contiguous), 3112486/3670016 blocks
Expand the filesystem to the volume size
# resize2fs /dev/vg_v12/lv_root
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/vg_v12/lv_root to 3932160 (4k) blocks.
The filesystem on /dev/vg_v12/lv_root is now 3932160 blocks long.
Check where the allocated extents are located
# pvdisplay -m
--- Physical volume ---
PV Name /dev/sda2
VG Name vg_v12
PV Size 19.51 GiB / not usable 3.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 4994
Free PE 138
Allocated PE 4856
PV UUID 93cTkO-pU2H-ARpf-0Ya9-4gBs-vkDK-AUkybo
--- Physical Segments ---
Physical extent 0 to 3839:
Logical volume /dev/vg_v12/lv_root
Logical extents 0 to 3839
Physical extent 3840 to 3977:
FREE
Physical extent 3978 to 4993:
Logical volume /dev/vg_v12/lv_swap
Logical extents 0 to 1015
--- Physical volume ---
PV Name /dev/sda3
VG Name vg_v12
PV Size 40.00 GiB / not usable 4.07 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 10238
Free PE 10238
Allocated PE 0
PV UUID njw34K-C0rg-xIqw-mMDV-d2h1-wITg-qO3kIa
--- Physical Segments ---
Physical extent 0 to 10237:
FREE
Remove the partition from the group
Remove the physical volume
Verify that everything is disconnected.
# pvdisplay -m
--- Physical volume ---
PV Name /dev/sda2
VG Name vg_v12
PV Size 19.51 GiB / not usable 3.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 4994
Free PE 138
Allocated PE 4856
PV UUID 93cTkO-pU2H-ARpf-0Ya9-4gBs-vkDK-AUkybo
--- Physical Segments ---
Physical extent 0 to 3839:
Logical volume /dev/vg_v12/lv_root
Logical extents 0 to 3839
Physical extent 3840 to 3977:
FREE
Physical extent 3978 to 4993:
Logical volume /dev/vg_v12/lv_swap
Logical extents 0 to 1015
Reboot and boot from disk.
Review the result:
# lvm lvs
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
lv_root vg_v12 -wi-ao--- 15.00g
lv_swap vg_v12 -wi-ao--- 3.97g
[root@v12 ~]# pvdisplay -m
--- Physical volume ---
PV Name /dev/sda2
VG Name vg_v12
PV Size 19.51 GiB / not usable 3.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 4994
Free PE 138
Allocated PE 4856
PV UUID 93cTkO-pU2H-ARpf-0Ya9-4gBs-vkDK-AUkybo
--- Physical Segments ---
Physical extent 0 to 3839:
Logical volume /dev/vg_v12/lv_root
Logical extents 0 to 3839
Physical extent 3840 to 3977:
FREE
Physical extent 3978 to 4993:
Logical volume /dev/vg_v12/lv_swap
Logical extents 0 to 1015
Check the disk on VMware
/vmfs/volumes/52b935ee-ffdedee8-43b8-00199990ce08/oblik # ls -lh
total 62914560
-rw------- 1 root root 60.0G Jan 21 07:21 V12-flat.vmdk
-rw------- 1 root root 488 Jan 21 07:10 V12.vmdk
Return to the host
# fdisk -l
Disk /dev/sda: 64.4 GB, 64424509440 bytes
255 heads, 63 sectors/track, 7832 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0007d1bf
Device Boot Start End Blocks Id System
/dev/sda1 * 1 64 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 64 2611 20458496 8e Linux LVM
/dev/sda3 2611 7832 41939020 8e Linux LVM
Remove sda3
Check:
# fdisk -l
Disk /dev/sda: 64.4 GB, 64424509440 bytes
255 heads, 63 sectors/track, 7832 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0007d1bf
Device Boot Start End Blocks Id System
/dev/sda1 * 1 64 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 64 2611 20458496 8e Linux LVM
Reboot succeeded. Shut down the machine and compact the disk file in VMware. Inspect the VMDK:
Extent description
BEFORE: RW 125829120 VMFS "V12-flat.vmdk"
AFTER: RW 44040192 VMFS "V12-flat.vmdk"
Recalculate and change the value on this line using 1024*1024*1024/512 * (disk size in gigabytes). For 21 GB, the result is 44040192.
The process is not immediate.
/vmfs/volumes/52b935ee-ffdedee8-43b8-00199990ce08/oblik # vmkfstools -i V12.vmdk small.vmdk
Destination disk format: VMFS zeroedthick
Cloning disk 'V12.vmdk'...
Clone: 34% done.
Result:
/vmfs/volumes/52b935ee-ffdedee8-43b8-00199990ce08/oblik # ls -lh
total 84934656
-rw------- 1 root root 60.0G Jan 21 07:39 V12-flat.vmdk
-rw------- 1 root root 487 Jan 21 07:34 V12.vmdk
-rw------- 1 root root 21.0G Jan 21 07:39 small-flat.vmdk
-rw------- 1 root root 489 Jan 21 07:49 small.vmdk
Disconnect the old disk from the machine and attach the new one.
Success.
# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_v12-lv_root
15G 12G 2.4G 84% /
tmpfs 504M 0 504M 0% /dev/shm
/dev/sda1 485M 36M 425M 8% /boot
[root@v12 ~]# lvm lvs
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
lv_root vg_v12 -wi-ao--- 15.00g
lv_swap vg_v12 -wi-ao--- 3.97g
Total execution time: 33 minutes