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 |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 20 | from pathlib import Path |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 21 | |
| 22 | from wic import WicError |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 23 | from wic.pluginbase import SourcePlugin |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 24 | from wic.misc import get_bitbake_var, exec_native_cmd |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 25 | |
| 26 | logger = logging.getLogger('wic') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 27 | |
| 28 | class RootfsPlugin(SourcePlugin): |
| 29 | """ |
| 30 | Populate partition content from a rootfs directory. |
| 31 | """ |
| 32 | |
| 33 | name = 'rootfs' |
| 34 | |
| 35 | @staticmethod |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 36 | def __validate_path(cmd, rootfs_dir, path): |
| 37 | if os.path.isabs(path): |
| 38 | logger.error("%s: Must be relative: %s" % (cmd, orig_path)) |
| 39 | sys.exit(1) |
| 40 | |
| 41 | # Disallow climbing outside of parent directory using '..', |
| 42 | # because doing so could be quite disastrous (we will delete the |
| 43 | # directory, or modify a directory outside OpenEmbedded). |
| 44 | full_path = os.path.realpath(os.path.join(rootfs_dir, path)) |
| 45 | if not full_path.startswith(os.path.realpath(rootfs_dir)): |
| 46 | logger.error("%s: Must point inside the rootfs:" % (cmd, path)) |
| 47 | sys.exit(1) |
| 48 | |
| 49 | return full_path |
| 50 | |
| 51 | @staticmethod |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 52 | def __get_rootfs_dir(rootfs_dir): |
| 53 | if os.path.isdir(rootfs_dir): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 54 | return os.path.realpath(rootfs_dir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 55 | |
| 56 | image_rootfs_dir = get_bitbake_var("IMAGE_ROOTFS", rootfs_dir) |
| 57 | if not os.path.isdir(image_rootfs_dir): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 58 | raise WicError("No valid artifact IMAGE_ROOTFS from image " |
| 59 | "named %s has been found at %s, exiting." % |
| 60 | (rootfs_dir, image_rootfs_dir)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 61 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 62 | return os.path.realpath(image_rootfs_dir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 63 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 64 | @staticmethod |
| 65 | def __get_pseudo(native_sysroot, rootfs, pseudo_dir): |
| 66 | pseudo = "export PSEUDO_PREFIX=%s/usr;" % native_sysroot |
| 67 | pseudo += "export PSEUDO_LOCALSTATEDIR=%s;" % pseudo_dir |
| 68 | pseudo += "export PSEUDO_PASSWD=%s;" % rootfs |
| 69 | pseudo += "export PSEUDO_NOSYMLINKEXP=1;" |
| 70 | pseudo += "%s " % get_bitbake_var("FAKEROOTCMD") |
| 71 | return pseudo |
| 72 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 73 | @classmethod |
| 74 | def do_prepare_partition(cls, part, source_params, cr, cr_workdir, |
| 75 | oe_builddir, bootimg_dir, kernel_dir, |
| 76 | krootfs_dir, native_sysroot): |
| 77 | """ |
| 78 | Called to do the actual content population for a partition i.e. it |
| 79 | 'prepares' the partition to be incorporated into the image. |
| 80 | In this case, prepare content for legacy bios boot partition. |
| 81 | """ |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 82 | if part.rootfs_dir is None: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 83 | if not 'ROOTFS_DIR' in krootfs_dir: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 84 | raise WicError("Couldn't find --rootfs-dir, exiting") |
| 85 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 86 | rootfs_dir = krootfs_dir['ROOTFS_DIR'] |
| 87 | else: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 88 | if part.rootfs_dir in krootfs_dir: |
| 89 | rootfs_dir = krootfs_dir[part.rootfs_dir] |
| 90 | elif part.rootfs_dir: |
| 91 | rootfs_dir = part.rootfs_dir |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 92 | else: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 93 | raise WicError("Couldn't find --rootfs-dir=%s connection or " |
| 94 | "it is not a valid path, exiting" % part.rootfs_dir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 95 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 96 | part.rootfs_dir = cls.__get_rootfs_dir(rootfs_dir) |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 97 | part.has_fstab = os.path.exists(os.path.join(part.rootfs_dir, "etc/fstab")) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 98 | pseudo_dir = os.path.join(part.rootfs_dir, "../pseudo") |
| 99 | if not os.path.lexists(pseudo_dir): |
| 100 | logger.warn("%s folder does not exist. " |
| 101 | "Usernames and permissions will be invalid " % pseudo_dir) |
| 102 | pseudo_dir = None |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 103 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 104 | new_rootfs = None |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 105 | new_pseudo = None |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 106 | # Handle excluded paths. |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 107 | if part.exclude_path or part.include_path or part.change_directory or part.update_fstab_in_rootfs: |
| 108 | # We need a new rootfs directory we can safely modify without |
| 109 | # interfering with other tasks. Copy to workdir. |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 110 | 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] | 111 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 112 | if os.path.lexists(new_rootfs): |
| 113 | shutil.rmtree(os.path.join(new_rootfs)) |
| 114 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 115 | if part.change_directory: |
| 116 | cd = part.change_directory |
| 117 | if cd[-1] == '/': |
| 118 | cd = cd[:-1] |
| 119 | orig_dir = cls.__validate_path("--change-directory", part.rootfs_dir, cd) |
| 120 | else: |
| 121 | orig_dir = part.rootfs_dir |
| 122 | copyhardlinktree(orig_dir, new_rootfs) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 123 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 124 | # Convert the pseudo directory to its new location |
| 125 | if (pseudo_dir): |
| 126 | new_pseudo = os.path.realpath( |
| 127 | os.path.join(cr_workdir, "pseudo%d" % part.lineno)) |
| 128 | if os.path.lexists(new_pseudo): |
| 129 | shutil.rmtree(new_pseudo) |
| 130 | os.mkdir(new_pseudo) |
| 131 | shutil.copy(os.path.join(pseudo_dir, "files.db"), |
| 132 | os.path.join(new_pseudo, "files.db")) |
| 133 | |
| 134 | pseudo_cmd = "%s -B -m %s -M %s" % (cls.__get_pseudo(native_sysroot, |
| 135 | new_rootfs, |
| 136 | new_pseudo), |
| 137 | orig_dir, new_rootfs) |
| 138 | exec_native_cmd(pseudo_cmd, native_sysroot) |
| 139 | |
| 140 | for in_path in part.include_path or []: |
| 141 | #parse arguments |
| 142 | include_path = in_path[0] |
| 143 | if len(in_path) > 2: |
| 144 | logger.error("'Invalid number of arguments for include-path") |
| 145 | sys.exit(1) |
| 146 | if len(in_path) == 2: |
| 147 | path = in_path[1] |
| 148 | else: |
| 149 | path = None |
| 150 | |
| 151 | # Pack files to be included into a tar file. |
| 152 | # We need to create a tar file, because that way we can keep the |
| 153 | # permissions from the files even when they belong to different |
| 154 | # pseudo enviroments. |
| 155 | # If we simply copy files using copyhardlinktree/copytree... the |
| 156 | # copied files will belong to the user running wic. |
| 157 | tar_file = os.path.realpath( |
| 158 | os.path.join(cr_workdir, "include-path%d.tar" % part.lineno)) |
| 159 | if os.path.isfile(include_path): |
| 160 | parent = os.path.dirname(os.path.realpath(include_path)) |
| 161 | tar_cmd = "tar c --owner=root --group=root -f %s -C %s %s" % ( |
| 162 | tar_file, parent, os.path.relpath(include_path, parent)) |
| 163 | exec_native_cmd(tar_cmd, native_sysroot) |
| 164 | else: |
| 165 | if include_path in krootfs_dir: |
| 166 | include_path = krootfs_dir[include_path] |
| 167 | include_path = cls.__get_rootfs_dir(include_path) |
| 168 | include_pseudo = os.path.join(include_path, "../pseudo") |
| 169 | if os.path.lexists(include_pseudo): |
| 170 | pseudo = cls.__get_pseudo(native_sysroot, include_path, |
| 171 | include_pseudo) |
| 172 | tar_cmd = "tar cf %s -C %s ." % (tar_file, include_path) |
| 173 | else: |
| 174 | pseudo = None |
| 175 | tar_cmd = "tar c --owner=root --group=root -f %s -C %s ." % ( |
| 176 | tar_file, include_path) |
| 177 | exec_native_cmd(tar_cmd, native_sysroot, pseudo) |
| 178 | |
| 179 | #create destination |
| 180 | if path: |
| 181 | destination = cls.__validate_path("--include-path", new_rootfs, path) |
| 182 | Path(destination).mkdir(parents=True, exist_ok=True) |
| 183 | else: |
| 184 | destination = new_rootfs |
| 185 | |
| 186 | #extract destination |
| 187 | untar_cmd = "tar xf %s -C %s" % (tar_file, destination) |
| 188 | if new_pseudo: |
| 189 | pseudo = cls.__get_pseudo(native_sysroot, new_rootfs, new_pseudo) |
| 190 | else: |
| 191 | pseudo = None |
| 192 | exec_native_cmd(untar_cmd, native_sysroot, pseudo) |
| 193 | os.remove(tar_file) |
| 194 | |
| 195 | for orig_path in part.exclude_path or []: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 196 | path = orig_path |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 197 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 198 | full_path = cls.__validate_path("--exclude-path", new_rootfs, path) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 199 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 200 | if not os.path.lexists(full_path): |
| 201 | continue |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 202 | |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 203 | if new_pseudo: |
| 204 | pseudo = cls.__get_pseudo(native_sysroot, new_rootfs, new_pseudo) |
| 205 | else: |
| 206 | pseudo = None |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 207 | if path.endswith(os.sep): |
| 208 | # Delete content only. |
| 209 | for entry in os.listdir(full_path): |
| 210 | full_entry = os.path.join(full_path, entry) |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 211 | rm_cmd = "rm -rf %s" % (full_entry) |
| 212 | exec_native_cmd(rm_cmd, native_sysroot, pseudo) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 213 | else: |
| 214 | # Delete whole directory. |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 215 | rm_cmd = "rm -rf %s" % (full_path) |
| 216 | exec_native_cmd(rm_cmd, native_sysroot, pseudo) |
| 217 | |
| 218 | # Update part.has_fstab here as fstab may have been added or |
| 219 | # removed by the above modifications. |
| 220 | part.has_fstab = os.path.exists(os.path.join(new_rootfs, "etc/fstab")) |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame^] | 221 | if part.update_fstab_in_rootfs and part.has_fstab and not part.no_fstab_update: |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 222 | fstab_path = os.path.join(new_rootfs, "etc/fstab") |
| 223 | # Assume that fstab should always be owned by root with fixed permissions |
| 224 | install_cmd = "install -m 0644 %s %s" % (part.updated_fstab_path, fstab_path) |
| 225 | if new_pseudo: |
| 226 | pseudo = cls.__get_pseudo(native_sysroot, new_rootfs, new_pseudo) |
| 227 | else: |
| 228 | pseudo = None |
| 229 | exec_native_cmd(install_cmd, native_sysroot, pseudo) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 230 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 231 | part.prepare_rootfs(cr_workdir, oe_builddir, |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 232 | new_rootfs or part.rootfs_dir, native_sysroot, |
| 233 | pseudo_dir = new_pseudo or pseudo_dir) |