Create Windows Vagrant Box

What Is Wrong With Virtualbox and VMWare?

I am a strong believer of virtualizing everything. So much that all of my development is done on virtual machines. The power of computers these days makes this experience extremely performant and the tooling around it has matured to a level that simplifies the process greatly.

  • Using Vagrant for creating Consul cluster on Linux probably was fun. But what about Windows hosts? Believe it or not, but more than half of developers are actually using Windows, so for most of the folks seeing how Vagrant creates Linux VMs is pretty useless. However, you can create and provision Windows VMs with Vagrant with little to no problem.
  • # goto default directory $ cd VirtualBox VMs/ # create base box from VM $ vagrant package -base Win10x64 -output Win10x64.box # add box $ vagrant box add lupin/windows10 Win10x64.box # check vagrant boxes $ vagrant box list Create and run test project.

I've been using Packer for a bit over a year now to create the Windows 2012 R2 Vagrant box that I regularly use for testing various server configuration scripts. My packer template has been evolving over time but is composed of some Boxstarter package setup and a few adhoc Powershell scripts. Create a provider for the box, matching the provider you need locally in Vagrant. Virtualbox is the most common provider. Upload the.box file for each provider, or use a url to the.box file that is publicly accessible You can find all of your boxes in the Vagrant section of Vagrant Cloud.

Up until recently, I have been using a combination of Packer, Vagrant, and Virtualbox to get the job done. Of which has produced an experience in which I have next to no complaints about.

So why am I switching to Hyper-V? Well, plain and simple, I am Windows developer. Meaning, I do all my development work on a Windows machine. Of which, without Hyper-V installed, I would not be able to do the following.

  • Work with Docker on Windows.
  • Use the built in emulators supplied by Visual Studio.

Seeing that running Hyper-V side-by-side with Virtualbox or VMWare is impossible to do with any sense of sanity, I said goodbye to VirtualBox and hello to Hyper-V. Following I will outline the process I used to create a Hyper-V Windows 10 Vagrant Box.

For more info on how I create my Windows development environment, be sure to visit Creating a Windows Development Environment With Packer, Vagrant & Chocolatey.

Installing Hyper-V

To start, we need to install Hyper-V.

  1. Ensure that hardware virtualization is supported on your computer and turned on in the BIOS settings.
    • To accomplish this, boot to your bios and enable the 'Virtualization Technology' switch.
  2. Head on over to the 'Turn Windows features on and off' settings and enable Hyper-V.

At this point, you will be informed that a reboot will take place. Once rebooted, Hyper-V will be installed and ready to do.

Building Your Windows 10 VM

On to the fun stuff, building our actual Hyper-V Box of Windows 10. Normally I would use Packer to automate this entire process. This time I decided to not follow suit and instead build it manually. For one, I wanted to use this as a learning experience to understand what actually makes up a Box. In addition, official Hyper-V support with Packer is very new. Meaning, documentation and relevant examples are sparse.

That being said, I trust that once you actually go through the process of building a Box manually once, you will quickly realize much like myself that it is as simple as can be.

Configuring A Network In Hyper-V

The first thing we need to do is create a virtual switch in which we can use to connect our VM to the internet.

  1. Open up your Hyper-V manager and select the 'Virtual Switch Manager' option located in the 'Actions Pane'.
  2. Once open, create a new Switch and configure it like so...

Installing Windows 10

Once your Virtual Switch has been created, you can start the process of installing creating a Windows 10 VM.

  1. Initiate a new VM creation from the Hyper-V Manager.
  2. You will be guided through a list of screens in which you can use to configure your VM.
    1. Specify a name for your VM and location.
    2. Select a 'Generation 2' type when prompted.
    3. Assign enough memory to operate you VM. A recommended safe starting point would be 4 gigs.
    4. When prompted to assign a network, assign the network that we created in our previous steps.
    5. When prompted to supply a Virtual Disk size, I went with 40 gigs. Of which can be expanded at any time after creation.
    6. On the Installation Options screen, target your Windows 10 .ISO to load on boot.
  3. After completing the initial configuration screens, you will enter the normal boot sequence associated with Windows 10. Follow all the normal Windows 10 install prompts until you have a fully installed version of Windows 10 on your new VM.

