]> git.proxmox.com Git - grub2.git/blob - util/raid.c
Shutdown using ACPI.
[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/util/raid.h>
25
26 #include <string.h>
27 #include <fcntl.h>
28 #include <sys/ioctl.h>
29 #include <errno.h>
30
31 #include <linux/types.h>
32 #include <linux/major.h>
33 #include <linux/raid/md_p.h>
34 #include <linux/raid/md_u.h>
35
36 static char *
37 grub_util_getdiskname (int major, int minor)
38 {
39 char *name = xmalloc (15);
40
41 if (major == LOOP_MAJOR)
42 sprintf (name, "/dev/loop%d", minor);
43 else if (major == IDE0_MAJOR)
44 sprintf (name, "/dev/hd%c", 'a' + minor / 64);
45 else if (major == IDE1_MAJOR)
46 sprintf (name, "/dev/hd%c", 'c' + minor / 64);
47 else if (major == IDE2_MAJOR)
48 sprintf (name, "/dev/hd%c", 'e' + minor / 64);
49 else if (major == IDE3_MAJOR)
50 sprintf (name, "/dev/hd%c", 'g' + minor / 64);
51 else if (major == SCSI_DISK0_MAJOR)
52 sprintf (name, "/dev/sd%c", 'a' + minor / 16);
53 else
54 grub_util_error ("unknown device number: %d, %d", major, minor);
55
56 return name;
57 }
58
59 char **
60 grub_util_raid_getmembers (char *name)
61 {
62 int fd, ret, i, j;
63 char *devname;
64 char **devicelist;
65 mdu_version_t version;
66 mdu_array_info_t info;
67 mdu_disk_info_t disk;
68
69 devname = xmalloc (strlen (name) + 6);
70 strcpy (devname, "/dev/");
71 strcpy (devname+5, name);
72
73 fd = open (devname, O_RDONLY);
74
75 if (fd == -1)
76 grub_util_error ("can't open %s: %s", devname, strerror (errno));
77
78 free (devname);
79
80 ret = ioctl (fd, RAID_VERSION, &version);
81 if (ret != 0)
82 grub_util_error ("ioctl RAID_VERSION error: %s", strerror (errno));
83
84 if (version.major != 0 || version.minor != 90)
85 grub_util_error ("unsupported RAID version: %d.%d",
86 version.major, version.minor);
87
88 ret = ioctl (fd, GET_ARRAY_INFO, &info);
89 if (ret != 0)
90 grub_util_error ("ioctl GET_ARRAY_INFO error: %s", strerror (errno));
91
92 devicelist = xmalloc ((info.nr_disks + 1) * sizeof (char *));
93
94 for (i = 0, j = 0; i <info.nr_disks; i++)
95 {
96 disk.number = i;
97 ret = ioctl (fd, GET_DISK_INFO, &disk);
98 if (ret != 0)
99 grub_util_error ("ioctl GET_DISK_INFO error: %s", strerror (errno));
100
101 if (disk.state & (1 << MD_DISK_ACTIVE))
102 {
103 devicelist[j] = grub_util_getdiskname (disk.major, disk.minor);
104 j++;
105 }
106 }
107
108 devicelist[j] = NULL;
109
110 return devicelist;
111 }
112
113 #endif /* ! __linux__ */