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