2003-11-17 18:07:09 +00:00
|
|
|
/* getroot.c - Get root device */
|
|
|
|
/*
|
2004-04-04 13:46:03 +00:00
|
|
|
* GRUB -- GRand Unified Bootloader
|
2010-07-20 10:10:49 +00:00
|
|
|
* Copyright (C) 1999,2000,2001,2002,2003,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
|
2003-11-17 18:07:09 +00:00
|
|
|
*
|
2007-07-21 23:32:33 +00:00
|
|
|
* GRUB is free software: you can redistribute it and/or modify
|
2003-11-17 18:07:09 +00:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
2007-07-21 23:32:33 +00:00
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
2003-11-17 18:07:09 +00:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2007-07-21 23:32:33 +00:00
|
|
|
* GRUB is distributed in the hope that it will be useful,
|
2003-11-17 18:07:09 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2007-07-21 23:32:33 +00:00
|
|
|
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
|
2003-11-17 18:07:09 +00:00
|
|
|
*/
|
|
|
|
|
2008-09-04 19:54:59 +00:00
|
|
|
#include <config.h>
|
2003-11-17 18:07:09 +00:00
|
|
|
#include <sys/stat.h>
|
2010-05-06 03:15:39 +00:00
|
|
|
#include <sys/types.h>
|
2010-07-30 19:43:12 +00:00
|
|
|
#include <assert.h>
|
2010-05-06 03:15:39 +00:00
|
|
|
#include <fcntl.h>
|
2003-11-17 18:07:09 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <dirent.h>
|
2010-05-04 03:39:03 +00:00
|
|
|
#include <errno.h>
|
2010-07-30 19:43:12 +00:00
|
|
|
#include <error.h>
|
2010-04-27 05:20:28 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2010-05-04 03:39:03 +00:00
|
|
|
#include <stdint.h>
|
2010-08-01 12:47:14 +00:00
|
|
|
#include <grub/util/misc.h>
|
2008-05-16 21:39:52 +00:00
|
|
|
|
2009-11-21 17:00:23 +00:00
|
|
|
#ifdef __GNU__
|
|
|
|
#include <hurd.h>
|
|
|
|
#include <hurd/lookup.h>
|
|
|
|
#include <hurd/fs.h>
|
2010-07-04 21:00:45 +00:00
|
|
|
#include <sys/mman.h>
|
2009-11-21 17:00:23 +00:00
|
|
|
#endif
|
|
|
|
|
2010-07-18 14:53:14 +00:00
|
|
|
#ifdef __linux__
|
|
|
|
# include <sys/types.h>
|
|
|
|
# include <sys/wait.h>
|
|
|
|
#endif
|
|
|
|
|
2010-07-30 19:43:12 +00:00
|
|
|
#if defined(HAVE_LIBZFS) && defined(HAVE_LIBNVPAIR)
|
|
|
|
# include <grub/util/libzfs.h>
|
|
|
|
# include <grub/util/libnvpair.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_GETFSSTAT
|
|
|
|
# include <sys/mount.h>
|
|
|
|
#endif
|
|
|
|
|
2010-04-27 05:20:28 +00:00
|
|
|
#include <grub/mm.h>
|
|
|
|
#include <grub/misc.h>
|
|
|
|
#include <grub/emu/misc.h>
|
|
|
|
#include <grub/emu/hostdisk.h>
|
|
|
|
#include <grub/emu/getroot.h>
|
2003-11-17 18:07:09 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
strip_extra_slashes (char *dir)
|
|
|
|
{
|
|
|
|
char *p = dir;
|
|
|
|
|
|
|
|
while ((p = strchr (p, '/')) != 0)
|
|
|
|
{
|
|
|
|
if (p[1] == '/')
|
|
|
|
{
|
|
|
|
memmove (p, p + 1, strlen (p));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (p[1] == '\0')
|
|
|
|
{
|
2008-05-16 21:39:52 +00:00
|
|
|
if (p > dir)
|
|
|
|
p[0] = '\0';
|
2003-11-17 18:07:09 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2003-11-17 18:07:09 +00:00
|
|
|
p++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
xgetcwd (void)
|
|
|
|
{
|
|
|
|
size_t size = 10;
|
|
|
|
char *path;
|
|
|
|
|
|
|
|
path = xmalloc (size);
|
|
|
|
while (! getcwd (path, size))
|
|
|
|
{
|
|
|
|
size <<= 1;
|
|
|
|
path = xrealloc (path, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2010-07-30 19:43:12 +00:00
|
|
|
static char *
|
|
|
|
find_mount_point_from_dir (const char *dir)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
typeof (st.st_dev) fs;
|
|
|
|
char *prev, *next, *slash, *statdir;
|
|
|
|
|
|
|
|
if (stat (dir, &st) == -1)
|
|
|
|
error (1, errno, "stat (%s)", dir);
|
|
|
|
|
|
|
|
fs = st.st_dev;
|
|
|
|
|
|
|
|
prev = xstrdup (dir);
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
/* Remove last slash. */
|
|
|
|
next = xstrdup (prev);
|
|
|
|
slash = strrchr (next, '/');
|
|
|
|
if (! slash)
|
|
|
|
{
|
|
|
|
free (next);
|
|
|
|
free (prev);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
*slash = '\0';
|
|
|
|
|
|
|
|
/* A next empty string counts as /. */
|
|
|
|
if (next[0] == '\0')
|
|
|
|
statdir = "/";
|
|
|
|
else
|
|
|
|
statdir = next;
|
|
|
|
|
|
|
|
if (stat (statdir, &st) == -1)
|
|
|
|
error (1, errno, "stat (%s)", next);
|
|
|
|
|
|
|
|
if (st.st_dev != fs)
|
|
|
|
{
|
|
|
|
/* Found mount point. */
|
|
|
|
free (next);
|
|
|
|
return prev;
|
|
|
|
}
|
|
|
|
|
|
|
|
free (prev);
|
|
|
|
prev = next;
|
|
|
|
|
|
|
|
/* We've already seen an empty string, which means we
|
|
|
|
reached /. Nothing left to do. */
|
|
|
|
if (prev[0] == '\0')
|
|
|
|
{
|
|
|
|
free (prev);
|
|
|
|
return xstrdup ("/");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-18 12:01:59 +00:00
|
|
|
#ifdef __linux__
|
2008-05-16 21:39:52 +00:00
|
|
|
|
2010-05-18 12:01:59 +00:00
|
|
|
/* Statting something on a btrfs filesystem always returns a virtual device
|
|
|
|
major/minor pair rather than the real underlying device, because btrfs
|
|
|
|
can span multiple underlying devices (and even if it's currently only
|
|
|
|
using a single device it can be dynamically extended onto another). We
|
|
|
|
can't deal with the multiple-device case yet, but in the meantime, we can
|
|
|
|
at least cope with the single-device case by scanning
|
|
|
|
/proc/self/mountinfo. */
|
|
|
|
static char *
|
|
|
|
find_root_device_from_mountinfo (const char *dir)
|
2003-11-17 18:07:09 +00:00
|
|
|
{
|
2010-05-18 12:01:59 +00:00
|
|
|
FILE *fp;
|
2010-06-01 17:05:29 +00:00
|
|
|
char *buf = NULL;
|
|
|
|
size_t len = 0;
|
2010-05-18 12:01:59 +00:00
|
|
|
char *ret = NULL;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2010-05-18 12:01:59 +00:00
|
|
|
fp = fopen ("/proc/self/mountinfo", "r");
|
|
|
|
if (! fp)
|
|
|
|
return NULL; /* fall through to other methods */
|
2003-11-17 18:07:09 +00:00
|
|
|
|
2010-06-01 17:05:29 +00:00
|
|
|
while (getline (&buf, &len, fp) > 0)
|
2010-05-18 12:01:59 +00:00
|
|
|
{
|
|
|
|
int mnt_id, parent_mnt_id;
|
|
|
|
unsigned int major, minor;
|
|
|
|
char enc_root[PATH_MAX], enc_path[PATH_MAX];
|
|
|
|
int count;
|
|
|
|
size_t enc_path_len;
|
|
|
|
const char *sep;
|
|
|
|
char fstype[PATH_MAX], device[PATH_MAX];
|
|
|
|
struct stat st;
|
2003-11-17 18:07:09 +00:00
|
|
|
|
2010-05-18 12:01:59 +00:00
|
|
|
if (sscanf (buf, "%d %d %u:%u %s %s%n",
|
|
|
|
&mnt_id, &parent_mnt_id, &major, &minor, enc_root, enc_path,
|
|
|
|
&count) < 6)
|
|
|
|
continue;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2010-05-18 12:01:59 +00:00
|
|
|
if (strcmp (enc_root, "/") != 0)
|
|
|
|
continue; /* only a subtree is mounted */
|
2003-11-17 18:07:09 +00:00
|
|
|
|
2010-05-18 12:01:59 +00:00
|
|
|
enc_path_len = strlen (enc_path);
|
|
|
|
if (strncmp (dir, enc_path, enc_path_len) != 0 ||
|
|
|
|
(dir[enc_path_len] && dir[enc_path_len] != '/'))
|
|
|
|
continue;
|
2003-11-17 18:07:09 +00:00
|
|
|
|
2010-05-18 12:01:59 +00:00
|
|
|
/* This is a parent of the requested directory. /proc/self/mountinfo
|
|
|
|
is in mount order, so it must be the closest parent we've
|
|
|
|
encountered so far. If it's virtual, return its device node;
|
|
|
|
otherwise, carry on to try to find something closer. */
|
2003-11-17 18:07:09 +00:00
|
|
|
|
2010-05-18 12:01:59 +00:00
|
|
|
free (ret);
|
|
|
|
ret = NULL;
|
2003-11-17 18:07:09 +00:00
|
|
|
|
2010-05-18 12:01:59 +00:00
|
|
|
if (major != 0)
|
|
|
|
continue; /* not a virtual device */
|
2003-11-17 18:07:09 +00:00
|
|
|
|
2010-05-18 12:01:59 +00:00
|
|
|
sep = strstr (buf + count, " - ");
|
|
|
|
if (!sep)
|
|
|
|
continue;
|
2003-11-17 18:07:09 +00:00
|
|
|
|
2010-06-01 17:05:29 +00:00
|
|
|
sep += sizeof (" - ") - 1;
|
2010-05-18 12:01:59 +00:00
|
|
|
if (sscanf (sep, "%s %s", fstype, device) != 2)
|
|
|
|
continue;
|
2003-11-17 18:07:09 +00:00
|
|
|
|
2010-05-18 12:01:59 +00:00
|
|
|
if (stat (device, &st) < 0)
|
|
|
|
continue;
|
2003-11-17 18:07:09 +00:00
|
|
|
|
2010-05-18 12:01:59 +00:00
|
|
|
if (!S_ISBLK (st.st_mode))
|
|
|
|
continue; /* not a block device */
|
2003-11-17 18:07:09 +00:00
|
|
|
|
2010-05-18 12:01:59 +00:00
|
|
|
ret = strdup (device);
|
2008-05-16 21:39:52 +00:00
|
|
|
}
|
2003-11-17 18:07:09 +00:00
|
|
|
|
2010-06-01 17:05:29 +00:00
|
|
|
free (buf);
|
2010-05-18 12:01:59 +00:00
|
|
|
fclose (fp);
|
|
|
|
return ret;
|
2003-11-17 18:07:09 +00:00
|
|
|
}
|
|
|
|
|
2010-05-18 12:01:59 +00:00
|
|
|
#endif /* __linux__ */
|
|
|
|
|
2010-07-30 19:43:12 +00:00
|
|
|
#if defined(HAVE_LIBZFS) && defined(HAVE_LIBNVPAIR)
|
|
|
|
|
|
|
|
/* ZFS has similar problems to those of btrfs (see above). */
|
|
|
|
static char *
|
|
|
|
find_root_device_from_libzfs (const char *dir)
|
|
|
|
{
|
|
|
|
char *device = NULL;
|
|
|
|
char *poolname = NULL;
|
|
|
|
char *poolfs = NULL;
|
|
|
|
char *mnt_point;
|
|
|
|
char *slash;
|
|
|
|
|
|
|
|
mnt_point = find_mount_point_from_dir (dir);
|
|
|
|
|
|
|
|
#ifdef HAVE_GETFSSTAT
|
|
|
|
{
|
|
|
|
int mnt_count = getfsstat (NULL, 0, MNT_WAIT);
|
|
|
|
if (mnt_count == -1)
|
|
|
|
error (1, errno, "getfsstat");
|
|
|
|
|
|
|
|
struct statfs *mnt = xmalloc (mnt_count * sizeof (*mnt));
|
|
|
|
|
|
|
|
mnt_count = getfsstat (mnt, mnt_count * sizeof (*mnt), MNT_WAIT);
|
|
|
|
if (mnt_count == -1)
|
|
|
|
error (1, errno, "getfsstat");
|
|
|
|
|
|
|
|
unsigned int i;
|
|
|
|
for (i = 0; i < (unsigned) mnt_count; i++)
|
|
|
|
if (!strcmp (mnt[i].f_fstypename, "zfs")
|
|
|
|
&& !strcmp (mnt[i].f_mntonname, mnt_point))
|
|
|
|
{
|
|
|
|
poolname = xstrdup (mnt[i].f_mntfromname);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
free (mnt);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (! poolname)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
slash = strchr (poolname, '/');
|
|
|
|
if (slash)
|
|
|
|
{
|
|
|
|
*slash = '\0';
|
|
|
|
poolfs = slash + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
zpool_handle_t *zpool;
|
|
|
|
nvlist_t *nvlist;
|
|
|
|
nvlist_t **nvlist_array;
|
|
|
|
unsigned int nvlist_count;
|
|
|
|
|
2010-08-01 13:23:44 +00:00
|
|
|
grub_util_init_libzfs ();
|
|
|
|
|
2010-07-30 19:43:12 +00:00
|
|
|
zpool = zpool_open (libzfs_handle, poolname);
|
|
|
|
nvlist = zpool_get_config (zpool, NULL);
|
|
|
|
|
|
|
|
if (nvlist_lookup_nvlist (nvlist, "vdev_tree", &nvlist) != 0)
|
|
|
|
error (1, errno, "nvlist_lookup_nvlist (\"vdev_tree\")");
|
|
|
|
|
|
|
|
if (nvlist_lookup_nvlist_array (nvlist, "children", &nvlist_array, &nvlist_count) != 0)
|
|
|
|
error (1, errno, "nvlist_lookup_nvlist_array (\"children\")");
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
assert (nvlist_count > 0);
|
|
|
|
} while (nvlist_lookup_nvlist_array (nvlist_array[0], "children",
|
|
|
|
&nvlist_array, &nvlist_count) == 0);
|
|
|
|
|
|
|
|
if (nvlist_lookup_string (nvlist_array[0], "path", &device) != 0)
|
|
|
|
error (1, errno, "nvlist_lookup_string (\"path\")");
|
|
|
|
|
|
|
|
zpool_close (zpool);
|
|
|
|
}
|
|
|
|
|
|
|
|
free (poolname);
|
|
|
|
|
|
|
|
return device;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-08-29 19:55:23 +00:00
|
|
|
#ifdef __MINGW32__
|
|
|
|
|
|
|
|
static char *
|
|
|
|
find_root_device (const char *dir __attribute__ ((unused)),
|
|
|
|
dev_t dev __attribute__ ((unused)))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif ! defined(__CYGWIN__)
|
2008-05-16 21:39:52 +00:00
|
|
|
|
2003-11-17 18:07:09 +00:00
|
|
|
static char *
|
|
|
|
find_root_device (const char *dir, dev_t dev)
|
|
|
|
{
|
|
|
|
DIR *dp;
|
|
|
|
char *saved_cwd;
|
|
|
|
struct dirent *ent;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2003-11-17 18:07:09 +00:00
|
|
|
dp = opendir (dir);
|
|
|
|
if (! dp)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
saved_cwd = xgetcwd ();
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_util_info ("changing current directory to %s", dir);
|
2003-11-17 18:07:09 +00:00
|
|
|
if (chdir (dir) < 0)
|
|
|
|
{
|
|
|
|
free (saved_cwd);
|
|
|
|
closedir (dp);
|
|
|
|
return 0;
|
|
|
|
}
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2003-11-17 18:07:09 +00:00
|
|
|
while ((ent = readdir (dp)) != 0)
|
|
|
|
{
|
|
|
|
struct stat st;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2008-08-23 12:19:43 +00:00
|
|
|
/* Avoid:
|
|
|
|
- dotfiles (like "/dev/.tmp.md0") since they could be duplicates.
|
|
|
|
- dotdirs (like "/dev/.static") since they could contain duplicates. */
|
|
|
|
if (ent->d_name[0] == '.')
|
2003-11-17 18:07:09 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (lstat (ent->d_name, &st) < 0)
|
|
|
|
/* Ignore any error. */
|
|
|
|
continue;
|
|
|
|
|
2010-05-28 10:51:50 +00:00
|
|
|
if (S_ISLNK (st.st_mode)) {
|
|
|
|
#ifdef __linux__
|
|
|
|
if (strcmp (dir, "mapper") == 0) {
|
|
|
|
/* Follow symbolic links under /dev/mapper/; the canonical name
|
|
|
|
may be something like /dev/dm-0, but the names under
|
|
|
|
/dev/mapper/ are more human-readable and so we prefer them if
|
|
|
|
we can get them. */
|
|
|
|
if (stat (ent->d_name, &st) < 0)
|
|
|
|
continue;
|
|
|
|
} else
|
|
|
|
#endif /* __linux__ */
|
|
|
|
/* Don't follow other symbolic links. */
|
2003-11-17 18:07:09 +00:00
|
|
|
continue;
|
2010-05-28 10:51:50 +00:00
|
|
|
}
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2008-08-23 12:19:43 +00:00
|
|
|
if (S_ISDIR (st.st_mode))
|
2003-11-17 18:07:09 +00:00
|
|
|
{
|
2008-08-23 12:19:43 +00:00
|
|
|
/* Find it recursively. */
|
2003-11-17 18:07:09 +00:00
|
|
|
char *res;
|
|
|
|
|
|
|
|
res = find_root_device (ent->d_name, dev);
|
|
|
|
|
|
|
|
if (res)
|
|
|
|
{
|
|
|
|
if (chdir (saved_cwd) < 0)
|
2010-01-16 00:26:52 +00:00
|
|
|
grub_util_error ("cannot restore the original directory");
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2003-11-17 18:07:09 +00:00
|
|
|
free (saved_cwd);
|
|
|
|
closedir (dp);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-20 20:03:18 +00:00
|
|
|
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__APPLE__)
|
2009-04-11 09:40:39 +00:00
|
|
|
if (S_ISCHR (st.st_mode) && st.st_rdev == dev)
|
|
|
|
#else
|
2003-11-17 18:07:09 +00:00
|
|
|
if (S_ISBLK (st.st_mode) && st.st_rdev == dev)
|
2009-04-11 09:40:39 +00:00
|
|
|
#endif
|
2003-11-17 18:07:09 +00:00
|
|
|
{
|
2008-07-25 19:05:06 +00:00
|
|
|
#ifdef __linux__
|
2008-07-27 05:57:10 +00:00
|
|
|
/* Skip device names like /dev/dm-0, which are short-hand aliases
|
|
|
|
to more descriptive device names, e.g. those under /dev/mapper */
|
2008-07-25 19:05:06 +00:00
|
|
|
if (ent->d_name[0] == 'd' &&
|
|
|
|
ent->d_name[1] == 'm' &&
|
|
|
|
ent->d_name[2] == '-' &&
|
|
|
|
ent->d_name[3] >= '0' &&
|
|
|
|
ent->d_name[3] <= '9')
|
|
|
|
continue;
|
|
|
|
#endif
|
|
|
|
|
2003-11-17 18:07:09 +00:00
|
|
|
/* Found! */
|
|
|
|
char *res;
|
|
|
|
char *cwd;
|
2010-04-10 15:07:33 +00:00
|
|
|
#if defined(__NetBSD__)
|
|
|
|
/* Convert this block device to its character (raw) device. */
|
|
|
|
const char *template = "%s/r%s";
|
|
|
|
#else
|
|
|
|
/* Keep the device name as it is. */
|
|
|
|
const char *template = "%s/%s";
|
|
|
|
#endif
|
2003-11-17 18:07:09 +00:00
|
|
|
|
|
|
|
cwd = xgetcwd ();
|
2010-04-10 15:07:33 +00:00
|
|
|
res = xmalloc (strlen (cwd) + strlen (ent->d_name) + 3);
|
|
|
|
sprintf (res, template, cwd, ent->d_name);
|
2003-11-17 18:07:09 +00:00
|
|
|
strip_extra_slashes (res);
|
|
|
|
free (cwd);
|
|
|
|
|
2007-04-10 22:00:24 +00:00
|
|
|
/* /dev/root is not a real block device keep looking, takes care
|
|
|
|
of situation where root filesystem is on the same partition as
|
|
|
|
grub files */
|
|
|
|
|
|
|
|
if (strcmp(res, "/dev/root") == 0)
|
|
|
|
continue;
|
|
|
|
|
2003-11-17 18:07:09 +00:00
|
|
|
if (chdir (saved_cwd) < 0)
|
2010-01-16 00:26:52 +00:00
|
|
|
grub_util_error ("cannot restore the original directory");
|
2003-11-17 18:07:09 +00:00
|
|
|
|
|
|
|
free (saved_cwd);
|
|
|
|
closedir (dp);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (chdir (saved_cwd) < 0)
|
2010-01-16 00:26:52 +00:00
|
|
|
grub_util_error ("cannot restore the original directory");
|
2003-11-17 18:07:09 +00:00
|
|
|
|
|
|
|
free (saved_cwd);
|
|
|
|
closedir (dp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-05-16 21:39:52 +00:00
|
|
|
#else /* __CYGWIN__ */
|
|
|
|
|
|
|
|
/* Read drive/partition serial number from mbr/boot sector,
|
|
|
|
return 0 on read error, ~0 on unknown serial. */
|
|
|
|
static unsigned
|
|
|
|
get_bootsec_serial (const char *os_dev, int mbr)
|
|
|
|
{
|
|
|
|
/* Read boot sector. */
|
|
|
|
int fd = open (os_dev, O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
return 0;
|
|
|
|
unsigned char buf[0x200];
|
|
|
|
int n = read (fd, buf, sizeof (buf));
|
|
|
|
close (fd);
|
|
|
|
if (n != sizeof(buf))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Check signature. */
|
|
|
|
if (!(buf[0x1fe] == 0x55 && buf[0x1ff] == 0xaa))
|
|
|
|
return ~0;
|
|
|
|
|
|
|
|
/* Serial number offset depends on boot sector type. */
|
|
|
|
if (mbr)
|
|
|
|
n = 0x1b8;
|
|
|
|
else if (memcmp (buf + 0x03, "NTFS", 4) == 0)
|
|
|
|
n = 0x048;
|
|
|
|
else if (memcmp (buf + 0x52, "FAT32", 5) == 0)
|
|
|
|
n = 0x043;
|
|
|
|
else if (memcmp (buf + 0x36, "FAT", 3) == 0)
|
|
|
|
n = 0x027;
|
|
|
|
else
|
|
|
|
return ~0;
|
|
|
|
|
|
|
|
unsigned serial = *(unsigned *)(buf + n);
|
|
|
|
if (serial == 0)
|
|
|
|
return ~0;
|
|
|
|
return serial;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
find_cygwin_root_device (const char *path, dev_t dev)
|
|
|
|
{
|
|
|
|
/* No root device for /cygdrive. */
|
|
|
|
if (dev == (DEV_CYGDRIVE_MAJOR << 16))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Convert to full POSIX and Win32 path. */
|
|
|
|
char fullpath[PATH_MAX], winpath[PATH_MAX];
|
|
|
|
cygwin_conv_to_full_posix_path (path, fullpath);
|
|
|
|
cygwin_conv_to_full_win32_path (fullpath, winpath);
|
|
|
|
|
|
|
|
/* If identical, this is no real filesystem path. */
|
|
|
|
if (strcmp (fullpath, winpath) == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Check for floppy drive letter. */
|
|
|
|
if (winpath[0] && winpath[1] == ':' && strchr ("AaBb", winpath[0]))
|
|
|
|
return xstrdup (strchr ("Aa", winpath[0]) ? "/dev/fd0" : "/dev/fd1");
|
|
|
|
|
|
|
|
/* Cygwin returns the partition serial number in stat.st_dev.
|
|
|
|
This is never identical to the device number of the emulated
|
|
|
|
/dev/sdXN device, so above find_root_device () does not work.
|
2009-05-04 20:06:05 +00:00
|
|
|
Search the partition with the same serial in boot sector instead. */
|
2008-05-16 21:39:52 +00:00
|
|
|
char devpath[sizeof ("/dev/sda15") + 13]; /* Size + Paranoia. */
|
|
|
|
int d;
|
|
|
|
for (d = 'a'; d <= 'z'; d++)
|
|
|
|
{
|
|
|
|
sprintf (devpath, "/dev/sd%c", d);
|
|
|
|
if (get_bootsec_serial (devpath, 1) == 0)
|
|
|
|
continue;
|
|
|
|
int p;
|
|
|
|
for (p = 1; p <= 15; p++)
|
|
|
|
{
|
|
|
|
sprintf (devpath, "/dev/sd%c%d", d, p);
|
|
|
|
unsigned ser = get_bootsec_serial (devpath, 0);
|
|
|
|
if (ser == 0)
|
|
|
|
break;
|
|
|
|
if (ser != (unsigned)~0 && dev == (dev_t)ser)
|
|
|
|
return xstrdup (devpath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* __CYGWIN__ */
|
|
|
|
|
2003-11-17 18:07:09 +00:00
|
|
|
char *
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_guess_root_device (const char *dir)
|
2003-11-17 18:07:09 +00:00
|
|
|
{
|
|
|
|
char *os_dev;
|
2009-11-21 17:00:23 +00:00
|
|
|
#ifdef __GNU__
|
|
|
|
file_t file;
|
|
|
|
mach_port_t *ports;
|
|
|
|
int *ints;
|
|
|
|
loff_t *offsets;
|
|
|
|
char *data;
|
|
|
|
error_t err;
|
|
|
|
mach_msg_type_number_t num_ports = 0, num_ints = 0, num_offsets = 0, data_len = 0;
|
|
|
|
size_t name_len;
|
|
|
|
|
|
|
|
file = file_name_lookup (dir, 0, 0);
|
|
|
|
if (file == MACH_PORT_NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err = file_get_storage_info (file,
|
|
|
|
&ports, &num_ports,
|
|
|
|
&ints, &num_ints,
|
|
|
|
&offsets, &num_offsets,
|
|
|
|
&data, &data_len);
|
|
|
|
|
|
|
|
if (num_ints < 1)
|
|
|
|
grub_util_error ("Storage info for `%s' does not include type", dir);
|
|
|
|
if (ints[0] != STORAGE_DEVICE)
|
|
|
|
grub_util_error ("Filesystem of `%s' is not stored on local disk", dir);
|
|
|
|
|
|
|
|
if (num_ints < 5)
|
|
|
|
grub_util_error ("Storage info for `%s' does not include name", dir);
|
|
|
|
name_len = ints[4];
|
|
|
|
if (name_len < data_len)
|
|
|
|
grub_util_error ("Bogus name length for storage info for `%s'", dir);
|
|
|
|
if (data[name_len - 1] != '\0')
|
|
|
|
grub_util_error ("Storage name for `%s' not NUL-terminated", dir);
|
|
|
|
|
|
|
|
os_dev = xmalloc (strlen ("/dev/") + data_len);
|
|
|
|
memcpy (os_dev, "/dev/", strlen ("/dev/"));
|
|
|
|
memcpy (os_dev + strlen ("/dev/"), data, data_len);
|
|
|
|
|
|
|
|
if (ports && num_ports > 0)
|
|
|
|
{
|
|
|
|
mach_msg_type_number_t i;
|
|
|
|
for (i = 0; i < num_ports; i++)
|
|
|
|
{
|
|
|
|
mach_port_t port = ports[i];
|
|
|
|
if (port != MACH_PORT_NULL)
|
|
|
|
mach_port_deallocate (mach_task_self(), port);
|
|
|
|
}
|
|
|
|
munmap ((caddr_t) ports, num_ports * sizeof (*ports));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ints && num_ints > 0)
|
|
|
|
munmap ((caddr_t) ints, num_ints * sizeof (*ints));
|
|
|
|
if (offsets && num_offsets > 0)
|
|
|
|
munmap ((caddr_t) offsets, num_offsets * sizeof (*offsets));
|
|
|
|
if (data && data_len > 0)
|
|
|
|
munmap (data, data_len);
|
|
|
|
mach_port_deallocate (mach_task_self (), file);
|
|
|
|
#else /* !__GNU__ */
|
|
|
|
struct stat st;
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2010-05-18 12:01:59 +00:00
|
|
|
#ifdef __linux__
|
|
|
|
os_dev = find_root_device_from_mountinfo (dir);
|
|
|
|
if (os_dev)
|
|
|
|
return os_dev;
|
|
|
|
#endif /* __linux__ */
|
|
|
|
|
2010-07-30 19:43:12 +00:00
|
|
|
#if defined(HAVE_LIBZFS) && defined(HAVE_LIBNVPAIR)
|
|
|
|
os_dev = find_root_device_from_libzfs (dir);
|
|
|
|
if (os_dev)
|
|
|
|
return os_dev;
|
|
|
|
#endif
|
|
|
|
|
2003-11-17 18:07:09 +00:00
|
|
|
if (stat (dir, &st) < 0)
|
2010-01-16 00:26:52 +00:00
|
|
|
grub_util_error ("cannot stat `%s'", dir);
|
2003-11-17 18:07:09 +00:00
|
|
|
|
2008-05-16 21:39:52 +00:00
|
|
|
#ifdef __CYGWIN__
|
|
|
|
/* Cygwin specific function. */
|
|
|
|
os_dev = find_cygwin_root_device (dir, st.st_dev);
|
2008-02-12 22:45:58 +00:00
|
|
|
|
2008-05-16 21:39:52 +00:00
|
|
|
#else
|
|
|
|
|
|
|
|
/* This might be truly slow, but is there any better way? */
|
|
|
|
os_dev = find_root_device ("/dev", st.st_dev);
|
2007-05-17 15:43:32 +00:00
|
|
|
#endif
|
2009-11-21 17:00:23 +00:00
|
|
|
#endif /* !__GNU__ */
|
2007-05-16 21:38:44 +00:00
|
|
|
|
|
|
|
return os_dev;
|
|
|
|
}
|
2010-02-07 01:47:18 +00:00
|
|
|
|
|
|
|
static int
|
2009-11-24 07:20:48 +00:00
|
|
|
grub_util_is_dmraid (const char *os_dev)
|
|
|
|
{
|
|
|
|
if (! strncmp (os_dev, "/dev/mapper/nvidia_", 19))
|
|
|
|
return 1;
|
|
|
|
else if (! strncmp (os_dev, "/dev/mapper/isw_", 16))
|
|
|
|
return 1;
|
|
|
|
else if (! strncmp (os_dev, "/dev/mapper/hpt37x_", 19))
|
|
|
|
return 1;
|
|
|
|
else if (! strncmp (os_dev, "/dev/mapper/hpt45x_", 19))
|
|
|
|
return 1;
|
|
|
|
else if (! strncmp (os_dev, "/dev/mapper/via_", 16))
|
|
|
|
return 1;
|
|
|
|
else if (! strncmp (os_dev, "/dev/mapper/lsi_", 16))
|
|
|
|
return 1;
|
|
|
|
else if (! strncmp (os_dev, "/dev/mapper/pdc_", 16))
|
|
|
|
return 1;
|
|
|
|
else if (! strncmp (os_dev, "/dev/mapper/jmicron_", 20))
|
|
|
|
return 1;
|
|
|
|
else if (! strncmp (os_dev, "/dev/mapper/asr_", 16))
|
|
|
|
return 1;
|
|
|
|
else if (! strncmp (os_dev, "/dev/mapper/sil_", 16))
|
|
|
|
return 1;
|
2009-12-23 16:41:32 +00:00
|
|
|
|
2009-11-24 07:20:48 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2007-05-16 21:38:44 +00:00
|
|
|
|
2008-01-12 15:11:57 +00:00
|
|
|
int
|
2009-12-20 13:09:16 +00:00
|
|
|
grub_util_get_dev_abstraction (const char *os_dev __attribute__((unused)))
|
2007-05-16 21:38:44 +00:00
|
|
|
{
|
2008-05-16 21:39:52 +00:00
|
|
|
#ifdef __linux__
|
2006-10-14 Yoshinori K. Okuji <okuji@enbug.org>
* DISTLIST: Added commands/echo.c, disk/lvm.c, disk/raid.c,
include/grub/bitmap.h, include/grub/lvm.h, include/grub/raid.h,
include/grub/i386/pc/vbeutil.h, include/grub/util/lvm.h,
include/grub/util/raid.h, util/lvm.c, util/raid.c, video/bitmap.c,
video/readers/tga.c and video/i386/pc/vbeutil.c.
2006-10-14 Jeroen Dekkers <jeroen@dekkers.cx>
Added support for RAID and LVM.
* disk/lvm.c: New file.
* disk/raid.c: Likewise.
* include/grub/lvm.h: Likewise.
* include/grub/raid.h: Likewise.
* include/grub/util/lvm.h: Likewise.
* include/grub/util/raid.h: Likewise.
* util/lvm.c: Likewise.
* util/raid.c: Likewise.
* include/grub/disk.h (grub_disk_dev_id): Add
GRUB_DISK_DEVICE_RAID_ID and GRUB_DISK_DEVICE_LVM_ID.
(grub_disk_get_size): New prototype.
* kern/disk.c (grub_disk_open): Check whether grub_partition_probe()
returns a partition.
(grub_disk_get_size): New function.
* kern/i386/pc/init.c (make_install_device): Copy the prefix
verbatim if grub_install_dos_part is -2.
* util/i386/pc/getroot.c (grub_guess_root_device): Support RAID
and LVM devices.
* util/i386/pc/grub-setup.c (setup): New argument
MUST_EMBED. Force embedding of GRUB when the argument is
true. Close FILE before returning.
(main): Add support for RAID and LVM.
* conf/common.rmk: Add RAID and LVM modules.
* conf/i386-pc.rmk (grub_setup_SOURCES): Add util/raid.c and
util/lvm.c.
(grub_emu_SOURCES): Add disk/raid.c and disk/lvm.c.
* kern/misc.c (grub_strstr): New function.
* include/grub/misc.h (grub_strstr): New prototype.
2006-10-14 15:24:53 +00:00
|
|
|
/* Check for LVM. */
|
2009-11-24 07:20:48 +00:00
|
|
|
if (!strncmp (os_dev, "/dev/mapper/", 12)
|
|
|
|
&& ! grub_util_is_dmraid (os_dev)
|
2009-11-24 08:55:28 +00:00
|
|
|
&& strncmp (os_dev, "/dev/mapper/mpath", 17) != 0)
|
2008-01-12 15:11:57 +00:00
|
|
|
return GRUB_DEV_ABSTRACTION_LVM;
|
2006-10-14 Yoshinori K. Okuji <okuji@enbug.org>
* DISTLIST: Added commands/echo.c, disk/lvm.c, disk/raid.c,
include/grub/bitmap.h, include/grub/lvm.h, include/grub/raid.h,
include/grub/i386/pc/vbeutil.h, include/grub/util/lvm.h,
include/grub/util/raid.h, util/lvm.c, util/raid.c, video/bitmap.c,
video/readers/tga.c and video/i386/pc/vbeutil.c.
2006-10-14 Jeroen Dekkers <jeroen@dekkers.cx>
Added support for RAID and LVM.
* disk/lvm.c: New file.
* disk/raid.c: Likewise.
* include/grub/lvm.h: Likewise.
* include/grub/raid.h: Likewise.
* include/grub/util/lvm.h: Likewise.
* include/grub/util/raid.h: Likewise.
* util/lvm.c: Likewise.
* util/raid.c: Likewise.
* include/grub/disk.h (grub_disk_dev_id): Add
GRUB_DISK_DEVICE_RAID_ID and GRUB_DISK_DEVICE_LVM_ID.
(grub_disk_get_size): New prototype.
* kern/disk.c (grub_disk_open): Check whether grub_partition_probe()
returns a partition.
(grub_disk_get_size): New function.
* kern/i386/pc/init.c (make_install_device): Copy the prefix
verbatim if grub_install_dos_part is -2.
* util/i386/pc/getroot.c (grub_guess_root_device): Support RAID
and LVM devices.
* util/i386/pc/grub-setup.c (setup): New argument
MUST_EMBED. Force embedding of GRUB when the argument is
true. Close FILE before returning.
(main): Add support for RAID and LVM.
* conf/common.rmk: Add RAID and LVM modules.
* conf/i386-pc.rmk (grub_setup_SOURCES): Add util/raid.c and
util/lvm.c.
(grub_emu_SOURCES): Add disk/raid.c and disk/lvm.c.
* kern/misc.c (grub_strstr): New function.
* include/grub/misc.h (grub_strstr): New prototype.
2006-10-14 15:24:53 +00:00
|
|
|
|
2007-05-16 21:38:44 +00:00
|
|
|
/* Check for RAID. */
|
2006-10-14 21:51:37 +00:00
|
|
|
if (!strncmp (os_dev, "/dev/md", 7))
|
2008-01-12 15:11:57 +00:00
|
|
|
return GRUB_DEV_ABSTRACTION_RAID;
|
2008-05-16 21:39:52 +00:00
|
|
|
#endif
|
2008-01-12 15:11:57 +00:00
|
|
|
|
|
|
|
/* No abstraction found. */
|
|
|
|
return GRUB_DEV_ABSTRACTION_NONE;
|
|
|
|
}
|
|
|
|
|
2010-07-18 14:53:14 +00:00
|
|
|
#ifdef __linux__
|
|
|
|
static char *
|
|
|
|
get_mdadm_name (const char *os_dev)
|
|
|
|
{
|
|
|
|
int mdadm_pipe[2];
|
|
|
|
pid_t mdadm_pid;
|
|
|
|
char *name = NULL;
|
|
|
|
|
|
|
|
if (pipe (mdadm_pipe) < 0)
|
|
|
|
{
|
|
|
|
grub_util_warn ("Unable to create pipe for mdadm: %s", strerror (errno));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
mdadm_pid = fork ();
|
|
|
|
if (mdadm_pid < 0)
|
|
|
|
grub_util_warn ("Unable to fork mdadm: %s", strerror (errno));
|
|
|
|
else if (mdadm_pid == 0)
|
|
|
|
{
|
|
|
|
/* Child. */
|
|
|
|
char *argv[5];
|
|
|
|
|
|
|
|
close (mdadm_pipe[0]);
|
|
|
|
dup2 (mdadm_pipe[1], STDOUT_FILENO);
|
|
|
|
close (mdadm_pipe[1]);
|
|
|
|
|
|
|
|
/* execvp has inconvenient types, hence the casts. None of these
|
|
|
|
strings will actually be modified. */
|
|
|
|
argv[0] = (char *) "mdadm";
|
|
|
|
argv[1] = (char *) "--detail";
|
|
|
|
argv[2] = (char *) "--export";
|
|
|
|
argv[3] = (char *) os_dev;
|
|
|
|
argv[4] = NULL;
|
|
|
|
execvp ("mdadm", argv);
|
|
|
|
exit (127);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Parent. Read mdadm's output. */
|
|
|
|
FILE *mdadm;
|
|
|
|
char *buf = NULL;
|
|
|
|
size_t len = 0;
|
|
|
|
|
|
|
|
close (mdadm_pipe[1]);
|
|
|
|
mdadm = fdopen (mdadm_pipe[0], "r");
|
|
|
|
if (! mdadm)
|
|
|
|
{
|
|
|
|
grub_util_warn ("Unable to open stream from mdadm: %s",
|
|
|
|
strerror (errno));
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (getline (&buf, &len, mdadm) > 0)
|
|
|
|
{
|
|
|
|
if (strncmp (buf, "MD_NAME=", sizeof ("MD_NAME=") - 1) == 0)
|
|
|
|
{
|
|
|
|
char *name_start, *colon;
|
|
|
|
size_t name_len;
|
|
|
|
|
|
|
|
free (name);
|
|
|
|
name_start = buf + sizeof ("MD_NAME=") - 1;
|
|
|
|
/* Strip off the homehost if present. */
|
|
|
|
colon = strchr (name_start, ':');
|
|
|
|
name = strdup (colon ? colon + 1 : name_start);
|
|
|
|
name_len = strlen (name);
|
|
|
|
if (name[name_len - 1] == '\n')
|
|
|
|
name[name_len - 1] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
close (mdadm_pipe[0]);
|
|
|
|
waitpid (mdadm_pid, NULL, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
#endif /* __linux__ */
|
|
|
|
|
2008-01-12 15:11:57 +00:00
|
|
|
char *
|
|
|
|
grub_util_get_grub_dev (const char *os_dev)
|
|
|
|
{
|
2010-07-18 14:53:14 +00:00
|
|
|
char *grub_dev = NULL;
|
2008-01-12 15:11:57 +00:00
|
|
|
|
|
|
|
switch (grub_util_get_dev_abstraction (os_dev))
|
2006-10-14 Yoshinori K. Okuji <okuji@enbug.org>
* DISTLIST: Added commands/echo.c, disk/lvm.c, disk/raid.c,
include/grub/bitmap.h, include/grub/lvm.h, include/grub/raid.h,
include/grub/i386/pc/vbeutil.h, include/grub/util/lvm.h,
include/grub/util/raid.h, util/lvm.c, util/raid.c, video/bitmap.c,
video/readers/tga.c and video/i386/pc/vbeutil.c.
2006-10-14 Jeroen Dekkers <jeroen@dekkers.cx>
Added support for RAID and LVM.
* disk/lvm.c: New file.
* disk/raid.c: Likewise.
* include/grub/lvm.h: Likewise.
* include/grub/raid.h: Likewise.
* include/grub/util/lvm.h: Likewise.
* include/grub/util/raid.h: Likewise.
* util/lvm.c: Likewise.
* util/raid.c: Likewise.
* include/grub/disk.h (grub_disk_dev_id): Add
GRUB_DISK_DEVICE_RAID_ID and GRUB_DISK_DEVICE_LVM_ID.
(grub_disk_get_size): New prototype.
* kern/disk.c (grub_disk_open): Check whether grub_partition_probe()
returns a partition.
(grub_disk_get_size): New function.
* kern/i386/pc/init.c (make_install_device): Copy the prefix
verbatim if grub_install_dos_part is -2.
* util/i386/pc/getroot.c (grub_guess_root_device): Support RAID
and LVM devices.
* util/i386/pc/grub-setup.c (setup): New argument
MUST_EMBED. Force embedding of GRUB when the argument is
true. Close FILE before returning.
(main): Add support for RAID and LVM.
* conf/common.rmk: Add RAID and LVM modules.
* conf/i386-pc.rmk (grub_setup_SOURCES): Add util/raid.c and
util/lvm.c.
(grub_emu_SOURCES): Add disk/raid.c and disk/lvm.c.
* kern/misc.c (grub_strstr): New function.
* include/grub/misc.h (grub_strstr): New prototype.
2006-10-14 15:24:53 +00:00
|
|
|
{
|
2008-01-12 15:11:57 +00:00
|
|
|
case GRUB_DEV_ABSTRACTION_LVM:
|
|
|
|
|
2008-09-04 19:54:59 +00:00
|
|
|
{
|
|
|
|
unsigned short i, len;
|
|
|
|
grub_size_t offset = sizeof ("/dev/mapper/") - 1;
|
2008-01-12 15:11:57 +00:00
|
|
|
|
2008-09-04 19:54:59 +00:00
|
|
|
len = strlen (os_dev) - offset + 1;
|
|
|
|
grub_dev = xmalloc (len);
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++, offset++)
|
|
|
|
{
|
|
|
|
grub_dev[i] = os_dev[offset];
|
|
|
|
if (os_dev[offset] == '-' && os_dev[offset + 1] == '-')
|
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
}
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2008-01-12 15:11:57 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GRUB_DEV_ABSTRACTION_RAID:
|
2007-05-17 23:23:03 +00:00
|
|
|
|
|
|
|
if (os_dev[7] == '_' && os_dev[8] == 'd')
|
|
|
|
{
|
|
|
|
/* This a partitionable RAID device of the form /dev/md_dNNpMM. */
|
2008-12-28 16:20:30 +00:00
|
|
|
|
|
|
|
char *p, *q;
|
2007-05-17 23:23:03 +00:00
|
|
|
|
2008-09-04 19:54:59 +00:00
|
|
|
p = strdup (os_dev + sizeof ("/dev/md_d") - 1);
|
|
|
|
|
2008-12-28 16:20:30 +00:00
|
|
|
q = strchr (p, 'p');
|
|
|
|
if (q)
|
|
|
|
*q = ',';
|
|
|
|
|
2009-12-07 16:46:24 +00:00
|
|
|
grub_dev = xasprintf ("md%s", p);
|
2008-12-28 16:20:30 +00:00
|
|
|
free (p);
|
|
|
|
}
|
|
|
|
else if (os_dev[7] == '/' && os_dev[8] == 'd')
|
|
|
|
{
|
|
|
|
/* This a partitionable RAID device of the form /dev/md/dNNpMM. */
|
|
|
|
|
|
|
|
char *p, *q;
|
|
|
|
|
|
|
|
p = strdup (os_dev + sizeof ("/dev/md/d") - 1);
|
|
|
|
|
2008-09-04 19:54:59 +00:00
|
|
|
q = strchr (p, 'p');
|
|
|
|
if (q)
|
|
|
|
*q = ',';
|
|
|
|
|
2009-12-07 16:46:24 +00:00
|
|
|
grub_dev = xasprintf ("md%s", p);
|
2008-09-04 19:54:59 +00:00
|
|
|
free (p);
|
2007-05-17 23:23:03 +00:00
|
|
|
}
|
|
|
|
else if (os_dev[7] >= '0' && os_dev[7] <= '9')
|
|
|
|
{
|
2009-02-04 10:52:25 +00:00
|
|
|
char *p , *q;
|
|
|
|
|
|
|
|
p = strdup (os_dev + sizeof ("/dev/md") - 1);
|
|
|
|
|
|
|
|
q = strchr (p, 'p');
|
|
|
|
if (q)
|
|
|
|
*q = ',';
|
|
|
|
|
2009-12-07 16:46:24 +00:00
|
|
|
grub_dev = xasprintf ("md%s", p);
|
2009-02-04 10:52:25 +00:00
|
|
|
free (p);
|
2008-09-04 19:54:59 +00:00
|
|
|
}
|
|
|
|
else if (os_dev[7] == '/' && os_dev[8] >= '0' && os_dev[8] <= '9')
|
|
|
|
{
|
2009-02-04 10:52:25 +00:00
|
|
|
char *p , *q;
|
|
|
|
|
|
|
|
p = strdup (os_dev + sizeof ("/dev/md/") - 1);
|
|
|
|
|
|
|
|
q = strchr (p, 'p');
|
|
|
|
if (q)
|
|
|
|
*q = ',';
|
|
|
|
|
2009-12-07 16:46:24 +00:00
|
|
|
grub_dev = xasprintf ("md%s", p);
|
2009-02-04 10:52:25 +00:00
|
|
|
free (p);
|
2007-05-17 23:23:03 +00:00
|
|
|
}
|
2009-11-08 00:57:17 +00:00
|
|
|
else if (os_dev[7] == '/')
|
|
|
|
{
|
|
|
|
/* mdraid 1.x with a free name. */
|
|
|
|
char *p , *q;
|
|
|
|
|
|
|
|
p = strdup (os_dev + sizeof ("/dev/md/") - 1);
|
|
|
|
|
|
|
|
q = strchr (p, 'p');
|
|
|
|
if (q)
|
|
|
|
*q = ',';
|
|
|
|
|
2010-07-20 22:09:45 +00:00
|
|
|
grub_dev = xasprintf ("md/%s", p);
|
2009-11-08 00:57:17 +00:00
|
|
|
free (p);
|
|
|
|
}
|
2007-05-17 23:23:03 +00:00
|
|
|
else
|
2010-01-16 00:26:52 +00:00
|
|
|
grub_util_error ("unknown kind of RAID device `%s'", os_dev);
|
2009-06-10 21:04:23 +00:00
|
|
|
|
2010-07-18 14:53:14 +00:00
|
|
|
#ifdef __linux__
|
|
|
|
{
|
|
|
|
char *mdadm_name = get_mdadm_name (os_dev);
|
|
|
|
|
|
|
|
if (mdadm_name)
|
|
|
|
{
|
|
|
|
free (grub_dev);
|
2010-07-20 22:09:45 +00:00
|
|
|
grub_dev = xasprintf ("md/%s", mdadm_name);
|
2010-07-18 14:53:14 +00:00
|
|
|
free (mdadm_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* __linux__ */
|
|
|
|
|
2008-01-12 15:11:57 +00:00
|
|
|
break;
|
2006-10-14 Yoshinori K. Okuji <okuji@enbug.org>
* DISTLIST: Added commands/echo.c, disk/lvm.c, disk/raid.c,
include/grub/bitmap.h, include/grub/lvm.h, include/grub/raid.h,
include/grub/i386/pc/vbeutil.h, include/grub/util/lvm.h,
include/grub/util/raid.h, util/lvm.c, util/raid.c, video/bitmap.c,
video/readers/tga.c and video/i386/pc/vbeutil.c.
2006-10-14 Jeroen Dekkers <jeroen@dekkers.cx>
Added support for RAID and LVM.
* disk/lvm.c: New file.
* disk/raid.c: Likewise.
* include/grub/lvm.h: Likewise.
* include/grub/raid.h: Likewise.
* include/grub/util/lvm.h: Likewise.
* include/grub/util/raid.h: Likewise.
* util/lvm.c: Likewise.
* util/raid.c: Likewise.
* include/grub/disk.h (grub_disk_dev_id): Add
GRUB_DISK_DEVICE_RAID_ID and GRUB_DISK_DEVICE_LVM_ID.
(grub_disk_get_size): New prototype.
* kern/disk.c (grub_disk_open): Check whether grub_partition_probe()
returns a partition.
(grub_disk_get_size): New function.
* kern/i386/pc/init.c (make_install_device): Copy the prefix
verbatim if grub_install_dos_part is -2.
* util/i386/pc/getroot.c (grub_guess_root_device): Support RAID
and LVM devices.
* util/i386/pc/grub-setup.c (setup): New argument
MUST_EMBED. Force embedding of GRUB when the argument is
true. Close FILE before returning.
(main): Add support for RAID and LVM.
* conf/common.rmk: Add RAID and LVM modules.
* conf/i386-pc.rmk (grub_setup_SOURCES): Add util/raid.c and
util/lvm.c.
(grub_emu_SOURCES): Add disk/raid.c and disk/lvm.c.
* kern/misc.c (grub_strstr): New function.
* include/grub/misc.h (grub_strstr): New prototype.
2006-10-14 15:24:53 +00:00
|
|
|
|
2008-01-12 15:11:57 +00:00
|
|
|
default: /* GRUB_DEV_ABSTRACTION_NONE */
|
|
|
|
grub_dev = grub_util_biosdisk_get_grub_dev (os_dev);
|
2006-10-14 Yoshinori K. Okuji <okuji@enbug.org>
* DISTLIST: Added commands/echo.c, disk/lvm.c, disk/raid.c,
include/grub/bitmap.h, include/grub/lvm.h, include/grub/raid.h,
include/grub/i386/pc/vbeutil.h, include/grub/util/lvm.h,
include/grub/util/raid.h, util/lvm.c, util/raid.c, video/bitmap.c,
video/readers/tga.c and video/i386/pc/vbeutil.c.
2006-10-14 Jeroen Dekkers <jeroen@dekkers.cx>
Added support for RAID and LVM.
* disk/lvm.c: New file.
* disk/raid.c: Likewise.
* include/grub/lvm.h: Likewise.
* include/grub/raid.h: Likewise.
* include/grub/util/lvm.h: Likewise.
* include/grub/util/raid.h: Likewise.
* util/lvm.c: Likewise.
* util/raid.c: Likewise.
* include/grub/disk.h (grub_disk_dev_id): Add
GRUB_DISK_DEVICE_RAID_ID and GRUB_DISK_DEVICE_LVM_ID.
(grub_disk_get_size): New prototype.
* kern/disk.c (grub_disk_open): Check whether grub_partition_probe()
returns a partition.
(grub_disk_get_size): New function.
* kern/i386/pc/init.c (make_install_device): Copy the prefix
verbatim if grub_install_dos_part is -2.
* util/i386/pc/getroot.c (grub_guess_root_device): Support RAID
and LVM devices.
* util/i386/pc/grub-setup.c (setup): New argument
MUST_EMBED. Force embedding of GRUB when the argument is
true. Close FILE before returning.
(main): Add support for RAID and LVM.
* conf/common.rmk: Add RAID and LVM modules.
* conf/i386-pc.rmk (grub_setup_SOURCES): Add util/raid.c and
util/lvm.c.
(grub_emu_SOURCES): Add disk/raid.c and disk/lvm.c.
* kern/misc.c (grub_strstr): New function.
* include/grub/misc.h (grub_strstr): New prototype.
2006-10-14 15:24:53 +00:00
|
|
|
}
|
2007-05-16 21:38:44 +00:00
|
|
|
|
2008-01-12 15:11:57 +00:00
|
|
|
return grub_dev;
|
2003-11-17 18:07:09 +00:00
|
|
|
}
|
2008-02-28 10:11:06 +00:00
|
|
|
|
2008-03-30 18:04:40 +00:00
|
|
|
const char *
|
2008-02-28 10:11:06 +00:00
|
|
|
grub_util_check_block_device (const char *blk_dev)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (stat (blk_dev, &st) < 0)
|
2010-01-16 00:26:52 +00:00
|
|
|
grub_util_error ("cannot stat `%s'", blk_dev);
|
2008-02-28 10:11:06 +00:00
|
|
|
|
|
|
|
if (S_ISBLK (st.st_mode))
|
|
|
|
return (blk_dev);
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
2009-04-11 09:40:39 +00:00
|
|
|
|
|
|
|
const char *
|
|
|
|
grub_util_check_char_device (const char *blk_dev)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (stat (blk_dev, &st) < 0)
|
2010-01-16 00:26:52 +00:00
|
|
|
grub_util_error ("cannot stat `%s'", blk_dev);
|
2009-04-11 09:40:39 +00:00
|
|
|
|
|
|
|
if (S_ISCHR (st.st_mode))
|
|
|
|
return (blk_dev);
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|