Create Windows Vagrant Box



Now that Packer created the Vagrant box, add that box to a Vagrant client, and use it in a project. First, use the local box file created from Packer to add the box: C: Scripts packer test vagrant box add. Windows10virtualbox.box -name=windows10. Next, create a new directory, and initialize the Vagrant box with the vagrant init command. Vagrant-centos-7-minimal is the machine's name as you have set it in VirtualBox' GUI. Packaging needs some time, but soon you will see that a file called package.box has been created in this directory. You can now import this base box into Vagrant with the command vagrant box add vagrant-centos-7-minimal package.box.

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.

Windows

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.

Create Windows 10 Vagrant Base Box

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.

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...

Testing your application in different environments can be hard to do manually, as its time consuming, error prone, and not easily reproducible. This article will show you a way on how to automatically create Windows environments, where you can test your application.

Tools

You will use the following tools:

  • Packer and packer-windows to create an up-to-date Windows base image from an ISO file.
  • Vagrant to launch and configure a base image tailored to a specific scenario.
  • Boxstarter to easily install software from Chocolatey packages.
  • VirtualBox to run virtual machines.

Install

You will need to run several commands. These must be run on a bash shell, which I’ll assume that you have installed following the instructions from Sane shell environment on Windows.

Make sure you have previously installed Packer, Vagrant and VirtualBox and have them available on your PATH, e.g. with:

Windows base image

Packer is a tool to automatically create a machine image from a template file.

packer-windows is a repository with pre-configured template files for installing different Windows versions.

We will use them to drive the installation of Windows 2012 R2 into a virtual machine image. This image will have the bare minimum needed to be later used by Vagrant.

This base image will be automatically configured with:

  • SSH server.
  • WinRM.
  • vagrant user account with a vagrant password.
  • Latest Windows updates.
  • VirtualBox guest additions.
  • 60 GB disk.

Start by getting the Windows template files:

NB These templates are for the trial versions of Windows, namely Windows 2012 R2 Standard, but you can also install from your own Windows ISO file.

In order to makes things easier for us we will tweak a couple of files.

Vagrant

We want to see how the installation is going on, for that we need to change the template file to start the virtual machine in non-headless mode. For that, edit the windows_2012_r2.json template file:

By default, the template is configured to install the latest Windows updates, but the default timeout might now be enough for the updates to finish in time, so increase the ssh_timeout from the default 2h to 8h:

Create Windows Server 2012 Vagrant Box

Also, sometimes, the last provisioning step fails, so you also need to change the line that has rm -rf to:

Also consider removing the ./scripts/compact.bat (it takes ages to run) and the ./scripts/chef.bat (you might not need it) lines.

Finally, build the VirtualBox based image:

Now be patient! It will download the Windows ISO file and install the latest Windows Updates. These steps take a lot of time to complete (about 2 hours in my environment). At the end you should have a .box file (about 1.8 GB), ready to be used by Vagrant.

Add the generated box file to vagrant:

NB You can distribute the box file to other system, or you can delete it (its no longer needed once added to vagrant)

We are now almost ready to launch a Windows virtual machine with Vagrant, for that we need to create a Vagrantfile:

And launch it:

NB The initial run will take about 4m to boot into Windows. Later runs take about 40s. But YMMV.

NB To troubleshoot you need pass the --debug flag to vagrant up.

Once Windows boots, try it a bit to check whether things are working fine, and then stop the virtual machine with:

And destroy it:

You are now ready to customize a vagrant environment to fit your needs. For that we will modify the Vagrantfile to use a provision script.

Provisioning

Customizing a vagrant environment is a mater of creating one or more provision scripts. Lets start simple, with one embedded within the Vagrantfile, it will just change the keyboard layout and timezone:

And launch it:

If its running fine, stop the virtual machine with:

Create Windows Vagrant Box

Now its time to install applications. This is handled by Boxstarter. Lets modify the provision script to install Boxstarter and some applications:

Box

Create the script to automatically install some software:

And launch it:

You will notice that vagrant up seems to fail with something like:

The following WinRM command responded with a non-zero exit status. Vagrant assumes that this means the command failed!

Vagrant Box Windows 10

powershell -ExecutionPolicy Bypass -OutputFormat Text -file c:tmpvagrant-shell.ps1

But that’s expected, you need to monitor the install yourself to known when its done. Doing that automatically would be nice, but I’ll leave that as an exercise for the reader (hint: You could create a new boxstarter Vagrant provisioner).

As a side note, you could also manually provision the machine and later export it as a new box file, e.g.:

The templates contained in the windows-packer repository are configured to install the trial versions of windows, but you can also install from a specific ISO file. For that, you need to set the ISO path and its MD5 checksum, e.g.:

The iso_checksum value can be computed with md5sum:

If you don’t have a volume license ISO, you also need to set the Product Key. For that open the answer_files/2012_r2/Autounattend.xml file, search for ProductKey and follow the instructions.

And thats it! You should now be able to create Windows environments to fit your needs.

You should also look into configuration management tools like Ansible, Chef, and Puppet. These might useful to actually install and configure your own application.





Comments are closed.