When working with Hyper-V dynamic virtual disks (VHDs) containing Linux guests, you'll encounter a common issue: Windows' native compaction tools only recognize zeroed blocks on NTFS filesystems. For Ubuntu systems using ext4, we need a different approach.
Before proceeding, ensure you have:
- A Hyper-V host running Windows Server 2008 R2 or later
- Ubuntu guest VM with ext4 filesystem
- SystemRescueCD ISO (latest version)
- Sufficient free space for temporary operations
# 1. Prepare your Ubuntu VM
sudo apt-get update
sudo apt-get install zerofree -y
sudo poweroff
Mount the SystemRescueCD ISO to your VM and boot into it. Then execute:
# 2. Run zerofree on the target partition
zerofree -v /dev/sda1
The -v
flag provides verbose output so you can monitor progress. For larger disks, this may take several hours.
For servers with multiple partitions or complex setups:
# Identify all partitions
fdisk -l
# Example for multi-partition systems
zerofree /dev/sda2
zerofree /dev/sda5
After zeroing out free space:
- Shut down the VM completely
- In Hyper-V Manager, right-click the VM
- Select "Edit Disk"
- Choose "Compact" option
For frequent compaction needs, consider this bash script:
#!/bin/bash
# Ubuntu VHD compaction helper script
echo "Preparing system for compaction..."
sudo apt-get -qq update
sudo apt-get -qq install zerofree
echo "Creating zerofree script..."
cat > /tmp/zerofree_script.sh << 'EOF'
#!/bin/bash
zerofree /dev/sda1
poweroff
EOF
chmod +x /tmp/zerofree_script.sh
echo "System ready for SystemRescueCD boot"
echo "Run: /tmp/zerofree_script.sh after booting into rescue mode"
Problem: Zerofree fails with "filesystem is mounted read-write"
Solution: Ensure you're running from SystemRescueCD or another live environment
Problem: Compaction results in minimal size reduction
Solution: Check for swap partitions (use swapoff
before zerofree)
When working with Hyper-V dynamic disks containing Linux partitions (particularly ext4), Windows Server's native compaction tools fail because they expect NTFS filesystems. Here's a proven workflow to reclaim disk space from Ubuntu VHDs:
- SystemRescueCD ISO (latest version)
- Hyper-V Manager with edit permissions
- Basic Linux command-line knowledge
1. Prepare the Rescue Environment:
# Download SystemRescueCD
wget https://sourceforge.net/projects/systemrescuecd/files/sysresccd-x86/latest/systemrescuecd-x86-*.iso/download
2. Attach ISO to VM:
In Hyper-V Manager:
- Right-click Ubuntu VM → Settings
- Add SCSI DVD Drive if not present
- Mount SystemRescueCD ISO
3. Boot into Rescue Mode:
Set BIOS boot order to prioritize CD-ROM, then run:
# After booting, identify your root partition
fdisk -l
# Typical output for reference:
# /dev/sda1 * 2048 104857599 52427776 83 Linux
4. Zero Out Free Space:
# Unmount filesystem first
umount /dev/sda1
# Run zerofree (may take hours for large disks)
zerofree -v /dev/sda1
# Alternative if zerofree isn't available:
dd if=/dev/zero of=/zero.file bs=4M; sync; rm /zero.file
5. Finalize the Process:
- Power off VM completely (not save state)
- In Hyper-V Manager: Edit Disk → Compact
- Monitor progress in Windows Disk Manager
For frequent compaction needs:
#!/bin/bash
# Auto-compact script for Hyper-V Ubuntu VHDs
echo "Preparing for VHD compaction..."
sudo apt-get install zerofree -y
echo "Creating maintenance mode script..."
cat < /usr/local/bin/prep_compact
#!/bin/sh
service apache2 stop
service mysql stop
service cron stop
umount /dev/sda1
zerofree -v /dev/sda1
shutdown -h now
EOF
chmod +x /usr/local/bin/prep_compact
echo "System ready for next Hyper-V compaction cycle"
- If compaction fails, check for:
- LVM volumes (requires additional steps)
- Swap partition (disable with
swapoff -a
)
- For LUKS encrypted partitions, decrypt before zerofree