Building Vagrant for Hyper-V
Essay by Scott Roberts • August 13, 2017 • Presentation or Speech • 1,395 Words (6 Pages) • 1,205 Views
Building Vagrant for Hyper-V
Sources
https://blog.engineyard.com/2014/building-a-vagrant-box
https://www.vagrantup.com/docs/boxes/base.html
https://www.vagrantup.com/docs/hyperv/
Vagrant is a great tool for making life easier when it comes to creating local development environments. Its power at making it easy to on-board new members of a team, have them up and running quickly with the same configuration and tools as the rest of the team as well as being able to set up and run a local copy of production quickly and to eliminate the ubiquitous errors of “It works on my machine” are all plus points.
Vagrant’s default VM environment is VirtualBox, but it does have providers for other environments such as VMWare, Hyper-V. The purpose of this article is to look at how to set up a Vagrant Box for Hyper-V, for those of you that choose to, want to or can only deploy to Hyper-V.
NOTE: I also run Docker on my VM Host machine and this article assumes Docker is installed.
Creating a Vagrant BOX file
BOX files are compressed files that contain meta data about an image and the Image of the Virtual Machine being built.
We are going to be creating a Hyper-V VM for Ubuntu from scratch and then package it for Vagrant and upload it to the Hashicorp Atlas site.
Creating the base image
We are going to set up our base image using the Hyper-V Manager. Once you have that up and running you will need to select New -> Virtual Machine from the actions menu and create a new machine with the following settings
Specify Name and Location
Name : hyperv-ubuntu64
Specify Generation :
Generation 2 (more on this bit in a moment)
Assign Memory
Startup memory : 4096mb
Use Dynamic Memory for the virtual machine : true
Configure Networking
Connection : External Virtual Switch
Connect Virtual Hard Disk
Create a virtual hard disk : true
Size : 20Gb
Installation Options
Install an operating system from a bootable image file – to use this you will need to download the latest Ubuntu image from https://www.ubuntu.com/download (I have used “Ubuntu Server 16.04.1 LTS”)
Once the base VM has been created you will need to disable “Secure Boot”, In Hyper-V Manager, open the settings for the hyperv-ubuntu64 VM and in Security un check “Enable Secure Boot” and “OK” the settings to save
Installing Ubuntu
We do need to create a Virtual Machine that we can use as our target for our BOX file. Setting up Ubuntu is fairly easy and you only have to follow the on-screen prompts. We do have some specific needs, so when prompted set the following
Hostname : hyperv-ubuntu64
Username : vagrant
Password : vagrant
Use weak password
https://www.vagrantup.com/docs/boxes/base.html
Installing Software - When asked what software can be installed, select Samba File Server and Open SSH Server.
Setting the root password
Vagrant has many aspects that expect the default user to have a passwordless sudo for the “vagrant” user which we need to set up.
Run the hyperv-ubuntu64 image and login using the vagrant users account. We now need to set this account up as a Super User. Enter the following
sudo passwd root
and when prompted for the new password, enter “vagrant”. You will need to sign in as the root user so that we can set up the Super User
su –
Setting up the Super User
As Vagrant must be able to run sudo commands without a password prompt, we need to add the “vagrant” user to the sudoers list, like this
sudo visudo -f /etc/sudoers.d/vagrant
Before you save this file add the following to it
vagrant ALL=(ALL) NOPASSWD:ALL
To test if the change we have just made works type
sudo pwd
Making the VM Visible
When Vagrant is executed to build the VM our image is going to be used for it needs to know the IP address of the image so that it can access it. By default Hyper-V will not report the IP address until we add the linux-cloud tools, which we do with the following
Sudo apt-get install linux-cloud-tools-$(uname -r)
$(uname -r) tells apt to get the correct version of linux-cloud-tools for our version of Ubuntu
After running this command, you should now be able to see in the “Network” tab the IP address that has been allocated of our VM.
Updating
We want our box to be as current as possible, so lets do that
sudo apt-get update -y
sudo apt-get upgrade -y
If there were any kernel updates as part of update and upgrade process, we will want to reboot our server
sudo shutdown -r now
Configuring OPENSSH Server
Vagrant uses SSH to communicate with our server and uses an insecure vagrant key to start with which we need to set up with the following
mkdir -p /home/vagrant/.ssh
chmod 0700 /home/vagrant/.ssh
wget --no-check-certificate \ https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub \ -O /home/vagrant/.ssh/authorized_keys
chmod 0600 /home/vagrant/.ssh/authorized_keys
chown -R vagrant /home/vagrant/.ssh
We need to tell the OPENSSH Server that we are using authorized_keys and where to find them, so using nano we are do the following
sudo nano /etc/ssh/sshd_config
and then find and uncomment the line, from
#AuthorizedKeysFile %h/.ssh/authorized_keys
To
AuthorizedKeysFile %h/.ssh/authorized_keys
...
...