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

How can I compact the VHD file with Ubuntu?

· 52CE 我爱测

I use windows server 2008r2 with role Hyper-V. The guest system is Ubuntu 12.04 LTC. It is situated on the dynamic virtual hard disk. I want to compact this VHD (The real size is 50 GB, 360 GB on the disk). But I can not do this,…

问题原文

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

I use windows server 2008r2 with role Hyper-V. The guest system is Ubuntu 12.04 LTC. It is situated on the dynamic virtual hard disk.

I want to compact this VHD (The real size is 50 GB, 360 GB on the disk). But I can not do this, because the Ubuntu file system is not NTFS. What do I need (gparted, sdelete, ...) for solving this problem?

The main problem is that the filesystem is not NTFS, but ext4.

The answer:

1) Download systemrescuecd

http://sourceforge.net/projects/systemrescuecd/files/sysresccd-x86/3.8.1/systemrescuecd-x86-3.8.1.iso/download

2) Add this .iso to the quest OS Ubuntu

3) Start this virtual machine

4) Use zerofree

zerofree /dev/sda1

5) Poweroff this virtual machine

6) Use Hyper-V manager (Edit -> defragmentation)

This vhd was 360 Gb. Now 70 GB.

社区高分解答

原帖链接 · Jochen · CC BY-SA 4.0

I can confirm: zerofree is working to compact virtual disk files

BUT: you don't need to use any rescue CDs or to mount the VHDX in another virtual machine:

sudo apt-get install zerofree

Boot into recovery mode, then run:

zerofree /dev/sda1

After that, we have to start the optimization for the virtual disk file. On the server hosting the VHDX file, create the following file ShrinkVhdx.ps1:

[CmdletBinding()]
Param(
    $VHDFiles
)

# Find the disks
foreach($VHD in $VHDFiles){
    Write-Verbose "Working on $VHD, please wait"
    Write-Verbose "Current size $([math]::truncate($(Get-VHD -Path $VHD).FileSize/ 1GB)) GB"
    
    Mount-VHD -Path $VHD -NoDriveLetter -ReadOnly
    Optimize-VHD -Path $VHD -Mode Full

    Write-Verbose "Optimize size $([math]::truncate($(Get-VHD -Path $VHD).FileSize/ 1GB)) GB"
    Dismount-VHD -Path $VHD
    
    Write-Verbose ""
}

And now run the file and enjoy the released disk space :-)

ShrinkVhdx.ps1 .\hdd1.vhdx

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

资料来源与适用范围

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

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

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