Skip to contentSkip to conversation

partx: support loop devices

PATCH In review Davidlohr Bueso 2 participants 5 messages Aug 15, 2011 – Aug 31, 2011 Util-Linux package

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 partx: support loop devices

From: Davidlohr Bueso <[email protected]>

Add support for loop devices to add partitions. For now we make use of the max_part parameter from the loop kernel module, otherwise the feature is disabled.

Below an example output:

root@offbook:~/projects/util-linux/partx# ./partx images-pt/dos+bsd.img 
# 1:        32-     7679 (     7648 sectors,      3 MB)
# 2:      7680-    16383 (     8704 sectors,      4 MB)
# 5:      7936-    12799 (     4864 sectors,      2 MB)
# 6:     12544-    16127 (     3584 sectors,      1 MB)
root@offbook:~/projects/util-linux/partx# ./partx -a -n 1:5 images-pt/dos+bsd.img
root@offbook:~/projects/util-linux/partx# ls /dev/loop0* -ltr
brw-rw---- 1 root disk 7, 0 2011-08-15 00:07 /dev/loop0
brw-rw---- 1 root disk 7, 5 2011-08-15 00:07 /dev/loop0p5
brw-rw---- 1 root disk 7, 2 2011-08-15 00:07 /dev/loop0p2
brw-rw---- 1 root disk 7, 1 2011-08-15 00:07 /dev/loop0p1
---
 partx/Makefile.am |    2 +
 partx/partx.c     |   77 ++++++++++++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 75 insertions(+), 4 deletions(-)
partx/Makefile.am +2 -0
index 6a72942..080bc47 100644
@@ -11,6 +11,8 @@ partx_SOURCES = partx.c partx.h \
 		$(top_srcdir)/lib/at.c \
 		$(top_srcdir)/lib/mbsalign.c \
 		$(top_srcdir)/lib/strutils.c \
+		$(top_srcdir)/lib/canonicalize.c \
+		$(top_srcdir)/lib/loopdev.c \
 		$(top_srcdir)/lib/linux_version.c
 
 partx_CFLAGS = -I$(ul_libblkid_incdir)
partx/partx.c +73 -4

125 lines not on this page · raw message

Signature · 1 line
1.7.4.1
maintainer
#1 lore raw

partx/partx.c int main(int argc, char **argv) · line 860

Quoted 7 lines · Davidlohr Bueso
partx/partx.c +1 -2
@@ -800,14 +857,23 @@ int main(int argc, char **argv)
 	if (what == ACT_ADD || what == ACT_DELETE) {
 		struct stat x;
 
-		if (stat(wholedisk, &x) || !S_ISBLK(x.st_mode))
-			errx(EXIT_FAILURE, _("%s: not a block device"), wholedisk);
+		if (stat(wholedisk, &x) || !S_ISBLK(x.st_mode)) {

&& S_ISREG() :-)

partx/partx.c int main(int argc, char **argv) · line 861

Quoted 1 line
partx/partx.c +1 -0
+			/* not a blkdev, try to associate it to a loop device */
            if (what == ACT_DELETE)
                errx(EXIT_FAILURE, _("%s: cannot delete partitions"), wholedisk);
Quoted 6 lines
partx/partx.c +6 -0
+			if (!loopmod_supports_parts())
+				errx(EXIT_FAILURE, _("%s: does not support loop device partitions"), 
+				     wholedisk);
+			assoc_loopdev(wholedisk);
+			wholedisk = xstrdup(lc.device);
+		}

partx/partx.c int main(int argc, char **argv) · line 876

Quoted 10 lines
partx/partx.c +4 -1
 	}
 	if ((fd = open(wholedisk, O_RDONLY)) == -1)
 		err(EXIT_FAILURE, _("%s: open failed"), wholedisk);
 
-	if (what == ACT_DELETE)
+	if (what == ACT_DELETE) {
+		if (loopdev)
+			errx(EXIT_FAILURE, _("%s: cannot delete partitions"), wholedisk);
 		rc = del_parts(fd, wholedisk, disk_devno, lower, upper);
+	}

Does it make sense to initialize loop device if ACT_DELETE is requested?

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
#2 lore raw
Quoted 26 lines · Karel Zak

Review int main(int argc, char **argv) · line 860

