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