Configuring Your VM To Be Vagrant Ready

Once you have a fully functional Window 10 VM inside your instance of Hyper-V, you now have to prepare the VM to be exported as a Vagrant Box. There are few goals we want to keep in mind as we go through this process.

  • We want to ensure we are producing...
    • A Box that Vagrant can talk to.
    • A Box that has any and all necessary software installed, based on your needs.
    • A Box that is as small as possible.
Create windows vagrant box centos

Talk To Me

Setting up your VM to be able to communicate is pretty straightforward.

Create Windows Server 2012 Vagrant Box

  1. Disabling UAC.
    • Hit the Windows key & type UAC.
    • Select 'Change User Account Control Settings'.
    • Disable UAC.
  2. Switch your network to private.
    • Follow the instructions at this link.
  3. Configure WinRM (communication pipe between Vagrant and Windows) by running the following commands.

What Software Should I Install?

This is highly dependent on the goals you have for your Box. Personally, I take the following approach.

  • Install as little software as possible.
    • This helps keep my Box as small as possible. Of which, I store on Dropbox. By doing so, I eliminate the need to sync a large Box on any new machines I am using for development.
    • All software needed for my dev environment is installed during provisioning of a new VM.
    • For more information on how to do this, you can visit my Pioneer Code Windows Development Environment GitHub repository.
  • Install Chocolatey to assist with provisions. If you don't know what Chocolatey is yet, I recommend it highly and urge you to try it out.

What About Turning Those 1(s) to 0(s)?

The last few steps we need to take will allow us to package our Box in a manner that will produce the smallest footprint possible.

  • Defrag your VM.
    • Hit the **windows** key, type 'defrag' and run the native windows defragmentation tool.
    • Defragging your disk will allow the next step to complete as efficient as possible.
  • Run sDelete to flip all unused bits to zero.
    • If you installed Chocolatey, simply run the following command to install sDelete.
    • If you have not installed Chocolatey, download sDelete at this link.
    • Once installed, run the following command against the sDelete executable.
    • This process will take a while to complete. Often in the range of 30 to 60 mins. It is also worth noting that sDelete at some point will appear to be stuck or frozen while reporting 100%. When you get to the point, do not terminate the process. Instead, be patient and wait it out. Eventually, sDelete will report back a completed status.

Building Your Hyper-V Box

Now that we have configured and created our VM, we can move to building our actual Vagrant Box.

Exporting Your Hyper-V VM

Right-click your VM in the Hyper-V manager and select 'Export'.

Specify a location and hit export.

Building Your Folder Structure

The Export process will take a couple of minutes and once done, you will end up with a folder structure like so.

Adding Vagrant metadata.json

Delete the 'Snapshots' folder and add a metadata.json file.

The metadata.json is used by Vagrant to understand what your box is made up of. Open your new metadata.json file and add the following to get started with.

This basic configuration will be enough to build a functioning Box but if you are interested in what else can be added, you can check out the official Box File Format documentation.

Packaging Your Box

A Box File is a compressed file of the type tar, tar.gz, or zip. Out of the three options, a tar.gz will often produce the best compression rate.

To start the process, ensure you have 7Zip installed. Again, you can download it directly from the 7Zip site or install it with Chocolatey.

Once installed, create a tar file with 7zip.

This process will take a few minutes. Once complete, gzip your new tar file in 7Zip. Depending on the 'Compression level' you choose, the time to completion will vary. For reference, when I ran 'Ultra', it was measurable in hours.

Though not needed, once complete, you can strip the tar.gz extension and replace it with .box.

Vagrant Up

The building of your new Box File is complete. To test it we need to first add your new Box to Vagrant. Open up a command prompt and run the following command.

Navigate to your VagarntFile directory and run the following.

VagrantFile

Setting up a VagrantFile is a whole topic in itself. Luckily for you, I have already done the grunt work and provided a fully functioning VagarntFile at Pioneer Code Windows Development Environment.

