From dd3b72b3e9f8188addc14fe574cd67ecdff43a51 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Tue, 1 Aug 2017 13:51:27 +0200 Subject: [PATCH] libfdisk: add --nopartno to mkpart sample * 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 --- libfdisk/samples/mkpart.c | 20 +++++++++++----- tests/expected/libfdisk/mkpart-mbr-nopartno | 26 +++++++++++++++++++++ tests/ts/libfdisk/mkpart | 9 +++++++ 3 files changed, 49 insertions(+), 6 deletions(-) create mode 100644 tests/expected/libfdisk/mkpart-mbr-nopartno diff --git a/libfdisk/samples/mkpart.c b/libfdisk/samples/mkpart.c index 7871015cf..3c964de99 100644 --- a/libfdisk/samples/mkpart.c +++ b/libfdisk/samples/mkpart.c @@ -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] ...", 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 disk label type\n", stdout); fputs(" -d, --device 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 index 000000000..178eaf2f2 --- /dev/null +++ b/tests/expected/libfdisk/mkpart-mbr-nopartno @@ -0,0 +1,26 @@ +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/ts/libfdisk/mkpart b/tests/ts/libfdisk/mkpart index 850b1c5aa..7365881c2 100755 --- a/tests/ts/libfdisk/mkpart +++ b/tests/ts/libfdisk/mkpart @@ -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