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)
186
mnt_point = grub_find_mount_point_from_dir (dir);
187
grub_find_zpool_from_mount_point (mnt_point, &poolname, &poolfs);
195
zpool_handle_t *zpool;
197
nvlist_t **nvlist_array;
198
unsigned int nvlist_count;
200
zpool = zpool_open (grub_get_libzfs_handle (), poolname);
201
nvlist = zpool_get_config (zpool, NULL);
203
if (nvlist_lookup_nvlist (nvlist, "vdev_tree", &nvlist) != 0)
204
error (1, errno, "nvlist_lookup_nvlist (\"vdev_tree\")");
206
if (nvlist_lookup_nvlist_array (nvlist, "children", &nvlist_array, &nvlist_count) != 0)
207
error (1, errno, "nvlist_lookup_nvlist_array (\"children\")");
211
assert (nvlist_count > 0);
212
} while (nvlist_lookup_nvlist_array (nvlist_array[0], "children",
213
&nvlist_array, &nvlist_count) == 0);
215
if (nvlist_lookup_string (nvlist_array[0], "path", &device) != 0)
216
error (1, errno, "nvlist_lookup_string (\"path\")");
232
find_root_device (const char *dir __attribute__ ((unused)),
233
dev_t dev __attribute__ ((unused)))
238
#elif ! defined(__CYGWIN__)
241
find_root_device (const char *dir, dev_t dev)
251
saved_cwd = xgetcwd ();
253
grub_util_info ("changing current directory to %s", dir);
261
while ((ent = readdir (dp)) != 0)
266
- dotfiles (like "/dev/.tmp.md0") since they could be duplicates.
267
- dotdirs (like "/dev/.static") since they could contain duplicates. */
268
if (ent->d_name[0] == '.')
271
if (lstat (ent->d_name, &st) < 0)
272
/* Ignore any error. */
275
if (S_ISLNK (st.st_mode)) {
277
if (strcmp (dir, "mapper") == 0) {
278
/* Follow symbolic links under /dev/mapper/; the canonical name
279
may be something like /dev/dm-0, but the names under
280
/dev/mapper/ are more human-readable and so we prefer them if
282
if (stat (ent->d_name, &st) < 0)
285
#endif /* __linux__ */
286
/* Don't follow other symbolic links. */
290
if (S_ISDIR (st.st_mode))
292
/* Find it recursively. */
295
res = find_root_device (ent->d_name, dev);
299
if (chdir (saved_cwd) < 0)
300
grub_util_error ("cannot restore the original directory");
308
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__APPLE__)
309
if (S_ISCHR (st.st_mode) && st.st_rdev == dev)
311
if (S_ISBLK (st.st_mode) && st.st_rdev == dev)
315
/* Skip device names like /dev/dm-0, which are short-hand aliases
316
to more descriptive device names, e.g. those under /dev/mapper */
317
if (ent->d_name[0] == 'd' &&
318
ent->d_name[1] == 'm' &&
319
ent->d_name[2] == '-' &&
320
ent->d_name[3] >= '0' &&
321
ent->d_name[3] <= '9')
328
#if defined(__NetBSD__)
329
/* Convert this block device to its character (raw) device. */
330
const char *template = "%s/r%s";
332
/* Keep the device name as it is. */
333
const char *template = "%s/%s";
337
res = xmalloc (strlen (cwd) + strlen (ent->d_name) + 3);
338
sprintf (res, template, cwd, ent->d_name);
339
strip_extra_slashes (res);
342
/* /dev/root is not a real block device keep looking, takes care
343
of situation where root filesystem is on the same partition as
346
if (strcmp(res, "/dev/root") == 0)
349
if (chdir (saved_cwd) < 0)
350
grub_util_error ("cannot restore the original directory");
358
if (chdir (saved_cwd) < 0)
359
grub_util_error ("cannot restore the original directory");
366
#else /* __CYGWIN__ */
368
/* Read drive/partition serial number from mbr/boot sector,
369
return 0 on read error, ~0 on unknown serial. */
371
get_bootsec_serial (const char *os_dev, int mbr)
373
/* Read boot sector. */
374
int fd = open (os_dev, O_RDONLY);
377
unsigned char buf[0x200];
378
int n = read (fd, buf, sizeof (buf));
380
if (n != sizeof(buf))
383
/* Check signature. */
384
if (!(buf[0x1fe] == 0x55 && buf[0x1ff] == 0xaa))
387
/* Serial number offset depends on boot sector type. */
390
else if (memcmp (buf + 0x03, "NTFS", 4) == 0)
392
else if (memcmp (buf + 0x52, "FAT32", 5) == 0)
394
else if (memcmp (buf + 0x36, "FAT", 3) == 0)
399
unsigned serial = *(unsigned *)(buf + n);
406
find_cygwin_root_device (const char *path, dev_t dev)
408
/* No root device for /cygdrive. */
409
if (dev == (DEV_CYGDRIVE_MAJOR << 16))
412
/* Convert to full POSIX and Win32 path. */
413
char fullpath[PATH_MAX], winpath[PATH_MAX];
414
cygwin_conv_to_full_posix_path (path, fullpath);
415
cygwin_conv_to_full_win32_path (fullpath, winpath);
417
/* If identical, this is no real filesystem path. */
418
if (strcmp (fullpath, winpath) == 0)
421
/* Check for floppy drive letter. */
422
if (winpath[0] && winpath[1] == ':' && strchr ("AaBb", winpath[0]))
423
return xstrdup (strchr ("Aa", winpath[0]) ? "/dev/fd0" : "/dev/fd1");
425
/* Cygwin returns the partition serial number in stat.st_dev.
426
This is never identical to the device number of the emulated
427
/dev/sdXN device, so above find_root_device () does not work.
428
Search the partition with the same serial in boot sector instead. */
429
char devpath[sizeof ("/dev/sda15") + 13]; /* Size + Paranoia. */
431
for (d = 'a'; d <= 'z'; d++)
433
sprintf (devpath, "/dev/sd%c", d);
434
if (get_bootsec_serial (devpath, 1) == 0)
437
for (p = 1; p <= 15; p++)
439
sprintf (devpath, "/dev/sd%c%d", d, p);
440
unsigned ser = get_bootsec_serial (devpath, 0);
443
if (ser != (unsigned)~0 && dev == (dev_t)ser)
444
return xstrdup (devpath);
450
#endif /* __CYGWIN__ */
453
grub_guess_root_device (const char *dir)
463
mach_msg_type_number_t num_ports = 0, num_ints = 0, num_offsets = 0, data_len = 0;
466
file = file_name_lookup (dir, 0, 0);
467
if (file == MACH_PORT_NULL)
470
err = file_get_storage_info (file,
473
&offsets, &num_offsets,
477
grub_util_error ("Storage info for `%s' does not include type", dir);
478
if (ints[0] != STORAGE_DEVICE)
479
grub_util_error ("Filesystem of `%s' is not stored on local disk", dir);
482
grub_util_error ("Storage info for `%s' does not include name", dir);
484
if (name_len < data_len)
485
grub_util_error ("Bogus name length for storage info for `%s'", dir);
486
if (data[name_len - 1] != '\0')
487
grub_util_error ("Storage name for `%s' not NUL-terminated", dir);
489
os_dev = xmalloc (strlen ("/dev/") + data_len);
490
memcpy (os_dev, "/dev/", strlen ("/dev/"));
491
memcpy (os_dev + strlen ("/dev/"), data, data_len);
493
if (ports && num_ports > 0)
495
mach_msg_type_number_t i;
496
for (i = 0; i < num_ports; i++)
498
mach_port_t port = ports[i];
499
if (port != MACH_PORT_NULL)
500
mach_port_deallocate (mach_task_self(), port);
502
munmap ((caddr_t) ports, num_ports * sizeof (*ports));
505
if (ints && num_ints > 0)
506
munmap ((caddr_t) ints, num_ints * sizeof (*ints));
507
if (offsets && num_offsets > 0)
508
munmap ((caddr_t) offsets, num_offsets * sizeof (*offsets));
509
if (data && data_len > 0)
510
munmap (data, data_len);
511
mach_port_deallocate (mach_task_self (), file);
516
os_dev = find_root_device_from_mountinfo (dir);
519
#endif /* __linux__ */
521
#if defined(HAVE_LIBZFS) && defined(HAVE_LIBNVPAIR)
522
os_dev = find_root_device_from_libzfs (dir);
527
if (stat (dir, &st) < 0)
528
grub_util_error ("cannot stat `%s'", dir);
531
/* Cygwin specific function. */
532
os_dev = find_cygwin_root_device (dir, st.st_dev);
536
/* This might be truly slow, but is there any better way? */
537
os_dev = find_root_device ("/dev", st.st_dev);
539
#endif /* !__GNU__ */
545
grub_util_is_dmraid (const char *os_dev)
547
if (! strncmp (os_dev, "/dev/mapper/nvidia_", 19))
549
else if (! strncmp (os_dev, "/dev/mapper/isw_", 16))
551
else if (! strncmp (os_dev, "/dev/mapper/hpt37x_", 19))
553
else if (! strncmp (os_dev, "/dev/mapper/hpt45x_", 19))
555
else if (! strncmp (os_dev, "/dev/mapper/via_", 16))
557
else if (! strncmp (os_dev, "/dev/mapper/lsi_", 16))
559
else if (! strncmp (os_dev, "/dev/mapper/pdc_", 16))
561
else if (! strncmp (os_dev, "/dev/mapper/jmicron_", 20))
563
else if (! strncmp (os_dev, "/dev/mapper/asr_", 16))
565
else if (! strncmp (os_dev, "/dev/mapper/sil_", 16))
572
grub_util_get_dev_abstraction (const char *os_dev __attribute__((unused)))
576
if (!strncmp (os_dev, "/dev/mapper/", 12)
577
&& ! grub_util_is_dmraid (os_dev)
578
&& strncmp (os_dev, "/dev/mapper/mpath", 17) != 0)
579
return GRUB_DEV_ABSTRACTION_LVM;
581
/* Check for RAID. */
582
if (!strncmp (os_dev, "/dev/md", 7))
583
return GRUB_DEV_ABSTRACTION_RAID;
586
/* No abstraction found. */
587
return GRUB_DEV_ABSTRACTION_NONE;
592
get_mdadm_name (const char *os_dev)
598
if (pipe (mdadm_pipe) < 0)
600
grub_util_warn ("Unable to create pipe for mdadm: %s", strerror (errno));
606
grub_util_warn ("Unable to fork mdadm: %s", strerror (errno));
607
else if (mdadm_pid == 0)
612
close (mdadm_pipe[0]);
613
dup2 (mdadm_pipe[1], STDOUT_FILENO);
614
close (mdadm_pipe[1]);
616
/* execvp has inconvenient types, hence the casts. None of these
617
strings will actually be modified. */
618
argv[0] = (char *) "mdadm";
619
argv[1] = (char *) "--detail";
620
argv[2] = (char *) "--export";
621
argv[3] = (char *) os_dev;
623
execvp ("mdadm", argv);
628
/* Parent. Read mdadm's output. */
633
close (mdadm_pipe[1]);
634
mdadm = fdopen (mdadm_pipe[0], "r");
637
grub_util_warn ("Unable to open stream from mdadm: %s",
642
while (getline (&buf, &len, mdadm) > 0)
644
if (strncmp (buf, "MD_NAME=", sizeof ("MD_NAME=") - 1) == 0)
646
char *name_start, *colon;
650
name_start = buf + sizeof ("MD_NAME=") - 1;
651
/* Strip off the homehost if present. */
652
colon = strchr (name_start, ':');
653
name = strdup (colon ? colon + 1 : name_start);
654
name_len = strlen (name);
655
if (name[name_len - 1] == '\n')
656
name[name_len - 1] = '\0';
661
close (mdadm_pipe[0]);
662
waitpid (mdadm_pid, NULL, 0);
667
#endif /* __linux__ */
670
grub_util_get_grub_dev (const char *os_dev)
672
char *grub_dev = NULL;
674
switch (grub_util_get_dev_abstraction (os_dev))
676
case GRUB_DEV_ABSTRACTION_LVM:
679
unsigned short i, len;
680
grub_size_t offset = sizeof ("/dev/mapper/") - 1;
682
len = strlen (os_dev) - offset + 1;
683
grub_dev = xmalloc (len);
685
for (i = 0; i < len; i++, offset++)
687
grub_dev[i] = os_dev[offset];
688
if (os_dev[offset] == '-' && os_dev[offset + 1] == '-')
695
case GRUB_DEV_ABSTRACTION_RAID:
697
if (os_dev[7] == '_' && os_dev[8] == 'd')
699
/* This a partitionable RAID device of the form /dev/md_dNNpMM. */
703
p = strdup (os_dev + sizeof ("/dev/md_d") - 1);
709
grub_dev = xasprintf ("md%s", p);
712
else if (os_dev[7] == '/' && os_dev[8] == 'd')
714
/* This a partitionable RAID device of the form /dev/md/dNNpMM. */
718
p = strdup (os_dev + sizeof ("/dev/md/d") - 1);
724
grub_dev = xasprintf ("md%s", p);
727
else if (os_dev[7] >= '0' && os_dev[7] <= '9')
731
p = strdup (os_dev + sizeof ("/dev/md") - 1);
737
grub_dev = xasprintf ("md%s", p);
740
else if (os_dev[7] == '/' && os_dev[8] >= '0' && os_dev[8] <= '9')
744
p = strdup (os_dev + sizeof ("/dev/md/") - 1);
750
grub_dev = xasprintf ("md%s", p);
753
else if (os_dev[7] == '/')
755
/* mdraid 1.x with a free name. */
758
p = strdup (os_dev + sizeof ("/dev/md/") - 1);
764
grub_dev = xasprintf ("md/%s", p);
768
grub_util_error ("unknown kind of RAID device `%s'", os_dev);
772
char *mdadm_name = get_mdadm_name (os_dev);
777
grub_dev = xasprintf ("md/%s", mdadm_name);
781
#endif /* __linux__ */
785
default: /* GRUB_DEV_ABSTRACTION_NONE */
786
grub_dev = grub_util_biosdisk_get_grub_dev (os_dev);
793
grub_util_check_block_device (const char *blk_dev)
797
if (stat (blk_dev, &st) < 0)
798
grub_util_error ("cannot stat `%s'", blk_dev);
800
if (S_ISBLK (st.st_mode))
807
grub_util_check_char_device (const char *blk_dev)
811
if (stat (blk_dev, &st) < 0)
812
grub_util_error ("cannot stat `%s'", blk_dev);
814
if (S_ISCHR (st.st_mode))