Skip to contentSkip to conversation

multipath-tool udev rules require missing dmsetup features

DISCUSSION Brian De Wolf 5 participants 7 messages Jul 19, 2007 – Aug 13, 2007 Device mapper

lore
Share & tools
lore
#0 lore raw

Hello,

Recently I've been using the latest git versions of multipath-tools and I've noticed that the udev rules that are included, specifically those for kpartx, refer to environment variables such as DM_TABLE_STATE, DM_PART, DM_UUID, etc. However, the kpartx.rules file contains nothing that would bring those environment variables into the environment. After some googling, I found
http://ftp.frugalware.org/pub/frugalware/frugalware-current/source/lib/device-mapper/
which has a patch that adds a command to dmsetup called 'export'. I've found various udev scripts that use this command to pull the environment variables I listed before for use in udev rules and the multipath-tools kpartx.rules file itself works perfectly when the correct 'dmsetup export' command is imported.

What really puzzles me is that this patch was very difficult to find, but is essential to the 'official' multipath-tools udev rules. Is there something that I'm missing that makes this all make sense?

Thanks!
Brian De Wolf

#1 lore raw
Quoted 13 lines · Brian De Wolf

Hello,

Recently I've been using the latest git versions of multipath-tools and I've noticed that the udev rules that are included, specifically those for kpartx, refer to environment variables such as DM_TABLE_STATE, DM_PART, DM_UUID, etc. However, the kpartx.rules file contains nothing that would bring those environment variables into the environment. After some googling, I found
http://ftp.frugalware.org/pub/frugalware/frugalware-current/source/lib/device-mapper/
which has a patch that adds a command to dmsetup called 'export'. I've found various udev scripts that use this command to pull the environment variables I listed before for use in udev rules and the multipath-tools kpartx.rules file itself works perfectly when the correct 'dmsetup export' command is imported.

Errm. Oops.

Well, I'm to blame here. When creating the rules I totally forgot that we've hacked dmsetup to provide an 'export' switch.

Quoted 4 lines

What really puzzles me is that this patch was very difficult to find, but is essential to the 'official' multipath-tools udev rules. Is there something that I'm missing that makes this all make sense?

As already said, it totally makes sense when you hack up dmsetup and then forget about the patch. Sorry about this.

Alasdair, care to incorporate it?

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke		      zSeries & Storage
[email protected]			      +49 911 74053 688
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 N�rnberg
GF: Markus Rex, HRB 16746 (AG N�rnberg)
dmsetup/dmsetup.c +89 -0
index 25f14a6..b693008 100644
@@ -995,6 +995,94 @@ static int _status(int argc, char **argv
 	return r;
 }
 
