2006-10-14 Yoshinori K. Okuji <okuji@enbug.org>

* DISTLIST: Added commands/echo.c, disk/lvm.c, disk/raid.c,
        include/grub/bitmap.h, include/grub/lvm.h, include/grub/raid.h,
        include/grub/i386/pc/vbeutil.h, include/grub/util/lvm.h,
        include/grub/util/raid.h, util/lvm.c, util/raid.c, video/bitmap.c,
        video/readers/tga.c and video/i386/pc/vbeutil.c.

2006-10-14  Jeroen Dekkers  <jeroen@dekkers.cx>

        Added support for RAID and LVM.

        * disk/lvm.c: New file.
        * disk/raid.c: Likewise.
        * include/grub/lvm.h: Likewise.
        * include/grub/raid.h: Likewise.
        * include/grub/util/lvm.h: Likewise.
        * include/grub/util/raid.h: Likewise.
        * util/lvm.c: Likewise.
        * util/raid.c: Likewise.

        * include/grub/disk.h (grub_disk_dev_id): Add
        GRUB_DISK_DEVICE_RAID_ID and GRUB_DISK_DEVICE_LVM_ID.
        (grub_disk_get_size): New prototype.
        * kern/disk.c (grub_disk_open): Check whether grub_partition_probe()
        returns a partition.
        (grub_disk_get_size): New function.

        * kern/i386/pc/init.c (make_install_device): Copy the prefix
        verbatim if grub_install_dos_part is -2.

        * util/i386/pc/getroot.c (grub_guess_root_device): Support RAID
        and LVM devices.

        * util/i386/pc/grub-setup.c (setup): New argument
        MUST_EMBED. Force embedding of GRUB when the argument is
        true. Close FILE before returning.
        (main): Add support for RAID and LVM.

        * conf/common.rmk: Add RAID and LVM modules.
        * conf/i386-pc.rmk (grub_setup_SOURCES): Add util/raid.c and
        util/lvm.c.
        (grub_emu_SOURCES): Add disk/raid.c and disk/lvm.c.

        * kern/misc.c (grub_strstr): New function.
        * include/grub/misc.h (grub_strstr): New prototype.
This commit is contained in:
okuji 2006-10-14 15:24:53 +00:00
parent 050548d018
commit 2b00217369
23 changed files with 2347 additions and 100 deletions

279
util/genmoddep.c Normal file
View file

