summaryrefslogtreecommitdiffstats
path: root/helper/kernel.c
blob: 88dc940503163a88309afc1666bedfdb240a96b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/* febootstrap-supermin-helper reimplementation in C.
 * Copyright (C) 2009-2010 Red Hat Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fnmatch.h>
#include <unistd.h>
#include <errno.h>
#include <sys/utsname.h>

#include "error.h"
#include "xvasprintf.h"

#include "helper.h"

/* Directory containing candidate kernels.  We could make this
 * configurable at some point.
 */
#define KERNELDIR "/boot"
#define MODULESDIR "/lib/modules"

static char* get_kernel_version (char* filename);
static const char *create_kernel_from_env (const char *hostcpu, const char *kernel, const char *kernel_env, const char *modpath_env);

static char *
get_modpath (const char *kernel_name)
{
  /* Ignore "vmlinuz-" at the beginning of the kernel name. */
  const char *version = &kernel_name[8];

  /* /lib/modules/<version> */
  char *modpath = xasprintf (MODULESDIR "/%s", version);
  if (!modpath) {
    perror ("xasprintf");
    exit (EXIT_FAILURE);
  }

  if (! isdir (modpath)) {
    char* path;
    char* version;
    path = xasprintf (KERNELDIR "/%s", kernel_name);
    if (!path) {
      perror ("xasprintf");
      exit (EXIT_FAILURE);
    }
    version = get_kernel_version (path);
    free (path);
    if (version != NULL) {
      free (modpath);
      modpath = xasprintf (MODULESDIR "/%s", version);
      free (version);
      if (!path) {
        perror ("xasprintf");
        exit (EXIT_FAILURE);
      }
    }
  }

  return modpath;
}

/* kernel_name is "vmlinuz-*".  Check if there is a corresponding
 * module path in /lib/modules.
 */
static int
has_modpath (const char *kernel_name)
{
  char *modpath = get_modpath (kernel_name);

  if (verbose)
    fprintf (stderr, "checking modpath %s is a directory\n", modpath);

  int r = isdir (modpath);

  if (r) {
    if (verbose)
      fprintf (stderr, "picked %s because modpath %s exists\n",
               kernel_name, modpath);
    free (modpath);
    return 1;
  }
  else {
    free (modpath);
    return 0;
  }
}

/* Create the kernel.  This chooses an appropriate kernel and makes a
 * symlink to it.
 *
 * Look for the most recent kernel named vmlinuz-*.<arch>* which has a
 * corresponding directory in /lib/modules/. If the architecture is
 * x86, look for any x86 kernel.
 *
 * RHEL 5 didn't append the arch to the kernel name, so look for
 * kernels without arch second.
 *
 * If no suitable kernel can be found, exit with an error.
 *
 * This function returns the module path (ie. /lib/modules/<version>).
 */
const char *
create_kernel (const char *hostcpu, const char *kernel)
{
  /* Override kernel selection using environment variables? */
  char *kernel_env = getenv ("FEBOOTSTRAP_KERNEL");
  if (kernel_env) {
    char *modpath_env = getenv ("FEBOOTSTRAP_MODULES");
    return create_kernel_from_env (hostcpu, kernel, kernel_env, modpath_env);
  }

  /* In original: ls -1dvr /boot/vmlinuz-*.$arch* 2>/dev/null | grep -v xen */
  const char *patt;
  if (hostcpu[0] == 'i' && hostcpu[2] == '8' && hostcpu[3] == '6' &&
      hostcpu[4] == '\0')
    patt = "vmlinuz-*.i?86*";
  else
    patt = xasprintf ("vmlinuz-*.%s*", hostcpu);

  char **all_files = read_dir (KERNELDIR);
  char **candidates;
  candidates = filter_fnmatch (all_files, patt, FNM_NOESCAPE);
  candidates = filter_notmatching_substring (candidates, "xen");
  candidates = filter (candidates, has_modpath);

  if (candidates[0] == NULL) {
    /* In original: ls -1dvr /boot/vmlinuz-* 2>/dev/null | grep -v xen */
    patt = "vmlinuz-*";
    candidates = filter_fnmatch (all_files, patt, FNM_NOESCAPE);
    candidates = filter_notmatching_substring (candidates, "xen");
    candidates = filter (candidates, has_modpath);

    if (candidates[0] == NULL)
      goto no_kernels;
  }

  sort (candidates, reverse_filevercmp);

  if (kernel) {
    /* Choose the first candidate. */
    char *tmp = xasprintf (KERNELDIR "/%s", candidates[0]);

    if (verbose >= 2)
      fprintf (stderr, "creating symlink %s -> %s\n", kernel, tmp);

    if (symlink (tmp, kernel) == -1)
      error (EXIT_FAILURE, errno, "symlink kernel");

    free (tmp);
  }

  return get_modpath (candidates[0]);

  /* Print more diagnostics here than the old script did. */
 no_kernels:
  fprintf (stderr,
           "febootstrap-supermin-helper: failed to find a suitable kernel.\n"
           "I looked for kernels in " KERNELDIR " and modules in " MODULESDIR
           ".\n"
           "If this is a Xen guest, and you only have Xen domU kernels\n"
           "installed, try installing a fullvirt kernel (only for\n"
           "febootstrap use, you shouldn't boot the Xen guest with it).\n");
  exit (EXIT_FAILURE);
}