Quoted 7 lines · Davidlohr Bueso
(quoted diff) +1 -2
@@ -800,14 +857,23 @@ int main(int argc, char **argv)
 	if (what == ACT_ADD || what == ACT_DELETE) {
 		struct stat x;
 
-		if (stat(wholedisk, &x) || !S_ISBLK(x.st_mode))
-			errx(EXIT_FAILURE, _("%s: not a block device"), wholedisk);
+		if (stat(wholedisk, &x) || !S_ISBLK(x.st_mode)) {

&& S_ISREG() :-)

Review int main(int argc, char **argv) · line 861

Quoted 1 line
(quoted diff) +1 -0
+			/* not a blkdev, try to associate it to a loop device */
            if (what == ACT_DELETE)
                errx(EXIT_FAILURE, _("%s: cannot delete partitions"), wholedisk);
Quoted 10 lines
(quoted diff) +6 -0
+			if (!loopmod_supports_parts())
+				errx(EXIT_FAILURE, _("%s: does not support loop device partitions"), 
+				     wholedisk);
+			assoc_loopdev(wholedisk);
+			wholedisk = xstrdup(lc.device);
+		}
 	}
 	if ((fd = open(wholedisk, O_RDONLY)) == -1)
 		err(EXIT_FAILURE, _("%s: open failed"), wholedisk);
 
Quoted 8 lines

Review int main(int argc, char **argv) · line 876

Quoted 6 lines
(quoted diff) +4 -1
-	if (what == ACT_DELETE)
+	if (what == ACT_DELETE) {
+		if (loopdev)
+			errx(EXIT_FAILURE, _("%s: cannot delete partitions"), wholedisk);
 		rc = del_parts(fd, wholedisk, disk_devno, lower, upper);
+	}

Does it make sense to initialize loop device if ACT_DELETE is requested?

Ah, yes, good catch. Below is the corrected patch, do you think this feature could go into 2.20?

From: Davidlohr Bueso <[email protected]>
Date: Tue, 16 Aug 2011 09:17:10 -0400
Subject: [PATCH] partx: support loop devices

Add support for loop devices to add partitions. For now we make use of the max_part parameter from the loop kernel module, otherwise the feature is disabled.

Below an example output:

root@offbook:~/projects/util-linux/partx# ./partx -a -n 1:5 images-pt/dos+bsd.img
root@offbook:~/projects/util-linux/partx# ls /dev/loop0* -ltr
brw-rw---- 1 root disk 7, 0 2011-08-15 00:07 /dev/loop0
brw-rw---- 1 root disk 7, 5 2011-08-15 00:07 /dev/loop0p5
brw-rw---- 1 root disk 7, 2 2011-08-15 00:07 /dev/loop0p2
brw-rw---- 1 root disk 7, 1 2011-08-15 00:07 /dev/loop0p1
---
 partx/Makefile.am |    2 +
 partx/partx.c     |   75 ++++++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 74 insertions(+), 3 deletions(-)
partx/Makefile.am +2 -0
index 6a72942..080bc47 100644
@@ -11,6 +11,8 @@ partx_SOURCES = partx.c partx.h \
 		$(top_srcdir)/lib/at.c \
 		$(top_srcdir)/lib/mbsalign.c \
 		$(top_srcdir)/lib/strutils.c \
+		$(top_srcdir)/lib/canonicalize.c \
+		$(top_srcdir)/lib/loopdev.c \
 		$(top_srcdir)/lib/linux_version.c
 
 partx_CFLAGS = -I$(ul_libblkid_incdir)
partx/partx.c +72 -3

118 lines not on this page · raw message

Signature · 1 line
1.7.4.1
maintainer
#3 lore raw
Quoted 36 lines · Davidlohr Bueso
Quoted 26 lines · Karel Zak

partx/partx.c int main(int argc, char **argv) · line 860

Quoted 7 lines · Davidlohr Bueso
partx/partx.c +1 -2
@@ -800,14 +857,23 @@ int main(int argc, char **argv)
 	if (what == ACT_ADD || what == ACT_DELETE) {
 		struct stat x;
 
-		if (stat(wholedisk, &x) || !S_ISBLK(x.st_mode))
-			errx(EXIT_FAILURE, _("%s: not a block device"), wholedisk);
+		if (stat(wholedisk, &x) || !S_ISBLK(x.st_mode)) {

&& S_ISREG() :-)

partx/partx.c int main(int argc, char **argv) · line 861

Quoted 1 line
partx/partx.c +1 -0
+			/* not a blkdev, try to associate it to a loop device */
            if (what == ACT_DELETE)
                errx(EXIT_FAILURE, _("%s: cannot delete partitions"), wholedisk);
Quoted 10 lines
partx/partx.c +6 -0
+			if (!loopmod_supports_parts())
+				errx(EXIT_FAILURE, _("%s: does not support loop device partitions"), 
+				     wholedisk);
+			assoc_loopdev(wholedisk);
+			wholedisk = xstrdup(lc.device);
+		}
 	}
 	if ((fd = open(wholedisk, O_RDONLY)) == -1)
 		err(EXIT_FAILURE, _("%s: open failed"), wholedisk);
 
