Error “Cannot unregister the machine” on Jenkins, ‘kitchen test’ command


Ok, it’s a hack… so don’t use it unless you need it, and on your own risk.
For some reason, although the Windows vagrant box is updated using Matt Wrock’s packer templates from here, and everything goes well, and also on the first manual kitchen test on the Jenkins server everything goes ok, when run from Jenkin’s pipeline mechanism, the box is not destroyed.
Three VirtualBox processes remain hanged in the system.
It’s not a clean solution, but having this error over and over again:

STDERR: There was an error while executing `VBoxManage`, a CLI used by Vagrant for controlling VirtualBox. The command and stderr is shown below.
Command: [“unregistervm”, “some GUID here”, “–delete”]
Stderr: VBoxManage.exe: error: Cannot unregister the machine ‘kitchen-your_cookbook-default-windows2012r2_default_some_numbers’ while it is locked
VBoxManage.exe: error: Details: code VBOX_E_INVALID_OBJECT_STATE (0x80bb0007), component MachineWrap, interface IMachine, callee IUnknown

it’s really annoying, so until the final solution will reveal itself, this might also be a way:

$cookbook = ‘your_cookbook_name’
gwmi -Query “select * from win32_process where name = ‘virtualbox.exe'” | `
? { $_.CommandLine.Contains(“kitchen-$cookbook”)} | `
Select CommandLine, Handle | `
% { Get-Process -Id $_.Handle } | `
Stop-Process -Force -ErrorAction Continue

You’ll need to execute it from Groovy with the bat command, something like

bat “powershell -File \”../killVBoxes.ps1\” ${cookbookName}”

For the path of the PowerShell script you’ll need to provide the correct path.

Note: Be aware that when copy-pasting, the quotes and possibly other characters get messed up by WordPress, you’ll have to replace them.


Clean VirtualBox virtual machines used by Chef on converge


From time to time I keep getting strange behavior, on local PC, with virtual machines used by Chef, maybe after a ChefDK or VirtualBox update.
Some VMs do not get destroyed properly, or maybe more than one VM is created for the same cookbook.
The following PowerShell script can be used to destroy all VMs for a Chef cookbook; it must be used from the parent folder of the cookbook, using the syntax:
.\clearCache.ps1 my_chef_cookbook .
And here the script clearCache.ps1, located one level above the cookbook, in order to be usable with all its siblings:


# VBOX_HOME is a user environment variable specifying
# where the VirtualBox VMs are stored
# Something like C:\Users\[your_user]\VBox_VMs

$dir = (pwd).Path
cd $cookbook
Write-Output “Removing folder content for .kitchen”
rm “.\.kitchen\” -Force -Recurse

Write-Output “Power-off VMs”
((& ‘C:\Program Files\Oracle\VirtualBox\vboxmanage’ list vms) | `
  % { ($_.Replace(‘”‘,”) -split ” “)[0] } | ` # Replace double quotes with nothing
  ? { $_.Contains(“kitchen-“) }) | `
  % { & ‘C:\Program Files\Oracle\VirtualBox\vboxmanage’ controlvm $_ poweroff }
Write-Output “Delete VMs”
((& ‘C:\Program Files\Oracle\VirtualBox\vboxmanage’ list vms) | `
  % { ($_.Replace(‘”‘,”) -split ” “)[0] } | `
  ? { $_.Contains(“kitchen-“) }) | `
  % { & ‘C:\Program Files\Oracle\VirtualBox\vboxmanage’ unregistervm $_ –delete }

Write-Output “Delete the VMs again, just in case we previously had an error …”
rm “$($env.VBOX_HOME)\kitchen-$cookbook*” -Force -Recurse

Write-Output “Destroy kitchen”
kitchen destroy
rm “.\.kitchen\” -Force -Recurse
vagrant global-status –prune
Write-Output “Done”
cd ..

Note: Be aware that when copy-pasting, the quotes and possibly other characters get messed up by WordPress, you’ll have to replace them.

Resize VirtualBox harddisk in Windows with VBoxManage with bypassing registering error


If you want to resize a virtual harddisk for a VirtualBox machine under Windows, you can use VBoxManage utility, as follows:

& ‘C:\Program Files\Oracle\VirtualBox\VBoxManage.exe’ modifyhd “.\Ubuntu14-04LTS.vdi” —-resize 30000

where the resize attribute specifies the new bigger size in megabytes.
Note: Before doing that, do a copy of the VDI file and detach the harddrive from the VM.

If you receive an error like

VBoxManage.exe: error: Cannot register the hard disk ‘c:\path_to_vdi\Ubuntu14-04LTS.vdi’ {xyzxyz-xyzxyz-blah-blah-…}
because a hard disk ‘c:\path_to_vdi\Ubuntu14-04LTS.vdi’ with UUID {xyzxyz-xyzxyz-blah-blah-…} already exists
VBoxManage.exe: error: Details: code E_INVALIDARG (0x80070057), component VirtualBox, interface IVirtualBox, callee IUnknown
VBoxManage.exe: error: Context: “OpenMedium(Bstr(pszFilenameOrUuid).raw(), enmDevType, enmAccessMode, fForceNewUuidOnOpen, pMedium.asOutParam())” at line 178 of file VBoxManageDisk.cpp

you can do one of the following:

  • & ‘C:\Program Files\Oracle\VirtualBox\VBoxManage.exe’ modifyhd “.\Ubuntu14-04LTS.vdi” —-resize 30000
  • In VirtualBox application, go to File menu, Virtual Media Manager … and delete the entry for your VDI file, after which you’ll have to add it again from Storage menu

Note 0: Commands were executed under PowerShell, that’s why they have an & before the path of VBoxManage. If you use command line, remove it.
Note 1: Credits go to StackOverflow, IT blogs and alike, sites that helped me with the code when searching for solutions.
Note 2: Be aware that when copy-pasting, the quotes and possibly other characters get messed up by WordPress, you’ll have to replace them.

%d bloggers like this: