blob: eee25a493dd9d6929a974abdb50cdcd2006d521c [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -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, 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#
26
27import os
28import tempfile
29import uuid
30from optparse import OptionValueError
31
32from pykickstart.commands.partition import FC4_PartData, FC4_Partition
33from wic.utils.oe.misc import msger, parse_sourceparams
34from wic.utils.oe.misc import exec_cmd, exec_native_cmd
35from wic.plugin import pluginmgr
36
37partition_methods = {
38 "do_stage_partition":None,
39 "do_prepare_partition":None,
40 "do_configure_partition":None,
41}
42
43class Wic_PartData(FC4_PartData):
44 removedKeywords = FC4_PartData.removedKeywords
45 removedAttrs = FC4_PartData.removedAttrs
46
47 def __init__(self, *args, **kwargs):
48 FC4_PartData.__init__(self, *args, **kwargs)
49 self.deleteRemovedAttrs()
50 self.align = kwargs.get("align", None)
51 self.extopts = kwargs.get("extopts", None)
52 self.part_type = kwargs.get("part_type", None)
53 self.source = kwargs.get("source", None)
54 self.sourceparams = kwargs.get("sourceparams", None)
55 self.rootfs = kwargs.get("rootfs-dir", None)
56 self.no_table = kwargs.get("no-table", False)
57 self.extra_space = kwargs.get("extra-space", "10M")
58 self.overhead_factor = kwargs.get("overhead-factor", 1.3)
59 self._use_uuid = False
60 self.uuid = kwargs.get("uuid", None)
61 self.use_uuid = kwargs.get("use-uuid", False)
62 self.source_file = ""
63 self.size = 0
64
65 def _getArgsAsStr(self):
66 retval = FC4_PartData._getArgsAsStr(self)
67
68 if self.align:
69 retval += " --align=%d" % self.align
70 if self.extopts:
71 retval += " --extoptions=%s" % self.extopts
72 if self.part_type:
73 retval += " --part-type=%s" % self.part_type
74 if self.source:
75 retval += " --source=%s" % self.source
76 if self.sourceparams:
77 retval += " --sourceparams=%s" % self.sourceparams
78 if self.rootfs:
79 retval += " --rootfs-dir=%s" % self.rootfs
80 if self.no_table:
81 retval += " --no-table"
82 if self.use_uuid:
83 retval += " --use-uuid"
84 if self.uuid:
85 retval += " --uuid=%s" % self.uuid
86 retval += " --extra-space=%s" % self.extra_space
87 retval += " --overhead-factor=%f" % self.overhead_factor
88
89 return retval
90
91 @property
92 def use_uuid(self):
93 return self._use_uuid
94
95 @use_uuid.setter
96 def use_uuid(self, value):
97 self._use_uuid = value
98 if value and not self.uuid:
99 self.uuid = str(uuid.uuid4())
100
101 def get_rootfs(self):
102 """
103 Acessor for rootfs dir
104 """
105 return self.rootfs
106
107 def set_rootfs(self, rootfs):
108 """
109 Acessor for actual rootfs dir, which must be set by source
110 plugins.
111 """
112 self.rootfs = rootfs
113
114 def get_size(self):
115 """
116 Accessor for partition size, 0 or --size before set_size().
117 """
118 return self.size
119
120 def set_size(self, size):
121 """
122 Accessor for actual partition size, which must be set by source
123 plugins.
124 """
125 self.size = size
126
127 def set_source_file(self, source_file):
128 """
129 Accessor for source_file, the location of the generated partition
130 image, which must be set by source plugins.
131 """
132 self.source_file = source_file
133
134 def get_extra_block_count(self, current_blocks):
135 """
136 The --size param is reflected in self.size (in kB), and we already
137 have current_blocks (1k) blocks, calculate and return the
138 number of (1k) blocks we need to add to get to --size, 0 if
139 we're already there or beyond.
140 """
141 msger.debug("Requested partition size for %s: %d" % \
142 (self.mountpoint, self.size))
143
144 if not self.size:
145 return 0
146
147 requested_blocks = self.size
148
149 msger.debug("Requested blocks %d, current_blocks %d" % \
150 (requested_blocks, current_blocks))
151
152 if requested_blocks > current_blocks:
153 return requested_blocks - current_blocks
154 else:
155 return 0
156
157 def prepare(self, creator, cr_workdir, oe_builddir, rootfs_dir, bootimg_dir,
158 kernel_dir, native_sysroot):
159 """
160 Prepare content for individual partitions, depending on
161 partition command parameters.
162 """
163 self.sourceparams_dict = {}
164
165 if self.sourceparams:
166 self.sourceparams_dict = parse_sourceparams(self.sourceparams)
167
168 if not self.source:
169 if not self.size:
170 msger.error("The %s partition has a size of zero. Please "
171 "specify a non-zero --size for that partition." % \
172 self.mountpoint)
173 if self.fstype and self.fstype == "swap":
174 self.prepare_swap_partition(cr_workdir, oe_builddir,
175 native_sysroot)
176 elif self.fstype:
177 rootfs = "%s/fs_%s.%s.%s" % (cr_workdir, self.label,
178 self.lineno, self.fstype)
179 if os.path.isfile(rootfs):
180 os.remove(rootfs)
181 for prefix in ("ext", "btrfs", "vfat", "squashfs"):
182 if self.fstype.startswith(prefix):
183 method = getattr(self,
184 "prepare_empty_partition_" + prefix)
185 method(rootfs, oe_builddir, native_sysroot)
186 self.source_file = rootfs
187 break
188 return
189
190 plugins = pluginmgr.get_source_plugins()
191
192 if self.source not in plugins:
193 msger.error("The '%s' --source specified for %s doesn't exist.\n\t"
194 "See 'wic list source-plugins' for a list of available"
195 " --sources.\n\tSee 'wic help source-plugins' for "
196 "details on adding a new source plugin." % \
197 (self.source, self.mountpoint))
198
199 self._source_methods = pluginmgr.get_source_plugin_methods(\
200 self.source, partition_methods)
201 self._source_methods["do_configure_partition"](self, self.sourceparams_dict,
202 creator, cr_workdir,
203 oe_builddir,
204 bootimg_dir,
205 kernel_dir,
206 native_sysroot)
207 self._source_methods["do_stage_partition"](self, self.sourceparams_dict,
208 creator, cr_workdir,
209 oe_builddir,
210 bootimg_dir, kernel_dir,
211 native_sysroot)
212 self._source_methods["do_prepare_partition"](self, self.sourceparams_dict,
213 creator, cr_workdir,
214 oe_builddir,
215 bootimg_dir, kernel_dir, rootfs_dir,
216 native_sysroot)
217
218 def prepare_rootfs_from_fs_image(self, cr_workdir, oe_builddir,
219 rootfs_dir):
220 """
221 Handle an already-created partition e.g. xxx.ext3
222 """
223 rootfs = oe_builddir
224 du_cmd = "du -Lbks %s" % rootfs
225 out = exec_cmd(du_cmd)
226 rootfs_size = out.split()[0]
227
228 self.size = rootfs_size
229 self.source_file = rootfs
230
231 def prepare_rootfs(self, cr_workdir, oe_builddir, rootfs_dir,
232 native_sysroot):
233 """
234 Prepare content for a rootfs partition i.e. create a partition
235 and fill it from a /rootfs dir.
236
237 Currently handles ext2/3/4, btrfs and vfat.
238 """
239 p_prefix = os.environ.get("PSEUDO_PREFIX", "%s/usr" % native_sysroot)
240 p_localstatedir = os.environ.get("PSEUDO_LOCALSTATEDIR",
241 "%s/../pseudo" % rootfs_dir)
242 p_passwd = os.environ.get("PSEUDO_PASSWD", rootfs_dir)
243 p_nosymlinkexp = os.environ.get("PSEUDO_NOSYMLINKEXP", "1")
244 pseudo = "export PSEUDO_PREFIX=%s;" % p_prefix
245 pseudo += "export PSEUDO_LOCALSTATEDIR=%s;" % p_localstatedir
246 pseudo += "export PSEUDO_PASSWD=%s;" % p_passwd
247 pseudo += "export PSEUDO_NOSYMLINKEXP=%s;" % p_nosymlinkexp
248 pseudo += "%s/usr/bin/pseudo " % native_sysroot
249
250 rootfs = "%s/rootfs_%s.%s.%s" % (cr_workdir, self.label,
251 self.lineno, self.fstype)
252 if os.path.isfile(rootfs):
253 os.remove(rootfs)
254
255 for prefix in ("ext", "btrfs", "vfat", "squashfs"):
256 if self.fstype.startswith(prefix):
257 method = getattr(self, "prepare_rootfs_" + prefix)
258 method(rootfs, oe_builddir, rootfs_dir, native_sysroot, pseudo)
259
260 self.source_file = rootfs
261
262 # get the rootfs size in the right units for kickstart (kB)
263 du_cmd = "du -Lbks %s" % rootfs
264 out = exec_cmd(du_cmd)
265 self.size = out.split()[0]
266
267 break
268
269 def prepare_rootfs_ext(self, rootfs, oe_builddir, rootfs_dir,
270 native_sysroot, pseudo):
271 """
272 Prepare content for an ext2/3/4 rootfs partition.
273 """
274 du_cmd = "du -ks %s" % rootfs_dir
275 out = exec_cmd(du_cmd)
276 actual_rootfs_size = int(out.split()[0])
277
278 extra_blocks = self.get_extra_block_count(actual_rootfs_size)
279 if extra_blocks < self.extra_space:
280 extra_blocks = self.extra_space
281
282 rootfs_size = actual_rootfs_size + extra_blocks
283 rootfs_size *= self.overhead_factor
284
285 msger.debug("Added %d extra blocks to %s to get to %d total blocks" % \
286 (extra_blocks, self.mountpoint, rootfs_size))
287
288 dd_cmd = "dd if=/dev/zero of=%s bs=1024 seek=%d count=0 bs=1k" % \
289 (rootfs, rootfs_size)
290 exec_cmd(dd_cmd)
291
292 extra_imagecmd = "-i 8192"
293
294 label_str = ""
295 if self.label:
296 label_str = "-L %s" % self.label
297
298 mkfs_cmd = "mkfs.%s -F %s %s %s -d %s" % \
299 (self.fstype, extra_imagecmd, rootfs, label_str, rootfs_dir)
300 exec_native_cmd(pseudo + mkfs_cmd, native_sysroot)
301
302 def prepare_rootfs_btrfs(self, rootfs, oe_builddir, rootfs_dir,
303 native_sysroot, pseudo):
304 """
305 Prepare content for a btrfs rootfs partition.
306
307 Currently handles ext2/3/4 and btrfs.
308 """
309 du_cmd = "du -ks %s" % rootfs_dir
310 out = exec_cmd(du_cmd)
311 actual_rootfs_size = int(out.split()[0])
312
313 extra_blocks = self.get_extra_block_count(actual_rootfs_size)
314 if extra_blocks < self.extra_space:
315 extra_blocks = self.extra_space
316
317 rootfs_size = actual_rootfs_size + extra_blocks
318 rootfs_size *= self.overhead_factor
319
320 msger.debug("Added %d extra blocks to %s to get to %d total blocks" % \
321 (extra_blocks, self.mountpoint, rootfs_size))
322
323 dd_cmd = "dd if=/dev/zero of=%s bs=1024 seek=%d count=0 bs=1k" % \
324 (rootfs, rootfs_size)
325 exec_cmd(dd_cmd)
326
327 label_str = ""
328 if self.label:
329 label_str = "-L %s" % self.label
330
331 mkfs_cmd = "mkfs.%s -b %d -r %s %s %s" % \
332 (self.fstype, rootfs_size * 1024, rootfs_dir, label_str, rootfs)
333 exec_native_cmd(pseudo + mkfs_cmd, native_sysroot)
334
335 def prepare_rootfs_vfat(self, rootfs, oe_builddir, rootfs_dir,
336 native_sysroot, pseudo):
337 """
338 Prepare content for a vfat rootfs partition.
339 """
340 du_cmd = "du -bks %s" % rootfs_dir
341 out = exec_cmd(du_cmd)
342 blocks = int(out.split()[0])
343
344 extra_blocks = self.get_extra_block_count(blocks)
345 if extra_blocks < self.extra_space:
346 extra_blocks = self.extra_space
347
348 blocks += extra_blocks
349
350 msger.debug("Added %d extra blocks to %s to get to %d total blocks" % \
351 (extra_blocks, self.mountpoint, blocks))
352
353 # Ensure total sectors is an integral number of sectors per
354 # track or mcopy will complain. Sectors are 512 bytes, and we
355 # generate images with 32 sectors per track. This calculation
356 # is done in blocks, thus the mod by 16 instead of 32. Apply
357 # sector count fix only when needed.
358 if blocks % 16 != 0:
359 blocks += (16 - (blocks % 16))
360
361 label_str = "-n boot"
362 if self.label:
363 label_str = "-n %s" % self.label
364
365 dosfs_cmd = "mkdosfs %s -S 512 -C %s %d" % (label_str, rootfs, blocks)
366 exec_native_cmd(dosfs_cmd, native_sysroot)
367
368 mcopy_cmd = "mcopy -i %s -s %s/* ::/" % (rootfs, rootfs_dir)
369 exec_native_cmd(mcopy_cmd, native_sysroot)
370
371 chmod_cmd = "chmod 644 %s" % rootfs
372 exec_cmd(chmod_cmd)
373
374 def prepare_rootfs_squashfs(self, rootfs, oe_builddir, rootfs_dir,
375 native_sysroot, pseudo):
376 """
377 Prepare content for a squashfs rootfs partition.
378 """
379 squashfs_cmd = "mksquashfs %s %s -noappend" % \
380 (rootfs_dir, rootfs)
381 exec_native_cmd(pseudo + squashfs_cmd, native_sysroot)
382
383 def prepare_empty_partition_ext(self, rootfs, oe_builddir,
384 native_sysroot):
385 """
386 Prepare an empty ext2/3/4 partition.
387 """
388 dd_cmd = "dd if=/dev/zero of=%s bs=1k seek=%d count=0" % \
389 (rootfs, self.size)
390 exec_cmd(dd_cmd)
391
392 extra_imagecmd = "-i 8192"
393
394 label_str = ""
395 if self.label:
396 label_str = "-L %s" % self.label
397
398 mkfs_cmd = "mkfs.%s -F %s %s %s" % \
399 (self.fstype, extra_imagecmd, label_str, rootfs)
400 exec_native_cmd(mkfs_cmd, native_sysroot)
401
402 def prepare_empty_partition_btrfs(self, rootfs, oe_builddir,
403 native_sysroot):
404 """
405 Prepare an empty btrfs partition.
406 """
407 dd_cmd = "dd if=/dev/zero of=%s bs=1k seek=%d count=0" % \
408 (rootfs, self.size)
409 exec_cmd(dd_cmd)
410
411 label_str = ""
412 if self.label:
413 label_str = "-L %s" % self.label
414
415 mkfs_cmd = "mkfs.%s -b %d %s %s" % \
416 (self.fstype, self.size * 1024, label_str, rootfs)
417 exec_native_cmd(mkfs_cmd, native_sysroot)
418
419 def prepare_empty_partition_vfat(self, rootfs, oe_builddir,
420 native_sysroot):
421 """
422 Prepare an empty vfat partition.
423 """
424 blocks = self.size
425
426 label_str = "-n boot"
427 if self.label:
428 label_str = "-n %s" % self.label
429
430 dosfs_cmd = "mkdosfs %s -S 512 -C %s %d" % (label_str, rootfs, blocks)
431 exec_native_cmd(dosfs_cmd, native_sysroot)
432
433 chmod_cmd = "chmod 644 %s" % rootfs
434 exec_cmd(chmod_cmd)
435
436 def prepare_empty_partition_squashfs(self, cr_workdir, oe_builddir,
437 native_sysroot):
438 """
439 Prepare an empty squashfs partition.
440 """
441 msger.warning("Creating of an empty squashfs %s partition was attempted. " \
442 "Proceeding as requested." % self.mountpoint)
443
444 path = "%s/fs_%s.%s" % (cr_workdir, self.label, self.fstype)
445 os.path.isfile(path) and os.remove(path)
446
447 # it is not possible to create a squashfs without source data,
448 # thus prepare an empty temp dir that is used as source
449 tmpdir = tempfile.mkdtemp()
450
451 squashfs_cmd = "mksquashfs %s %s -noappend" % \
452 (tmpdir, path)
453 exec_native_cmd(squashfs_cmd, native_sysroot)
454
455 os.rmdir(tmpdir)
456
457 # get the rootfs size in the right units for kickstart (kB)
458 du_cmd = "du -Lbks %s" % path
459 out = exec_cmd(du_cmd)
460 fs_size = out.split()[0]
461
462 self.size = fs_size
463
464 def prepare_swap_partition(self, cr_workdir, oe_builddir, native_sysroot):
465 """
466 Prepare a swap partition.
467 """
468 path = "%s/fs.%s" % (cr_workdir, self.fstype)
469
470 dd_cmd = "dd if=/dev/zero of=%s bs=1k seek=%d count=0" % \
471 (path, self.size)
472 exec_cmd(dd_cmd)
473
474 import uuid
475 label_str = ""
476 if self.label:
477 label_str = "-L %s" % self.label
478 mkswap_cmd = "mkswap %s -U %s %s" % (label_str, str(uuid.uuid1()), path)
479 exec_native_cmd(mkswap_cmd, native_sysroot)
480
481
482class Wic_Partition(FC4_Partition):
483 removedKeywords = FC4_Partition.removedKeywords
484 removedAttrs = FC4_Partition.removedAttrs
485
486 def _getParser(self):
487 def overhead_cb(option, opt_str, value, parser):
488 if value < 1:
489 raise OptionValueError("Option %s: invalid value: %r" % \
490 (option, value))
491 setattr(parser.values, option.dest, value)
492
493 parser = FC4_Partition._getParser(self)
494
495 # The alignment value is given in kBytes. e.g., value 8 means that
496 # the partition is aligned to start from 8096 byte boundary.
497 parser.add_option("--align", type="int", action="store", dest="align",
498 default=None)
499 parser.add_option("--extoptions", type="string", action="store", dest="extopts",
500 default=None)
501 parser.add_option("--part-type", type="string", action="store", dest="part_type",
502 default=None)
503 # use specified source file to fill the partition
504 # and calculate partition size
505 parser.add_option("--source", type="string", action="store",
506 dest="source", default=None)
507 # comma-separated list of param=value pairs
508 parser.add_option("--sourceparams", type="string", action="store",
509 dest="sourceparams", default=None)
510 # use specified rootfs path to fill the partition
511 parser.add_option("--rootfs-dir", type="string", action="store",
512 dest="rootfs", default=None)
513 # wether to add the partition in the partition table
514 parser.add_option("--no-table", dest="no_table", action="store_true",
515 default=False)
516 # extra space beyond the partition size
517 parser.add_option("--extra-space", dest="extra_space", action="store",
518 type="size", nargs=1, default="10M")
519 parser.add_option("--overhead-factor", dest="overhead_factor",
520 action="callback", callback=overhead_cb, type="float",
521 nargs=1, default=1.3)
522 parser.add_option("--use-uuid", dest="use_uuid", action="store_true",
523 default=False)
524 parser.add_option("--uuid")
525
526 return parser