How to Create a Windows 7 Vagrant Base Box?
Introduction
Since Vagrant 1.6 release, Vagrant add support for Windows VMs. We will go through the steps to follow to create a Windows 7 Vagrant base box.
Pre-requisites
- VirtualBox/VMWare
- Vagrant 1.6 or above
- Windows 7 ISO file
- VirtualBox Guest Additions
- A valid license key for the version of Windows 7 to be installed
Create New Virtual Machine
Start VirtualBox and create a new Windows 7 virtual machine. Name it whatever you like, though keep it simple as we will override this name later in Vagrantfile. We will follow most of the advice given in the Vagrant documentation regarding base boxes.
- Disk Space: Choose a virtual hard disk (VDI) dynamically allocated and choose a high upper limit for the size. The default max-size is pretty low (32 GB).
- Memory: Choose a low value here e.g. 1 GB, the reason is users that use the base box can always increase the memory in their Vagrantfile. I chose 2048 MB, but 1024 MB would do as well.
- CPU: Leave this at the default of 1. Again this is something users can override in the Vagrantfile, so try not to require a higher number to start with.
- Disable any unnecessary hardware like audio. Leave 2D/3D acceleration disabled.
The idea behind these choices is to create your base box as light on resources as possible with as much room as possible for later customization. When creating a new machine in VirtualBox in the wizard a lot of options are auto-configured, so we may need to edit the machine after it is created, going into Settings, to change some options.
- Settings > General > Advanced: Enable Host-to-Guest shared clipboard.
- Settings > General > System > Motherboard: Disable Floppy boot.
- Settings > General > Audio: Disable audio.
You can follow the New Virtual Machine Wizard with Custom (advanced) configuration to create the Windows 7 virtual machine. Select similar options as VirtualBox, although some options might be different.
- Select the version of Windows to install
- Full name: vagrant
- Password: vagrant
- Firmware type: BIOS
- Network connection: NAT
- Disk: Store virtual disk as a single file
Users of your base box can always modify these options in their Vagrantfile, using the VirtualBox specific configuration or VMWare specific configuration.
Install Windows 7
You should have your Windows 10 ISO ready at hand, then configure the ISO file to boot from in VirtualBox, skip prompts to enter a license key and this can always be entered later through Control Panel or command line, after this, installation is straight forward.
Towards the end of the installation you will be prompted to sign in your Microsoft account, again skip this. You want to create a local admin account on your clean install named vagrant with password also vagrant. This is required if you want Vagrant to automatically connect to and provision machines on your base box.
Let the Windows setup complete, you will be presented with a login prompt for your user vagrant. Go ahead and login to the Windows 10 desktop. When connecting to the network windows will ask you if the network you are connected to is public or private. Choose private, otherwise you will get issues with WinRM in the next step.
Prepare Windows For Vagrant
- Install VirtualBox Guest Additions/VMware Tools on your base box from the menu.
- Control Panel > Programs and Features > Turn Windows features on or off.
- Disable UAC: Control Panel > User Accounts > Change user account control settings, drop the slider to the bottom - Never notify.
- Turn off System Protection: Control Panel > System > Advanced System Settings > System Protection > Turn off protection.
- Enable WinRM service:Open command prompt as Administrator and paste each line and press enter. The last line configures WinRM services to start automatically. This will allow Vagrant to connect to the box and control it. WinRM is the alternative to ssh for windows boxes.
winrm quickconfig -q
winrm set winrm/config/winrs @{MaxMemoryPerShellMB="512"}
winrm set winrm/config @{MaxTimeoutms="1800000"}
winrm set winrm/config/service @{AllowUnencrypted="true"}
winrm set winrm/config/service/auth @{Basic="true"}
sc config WinRM start= auto
-
Relax the Powershell execution policy: Run Powershell as
Administrator and execute this command:
Set-ExecutionPolicy -ExecutionPolicy Unrestricted
, choose Yes to any prompts. This will allow Powershell scripts to provision your base box without annoying restrictions. - Enable remote connection to your box: Control Panel > System > Advanced System Settings > Remote > Allow remote connections to this computer.
- Turn off Windows Update: Control Panel > System > Turn automatic updating on or off > Never check for updates
- The Advanced tab > Performance Settings, tweak visual effects to performance, then further on the Advanced tab limit the maximum virtual memory paging file size to 1024 MB. Restart if prompted.
- Finally, use disk clean to reduce the box size
Exporting Your VirtualBox Virtual Machine as Base Box
vagrant package --base <Name of the VM in the Virtualbox GUI> --output /path/to/output/Windows7.box
Exporting Your VMWare Virtual Machine as Base Box
Vagrant also support to create boxes for VMware provider. You will
need to first install the Vagrant VMware support following this
guide. Create a metadata.json
file inside
vmwarevm folder with following content.
{
"provider": "vmware_desktop"
}
Remove any extraneous files from the vmwarevm folder and package it. Prior to packaging up a box, we should shrink the hard drives as much as possible. This can be done with
vmware-vdiskmanager -d /path/to/vmwarevm/main.vmdk
vmware-vdiskmanager -k /path/to/vmwarevm/main.vmdk
cd /path/to/vmwarevm
tar cvzf /path/to/output/Windows7.box ./*
Test Your Base Box Locally
vagrant box add /path/to/output/Windows7.box --name Windows7
vagrant init Windows7
vagrant up --provider=vmware_desktop
Publish Your Base Box
Open
https://app.vagrantup.com/boxes/new
and follow the instruction to create a Box. Once the Box is
created, select Add a provider
, and then choose
corresponding provider and upload the box file. You can also use
my box distribution.
Use Your Base Box
You can use vagrant up
command with the
--provider
flag to specify the provider you want to
use and then specify the base box distribution with
config.vm.box = "vitamina/Windows7"
.
Reference
- Creating a Windows 10 Base Box for Vagrant with VirtualBox
- Vagrant: Creating a Base Box
- Vagrant: CLI package command
- Vagrant: Creating a New Vagrant Box
- Vagrant Windows Machine
- In search of a lightweight windows vagrant box
- Vagrant Box for Windows 10
- Vagrant Windows 7 Machine
- Vagrant: Basic Provider Usage
- Vagrant: VMware Provider
- Vagrant: Default Provider
- Vagrant: Box File Format
- CREATE A WINDOWS 10 VAGRANT BOX
- Create Windows 10 Vagrant Base Box
- Reusable Windows VMs with Vagrant
- Creating a Windows Box with Vagrant 1.6