@ -0,0 +1,279 @@
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2002 Free Software Foundation, Inc.
*
* 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; either version 2 of the License, or
* (at your option) any later version.
*
* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#define BUF_SIZE 1024
#define SYMTAB_SIZE 509
struct symbol
{
const char *name;
const char *mod;
struct symbol *next;
};
struct module
{
const char *name;
struct module *next;
};
static char buf[BUF_SIZE];
static struct symbol *symtab[SYMTAB_SIZE];
static void
err (const char *fmt, ...)
{
va_list ap;
fprintf (stderr, "genmoddep: error: ");
va_start (ap, fmt);
vfprintf (stderr, fmt, ap);
va_end (ap);
fputc ('\n', stderr);
exit (1);
}
static void *
xmalloc (size_t size)
{
void *p;
p = malloc (size);
if (! p)
err ("out of memory");
return p;
}
static char *
xstrdup (const char *str)
{
char *s;
size_t len;
len = strlen (str);
s = (char *) xmalloc (len + 1);
memcpy (s, str, len + 1);
return s;
}
static void
chomp (char *str)
{
int end;
end = strlen (str) - 1;
if (end < 0)
err ("empty string");
if (str[end] == '\n')
str[end] = '\0';
}
static unsigned
symbol_hash (const char *s)
{
unsigned key = 0;
while (*s)
key = key * 65599 + *s++;
return (key + (key >> 5)) % SYMTAB_SIZE;
}
static struct symbol *
get_symbol (const char *name)
{
unsigned k;
struct symbol *sym;
k = symbol_hash (name);
for (sym = symtab[k]; sym; sym = sym->next)
if (strcmp (sym->name, name) == 0)
return sym;
return 0;
}
static void
add_symbol (const char *name, const char *mod)
{
unsigned k;
struct symbol *sym;
if (get_symbol (name))
err ("duplicated symbol: %s", name);
sym = (struct symbol *) xmalloc (sizeof (*sym));
sym->name = xstrdup (name);
sym->mod = xstrdup (mod);
k = symbol_hash (name);
sym->next = symtab[k];
symtab[k] = sym;
}
static void
free_symbols (void)
{
int i;
for (i = 0; i < SYMTAB_SIZE; i++)
{
struct symbol *p, *q;
p = symtab[i];
while (p)
{
q = p->next;
free ((void *) p->name);
free ((void *) p->mod);
free (p);
p = q;
}
}
}
static void
read_defined_symbols (FILE *fp)
{
while (fgets (buf, sizeof (buf), fp))
{
char *p;
if (! *buf)
err ("empty symbol name: %s", buf);
p = strchr (buf, ' ');
if (! p)
err ("invalid line format: %s", buf);
p++;
if (! *p)
err ("empty module name: %s", buf);
*(p - 1) = '\0';
chomp (p);
add_symbol (buf, p);
}
}
static void
add_module (struct module **head, const char *name)
{
struct module *mod;
for (mod = *head; mod; mod = mod->next)
if (strcmp (mod->name, name) == 0)
return;
mod = (struct module *) xmalloc (sizeof (*mod));
mod->name = xstrdup (name);
mod->next = *head;
*head = mod;
}
static void
free_modules (struct module *head)
{
struct module *next;
while (head)
{
next = head->next;
free ((void *) head->name);
free (head);
head = next;
}
}
static void
find_dependencies (FILE *fp)
{
char *mod_name;
struct module *mod_list = 0;
struct module *mod;
if (! fgets (buf, sizeof (buf), fp) || buf[0] == '\n' || buf[0] == '\0')
err ("no module name");
chomp (buf);
mod_name = xstrdup (buf);
while (fgets (buf, sizeof (buf), fp))
{
struct symbol *sym;
chomp (buf);
sym = get_symbol (buf);
if (! sym)
err ("%s in %s is not defined", buf, mod_name);
add_module (&mod_list, sym->mod);
}
printf ("%s:", mod_name);
for (mod = mod_list; mod; mod = mod->next)
if (strcmp (mod->name, "kernel") != 0)
printf (" %s", mod->name);
putchar ('\n');
free_modules (mod_list);
}
int
main (int argc, char *argv[])
{
int i;
/* First, get defined symbols. */
read_defined_symbols (stdin);
/* Second, find the dependecies. */
for (i = 1; i < argc; i++)
{
FILE *fp;
fp = fopen (argv[i], "r");
if (! fp)
err ("cannot open %s", argv[i]);
find_dependencies (fp);
fclose (fp);
}
/* Last, free memory. */
free_symbols ();
return 0;
}

View file

@ -1,7 +1,7 @@
/* getroot.c - Get root device */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 1999,2000,2001,2002,2003 Free Software Foundation, Inc.
* Copyright (C) 1999,2000,2001,2002,2003,2006 Free Software Foundation, Inc.
*
* GRUB is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -223,5 +223,27 @@ grub_guess_root_device (const char *dir)
if (! os_dev)
return 0;
#ifdef __linux__
/* Check for LVM. */
if (!strncmp (os_dev, "/dev/mapper/", 12))
{
char *grub_dev = xmalloc (strlen (os_dev) - 12);
strcpy (grub_dev, os_dev+12);
return grub_dev;
}
if (!strncmp (os_dev, "/dev/md", 7) || !strncmp (os_dev, "/dev/.static/dev/md", 19))
{
char *p, *grub_dev = xmalloc (8);
p = strchr (os_dev, 'm');
strncpy (grub_dev, p, 8);
return grub_dev;
}
#endif
return grub_util_biosdisk_get_grub_dev (os_dev);
}

View file

