+/* NB: lock on checksum file must be held when this is called. */
+static int
+hard_link_to_cached_appliance (guestfs_h *g,
+ const char *cachedir,
+ char **kernel, char **initrd, char **appliance)
+{
+ pid_t pid = getpid ();
+ size_t len = strlen (cachedir) + 32;
+
+ *kernel = safe_malloc (g, len);
+ *initrd = safe_malloc (g, len);
+ *appliance = safe_malloc (g, len);
+ snprintf (*kernel, len, "%s/kernel.%d", cachedir, pid);
+ snprintf (*initrd, len, "%s/initrd.%d", cachedir, pid);
+ snprintf (*appliance, len, "%s/root.%d", cachedir, pid);
+
+ char filename[len];
+ snprintf (filename, len, "%s/kernel", cachedir);
+ (void) unlink (*kernel);
+ if (link (filename, *kernel) == -1) {
+ perrorf (g, "link: %s %s", filename, *kernel);
+ goto error;
+ }
+ (void) lutimes (filename, NULL); /* lutimes because it's a symlink */
+
+ snprintf (filename, len, "%s/initrd", cachedir);
+ (void) unlink (*initrd);
+ if (link (filename, *initrd) == -1) {
+ perrorf (g, "link: %s %s", filename, *initrd);
+ goto error;
+ }
+ (void) utime (filename, NULL);
+
+ snprintf (filename, len, "%s/root", cachedir);
+ (void) unlink (*appliance);
+ if (link (filename, *appliance) == -1) {
+ perrorf (g, "link: %s %s", filename, *appliance);
+ goto error;
+ }
+ (void) utime (filename, NULL);
+
+ return 0;
+
+ error:
+ free (*kernel);
+ free (*initrd);
+ free (*appliance);
+ return -1;
+}
+
+/* Run febootstrap-supermin-helper and tell it to generate the
+ * appliance.
+ */
+static int
+run_supermin_helper (guestfs_h *g, const char *supermin_path,
+ const char *cachedir, size_t cdlen)
+{
+ size_t pathlen = strlen (supermin_path);
+
+ const char *argv[30];
+ size_t i = 0;
+
+ char uid[32];
+ snprintf (uid, sizeof uid, "%i", geteuid ());
+ char gid[32];
+ snprintf (gid, sizeof gid, "%i", getegid ());
+ char supermin_d[pathlen + 32];
+ snprintf (supermin_d, pathlen + 32, "%s/supermin.d", supermin_path);
+ char kernel[cdlen + 32];
+ snprintf (kernel, cdlen + 32, "%s/kernel", cachedir);
+ char initrd[cdlen + 32];
+ snprintf (initrd, cdlen + 32, "%s/initrd", cachedir);
+ char root[cdlen + 32];
+ snprintf (root, cdlen + 32, "%s/root", cachedir);
+
+ int pass_u_g_args = getuid () != geteuid () || getgid () != getegid ();
+
+ argv[i++] = "febootstrap-supermin-helper";
+ if (g->verbose)
+ argv[i++] = "--verbose";
+ if (pass_u_g_args) {
+ argv[i++] = "-u";
+ argv[i++] = uid;
+ argv[i++] = "-g";
+ argv[i++] = gid;
+ }
+ argv[i++] = "-f";
+ argv[i++] = "ext2";
+ argv[i++] = supermin_d;
+ argv[i++] = host_cpu;
+ argv[i++] = kernel;
+ argv[i++] = initrd;
+ argv[i++] = root;
+ argv[i++] = NULL;
+
+ pid_t pid = fork ();
+ if (pid == -1) {
+ perrorf (g, "fork");
+ return -1;
+ }
+
+ if (pid > 0) { /* Parent. */
+ if (g->verbose)
+ guestfs___print_timestamped_argv (g, argv);
+
+ int status;
+ if (waitpid (pid, &status, 0) == -1) {
+ perrorf (g, "waitpid");
+ return -1;
+ }
+ if (!WIFEXITED (status) || WEXITSTATUS (status) != 0) {
+ error (g, _("external command failed, see earlier error messages"));
+ return -1;
+ }
+ return 0;
+ }
+
+ /* Child. */
+
+ /* Set a sensible umask in the subprocess, so kernel and initrd
+ * output files are world-readable (RHBZ#610880).
+ */
+ umask (0022);
+
+ execvp ("febootstrap-supermin-helper", (char * const *) argv);
+ perror ("execvp");
+ _exit (EXIT_FAILURE);
+}
+