1
/* getroot.c - Get root device */
3
* GRUB -- GRand Unified Bootloader
4
* Copyright (C) 1999,2000,2001,2002,2003,2006,2007,2008,2009,2010 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/>.
22
#include <sys/types.h>
33
#include <grub/util/misc.h>
37
#include <hurd/lookup.h>
43
# include <sys/types.h>
44
# include <sys/wait.h>
47
#if defined(HAVE_LIBZFS) && defined(HAVE_LIBNVPAIR)
48
# include <grub/util/libzfs.h>
49
# include <grub/util/libnvpair.h>
53
#include <grub/misc.h>
54
#include <grub/emu/misc.h>
55
#include <grub/emu/hostdisk.h>
56
#include <grub/emu/getroot.h>
59
strip_extra_slashes (char *dir)
63
while ((p = strchr (p, '/')) != 0)
67
memmove (p, p + 1, strlen (p));
70
else if (p[1] == '\0')
87
path = xmalloc (size);
88
while (! getcwd (path, size))
91
path = xrealloc (path, size);
99
/* Statting something on a btrfs filesystem always returns a virtual device
100
major/minor pair rather than the real underlying device, because btrfs
101
can span multiple underlying devices (and even if it's currently only
102
using a single device it can be dynamically extended onto another). We
103
can't deal with the multiple-device case yet, but in the meantime, we can
104
at least cope with the single-device case by scanning
105
/proc/self/mountinfo. */
107
find_root_device_from_mountinfo (const char *dir)
114
fp = fopen ("/proc/self/mountinfo", "r");
116
return NULL; /* fall through to other methods */
118
while (getline (&buf, &len, fp) > 0)
120
int mnt_id, parent_mnt_id;
121
unsigned int major, minor;
122
char enc_root[PATH_MAX], enc_path[PATH_MAX];
126
char fstype[PATH_MAX], device[PATH_MAX];
129
if (sscanf (buf, "%d %d %u:%u %s %s%n",
130
&mnt_id, &parent_mnt_id, &major, &minor, enc_root, enc_path,
134
if (strcmp (enc_root, "/") != 0)
135
continue; /* only a subtree is mounted */
137
enc_path_len = strlen (enc_path);
138
/* Check that enc_path is a prefix of dir. The prefix must either be
139
the entire string, or end with a slash, or be immediately followed
141
if (strncmp (dir, enc_path, enc_path_len) != 0 ||
142
(enc_path_len && dir[enc_path_len - 1] != '/' &&
143
dir[enc_path_len] && dir[enc_path_len] != '/'))
146
/* This is a parent of the requested directory. /proc/self/mountinfo
147
is in mount order, so it must be the closest parent we've
148
encountered so far. If it's virtual, return its device node;
149
otherwise, carry on to try to find something closer. */
155
continue; /* not a virtual device */
157
sep = strstr (buf + count, " - ");
161
sep += sizeof (" - ") - 1;
162
if (sscanf (sep, "%s %s", fstype, device) != 2)
165
if (stat (device, &st) < 0)
168
if (!S_ISBLK (st.st_mode))
169
continue; /* not a block device */
171
ret = strdup (device);
179
#endif /* __linux__ */
181
#if defined(HAVE_LIBZFS) && defined(HAVE_LIBNVPAIR)
183
find_root_device_from_libzfs (const char *dir)
189
grub_find_zpool_from_dir (dir, &poolname, &poolfs);
194
zpool_handle_t *zpool;
195
libzfs_handle_t *libzfs;
196
nvlist_t *config, *vdev_tree;
197
nvlist_t **children, **path;
198
unsigned int nvlist_count;
201
libzfs = grub_get_libzfs_handle ();
205
zpool = zpool_open (libzfs, poolname);
206
config = zpool_get_config (zpool, NULL);
208
if (nvlist_lookup_nvlist (config, "vdev_tree", &vdev_tree) != 0)
209
error (1, errno, "nvlist_lookup_nvlist (\"vdev_tree\")");
211
if (nvlist_lookup_nvlist_array (vdev_tree, "children", &children, &nvlist_count) != 0)
212
error (1, errno, "nvlist_lookup_nvlist_array (\"children\")");
213
assert (nvlist_count > 0);
215
while (nvlist_lookup_nvlist_array (children[0], "children",
216
&children, &nvlist_count) == 0)
217
assert (nvlist_count > 0);
219
for (i = 0; i < nvlist_count; i++)
221
if (nvlist_lookup_string (children[i], "path", &device) != 0)
222
error (1, errno, "nvlist_lookup_string (\"path\")");
225
if (stat (device, &st) == 0)
245
grub_find_device (const char *dir __attribute__ ((unused)),
246
dev_t dev __attribute__ ((unused)))
251
#elif ! defined(__CYGWIN__)
254
grub_find_device (const char *dir, dev_t dev)
273
saved_cwd = xgetcwd ();
275
grub_util_info ("changing current directory to %s", dir);
283
while ((ent = readdir (dp)) != 0)
288
- dotfiles (like "/dev/.tmp.md0") since they could be duplicates.
289
- dotdirs (like "/dev/.static") since they could contain duplicates. */
290
if (ent->d_name[0] == '.')
293
if (lstat (ent->d_name, &st) < 0)
294
/* Ignore any error. */
297
if (S_ISLNK (st.st_mode)) {
299
if (strcmp (dir, "mapper") == 0) {
300
/* Follow symbolic links under /dev/mapper/; the canonical name
301
may be something like /dev/dm-0, but the names under
302
/dev/mapper/ are more human-readable and so we prefer them if
304
if (stat (ent->d_name, &st) < 0)
307
#endif /* __linux__ */
308
/* Don't follow other symbolic links. */
312
if (S_ISDIR (st.st_mode))
314
/* Find it recursively. */
317
res = grub_find_device (ent->d_name, dev);
321
if (chdir (saved_cwd) < 0)
322
grub_util_error ("cannot restore the original directory");
330
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__APPLE__)
331
if (S_ISCHR (st.st_mode) && st.st_rdev == dev)
333
if (S_ISBLK (st.st_mode) && st.st_rdev == dev)
337
/* Skip device names like /dev/dm-0, which are short-hand aliases
338
to more descriptive device names, e.g. those under /dev/mapper */
339
if (ent->d_name[0] == 'd' &&
340
ent->d_name[1] == 'm' &&
341
ent->d_name[2] == '-' &&
342
ent->d_name[3] >= '0' &&
343
ent->d_name[3] <= '9')
350
#if defined(__NetBSD__)
351
/* Convert this block device to its character (raw) device. */
352
const char *template = "%s/r%s";
354
/* Keep the device name as it is. */
355
const char *template = "%s/%s";
359
res = xmalloc (strlen (cwd) + strlen (ent->d_name) + 3);
360
sprintf (res, template, cwd, ent->d_name);
361
strip_extra_slashes (res);
364
/* /dev/root is not a real block device keep looking, takes care
365
of situation where root filesystem is on the same partition as
368
if (strcmp(res, "/dev/root") == 0)
371
if (chdir (saved_cwd) < 0)
372
grub_util_error ("cannot restore the original directory");
380
if (chdir (saved_cwd) < 0)
381
grub_util_error ("cannot restore the original directory");
388
#else /* __CYGWIN__ */
390
/* Read drive/partition serial number from mbr/boot sector,
391
return 0 on read error, ~0 on unknown serial. */
393
get_bootsec_serial (const char *os_dev, int mbr)
395
/* Read boot sector. */
396
int fd = open (os_dev, O_RDONLY);
399
unsigned char buf[0x200];
400
int n = read (fd, buf, sizeof (buf));
402
if (n != sizeof(buf))
405
/* Check signature. */
406
if (!(buf[0x1fe] == 0x55 && buf[0x1ff] == 0xaa))
409
/* Serial number offset depends on boot sector type. */
412
else if (memcmp (buf + 0x03, "NTFS", 4) == 0)
414
else if (memcmp (buf + 0x52, "FAT32", 5) == 0)
416
else if (memcmp (buf + 0x36, "FAT", 3) == 0)
421
unsigned serial = *(unsigned *)(buf + n);
428
grub_find_device (const char *path, dev_t dev)
430
/* No root device for /cygdrive. */
431
if (dev == (DEV_CYGDRIVE_MAJOR << 16))
434
/* Convert to full POSIX and Win32 path. */
435
char fullpath[PATH_MAX], winpath[PATH_MAX];
436
cygwin_conv_to_full_posix_path (path, fullpath);
437
cygwin_conv_to_full_win32_path (fullpath, winpath);
439
/* If identical, this is no real filesystem path. */
440
if (strcmp (fullpath, winpath) == 0)
443
/* Check for floppy drive letter. */
444
if (winpath[0] && winpath[1] == ':' && strchr ("AaBb", winpath[0]))
445
return xstrdup (strchr ("Aa", winpath[0]) ? "/dev/fd0" : "/dev/fd1");
447
/* Cygwin returns the partition serial number in stat.st_dev.
448
This is never identical to the device number of the emulated
449
/dev/sdXN device, so above grub_find_device () does not work.
450
Search the partition with the same serial in boot sector instead. */
451
char devpath[sizeof ("/dev/sda15") + 13]; /* Size + Paranoia. */
453
for (d = 'a'; d <= 'z'; d++)
455
sprintf (devpath, "/dev/sd%c", d);
456
if (get_bootsec_serial (devpath, 1) == 0)
459
for (p = 1; p <= 15; p++)
461
sprintf (devpath, "/dev/sd%c%d", d, p);
462
unsigned ser = get_bootsec_serial (devpath, 0);
465
if (ser != (unsigned)~0 && dev == (dev_t)ser)
466
return xstrdup (devpath);
472
#endif /* __CYGWIN__ */
475
grub_guess_root_device (const char *dir)
485
mach_msg_type_number_t num_ports = 0, num_ints = 0, num_offsets = 0, data_len = 0;
488
file = file_name_lookup (dir, 0, 0);
489
if (file == MACH_PORT_NULL)
492
err = file_get_storage_info (file,
495
&offsets, &num_offsets,
499
grub_util_error ("Storage info for `%s' does not include type", dir);
500
if (ints[0] != STORAGE_DEVICE)
501
grub_util_error ("Filesystem of `%s' is not stored on local disk", dir);
504
grub_util_error ("Storage info for `%s' does not include name", dir);
506
if (name_len < data_len)
507
grub_util_error ("Bogus name length for storage info for `%s'", dir);
508
if (data[name_len - 1] != '\0')
509
grub_util_error ("Storage name for `%s' not NUL-terminated", dir);
511
os_dev = xmalloc (strlen ("/dev/") + data_len);
512
memcpy (os_dev, "/dev/", strlen ("/dev/"));
513
memcpy (os_dev + strlen ("/dev/"), data, data_len);
515
if (ports && num_ports > 0)
517
mach_msg_type_number_t i;
518
for (i = 0; i < num_ports; i++)
520
mach_port_t port = ports[i];
521
if (port != MACH_PORT_NULL)
522
mach_port_deallocate (mach_task_self(), port);
524
munmap ((caddr_t) ports, num_ports * sizeof (*ports));
527
if (ints && num_ints > 0)
528
munmap ((caddr_t) ints, num_ints * sizeof (*ints));
529
if (offsets && num_offsets > 0)
530
munmap ((caddr_t) offsets, num_offsets * sizeof (*offsets));
531
if (data && data_len > 0)
532
munmap (data, data_len);
533
mach_port_deallocate (mach_task_self (), file);
538
os_dev = find_root_device_from_mountinfo (dir);
541
#endif /* __linux__ */
543
#if defined(HAVE_LIBZFS) && defined(HAVE_LIBNVPAIR)
544
os_dev = find_root_device_from_libzfs (dir);
549
if (stat (dir, &st) < 0)
550
grub_util_error ("cannot stat `%s'", dir);
553
/* Cygwin specific function. */
554
os_dev = grub_find_device (dir, st.st_dev);
558
/* This might be truly slow, but is there any better way? */
559
os_dev = grub_find_device ("/dev", st.st_dev);
561
#endif /* !__GNU__ */
567
grub_util_is_dmraid (const char *os_dev)
569
if (! strncmp (os_dev, "/dev/mapper/nvidia_", 19))
571
else if (! strncmp (os_dev, "/dev/mapper/isw_", 16))
573
else if (! strncmp (os_dev, "/dev/mapper/hpt37x_", 19))
575
else if (! strncmp (os_dev, "/dev/mapper/hpt45x_", 19))
577
else if (! strncmp (os_dev, "/dev/mapper/via_", 16))
579
else if (! strncmp (os_dev, "/dev/mapper/lsi_", 16))
581
else if (! strncmp (os_dev, "/dev/mapper/pdc_", 16))
583
else if (! strncmp (os_dev, "/dev/mapper/jmicron_", 20))
585
else if (! strncmp (os_dev, "/dev/mapper/asr_", 16))
587
else if (! strncmp (os_dev, "/dev/mapper/sil_", 16))
589
else if (! strncmp (os_dev, "/dev/mapper/ddf1_", 17))
596
grub_util_get_dev_abstraction (const char *os_dev __attribute__((unused)))
599
/* User explicitly claims that this drive is visible by BIOS. */
600
if (grub_util_biosdisk_is_present (os_dev))
601
return GRUB_DEV_ABSTRACTION_NONE;
604
if (!strncmp (os_dev, "/dev/mapper/", 12)
605
&& ! grub_util_is_dmraid (os_dev)
606
&& strncmp (os_dev, "/dev/mapper/mpath", 17) != 0)
607
return GRUB_DEV_ABSTRACTION_LVM;
609
/* Check for RAID. */
610
if (!strncmp (os_dev, "/dev/md", 7))
611
return GRUB_DEV_ABSTRACTION_RAID;
614
/* No abstraction found. */
615
return GRUB_DEV_ABSTRACTION_NONE;
620
get_mdadm_name (const char *os_dev)
626
if (pipe (mdadm_pipe) < 0)
628
grub_util_warn ("Unable to create pipe for mdadm: %s", strerror (errno));
634
grub_util_warn ("Unable to fork mdadm: %s", strerror (errno));
635
else if (mdadm_pid == 0)
640
close (mdadm_pipe[0]);
641
dup2 (mdadm_pipe[1], STDOUT_FILENO);
642
close (mdadm_pipe[1]);
644
/* execvp has inconvenient types, hence the casts. None of these
645
strings will actually be modified. */
646
argv[0] = (char *) "mdadm";
647
argv[1] = (char *) "--detail";
648
argv[2] = (char *) "--export";
649
argv[3] = (char *) os_dev;
651
execvp ("mdadm", argv);
656
/* Parent. Read mdadm's output. */
661
close (mdadm_pipe[1]);
662
mdadm = fdopen (mdadm_pipe[0], "r");
665
grub_util_warn ("Unable to open stream from mdadm: %s",
670
while (getline (&buf, &len, mdadm) > 0)
672
if (strncmp (buf, "MD_NAME=", sizeof ("MD_NAME=") - 1) == 0)
674
char *name_start, *colon;
678
name_start = buf + sizeof ("MD_NAME=") - 1;
679
/* Strip off the homehost if present. */
680
colon = strchr (name_start, ':');
681
name = strdup (colon ? colon + 1 : name_start);
682
name_len = strlen (name);
683
if (name[name_len - 1] == '\n')
684
name[name_len - 1] = '\0';
689
close (mdadm_pipe[0]);
690
waitpid (mdadm_pid, NULL, 0);
695
#endif /* __linux__ */
698
grub_util_get_grub_dev (const char *os_dev)
700
char *grub_dev = NULL;
702
switch (grub_util_get_dev_abstraction (os_dev))
704
case GRUB_DEV_ABSTRACTION_LVM:
707
unsigned short i, len;
708
grub_size_t offset = sizeof ("/dev/mapper/") - 1;
710
len = strlen (os_dev) - offset + 1;
711
grub_dev = xmalloc (len);
713
for (i = 0; i < len; i++, offset++)
715
grub_dev[i] = os_dev[offset];
716
if (os_dev[offset] == '-' && os_dev[offset + 1] == '-')
723
case GRUB_DEV_ABSTRACTION_RAID:
725
if (os_dev[7] == '_' && os_dev[8] == 'd')
727
/* This a partitionable RAID device of the form /dev/md_dNNpMM. */
731
p = strdup (os_dev + sizeof ("/dev/md_d") - 1);
737
grub_dev = xasprintf ("md%s", p);
740
else if (os_dev[7] == '/' && os_dev[8] == 'd')
742
/* This a partitionable RAID device of the form /dev/md/dNNpMM. */
746
p = strdup (os_dev + sizeof ("/dev/md/d") - 1);
752
grub_dev = xasprintf ("md%s", p);
755
else if (os_dev[7] >= '0' && os_dev[7] <= '9')
759
p = strdup (os_dev + sizeof ("/dev/md") - 1);
765
grub_dev = xasprintf ("md%s", p);
768
else if (os_dev[7] == '/' && os_dev[8] >= '0' && os_dev[8] <= '9')
772
p = strdup (os_dev + sizeof ("/dev/md/") - 1);
778
grub_dev = xasprintf ("md%s", p);
781
else if (os_dev[7] == '/')
783
/* mdraid 1.x with a free name. */
786
p = strdup (os_dev + sizeof ("/dev/md/") - 1);
792
grub_dev = xasprintf ("md/%s", p);
796
grub_util_error ("unknown kind of RAID device `%s'", os_dev);
800
char *mdadm_name = get_mdadm_name (os_dev);
806
newname = xasprintf ("/dev/md/%s", mdadm_name);
807
if (stat (newname, &st) == 0)
810
grub_dev = xasprintf ("md/%s", mdadm_name);
816
#endif /* __linux__ */
820
default: /* GRUB_DEV_ABSTRACTION_NONE */
821
grub_dev = grub_util_biosdisk_get_grub_dev (os_dev);
828
grub_util_check_block_device (const char *blk_dev)
832
if (stat (blk_dev, &st) < 0)
833
grub_util_error ("cannot stat `%s'", blk_dev);
835
if (S_ISBLK (st.st_mode))
842
grub_util_check_char_device (const char *blk_dev)
846
if (stat (blk_dev, &st) < 0)
847
grub_util_error ("cannot stat `%s'", blk_dev);
849
if (S_ISCHR (st.st_mode))