Skip to content

Commit e9d9755

Browse files
adjust paths in vm config when --restore-root is used
1 parent 88a63af commit e9d9755

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

libvirtnbdbackup/restore/files.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,20 @@
3131
from libvirtnbdbackup.exceptions import RestoreError
3232

3333

34-
def restore(args: Namespace, vmConfig: str, virtClient: virt.client) -> None:
34+
def restore(
35+
args: Namespace, vmConfig: str, virtClient: virt.client, restConfig: bytes
36+
) -> bytes:
3537
"""Notice user if backed up vm had loader / nvram"""
3638
config = vmconfig.read(vmConfig)
3739
info = virtClient.getDomainInfo(config)
40+
restored_files = {}
3841

3942
for setting, val in info.items():
4043
f = lib.getLatest(args.input, f"*{os.path.basename(val)}*", -1)
4144
if args.restore_root is not None:
4245
_, _, val_as_relative = os.path.splitroot(val)
4346
val = os.path.join(args.restore_root, val_as_relative)
47+
restored_files[setting] = os.path.abspath(val)
4448
if lib.exists(args, val):
4549
logging.info(
4650
"File [%s]: for boot option [%s] already exists, skipping.",
@@ -53,6 +57,10 @@ def restore(args: Namespace, vmConfig: str, virtClient: virt.client) -> None:
5357
"Restoring configured file [%s] for boot option [%s]", val, setting
5458
)
5559
lib.copy(args, f[0], val)
60+
if restConfig != b"" and args.adjust_config is True:
61+
return vmconfig.apply_paths(restConfig, restored_files)
62+
else:
63+
return restConfig
5664

5765

5866
def verify(args: Namespace, dataFiles: List[str]) -> bool:

libvirtnbdbackup/restore/vmconfig.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
from libvirtnbdbackup.virt import xml
2626
from libvirtnbdbackup.virt import disktype
2727

28+
from typing import Dict
29+
2830

2931
def read(ConfigFile: str) -> str:
3032
"""Read saved virtual machine config'"""
@@ -138,3 +140,11 @@ def restore(
138140
lib.copy(args, vmConfig, targetFile)
139141
logging.info("Copied original vm config to [%s]", targetFile)
140142
logging.info("Note: virtual machine config must be adjusted manually.")
143+
144+
145+
def apply_paths(config: bytes, restored_files: Dict[str, str]):
146+
tree = xml.asTree(config)
147+
os_config = tree.find("os")
148+
for flag, val in restored_files.items():
149+
os_config.find(flag).text = val
150+
return xml.ElementTree.tostring(tree, encoding="utf8", method="xml")

virtnbdrestore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ def main() -> None:
278278
logging.error("Disk restore failed: [%s]", errmsg)
279279
sys.exit(1)
280280

281-
files.restore(args, ConfigFile, virtClient)
281+
restConfig = files.restore(args, ConfigFile, virtClient, restConfig)
282282
vmconfig.restore(args, ConfigFile, restConfig, args.config_file)
283283
virtClient.refreshPool(args.output)
284284
if args.define is True:

0 commit comments

Comments
 (0)