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