Setting Up an Ubuntu VM for Developers in Hyper-V

Introduction

On occasion I will need to setup a new Ubuntu virtual machine in Hyper-V, for doing development or creating new courses for Pluralsight.

It’s not something I do every day though, so I have instructions to remind me of all the steps involved, what buttons to push, and so on.

The problem is my instructions have been scattered across multiple files in a few repositories. Today I finally decided enough was enough.

The Instructions

This is kind of an unusual blog post, in that it doesn’t contain a lot of content. The instructions were rather long, and if I’d tried to do it as a blog post I’d have had to break it into multiple posts, making it difficult to follow, or have one blog post that was 3 miles long.

Instead, I’ve created a repository on my GitHub site, https://github.com/arcanecode/Setting-Up-an-Ubuntu-VM-for-Developers-in-Hyper-V. There you will find the instructions broken down into small, easily digestible parts.

Buyer Beware

I want to make it understood, this is not meant to be an in-depth tutorial. There aren’t a lot of screen shots, nor do I take time to explain “why” (at least not often).

Instead think of it like a check list. If you are familiar with both Hyper-V and Ubuntu, you should not have any problems following the instructions.

Decisions, Decisions

Many of the configuration choices I made were the result of this being in a VM. I turn off things like screen blanking, auto locking, and the like. I figure the host machine should be the one to manage these types of things.

In addition, the software I chose to install was a result of my needs. VSCode, PowerShell, Azure Data Studio, and gcc/g++ were the core tools installed.

There are too many other options out there. Python, PHP, Ruby, Java, Rust, and on and on and on. I feel these instructions will get you to the basic platform, then you can add on your specific language and other tools from there.

Into The Future

I’ll keep this updated when new versions of Ubuntu or Hyper-V are released that may invalidate the instructions.

Also, it just screams for automation. I’d like to write some PowerShell Core to run on Windows to create and configure the VM, then a shell script to run inside Ubuntu to handle as much installation and configuration as possible.

I actually have some PowerShell Core samples for generating a Hyper-V VM, I just need to assemble them into a useable script. So that may be something I tackle in the near future.

Conslusion

Well this was a rather unusual post, as most of the info is over in my GitHub repository. Please check it out, let me know what you think.

Even if you don’t have a big need for an Ubuntu VM, I’d be curious how you find the experience of most of the info being in GitHub. Was it easy to follow, well organized?

If this works out, I may do some future work where the bulk of the information is on my GitHub site, and my blog post provides a brief overview.

Advertisement

Expanding The Size of a Hyper-V Virtual Disk

Introduction

There are tasks that we all do, but rarely. It’s helpful to have a reference to go back to.

Expanding the size of a Virtual Hard Disk, or VHDX file, is one of those. I use Hyper-V quite a bit to create virtual machines for testing, development, and the like. Every so often though I’ll underestimate the amount of space I’ll need for a machine.

As it turns out expanding the drive size isn’t terribly difficult, but there’s quite a few steps involved. This post will server as a reminder to myself, and hopefully guide others, in expanding the size of a VM drive.

I’ll break this into two parts. In the first half, we’ll see how to expand the drive within Hyper-V. This will expand the VHDX file to a new larger size.

In the second half, we’ll go into the Windows running in the VM to tell it to use the newly expanded space.

Expanding the Drive in Hyper-V

Begin by opening Hyper-V. In the Hyper-V manager, click on Edit Disk in the Actions.

This will open the Edit Virtual Hard Disk Wizard.

If this is the first time you’ve run the wizard, you’ll see a welcoming screen. If you see this, I’d suggest clicking on “Do not show this page again” and clicking Next.

Now use the Browse… button to locate the VHDX file you wish to modify. Once you’ve done that, click Next.

Now we’ll select the action, in this case we’ll pick the Expand option and click Next.

Now we’ll enter a new size for the drive. It shows the current size as 250 GB, so I’ve entered 500 so I can double the size. Obviously you’ll enter a size appropriate to your needs.

