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: