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