From c0e103e4dacffd9d9348cc753eaf89405564df98 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Mon, 8 Nov 2010 16:51:50 +0100 Subject: [PATCH 1/6] Support for partitioned loop devices. Improved devmapper support --- grub-core/kern/emu/getroot.c | 88 +++++++++++++++++++++++++---------- grub-core/kern/emu/hostdisk.c | 71 +++++++++++++++++++++++++++- 2 files changed, 133 insertions(+), 26 deletions(-) diff --git a/grub-core/kern/emu/getroot.c b/grub-core/kern/emu/getroot.c index 0433d49ed..a99bee9a4 100644 --- a/grub-core/kern/emu/getroot.c +++ b/grub-core/kern/emu/getroot.c @@ -32,6 +32,10 @@ #include #include +#ifdef HAVE_DEVICE_MAPPER +# include +#endif + #ifdef __GNU__ #include #include @@ -560,30 +564,66 @@ grub_guess_root_device (const char *dir) } static int -grub_util_is_dmraid (const char *os_dev) +grub_util_is_lvm (const char *os_dev) { - if (! strncmp (os_dev, "/dev/mapper/nvidia_", 19)) - return 1; - else if (! strncmp (os_dev, "/dev/mapper/isw_", 16)) - return 1; - else if (! strncmp (os_dev, "/dev/mapper/hpt37x_", 19)) - return 1; - else if (! strncmp (os_dev, "/dev/mapper/hpt45x_", 19)) - return 1; - else if (! strncmp (os_dev, "/dev/mapper/via_", 16)) - return 1; - else if (! strncmp (os_dev, "/dev/mapper/lsi_", 16)) - return 1; - else if (! strncmp (os_dev, "/dev/mapper/pdc_", 16)) - return 1; - else if (! strncmp (os_dev, "/dev/mapper/jmicron_", 20)) - return 1; - else if (! strncmp (os_dev, "/dev/mapper/asr_", 16)) - return 1; - else if (! strncmp (os_dev, "/dev/mapper/sil_", 16)) - return 1; + if ((strncmp ("/dev/mapper/", os_dev, 12) != 0)) + return 0; + +#ifdef HAVE_DEVICE_MAPPER + { + struct dm_tree *tree; + uint32_t maj, min; + struct dm_tree_node *node = NULL, *child; + void *handle; + const char *node_uuid, *mapper_name = NULL, *child_uuid, *child_name; + struct stat st; - return 0; + if (stat (os_dev, &st) < 0) + return 0; + + tree = dm_tree_create (); + if (! tree) + { + grub_printf ("Failed to create tree\n"); + grub_dprintf ("hostdisk", "dm_tree_create failed\n"); + return 0; + } + + maj = major (st.st_rdev); + min = minor (st.st_rdev); + + if (! dm_tree_add_dev (tree, maj, min)) + { + grub_dprintf ("hostdisk", "dm_tree_add_dev failed\n"); + dm_tree_free (tree); + return 0; + } + + node = dm_tree_find_node (tree, maj, min); + if (! node) + { + grub_dprintf ("hostdisk", "dm_tree_find_node failed\n"); + dm_tree_free (tree); + return 0; + } + node_uuid = dm_tree_node_get_uuid (node); + if (! node_uuid) + { + grub_dprintf ("hostdisk", "%s has no DM uuid\n", os_dev); + dm_tree_free (tree); + return 0; + } + if (strncmp (node_uuid, "LVM-", 4) != 0) + { + dm_tree_free (tree); + return 0; + } + dm_tree_free (tree); + return 1; + } +#else + return 1; +#endif /* HAVE_DEVICE_MAPPER */ } int @@ -595,9 +635,7 @@ grub_util_get_dev_abstraction (const char *os_dev __attribute__((unused))) return GRUB_DEV_ABSTRACTION_NONE; /* Check for LVM. */ - if (!strncmp (os_dev, "/dev/mapper/", 12) - && ! grub_util_is_dmraid (os_dev) - && strncmp (os_dev, "/dev/mapper/mpath", 17) != 0) + if (grub_util_is_lvm (os_dev)) return GRUB_DEV_ABSTRACTION_LVM; /* Check for RAID. */ diff --git a/grub-core/kern/emu/hostdisk.c b/grub-core/kern/emu/hostdisk.c index 12dbe7469..2a798a3cd 100644 --- a/grub-core/kern/emu/hostdisk.c +++ b/grub-core/kern/emu/hostdisk.c @@ -1035,6 +1035,55 @@ make_device_name (int drive, int dos_part, int bsd_part) return ret; } +#ifdef HAVE_DEVICE_MAPPER +static int +grub_util_get_dm_node_linear_info (const char *dev, + int *maj, int *min) +{ + struct dm_task *dmt; + void *next = NULL; + uint64_t length, start; + char *target, *params; + const char *node_name; + char *ptr; + int major, minor; + + dmt = dm_task_create(DM_DEVICE_TABLE); + if (!dmt) + return 0; + + if (!dm_task_set_name(dmt, dev)) + return 0; + dm_task_no_open_count(dmt); + if (!dm_task_run(dmt)) + return 0; + next = dm_get_next_target(dmt, next, &start, &length, + &target, ¶ms); + if (grub_strcmp (target, "linear") != 0) + return 0; + major = grub_strtoul (params, &ptr, 10); + if (grub_errno) + { + grub_errno = GRUB_ERR_NONE; + return 0; + } + if (*ptr != ':') + return 0; + ptr++; + minor = grub_strtoul (ptr, 0, 10); + if (grub_errno) + { + grub_errno = GRUB_ERR_NONE; + return 0; + } + if (maj) + *maj = major; + if (min) + *min = minor; + return 1; +} +#endif + static char * convert_system_partition_to_system_disk (const char *os_dev, struct stat *st) { @@ -1211,9 +1260,29 @@ convert_system_partition_to_system_disk (const char *os_dev, struct stat *st) node = NULL; goto devmapper_out; } - else if (strncmp (node_uuid, "DMRAID-", 7) != 0) + if (strncmp (node_uuid, "LVM-", 4) == 0) { + grub_dprintf ("hostdisk", "%s is an LVM\n", path); + node = NULL; + goto devmapper_out; + } + if (strncmp (node_uuid, "DMRAID-", 7) != 0) + { + int major, minor; + const char *node_name; grub_dprintf ("hostdisk", "%s is not DM-RAID\n", path); + + if ((node_name = dm_tree_node_get_name (node)) + && grub_util_get_dm_node_linear_info (node_name, + &major, &minor)) + { + if (tree) + dm_tree_free (tree); + free (path); + char *ret = grub_find_device (NULL, (major << 8) | minor); + return ret; + } + node = NULL; goto devmapper_out; } From 304e349b7fcfeaaa2a340c0adf30a281b82e62ff Mon Sep 17 00:00:00 2001 From: Colin Watson Date: Wed, 5 Jan 2011 11:41:12 +0000 Subject: [PATCH 2/6] include for grub_find_device --- grub-core/kern/emu/hostdisk.c | 1 + 1 file changed, 1 insertion(+) diff --git a/grub-core/kern/emu/hostdisk.c b/grub-core/kern/emu/hostdisk.c index 2a798a3cd..b21ac3285 100644 --- a/grub-core/kern/emu/hostdisk.c +++ b/grub-core/kern/emu/hostdisk.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include From 6383772c926fb299953c8830b0d115b55c110f0d Mon Sep 17 00:00:00 2001 From: Colin Watson Date: Thu, 6 Jan 2011 18:08:01 +0000 Subject: [PATCH 3/6] remove unused variable --- grub-core/kern/emu/hostdisk.c | 1 - 1 file changed, 1 deletion(-) diff --git a/grub-core/kern/emu/hostdisk.c b/grub-core/kern/emu/hostdisk.c index b21ac3285..ee3cc0a36 100644 --- a/grub-core/kern/emu/hostdisk.c +++ b/grub-core/kern/emu/hostdisk.c @@ -1045,7 +1045,6 @@ grub_util_get_dm_node_linear_info (const char *dev, void *next = NULL; uint64_t length, start; char *target, *params; - const char *node_name; char *ptr; int major, minor; From 4b0782664739170dc3688a55e667a56146128e78 Mon Sep 17 00:00:00 2001 From: Vladimir 'phcoder' Serbinenko Date: Sat, 8 Jan 2011 16:33:49 +0100 Subject: [PATCH 4/6] partitioned mdraid support (untested) --- grub-core/kern/emu/getroot.c | 2 +- grub-core/kern/emu/hostdisk.c | 15 ++++++++++----- include/grub/emu/misc.h | 2 ++ 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/grub-core/kern/emu/getroot.c b/grub-core/kern/emu/getroot.c index a99bee9a4..0035c734a 100644 --- a/grub-core/kern/emu/getroot.c +++ b/grub-core/kern/emu/getroot.c @@ -639,7 +639,7 @@ grub_util_get_dev_abstraction (const char *os_dev __attribute__((unused))) return GRUB_DEV_ABSTRACTION_LVM; /* Check for RAID. */ - if (!strncmp (os_dev, "/dev/md", 7)) + if (!strncmp (os_dev, "/dev/md", 7) && ! grub_util_device_is_mapped (os_dev)) return GRUB_DEV_ABSTRACTION_RAID; #endif diff --git a/grub-core/kern/emu/hostdisk.c b/grub-core/kern/emu/hostdisk.c index ee3cc0a36..eb84d0220 100644 --- a/grub-core/kern/emu/hostdisk.c +++ b/grub-core/kern/emu/hostdisk.c @@ -325,18 +325,23 @@ grub_util_biosdisk_open (const char *name, grub_disk_t disk) return GRUB_ERR_NONE; } -#ifdef HAVE_DEVICE_MAPPER -static int -device_is_mapped (const char *dev) +int +grub_util_device_is_mapped (const char *dev) { +#ifdef HAVE_DEVICE_MAPPER struct stat st; + if (!grub_device_mapper_supported ()) + return 0; + if (stat (dev, &st) < 0) return 0; return dm_is_dm_major (major (st.st_rdev)); -} +#else + return 0; #endif /* HAVE_DEVICE_MAPPER */ +} #if defined(__linux__) || defined(__CYGWIN__) || defined(HAVE_DIOCGDINFO) static grub_disk_addr_t @@ -351,7 +356,7 @@ find_partition_start (const char *dev) # endif /* !defined(HAVE_DIOCGDINFO) */ # ifdef HAVE_DEVICE_MAPPER - if (grub_device_mapper_supported () && device_is_mapped (dev)) { + if (grub_util_device_is_mapped (dev)) { struct dm_task *task = NULL; grub_uint64_t start, length; char *target_type, *params, *space; diff --git a/include/grub/emu/misc.h b/include/grub/emu/misc.h index ef0d18300..03909e025 100644 --- a/include/grub/emu/misc.h +++ b/include/grub/emu/misc.h @@ -54,6 +54,8 @@ void grub_find_zpool_from_dir (const char *dir, char *grub_make_system_path_relative_to_its_root (const char *path) __attribute__ ((warn_unused_result)); +int +grub_util_device_is_mapped (const char *dev); void * EXPORT_FUNC(xmalloc) (grub_size_t size) __attribute__ ((warn_unused_result)); void * EXPORT_FUNC(xrealloc) (void *ptr, grub_size_t size) __attribute__ ((warn_unused_result)); From 307ed0b484330ed600cb01e33f45f6a974aae7c1 Mon Sep 17 00:00:00 2001 From: Colin Watson Date: Tue, 25 Jan 2011 12:28:11 +0000 Subject: [PATCH 5/6] remove unused variables --- grub-core/kern/emu/getroot.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/grub-core/kern/emu/getroot.c b/grub-core/kern/emu/getroot.c index 0035c734a..4f8a0e8a7 100644 --- a/grub-core/kern/emu/getroot.c +++ b/grub-core/kern/emu/getroot.c @@ -573,9 +573,8 @@ grub_util_is_lvm (const char *os_dev) { struct dm_tree *tree; uint32_t maj, min; - struct dm_tree_node *node = NULL, *child; - void *handle; - const char *node_uuid, *mapper_name = NULL, *child_uuid, *child_name; + struct dm_tree_node *node = NULL; + const char *node_uuid; struct stat st; if (stat (os_dev, &st) < 0) From 118fb264ee5e92e43a57a82a7a262a08a850b8b3 Mon Sep 17 00:00:00 2001 From: Colin Watson Date: Tue, 25 Jan 2011 16:51:23 +0000 Subject: [PATCH 6/6] Support probing multipath disks. --- grub-core/kern/emu/hostdisk.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/grub-core/kern/emu/hostdisk.c b/grub-core/kern/emu/hostdisk.c index eb84d0220..ea42d945e 100644 --- a/grub-core/kern/emu/hostdisk.c +++ b/grub-core/kern/emu/hostdisk.c @@ -1271,6 +1271,16 @@ convert_system_partition_to_system_disk (const char *os_dev, struct stat *st) node = NULL; 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_dprintf ("hostdisk", "%s is a multipath disk\n", path); + mapper_name = dm_tree_node_get_name (node); + goto devmapper_out; + } if (strncmp (node_uuid, "DMRAID-", 7) != 0) { int major, minor;