Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | # |
| 2 | # Copyright (c) 2014, Intel Corporation. |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 3 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame^] | 4 | # SPDX-License-Identifier: GPL-2.0-only |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 5 | # |
| 6 | # DESCRIPTION |
| 7 | # This implements the 'rootfs' source plugin class for 'wic' |
| 8 | # |
| 9 | # AUTHORS |
| 10 | # Tom Zanussi <tom.zanussi (at] linux.intel.com> |
| 11 | # Joao Henrique Ferreira de Freitas <joaohf (at] gmail.com> |
| 12 | # |
| 13 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 14 | import logging |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 15 | import os |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 16 | import shutil |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 17 | import sys |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 18 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 19 | from oe.path import copyhardlinktree |
| 20 | |
| 21 | from wic import WicError |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 22 | from wic.pluginbase import SourcePlugin |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 23 | from wic.misc import get_bitbake_var |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 24 | |
| 25 | logger = logging.getLogger('wic') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 26 | |
| 27 | class RootfsPlugin(SourcePlugin): |
| 28 | """ |
| 29 | Populate partition content from a rootfs directory. |
| 30 | """ |
| 31 | |
| 32 | name = 'rootfs' |
| 33 | |
| 34 | @staticmethod |
| 35 | def __get_rootfs_dir(rootfs_dir): |
| 36 | if os.path.isdir(rootfs_dir): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 37 | return os.path.realpath(rootfs_dir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 38 | |
| 39 | image_rootfs_dir = get_bitbake_var("IMAGE_ROOTFS", rootfs_dir) |
| 40 | if not os.path.isdir(image_rootfs_dir): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 41 | raise WicError("No valid artifact IMAGE_ROOTFS from image " |
| 42 | "named %s has been found at %s, exiting." % |
| 43 | (rootfs_dir, image_rootfs_dir)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 44 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 45 | return os.path.realpath(image_rootfs_dir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 46 | |
| 47 | @classmethod |
| 48 | def do_prepare_partition(cls, part, source_params, cr, cr_workdir, |
| 49 | oe_builddir, bootimg_dir, kernel_dir, |
| 50 | krootfs_dir, native_sysroot): |
| 51 | """ |
| 52 | Called to do the actual content population for a partition i.e. it |
| 53 | 'prepares' the partition to be incorporated into the image. |
| 54 | In this case, prepare content for legacy bios boot partition. |
| 55 | """ |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 56 | if part.rootfs_dir is None: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 57 | if not 'ROOTFS_DIR' in krootfs_dir: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 58 | raise WicError("Couldn't find --rootfs-dir, exiting") |
| 59 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 60 | rootfs_dir = krootfs_dir['ROOTFS_DIR'] |
| 61 | else: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 62 | if part.rootfs_dir in krootfs_dir: |
| 63 | rootfs_dir = krootfs_dir[part.rootfs_dir] |
| 64 | elif part.rootfs_dir: |
| 65 | rootfs_dir = part.rootfs_dir |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 66 | else: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 67 | raise WicError("Couldn't find --rootfs-dir=%s connection or " |
| 68 | "it is not a valid path, exiting" % part.rootfs_dir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 69 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 70 | part.rootfs_dir = cls.__get_rootfs_dir(rootfs_dir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 71 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 72 | new_rootfs = None |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 73 | # Handle excluded paths. |
| 74 | if part.exclude_path is not None: |
| 75 | # We need a new rootfs directory we can delete files from. Copy to |
| 76 | # workdir. |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 77 | new_rootfs = os.path.realpath(os.path.join(cr_workdir, "rootfs%d" % part.lineno)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 78 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 79 | if os.path.lexists(new_rootfs): |
| 80 | shutil.rmtree(os.path.join(new_rootfs)) |
| 81 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 82 | copyhardlinktree(part.rootfs_dir, new_rootfs) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 83 | |
| 84 | for orig_path in part.exclude_path: |
| 85 | path = orig_path |
| 86 | if os.path.isabs(path): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 87 | logger.error("Must be relative: --exclude-path=%s" % orig_path) |
| 88 | sys.exit(1) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 89 | |
| 90 | full_path = os.path.realpath(os.path.join(new_rootfs, path)) |
| 91 | |
| 92 | # Disallow climbing outside of parent directory using '..', |
| 93 | # because doing so could be quite disastrous (we will delete the |
| 94 | # directory). |
| 95 | if not full_path.startswith(new_rootfs): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 96 | logger.error("'%s' points to a path outside the rootfs" % orig_path) |
| 97 | sys.exit(1) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 98 | |
| 99 | if path.endswith(os.sep): |
| 100 | # Delete content only. |
| 101 | for entry in os.listdir(full_path): |
| 102 | full_entry = os.path.join(full_path, entry) |
| 103 | if os.path.isdir(full_entry) and not os.path.islink(full_entry): |
| 104 | shutil.rmtree(full_entry) |
| 105 | else: |
| 106 | os.remove(full_entry) |
| 107 | else: |
| 108 | # Delete whole directory. |
| 109 | shutil.rmtree(full_path) |
| 110 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 111 | part.prepare_rootfs(cr_workdir, oe_builddir, |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 112 | new_rootfs or part.rootfs_dir, native_sysroot) |