Util-Linux package development
 help / color / mirror / Atom feed
* [PATCH] Use predictable /dev/mapper partition names for /dev/dm-N
@ 2015-05-26 15:59 Stanislav Brabec
  2015-05-28 10:32 ` Karel Zak
  0 siblings, 1 reply; 2+ messages in thread
From: Stanislav Brabec @ 2015-05-26 15:59 UTC (permalink / raw)
  To: util-linux

It is impossible to predict /dev/dm-N partition names. Use predictable
and better readable /dev/mapper names instead.

Signed-off-by: Stanislav Brabec <sbrabec@suse.cz>
---
 libfdisk/src/utils.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/libfdisk/src/utils.c b/libfdisk/src/utils.c
index 4789dbd..569ee50 100644
--- a/libfdisk/src/utils.c
+++ b/libfdisk/src/utils.c
@@ -1,6 +1,7 @@
 
 #include "fdiskP.h"
 #include "pathnames.h"
+#include "canonicalize.h"
 
 #include <ctype.h>
 
@@ -115,6 +116,7 @@ char *fdisk_partname(const char *dev, size_t partno)
 {
 	char *res = NULL;
 	const char *p = "";
+	char *dev_mapped = NULL;
 	int w = 0;
 
 	if (!dev || !*dev) {
@@ -123,6 +125,13 @@ char *fdisk_partname(const char *dev, size_t partno)
 		return NULL;
 	}
 
+	/* It is impossible to predict /dev/dm-N partition names. */
+	if (strncmp(dev, "/dev/dm-", sizeof("/dev/dm-") - 1) == 0) {
+		dev_mapped = canonicalize_dm_name (dev + 5);
+		if (dev_mapped)
+			dev = dev_mapped;
+	}
+
 	w = strlen(dev);
 	if (isdigit(dev[w - 1]))
 #ifdef __GNU__
@@ -147,10 +156,13 @@ char *fdisk_partname(const char *dev, size_t partno)
 	       p = "-part";
 	}
 
-	if (asprintf(&res, "%.*s%s%zu", w, dev, p, partno) > 0)
-		return res;
+	if (asprintf(&res, "%.*s%s%zu", w, dev, p, partno) <= 0)
+		res = NULL;
+
+	if (dev_mapped)
+		free(dev_mapped);
 
-	return NULL;
+	return res;
 }
 
 #ifdef TEST_PROGRAM
-- 
2.4.1
-- 
Best Regards / S pozdravem,

Stanislav Brabec
software developer
---------------------------------------------------------------------
SUSE LINUX, s. r. o.                          e-mail: sbrabec@suse.cz
Lihovarská 1060/12                            tel: +49 911 7405384547
190 00 Praha 9                                 fax:  +420 284 084 001
Czech Republic                                    http://www.suse.cz/
PGP: 830B 40D5 9E05 35D8 5E27 6FA3 717C 209F A04F CD76

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] Use predictable /dev/mapper partition names for /dev/dm-N
  2015-05-26 15:59 [PATCH] Use predictable /dev/mapper partition names for /dev/dm-N Stanislav Brabec
@ 2015-05-28 10:32 ` Karel Zak
  0 siblings, 0 replies; 2+ messages in thread
From: Karel Zak @ 2015-05-28 10:32 UTC (permalink / raw)
  To: Stanislav Brabec; +Cc: util-linux

On Tue, May 26, 2015 at 05:59:29PM +0200, Stanislav Brabec wrote:
>  libfdisk/src/utils.c | 18 +++++++++++++++---
>  1 file changed, 15 insertions(+), 3 deletions(-)

 Applied, thanks.

> +	if (dev_mapped)
> +		free(dev_mapped);

 Don't use if-before-free, it's unnecessary :-)

    Karel
-- 
 Karel Zak  <kzak@redhat.com>
 http://karelzak.blogspot.com

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2015-05-28 10:32 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-05-26 15:59 [PATCH] Use predictable /dev/mapper partition names for /dev/dm-N Stanislav Brabec
2015-05-28 10:32 ` Karel Zak

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox