blob: 939e66731b70d2d8fb7bd5e62869e13f2c74fe77 [file] [log] [blame]
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001# 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 Bishop6e60e8b2018-02-01 10:27:11 -050027import logging
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050028import os
29import tempfile
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050030
Brad Bishop6e60e8b2018-02-01 10:27:11 -050031from wic import WicError
32from wic.utils.misc import exec_cmd, exec_native_cmd, get_bitbake_var
33from wic.pluginbase import PluginMgr
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050034
Brad Bishop6e60e8b2018-02-01 10:27:11 -050035logger = logging.getLogger('wic')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050036
Patrick Williamsc0f7c042017-02-23 20:41:17 -060037class Partition():
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050038
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 Bishop6e60e8b2018-02-01 10:27:11 -050044 self.device = None
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050045 self.extra_space = args.extra_space
Brad Bishop6e60e8b2018-02-01 10:27:11 -050046 self.exclude_path = args.exclude_path
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050047 self.fsopts = args.fsopts
48 self.fstype = args.fstype
49 self.label = args.label
50 self.mountpoint = args.mountpoint
51 self.no_table = args.no_table
Brad Bishop6e60e8b2018-02-01 10:27:11 -050052 self.num = None
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050053 self.overhead_factor = args.overhead_factor
54 self.part_type = args.part_type
55 self.rootfs_dir = args.rootfs_dir
56 self.size = args.size
Brad Bishop6e60e8b2018-02-01 10:27:11 -050057 self.fixed_size = args.fixed_size
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050058 self.source = args.source
59 self.sourceparams = args.sourceparams
Patrick Williamsc0f7c042017-02-23 20:41:17 -060060 self.system_id = args.system_id
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050061 self.use_uuid = args.use_uuid
62 self.uuid = args.uuid
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050063
64 self.lineno = lineno
65 self.source_file = ""
66 self.sourceparams_dict = {}
67
68 def get_extra_block_count(self, current_blocks):
69 """
70 The --size param is reflected in self.size (in kB), and we already
71 have current_blocks (1k) blocks, calculate and return the
72 number of (1k) blocks we need to add to get to --size, 0 if
73 we're already there or beyond.
74 """
Brad Bishop6e60e8b2018-02-01 10:27:11 -050075 logger.debug("Requested partition size for %s: %d",
76 self.mountpoint, self.size)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050077
78 if not self.size:
79 return 0
80
81 requested_blocks = self.size
82
Brad Bishop6e60e8b2018-02-01 10:27:11 -050083 logger.debug("Requested blocks %d, current_blocks %d",
84 requested_blocks, current_blocks)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050085
86 if requested_blocks > current_blocks:
87 return requested_blocks - current_blocks
88 else:
89 return 0
90
Brad Bishop6e60e8b2018-02-01 10:27:11 -050091 def get_rootfs_size(self, actual_rootfs_size=0):
92 """
93 Calculate the required size of rootfs taking into consideration
94 --size/--fixed-size flags as well as overhead and extra space, as
95 specified in kickstart file. Raises an error if the
96 `actual_rootfs_size` is larger than fixed-size rootfs.
97
98 """
99 if self.fixed_size:
100 rootfs_size = self.fixed_size
101 if actual_rootfs_size > rootfs_size:
102 raise WicError("Actual rootfs size (%d kB) is larger than "
103 "allowed size %d kB" %
104 (actual_rootfs_size, rootfs_size))
105 else:
106 extra_blocks = self.get_extra_block_count(actual_rootfs_size)
107 if extra_blocks < self.extra_space:
108 extra_blocks = self.extra_space
109
110 rootfs_size = actual_rootfs_size + extra_blocks
111 rootfs_size *= self.overhead_factor
112
113 logger.debug("Added %d extra blocks to %s to get to %d total blocks",
114 extra_blocks, self.mountpoint, rootfs_size)
115
116 return rootfs_size
117
118 @property
119 def disk_size(self):
120 """
121 Obtain on-disk size of partition taking into consideration
122 --size/--fixed-size options.
123
124 """
125 return self.fixed_size if self.fixed_size else self.size
126
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500127 def prepare(self, creator, cr_workdir, oe_builddir, rootfs_dir,
128 bootimg_dir, kernel_dir, native_sysroot):
129 """
130 Prepare content for individual partitions, depending on
131 partition command parameters.
132 """
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500133 if not self.source:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500134 if not self.size and not self.fixed_size:
135 raise WicError("The %s partition has a size of zero. Please "
136 "specify a non-zero --size/--fixed-size for that "
137 "partition." % self.mountpoint)
138
139 if self.fstype == "swap":
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500140 self.prepare_swap_partition(cr_workdir, oe_builddir,
141 native_sysroot)
142 self.source_file = "%s/fs.%s" % (cr_workdir, self.fstype)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500143 else:
144 if self.fstype == 'squashfs':
145 raise WicError("It's not possible to create empty squashfs "
146 "partition '%s'" % (self.mountpoint))
147
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500148 rootfs = "%s/fs_%s.%s.%s" % (cr_workdir, self.label,
149 self.lineno, self.fstype)
150 if os.path.isfile(rootfs):
151 os.remove(rootfs)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500152
153 prefix = "ext" if self.fstype.startswith("ext") else self.fstype
154 method = getattr(self, "prepare_empty_partition_" + prefix)
155 method(rootfs, oe_builddir, native_sysroot)
156 self.source_file = rootfs
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500157 return
158
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500159 plugins = PluginMgr.get_plugins('source')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500160
161 if self.source not in plugins:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500162 raise WicError("The '%s' --source specified for %s doesn't exist.\n\t"
163 "See 'wic list source-plugins' for a list of available"
164 " --sources.\n\tSee 'wic help source-plugins' for "
165 "details on adding a new source plugin." %
166 (self.source, self.mountpoint))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500167
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500168 srcparams_dict = {}
169 if self.sourceparams:
170 # Split sourceparams string of the form key1=val1[,key2=val2,...]
171 # into a dict. Also accepts valueless keys i.e. without =
172 splitted = self.sourceparams.split(',')
173 srcparams_dict = dict(par.split('=') for par in splitted if par)
174
175 plugin = PluginMgr.get_plugins('source')[self.source]
176 plugin.do_configure_partition(self, srcparams_dict, creator,
177 cr_workdir, oe_builddir, bootimg_dir,
178 kernel_dir, native_sysroot)
179 plugin.do_stage_partition(self, srcparams_dict, creator,
180 cr_workdir, oe_builddir, bootimg_dir,
181 kernel_dir, native_sysroot)
182 plugin.do_prepare_partition(self, srcparams_dict, creator,
183 cr_workdir, oe_builddir, bootimg_dir,
184 kernel_dir, rootfs_dir, native_sysroot)
185
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500186 # further processing required Partition.size to be an integer, make
187 # sure that it is one
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500188 if not isinstance(self.size, int):
189 raise WicError("Partition %s internal size is not an integer. "
190 "This a bug in source plugin %s and needs to be fixed." %
191 (self.mountpoint, self.source))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500192
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500193 if self.fixed_size and self.size > self.fixed_size:
194 raise WicError("File system image of partition %s is "
195 "larger (%d kB) than its allowed size %d kB" %
196 (self.mountpoint, self.size, self.fixed_size))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500197
198 def prepare_rootfs(self, cr_workdir, oe_builddir, rootfs_dir,
199 native_sysroot):
200 """
201 Prepare content for a rootfs partition i.e. create a partition
202 and fill it from a /rootfs dir.
203
204 Currently handles ext2/3/4, btrfs and vfat.
205 """
206 p_prefix = os.environ.get("PSEUDO_PREFIX", "%s/usr" % native_sysroot)
207 p_localstatedir = os.environ.get("PSEUDO_LOCALSTATEDIR",
208 "%s/../pseudo" % rootfs_dir)
209 p_passwd = os.environ.get("PSEUDO_PASSWD", rootfs_dir)
210 p_nosymlinkexp = os.environ.get("PSEUDO_NOSYMLINKEXP", "1")
211 pseudo = "export PSEUDO_PREFIX=%s;" % p_prefix
212 pseudo += "export PSEUDO_LOCALSTATEDIR=%s;" % p_localstatedir
213 pseudo += "export PSEUDO_PASSWD=%s;" % p_passwd
214 pseudo += "export PSEUDO_NOSYMLINKEXP=%s;" % p_nosymlinkexp
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500215 pseudo += "%s " % get_bitbake_var("FAKEROOTCMD")
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500216
217 rootfs = "%s/rootfs_%s.%s.%s" % (cr_workdir, self.label,
218 self.lineno, self.fstype)
219 if os.path.isfile(rootfs):
220 os.remove(rootfs)
221
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500222 # Get rootfs size from bitbake variable if it's not set in .ks file
223 if not self.size:
224 # Bitbake variable ROOTFS_SIZE is calculated in
225 # Image._get_rootfs_size method from meta/lib/oe/image.py
226 # using IMAGE_ROOTFS_SIZE, IMAGE_ROOTFS_ALIGNMENT,
227 # IMAGE_OVERHEAD_FACTOR and IMAGE_ROOTFS_EXTRA_SPACE
228 rsize_bb = get_bitbake_var('ROOTFS_SIZE')
229 if rsize_bb:
230 logger.warning('overhead-factor was specified, but size was not,'
231 ' so bitbake variables will be used for the size.'
232 ' In this case both IMAGE_OVERHEAD_FACTOR and '
233 '--overhead-factor will be applied')
234 self.size = int(round(float(rsize_bb)))
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500235
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500236 prefix = "ext" if self.fstype.startswith("ext") else self.fstype
237 method = getattr(self, "prepare_rootfs_" + prefix)
238 method(rootfs, oe_builddir, rootfs_dir, native_sysroot, pseudo)
239 self.source_file = rootfs
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500240
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500241 # get the rootfs size in the right units for kickstart (kB)
242 du_cmd = "du -Lbks %s" % rootfs
243 out = exec_cmd(du_cmd)
244 self.size = int(out.split()[0])
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500245
246 def prepare_rootfs_ext(self, rootfs, oe_builddir, rootfs_dir,
247 native_sysroot, pseudo):
248 """
249 Prepare content for an ext2/3/4 rootfs partition.
250 """
251 du_cmd = "du -ks %s" % rootfs_dir
252 out = exec_cmd(du_cmd)
253 actual_rootfs_size = int(out.split()[0])
254
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500255 rootfs_size = self.get_rootfs_size(actual_rootfs_size)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500256
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500257 with open(rootfs, 'w') as sparse:
258 os.ftruncate(sparse.fileno(), rootfs_size * 1024)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500259
260 extra_imagecmd = "-i 8192"
261
262 label_str = ""
263 if self.label:
264 label_str = "-L %s" % self.label
265
266 mkfs_cmd = "mkfs.%s -F %s %s %s -d %s" % \
267 (self.fstype, extra_imagecmd, rootfs, label_str, rootfs_dir)
268 exec_native_cmd(mkfs_cmd, native_sysroot, pseudo=pseudo)
269
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500270 mkfs_cmd = "fsck.%s -pvfD %s" % (self.fstype, rootfs)
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500271 exec_native_cmd(mkfs_cmd, native_sysroot, pseudo=pseudo)
272
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500273 def prepare_rootfs_btrfs(self, rootfs, oe_builddir, rootfs_dir,
274 native_sysroot, pseudo):
275 """
276 Prepare content for a btrfs rootfs partition.
277
278 Currently handles ext2/3/4 and btrfs.
279 """
280 du_cmd = "du -ks %s" % rootfs_dir
281 out = exec_cmd(du_cmd)
282 actual_rootfs_size = int(out.split()[0])
283
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500284 rootfs_size = self.get_rootfs_size(actual_rootfs_size)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500285
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500286 with open(rootfs, 'w') as sparse:
287 os.ftruncate(sparse.fileno(), rootfs_size * 1024)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500288
289 label_str = ""
290 if self.label:
291 label_str = "-L %s" % self.label
292
293 mkfs_cmd = "mkfs.%s -b %d -r %s %s %s" % \
294 (self.fstype, rootfs_size * 1024, rootfs_dir, label_str, rootfs)
295 exec_native_cmd(mkfs_cmd, native_sysroot, pseudo=pseudo)
296
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500297 def prepare_rootfs_msdos(self, rootfs, oe_builddir, rootfs_dir,
298 native_sysroot, pseudo):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500299 """
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500300 Prepare content for a msdos/vfat rootfs partition.
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500301 """
302 du_cmd = "du -bks %s" % rootfs_dir
303 out = exec_cmd(du_cmd)
304 blocks = int(out.split()[0])
305
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500306 rootfs_size = self.get_rootfs_size(blocks)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500307
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500308 label_str = "-n boot"
309 if self.label:
310 label_str = "-n %s" % self.label
311
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500312 size_str = ""
313 if self.fstype == 'msdos':
314 size_str = "-F 16" # FAT 16
315
316 dosfs_cmd = "mkdosfs %s -S 512 %s -C %s %d" % (label_str, size_str,
317 rootfs, rootfs_size)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500318 exec_native_cmd(dosfs_cmd, native_sysroot)
319
320 mcopy_cmd = "mcopy -i %s -s %s/* ::/" % (rootfs, rootfs_dir)
321 exec_native_cmd(mcopy_cmd, native_sysroot)
322
323 chmod_cmd = "chmod 644 %s" % rootfs
324 exec_cmd(chmod_cmd)
325
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500326 prepare_rootfs_vfat = prepare_rootfs_msdos
327
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500328 def prepare_rootfs_squashfs(self, rootfs, oe_builddir, rootfs_dir,
329 native_sysroot, pseudo):
330 """
331 Prepare content for a squashfs rootfs partition.
332 """
333 squashfs_cmd = "mksquashfs %s %s -noappend" % \
334 (rootfs_dir, rootfs)
335 exec_native_cmd(squashfs_cmd, native_sysroot, pseudo=pseudo)
336
337 def prepare_empty_partition_ext(self, rootfs, oe_builddir,
338 native_sysroot):
339 """
340 Prepare an empty ext2/3/4 partition.
341 """
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500342 size = self.disk_size
343 with open(rootfs, 'w') as sparse:
344 os.ftruncate(sparse.fileno(), size * 1024)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500345
346 extra_imagecmd = "-i 8192"
347
348 label_str = ""
349 if self.label:
350 label_str = "-L %s" % self.label
351
352 mkfs_cmd = "mkfs.%s -F %s %s %s" % \
353 (self.fstype, extra_imagecmd, label_str, rootfs)
354 exec_native_cmd(mkfs_cmd, native_sysroot)
355
356 def prepare_empty_partition_btrfs(self, rootfs, oe_builddir,
357 native_sysroot):
358 """
359 Prepare an empty btrfs partition.
360 """
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500361 size = self.disk_size
362 with open(rootfs, 'w') as sparse:
363 os.ftruncate(sparse.fileno(), size * 1024)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500364
365 label_str = ""
366 if self.label:
367 label_str = "-L %s" % self.label
368
369 mkfs_cmd = "mkfs.%s -b %d %s %s" % \
370 (self.fstype, self.size * 1024, label_str, rootfs)
371 exec_native_cmd(mkfs_cmd, native_sysroot)
372
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500373 def prepare_empty_partition_msdos(self, rootfs, oe_builddir,
374 native_sysroot):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500375 """
376 Prepare an empty vfat partition.
377 """
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500378 blocks = self.disk_size
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500379
380 label_str = "-n boot"
381 if self.label:
382 label_str = "-n %s" % self.label
383
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500384 size_str = ""
385 if self.fstype == 'msdos':
386 size_str = "-F 16" # FAT 16
387
388 dosfs_cmd = "mkdosfs %s -S 512 %s -C %s %d" % (label_str, size_str,
389 rootfs, blocks)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500390 exec_native_cmd(dosfs_cmd, native_sysroot)
391
392 chmod_cmd = "chmod 644 %s" % rootfs
393 exec_cmd(chmod_cmd)
394
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500395 prepare_empty_partition_vfat = prepare_empty_partition_msdos
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500396
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 Bishop6e60e8b2018-02-01 10:27:11 -0500403 with open(path, 'w') as sparse:
404 os.ftruncate(sparse.fileno(), self.size * 1024)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500405
406 import uuid
407 label_str = ""
408 if self.label:
409 label_str = "-L %s" % self.label
410 mkswap_cmd = "mkswap %s -U %s %s" % (label_str, str(uuid.uuid1()), path)
411 exec_native_cmd(mkswap_cmd, native_sysroot)