/* Select the kernel from environment variables set by the user.
 * modpath_env may be NULL, in which case we attempt to work it out
 * from kernel_env.
 */
static const char *
create_kernel_from_env (const char *hostcpu, const char *kernel,
                        const char *kernel_env, const char *modpath_env)
{
  if (verbose) {
    fprintf (stderr,
             "febootstrap-supermin-helper: using environment variable(s) FEBOOTSTRAP_* to\n"
             "select kernel %s", kernel_env);
    if (modpath_env)
      fprintf (stderr, " and module path %s", modpath_env);
    fprintf (stderr, "\n");
  }

  if (!isfile (kernel_env)) {
    fprintf (stderr,
             "febootstrap-supermin-helper: %s: not a regular file\n"
             "(what is $FEBOOTSTRAP_KERNEL set to?)\n", kernel_env);
    exit (EXIT_FAILURE);
  }

  if (!modpath_env) {
    /* Try to guess modpath from kernel path. */
    const char *p = strrchr (kernel_env, '/');
    if (p) p++; else p = kernel_env;

    /* NB: We need the extra test to ensure calling get_modpath is safe. */
    if (strncmp (p, "vmlinuz-", 8) != 0) {
      fprintf (stderr,
               "febootstrap-supermin-helper: cannot guess module path.\n"
               "Set $FEBOOTSTRAP_MODULES to the modules directory corresponding to\n"
               "kernel %s, or unset $FEBOOTSTRAP_KERNEL to autoselect a kernel.\n",
               kernel_env);
      exit (EXIT_FAILURE);
    }

    modpath_env = get_modpath (p);
  }

  if (!isdir (modpath_env)) {
    fprintf (stderr,
             "febootstrap-supermin-helper: %s: not a directory\n"
             "(what is $FEBOOTSTRAP_MODULES set to?)\n", modpath_env);
    exit (EXIT_FAILURE);
  }

  /* Create the symlink. */
  if (kernel) {
    if (verbose >= 2)
      fprintf (stderr, "creating symlink %s -> %s\n", kernel_env, kernel);

    if (symlink (kernel_env, kernel) == -1)
      error (EXIT_FAILURE, errno, "symlink kernel");
  }

  return modpath_env;
}

/* Read an unsigned little endian short at a specified offset in a file.
 * Returns a non-negative int on success or -1 on failure.
 */
static int
read_leshort (FILE* fp, int offset)
{
  char buf[2];
  if (fseek (fp, offset, SEEK_SET) != 0 ||
      fread (buf, sizeof(char), 2, fp) != 2)
  {
    return -1;
  }
  return ((buf[1] & 0xFF) << 8) | (buf[0] & 0xFF);
}

/* Extract the kernel version from a Linux kernel file.
 * Returns a malloc'd string containing the version or NULL if the
 * file can't be read, is not a Linux kernel, or the version can't
 * be found.
 *
 * See ftp://ftp.astron.com/pub/file/file-<ver>.tar.gz
 * (file-<ver>/magic/Magdir/linux) for the rules used to find the
 * version number:
 *   514             string  HdrS     Linux kernel
 *   >518            leshort >0x1ff
 *   >>(526.s+0x200) string  >\0      version %s,
 *
 * Bugs: probably limited to x86 kernels.
 */
static char*
get_kernel_version (char* filename)
{
  FILE* fp;
  int size = 132;
  char buf[size];
  int offset;

  fp = fopen (filename, "rb");

  if (fseek (fp, 514, SEEK_SET) != 0 ||
      fgets (buf, size, fp) == NULL ||
      strncmp (buf, "HdrS", 4) != 0 ||
      read_leshort (fp, 518) < 0x1FF)
  {
    /* not a Linux kernel */
    fclose (fp);
    return NULL;
  }

  offset = read_leshort (fp, 526);
  if (offset == -1)
  {
    /* can't read version offset */
    fclose (fp);
    return NULL;
  }

  if (fseek (fp, offset + 0x200, SEEK_SET) != 0 ||
      fgets (buf, size, fp) == NULL)
  {
    /* can't read version string */
    fclose (fp);
    return NULL;
  }

  fclose (fp);

  buf[strcspn (buf, " \t\n")] = '\0';
  return strdup (buf);
}