Once done, click Next.

On the final page of the wizard it shows what is about to happen. It lists the name of the VHDX file we’re working on, what the action is (Expand), and what the size will be of the new drive.

Just click Finish and the VHDX file will be updated.

Accessing The Expanded Space in Windows

In this example we’ll be using Windows 10 inside our Virtual Machine. Go ahead and start, then connect to your Windows 10 Virtual Machine.

If you go into File Explorer you’ll see something interesting.

Even though we expanded the VHDX to 500 GB, our virtual machine still thinks the C drive is 249 GB.

What we need to do is expand the already existing drive into the newly allocated space.

In the Windows 10 menu, go down to Windows Administrative Tools, then pick Computer Management.

The Computer Management window has a tree on the left. If the Storage tree is not expanded, do so and click on the Disk Management branch.

In the screen capture above, you can see the orange arrow is pointing to the existing C drive area. To the right of that a green arrow points to the newly added but still unallocated space.

Right click in the C: drive area, and in the menu that appears select Extend Volume…

The Extend Volume Wizard now appears, just click Next to proceed past the welcome screen.

By default the wizard will put the only unallocated partition in the selected area, but if you have more than one unallocated partition you can select a different one.

At the bottom, the “Total volume size will” show the total amount of space on the new drive, once the unallocated space has been added.

The next line shows the maximum space in the unallocated partition.

The final line allows you to select the total amount of space to pull from the unallocated area. By default it is set to the max space in the unallocated area, but if for some reason you want to save some of that you can lower the amount.

In this case I’ll take the default options and click Next.

You’ve now reached the final screen of the wizard, just click Finish to have it do its work.

The Computer Management window now shows the expanded C drive. You can now close the Computer Management window.

If you go back to the File Explorer and refresh it, your C drive will now show the new size.

Conclusion

In this article, we expanded the size of a Hyper-V virtual hard disk (VHDX) that hosted a Windows 10 installation. As you saw, it was pretty simple to do, but did require a few steps.

Hopefully you’ll find this useful in working with your Hyper-V machines.

Adjust the Screen Resolution of an Ubuntu Hyper-V Virtual Machine

I use Ubuntu for a lot of the courses I teach, due to its popularity. While I have some computers running it “bare metal” as they say, in order to test different scenarios, as well as record my Pluralsight courses, I also setup virtual machines within Hyper-V.

It’s a bit annoying though, as it doesn’t seem to allow the guest extensions to easily resize the VM. (I create my VMs from the downloaded ISOs as opposed to using the pre-built images in the Hyper-V store). But it can be done! All you need is a few quick edits to the grub file.

Start by opening up a terminal window. Then you can use your favorite editor to open the grub file. I’m using VIM in this example, but you could substitute nano or another text editor of your choice. (I’ll assume you know how to use your editor to edit and save changes.)

sudo vim /etc/default/grub

Now scroll down and find the line that begins with GRUB_CMDLINE_LINUX_DEFAULT. To the end of it, append the following string:

video=hyperv_fb:1280x720

Or use 1920×1080, 2560×1440, or whatever resolution you prefer. The line should look something like the following, all on one line without any wrapping.

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash video=hyperv_fb:1280x720"

Looking good, but you’re not quite done yet. You’ll need to append the same to the next line so it looks similar to the following, again all on one line with no wrapping.

GRUB_CMDLINE_LINUX="quiet splash video=hyperv_fb:1280x720"

Of course, you’ll want to make sure the resolution you select matches on both lines, 1920×1080, etc.

Now save the contents and exit your editor.

Next, and this is an important step, you have to update grub using the following command:

sudo update-grub

If you skip this step, you won’t see your resolution updated.

Finally, you’ll need to reboot. I’ve not had great success with doing a reboot of Ubuntu running in Hyper-V, it frequently hangs, so I suggest doing a power off, then start Ubuntu again in Hyper-V.

When it does reboot, you should be running at your new resolution.