Add more information when patching .vbox file. Ref https://github.com/GNS3/gns3-gui/issues/3542

This commit is contained in:
grossmj 2025-01-20 13:22:22 +10:00
parent ccc8974d92
commit bd58196817
No known key found for this signature in database
GPG Key ID: 1E7DD6DBB53FF3D7

View File

@ -218,14 +218,18 @@ class VirtualBoxVM(BaseNode):
""" """
Fix the VM uuid in the case of linked clone Fix the VM uuid in the case of linked clone
""" """
if os.path.exists(self._linked_vbox_file()):
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))
try: try:
tree = ET.parse(self._linked_vbox_file()) tree = ET.parse(linked_vbox_file)
except ET.ParseError: except ET.ParseError:
raise VirtualBoxError("Cannot modify VirtualBox linked nodes file. " raise VirtualBoxError("Cannot modify VirtualBox linked node file. "
"File {} is corrupted.".format(self._linked_vbox_file())) "File {} is corrupted.".format(linked_vbox_file))
except OSError as e: except OSError as e:
raise VirtualBoxError("Cannot modify VirtualBox linked nodes file '{}': {}".format(self._linked_vbox_file(), e)) raise VirtualBoxError("Cannot modify VirtualBox linked node file '{}': {}".format(linked_vbox_file, e))
machine = tree.getroot().find("{http://www.virtualbox.org/}Machine") machine = tree.getroot().find("{http://www.virtualbox.org/}Machine")
if machine is not None and machine.get("uuid") != "{" + self.id + "}": if machine is not None and machine.get("uuid") != "{" + self.id + "}":
@ -234,12 +238,25 @@ class VirtualBoxVM(BaseNode):
currentSnapshot = machine.get("currentSnapshot") currentSnapshot = machine.get("currentSnapshot")
if currentSnapshot: if currentSnapshot:
newSnapshot = re.sub(r"\{.*\}", "{" + str(uuid.uuid4()) + "}", currentSnapshot) newSnapshot = re.sub(r"\{.*\}", "{" + str(uuid.uuid4()) + "}", currentSnapshot)
shutil.move(os.path.join(self.working_dir, self._vmname, "Snapshots", currentSnapshot) + ".vdi", shutil.move(
os.path.join(self.working_dir, self._vmname, "Snapshots", newSnapshot) + ".vdi") 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) 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 + "}") machine.set("uuid", "{" + self.id + "}")
tree.write(self._linked_vbox_file()) tree.write(linked_vbox_file)
async def check_hw_virtualization(self): async def check_hw_virtualization(self):
""" """
@ -458,7 +475,7 @@ class VirtualBoxVM(BaseNode):
async def save_linked_hdds_info(self): async def save_linked_hdds_info(self):
""" """
Save linked cloned hard disks information. Save linked cloned hard disk information.
:returns: disk table information :returns: disk table information
""" """