Hyper-V+Ubuntu18.04的VHDX文件压缩

If you configured your Hyper-V virtual machines with dynamically expanding virtual disks you will find that the VHDX files backing the virtual disks always grow in size, they never shrink. This article shows how to do it for Ubuntu Linux VMs.

  1. Faster Trimming & Compacting of Hyper-V Ubuntu VMs

Ubuntu running in a Hyper-V virtual machine has been supporting the trim command since version 14.04. The OS issues trim commands when files are deleted to tell the disk that the blocks occupied by the files can be used elsewhere. The hypervisor cannot know when blocks are ready to be reused unless the VM tells it.

# To tell the hypervisor which blocks can be reused issue the following command:
sudo fstrim -v /
sudo shutdown -h now

Compacting the Hyper-V Ubuntu VMs as the following picture:

Hyper-V+Ubuntu18.04的VHDX文件压缩_第1张图片

  1. Slower Trimming & Compacting of Hyper-V Ubuntu VMs

The key to success is to zero all deleted blocks on the disk. For performance reasons the OS does not do that but simply marks those blocks as deleted, keeping the actual data as it is.

# We can politely convince Linux to fill all deleted blocks with nulls using the following commands:
sudo apt-get install secure-delete
cd /
sudo sfill -llz .
sudo shutdown -h now

Compacting the Hyper-V Ubuntu VMs as the following picture:

Hyper-V+Ubuntu18.04的VHDX文件压缩_第2张图片

你可能感兴趣的:(linux)