11

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.

AmShegar
  • 259
  • 1
  • 2
  • 9
  • 1
    If you want to answer your own question, please post an *answer*, not an edit to the question itself. – Massimo Jun 18 '16 at 00:31
  • consider `fstrim` in 2019, especially for ext4 under LVM https://superuser.com/questions/1491437/how-to-properly-compact-hyper-v-dynamic-sized-vhdx-files-for-linux-vms – Alec Istomin Dec 01 '19 at 01:04

6 Answers6

6

Poweroff your VM, backup the VHD, and run another virtual machine (I use SystemRescueCD since it's small and fast) that has the ext4 partition available to it, i.e., a HD for SysRescue (but don't mount it!). From the SysRescue VM, run 'zerofree' on the ext4 partition, poweroff that VM, and then use your Administration Website to compact the VHD.

rickhg12hs
  • 394
  • 2
  • 9
  • I begin do it (download SystemRescueCD, then zerofree /dev/sda1), but I do not know can I compact this VHD in Windows (after all free space will be null) – AmShegar Nov 12 '13 at 12:15
  • 1
    Try the [Hyper-V Manager](http://virtuatopia.com/index.php/Creating_and_Configuring_Hyper-V_Virtual_Hard_Disks_(VHDs)#Modifying_Existing_Hyper-V_Virtual_Hard_Disks). – rickhg12hs Nov 12 '13 at 18:55
5

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
Nathan Campos
  • 241
  • 2
  • 11
Jochen
  • 171
  • 1
  • 2
2

The process appears to be

  • Backup your current VHD file.
  • Use Snapshot manager to delete/merge any snapshots.
  • Zero fill the unused disk space

viz

sudo apt-get install secure-delete 
sudo sfill -llz .

This creates a file called oooooooo.ooo filled with 0s. You should repeat the sfill command on all partitions I guess.

Note this isn't quick either, it will take hours if not days to complete.

  • Compact
  • Profit
user9517
  • 114,104
  • 20
  • 206
  • 289
  • thanks, but I do not have the internet on the machine with hyper-v and vhd with linux. So I can not do "sudo apt-get install secure-delete" – AmShegar Nov 12 '13 at 12:17
1

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)

AmShegar
  • 259
  • 1
  • 2
  • 9
0

I did try to start Systemrescuecd in a 2nd gen machine. But it didn't work, so I created a new 1st gen machine and added the VHDX to it. systemrescuecd started. I didn't do any operations. Removed the 1st gen machine and put the VHDX back in the original machine. Then Ubuntu didn't start up at all. Only Grub-menu.

See my post here: Hyper-V Ubuntu vhdx scsi ide

So be aware of this, and do take backup (I didn't this time)...

Sam
  • 350
  • 1
  • 4
  • 16
0

You can do this by checking some parameters in the following files: /sys/block//queue/discard_granularity /sys/block//queue/discard_max_bytes if either have a 0 it's probably not supported by your hardware + software configuration.

Here's a full tutorial on doing this at: http://www.1337admin.org/uncategorized/compacting-virtual-disks-on-generation-2-linux-hyper-v-2012-r2-vms/

Brad
  • 250
  • 1
  • 11