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,28 +218,45 @@ 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()):
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") linked_vbox_file = self._linked_vbox_file()
if machine is not None and machine.get("uuid") != "{" + self.id + "}": 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"): try:
currentSnapshot = machine.get("currentSnapshot") tree = ET.parse(linked_vbox_file)
if currentSnapshot: except ET.ParseError:
newSnapshot = re.sub(r"\{.*\}", "{" + str(uuid.uuid4()) + "}", currentSnapshot) raise VirtualBoxError("Cannot modify VirtualBox linked node file. "
shutil.move(os.path.join(self.working_dir, self._vmname, "Snapshots", currentSnapshot) + ".vdi", "File {} is corrupted.".format(linked_vbox_file))
os.path.join(self.working_dir, self._vmname, "Snapshots", newSnapshot) + ".vdi") except OSError as e:
image.set("uuid", newSnapshot) raise VirtualBoxError("Cannot modify VirtualBox linked node file '{}': {}".format(linked_vbox_file, e))
machine.set("uuid", "{" + self.id + "}") machine = tree.getroot().find("{http://www.virtualbox.org/}Machine")
tree.write(self._linked_vbox_file()) 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): 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
""" """