Introduction

Running Ubuntu 24.04 LTS on Hyper-V can give you a robust, performant, and efficient virtualization environment if properly configured. In this blog, we’ll outline key best practices to help you optimize your Ubuntu 24.04 LTS virtual machine (VM) on Hyper-V, ensuring maximum performance, stability, and efficiency.

1. Dynamic VHDX for Linux File Systems

Dynamic VHDX files in Hyper-V expand automatically as required. However, Linux file systems interact with these files differently compared to Windows, which can lead to inefficient space utilization. To enhance performance and disk utilization:

  • Use 1 MB Block Size: By default, Hyper-V creates dynamic VHDX files with large block sizes optimized for Windows systems. For Linux, setting a smaller block size, like 1 MB, greatly improves storage efficiency and performance.

Here’s how you create a dynamic VHDX file with a 1 MB block size using PowerShell:

New-VHD -Path 'C:\HyperV\UbuntuVM.vhdx' -SizeBytes 60GB -Dynamic -BlockSizeBytes 1MB

2. Use Generation 2 VMs

Ubuntu 24.04 fully supports Generation 2 VMs, offering enhanced performance, security, and features like Secure Boot, UEFI firmware, and better hardware compatibility.

  • Create your VM using Generation 2 in Hyper-V Manager for the best experience:
New-VM -Name 'UbuntuVM' -MemoryStartupBytes 8GB -Generation 2 -NewVHDPath 'C:\HyperV\UbuntuVM.vhdx' -NewVHDSizeBytes 60GB

2.1 Secure Boot Settings

When setting up Generation 2 VMs, remember to disable Secure Boot initially if you’re facing installation or boot issues. After installation, you can re-enable it:

  • Go to Hyper-V Manager > VM Settings > Security and disable Secure Boot initially for smoother Linux installations.

2. Installing Ubuntu and Hyper-V Integration Services

Ubuntu 24.04 LTS includes built-in support for Hyper-V integration services. Follow these steps for best results:

  • Use the latest Ubuntu ISO.
  • During installation, select defaults or customized partitions as needed.
  • After installation, run:
sudo apt update
sudo apt upgrade
sudo apt install linux-tools-virtual linux-cloud-tools-virtual

These tools enhance VM compatibility and performance.

3. Optimizing Virtual Machine Configuration

To maximize performance, pay attention to these VM settings:

  • Memory Configuration:
    • Set dynamic memory allocation to manage resources efficiently:
Set-VMMemory -VMName 'UbuntuVM' -DynamicMemoryEnabled $true -MinimumBytes 4GB -StartupBytes 8GB -MaximumBytes 16GB
  • Processor Settings:
    • Allocate multiple virtual processors (but avoid over-allocation). Typically, 4-10 cores are optimal:
Set-VMProcessor -VMName 'UbuntuVM' -Count 4
  • Network Configuration:
    • Choose a virtual network adapter optimized for performance, like the Synthetic Network Adapter (default for Generation 2).

4. Performance Monitoring and Maintenance

Regular monitoring and maintenance ensure ongoing performance and stability:

  • Use built-in Linux tools like htop, vmstat, and iotop to monitor performance.
  • Regularly check disk usage and optimize VHDX files periodically with PowerShell commands:
Optimize-VHD -Path 'C:\HyperV\UbuntuVM.vhdx'
  • Schedule regular backups and snapshots to protect your VM environment.

Conclusion

By following these best practices, you’ll ensure that your Ubuntu 24.04 LTS virtual machine on Hyper-V remains optimized, secure, and highly efficient. Stay proactive, monitor regularly, and enjoy the reliability and flexibility Hyper-V and Ubuntu have to offer!

Categorized in: