- Best Practices for running Linux on Hyper-V
- Tuning Linux File Systems on Dynamic VHDX Files
- Grub Menu Timeout on Generation 2 Virtual Machines
- PxE Boot on Generation 2 Virtual Machines
- Use static MAC addresses with failover clustering
- Use Hyper-V-specific network adapters, not the legacy network adapter
- Use I/O scheduler noop/none for better disk I/O performance
- NUMA
- Reserve more memory for kdump
- Shrinking VHDX or expanding VHD and VHDX files can result in erroneous GPT partition tables
- Additional References
- How to run an Ubuntu Desktop virtual machine using VirtualBox 7
- What you’ll learn
- What you’ll need
- Download an Ubuntu Image
- Download and install VirtualBox
- 2. Create a new virtual machine
- Create a user profile
- Define the Virtual Machine’s resources
- 3. Install your image
- 4. Explore Virtual Box
- 5. Tell us your thoughts!
- How will you use this tutorial?
Best Practices for running Linux on Hyper-V
Applies to: Windows Server 2022, Azure Stack HCI, version 20H2; Windows Server 2019, Windows Server 2016, Hyper-V Server 2016, Windows Server 2012 R2, Hyper-V Server 2012 R2, Windows Server 2012, Hyper-V Server 2012, Windows Server 2008 R2, Windows 10, Windows 8.1, Windows 8, Windows 7.1, Windows 7
This topic contains a list of recommendations for running Linux virtual machine on Hyper-V.
Tuning Linux File Systems on Dynamic VHDX Files
Some Linux file systems may consume significant amounts of real disk space even when the file system is mostly empty. To reduce the amount of real disk space usage of dynamic VHDX files, consider the following recommendations:
PS > New-VHD -Path C:\MyVHDs\test.vhdx -SizeBytes 127GB -Dynamic -BlockSizeBytes 1MB
- The ext4 format is preferred to ext3 because ext4 is more space efficient than ext3 when used with dynamic VHDX files.
- When creating the filesystem specify the number of groups to be 4096, for example:
Grub Menu Timeout on Generation 2 Virtual Machines
Because of legacy hardware being removed from emulation in Generation 2 virtual machines, the grub menu countdown timer counts down too quickly for the grub menu to be displayed, immediately loading the default entry. Until grub is fixed to use the EFI-supported timer, modify /boot/grub/grub.conf, /etc/default/grub, or equivalent to have «timeout=100000» instead of the default «timeout=5».
PxE Boot on Generation 2 Virtual Machines
Because the PIT timer is not present in Generation 2 Virtual Machines, network connections to the PxE TFTP server can be prematurely terminated and prevent the bootloader from reading Grub configuration and loading a kernel from the server.
On Linux distributions other than RHEL 6.x, similar steps can be followed to configure grub v0.97 to load Linux kernels from a PxE server.
Additionally, on RHEL/CentOS 6.6 keyboard and mouse input will not work with the pre-install kernel which prevents specifying installation options in the menu. A serial console must be configured to allow choosing installation options.
- In the efidefault file on the PxE server, add the following kernel parameter «console=ttyS1»
- On the VM in Hyper-V, set up a COM port using this PowerShell cmdlet:
Set-VMComPort -VMName -Number 2 -Path \\.\pipe\dbg1
Specifying a kickstart file to the pre-install kernel would also avoid the need for keyboard and mouse input during installation.
Use static MAC addresses with failover clustering
Linux virtual machines that will be deployed using failover clustering should be configured with a static media access control (MAC) address for each virtual network adapter. In some versions of Linux, the networking configuration may be lost after failover because a new MAC address is assigned to the virtual network adapter. To avoid losing the network configuration, ensure that each virtual network adapter has a static MAC address. You can configure the MAC address by editing the settings of the virtual machine in Hyper-V Manager or Failover Cluster Manager.
Use Hyper-V-specific network adapters, not the legacy network adapter
Configure and use the virtual Ethernet adapter, which is a Hyper-V-specific network card with enhanced performance. If both legacy and Hyper-V-specific network adapters are attached to a virtual machine, the network names in the output of ifconfig -a might show random values such as _tmp12000801310. To avoid this issue, remove all legacy network adapters when using Hyper-V-specific network adapters in a Linux virtual machine.
Use I/O scheduler noop/none for better disk I/O performance
The Linux kernel offers two sets of disk I/O schedulers to reorder requests. One set is for the older ‘blk’ subsystem and one set is for the newer ‘blk-mq’ subsystem. In either case, with today’s solid state disks it is recommended to use a scheduler that passes the scheduling decisions to the underlying Hyper-V hypervisor. For Linux kernels using the ‘blk’ subsystem, this is the “noop” scheduler. For Linux kernels using the ‘blk-mq’ subsystem, this is the “none” scheduler.
For a particular disk, the available schedulers can be seen at this file system location: /sys/class/block/ /queue/scheduler, with the currently selected scheduler in square brackets. You can change the scheduler by writing to this file system location. The change must be added to an initialization script in order to persist across reboots. Consult your Linux distro documentation for details.
NUMA
Linux kernel versions earlier than 2.6.37 don’t support NUMA on Hyper-V with larger VM sizes. This issue primarily impacts older distributions using the upstream Red Hat 2.6.32 kernel, and was fixed in Red Hat Enterprise Linux (RHEL) 6.6 (kernel-2.6.32-504). Systems running custom kernels older than 2.6.37, or RHEL-based kernels older than 2.6.32-504 must set the boot parameter numa=off on the kernel command line in grub.conf. For more information, see Red Hat KB 436883.
Reserve more memory for kdump
In case the dump capture kernel ends up with a panic on boot, reserve more memory for the kernel. For example, change the parameter crashkernel=384M-:128M to crashkernel=384M-:256M in the Ubuntu grub configuration file.
Shrinking VHDX or expanding VHD and VHDX files can result in erroneous GPT partition tables
Hyper-V allows shrinking virtual disk (VHDX) files without regard for any partition, volume, or file system data structures that may exist on the disk. If the VHDX is shrunk to where the end of the VHDX comes before the end of a partition, data can be lost, that partition can become corrupted, or invalid data can be returned when the partition is read.
After resizing a VHD or VHDX, administrators should use a utility like fdisk or parted to update the partition, volume, and file system structures to reflect the change in the size of the disk. Shrinking or expanding the size of a VHD or VHDX that has a GUID Partition Table (GPT) will cause a warning when a partition management tool is used to check the partition layout, and the administrator will be warned to fix the first and secondary GPT headers. This manual step is safe to perform without data loss.
Additional References
How to run an Ubuntu Desktop virtual machine using VirtualBox 7
In this tutorial, we’ll walk you through one of the easiest ways to try out Ubuntu Desktop on a virtual machine. VirtualBox is a general purpose virtualiser that is available across Linux, Mac OS and Windows. It’s a great way to experience Ubuntu regardless of your current operating system.
VirtualBox 7 and above includes a new feature called Unattended Guest OS Install which significantly streamlines the setup experience for common operating systems like Ubuntu, making it easier than ever to get started.
Note: This tutorial will also work for other distributions, so try it out with some of the Ubuntu flavours as well!
What you’ll learn
- How to install and configure VirtualBox
- How to import an Ubuntu image
- How to run a virtual instance of Ubuntu Desktop
- Further configuration options
What you’ll need
Download an Ubuntu Image
You can download an Ubuntu image here. Make sure to save it to a memorable location on your PC! For this tutorial, we will use the latest Ubuntu 22.10 release.
Download and install VirtualBox
You can download VirtualBox from the downloads page here. This page includes instructions on how to install VirtualBox for your specific OS so we won’t repeat those here.
Once you have completed the installation, go ahead and run VirtualBox.
2. Create a new virtual machine
Click New to create a new virtual machine. Fill in the appropriate details:
- Name: If you include the word Ubuntu in your name the Type and Version will auto-update.
- Machine Folder: This is where your virtual machines will be stored so you can resume working on them whenever you like.
- ISO Image: Here you need to add a link to the ISO you downloaded from the Ubuntu website.
We want to install Ubuntu unattendedly so we can leave the checkbox to skip unchecked.
Create a user profile
To enable the automatic install we need to prepopulate our username and password here in addition to our machine name so that it can be configured automatically during first boot.
The default credentials are:
It is important to change these values since the defaults will create a user without sudo access.
Ensure your Hostname has no spaces to proceed!
It is also recommended to check the Guest Additions box to install the default Guest Additions ISO that is downloaded as part of VirtualBox. Guest additions enables a number of quality of life features such as changing resolution and dynamic screen resizing so it is highly recommended!
Note: If you choose not to use unattended install then this step will be skipped and you will go straight to the following screen. Once your machine has been created you will be able to create a username and password by proceeding through the standard Ubuntu Desktop installation flow on first boot.
Define the Virtual Machine’s resources
In the next section we can specifiy how much of our host machine’s memory and processors the virtual machine can use. For good performance it’s recommended to provide your VM with around 8GB of RAM (althought 4GB will still be usable) and 4 CPUs. Try to remain in the green areas of each slider to prevent issues with your machine running both the VM and the host OS.
Then we need to specify the size of the hard disc for the virtual machine. For Ubuntu we recommend around 25 GB as a minimum. By default the hard disk will scale dynamically as more memory is required up to the defined limit. If you want to pre-allocate the full amount, check the ‘Pre-allocate Full Size’ check box. This will improve performance but may take up unnecessary space.
Click Next to continue and view a summary of your machine setting.
After this click Finish to initialize the machine!
3. Install your image
Click Start to launch the virtual machine.
You will see a message saying ‘Powering VM up …’ and your desktop window will appear.
On first boot the unattended installation will kick in so do not interact with the prompt to ‘Try and Install Ubuntu’ and let it progress automatically to the splash screen and into the installer.
Note: If you chose not to use unattended install then you will need to progress through the Ubuntu install manually. Check out our Ubuntu Desktop installation tutorial for more details.
You will notice at this stage that the resolution of the window is fixed at 800×600. This is because the Guest Additions features are not installed until after the Ubuntu installation has completed.
Once the installation completes, the machine will automatically reboot to complete the installation.
Finally you will be greeted with the Ubuntu log-in screen where you can enter your username and password defined during the initial setup (don’t forget that the default password is ‘changeme’ if you left everything as the default).
4. Explore Virtual Box
Enjoy your shiny new Ubuntu Desktop!
As always we recommend opening a terminal and running sudo apt update && sudo apt upgrade -y and then sudo snap refresh to get everything updated to the latest versions.
Once you’ve finished your session you can close your machine by clicking the X in the top right of the window and choosing whether to keep your machine frozen in its current state or shut it down completely.
As you can probably tell, there are tonnes of further configuration options available in VirtualBox and we’ve only scratched the surface.
VirtualBox allows you to create and configure multiple virtual machines, so don’t be afraid to create new instances of Ubuntu to try out different system and storage configurations to fine tune your performance.
Why not try following the tutorial above with one of the Ubuntu flavours!
5. Tell us your thoughts!
Thank you for following this tutorial, we’d love to hear how you got on.
Give us feedback in the Ubuntu Discourse if you have any issues.
To help us improve our tutorials, we’d love to hear more about you: