From 70ad7ba01b43404585c76512bffe99ede0c0f55a Mon Sep 17 00:00:00 2001 From: Jeremy Kerr Date: Wed, 8 May 2013 16:46:26 +0800 Subject: [PATCH] discover/grub2: handle search commands to specify root filesystems Add a resource type for grub, allowing us to parse search parameters. Signed-off-by: Jeremy Kerr --- discover/grub2-parser.c | 95 ++++++++++++++++++++++++++++-- test/parser/test-grub2-f18-ppc64.c | 27 +++++++-- 2 files changed, 113 insertions(+), 9 deletions(-) diff --git a/discover/grub2-parser.c b/discover/grub2-parser.c index cb9635e..a2308ee 100644 --- a/discover/grub2-parser.c +++ b/discover/grub2-parser.c @@ -30,15 +30,70 @@ #include "types/types.h" #include "parser-conf.h" #include "parser-utils.h" +#include "paths.h" #include "resource.h" +struct grub2_root { + char *uuid; +}; + struct grub2_state { struct discover_boot_option *opt; char *desc_image; char *desc_initrd; const char *const *known_names; + struct grub2_root *root; +}; + +struct grub2_resource_info { + struct grub2_root *root; + char *path; }; +/* we use slightly different resources for grub2 */ +static struct resource *create_grub2_resource(void *ctx, + struct discover_device *orig_device, + struct grub2_root *root, const char *path) +{ + struct grub2_resource_info *info; + struct resource *res; + + res = talloc(ctx, struct resource); + + if (root) { + info = talloc(res, struct grub2_resource_info); + info->root = root; + talloc_reference(info, root); + info->path = talloc_strdup(info, path); + + res->resolved = false; + res->info = info; + + } else + resolve_resource_against_device(res, orig_device, path); + + return res; +} + +static bool resolve_grub2_resource(struct device_handler *handler, + struct resource *res) +{ + struct grub2_resource_info *info = res->info; + struct discover_device *dev; + + assert(!res->resolved); + + dev = device_lookup_by_uuid(handler, info->root->uuid); + + if (!dev) + return false; + + resolve_resource_against_device(res, dev, info->path); + talloc_free(info); + + return true; +} + static void grub2_finish(struct conf_context *conf) { struct device *dev = conf->dc->device->device; @@ -119,8 +174,8 @@ static void grub2_process_pair(struct conf_context *conf, const char *name, if (sep) *sep = 0; - opt->boot_image = create_devpath_resource(opt, - conf->dc->device, value); + opt->boot_image = create_grub2_resource(opt, conf->dc->device, + state->root, value); state->desc_image = talloc_strdup(opt, value); if (sep) @@ -130,13 +185,42 @@ static void grub2_process_pair(struct conf_context *conf, const char *name, } if (streq(name, "initrd")) { - opt->initrd = create_devpath_resource(opt, - conf->dc->device, value); + opt->initrd = create_grub2_resource(opt, conf->dc->device, + state->root, value); state->desc_initrd = talloc_asprintf(state, "initrd=%s", value); return; } + if (streq(name, "search")) { + struct grub2_root *root; + char *uuid; + + if (!strstr(value, "--set=root")) { + pb_log("%s: no root\n", __func__); + return; + } + + /* The UUID should be the last argument to the search command. + * FIXME: this is a little fragile; would be nice to have some + * parser helpers to deal with "command args" parsing + */ + uuid = strrchr(value, ' '); + if (!uuid) + return; + + uuid++; + pb_log("%s: uuid %s\n", __func__, uuid); + + if (state->root) + talloc_unlink(state, state->root); + + root = talloc(state, struct grub2_root); + root->uuid = talloc_strdup(root, uuid); + state->root = root; + return; + } + pb_log("%s: unknown name: %s\n", __func__, name); } @@ -162,6 +246,7 @@ static const char *grub2_known_names[] = { "menuentry", "linux", "initrd", + "search", NULL }; @@ -195,7 +280,7 @@ static struct parser grub2_parser = { .method = CONF_METHOD_LOCAL_FILE, .parse = grub2_parse, .filenames = grub2_conf_files, - .resolve_resource = resolve_devpath_resource, + .resolve_resource = resolve_grub2_resource, }; register_parser(grub2_parser); diff --git a/test/parser/test-grub2-f18-ppc64.c b/test/parser/test-grub2-f18-ppc64.c index 4d7c8ec..94eb6a4 100644 --- a/test/parser/test-grub2-f18-ppc64.c +++ b/test/parser/test-grub2-f18-ppc64.c @@ -5,6 +5,7 @@ void run_test(struct parser_test *test) { struct discover_boot_option *opt; struct discover_context *ctx; + struct discover_device *dev; int i; test_read_conf_file(test, "grub2-f18-ppc64.conf"); @@ -17,10 +18,8 @@ void run_test(struct parser_test *test) for (i = 0; i < 2; i++) { opt = get_boot_option(ctx, i); - check_resolved_local_resource(opt->boot_image, ctx->device, - "/vmlinuz-3.6.10-4.fc18.ppc64p7"); - check_resolved_local_resource(opt->initrd, ctx->device, - "/initramfs-3.6.10-4.fc18.ppc64p7.img"); + check_unresolved_resource(opt->boot_image); + check_unresolved_resource(opt->initrd); check_args(opt, "root=/dev/mapper/fedora_ltcfbl8eb-root ro " "rd.lvm.lv=fedora_ltcfbl8eb/swap rd.dm=0 " @@ -30,5 +29,25 @@ void run_test(struct parser_test *test) check_name(opt, i == 0 ? "Fedora" : "Fedora, with Linux 3.6.10-4.fc18.ppc64p7"); + if (i == 0) + check_name(opt, "Fedora"); + else + check_name(opt, "Fedora, " + "with Linux 3.6.10-4.fc18.ppc64p7"); + } + + /* hotplug a device with a maching UUID, and check that our + * resources become resolved */ + dev = test_create_device(ctx, "external"); + dev->uuid = "773653a7-660e-490e-9a74-d9fdfc9bbbf6"; + test_hotplug_device(test, dev); + + for (i = 0; i < 2; i++) { + opt = get_boot_option(ctx, i); + + check_resolved_local_resource(opt->boot_image, dev, + "/vmlinuz-3.6.10-4.fc18.ppc64p7"); + check_resolved_local_resource(opt->initrd, dev, + "/initramfs-3.6.10-4.fc18.ppc64p7.img"); } } -- 2.39.2