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