2
* GRUB -- GRand Unified Bootloader
3
* Copyright (C) 1999,2000,2001,2002,2003,2006,2007,2008,2009,2010,2011 Free Software Foundation, Inc.
5
* GRUB is free software: you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation, either version 3 of the License, or
8
* (at your option) any later version.
10
* GRUB is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
19
#include <config-util.h>
23
#include <sys/types.h>
37
#include <grub/util/misc.h>
38
#include <grub/emu/exec.h>
40
#include <grub/cryptodisk.h>
41
#include <grub/i18n.h>
43
#if !defined (__MINGW32__) && !defined (__CYGWIN__) && !defined (__AROS__) && !defined (__HAIKU__)
46
#include <sys/ioctl.h> /* ioctl */
47
#include <sys/mount.h>
51
# endif /* ! MINORBITS */
52
# define MAJOR(dev) ((unsigned) ((dev) >> MINORBITS))
55
# define FLOPPY_MAJOR 2
56
#endif /* ! FLOPPY_MAJOR */
59
#include <sys/types.h>
61
#if defined(HAVE_LIBZFS) && defined(HAVE_LIBNVPAIR)
62
# include <grub/util/libzfs.h>
63
# include <grub/util/libnvpair.h>
67
#include <grub/misc.h>
68
#include <grub/emu/misc.h>
69
#include <grub/emu/hostdisk.h>
70
#include <grub/emu/getroot.h>
72
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
73
# define MAJOR(dev) major(dev)
74
# define FLOPPY_MAJOR 2
77
#if defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
78
#include <sys/mount.h>
81
#if defined(__NetBSD__) || defined(__OpenBSD__)
82
# include <sys/ioctl.h>
83
# include <sys/disklabel.h> /* struct disklabel */
84
# include <sys/disk.h> /* struct dkwedge_info */
85
#include <sys/param.h>
86
#include <sys/mount.h>
87
#endif /* defined(__NetBSD__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) */
89
#if defined(__NetBSD__) || defined(__OpenBSD__)
90
# define MAJOR(dev) major(dev)
91
# ifdef HAVE_GETRAWPARTITION
92
# include <util.h> /* getrawpartition */
93
# endif /* HAVE_GETRAWPARTITION */
94
#if defined(__NetBSD__)
95
# include <sys/fdio.h>
98
# define FLOPPY_MAJOR 2
99
# endif /* ! FLOPPY_MAJOR */
100
# ifndef RAW_FLOPPY_MAJOR
101
# define RAW_FLOPPY_MAJOR 9
102
# endif /* ! RAW_FLOPPY_MAJOR */
103
#endif /* defined(__NetBSD__) */
105
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
106
#define LVM_DEV_MAPPER_STRING "/dev/linux_lvm/"
108
#define LVM_DEV_MAPPER_STRING "/dev/mapper/"
111
#include <sys/types.h>
112
#include <sys/wait.h>
114
#if defined(HAVE_STRUCT_STATFS_F_FSTYPENAME) && defined(HAVE_STRUCT_STATFS_F_MNTFROMNAME)
115
#include <sys/param.h>
116
#include <sys/mount.h>
119
#if !defined (__GNU__)
121
strip_extra_slashes (char *dir)
125
while ((p = strchr (p, '/')) != 0)
129
memmove (p, p + 1, strlen (p));
132
else if (p[1] == '\0')
149
path = xmalloc (size);
150
while (! getcwd (path, size))
153
path = xrealloc (path, size);
160
grub_util_find_root_devices_from_poolname (char *poolname)
164
size_t devices_allocated = 0;
166
#if defined(HAVE_LIBZFS) && defined(HAVE_LIBNVPAIR)
167
zpool_handle_t *zpool;
168
libzfs_handle_t *libzfs;
169
nvlist_t *config, *vdev_tree;
171
unsigned int nvlist_count;
175
libzfs = grub_get_libzfs_handle ();
179
zpool = zpool_open (libzfs, poolname);
180
config = zpool_get_config (zpool, NULL);
182
if (nvlist_lookup_nvlist (config, "vdev_tree", &vdev_tree) != 0)
183
error (1, errno, "nvlist_lookup_nvlist (\"vdev_tree\")");
185
if (nvlist_lookup_nvlist_array (vdev_tree, "children", &children, &nvlist_count) != 0)
186
error (1, errno, "nvlist_lookup_nvlist_array (\"children\")");
187
assert (nvlist_count > 0);
189
while (nvlist_lookup_nvlist_array (children[0], "children",
190
&children, &nvlist_count) == 0)
191
assert (nvlist_count > 0);
193
for (i = 0; i < nvlist_count; i++)
195
if (nvlist_lookup_string (children[i], "path", &device) != 0)
196
error (1, errno, "nvlist_lookup_string (\"path\")");
199
if (stat (device, &st) == 0)
202
if (grub_memcmp (device, "/dev/dsk/", sizeof ("/dev/dsk/") - 1)
204
device = xasprintf ("/dev/rdsk/%s",
205
device + sizeof ("/dev/dsk/") - 1);
206
else if (grub_memcmp (device, "/devices", sizeof ("/devices") - 1)
208
&& grub_memcmp (device + strlen (device) - 4,
210
device = xasprintf ("%s,raw", device);
213
device = xstrdup (device);
214
if (ndevices >= devices_allocated)
216
devices_allocated = 2 * (devices_allocated + 8);
217
devices = xrealloc (devices, sizeof (devices[0])
218
* devices_allocated);
220
devices[ndevices++] = device;
234
char name[PATH_MAX + 1], state[257], readlen[257], writelen[257];
235
char cksum[257], notes[257];
246
pid = grub_util_exec_pipe (argv, &fd);
250
fp = fdopen (fd, "r");
253
grub_util_warn (_("Unable to open stream from %s: %s"),
254
"zpool", strerror (errno));
262
ret = getline (&line, &len, fp);
266
if (sscanf (line, " %s %256s %256s %256s %256s %256s",
267
name, state, readlen, writelen, cksum, notes) >= 5)
271
if (!strcmp (name, "NAME")
272
&& !strcmp (state, "STATE")
273
&& !strcmp (readlen, "READ")
274
&& !strcmp (writelen, "WRITE")
275
&& !strcmp (cksum, "CKSUM"))
283
if (strncmp (ptr, poolname, strlen (poolname)) == 0
284
&& grub_isspace(ptr[strlen (poolname)]))
286
if (!grub_isspace (*ptr))
293
if (strcmp (name, "mirror") && !sscanf (name, "mirror-%u", &dummy)
294
&& !sscanf (name, "raidz%u", &dummy)
295
&& !sscanf (name, "raidz1%u", &dummy)
296
&& !sscanf (name, "raidz2%u", &dummy)
297
&& !sscanf (name, "raidz3%u", &dummy)
298
&& !strcmp (state, "ONLINE"))
300
if (ndevices >= devices_allocated)
302
devices_allocated = 2 * (devices_allocated + 8);
303
devices = xrealloc (devices, sizeof (devices[0])
304
* devices_allocated);
307
devices[ndevices++] = xstrdup (name);
309
devices[ndevices++] = xasprintf ("/dev/%s", name);
319
waitpid (pid, NULL, 0);
323
if (ndevices >= devices_allocated)
325
devices_allocated = 2 * (devices_allocated + 8);
326
devices = xrealloc (devices, sizeof (devices[0])
327
* devices_allocated);
329
devices[ndevices++] = 0;
335
find_root_devices_from_libzfs (const char *dir)
337
char **devices = NULL;
341
grub_find_zpool_from_dir (dir, &poolname, &poolfs);
345
devices = grub_util_find_root_devices_from_poolname (poolname);
355
grub_find_device (const char *dir, dev_t dev)
368
saved_cwd = xgetcwd ();
370
grub_util_info ("changing current directory to %s", dir);
378
while ((ent = readdir (dp)) != 0)
383
- dotfiles (like "/dev/.tmp.md0") since they could be duplicates.
384
- dotdirs (like "/dev/.static") since they could contain duplicates. */
385
if (ent->d_name[0] == '.')
388
if (lstat (ent->d_name, &st) < 0)
389
/* Ignore any error. */
392
if (S_ISLNK (st.st_mode)) {
394
if (strcmp (dir, "mapper") == 0 || strcmp (dir, "/dev/mapper") == 0) {
395
/* Follow symbolic links under /dev/mapper/; the canonical name
396
may be something like /dev/dm-0, but the names under
397
/dev/mapper/ are more human-readable and so we prefer them if
399
if (stat (ent->d_name, &st) < 0)
402
#endif /* __linux__ */
403
/* Don't follow other symbolic links. */
407
if (S_ISDIR (st.st_mode))
409
/* Find it recursively. */
412
res = grub_find_device (ent->d_name, dev);
416
if (chdir (saved_cwd) < 0)
417
grub_util_error ("%s",
418
_("cannot restore the original directory"));
426
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__APPLE__)
427
if (S_ISCHR (st.st_mode) && st.st_rdev == dev)
429
if (S_ISBLK (st.st_mode) && st.st_rdev == dev)
433
/* Skip device names like /dev/dm-0, which are short-hand aliases
434
to more descriptive device names, e.g. those under /dev/mapper */
435
if (ent->d_name[0] == 'd' &&
436
ent->d_name[1] == 'm' &&
437
ent->d_name[2] == '-' &&
438
ent->d_name[3] >= '0' &&
439
ent->d_name[3] <= '9')
448
res = xmalloc (strlen (cwd) + strlen (ent->d_name) + 3);
450
#if defined(__NetBSD__) || defined(__OpenBSD__)
451
/* Convert this block device to its character (raw) device. */
454
/* Keep the device name as it is. */
458
strip_extra_slashes (res);
461
/* /dev/root is not a real block device keep looking, takes care
462
of situation where root filesystem is on the same partition as
465
if (strcmp(res, "/dev/root") == 0)
471
if (chdir (saved_cwd) < 0)
472
grub_util_error ("%s", _("cannot restore the original directory"));
480
if (chdir (saved_cwd) < 0)
481
grub_util_error ("%s", _("cannot restore the original directory"));
489
grub_guess_root_devices (const char *dir_in)
491
char **os_dev = NULL;
494
char *dir = canonicalize_file_name (dir_in);
497
grub_util_error (_("failed to get canonical path of `%s'"), dir_in);
501
os_dev = grub_find_root_devices_from_mountinfo (dir, NULL);
502
#endif /* __linux__ */
505
os_dev = find_root_devices_from_libzfs (dir);
510
for (cur = os_dev; *cur; cur++)
514
if (strcmp (*cur, "/dev/root") == 0
515
|| strncmp (*cur, "/dev/dm-", sizeof ("/dev/dm-") - 1) == 0)
519
*cur = canonicalize_file_name (tmp);
521
grub_util_error (_("failed to get canonical path of `%s'"), tmp);
524
root = (strcmp (*cur, "/dev/root") == 0);
525
dm = (strncmp (*cur, "/dev/dm-", sizeof ("/dev/dm-") - 1) == 0);
528
if (stat (*cur, &st) < 0)
532
*cur = grub_find_device (dm ? "/dev/mapper" : "/dev", dev);
536
for (cur = os_dev; *cur; cur++)
542
if (stat (dir, &st) < 0)
543
grub_util_error (_("cannot stat `%s': %s"), dir, strerror (errno));
547
os_dev = xmalloc (2 * sizeof (os_dev[0]));
549
/* This might be truly slow, but is there any better way? */
550
os_dev[0] = grub_find_device ("/dev", dev);
566
grub_util_pull_lvm_by_command (const char *os_dev)
578
grub_size_t vgidlen = 0;
580
vgid = grub_util_get_vg_uuid (os_dev);
582
vgidlen = grub_strlen (vgid);
586
if (strncmp (os_dev, LVM_DEV_MAPPER_STRING,
587
sizeof (LVM_DEV_MAPPER_STRING) - 1)
591
vgname = xmalloc (strlen (os_dev + sizeof (LVM_DEV_MAPPER_STRING) - 1) + 1);
592
for (iptr = os_dev + sizeof (LVM_DEV_MAPPER_STRING) - 1, optr = vgname; *iptr; )
595
else if (iptr[0] == '-' && iptr[1] == '-')
605
/* by default PV name is left aligned in 10 character field, meaning that
606
we do not know where name ends. Using dummy --separator disables
607
alignment. We have a single field, so separator itself is not output */
609
argv[1] = "--options";
611
argv[2] = "vg_uuid,pv_name";
614
argv[3] = "--noheadings";
615
argv[4] = "--separator";
620
pid = grub_util_exec_pipe (argv, &fd);
626
/* Parent. Read vgs' output. */
627
vgs = fdopen (fd, "r");
630
grub_util_warn (_("Unable to open stream from %s: %s"),
631
"vgs", strerror (errno));
635
while (getline (&buf, &len, vgs) > 0)
638
/* LVM adds two spaces as standard prefix */
639
for (ptr = buf; ptr < buf + 2 && *ptr == ' '; ptr++);
641
if (vgid && (grub_strncmp (vgid, ptr, vgidlen) != 0
642
|| ptr[vgidlen] != ':'))
648
*(ptr + strlen (ptr) - 1) = '\0';
649
grub_util_pull_device (ptr);
654
waitpid (pid, NULL, 0);
658
/* ZFS has similar problems to those of btrfs (see above). */
660
grub_find_zpool_from_dir (const char *dir, char **poolname, char **poolfs)
664
*poolname = *poolfs = NULL;
666
#if defined(HAVE_STRUCT_STATFS_F_FSTYPENAME) && defined(HAVE_STRUCT_STATFS_F_MNTFROMNAME)
667
/* FreeBSD and GNU/kFreeBSD. */
671
if (statfs (dir, &mnt) != 0)
674
if (strcmp (mnt.f_fstypename, "zfs") != 0)
677
*poolname = xstrdup (mnt.f_mntfromname);
679
#elif defined(HAVE_GETEXTMNTENT)
683
struct extmnttab mnt;
685
if (stat (dir, &st) != 0)
688
FILE *mnttab = grub_util_fopen ("/etc/mnttab", "r");
692
while (getextmntent (mnttab, &mnt, sizeof (mnt)) == 0)
694
if (makedev (mnt.mnt_major, mnt.mnt_minor) == st.st_dev
695
&& !strcmp (mnt.mnt_fstype, "zfs"))
697
*poolname = xstrdup (mnt.mnt_special);
709
slash = strchr (*poolname, '/');
713
*poolfs = xstrdup (slash + 1);
716
*poolfs = xstrdup ("");
720
grub_util_biosdisk_is_floppy (grub_disk_t disk)
726
dname = grub_util_biosdisk_get_osdev (disk);
731
fd = open (dname, O_RDONLY);
732
/* Shouldn't happen. */
736
/* Shouldn't happen either. */
737
if (fstat (fd, &st) < 0)
745
#if defined(__NetBSD__)
746
if (major(st.st_rdev) == RAW_FLOPPY_MAJOR)
750
#if defined(FLOPPY_MAJOR)
751
if (major(st.st_rdev) == FLOPPY_MAJOR)
753
/* Some kernels (e.g. kFreeBSD) don't have a static major number
754
for floppies, but they still use a "fd[0-9]" pathname. */
767
#include <grub/emu/getroot.h>
770
grub_util_pull_lvm_by_command (const char *os_dev __attribute__ ((unused)))