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