summaryrefslogtreecommitdiffstats
path: root/scripts/lib/wic/partition.py
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/lib/wic/partition.py')
-rw-r--r--scripts/lib/wic/partition.py554
1 files changed, 0 insertions, 554 deletions
diff --git a/scripts/lib/wic/partition.py b/scripts/lib/wic/partition.py
deleted file mode 100644
index b34691d313..0000000000
--- a/scripts/lib/wic/partition.py
+++ /dev/null
@@ -1,554 +0,0 @@
1#
2# Copyright (c) 2013-2016 Intel Corporation.
3#
4# SPDX-License-Identifier: GPL-2.0-only
5#
6# DESCRIPTION
7# This module provides the OpenEmbedded partition object definitions.
8#
9# AUTHORS
10# Tom Zanussi <tom.zanussi (at] linux.intel.com>
11# Ed Bartosh <ed.bartosh> (at] linux.intel.com>
12
13import logging
14import os
15import uuid
16
17from wic import WicError
18from wic.misc import exec_cmd, exec_native_cmd, get_bitbake_var
19from wic.pluginbase import PluginMgr
20
21logger = logging.getLogger('wic')
22
23class Partition():
24
25 def __init__(self, args, lineno):
26 self.args = args
27 self.active = args.active
28 self.align = args.align
29 self.disk = args.disk
30 self.device = None
31 self.extra_space = args.extra_space
32 self.exclude_path = args.exclude_path
33 self.include_path = args.include_path
34 self.change_directory = args.change_directory
35 self.fsopts = args.fsopts
36 self.fspassno = args.fspassno
37 self.fstype = args.fstype
38 self.label = args.label
39 self.use_label = args.use_label
40 self.mkfs_extraopts = args.mkfs_extraopts
41 self.mountpoint = args.mountpoint
42 self.no_table = args.no_table
43 self.num = None
44 self.offset = args.offset
45 self.overhead_factor = args.overhead_factor
46 self.part_name = args.part_name
47 self.part_type = args.part_type
48 self.rootfs_dir = args.rootfs_dir
49 self.size = args.size
50 self.fixed_size = args.fixed_size
51 self.source = args.source
52 self.sourceparams = args.sourceparams
53 self.system_id = args.system_id
54 self.use_uuid = args.use_uuid
55 self.uuid = args.uuid
56 self.fsuuid = args.fsuuid
57 self.type = args.type
58 self.no_fstab_update = args.no_fstab_update
59 self.updated_fstab_path = None
60 self.has_fstab = False
61 self.update_fstab_in_rootfs = False
62 self.hidden = args.hidden
63 self.mbr = args.mbr
64
65 self.lineno = lineno
66 self.source_file = ""
67
68 def get_extra_block_count(self, current_blocks):
69 """
70 The --size param is reflected in self.size (in kB), and we already
71 have current_blocks (1k) blocks, calculate and return the
72 number of (1k) blocks we need to add to get to --size, 0 if
73 we're already there or beyond.
74 """
75 logger.debug("Requested partition size for %s: %d",
76 self.mountpoint, self.size)
77
78 if not self.size:
79 return 0
80
81 requested_blocks = self.size
82
83 logger.debug("Requested blocks %d, current_blocks %d",
84 requested_blocks, current_blocks)
85
86 if requested_blocks > current_blocks:
87 return requested_blocks - current_blocks
88 else:
89 return 0
90
91 def get_rootfs_size(self, actual_rootfs_size=0):
92 """
93 Calculate the required size of rootfs taking into consideration
94 --size/--fixed-size flags as well as overhead and extra space, as
95 specified in kickstart file. Raises an error if the
96 `actual_rootfs_size` is larger than fixed-size rootfs.
97
98 """
99 if self.fixed_size:
100 rootfs_size = self.fixed_size
101 if actual_rootfs_size > rootfs_size:
102 raise WicError("Actual rootfs size (%d kB) is larger than "
103 "allowed size %d kB" %
104 (actual_rootfs_size, rootfs_size))
105 else:
106 extra_blocks = self.get_extra_block_count(actual_rootfs_size)
107 if extra_blocks < self.extra_space:
108 extra_blocks = self.extra_space
109
110 rootfs_size = actual_rootfs_size + extra_blocks
111 rootfs_size = int(rootfs_size * self.overhead_factor)
112
113 logger.debug("Added %d extra blocks to %s to get to %d total blocks",
114 extra_blocks, self.mountpoint, rootfs_size)
115
116 return rootfs_size
117
118 @property
119 def disk_size(self):
120 """
121 Obtain on-disk size of partition taking into consideration
122 --size/--fixed-size options.
123
124 """
125 return self.fixed_size if self.fixed_size else self.size
126
127 def prepare(self, creator, cr_workdir, oe_builddir, rootfs_dir,
128 bootimg_dir, kernel_dir, native_sysroot, updated_fstab_path):
129 """
130 Prepare content for individual partitions, depending on
131 partition command parameters.
132 """
133 self.updated_fstab_path = updated_fstab_path
134 if self.updated_fstab_path and not (self.fstype.startswith("ext") or self.fstype == "msdos"):
135 self.update_fstab_in_rootfs = True
136
137 if not self.source:
138 if self.fstype == "none" or self.no_table:
139 return
140 if not self.size and not self.fixed_size:
141 raise WicError("The %s partition has a size of zero. Please "
142 "specify a non-zero --size/--fixed-size for that "
143 "partition." % self.mountpoint)
144
145 if self.fstype == "swap":
146 self.prepare_swap_partition(cr_workdir, oe_builddir,
147 native_sysroot)
148 self.source_file = "%s/fs.%s" % (cr_workdir, self.fstype)
149 else:
150 if self.fstype in ('squashfs', 'erofs'):
151 raise WicError("It's not possible to create empty %s "
152 "partition '%s'" % (self.fstype, self.mountpoint))
153
154 rootfs = "%s/fs_%s.%s.%s" % (cr_workdir, self.label,
155 self.lineno, self.fstype)
156 if os.path.isfile(rootfs):
157 os.remove(rootfs)
158
159 prefix = "ext" if self.fstype.startswith("ext") else self.fstype
160 method = getattr(self, "prepare_empty_partition_" + prefix)
161 method(rootfs, oe_builddir, native_sysroot)
162 self.source_file = rootfs
163 return
164
165 plugins = PluginMgr.get_plugins('source')
166
167 # Don't support '-' in plugin names
168 self.source = self.source.replace("-", "_")
169
170 if self.source not in plugins:
171 raise WicError("The '%s' --source specified for %s doesn't exist.\n\t"
172 "See 'wic list source-plugins' for a list of available"
173 " --sources.\n\tSee 'wic help source-plugins' for "
174 "details on adding a new source plugin." %
175 (self.source, self.mountpoint))
176
177 srcparams_dict = {}
178 if self.sourceparams:
179 # Split sourceparams string of the form key1=val1[,key2=val2,...]
180 # into a dict. Also accepts valueless keys i.e. without =
181 splitted = self.sourceparams.split(',')
182 srcparams_dict = dict((par.split('=', 1) + [None])[:2] for par in splitted if par)
183
184 plugin = plugins[self.source]
185 plugin.do_configure_partition(self, srcparams_dict, creator,
186 cr_workdir, oe_builddir, bootimg_dir,
187 kernel_dir, native_sysroot)
188 plugin.do_stage_partition(self, srcparams_dict, creator,
189 cr_workdir, oe_builddir, bootimg_dir,
190 kernel_dir, native_sysroot)
191 plugin.do_prepare_partition(self, srcparams_dict, creator,
192 cr_workdir, oe_builddir, bootimg_dir,
193 kernel_dir, rootfs_dir, native_sysroot)
194 plugin.do_post_partition(self, srcparams_dict, creator,
195 cr_workdir, oe_builddir, bootimg_dir,
196 kernel_dir, rootfs_dir, native_sysroot)
197
198 # further processing required Partition.size to be an integer, make
199 # sure that it is one
200 if not isinstance(self.size, int):
201 raise WicError("Partition %s internal size is not an integer. "
202 "This a bug in source plugin %s and needs to be fixed." %
203 (self.mountpoint, self.source))
204
205 if self.fixed_size and self.size > self.fixed_size:
206 raise WicError("File system image of partition %s is "
207 "larger (%d kB) than its allowed size %d kB" %
208 (self.mountpoint, self.size, self.fixed_size))
209
210 def prepare_rootfs(self, cr_workdir, oe_builddir, rootfs_dir,
211 native_sysroot, real_rootfs = True, pseudo_dir = None):
212 """
213 Prepare content for a rootfs partition i.e. create a partition
214 and fill it from a /rootfs dir.
215
216 Currently handles ext2/3/4, btrfs, vfat and squashfs.
217 """
218
219 rootfs = "%s/rootfs_%s.%s.%s" % (cr_workdir, self.label,
220 self.lineno, self.fstype)
221 if os.path.isfile(rootfs):
222 os.remove(rootfs)
223
224 p_prefix = os.environ.get("PSEUDO_PREFIX", "%s/usr" % native_sysroot)
225 if (pseudo_dir):
226 # Canonicalize the ignore paths. This corresponds to
227 # calling oe.path.canonicalize(), which is used in bitbake.conf.
228 include_paths = [rootfs_dir] + (get_bitbake_var("PSEUDO_INCLUDE_PATHS") or "").split(",")
229 canonical_paths = []
230 for path in include_paths:
231 if "$" not in path:
232 trailing_slash = path.endswith("/") and "/" or ""
233 canonical_paths.append(os.path.realpath(path) + trailing_slash)
234 include_paths = ",".join(canonical_paths)
235
236 pseudo = "export PSEUDO_PREFIX=%s;" % p_prefix
237 pseudo += "export PSEUDO_LOCALSTATEDIR=%s;" % pseudo_dir
238 pseudo += "export PSEUDO_PASSWD=%s;" % rootfs_dir
239 pseudo += "export PSEUDO_NOSYMLINKEXP=1;"
240 pseudo += "export PSEUDO_INCLUDE_PATHS=%s;" % include_paths
241 pseudo += "%s " % get_bitbake_var("FAKEROOTCMD")
242 else:
243 pseudo = None
244
245 if not self.size and real_rootfs:
246 # The rootfs size is not set in .ks file so try to get it
247 # from bitbake variable
248 rsize_bb = get_bitbake_var('ROOTFS_SIZE')
249 rdir = get_bitbake_var('IMAGE_ROOTFS')
250 if rsize_bb and (rdir == rootfs_dir or (rootfs_dir.split('/')[-2] == "tmp-wic" and rootfs_dir.split('/')[-1][:6] == "rootfs")):
251 # Bitbake variable ROOTFS_SIZE is calculated in
252 # Image._get_rootfs_size method from meta/lib/oe/image.py
253 # using IMAGE_ROOTFS_SIZE, IMAGE_ROOTFS_ALIGNMENT,
254 # IMAGE_OVERHEAD_FACTOR and IMAGE_ROOTFS_EXTRA_SPACE
255 self.size = int(round(float(rsize_bb)))
256 else:
257 # Bitbake variable ROOTFS_SIZE is not defined so compute it
258 # from the rootfs_dir size using the same logic found in
259 # get_rootfs_size() from meta/classes/image.bbclass
260 du_cmd = "du -ks %s" % rootfs_dir
261 out = exec_cmd(du_cmd)
262 self.size = int(out.split()[0])
263
264 prefix = "ext" if self.fstype.startswith("ext") else self.fstype
265 method = getattr(self, "prepare_rootfs_" + prefix)
266 method(rootfs, cr_workdir, oe_builddir, rootfs_dir, native_sysroot, pseudo)
267 self.source_file = rootfs
268
269 # get the rootfs size in the right units for kickstart (kB)
270 du_cmd = "du -Lbks %s" % rootfs
271 out = exec_cmd(du_cmd)
272 self.size = int(out.split()[0])
273
274 def prepare_rootfs_ext(self, rootfs, cr_workdir, oe_builddir, rootfs_dir,
275 native_sysroot, pseudo):
276 """
277 Prepare content for an ext2/3/4 rootfs partition.
278 """
279 du_cmd = "du -ks %s" % rootfs_dir
280 out = exec_cmd(du_cmd)
281 actual_rootfs_size = int(out.split()[0])
282
283 rootfs_size = self.get_rootfs_size(actual_rootfs_size)
284
285 with open(rootfs, 'w') as sparse:
286 os.ftruncate(sparse.fileno(), rootfs_size * 1024)
287
288 extraopts = self.mkfs_extraopts or "-F -i 8192"
289
290 # use hash_seed to generate reproducible ext4 images
291 (extraopts, pseudo) = self.get_hash_seed_ext4(extraopts, pseudo)
292
293 label_str = ""
294 if self.label:
295 label_str = "-L %s" % self.label
296
297 mkfs_cmd = "mkfs.%s %s %s %s -U %s -d %s" % \
298 (self.fstype, extraopts, rootfs, label_str, self.fsuuid, rootfs_dir)
299 exec_native_cmd(mkfs_cmd, native_sysroot, pseudo=pseudo)
300
301 if self.updated_fstab_path and self.has_fstab and not self.no_fstab_update:
302 debugfs_script_path = os.path.join(cr_workdir, "debugfs_script")
303 with open(debugfs_script_path, "w") as f:
304 f.write("cd etc\n")
305 f.write("rm fstab\n")
306 f.write("write %s fstab\n" % (self.updated_fstab_path))
307 debugfs_cmd = "debugfs -w -f %s %s" % (debugfs_script_path, rootfs)
308 exec_native_cmd(debugfs_cmd, native_sysroot)
309
310 mkfs_cmd = "fsck.%s -pvfD %s" % (self.fstype, rootfs)
311 exec_native_cmd(mkfs_cmd, native_sysroot, pseudo=pseudo)
312
313 if os.getenv('SOURCE_DATE_EPOCH'):
314 sde_time = hex(int(os.getenv('SOURCE_DATE_EPOCH')))
315 debugfs_script_path = os.path.join(cr_workdir, "debugfs_script")
316 files = []
317 for root, dirs, others in os.walk(rootfs_dir):
318 base = root.replace(rootfs_dir, "").rstrip(os.sep)
319 files += [ "/" if base == "" else base ]
320 files += [ base + "/" + n for n in dirs + others ]
321 with open(debugfs_script_path, "w") as f:
322 f.write("set_current_time %s\n" % (sde_time))
323 if self.updated_fstab_path and self.has_fstab and not self.no_fstab_update:
324 f.write("set_inode_field /etc/fstab mtime %s\n" % (sde_time))
325 f.write("set_inode_field /etc/fstab mtime_extra 0\n")
326 for file in set(files):
327 for time in ["atime", "ctime", "crtime"]:
328 f.write("set_inode_field \"%s\" %s %s\n" % (file, time, sde_time))
329 f.write("set_inode_field \"%s\" %s_extra 0\n" % (file, time))
330 for time in ["wtime", "mkfs_time", "lastcheck"]:
331 f.write("set_super_value %s %s\n" % (time, sde_time))
332 for time in ["mtime", "first_error_time", "last_error_time"]:
333 f.write("set_super_value %s 0\n" % (time))
334 debugfs_cmd = "debugfs -w -f %s %s" % (debugfs_script_path, rootfs)
335 exec_native_cmd(debugfs_cmd, native_sysroot)
336
337 self.check_for_Y2038_problem(rootfs, native_sysroot)
338
339 def get_hash_seed_ext4(self, extraopts, pseudo):
340 if os.getenv('SOURCE_DATE_EPOCH'):
341 sde_time = int(os.getenv('SOURCE_DATE_EPOCH'))
342 if pseudo:
343 pseudo = "export E2FSPROGS_FAKE_TIME=%s;%s " % (sde_time, pseudo)
344 else:
345 pseudo = "export E2FSPROGS_FAKE_TIME=%s; " % sde_time
346
347 # Set hash_seed to generate deterministic directory indexes
348 namespace = uuid.UUID("e7429877-e7b3-4a68-a5c9-2f2fdf33d460")
349 if self.fsuuid:
350 namespace = uuid.UUID(self.fsuuid)
351 hash_seed = str(uuid.uuid5(namespace, str(sde_time)))
352 extraopts += " -E hash_seed=%s" % hash_seed
353
354 return (extraopts, pseudo)
355
356 def prepare_rootfs_btrfs(self, rootfs, cr_workdir, oe_builddir, rootfs_dir,
357 native_sysroot, pseudo):
358 """
359 Prepare content for a btrfs rootfs partition.
360 """
361 du_cmd = "du -ks %s" % rootfs_dir
362 out = exec_cmd(du_cmd)
363 actual_rootfs_size = int(out.split()[0])
364
365 rootfs_size = self.get_rootfs_size(actual_rootfs_size)
366
367 with open(rootfs, 'w') as sparse:
368 os.ftruncate(sparse.fileno(), rootfs_size * 1024)
369
370 label_str = ""
371 if self.label:
372 label_str = "-L %s" % self.label
373
374 mkfs_cmd = "mkfs.%s -b %d -r %s %s %s -U %s %s" % \
375 (self.fstype, rootfs_size * 1024, rootfs_dir, label_str,
376 self.mkfs_extraopts, self.fsuuid, rootfs)
377 exec_native_cmd(mkfs_cmd, native_sysroot, pseudo=pseudo)
378
379 def prepare_rootfs_msdos(self, rootfs, cr_workdir, oe_builddir, rootfs_dir,
380 native_sysroot, pseudo):
381 """
382 Prepare content for a msdos/vfat rootfs partition.
383 """
384 du_cmd = "du -bks %s" % rootfs_dir
385 out = exec_cmd(du_cmd)
386 blocks = int(out.split()[0])
387
388 rootfs_size = self.get_rootfs_size(blocks)
389
390 label_str = "-n boot"
391 if self.label:
392 label_str = "-n %s" % self.label
393
394 size_str = ""
395
396 extraopts = self.mkfs_extraopts or '-S 512'
397
398 dosfs_cmd = "mkdosfs %s -i %s %s %s -C %s %d" % \
399 (label_str, self.fsuuid, size_str, extraopts, rootfs,
400 rootfs_size)
401 exec_native_cmd(dosfs_cmd, native_sysroot)
402
403 mcopy_cmd = "mcopy -i %s -s %s/* ::/" % (rootfs, rootfs_dir)
404 exec_native_cmd(mcopy_cmd, native_sysroot)
405
406 if self.updated_fstab_path and self.has_fstab and not self.no_fstab_update:
407 mcopy_cmd = "mcopy -m -i %s %s ::/etc/fstab" % (rootfs, self.updated_fstab_path)
408 exec_native_cmd(mcopy_cmd, native_sysroot)
409
410 chmod_cmd = "chmod 644 %s" % rootfs
411 exec_cmd(chmod_cmd)
412
413 prepare_rootfs_vfat = prepare_rootfs_msdos
414
415 def prepare_rootfs_squashfs(self, rootfs, cr_workdir, oe_builddir, rootfs_dir,
416 native_sysroot, pseudo):
417 """
418 Prepare content for a squashfs rootfs partition.
419 """
420 extraopts = self.mkfs_extraopts or '-noappend'
421 squashfs_cmd = "mksquashfs %s %s %s" % \
422 (rootfs_dir, rootfs, extraopts)
423 exec_native_cmd(squashfs_cmd, native_sysroot, pseudo=pseudo)
424
425 def prepare_rootfs_erofs(self, rootfs, cr_workdir, oe_builddir, rootfs_dir,
426 native_sysroot, pseudo):
427 """
428 Prepare content for a erofs rootfs partition.
429 """
430 extraopts = self.mkfs_extraopts or ''
431 erofs_cmd = "mkfs.erofs %s -U %s %s %s" % \
432 (extraopts, self.fsuuid, rootfs, rootfs_dir)
433 exec_native_cmd(erofs_cmd, native_sysroot, pseudo=pseudo)
434
435 def prepare_empty_partition_none(self, rootfs, oe_builddir, native_sysroot):
436 pass
437
438 def prepare_empty_partition_ext(self, rootfs, oe_builddir,
439 native_sysroot):
440 """
441 Prepare an empty ext2/3/4 partition.
442 """
443 size = self.disk_size
444 with open(rootfs, 'w') as sparse:
445 os.ftruncate(sparse.fileno(), size * 1024)
446
447 extraopts = self.mkfs_extraopts or "-i 8192"
448
449 # use hash_seed to generate reproducible ext4 images
450 (extraopts, pseudo) = self.get_hash_seed_ext4(extraopts, None)
451
452 label_str = ""
453 if self.label:
454 label_str = "-L %s" % self.label
455
456 mkfs_cmd = "mkfs.%s -F %s %s -U %s %s" % \
457 (self.fstype, extraopts, label_str, self.fsuuid, rootfs)
458 exec_native_cmd(mkfs_cmd, native_sysroot, pseudo=pseudo)
459
460 self.check_for_Y2038_problem(rootfs, native_sysroot)
461
462 def prepare_empty_partition_btrfs(self, rootfs, oe_builddir,
463 native_sysroot):
464 """
465 Prepare an empty btrfs partition.
466 """
467 size = self.disk_size
468 with open(rootfs, 'w') as sparse:
469 os.ftruncate(sparse.fileno(), size * 1024)
470
471 label_str = ""
472 if self.label:
473 label_str = "-L %s" % self.label
474
475 mkfs_cmd = "mkfs.%s -b %d %s -U %s %s %s" % \
476 (self.fstype, self.size * 1024, label_str, self.fsuuid,
477 self.mkfs_extraopts, rootfs)
478 exec_native_cmd(mkfs_cmd, native_sysroot)
479
480 def prepare_empty_partition_msdos(self, rootfs, oe_builddir,
481 native_sysroot):
482 """
483 Prepare an empty vfat partition.
484 """
485 blocks = self.disk_size
486
487 label_str = "-n boot"
488 if self.label:
489 label_str = "-n %s" % self.label
490
491 size_str = ""
492
493 extraopts = self.mkfs_extraopts or '-S 512'
494
495 dosfs_cmd = "mkdosfs %s -i %s %s %s -C %s %d" % \
496 (label_str, self.fsuuid, extraopts, size_str, rootfs,
497 blocks)
498
499 exec_native_cmd(dosfs_cmd, native_sysroot)
500
501 chmod_cmd = "chmod 644 %s" % rootfs
502 exec_cmd(chmod_cmd)
503
504 prepare_empty_partition_vfat = prepare_empty_partition_msdos
505
506 def prepare_swap_partition(self, cr_workdir, oe_builddir, native_sysroot):
507 """
508 Prepare a swap partition.
509 """
510 path = "%s/fs.%s" % (cr_workdir, self.fstype)
511
512 with open(path, 'w') as sparse:
513 os.ftruncate(sparse.fileno(), self.size * 1024)
514
515 label_str = ""
516 if self.label:
517 label_str = "-L %s" % self.label
518
519 mkswap_cmd = "mkswap %s -U %s %s" % (label_str, self.fsuuid, path)
520 exec_native_cmd(mkswap_cmd, native_sysroot)
521
522 def check_for_Y2038_problem(self, rootfs, native_sysroot):
523 """
524 Check if the filesystem is affected by the Y2038 problem
525 (Y2038 problem = 32 bit time_t overflow in January 2038)
526 """
527 def get_err_str(part):
528 err = "The {} filesystem {} has no Y2038 support."
529 if part.mountpoint:
530 args = [part.fstype, "mounted at %s" % part.mountpoint]
531 elif part.label:
532 args = [part.fstype, "labeled '%s'" % part.label]
533 elif part.part_name:
534 args = [part.fstype, "in partition '%s'" % part.part_name]
535 else:
536 args = [part.fstype, "in partition %s" % part.num]
537 return err.format(*args)
538
539 # ext2 and ext3 are always affected by the Y2038 problem
540 if self.fstype in ["ext2", "ext3"]:
541 logger.warn(get_err_str(self))
542 return
543
544 ret, out = exec_native_cmd("dumpe2fs %s" % rootfs, native_sysroot)
545
546 # if ext4 is affected by the Y2038 problem depends on the inode size
547 for line in out.splitlines():
548 if line.startswith("Inode size:"):
549 size = int(line.split(":")[1].strip())
550 if size < 256:
551 logger.warn("%s Inodes (of size %d) are too small." %
552 (get_err_str(self), size))
553 break
554