Skip to contentSkip to conversation

kpartx: Add -N option to allow specifying a devmapping name

PATCH In review Glenn Washburn 2 participants 2 messages Apr 5, 2024 Device mapper

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kpartx: Add -N option to allow specifying a devmapping name

Running kpartx -a disk.img will create device names like /dev/mapper/loopNpM where N and M are positive integers. The issue is that loopN is not known before hand. losetup -f can be used to figure out what the next loop device will be, but the loop device might be taken by something else between then and when kpartx finds the next loop device. Add a new option -N which takes a string argument that will be used as the base name of the devmapper device.

---
 kpartx/kpartx.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
kpartx/kpartx.c +6 -2
index 46cb76bac678..750b889327e9 100644
@@ -88,7 +88,7 @@ initpts(void)
 	addpts("ps3", read_ps3_pt);
 }
 
-static char short_opts[] = "rladfgvp:t:snu";
+static char short_opts[] = "rladfgvp:t:snuN:";
 
 /* Used in gpt.c */
 int force_gpt=0;
@@ -99,7 +99,7 @@ static int
 usage(void) {
 	printf(VERSION_STRING);
 	printf("Usage:\n");
-	printf("  kpartx [-a|-d|-u|-l] [-r] [-p] [-f] [-g] [-s|-n] [-v] wholedisk\n");
+	printf("  kpartx [-a|-d|-u|-l] [-r] [-p] [-f] [-g] [-s|-n] [-v] [-N name] wholedisk\n");
 	printf("\t-a add partition devmappings\n");
 	printf("\t-r devmappings will be readonly\n");
 	printf("\t-d del partition devmappings\n");
@@ -111,6 +111,7 @@ usage(void) {
 	printf("\t-v verbose\n");
 	printf("\t-n nosync mode. Return before the partitions are created\n");
 	printf("\t-s sync mode (Default). Don't return until the partitions are created\n");
+	printf("\t-N use name as base name for device\n");
 	return 1;
 }
 
@@ -312,6 +313,9 @@ main(int argc, char **argv){
 		case 'u':
 			what = UPDATE;
 			break;
+		case 'N':
+			mapname = optarg;
+			break;
 		default:
 			usage();
 			exit(1);
Signature · 1 line
2.34.1
#1 lore raw
Quoted 13 lines · Glenn Washburn

Running kpartx -a disk.img will create device names like /dev/mapper/loopNpM where N and M are positive integers. The issue is
that loopN is not known before hand. losetup -f can be used to figure
out what the next loop device will be, but the loop device might be taken
by something else between then and when kpartx finds the next loop device.
Add a new option -N which takes a string argument that will be used as
the base name of the devmapper device.

Quoted 1 line

Thanks for the patch, this looks useful. But you should warn / error out if "-N" was used with a real block device rather than an image (or, alternatively, implement using the map basename for block devices, too). If the image is already loop-mounted, the loop device _is_ known beforehand; it needs to be clarified and documented how this option behaves in that case (your commit description refers only to the case in which the loop device is created by kpartx).

Also, please update the kpartx man page.

Regards,
Martin

Quoted 48 lines

---
 kpartx/kpartx.c | 8 ++++++--

 1 file changed, 6 insertions(+), 2 deletions(-)
kpartx/kpartx.c +0 -0
index 46cb76bac678..750b889327e9 100644
@@ -88,7 +88,7 @@ initpts(void)
  	addpts("ps3", read_ps3_pt);
  }

kpartx/kpartx.c +1 -1
-static char short_opts[] = "rladfgvp:t:snu";
+static char short_opts[] = "rladfgvp:t:snuN:";


 /* Used in gpt.c */
 int force_gpt=0;

kpartx/kpartx.c +1 -1
@@ -99,7 +99,7 @@ static int
  usage(void) {
  	printf(VERSION_STRING);
  	printf("Usage:\n");
-	printf("  kpartx [-a|-d|-u|-l] [-r] [-p] [-f] [-g] [-s|-n]
 [-v] wholedisk\n");
+	printf("  kpartx [-a|-d|-u|-l] [-r] [-p] [-f] [-g] [-s|-n]
 [-v] [-N name] wholedisk\n");
  	printf("\t-a add partition devmappings\n");

  printf("\t-r devmappings will be readonly\n"); printf("\t-d del partition devmappings\n");

kpartx/kpartx.c +0 -0
@@ -111,6 +111,7 @@ usage(void) {
  	printf("\t-v verbose\n");

  printf("\t-n nosync mode. Return before the partitions are created\n");
  printf("\t-s sync mode (Default). Don't return until the partitions are created\n");

kpartx/kpartx.c +1 -0
+	printf("\t-N use name as base name for device\n");

  return 1;
 }

kpartx/kpartx.c +0 -0
@@ -312,6 +313,9 @@ main(int argc, char **argv){

  case 'u':
  what = UPDATE;
  break;

kpartx/kpartx.c +3 -0
+		case 'N':
+			mapname = optarg;
+			break;

  default:
  usage();
  exit(1);