Skip to content

Instantly share code, notes, and snippets.

@tknerr
Last active February 11, 2019 19:42
Show Gist options
  • Save tknerr/7fc4a4191903e3611c45 to your computer and use it in GitHub Desktop.
Save tknerr/7fc4a4191903e3611c45 to your computer and use it in GitHub Desktop.
Trying to get a minimal Vagrant with a Windows guest example to work...

Environment

If you are trying to reproduce this exactly, I'm using the following environment:

Basebox

First challenge is to get a windows basebox from somewhere. I couldn't find a good one, so built one myself from the windows/boxcutter packer templates:

git clone https://github.com/boxcutter/windows.git windows-boxes
cd windows-boxes
make virtualbox/eval-win7x86-enterprise

This takes a while (~45 minutes, depending on your internet connection).

Once the basebox is built, you can import it to Vagrant:

vagrant box add boxcutter/eval-win7x86-enterprise box/virtualbox/eval-win7x86-enterprise-nocm-1.0.4.box

Goal: Minimal Vagrantfile Example

Next I was trying to figure out a minimal Vagrantfile example, which works for all the basic Vagrant things, e.g.:

  • vagrant up should not work reliably (not hang / run into timeouts)
  • vagrant ssh should work
  • vagrant ssh -c "echo foo" should run the command
  • vagrant provision should work, with either inline: or path: (.bat / .ps) "shell" provisioner
  • synced folders should work (via vboxsf or smb ideally)

First Try

The Vagrantfile that is packaged within the above built basebox already includes some windows specific settings, such as setting config.vm.communicator = "winrm" and config.vm.guest = :windows, so we don't need to repeat that in our Vagrantfile.

So this was my first try:

Vagrant.configure(2) do |config|
  config.vm.box = "boxcutter/eval-win7x86-enterprise-nocm"
  config.vm.provision "shell", inline: "echo hello!"
  config.vm.synced_folder ".", "/vagrant", disabled: true
end

No luck though...

  • vagrant up works, but hangs and never completes (I can Ctrl+C though and continue working with the VM)
  • vagrant ssh works, yay! :-)
  • vagrant ssh -c "echo foo" drops me into the shell instead of executing the command
  • vagrant provision hangs and times out
  • synced folders I explicitly disabled for now to make it even simpler
