Tag Archives: Operating Systems under Virtual PC / Virtual Server / Hyper-V

Testing a USB Windows Install disk with Hyper-V

Recently I was putting together a new Windows USB Install disk (if you are not familiar with the process for doing this – here is a good article: http://windowsitpro.com/windows-8/q-how-do-i-create-bootable-usb-stick-can-install-uefi-system).  I really wanted to use virtual machines to test the disk as I was putting together (I am using unattended installation files and various scripts) however you cannot boot a Hyper-V virtual machine off of a USB key.

So what could I do?

The answer is surprisingly simple.  If you setup a virtual hard disk in exactly the same way that you do a USB install disk – you can use it to install Windows.

The process that I followed was to:

  1. Create a 32GB dynamic VHDX file using Hyper-V
  2. Mount the VHDX file in the host operating system – and then use DISKPART to create a FAT32 partition on it (that is marked as active), as discussed in the article above
  3. Copy in the Windows install files (and my custom scripts and tweaks)
  4. Create a virtual machine with this 32GB VHDX as the first hard disk, and a blank VHDX as the second hard disk
  5. Boot the virtual machine and install the operating system

This allowed me to quickly and easily test my install scripts.  Once I had everything working the way I wanted – I just had to copy my files to a physical USB key and go.

Cheers,
Ben

Building a Daily Ubuntu image for Hyper-V

After getting my Windows Image Factory up and running – the next thing I started to look at was how to create an “always up-to-date Ubuntu image”.  To solve this problem I ended up taking a very different route.  Some of the tools / services I used are as follows:

  • Ubuntu Cloud Images: Canonical provides daily builds of Ubuntu server for cloud deployments (https://cloud-images.ubuntu.com/).  My script checks for the latest one and pulls it down.
  • Cloud-init: The Ubuntu cloud images come preloaded with the Cloud-init client (http://cloudinit.readthedocs.org/en/latest/) – so I use this to customize the image.
  • Qemu-img: As I mentioned yesterday, qemu-img is a great tool that helps you to convert KVM images (like the ones provided by Canonical) into Hyper-V Images.
  • OSCDimg: As part of this script I need to create an ISO image on the fly.  I do this using OSCDimg – which is part of the deployment tools in the Windows ADK

This script will then:

  • Check if we already have the latest Ubuntu cloud image
  • If not – download the latest
  • Create Cloud-init metadata files
  • Convert the Qemu image to Hyper-V
  • Create an ISO with the Cloud-init metadata files in it
  • Create a virtual machine
  • Start the virtual machine, and connect to it

The script is also attached to the end of this blog post.

 

$tempPath = [System.IO.Path]::GetTempPath() + [System.Guid]::NewGuid().ToString()

 

# ADK Download – https://www.microsoft.com/en-us/download/confirmation.aspx?id=39982

# You only need to install the deployment tools

$oscdimgPath = “C:Program Files (x86)Windows Kits8.1Assessment and Deployment KitDeployment Toolsamd64Oscdimgoscdimg.exe”

 

# Download qemu-img from here: http://www.cloudbase.it/qemu-img-windows/

$qemuImgPath = “C:Working Spaceqemu-imgqemu-img.exe”

 

# Update this to the release of Ubuntu that you want

$ubuntuPath = “http://cloud-images.ubuntu.com/trusty/current/trusty-server-cloudimg-amd64”

 

$GuestOSName = “Hyper-V-VM”

$GuestOSID = “iid-123456”

$GuestAdminPassword = “P@ssw0rd”

 

$VMName = “Ubuntu Test”

$virtualSwitchName = “Virtual Switch”

 

$vmPath = “C:Working SpaceVM”

$imageCachePath = “C:Working Space”

$vhdx = $($vmPath)test.vhdx

$metaDataIso = $($vmPath)metadata.iso

 

# Get the timestamp of the latest build on the Ubuntu cloud-images site

$stamp = (Invoke-WebRequest $($ubuntuPath).manifest”).BaseResponse.LastModified.ToFileTimeUtc()

 

$metadata = @”

instance-id: $($GuestOSID)

local-hostname: $($GuestOSName)

“@

 

$userdata = @”

#cloud-config

password: $($GuestAdminPassword)

runcmd:

 – [ useradd, -m, -p, “”, ben ]

 [ chage, -d, 0, ben ]

“@

 

# Check Paths

if (!(test-path $vmPath)) {mkdir $vmPath}

if (!(test-path $imageCachePath)) {mkdir $imageCachePath}

 

# Helper function for no error file cleanup

Function cleanupFile ([string]$file) {if (test-path $file) {Remove-Item $file}}

 

# Delete the VM if it is around

If ((Get-VM | ? name eq $VMName).Count gt 0)

      {stop-vm $VMName TurnOff -Confirm:$false Passthru | Remove-VM -Force}

 

cleanupFile $vhdx

cleanupFile $metaDataIso

 

# Make temp location

md -Path $tempPath

md -Path $($tempPath)Bits”

 

if (!(test-path $($imageCachePath)ubuntu$($stamp).img)) {

      # If we do not have a matching image – delete the old ones and download the new one

      Remove-Item $($imageCachePath)ubuntu-*.img

      Invoke-WebRequest $($ubuntuPath)-disk1.img” UseBasicParsing OutFile $($imageCachePath)ubuntu$($stamp).img

                  }

 

# Output meta and user data to files

sc $($tempPath)Bitsmeta-data” ([byte[]][char[]] $metadata) -Encoding Byte

sc $($tempPath)Bitsuser-data” ([byte[]][char[]] $userdata) -Encoding Byte

 

# Convert cloud image to VHDX

& $qemuImgPath convert -f qcow2 $($imageCachePath)ubuntu$($stamp).img -O vhdx -o subformat=dynamic $vhdx

Resize-VHD -Path $vhdx SizeBytes 50GB

 

# Create meta data ISO image

& $oscdimgPath $($tempPath)Bits” $metaDataIso -j2 lcidata

 

# Clean up temp directory

rd -Path $tempPath Recurse -Force

 

# Create new virtual machine and start it

new-vm $VMName MemoryStartupBytes 2048mb VHDPath $vhdx -Generation 1 `

               SwitchName $virtualSwitchName -Path $vmPath | Out-Null

set-vm -Name $VMName ProcessorCount 2

Set-VMDvdDrive VMName $VMName -Path $metaDataIso

Start-VM $VMName

 

# Open up VMConnect

Invoke-Expression “vmconnect.exe localhost `”$VMName`””

Cheers,
Ben 

Enabling Secure Boot on Linux with the Windows Technical Preview

When we released Windows Server 2012 R2 / Windows 8.1 and introduced the world to Generation 2 virtual machines – we were only able to run Windows guest operating systems.  In the following months we worked with a bunch of folks in the Linux community and were able to get a number of Linux distributions running on Generation 2 virtual machines.

With the Windows technical preview released we have worked to make this even better.  For the first time you can enable Secure Boot on a virtual machine running Linux.  To do this you will need to:

  1. Create a Generation 2 virtual machine
  2. Change the Secure Boot certificate of the virtual machine using the following PowerShell command:
    Set-VMFirmware “VM Name” -SecureBootTemplate MicrosoftUEFICertificateAuthority
  3. Install a version of Linux that supports SecureBoot using this template (presently Ubuntu or SuSE – latest versions)

Once you have done this – you can verify that Secure Boot is present and functionaly in the system by running:

sudo apt-get install fwts
sudo fwts uefidump – | grep Secure

Cheers,
Ben

Copying the VHD of a Generation 2 Linux VM–and not booting afterwards…

If you have installed Linux in a Generation 2 virtual machine – everything is probably working great for you!  You can even export the virtual machine and import it somewhere else.  However, things will not go well if you just shutdown the virtual machine and copy the virtual hard drive to a new location.

If you do this – and create a new virtual machine with the copied virtual hard drive – you will find that it does not boot.

Why does this happen?

Well, when Linux is installed in a Generation 2 virtual machine it stores key boot information in the UEFI firmware of the virtual machine.  This is, in turn, stored in the virtual machine configuration file.  If you separate the virtual hard drive and virtual machine configuration file – you can no longer boot.

Luckily – you can avoid this.  Here are the steps that I had to take with an Ubuntu virtual machine to enable VHD copying:

  1. Log in to the virtual machine.
  2. Change directory to the boot EFI directory : cd /boot/efi/EFI
  3. Copy the ubuntu directory in to a new directory named boot : sudo cp –r ubuntu/ boot
  4. Change directory to the newly created boot directory : cd boot
  5. Rename the shimx64.efi file : sudo mv shimx64.efi bootx64.efi

What this does is move the boot information out of the UEFI firmware and onto the disk.  With this done you can copy and boot the virtual hard drive with ease.

Cheers,
Ben

Changing Ubuntu Screen Resolution in a Hyper-V VM

If you install Ubuntu 14.04 inside a Hyper-V virtual machine – you automatically get all the integration components and virtualized drivers.  Including the Hyper-V video driver.  However, if you try to change the screen resolution inside your virtual machine you will notice something odd:

image

There is only one choice!

Luckily, you can change the screen resolution.  Just not here.  What you need to do is:

  1. Open Terminal
  2. Type: sudo vi /etc/default/grub
  3. Find the line starting with GRUB_CMDLINE_LINUX_DEFAULT, and add video=hyperv_fb:[the resolution you want].  The resolution I want is 1280x720.  So my line ends up looking like this: GRUB_CMDLINE_LINUX_DEFAULT="quiet splash video=hyperv_fb:1280x720"
  4. Write the changes and quit vi.
  5. Run: sudo update-grub
  6. Reboot the virtual machine

Now you will get the resolution that you want!

Cheers,
Ben

Updated Documentation on Supported Guest Operating Systems

A couple of weeks ago I posted about the fact that Windows Server 2012 R2 is not supported on top of Windows Server 2008 R2

This generated a lot of feedback about the need to improve our documentation for which operating systems are supported on which versions of Hyper-V.  So the Hyper-V documentation team has been busy at work.  Specifically they have pulled the guest OS support information out of the current Hyper-V overview and split it into 2 topics:

They have also added a note about Windows Server 2012 being the last version of Windows that will be supported as a guest operating system on 2008 R2 to this topic: http://technet.microsoft.com/library/cc794868.aspx

Cheers,
Ben

Can you run Windows Server 2012 R2 on Windows Server 2008 R2?

I have been asked this question a couple of times recently.  The odd thing, to me anyway, is that most of the people who have asked me have stated:

I read this article: http://msdn.microsoft.com/en-us/library/cc794868(v=ws.10).aspx about supported guest operating systems for Windows Server 2008 R2, and did not see Windows Server 2012 R2 listed.  Is it supported?

The answer is – no.  That is why it is not listed.

Generally speaking, we make sure that the we support the release immediately after the host operating system (i.e. 2012 on 2008 R2, 2012 R2 on 2012, etc…).  But we do not go beyond that.

If you really want to run Windows Server 2012 R2 in a virtual machine – I highly recommend that you upgrade your host first.  Besides which, there are so many great new features in Hyper-V in Windows Server 2012 and 2012 R2 that you should want to take advantage of!

Cheers,
Ben

How to get the best disk performance with Linux on Hyper-V

I was recently reading our documented Best Practices for running Linux on Hyper-V and noticed this section:

Use I/O scheduler NOOP for better disk I/O performance.


The Linux kernel has four different I/O schedulers to reorder requests with different algorithms. NOOP is a first-in first-out queue that passes the schedule decision to be made by the hypervisor. It is recommended to use NOOP as the scheduler when running Linux virtual machine on Hyper-V. To change the scheduler for a specific device, in the boot loader’s configuration (/etc/grub.conf, for example), add elevator=noop to the kernel parameters, and then restart.

This is interesting – as I often get asked by people about what they can do to ensure the best performance when running Linux on Hyper-V.

For a bit of background here – Linux utilizes a number of techniques to try and get the best performance out of your storage (you can read all about this if you do a search on “Linux IO elevator”).  Unfortunately, all of this logic is completely undone inside of a virtual machine – as we are then responsible for mapping virtual storage to physical storage in a way that is hidden from the guest operating system.  Turning effect of turning on the NOOP I/O scheduler is that Linux stops trying to be clever about storage activity – and instead relies on the underlying hardware (virtual hardware in this case) to do the right thing.

In our testing this has always yielded the best results.

Cheers,
Ben

What version of Linux Supports what in Hyper-V?

We have been doing a lot of work to increase the functionality of Linux on top of Hyper-V, and we have been contributing the necessary changes directly to the main Linux source.  While this is all good – it has caused confusion for some people as they are trying to figure out which versions of Linux support which features on Hyper-V.

Thankfully – our documentation team have been busy putting together some awesome guidance here.

First there is the “Feature Descriptions for Linux virtual machines on Hyper-V” page – that explains all the features and options that are available.

Second – there are pages for a number of the major Linux variants:

Each of these pages tell you exactly what features are supported in what versions.  Very helpful!

Cheers,
Ben

Ubuntu 14.04 in a Generation 2 VM

Recently, Canonical released Ubuntu 14.04.  This is the first Linux release to support running inside of a Generation 2 virtual machine.  To get this working in your environment, you need to have Windows 8.1 or Windows Server 2012 R2 installed.  Then you need to download an installation ISO from http://www.ubuntu.com/download.

You can download the Desktop or Server versions of 14.04, just make sure that you get the 64-bit versions.

Once you have done this – create a new Generation 2 virtual machine and configure it the way you want it.  Before you start the installation you will need to go into the virtual machine settings, change to the Firmware settings page and uncheck Enable Secure Boot.

image

You can then boot from the ISO image and select to Install Ubuntu.

image

The installation process is quite simple, and there are no tricks / special options to get it running well under Hyper-V.  I even noticed that dynamic memory was active during the installation process!

Soon you will be done – and you will have an Ubuntu Generation 2 virtual machine, with all the cool features (like dynamic memory, online backup and more) already enabled and ready to go:

image

Cheers,
Ben