Skip to content

Commit

Permalink
split code to remove uuid and name into seperate functions, call only…
Browse files Browse the repository at this point in the history
… once
  • Loading branch information
abbbi committed Nov 5, 2024
1 parent 10434fb commit b099a16
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
1 change: 1 addition & 0 deletions Changelog
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Version 2.16
* virtnbdrestore: If domain with multiple disks is restored and config
adjustment is requested, only the path to the last disk is updated. (#232).
Thanks Richard Stephens for report and fix.
* virtnbdrestore: fix logic for adjusting the vm name

Version 2.15
---------
Expand Down
6 changes: 5 additions & 1 deletion libvirtnbdbackup/restore/disk.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,13 @@ def restore( # pylint: disable=too-many-branches

_backingstore(args, disk)
if args.adjust_config is True:
restConfig = vmconfig.adjust(args, disk, restConfig.decode(), targetFile)
restConfig = vmconfig.adjust(disk, restConfig.decode(), targetFile)

logging.debug("Closing NBD connection")
connection.disconnect()

if args.adjust_config is True:
restConfig = vmconfig.removeUuid(restConfig.decode())
restConfig = vmconfig.setVMName(args, restConfig)

return restConfig
32 changes: 22 additions & 10 deletions libvirtnbdbackup/restore/vmconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,9 @@ def removeDisk(vmConfig: str, excluded) -> bytes:
return xml.ElementTree.tostring(tree, encoding="utf8", method="xml")


def adjust(
args: Namespace, restoreDisk: DomainDisk, vmConfig: str, targetFile: str
) -> bytes:
"""Adjust virtual machine configuration after restoring. Changes
the paths to the virtual machine disks and attempts to remove
components excluded during restore."""
def removeUuid(vmConfig: str) -> bytes:
"""Remove the auto generated UUID from the config file to allow
for restore into new name"""
tree = xml.asTree(vmConfig)

try:
Expand All @@ -65,14 +62,29 @@ def adjust(
except IndexError:
pass

return xml.ElementTree.tostring(tree, encoding="utf8", method="xml")


def setVMName(args: Namespace, vmConfig: bytes) -> bytes:
"""Change / set the VM name to be restored"""
tree = xml.asTree(vmConfig)
name = tree.xpath("name")[0]
if args.name is None:
if args.name is None and not name.text.startswith("restore"):
domainName = f"restore_{name.text}"
logging.info("Change VM name from [%s] to [%s]", name.text, domainName)
name.text = domainName
else:
domainName = args.name
logging.info("Changing name from [%s] to [%s]", name.text, domainName)
name.text = domainName
logging.info("Set name from [%s] to [%s]", name.text, args.name)
name.text = args.name

return xml.ElementTree.tostring(tree, encoding="utf8", method="xml")


def adjust(restoreDisk: DomainDisk, vmConfig: str, targetFile: str) -> bytes:
"""Adjust virtual machine configuration after restoring. Changes
the paths to the virtual machine disks and attempts to remove
components excluded during restore."""
tree = xml.asTree(vmConfig)
for disk in tree.xpath("devices/disk"):
if disk.get("type") == "volume":
logging.info("Disk has type volume, resetting to type file.")
Expand Down

0 comments on commit b099a16

Please sign in to comment.