Make grub_util_devmapper_part_to_disk and grub_util_find_partition_start

follow the same algorithm to avoid method mismatch. Don't assume
	DMRAID- UUID to mean full disk but instead check that mapping is linear.
This commit is contained in:
Vladimir Serbinenko 2013-12-24 14:16:57 +01:00
parent da98622662
commit e88f0420b9
6 changed files with 35 additions and 127 deletions

View file

@ -1,3 +1,9 @@
2013-12-24 Vladimir Serbinenko <phcoder@gmail.com>
Make grub_util_devmapper_part_to_disk and grub_util_find_partition_start
follow the same algorithm to avoid method mismatch. Don't assume
DMRAID- UUID to mean full disk but instead check that mapping is linear.
2013-12-24 Vladimir Serbinenko <phcoder@gmail.com> 2013-12-24 Vladimir Serbinenko <phcoder@gmail.com>
Declare GRUB_EFI_VENDOR_APPLE_GUID. Declare GRUB_EFI_VENDOR_APPLE_GUID.

View file

@ -198,124 +198,17 @@ char *
grub_util_devmapper_part_to_disk (struct stat *st, grub_util_devmapper_part_to_disk (struct stat *st,
int *is_part, const char *path) int *is_part, const char *path)
{ {
struct dm_tree *tree; int major, minor;
uint32_t maj, min;
struct dm_tree_node *node = NULL, *child;
void *handle;
const char *node_uuid, *mapper_name = NULL, *child_uuid, *child_name;
tree = dm_tree_create (); if (grub_util_get_dm_node_linear_info (st->st_rdev,
if (! tree) &major, &minor, 0))
{ {
grub_util_info ("dm_tree_create failed"); *is_part = 1;
goto devmapper_out; return grub_find_device ("/dev",
(major << 8) | minor);
} }
*is_part = 0;
maj = major (st->st_rdev); return xstrdup (path);
min = minor (st->st_rdev);
if (! dm_tree_add_dev (tree, maj, min))
{
grub_util_info ("dm_tree_add_dev failed");
goto devmapper_out;
}
node = dm_tree_find_node (tree, maj, min);
if (! node)
{
grub_util_info ("dm_tree_find_node failed");
goto devmapper_out;
}
reiterate:
node_uuid = dm_tree_node_get_uuid (node);
if (! node_uuid)
{
grub_util_info ("%s has no DM uuid", path);
goto devmapper_out;
}
if (strncmp (node_uuid, "LVM-", 4) == 0)
{
grub_util_info ("%s is an LVM", path);
goto devmapper_out;
}
if (strncmp (node_uuid, "mpath-", 6) == 0)
{
/* Multipath partitions have partN-mpath-* UUIDs, and are
linear mappings so are handled by
grub_util_get_dm_node_linear_info. Multipath disks are not
linear mappings and must be handled specially. */
grub_util_info ("%s is a multipath disk", path);
goto devmapper_out;
}
if (strncmp (node_uuid, "DMRAID-", 7) != 0)
{
int major, minor;
const char *node_name;
grub_util_info ("%s is not DM-RAID", path);
if ((node_name = dm_tree_node_get_name (node))
&& grub_util_get_dm_node_linear_info (node_name,
&major, &minor, 0))
{
*is_part = 1;
if (tree)
dm_tree_free (tree);
char *ret = grub_find_device ("/dev",
(major << 8) | minor);
return ret;
}
goto devmapper_out;
}
handle = NULL;
/* Counter-intuitively, device-mapper refers to the disk-like
device containing a DM-RAID partition device as a "child" of
the partition device. */
child = dm_tree_next_child (&handle, node, 0);
if (! child)
{
grub_util_info ("%s has no DM children", path);
goto devmapper_out;
}
child_uuid = dm_tree_node_get_uuid (child);
if (! child_uuid)
{
grub_util_info ("%s child has no DM uuid", path);
goto devmapper_out;
}
else if (strncmp (child_uuid, "DMRAID-", 7) != 0)
{
grub_util_info ("%s child is not DM-RAID", path);
goto devmapper_out;
}
child_name = dm_tree_node_get_name (child);
if (! child_name)
{
grub_util_info ("%s child has no DM name", path);
goto devmapper_out;
}
mapper_name = child_name;
*is_part = 1;
node = child;
goto reiterate;
devmapper_out:
if (! mapper_name && node)
{
/* This is a DM-RAID disk, not a partition. */
mapper_name = dm_tree_node_get_name (node);
if (! mapper_name)
grub_util_info ("%s has no DM name", path);
}
char *ret;
if (mapper_name)
ret = xasprintf ("/dev/mapper/%s", mapper_name);
else
ret = NULL;
if (tree)
dm_tree_free (tree);
return ret;
} }
int int

