Use Docker and libguestfs-tools to Shrink Virtual Machine Disks (VMDKs)

Existing VMDK Details

Target VMDK Requirements

Understanding the Existing Disk Descriptor File (Ubuntu-5GB.vmdk)

#Disk Descriptor File
version=1
encoding="windows-1252"
CID=86c08749
parentCID=ffffffff
createType="monolithicFlat"
# Extent description
RW 10485760 FLAT "Ubuntu-5GB-flat.vmdk" 0
# The Disk Data Base
#DDB
ddb.adapterType = "lsilogic"
ddb.geometry.cylinders = "652"
ddb.geometry.heads = "255"
ddb.geometry.sectors = "63"
ddb.longContentID = "06042b12bc30b7187008416b86c08749"
ddb.uuid = "60 00 C2 98 c0 d3 5e 02-6a d0 ad e1 c9 e6 05 97"
ddb.virtualHWVersion = "16"
ddb.adapterType = "lsilogic"
<number of cylinders> = <size in sectors> / 16065
Cylinders = 10485760/16065 = 652

Create New Disk Descriptor File (Ubuntu-3GB.vmdk) for 3GB Target Disk

3GB Target Disk -> 3GB * (1024^3) = 3,221,225,472 bytes
(3,221,225,472 bytes / 512 bytes per sector) = 6291456 sectors
<number of cylinders> = <size in sectors> / 16065 
Number of Cylinders = 6291456/16065 = 391
#Disk Descriptor File
version=1
encoding="windows-1252"
CID=86c08749
parentCID=ffffffff
createType="monolithicFlat"
# Extent description
RW 6291456 FLAT "Ubuntu-3GB-flat.vmdk" 0
# The Disk Data Base
#DDB
ddb.adapterType = "lsilogic"
ddb.geometry.cylinders = "391"
ddb.geometry.heads = "255"
ddb.geometry.sectors = "63"
ddb.longContentID = "06042b12bc30b7187008416b86c08749"
ddb.uuid = "60 00 C2 98 c0 d3 5e 02-6a d0 ad e1 c9 e6 05 97"
ddb.virtualHWVersion = "16"

Custom Libguestfs-tools Docker Image

FROM ubuntu:19.10ARG username=docker_devRUN apt-get update && apt-get install -y libguestfs-tools \
sudo linux-image-generic \
&& apt-get autoremove -yqq --purge \
&& apt-get clean \
&& rm -rf \
/var/lib/apt/lists/*
RUN useradd -ms /bin/bash -d /home/${username} ${username} \
&& usermod -aG sudo $username \
&& echo "$username ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
ENV HOME=/home/${username}
USER ${username}
sudo docker build -t guest-fs-tools .

Establish Shared Mount between Docker Container and Host

ls /mnt/hgfs/vm-disksUbuntu-5GB-flat.vmdk
Ubuntu-5GB.vmdk
Ubuntu-3GB.vmdk
sudo docker run -it --rm --privileged -v /mnt/hgfs/vm-disks:/mnt/vm-disks guest-fs-tools

Query Existing (Ubuntu-5GB-flat.vmdk) Disk Details

docker_dev@48fbd2885d25:/mnt/vm-disks$ sudo virt-filesystems --long --parts --blkdevs -a Ubuntu-5GB-flat.vmdkName       Type       MBR  Size        Parent
/dev/sda1 partition 83 5367660544 /dev/sda
/dev/sda device - 5368709120 -
docker_dev@d8785ff73766:/mnt/vm-disks$ sudo virt-list-partitions --long -t  Ubuntu-5GB-flat.vmdk/dev/sda1 ext4 5367660544
/dev/sda device 5368709120
docker_dev@48fbd2885d25:/mnt/vm-disks$ sudo virt-df -h -a Ubuntu-5GB-flat.vmdkFilesystem                      Size       Used  Available  Use%
Ubuntu-5GB-flat.vmdk:/dev/sda1 4.9G 130M 4.5G 3%
docker_dev@aaed9d58c24d:/mnt/vm-disks$ sudo virt-df -a Ubuntu-5GB-flat.vmdkFilesystem                     1K-blocks       Used  Available  Use%
Ubuntu-5GB-flat.vmdk:/dev/sda1 5094016 133112 4692908 3%

Prepare to Resize Existing Disk (Ubuntu-5GB-flat.vmdk)

Resize Filesystem for Ubuntu-5GB-flat.vmdk

docker_dev@aaed9d58c24d:/mnt/vm-disks$ sudo guestfish -a Ubuntu-5GB-flat.vmdk
><fs> run
100% [#######################################################################] --:--
><fs> list-filesystems
/dev/sda1: ext4
><fs> e2fsck-f /dev/sda1
><fs> resize2fs-size /dev/sda1 3140000K
><fs> e2fsck-f /dev/sda1
><fs> quit

Check Post-Resize File System

docker_dev@aaed9d58c24d:/mnt/vm-disks$ sudo virt-df -a Ubuntu-5GB-flat.vmdk

Filesystem 1K-blocks Used Available Use%
Ubuntu-5GB-flat.vmdk:/dev/sda1 3025072 130560 2727416 5%
3025072 * 1k-blocks  = 3025072 * 1024 = 3,097,673,728 Bytes

Create Target 3GB Disk (Ubuntu-3GB-flat.vmdk) and Migrate

docker_dev@aaed9d58c24d:/mnt/vm-disks$ truncate -s 3G Ubuntu-3GB-flat.vmdk
docker_dev@aaed9d58c24d:/mnt/vm-disks$ sudo virt-resize --shrink  /dev/sda1 Ubuntu-5GB-flat.vmdk Ubuntu-3GB-flat.vmdk
[   0.0] Examining Ubuntu-5GB-flat.vmdk
100% [####################################################] --:--
**********
Summary of changes:/dev/sda1: This partition will be resized from 5.0G to 3.0G. The
filesystem ext4 on /dev/sda1 will be expanded using the ‘resize2fs’
method.
**********
[ 12.4] Setting up initial partition table on Ubuntu-3GB-flat.vmdk
[ 15.3] Copying /dev/sda1
100% [####################################################] 00:00
100% [####################################################] --:--
[ 66.4] Expanding /dev/sda1 using the ‘resize2fs’ method
Resize operation completed with no errors. Before deleting the old disk,
carefully check that the resized disk boots and works correctly.

Update Virtual Machine Configuration

Add New 3GB Drive to VM Hardware

Verification Checks

sudo mount /dev/sda1 <mountpoint>

Final Points

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store