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