Add grub2 conf file parser
authorGeoff Levand <geoff@infradead.org>
Mon, 19 Mar 2012 05:58:29 +0000 (22:58 -0700)
committerGeoff Levand <geoff@infradead.org>
Mon, 19 Mar 2012 05:58:29 +0000 (22:58 -0700)
Add grub2 parser and sample config file.
Reorder parser priorities:
  From (yaboot -> kboot) to (kboot -> grub2 -> yaboot).

Signed-off-by: Geoff Levand <geoff@infradead.org>
discover/Makefile.am
discover/grub2-parser.c [new file with mode: 0644]
discover/parser.c
test/parser/Makefile.am
test/parser/data/grub-01/grub.cfg [new file with mode: 0644]

index 5ddba1fd74823520e2511165bcbacfa08a44061a..829270aa489d1a4d48085ae35dd757e429d47f04 100644 (file)
@@ -31,6 +31,7 @@ libparser_la_SOURCES = \
        paths.c \
        paths.h \
        kboot-parser.c \
+       grub2-parser.c \
        yaboot-parser.c
 
 EXTRA_DIST = native-parser.c
diff --git a/discover/grub2-parser.c b/discover/grub2-parser.c
new file mode 100644 (file)
index 0000000..689c200
--- /dev/null
@@ -0,0 +1,185 @@
+/*
+ *  Copyright Geoff Levand <geoff@infradead.org>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; version 2 of the License.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#define _GNU_SOURCE
+
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "log/log.h"
+#include "talloc/talloc.h"
+#include "pb-protocol/pb-protocol.h"
+#include "parser-conf.h"
+#include "parser-utils.h"
+#include "paths.h"
+
+struct grub2_state {
+       struct boot_option *opt;
+       char *desc_image;
+       char *desc_initrd;
+       const char *const *known_names;
+};
+
+static void grub2_finish(struct conf_context *conf)
+{
+       struct grub2_state *state = conf->parser_info;
+
+       if (!state->desc_image) {
+               pb_log("%s: %s: no image found\n", __func__,
+                       conf->dc->device->id);
+               return;
+       }
+
+       assert(state->opt);
+       assert(state->opt->name);
+       assert(state->opt->boot_args);
+
+       state->opt->description = talloc_asprintf(state->opt, "%s %s %s",
+               state->desc_image,
+               (state->desc_initrd ? state->desc_initrd : ""),
+               state->opt->boot_args);
+
+       talloc_free(state->desc_initrd);
+       state->desc_initrd = NULL;
+
+       conf_strip_str(state->opt->boot_args);
+       conf_strip_str(state->opt->description);
+
+       /* opt is persistent, so must be associated with device */
+
+       device_add_boot_option(conf->dc->device, state->opt);
+       state->opt = talloc_zero(conf->dc->device, struct boot_option);
+       state->opt->boot_args = talloc_strdup(state->opt, "");
+
+       talloc_free(state->desc_image);
+       state->desc_image = NULL;
+}
+
+static void grub2_process_pair(struct conf_context *conf, const char *name,
+               char *value)
+{
+       struct grub2_state *state = conf->parser_info;
+
+       if (!name || !conf_param_in_list(state->known_names, name))
+               return;
+
+       if (streq(name, "menuentry")) {
+               char *sep;
+
+               grub2_finish(conf);
+
+               /* Then start the new image. */
+
+               sep = strchr(value, '\'');
+
+               if (sep)
+                       *sep = 0;
+
+               state->opt->id = talloc_asprintf(state->opt, "%s#%s",
+                       conf->dc->device->id, value);
+               state->opt->name = talloc_strdup(state->opt, value);
+
+               return;
+       }
+
+       if (streq(name, "linux")) {
+               char *sep;
+
+               sep = strchr(value, ' ');
+
+               if (sep)
+                       *sep = 0;
+
+               state->opt->boot_image_file = resolve_path(state->opt,
+                       value, conf->dc->device_path);
+               state->desc_image = talloc_strdup(state->opt, value);
+
+               if (sep)
+                       state->opt->boot_args = talloc_strdup(state->opt,
+                               sep + 1);
+
+               return;
+       }
+
+       if (streq(name, "initrd")) {
+               state->opt->initrd_file = resolve_path(state->opt,
+                       value, conf->dc->device_path);
+               state->desc_initrd = talloc_asprintf(state, "initrd=%s",
+                       value);
+               return;
+       }
+
+       pb_log("%s: unknown name: %s\n", __func__, name);
+}
+
+static const char *const grub2_conf_files[] = {
+       "/grub.cfg",
+       "/menu.lst",
+       "/boot/grub/grub.cfg",
+       "/boot/grub/menu.lst",
+       "/GRUB.CFG",
+       "/MENU.LST",
+       "/BOOT/GRUB/GRUB.CFG",
+       "/BOOT/GRUB/MENU.LST",
+       NULL
+};
+
+static const char *grub2_known_names[] = {
+       "menuentry",
+       "linux",
+       "initrd",
+       NULL
+};
+
+static int grub2_parse(struct discover_context *dc)
+{
+       struct conf_context *conf;
+       struct grub2_state *state;
+       int rc;
+
+       conf = talloc_zero(dc, struct conf_context);
+
+       if (!conf)
+               return 0;
+
+       conf->dc = dc;
+       conf_init_global_options(conf);
+       conf->conf_files = grub2_conf_files,
+       conf->get_pair = conf_get_pair_space;
+       conf->process_pair = grub2_process_pair;
+       conf->finish = grub2_finish;
+       conf->parser_info = state = talloc_zero(conf, struct grub2_state);
+
+       state->known_names = grub2_known_names;
+
+       /* opt is persistent, so must be associated with device */
+
+       state->opt = talloc_zero(conf->dc->device, struct boot_option);
+       state->opt->boot_args = talloc_strdup(state->opt, "");
+
+       rc = conf_parse(conf);
+
+       talloc_free(conf);
+       return rc;
+}
+
+define_parser(grub2, grub2_parse);
index c106dec0ae20c0a6210d317060cbe125f9656450..beaacccbdb1973e3ccb1ba0b3a1e570f4c87cc61 100644 (file)
@@ -8,15 +8,16 @@
 #include "parser.h"
 #include "parser-utils.h"
 
