How to Shrink vCenter Server Appliance (vCSA) Disks

After upgrading a vCenter Server Appliance (vCSA) multiple times, it might result in having very large virtual disks. The reason for the growth is, whenever you upgrade the vCenter you have to select a bigger size. In some cases, you might end up with a vCenter that has over 1TB of storage allocated, but less than 100GB in use.

When upgrading vCenter 6.7 to vCenter 7.0, the upgrader calculates the source machine size based on the old Virtual Machines Disks and allocated memory. It doesn’t matter how much is actually in use. Here is an example of a “Tiny” vCSA 6.7 that I want to upgrade. The system currently has 416 GB allocated, which means that I can’t upgrade to “Tiny”.
In this article, I’m describing how to shrink the Virtual machine to prevent it from growing during the upgrade. I recommend using this method only before an upgrade because it will change the order of VMDKs attached to the vCenter. While this shouldn’t be a problem for the vCenter itself, it might result in problems when you need VMware GSS.

Warning: The procedure described here is not supported by VMware. Currently, there is no supported way to shrink a vCenter Server.

First of all, you should make sure that you have a working backup. I’m not a fan of snapshots when you are changing the Virtual Machine itself (adding and removing virtual disks). Creating and working with a clone is a good practice here.

When you are familiar with Linux and LVM, nothing special is going to happen here. You just have to shrink the filesystem, reduce the logical volume size, and migrate the logical volume to a smaller virtual disk.

  1. Make a backup or create a clone. I recommend using a clone because when the process fails, you can simply delete the clone and start the working original.
  2. Connect to the vCSA using SSH and login as root.
  3. Stop all Services.
    # service-control --stop --all
  4. Select the filesystem you want to shrink. In this example, /storage/seat has a size of 296GB but only 67 MB in use. Note the filesystem and mount point you want to shrink.
    Filesystem: /dev/mapper/seat_vg-seat
    Mount point: /storage/seat
    From the Filesystem name, you can also identify the Volume Group (seat_vg) and Logical Volume (seat)
    # df -h
    Filesystem Size Used Avail Use% Mounted on
    devtmpfs 4.9G 0 4.9G 0% /dev
    tmpfs 4.9G 588K 4.9G 1% /dev/shm
    tmpfs 4.9G 696K 4.9G 1% /run
    tmpfs 4.9G 0 4.9G 0% /sys/fs/cgroup
    /dev/sda3 11G 4.4G 5.7G 44% /
    tmpfs 4.9G 1.6M 4.9G 1% /tmp
    /dev/sda1 120M 34M 78M 31% /boot
    /dev/mapper/core_vg-core 25G 44M 24G 1% /storage/core
    /dev/mapper/log_vg-log 9.8G 72M 9.2G 1% /storage/log
    /dev/mapper/db_vg-db 9.8G 101M 9.1G 2% /storage/db
    /dev/mapper/dblog_vg-dblog 15G 86M 14G 1% /storage/dblog
    /dev/mapper/seat_vg-seat 296G 67M 283G 1% /storage/seat <--- This is going to be shrinked
    /dev/mapper/netdump_vg-netdump 985M 1.3M 916M 1% /storage/netdump
    /dev/mapper/autodeploy_vg-autodeploy 9.8G 23M 9.2G 1% /storage/autodeploy
    /dev/mapper/imagebuilder_vg-imagebuilder 9.8G 23M 9.2G 1% /storage/imagebuilder
    /dev/mapper/updatemgr_vg-updatemgr 99G 75M 94G 1% /storage/updatemgr
    /dev/mapper/archive_vg-archive 50G 64M 47G 1% /storage/archive
  5. Unmount the filesystem.
    # umount /storage/seat/
  6. Run a filesystem check.
    # e2fsck -f /dev/mapper/seat_vg-seat
  7. I want to resize to a 20GB disk. Don’t make the filesystem too small.
    Due to overhead, the filesystem and Logical Volume needs to be slightly slower. To not waste any space, this will be resized at the end.
  8. Resize the filesystem to the intended size -2GB (=18 GB)
    resize2fs /dev/mapper/seat_vg-seat 18G
  9. Resize the Logical Volume to the intended size -1GB (=19GB)
    lvreduce -L 19G /dev/seat_vg/seat
  10. Add a New Virtual Disk to the virtual machine with 20GB. T
  11. Rescan the SCSI Bus
    # echo "- - -" > /sys/class/scsi_host/host2/scan

    Additional note: You have to rescan host adapter ID 2 (host2). If you want to figure out the host adapter that is in use for virtual disks, run “lsscsi“. Don’t use “rescan-scsi-bus.sh”.

  12. Run dmesg to identify the latest device. (sdn)
    # dmesg
    [...]
    [ 7649.086349] sd 2:0:14:0: Attached scsi generic sg17 type 0
    [ 7649.087607] sd 2:0:14:0: [sdn] Attached SCSI disk
  13. Initialize the device to be used by LVM.
    # pvcreate /dev/sdn
  14. Add the sdn device to the Volumegroup (Identified in step 4: seat_vg)
    # vgextend seat_vg /dev/sdn
  15. Now you should have to devices in the seat_vg – sdh (old large disk) and sdn (new small disk).
    # pvs |grep seat_vg
    /dev/sdh seat_vg lvm2 a-- 299.99g 279.99g
    /dev/sdn seat_vg lvm2 a-- 24.99g 24.99g
  16. Move all physical extends from sdh to sdn
    # pvmove /dev/sdh /dev/sdn
  17. When the migration is finished, sdh should be empty. Verify that there are no physical segments.
    (Check that Allocated PE = 0 and nothing except FREE is listed in physical segments)
     root@vc [ ~ ]# pvdisplay -m /dev/sdh --- Physical volume --- PV Name /dev/sdh VG Name seat_vg PV Size 300.00 GiB / not usable 7.00 MiB Allocatable yes PE Size 8.00 MiB Total PE 38399 Free PE 38399 Allocated PE 0 PV UUID V7lkDg-Fxyr-qX4x-d3oi-KhNO-XZyT-EHgibI --- Physical Segments --- Physical extent 0 to 38398: FREE
  18. Remove sdh from the Volume Group
    # vgreduce seat_vg /dev/sdh
  19. Remove LVM labels from sdh
    # pvremove /dev/sdh
  20. Run the autogrow.sh script to resize all filesystems to the virtual disk size.
    /usr/lib/applmgmt/support/scripts/autogrow.sh
  21. The last step is critical. You have to delete the old disk from the vCenter Virtual Machine. Double-check to delete the right disk! Identify the SCSI ID with lsscsi:
    # lsscsi |grep sdh
    [2:0:8:0] disk VMware Virtual disk 1.0 /dev/sdh
  22. Delete the virtual disk with the SCSI ID [2:0:8:0]. Please keep in mind that the Hard Disk number is not necessarily the SCSI ID! Delete the Hard Disk with SCSI(0:8).
  23. That’s it. Reboot the vCenter Server Appliance to make sure everything works as expected.

