blob: ec3aa6622c513587634e59c996f16663974a932b [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
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050029
30from wic.utils.oe.misc import msger, parse_sourceparams
31from wic.utils.oe.misc import exec_cmd, exec_native_cmd
32from wic.plugin import pluginmgr
33
34partition_methods = {
35 "do_stage_partition":None,
36 "do_prepare_partition":None,
37 "do_configure_partition":None,
38}
39
Patrick Williamsc0f7c042017-02-23 20:41:17 -060040class Partition():
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050041
42 def __init__(self, args, lineno):
43 self.args = args
44 self.active = args.active
45 self.align = args.align
46 self.disk = args.disk
47 self.extra_space = args.extra_space
48 self.fsopts = args.fsopts
49 self.fstype = args.fstype
50 self.label = args.label
51 self.mountpoint = args.mountpoint
52 self.no_table = args.no_table
53 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
57 self.source = args.source
58 self.sourceparams = args.sourceparams
Patrick Williamsc0f7c042017-02-23 20:41:17 -060059 self.system_id = args.system_id
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050060 self.use_uuid = args.use_uuid
61 self.uuid = args.uuid
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050062
63 self.lineno = lineno
64 self.source_file = ""
65 self.sourceparams_dict = {}
66
67 def get_extra_block_count(self, current_blocks):
68 """
69 The --size param is reflected in self.size (in kB), and we already
70 have current_blocks (1k) blocks, calculate and return the
71 number of (1k) blocks we need to add to get to --size, 0 if
72 we're already there or beyond.
73 """
74 msger.debug("Requested partition size for %s: %d" % \
75 (self.mountpoint, self.size))
76
77 if not self.size:
78 return 0
79
80 requested_blocks = self.size
81
82 msger.debug("Requested blocks %d, current_blocks %d" % \
83 (requested_blocks, current_blocks))
84
85 if requested_blocks > current_blocks:
86 return requested_blocks - current_blocks
87 else:
88 return 0
89
90 def prepare(self, creator, cr_workdir, oe_builddir, rootfs_dir,
91 bootimg_dir, kernel_dir, native_sysroot):
92 """
93 Prepare content for individual partitions, depending on
94 partition command parameters.
95 """
96 if self.sourceparams:
97 self.sourceparams_dict = parse_sourceparams(self.sourceparams)
98
99 if not self.source:
100 if not self.size:
101 msger.error("The %s partition has a size of zero. Please "
102 "specify a non-zero --size for that partition." % \
103 self.mountpoint)
104 if self.fstype and self.fstype == "swap":
105 self.prepare_swap_partition(cr_workdir, oe_builddir,
106 native_sysroot)
107 self.source_file = "%s/fs.%s" % (cr_workdir, self.fstype)
108 elif self.fstype:
109 rootfs = "%s/fs_%s.%s.%s" % (cr_workdir, self.label,
110 self.lineno, self.fstype)
111 if os.path.isfile(rootfs):
112 os.remove(rootfs)
113 for prefix in ("ext", "btrfs", "vfat", "squashfs"):
114 if self.fstype.startswith(prefix):
115 method = getattr(self,
116 "prepare_empty_partition_" + prefix)
117 method(rootfs, oe_builddir, native_sysroot)
118 self.source_file = rootfs
119 break
120 return
121
122 plugins = pluginmgr.get_source_plugins()
123
124 if self.source not in plugins:
125 msger.error("The '%s' --source specified for %s doesn't exist.\n\t"
126 "See 'wic list source-plugins' for a list of available"
127 " --sources.\n\tSee 'wic help source-plugins' for "
128 "details on adding a new source plugin." % \
129 (self.source, self.mountpoint))
130
131 self._source_methods = pluginmgr.get_source_plugin_methods(\
132 self.source, partition_methods)
133 self._source_methods["do_configure_partition"](self, self.sourceparams_dict,
134 creator, cr_workdir,
135 oe_builddir,
136 bootimg_dir,
137 kernel_dir,
138 native_sysroot)
139 self._source_methods["do_stage_partition"](self, self.sourceparams_dict,
140 creator, cr_workdir,
141 oe_builddir,
142 bootimg_dir, kernel_dir,
143 native_sysroot)
144 self._source_methods["do_prepare_partition"](self, self.sourceparams_dict,
145 creator, cr_workdir,
146 oe_builddir,
147 bootimg_dir, kernel_dir, rootfs_dir,
148 native_sysroot)
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500149 # further processing required Partition.size to be an integer, make
150 # sure that it is one
151 if type(self.size) is not int:
152 msger.error("Partition %s internal size is not an integer. " \
153 "This a bug in source plugin %s and needs to be fixed." \
154 % (self.mountpoint, self.source))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500155
156 def prepare_rootfs_from_fs_image(self, cr_workdir, oe_builddir,
157 rootfs_dir):
158 """
159 Handle an already-created partition e.g. xxx.ext3
160 """
161 rootfs = oe_builddir
162 du_cmd = "du -Lbks %s" % rootfs
163 out = exec_cmd(du_cmd)
164 rootfs_size = out.split()[0]
165
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500166 self.size = int(rootfs_size)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500167 self.source_file = rootfs
168
169 def prepare_rootfs(self, cr_workdir, oe_builddir, rootfs_dir,
170 native_sysroot):
171 """
172 Prepare content for a rootfs partition i.e. create a partition
173 and fill it from a /rootfs dir.
174
175 Currently handles ext2/3/4, btrfs and vfat.
176 """
177 p_prefix = os.environ.get("PSEUDO_PREFIX", "%s/usr" % native_sysroot)
178 p_localstatedir = os.environ.get("PSEUDO_LOCALSTATEDIR",
179 "%s/../pseudo" % rootfs_dir)
180 p_passwd = os.environ.get("PSEUDO_PASSWD", rootfs_dir)
181 p_nosymlinkexp = os.environ.get("PSEUDO_NOSYMLINKEXP", "1")
182 pseudo = "export PSEUDO_PREFIX=%s;" % p_prefix
183 pseudo += "export PSEUDO_LOCALSTATEDIR=%s;" % p_localstatedir
184 pseudo += "export PSEUDO_PASSWD=%s;" % p_passwd
185 pseudo += "export PSEUDO_NOSYMLINKEXP=%s;" % p_nosymlinkexp
186 pseudo += "%s/usr/bin/pseudo " % native_sysroot
187
188 rootfs = "%s/rootfs_%s.%s.%s" % (cr_workdir, self.label,
189 self.lineno, self.fstype)
190 if os.path.isfile(rootfs):
191 os.remove(rootfs)
192
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500193 if not self.fstype:
194 msger.error("File system for partition %s not specified in kickstart, " \
195 "use --fstype option" % (self.mountpoint))
196
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500197 for prefix in ("ext", "btrfs", "vfat", "squashfs"):
198 if self.fstype.startswith(prefix):
199 method = getattr(self, "prepare_rootfs_" + prefix)
200 method(rootfs, oe_builddir, rootfs_dir, native_sysroot, pseudo)
201
202 self.source_file = rootfs
203
204 # get the rootfs size in the right units for kickstart (kB)
205 du_cmd = "du -Lbks %s" % rootfs
206 out = exec_cmd(du_cmd)
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500207 self.size = int(out.split()[0])
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500208
209 break
210
211 def prepare_rootfs_ext(self, rootfs, oe_builddir, rootfs_dir,
212 native_sysroot, pseudo):
213 """
214 Prepare content for an ext2/3/4 rootfs partition.
215 """
216 du_cmd = "du -ks %s" % rootfs_dir
217 out = exec_cmd(du_cmd)
218 actual_rootfs_size = int(out.split()[0])
219
220 extra_blocks = self.get_extra_block_count(actual_rootfs_size)
221 if extra_blocks < self.extra_space:
222 extra_blocks = self.extra_space
223
224 rootfs_size = actual_rootfs_size + extra_blocks
225 rootfs_size *= self.overhead_factor
226
227 msger.debug("Added %d extra blocks to %s to get to %d total blocks" % \
228 (extra_blocks, self.mountpoint, rootfs_size))
229
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600230 exec_cmd("truncate %s -s %d" % (rootfs, rootfs_size * 1024))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500231
232 extra_imagecmd = "-i 8192"
233
234 label_str = ""
235 if self.label:
236 label_str = "-L %s" % self.label
237
238 mkfs_cmd = "mkfs.%s -F %s %s %s -d %s" % \
239 (self.fstype, extra_imagecmd, rootfs, label_str, rootfs_dir)
240 exec_native_cmd(mkfs_cmd, native_sysroot, pseudo=pseudo)
241
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500242 mkfs_cmd = "fsck.%s -pvfD %s || [ $? -le 3 ]" % (self.fstype, rootfs)
243 exec_native_cmd(mkfs_cmd, native_sysroot, pseudo=pseudo)
244
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500245 def prepare_rootfs_btrfs(self, rootfs, oe_builddir, rootfs_dir,
246 native_sysroot, pseudo):
247 """
248 Prepare content for a btrfs rootfs partition.
249
250 Currently handles ext2/3/4 and btrfs.
251 """
252 du_cmd = "du -ks %s" % rootfs_dir
253 out = exec_cmd(du_cmd)
254 actual_rootfs_size = int(out.split()[0])
255
256 extra_blocks = self.get_extra_block_count(actual_rootfs_size)
257 if extra_blocks < self.extra_space:
258 extra_blocks = self.extra_space
259
260 rootfs_size = actual_rootfs_size + extra_blocks
261 rootfs_size *= self.overhead_factor
262
263 msger.debug("Added %d extra blocks to %s to get to %d total blocks" % \
264 (extra_blocks, self.mountpoint, rootfs_size))
265
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600266 exec_cmd("truncate %s -s %d" % (rootfs, rootfs_size * 1024))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500267
268 label_str = ""
269 if self.label:
270 label_str = "-L %s" % self.label
271
272 mkfs_cmd = "mkfs.%s -b %d -r %s %s %s" % \
273 (self.fstype, rootfs_size * 1024, rootfs_dir, label_str, rootfs)
274 exec_native_cmd(mkfs_cmd, native_sysroot, pseudo=pseudo)
275
276 def prepare_rootfs_vfat(self, rootfs, oe_builddir, rootfs_dir,
277 native_sysroot, pseudo):
278 """
279 Prepare content for a vfat rootfs partition.
280 """
281 du_cmd = "du -bks %s" % rootfs_dir
282 out = exec_cmd(du_cmd)
283 blocks = int(out.split()[0])
284
285 extra_blocks = self.get_extra_block_count(blocks)
286 if extra_blocks < self.extra_space:
287 extra_blocks = self.extra_space
288
289 blocks += extra_blocks
290
291 msger.debug("Added %d extra blocks to %s to get to %d total blocks" % \
292 (extra_blocks, self.mountpoint, blocks))
293
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500294 label_str = "-n boot"
295 if self.label:
296 label_str = "-n %s" % self.label
297
298 dosfs_cmd = "mkdosfs %s -S 512 -C %s %d" % (label_str, rootfs, blocks)
299 exec_native_cmd(dosfs_cmd, native_sysroot)
300
301 mcopy_cmd = "mcopy -i %s -s %s/* ::/" % (rootfs, rootfs_dir)
302 exec_native_cmd(mcopy_cmd, native_sysroot)
303
304 chmod_cmd = "chmod 644 %s" % rootfs
305 exec_cmd(chmod_cmd)
306
307 def prepare_rootfs_squashfs(self, rootfs, oe_builddir, rootfs_dir,
308 native_sysroot, pseudo):
309 """
310 Prepare content for a squashfs rootfs partition.
311 """
312 squashfs_cmd = "mksquashfs %s %s -noappend" % \
313 (rootfs_dir, rootfs)
314 exec_native_cmd(squashfs_cmd, native_sysroot, pseudo=pseudo)
315
316 def prepare_empty_partition_ext(self, rootfs, oe_builddir,
317 native_sysroot):
318 """
319 Prepare an empty ext2/3/4 partition.
320 """
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600321 exec_cmd("truncate %s -s %d" % (rootfs, self.size * 1024))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500322
323 extra_imagecmd = "-i 8192"
324
325 label_str = ""
326 if self.label:
327 label_str = "-L %s" % self.label
328
329 mkfs_cmd = "mkfs.%s -F %s %s %s" % \
330 (self.fstype, extra_imagecmd, label_str, rootfs)
331 exec_native_cmd(mkfs_cmd, native_sysroot)
332
333 def prepare_empty_partition_btrfs(self, rootfs, oe_builddir,
334 native_sysroot):
335 """
336 Prepare an empty btrfs partition.
337 """
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600338 exec_cmd("truncate %s -s %d" % (rootfs, self.size * 1024))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500339
340 label_str = ""
341 if self.label:
342 label_str = "-L %s" % self.label
343
344 mkfs_cmd = "mkfs.%s -b %d %s %s" % \
345 (self.fstype, self.size * 1024, label_str, rootfs)
346 exec_native_cmd(mkfs_cmd, native_sysroot)
347
348 def prepare_empty_partition_vfat(self, rootfs, oe_builddir,
349 native_sysroot):
350 """
351 Prepare an empty vfat partition.
352 """
353 blocks = self.size
354
355 label_str = "-n boot"
356 if self.label:
357 label_str = "-n %s" % self.label
358
359 dosfs_cmd = "mkdosfs %s -S 512 -C %s %d" % (label_str, rootfs, blocks)
360 exec_native_cmd(dosfs_cmd, native_sysroot)
361
362 chmod_cmd = "chmod 644 %s" % rootfs
363 exec_cmd(chmod_cmd)
364
365 def prepare_empty_partition_squashfs(self, cr_workdir, oe_builddir,
366 native_sysroot):
367 """
368 Prepare an empty squashfs partition.
369 """
370 msger.warning("Creating of an empty squashfs %s partition was attempted. " \
371 "Proceeding as requested." % self.mountpoint)
372
373 path = "%s/fs_%s.%s" % (cr_workdir, self.label, self.fstype)
374 os.path.isfile(path) and os.remove(path)
375
376 # it is not possible to create a squashfs without source data,
377 # thus prepare an empty temp dir that is used as source
378 tmpdir = tempfile.mkdtemp()
379
380 squashfs_cmd = "mksquashfs %s %s -noappend" % \
381 (tmpdir, path)
382 exec_native_cmd(squashfs_cmd, native_sysroot)
383
384 os.rmdir(tmpdir)
385
386 # get the rootfs size in the right units for kickstart (kB)
387 du_cmd = "du -Lbks %s" % path
388 out = exec_cmd(du_cmd)
389 fs_size = out.split()[0]
390
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500391 self.size = int(fs_size)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500392
393 def prepare_swap_partition(self, cr_workdir, oe_builddir, native_sysroot):
394 """
395 Prepare a swap partition.
396 """
397 path = "%s/fs.%s" % (cr_workdir, self.fstype)
398
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600399 exec_cmd("truncate %s -s %d" % (path, self.size * 1024))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500400
401 import uuid
402 label_str = ""
403 if self.label:
404 label_str = "-L %s" % self.label
405 mkswap_cmd = "mkswap %s -U %s %s" % (label_str, str(uuid.uuid1()), path)
406 exec_native_cmd(mkswap_cmd, native_sysroot)
407