Last active
August 26, 2022 01:20
-
-
Save stuaxo/3839938 to your computer and use it in GitHub Desktop.
Update virtualbox hard disk uuids
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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