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
|
2011-10-09 19:13:00 +00:00
|
|
|
* Copyright (C) 1999,2000,2001,2002,2003,2006,2007,2008,2009,2010,2011 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
|
|
|
*/
|
|
|
|
|
2011-01-22 14:37:05 +00:00
|
|
|
#include <config-util.h>
|
2008-09-04 19:54:59 +00:00
|
|
|
#include <config.h>
|
2011-01-22 14:37:05 +00:00
|
|
|
|
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>
|
2011-11-25 22:07:33 +00:00
|
|
|
#ifdef HAVE_LIMITS_H
|
|
|
|
#include <limits.h>
|
|
|
|
#endif
|
2010-08-01 12:47:14 +00:00
|
|
|
#include <grub/util/misc.h>
|
2013-10-08 19:01:09 +00:00
|
|
|
|
2011-04-25 12:52:07 +00:00
|
|
|
#include <grub/cryptodisk.h>
|
2011-11-11 23:34:14 +00:00
|
|
|
#include <grub/i18n.h>
|
2008-05-16 21:39:52 +00:00
|
|
|
|
2013-09-22 05:36:17 +00:00
|
|
|
#ifdef __linux__
|
|
|
|
#include <sys/ioctl.h> /* ioctl */
|
|
|
|
#include <sys/mount.h>
|
2010-11-08 15:51:50 +00:00
|
|
|
#endif
|
|
|
|
|
2012-04-21 14:49:56 +00:00
|
|
|
#include <sys/types.h>
|
2010-07-18 14:53:14 +00:00
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
2013-09-22 05:36:17 +00:00
|
|
|
#if defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
|
|
|
|
#include <sys/mount.h>
|
2012-01-29 13:28:01 +00:00
|
|
|
#endif
|
|
|
|
|
2013-08-21 19:05:01 +00:00
|
|
|
#if defined(__NetBSD__) || defined(__OpenBSD__)
|
2012-01-29 13:28:01 +00:00
|
|
|
# include <sys/ioctl.h>
|
|
|
|
# include <sys/disklabel.h> /* struct disklabel */
|
2012-02-03 11:32:21 +00:00
|
|
|
# include <sys/disk.h> /* struct dkwedge_info */
|
2013-09-22 05:36:17 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/mount.h>
|
2012-01-29 13:28:01 +00:00
|
|
|
#endif /* defined(__NetBSD__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) */
|
|
|
|
|
2013-08-21 19:05:01 +00:00
|
|
|
#if defined(__NetBSD__)
|
2012-01-29 13:28:01 +00:00
|
|
|
# include <sys/fdio.h>
|
2013-08-21 19:05:01 +00:00
|
|
|
#endif
|
2012-01-29 13:28:01 +00:00
|
|
|
|
2013-09-22 05:36:17 +00:00
|
|
|
grub_disk_addr_t
|
|
|
|
grub_util_find_partition_start (const char *dev)
|
2011-04-22 00:46:36 +00:00
|
|
|
{
|
2013-12-24 13:16:57 +00:00
|
|
|
#if GRUB_UTIL_FD_STAT_IS_FUNCTIONAL
|
|
|
|
struct stat st;
|
2013-09-22 05:36:17 +00:00
|
|
|
grub_disk_addr_t partition_start;
|
2013-12-24 13:16:57 +00:00
|
|
|
|
|
|
|
if (stat (dev, &st) >= 0
|
|
|
|
&& grub_util_device_is_mapped_stat (&st)
|
|
|
|
&& grub_util_get_dm_node_linear_info (st.st_rdev, 0, 0, &partition_start))
|
2013-09-22 05:36:17 +00:00
|
|
|
return partition_start;
|
2013-12-24 13:16:57 +00:00
|
|
|
#endif
|
2011-04-22 00:46:36 +00:00
|
|
|
|
2013-09-22 05:36:17 +00:00
|
|
|
return grub_util_find_partition_start_os (dev);
|
2011-04-22 00:46:36 +00:00
|
|
|
}
|
|
|
|
|
2013-09-22 05:36:17 +00:00
|
|
|
void
|
|
|
|
grub_util_pull_device (const char *os_dev)
|
2009-11-24 07:20:48 +00:00
|
|
|
{
|
2013-09-22 05:36:17 +00:00
|
|
|
enum grub_dev_abstraction_types ab;
|
|
|
|
ab = grub_util_get_dev_abstraction (os_dev);
|
|
|
|
switch (ab)
|
2012-03-02 10:36:07 +00:00
|
|
|
{
|
2013-09-22 05:36:17 +00:00
|
|
|
case GRUB_DEV_ABSTRACTION_LVM:
|
2013-09-23 09:21:09 +00:00
|
|
|
grub_util_pull_lvm_by_command (os_dev);
|
2017-04-04 16:23:55 +00:00
|
|
|
/* Fallthrough - in case that lvm-tools are unavailable. */
|
2013-09-22 05:36:17 +00:00
|
|
|
case GRUB_DEV_ABSTRACTION_LUKS:
|
|
|
|
grub_util_pull_devmapper (os_dev);
|
|
|
|
return;
|
2011-04-22 13:13:12 +00:00
|
|
|
|
2013-09-22 05:36:17 +00:00
|
|
|
default:
|
|
|
|
if (grub_util_pull_device_os (os_dev, ab))
|
|
|
|
return;
|
2015-01-27 16:17:58 +00:00
|
|
|
/* Fallthrough. */
|
2013-09-22 05:36:17 +00:00
|
|
|
case GRUB_DEV_ABSTRACTION_NONE:
|
|
|
|
free (grub_util_biosdisk_get_grub_dev (os_dev));
|
|
|
|
return;
|
2011-04-21 10:39:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-22 05:36:17 +00:00
|
|
|
char *
|
|
|
|
grub_util_get_grub_dev (const char *os_dev)
|
2011-04-25 12:52:07 +00:00
|
|
|
{
|
2013-09-22 05:36:17 +00:00
|
|
|
char *ret;
|
2011-04-25 12:52:07 +00:00
|
|
|
|
2013-09-22 05:36:17 +00:00
|
|
|
grub_util_pull_device (os_dev);
|
2011-04-25 12:52:07 +00:00
|
|
|
|
2013-09-22 05:36:17 +00:00
|
|
|
ret = grub_util_get_devmapper_grub_dev (os_dev);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
ret = grub_util_get_grub_dev_os (os_dev);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
return grub_util_biosdisk_get_grub_dev (os_dev);
|
2009-11-24 07:20:48 +00:00
|
|
|
}
|
2013-09-22 05:36:17 +00:00
|
|
|
|
2008-01-12 15:11:57 +00:00
|
|
|
int
|
2011-11-11 21:58:18 +00:00
|
|
|
grub_util_get_dev_abstraction (const char *os_dev)
|
2007-05-16 21:38:44 +00:00
|
|
|
{
|
2013-09-22 05:36:17 +00:00
|
|
|
enum grub_dev_abstraction_types ret;
|
|
|
|
|
2010-09-13 11:09:58 +00:00
|
|
|
/* User explicitly claims that this drive is visible by BIOS. */
|
|
|
|
if (grub_util_biosdisk_is_present (os_dev))
|
|
|
|
return GRUB_DEV_ABSTRACTION_NONE;
|
|
|
|
|
2011-04-21 10:39:31 +00:00
|
|
|
/* Check for LVM and LUKS. */
|
|
|
|
ret = grub_util_get_dm_abstraction (os_dev);
|
|
|
|
|
|
|
|
if (ret != GRUB_DEV_ABSTRACTION_NONE)
|
|
|
|
return ret;
|
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
|
|
|
|
2013-09-22 05:36:17 +00:00
|
|
|
return grub_util_get_dev_abstraction_os (os_dev);
|
2008-01-12 15:11:57 +00:00
|
|
|
}
|
|
|
|
|
2012-01-29 13:28:01 +00:00
|
|
|
static char *
|
2013-10-19 14:29:20 +00:00
|
|
|
convert_system_partition_to_system_disk (const char *os_dev, int *is_part)
|
2012-01-29 13:28:01 +00:00
|
|
|
{
|
2013-10-19 14:29:20 +00:00
|
|
|
#if GRUB_UTIL_FD_STAT_IS_FUNCTIONAL
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (stat (os_dev, &st) < 0)
|
|
|
|
{
|
|
|
|
const char *errstr = strerror (errno);
|
|
|
|
grub_error (GRUB_ERR_BAD_DEVICE, N_("cannot stat `%s': %s"),
|
|
|
|
os_dev, errstr);
|
|
|
|
grub_util_info (_("cannot stat `%s': %s"), os_dev, errstr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-21 14:42:28 +00:00
|
|
|
*is_part = 0;
|
|
|
|
|
2013-10-19 14:29:20 +00:00
|
|
|
if (grub_util_device_is_mapped_stat (&st))
|
|
|
|
return grub_util_devmapper_part_to_disk (&st, is_part, os_dev);
|
2012-01-29 13:28:01 +00:00
|
|
|
|
2013-09-22 05:36:17 +00:00
|
|
|
*is_part = 0;
|
2012-01-29 13:28:01 +00:00
|
|
|
|
2013-10-19 14:29:20 +00:00
|
|
|
return grub_util_part_to_disk (os_dev, &st, is_part);
|
|
|
|
#else
|
|
|
|
*is_part = 0;
|
|
|
|
|
|
|
|
return grub_util_part_to_disk (os_dev, NULL, is_part);
|
|
|
|
#endif
|
2012-01-29 13:28:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
2013-10-19 14:29:20 +00:00
|
|
|
find_system_device (const char *os_dev)
|
2012-01-29 13:28:01 +00:00
|
|
|
{
|
|
|
|
char *os_disk;
|
|
|
|
const char *drive;
|
2012-04-21 14:42:28 +00:00
|
|
|
int is_part;
|
2012-01-29 13:28:01 +00:00
|
|
|
|
2013-10-19 14:29:20 +00:00
|
|
|
os_disk = convert_system_partition_to_system_disk (os_dev, &is_part);
|
2012-01-29 13:28:01 +00:00
|
|
|
if (! os_disk)
|
|
|
|
return NULL;
|
|
|
|
|
2013-10-19 14:29:20 +00:00
|
|
|
drive = grub_hostdisk_os_dev_to_grub_drive (os_disk, 0);
|
2012-01-29 13:28:01 +00:00
|
|
|
free (os_disk);
|
|
|
|
return drive;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
2013-10-10 08:21:07 +00:00
|
|
|
make_device_name (const char *drive)
|
2012-01-29 13:28:01 +00:00
|
|
|
{
|
2013-10-10 08:21:07 +00:00
|
|
|
char *ret, *ptr;
|
2012-01-29 13:28:01 +00:00
|
|
|
const char *iptr;
|
|
|
|
|
2013-10-10 08:21:07 +00:00
|
|
|
ret = xmalloc (strlen (drive) * 2);
|
2012-01-29 13:28:01 +00:00
|
|
|
ptr = ret;
|
|
|
|
for (iptr = drive; *iptr; iptr++)
|
|
|
|
{
|
2013-09-23 09:21:09 +00:00
|
|
|
if (*iptr == ',' || *iptr == '\\')
|
2012-01-29 13:28:01 +00:00
|
|
|
*ptr++ = '\\';
|
|
|
|
*ptr++ = *iptr;
|
|
|
|
}
|
|
|
|
*ptr = 0;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-02-03 21:36:51 +00:00
|
|
|
char *
|
|
|
|
grub_util_get_os_disk (const char *os_dev)
|
|
|
|
{
|
2012-04-21 14:42:28 +00:00
|
|
|
int is_part;
|
2012-02-03 21:36:51 +00:00
|
|
|
|
|
|
|
grub_util_info ("Looking for %s", os_dev);
|
|
|
|
|
2013-10-19 14:29:20 +00:00
|
|
|
return convert_system_partition_to_system_disk (os_dev, &is_part);
|
2012-02-03 21:36:51 +00:00
|
|
|
}
|
|
|
|
|
2013-09-22 05:36:17 +00:00
|
|
|
#if !defined(__APPLE__)
|
2013-01-20 15:52:15 +00:00
|
|
|
/* Context for grub_util_biosdisk_get_grub_dev. */
|
|
|
|
struct grub_util_biosdisk_get_grub_dev_ctx
|
|
|
|
{
|
|
|
|
char *partname;
|
|
|
|
grub_disk_addr_t start;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Helper for grub_util_biosdisk_get_grub_dev. */
|
|
|
|
static int
|
|
|
|
find_partition (grub_disk_t dsk __attribute__ ((unused)),
|
|
|
|
const grub_partition_t partition, void *data)
|
|
|
|
{
|
|
|
|
struct grub_util_biosdisk_get_grub_dev_ctx *ctx = data;
|
|
|
|
grub_disk_addr_t part_start = 0;
|
2013-12-17 15:04:47 +00:00
|
|
|
grub_util_info ("Partition %d starts from %" GRUB_HOST_PRIuLONG_LONG,
|
|
|
|
partition->number, (unsigned long long) partition->start);
|
2013-01-20 15:52:15 +00:00
|
|
|
|
|
|
|
part_start = grub_partition_get_start (partition);
|
|
|
|
|
|
|
|
if (ctx->start == part_start)
|
|
|
|
{
|
|
|
|
ctx->partname = grub_partition_get_name (partition);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-01-29 13:28:01 +00:00
|
|
|
char *
|
|
|
|
grub_util_biosdisk_get_grub_dev (const char *os_dev)
|
|
|
|
{
|
|
|
|
const char *drive;
|
|
|
|
char *sys_disk;
|
2012-04-21 14:42:28 +00:00
|
|
|
int is_part;
|
2012-01-29 13:28:01 +00:00
|
|
|
|
|
|
|
grub_util_info ("Looking for %s", os_dev);
|
|
|
|
|
2013-10-19 14:29:20 +00:00
|
|
|
sys_disk = convert_system_partition_to_system_disk (os_dev, &is_part);
|
2013-09-23 09:21:09 +00:00
|
|
|
|
2012-06-14 17:16:30 +00:00
|
|
|
if (!sys_disk)
|
|
|
|
return 0;
|
2013-10-19 14:29:20 +00:00
|
|
|
|
|
|
|
drive = grub_hostdisk_os_dev_to_grub_drive (sys_disk, 1);
|
|
|
|
|
2012-06-07 12:24:48 +00:00
|
|
|
grub_util_info ("%s is a parent of %s", sys_disk, os_dev);
|
2012-06-14 17:16:30 +00:00
|
|
|
if (!is_part)
|
2012-01-29 13:28:01 +00:00
|
|
|
{
|
|
|
|
free (sys_disk);
|
2013-10-10 08:21:07 +00:00
|
|
|
return make_device_name (drive);
|
2012-01-29 13:28:01 +00:00
|
|
|
}
|
|
|
|
free (sys_disk);
|
|
|
|
|
2013-09-22 05:36:17 +00:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
/* Apple uses "/dev/r?disk[0-9]+(s[0-9]+)?". */
|
2013-10-10 08:21:07 +00:00
|
|
|
/*
|
|
|
|
* Note: we do not use the new partition naming scheme as dos_part does not
|
|
|
|
* necessarily correspond to an msdos partition.
|
|
|
|
*/
|
2013-09-22 05:36:17 +00:00
|
|
|
{
|
|
|
|
const char *p;
|
2013-10-10 08:21:07 +00:00
|
|
|
char *dri, *ret;
|
|
|
|
int part;
|
2013-09-22 05:36:17 +00:00
|
|
|
int disk = (grub_memcmp (os_dev, "/dev/disk", sizeof ("/dev/disk") - 1)
|
|
|
|
== 0);
|
|
|
|
int rdisk = (grub_memcmp (os_dev, "/dev/rdisk", sizeof ("/dev/rdisk") - 1)
|
|
|
|
== 0);
|
2013-10-10 08:21:07 +00:00
|
|
|
|
2013-11-11 21:49:52 +00:00
|
|
|
dri = make_device_name (drive);
|
2013-09-22 05:36:17 +00:00
|
|
|
|
|
|
|
if (!disk && !rdisk)
|
2013-10-10 08:21:07 +00:00
|
|
|
return dri;
|
2013-09-22 05:36:17 +00:00
|
|
|
|
|
|
|
p = os_dev + sizeof ("/dev/disk") + rdisk - 1;
|
|
|
|
while (*p >= '0' && *p <= '9')
|
|
|
|
p++;
|
|
|
|
if (*p != 's')
|
2013-10-10 08:21:07 +00:00
|
|
|
return dri;
|
2013-09-22 05:36:17 +00:00
|
|
|
p++;
|
|
|
|
|
2013-10-10 08:21:07 +00:00
|
|
|
part = strtol (p, NULL, 10);
|
|
|
|
if (part == 0)
|
|
|
|
return dri;
|
|
|
|
|
|
|
|
ret = xasprintf ("%s,%d", dri, part);
|
|
|
|
free (dri);
|
|
|
|
|
|
|
|
return ret;
|
2013-09-22 05:36:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
2012-01-29 13:28:01 +00:00
|
|
|
|
|
|
|
/* Linux counts partitions uniformly, whether a BSD partition or a DOS
|
|
|
|
partition, so mapping them to GRUB devices is not trivial.
|
|
|
|
Here, get the start sector of a partition by HDIO_GETGEO, and
|
|
|
|
compare it with each partition GRUB recognizes.
|
|
|
|
|
|
|
|
Cygwin /dev/sdXN emulation uses Windows partition mapping. It
|
|
|
|
does not count the extended partition and missing primary
|
|
|
|
partitions. Use same method as on Linux here.
|
|
|
|
|
|
|
|
For NetBSD and FreeBSD, proceed as for Linux, except that the start
|
|
|
|
sector is obtained from the disk label. */
|
|
|
|
{
|
2013-01-20 15:52:15 +00:00
|
|
|
char *name;
|
2012-01-29 13:28:01 +00:00
|
|
|
grub_disk_t disk;
|
2013-01-20 15:52:15 +00:00
|
|
|
struct grub_util_biosdisk_get_grub_dev_ctx ctx;
|
2012-01-29 13:28:01 +00:00
|
|
|
|
2013-10-10 08:21:07 +00:00
|
|
|
name = make_device_name (drive);
|
2012-01-29 13:28:01 +00:00
|
|
|
|
2013-09-22 05:36:17 +00:00
|
|
|
ctx.start = grub_util_find_partition_start (os_dev);
|
2012-01-29 13:28:01 +00:00
|
|
|
if (grub_errno != GRUB_ERR_NONE)
|
|
|
|
{
|
|
|
|
free (name);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-09-22 05:36:17 +00:00
|
|
|
#if defined(__GNU__)
|
|
|
|
/* Some versions of Hurd use badly glued Linux code to handle partitions
|
|
|
|
resulting in partitions being promoted to disks. */
|
|
|
|
/* GNU uses "/dev/[hs]d[0-9]+(s[0-9]+[a-z]?)?". */
|
2013-10-10 08:21:07 +00:00
|
|
|
/*
|
|
|
|
* Note: we do not use the new partition naming scheme as dos_part does not
|
|
|
|
* necessarily correspond to an msdos partition.
|
|
|
|
*/
|
2013-09-22 05:36:17 +00:00
|
|
|
if (ctx.start == (grub_disk_addr_t) -1)
|
|
|
|
{
|
|
|
|
char *p;
|
2013-10-10 08:21:07 +00:00
|
|
|
char *dri;
|
2013-09-22 05:36:17 +00:00
|
|
|
|
2013-10-10 08:21:07 +00:00
|
|
|
dri = make_device_name (drive);
|
|
|
|
|
2013-09-22 05:36:17 +00:00
|
|
|
p = strrchr (os_dev + sizeof ("/dev/hd") - 1, 's');
|
|
|
|
if (p)
|
|
|
|
{
|
|
|
|
long int n;
|
|
|
|
char *q;
|
|
|
|
|
|
|
|
p++;
|
|
|
|
n = strtol (p, &q, 10);
|
2013-10-10 08:21:07 +00:00
|
|
|
if (p != q && n > 0 && n != GRUB_LONG_MAX)
|
2013-09-22 05:36:17 +00:00
|
|
|
{
|
2013-10-10 08:21:07 +00:00
|
|
|
char *t;
|
|
|
|
t = dri;
|
2013-09-22 05:36:17 +00:00
|
|
|
if (*q >= 'a' && *q <= 'g')
|
2013-12-20 14:02:27 +00:00
|
|
|
dri = xasprintf ("%s,%ld,%d", t, n, *q - 'a' + 1);
|
2013-10-10 08:21:07 +00:00
|
|
|
else
|
2013-12-20 14:02:27 +00:00
|
|
|
dri = xasprintf ("%s,%ld", t, n);
|
2013-10-10 08:21:07 +00:00
|
|
|
free (t);
|
2013-09-22 05:36:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-10 08:21:07 +00:00
|
|
|
return dri;
|
2013-09-22 05:36:17 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-12-17 15:04:47 +00:00
|
|
|
grub_util_info ("%s starts from %" GRUB_HOST_PRIuLONG_LONG,
|
|
|
|
os_dev, (unsigned long long) ctx.start);
|
2012-01-29 13:28:01 +00:00
|
|
|
|
|
|
|
grub_util_info ("opening the device %s", name);
|
|
|
|
disk = grub_disk_open (name);
|
|
|
|
free (name);
|
|
|
|
|
|
|
|
if (! disk)
|
|
|
|
{
|
|
|
|
/* We already know that the partition exists. Given that we already
|
|
|
|
checked the device map above, we can only get
|
|
|
|
GRUB_ERR_UNKNOWN_DEVICE at this point if the disk does not exist.
|
|
|
|
This can happen on Xen, where disk images in the host can be
|
|
|
|
assigned to devices that have partition-like names in the guest
|
|
|
|
but are really more like disks. */
|
|
|
|
if (grub_errno == GRUB_ERR_UNKNOWN_DEVICE)
|
|
|
|
{
|
2012-06-14 17:16:30 +00:00
|
|
|
char *canon;
|
2012-01-29 13:28:01 +00:00
|
|
|
grub_util_warn
|
2012-02-03 10:56:49 +00:00
|
|
|
(_("disk does not exist, so falling back to partition device %s"),
|
2012-01-29 13:28:01 +00:00
|
|
|
os_dev);
|
2013-08-13 15:48:56 +00:00
|
|
|
grub_errno = GRUB_ERR_NONE;
|
2012-01-29 13:28:01 +00:00
|
|
|
|
2015-03-04 00:00:19 +00:00
|
|
|
canon = grub_canonicalize_file_name (os_dev);
|
2013-10-19 14:29:20 +00:00
|
|
|
drive = grub_hostdisk_os_dev_to_grub_drive (canon ? : os_dev, 1);
|
2012-06-14 17:16:30 +00:00
|
|
|
if (canon)
|
|
|
|
free (canon);
|
2013-10-10 08:21:07 +00:00
|
|
|
return make_device_name (drive);
|
2012-01-29 13:28:01 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-20 15:52:15 +00:00
|
|
|
name = grub_util_get_ldm (disk, ctx.start);
|
2012-01-29 13:28:01 +00:00
|
|
|
if (name)
|
2015-01-26 08:54:14 +00:00
|
|
|
{
|
|
|
|
grub_disk_close (disk);
|
|
|
|
return name;
|
|
|
|
}
|
2012-01-29 13:28:01 +00:00
|
|
|
|
2013-01-20 15:52:15 +00:00
|
|
|
ctx.partname = NULL;
|
2012-01-29 13:28:01 +00:00
|
|
|
|
2013-01-20 15:52:15 +00:00
|
|
|
grub_partition_iterate (disk, find_partition, &ctx);
|
2012-01-29 13:28:01 +00:00
|
|
|
if (grub_errno != GRUB_ERR_NONE)
|
|
|
|
{
|
|
|
|
grub_disk_close (disk);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-20 15:52:15 +00:00
|
|
|
if (ctx.partname == NULL)
|
2012-01-29 13:28:01 +00:00
|
|
|
{
|
|
|
|
grub_disk_close (disk);
|
|
|
|
grub_util_info ("cannot find the partition of `%s'", os_dev);
|
|
|
|
grub_error (GRUB_ERR_BAD_DEVICE,
|
|
|
|
"cannot find the partition of `%s'", os_dev);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-20 15:52:15 +00:00
|
|
|
name = grub_xasprintf ("%s,%s", disk->name, ctx.partname);
|
|
|
|
free (ctx.partname);
|
2012-01-29 13:28:01 +00:00
|
|
|
grub_disk_close (disk);
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
grub_util_biosdisk_is_present (const char *os_dev)
|
|
|
|
{
|
2013-10-19 14:29:20 +00:00
|
|
|
int ret = (find_system_device (os_dev) != NULL);
|
2013-09-19 06:48:54 +00:00
|
|
|
grub_util_info ((ret ? "%s is present" : "%s is not present"),
|
|
|
|
os_dev);
|
|
|
|
return ret;
|
2012-01-29 13:28:01 +00:00
|
|
|
}
|
|
|
|
|
2011-07-08 11:33:12 +00:00
|
|
|
#ifdef HAVE_LIBZFS
|
|
|
|
static libzfs_handle_t *__libzfs_handle;
|
|
|
|
|
|
|
|
static void
|
|
|
|
fini_libzfs (void)
|
|
|
|
{
|
|
|
|
libzfs_fini (__libzfs_handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
libzfs_handle_t *
|
|
|
|
grub_get_libzfs_handle (void)
|
|
|
|
{
|
|
|
|
if (! __libzfs_handle)
|
|
|
|
{
|
|
|
|
__libzfs_handle = libzfs_init ();
|
|
|
|
|
|
|
|
if (__libzfs_handle)
|
|
|
|
atexit (fini_libzfs);
|
|
|
|
}
|
|
|
|
|
|
|
|
return __libzfs_handle;
|
|
|
|
}
|
|
|
|
#endif /* HAVE_LIBZFS */
|
|
|
|
|