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 | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 215 | # If size is not specified compute it from the rootfs_dir size |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 216 | if not self.size and real_rootfs: |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 217 | # Use the same logic found in get_rootfs_size() |
| 218 | # from meta/classes/image.bbclass |
| 219 | du_cmd = "du -ks %s" % rootfs_dir |
| 220 | out = exec_cmd(du_cmd) |
| 221 | self.size = int(out.split()[0]) |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 222 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 223 | prefix = "ext" if self.fstype.startswith("ext") else self.fstype |
| 224 | method = getattr(self, "prepare_rootfs_" + prefix) |
| 225 | method(rootfs, oe_builddir, rootfs_dir, native_sysroot, pseudo) |
| 226 | self.source_file = rootfs |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 227 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 228 | # get the rootfs size in the right units for kickstart (kB) |
| 229 | du_cmd = "du -Lbks %s" % rootfs |
| 230 | out = exec_cmd(du_cmd) |
| 231 | self.size = int(out.split()[0]) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 232 | |
| 233 | def prepare_rootfs_ext(self, rootfs, oe_builddir, rootfs_dir, |
| 234 | native_sysroot, pseudo): |
| 235 | """ |
| 236 | Prepare content for an ext2/3/4 rootfs partition. |
| 237 | """ |
| 238 | du_cmd = "du -ks %s" % rootfs_dir |
| 239 | out = exec_cmd(du_cmd) |
| 240 | actual_rootfs_size = int(out.split()[0]) |
| 241 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 242 | rootfs_size = self.get_rootfs_size(actual_rootfs_size) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 243 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 244 | with open(rootfs, 'w') as sparse: |
| 245 | os.ftruncate(sparse.fileno(), rootfs_size * 1024) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 246 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 247 | extraopts = self.mkfs_extraopts or "-F -i 8192" |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 248 | |
| 249 | label_str = "" |
| 250 | if self.label: |
| 251 | label_str = "-L %s" % self.label |
| 252 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 253 | mkfs_cmd = "mkfs.%s %s %s %s -U %s -d %s" % \ |
| 254 | (self.fstype, extraopts, rootfs, label_str, self.fsuuid, rootfs_dir) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 255 | exec_native_cmd(mkfs_cmd, native_sysroot, pseudo=pseudo) |
| 256 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 257 | mkfs_cmd = "fsck.%s -pvfD %s" % (self.fstype, rootfs) |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 258 | exec_native_cmd(mkfs_cmd, native_sysroot, pseudo=pseudo) |
| 259 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 260 | def prepare_rootfs_btrfs(self, rootfs, oe_builddir, rootfs_dir, |
| 261 | native_sysroot, pseudo): |
| 262 | """ |
| 263 | Prepare content for a btrfs rootfs partition. |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 264 | """ |
| 265 | du_cmd = "du -ks %s" % rootfs_dir |
| 266 | out = exec_cmd(du_cmd) |
| 267 | actual_rootfs_size = int(out.split()[0]) |
| 268 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 269 | rootfs_size = self.get_rootfs_size(actual_rootfs_size) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 270 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 271 | with open(rootfs, 'w') as sparse: |
| 272 | os.ftruncate(sparse.fileno(), rootfs_size * 1024) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 273 | |
| 274 | label_str = "" |
| 275 | if self.label: |
| 276 | label_str = "-L %s" % self.label |
| 277 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 278 | 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] | 279 | (self.fstype, rootfs_size * 1024, rootfs_dir, label_str, |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 280 | self.mkfs_extraopts, self.fsuuid, rootfs) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 281 | exec_native_cmd(mkfs_cmd, native_sysroot, pseudo=pseudo) |
| 282 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 283 | def prepare_rootfs_msdos(self, rootfs, oe_builddir, rootfs_dir, |
| 284 | native_sysroot, pseudo): |
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 | Prepare content for a msdos/vfat rootfs partition. |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 287 | """ |
| 288 | du_cmd = "du -bks %s" % rootfs_dir |
| 289 | out = exec_cmd(du_cmd) |
| 290 | blocks = int(out.split()[0]) |
| 291 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 292 | rootfs_size = self.get_rootfs_size(blocks) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 293 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 294 | label_str = "-n boot" |
| 295 | if self.label: |
| 296 | label_str = "-n %s" % self.label |
| 297 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 298 | size_str = "" |
| 299 | if self.fstype == 'msdos': |
| 300 | size_str = "-F 16" # FAT 16 |
| 301 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 302 | extraopts = self.mkfs_extraopts or '-S 512' |
| 303 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 304 | dosfs_cmd = "mkdosfs %s -i %s %s %s -C %s %d" % \ |
| 305 | (label_str, self.fsuuid, size_str, extraopts, rootfs, |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 306 | rootfs_size) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 307 | exec_native_cmd(dosfs_cmd, native_sysroot) |
| 308 | |
| 309 | mcopy_cmd = "mcopy -i %s -s %s/* ::/" % (rootfs, rootfs_dir) |
| 310 | exec_native_cmd(mcopy_cmd, native_sysroot) |
| 311 | |
| 312 | chmod_cmd = "chmod 644 %s" % rootfs |
| 313 | exec_cmd(chmod_cmd) |
| 314 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 315 | prepare_rootfs_vfat = prepare_rootfs_msdos |
| 316 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 317 | def prepare_rootfs_squashfs(self, rootfs, oe_builddir, rootfs_dir, |
| 318 | native_sysroot, pseudo): |
| 319 | """ |
| 320 | Prepare content for a squashfs rootfs partition. |
| 321 | """ |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 322 | extraopts = self.mkfs_extraopts or '-noappend' |
| 323 | squashfs_cmd = "mksquashfs %s %s %s" % \ |
| 324 | (rootfs_dir, rootfs, extraopts) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 325 | exec_native_cmd(squashfs_cmd, native_sysroot, pseudo=pseudo) |
| 326 | |
| 327 | def prepare_empty_partition_ext(self, rootfs, oe_builddir, |
| 328 | native_sysroot): |
| 329 | """ |
| 330 | Prepare an empty ext2/3/4 partition. |
| 331 | """ |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 332 | size = self.disk_size |
| 333 | with open(rootfs, 'w') as sparse: |
| 334 | os.ftruncate(sparse.fileno(), size * 1024) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 335 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 336 | extraopts = self.mkfs_extraopts or "-i 8192" |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 337 | |
| 338 | label_str = "" |
| 339 | if self.label: |
| 340 | label_str = "-L %s" % self.label |
| 341 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 342 | mkfs_cmd = "mkfs.%s -F %s %s -U %s %s" % \ |
| 343 | (self.fstype, extraopts, label_str, self.fsuuid, rootfs) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 344 | exec_native_cmd(mkfs_cmd, native_sysroot) |
| 345 | |
| 346 | def prepare_empty_partition_btrfs(self, rootfs, oe_builddir, |
| 347 | native_sysroot): |
| 348 | """ |
| 349 | Prepare an empty btrfs partition. |
| 350 | """ |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 351 | size = self.disk_size |
| 352 | with open(rootfs, 'w') as sparse: |
| 353 | os.ftruncate(sparse.fileno(), size * 1024) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 354 | |
| 355 | label_str = "" |
| 356 | if self.label: |
| 357 | label_str = "-L %s" % self.label |
| 358 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 359 | mkfs_cmd = "mkfs.%s -b %d %s -U %s %s %s" % \ |
| 360 | (self.fstype, self.size * 1024, label_str, self.fsuuid, |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 361 | self.mkfs_extraopts, rootfs) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 362 | exec_native_cmd(mkfs_cmd, native_sysroot) |
| 363 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 364 | def prepare_empty_partition_msdos(self, rootfs, oe_builddir, |
| 365 | native_sysroot): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 366 | """ |
| 367 | Prepare an empty vfat partition. |
| 368 | """ |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 369 | blocks = self.disk_size |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 370 | |
| 371 | label_str = "-n boot" |
| 372 | if self.label: |
| 373 | label_str = "-n %s" % self.label |
| 374 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 375 | size_str = "" |
| 376 | if self.fstype == 'msdos': |
| 377 | size_str = "-F 16" # FAT 16 |
| 378 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 379 | extraopts = self.mkfs_extraopts or '-S 512' |
| 380 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 381 | dosfs_cmd = "mkdosfs %s -i %s %s %s -C %s %d" % \ |
| 382 | (label_str, self.fsuuid, extraopts, size_str, rootfs, |
| 383 | blocks) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 384 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 385 | exec_native_cmd(dosfs_cmd, native_sysroot) |
| 386 | |
| 387 | chmod_cmd = "chmod 644 %s" % rootfs |
| 388 | exec_cmd(chmod_cmd) |
| 389 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 390 | prepare_empty_partition_vfat = prepare_empty_partition_msdos |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 391 | |
| 392 | def prepare_swap_partition(self, cr_workdir, oe_builddir, native_sysroot): |
| 393 | """ |
| 394 | Prepare a swap partition. |
| 395 | """ |
| 396 | path = "%s/fs.%s" % (cr_workdir, self.fstype) |
| 397 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 398 | with open(path, 'w') as sparse: |
| 399 | os.ftruncate(sparse.fileno(), self.size * 1024) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 400 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 401 | label_str = "" |
| 402 | if self.label: |
| 403 | label_str = "-L %s" % self.label |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 404 | |
| 405 | mkswap_cmd = "mkswap %s -U %s %s" % (label_str, self.fsuuid, path) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 406 | exec_native_cmd(mkswap_cmd, native_sysroot) |