View file

@ -83,7 +83,7 @@ grub_util_device_is_mapped (const char *dev)
} }
int int
grub_util_get_dm_node_linear_info (const char *dev, grub_util_get_dm_node_linear_info (dev_t dev,
int *maj, int *min, int *maj, int *min,
grub_disk_addr_t *st) grub_disk_addr_t *st)
{ {
@ -97,14 +97,16 @@ grub_util_get_dm_node_linear_info (const char *dev,
grub_disk_addr_t partstart = 0; grub_disk_addr_t partstart = 0;
const char *node_uuid; const char *node_uuid;
major = major (dev);
minor = minor (dev);
while (1) while (1)
{ {
dmt = dm_task_create(DM_DEVICE_TABLE); dmt = dm_task_create(DM_DEVICE_TABLE);
if (!dmt) if (!dmt)
break; break;
if (! (first ? dm_task_set_name (dmt, dev) if (! (dm_task_set_major_minor (dmt, major, minor, 0)))
: dm_task_set_major_minor (dmt, major, minor, 0)))
{ {
dm_task_destroy (dmt); dm_task_destroy (dmt);
break; break;
@ -117,8 +119,7 @@ grub_util_get_dm_node_linear_info (const char *dev,
} }
node_uuid = dm_task_get_uuid (dmt); node_uuid = dm_task_get_uuid (dmt);
if (node_uuid && (strncmp (node_uuid, "LVM-", 4) == 0 if (node_uuid && (strncmp (node_uuid, "LVM-", 4) == 0
|| strncmp (node_uuid, "mpath-", 6) == 0 || strncmp (node_uuid, "mpath-", 6) == 0))
|| strncmp (node_uuid, "DMRAID-", 7) == 0))
{ {
dm_task_destroy (dmt); dm_task_destroy (dmt);
break; break;
@ -190,7 +191,7 @@ grub_util_device_is_mapped (const char *dev __attribute__ ((unused)))
} }
int int
grub_util_get_dm_node_linear_info (const char *dev __attribute__ ((unused)), grub_util_get_dm_node_linear_info (dev_t dev __attribute__ ((unused)),
int *maj __attribute__ ((unused)), int *maj __attribute__ ((unused)),
int *min __attribute__ ((unused)), int *min __attribute__ ((unused)),
grub_disk_addr_t *st __attribute__ ((unused))) grub_disk_addr_t *st __attribute__ ((unused)))

View file

@ -288,6 +288,7 @@ grub_hostdisk_linux_find_partition (char *dev, grub_disk_addr_t sector)
{ {
grub_util_fd_t fd; grub_util_fd_t fd;
grub_disk_addr_t start; grub_disk_addr_t start;
struct stat st;
sprintf (p, format, i); sprintf (p, format, i);
@ -300,14 +301,16 @@ grub_hostdisk_linux_find_partition (char *dev, grub_disk_addr_t sector)
return 0; return 0;
} }
missing = 0; missing = 0;
close (fd);
if (!grub_util_device_is_mapped (real_dev) if (fstat (fd, &st) < 0
|| !grub_util_get_dm_node_linear_info (real_dev, 0, 0, &start)) || !grub_util_device_is_mapped_stat (&st)
|| !grub_util_get_dm_node_linear_info (st.st_rdev, 0, 0, &start))
start = grub_util_find_partition_start_os (real_dev); start = grub_util_find_partition_start_os (real_dev);
/* We don't care about errors here. */ /* We don't care about errors here. */
grub_errno = GRUB_ERR_NONE; grub_errno = GRUB_ERR_NONE;
close (fd);
if (start == sector) if (start == sector)
{ {
struct linux_partition_cache *new_cache_item; struct linux_partition_cache *new_cache_item;

View file

@ -61,7 +61,7 @@ char *
grub_util_get_os_disk (const char *os_dev); grub_util_get_os_disk (const char *os_dev);
int int
grub_util_get_dm_node_linear_info (const char *dev, grub_util_get_dm_node_linear_info (dev_t dev,
int *maj, int *min, int *maj, int *min,
grub_disk_addr_t *st); grub_disk_addr_t *st);

View file

@ -77,10 +77,15 @@
grub_disk_addr_t grub_disk_addr_t
grub_util_find_partition_start (const char *dev) grub_util_find_partition_start (const char *dev)
{ {
#if GRUB_UTIL_FD_STAT_IS_FUNCTIONAL
struct stat st;
grub_disk_addr_t partition_start; grub_disk_addr_t partition_start;
if (grub_util_device_is_mapped (dev)
&& grub_util_get_dm_node_linear_info (dev, 0, 0, &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; return partition_start;
#endif
return grub_util_find_partition_start_os (dev); return grub_util_find_partition_start_os (dev);
} }