From: Karel Zak Date: Wed, 19 Jul 2017 09:42:50 +0000 (+0200) Subject: libfdisk: support default partno in mkpart-fullspec sample X-Git-Tag: v2.31-rc1~182 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=ea36907965313ca70ec6d803814aeba02a89667e;p=thirdparty%2Futil-linux.git libfdisk: support default partno in mkpart-fullspec sample Signed-off-by: Karel Zak --- diff --git a/libfdisk/samples/mkpart-fullspec.c b/libfdisk/samples/mkpart-fullspec.c index e919c545fb..01f1ebabe1 100644 --- a/libfdisk/samples/mkpart-fullspec.c +++ b/libfdisk/samples/mkpart-fullspec.c @@ -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] ...", program_invocation_short_name); + printf("%s [options] -- ...", program_invocation_short_name); fputs(USAGE_SEPARATOR, stdout); puts("Make disklabel and partitions."); - puts(" number from 1..n (4th is extended for MBR)"); + puts(" 1..n (4th is extended for MBR), or '-' for default"); puts(" partition start offset in sectors"); puts(" 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: \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: \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: ,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 index 0000000000..2c1423b751 --- /dev/null +++ b/tests/expected/libfdisk/mkpart-full-gpt-nopartno @@ -0,0 +1,30 @@ +Requested partition: ,start=2048,size=2048> +Requested partition: ,start=4096,size=2048> +Requested partition: ,start=6144,size=2048> +Requested partition: ,start=8192,size=2048> +Requested partition: ,start=10240,size=2048> +Requested partition: ,start=12288,size=2048> +Requested partition: ,start=14336,size=2048> +Created a new . +Created a new . +Created a new . +Created a new . +Created a new . +Created a new . +Created a new . +Created a new . +Disk : 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 / bytes +Disklabel type: gpt +Disk identifier: + +Device Start End Sectors Size Type +1 2048 4095 2048 1M Linux filesystem +2 4096 6143 2048 1M Linux filesystem +3 6144 8191 2048 1M Linux filesystem +4 8192 10239 2048 1M Linux filesystem +5 10240 12287 2048 1M Linux filesystem +6 12288 14335 2048 1M Linux filesystem +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 index 0000000000..23f7ad5a75 --- /dev/null +++ b/tests/expected/libfdisk/mkpart-full-mbr-nopartno @@ -0,0 +1,33 @@ +Requested partition: ,start=2048,size=2048> +Requested partition: ,start=4096,size=2048> +Requested partition: ,start=6144,size=2048> +Requested partition: ,start=8192,size=22528> +Requested partition: ,start=10240,size=2048> +Requested partition: ,start=14336,size=2048> +Requested partition: ,start=18432,size=12288> +Created a new . +Created a new . +Created a new . +Created a new . +Created a new . +Adding logical partition 5 +Created a new . +Adding logical partition 6 +Created a new . +Adding logical partition 7 +Created a new . +Disk : 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 / bytes +Disklabel type: dos +Disk identifier: + +Device Boot Start End Sectors Size Id Type +1 2048 4095 2048 1M 83 Linux +2 4096 6143 2048 1M 83 Linux +3 6144 8191 2048 1M 83 Linux +4 8192 30719 22528 11M 5 Extended +5 10240 12287 2048 1M 83 Linux +6 14336 16383 2048 1M 83 Linux +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 index 0000000000..90a71f3ab2 --- /dev/null +++ b/tests/expected/libfdisk/mkpart-full-mbr-primary-nopartno @@ -0,0 +1,21 @@ +Requested partition: ,start=2048,size=2048> +Requested partition: ,start=4096,size=2048> +Requested partition: ,start=6144,size=2048> +Requested partition: ,start=8192,size=22528> +Created a new . +Created a new . +Created a new . +Created a new . +Created a new . +Disk : 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 / bytes +Disklabel type: dos +Disk identifier: + +Device Boot Start End Sectors Size Id Type +1 2048 4095 2048 1M 83 Linux +2 4096 6143 2048 1M 83 Linux +3 6144 8191 2048 1M 83 Linux +4 8192 30719 22528 11M 83 Linux diff --git a/tests/ts/libfdisk/mkpart-full b/tests/ts/libfdisk/mkpart-full index 0b7cfcb0c6..eda506ef6b 100755 --- a/tests/ts/libfdisk/mkpart-full +++ b/tests/ts/libfdisk/mkpart-full @@ -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} \