W:\repo\windows-examples\minimini>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'boxcutter/eval-win7x86-enterprise-nocm'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: minimini_default_1430891237003_64906
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
==> default: Forwarding ports...
default: 3389 => 3389 (adapter 1)
default: 5985 => 5985 (adapter 1)
default: 22 => 2222 (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
Timed out while waiting for the machine to boot. This means that
Vagrant was unable to communicate with the guest machine within
the configured ("config.vm.boot_timeout" value) time period.
If you look above, you should be able to see the error(s) that
Vagrant had when attempting to connect to the machine. These errors
are usually good hints as to what may be wrong.
If you're using a custom box, make sure that networking is properly
working and you're able to connect to the machine. It is a common
problem that networking isn't setup properly in these boxes.
Verify that authentication configurations are also setup properly,
as well.
If the box appears to be booting properly, you may want to increase
the timeout ("config.vm.boot_timeout") value.
W:\repo\windows-examples\minimini>vagrant ssh
Last login: Tue May 5 23:05:12 2015 from 10.0.2.2
Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\Users\vagrant>echo "oh yeah!"
"oh yeah!"
C:\Users\vagrant>exit
Connection to 127.0.0.1 closed.
W:\repo\windows-examples\minimini>vagrant ssh -c "echo just run it!"
Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\Users\vagrant>exit
Connection to 127.0.0.1 closed.
W:\repo\windows-examples\minimini>vagrant provision
==> default: Waiting for cleanup before exiting...
Terminate batch job (Y/N)? Y
@tknerr
Copy link
Author

tknerr commented May 6, 2015

Hmmmm... running vagrant provision --debug looks like it can't connect to winrm?

This is where it stalls:

....
DEBUG subprocess: Waiting for process to exit. Remaining to timeout: 32000
DEBUG subprocess: Exit status: 0
DEBUG virtualbox_4_3:   - [1, "rdp", 3389, 3389]
DEBUG virtualbox_4_3:   - [1, "ssh", 2222, 22]
DEBUG virtualbox_4_3:   - [1, "winrm", 5985, 5985]
DEBUG winrmshell: initializing WinRMShell
DEBUG winrmshell: powershell executing:
hostname
if ($?) { exit 0 } else { if($LASTEXITCODE) { exit $LASTEXITCODE } else { exit 1 } }
 INFO winrmshell: Attempting to connect to WinRM...
 INFO winrmshell:   - Host: 127.0.0.1
 INFO winrmshell:   - Port: 5985
 INFO winrmshell:   - Username: vagrant

@tknerr
Copy link
Author

tknerr commented May 6, 2015

Trying this WinRM troubleshooting guide:
https://github.com/WinRb/WinRM#troubleshooting

I'm now running into an error with the network adapter (says "winrm firewall exception will not work since the network connection type on this machine is set to public" and I should change it to either "domain" or "private"...)

darn I need the guest additions so I can copy/paste...

@chrisbaldauf
Copy link

Looks like winrm is the likely culprit. I'm surprised that vagrant ssh works, but I'm guessing that the box utter you built was with openssh and not winrm.

Have you tried the vagrant-winrm plugin? You can use the vagrant winrm -c (just like vagrant ssh -c) command to try to help debug.

I'm sure the troubleshooting guide talks about it but here are some things I've run into:

  • make sure winrm service is set to automatic start. I believe the default is delayed auto
  • enable basic authentication
  • allow unencrypted communication

There is also a winrm log in Event Viewer which might give you more insight, provided you can RPD in
http://blogs.msdn.com/b/wmi/archive/2010/03/16/collecting-winrm-traces.aspx

@chrisbaldauf
Copy link

Also, sc config winrm type= own will tell winrm to run in its own process, rather than a shared space.

@sneal
Copy link

sneal commented May 7, 2015

Since Packer only supports SSH I assume the boxcutter template has both WinRM and SSH configured. The fact that vagrant ssh works is a good indication of that.

Don't use the vagrant-windows plugin like @chrisbaldauf mentioned, it was meant for Vagrant < 1.5 that didn't natively support Windows guests. Newer versions of Vagrant have native WinRM support.

Vagrant has some issues with swallowing connection errors (fixed in master), so you're left with a hung box instead of failing fast with an authentication error telling you what the underlying problem is. Try running vagrant with the --debug flag, this will help isolate the actual error. More than likely you don't have WinRM properly configured or are using the wrong username and password. This is the PowerShell script I use to configure WinRM via my Packer build.

@jimklo
Copy link

jimklo commented May 7, 2015

have you configured winrm inside windows? AND ensured Windows Defender isn't blocking the ports?

@tknerr
Copy link
Author

tknerr commented May 9, 2015

Hey @chrisbaldauf @sneal @jimklo thanks for your help!

Seems like I didn't get email notified about your comments since gists don't support that yet :-/

Anyways, I got my issue resolved and it was in fact a WinRM configuration issue, that somehow sneaked in when I built the boxcutter/eval-win7x86-enterprise box. I had to manually set the network adapter to private and run the winrm config mentioned here again: https://github.com/WinRb/WinRM#troubleshooting

At some point in time later, I rebuilt the same boxcutter box (nothing changed or updated), and it suddenly worked. I have rebuilt the box several times since then, and it seems to reliably work right now.

=> i.e. the boxcutter boxes are fine and all of the above commands now work as expected out-of-the-box.

Also thanks for the vagrant-winrm plugin mention, this is super helpful too!

@tknerr
Copy link
Author

tknerr commented May 9, 2015

P.S.: even sync folder work perfectly too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment