blob: 795707ec5d9f7f5e2ced7c4ce248a5336b9d17b7 [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 Williamse760df82023-05-26 11:10:49 -050062 self.hidden = args.hidden
Andrew Geissler5082cc72023-09-11 08:41:39 -040063 self.mbr = args.mbr
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050064
65 self.lineno = lineno
66 self.source_file = ""
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050067
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 """
Brad Bishop6e60e8b2018-02-01 10:27:11 -050075 logger.debug("Requested partition size for %s: %d",
76 self.mountpoint, self.size)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050077
78 if not self.size:
79 return 0
80
81 requested_blocks = self.size
82
Brad Bishop6e60e8b2018-02-01 10:27:11 -050083 logger.debug("Requested blocks %d, current_blocks %d",
84 requested_blocks, current_blocks)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050085
86 if requested_blocks > current_blocks:
87 return requested_blocks - current_blocks
88 else:
89 return 0
90
Brad Bishop6e60e8b2018-02-01 10:27:11 -050091 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
Andrew Geissler5199d832021-09-24 16:47:35 -0500111 rootfs_size = int(rootfs_size * self.overhead_factor)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500112
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
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500127 def prepare(self, creator, cr_workdir, oe_builddir, rootfs_dir,
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600128 bootimg_dir, kernel_dir, native_sysroot, updated_fstab_path):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500129 """
130 Prepare content for individual partitions, depending on
131 partition command parameters.
132 """
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600133 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
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500137 if not self.source:
Andrew Geissler5082cc72023-09-11 08:41:39 -0400138 if self.fstype == "none" or self.no_table:
Andrew Geissler6aa7eec2023-03-03 12:41:14 -0600139 return
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500140 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":
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500146 self.prepare_swap_partition(cr_workdir, oe_builddir,
147 native_sysroot)
148 self.source_file = "%s/fs.%s" % (cr_workdir, self.fstype)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500149 else:
William A. Kennington IIIac69b482021-06-02 12:28:27 -0700150 if self.fstype in ('squashfs', 'erofs'):
151 raise WicError("It's not possible to create empty %s "
152 "partition '%s'" % (self.fstype, self.mountpoint))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500153
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500154 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)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500158
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
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500163 return
164
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500165 plugins = PluginMgr.get_plugins('source')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500166
167 if self.source not in plugins:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500168 raise WicError("The '%s' --source specified for %s doesn't exist.\n\t"
169 "See 'wic list source-plugins' for a list of available"
170 " --sources.\n\tSee 'wic help source-plugins' for "
171 "details on adding a new source plugin." %
172 (self.source, self.mountpoint))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500173
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500174 srcparams_dict = {}
175 if self.sourceparams:
176 # Split sourceparams string of the form key1=val1[,key2=val2,...]
177 # into a dict. Also accepts valueless keys i.e. without =
178 splitted = self.sourceparams.split(',')
Andrew Geissler7e0e3c02022-02-25 20:34:39 +0000179 srcparams_dict = dict((par.split('=', 1) + [None])[:2] for par in splitted if par)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500180
181 plugin = PluginMgr.get_plugins('source')[self.source]
182 plugin.do_configure_partition(self, srcparams_dict, creator,
183 cr_workdir, oe_builddir, bootimg_dir,
184 kernel_dir, native_sysroot)
185 plugin.do_stage_partition(self, srcparams_dict, creator,
186 cr_workdir, oe_builddir, bootimg_dir,
187 kernel_dir, native_sysroot)
188 plugin.do_prepare_partition(self, srcparams_dict, creator,
189 cr_workdir, oe_builddir, bootimg_dir,
190 kernel_dir, rootfs_dir, native_sysroot)
Brad Bishop316dfdd2018-06-25 12:45:53 -0400191 plugin.do_post_partition(self, srcparams_dict, creator,
192 cr_workdir, oe_builddir, bootimg_dir,
193 kernel_dir, rootfs_dir, native_sysroot)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500194
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500195 # further processing required Partition.size to be an integer, make
196 # sure that it is one
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500197 if not isinstance(self.size, int):
198 raise WicError("Partition %s internal size is not an integer. "
199 "This a bug in source plugin %s and needs to be fixed." %
200 (self.mountpoint, self.source))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500201
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500202 if self.fixed_size and self.size > self.fixed_size:
203 raise WicError("File system image of partition %s is "
204 "larger (%d kB) than its allowed size %d kB" %
205 (self.mountpoint, self.size, self.fixed_size))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500206
207 def prepare_rootfs(self, cr_workdir, oe_builddir, rootfs_dir,
Andrew Geissler82c905d2020-04-13 13:39:40 -0500208 native_sysroot, real_rootfs = True, pseudo_dir = None):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500209 """
210 Prepare content for a rootfs partition i.e. create a partition
211 and fill it from a /rootfs dir.
212
Brad Bishop316dfdd2018-06-25 12:45:53 -0400213 Currently handles ext2/3/4, btrfs, vfat and squashfs.
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500214 """
Andrew Geisslerf0343792020-11-18 10:42:21 -0600215
216 rootfs = "%s/rootfs_%s.%s.%s" % (cr_workdir, self.label,
217 self.lineno, self.fstype)
218 if os.path.isfile(rootfs):
219 os.remove(rootfs)
220
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500221 p_prefix = os.environ.get("PSEUDO_PREFIX", "%s/usr" % native_sysroot)
Andrew Geissler82c905d2020-04-13 13:39:40 -0500222 if (pseudo_dir):
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600223 # Canonicalize the ignore paths. This corresponds to
224 # calling oe.path.canonicalize(), which is used in bitbake.conf.
225 ignore_paths = [rootfs] + (get_bitbake_var("PSEUDO_IGNORE_PATHS") or "").split(",")
226 canonical_paths = []
227 for path in ignore_paths:
228 if "$" not in path:
229 trailing_slash = path.endswith("/") and "/" or ""
230 canonical_paths.append(os.path.realpath(path) + trailing_slash)
231 ignore_paths = ",".join(canonical_paths)
232
Andrew Geissler82c905d2020-04-13 13:39:40 -0500233 pseudo = "export PSEUDO_PREFIX=%s;" % p_prefix
234 pseudo += "export PSEUDO_LOCALSTATEDIR=%s;" % pseudo_dir
235 pseudo += "export PSEUDO_PASSWD=%s;" % rootfs_dir
236 pseudo += "export PSEUDO_NOSYMLINKEXP=1;"
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600237 pseudo += "export PSEUDO_IGNORE_PATHS=%s;" % ignore_paths
Andrew Geissler82c905d2020-04-13 13:39:40 -0500238 pseudo += "%s " % get_bitbake_var("FAKEROOTCMD")
239 else:
240 pseudo = None
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500241
Brad Bishop316dfdd2018-06-25 12:45:53 -0400242 if not self.size and real_rootfs:
Brad Bishop00e122a2019-10-05 11:10:57 -0400243 # The rootfs size is not set in .ks file so try to get it
244 # from bitbake variable
245 rsize_bb = get_bitbake_var('ROOTFS_SIZE')
246 rdir = get_bitbake_var('IMAGE_ROOTFS')
247 if rsize_bb and rdir == rootfs_dir:
248 # Bitbake variable ROOTFS_SIZE is calculated in
249 # Image._get_rootfs_size method from meta/lib/oe/image.py
250 # using IMAGE_ROOTFS_SIZE, IMAGE_ROOTFS_ALIGNMENT,
251 # IMAGE_OVERHEAD_FACTOR and IMAGE_ROOTFS_EXTRA_SPACE
252 self.size = int(round(float(rsize_bb)))
253 else:
254 # Bitbake variable ROOTFS_SIZE is not defined so compute it
255 # from the rootfs_dir size using the same logic found in
256 # get_rootfs_size() from meta/classes/image.bbclass
257 du_cmd = "du -ks %s" % rootfs_dir
258 out = exec_cmd(du_cmd)
259 self.size = int(out.split()[0])
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500260
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500261 prefix = "ext" if self.fstype.startswith("ext") else self.fstype
262 method = getattr(self, "prepare_rootfs_" + prefix)
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600263 method(rootfs, cr_workdir, oe_builddir, rootfs_dir, native_sysroot, pseudo)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500264 self.source_file = rootfs
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500265
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500266 # get the rootfs size in the right units for kickstart (kB)
267 du_cmd = "du -Lbks %s" % rootfs
268 out = exec_cmd(du_cmd)
269 self.size = int(out.split()[0])
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500270
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600271 def prepare_rootfs_ext(self, rootfs, cr_workdir, oe_builddir, rootfs_dir,
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500272 native_sysroot, pseudo):
273 """
274 Prepare content for an ext2/3/4 rootfs partition.
275 """
276 du_cmd = "du -ks %s" % rootfs_dir
277 out = exec_cmd(du_cmd)
278 actual_rootfs_size = int(out.split()[0])
279
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500280 rootfs_size = self.get_rootfs_size(actual_rootfs_size)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500281
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500282 with open(rootfs, 'w') as sparse:
283 os.ftruncate(sparse.fileno(), rootfs_size * 1024)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500284
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500285 extraopts = self.mkfs_extraopts or "-F -i 8192"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500286
Patrick Williams169d7bc2024-01-05 11:33:25 -0600287 if os.getenv('SOURCE_DATE_EPOCH'):
288 sde_time = int(os.getenv('SOURCE_DATE_EPOCH'))
289 if pseudo:
290 pseudo = "export E2FSPROGS_FAKE_TIME=%s;%s " % (sde_time, pseudo)
291 else:
292 pseudo = "export E2FSPROGS_FAKE_TIME=%s; " % sde_time
293
294 # Set hash_seed to generate deterministic directory indexes
295 namespace = uuid.UUID("e7429877-e7b3-4a68-a5c9-2f2fdf33d460")
296 if self.fsuuid:
297 namespace = uuid.UUID(self.fsuuid)
298 hash_seed = str(uuid.uuid5(namespace, str(sde_time)))
299 extraopts += " -E hash_seed=%s" % hash_seed
300
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500301 label_str = ""
302 if self.label:
303 label_str = "-L %s" % self.label
304
Brad Bishop316dfdd2018-06-25 12:45:53 -0400305 mkfs_cmd = "mkfs.%s %s %s %s -U %s -d %s" % \
306 (self.fstype, extraopts, rootfs, label_str, self.fsuuid, rootfs_dir)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500307 exec_native_cmd(mkfs_cmd, native_sysroot, pseudo=pseudo)
308
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500309 if self.updated_fstab_path and self.has_fstab and not self.no_fstab_update:
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600310 debugfs_script_path = os.path.join(cr_workdir, "debugfs_script")
311 with open(debugfs_script_path, "w") as f:
312 f.write("cd etc\n")
313 f.write("rm fstab\n")
314 f.write("write %s fstab\n" % (self.updated_fstab_path))
315 debugfs_cmd = "debugfs -w -f %s %s" % (debugfs_script_path, rootfs)
316 exec_native_cmd(debugfs_cmd, native_sysroot)
317
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500318 mkfs_cmd = "fsck.%s -pvfD %s" % (self.fstype, rootfs)
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500319 exec_native_cmd(mkfs_cmd, native_sysroot, pseudo=pseudo)
320
Patrick Williams7784c422022-11-17 07:29:11 -0600321 if os.getenv('SOURCE_DATE_EPOCH'):
322 sde_time = hex(int(os.getenv('SOURCE_DATE_EPOCH')))
323 debugfs_script_path = os.path.join(cr_workdir, "debugfs_script")
324 files = []
325 for root, dirs, others in os.walk(rootfs_dir):
326 base = root.replace(rootfs_dir, "").rstrip(os.sep)
327 files += [ "/" if base == "" else base ]
328 files += [ base + "/" + n for n in dirs + others ]
329 with open(debugfs_script_path, "w") as f:
330 f.write("set_current_time %s\n" % (sde_time))
331 if self.updated_fstab_path and self.has_fstab and not self.no_fstab_update:
332 f.write("set_inode_field /etc/fstab mtime %s\n" % (sde_time))
333 f.write("set_inode_field /etc/fstab mtime_extra 0\n")
334 for file in set(files):
335 for time in ["atime", "ctime", "crtime"]:
336 f.write("set_inode_field \"%s\" %s %s\n" % (file, time, sde_time))
337 f.write("set_inode_field \"%s\" %s_extra 0\n" % (file, time))
338 for time in ["wtime", "mkfs_time", "lastcheck"]:
339 f.write("set_super_value %s %s\n" % (time, sde_time))
340 for time in ["mtime", "first_error_time", "last_error_time"]:
341 f.write("set_super_value %s 0\n" % (time))
342 debugfs_cmd = "debugfs -w -f %s %s" % (debugfs_script_path, rootfs)
343 exec_native_cmd(debugfs_cmd, native_sysroot)
344
Andrew Geissler90fd73c2021-03-05 15:25:55 -0600345 self.check_for_Y2038_problem(rootfs, native_sysroot)
346
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600347 def prepare_rootfs_btrfs(self, rootfs, cr_workdir, oe_builddir, rootfs_dir,
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500348 native_sysroot, pseudo):
349 """
350 Prepare content for a btrfs rootfs partition.
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500351 """
352 du_cmd = "du -ks %s" % rootfs_dir
353 out = exec_cmd(du_cmd)
354 actual_rootfs_size = int(out.split()[0])
355
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500356 rootfs_size = self.get_rootfs_size(actual_rootfs_size)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500357
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500358 with open(rootfs, 'w') as sparse:
359 os.ftruncate(sparse.fileno(), rootfs_size * 1024)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500360
361 label_str = ""
362 if self.label:
363 label_str = "-L %s" % self.label
364
Brad Bishop316dfdd2018-06-25 12:45:53 -0400365 mkfs_cmd = "mkfs.%s -b %d -r %s %s %s -U %s %s" % \
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500366 (self.fstype, rootfs_size * 1024, rootfs_dir, label_str,
Brad Bishop316dfdd2018-06-25 12:45:53 -0400367 self.mkfs_extraopts, self.fsuuid, rootfs)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500368 exec_native_cmd(mkfs_cmd, native_sysroot, pseudo=pseudo)
369
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600370 def prepare_rootfs_msdos(self, rootfs, cr_workdir, oe_builddir, rootfs_dir,
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500371 native_sysroot, pseudo):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500372 """
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500373 Prepare content for a msdos/vfat rootfs partition.
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500374 """
375 du_cmd = "du -bks %s" % rootfs_dir
376 out = exec_cmd(du_cmd)
377 blocks = int(out.split()[0])
378
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500379 rootfs_size = self.get_rootfs_size(blocks)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500380
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500381 label_str = "-n boot"
382 if self.label:
383 label_str = "-n %s" % self.label
384
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500385 size_str = ""
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500386
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500387 extraopts = self.mkfs_extraopts or '-S 512'
388
Brad Bishop316dfdd2018-06-25 12:45:53 -0400389 dosfs_cmd = "mkdosfs %s -i %s %s %s -C %s %d" % \
390 (label_str, self.fsuuid, size_str, extraopts, rootfs,
Brad Bishopc342db32019-05-15 21:57:59 -0400391 rootfs_size)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500392 exec_native_cmd(dosfs_cmd, native_sysroot)
393
394 mcopy_cmd = "mcopy -i %s -s %s/* ::/" % (rootfs, rootfs_dir)
395 exec_native_cmd(mcopy_cmd, native_sysroot)
396
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500397 if self.updated_fstab_path and self.has_fstab and not self.no_fstab_update:
Patrick Williams2390b1b2022-11-03 13:47:49 -0500398 mcopy_cmd = "mcopy -m -i %s %s ::/etc/fstab" % (rootfs, self.updated_fstab_path)
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600399 exec_native_cmd(mcopy_cmd, native_sysroot)
400
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500401 chmod_cmd = "chmod 644 %s" % rootfs
402 exec_cmd(chmod_cmd)
403
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500404 prepare_rootfs_vfat = prepare_rootfs_msdos
405
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600406 def prepare_rootfs_squashfs(self, rootfs, cr_workdir, oe_builddir, rootfs_dir,
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500407 native_sysroot, pseudo):
408 """
409 Prepare content for a squashfs rootfs partition.
410 """
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500411 extraopts = self.mkfs_extraopts or '-noappend'
412 squashfs_cmd = "mksquashfs %s %s %s" % \
413 (rootfs_dir, rootfs, extraopts)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500414 exec_native_cmd(squashfs_cmd, native_sysroot, pseudo=pseudo)
415
William A. Kennington IIIac69b482021-06-02 12:28:27 -0700416 def prepare_rootfs_erofs(self, rootfs, cr_workdir, oe_builddir, rootfs_dir,
417 native_sysroot, pseudo):
418 """
419 Prepare content for a erofs rootfs partition.
420 """
421 extraopts = self.mkfs_extraopts or ''
422 erofs_cmd = "mkfs.erofs %s -U %s %s %s" % \
423 (extraopts, self.fsuuid, rootfs, rootfs_dir)
424 exec_native_cmd(erofs_cmd, native_sysroot, pseudo=pseudo)
425
Andrew Geissler6aa7eec2023-03-03 12:41:14 -0600426 def prepare_empty_partition_none(self, rootfs, oe_builddir, native_sysroot):
427 pass
428
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500429 def prepare_empty_partition_ext(self, rootfs, oe_builddir,
430 native_sysroot):
431 """
432 Prepare an empty ext2/3/4 partition.
433 """
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500434 size = self.disk_size
435 with open(rootfs, 'w') as sparse:
436 os.ftruncate(sparse.fileno(), size * 1024)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500437
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500438 extraopts = self.mkfs_extraopts or "-i 8192"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500439
440 label_str = ""
441 if self.label:
442 label_str = "-L %s" % self.label
443
Brad Bishop316dfdd2018-06-25 12:45:53 -0400444 mkfs_cmd = "mkfs.%s -F %s %s -U %s %s" % \
445 (self.fstype, extraopts, label_str, self.fsuuid, rootfs)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500446 exec_native_cmd(mkfs_cmd, native_sysroot)
447
Andrew Geissler90fd73c2021-03-05 15:25:55 -0600448 self.check_for_Y2038_problem(rootfs, native_sysroot)
449
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500450 def prepare_empty_partition_btrfs(self, rootfs, oe_builddir,
451 native_sysroot):
452 """
453 Prepare an empty btrfs partition.
454 """
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500455 size = self.disk_size
456 with open(rootfs, 'w') as sparse:
457 os.ftruncate(sparse.fileno(), size * 1024)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500458
459 label_str = ""
460 if self.label:
461 label_str = "-L %s" % self.label
462
Brad Bishop316dfdd2018-06-25 12:45:53 -0400463 mkfs_cmd = "mkfs.%s -b %d %s -U %s %s %s" % \
464 (self.fstype, self.size * 1024, label_str, self.fsuuid,
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500465 self.mkfs_extraopts, rootfs)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500466 exec_native_cmd(mkfs_cmd, native_sysroot)
467
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500468 def prepare_empty_partition_msdos(self, rootfs, oe_builddir,
469 native_sysroot):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500470 """
471 Prepare an empty vfat partition.
472 """
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500473 blocks = self.disk_size
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500474
475 label_str = "-n boot"
476 if self.label:
477 label_str = "-n %s" % self.label
478
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500479 size_str = ""
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500480
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500481 extraopts = self.mkfs_extraopts or '-S 512'
482
Brad Bishop316dfdd2018-06-25 12:45:53 -0400483 dosfs_cmd = "mkdosfs %s -i %s %s %s -C %s %d" % \
484 (label_str, self.fsuuid, extraopts, size_str, rootfs,
485 blocks)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500486
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500487 exec_native_cmd(dosfs_cmd, native_sysroot)
488
489 chmod_cmd = "chmod 644 %s" % rootfs
490 exec_cmd(chmod_cmd)
491
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500492 prepare_empty_partition_vfat = prepare_empty_partition_msdos
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500493
494 def prepare_swap_partition(self, cr_workdir, oe_builddir, native_sysroot):
495 """
496 Prepare a swap partition.
497 """
498 path = "%s/fs.%s" % (cr_workdir, self.fstype)
499
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500500 with open(path, 'w') as sparse:
501 os.ftruncate(sparse.fileno(), self.size * 1024)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500502
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500503 label_str = ""
504 if self.label:
505 label_str = "-L %s" % self.label
Brad Bishop316dfdd2018-06-25 12:45:53 -0400506
507 mkswap_cmd = "mkswap %s -U %s %s" % (label_str, self.fsuuid, path)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500508 exec_native_cmd(mkswap_cmd, native_sysroot)
Andrew Geissler90fd73c2021-03-05 15:25:55 -0600509
510 def check_for_Y2038_problem(self, rootfs, native_sysroot):
511 """
512 Check if the filesystem is affected by the Y2038 problem
513 (Y2038 problem = 32 bit time_t overflow in January 2038)
514 """
515 def get_err_str(part):
516 err = "The {} filesystem {} has no Y2038 support."
517 if part.mountpoint:
518 args = [part.fstype, "mounted at %s" % part.mountpoint]
519 elif part.label:
520 args = [part.fstype, "labeled '%s'" % part.label]
521 elif part.part_name:
522 args = [part.fstype, "in partition '%s'" % part.part_name]
523 else:
524 args = [part.fstype, "in partition %s" % part.num]
525 return err.format(*args)
526
527 # ext2 and ext3 are always affected by the Y2038 problem
528 if self.fstype in ["ext2", "ext3"]:
529 logger.warn(get_err_str(self))
530 return
531
532 ret, out = exec_native_cmd("dumpe2fs %s" % rootfs, native_sysroot)
533
534 # if ext4 is affected by the Y2038 problem depends on the inode size
535 for line in out.splitlines():
536 if line.startswith("Inode size:"):
537 size = int(line.split(":")[1].strip())
538 if size < 256:
539 logger.warn("%s Inodes (of size %d) are too small." %
540 (get_err_str(self), size))
541 break
542