+static int _export(int argc, char **argv, void *data)
+{
+	int r = 0;
+	struct dm_task *dmt = NULL;
+	void *next = NULL;
+	uint64_t start, length;
+	char *target_type = NULL;
+	char *params;
+	struct dm_names *names = (struct dm_names *) data;
+	const char *name = NULL;
+	const char *uuid = NULL;
+	struct dm_info info;
+
+	if (data)
+		name = names->name;
+	else if (argc == 2)
+		name = argv[1];
+
+	if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
+		goto out;
+
+	if (!_set_task_device(dmt, name, 0))
+		goto out;
+
+	if (!dm_task_run(dmt))
+		goto out;
+
+	if (!dm_task_get_info(dmt, &info) || !info.exists)
+		goto out;
+
+	if (!name)
+		name = dm_task_get_name(dmt);
+
+	uuid = dm_task_get_uuid(dmt);
+	printf("DM_NAME=%s\n", name);
+
+	if ((uuid = dm_task_get_uuid(dmt)) && *uuid)
+		printf("DM_UUID=%s\n", uuid);
+
+	if (!info.exists) {
+		printf("DM_STATE=NOTPRESENT\n");
+		goto out;
+	}
+
+	printf("DM_STATE=%s\n",
+	       info.suspended ? "SUSPENDED" :
+	       (info.read_only ? " READONLY" : "ACTIVE"));
+
+	if (!info.live_table && !info.inactive_table)
+		printf("DM_TABLE_STATE=NONE\n");
+	else
+		printf("DM_TABLE_STATE=%s%s%s\n",
+		       info.live_table ? "LIVE" : "",
+		       info.live_table && info.inactive_table ? "/" : "",
+		       info.inactive_table ? "INACTIVE" : "");
+
+	if (info.open_count != -1)
+		printf("DM_OPENCOUNT=%d\n", info.open_count);
+
+	printf("DM_LAST_EVENT_NR=%" PRIu32 "\n", info.event_nr);
+
+	printf("DM_MAJOR=%d\n", info.major);
+	printf("DM_MINOR=%d\n", info.minor);
+
+	if (info.target_count != -1)
+		printf("DM_TARGET_COUNT=%d\n", info.target_count);
+
+	/* export all table types */
+	next = dm_get_next_target(dmt, next, &start, &length,
+				  &target_type, &params);
+	if (target_type) {
+		printf("DM_TARGET_TYPES=%s", target_type);
+		while (next) {
+			next = dm_get_next_target(dmt, next, &start, &length,
+						  &target_type, &params);
+			if (target_type)
+				printf(",%s", target_type);
+		}
+		printf("\n");
+	}
+
+	r = 1;
+      out:
+	if (dmt)
+		dm_task_destroy(dmt);
+	return r;
+}
+
 /* Show target names and their version numbers */
 static int _targets(int argc __attribute((unused)), char **argv __attribute((unused)), void *data __attribute((unused)))
 {
@@ -1699,6 +1787,7 @@ static struct command _commands[] = {
 	{"info", "[<device>]", 0, 1, _info},
 	{"deps", "[<device>]", 0, 1, _deps},
 	{"status", "[<device>] [--target <target_type>]", 0, 1, _status},
+	{"export", "[<device>]", 0, 1, _export},
 	{"table", "[<device>] [--target <target_type>] [--showkeys]", 0, 1, _status},
 	{"wait", "<device> [<event_nr>]", 0, 2, _wait},
 	{"mknodes", "[<device>]", 0, 1, _mknodes},
man/dmsetup.8 +8 -1
index 5b2ed9f..c4524aa 100644
@@ -39,13 +39,16 @@ dmsetup \- low level logical volume mana
 .B dmsetup ls
 .I [--target target_type] [--exec command] [--tree [-o options]]
 .br
-.B dmsetup info 
+.B dmsetup info
 .I [device_name]
 .br
 .B dmsetup info -c|-C|--columns
 .I [--noheadings] [--separator separator] [-o fields] [-O|--sort sort_fields]
 .I [device_name]
 .br
+.B dmsetup export
+.I [device_name]
+.br
 .B dmsetup deps
 .I [device_name]
 .br
@@ -138,6 +141,10 @@ device_name in subsequent dmsetup comman
 If successful a device will appear as
 /dev/device-mapper/<device-name>.  
 See below for information on the table format.
+.IP \fBexport
+.I [device_name]
+.br
+Outputs information in key/value format to be imported by other programs.
 .IP \fBdeps
 .I [device_name]
 .br
#2 lore raw
Quoted 3 lines · Hannes Reinecke

As already said, it totally makes sense when you hack up dmsetup and then forget about the patch. Sorry about this.
Alasdair, care to incorporate it?

Kay mentioned this patch to me at OLS.

My answer was that I want to see a generic implementation within the reporting code. In other words a new command line flag would instruct the reporting code to produce its output in this sort of format. The output needs to be useful when reporting on multiple objects at once, so to guarantee unique output lines, you might want to include fields acting as a unique key inside the variable names, which can be all-caps versions of the existing reporting field names.

Alasdair

Signature · 1 line
[email protected]
#3 lore raw

Hi Alasdair,

Quoted 7 lines · Alasdair G Kergon
Quoted 3 lines · Hannes Reinecke

As already said, it totally makes sense when you hack up dmsetup and then forget about the patch. Sorry about this.
Alasdair, care to incorporate it?

Kay mentioned this patch to me at OLS.

Quoted 7 lines

My answer was that I want to see a generic implementation within the reporting code. In other words a new command line flag would instruct the reporting code to produce its output in this sort of format. The output needs to be useful when reporting on multiple objects at once, so to guarantee unique output lines, you might want to include fields acting as a unique key inside the variable names, which can be all-caps versions of the existing reporting field names.

Does the attached version look better?
Cheers,
-- Guido

dmsetup/dmsetup.c +41 -10
index a86fed2..0226e77 100644
@@ -108,6 +108,7 @@ enum {
 	READ_ONLY = 0,
 	COLS_ARG,
 	EXEC_ARG,
+	EXPORT_ARG,
 	FORCE_ARG,
 	GID_ARG,
 	MAJOR_ARG,
@@ -322,6 +323,33 @@ static int _display_info_cols(struct dm_task *dmt, struct dm_info *info)
 
 static void _display_info_long(struct dm_task *dmt, struct dm_info *info)
 {
+	enum { name = 0, state, tablestate, opencount,
+	       eventnumber, devno, numtargets, uuidname };
+
+	const char* info_formatstrings[] = {
+		[name]        = "Name:              %s\n",
+		[state]       = "State:             %s%s\n",
+		[tablestate]  = "Tables present:    %s%s%s\n",
+		[opencount]   = "Open count:        %d\n",
+		[eventnumber] = "Event number:      %" PRIu32 "\n",
+		[devno]       = "Major, minor:      %d, %d\n",
+		[numtargets]  = "Number of targets: %d\n",
+		[uuidname]    = "UUID: %s\n",
+		};
+
+	const char* export_formatstrings[] = {	
+		[name]        = "DM_NAME=%s\n",
+		[state]       = "DM_STATE=\"%s%s\"\n",
+		[tablestate]  = "DM_TABLE_STATE=\"%s%s%s\"\n",
+		[opencount]   = "DM_OPENCOUNT=%d\n",
+		[eventnumber] = "DM_LAST_EVENT_NR=%" PRIu32 "\n",
+		[devno]       = "DM_MAJOR=%d\nDM_MINOR=%d\n",
+		[numtargets]  = "DM_TARGET_COUNT=%d\n",
+		[uuidname]    = "DM_UUID=%s\n",
+ 		};
+
+	const char** format = _switches[EXPORT_ARG] ? export_formatstrings 
+	                                            : info_formatstrings;
 	const char *uuid;
 
 	if (!info->exists) {
@@ -329,31 +357,31 @@ static void _display_info_long(struct dm_task *dmt, struct dm_info *info)
 		return;
 	}
 
-	printf("Name:              %s\n", dm_task_get_name(dmt));
+	printf(format[name], dm_task_get_name(dmt));
 
-	printf("State:             %s%s\n",
+	printf(format[state],
 	       info->suspended ? "SUSPENDED" : "ACTIVE",
 	       info->read_only ? " (READ-ONLY)" : "");
 
 	if (!info->live_table && !info->inactive_table)
-		printf("Tables present:    None\n");
+		printf(format[tablestate], "", "", "None\n");
 	else
-		printf("Tables present:    %s%s%s\n",
+		printf(format[tablestate],
 		       info->live_table ? "LIVE" : "",
 		       info->live_table && info->inactive_table ? " & " : "",
 		       info->inactive_table ? "INACTIVE" : "");
 
 	if (info->open_count != -1)
-		printf("Open count:        %d\n", info->open_count);
+		printf(format[opencount], info->open_count);
 
-	printf("Event number:      %" PRIu32 "\n", info->event_nr);
-	printf("Major, minor:      %d, %d\n", info->major, info->minor);
+	printf(format[eventnumber], info->event_nr);
+	printf(format[devno], info->major, info->minor);
 
 	if (info->target_count != -1)
-		printf("Number of targets: %d\n", info->target_count);
+		printf(format[numtargets], info->target_count);
 
 	if ((uuid = dm_task_get_uuid(dmt)) && *uuid)
-		printf("UUID: %s\n", uuid);
+		printf(format[uuidname], uuid);
 
 	printf("\n");
 }
@@ -2355,6 +2383,7 @@ static int _process_switches(int *argc, char ***argv)
 		{"readonly", 0, &ind, READ_ONLY},
 		{"columns", 0, &ind, COLS_ARG},
 		{"exec", 1, &ind, EXEC_ARG},
+		{"export", 0, &ind, EXPORT_ARG},
 		{"force", 0, &ind, FORCE_ARG},
 		{"gid", 1, &ind, GID_ARG},
 		{"major", 1, &ind, MAJOR_ARG},
@@ -2431,12 +2460,14 @@ static int _process_switches(int *argc, char ***argv)
 
 	optarg = 0;
 	optind = OPTIND_INIT;
-	while ((ind = -1, c = GETOPTLONG_FN(*argc, *argv, "cCfG:j:m:M:no:O:ru:U:v",
+	while ((ind = -1, c = GETOPTLONG_FN(*argc, *argv, "cCefG:j:m:M:no:O:ru:U:v",
 					    long_options, NULL)) != -1) {
 		if (c == ':' || c == '?')
 			return 0;
 		if (c == 'c' || c == 'C' || ind == COLS_ARG)
 			_switches[COLS_ARG]++;
+		if (c == 'e' || ind == EXPORT_ARG)
+			_switches[EXPORT_ARG]++;
 		if (c == 'f' || ind == FORCE_ARG)
 			_switches[FORCE_ARG]++;
 		if (c == 'r' || ind == READ_ONLY)
#4 lore raw

dmsetup/dmsetup.c static int _display_info_cols(struct dm_task *dmt, struct dm_info *info) · line 343

Quoted 2 lines · Guido Guenther
dmsetup/dmsetup.c +2 -0
+		[state]       = "DM_STATE=\"%s%s\"\n",
+		[tablestate]  = "DM_TABLE_STATE=\"%s%s%s\"\n",

Why use freetext fields if you can set it in a splitted way?

DM_STATE for example shows both suspended and read-only state without making in properly parsable.

dmsetup/dmsetup.c static int _display_info_cols(struct dm_task *dmt, struct dm_info *info) · line 352

Quoted 2 lines
dmsetup/dmsetup.c +2 -0
+	const char** format = _switches[EXPORT_ARG] ? export_formatstrings 
+	                                            : info_formatstrings;

const char *const *.

Export without a device name makes no sense.

Bastian

Signature · 2 lines
Landru! Guide us!
		-- A Beta 3-oid, "The Return of the Archons", stardate 3157.4
#6 lore raw
In reply to Guido Guenther
Quoted 1 line · Guido Guenther

Does the attached version look better?

I'd like it integrating into the *library*, not hard-coding into 'dmsetup'. i.e. an alternative output format for the library's reporting mechanism.

so 'lvs -o vg_name,lv_name --asvariables' (with some more-descriptive choice of cmdline arg) might produce:
LVM_VG_NAME_1="vg1"
LVM_LV_NAME_1="lv1"
LVM_VG_NAME_2="vg1"
LVM_LV_NAME_2="lv2"
var name generated by library from prefix + col name in caps + row no suffix (of course if you only ask for one object at a time, you'll always have suffix of _1 so it should work fine for your scripts)

Alasdair

Signature · 1 line
[email protected]
#5 lore raw
In reply to Bastian Blank
Quoted 8 lines · Bastian Blank

dmsetup/dmsetup.c static int _display_info_cols(struct dm_task *dmt, struct dm_info *info) · line 343

Quoted 2 lines · Guido Guenther
dmsetup/dmsetup.c +2 -0
+		[state]       = "DM_STATE=\"%s%s\"\n",
+		[tablestate]  = "DM_TABLE_STATE=\"%s%s%s\"\n",

Why use freetext fields if you can set it in a splitted way?

DM_STATE for example shows both suspended and read-only state without making in properly parsable.

This is just does what the patch I based this on does, I'd rather split this into separate fields too.
Cheers,
-- Guido