blob: 90f65a1e3976a5460cd1b265b238168cce22781f [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)
149
150 def prepare_rootfs_from_fs_image(self, cr_workdir, oe_builddir,
151 rootfs_dir):
152 """
153 Handle an already-created partition e.g. xxx.ext3
154 """
155 rootfs = oe_builddir
156 du_cmd = "du -Lbks %s" % rootfs
157 out = exec_cmd(du_cmd)
158 rootfs_size = out.split()[0]
159
160 self.size = rootfs_size
161 self.source_file = rootfs
162
163 def prepare_rootfs(self, cr_workdir, oe_builddir, rootfs_dir,
164 native_sysroot):
165 """
166 Prepare content for a rootfs partition i.e. create a partition
167 and fill it from a /rootfs dir.
168
169 Currently handles ext2/3/4, btrfs and vfat.
170 """
171 p_prefix = os.environ.get("PSEUDO_PREFIX", "%s/usr" % native_sysroot)
172 p_localstatedir = os.environ.get("PSEUDO_LOCALSTATEDIR",
173 "%s/../pseudo" % rootfs_dir)
174 p_passwd = os.environ.get("PSEUDO_PASSWD", rootfs_dir)
175 p_nosymlinkexp = os.environ.get("PSEUDO_NOSYMLINKEXP", "1")
176 pseudo = "export PSEUDO_PREFIX=%s;" % p_prefix
177 pseudo += "export PSEUDO_LOCALSTATEDIR=%s;" % p_localstatedir
178 pseudo += "export PSEUDO_PASSWD=%s;" % p_passwd
179 pseudo += "export PSEUDO_NOSYMLINKEXP=%s;" % p_nosymlinkexp
180 pseudo += "%s/usr/bin/pseudo " % native_sysroot
181
182 rootfs = "%s/rootfs_%s.%s.%s" % (cr_workdir, self.label,
183 self.lineno, self.fstype)
184 if os.path.isfile(rootfs):
185 os.remove(rootfs)
186
187 for prefix in ("ext", "btrfs", "vfat", "squashfs"):
188 if self.fstype.startswith(prefix):
189 method = getattr(self, "prepare_rootfs_" + prefix)
190 method(rootfs, oe_builddir, rootfs_dir, native_sysroot, pseudo)
191
192 self.source_file = rootfs
193
194 # get the rootfs size in the right units for kickstart (kB)
195 du_cmd = "du -Lbks %s" % rootfs
196 out = exec_cmd(du_cmd)
197 self.size = out.split()[0]
198
199 break
200
201 def prepare_rootfs_ext(self, rootfs, oe_builddir, rootfs_dir,
202 native_sysroot, pseudo):
203 """
204 Prepare content for an ext2/3/4 rootfs partition.
205 """
206 du_cmd = "du -ks %s" % rootfs_dir
207 out = exec_cmd(du_cmd)
208 actual_rootfs_size = int(out.split()[0])
209
210 extra_blocks = self.get_extra_block_count(actual_rootfs_size)
211 if extra_blocks < self.extra_space:
212 extra_blocks = self.extra_space
213
214 rootfs_size = actual_rootfs_size + extra_blocks
215 rootfs_size *= self.overhead_factor
216
217 msger.debug("Added %d extra blocks to %s to get to %d total blocks" % \
218 (extra_blocks, self.mountpoint, rootfs_size))
219
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600220 exec_cmd("truncate %s -s %d" % (rootfs, rootfs_size * 1024))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500221
222 extra_imagecmd = "-i 8192"
223
224 label_str = ""
225 if self.label:
226 label_str = "-L %s" % self.label
227
228 mkfs_cmd = "mkfs.%s -F %s %s %s -d %s" % \
229 (self.fstype, extra_imagecmd, rootfs, label_str, rootfs_dir)
230 exec_native_cmd(mkfs_cmd, native_sysroot, pseudo=pseudo)
231
232 def prepare_rootfs_btrfs(self, rootfs, oe_builddir, rootfs_dir,
233 native_sysroot, pseudo):
234 """
235 Prepare content for a btrfs rootfs partition.
236
237 Currently handles ext2/3/4 and btrfs.
238 """
239 du_cmd = "du -ks %s" % rootfs_dir
240 out = exec_cmd(du_cmd)
241 actual_rootfs_size = int(out.split()[0])
242
243 extra_blocks = self.get_extra_block_count(actual_rootfs_size)
244 if extra_blocks < self.extra_space:
245 extra_blocks = self.extra_space
246
247 rootfs_size = actual_rootfs_size + extra_blocks
248 rootfs_size *= self.overhead_factor
249
250 msger.debug("Added %d extra blocks to %s to get to %d total blocks" % \
251 (extra_blocks, self.mountpoint, rootfs_size))
252
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600253 exec_cmd("truncate %s -s %d" % (rootfs, rootfs_size * 1024))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500254
255 label_str = ""
256 if self.label:
257 label_str = "-L %s" % self.label
258
259 mkfs_cmd = "mkfs.%s -b %d -r %s %s %s" % \
260 (self.fstype, rootfs_size * 1024, rootfs_dir, label_str, rootfs)
261 exec_native_cmd(mkfs_cmd, native_sysroot, pseudo=pseudo)
262
263 def prepare_rootfs_vfat(self, rootfs, oe_builddir, rootfs_dir,
264 native_sysroot, pseudo):
265 """
266 Prepare content for a vfat rootfs partition.
267 """
268 du_cmd = "du -bks %s" % rootfs_dir
269 out = exec_cmd(du_cmd)
270 blocks = int(out.split()[0])
271
272 extra_blocks = self.get_extra_block_count(blocks)
273 if extra_blocks < self.extra_space:
274 extra_blocks = self.extra_space
275
276 blocks += extra_blocks
277
278 msger.debug("Added %d extra blocks to %s to get to %d total blocks" % \
279 (extra_blocks, self.mountpoint, blocks))
280
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500281 label_str = "-n boot"
282 if self.label:
283 label_str = "-n %s" % self.label
284
285 dosfs_cmd = "mkdosfs %s -S 512 -C %s %d" % (label_str, rootfs, blocks)
286 exec_native_cmd(dosfs_cmd, native_sysroot)
287
288 mcopy_cmd = "mcopy -i %s -s %s/* ::/" % (rootfs, rootfs_dir)
289 exec_native_cmd(mcopy_cmd, native_sysroot)
290
291 chmod_cmd = "chmod 644 %s" % rootfs
292 exec_cmd(chmod_cmd)
293
294 def prepare_rootfs_squashfs(self, rootfs, oe_builddir, rootfs_dir,
295 native_sysroot, pseudo):
296 """
297 Prepare content for a squashfs rootfs partition.
298 """
299 squashfs_cmd = "mksquashfs %s %s -noappend" % \
300 (rootfs_dir, rootfs)
301 exec_native_cmd(squashfs_cmd, native_sysroot, pseudo=pseudo)
302
303 def prepare_empty_partition_ext(self, rootfs, oe_builddir,
304 native_sysroot):
305 """
306 Prepare an empty ext2/3/4 partition.
307 """
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600308 exec_cmd("truncate %s -s %d" % (rootfs, self.size * 1024))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500309
310 extra_imagecmd = "-i 8192"
311
312 label_str = ""
313 if self.label:
314 label_str = "-L %s" % self.label
315
316 mkfs_cmd = "mkfs.%s -F %s %s %s" % \
317 (self.fstype, extra_imagecmd, label_str, rootfs)
318 exec_native_cmd(mkfs_cmd, native_sysroot)
319
320 def prepare_empty_partition_btrfs(self, rootfs, oe_builddir,
321 native_sysroot):
322 """
323 Prepare an empty btrfs partition.
324 """
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600325 exec_cmd("truncate %s -s %d" % (rootfs, self.size * 1024))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500326
327 label_str = ""
328 if self.label:
329 label_str = "-L %s" % self.label
330
331 mkfs_cmd = "mkfs.%s -b %d %s %s" % \
332 (self.fstype, self.size * 1024, label_str, rootfs)
333 exec_native_cmd(mkfs_cmd, native_sysroot)
334
335 def prepare_empty_partition_vfat(self, rootfs, oe_builddir,
336 native_sysroot):
337 """
338 Prepare an empty vfat partition.
339 """
340 blocks = self.size
341
342 label_str = "-n boot"
343 if self.label:
344 label_str = "-n %s" % self.label
345
346 dosfs_cmd = "mkdosfs %s -S 512 -C %s %d" % (label_str, rootfs, blocks)
347 exec_native_cmd(dosfs_cmd, native_sysroot)
348
349 chmod_cmd = "chmod 644 %s" % rootfs
350 exec_cmd(chmod_cmd)
351
352 def prepare_empty_partition_squashfs(self, cr_workdir, oe_builddir,
353 native_sysroot):
354 """
355 Prepare an empty squashfs partition.
356 """
357 msger.warning("Creating of an empty squashfs %s partition was attempted. " \
358 "Proceeding as requested." % self.mountpoint)
359
360 path = "%s/fs_%s.%s" % (cr_workdir, self.label, self.fstype)
361 os.path.isfile(path) and os.remove(path)
362
363 # it is not possible to create a squashfs without source data,
364 # thus prepare an empty temp dir that is used as source
365 tmpdir = tempfile.mkdtemp()
366
367 squashfs_cmd = "mksquashfs %s %s -noappend" % \
368 (tmpdir, path)
369 exec_native_cmd(squashfs_cmd, native_sysroot)
370
371 os.rmdir(tmpdir)
372
373 # get the rootfs size in the right units for kickstart (kB)
374 du_cmd = "du -Lbks %s" % path
375 out = exec_cmd(du_cmd)
376 fs_size = out.split()[0]
377
378 self.size = fs_size
379
380 def prepare_swap_partition(self, cr_workdir, oe_builddir, native_sysroot):
381 """
382 Prepare a swap partition.
383 """
384 path = "%s/fs.%s" % (cr_workdir, self.fstype)
385
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600386 exec_cmd("truncate %s -s %d" % (path, self.size * 1024))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500387
388 import uuid
389 label_str = ""
390 if self.label:
391 label_str = "-L %s" % self.label
392 mkswap_cmd = "mkswap %s -U %s %s" % (label_str, str(uuid.uuid1()), path)
393 exec_native_cmd(mkswap_cmd, native_sysroot)
394