Quoted 8 lines

Review int main(int argc, char **argv) · line 876

Quoted 6 lines
(quoted diff) +4 -1
-	if (what == ACT_DELETE)
+	if (what == ACT_DELETE) {
+		if (loopdev)
+			errx(EXIT_FAILURE, _("%s: cannot delete partitions"), wholedisk);
 		rc = del_parts(fd, wholedisk, disk_devno, lower, upper);
+	}

Does it make sense to initialize loop device if ACT_DELETE is requested?

Quoted 2 lines

Ah, yes, good catch. Below is the corrected patch, do you think this feature could go into 2.20?

2.21, for 2.20 is too late

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
maintainer
#4 lore raw
In reply to Davidlohr Bueso

Review

Quoted 10 lines · Davidlohr Bueso
(quoted diff) +10 -0
+static int loopmod_supports_parts(void)
+{
+	int rc, ret = 0;
+	FILE *f = fopen("/sys/module/loop/parameters/max_part", "r");
+	
+	if (!f)
+		return 0;
+	rc = fscanf(f, "%d", &ret);
+	fclose(f);
+	return ret;

return rc = 1 ? ret : 0;

Right?

Quoted 4 lines
(quoted diff) +4 -0
+}
+
+static void assoc_loopdev(const char *fname)
+{

[...]

Review

Quoted 7 lines
(quoted diff) +4 -0
+		err(EXIT_FAILURE, "failed to find unused device");
 
+		printf("Trying to use '%s' for the loop device\n", loopcxt_get_device(&lc));
 
+		err(EXIT_FAILURE, "failed to set backing file");
 
+		err(EXIT_FAILURE, "failed to setup device for %s", fname);

you forgot _( )

Review

Quoted 1 line
(quoted diff) +1 -0
+		if (stat(wholedisk, &x) || !S_ISBLK(x.st_mode) && S_ISREG(x.st_mode)) {
  || && :-)

Review

Quoted 10 lines
(quoted diff) +10 -0
+			/* not a blkdev, try to associate it to a loop device */
+			if (what == ACT_DELETE)
+				errx(EXIT_FAILURE, _("%s: cannot delete partitions"), wholedisk);
+
+			if (!loopmod_supports_parts())
+				errx(EXIT_FAILURE, _("%s: does not support loop device partitions"), 
+				     wholedisk);
+			assoc_loopdev(wholedisk);
+			wholedisk = xstrdup(lc.device);
+		}

Fixed (see below), applied to may private repository. Thanks.

Note that Kay's loopdev changes are in Linus' tree, so we can start to play with that (kernel will not add all partition).

Karel

Quoted 1 line

From 0e9381133e2d4a79715d5b9e2326198f8ed72d35 Mon Sep 17 00:00:00 2001

From: Davidlohr Bueso <[email protected]>
Date: Tue, 16 Aug 2011 09:20:07 -0400
Subject: [PATCH] partx: support loop devices

Add support for loop devices to add partitions. For now we make use of the max_part parameter from the loop kernel module, otherwise the feature is disabled.

Below an example output:

root@offbook:~/projects/util-linux/partx# ./partx -a -n 1:5 images-pt/dos+bsd.img
root@offbook:~/projects/util-linux/partx# ls /dev/loop0* -ltr
brw-rw---- 1 root disk 7, 0 2011-08-15 00:07 /dev/loop0
brw-rw---- 1 root disk 7, 5 2011-08-15 00:07 /dev/loop0p5
brw-rw---- 1 root disk 7, 2 2011-08-15 00:07 /dev/loop0p2
brw-rw---- 1 root disk 7, 1 2011-08-15 00:07 /dev/loop0p1
---
 partx/Makefile.am |    2 +
 partx/partx.c     |   79 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 79 insertions(+), 2 deletions(-)
partx/Makefile.am +2 -0
index 6a72942..080bc47 100644
@@ -11,6 +11,8 @@ partx_SOURCES = partx.c partx.h \
 		$(top_srcdir)/lib/at.c \
 		$(top_srcdir)/lib/mbsalign.c \
 		$(top_srcdir)/lib/strutils.c \
+		$(top_srcdir)/lib/canonicalize.c \
+		$(top_srcdir)/lib/loopdev.c \
 		$(top_srcdir)/lib/linux_version.c
 
 partx_CFLAGS = -I$(ul_libblkid_incdir)
partx/partx.c +77 -2

122 lines not on this page · raw message

Signature · 1 line
1.7.6