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