Conclusion

And that is that. You know should be in the position to create Hyper-V Windows 10 VMs using a pre-configured Box File. If you have any question, be sure to let me know in the comments section below.

8i 9i 10g 11g 12c 13c 18c 19c 21c Misc PL/SQL SQL RAC WebLogic Linux

Home » Articles » Vm » Here

When using Vagrant you often use an existing Box as the base for your installation. In some situations you will want to create your own boxes. This could be because a box doesn't already exist, you have common setup you would prefer not the repeat for every build, or because you prefer not to use other people's boxes. In this article we demonstrate the creation of a new Vagrant base box using a Fedora distribution and VirtualBox. The approach would be similar for other Linux distributions.

Related articles.

Create a Virtual Machine (VM)

We start by manually creating a virtual machine (VM) in VirtualBox in the normal way. In this example we are building a box for Fedora 30 beta, because at the time of writing there isn't a box available for the beta release. The VM was created with the following details.

  • Name: fedora-30
  • Operating System: Fedora (64-bit)
  • Base Memory: 1024MB
  • Network: Default. Adapter 1 Intel PROD/1000 MT Desktop (NAT)
  • Storage: 60G VMDK Dynamically allocated storage.
  • Boot Order: Hard Disk first.

The VM definition is purposely simple, as we will tailor it for every installation using the Vagrantfile for that specific installation. That includes changing the VM name, CPU, memory, network configuration and any additional storage. The only thing we really care about is the virtual disk is big enough to be useful for basic work, and there is a NAT network adaptor, which is mandatory for a Vagrant box.

In this example we installed the operating system using the 'Fedora-Server-dvd-x86_64-30_Beta-1.8.iso' media. We did a basic installation, trying not to deviate from the defaults, and with minimal additional software. Once complete, we made sure the installation media was no longer attached to the VM.

At this point we have a functioning VM, with very little installed on it.

Remember not to put anything private on the box, as anyone else using the box may have access to it!

Mandatory Vagrant Configuration

With the VM in place we need to do the mandatory Vagrant-specific configuration. A Vagrant base box must conform to a few specific standards in terms of configuration. All this configuration is done as the 'root' user inside the VM.

The 'root' user password must be set to 'vagrant' and there needs to be a user called 'vagrant' with a password of 'vagrant'. You could do this setup during the installation, but if not here is what you need to do.

We need to give the 'vagrant' user password-less sudo, by amending the '/etc/sudoers' file.

All vagrant boxes use a known insecure public key, so Vagrant can make the initial connection when provisioning a new VM. It sounds scary, but when you look at the output when creating a new VM from a box you will see something like this.

We can see the insecure key was replaced by a newly generated keypair, and the insecure keypair removed.

The insecure public key is available from github. The commands below download it to create the 'authorized_keys' file for the 'vagrant' user.

Next we need to install the VirtualBox guest additions. Before we can do that, we need to make sure some prerequisite packages are present. We will also take the opportunity to update all the existing packages and reboot the VM.

Once the VM is rebooted, we can install the guest additions.

We attach the guest additions image using the 'Devices > Insert Guest Additions CD image...' menu option on the VM window. We then need to mount the CD and install the guest additions using the following commands. Notice we eject the guest additions media once complete.

We've not installed X or any window manager, so the graphics modules will fail to compile. This is fine.

Create Vagrant Box

The Vagrant configuration is complete, so we need to shut down the VM.

Package the Box

Once the VM is created and the Vagrant configuration is complete, we can turn the VM into a Vagrant box using the 'vagrant package' command. We switch to a directory where we want the package to be created, then we issue the 'vagrant package' command. The '--base' flag is used to specify which VM should be packaged into a box, and the '--output' flag allows us to name the resulting package. Depending on the size of the VM disks, this can take a while to complete.

Once we have the package, we can make it available for use by adding it to our list of available boxes. The follow commands add the new box and list the currently available boxes.

Now you have the box, you can use it like any other box by referencing it in a Vagrantfile for a new build.

For more information see:

Hope this helps. Regards Tim...