blob: f40d1bc8b643265353be4c674410861aa7f35f0a [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
27import os
28import tempfile
29import uuid
30
31from wic.utils.oe.misc import msger, parse_sourceparams
32from wic.utils.oe.misc import exec_cmd, exec_native_cmd
33from wic.plugin import pluginmgr
34
35partition_methods = {
36 "do_stage_partition":None,
37 "do_prepare_partition":None,
38 "do_configure_partition":None,
39}
40
41class Partition(object):
42
43 def __init__(self, args, lineno):
44 self.args = args
45 self.active = args.active
46 self.align = args.align
47 self.disk = args.disk
48 self.extra_space = args.extra_space
49 self.fsopts = args.fsopts
50 self.fstype = args.fstype
51 self.label = args.label
52 self.mountpoint = args.mountpoint
53 self.no_table = args.no_table
54 self.overhead_factor = args.overhead_factor
55 self.part_type = args.part_type
56 self.rootfs_dir = args.rootfs_dir
57 self.size = args.size
58 self.source = args.source
59 self.sourceparams = args.sourceparams
60 self.use_uuid = args.use_uuid
61 self.uuid = args.uuid
62 if args.use_uuid and not self.uuid:
63 self.uuid = str(uuid.uuid4())
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 """
76 msger.debug("Requested partition size for %s: %d" % \
77 (self.mountpoint, self.size))
78
79 if not self.size:
80 return 0
81
82 requested_blocks = self.size
83
84 msger.debug("Requested blocks %d, current_blocks %d" % \
85 (requested_blocks, current_blocks))
86
87 if requested_blocks > current_blocks:
88 return requested_blocks - current_blocks
89 else:
90 return 0
91
92 def prepare(self, creator, cr_workdir, oe_builddir, rootfs_dir,
93 bootimg_dir, kernel_dir, native_sysroot):
94 """
95 Prepare content for individual partitions, depending on
96 partition command parameters.
97 """
98 if self.sourceparams:
99 self.sourceparams_dict = parse_sourceparams(self.sourceparams)
100
101 if not self.source:
102 if not self.size:
103 msger.error("The %s partition has a size of zero. Please "
104 "specify a non-zero --size for that partition." % \
105 self.mountpoint)
106 if self.fstype and self.fstype == "swap":
107 self.prepare_swap_partition(cr_workdir, oe_builddir,
108 native_sysroot)
109 self.source_file = "%s/fs.%s" % (cr_workdir, self.fstype)
110 elif self.fstype:
111 rootfs = "%s/fs_%s.%s.%s" % (cr_workdir, self.label,
112 self.lineno, self.fstype)
113 if os.path.isfile(rootfs):
114 os.remove(rootfs)
115 for prefix in ("ext", "btrfs", "vfat", "squashfs"):
116 if self.fstype.startswith(prefix):
117 method = getattr(self,
118 "prepare_empty_partition_" + prefix)
119 method(rootfs, oe_builddir, native_sysroot)
120 self.source_file = rootfs
121 break
122 return
123
124 plugins = pluginmgr.get_source_plugins()
125
126 if self.source not in plugins:
127 msger.error("The '%s' --source specified for %s doesn't exist.\n\t"
128 "See 'wic list source-plugins' for a list of available"
129 " --sources.\n\tSee 'wic help source-plugins' for "
130 "details on adding a new source plugin." % \
131 (self.source, self.mountpoint))
132
133 self._source_methods = pluginmgr.get_source_plugin_methods(\
134 self.source, partition_methods)
135 self._source_methods["do_configure_partition"](self, self.sourceparams_dict,
136 creator, cr_workdir,
137 oe_builddir,
138 bootimg_dir,
139 kernel_dir,
140 native_sysroot)
141 self._source_methods["do_stage_partition"](self, self.sourceparams_dict,
142 creator, cr_workdir,
143 oe_builddir,
144 bootimg_dir, kernel_dir,
145 native_sysroot)
146 self._source_methods["do_prepare_partition"](self, self.sourceparams_dict,
147 creator, cr_workdir,
148 oe_builddir,
149 bootimg_dir, kernel_dir, rootfs_dir,
150 native_sysroot)
151
152 def prepare_rootfs_from_fs_image(self, cr_workdir, oe_builddir,
153 rootfs_dir):
154 """
155 Handle an already-created partition e.g. xxx.ext3
156 """
157 rootfs = oe_builddir
158 du_cmd = "du -Lbks %s" % rootfs
159 out = exec_cmd(du_cmd)
160 rootfs_size = out.split()[0]
161
162 self.size = rootfs_size
163 self.source_file = rootfs
164
165 def prepare_rootfs(self, cr_workdir, oe_builddir, rootfs_dir,
166 native_sysroot):
167 """
168 Prepare content for a rootfs partition i.e. create a partition
169 and fill it from a /rootfs dir.
170
171 Currently handles ext2/3/4, btrfs and vfat.
172 """
173 p_prefix = os.environ.get("PSEUDO_PREFIX", "%s/usr" % native_sysroot)
174 p_localstatedir = os.environ.get("PSEUDO_LOCALSTATEDIR",
175 "%s/../pseudo" % rootfs_dir)
176 p_passwd = os.environ.get("PSEUDO_PASSWD", rootfs_dir)
177 p_nosymlinkexp = os.environ.get("PSEUDO_NOSYMLINKEXP", "1")
178 pseudo = "export PSEUDO_PREFIX=%s;" % p_prefix
179 pseudo += "export PSEUDO_LOCALSTATEDIR=%s;" % p_localstatedir
180 pseudo += "export PSEUDO_PASSWD=%s;" % p_passwd
181 pseudo += "export PSEUDO_NOSYMLINKEXP=%s;" % p_nosymlinkexp
182 pseudo += "%s/usr/bin/pseudo " % native_sysroot
183
184 rootfs = "%s/rootfs_%s.%s.%s" % (cr_workdir, self.label,
185 self.lineno, self.fstype)
186 if os.path.isfile(rootfs):
187 os.remove(rootfs)
188
189 for prefix in ("ext", "btrfs", "vfat", "squashfs"):
190 if self.fstype.startswith(prefix):
191 method = getattr(self, "prepare_rootfs_" + prefix)
192 method(rootfs, oe_builddir, rootfs_dir, native_sysroot, pseudo)
193
194 self.source_file = rootfs
195
196 # get the rootfs size in the right units for kickstart (kB)
197 du_cmd = "du -Lbks %s" % rootfs
198 out = exec_cmd(du_cmd)
199 self.size = out.split()[0]
200
201 break
202
203 def prepare_rootfs_ext(self, rootfs, oe_builddir, rootfs_dir,
204 native_sysroot, pseudo):
205 """
206 Prepare content for an ext2/3/4 rootfs partition.
207 """
208 du_cmd = "du -ks %s" % rootfs_dir
209 out = exec_cmd(du_cmd)
210 actual_rootfs_size = int(out.split()[0])
211
212 extra_blocks = self.get_extra_block_count(actual_rootfs_size)
213 if extra_blocks < self.extra_space:
214 extra_blocks = self.extra_space
215
216 rootfs_size = actual_rootfs_size + extra_blocks
217 rootfs_size *= self.overhead_factor
218
219 msger.debug("Added %d extra blocks to %s to get to %d total blocks" % \
220 (extra_blocks, self.mountpoint, rootfs_size))
221
222 dd_cmd = "dd if=/dev/zero of=%s bs=1024 seek=%d count=0 bs=1k" % \
223 (rootfs, rootfs_size)
224 exec_cmd(dd_cmd)
225
226 extra_imagecmd = "-i 8192"
227
228 label_str = ""
229 if self.label:
230 label_str = "-L %s" % self.label
231
232 mkfs_cmd = "mkfs.%s -F %s %s %s -d %s" % \
233 (self.fstype, extra_imagecmd, rootfs, label_str, rootfs_dir)
234 exec_native_cmd(mkfs_cmd, native_sysroot, pseudo=pseudo)
235
236 def prepare_rootfs_btrfs(self, rootfs, oe_builddir, rootfs_dir,
237 native_sysroot, pseudo):
238 """
239 Prepare content for a btrfs rootfs partition.
240
241 Currently handles ext2/3/4 and btrfs.
242 """
243 du_cmd = "du -ks %s" % rootfs_dir
244 out = exec_cmd(du_cmd)
245 actual_rootfs_size = int(out.split()[0])
246
247 extra_blocks = self.get_extra_block_count(actual_rootfs_size)
248 if extra_blocks < self.extra_space:
249 extra_blocks = self.extra_space
250
251 rootfs_size = actual_rootfs_size + extra_blocks
252 rootfs_size *= self.overhead_factor
253
254 msger.debug("Added %d extra blocks to %s to get to %d total blocks" % \
255 (extra_blocks, self.mountpoint, rootfs_size))
256
257 dd_cmd = "dd if=/dev/zero of=%s bs=1024 seek=%d count=0 bs=1k" % \
258 (rootfs, rootfs_size)
259 exec_cmd(dd_cmd)
260
261 label_str = ""
262 if self.label:
263 label_str = "-L %s" % self.label
264
265 mkfs_cmd = "mkfs.%s -b %d -r %s %s %s" % \
266 (self.fstype, rootfs_size * 1024, rootfs_dir, label_str, rootfs)
267 exec_native_cmd(mkfs_cmd, native_sysroot, pseudo=pseudo)
268
269 def prepare_rootfs_vfat(self, rootfs, oe_builddir, rootfs_dir,
270 native_sysroot, pseudo):
271 """
272 Prepare content for a vfat rootfs partition.
273 """
274 du_cmd = "du -bks %s" % rootfs_dir
275 out = exec_cmd(du_cmd)
276 blocks = int(out.split()[0])
277
278 extra_blocks = self.get_extra_block_count(blocks)
279 if extra_blocks < self.extra_space:
280 extra_blocks = self.extra_space
281
282 blocks += extra_blocks
283
284 msger.debug("Added %d extra blocks to %s to get to %d total blocks" % \
285 (extra_blocks, self.mountpoint, blocks))
286
287 # Ensure total sectors is an integral number of sectors per
288 # track or mcopy will complain. Sectors are 512 bytes, and we
289 # generate images with 32 sectors per track. This calculation
290 # is done in blocks, thus the mod by 16 instead of 32. Apply
291 # sector count fix only when needed.
292 if blocks % 16 != 0:
293 blocks += (16 - (blocks % 16))
294
295 label_str = "-n boot"
296 if self.label:
297 label_str = "-n %s" % self.label
298
299 dosfs_cmd = "mkdosfs %s -S 512 -C %s %d" % (label_str, rootfs, blocks)
300 exec_native_cmd(dosfs_cmd, native_sysroot)
301
302 mcopy_cmd = "mcopy -i %s -s %s/* ::/" % (rootfs, rootfs_dir)
303 exec_native_cmd(mcopy_cmd, native_sysroot)
304
305 chmod_cmd = "chmod 644 %s" % rootfs
306 exec_cmd(chmod_cmd)
307
308 def prepare_rootfs_squashfs(self, rootfs, oe_builddir, rootfs_dir,
309 native_sysroot, pseudo):
310 """
311 Prepare content for a squashfs rootfs partition.
312 """
313 squashfs_cmd = "mksquashfs %s %s -noappend" % \
314 (rootfs_dir, rootfs)
315 exec_native_cmd(squashfs_cmd, native_sysroot, pseudo=pseudo)
316
317 def prepare_empty_partition_ext(self, rootfs, oe_builddir,
318 native_sysroot):
319 """
320 Prepare an empty ext2/3/4 partition.
321 """
322 dd_cmd = "dd if=/dev/zero of=%s bs=1k seek=%d count=0" % \
323 (rootfs, self.size)
324 exec_cmd(dd_cmd)
325
326 extra_imagecmd = "-i 8192"
327
328 label_str = ""
329 if self.label:
330 label_str = "-L %s" % self.label
331
332 mkfs_cmd = "mkfs.%s -F %s %s %s" % \
333 (self.fstype, extra_imagecmd, label_str, rootfs)
334 exec_native_cmd(mkfs_cmd, native_sysroot)
335
336 def prepare_empty_partition_btrfs(self, rootfs, oe_builddir,
337 native_sysroot):
338 """
339 Prepare an empty btrfs partition.
340 """
341 dd_cmd = "dd if=/dev/zero of=%s bs=1k seek=%d count=0" % \
342 (rootfs, self.size)
343 exec_cmd(dd_cmd)
344
345 label_str = ""
346 if self.label:
347 label_str = "-L %s" % self.label
348
349 mkfs_cmd = "mkfs.%s -b %d %s %s" % \
350 (self.fstype, self.size * 1024, label_str, rootfs)
351 exec_native_cmd(mkfs_cmd, native_sysroot)
352
353 def prepare_empty_partition_vfat(self, rootfs, oe_builddir,
354 native_sysroot):
355 """
356 Prepare an empty vfat partition.
357 """
358 blocks = self.size
359
360 label_str = "-n boot"
361 if self.label:
362 label_str = "-n %s" % self.label
363
364 dosfs_cmd = "mkdosfs %s -S 512 -C %s %d" % (label_str, rootfs, blocks)
365 exec_native_cmd(dosfs_cmd, native_sysroot)
366
367 chmod_cmd = "chmod 644 %s" % rootfs
368 exec_cmd(chmod_cmd)
369
370 def prepare_empty_partition_squashfs(self, cr_workdir, oe_builddir,
371 native_sysroot):
372 """
373 Prepare an empty squashfs partition.
374 """
375 msger.warning("Creating of an empty squashfs %s partition was attempted. " \
376 "Proceeding as requested." % self.mountpoint)
377
378 path = "%s/fs_%s.%s" % (cr_workdir, self.label, self.fstype)
379 os.path.isfile(path) and os.remove(path)
380
381 # it is not possible to create a squashfs without source data,
382 # thus prepare an empty temp dir that is used as source
383 tmpdir = tempfile.mkdtemp()
384
385 squashfs_cmd = "mksquashfs %s %s -noappend" % \
386 (tmpdir, path)
387 exec_native_cmd(squashfs_cmd, native_sysroot)
388
389 os.rmdir(tmpdir)
390
391 # get the rootfs size in the right units for kickstart (kB)
392 du_cmd = "du -Lbks %s" % path
393 out = exec_cmd(du_cmd)
394 fs_size = out.split()[0]
395
396 self.size = fs_size
397
398 def prepare_swap_partition(self, cr_workdir, oe_builddir, native_sysroot):
399 """
400 Prepare a swap partition.
401 """
402 path = "%s/fs.%s" % (cr_workdir, self.fstype)
403
404 dd_cmd = "dd if=/dev/zero of=%s bs=1k seek=%d count=0" % \
405 (path, self.size)
406 exec_cmd(dd_cmd)
407
408 import uuid
409 label_str = ""
410 if self.label:
411 label_str = "-L %s" % self.label
412 mkswap_cmd = "mkswap %s -U %s %s" % (label_str, str(uuid.uuid1()), path)
413 exec_native_cmd(mkswap_cmd, native_sysroot)
414