Skip to content

Instantly share code, notes, and snippets.

@stuaxo
Last active August 26, 2022 01:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stuaxo/3839938 to your computer and use it in GitHub Desktop.
Save stuaxo/3839938 to your computer and use it in GitHub Desktop.
Update virtualbox hard disk uuids
def update_virtualbox_hd_uuids(vbox_conf):
'''
After using clonevm (ie VBoxManage clonevm "my-virtual-machine" --name "new_vm")
pass in name of VirtualBox configuration xml '''
from xml.dom.minidom import parse, parseString
vbox = parse(vbox_conf).documentElement
for hardDisk in vbox.getElementsByTagName('HardDisk'):
location = hardDisk.getAttribute('location')
if os.path.exists(os.path.basename(location)):
location = './' + os.path.basename(location)
hardDisk.setAttribute('location', location) # Make HD paths relative
old_uuid = hardDisk.getAttribute('uuid')
uuid = '{%s}' % local('VBoxManage internalcommands sethduuid ' + location, capture=True).partition(':')[2][1:]
print 'Updating uuid %s to %s' % (old_uuid, uuid)
hardDisk.setAttribute('uuid', uuid)
for image in vbox.getElementsByTagName('Image'):
if image.hasAttribute('uuid'):
if image.getAttribute('uuid') == old_uuid:
image.setAttribute('uuid', uuid)
vbox.writexml(open(vbox_conf, mode='w'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment