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