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
|
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
|
|
|
* Copyright (C) 1999,2000,2001,2002,2003,2006 Free Software Foundation, Inc.
|
2003-11-17 18:07:09 +00:00
|
|
|
*
|
2004-04-04 13:46:03 +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
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* 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
|
2004-04-04 13:46:03 +00:00
|
|
|
* along with GRUB; if not, write to the Free Software
|
2003-11-17 18:07:09 +00:00
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
#include <grub/util/misc.h>
|
|
|
|
#include <grub/i386/pc/util/biosdisk.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')
|
|
|
|
{
|
|
|
|
p[0] = '\0';
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (strcmp (ent->d_name, ".") == 0 || strcmp (ent->d_name, "..") == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (lstat (ent->d_name, &st) < 0)
|
|
|
|
/* Ignore any error. */
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (S_ISLNK (st.st_mode))
|
|
|
|
/* Don't follow symbolic links. */
|
|
|
|
continue;
|
|
|
|
|
2006-09-14 18:52:50 +00:00
|
|
|
if (S_ISDIR (st.st_mode) && ent->d_name[0] != '.')
|
2003-11-17 18:07:09 +00:00
|
|
|
{
|
2006-09-14 18:52:50 +00:00
|
|
|
/* Find it recursively, but avoid dotdirs (like ".static") since they
|
|
|
|
could contain duplicates, which would later break the
|
|
|
|
pathname-based check */
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (S_ISBLK (st.st_mode) && st.st_rdev == dev)
|
|
|
|
{
|
|
|
|
/* 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);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
/* This might be truly slow, but is there any better way? */
|
|
|
|
os_dev = find_root_device ("/dev", st.st_dev);
|
|
|
|
if (! os_dev)
|
|
|
|
return 0;
|
|
|
|
|
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
|
|
|
#ifdef __linux__
|
|
|
|
/* Check for LVM. */
|
|
|
|
if (!strncmp (os_dev, "/dev/mapper/", 12))
|
|
|
|
{
|
|
|
|
char *grub_dev = xmalloc (strlen (os_dev) - 12);
|
|
|
|
|
|
|
|
strcpy (grub_dev, os_dev+12);
|
|
|
|
|
|
|
|
return grub_dev;
|
|
|
|
}
|
|
|
|
|
2006-10-14 21:51:37 +00:00
|
|
|
if (!strncmp (os_dev, "/dev/md", 7))
|
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
|
|
|
{
|
|
|
|
char *p, *grub_dev = xmalloc (8);
|
|
|
|
|
|
|
|
p = strchr (os_dev, 'm');
|
|
|
|
strncpy (grub_dev, p, 8);
|
|
|
|
|
|
|
|
return grub_dev;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-10-14 18:59:34 +00:00
|
|
|
return os_dev;
|
2003-11-17 18:07:09 +00:00
|
|
|
}
|