@ -32,6 +32,8 @@
#include <grub/machine/boot.h>
#include <grub/machine/kernel.h>
#include <grub/term.h>
#include <grub/util/raid.h>
#include <grub/util/lvm.h>
#include <stdio.h>
#include <unistd.h>
@ -91,7 +93,7 @@ grub_refresh (void)
static void
setup (const char *prefix, const char *dir,
const char *boot_file, const char *core_file,
const char *root, const char *dest)
const char *root, const char *dest, int must_embed)
{
char *boot_path, *core_path;
char *boot_img, *core_img;
@ -218,9 +220,12 @@ setup (const char *prefix, const char *dir,
+ GRUB_KERNEL_MACHINE_PREFIX);
/* Open the root device and the destination device. */
root_dev = grub_device_open (root);
if (! root_dev)
grub_util_error ("%s", grub_errmsg);
if (!must_embed)
{
root_dev = grub_device_open (root);
if (! root_dev)
grub_util_error ("%s", grub_errmsg);
}
dest_dev = grub_device_open (dest);
if (! dest_dev)
@ -280,7 +285,9 @@ setup (const char *prefix, const char *dir,
block->segment = 0;
/* Embed information about the installed location. */
if (root_dev->disk->partition)
if (must_embed)
*install_dos_part = *install_bsd_part = grub_cpu_to_le32 (-2);
else if (root_dev->disk->partition)
{
struct grub_pc_partition *pcdata =
root_dev->disk->partition->data;
@ -316,6 +323,9 @@ setup (const char *prefix, const char *dir,
goto finish;
}
}
else if (must_embed)
grub_util_error ("Can't embed the core image, but this is required when\n"
"the root device is on a RAID array or LVM volume.");
/* The core image must be put on a filesystem unfortunately. */
grub_util_info ("will leave the core image on the filesystem");
@ -422,6 +432,8 @@ setup (const char *prefix, const char *dir,
!= (grub_ssize_t) core_size - GRUB_DISK_SECTOR_SIZE)
grub_util_error ("Failed to read the rest sectors of the core image");
grub_file_close (file);
free (core_path);
free (tmp_img);
@ -481,7 +493,8 @@ setup (const char *prefix, const char *dir,
free (core_img);
free (boot_img);
grub_device_close (dest_dev);
grub_device_close (root_dev);
if (!must_embed)
grub_device_close (root_dev);
}
static struct option options[] =
@ -548,6 +561,7 @@ main (int argc, char *argv[])
char *root_dev = 0;
char *prefix;
char *dest_dev;
int must_embed = 0;
progname = "grub-setup";
@ -678,12 +692,50 @@ main (int argc, char *argv[])
}
}
#ifdef __linux__
if (grub_util_lvm_isvolume (root_dev))
{
char *newprefix;
must_embed = 1;
newprefix = xmalloc (1 + strlen (root_dev) + 1 + strlen (prefix) + 1);
sprintf (newprefix, "(%s)%s", root_dev, prefix);
free (prefix);
prefix = newprefix;
}
if (dest_dev[0] == 'm' && dest_dev[1] == 'd'
&& dest_dev[2] >= '0' && dest_dev[2] <= '9')
{
char **devicelist;
char *raid_prefix;
int i;
raid_prefix = xmalloc (1 + strlen (dest_dev) + 1 + strlen (prefix) + 1);
sprintf (raid_prefix, "(%s)%s", dest_dev, prefix);
devicelist = grub_util_raid_getmembers (dest_dev);
for (i = 0; devicelist[i]; i++)
{
setup (raid_prefix,
dir ? : DEFAULT_DIRECTORY,
boot_file ? : DEFAULT_BOOT_FILE,
core_file ? : DEFAULT_CORE_FILE,
root_dev, grub_util_biosdisk_get_grub_dev (devicelist[i]), 1);
}
free (raid_prefix);
}
else
#endif
/* Do the real work. */
setup (prefix,
dir ? : DEFAULT_DIRECTORY,
boot_file ? : DEFAULT_BOOT_FILE,
core_file ? : DEFAULT_CORE_FILE,
root_dev, dest_dev);
setup (prefix,
dir ? : DEFAULT_DIRECTORY,
boot_file ? : DEFAULT_BOOT_FILE,
core_file ? : DEFAULT_CORE_FILE,
root_dev, dest_dev, must_embed);
/* Free resources. */
grub_ext2_fini ();

