]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libfdisk: support default partno in mkpart-fullspec sample
authorKarel Zak <kzak@redhat.com>
Wed, 19 Jul 2017 09:42:50 +0000 (11:42 +0200)
committerKarel Zak <kzak@redhat.com>
Wed, 19 Jul 2017 10:19:38 +0000 (12:19 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
libfdisk/samples/mkpart-fullspec.c
tests/expected/libfdisk/mkpart-full-gpt-nopartno [new file with mode: 0644]
tests/expected/libfdisk/mkpart-full-mbr-nopartno [new file with mode: 0644]
tests/expected/libfdisk/mkpart-full-mbr-primary-nopartno [new file with mode: 0644]
tests/ts/libfdisk/mkpart-full

index e919c545fbffc08a9fa5047bad24bace7c1ef03a..01f1ebabe11d6d8dc96f12034fa09776063f1840 100644 (file)
@@ -57,6 +57,7 @@ int main(int argc, char *argv[])
        struct fdisk_partition *pa;
        const char *label = NULL, *device = NULL;
        int c;
+       size_t n = 1;
 
        static const struct option longopts[] = {
                { "label",  required_argument, NULL, 'x' },
@@ -78,10 +79,10 @@ int main(int argc, char *argv[])
                        device = optarg;
                        break;
                case 'h':
-                       printf("%s [options] <partno,start,size> ...", program_invocation_short_name);
+                       printf("%s [options] -- <partno,start,size> ...", program_invocation_short_name);
                        fputs(USAGE_SEPARATOR, stdout);
                        puts("Make disklabel and partitions.");
-                       puts(" <partno>                     number from 1..n (4th is extended for MBR)");
+                       puts(" <partno>                     1..n (4th is extended for MBR), or '-' for default");
                        puts(" <start>                      partition start offset in sectors");
                        puts(" <size>                       partition size in sectors");
                        fputs(USAGE_OPTIONS, stdout);
@@ -112,34 +113,51 @@ int main(int argc, char *argv[])
        if (fdisk_create_disklabel(cxt, label))
                err(EXIT_FAILURE, "failed to create disk label");
 
+       fdisk_disable_dialogs(cxt, 1);
+
        while (optind < argc) {
                int rc;
                unsigned int partno = 0;
                uint64_t start = 0, size = 0;
                const char *str = argv[optind];
 
-               if (sscanf(str, "%u,%"SCNu64",%"SCNu64"", &partno, &start, &size) != 3)
-                       errx(EXIT_FAILURE, "faild to parse %s", str);
-
-               /* disable defaults */
-               fdisk_partition_partno_follow_default(pa, 0);
+               fdisk_reset_partition(pa);
                fdisk_partition_end_follow_default(pa, 0);
 
-               /* set all */
-               fdisk_partition_set_partno(pa, partno - 1);     /* library uses 0..n */
+               if (*str == '-') {
+                       /* partno unspecified */
+                       if (sscanf(str, "-,%"SCNu64",%"SCNu64"", &start, &size) != 2)
+                               errx(EXIT_FAILURE, "faild to parse %s", str);
+                       fdisk_partition_partno_follow_default(pa, 1);
+                       fdisk_partition_unset_partno(pa);
+               } else {
+                       /* partno specified */
+                       if (sscanf(str, "%u,%"SCNu64",%"SCNu64"", &partno, &start, &size) != 3)
+                               errx(EXIT_FAILURE, "faild to parse %s", str);
+
+                       fdisk_partition_partno_follow_default(pa, 0);
+                       fdisk_partition_set_partno(pa, partno - 1);     /* library uses 0..n */
+               }
+
                fdisk_partition_set_start(pa, start);
                fdisk_partition_set_size(pa, size);
 
-               fprintf(stderr, "Requested partition: <partno=%zu,start=%ju,size=%ju>\n",
-                               fdisk_partition_get_partno(pa),
-                               (uintmax_t) fdisk_partition_get_start(pa),
-                               (uintmax_t) fdisk_partition_get_size(pa));
+               if (fdisk_partition_has_partno(pa))
+                       fprintf(stderr, "Requested partition: <partno=%zu,start=%ju,size=%ju>\n",
+                                       fdisk_partition_get_partno(pa),
+                                       (uintmax_t) fdisk_partition_get_start(pa),
+                                       (uintmax_t) fdisk_partition_get_size(pa));
+               else
+                       fprintf(stderr, "Requested partition: <partno=<default>,start=%ju,size=%ju>\n",
+                                       (uintmax_t) fdisk_partition_get_start(pa),
+                                       (uintmax_t) fdisk_partition_get_size(pa));
 
                if (fdisk_is_label(cxt, DOS)) {
                        /* Make sure last primary partition is extended if user
                         * wants more than 4 partitions.
                         */
-                       if (partno == 4 && optind + 1 < argc) {
+                       if ((partno == 4 || (n == 4 && !fdisk_partition_has_partno(pa)))
+                           && optind + 1 < argc) {
                                struct fdisk_parttype *type =
                                        fdisk_label_parse_parttype(
                                                        fdisk_get_label(cxt, NULL), "05");
@@ -153,11 +171,14 @@ int main(int argc, char *argv[])
                rc = fdisk_add_partition(cxt, pa, NULL);
                if (rc) {
                        errno = -rc;
-                       errx(EXIT_FAILURE, "failed to add #%d partition", partno);
+                       errx(EXIT_FAILURE, "failed to add #%zu partition",
+                                       fdisk_partition_has_partno(pa) ?
+                                       fdisk_partition_get_partno(pa) + 1: n);
                }
 
                fdisk_reset_partition(pa);
                optind++;
+               n++;
        }
 
        if (fdisk_write_disklabel(cxt))
diff --git a/tests/expected/libfdisk/mkpart-full-gpt-nopartno b/tests/expected/libfdisk/mkpart-full-gpt-nopartno
new file mode 100644 (file)
index 0000000..2c1423b
--- /dev/null
@@ -0,0 +1,30 @@
+Requested partition: <partno=<default>,start=2048,size=2048>
+Requested partition: <partno=<default>,start=4096,size=2048>
+Requested partition: <partno=<default>,start=6144,size=2048>
+Requested partition: <partno=<default>,start=8192,size=2048>
+Requested partition: <partno=<default>,start=10240,size=2048>
+Requested partition: <partno=<default>,start=12288,size=2048>
+Requested partition: <partno=<default>,start=14336,size=2048>
+Created a new <removed>.
+Created a new <removed>.
+Created a new <removed>.
+Created a new <removed>.
+Created a new <removed>.
+Created a new <removed>.
+Created a new <removed>.
+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: gpt
+Disk identifier: <removed>
+
+Device             Start   End Sectors Size Type
+<removed>1  2048  4095    2048   1M Linux filesystem
+<removed>2  4096  6143    2048   1M Linux filesystem
+<removed>3  6144  8191    2048   1M Linux filesystem
+<removed>4  8192 10239    2048   1M Linux filesystem
+<removed>5 10240 12287    2048   1M Linux filesystem
+<removed>6 12288 14335    2048   1M Linux filesystem
+<removed>7 14336 16383    2048   1M Linux filesystem
diff --git a/tests/expected/libfdisk/mkpart-full-mbr-nopartno b/tests/expected/libfdisk/mkpart-full-mbr-nopartno
new file mode 100644 (file)
index 0000000..23f7ad5
--- /dev/null
@@ -0,0 +1,33 @@
+Requested partition: <partno=<default>,start=2048,size=2048>
+Requested partition: <partno=<default>,start=4096,size=2048>
+Requested partition: <partno=<default>,start=6144,size=2048>
+Requested partition: <partno=<default>,start=8192,size=22528>
+Requested partition: <partno=<default>,start=10240,size=2048>
+Requested partition: <partno=<default>,start=14336,size=2048>
+Requested partition: <partno=<default>,start=18432,size=12288>
+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
diff --git a/tests/expected/libfdisk/mkpart-full-mbr-primary-nopartno b/tests/expected/libfdisk/mkpart-full-mbr-primary-nopartno
new file mode 100644 (file)
index 0000000..90a71f3
--- /dev/null
@@ -0,0 +1,21 @@
+Requested partition: <partno=<default>,start=2048,size=2048>
+Requested partition: <partno=<default>,start=4096,size=2048>
+Requested partition: <partno=<default>,start=6144,size=2048>
+Requested partition: <partno=<default>,start=8192,size=22528>
+Created a new <removed>.
+Created a new <removed>.
+Created a new <removed>.
+Created a new <removed>.
+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 83 Linux
index 0b7cfcb0c601ecdbca1400ee4ee82c6100c83e39..eda506ef6b90168ba210735694b87e9fff69c158 100755 (executable)
@@ -43,6 +43,20 @@ ts_finalize_subtest
 
 $TS_CMD_WIPEFS --all --force ${TEST_IMAGE_NAME} &> /dev/null
 
+
+ts_init_subtest "mbr-primary-nopartno"
+$TESTPROG --label mbr --device ${TEST_IMAGE_NAME} -- \
+       -,2048,2048 \
+       -,4096,2048 \
+       -,6144,2048 \
+       -,8192,22528 \
+       >> $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
+
 ## no extended but partno > 4 requested
 ts_init_subtest "mbr-err-primary"
 $TESTPROG --label mbr --device ${TEST_IMAGE_NAME} \
@@ -86,6 +100,22 @@ ts_finalize_subtest
 
 $TS_CMD_WIPEFS --all --force ${TEST_IMAGE_NAME} &> /dev/null
 
+ts_init_subtest "mbr-nopartno"
+$TESTPROG --label mbr --device ${TEST_IMAGE_NAME} -- \
+       -,2048,2048 \
+       -,4096,2048 \
+       -,6144,2048 \
+       -,8192,22528 \
+       -,10240,2048 \
+       -,14336,2048 \
+       -,18432,12288 \
+               >> $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
+
 ### 6th partition (logical) out of extended
 ts_init_subtest "mbr-err-logical"
 $TESTPROG --label mbr --device ${TEST_IMAGE_NAME} \
@@ -131,6 +161,22 @@ ts_finalize_subtest
 
 $TS_CMD_WIPEFS --all --force ${TEST_IMAGE_NAME} &> /dev/null
 
+ts_init_subtest "gpt-nopartno"
+$TESTPROG --label gpt --device ${TEST_IMAGE_NAME} -- \
+       -,2048,2048 \
+       -,4096,2048 \
+       -,6144,2048 \
+       -,8192,2048 \
+       -,10240,2048 \
+       -,12288,2048 \
+       -,14336,2048 \
+               >> $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
+
 ### 4th partition overlap 4th and 5th
 ts_init_subtest "gpt-err-overlap"
 $TESTPROG --label gpt --device ${TEST_IMAGE_NAME} \