|
7 | 7 | import os
|
8 | 8 | import platform
|
9 | 9 | import shutil
|
| 10 | +import subprocess |
10 | 11 | import sys
|
11 | 12 | import tempfile
|
12 | 13 | from pathlib import Path
|
|
18 | 19 | from mkosi.log import log_notice, log_setup
|
19 | 20 | from mkosi.run import find_binary, run, uncaught_exception_handler
|
20 | 21 | from mkosi.sandbox import __version__, umask
|
21 |
| -from mkosi.tree import copy_tree |
| 22 | +from mkosi.tree import copy_tree, rmtree |
22 | 23 | from mkosi.util import PathString, mandatory_variable, resource_path
|
23 | 24 |
|
24 | 25 |
|
@@ -141,12 +142,32 @@ def initrd_finalize(staging_dir: str, output: str, output_dir: str) -> None:
|
141 | 142 | else:
|
142 | 143 | output_dir = os.fspath(Path.cwd())
|
143 | 144 |
|
| 145 | + # backup current image |
| 146 | + if Path(f"{output_dir}/{output}").exists(): |
| 147 | + log_notice(f"Backup {output_dir}/{output} to {staging_dir}/{output}.bak") |
| 148 | + copy_tree( |
| 149 | + Path(f"{output_dir}/{output}"), |
| 150 | + Path(f"{staging_dir}/{output}.bak"), |
| 151 | + ) |
| 152 | + |
144 | 153 | log_notice(f"Copying {staging_dir}/{output} to {output_dir}/{output}")
|
145 |
| - # mkosi symlinks the expected output image, so dereference it |
146 |
| - copy_tree( |
147 |
| - Path(f"{staging_dir}/{output}").resolve(), |
148 |
| - Path(f"{output_dir}/{output}"), |
149 |
| - ) |
| 154 | + try: |
| 155 | + # mkosi symlinks the expected output image, so dereference it |
| 156 | + copy_tree( |
| 157 | + Path(f"{staging_dir}/{output}").resolve(), |
| 158 | + Path(f"{output_dir}/{output}"), |
| 159 | + ) |
| 160 | + except subprocess.CalledProcessError: |
| 161 | + # restore image backup on error |
| 162 | + if Path(f"{staging_dir}/{output}.bak").exists(): |
| 163 | + log_notice(f"Removing incomplete {output_dir}/{output}") |
| 164 | + rmtree(Path(f"{output_dir}/{output}")) |
| 165 | + log_notice(f"Restoring backup {staging_dir}/{output}.bak to {output_dir}/{output}") |
| 166 | + copy_tree( |
| 167 | + Path(f"{staging_dir}/{output}.bak"), |
| 168 | + Path(f"{output_dir}/{output}"), |
| 169 | + ) |
| 170 | + raise |
150 | 171 |
|
151 | 172 |
|
152 | 173 | def initrd_common_args(parser: argparse.ArgumentParser) -> None:
|
|
0 commit comments