Skip to content
Henryk Paluch edited this page Dec 17, 2023 · 1 revision

Hyper-V notes

Here are my notes common to various Hyper-V versions.

Linux guest tips

Enforce sane screen resolution (for Debian, Ubuntu,...)

  • add video=hyperv_fb:800x600 to our /etc/default/grub (you can use nay of GRUB_CMDLINE_LINUX_DEFAULT or GRUB_CMDLINE_LINUX variable) and run as root update-grub to apply these changes to /boot/grub/grub.cfg

Making VHDX "compactable"

You should thinks twice before creating VHDX file - because when you want to Compact VHDX file, the smallest "compactable" block is block of Zeroes of size "BlockSize". And that's problem - in my case default block size is 32MB! In practice I encountered situation where despite only 1.5GB usage by Ubuntu guest, it was not possible to Compact VHDX it below 5GB(!)

So you if you want to make VHDX really small you need to Create it with smaller block-size - in my example I use 1MB. However note that it MAY slow down VHDX file I/O because there will more overhead handling bigger metadata...

Here is PowerShell command to query VHDX file information:

Get-VHD "PATH_TO_VDHX_FILE"

And here is example how to create Dynamic VHDX with maximum size 12GB and BlockSize of 1MB:

$vhd="C:\Hyper-V\Virtual Disks\ubu22-small.vhdx"                                                                  
new-vhd -Path $vhd -Dynamic -SizeBytes 12GB -BlockSizeBytes 1MB `
  -LogicalSectorSizeBytes 512 -PhysicalSectorSizeBytes 4096

And here is classic procedure to Compact VHDX:

  • first trim (using fstrim -av) or zero-out disk (using zerefree -v DEVICE - must be on read-only mounted disk!). Under Windows guest you can use defrag DRIVE: /l /v or sdelete -z DRIVE:.
  • second use this standard way to remove zeroed 1MB blocks from VHDX file:
$vhd="C:\Hyper-V\Virtual Disks\ubu22-small.vhdx"                                                                  
get-vhd $vhd
Mount-VHD -Path $vhd -ReadOnly
Optimize-VHD -Path $vhd -Mode Full
DisMount-VHD -Path $vhd
get-vhd $vhd | select path,filesize,size,blocksize

In my case when small Ubuntu 22.04 installation was inside reduced to around 1.3GB, the VHDX with block-size 1MB was compacted to 1.7GB which is fine (when installed on default VHDX with block size 32MB it was not possible to compact it below 5GB).

General PowerShell tips

When using all commands including history, PowerShell will truncate output to fit it on console. To allow text wrapping you have to use Format-Table -Wrap for example:

history | Format-Table -Wrap`

See https://superuser.com/questions/1049531/how-to-fix-truncated-powershell-output-even-when-ive-specified-width-300 for extensive discussion.

Clone this wiki locally