I can now upgrade my vCenter Server Appliance to Tiny:

When you want to shrink another filesystem, make sure to use the corresponding Logical Volume, Volume Group, and Virtual Disk. Use the following commands to identify the correct values:

List Filesystem size and mount points

# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 4.9G 0 4.9G 0% /dev
tmpfs 4.9G 588K 4.9G 1% /dev/shm
tmpfs 4.9G 696K 4.9G 1% /run
tmpfs 4.9G 0 4.9G 0% /sys/fs/cgroup
/dev/sda3 11G 4.4G 5.7G 44% /
tmpfs 4.9G 1.6M 4.9G 1% /tmp
/dev/sda1 120M 34M 78M 31% /boot
/dev/mapper/core_vg-core 25G 44M 24G 1% /storage/core
/dev/mapper/log_vg-log 9.8G 72M 9.2G 1% /storage/log
/dev/mapper/db_vg-db 9.8G 101M 9.1G 2% /storage/db
/dev/mapper/dblog_vg-dblog 15G 86M 14G 1% /storage/dblog
/dev/mapper/seat_vg-seat 296G 67M 283G 1% /storage/seat
/dev/mapper/netdump_vg-netdump 985M 1.3M 916M 1% /storage/netdump
/dev/mapper/autodeploy_vg-autodeploy 9.8G 23M 9.2G 1% /storage/autodeploy
/dev/mapper/imagebuilder_vg-imagebuilder 9.8G 23M 9.2G 1% /storage/imagebuilder
/dev/mapper/updatemgr_vg-updatemgr 99G 75M 94G 1% /storage/updatemgr
/dev/mapper/archive_vg-archive 50G 64M 47G 1% /storage/archive

List SCSI Devices with SCSI ID and their device name

