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
|
2009-04-11 09:40:39 +00:00
|
|
|
* Copyright (C) 1999,2000,2001,2002,2003,2006,2007,2008,2009 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>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
|
2008-05-16 21:39:52 +00:00
|
|
|
#ifdef __CYGWIN__
|
|
|
|
# include <sys/fcntl.h>
|
|
|
|
# include <sys/cygwin.h>
|
|
|
|
# include <limits.h>
|
|
|
|
# define DEV_CYGDRIVE_MAJOR 98
|
|
|
|
#endif
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
#include <grub/util/misc.h>
|
2008-09-08 13:52:30 +00:00
|
|
|
#include <grub/util/hostdisk.h>
|
2008-01-12 15:11:57 +00:00
|
|
|
#include <grub/util/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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-05-16 21:39:52 +00:00
|
|
|
#ifdef __CYGWIN__
|
|
|
|
/* Convert POSIX path to Win32 path,
|
|
|
|
remove drive letter, replace backslashes. */
|
|
|
|
static char *
|
|
|
|
get_win32_path (const char *path)
|
|
|
|
{
|
|
|
|
char winpath[PATH_MAX];
|
|
|
|
cygwin_conv_to_full_win32_path (path, winpath);
|
|
|
|
|
|
|
|
int len = strlen (winpath);
|
|
|
|
if (len > 2 && winpath[1] == ':')
|
|
|
|
{
|
|
|
|
len -= 2;
|
|
|
|
memmove (winpath, winpath + 2, len + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
if (winpath[i] == '\\')
|
|
|
|
winpath[i] = '/';
|
|
|
|
return xstrdup (winpath);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-11-17 18:07:09 +00:00
|
|
|
char *
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_get_prefix (const char *dir)
|
2003-11-17 18:07:09 +00:00
|
|
|
{
|
|
|
|
char *saved_cwd;
|
|
|
|
char *abs_dir, *prev_dir;
|
|
|
|
char *prefix;
|
|
|
|
struct stat st, prev_st;
|
|
|
|
|
|
|
|
/* Save the current directory. */
|
|
|
|
saved_cwd = xgetcwd ();
|
|
|
|
|
|
|
|
if (chdir (dir) < 0)
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_util_error ("Cannot change directory to `%s'", dir);
|
2003-11-17 18:07:09 +00:00
|
|
|
|
|
|
|
abs_dir = xgetcwd ();
|
|
|
|
strip_extra_slashes (abs_dir);
|
|
|
|
prev_dir = xstrdup (abs_dir);
|
|
|
|
|
|
|
|
if (stat (".", &prev_st) < 0)
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_util_error ("Cannot stat `%s'", dir);
|
2003-11-17 18:07:09 +00:00
|
|
|
|
|
|
|
if (! S_ISDIR (prev_st.st_mode))
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_util_error ("`%s' is not a directory", dir);
|
2003-11-17 18:07:09 +00:00
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
if (chdir ("..") < 0)
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_util_error ("Cannot change directory to the parent");
|
2003-11-17 18:07:09 +00:00
|
|
|
|
|
|
|
if (stat (".", &st) < 0)
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_util_error ("Cannot stat current directory");
|
2003-11-17 18:07:09 +00:00
|
|
|
|
|
|
|
if (! S_ISDIR (st.st_mode))
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_util_error ("Current directory is not a directory???");
|
2003-11-17 18:07:09 +00:00
|
|
|
|
|
|
|
if (prev_st.st_dev != st.st_dev || prev_st.st_ino == st.st_ino)
|
|
|
|
break;
|
|
|
|
|
|
|
|
free (prev_dir);
|
|
|
|
prev_dir = xgetcwd ();
|
|
|
|
prev_st = st;
|
|
|
|
}
|
|
|
|
|
|
|
|
strip_extra_slashes (prev_dir);
|
|
|
|
prefix = xmalloc (strlen (abs_dir) - strlen (prev_dir) + 2);
|
|
|
|
prefix[0] = '/';
|
|
|
|
strcpy (prefix + 1, abs_dir + strlen (prev_dir));
|
|
|
|
strip_extra_slashes (prefix);
|
|
|
|
|
|
|
|
if (chdir (saved_cwd) < 0)
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_util_error ("Cannot change directory to `%s'", dir);
|
2003-11-17 18:07:09 +00:00
|
|
|
|
2008-05-16 21:39:52 +00:00
|
|
|
#ifdef __CYGWIN__
|
|
|
|
if (st.st_dev != (DEV_CYGDRIVE_MAJOR << 16))
|
|
|
|
{
|
|
|
|
/* Reached some mount point not below /cygdrive.
|
|
|
|
GRUB does not know Cygwin's emulated mounts,
|
|
|
|
convert to Win32 path. */
|
|
|
|
grub_util_info ("Cygwin prefix = %s", prefix);
|
|
|
|
char * wprefix = get_win32_path (prefix);
|
|
|
|
free (prefix);
|
|
|
|
prefix = wprefix;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-11-17 18:07:09 +00:00
|
|
|
free (saved_cwd);
|
|
|
|
free (abs_dir);
|
|
|
|
free (prev_dir);
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
grub_util_info ("prefix = %s", prefix);
|
2003-11-17 18:07:09 +00:00
|
|
|
return prefix;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((ent = readdir (dp)) != 0)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (S_ISLNK (st.st_mode))
|
|
|
|
/* Don't follow symbolic links. */
|
|
|
|
continue;
|
|
|
|
|
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)
|
2004-04-04 13:46:03 +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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-11 18:46:12 +00:00
|
|
|
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
|
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;
|
|
|
|
|
|
|
|
cwd = xgetcwd ();
|
|
|
|
res = xmalloc (strlen (cwd) + strlen (ent->d_name) + 2);
|
|
|
|
sprintf (res, "%s/%s", cwd, ent->d_name);
|
|
|
|
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)
|
2004-04-04 13:46:03 +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)
|
2004-04-04 13:46:03 +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
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
char *os_dev;
|
|
|
|
|
|
|
|
if (stat (dir, &st) < 0)
|
2004-04-04 13:46:03 +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
|
2007-05-16 21:38:44 +00:00
|
|
|
|
|
|
|
return os_dev;
|
|
|
|
}
|
|
|
|
|
2008-01-12 15:11:57 +00:00
|
|
|
int
|
2009-05-26 20:47:00 +00:00
|
|
|
grub_util_get_dev_abstraction (const char *os_dev 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. */
|
|
|
|
if (!strncmp (os_dev, "/dev/mapper/", 12))
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
grub_util_get_grub_dev (const char *os_dev)
|
|
|
|
{
|
|
|
|
char *grub_dev;
|
|
|
|
|
|
|
|
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++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 = ',';
|
|
|
|
|
|
|
|
asprintf (&grub_dev, "md%s", p);
|
|
|
|
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 = ',';
|
|
|
|
|
|
|
|
asprintf (&grub_dev, "md%s", p);
|
|
|
|
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 = ',';
|
|
|
|
|
|
|
|
asprintf (&grub_dev, "md%s", p);
|
|
|
|
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 = ',';
|
|
|
|
|
|
|
|
asprintf (&grub_dev, "md%s", p);
|
|
|
|
free (p);
|
2007-05-17 23:23:03 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
grub_util_error ("Unknown kind of RAID device `%s'", os_dev);
|
2008-09-04 19:54:59 +00:00
|
|
|
|
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)
|
|
|
|
grub_util_error ("Cannot stat `%s'", blk_dev);
|
|
|
|
|
|
|
|
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)
|
|
|
|
grub_util_error ("Cannot stat `%s'", blk_dev);
|
|
|
|
|
|
|
|
if (S_ISCHR (st.st_mode))
|
|
|
|
return (blk_dev);
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|