blob: e50871b8d74952cfcf1828c6118ed2483e68b868 [file] [log] [blame]
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001#
2# Copyright (c) 2013-2016 Intel Corporation.
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05003#
Brad Bishopc342db32019-05-15 21:57:59 -04004# SPDX-License-Identifier: GPL-2.0-only
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05005#
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
Brad Bishop6e60e8b2018-02-01 10:27:11 -050013import logging
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050014import os
Brad Bishop316dfdd2018-06-25 12:45:53 -040015import uuid
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050016
Brad Bishop6e60e8b2018-02-01 10:27:11 -050017from wic import WicError
Brad Bishopd7bf8c12018-02-25 22:55:05 -050018from wic.misc import exec_cmd, exec_native_cmd, get_bitbake_var
Brad Bishop6e60e8b2018-02-01 10:27:11 -050019from wic.pluginbase import PluginMgr
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050020
Brad Bishop6e60e8b2018-02-01 10:27:11 -050021logger = logging.getLogger('wic')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050022
Patrick Williamsc0f7c042017-02-23 20:41:17 -060023class Partition():
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050024
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
Brad Bishop6e60e8b2018-02-01 10:27:11 -050030 self.device = None
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050031 self.extra_space = args.extra_space
Brad Bishop6e60e8b2018-02-01 10:27:11 -050032 self.exclude_path = args.exclude_path
Andrew Geissler82c905d2020-04-13 13:39:40 -050033 self.include_path = args.include_path
34 self.change_directory = args.change_directory
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050035 self.fsopts = args.fsopts
Andrew Geisslerd5838332022-05-27 11:33:10 -050036 self.fspassno = args.fspassno
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050037 self.fstype = args.fstype
38 self.label = args.label
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080039 self.use_label = args.use_label
Brad Bishopd7bf8c12018-02-25 22:55:05 -050040 self.mkfs_extraopts = args.mkfs_extraopts
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050041 self.mountpoint = args.mountpoint
42 self.no_table = args.no_table
Brad Bishop6e60e8b2018-02-01 10:27:11 -050043 self.num = None
Andrew Geissler4ed12e12020-06-05 18:00:41 -050044 self.offset = args.offset
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050045 self.overhead_factor = args.overhead_factor
Brad Bishopd7bf8c12018-02-25 22:55:05 -050046 self.part_name = args.part_name
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050047 self.part_type = args.part_type
48 self.rootfs_dir = args.rootfs_dir
49 self.size = args.size
Brad Bishop6e60e8b2018-02-01 10:27:11 -050050 self.fixed_size = args.fixed_size
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050051 self.source = args.source
52 self.sourceparams = args.sourceparams
Patrick Williamsc0f7c042017-02-23 20:41:17 -060053 self.system_id = args.system_id
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050054 self.use_uuid = args.use_uuid
55 self.uuid = args.uuid
Brad Bishop316dfdd2018-06-25 12:45:53 -040056 self.fsuuid = args.fsuuid
Brad Bishop08902b02019-08-20 09:16:51 -040057 self.type = args.type
Andrew Geisslerd159c7f2021-09-02 21:05:58 -050058 self.no_fstab_update = args.no_fstab_update
Andrew Geisslerd1e89492021-02-12 15:35:20 -060059 self.updated_fstab_path = None
60 self.has_fstab = False
61 self.update_fstab_in_rootfs = False
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050062
63 self.lineno = lineno
64 self.source_file = ""
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050065
66 def get_extra_block_count(self, current_blocks):
67 """
68 The --size param is reflected in self.size (in kB), and we already
69 have current_blocks (1k) blocks, calculate and return the
70 number of (1k) blocks we need to add to get to --size, 0 if
71 we're already there or beyond.
72 """
Brad Bishop6e60e8b2018-02-01 10:27:11 -050073 logger.debug("Requested partition size for %s: %d",
74 self.mountpoint, self.size)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050075
76 if not self.size:
77 return 0
78
79 requested_blocks = self.size
80
Brad Bishop6e60e8b2018-02-01 10:27:11 -050081 logger.debug("Requested blocks %d, current_blocks %d",
82 requested_blocks, current_blocks)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050083
84 if requested_blocks > current_blocks:
85 return requested_blocks - current_blocks
86 else:
87 return 0
88
Brad Bishop6e60e8b2018-02-01 10:27:11 -050089 def get_rootfs_size(self, actual_rootfs_size=0):
90 """
91 Calculate the required size of rootfs taking into consideration
92 --size/--fixed-size flags as well as overhead and extra space, as
93 specified in kickstart file. Raises an error if the
94 `actual_rootfs_size` is larger than fixed-size rootfs.
95
96 """
97 if self.fixed_size:
98 rootfs_size = self.fixed_size
99 if actual_rootfs_size > rootfs_size:
100 raise WicError("Actual rootfs size (%d kB) is larger than "
101 "allowed size %d kB" %
102 (actual_rootfs_size, rootfs_size))
103 else:
104 extra_blocks = self.get_extra_block_count(actual_rootfs_size)
105 if extra_blocks < self.extra_space:
106 extra_blocks = self.extra_space
107
108 rootfs_size = actual_rootfs_size + extra_blocks
Andrew Geissler5199d832021-09-24 16:47:35 -0500109 rootfs_size = int(rootfs_size * self.overhead_factor)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500110
111 logger.debug("Added %d extra blocks to %s to get to %d total blocks",
112 extra_blocks, self.mountpoint, rootfs_size)
113
114 return rootfs_size
115
116 @property
117 def disk_size(self):
118 """
119 Obtain on-disk size of partition taking into consideration
120 --size/--fixed-size options.
121
122 """
123 return self.fixed_size if self.fixed_size else self.size
124
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500125 def prepare(self, creator, cr_workdir, oe_builddir, rootfs_dir,
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600126 bootimg_dir, kernel_dir, native_sysroot, updated_fstab_path):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500127 """
128 Prepare content for individual partitions, depending on
129 partition command parameters.
130 """
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600131 self.updated_fstab_path = updated_fstab_path
132 if self.updated_fstab_path and not (self.fstype.startswith("ext") or self.fstype == "msdos"):
133 self.update_fstab_in_rootfs = True
134
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500135 if not self.source:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500136 if not self.size and not self.fixed_size:
137 raise WicError("The %s partition has a size of zero. Please "
138 "specify a non-zero --size/--fixed-size for that "
139 "partition." % self.mountpoint)
140
141 if self.fstype == "swap":
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500142 self.prepare_swap_partition(cr_workdir, oe_builddir,
143 native_sysroot)
144 self.source_file = "%s/fs.%s" % (cr_workdir, self.fstype)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500145 else:
William A. Kennington IIIac69b482021-06-02 12:28:27 -0700146 if self.fstype in ('squashfs', 'erofs'):
147 raise WicError("It's not possible to create empty %s "
148 "partition '%s'" % (self.fstype, self.mountpoint))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500149
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500150 rootfs = "%s/fs_%s.%s.%s" % (cr_workdir, self.label,
151 self.lineno, self.fstype)
152 if os.path.isfile(rootfs):
153 os.remove(rootfs)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500154
155 prefix = "ext" if self.fstype.startswith("ext") else self.fstype
156 method = getattr(self, "prepare_empty_partition_" + prefix)
157 method(rootfs, oe_builddir, native_sysroot)
158 self.source_file = rootfs
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500159 return
160
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500161 plugins = PluginMgr.get_plugins('source')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500162
163 if self.source not in plugins:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500164 raise WicError("The '%s' --source specified for %s doesn't exist.\n\t"
165 "See 'wic list source-plugins' for a list of available"
166 " --sources.\n\tSee 'wic help source-plugins' for "
167 "details on adding a new source plugin." %
168 (self.source, self.mountpoint))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500169
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500170 srcparams_dict = {}
171 if self.sourceparams:
172 # Split sourceparams string of the form key1=val1[,key2=val2,...]
173 # into a dict. Also accepts valueless keys i.e. without =
174 splitted = self.sourceparams.split(',')
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000175 srcparams_dict = dict((par.split('=', 1) + [None])[:2] for par in splitted if par)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500176
177 plugin = PluginMgr.get_plugins('source')[self.source]
178 plugin.do_configure_partition(self, srcparams_dict, creator,
179 cr_workdir, oe_builddir, bootimg_dir,
180 kernel_dir, native_sysroot)
181 plugin.do_stage_partition(self, srcparams_dict, creator,
182 cr_workdir, oe_builddir, bootimg_dir,
183 kernel_dir, native_sysroot)
184 plugin.do_prepare_partition(self, srcparams_dict, creator,
185 cr_workdir, oe_builddir, bootimg_dir,
186 kernel_dir, rootfs_dir, native_sysroot)
Brad Bishop316dfdd2018-06-25 12:45:53 -0400187 plugin.do_post_partition(self, srcparams_dict, creator,
188 cr_workdir, oe_builddir, bootimg_dir,
189 kernel_dir, rootfs_dir, native_sysroot)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500190
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500191 # further processing required Partition.size to be an integer, make
192 # sure that it is one
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500193 if not isinstance(self.size, int):
194 raise WicError("Partition %s internal size is not an integer. "
195 "This a bug in source plugin %s and needs to be fixed." %
196 (self.mountpoint, self.source))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500197
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500198 if self.fixed_size and self.size > self.fixed_size:
199 raise WicError("File system image of partition %s is "
200 "larger (%d kB) than its allowed size %d kB" %
201 (self.mountpoint, self.size, self.fixed_size))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500202
203 def prepare_rootfs(self, cr_workdir, oe_builddir, rootfs_dir,
Andrew Geissler82c905d2020-04-13 13:39:40 -0500204 native_sysroot, real_rootfs = True, pseudo_dir = None):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500205 """
206 Prepare content for a rootfs partition i.e. create a partition
207 and fill it from a /rootfs dir.
208
Brad Bishop316dfdd2018-06-25 12:45:53 -0400209 Currently handles ext2/3/4, btrfs, vfat and squashfs.
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500210 """
Andrew Geisslerf0343792020-11-18 10:42:21 -0600211
212 rootfs = "%s/rootfs_%s.%s.%s" % (cr_workdir, self.label,
213 self.lineno, self.fstype)
214 if os.path.isfile(rootfs):
215 os.remove(rootfs)
216
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500217 p_prefix = os.environ.get("PSEUDO_PREFIX", "%s/usr" % native_sysroot)
Andrew Geissler82c905d2020-04-13 13:39:40 -0500218 if (pseudo_dir):
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600219 # Canonicalize the ignore paths. This corresponds to
220 # calling oe.path.canonicalize(), which is used in bitbake.conf.
221 ignore_paths = [rootfs] + (get_bitbake_var("PSEUDO_IGNORE_PATHS") or "").split(",")
222 canonical_paths = []
223 for path in ignore_paths:
224 if "$" not in path:
225 trailing_slash = path.endswith("/") and "/" or ""
226 canonical_paths.append(os.path.realpath(path) + trailing_slash)
227 ignore_paths = ",".join(canonical_paths)
228
Andrew Geissler82c905d2020-04-13 13:39:40 -0500229 pseudo = "export PSEUDO_PREFIX=%s;" % p_prefix
230 pseudo += "export PSEUDO_LOCALSTATEDIR=%s;" % pseudo_dir
231 pseudo += "export PSEUDO_PASSWD=%s;" % rootfs_dir
232 pseudo += "export PSEUDO_NOSYMLINKEXP=1;"
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600233 pseudo += "export PSEUDO_IGNORE_PATHS=%s;" % ignore_paths
Andrew Geissler82c905d2020-04-13 13:39:40 -0500234 pseudo += "%s " % get_bitbake_var("FAKEROOTCMD")
235 else:
236 pseudo = None
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500237
Brad Bishop316dfdd2018-06-25 12:45:53 -0400238 if not self.size and real_rootfs:
Brad Bishop00e122a2019-10-05 11:10:57 -0400239 # The rootfs size is not set in .ks file so try to get it
240 # from bitbake variable
241 rsize_bb = get_bitbake_var('ROOTFS_SIZE')
242 rdir = get_bitbake_var('IMAGE_ROOTFS')
243 if rsize_bb and rdir == rootfs_dir:
244 # Bitbake variable ROOTFS_SIZE is calculated in
245 # Image._get_rootfs_size method from meta/lib/oe/image.py
246 # using IMAGE_ROOTFS_SIZE, IMAGE_ROOTFS_ALIGNMENT,
247 # IMAGE_OVERHEAD_FACTOR and IMAGE_ROOTFS_EXTRA_SPACE
248 self.size = int(round(float(rsize_bb)))
249 else:
250 # Bitbake variable ROOTFS_SIZE is not defined so compute it
251 # from the rootfs_dir size using the same logic found in
252 # get_rootfs_size() from meta/classes/image.bbclass
253 du_cmd = "du -ks %s" % rootfs_dir
254 out = exec_cmd(du_cmd)
255 self.size = int(out.split()[0])
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500256
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500257 prefix = "ext" if self.fstype.startswith("ext") else self.fstype
258 method = getattr(self, "prepare_rootfs_" + prefix)
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600259 method(rootfs, cr_workdir, oe_builddir, rootfs_dir, native_sysroot, pseudo)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500260 self.source_file = rootfs
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500261
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500262 # get the rootfs size in the right units for kickstart (kB)
263 du_cmd = "du -Lbks %s" % rootfs
264 out = exec_cmd(du_cmd)
265 self.size = int(out.split()[0])
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500266
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600267 def prepare_rootfs_ext(self, rootfs, cr_workdir, oe_builddir, rootfs_dir,
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500268 native_sysroot, pseudo):
269 """
270 Prepare content for an ext2/3/4 rootfs partition.
271 """
272 du_cmd = "du -ks %s" % rootfs_dir
273 out = exec_cmd(du_cmd)
274 actual_rootfs_size = int(out.split()[0])
275
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500276 rootfs_size = self.get_rootfs_size(actual_rootfs_size)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500277
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500278 with open(rootfs, 'w') as sparse:
279 os.ftruncate(sparse.fileno(), rootfs_size * 1024)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500280
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500281 extraopts = self.mkfs_extraopts or "-F -i 8192"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500282
283 label_str = ""
284 if self.label:
285 label_str = "-L %s" % self.label
286
Brad Bishop316dfdd2018-06-25 12:45:53 -0400287 mkfs_cmd = "mkfs.%s %s %s %s -U %s -d %s" % \
288 (self.fstype, extraopts, rootfs, label_str, self.fsuuid, rootfs_dir)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500289 exec_native_cmd(mkfs_cmd, native_sysroot, pseudo=pseudo)
290
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500291 if self.updated_fstab_path and self.has_fstab and not self.no_fstab_update:
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600292 debugfs_script_path = os.path.join(cr_workdir, "debugfs_script")
293 with open(debugfs_script_path, "w") as f:
294 f.write("cd etc\n")
295 f.write("rm fstab\n")
296 f.write("write %s fstab\n" % (self.updated_fstab_path))
297 debugfs_cmd = "debugfs -w -f %s %s" % (debugfs_script_path, rootfs)
298 exec_native_cmd(debugfs_cmd, native_sysroot)
299
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500300 mkfs_cmd = "fsck.%s -pvfD %s" % (self.fstype, rootfs)
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500301 exec_native_cmd(mkfs_cmd, native_sysroot, pseudo=pseudo)
302
Andrew Geissler90fd73c2021-03-05 15:25:55 -0600303 self.check_for_Y2038_problem(rootfs, native_sysroot)
304
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600305 def prepare_rootfs_btrfs(self, rootfs, cr_workdir, oe_builddir, rootfs_dir,
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500306 native_sysroot, pseudo):
307 """
308 Prepare content for a btrfs rootfs partition.
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500309 """
310 du_cmd = "du -ks %s" % rootfs_dir
311 out = exec_cmd(du_cmd)
312 actual_rootfs_size = int(out.split()[0])
313
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500314 rootfs_size = self.get_rootfs_size(actual_rootfs_size)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500315
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500316 with open(rootfs, 'w') as sparse:
317 os.ftruncate(sparse.fileno(), rootfs_size * 1024)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500318
319 label_str = ""
320 if self.label:
321 label_str = "-L %s" % self.label
322
Brad Bishop316dfdd2018-06-25 12:45:53 -0400323 mkfs_cmd = "mkfs.%s -b %d -r %s %s %s -U %s %s" % \
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500324 (self.fstype, rootfs_size * 1024, rootfs_dir, label_str,
Brad Bishop316dfdd2018-06-25 12:45:53 -0400325 self.mkfs_extraopts, self.fsuuid, rootfs)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500326 exec_native_cmd(mkfs_cmd, native_sysroot, pseudo=pseudo)
327
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600328 def prepare_rootfs_msdos(self, rootfs, cr_workdir, oe_builddir, rootfs_dir,
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500329 native_sysroot, pseudo):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500330 """
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500331 Prepare content for a msdos/vfat rootfs partition.
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500332 """
333 du_cmd = "du -bks %s" % rootfs_dir
334 out = exec_cmd(du_cmd)
335 blocks = int(out.split()[0])
336
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500337 rootfs_size = self.get_rootfs_size(blocks)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500338
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500339 label_str = "-n boot"
340 if self.label:
341 label_str = "-n %s" % self.label
342
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500343 size_str = ""
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500344
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500345 extraopts = self.mkfs_extraopts or '-S 512'
346
Brad Bishop316dfdd2018-06-25 12:45:53 -0400347 dosfs_cmd = "mkdosfs %s -i %s %s %s -C %s %d" % \
348 (label_str, self.fsuuid, size_str, extraopts, rootfs,
Brad Bishopc342db32019-05-15 21:57:59 -0400349 rootfs_size)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500350 exec_native_cmd(dosfs_cmd, native_sysroot)
351
352 mcopy_cmd = "mcopy -i %s -s %s/* ::/" % (rootfs, rootfs_dir)
353 exec_native_cmd(mcopy_cmd, native_sysroot)
354
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500355 if self.updated_fstab_path and self.has_fstab and not self.no_fstab_update:
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600356 mcopy_cmd = "mcopy -i %s %s ::/etc/fstab" % (rootfs, self.updated_fstab_path)
357 exec_native_cmd(mcopy_cmd, native_sysroot)
358
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500359 chmod_cmd = "chmod 644 %s" % rootfs
360 exec_cmd(chmod_cmd)
361
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500362 prepare_rootfs_vfat = prepare_rootfs_msdos
363
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600364 def prepare_rootfs_squashfs(self, rootfs, cr_workdir, oe_builddir, rootfs_dir,
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500365 native_sysroot, pseudo):
366 """
367 Prepare content for a squashfs rootfs partition.
368 """
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500369 extraopts = self.mkfs_extraopts or '-noappend'
370 squashfs_cmd = "mksquashfs %s %s %s" % \
371 (rootfs_dir, rootfs, extraopts)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500372 exec_native_cmd(squashfs_cmd, native_sysroot, pseudo=pseudo)
373
William A. Kennington IIIac69b482021-06-02 12:28:27 -0700374 def prepare_rootfs_erofs(self, rootfs, cr_workdir, oe_builddir, rootfs_dir,
375 native_sysroot, pseudo):
376 """
377 Prepare content for a erofs rootfs partition.
378 """
379 extraopts = self.mkfs_extraopts or ''
380 erofs_cmd = "mkfs.erofs %s -U %s %s %s" % \
381 (extraopts, self.fsuuid, rootfs, rootfs_dir)
382 exec_native_cmd(erofs_cmd, native_sysroot, pseudo=pseudo)
383
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500384 def prepare_empty_partition_ext(self, rootfs, oe_builddir,
385 native_sysroot):
386 """
387 Prepare an empty ext2/3/4 partition.
388 """
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500389 size = self.disk_size
390 with open(rootfs, 'w') as sparse:
391 os.ftruncate(sparse.fileno(), size * 1024)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500392
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500393 extraopts = self.mkfs_extraopts or "-i 8192"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500394
395 label_str = ""
396 if self.label:
397 label_str = "-L %s" % self.label
398
Brad Bishop316dfdd2018-06-25 12:45:53 -0400399 mkfs_cmd = "mkfs.%s -F %s %s -U %s %s" % \
400 (self.fstype, extraopts, label_str, self.fsuuid, rootfs)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500401 exec_native_cmd(mkfs_cmd, native_sysroot)
402
Andrew Geissler90fd73c2021-03-05 15:25:55 -0600403 self.check_for_Y2038_problem(rootfs, native_sysroot)
404
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500405 def prepare_empty_partition_btrfs(self, rootfs, oe_builddir,
406 native_sysroot):
407 """
408 Prepare an empty btrfs partition.
409 """
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500410 size = self.disk_size
411 with open(rootfs, 'w') as sparse:
412 os.ftruncate(sparse.fileno(), size * 1024)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500413
414 label_str = ""
415 if self.label:
416 label_str = "-L %s" % self.label
417
Brad Bishop316dfdd2018-06-25 12:45:53 -0400418 mkfs_cmd = "mkfs.%s -b %d %s -U %s %s %s" % \
419 (self.fstype, self.size * 1024, label_str, self.fsuuid,
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500420 self.mkfs_extraopts, rootfs)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500421 exec_native_cmd(mkfs_cmd, native_sysroot)
422
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500423 def prepare_empty_partition_msdos(self, rootfs, oe_builddir,
424 native_sysroot):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500425 """
426 Prepare an empty vfat partition.
427 """
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500428 blocks = self.disk_size
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500429
430 label_str = "-n boot"
431 if self.label:
432 label_str = "-n %s" % self.label
433
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500434 size_str = ""
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500435
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500436 extraopts = self.mkfs_extraopts or '-S 512'
437
Brad Bishop316dfdd2018-06-25 12:45:53 -0400438 dosfs_cmd = "mkdosfs %s -i %s %s %s -C %s %d" % \
439 (label_str, self.fsuuid, extraopts, size_str, rootfs,
440 blocks)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500441
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500442 exec_native_cmd(dosfs_cmd, native_sysroot)
443
444 chmod_cmd = "chmod 644 %s" % rootfs
445 exec_cmd(chmod_cmd)
446
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500447 prepare_empty_partition_vfat = prepare_empty_partition_msdos
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500448
449 def prepare_swap_partition(self, cr_workdir, oe_builddir, native_sysroot):
450 """
451 Prepare a swap partition.
452 """
453 path = "%s/fs.%s" % (cr_workdir, self.fstype)
454
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500455 with open(path, 'w') as sparse:
456 os.ftruncate(sparse.fileno(), self.size * 1024)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500457
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500458 label_str = ""
459 if self.label:
460 label_str = "-L %s" % self.label
Brad Bishop316dfdd2018-06-25 12:45:53 -0400461
462 mkswap_cmd = "mkswap %s -U %s %s" % (label_str, self.fsuuid, path)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500463 exec_native_cmd(mkswap_cmd, native_sysroot)
Andrew Geissler90fd73c2021-03-05 15:25:55 -0600464
465 def check_for_Y2038_problem(self, rootfs, native_sysroot):
466 """
467 Check if the filesystem is affected by the Y2038 problem
468 (Y2038 problem = 32 bit time_t overflow in January 2038)
469 """
470 def get_err_str(part):
471 err = "The {} filesystem {} has no Y2038 support."
472 if part.mountpoint:
473 args = [part.fstype, "mounted at %s" % part.mountpoint]
474 elif part.label:
475 args = [part.fstype, "labeled '%s'" % part.label]
476 elif part.part_name:
477 args = [part.fstype, "in partition '%s'" % part.part_name]
478 else:
479 args = [part.fstype, "in partition %s" % part.num]
480 return err.format(*args)
481
482 # ext2 and ext3 are always affected by the Y2038 problem
483 if self.fstype in ["ext2", "ext3"]:
484 logger.warn(get_err_str(self))
485 return
486
487 ret, out = exec_native_cmd("dumpe2fs %s" % rootfs, native_sysroot)
488
489 # if ext4 is affected by the Y2038 problem depends on the inode size
490 for line in out.splitlines():
491 if line.startswith("Inode size:"):
492 size = int(line.split(":")[1].strip())
493 if size < 256:
494 logger.warn("%s Inodes (of size %d) are too small." %
495 (get_err_str(self), size))
496 break
497