1 /* febootstrap-supermin-helper reimplementation in C.
2 * Copyright (C) 2009-2010 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include <sys/utsname.h>
30 #include "xvasprintf.h"
34 /* Directory containing candidate kernels. We could make this
35 * configurable at some point.
37 #define KERNELDIR "/boot"
38 #define MODULESDIR "/lib/modules"
40 static char* get_kernel_version (char* filename);
41 static const char *create_kernel_from_env (const char *hostcpu, const char *kernel, const char *kernel_env, const char *modpath_env);
44 get_modpath (const char *kernel_name)
46 /* Ignore "vmlinuz-" at the beginning of the kernel name. */
47 const char *version = &kernel_name[8];
49 /* /lib/modules/<version> */
50 char *modpath = xasprintf (MODULESDIR "/%s", version);
56 if (! isdir (modpath)) {
59 path = xasprintf (KERNELDIR "/%s", kernel_name);
64 version = get_kernel_version (path);
66 if (version != NULL) {
68 modpath = xasprintf (MODULESDIR "/%s", version);
80 /* kernel_name is "vmlinuz-*". Check if there is a corresponding
81 * module path in /lib/modules.
84 has_modpath (const char *kernel_name)
86 char *modpath = get_modpath (kernel_name);
89 fprintf (stderr, "checking modpath %s is a directory\n", modpath);
91 int r = isdir (modpath);
95 fprintf (stderr, "picked %s because modpath %s exists\n",
96 kernel_name, modpath);
106 /* Create the kernel. This chooses an appropriate kernel and makes a
109 * Look for the most recent kernel named vmlinuz-*.<arch>* which has a
110 * corresponding directory in /lib/modules/. If the architecture is
111 * x86, look for any x86 kernel.
113 * RHEL 5 didn't append the arch to the kernel name, so look for
114 * kernels without arch second.
116 * If no suitable kernel can be found, exit with an error.
118 * This function returns the module path (ie. /lib/modules/<version>).
121 create_kernel (const char *hostcpu, const char *kernel)
123 /* Override kernel selection using environment variables? */
124 char *kernel_env = getenv ("FEBOOTSTRAP_KERNEL");
126 char *modpath_env = getenv ("FEBOOTSTRAP_MODULES");
127 return create_kernel_from_env (hostcpu, kernel, kernel_env, modpath_env);
130 /* In original: ls -1dvr /boot/vmlinuz-*.$arch* 2>/dev/null | grep -v xen */
132 if (hostcpu[0] == 'i' && hostcpu[2] == '8' && hostcpu[3] == '6' &&
134 patt = "vmlinuz-*.i?86*";
136 patt = xasprintf ("vmlinuz-*.%s*", hostcpu);
138 char **all_files = read_dir (KERNELDIR);
140 candidates = filter_fnmatch (all_files, patt, FNM_NOESCAPE);
141 candidates = filter_notmatching_substring (candidates, "xen");
142 candidates = filter (candidates, has_modpath);
144 if (candidates[0] == NULL) {
145 /* In original: ls -1dvr /boot/vmlinuz-* 2>/dev/null | grep -v xen */
147 candidates = filter_fnmatch (all_files, patt, FNM_NOESCAPE);
148 candidates = filter_notmatching_substring (candidates, "xen");
149 candidates = filter (candidates, has_modpath);
151 if (candidates[0] == NULL)
155 sort (candidates, reverse_filevercmp);
158 /* Choose the first candidate. */
159 char *tmp = xasprintf (KERNELDIR "/%s", candidates[0]);
162 fprintf (stderr, "creating symlink %s -> %s\n", kernel, tmp);
164 if (symlink (tmp, kernel) == -1)
165 error (EXIT_FAILURE, errno, "symlink kernel");
170 return get_modpath (candidates[0]);
172 /* Print more diagnostics here than the old script did. */
175 "febootstrap-supermin-helper: failed to find a suitable kernel.\n"
176 "I looked for kernels in " KERNELDIR " and modules in " MODULESDIR
178 "If this is a Xen guest, and you only have Xen domU kernels\n"
179 "installed, try installing a fullvirt kernel (only for\n"
180 "febootstrap use, you shouldn't boot the Xen guest with it).\n");
184 /* Select the kernel from environment variables set by the user.
185 * modpath_env may be NULL, in which case we attempt to work it out
189 create_kernel_from_env (const char *hostcpu, const char *kernel,
190 const char *kernel_env, const char *modpath_env)
194 "febootstrap-supermin-helper: using environment variable(s) FEBOOTSTRAP_* to\n"
195 "select kernel %s", kernel_env);
197 fprintf (stderr, " and module path %s", modpath_env);
198 fprintf (stderr, "\n");
201 if (!isfile (kernel_env)) {
203 "febootstrap-supermin-helper: %s: not a regular file\n"
204 "(what is $FEBOOTSTRAP_KERNEL set to?)\n", kernel_env);
209 /* Try to guess modpath from kernel path. */
210 const char *p = strrchr (kernel_env, '/');
211 if (p) p++; else p = kernel_env;
213 /* NB: We need the extra test to ensure calling get_modpath is safe. */
214 if (strncmp (p, "vmlinuz-", 8) != 0) {
216 "febootstrap-supermin-helper: cannot guess module path.\n"
217 "Set $FEBOOTSTRAP_MODULES to the modules directory corresponding to\n"
218 "kernel %s, or unset $FEBOOTSTRAP_KERNEL to autoselect a kernel.\n",
223 modpath_env = get_modpath (p);
226 if (!isdir (modpath_env)) {
228 "febootstrap-supermin-helper: %s: not a directory\n"
229 "(what is $FEBOOTSTRAP_MODULES set to?)\n", modpath_env);
233 /* Create the symlink. */
236 fprintf (stderr, "creating symlink %s -> %s\n", kernel_env, kernel);
238 if (symlink (kernel_env, kernel) == -1)
239 error (EXIT_FAILURE, errno, "symlink kernel");
245 /* Read an unsigned little endian short at a specified offset in a file.
246 * Returns a non-negative int on success or -1 on failure.
249 read_leshort (FILE* fp, int offset)
252 if (fseek (fp, offset, SEEK_SET) != 0 ||
253 fread (buf, sizeof(char), 2, fp) != 2)
257 return ((buf[1] & 0xFF) << 8) | (buf[0] & 0xFF);
260 /* Extract the kernel version from a Linux kernel file.
261 * Returns a malloc'd string containing the version or NULL if the
262 * file can't be read, is not a Linux kernel, or the version can't
265 * See ftp://ftp.astron.com/pub/file/file-<ver>.tar.gz
266 * (file-<ver>/magic/Magdir/linux) for the rules used to find the
268 * 514 string HdrS Linux kernel
269 * >518 leshort >0x1ff
270 * >>(526.s+0x200) string >\0 version %s,
272 * Bugs: probably limited to x86 kernels.
275 get_kernel_version (char* filename)
282 fp = fopen (filename, "rb");
284 if (fseek (fp, 514, SEEK_SET) != 0 ||
285 fgets (buf, size, fp) == NULL ||
286 strncmp (buf, "HdrS", 4) != 0 ||
287 read_leshort (fp, 518) < 0x1FF)
289 /* not a Linux kernel */
294 offset = read_leshort (fp, 526);
297 /* can't read version offset */
302 if (fseek (fp, offset + 0x200, SEEK_SET) != 0 ||
303 fgets (buf, size, fp) == NULL)
305 /* can't read version string */
312 buf[strcspn (buf, " \t\n")] = '\0';