summaryrefslogtreecommitdiffstats
path: root/scripts/lib/mic/kickstart/custom_commands/partition.py
diff options
context:
space:
mode:
authorAdrian Dudau <adrian.dudau@enea.com>2013-12-12 13:38:32 +0100
committerAdrian Dudau <adrian.dudau@enea.com>2013-12-12 13:50:20 +0100
commite2e6f6fe07049f33cb6348780fa975162752e421 (patch)
treeb1813295411235d1297a0ed642b1346b24fdfb12 /scripts/lib/mic/kickstart/custom_commands/partition.py
downloadpoky-e2e6f6fe07049f33cb6348780fa975162752e421.tar.gz
initial commit of Enea Linux 3.1
Migrated from the internal git server on the dora-enea branch Signed-off-by: Adrian Dudau <adrian.dudau@enea.com>
Diffstat (limited to 'scripts/lib/mic/kickstart/custom_commands/partition.py')
-rw-r--r--scripts/lib/mic/kickstart/custom_commands/partition.py370
1 files changed, 370 insertions, 0 deletions
diff --git a/scripts/lib/mic/kickstart/custom_commands/partition.py b/scripts/lib/mic/kickstart/custom_commands/partition.py
new file mode 100644
index 0000000000..302cace234
--- /dev/null
+++ b/scripts/lib/mic/kickstart/custom_commands/partition.py
@@ -0,0 +1,370 @@
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
28
29from pykickstart.commands.partition import *
30from mic.utils.oe.misc import *
31
32from mic.kickstart.custom_commands import *
33
34BOOTDD_EXTRA_SPACE = 16384
35
36class Wic_PartData(Mic_PartData):
37 removedKeywords = Mic_PartData.removedKeywords
38 removedAttrs = Mic_PartData.removedAttrs
39
40 def __init__(self, *args, **kwargs):
41 Mic_PartData.__init__(self, *args, **kwargs)
42 self.deleteRemovedAttrs()
43 self.source = kwargs.get("source", None)
44 self.source_file = ""
45 self.size = 0
46
47 def _getArgsAsStr(self):
48 retval = Mic_PartData._getArgsAsStr(self)
49
50 if self.source:
51 retval += " --source=%s" % self.source
52
53 return retval
54
55 def prepare(self, cr_workdir, oe_builddir, boot_type, rootfs_dir,
56 bootimg_dir, kernel_dir, native_sysroot):
57 """
58 Prepare content for individual partitions, depending on
59 partition command parameters.
60 """
61 if not self.source:
62 if self.fstype and self.fstype == "swap":
63 self.prepare_swap_partition(cr_workdir, oe_builddir,
64 native_sysroot)
65 elif self.fstype:
66 self.prepare_empty_partition(cr_workdir, oe_builddir,
67 native_sysroot)
68 return
69
70 if self.source == "bootimg" and boot_type == "pcbios":
71 self.prepare_bootimg_pcbios(cr_workdir, oe_builddir, bootimg_dir,
72 kernel_dir, native_sysroot)
73 elif self.source == "bootimg" and boot_type == "efi":
74 self.prepare_bootimg_efi(cr_workdir, oe_builddir, bootimg_dir,
75 kernel_dir, native_sysroot)
76 elif self.source.startswith("rootfs"):
77 self.prepare_rootfs(cr_workdir, oe_builddir, rootfs_dir,
78 native_sysroot)
79
80 def prepare_bootimg_pcbios(self, cr_workdir, oe_builddir, bootimg_dir,
81 kernel_dir, native_sysroot):
82 """
83 Prepare content for a legacy bios boot partition.
84 """
85 staging_kernel_dir = kernel_dir
86 staging_data_dir = bootimg_dir
87
88 hdddir = "%s/hdd/boot" % cr_workdir
89
90 install_cmd = "install -m 0644 %s/bzImage %s/vmlinuz" \
91 % (staging_kernel_dir, hdddir)
92 tmp = exec_cmd(install_cmd)
93
94 install_cmd = "install -m 444 %s/syslinux/ldlinux.sys %s/ldlinux.sys" \
95 % (staging_data_dir, hdddir)
96 tmp = exec_cmd(install_cmd)
97
98 du_cmd = "du -bks %s" % hdddir
99 rc, out = exec_cmd(du_cmd)
100 blocks = int(out.split()[0])
101
102 blocks += BOOTDD_EXTRA_SPACE
103
104 # Ensure total sectors is an integral number of sectors per
105 # track or mcopy will complain. Sectors are 512 bytes, and we
106 # generate images with 32 sectors per track. This calculation is
107 # done in blocks, thus the mod by 16 instead of 32.
108 blocks += (16 - (blocks % 16))
109
110 # dosfs image, created by mkdosfs
111 bootimg = "%s/boot.img" % cr_workdir
112
113 dosfs_cmd = "mkdosfs -n boot -S 512 -C %s %d" % (bootimg, blocks)
114 exec_native_cmd(dosfs_cmd, native_sysroot)
115
116 mcopy_cmd = "mcopy -i %s -s %s/* ::/" % (bootimg, hdddir)
117 exec_native_cmd(mcopy_cmd, native_sysroot)
118
119 syslinux_cmd = "syslinux %s" % bootimg
120 exec_native_cmd(syslinux_cmd, native_sysroot)
121
122 chmod_cmd = "chmod 644 %s" % bootimg
123 exec_cmd(chmod_cmd)
124
125 du_cmd = "du -Lbms %s" % bootimg
126 rc, out = exec_cmd(du_cmd)
127 bootimg_size = out.split()[0]
128
129 self.size = bootimg_size
130 self.source_file = bootimg
131
132 def prepare_bootimg_efi(self, cr_workdir, oe_builddir, bootimg_dir,
133 kernel_dir, native_sysroot):
134 """
135 Prepare content for an EFI (grub) boot partition.
136 """
137 staging_kernel_dir = kernel_dir
138 staging_data_dir = bootimg_dir
139
140 hdddir = "%s/hdd/boot" % cr_workdir
141
142 install_cmd = "install -m 0644 %s/bzImage %s/vmlinuz" % \
143 (staging_kernel_dir, hdddir)
144 tmp = exec_cmd(install_cmd)
145
146 shutil.copyfile("%s/hdd/boot/EFI/BOOT/grub.cfg" % cr_workdir,
147 "%s/grub.cfg" % cr_workdir)
148
149 cp_cmd = "cp %s/EFI/BOOT/* %s/EFI/BOOT" % (staging_data_dir, hdddir)
150 exec_cmd(cp_cmd, True)
151
152 shutil.move("%s/grub.cfg" % cr_workdir,
153 "%s/hdd/boot/EFI/BOOT/grub.cfg" % cr_workdir)
154
155 du_cmd = "du -bks %s" % hdddir
156 rc, out = exec_cmd(du_cmd)
157 blocks = int(out.split()[0])
158
159 blocks += BOOTDD_EXTRA_SPACE
160
161 # Ensure total sectors is an integral number of sectors per
162 # track or mcopy will complain. Sectors are 512 bytes, and we
163 # generate images with 32 sectors per track. This calculation is
164 # done in blocks, thus the mod by 16 instead of 32.
165 blocks += (16 - (blocks % 16))
166
167 # dosfs image, created by mkdosfs
168 bootimg = "%s/boot.img" % cr_workdir
169
170 dosfs_cmd = "mkdosfs -n efi -C %s %d" % (bootimg, blocks)
171 exec_native_cmd(dosfs_cmd, native_sysroot)
172
173 mcopy_cmd = "mcopy -i %s -s %s/* ::/" % (bootimg, hdddir)
174 exec_native_cmd(mcopy_cmd, native_sysroot)
175
176 chmod_cmd = "chmod 644 %s" % bootimg
177 exec_cmd(chmod_cmd)
178
179 du_cmd = "du -Lbms %s" % bootimg
180 rc, out = exec_cmd(du_cmd)
181 bootimg_size = out.split()[0]
182
183 self.size = bootimg_size
184 self.source_file = bootimg
185
186 def prepare_rootfs_from_fs_image(self, cr_workdir, oe_builddir,
187 rootfs_dir):
188 """
189 Handle an already-created partition e.g. xxx.ext3
190 """
191 rootfs = oe_builddir
192 du_cmd = "du -Lbms %s" % rootfs
193 rc, out = exec_cmd(du_cmd)
194 rootfs_size = out.split()[0]
195
196 self.size = rootfs_size
197 self.source_file = rootfs
198
199 def prepare_rootfs(self, cr_workdir, oe_builddir, rootfs_dir,
200 native_sysroot):
201 """
202 Prepare content for a rootfs partition i.e. create a partition
203 and fill it from a /rootfs dir.
204
205 Currently handles ext2/3/4 and btrfs.
206 """
207 if self.fstype.startswith("ext"):
208 return self.prepare_rootfs_ext(cr_workdir, oe_builddir,
209 rootfs_dir, native_sysroot)
210 elif self.fstype.startswith("btrfs"):
211 return self.prepare_rootfs_btrfs(cr_workdir, oe_builddir,
212 rootfs_dir, native_sysroot)
213
214 def prepare_rootfs_ext(self, cr_workdir, oe_builddir, rootfs_dir,
215 native_sysroot):
216 """
217 Prepare content for an ext2/3/4 rootfs partition.
218 """
219 populate_script = "%s/usr/bin/populate-extfs.sh" % native_sysroot
220 image_extra_space = 10240
221
222 image_rootfs = rootfs_dir
223 rootfs = "%s/rootfs.%s" % (cr_workdir, self.fstype)
224
225 du_cmd = "du -ks %s" % image_rootfs
226 rc, out = exec_cmd(du_cmd)
227 actual_rootfs_size = out.split()[0]
228
229 rootfs_size = int(actual_rootfs_size) + image_extra_space
230
231 dd_cmd = "dd if=/dev/zero of=%s bs=1024 seek=%d count=0 bs=1k" % \
232 (rootfs, rootfs_size)
233 rc, out = exec_cmd(dd_cmd)
234
235 extra_imagecmd = "-i 8192"
236
237 mkfs_cmd = "mkfs.%s -F %s %s" % (self.fstype, extra_imagecmd, rootfs)
238 rc, out = exec_native_cmd(mkfs_cmd, native_sysroot)
239
240 populate_cmd = populate_script + " " + image_rootfs + " " + rootfs
241 rc, out = exec_native_cmd(populate_cmd, native_sysroot)
242
243 # get the rootfs size in the right units for kickstart (Mb)
244 du_cmd = "du -Lbms %s" % rootfs
245 rc, out = exec_cmd(du_cmd)
246 rootfs_size = out.split()[0]
247
248 self.size = rootfs_size
249 self.source_file = rootfs
250
251 return 0
252
253 def prepare_rootfs_btrfs(self, cr_workdir, oe_builddir, rootfs_dir,
254 native_sysroot):
255 """
256 Prepare content for a btrfs rootfs partition.
257
258 Currently handles ext2/3/4 and btrfs.
259 """
260 image_extra_space = 10240
261
262 image_rootfs = rootfs_dir
263 rootfs = "%s/rootfs.%s" % (cr_workdir, self.fstype)
264
265 du_cmd = "du -ks %s" % image_rootfs
266 rc, out = exec_cmd(du_cmd)
267 actual_rootfs_size = out.split()[0]
268
269 rootfs_size = int(actual_rootfs_size) + image_extra_space
270
271 dd_cmd = "dd if=/dev/zero of=%s bs=1024 seek=%d count=0 bs=1k" % \
272 (rootfs, rootfs_size)
273 rc, out = exec_cmd(dd_cmd)
274
275 mkfs_cmd = "mkfs.%s -b %d -r %s %s" % \
276 (self.fstype, rootfs_size * 1024, image_rootfs, rootfs)
277 rc, out = exec_native_cmd(mkfs_cmd, native_sysroot)
278
279 # get the rootfs size in the right units for kickstart (Mb)
280 du_cmd = "du -Lbms %s" % rootfs
281 rc, out = exec_cmd(du_cmd)
282 rootfs_size = out.split()[0]
283
284 self.size = rootfs_size
285 self.source_file = rootfs
286
287 def prepare_empty_partition(self, cr_workdir, oe_builddir, native_sysroot):
288 """
289 Prepare an empty partition.
290 """
291 if self.fstype.startswith("ext"):
292 return self.prepare_empty_partition_ext(cr_workdir, oe_builddir,
293 native_sysroot)
294 elif self.fstype.startswith("btrfs"):
295 return self.prepare_empty_partition_btrfs(cr_workdir, oe_builddir,
296 native_sysroot)
297
298 def prepare_empty_partition_ext(self, cr_workdir, oe_builddir,
299 native_sysroot):
300 """
301 Prepare an empty ext2/3/4 partition.
302 """
303 fs = "%s/fs.%s" % (cr_workdir, self.fstype)
304
305 dd_cmd = "dd if=/dev/zero of=%s bs=1M seek=%d count=0" % \
306 (fs, self.size)
307 rc, out = exec_cmd(dd_cmd)
308
309 extra_imagecmd = "-i 8192"
310
311 mkfs_cmd = "mkfs.%s -F %s %s" % (self.fstype, extra_imagecmd, fs)
312 rc, out = exec_native_cmd(mkfs_cmd, native_sysroot)
313
314 self.source_file = fs
315
316 return 0
317
318 def prepare_empty_partition_btrfs(self, cr_workdir, oe_builddir,
319 native_sysroot):
320 """
321 Prepare an empty btrfs partition.
322 """
323 fs = "%s/fs.%s" % (cr_workdir, self.fstype)
324
325 dd_cmd = "dd if=/dev/zero of=%s bs=1M seek=%d count=0" % \
326 (fs, self.size)
327 rc, out = exec_cmd(dd_cmd)
328
329 mkfs_cmd = "mkfs.%s -b %d %s" % (self.fstype, self.size * 1024, rootfs)
330 rc, out = exec_native_cmd(mkfs_cmd, native_sysroot)
331
332 mkfs_cmd = "mkfs.%s -F %s %s" % (self.fstype, extra_imagecmd, fs)
333 rc, out = exec_native_cmd(mkfs_cmd, native_sysroot)
334
335 self.source_file = fs
336
337 return 0
338
339 def prepare_swap_partition(self, cr_workdir, oe_builddir, native_sysroot):
340 """
341 Prepare a swap partition.
342 """
343 fs = "%s/fs.%s" % (cr_workdir, self.fstype)
344
345 dd_cmd = "dd if=/dev/zero of=%s bs=1M seek=%d count=0" % \
346 (fs, self.size)
347 rc, out = exec_cmd(dd_cmd)
348
349 import uuid
350 label_str = ""
351 if self.label:
352 label_str = "-L %s" % self.label
353 mkswap_cmd = "mkswap %s -U %s %s" % (label_str, str(uuid.uuid1()), fs)
354 rc, out = exec_native_cmd(mkswap_cmd, native_sysroot)
355
356 self.source_file = fs
357
358 return 0
359
360class Wic_Partition(Mic_Partition):
361 removedKeywords = Mic_Partition.removedKeywords
362 removedAttrs = Mic_Partition.removedAttrs
363
364 def _getParser(self):
365 op = Mic_Partition._getParser(self)
366 # use specified source file to fill the partition
367 # and calculate partition size
368 op.add_option("--source", type="string", action="store",
369 dest="source", default=None)
370 return op