]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libfdisk: add --nopartno to mkpart sample
authorKarel Zak <kzak@redhat.com>
Tue, 1 Aug 2017 11:51:27 +0000 (13:51 +0200)
committerKarel Zak <kzak@redhat.com>
Tue, 1 Aug 2017 11:51:27 +0000 (13:51 +0200)
* disable dialogs for the mkpart sample
* add --nopartno use-case to force libfdisk to use default partno(s)
* add test for this feature

Signed-off-by: Karel Zak <kzak@redhat.com>
libfdisk/samples/mkpart.c
tests/expected/libfdisk/mkpart-mbr-nopartno [new file with mode: 0644]
tests/ts/libfdisk/mkpart

index 7871015cf845e0c9045d3f15201717a539c2e91d..3c964de996052ab9a18ceb03131613b9a6bf82de 100644 (file)
@@ -68,12 +68,13 @@ int main(int argc, char *argv[])
        struct fdisk_context *cxt;
        struct fdisk_partition *pa;
        const char *label = NULL, *device = NULL;
-       int n = 0, c;
+       int n = 0, c, nopartno = 0;
        unsigned int sectorsize;
 
        static const struct option longopts[] = {
                { "label",  required_argument, NULL, 'x' },
                { "device", required_argument, NULL, 'd' },
+               { "nopartno", no_argument, NULL, 'p' },
                { "help",   no_argument, NULL, 'h' },
                { NULL, 0, NULL, 0 },
        };
@@ -90,6 +91,9 @@ int main(int argc, char *argv[])
                case 'd':
                        device = optarg;
                        break;
+               case 'p':
+                       nopartno = 1;
+                       break;
                case 'h':
                        printf("%s [options] <size> ...", program_invocation_short_name);
                        fputs(USAGE_SEPARATOR, stdout);
@@ -97,6 +101,7 @@ int main(int argc, char *argv[])
                        fputs(USAGE_OPTIONS, stdout);
                        fputs(" -x, --label <dos,gpt,...>    disk label type\n", stdout);
                        fputs(" -d, --device <path>          block device\n", stdout);
+                       fputs(" -p, --nopartno               don't set partno (use default)\n", stdout);
                        fputs(" -h, --help                   this help\n", stdout);
                        fputs(USAGE_SEPARATOR, stdout);
                        return EXIT_SUCCESS;
@@ -124,6 +129,8 @@ int main(int argc, char *argv[])
 
        sectorsize = fdisk_get_sector_size(cxt);
 
+       fdisk_disable_dialogs(cxt, 1);
+
        while (optind < argc) {
                int rc;
                uint64_t size;
@@ -132,6 +139,7 @@ int main(int argc, char *argv[])
                /* defaults */
                fdisk_partition_start_follow_default(pa, 1);
                fdisk_partition_end_follow_default(pa, 1);
+               fdisk_partition_partno_follow_default(pa, 1);
 
                /* set size */
                if (isdigit(*str)) {
@@ -148,8 +156,10 @@ int main(int argc, char *argv[])
                         * This is possible by explicitly specified partition
                         * number, <4 means primary, >=4 means logical.
                         */
-                       fdisk_partition_partno_follow_default(pa, 0);
-                       fdisk_partition_set_partno(pa, n);
+                       if (!nopartno) {
+                               fdisk_partition_partno_follow_default(pa, 0);
+                               fdisk_partition_set_partno(pa, n);
+                       }
 
                        /* Make sure last primary partition is extended if user
                         * wants more than 4 partitions.
@@ -163,9 +173,7 @@ int main(int argc, char *argv[])
                                fdisk_partition_set_type(pa, type);
                                fdisk_unref_parttype(type);
                        }
-               } else
-                       fdisk_partition_partno_follow_default(pa, 1);
-
+               }
 
                rc = fdisk_add_partition(cxt, pa, NULL);
                if (rc) {
diff --git a/tests/expected/libfdisk/mkpart-mbr-nopartno b/tests/expected/libfdisk/mkpart-mbr-nopartno
new file mode 100644 (file)
index 0000000..178eaf2
--- /dev/null
@@ -0,0 +1,26 @@
+Created a new <removed>.
+Created a new <removed>.
+Created a new <removed>.
+Created a new <removed>.
+Created a new <removed>.
+Adding logical partition 5
+Created a new <removed>.
+Adding logical partition 6
+Created a new <removed>.
+Adding logical partition 7
+Created a new <removed>.
+Disk <removed>: 15 MiB, 15728640 bytes, 30720 sectors
+Units: sectors of 1 * 512 = 512 bytes
+Sector size (logical/physical): 512 bytes / 512 bytes
+I/O size (minimum/optimal): 512 bytes / <removed> bytes
+Disklabel type: dos
+Disk identifier: <removed>
+
+Device     Boot Start   End Sectors Size Id Type
+<removed>1       2048  4095    2048   1M 83 Linux
+<removed>2       4096  6143    2048   1M 83 Linux
+<removed>3       6144  8191    2048   1M 83 Linux
+<removed>4       8192 30719   22528  11M  5 Extended
+<removed>5      10240 12287    2048   1M 83 Linux
+<removed>6      14336 16383    2048   1M 83 Linux
+<removed>7      18432 30719   12288   6M 83 Linux
index 850b1c5aaed5cff2774dbccdff5eab0430572d30..7365881c2feab81b23094a9e86cace6718065e7d 100755 (executable)
@@ -46,6 +46,15 @@ ts_finalize_subtest
 
 $TS_CMD_WIPEFS --all --force ${TEST_IMAGE_NAME} &> /dev/null
 
+ts_init_subtest "mbr-nopartno"
+$TESTPROG --nopartno --label mbr --device ${TEST_IMAGE_NAME} \
+       1M 1M 1M - 1M 1M ->> $TS_OUTPUT 2>&1
+$TS_CMD_SFDISK --list ${TEST_IMAGE_NAME} >> $TS_OUTPUT 2>&1
+ts_fdisk_clean ${TEST_IMAGE_NAME}
+ts_finalize_subtest
+
+$TS_CMD_WIPEFS --all --force ${TEST_IMAGE_NAME} &> /dev/null
+
 ts_init_subtest "gpt"
 $TESTPROG --label gpt --device ${TEST_IMAGE_NAME} \
        1M 1M 1M 1M 1M 1M - >> $TS_OUTPUT 2>&1