2009-04-15 Felix Zielcke <fzielcke@z-51.de>
[grub2/phcoder.git] / util / raid.c
blobad6e407f25c1f1b6932a8767999de3bcae87d5db
1 /* raid.c - RAID support for GRUB utils. */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2006,2007,2008 Free Software Foundation, Inc.
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
20 /* We only support RAID on Linux. */
21 #ifdef __linux__
22 #include <grub/util/misc.h>
23 #include <grub/util/raid.h>
25 #include <string.h>
26 #include <fcntl.h>
27 #include <sys/ioctl.h>
28 #include <errno.h>
30 #include <linux/types.h>
31 #include <linux/major.h>
32 #include <linux/raid/md_p.h>
33 #include <linux/raid/md_u.h>
35 static char *
36 grub_util_getdiskname (int major, int minor)
38 char *name = xmalloc (15);
40 if (major == LOOP_MAJOR)
41 sprintf (name, "/dev/loop%d", minor);
42 else if (major == IDE0_MAJOR)
43 sprintf (name, "/dev/hd%c", 'a' + minor / 64);
44 else if (major == IDE1_MAJOR)
45 sprintf (name, "/dev/hd%c", 'c' + minor / 64);
46 else if (major == IDE2_MAJOR)
47 sprintf (name, "/dev/hd%c", 'e' + minor / 64);
48 else if (major == IDE3_MAJOR)
49 sprintf (name, "/dev/hd%c", 'g' + minor / 64);
50 else if (major == SCSI_DISK0_MAJOR)
51 sprintf (name, "/dev/sd%c", 'a' + minor / 16);
52 else
53 grub_util_error ("Unknown device number: %d, %d", major, minor);
55 return name;
58 char **
59 grub_util_raid_getmembers (char *name)
61 int fd, ret, i, j;
62 char *devname;
63 char **devicelist;
64 mdu_version_t version;
65 mdu_array_info_t info;
66 mdu_disk_info_t disk;
68 devname = xmalloc (strlen (name) + 6);
69 strcpy (devname, "/dev/");
70 strcpy (devname+5, name);
72 fd = open (devname, O_RDONLY);
74 if (fd == -1)
75 grub_util_error ("Can't open %s: %s", devname, strerror (errno));
77 free (devname);
79 ret = ioctl (fd, RAID_VERSION, &version);
80 if (ret != 0)
81 grub_util_error ("ioctl RAID_VERSION error: %s", strerror (errno));
83 if (version.major != 0 || version.minor != 90)
84 grub_util_error ("Unsupported RAID version: %d.%d",
85 version.major, version.minor);
87 ret = ioctl (fd, GET_ARRAY_INFO, &info);
88 if (ret != 0)
89 grub_util_error ("ioctl GET_ARRAY_INFO error: %s", strerror (errno));
91 devicelist = xmalloc ((info.nr_disks + 1) * sizeof (char *));
93 for (i = 0, j = 0; i <info.nr_disks; i++)
95 disk.number = i;
96 ret = ioctl (fd, GET_DISK_INFO, &disk);
97 if (ret != 0)
98 grub_util_error ("ioctl GET_DISK_INFO error: %s", strerror (errno));
100 if (disk.state & (1 << MD_DISK_ACTIVE))
102 devicelist[j] = grub_util_getdiskname (disk.major, disk.minor);
103 j++;
107 devicelist[j] = NULL;
109 return devicelist;
112 #endif /* ! __linux__ */