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
if (strncmp (dir, enc_path, enc_path_len) != 0 ||
139
(dir[enc_path_len] && dir[enc_path_len] != '/'))
142
/* This is a parent of the requested directory. /proc/self/mountinfo
143
is in mount order, so it must be the closest parent we've
144
encountered so far. If it's virtual, return its device node;
145
otherwise, carry on to try to find something closer. */
151
continue; /* not a virtual device */
153
sep = strstr (buf + count, " - ");
157
sep += sizeof (" - ") - 1;
158
if (sscanf (sep, "%s %s", fstype, device) != 2)
161
if (stat (device, &st) < 0)
164
if (!S_ISBLK (st.st_mode))
165
continue; /* not a block device */
167
ret = strdup (device);
175
#endif /* __linux__ */
177
#if defined(HAVE_LIBZFS) && defined(HAVE_LIBNVPAIR)
179
find_root_device_from_libzfs (const char *dir)
185
grub_find_zpool_from_dir (dir, &poolname, &poolfs);
190
zpool_handle_t *zpool;
191
libzfs_handle_t *libzfs;
192
nvlist_t *config, *vdev_tree;
193
nvlist_t **children, **path;
194
unsigned int nvlist_count;
197
libzfs = grub_get_libzfs_handle ();
201
zpool = zpool_open (libzfs, poolname);
202
config = zpool_get_config (zpool, NULL);
204
if (nvlist_lookup_nvlist (config, "vdev_tree", &vdev_tree) != 0)
205
error (1, errno, "nvlist_lookup_nvlist (\"vdev_tree\")");
207
if (nvlist_lookup_nvlist_array (vdev_tree, "children", &children, &nvlist_count) != 0)
208
error (1, errno, "nvlist_lookup_nvlist_array (\"children\")");
209
assert (nvlist_count > 0);
211
while (nvlist_lookup_nvlist_array (children[0], "children",
212
&children, &nvlist_count) == 0)
213
assert (nvlist_count > 0);
215
for (i = 0; i < nvlist_count; i++)
217
if (nvlist_lookup_string (children[i], "path", &device) != 0)
218
error (1, errno, "nvlist_lookup_string (\"path\")");
221
if (stat (device, &st) == 0)
241
grub_find_device (const char *dir __attribute__ ((unused)),
242
dev_t dev __attribute__ ((unused)))
247
#elif ! defined(__CYGWIN__)
250
grub_find_device (const char *dir, dev_t dev)
269
saved_cwd = xgetcwd ();
271
grub_util_info ("changing current directory to %s", dir);
279
while ((ent = readdir (dp)) != 0)
284
- dotfiles (like "/dev/.tmp.md0") since they could be duplicates.
285
- dotdirs (like "/dev/.static") since they could contain duplicates. */
286
if (ent->d_name[0] == '.')
289
if (lstat (ent->d_name, &st) < 0)
290
/* Ignore any error. */
293
if (S_ISLNK (st.st_mode)) {
295
if (strcmp (dir, "mapper") == 0) {
296
/* Follow symbolic links under /dev/mapper/; the canonical name
297
may be something like /dev/dm-0, but the names under
298
/dev/mapper/ are more human-readable and so we prefer them if
300
if (stat (ent->d_name, &st) < 0)
303
#endif /* __linux__ */
304
/* Don't follow other symbolic links. */
308
if (S_ISDIR (st.st_mode))
310
/* Find it recursively. */
313
res = grub_find_device (ent->d_name, dev);
317
if (chdir (saved_cwd) < 0)
318
grub_util_error ("cannot restore the original directory");
326
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__APPLE__)
327
if (S_ISCHR (st.st_mode) && st.st_rdev == dev)
329
if (S_ISBLK (st.st_mode) && st.st_rdev == dev)
333
/* Skip device names like /dev/dm-0, which are short-hand aliases
334
to more descriptive device names, e.g. those under /dev/mapper */
335
if (ent->d_name[0] == 'd' &&
336
ent->d_name[1] == 'm' &&
337
ent->d_name[2] == '-' &&
338
ent->d_name[3] >= '0' &&
339
ent->d_name[3] <= '9')
346
#if defined(__NetBSD__)
347
/* Convert this block device to its character (raw) device. */
348
const char *template = "%s/r%s";
350
/* Keep the device name as it is. */
351
const char *template = "%s/%s";
355
res = xmalloc (strlen (cwd) + strlen (ent->d_name) + 3);
356
sprintf (res, template, cwd, ent->d_name);
357
strip_extra_slashes (res);
360
/* /dev/root is not a real block device keep looking, takes care
361
of situation where root filesystem is on the same partition as
364
if (strcmp(res, "/dev/root") == 0)
367
if (chdir (saved_cwd) < 0)
368
grub_util_error ("cannot restore the original directory");
376
if (chdir (saved_cwd) < 0)
377
grub_util_error ("cannot restore the original directory");
384
#else /* __CYGWIN__ */
386
/* Read drive/partition serial number from mbr/boot sector,
387
return 0 on read error, ~0 on unknown serial. */
389
get_bootsec_serial (const char *os_dev, int mbr)
391
/* Read boot sector. */
392
int fd = open (os_dev, O_RDONLY);
395
unsigned char buf[0x200];
396
int n = read (fd, buf, sizeof (buf));
398
if (n != sizeof(buf))
401
/* Check signature. */
402
if (!(buf[0x1fe] == 0x55 && buf[0x1ff] == 0xaa))
405
/* Serial number offset depends on boot sector type. */
408
else if (memcmp (buf + 0x03, "NTFS", 4) == 0)
410
else if (memcmp (buf + 0x52, "FAT32", 5) == 0)
412
else if (memcmp (buf + 0x36, "FAT", 3) == 0)
417
unsigned serial = *(unsigned *)(buf + n);
424
grub_find_device (const char *path, dev_t dev)
426
/* No root device for /cygdrive. */
427
if (dev == (DEV_CYGDRIVE_MAJOR << 16))
430
/* Convert to full POSIX and Win32 path. */
431
char fullpath[PATH_MAX], winpath[PATH_MAX];
432
cygwin_conv_to_full_posix_path (path, fullpath);
433
cygwin_conv_to_full_win32_path (fullpath, winpath);
435
/* If identical, this is no real filesystem path. */
436
if (strcmp (fullpath, winpath) == 0)
439
/* Check for floppy drive letter. */
440
if (winpath[0] && winpath[1] == ':' && strchr ("AaBb", winpath[0]))
441
return xstrdup (strchr ("Aa", winpath[0]) ? "/dev/fd0" : "/dev/fd1");
443
/* Cygwin returns the partition serial number in stat.st_dev.
444
This is never identical to the device number of the emulated
445
/dev/sdXN device, so above grub_find_device () does not work.
446
Search the partition with the same serial in boot sector instead. */
447
char devpath[sizeof ("/dev/sda15") + 13]; /* Size + Paranoia. */
449
for (d = 'a'; d <= 'z'; d++)
451
sprintf (devpath, "/dev/sd%c", d);
452
if (get_bootsec_serial (devpath, 1) == 0)
455
for (p = 1; p <= 15; p++)
457
sprintf (devpath, "/dev/sd%c%d", d, p);
458
unsigned ser = get_bootsec_serial (devpath, 0);
461
if (ser != (unsigned)~0 && dev == (dev_t)ser)
462
return xstrdup (devpath);
468
#endif /* __CYGWIN__ */
471
grub_guess_root_device (const char *dir)
481
mach_msg_type_number_t num_ports = 0, num_ints = 0, num_offsets = 0, data_len = 0;
484
file = file_name_lookup (dir, 0, 0);
485
if (file == MACH_PORT_NULL)
488
err = file_get_storage_info (file,
491
&offsets, &num_offsets,
495
grub_util_error ("Storage info for `%s' does not include type", dir);
496
if (ints[0] != STORAGE_DEVICE)
497
grub_util_error ("Filesystem of `%s' is not stored on local disk", dir);
500
grub_util_error ("Storage info for `%s' does not include name", dir);
502
if (name_len < data_len)
503
grub_util_error ("Bogus name length for storage info for `%s'", dir);
504
if (data[name_len - 1] != '\0')
505
grub_util_error ("Storage name for `%s' not NUL-terminated", dir);
507
os_dev = xmalloc (strlen ("/dev/") + data_len);
508
memcpy (os_dev, "/dev/", strlen ("/dev/"));
509
memcpy (os_dev + strlen ("/dev/"), data, data_len);
511
if (ports && num_ports > 0)
513
mach_msg_type_number_t i;
514
for (i = 0; i < num_ports; i++)
516
mach_port_t port = ports[i];
517
if (port != MACH_PORT_NULL)
518
mach_port_deallocate (mach_task_self(), port);
520
munmap ((caddr_t) ports, num_ports * sizeof (*ports));
523
if (ints && num_ints > 0)
524
munmap ((caddr_t) ints, num_ints * sizeof (*ints));
525
if (offsets && num_offsets > 0)
526
munmap ((caddr_t) offsets, num_offsets * sizeof (*offsets));
527
if (data && data_len > 0)
528
munmap (data, data_len);
529
mach_port_deallocate (mach_task_self (), file);
534
os_dev = find_root_device_from_mountinfo (dir);
537
#endif /* __linux__ */
539
#if defined(HAVE_LIBZFS) && defined(HAVE_LIBNVPAIR)
540
os_dev = find_root_device_from_libzfs (dir);
545
if (stat (dir, &st) < 0)
546
grub_util_error ("cannot stat `%s'", dir);
549
/* Cygwin specific function. */
550
os_dev = grub_find_device (dir, st.st_dev);
554
/* This might be truly slow, but is there any better way? */
555
os_dev = grub_find_device ("/dev", st.st_dev);
557
#endif /* !__GNU__ */
563
grub_util_is_dmraid (const char *os_dev)
565
if (! strncmp (os_dev, "/dev/mapper/nvidia_", 19))
567
else if (! strncmp (os_dev, "/dev/mapper/isw_", 16))
569
else if (! strncmp (os_dev, "/dev/mapper/hpt37x_", 19))
571
else if (! strncmp (os_dev, "/dev/mapper/hpt45x_", 19))
573
else if (! strncmp (os_dev, "/dev/mapper/via_", 16))
575
else if (! strncmp (os_dev, "/dev/mapper/lsi_", 16))
577
else if (! strncmp (os_dev, "/dev/mapper/pdc_", 16))
579
else if (! strncmp (os_dev, "/dev/mapper/jmicron_", 20))
581
else if (! strncmp (os_dev, "/dev/mapper/asr_", 16))
583
else if (! strncmp (os_dev, "/dev/mapper/sil_", 16))
585
else if (! strncmp (os_dev, "/dev/mapper/ddf1_", 17))
592
grub_util_get_dev_abstraction (const char *os_dev __attribute__((unused)))
595
/* User explicitly claims that this drive is visible by BIOS. */
596
if (grub_util_biosdisk_is_present (os_dev))
597
return GRUB_DEV_ABSTRACTION_NONE;
600
if (!strncmp (os_dev, "/dev/mapper/", 12)
601
&& ! grub_util_is_dmraid (os_dev)
602
&& strncmp (os_dev, "/dev/mapper/mpath", 17) != 0)
603
return GRUB_DEV_ABSTRACTION_LVM;
605
/* Check for RAID. */
606
if (!strncmp (os_dev, "/dev/md", 7))
607
return GRUB_DEV_ABSTRACTION_RAID;
610
/* No abstraction found. */
611
return GRUB_DEV_ABSTRACTION_NONE;
616
get_mdadm_name (const char *os_dev)
622
if (pipe (mdadm_pipe) < 0)
624
grub_util_warn ("Unable to create pipe for mdadm: %s", strerror (errno));
630
grub_util_warn ("Unable to fork mdadm: %s", strerror (errno));
631
else if (mdadm_pid == 0)
636
close (mdadm_pipe[0]);
637
dup2 (mdadm_pipe[1], STDOUT_FILENO);
638
close (mdadm_pipe[1]);
640
/* execvp has inconvenient types, hence the casts. None of these
641
strings will actually be modified. */
642
argv[0] = (char *) "mdadm";
643
argv[1] = (char *) "--detail";
644
argv[2] = (char *) "--export";
645
argv[3] = (char *) os_dev;
647
execvp ("mdadm", argv);
652
/* Parent. Read mdadm's output. */
657
close (mdadm_pipe[1]);
658
mdadm = fdopen (mdadm_pipe[0], "r");
661
grub_util_warn ("Unable to open stream from mdadm: %s",
666
while (getline (&buf, &len, mdadm) > 0)
668
if (strncmp (buf, "MD_NAME=", sizeof ("MD_NAME=") - 1) == 0)
670
char *name_start, *colon;
674
name_start = buf + sizeof ("MD_NAME=") - 1;
675
/* Strip off the homehost if present. */
676
colon = strchr (name_start, ':');
677
name = strdup (colon ? colon + 1 : name_start);
678
name_len = strlen (name);
679
if (name[name_len - 1] == '\n')
680
name[name_len - 1] = '\0';
685
close (mdadm_pipe[0]);
686
waitpid (mdadm_pid, NULL, 0);
691
#endif /* __linux__ */
694
grub_util_get_grub_dev (const char *os_dev)
696
char *grub_dev = NULL;
698
switch (grub_util_get_dev_abstraction (os_dev))
700
case GRUB_DEV_ABSTRACTION_LVM:
703
unsigned short i, len;
704
grub_size_t offset = sizeof ("/dev/mapper/") - 1;
706
len = strlen (os_dev) - offset + 1;
707
grub_dev = xmalloc (len);
709
for (i = 0; i < len; i++, offset++)
711
grub_dev[i] = os_dev[offset];
712
if (os_dev[offset] == '-' && os_dev[offset + 1] == '-')
719
case GRUB_DEV_ABSTRACTION_RAID:
721
if (os_dev[7] == '_' && os_dev[8] == 'd')
723
/* This a partitionable RAID device of the form /dev/md_dNNpMM. */
727
p = strdup (os_dev + sizeof ("/dev/md_d") - 1);
733
grub_dev = xasprintf ("md%s", p);
736
else if (os_dev[7] == '/' && os_dev[8] == 'd')
738
/* This a partitionable RAID device of the form /dev/md/dNNpMM. */
742
p = strdup (os_dev + sizeof ("/dev/md/d") - 1);
748
grub_dev = xasprintf ("md%s", p);
751
else if (os_dev[7] >= '0' && os_dev[7] <= '9')
755
p = strdup (os_dev + sizeof ("/dev/md") - 1);
761
grub_dev = xasprintf ("md%s", p);
764
else if (os_dev[7] == '/' && os_dev[8] >= '0' && os_dev[8] <= '9')
768
p = strdup (os_dev + sizeof ("/dev/md/") - 1);
774
grub_dev = xasprintf ("md%s", p);
777
else if (os_dev[7] == '/')
779
/* mdraid 1.x with a free name. */
782
p = strdup (os_dev + sizeof ("/dev/md/") - 1);
788
grub_dev = xasprintf ("md/%s", p);
792
grub_util_error ("unknown kind of RAID device `%s'", os_dev);
796
char *mdadm_name = get_mdadm_name (os_dev);
801
grub_dev = xasprintf ("md/%s", mdadm_name);
805
#endif /* __linux__ */
809
default: /* GRUB_DEV_ABSTRACTION_NONE */
810
grub_dev = grub_util_biosdisk_get_grub_dev (os_dev);
817
grub_util_check_block_device (const char *blk_dev)
821
if (stat (blk_dev, &st) < 0)
822
grub_util_error ("cannot stat `%s'", blk_dev);
824
if (S_ISBLK (st.st_mode))
831
grub_util_check_char_device (const char *blk_dev)
835
if (stat (blk_dev, &st) < 0)
836
grub_util_error ("cannot stat `%s'", blk_dev);
838
if (S_ISCHR (st.st_mode))