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