]> git.proxmox.com Git - grub2.git/blob - util/raid.c
Remerge net branch
[grub2.git] / util / raid.c
1 /* raid.c - RAID support for GRUB utils. */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2006,2007,2008 Free Software Foundation, Inc.
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
20 /* We only support RAID on Linux. */
21 #ifdef __linux__
22 #include <grub/emu/misc.h>
23 #include <grub/util/misc.h>
24 #include <grub/emu/getroot.h>
25
26 #include <string.h>
27 #include <fcntl.h>
28 #include <sys/ioctl.h>
29 #include <errno.h>
30 #include <sys/types.h>
31
32 #include <linux/types.h>
33 #include <linux/major.h>
34 #include <linux/raid/md_p.h>
35 #include <linux/raid/md_u.h>
36 #include <grub/i18n.h>
37
38 char **
39 grub_util_raid_getmembers (const char *name, int bootable)
40 {
41 int fd, ret, i, j;
42 char **devicelist;
43 mdu_version_t version;
44 mdu_array_info_t info;
45 mdu_disk_info_t disk;
46
47 fd = open (name, O_RDONLY);
48
49 if (fd == -1)
50 grub_util_error (_("can't open %s: %s"), name, strerror (errno));
51
52 ret = ioctl (fd, RAID_VERSION, &version);
53 if (ret != 0)
54 grub_util_error (_("ioctl RAID_VERSION error: %s"), strerror (errno));
55
56 if ((version.major != 0 || version.minor != 90)
57 && (version.major != 1 || version.minor != 0)
58 && (version.major != 1 || version.minor != 1)
59 && (version.major != 1 || version.minor != 2))
60 grub_util_error (_("unsupported RAID version: %d.%d"),
61 version.major, version.minor);
62
63 if (bootable && (version.major != 0 || version.minor != 90))
64 grub_util_error (_("unsupported RAID version: %d.%d"),
65 version.major, version.minor);
66
67 ret = ioctl (fd, GET_ARRAY_INFO, &info);
68 if (ret != 0)
69 grub_util_error (_("ioctl GET_ARRAY_INFO error: %s"), strerror (errno));
70
71 devicelist = xmalloc ((info.nr_disks + 1) * sizeof (char *));
72
73 for (i = 0, j = 0; i <info.nr_disks; i++)
74 {
75 disk.number = i;
76 ret = ioctl (fd, GET_DISK_INFO, &disk);
77 if (ret != 0)
78 grub_util_error (_("ioctl GET_DISK_INFO error: %s"), strerror (errno));
79
80 if (disk.state & (1 << MD_DISK_ACTIVE))
81 {
82 devicelist[j] = grub_find_device (NULL,
83 makedev (disk.major, disk.minor));
84 j++;
85 }
86 }
87
88 devicelist[j] = NULL;
89
90 close (fd);
91
92 return devicelist;
93 }
94
95 #endif /* ! __linux__ */