1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
|
/* getroot.c - Get root device */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 1999,2000,2001,2002,2003,2006,2007,2008,2009,2010,2011 Free Software Foundation, Inc.
*
* GRUB is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GRUB 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
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
*/
#include <config-util.h>
#include <config.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <assert.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <dirent.h>
#include <errno.h>
#include <error.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#ifdef HAVE_LIMITS_H
#include <limits.h>
#endif
#include <grub/util/misc.h>
#include <grub/cryptodisk.h>
#include <grub/i18n.h>
#ifdef __linux__
#include <sys/ioctl.h> /* ioctl */
#include <sys/mount.h>
#endif
#include <sys/types.h>
#if defined(HAVE_LIBZFS) && defined(HAVE_LIBNVPAIR)
# include <grub/util/libzfs.h>
# include <grub/util/libnvpair.h>
#endif
#include <grub/mm.h>
#include <grub/misc.h>
#include <grub/emu/misc.h>
#include <grub/emu/hostdisk.h>
#include <grub/emu/getroot.h>
#if defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
#include <sys/mount.h>
#endif
#if defined(__NetBSD__) || defined(__OpenBSD__)
# include <sys/ioctl.h>
# include <sys/disklabel.h> /* struct disklabel */
# include <sys/disk.h> /* struct dkwedge_info */
#include <sys/param.h>
#include <sys/mount.h>
#endif /* defined(__NetBSD__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) */
#if defined(__NetBSD__)
# include <sys/fdio.h>
#endif
grub_disk_addr_t
grub_util_find_partition_start (const char *dev)
{
#if GRUB_UTIL_FD_STAT_IS_FUNCTIONAL
struct stat st;
grub_disk_addr_t partition_start;
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))
return partition_start;
#endif
return grub_util_find_partition_start_os (dev);
}
void
grub_util_pull_device (const char *os_dev)
{
enum grub_dev_abstraction_types ab;
ab = grub_util_get_dev_abstraction (os_dev);
switch (ab)
{
case GRUB_DEV_ABSTRACTION_LVM:
grub_util_pull_lvm_by_command (os_dev);
/* Fallthrough - in case that lvm-tools are unavailable. */
case GRUB_DEV_ABSTRACTION_LUKS:
grub_util_pull_devmapper (os_dev);
return;
default:
if (grub_util_pull_device_os (os_dev, ab))
return;
/* Fallthrough. */
case GRUB_DEV_ABSTRACTION_NONE:
free (grub_util_biosdisk_get_grub_dev (os_dev));
return;
}
}
char *
grub_util_get_grub_dev (const char *os_dev)
{
char *ret;
grub_util_pull_device (os_dev);
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);
}
int
grub_util_get_dev_abstraction (const char *os_dev)
{
enum grub_dev_abstraction_types ret;
/* User explicitly claims that this drive is visible by BIOS. */
if (grub_util_biosdisk_is_present (os_dev))
return GRUB_DEV_ABSTRACTION_NONE;
/* Check for LVM and LUKS. */
ret = grub_util_get_dm_abstraction (os_dev);
if (ret != GRUB_DEV_ABSTRACTION_NONE)
return ret;
return grub_util_get_dev_abstraction_os (os_dev);
}
static char *
convert_system_partition_to_system_disk (const char *os_dev, int *is_part)
{
#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;
}
*is_part = 0;
if (grub_util_device_is_mapped_stat (&st))
return grub_util_devmapper_part_to_disk (&st, is_part, os_dev);
*is_part = 0;
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
}
static const char *
find_system_device (const char *os_dev)
{
char *os_disk;
const char *drive;
int is_part;
os_disk = convert_system_partition_to_system_disk (os_dev, &is_part);
if (! os_disk)
return NULL;
drive = grub_hostdisk_os_dev_to_grub_drive (os_disk, 0);
free (os_disk);
return drive;
}
static char *
make_device_name (const char *drive)
{
char *ret, *ptr;
const char *iptr;
ret = xcalloc (2, strlen (drive));
ptr = ret;
for (iptr = drive; *iptr; iptr++)
{
if (*iptr == ',' || *iptr == '\\')
*ptr++ = '\\';
*ptr++ = *iptr;
}
*ptr = 0;
return ret;
}
char *
grub_util_get_os_disk (const char *os_dev)
{
int is_part;
grub_util_info ("Looking for %s", os_dev);
return convert_system_partition_to_system_disk (os_dev, &is_part);
}
#if !defined(__APPLE__)
/* 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;
grub_util_info ("Partition %d starts from %" GRUB_HOST_PRIuLONG_LONG,
partition->number, (unsigned long long) partition->start);
part_start = grub_partition_get_start (partition);
if (ctx->start == part_start)
{
/* This is dreadfully hardcoded, but there's a limit to what GRUB
Legacy was able to deal with anyway. */
if (getenv ("GRUB_LEGACY_0_BASED_PARTITIONS"))
{
if (partition->parent)
/* Probably a BSD slice. */
ctx->partname = xasprintf ("%d,%d", partition->parent->number,
partition->number + 1);
else
ctx->partname = xasprintf ("%d", partition->number);
return 1;
}
ctx->partname = grub_partition_get_name (partition);
return 1;
}
return 0;
}
#endif
char *
grub_util_biosdisk_get_grub_dev (const char *os_dev)
{
const char *drive;
char *sys_disk;
int is_part;
grub_util_info ("Looking for %s", os_dev);
sys_disk = convert_system_partition_to_system_disk (os_dev, &is_part);
if (!sys_disk)
return 0;
drive = grub_hostdisk_os_dev_to_grub_drive (sys_disk, 1);
grub_util_info ("%s is a parent of %s", sys_disk, os_dev);
if (!is_part)
{
free (sys_disk);
return make_device_name (drive);
}
free (sys_disk);
#if defined(__APPLE__)
/* Apple uses "/dev/r?disk[0-9]+(s[0-9]+)?". */
/*
* Note: we do not use the new partition naming scheme as dos_part does not
* necessarily correspond to an msdos partition.
*/
{
const char *p;
char *dri, *ret;
int part;
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);
dri = make_device_name (drive);
if (!disk && !rdisk)
return dri;
p = os_dev + sizeof ("/dev/disk") + rdisk - 1;
while (*p >= '0' && *p <= '9')
p++;
if (*p != 's')
return dri;
p++;
part = strtol (p, NULL, 10);
if (part == 0)
return dri;
ret = xasprintf ("%s,%d", dri, part);
free (dri);
return ret;
}
#else
/* 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. */
{
char *name;
grub_disk_t disk;
struct grub_util_biosdisk_get_grub_dev_ctx ctx;
name = make_device_name (drive);
ctx.start = grub_util_find_partition_start (os_dev);
if (grub_errno != GRUB_ERR_NONE)
{
free (name);
return 0;
}
#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]?)?". */
/*
* Note: we do not use the new partition naming scheme as dos_part does not
* necessarily correspond to an msdos partition.
*/
if (ctx.start == (grub_disk_addr_t) -1)
{
char *p;
char *dri;
dri = make_device_name (drive);
p = strrchr (os_dev + sizeof ("/dev/hd") - 1, 's');
if (p)
{
long int n;
char *q;
p++;
n = strtol (p, &q, 10);
if (p != q && n > 0 && n != GRUB_LONG_MAX)
{
char *t;
t = dri;
if (*q >= 'a' && *q <= 'g')
dri = xasprintf ("%s,%ld,%d", t, n, *q - 'a' + 1);
else
dri = xasprintf ("%s,%ld", t, n);
free (t);
}
}
return dri;
}
#endif
grub_util_info ("%s starts from %" GRUB_HOST_PRIuLONG_LONG,
os_dev, (unsigned long long) ctx.start);
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)
{
char *canon;
grub_util_warn
(_("disk does not exist, so falling back to partition device %s"),
os_dev);
grub_errno = GRUB_ERR_NONE;
canon = grub_canonicalize_file_name (os_dev);
drive = grub_hostdisk_os_dev_to_grub_drive (canon ? : os_dev, 1);
if (canon)
free (canon);
return make_device_name (drive);
}
else
return 0;
}
name = grub_util_get_ldm (disk, ctx.start);
if (name)
{
grub_disk_close (disk);
return name;
}
ctx.partname = NULL;
grub_partition_iterate (disk, find_partition, &ctx);
if (grub_errno != GRUB_ERR_NONE)
{
grub_disk_close (disk);
return 0;
}
if (ctx.partname == NULL)
{
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;
}
name = grub_xasprintf ("%s,%s", disk->name, ctx.partname);
free (ctx.partname);
grub_disk_close (disk);
return name;
}
#endif
}
int
grub_util_biosdisk_is_present (const char *os_dev)
{
int ret = (find_system_device (os_dev) != NULL);
grub_util_info ((ret ? "%s is present" : "%s is not present"),
os_dev);
return ret;
}
#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 */
|