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 |
Andrew Geissler | 5082cc7 | 2023-09-11 08:41:39 -0400 | [diff] [blame] | 63 | self.mbr = args.mbr |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 64 | |
| 65 | self.lineno = lineno |
| 66 | self.source_file = "" |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 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 | """ |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 75 | logger.debug("Requested partition size for %s: %d", |
| 76 | self.mountpoint, self.size) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 77 | |
| 78 | if not self.size: |
| 79 | return 0 |
| 80 | |
| 81 | requested_blocks = self.size |
| 82 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 83 | logger.debug("Requested blocks %d, current_blocks %d", |
| 84 | requested_blocks, current_blocks) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 85 | |
| 86 | if requested_blocks > current_blocks: |
| 87 | return requested_blocks - current_blocks |
| 88 | else: |
| 89 | return 0 |
| 90 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 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 |
Andrew Geissler | 5199d83 | 2021-09-24 16:47:35 -0500 | [diff] [blame] | 111 | rootfs_size = int(rootfs_size * self.overhead_factor) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 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 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 127 | def prepare(self, creator, cr_workdir, oe_builddir, rootfs_dir, |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 128 | bootimg_dir, kernel_dir, native_sysroot, updated_fstab_path): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 129 | """ |
| 130 | Prepare content for individual partitions, depending on |
| 131 | partition command parameters. |
| 132 | """ |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 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 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 137 | if not self.source: |
Andrew Geissler | 5082cc7 | 2023-09-11 08:41:39 -0400 | [diff] [blame] | 138 | if self.fstype == "none" or self.no_table: |
Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame] | 139 | return |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 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": |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 146 | self.prepare_swap_partition(cr_workdir, oe_builddir, |
| 147 | native_sysroot) |
| 148 | self.source_file = "%s/fs.%s" % (cr_workdir, self.fstype) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 149 | else: |
William A. Kennington III | ac69b48 | 2021-06-02 12:28:27 -0700 | [diff] [blame] | 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)) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 153 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 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) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 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 |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 163 | return |
| 164 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 165 | plugins = PluginMgr.get_plugins('source') |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 166 | |
| 167 | if self.source not in plugins: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 168 | 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 173 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 174 | 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 Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 179 | 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] | 180 | |
| 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 Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 191 | plugin.do_post_partition(self, srcparams_dict, creator, |
| 192 | cr_workdir, oe_builddir, bootimg_dir, |
| 193 | kernel_dir, rootfs_dir, native_sysroot) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 194 | |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 195 | # further processing required Partition.size to be an integer, make |
| 196 | # sure that it is one |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 197 | 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 201 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 202 | 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 206 | |
| 207 | def prepare_rootfs(self, cr_workdir, oe_builddir, rootfs_dir, |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 208 | native_sysroot, real_rootfs = True, pseudo_dir = None): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 209 | """ |
| 210 | Prepare content for a rootfs partition i.e. create a partition |
| 211 | and fill it from a /rootfs dir. |
| 212 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 213 | Currently handles ext2/3/4, btrfs, vfat and squashfs. |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 214 | """ |
Andrew Geissler | f034379 | 2020-11-18 10:42:21 -0600 | [diff] [blame] | 215 | |
| 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 221 | p_prefix = os.environ.get("PSEUDO_PREFIX", "%s/usr" % native_sysroot) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 222 | if (pseudo_dir): |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 223 | # 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 Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 233 | 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 Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 237 | pseudo += "export PSEUDO_IGNORE_PATHS=%s;" % ignore_paths |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 238 | pseudo += "%s " % get_bitbake_var("FAKEROOTCMD") |
| 239 | else: |
| 240 | pseudo = None |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 241 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 242 | if not self.size and real_rootfs: |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 243 | # 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 Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 260 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 261 | prefix = "ext" if self.fstype.startswith("ext") else self.fstype |
| 262 | method = getattr(self, "prepare_rootfs_" + prefix) |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 263 | method(rootfs, cr_workdir, oe_builddir, rootfs_dir, native_sysroot, pseudo) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 264 | self.source_file = rootfs |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 265 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 266 | # 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 270 | |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 271 | def prepare_rootfs_ext(self, rootfs, cr_workdir, oe_builddir, rootfs_dir, |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 272 | 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 Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 280 | rootfs_size = self.get_rootfs_size(actual_rootfs_size) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 281 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 282 | with open(rootfs, 'w') as sparse: |
| 283 | os.ftruncate(sparse.fileno(), rootfs_size * 1024) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 284 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 285 | extraopts = self.mkfs_extraopts or "-F -i 8192" |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 286 | |
| 287 | label_str = "" |
| 288 | if self.label: |
| 289 | label_str = "-L %s" % self.label |
| 290 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 291 | mkfs_cmd = "mkfs.%s %s %s %s -U %s -d %s" % \ |
| 292 | (self.fstype, extraopts, rootfs, label_str, self.fsuuid, rootfs_dir) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 293 | exec_native_cmd(mkfs_cmd, native_sysroot, pseudo=pseudo) |
| 294 | |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 295 | 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] | 296 | debugfs_script_path = os.path.join(cr_workdir, "debugfs_script") |
| 297 | with open(debugfs_script_path, "w") as f: |
| 298 | f.write("cd etc\n") |
| 299 | f.write("rm fstab\n") |
| 300 | f.write("write %s fstab\n" % (self.updated_fstab_path)) |
| 301 | debugfs_cmd = "debugfs -w -f %s %s" % (debugfs_script_path, rootfs) |
| 302 | exec_native_cmd(debugfs_cmd, native_sysroot) |
| 303 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 304 | mkfs_cmd = "fsck.%s -pvfD %s" % (self.fstype, rootfs) |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 305 | exec_native_cmd(mkfs_cmd, native_sysroot, pseudo=pseudo) |
| 306 | |
Patrick Williams | 7784c42 | 2022-11-17 07:29:11 -0600 | [diff] [blame] | 307 | if os.getenv('SOURCE_DATE_EPOCH'): |
| 308 | sde_time = hex(int(os.getenv('SOURCE_DATE_EPOCH'))) |
| 309 | debugfs_script_path = os.path.join(cr_workdir, "debugfs_script") |
| 310 | files = [] |
| 311 | for root, dirs, others in os.walk(rootfs_dir): |
| 312 | base = root.replace(rootfs_dir, "").rstrip(os.sep) |
| 313 | files += [ "/" if base == "" else base ] |
| 314 | files += [ base + "/" + n for n in dirs + others ] |
| 315 | with open(debugfs_script_path, "w") as f: |
| 316 | f.write("set_current_time %s\n" % (sde_time)) |
| 317 | if self.updated_fstab_path and self.has_fstab and not self.no_fstab_update: |
| 318 | f.write("set_inode_field /etc/fstab mtime %s\n" % (sde_time)) |
| 319 | f.write("set_inode_field /etc/fstab mtime_extra 0\n") |
| 320 | for file in set(files): |
| 321 | for time in ["atime", "ctime", "crtime"]: |
| 322 | f.write("set_inode_field \"%s\" %s %s\n" % (file, time, sde_time)) |
| 323 | f.write("set_inode_field \"%s\" %s_extra 0\n" % (file, time)) |
| 324 | for time in ["wtime", "mkfs_time", "lastcheck"]: |
| 325 | f.write("set_super_value %s %s\n" % (time, sde_time)) |
| 326 | for time in ["mtime", "first_error_time", "last_error_time"]: |
| 327 | f.write("set_super_value %s 0\n" % (time)) |
| 328 | debugfs_cmd = "debugfs -w -f %s %s" % (debugfs_script_path, rootfs) |
| 329 | exec_native_cmd(debugfs_cmd, native_sysroot) |
| 330 | |
Andrew Geissler | 90fd73c | 2021-03-05 15:25:55 -0600 | [diff] [blame] | 331 | self.check_for_Y2038_problem(rootfs, native_sysroot) |
| 332 | |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 333 | def prepare_rootfs_btrfs(self, rootfs, cr_workdir, oe_builddir, rootfs_dir, |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 334 | native_sysroot, pseudo): |
| 335 | """ |
| 336 | Prepare content for a btrfs rootfs partition. |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 337 | """ |
| 338 | du_cmd = "du -ks %s" % rootfs_dir |
| 339 | out = exec_cmd(du_cmd) |
| 340 | actual_rootfs_size = int(out.split()[0]) |
| 341 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 342 | rootfs_size = self.get_rootfs_size(actual_rootfs_size) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 343 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 344 | with open(rootfs, 'w') as sparse: |
| 345 | os.ftruncate(sparse.fileno(), rootfs_size * 1024) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 346 | |
| 347 | label_str = "" |
| 348 | if self.label: |
| 349 | label_str = "-L %s" % self.label |
| 350 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 351 | 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] | 352 | (self.fstype, rootfs_size * 1024, rootfs_dir, label_str, |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 353 | self.mkfs_extraopts, self.fsuuid, rootfs) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 354 | exec_native_cmd(mkfs_cmd, native_sysroot, pseudo=pseudo) |
| 355 | |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 356 | def prepare_rootfs_msdos(self, rootfs, cr_workdir, oe_builddir, rootfs_dir, |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 357 | native_sysroot, pseudo): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 358 | """ |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 359 | Prepare content for a msdos/vfat rootfs partition. |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 360 | """ |
| 361 | du_cmd = "du -bks %s" % rootfs_dir |
| 362 | out = exec_cmd(du_cmd) |
| 363 | blocks = int(out.split()[0]) |
| 364 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 365 | rootfs_size = self.get_rootfs_size(blocks) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 366 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 367 | label_str = "-n boot" |
| 368 | if self.label: |
| 369 | label_str = "-n %s" % self.label |
| 370 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 371 | size_str = "" |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 372 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 373 | extraopts = self.mkfs_extraopts or '-S 512' |
| 374 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 375 | dosfs_cmd = "mkdosfs %s -i %s %s %s -C %s %d" % \ |
| 376 | (label_str, self.fsuuid, size_str, extraopts, rootfs, |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 377 | rootfs_size) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 378 | exec_native_cmd(dosfs_cmd, native_sysroot) |
| 379 | |
| 380 | mcopy_cmd = "mcopy -i %s -s %s/* ::/" % (rootfs, rootfs_dir) |
| 381 | exec_native_cmd(mcopy_cmd, native_sysroot) |
| 382 | |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 383 | 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] | 384 | 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] | 385 | exec_native_cmd(mcopy_cmd, native_sysroot) |
| 386 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 387 | chmod_cmd = "chmod 644 %s" % rootfs |
| 388 | exec_cmd(chmod_cmd) |
| 389 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 390 | prepare_rootfs_vfat = prepare_rootfs_msdos |
| 391 | |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 392 | def prepare_rootfs_squashfs(self, rootfs, cr_workdir, oe_builddir, rootfs_dir, |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 393 | native_sysroot, pseudo): |
| 394 | """ |
| 395 | Prepare content for a squashfs rootfs partition. |
| 396 | """ |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 397 | extraopts = self.mkfs_extraopts or '-noappend' |
| 398 | squashfs_cmd = "mksquashfs %s %s %s" % \ |
| 399 | (rootfs_dir, rootfs, extraopts) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 400 | exec_native_cmd(squashfs_cmd, native_sysroot, pseudo=pseudo) |
| 401 | |
William A. Kennington III | ac69b48 | 2021-06-02 12:28:27 -0700 | [diff] [blame] | 402 | def prepare_rootfs_erofs(self, rootfs, cr_workdir, oe_builddir, rootfs_dir, |
| 403 | native_sysroot, pseudo): |
| 404 | """ |
| 405 | Prepare content for a erofs rootfs partition. |
| 406 | """ |
| 407 | extraopts = self.mkfs_extraopts or '' |
| 408 | erofs_cmd = "mkfs.erofs %s -U %s %s %s" % \ |
| 409 | (extraopts, self.fsuuid, rootfs, rootfs_dir) |
| 410 | exec_native_cmd(erofs_cmd, native_sysroot, pseudo=pseudo) |
| 411 | |
Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame] | 412 | def prepare_empty_partition_none(self, rootfs, oe_builddir, native_sysroot): |
| 413 | pass |
| 414 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 415 | def prepare_empty_partition_ext(self, rootfs, oe_builddir, |
| 416 | native_sysroot): |
| 417 | """ |
| 418 | Prepare an empty ext2/3/4 partition. |
| 419 | """ |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 420 | size = self.disk_size |
| 421 | with open(rootfs, 'w') as sparse: |
| 422 | os.ftruncate(sparse.fileno(), size * 1024) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 423 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 424 | extraopts = self.mkfs_extraopts or "-i 8192" |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 425 | |
| 426 | label_str = "" |
| 427 | if self.label: |
| 428 | label_str = "-L %s" % self.label |
| 429 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 430 | mkfs_cmd = "mkfs.%s -F %s %s -U %s %s" % \ |
| 431 | (self.fstype, extraopts, label_str, self.fsuuid, rootfs) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 432 | exec_native_cmd(mkfs_cmd, native_sysroot) |
| 433 | |
Andrew Geissler | 90fd73c | 2021-03-05 15:25:55 -0600 | [diff] [blame] | 434 | self.check_for_Y2038_problem(rootfs, native_sysroot) |
| 435 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 436 | def prepare_empty_partition_btrfs(self, rootfs, oe_builddir, |
| 437 | native_sysroot): |
| 438 | """ |
| 439 | Prepare an empty btrfs partition. |
| 440 | """ |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 441 | size = self.disk_size |
| 442 | with open(rootfs, 'w') as sparse: |
| 443 | os.ftruncate(sparse.fileno(), size * 1024) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 444 | |
| 445 | label_str = "" |
| 446 | if self.label: |
| 447 | label_str = "-L %s" % self.label |
| 448 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 449 | mkfs_cmd = "mkfs.%s -b %d %s -U %s %s %s" % \ |
| 450 | (self.fstype, self.size * 1024, label_str, self.fsuuid, |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 451 | self.mkfs_extraopts, rootfs) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 452 | exec_native_cmd(mkfs_cmd, native_sysroot) |
| 453 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 454 | def prepare_empty_partition_msdos(self, rootfs, oe_builddir, |
| 455 | native_sysroot): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 456 | """ |
| 457 | Prepare an empty vfat partition. |
| 458 | """ |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 459 | blocks = self.disk_size |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 460 | |
| 461 | label_str = "-n boot" |
| 462 | if self.label: |
| 463 | label_str = "-n %s" % self.label |
| 464 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 465 | size_str = "" |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 466 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 467 | extraopts = self.mkfs_extraopts or '-S 512' |
| 468 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 469 | dosfs_cmd = "mkdosfs %s -i %s %s %s -C %s %d" % \ |
| 470 | (label_str, self.fsuuid, extraopts, size_str, rootfs, |
| 471 | blocks) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 472 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 473 | exec_native_cmd(dosfs_cmd, native_sysroot) |
| 474 | |
| 475 | chmod_cmd = "chmod 644 %s" % rootfs |
| 476 | exec_cmd(chmod_cmd) |
| 477 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 478 | prepare_empty_partition_vfat = prepare_empty_partition_msdos |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 479 | |
| 480 | def prepare_swap_partition(self, cr_workdir, oe_builddir, native_sysroot): |
| 481 | """ |
| 482 | Prepare a swap partition. |
| 483 | """ |
| 484 | path = "%s/fs.%s" % (cr_workdir, self.fstype) |
| 485 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 486 | with open(path, 'w') as sparse: |
| 487 | os.ftruncate(sparse.fileno(), self.size * 1024) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 488 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 489 | label_str = "" |
| 490 | if self.label: |
| 491 | label_str = "-L %s" % self.label |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 492 | |
| 493 | mkswap_cmd = "mkswap %s -U %s %s" % (label_str, self.fsuuid, path) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 494 | exec_native_cmd(mkswap_cmd, native_sysroot) |
Andrew Geissler | 90fd73c | 2021-03-05 15:25:55 -0600 | [diff] [blame] | 495 | |
| 496 | def check_for_Y2038_problem(self, rootfs, native_sysroot): |
| 497 | """ |
| 498 | Check if the filesystem is affected by the Y2038 problem |
| 499 | (Y2038 problem = 32 bit time_t overflow in January 2038) |
| 500 | """ |
| 501 | def get_err_str(part): |
| 502 | err = "The {} filesystem {} has no Y2038 support." |
| 503 | if part.mountpoint: |
| 504 | args = [part.fstype, "mounted at %s" % part.mountpoint] |
| 505 | elif part.label: |
| 506 | args = [part.fstype, "labeled '%s'" % part.label] |
| 507 | elif part.part_name: |
| 508 | args = [part.fstype, "in partition '%s'" % part.part_name] |
| 509 | else: |
| 510 | args = [part.fstype, "in partition %s" % part.num] |
| 511 | return err.format(*args) |
| 512 | |
| 513 | # ext2 and ext3 are always affected by the Y2038 problem |
| 514 | if self.fstype in ["ext2", "ext3"]: |
| 515 | logger.warn(get_err_str(self)) |
| 516 | return |
| 517 | |
| 518 | ret, out = exec_native_cmd("dumpe2fs %s" % rootfs, native_sysroot) |
| 519 | |
| 520 | # if ext4 is affected by the Y2038 problem depends on the inode size |
| 521 | for line in out.splitlines(): |
| 522 | if line.startswith("Inode size:"): |
| 523 | size = int(line.split(":")[1].strip()) |
| 524 | if size < 256: |
| 525 | logger.warn("%s Inodes (of size %d) are too small." % |
| 526 | (get_err_str(self), size)) |
| 527 | break |
| 528 | |