blob: e29f3a4c2f0901ff081993ff82fdf176e6c34307 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#
2# Copyright (c) 2014, Intel Corporation.
Patrick Williamsc124f4f2015-09-15 14:41:29 -05003#
Brad Bishopc342db32019-05-15 21:57:59 -04004# SPDX-License-Identifier: GPL-2.0-only
Patrick Williamsc124f4f2015-09-15 14:41:29 -05005#
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 Bishop6e60e8b2018-02-01 10:27:11 -050014import logging
Patrick Williamsc124f4f2015-09-15 14:41:29 -050015import os
Brad Bishop6e60e8b2018-02-01 10:27:11 -050016import shutil
Brad Bishopd7bf8c12018-02-25 22:55:05 -050017import sys
Patrick Williamsc124f4f2015-09-15 14:41:29 -050018
Brad Bishop6e60e8b2018-02-01 10:27:11 -050019from oe.path import copyhardlinktree
Andrew Geissler82c905d2020-04-13 13:39:40 -050020from pathlib import Path
Brad Bishop6e60e8b2018-02-01 10:27:11 -050021
22from wic import WicError
Patrick Williamsc124f4f2015-09-15 14:41:29 -050023from wic.pluginbase import SourcePlugin
Andrew Geissler82c905d2020-04-13 13:39:40 -050024from wic.misc import get_bitbake_var, exec_native_cmd
Brad Bishop6e60e8b2018-02-01 10:27:11 -050025
26logger = logging.getLogger('wic')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050027
28class RootfsPlugin(SourcePlugin):
29 """
30 Populate partition content from a rootfs directory.
31 """
32
33 name = 'rootfs'
34
35 @staticmethod
Andrew Geissler82c905d2020-04-13 13:39:40 -050036 def __validate_path(cmd, rootfs_dir, path):
37 if os.path.isabs(path):
Patrick Williamsdb4c27e2022-08-05 08:10:29 -050038 logger.error("%s: Must be relative: %s" % (cmd, path))
Andrew Geissler82c905d2020-04-13 13:39:40 -050039 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 Williamsc124f4f2015-09-15 14:41:29 -050052 def __get_rootfs_dir(rootfs_dir):
Andrew Geisslerd5838332022-05-27 11:33:10 -050053 if rootfs_dir and os.path.isdir(rootfs_dir):
Brad Bishopd7bf8c12018-02-25 22:55:05 -050054 return os.path.realpath(rootfs_dir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050055
56 image_rootfs_dir = get_bitbake_var("IMAGE_ROOTFS", rootfs_dir)
57 if not os.path.isdir(image_rootfs_dir):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050058 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 Williamsc124f4f2015-09-15 14:41:29 -050061
Brad Bishopd7bf8c12018-02-25 22:55:05 -050062 return os.path.realpath(image_rootfs_dir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050063
Andrew Geissler82c905d2020-04-13 13:39:40 -050064 @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 Williamsc124f4f2015-09-15 14:41:29 -050073 @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 Williamsd8c66bc2016-06-20 12:57:21 -050082 if part.rootfs_dir is None:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050083 if not 'ROOTFS_DIR' in krootfs_dir:
Brad Bishop6e60e8b2018-02-01 10:27:11 -050084 raise WicError("Couldn't find --rootfs-dir, exiting")
85
Patrick Williamsc124f4f2015-09-15 14:41:29 -050086 rootfs_dir = krootfs_dir['ROOTFS_DIR']
87 else:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050088 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 Williamsc124f4f2015-09-15 14:41:29 -050092 else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -050093 raise WicError("Couldn't find --rootfs-dir=%s connection or "
94 "it is not a valid path, exiting" % part.rootfs_dir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050095
Brad Bishopd7bf8c12018-02-25 22:55:05 -050096 part.rootfs_dir = cls.__get_rootfs_dir(rootfs_dir)
Andrew Geisslerd1e89492021-02-12 15:35:20 -060097 part.has_fstab = os.path.exists(os.path.join(part.rootfs_dir, "etc/fstab"))
Andrew Geissler82c905d2020-04-13 13:39:40 -050098 pseudo_dir = os.path.join(part.rootfs_dir, "../pseudo")
99 if not os.path.lexists(pseudo_dir):
Andrew Geisslerd5838332022-05-27 11:33:10 -0500100 pseudo_dir = os.path.join(cls.__get_rootfs_dir(None), '../pseudo')
101
102 if not os.path.lexists(pseudo_dir):
Andrew Geissler82c905d2020-04-13 13:39:40 -0500103 logger.warn("%s folder does not exist. "
104 "Usernames and permissions will be invalid " % pseudo_dir)
105 pseudo_dir = None
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500106
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500107 new_rootfs = None
Andrew Geissler82c905d2020-04-13 13:39:40 -0500108 new_pseudo = None
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500109 # Handle excluded paths.
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600110 if part.exclude_path or part.include_path or part.change_directory or part.update_fstab_in_rootfs:
111 # We need a new rootfs directory we can safely modify without
112 # interfering with other tasks. Copy to workdir.
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500113 new_rootfs = os.path.realpath(os.path.join(cr_workdir, "rootfs%d" % part.lineno))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500114
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500115 if os.path.lexists(new_rootfs):
116 shutil.rmtree(os.path.join(new_rootfs))
117
Andrew Geissler82c905d2020-04-13 13:39:40 -0500118 if part.change_directory:
119 cd = part.change_directory
120 if cd[-1] == '/':
121 cd = cd[:-1]
122 orig_dir = cls.__validate_path("--change-directory", part.rootfs_dir, cd)
123 else:
124 orig_dir = part.rootfs_dir
125 copyhardlinktree(orig_dir, new_rootfs)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500126
Andrew Geissler82c905d2020-04-13 13:39:40 -0500127 # Convert the pseudo directory to its new location
128 if (pseudo_dir):
129 new_pseudo = os.path.realpath(
130 os.path.join(cr_workdir, "pseudo%d" % part.lineno))
131 if os.path.lexists(new_pseudo):
132 shutil.rmtree(new_pseudo)
133 os.mkdir(new_pseudo)
134 shutil.copy(os.path.join(pseudo_dir, "files.db"),
135 os.path.join(new_pseudo, "files.db"))
136
137 pseudo_cmd = "%s -B -m %s -M %s" % (cls.__get_pseudo(native_sysroot,
138 new_rootfs,
139 new_pseudo),
140 orig_dir, new_rootfs)
141 exec_native_cmd(pseudo_cmd, native_sysroot)
142
143 for in_path in part.include_path or []:
144 #parse arguments
145 include_path = in_path[0]
146 if len(in_path) > 2:
147 logger.error("'Invalid number of arguments for include-path")
148 sys.exit(1)
149 if len(in_path) == 2:
150 path = in_path[1]
151 else:
152 path = None
153
154 # Pack files to be included into a tar file.
155 # We need to create a tar file, because that way we can keep the
156 # permissions from the files even when they belong to different
157 # pseudo enviroments.
158 # If we simply copy files using copyhardlinktree/copytree... the
159 # copied files will belong to the user running wic.
160 tar_file = os.path.realpath(
161 os.path.join(cr_workdir, "include-path%d.tar" % part.lineno))
162 if os.path.isfile(include_path):
163 parent = os.path.dirname(os.path.realpath(include_path))
164 tar_cmd = "tar c --owner=root --group=root -f %s -C %s %s" % (
165 tar_file, parent, os.path.relpath(include_path, parent))
166 exec_native_cmd(tar_cmd, native_sysroot)
167 else:
168 if include_path in krootfs_dir:
169 include_path = krootfs_dir[include_path]
170 include_path = cls.__get_rootfs_dir(include_path)
171 include_pseudo = os.path.join(include_path, "../pseudo")
172 if os.path.lexists(include_pseudo):
173 pseudo = cls.__get_pseudo(native_sysroot, include_path,
174 include_pseudo)
175 tar_cmd = "tar cf %s -C %s ." % (tar_file, include_path)
176 else:
177 pseudo = None
178 tar_cmd = "tar c --owner=root --group=root -f %s -C %s ." % (
179 tar_file, include_path)
180 exec_native_cmd(tar_cmd, native_sysroot, pseudo)
181
182 #create destination
183 if path:
184 destination = cls.__validate_path("--include-path", new_rootfs, path)
185 Path(destination).mkdir(parents=True, exist_ok=True)
186 else:
187 destination = new_rootfs
188
189 #extract destination
190 untar_cmd = "tar xf %s -C %s" % (tar_file, destination)
191 if new_pseudo:
192 pseudo = cls.__get_pseudo(native_sysroot, new_rootfs, new_pseudo)
193 else:
194 pseudo = None
195 exec_native_cmd(untar_cmd, native_sysroot, pseudo)
196 os.remove(tar_file)
197
198 for orig_path in part.exclude_path or []:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500199 path = orig_path
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500200
Andrew Geissler82c905d2020-04-13 13:39:40 -0500201 full_path = cls.__validate_path("--exclude-path", new_rootfs, path)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500202
Andrew Geissler82c905d2020-04-13 13:39:40 -0500203 if not os.path.lexists(full_path):
204 continue
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500205
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600206 if new_pseudo:
207 pseudo = cls.__get_pseudo(native_sysroot, new_rootfs, new_pseudo)
208 else:
209 pseudo = None
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500210 if path.endswith(os.sep):
211 # Delete content only.
212 for entry in os.listdir(full_path):
213 full_entry = os.path.join(full_path, entry)
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600214 rm_cmd = "rm -rf %s" % (full_entry)
215 exec_native_cmd(rm_cmd, native_sysroot, pseudo)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500216 else:
217 # Delete whole directory.
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600218 rm_cmd = "rm -rf %s" % (full_path)
219 exec_native_cmd(rm_cmd, native_sysroot, pseudo)
220
221 # Update part.has_fstab here as fstab may have been added or
222 # removed by the above modifications.
223 part.has_fstab = os.path.exists(os.path.join(new_rootfs, "etc/fstab"))
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500224 if part.update_fstab_in_rootfs and part.has_fstab and not part.no_fstab_update:
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600225 fstab_path = os.path.join(new_rootfs, "etc/fstab")
226 # Assume that fstab should always be owned by root with fixed permissions
Patrick Williams2390b1b2022-11-03 13:47:49 -0500227 install_cmd = "install -m 0644 -p %s %s" % (part.updated_fstab_path, fstab_path)
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600228 if new_pseudo:
229 pseudo = cls.__get_pseudo(native_sysroot, new_rootfs, new_pseudo)
230 else:
231 pseudo = None
232 exec_native_cmd(install_cmd, native_sysroot, pseudo)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500233
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500234 part.prepare_rootfs(cr_workdir, oe_builddir,
Andrew Geissler82c905d2020-04-13 13:39:40 -0500235 new_rootfs or part.rootfs_dir, native_sysroot,
236 pseudo_dir = new_pseudo or pseudo_dir)