2 * Copyright (C) 2010 Red Hat Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library 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 GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 #include <sys/select.h>
33 #ifdef HAVE_SYS_TYPES_H
34 #include <sys/types.h>
38 #include "guestfs-internal.h"
39 #include "guestfs-internal-actions.h"
40 #include "guestfs_protocol.h"
42 static const char *kernel_name = "vmlinuz." REPO "." host_cpu;
43 static const char *initrd_name = "initramfs." REPO "." host_cpu ".img";
45 static int find_path (guestfs_h *g, int (*pred) (guestfs_h *g, const char *pelem, void *data), void *data, char **pelem);
46 static int dir_contains_file (const char *dir, const char *file);
47 static int dir_contains_files (const char *dir, ...);
48 static int contains_supermin_appliance (guestfs_h *g, const char *path, void *data);
49 static int contains_ordinary_appliance (guestfs_h *g, const char *path, void *data);
50 static char *calculate_supermin_checksum (guestfs_h *g, const char *supermin_path);
51 static int check_for_cached_appliance (guestfs_h *g, const char *supermin_path, const char *checksum, char **kernel, char **initrd, char **appliance);
52 static int build_supermin_appliance (guestfs_h *g, const char *supermin_path, const char *checksum, char **kernel, char **initrd, char **appliance);
53 static int run_supermin_helper (guestfs_h *g, const char *supermin_path, const char *cachedir, size_t cdlen);
55 /* Locate or build the appliance.
57 * This function locates or builds the appliance as necessary,
58 * handling the supermin appliance, caching of supermin-built
59 * appliances, or using an ordinary appliance.
61 * The return value is 0 = good, -1 = error. Returned in '*kernel'
62 * will be the name of the kernel to use, '*initrd' the name of the
63 * initrd, '*appliance' the name of the ext2 root filesystem.
64 * '*appliance' can be NULL, meaning that we are using an ordinary
65 * (non-ext2) appliance. All three strings must be freed by the
66 * caller. However the referenced files themselves must not be
69 * The process is as follows:
71 * (1) Look for the first element of g->path which contains a
72 * supermin appliance skeleton. If no element has this, skip
73 * straight to step (5).
74 * (2) Calculate the checksum of this supermin appliance.
75 * (3) Check whether $TMPDIR/$checksum/ directory exists, contains
76 * a cached appliance, and passes basic security checks. If so,
77 * return this appliance.
78 * (4) Try to build the supermin appliance into $TMPDIR/$checksum/.
79 * If this is successful, return it.
80 * (5) Check each element of g->path, looking for an ordinary appliance.
81 * If one is found, return it.
84 guestfs___build_appliance (guestfs_h *g,
85 char **kernel, char **initrd, char **appliance)
91 r = find_path (g, contains_supermin_appliance, NULL, &supermin_path);
96 /* Step (2): calculate checksum. */
97 char *checksum = calculate_supermin_checksum (g, supermin_path);
99 /* Step (3): cached appliance exists? */
100 r = check_for_cached_appliance (g, supermin_path, checksum,
101 kernel, initrd, appliance);
103 free (supermin_path);
105 return r == 1 ? 0 : -1;
108 /* Step (4): build supermin appliance. */
109 r = build_supermin_appliance (g, supermin_path, checksum,
110 kernel, initrd, appliance);
111 free (supermin_path);
115 free (supermin_path);
120 r = find_path (g, contains_ordinary_appliance, NULL, &path);
125 size_t len = strlen (path);
126 *kernel = safe_malloc (g, len + strlen (kernel_name) + 2);
127 *initrd = safe_malloc (g, len + strlen (initrd_name) + 2);
128 sprintf (*kernel, "%s/%s", path, kernel_name);
129 sprintf (*initrd, "%s/%s", path, initrd_name);
136 error (g, _("cannot find any suitable libguestfs supermin or ordinary appliance on LIBGUESTFS_PATH (search path: %s)"),
142 contains_supermin_appliance (guestfs_h *g, const char *path, void *data)
144 return dir_contains_files (path, "supermin.d", "kmod.whitelist", NULL);
148 contains_ordinary_appliance (guestfs_h *g, const char *path, void *data)
150 return dir_contains_files (path, kernel_name, initrd_name, NULL);
153 /* supermin_path is a path which is known to contain a supermin
154 * appliance. Using febootstrap-supermin-helper -f checksum calculate
155 * the checksum so we can see if it is cached.
158 calculate_supermin_checksum (guestfs_h *g, const char *supermin_path)
160 size_t len = 2 * strlen (supermin_path) + 256;
163 "febootstrap-supermin-helper%s "
165 "-k '%s/kmod.whitelist' "
168 g->verbose ? " --verbose" : "",
173 guestfs___print_timestamped_message (g, "%s", cmd);
175 /* Errors here are non-fatal, so we don't need to call error(). */
176 FILE *pp = popen (cmd, "r");
181 if (fgets (checksum, sizeof checksum, pp) == NULL) {
186 if (pclose (pp) == -1) {
191 len = strlen (checksum);
193 if (len < 16) { /* sanity check */
194 fprintf (stderr, "libguestfs: internal error: febootstrap-supermin-helper -f checksum returned a short string\n");
198 if (len > 0 && checksum[len-1] == '\n')
199 checksum[--len] = '\0';
201 return safe_strndup (g, checksum, len);
204 /* Check for cached appliance in $TMPDIR/$checksum. Check it exists
205 * and passes some basic security checks.
208 * 1 = exists, and passes
210 * -1 = error which should abort the whole launch process
213 security_check_cache_file (guestfs_h *g, const char *filename,
214 const struct stat *statbuf)
216 uid_t uid = geteuid ();
218 if (statbuf->st_uid != uid) {
219 error (g, ("libguestfs cached appliance %s is not owned by UID %d\n"),
224 if ((statbuf->st_mode & 0022) != 0) {
225 error (g, ("libguestfs cached appliance %s is writable by group or other (mode %o)\n"),
226 filename, statbuf->st_mode);
234 check_for_cached_appliance (guestfs_h *g,
235 const char *supermin_path, const char *checksum,
236 char **kernel, char **initrd, char **appliance)
238 const char *tmpdir = guestfs_tmpdir ();
240 size_t len = strlen (tmpdir) + strlen (checksum) + 2;
242 snprintf (cachedir, len, "%s/%s", tmpdir, checksum);
244 /* Touch the directory to prevent it being deleting in a rare race
245 * between us doing the checks and a tmp cleaner running. Note this
246 * doesn't create the directory, and we ignore any error.
248 (void) utime (cachedir, NULL);
250 /* See if the cache directory exists and passes some simple checks
251 * to make sure it has not been tampered with. Note that geteuid()
252 * forms a part of the checksum.
255 if (lstat (cachedir, &statbuf) == -1)
258 if (security_check_cache_file (g, cachedir, &statbuf) == -1)
263 *kernel = safe_malloc (g, len + 8 /* / + "kernel" + \0 */);
264 *initrd = safe_malloc (g, len + 8 /* / + "initrd" + \0 */);
265 *appliance = safe_malloc (g, len + 6 /* / + "root" + \0 */);
266 sprintf (*kernel, "%s/kernel", cachedir);
267 sprintf (*initrd, "%s/initrd", cachedir);
268 sprintf (*appliance, "%s/root", cachedir);
270 /* Touch the files to prevent them being deleted, and to bring the
271 * cache up to date. Note this doesn't create the files.
273 (void) utime (*kernel, NULL);
275 /* NB. *kernel is a symlink, so we want to check the kernel, not the
276 * link (stat, not lstat). We don't do a security check on the
277 * kernel since it's always under /boot.
279 if (stat (*kernel, &statbuf) == -1) {
284 (void) utime (*initrd, NULL);
286 if (lstat (*initrd, &statbuf) == -1) {
291 if (security_check_cache_file (g, *initrd, &statbuf) == -1) {
296 (void) utime (*appliance, NULL);
298 if (lstat (*appliance, &statbuf) == -1) {
303 if (security_check_cache_file (g, *appliance, &statbuf) == -1) {
318 /* Build supermin appliance from supermin_path to $TMPDIR/$checksum.
322 * -1 = error (aborts launch)
325 build_supermin_appliance (guestfs_h *g,
326 const char *supermin_path, const char *checksum,
327 char **kernel, char **initrd, char **appliance)
330 guestfs___print_timestamped_message (g, "begin building supermin appliance");
332 const char *tmpdir = guestfs_tmpdir ();
333 size_t cdlen = strlen (tmpdir) + strlen (checksum) + 2;
334 char cachedir[cdlen];
335 snprintf (cachedir, cdlen, "%s/%s", tmpdir, checksum);
337 /* Don't worry about this failing, because the
338 * febootstrap-supermin-helper command will fail if the directory
339 * doesn't exist. Note the directory might already exist, eg. if a
340 * tmp cleaner has removed the existing appliance but not the
343 (void) mkdir (cachedir, 0755);
346 guestfs___print_timestamped_message (g, "run febootstrap-supermin-helper");
348 int r = run_supermin_helper (g, supermin_path, cachedir, cdlen);
353 guestfs___print_timestamped_message (g, "finished building supermin appliance");
355 *kernel = safe_malloc (g, cdlen + 8 /* / + "kernel" + \0 */);
356 *initrd = safe_malloc (g, cdlen + 8 /* / + "initrd" + \0 */);
357 *appliance = safe_malloc (g, cdlen + 6 /* / + "root" + \0 */);
358 sprintf (*kernel, "%s/kernel", cachedir);
359 sprintf (*initrd, "%s/initrd", cachedir);
360 sprintf (*appliance, "%s/root", cachedir);
365 /* Run febootstrap-supermin-helper and tell it to generate the
366 * appliance. Note that we have to do an explicit fork/exec here.
367 * 'system' goes via the shell, and on systems that have bash, bash
368 * has a misfeature where it resets the euid to uid which breaks
369 * virt-v2v. 'posix_spawn' was also considered but that doesn't allow
370 * us to reset the umask.
373 run_supermin_helper (guestfs_h *g, const char *supermin_path,
374 const char *cachedir, size_t cdlen)
382 if (pid > 0) { /* Parent. */
384 if (waitpid (pid, &status, 0) == -1) {
385 perrorf (g, "waitpid");
388 if (!WIFEXITED (status) || WEXITSTATUS (status) != 0) {
389 error (g, _("external command failed, see earlier error messages"));
397 /* Set a sensible umask in the subprocess, so kernel and initrd
398 * output files are world-readable (RHBZ#610880).
402 /* Set uid/gid in the child. This is a workaround for a misfeature
403 * in bash which breaks virt-v2v - see the comment at the top of
406 if (getuid () == 0) {
407 int egid = getegid ();
408 int euid = geteuid ();
410 if (egid != 0 || euid != 0) {
411 if (seteuid (0) == -1) {
413 _exit (EXIT_FAILURE);
416 if (setgid (egid) == -1) {
418 _exit (EXIT_FAILURE);
421 if (setuid (euid) == -1) {
423 _exit (EXIT_FAILURE);
428 size_t pathlen = strlen (supermin_path);
430 const char *argv[30];
433 argv[i++] = "febootstrap-supermin-helper";
435 argv[i++] = "--verbose";
439 char whitelist[pathlen + 32];
440 snprintf (whitelist, pathlen + 32, "%s/kmod.whitelist", supermin_path);
441 argv[i++] = whitelist;
442 char supermin_d[pathlen + 32];
443 snprintf (supermin_d, pathlen + 32, "%s/supermin.d", supermin_path);
444 argv[i++] = supermin_d;
445 argv[i++] = host_cpu;
446 char kernel[cdlen + 32];
447 snprintf (kernel, cdlen + 32, "%s/kernel", cachedir);
449 char initrd[cdlen + 32];
450 snprintf (initrd, cdlen + 32, "%s/initrd", cachedir);
452 char root[cdlen + 32];
453 snprintf (root, cdlen + 32, "%s/root", cachedir);
457 execvp ("febootstrap-supermin-helper", (char * const *) argv);
459 _exit (EXIT_FAILURE);
462 /* Search elements of g->path, returning the first path element which
463 * matches the predicate function 'pred'.
465 * Function 'pred' must return a true or false value. If it returns
466 * -1 then the entire search is aborted.
469 * 1 = a path element matched, it is returned in *pelem_ret and must be
470 * freed by the caller,
471 * 0 = no path element matched, *pelem_ret is set to NULL, or
472 * -1 = error which aborts the launch process
475 find_path (guestfs_h *g,
476 int (*pred) (guestfs_h *g, const char *pelem, void *data),
482 const char *pelem = g->path;
484 /* Note that if g->path is an empty string, we want to check the
485 * current directory (for backwards compatibility with
486 * libguestfs < 1.5.4).
489 len = strcspn (pelem, ":");
491 /* Empty element or "." means current directory. */
493 *pelem_ret = safe_strdup (g, ".");
495 *pelem_ret = safe_strndup (g, pelem, len);
497 r = pred (g, *pelem_ret, data);
503 if (r != 0) /* predicate matched */
508 if (pelem[len] == ':')
514 /* Predicate didn't match on any path element. */
519 /* Returns true iff file is contained in dir. */
521 dir_contains_file (const char *dir, const char *file)
523 size_t dirlen = strlen (dir);
524 size_t filelen = strlen (file);
525 size_t len = dirlen + filelen + 2;
528 snprintf (path, len, "%s/%s", dir, file);
529 return access (path, F_OK) == 0;
532 /* Returns true iff every listed file is contained in 'dir'. */
534 dir_contains_files (const char *dir, ...)
539 va_start (args, dir);
540 while ((file = va_arg (args, const char *)) != NULL) {
541 if (!dir_contains_file (dir, file)) {