diff options
Diffstat (limited to 'scripts/lib/wic/engine.py')
-rw-r--r-- | scripts/lib/wic/engine.py | 63 |
1 files changed, 49 insertions, 14 deletions
diff --git a/scripts/lib/wic/engine.py b/scripts/lib/wic/engine.py index b9e60cbe4e..9d596be3a7 100644 --- a/scripts/lib/wic/engine.py +++ b/scripts/lib/wic/engine.py | |||
@@ -345,29 +345,64 @@ class Disk: | |||
345 | path)) | 345 | path)) |
346 | 346 | ||
347 | def copy(self, src, dest): | 347 | def copy(self, src, dest): |
348 | """Copy partition image into wic image.""" | 348 | """Copy files or directories to/from the vfat or ext* partition.""" |
349 | pnum = dest.part if isinstance(src, str) else src.part | 349 | pnum = dest.part if isinstance(src, str) else src.part |
350 | partimg = self._get_part_image(pnum) | ||
350 | 351 | ||
351 | if self.partitions[pnum].fstype.startswith('ext'): | 352 | if self.partitions[pnum].fstype.startswith('ext'): |
352 | if isinstance(src, str): | 353 | if isinstance(src, str): # host to image case |
353 | cmd = "printf 'cd {}\nwrite {} {}\n' | {} -w {}".\ | 354 | if os.path.isdir(src): |
354 | format(os.path.dirname(dest.path), src, os.path.basename(src), | 355 | base = os.path.abspath(src) |
355 | self.debugfs, self._get_part_image(pnum)) | 356 | base_parent = os.path.dirname(base) |
356 | else: # copy from wic | 357 | cmds = [] |
357 | # run both dump and rdump to support both files and directory | 358 | made = set() |
359 | |||
360 | for root, dirs, files in os.walk(base): | ||
361 | for fname in files: | ||
362 | host_file = os.path.join(root, fname) | ||
363 | rel = os.path.relpath(host_file, base_parent) | ||
364 | dest_file = os.path.join(dest.path, rel) | ||
365 | dest_dir = os.path.dirname(dest_file) | ||
366 | |||
367 | # create dir structure (mkdir -p) | ||
368 | parts = dest_dir.strip('/').split('/') | ||
369 | cur = '' | ||
370 | for p in parts: | ||
371 | cur = cur + '/' + p | ||
372 | if cur not in made: | ||
373 | cmds.append(f'mkdir "{cur}"') | ||
374 | made.add(cur) | ||
375 | |||
376 | cmds.append(f'write "{host_file}" "{dest_file}"') | ||
377 | |||
378 | # write script to a temp file | ||
379 | with tempfile.NamedTemporaryFile(mode='w', delete=False, | ||
380 | prefix='wic-debugfs-') as tf: | ||
381 | for line in cmds: | ||
382 | tf.write(line + '\n') | ||
383 | scriptname = tf.name | ||
384 | |||
385 | cmd = f"{self.debugfs} -w -f {scriptname} {partimg}" | ||
386 | |||
387 | else: # single file | ||
388 | cmd = "printf 'cd {}\nwrite {} {}\n' | {} -w {}".\ | ||
389 | format(os.path.dirname(dest.path), src, | ||
390 | os.path.basename(src), self.debugfs, partimg) | ||
391 | |||
392 | else: # image to host case | ||
358 | cmd = "printf 'cd {}\ndump /{} {}\nrdump /{} {}\n' | {} {}".\ | 393 | cmd = "printf 'cd {}\ndump /{} {}\nrdump /{} {}\n' | {} {}".\ |
359 | format(os.path.dirname(src.path), src.path, | 394 | format(os.path.dirname(src.path), src.path, |
360 | dest, src.path, dest, self.debugfs, | 395 | dest, src.path, dest, self.debugfs, partimg) |
361 | self._get_part_image(pnum)) | 396 | |
362 | else: # fat | 397 | else: # fat |
363 | if isinstance(src, str): | 398 | if isinstance(src, str): |
364 | cmd = "{} -i {} -snop {} ::{}".format(self.mcopy, | 399 | cmd = "{} -i {} -snop {} ::{}".format(self.mcopy, |
365 | self._get_part_image(pnum), | 400 | partimg, |
366 | src, dest.path) | 401 | src, dest.path) |
367 | else: | 402 | else: |
368 | cmd = "{} -i {} -snop ::{} {}".format(self.mcopy, | 403 | cmd = "{} -i {} -snop ::{} {}".format(self.mcopy, |
369 | self._get_part_image(pnum), | 404 | partimg, |
370 | src.path, dest) | 405 | src.path, dest) |
371 | 406 | ||
372 | exec_cmd(cmd, as_shell=True) | 407 | exec_cmd(cmd, as_shell=True) |
373 | self._put_part_image(pnum) | 408 | self._put_part_image(pnum) |