grub/util/raid.c
okuji 2b00217369 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.
2006-10-14 15:24:53 +00:00

112 lines
3 KiB
C

/* 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__ */