50
util/lvm.c Normal file
View file

@ -0,0 +1,50 @@
/* lvm.c - LVM support for GRUB utils. */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2006 Free Software Foundation, Inc.
*
* GRUB 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; either version 2 of the License, or
* (at your option) any later version.
*
* 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 GRUB; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/* We only support LVM on Linux. */
#ifdef __linux__
#include <grub/util/misc.h>
#include <string.h>
#include <sys/stat.h>
int
grub_util_lvm_isvolume (char *name)
{
char *devname;
struct stat st;
int err;
devname = xmalloc (strlen (name) + 13);
strcpy (devname, "/dev/mapper/");
strcpy (devname+12, name);
err = stat (devname, &st);
free (devname);
if (err)
return 0;
else
return 1;
}
#endif /* ! __linux__ */

112
util/raid.c Normal file
View file

@ -0,0 +1,112 @@
/* raid.c - RAID support for GRUB utils. */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2006 Free Software Foundation, Inc.
*
* GRUB 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; either version 2 of the License, or
* (at your option) any later version.
*
* 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 GRUB; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/* We only support RAID on Linux. */
#ifdef __linux__
#include <grub/util/misc.h>
#include <string.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <linux/types.h>
#include <linux/major.h>
#include <linux/raid/md_p.h>
#include <linux/raid/md_u.h>
char *
grub_util_getdiskname (int major, int minor)
{
char *name = xmalloc (15);
if (major == LOOP_MAJOR)
sprintf (name, "/dev/loop%d", minor);
else if (major == IDE0_MAJOR)
sprintf (name, "/dev/hd%c", 'a' + minor / 64);
else if (major == IDE1_MAJOR)
sprintf (name, "/dev/hd%c", 'c' + minor / 64);
else if (major == IDE2_MAJOR)
sprintf (name, "/dev/hd%c", 'e' + minor / 64);
else if (major == IDE3_MAJOR)
sprintf (name, "/dev/hd%c", 'g' + minor / 64);
else if (major == SCSI_DISK0_MAJOR)
sprintf (name, "/dev/sd%c", 'a' + minor / 16);
else
grub_util_error ("Unknown device number: %d, %d", major, minor);
return name;
}
char **
grub_util_raid_getmembers (char *name)
{
int fd, ret, i, j;
char *devname;
char **devicelist;
mdu_version_t version;
mdu_array_info_t info;
mdu_disk_info_t disk;
devname = xmalloc (strlen (name) + 6);
strcpy (devname, "/dev/");
strcpy (devname+5, name);
fd = open (devname, O_RDONLY);
if (fd == -1)
grub_util_error ("Can't open %s: %s", devname, strerror (errno));
free (devname);
ret = ioctl (fd, RAID_VERSION, &version);
if (ret != 0)
grub_util_error ("ioctl RAID_VERSION error: %s", strerror (errno));
if (version.major != 0 || version.minor != 90)
grub_util_error ("Unsupported RAID version: %d.%d",
version.major, version.minor);
ret = ioctl (fd, GET_ARRAY_INFO, &info);
if (ret != 0)
grub_util_error ("ioctl GET_ARRAY_INFO error: %s", strerror (errno));
devicelist = xmalloc ((info.nr_disks + 1) * sizeof (char *));
for (i = 0, j = 0; i <info.nr_disks; i++)
{
disk.number = i;
ret = ioctl (fd, GET_DISK_INFO, &disk);
if (ret != 0)
grub_util_error ("ioctl GET_DISK_INFO error: %s", strerror (errno));
if (disk.state & (1 << MD_DISK_ACTIVE))
{
devicelist[j] = grub_util_getdiskname (disk.major, disk.minor);
j++;
}
}
devicelist[j] = NULL;
return devicelist;
}
#endif /* ! __linux__ */