]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - libfdisk/samples/mkpart.c
wipefs: add --lock and LOCK_BLOCK_DEVICE
[thirdparty/util-linux.git] / libfdisk / samples / mkpart.c
index 7871015cf845e0c9045d3f15201717a539c2e91d..1e5fd99e551ba488ee3c740ec44938e252705409 100644 (file)
@@ -68,12 +68,15 @@ 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;
+       uint64_t grain = 0;
 
        static const struct option longopts[] = {
                { "label",  required_argument, NULL, 'x' },
                { "device", required_argument, NULL, 'd' },
+               { "nopartno", no_argument, NULL, 'p' },
+               { "grain", required_argument, NULL, 'g' },
                { "help",   no_argument, NULL, 'h' },
                { NULL, 0, NULL, 0 },
        };
@@ -82,7 +85,7 @@ int main(int argc, char *argv[])
 
        fdisk_init_debug(0);
 
-       while((c = getopt_long(argc, argv, "x:d:h", longopts, NULL)) != -1) {
+       while((c = getopt_long(argc, argv, "g:x:d:h", longopts, NULL)) != -1) {
                switch(c) {
                case 'x':
                        label = optarg;
@@ -90,6 +93,12 @@ int main(int argc, char *argv[])
                case 'd':
                        device = optarg;
                        break;
+               case 'p':
+                       nopartno = 1;
+                       break;
+               case 'g':
+                       grain = strtosize_or_err(optarg, "failed to parse grain");
+                       break;
                case 'h':
                        printf("%s [options] <size> ...", program_invocation_short_name);
                        fputs(USAGE_SEPARATOR, stdout);
@@ -97,6 +106,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;
@@ -113,6 +123,9 @@ int main(int argc, char *argv[])
                err_oom();
        fdisk_set_ask(cxt, ask_callback, NULL);
 
+       if (grain)
+               fdisk_save_user_grain(cxt, grain);
+
        pa = fdisk_new_partition();
        if (!pa)
                err_oom();
@@ -124,6 +137,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 +147,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 +164,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 +181,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) {