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