diff options
Diffstat (limited to 'scripts/lib')
-rw-r--r-- | scripts/lib/wic/plugins/source/rootfs_pcbios_ext.py | 182 | ||||
-rw-r--r-- | scripts/lib/wic/utils/syslinux.py | 58 |
2 files changed, 240 insertions, 0 deletions
diff --git a/scripts/lib/wic/plugins/source/rootfs_pcbios_ext.py b/scripts/lib/wic/plugins/source/rootfs_pcbios_ext.py new file mode 100644 index 0000000000..ebf95ae097 --- /dev/null +++ b/scripts/lib/wic/plugins/source/rootfs_pcbios_ext.py | |||
@@ -0,0 +1,182 @@ | |||
1 | # ex:ts=4:sw=4:sts=4:et | ||
2 | # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- | ||
3 | # | ||
4 | # This program is free software; you can distribute it and/or modify | ||
5 | # it under the terms of the GNU General Public License version 2 as | ||
6 | # published by the Free Software Foundation. | ||
7 | # | ||
8 | # This program is distributed in the hope that it will be useful, | ||
9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
11 | # GNU General Public License for mo details. | ||
12 | # | ||
13 | # You should have received a copy of the GNU General Public License along | ||
14 | # with this program; if not, write to the Free Software Foundation, Inc., | ||
15 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
16 | # | ||
17 | # AUTHOR | ||
18 | # Adrian Freihofer <adrian.freihofer (at] neratec.com> | ||
19 | # | ||
20 | |||
21 | import os | ||
22 | from wic import kickstart | ||
23 | from wic import msger | ||
24 | from wic.utils import syslinux | ||
25 | from wic.utils import runner | ||
26 | from wic.utils.oe import misc | ||
27 | from wic.utils.errors import ImageError | ||
28 | from wic.pluginbase import SourcePlugin | ||
29 | |||
30 | |||
31 | # pylint: disable=no-init | ||
32 | class RootfsPlugin(SourcePlugin): | ||
33 | """ | ||
34 | Create root partition and install syslinux bootloader | ||
35 | |||
36 | This plugin creates a disk image containing a bootable root partition with | ||
37 | syslinux installed. The filesystem is ext2/3/4, no extra boot partition is | ||
38 | required. | ||
39 | |||
40 | Example kickstart file: | ||
41 | part / --source rootfs-pcbios-ext --ondisk sda --fstype=ext4 --label rootfs --align 1024 | ||
42 | bootloader --source rootfs-pcbios-ext --timeout=0 --append="rootwait rootfstype=ext4" | ||
43 | |||
44 | The first line generates a root file system including a syslinux.cfg file | ||
45 | The "--source rootfs-pcbios-ext" in the second line triggers the installation | ||
46 | of ldlinux.sys into the image. | ||
47 | """ | ||
48 | |||
49 | name = 'rootfs-pcbios-ext' | ||
50 | |||
51 | @staticmethod | ||
52 | def _get_rootfs_dir(rootfs_dir): | ||
53 | """ | ||
54 | Find rootfs pseudo dir | ||
55 | |||
56 | If rootfs_dir is a directory consider it as rootfs directory. | ||
57 | Otherwise ask bitbake about the IMAGE_ROOTFS directory. | ||
58 | """ | ||
59 | if os.path.isdir(rootfs_dir): | ||
60 | return rootfs_dir | ||
61 | |||
62 | bitbake_env_lines = misc.find_bitbake_env_lines(rootfs_dir) | ||
63 | if not bitbake_env_lines: | ||
64 | msger.error("Couldn't get bitbake environment, exiting.") | ||
65 | |||
66 | image_rootfs_dir = misc.find_artifact(bitbake_env_lines, "IMAGE_ROOTFS") | ||
67 | if not os.path.isdir(image_rootfs_dir): | ||
68 | msg = "No valid artifact IMAGE_ROOTFS from image named" | ||
69 | msg += " %s has been found at %s, exiting.\n" % \ | ||
70 | (rootfs_dir, image_rootfs_dir) | ||
71 | msger.error(msg) | ||
72 | |||
73 | return image_rootfs_dir | ||
74 | |||
75 | # pylint: disable=unused-argument | ||
76 | @classmethod | ||
77 | def do_configure_partition(cls, part, source_params, image_creator, | ||
78 | image_creator_workdir, oe_builddir, bootimg_dir, | ||
79 | kernel_dir, native_sysroot): | ||
80 | """ | ||
81 | Creates syslinux config in rootfs directory | ||
82 | |||
83 | Called before do_prepare_partition() | ||
84 | """ | ||
85 | rootdev = image_creator._get_boot_config()[0] | ||
86 | options = image_creator.ks.handler.bootloader.appendLine | ||
87 | |||
88 | syslinux_conf = "" | ||
89 | syslinux_conf += "PROMPT 0\n" | ||
90 | |||
91 | timeout = kickstart.get_timeout(image_creator.ks) | ||
92 | if not timeout: | ||
93 | timeout = 0 | ||
94 | syslinux_conf += "TIMEOUT " + str(timeout) + "\n" | ||
95 | syslinux_conf += "ALLOWOPTIONS 1\n" | ||
96 | |||
97 | # Derive SERIAL... line from from kernel boot parameters | ||
98 | syslinux_conf += syslinux.serial_console_form_kargs(options) + "\n" | ||
99 | |||
100 | syslinux_conf += "DEFAULT linux\n" | ||
101 | syslinux_conf += "LABEL linux\n" | ||
102 | syslinux_conf += " KERNEL /boot/bzImage\n" | ||
103 | |||
104 | if image_creator._ptable_format == 'msdos': | ||
105 | rootstr = rootdev | ||
106 | else: | ||
107 | raise ImageError("Unsupported partition table format found") | ||
108 | |||
109 | syslinux_conf += " APPEND label=boot root=%s %s\n" % (rootstr, options) | ||
110 | |||
111 | syslinux_cfg = os.path.join(image_creator.rootfs_dir['ROOTFS_DIR'], "boot", "syslinux.cfg") | ||
112 | msger.debug("Writing syslinux config %s" % syslinux_cfg) | ||
113 | with open(syslinux_cfg, "w") as cfg: | ||
114 | cfg.write(syslinux_conf) | ||
115 | |||
116 | @classmethod | ||
117 | def do_prepare_partition(cls, part, source_params, image_creator, | ||
118 | image_creator_workdir, oe_builddir, bootimg_dir, | ||
119 | kernel_dir, krootfs_dir, native_sysroot): | ||
120 | """ | ||
121 | Creates partition out of rootfs directory | ||
122 | |||
123 | Prepare content for a rootfs partition i.e. create a partition | ||
124 | and fill it from a /rootfs dir. | ||
125 | Install syslinux bootloader into root partition image file | ||
126 | """ | ||
127 | def is_exe(exepath): | ||
128 | """Verify exepath is an executable file""" | ||
129 | return os.path.isfile(exepath) and os.access(exepath, os.X_OK) | ||
130 | |||
131 | # Make sure syslinux-nomtools is available in native sysroot or fail | ||
132 | native_syslinux_nomtools = os.path.join(native_sysroot, "usr/bin/syslinux-nomtools") | ||
133 | if not is_exe(native_syslinux_nomtools): | ||
134 | msger.info("building syslinux-native...") | ||
135 | misc.exec_cmd("bitbake syslinux-native") | ||
136 | if not is_exe(native_syslinux_nomtools): | ||
137 | msger.error("Couldn't find syslinux-nomtools (%s), exiting\n" % | ||
138 | native_syslinux_nomtools) | ||
139 | |||
140 | if part.rootfs is None: | ||
141 | if 'ROOTFS_DIR' not in krootfs_dir: | ||
142 | msger.error("Couldn't find --rootfs-dir, exiting") | ||
143 | rootfs_dir = krootfs_dir['ROOTFS_DIR'] | ||
144 | else: | ||
145 | if part.rootfs in krootfs_dir: | ||
146 | rootfs_dir = krootfs_dir[part.rootfs] | ||
147 | elif part.rootfs: | ||
148 | rootfs_dir = part.rootfs | ||
149 | else: | ||
150 | msg = "Couldn't find --rootfs-dir=%s connection" | ||
151 | msg += " or it is not a valid path, exiting" | ||
152 | msger.error(msg % part.rootfs) | ||
153 | |||
154 | real_rootfs_dir = cls._get_rootfs_dir(rootfs_dir) | ||
155 | |||
156 | part.set_rootfs(real_rootfs_dir) | ||
157 | part.prepare_rootfs(image_creator_workdir, oe_builddir, real_rootfs_dir, native_sysroot) | ||
158 | |||
159 | # install syslinux into rootfs partition | ||
160 | syslinux_cmd = "syslinux-nomtools -d /boot -i %s" % part.source_file | ||
161 | misc.exec_native_cmd(syslinux_cmd, native_sysroot) | ||
162 | |||
163 | @classmethod | ||
164 | def do_install_disk(cls, disk, disk_name, image_creator, workdir, oe_builddir, | ||
165 | bootimg_dir, kernel_dir, native_sysroot): | ||
166 | """ | ||
167 | Assemble partitions to disk image | ||
168 | |||
169 | Called after all partitions have been prepared and assembled into a | ||
170 | disk image. In this case, we install the MBR. | ||
171 | """ | ||
172 | mbrfile = os.path.join(native_sysroot, "usr/share/syslinux/mbr.bin") | ||
173 | if not os.path.exists(mbrfile): | ||
174 | msger.error("Couldn't find %s. Has syslinux-native been baked?" % mbrfile) | ||
175 | |||
176 | full_path = disk['disk'].device | ||
177 | msger.debug("Installing MBR on disk %s as %s with size %s bytes" \ | ||
178 | % (disk_name, full_path, disk['min_size'])) | ||
179 | |||
180 | ret_code = runner.show(['dd', 'if=%s' % mbrfile, 'of=%s' % full_path, 'conv=notrunc']) | ||
181 | if ret_code != 0: | ||
182 | raise ImageError("Unable to set MBR to %s" % full_path) | ||
diff --git a/scripts/lib/wic/utils/syslinux.py b/scripts/lib/wic/utils/syslinux.py new file mode 100644 index 0000000000..aace2863c1 --- /dev/null +++ b/scripts/lib/wic/utils/syslinux.py | |||
@@ -0,0 +1,58 @@ | |||
1 | # ex:ts=4:sw=4:sts=4:et | ||
2 | # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- | ||
3 | # | ||
4 | # This program is free software; you can redistribute it and/or modify it | ||
5 | # under the terms of the GNU General Public License as published by the Free | ||
6 | # Software Foundation; version 2 of the License | ||
7 | # | ||
8 | # This program is distributed in the hope that it will be useful, but | ||
9 | # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
10 | # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
11 | # for more details. | ||
12 | # | ||
13 | # You should have received a copy of the GNU General Public License along | ||
14 | # with this program; if not, write to the Free Software Foundation, Inc., 59 | ||
15 | # Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
16 | # | ||
17 | # AUTHOR | ||
18 | # Adrian Freihofer <adrian.freihofer (at] neratec.com> | ||
19 | |||
20 | |||
21 | import re | ||
22 | from wic import msger | ||
23 | |||
24 | |||
25 | def serial_console_form_kargs(kernel_args): | ||
26 | """ | ||
27 | Create SERIAL... line from kernel parameters | ||
28 | |||
29 | syslinux needs a line SERIAL port [baudrate [flowcontrol]] | ||
30 | in the syslinux.cfg file. The config line is generated based | ||
31 | on kernel boot parameters. The the parameters of the first | ||
32 | ttyS console are considered for syslinux config. | ||
33 | @param kernel_args kernel command line | ||
34 | @return line for syslinux config file e.g. "SERIAL 0 115200" | ||
35 | """ | ||
36 | syslinux_conf = "" | ||
37 | for param in kernel_args.split(): | ||
38 | param_match = re.match("console=ttyS([0-9]+),?([0-9]*)([noe]?)([0-9]?)(r?)", param) | ||
39 | if param_match: | ||
40 | syslinux_conf += "SERIAL " + param_match.group(1) | ||
41 | # baudrate | ||
42 | if param_match.group(2): | ||
43 | syslinux_conf += " " + param_match.group(2) | ||
44 | # parity | ||
45 | if param_match.group(3) and param_match.group(3) != 'n': | ||
46 | msger.warning("syslinux does not support parity for console. {} is ignored." | ||
47 | .format(param_match.group(3))) | ||
48 | # number of bits | ||
49 | if param_match.group(4) and param_match.group(4) != '8': | ||
50 | msger.warning("syslinux supports 8 bit console configuration only. {} is ignored." | ||
51 | .format(param_match.group(4))) | ||
52 | # flow control | ||
53 | if param_match.group(5) and param_match.group(5) != '': | ||
54 | msger.warning("syslinux console flowcontrol configuration. {} is ignored." | ||
55 | .format(param_match.group(5))) | ||
56 | break | ||
57 | |||
58 | return syslinux_conf | ||