summaryrefslogtreecommitdiffstats
path: root/discover
diff options
context:
space:
mode:
Diffstat (limited to 'discover')
-rw-r--r--discover/boot.c157
-rw-r--r--discover/paths.c277
-rw-r--r--discover/paths.h3
3 files changed, 431 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;
}
diff --git a/discover/paths.c b/discover/paths.c
index c403691..ef8a45f 100644
--- a/discover/paths.c
+++ b/discover/paths.c
@@ -5,6 +5,9 @@
#include <stdlib.h>
#include <talloc/talloc.h>
+#include <system/system.h>
+#include <url/url.h>
+#include <log/log.h>
#include "paths.h"
@@ -153,3 +156,277 @@ char *join_paths(void *alloc_ctx, const char *a, const char *b)
return full_path;
}
+
+static char *local_name(void *ctx)
+{
+ char *tmp, *ret;
+
+ tmp = tempnam(NULL, "pb-");
+
+ if (!tmp)
+ return NULL;
+
+ ret = talloc_strdup(ctx, tmp);
+ free(tmp);
+
+ return ret;
+}
+
+/**
+ * pb_load_nfs - Mounts the NFS export and returns the local file path.
+ *
+ * Returns the local file path in a talloc'ed character string on success,
+ * or NULL on error.
+ */
+static char *load_nfs(void *ctx, struct pb_url *url)
+{
+ int result;
+ const char *argv[8];
+ const char **p;
+ char *local;
+ char *opts;
+
+ local = local_name(ctx);
+
+ if (!local)
+ return NULL;
+
+ result = pb_mkdir_recursive(local);
+
+ if (result)
+ goto fail;
+
+ opts = talloc_strdup(NULL, "ro,nolock,nodiratime");
+
+ if (url->port)
+ opts = talloc_asprintf_append(opts, ",port=%s", url->port);
+
+ p = argv;
+ *p++ = pb_system_apps.mount; /* 1 */
+ *p++ = "-t"; /* 2 */
+ *p++ = "nfs"; /* 3 */
+ *p++ = opts; /* 4 */
+ *p++ = url->host; /* 5 */
+ *p++ = url->dir; /* 6 */
+ *p++ = local; /* 7 */
+ *p++ = NULL; /* 8 */
+
+ result = pb_run_cmd(argv, 1, 0);
+
+ talloc_free(opts);
+
+ if (result)
+ goto fail;
+
+ local = talloc_asprintf_append(local, "/%s", url->path);
+ pb_log("%s: local '%s'\n", __func__, local);
+
+ return local;
+
+fail:
+ pb_rmdir_recursive("/", local);
+ talloc_free(local);
+ return NULL;
+}
+
+/**
+ * pb_load_sftp - Loads a remote file via sftp and returns the local file path.
+ *
+ * Returns the local file path in a talloc'ed character string on success,
+ * or NULL on error.
+ */
+static char *load_sftp(void *ctx, struct pb_url *url)
+{
+ int result;
+ const char *argv[4];
+ const char **p;
+ char *local;
+
+ local = local_name(ctx);
+
+ if (!local)
+ return NULL;
+
+ p = argv;
+ *p++ = pb_system_apps.sftp; /* 1 */
+ *p++ = talloc_asprintf(local, "%s:%s", url->host, url->path); /* 2 */
+ *p++ = local; /* 3 */
+ *p++ = NULL; /* 4 */
+
+ result = pb_run_cmd(argv, 1, 0);
+
+ if (result)
+ goto fail;
+
+ return local;
+
+fail:
+ talloc_free(local);
+ return NULL;
+}
+
+/**
+ * pb_load_tftp - Loads a remote file via tftp and returns the local file path.
+ *
+ * Returns the local file path in a talloc'ed character string on success,
+ * or NULL on error.
+ */
+
+static char *load_tftp(void *ctx, struct pb_url *url)
+{
+ int result;
+ const char *argv[10];
+ const char **p;
+ char *local;
+
+ local = local_name(ctx);
+
+ if (!local)
+ return NULL;
+
+ /* first try busybox tftp args */
+
+ p = argv;
+ *p++ = pb_system_apps.tftp; /* 1 */
+ *p++ = "-g"; /* 2 */
+ *p++ = "-l"; /* 3 */
+ *p++ = local; /* 4 */
+ *p++ = "-r"; /* 5 */
+ *p++ = url->path; /* 6 */
+ *p++ = url->host; /* 7 */
+ if (url->port)
+ *p++ = url->port; /* 8 */
+ *p++ = NULL; /* 9 */
+
+ result = pb_run_cmd(argv, 1, 0);
+
+ if (!result)
+ return local;
+
+ /* next try tftp-hpa args */
+
+ p = argv;
+ *p++ = pb_system_apps.tftp; /* 1 */
+ *p++ = "-m"; /* 2 */
+ *p++ = "binary"; /* 3 */
+ *p++ = url->host; /* 4 */
+ if (url->port)
+ *p++ = url->port; /* 5 */
+ *p++ = "-c"; /* 6 */
+ *p++ = "get"; /* 7 */
+ *p++ = url->path; /* 8 */
+ *p++ = local; /* 9 */
+ *p++ = NULL; /* 10 */
+
+ result = pb_run_cmd(argv, 1, 0);
+
+ if (!result)
+ return local;
+
+ talloc_free(local);
+ return NULL;
+}
+
+enum wget_flags {
+ wget_empty = 0,
+ wget_no_check_certificate = 1,
+};
+
+/**
+ * pb_load_wget - Loads a remote file via wget and returns the local file path.
+ *
+ * Returns the local file path in a talloc'ed character string on success,
+ * or NULL on error.
+ */
+
+static char *load_wget(void *ctx, struct pb_url *url, enum wget_flags flags)
+{
+ int result;
+ const char *argv[7];
+ const char **p;
+ char *local;
+
+ local = local_name(ctx);
+
+ if (!local)
+ return NULL;
+
+ p = argv;
+ *p++ = pb_system_apps.wget; /* 1 */
+#if !defined(DEBUG)
+ *p++ = "--quiet"; /* 2 */
+#endif
+ *p++ = "-O"; /* 3 */
+ *p++ = local; /* 4 */
+ *p++ = url->full; /* 5 */
+ if (flags & wget_no_check_certificate)
+ *p++ = "--no-check-certificate"; /* 6 */
+ *p++ = NULL; /* 7 */
+
+ result = pb_run_cmd(argv, 1, 0);
+
+ if (result)
+ goto fail;
+
+ return local;
+
+fail:
+ talloc_free(local);
+ return NULL;
+}
+
+/**
+ * pb_load_file - Loads a (possibly) remote file and returns the local file
+ * path.
+ * @ctx: The talloc context to associate with the returned string.
+ * @remote: The remote file URL.
+ * @tempfile: An optional variable pointer to be set when a temporary local
+ * file is created.
+ *
+ * Returns the local file path in a talloc'ed character string on success,
+ * or NULL on error.
+ */
+
+char *load_file(void *ctx, const char *remote, unsigned int *tempfile)
+{
+ struct pb_url *url = pb_url_parse(ctx, remote);
+ char *local;
+ int tmp = 0;
+
+ if (!url)
+ return NULL;
+
+ switch (url->scheme) {
+ case pb_url_ftp:
+ case pb_url_http:
+ local = load_wget(ctx, url, 0);
+ tmp = !!local;
+ break;
+ case pb_url_https:
+ local = load_wget(ctx, url, wget_no_check_certificate);
+ tmp = !!local;
+ break;
+ case pb_url_nfs:
+ local = load_nfs(ctx, url);
+ tmp = !!local;
+ break;
+ case pb_url_sftp:
+ local = load_sftp(ctx, url);
+ tmp = !!local;
+ break;
+ case pb_url_tftp:
+ local = load_tftp(ctx, url);
+ tmp = !!local;
+ break;
+ default:
+ local = talloc_strdup(ctx, url->full);
+ tmp = 0;
+ break;
+ }
+
+ if (tempfile)
+ *tempfile = tmp;
+
+ talloc_free(url);
+ return local;
+}
diff --git a/discover/paths.h b/discover/paths.h
index e7c23e5..34de79a 100644
--- a/discover/paths.h
+++ b/discover/paths.h
@@ -51,4 +51,7 @@ char *encode_label(void *alloc_ctx, const char *label);
*/
const char *mount_base(void);
+/* Load a (potentially remote) file, and return a guaranteed-local name */
+char *load_file(void *ctx, const char *remote, unsigned int *tempfile);
+
#endif /* PATHS_H */
OpenPOWER on IntegriCloud