Skip to content

Commit

Permalink
Add more information when patching .vbox file. Ref GNS3/gns3-gui#3542
Browse files Browse the repository at this point in the history
  • Loading branch information
grossmj committed Jan 20, 2025
1 parent ccc8974 commit bd58196
Showing 1 changed file with 38 additions and 21 deletions.
59 changes: 38 additions & 21 deletions gns3server/compute/virtualbox/virtualbox_vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,28 +218,45 @@ def _patch_vm_uuid(self):
"""
Fix the VM uuid in the case of linked clone
"""
if os.path.exists(self._linked_vbox_file()):
try:
tree = ET.parse(self._linked_vbox_file())
except ET.ParseError:
raise VirtualBoxError("Cannot modify VirtualBox linked nodes file. "
"File {} is corrupted.".format(self._linked_vbox_file()))
except OSError as e:
raise VirtualBoxError("Cannot modify VirtualBox linked nodes file '{}': {}".format(self._linked_vbox_file(), e))

machine = tree.getroot().find("{http://www.virtualbox.org/}Machine")
if machine is not None and machine.get("uuid") != "{" + self.id + "}":
linked_vbox_file = self._linked_vbox_file()
if not os.path.exists(linked_vbox_file):
raise VirtualBoxError("Cannot find VirtualBox linked node file: {}".format(linked_vbox_file))

for image in tree.getroot().findall("{http://www.virtualbox.org/}Image"):
currentSnapshot = machine.get("currentSnapshot")
if currentSnapshot:
newSnapshot = re.sub(r"\{.*\}", "{" + str(uuid.uuid4()) + "}", currentSnapshot)
shutil.move(os.path.join(self.working_dir, self._vmname, "Snapshots", currentSnapshot) + ".vdi",
os.path.join(self.working_dir, self._vmname, "Snapshots", newSnapshot) + ".vdi")
image.set("uuid", newSnapshot)

machine.set("uuid", "{" + self.id + "}")
tree.write(self._linked_vbox_file())
try:
tree = ET.parse(linked_vbox_file)
except ET.ParseError:
raise VirtualBoxError("Cannot modify VirtualBox linked node file. "
"File {} is corrupted.".format(linked_vbox_file))
except OSError as e:
raise VirtualBoxError("Cannot modify VirtualBox linked node file '{}': {}".format(linked_vbox_file, e))

machine = tree.getroot().find("{http://www.virtualbox.org/}Machine")
if machine is not None and machine.get("uuid") != "{" + self.id + "}":

for image in tree.getroot().findall("{http://www.virtualbox.org/}Image"):
currentSnapshot = machine.get("currentSnapshot")
if currentSnapshot:
newSnapshot = re.sub(r"\{.*\}", "{" + str(uuid.uuid4()) + "}", currentSnapshot)
shutil.move(
os.path.join(self.working_dir, self._vmname, "Snapshots", currentSnapshot) + ".vdi",
os.path.join(self.working_dir, self._vmname, "Snapshots", newSnapshot) + ".vdi"
)
log.info("VirtualBox VM '{name}' [{id}] snapshot file moved from '{current}' to '{new}'".format(
name=self.name,
id=self.id,
current=currentSnapshot,
new=newSnapshot,
))
image.set("uuid", newSnapshot)

log.info("VirtualBox VM '{name}' [{id}] '{vbox_file}' has been patched".format(
name=self.name,
id=self.id,
vbox_file=linked_vbox_file,
))
machine.set("uuid", "{" + self.id + "}")
tree.write(linked_vbox_file)

async def check_hw_virtualization(self):
"""
Expand Down Expand Up @@ -458,7 +475,7 @@ async def _reattach_linked_hdds(self):

async def save_linked_hdds_info(self):
"""
Save linked cloned hard disks information.
Save linked cloned hard disk information.
:returns: disk table information
"""
Expand Down

0 comments on commit bd58196

Please sign in to comment.