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