From 163a834d05a138fa5e9a3558795ce2f7dbb5a7ac Mon Sep 17 00:00:00 2001 From: David Zeuthen Date: Tue, 12 Jan 2010 05:56:33 -0500 Subject: [PATCH] Get the LVM LV UUIDs from sysfs to avoid using liblvm2 in the dm prober --- src/device.c | 50 ++++++++++--- src/probers/udisks-dm-export.c | 156 ----------------------------------------- 2 files changed, 42 insertions(+), 164 deletions(-) diff --git a/src/device.c b/src/device.c index 006e70a..9cfd513 100644 --- a/src/device.c +++ b/src/device.c @@ -2844,24 +2844,56 @@ update_info_luks_cleartext (Device *device) /* ---------------------------------------------------------------------------------------------------- */ +static gchar * +extract_lvm_uuid (const gchar *s) +{ + GString *str; + + if (s == NULL || strlen (s) < 32) + return NULL; + + str = g_string_new_len (s, 6); g_string_append_c (str, '-'); s += 6; + g_string_append_len (str, s, 4); g_string_append_c (str, '-'); s += 4; + g_string_append_len (str, s, 4); g_string_append_c (str, '-'); s += 4; + g_string_append_len (str, s, 4); g_string_append_c (str, '-'); s += 4; + g_string_append_len (str, s, 4); g_string_append_c (str, '-'); s += 4; + g_string_append_len (str, s, 4); g_string_append_c (str, '-'); s += 4; + g_string_append_len (str, s, 6); + + return g_string_free (str, FALSE); +} + /* update device_is_linux_lvm2_lv and linux_lvm2_lv_* properties */ static gboolean update_info_linux_lvm2_lv (Device *device) { const gchar *lv_name; - const gchar *lv_uuid; const gchar *vg_name; - const gchar *vg_uuid; + const gchar *uuid; + gchar *lv_uuid; + gchar *vg_uuid; gboolean is_lv; - lv_name = g_udev_device_get_property (device->priv->d, "UDISKS_LVM2_LV_NAME"); - lv_uuid = g_udev_device_get_property (device->priv->d, "UDISKS_LVM2_LV_UUID"); - vg_name = g_udev_device_get_property (device->priv->d, "UDISKS_LVM2_LV_VG_NAME"); - vg_uuid = g_udev_device_get_property (device->priv->d, "UDISKS_LVM2_LV_VG_UUID"); - is_lv = FALSE; + lv_uuid = NULL; + vg_uuid = NULL; - if (lv_name == NULL) + lv_name = g_udev_device_get_property (device->priv->d, "DM_LV_NAME"); + vg_name = g_udev_device_get_property (device->priv->d, "DM_VG_NAME"); + + if (lv_name == NULL || vg_name == NULL) + goto out; + + uuid = g_udev_device_get_sysfs_attr (device->priv->d, "dm/uuid"); + if (uuid == NULL || !g_str_has_prefix (uuid, "LVM-")) + goto out; + + vg_uuid = extract_lvm_uuid (uuid + 4); + if (vg_uuid == NULL) + goto out; + + lv_uuid = extract_lvm_uuid (uuid + 4 + 32); + if (lv_uuid == NULL) goto out; is_lv = TRUE; @@ -2874,6 +2906,8 @@ update_info_linux_lvm2_lv (Device *device) out: device_set_device_is_linux_lvm2_lv (device, is_lv); + g_free (vg_uuid); + g_free (lv_uuid); return TRUE; } diff --git a/src/probers/udisks-dm-export.c b/src/probers/udisks-dm-export.c index 6d4c38e..4f05b13 100644 --- a/src/probers/udisks-dm-export.c +++ b/src/probers/udisks-dm-export.c @@ -10,9 +10,6 @@ #include -static gchar *vg_uuid = NULL; -static gchar *lv_uuid = NULL; - static void usage (void) { @@ -82,11 +79,6 @@ dm_export (int major, int minor) if (uuid != NULL) { g_print ("UDISKS_DM_UUID=%s\n", uuid); - if (g_str_has_prefix (uuid, "LVM-") && strlen (uuid) == 4 + 32 + 32) - { - vg_uuid = g_strndup (uuid + 4, 32); - lv_uuid = g_strndup (uuid + 4 + 32, 32); - } } if (!info.exists) @@ -149,145 +141,6 @@ dm_export (int major, int minor) /* ---------------------------------------------------------------------------------------------------- */ -static void -strip_hyphens (gchar *s) -{ - gchar *p; - - g_return_if_fail (s != NULL); - - while ((p = strstr (s, "-")) != NULL) - { - gsize len; - - len = strlen (p + 1); - g_memmove (p, p + 1, len); - p[len] = '\0'; - } -} - -static vg_t -find_vg (lvm_t lvm_ctx, - const gchar *vg_uuid) -{ - vg_t ret; - struct dm_list *vg_names; - struct lvm_str_list *str_list; - - ret = NULL; - - vg_names = lvm_list_vg_names (lvm_ctx); - dm_list_iterate_items (str_list, vg_names) - { - vg_t vg; - - vg = lvm_vg_open (lvm_ctx, str_list->str, "r", 0); - if (vg != NULL) - { - char *uuid; - - uuid = lvm_vg_get_uuid (vg); - if (uuid != NULL) - { - /* gah, remove hyphens so we can match things up... */ - strip_hyphens (uuid); - if (g_strcmp0 (uuid, vg_uuid) == 0) - { - ret = vg; - dm_free (uuid); - goto out; - } - } - if (uuid != NULL) - dm_free (uuid); - lvm_vg_close (vg); - } - } - - out: - return ret; -} - -static gboolean -lvm_export (void) -{ - gboolean ret; - lvm_t lvm_ctx; - vg_t vg; - lv_t our_lv; - char *s; - struct dm_list *lvs; - struct lvm_lv_list *lv_list; - - ret = FALSE; - lvm_ctx = NULL; - vg = NULL; - - lvm_ctx = lvm_init (NULL); - if (lvm_ctx == NULL) - { - perror ("lvm_init"); - goto out; - } - - ret = TRUE; - - vg = find_vg (lvm_ctx, vg_uuid); - if (vg == NULL) - { - g_printerr ("Cannot find VG for uuid %s\n", vg_uuid); - goto out; - } - - /* Print information about the VG that the LV is part of */ - s = lvm_vg_get_uuid (vg); g_print ("UDISKS_LVM2_LV_VG_UUID=%s\n", s); dm_free (s); - s = lvm_vg_get_name (vg); g_print ("UDISKS_LVM2_LV_VG_NAME=%s\n", s); dm_free (s); - - /* Find the LV object */ - our_lv = NULL; - lvs = lvm_vg_list_lvs (vg); - if (lvs == NULL) - { - g_printerr ("Cannot find any LVs for VG with uuid %s\n", vg_uuid); - goto out; - } - dm_list_iterate_items (lv_list, lvs) - { - lv_t lv = lv_list->lv; - char *uuid; - - uuid = lvm_lv_get_uuid (lv); - if (uuid != NULL) - { - strip_hyphens (uuid); - if (g_strcmp0 (uuid, lv_uuid) == 0) - { - our_lv = lv; - } - dm_free (uuid); - } - } - - if (our_lv == NULL) - { - g_printerr ("Cannot find LV for uuid %s\n", lv_uuid); - goto out; - } - - /* Finally print information about the LV itself */ - s = lvm_lv_get_uuid (our_lv); g_print ("UDISKS_LVM2_LV_UUID=%s\n", s); dm_free (s); - s = lvm_lv_get_name (our_lv); g_print ("UDISKS_LVM2_LV_NAME=%s\n", s); dm_free (s); - - out: - if (vg != NULL) - lvm_vg_close (vg); - if (lvm_ctx != NULL) - lvm_quit (lvm_ctx); - return ret; -} - -/* ---------------------------------------------------------------------------------------------------- */ - int main (int argc, char *argv[]) @@ -323,17 +176,8 @@ main (int argc, if (!dm_export (major, minor)) goto out; - /* If the mapped device is a LVM Logical Volume, export information about the LV and VG */ - if (vg_uuid != NULL && lv_uuid != NULL) - { - if (!lvm_export ()) - goto out; - } - ret = 0; out: - g_free (vg_uuid); - g_free (lv_uuid); return ret; } -- 2.7.4