# lsscsi
[0:0:0:0] cd/dvd NECVMWar VMware IDE CDR00 1.00 /dev/sr0
[2:0:0:0] disk VMware Virtual disk 1.0 /dev/sda
[2:0:1:0] disk VMware Virtual disk 1.0 /dev/sdb
[2:0:2:0] disk VMware Virtual disk 1.0 /dev/sdc
[2:0:3:0] disk VMware Virtual disk 1.0 /dev/sdd
[2:0:4:0] disk VMware Virtual disk 1.0 /dev/sde
[2:0:5:0] disk VMware Virtual disk 1.0 /dev/sdf
[2:0:6:0] disk VMware Virtual disk 1.0 /dev/sdg
[2:0:8:0] disk VMware Virtual disk 1.0 /dev/sdh
[2:0:9:0] disk VMware Virtual disk 1.0 /dev/sdi
[2:0:10:0] disk VMware Virtual disk 1.0 /dev/sdj
[2:0:11:0] disk VMware Virtual disk 1.0 /dev/sdk
[2:0:12:0] disk VMware Virtual disk 1.0 /dev/sdl
[2:0:13:0] disk VMware Virtual disk 1.0 /dev/sdm

List Logical Volumes

# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert archive archive_vg -wi-ao---- 49.99g autodeploy autodeploy_vg -wi-ao---- 9.99g core core_vg -wi-ao---- 24.99g db db_vg -wi-ao---- 9.99g dblog dblog_vg -wi-ao---- 14.99g imagebuilder imagebuilder_vg -wi-ao---- 9.99g log log_vg -wi-ao---- 9.99g netdump netdump_vg -wi-ao---- 1016.00m seat seat_vg -wi-ao---- 20.00g swap1 swap_vg -wi-ao---- 24.99g updatemgr updatemgr_vg -wi-ao---- 99.99g

List Volume Groups

# vgs VG #PV #LV #SN Attr VSize VFree archive_vg 1 1 0 wz--n- 49.99g 0 autodeploy_vg 1 1 0 wz--n- 9.99g 0 core_vg 1 1 0 wz--n- 24.99g 0 db_vg 1 1 0 wz--n- 9.99g 0 dblog_vg 1 1 0 wz--n- 14.99g 0 imagebuilder_vg 1 1 0 wz--n- 9.99g 0 log_vg 1 1 0 wz--n- 9.99g 0 netdump_vg 1 1 0 wz--n- 1016.00m 0 seat_vg 1 1 0 wz--n- 299.99g 279.99g <--- THIS swap_vg 1 1 0 wz--n- 24.99g 0 updatemgr_vg 1 1 0 wz--n- 99.99g 0

List Disks and their corresponding Volume Group

# pvs PV VG Fmt Attr PSize PFree /dev/sdc swap_vg lvm2 a-- 24.99g 0 /dev/sdd core_vg lvm2 a-- 24.99g 0 /dev/sde log_vg lvm2 a-- 9.99g 0 /dev/sdf db_vg lvm2 a-- 9.99g 0 /dev/sdg dblog_vg lvm2 a-- 14.99g 0 /dev/sdh seat_vg lvm2 a-- 299.99g 279.99g /dev/sdi netdump_vg lvm2 a-- 1016.00m 0 /dev/sdj autodeploy_vg lvm2 a-- 9.99g 0 /dev/sdk imagebuilder_vg lvm2 a-- 9.99g 0 /dev/sdl updatemgr_vg lvm2 a-- 99.99g 0 /dev/sdm archive_vg lvm2 a-- 49.99g 0

Find all disks that in a Volume Group

# pvs |grep seat_vg /dev/sdh seat_vg lvm2 a-- 299.99g 279.99g /dev/sdn seat_vg lvm2 a-- 24.99g 24.99g 

Display LVM information from the perspective of the disk

# pvdisplay -m /dev/sdh --- Physical volume --- PV Name /dev/sdh VG Name seat_vg PV Size 300.00 GiB / not usable 7.00 MiB Allocatable yes PE Size 8.00 MiB Total PE 38399 Free PE 35839 Allocated PE 2560 PV UUID V7lkDg-Fxyr-qX4x-d3oi-KhNO-XZyT-EHgibI --- Physical Segments --- Physical extent 0 to 2559: Logical volume /dev/seat_vg/seat Logical extents 0 to 2559 Physical extent 2560 to 38398: FREE

Posted by Editor