]> git.ozlabs.org Git - petitboot/blobdiff - ui/common/ui-system.c
ui: callback & boot actions: kexec -> boot
[petitboot] / ui / common / ui-system.c
index 3f54191bfd5671c4e92476353a30401a8741bab3..157c6db7797c30ec286ed010094b920e98772b63 100644 (file)
 #include "ui-system.h"
 
 /**
- * run_kexec_local - Final kexec helper.
+ * pb_start_daemon - start the pb-discover daemon.
+ */
+
+int pb_start_daemon(void)
+{
+       int result;
+       const char *argv[2];
+       char *name = talloc_asprintf(NULL, "%s/sbin/pb-discover",
+               pb_system_apps.prefix);
+
+       argv[0] = name;
+       argv[1] =  NULL;
+
+       result = pb_run_cmd(argv, 0, 0);
+
+       talloc_free(name);
+
+       if (result)
+               pb_log("%s: failed: (%d)\n", __func__, result);
+
+       return result;
+}
+
+/**
+ * kexec_load - kexec load helper.
  * @l_image: The local image file for kexec to execute.
  * @l_initrd: Optional local initrd file for kexec --initrd, can be NULL.
  * @args: Optional command line args for kexec --append, can be NULL.
  */
 
-static int run_kexec_local(const char *l_image, const char *l_initrd,
-       const char *args)
+static int kexec_load(const char *l_image, const char *l_initrd,
+       const char *args, int dry_run)
 {
        int result;
-       const char *argv[8];
+       const char *argv[6];
        const char **p;
+       char *s_initrd = NULL;
+       char *s_args = NULL;
 
        p = argv;
-       *p++ = pb_system_apps.kexec;            /* 1 */
+       *p++ = pb_system_apps.kexec;    /* 1 */
+       *p++ = "-l";                    /* 2 */
 
        if (l_initrd) {
-               *p++ = "--initrd";              /* 2 */
-               *p++ = l_initrd;                /* 3 */
+               s_initrd = talloc_asprintf(NULL, "--initrd=%s", l_initrd);
+               assert(s_initrd);
+               *p++ = s_initrd;         /* 3 */
        }
 
        if (args) {
-               *p++ = "--append";              /* 4 */
-               *p++ = args;                    /* 5 */
+               s_args = talloc_asprintf(NULL, "--append=%s", args);
+               assert(s_args);
+               *p++ = s_args;          /* 4 */
        }
 
-       /* First try by telling kexec to run shutdown */
+       *p++ = l_image;                 /* 5 */
+       *p++ = NULL;                    /* 6 */
+
+       result = pb_run_cmd(argv, 1, dry_run);
+
+       if (result)
+               pb_log("%s: failed: (%d)\n", __func__, result);
+
+       talloc_free(s_initrd);
+       talloc_free(s_args);
 
-       *(p + 0) = l_image;
-       *(p + 1) = NULL;
+       return result;
+}
+
+/**
+ * kexec_reboot - Helper to boot the new kernel.
+ *
+ * Must only be called after a successful call to kexec_load().
+ */
+
+static int kexec_reboot(int dry_run)
+{
+       int result = 0;
+       const char *argv[4];
+       const char **p;
+
+       /* First try running shutdown.  Init scripts should run 'exec -e' */
 
-       result = pb_run_cmd(argv);
+       p = argv;
+       *p++ = pb_system_apps.shutdown; /* 1 */
+       *p++ =  "-r";                   /* 2 */
+       *p++ =  "now";                  /* 3 */
+       *p++ =  NULL;                   /* 4 */
+
+       result = pb_run_cmd(argv, 1, dry_run);
 
-       /* kexec will return zero on success */
-       /* On error, force a kexec with the -f option */
+       /* On error, force a kexec with the -e option */
 
        if (result) {
-               *(p + 0) = "-f";                /* 6 */
-               *(p + 1) = l_image;             /* 7 */
-               *(p + 2) = NULL;                /* 8 */
+               p = argv;
+               *p++ = pb_system_apps.kexec;    /* 1 */
+               *p++ = "-e";                    /* 2 */
+               *p++ = NULL;                    /* 3 */
 
-               result = pb_run_cmd(argv);
+               result = pb_run_cmd(argv, 1, 0);
        }
 
        if (result)
@@ -84,39 +142,52 @@ static int run_kexec_local(const char *l_image, const char *l_initrd,
 }
 
 /**
- * pb_run_kexec - Run kexec with the supplied boot options.
- *
- * For the convenience of the user, tries to load both files before
- * returning error.
+ * pb_boot - Run kexec with the supplied boot options.
  */
 
-int pb_run_kexec(const struct pb_kexec_data *kd)
+int pb_boot(const struct pb_boot_data *bd, int dry_run)
 {
        int result;
-       char *l_image;
-       char *l_initrd;
-
-       pb_log("%s: image:  '%s'\n", __func__, kd->image);
-       pb_log("%s: initrd: '%s'\n", __func__, kd->initrd);
-       pb_log("%s: args:   '%s'\n", __func__, kd->args);
-
-       if (kd->image)
-               l_image = pb_load_file(NULL, kd->image);
-       else {
-               l_image = NULL;
-               pb_log("%s: error null image\n", __func__);
+       char *l_image = NULL;
+       char *l_initrd = NULL;
+       unsigned int clean_image = 0;
+       unsigned int clean_initrd = 0;
+
+       pb_log("%s: image:   '%s'\n", __func__, bd->image);
+       pb_log("%s: initrd:  '%s'\n", __func__, bd->initrd);
+       pb_log("%s: args:    '%s'\n", __func__, bd->args);
+
+       result = -1;
+
+       if (bd->image) {
+               l_image = pb_load_file(NULL, bd->image, &clean_image);
+               if (!l_image)
+                       goto no_load;
+       }
+
+       if (bd->initrd) {
+               l_initrd = pb_load_file(NULL, bd->initrd, &clean_initrd);
+               if (!l_initrd)
+                       goto no_load;
        }
 
-       l_initrd = kd->initrd ? pb_load_file(NULL, kd->initrd) : NULL;
+       if (!l_image && !l_initrd)
+               goto no_load;
 
-       if (!l_image || (kd->initrd && !l_initrd))
-               result = -1;
-       else
-               result = run_kexec_local(l_image, l_initrd, kd->args);
+       result = kexec_load(l_image, l_initrd, bd->args, dry_run);
+
+no_load:
+       if (clean_image)
+               unlink(l_image);
+       if (clean_initrd)
+               unlink(l_initrd);
 
        talloc_free(l_image);
        talloc_free(l_initrd);
 
+       if (!result)
+               result = kexec_reboot(dry_run);
+
        return result;
 }