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.
29 #include "xvasprintf.h"
33 /* Directory containing candidate kernels. We could make this
34 * configurable at some point.
36 #define KERNELDIR "/boot"
37 #define MODULESDIR "/lib/modules"
40 get_modpath (const char *kernel_name)
42 /* Ignore "vmlinuz-" at the beginning of the kernel name. */
43 const char *version = &kernel_name[8];
45 /* /lib/modules/<version> */
46 char *modpath = xasprintf (MODULESDIR "/%s", version);
55 /* kernel_name is "vmlinuz-*". Check if there is a corresponding
56 * module path in /lib/modules.
59 has_modpath (const char *kernel_name)
61 char *modpath = get_modpath (kernel_name);
64 fprintf (stderr, "checking modpath %s is a directory\n", modpath);
66 int r = isdir (modpath);
70 fprintf (stderr, "picked %s because modpath %s exists\n",
71 kernel_name, modpath);
81 static char *create_kernel_archlinux (const char *hostcpu, const char *kernel);
83 /* Create the kernel. This chooses an appropriate kernel and makes a
86 * Look for the most recent kernel named vmlinuz-*.<arch>* which has a
87 * corresponding directory in /lib/modules/. If the architecture is
88 * x86, look for any x86 kernel.
90 * RHEL 5 didn't append the arch to the kernel name, so look for
91 * kernels without arch second.
93 * If no suitable kernel can be found, exit with an error.
95 * This function returns the module path (ie. /lib/modules/<version>).
98 create_kernel (const char *hostcpu, const char *kernel)
100 /* In ArchLinux, kernel is always named /boot/vmlinuz26. */
101 if (access ("/boot/vmlinuz26", F_OK) == 0)
102 return create_kernel_archlinux (hostcpu, kernel);
104 /* In original: ls -1dvr /boot/vmlinuz-*.$arch* 2>/dev/null | grep -v xen */
106 if (hostcpu[0] == 'i' && hostcpu[2] == '8' && hostcpu[3] == '6' &&
108 patt = "vmlinuz-*.i?86*";
110 patt = xasprintf ("vmlinuz-*.%s*", hostcpu);
112 char **all_files = read_dir (KERNELDIR);
114 candidates = filter_fnmatch (all_files, patt, FNM_NOESCAPE);
115 candidates = filter_notmatching_substring (candidates, "xen");
116 candidates = filter (candidates, has_modpath);
118 if (candidates[0] == NULL) {
119 /* In original: ls -1dvr /boot/vmlinuz-* 2>/dev/null | grep -v xen */
121 candidates = filter_fnmatch (all_files, patt, FNM_NOESCAPE);
122 candidates = filter_notmatching_substring (candidates, "xen");
123 candidates = filter (candidates, has_modpath);
125 if (candidates[0] == NULL)
129 sort (candidates, reverse_filevercmp);
132 /* Choose the first candidate. */
133 char *tmp = xasprintf (KERNELDIR "/%s", candidates[0]);
136 fprintf (stderr, "creating symlink %s -> %s\n", kernel, tmp);
138 if (symlink (tmp, kernel) == -1)
139 error (EXIT_FAILURE, errno, "symlink kernel");
144 return get_modpath (candidates[0]);
146 /* Print more diagnostics here than the old script did. */
149 "febootstrap-supermin-helper: failed to find a suitable kernel.\n"
150 "I looked for kernels in " KERNELDIR " and modules in " MODULESDIR
152 "If this is a Xen guest, and you only have Xen domU kernels\n"
153 "installed, try installing a fullvirt kernel (only for\n"
154 "febootstrap use, you shouldn't boot the Xen guest with it).\n");
158 /* In ArchLinux, kernel is always named /boot/vmlinuz26, and we have
159 * to use the 'file' command to work out what version it is.
162 create_kernel_archlinux (const char *hostcpu, const char *kernel)
164 const char *file_cmd = "file /boot/vmlinuz26 | awk '{print $9}'";
166 char modversion[256];
170 pp = popen (file_cmd, "r");
173 fprintf (stderr, "febootstrap-supermin-helper: %s: command failed\n",
178 if (fgets (modversion, sizeof modversion, pp) == NULL)
181 if (pclose (pp) == -1)
185 len = strlen (modversion);
186 if (len > 0 && modversion[len-1] == '\n') {
187 modversion[len-1] = '\0';
191 /* Generate module path. */
192 modpath = xasprintf (MODULESDIR "/%s", modversion);
194 /* Check module path is a directory. */
195 if (!isdir (modpath)) {
196 fprintf (stderr, "febootstrap-supermin-helper: /boot/vmlinuz26 kernel exists but %s is not a valid module path\n",
202 /* Symlink from kernel to /boot/vmlinuz26. */
203 if (symlink ("/boot/vmlinuz26", kernel) == -1)
204 error (EXIT_FAILURE, errno, "symlink kernel");
207 /* Return module path. */