diff options
-rw-r--r-- | meta/lib/oe/rootfs.py | 328 |
1 files changed, 328 insertions, 0 deletions
diff --git a/meta/lib/oe/rootfs.py b/meta/lib/oe/rootfs.py new file mode 100644 index 0000000000..abdb0dd18a --- /dev/null +++ b/meta/lib/oe/rootfs.py | |||
@@ -0,0 +1,328 @@ | |||
1 | from abc import ABCMeta, abstractmethod | ||
2 | from oe.utils import execute_pre_post_process | ||
3 | from oe.utils import contains as base_contains | ||
4 | from oe.package_manager import * | ||
5 | from oe.manifest import * | ||
6 | import shutil | ||
7 | import os | ||
8 | import subprocess | ||
9 | import re | ||
10 | |||
11 | |||
12 | class Rootfs(object): | ||
13 | """ | ||
14 | This is an abstract class. Do not instantiate this directly. | ||
15 | """ | ||
16 | __metaclass__ = ABCMeta | ||
17 | |||
18 | def __init__(self, d, manifest_dir=None): | ||
19 | self.d = d | ||
20 | self.pm = None | ||
21 | self.manifest = None | ||
22 | self.image_rootfs = self.d.getVar('IMAGE_ROOTFS', True) | ||
23 | self.deploy_dir_image = self.d.getVar('DEPLOY_DIR_IMAGE', True) | ||
24 | |||
25 | bb.utils.remove(self.image_rootfs, True) | ||
26 | bb.utils.remove(self.d.getVar('MULTILIB_TEMP_ROOTFS', True), True) | ||
27 | |||
28 | self.install_order = ["lgp", "mip", "aop", "mlp"] | ||
29 | |||
30 | @abstractmethod | ||
31 | def _create(self): | ||
32 | pass | ||
33 | |||
34 | @abstractmethod | ||
35 | def _get_delayed_postinsts(self): | ||
36 | pass | ||
37 | |||
38 | @abstractmethod | ||
39 | def _save_postinsts(self): | ||
40 | pass | ||
41 | |||
42 | @abstractmethod | ||
43 | def _log_check(self): | ||
44 | pass | ||
45 | |||
46 | @abstractmethod | ||
47 | def _insert_feed_uris(self): | ||
48 | pass | ||
49 | |||
50 | @abstractmethod | ||
51 | def _handle_intercept_failure(self, failed_script): | ||
52 | pass | ||
53 | |||
54 | def _exec_shell_cmd(self, cmd): | ||
55 | fakerootcmd = self.d.getVar('FAKEROOT', True) | ||
56 | if fakerootcmd is not None: | ||
57 | exec_cmd = [fakerootcmd, cmd] | ||
58 | else: | ||
59 | exec_cmd = cmd | ||
60 | |||
61 | try: | ||
62 | subprocess.check_output(exec_cmd) | ||
63 | except subprocess.CalledProcessError as e: | ||
64 | return("Command %s returned %d!" % (e.cmd, e.returncode)) | ||
65 | |||
66 | return None | ||
67 | |||
68 | def create(self): | ||
69 | bb.note("###### Generate rootfs #######") | ||
70 | pre_process_cmds = self.d.getVar("ROOTFS_PREPROCESS_COMMAND", True) | ||
71 | post_process_cmds = self.d.getVar("ROOTFS_POSTPROCESS_COMMAND", True) | ||
72 | |||
73 | intercepts_dir = os.path.join(self.d.getVar('WORKDIR', True), | ||
74 | "intercept_scripts") | ||
75 | |||
76 | bb.utils.mkdirhier(self.image_rootfs) | ||
77 | |||
78 | bb.utils.mkdirhier(self.deploy_dir_image) | ||
79 | |||
80 | shutil.copytree(self.d.expand("${COREBASE}/scripts/postinst-intercepts"), | ||
81 | intercepts_dir) | ||
82 | |||
83 | shutil.copy(self.d.expand("${COREBASE}/meta/files/deploydir_readme.txt"), | ||
84 | self.deploy_dir_image + | ||
85 | "/README_-_DO_NOT_DELETE_FILES_IN_THIS_DIRECTORY.txt") | ||
86 | |||
87 | execute_pre_post_process(self.d, pre_process_cmds) | ||
88 | |||
89 | # call the package manager dependent create method | ||
90 | self._create() | ||
91 | |||
92 | sysconfdir = self.image_rootfs + self.d.getVar('sysconfdir', True) | ||
93 | bb.utils.mkdirhier(sysconfdir) | ||
94 | with open(sysconfdir + "/version", "w+") as ver: | ||
95 | ver.write(self.d.getVar('BUILDNAME', True) + "\n") | ||
96 | |||
97 | self._run_intercepts() | ||
98 | |||
99 | execute_pre_post_process(self.d, post_process_cmds) | ||
100 | |||
101 | if base_contains("IMAGE_FEATURES", "read-only-rootfs", | ||
102 | True, False, self.d): | ||
103 | delayed_postinsts = self._get_delayed_postinsts() | ||
104 | if delayed_postinsts is not None: | ||
105 | bb.fatal("The following packages could not be configured" | ||
106 | "offline and rootfs is read-only: %s" % | ||
107 | delayed_postinsts) | ||
108 | |||
109 | self._create_devfs() | ||
110 | |||
111 | self._uninstall_uneeded() | ||
112 | |||
113 | self._insert_feed_uris() | ||
114 | |||
115 | self._run_ldconfig() | ||
116 | |||
117 | self._generate_kernel_module_deps() | ||
118 | |||
119 | def _uninstall_uneeded(self): | ||
120 | if base_contains("IMAGE_FEATURES", "package-management", | ||
121 | True, False, self.d): | ||
122 | return | ||
123 | |||
124 | delayed_postinsts = self._get_delayed_postinsts() | ||
125 | if delayed_postinsts is None: | ||
126 | self.pm.remove(["update-rc.d", | ||
127 | "base-passwd", | ||
128 | self.d.getVar("ROOTFS_BOOTSTRAP_INSTALL", True)], | ||
129 | False) | ||
130 | |||
131 | if os.path.exists(self.d.expand("${IMAGE_ROOTFS}${sysconfdir}/init.d/run-postinsts")): | ||
132 | self._exec_shell_cmd(["update-rc.d", "-f", "-r", | ||
133 | self.d.getVar('IMAGE_ROOTFS', True), | ||
134 | "run-postinsts", "remove"]) | ||
135 | else: | ||
136 | self._save_postinsts() | ||
137 | |||
138 | self.pm.remove_packaging_data() | ||
139 | |||
140 | def _run_intercepts(self): | ||
141 | intercepts_dir = os.path.join(self.d.getVar('WORKDIR', True), | ||
142 | "intercept_scripts") | ||
143 | |||
144 | bb.note("Running intercept scripts:") | ||
145 | for script in os.listdir(intercepts_dir): | ||
146 | script_full = os.path.join(intercepts_dir, script) | ||
147 | |||
148 | if script == "postinst_intercept" or not os.access(script_full, os.X_OK): | ||
149 | continue | ||
150 | |||
151 | bb.note("> Executing %s intercept ..." % script) | ||
152 | |||
153 | try: | ||
154 | subprocess.check_output(script_full) | ||
155 | except subprocess.CalledProcessError as e: | ||
156 | bb.note("WARNING: intercept script '%s' failed with %d!" % | ||
157 | (script, e.returncode)) | ||
158 | |||
159 | with open(script_full) as intercept: | ||
160 | registered_pkgs = None | ||
161 | for line in intercept.read().split("\n"): | ||
162 | m = re.match("^##PKGS:(.*)", line) | ||
163 | if m is not None: | ||
164 | registered_pkgs = m.group(1).strip() | ||
165 | break | ||
166 | |||
167 | if registered_pkgs is not None: | ||
168 | bb.note("The postinstalls for the following packages " | ||
169 | "will be postponed for first boot: %s" % | ||
170 | registered_pkgs) | ||
171 | |||
172 | # call the backend dependent handler | ||
173 | self._handle_intercept_failure(registered_pkgs) | ||
174 | |||
175 | def _run_ldconfig(self): | ||
176 | if self.d.getVar('LDCONFIGDEPEND', True) is not None: | ||
177 | bb.note("Executing: ldconfig -r" + self.image_rootfs + "-c new -v") | ||
178 | self._exec_shell_cmd(['ldconfig', '-r', self.image_rootfs, '-c', | ||
179 | 'new', '-v']) | ||
180 | |||
181 | def _generate_kernel_module_deps(self): | ||
182 | kernel_abi_ver_file = os.path.join(self.d.getVar('STAGING_KERNEL_DIR', True), | ||
183 | 'kernel-abiversion') | ||
184 | if os.path.exists(kernel_abi_ver_file): | ||
185 | kernel_ver = open(kernel_abi_ver_file).read() | ||
186 | modules_dir = os.path.join(self.image_rootfs, 'lib', 'modules', kernel_ver) | ||
187 | |||
188 | bb.utils.mkdirhier(modules_dir) | ||
189 | |||
190 | self._exec_shell_cmd(['depmodwrapper', '-a', '-b', self.image_rootfs, | ||
191 | kernel_ver]) | ||
192 | |||
193 | """ | ||
194 | Create devfs: | ||
195 | * IMAGE_DEVICE_TABLE is the old name to an absolute path to a device table file | ||
196 | * IMAGE_DEVICE_TABLES is a new name for a file, or list of files, seached | ||
197 | for in the BBPATH | ||
198 | If neither are specified then the default name of files/device_table-minimal.txt | ||
199 | is searched for in the BBPATH (same as the old version.) | ||
200 | """ | ||
201 | def _create_devfs(self): | ||
202 | devtable_list = [] | ||
203 | devtable = self.d.getVar('IMAGE_DEVICE_TABLE', True) | ||
204 | if devtable is not None: | ||
205 | devtable_list.append(devtable) | ||
206 | else: | ||
207 | devtables = self.d.getVar('IMAGE_DEVICE_TABLES', True) | ||
208 | if devtables is None: | ||
209 | devtables = 'files/device_table-minimal.txt' | ||
210 | for devtable in devtables.split(): | ||
211 | devtable_list.append("%s" % bb.utils.which(self.d.getVar('BBPATH', True), devtable)) | ||
212 | |||
213 | for devtable in devtable_list: | ||
214 | self._exec_shell_cmd(["makedevs", "-r", | ||
215 | self.image_rootfs, "-D", devtable]) | ||
216 | |||
217 | |||
218 | class RpmRootfs(Rootfs): | ||
219 | def __init__(self, manifest): | ||
220 | super(RpmRootfs, self).__init__(manifest) | ||
221 | """ | ||
222 | TBD | ||
223 | """ | ||
224 | |||
225 | |||
226 | class DpkgRootfs(Rootfs): | ||
227 | def __init__(self, d, manifest_dir): | ||
228 | super(DpkgRootfs, self).__init__(d, manifest_dir) | ||
229 | |||
230 | self.manifest = DpkgManifest(d, manifest_dir) | ||
231 | self.pm = DpkgPM(d, d.getVar('IMAGE_ROOTFS', True), | ||
232 | d.getVar('PACKAGE_ARCHS', True), | ||
233 | d.getVar('DPKG_ARCH', True)) | ||
234 | |||
235 | def _create(self): | ||
236 | pkgs_to_install = self.manifest.parse_initial_manifest() | ||
237 | |||
238 | alt_dir = self.d.expand("${IMAGE_ROOTFS}/var/lib/dpkg/alternatives") | ||
239 | bb.utils.mkdirhier(alt_dir) | ||
240 | |||
241 | # update PM index files | ||
242 | self.pm.write_index() | ||
243 | |||
244 | self.pm.update() | ||
245 | |||
246 | for pkg_type in self.install_order: | ||
247 | if pkg_type in pkgs_to_install: | ||
248 | self.pm.install(pkgs_to_install[pkg_type], | ||
249 | [False, True][pkg_type == "aop"]) | ||
250 | |||
251 | self.pm.install_complementary() | ||
252 | |||
253 | self.pm.fix_broken_dependencies() | ||
254 | |||
255 | self.pm.mark_packages("installed") | ||
256 | |||
257 | self.pm.run_pre_post_installs() | ||
258 | |||
259 | def _get_delayed_postinsts(self): | ||
260 | pkg_list = [] | ||
261 | with open(self.image_rootfs + "/var/lib/dpkg/status") as status: | ||
262 | for line in status: | ||
263 | m_pkg = re.match("^Package: (.*)", line) | ||
264 | m_status = re.match("^Status:.*unpacked", line) | ||
265 | if m_pkg is not None: | ||
266 | pkg_name = m_pkg.group(1) | ||
267 | elif m_status is not None: | ||
268 | pkg_list.append(pkg_name) | ||
269 | |||
270 | if len(pkg_list) == 0: | ||
271 | return None | ||
272 | |||
273 | return pkg_list | ||
274 | |||
275 | def _save_postinsts(self): | ||
276 | num = 0 | ||
277 | for p in self._get_delayed_postinsts(): | ||
278 | dst_postinst_dir = self.d.expand("${IMAGE_ROOTFS}${sysconfdir}/deb-postinsts") | ||
279 | src_postinst_dir = self.d.expand("${IMAGE_ROOTFS}/var/lib/dpkg/info") | ||
280 | |||
281 | bb.utils.mkdirhier(dst_postinst_dir) | ||
282 | |||
283 | if os.path.exists(os.path.join(src_postinst_dir, p + ".postinst")): | ||
284 | shutil.copy(os.path.join(src_postinst_dir, p + ".postinst"), | ||
285 | os.path.join(dst_postinst_dir, "%03d-%s" % (num, p))) | ||
286 | |||
287 | num += 1 | ||
288 | |||
289 | def _handle_intercept_failure(self, registered_pkgs): | ||
290 | self.pm.mark_packages("unpacked", registered_pkgs.split()) | ||
291 | |||
292 | def _log_check(self): | ||
293 | pass | ||
294 | |||
295 | def _insert_feed_uris(self): | ||
296 | pass | ||
297 | |||
298 | |||
299 | class OpkgRootfs(Rootfs): | ||
300 | def __init__(self, manifest): | ||
301 | super(OpkgRootfs, self).__init__(manifest) | ||
302 | """ | ||
303 | TBD | ||
304 | """ | ||
305 | |||
306 | |||
307 | def create_rootfs(d, manifest_dir=None): | ||
308 | env_bkp = os.environ.copy() | ||
309 | |||
310 | img_type = d.getVar('IMAGE_PKGTYPE', True) | ||
311 | if img_type == "rpm": | ||
312 | bb.fatal("RPM backend was not implemented yet...") | ||
313 | elif img_type == "ipk": | ||
314 | bb.fatal("IPK backend was not implemented yet...") | ||
315 | elif img_type == "deb": | ||
316 | DpkgRootfs(d, manifest_dir).create() | ||
317 | |||
318 | os.environ.clear() | ||
319 | os.environ.update(env_bkp) | ||
320 | |||
321 | if __name__ == "__main__": | ||
322 | """ | ||
323 | We should be able to run this as a standalone script, from outside bitbake | ||
324 | environment. | ||
325 | """ | ||
326 | """ | ||
327 | TBD | ||
328 | """ | ||