+struct parser __grub2_parser;
+struct parser __kboot_parser;
 struct parser __native_parser;
 struct parser __yaboot_parser;
-struct parser __kboot_parser;
-struct parser __grub2_parser;
 
 static const struct parser *const parsers[] = {
 //     &__native_parser,
-       &__yaboot_parser,
        &__kboot_parser,
+       &__grub2_parser,
+       &__yaboot_parser,
        NULL
 };
 
index 107e8f153ff7daf7b21b023ca442aa6a38b97fd1..e863773c4f8f0cb79c3166d2ed436fc715481cae 100644 (file)
@@ -31,6 +31,7 @@ parser_test_SOURCES = parser-test.c
 parser_test_LDADD = $(common_libs)
 
 noinst_DATA = \
+       data/grub-01/grub.cfg \
        data/kboot-01/expected-output \
        data/kboot-01/etc/kboot.conf \
        data/kboot-02/expected-output \
diff --git a/test/parser/data/grub-01/grub.cfg b/test/parser/data/grub-01/grub.cfg
new file mode 100644 (file)
index 0000000..57d6a8b
--- /dev/null
@@ -0,0 +1,300 @@
+#
+# DO NOT EDIT THIS FILE
+#
+# It is automatically generated by grub-mkconfig using templates
+# from /etc/grub.d and settings from /etc/default/grub
+#
+
+### BEGIN /etc/grub.d/00_header ###
+if [ -s $prefix/grubenv ]; then
+  set have_grubenv=true
+  load_env
+fi
+set default="0"
+if [ "${prev_saved_entry}" ]; then
+  set saved_entry="${prev_saved_entry}"
+  save_env saved_entry
+  set prev_saved_entry=
+  save_env prev_saved_entry
+  set boot_once=true
+fi
+
+function savedefault {
+  if [ -z "${boot_once}" ]; then
+    saved_entry="${chosen}"
+    save_env saved_entry
+  fi
+}
+
+function recordfail {
+  set recordfail=1
+  if [ -n "${have_grubenv}" ]; then if [ -z "${boot_once}" ]; then save_env recordfail; fi; fi
+}
+
+function load_video {
+  insmod vbe
+  insmod vga
+  insmod video_bochs
+  insmod video_cirrus
+}
+
+terminal_input console
+terminal_output console
+if [ "${recordfail}" = 1 ]; then
+  set timeout=-1
+else
+  set timeout=30
+fi
+play 480 440 1
+### END /etc/grub.d/00_header ###
+
+### BEGIN /etc/grub.d/05_debian_theme ###
+set menu_color_normal=white/black
+set menu_color_highlight=black/light-gray
+if background_color 0,71,115; then
+  clear
+fi
+### END /etc/grub.d/05_debian_theme ###
+
+### BEGIN /etc/grub.d/10_linux ###
+if [ ${recordfail} != 1 ]; then
+  if [ -e ${prefix}/gfxblacklist.txt ]; then
+    if hwmatch ${prefix}/gfxblacklist.txt 3; then
+      if [ ${match} = 0 ]; then
+        set linux_gfx_mode=keep
+      else
+        set linux_gfx_mode=text
+      fi
+    else
+      set linux_gfx_mode=text
+    fi
+  else
+    set linux_gfx_mode=keep
+  fi
+else
+  set linux_gfx_mode=text
+fi
+export linux_gfx_mode
+if [ "$linux_gfx_mode" != "text" ]; then load_video; fi
+menuentry 'Ubuntu, with Linux 3.2.0-18-generic' --class ubuntu --class gnu-linux --class gnu --class os {
+       recordfail
+       set gfxpayload=$linux_gfx_mode
+       insmod gzio
+       insmod part_gpt
+       insmod ext2
+       set root='(hd0,gpt2)'
+       search --no-floppy --fs-uuid --set=root 2ceaeb4b-f75d-431c-bcde-210f83d7c1e1
+       linux   /boot/vmlinuz-3.2.0-18-generic root=UUID=2ceaeb4b-f75d-431c-bcde-210f83d7c1e1 ro   
+       initrd  /boot/initrd.img-3.2.0-18-generic
+}
+menuentry 'Ubuntu, with Linux 3.2.0-18-generic (recovery mode)' --class ubuntu --class gnu-linux --class gnu --class os {
+       recordfail
+       insmod gzio
+       insmod part_gpt
+       insmod ext2
+       set root='(hd0,gpt2)'
+       search --no-floppy --fs-uuid --set=root 2ceaeb4b-f75d-431c-bcde-210f83d7c1e1
+       echo    'Loading Linux 3.2.0-18-generic ...'
+       linux   /boot/vmlinuz-3.2.0-18-generic root=UUID=2ceaeb4b-f75d-431c-bcde-210f83d7c1e1 ro recovery nomodeset 
+       echo    'Loading initial ramdisk ...'
+       initrd  /boot/initrd.img-3.2.0-18-generic
+}
+submenu "Previous Linux versions" {
+menuentry 'Ubuntu, with Linux 3.2.0-17-generic' --class ubuntu --class gnu-linux --class gnu --class os {
+       recordfail
+       set gfxpayload=$linux_gfx_mode
+       insmod gzio
+       insmod part_gpt
+       insmod ext2
+       set root='(hd0,gpt2)'
+       search --no-floppy --fs-uuid --set=root 2ceaeb4b-f75d-431c-bcde-210f83d7c1e1
+       linux   /boot/vmlinuz-3.2.0-17-generic root=UUID=2ceaeb4b-f75d-431c-bcde-210f83d7c1e1 ro   
+       initrd  /boot/initrd.img-3.2.0-17-generic
+}
+menuentry 'Ubuntu, with Linux 3.2.0-17-generic (recovery mode)' --class ubuntu --class gnu-linux --class gnu --class os {
+       recordfail
+       insmod gzio
+       insmod part_gpt
+       insmod ext2
+       set root='(hd0,gpt2)'
+       search --no-floppy --fs-uuid --set=root 2ceaeb4b-f75d-431c-bcde-210f83d7c1e1
+       echo    'Loading Linux 3.2.0-17-generic ...'
+       linux   /boot/vmlinuz-3.2.0-17-generic root=UUID=2ceaeb4b-f75d-431c-bcde-210f83d7c1e1 ro recovery nomodeset 
+       echo    'Loading initial ramdisk ...'
+       initrd  /boot/initrd.img-3.2.0-17-generic
+}
+menuentry 'Ubuntu, with Linux 3.2.0-16-generic' --class ubuntu --class gnu-linux --class gnu --class os {
+       recordfail
+       set gfxpayload=$linux_gfx_mode
+       insmod gzio
+       insmod part_gpt
+       insmod ext2
+       set root='(hd0,gpt2)'
+       search --no-floppy --fs-uuid --set=root 2ceaeb4b-f75d-431c-bcde-210f83d7c1e1
+       linux   /boot/vmlinuz-3.2.0-16-generic root=UUID=2ceaeb4b-f75d-431c-bcde-210f83d7c1e1 ro   
+       initrd  /boot/initrd.img-3.2.0-16-generic
+}
+menuentry 'Ubuntu, with Linux 3.2.0-16-generic (recovery mode)' --class ubuntu --class gnu-linux --class gnu --class os {
+       recordfail
+       insmod gzio
+       insmod part_gpt
+       insmod ext2
+       set root='(hd0,gpt2)'
+       search --no-floppy --fs-uuid --set=root 2ceaeb4b-f75d-431c-bcde-210f83d7c1e1
+       echo    'Loading Linux 3.2.0-16-generic ...'
+       linux   /boot/vmlinuz-3.2.0-16-generic root=UUID=2ceaeb4b-f75d-431c-bcde-210f83d7c1e1 ro recovery nomodeset 
+       echo    'Loading initial ramdisk ...'
+       initrd  /boot/initrd.img-3.2.0-16-generic
+}
+menuentry 'Ubuntu, with Linux 3.2.0-15-generic' --class ubuntu --class gnu-linux --class gnu --class os {
+       recordfail
+       set gfxpayload=$linux_gfx_mode
+       insmod gzio
+       insmod part_gpt
+       insmod ext2
+       set root='(hd0,gpt2)'
+       search --no-floppy --fs-uuid --set=root 2ceaeb4b-f75d-431c-bcde-210f83d7c1e1
+       linux   /boot/vmlinuz-3.2.0-15-generic root=UUID=2ceaeb4b-f75d-431c-bcde-210f83d7c1e1 ro   
+       initrd  /boot/initrd.img-3.2.0-15-generic
+}
+menuentry 'Ubuntu, with Linux 3.2.0-15-generic (recovery mode)' --class ubuntu --class gnu-linux --class gnu --class os {
+       recordfail
+       insmod gzio
+       insmod part_gpt
+       insmod ext2
+       set root='(hd0,gpt2)'
+       search --no-floppy --fs-uuid --set=root 2ceaeb4b-f75d-431c-bcde-210f83d7c1e1
+       echo    'Loading Linux 3.2.0-15-generic ...'
+       linux   /boot/vmlinuz-3.2.0-15-generic root=UUID=2ceaeb4b-f75d-431c-bcde-210f83d7c1e1 ro recovery nomodeset 
+       echo    'Loading initial ramdisk ...'
+       initrd  /boot/initrd.img-3.2.0-15-generic
+}
+menuentry 'Ubuntu, with Linux 3.2.0-12-generic' --class ubuntu --class gnu-linux --class gnu --class os {
+       recordfail
+       set gfxpayload=$linux_gfx_mode
+       insmod gzio
+       insmod part_gpt
+       insmod ext2
+       set root='(hd0,gpt2)'
+       search --no-floppy --fs-uuid --set=root 2ceaeb4b-f75d-431c-bcde-210f83d7c1e1
+       linux   /boot/vmlinuz-3.2.0-12-generic root=UUID=2ceaeb4b-f75d-431c-bcde-210f83d7c1e1 ro   
+       initrd  /boot/initrd.img-3.2.0-12-generic
+}
+menuentry 'Ubuntu, with Linux 3.2.0-12-generic (recovery mode)' --class ubuntu --class gnu-linux --class gnu --class os {
+       recordfail
+       insmod gzio
+       insmod part_gpt
+       insmod ext2
+       set root='(hd0,gpt2)'
+       search --no-floppy --fs-uuid --set=root 2ceaeb4b-f75d-431c-bcde-210f83d7c1e1
+       echo    'Loading Linux 3.2.0-12-generic ...'
+       linux   /boot/vmlinuz-3.2.0-12-generic root=UUID=2ceaeb4b-f75d-431c-bcde-210f83d7c1e1 ro recovery nomodeset 
+       echo    'Loading initial ramdisk ...'
+       initrd  /boot/initrd.img-3.2.0-12-generic
+}
+menuentry 'Ubuntu, with Linux 3.2.0-11-generic' --class ubuntu --class gnu-linux --class gnu --class os {
+       recordfail
+       set gfxpayload=$linux_gfx_mode
+       insmod gzio
+       insmod part_gpt
+       insmod ext2
+       set root='(hd0,gpt2)'
+       search --no-floppy --fs-uuid --set=root 2ceaeb4b-f75d-431c-bcde-210f83d7c1e1
+       linux   /boot/vmlinuz-3.2.0-11-generic root=UUID=2ceaeb4b-f75d-431c-bcde-210f83d7c1e1 ro   
+       initrd  /boot/initrd.img-3.2.0-11-generic
+}
+menuentry 'Ubuntu, with Linux 3.2.0-11-generic (recovery mode)' --class ubuntu --class gnu-linux --class gnu --class os {
+       recordfail
+       insmod gzio
+       insmod part_gpt
+       insmod ext2
+       set root='(hd0,gpt2)'
+       search --no-floppy --fs-uuid --set=root 2ceaeb4b-f75d-431c-bcde-210f83d7c1e1
+       echo    'Loading Linux 3.2.0-11-generic ...'
+       linux   /boot/vmlinuz-3.2.0-11-generic root=UUID=2ceaeb4b-f75d-431c-bcde-210f83d7c1e1 ro recovery nomodeset 
+       echo    'Loading initial ramdisk ...'
+       initrd  /boot/initrd.img-3.2.0-11-generic
+}
+menuentry 'Ubuntu, with Linux 3.2.0-10-generic' --class ubuntu --class gnu-linux --class gnu --class os {
+       recordfail
+       set gfxpayload=$linux_gfx_mode
+       insmod gzio
+       insmod part_gpt
+       insmod ext2
+       set root='(hd0,gpt2)'
+       search --no-floppy --fs-uuid --set=root 2ceaeb4b-f75d-431c-bcde-210f83d7c1e1
+       linux   /boot/vmlinuz-3.2.0-10-generic root=UUID=2ceaeb4b-f75d-431c-bcde-210f83d7c1e1 ro   
+       initrd  /boot/initrd.img-3.2.0-10-generic
+}
+menuentry 'Ubuntu, with Linux 3.2.0-10-generic (recovery mode)' --class ubuntu --class gnu-linux --class gnu --class os {
+       recordfail
+       insmod gzio
+       insmod part_gpt
+       insmod ext2
+       set root='(hd0,gpt2)'
+       search --no-floppy --fs-uuid --set=root 2ceaeb4b-f75d-431c-bcde-210f83d7c1e1
+       echo    'Loading Linux 3.2.0-10-generic ...'
+       linux   /boot/vmlinuz-3.2.0-10-generic root=UUID=2ceaeb4b-f75d-431c-bcde-210f83d7c1e1 ro recovery nomodeset 
+       echo    'Loading initial ramdisk ...'
+       initrd  /boot/initrd.img-3.2.0-10-generic
+}
+menuentry 'Ubuntu, with Linux 3.2.0-9-generic' --class ubuntu --class gnu-linux --class gnu --class os {
+       recordfail
+       set gfxpayload=$linux_gfx_mode
+       insmod gzio
+       insmod part_gpt
+       insmod ext2
+       set root='(hd0,gpt2)'
+       search --no-floppy --fs-uuid --set=root 2ceaeb4b-f75d-431c-bcde-210f83d7c1e1
+       linux   /boot/vmlinuz-3.2.0-9-generic root=UUID=2ceaeb4b-f75d-431c-bcde-210f83d7c1e1 ro   
+       initrd  /boot/initrd.img-3.2.0-9-generic
+}
+menuentry 'Ubuntu, with Linux 3.2.0-9-generic (recovery mode)' --class ubuntu --class gnu-linux --class gnu --class os {
+       recordfail
+       insmod gzio
+       insmod part_gpt
+       insmod ext2
+       set root='(hd0,gpt2)'
+       search --no-floppy --fs-uuid --set=root 2ceaeb4b-f75d-431c-bcde-210f83d7c1e1
+       echo    'Loading Linux 3.2.0-9-generic ...'
+       linux   /boot/vmlinuz-3.2.0-9-generic root=UUID=2ceaeb4b-f75d-431c-bcde-210f83d7c1e1 ro recovery nomodeset 
+       echo    'Loading initial ramdisk ...'
+       initrd  /boot/initrd.img-3.2.0-9-generic
+}
+}
+### END /etc/grub.d/10_linux ###
+
+### BEGIN /etc/grub.d/20_linux_xen ###
+### END /etc/grub.d/20_linux_xen ###
+
+### BEGIN /etc/grub.d/20_memtest86+ ###
+menuentry "Memory test (memtest86+)" {
+       insmod part_gpt
+       insmod ext2
+       set root='(hd0,gpt2)'
+       search --no-floppy --fs-uuid --set=root 2ceaeb4b-f75d-431c-bcde-210f83d7c1e1
+       linux16 /boot/memtest86+.bin
+}
+menuentry "Memory test (memtest86+, serial console 115200)" {
+       insmod part_gpt
+       insmod ext2
+       set root='(hd0,gpt2)'
+       search --no-floppy --fs-uuid --set=root 2ceaeb4b-f75d-431c-bcde-210f83d7c1e1
+       linux16 /boot/memtest86+.bin console=ttyS0,115200n8
+}
+### END /etc/grub.d/20_memtest86+ ###
+
+### BEGIN /etc/grub.d/30_os-prober ###
+### END /etc/grub.d/30_os-prober ###
+
+### BEGIN /etc/grub.d/40_custom ###
+# This file provides an easy way to add custom menu entries.  Simply type the
+# menu entries you want to add after this comment.  Be careful not to change
+# the 'exec tail' line above.
+### END /etc/grub.d/40_custom ###
+
+### BEGIN /etc/grub.d/41_custom ###
+if [ -f  $prefix/custom.cfg ]; then
+  source $prefix/custom.cfg;
+fi
+### END /etc/grub.d/41_custom ###