summaryrefslogtreecommitdiffstats
path: root/discover/boot.c
diff options
context:
space:
mode:
authorJeremy Kerr <jk@ozlabs.org>2013-02-27 16:45:21 +0800
committerJeremy Kerr <jk@ozlabs.org>2013-04-15 15:42:27 +0800
commitc62667e5c78ea212e5ac49244e9792954a1d8f71 (patch)
tree4206cfff1ddd26ad16e279b065fdf41c00664bb6 /discover/boot.c
parentb8122dc9340e2f208220f0c88b4d71f91b78774f (diff)
downloadtalos-petitboot-c62667e5c78ea212e5ac49244e9792954a1d8f71.tar.gz
talos-petitboot-c62667e5c78ea212e5ac49244e9792954a1d8f71.zip
Move boot to discover server
This change moves the boot-via-kexec functionality from the UIs to the discover server. On the UI side: rather than run kexec directly, we just send a message to the discover server. Because this is generic discover client functionality, we no longer need the boot callbacks in the twin- and ncurses-specific code. We also remove the kexec and URL-loading code from the UIs, and add it to the discover server code, in paths.c. We expose this to the server though a new function: load_path(void *, const char *, unsigned int *); On the server side, we simply move hook up the boot() function to use the load_file and kexec calls. Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
Diffstat (limited to 'discover/boot.c')
-rw-r--r--discover/boot.c157
1 files changed, 151 insertions, 6 deletions
diff --git a/discover/boot.c b/discover/boot.c
index ddb9e7d..6109562 100644
--- a/discover/boot.c
+++ b/discover/boot.c
@@ -1,14 +1,159 @@
+#include <assert.h>
+
+#include <log/log.h>
+#include <pb-protocol/pb-protocol.h>
+#include <system/system.h>
+#include <talloc/talloc.h>
+
#include "boot.h"
+#include "paths.h"
+
+/**
+ * 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 kexec_load(const char *l_image, const char *l_initrd,
+ const char *args, int dry_run)
+{
+ int result;
+ const char *argv[6];
+ const char **p;
+ char *s_initrd = NULL;
+ char *s_args = NULL;
+
+ p = argv;
+ *p++ = pb_system_apps.kexec; /* 1 */
+ *p++ = "-l"; /* 2 */
+
+ if (l_initrd) {
+ s_initrd = talloc_asprintf(NULL, "--initrd=%s", l_initrd);
+ assert(s_initrd);
+ *p++ = s_initrd; /* 3 */
+ }
+
+ if (args) {
+ s_args = talloc_asprintf(NULL, "--append=%s", args);
+ assert(s_args);
+ *p++ = s_args; /* 4 */
+ }
+
+ *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);
+
+ 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' */
+
+ 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);
+
+ /* On error, force a kexec with the -e option */
+
+ if (result) {
+ p = argv;
+ *p++ = pb_system_apps.kexec; /* 1 */
+ *p++ = "-e"; /* 2 */
+ *p++ = NULL; /* 3 */
+
+ result = pb_run_cmd(argv, 1, 0);
+ }
+
+ if (result)
+ pb_log("%s: failed: (%d)\n", __func__, result);
+
+ return result;
+}
int boot(void *ctx, struct boot_option *opt, struct boot_command *cmd,
int dry_run)
{
- /* todo: run kexec with options from opt & cmd */
- (void)ctx;
- (void)opt;
- (void)cmd;
- (void)dry_run;
+ char *local_image, *local_initrd;
+ unsigned int clean_image = 0;
+ unsigned int clean_initrd = 0;
+ char *image, *initrd, *args;
+ int result;
+
+ image = NULL;
+ initrd = NULL;
+ args = NULL;
+
+ if (cmd->boot_image_file) {
+ image = talloc_strdup(ctx, cmd->boot_image_file);
+ } else if (opt && opt->boot_image_file) {
+ image = talloc_strdup(ctx, opt->boot_image_file);
+ } else {
+ pb_log("%s: no image specified", __func__);
+ return -1;
+ }
+
+ if (cmd->initrd_file) {
+ image = talloc_strdup(ctx, cmd->initrd_file);
+ } else if (opt && opt->initrd_file) {
+ image = talloc_strdup(ctx, opt->initrd_file);
+ }
+
+ if (cmd->boot_args) {
+ args = talloc_strdup(ctx, cmd->boot_args);
+ } else if (opt && opt->boot_args) {
+ args = talloc_strdup(ctx, opt->boot_args);
+ }
+
+ result = -1;
+
+ local_image = load_file(NULL, image, &clean_image);
+ if (!local_image)
+ goto no_load;
+
+ local_initrd = NULL;
+ if (initrd) {
+ local_initrd = load_file(NULL, initrd, &clean_initrd);
+ if (!local_initrd)
+ goto no_load;
+ }
+
+ result = kexec_load(local_image, local_initrd, args, dry_run);
+
+no_load:
+ if (clean_image)
+ unlink(local_image);
+ if (clean_initrd)
+ unlink(local_initrd);
+
+ talloc_free(local_image);
+ talloc_free(local_initrd);
+
+ if (!result)
+ result = kexec_reboot(dry_run);
- return 0;
+ return result;
}
OpenPOWER on IntegriCloud