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' },
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);
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");
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))
--- /dev/null
+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
--- /dev/null
+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
--- /dev/null
+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
$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} \
$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} \
$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} \