大网数据广告活动
服务器运维问答

Auto clean unused space on disks in VM

· 52CE 我爱测

I run KVM based virtualization server (namely, Proxmox) where some Debian based machines are run in KVM VM's. Proxmox can create backups of VMs, it also compress the VM disks images. As we understand, backup sizes are increasing …

问题原文

原帖链接 · Alexander · CC BY-SA 3.0

I run KVM based virtualization server (namely, Proxmox) where some Debian based machines are run in KVM VM's. Proxmox can create backups of VMs, it also compress the VM disks images.

As we understand, backup sizes are increasing over time, since more data stored on each VM disk and more 'clean' blocks of VM disk become 'dirty' (that is, contains remaining of old files). So, even if I delete all files on such a virtual disk by rm -rf it, in fact the backup will be of the same size since this won't clear all the blocks of VM disk.

I can 'clear' the VM disk by doing something like dd if=/dev/zero of=/BIG.txt and then rm -f /BIG.txt - this way I create big file full of zeros that will use all of the disk space and after I delete it its ex-blocks will contain zeroes. The downside is that for a moment the disk become full which affects every program that want to write anything.

But maybe there are some other way to clear unused disk blocks with zeroes so backup will compress such a disk with better rate? Some Windows based programs offer options to 'clear unused disk space' (e.g. CCleaner), but I need that for Linux.

已采纳解答

原帖链接 · shodanshok · CC BY-SA 3.0

Recent libvirt/kvm versions support the discard vdisc option (for SCSI vdisk type only). With this option enabled, you can issue fstrim / on the guest fileststem and unused blocks will be immediately discarded by the host vm image, compacting/reducing it via hole punching.

See here (driver section, search for 'discard') and here for more information.

If you can't use the trim/discard method, you can continue using your current zeroing method (dd from /dev/zero), with a twist: issue two dd passes, each with only little more than half the free disk space, spaced out by a fsync; rm BIG.txt command. This should be enough to recover almost all the free space, without filling it all at once.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-06-30)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。