]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
cfdisk: add --sector-size commanand line option
authorKarel Zak <kzak@redhat.com>
Mon, 14 Oct 2024 13:48:46 +0000 (15:48 +0200)
committerKarel Zak <kzak@redhat.com>
Mon, 14 Oct 2024 13:48:46 +0000 (15:48 +0200)
* improves compatibility with fdisk
* add ability to work with disk images where libfdisk defaults to 512

Signed-off-by: Karel Zak <kzak@redhat.com>
disk-utils/cfdisk.8.adoc
disk-utils/cfdisk.c

index 97fad621d126ad6d9b5da59c77388a3c779ca621..759f39f0e1a37b16c20eeca521b535b3083d8d4d 100644 (file)
@@ -55,6 +55,9 @@ Use exclusive BSD lock for device or file it operates. The optional argument _mo
 *-r*, *--read-only*::
 Forced open in read-only mode.
 
+*-b*, *--sector-size* _sectorsize_::
+Specify the sector size of the disk. Valid values are 512, 1024, 2048, and 4096. The kernel is aware of the sector size for regular block devices. Use this option only on very old kernels, when working with disk images, or to override the kernel's default sector size. Since util-linux-2.17, *fdisk* distinguishes between logical and physical sector size. This option changes both sector sizes to the specified _sectorsize_.
+
 *-z*, *--zero*::
 Start with an in-memory zeroed partition table. This option does not zero the partition table on the disk; rather, it simply starts the program without reading the existing partition table. This option allows you to create a new partition table from scratch or from an *sfdisk*(8)-compatible script.
 
index 2e0aada474a5a019d3f95cbb7edaac3c07b8e46b..bc1c87d83e4ef429d694e5e2224abfe89bf28963 100644 (file)
@@ -2731,6 +2731,8 @@ static void __attribute__((__noreturn__)) usage(void)
              _("     --lock[=<mode>]      use exclusive device lock (%s, %s or %s)\n"), "yes", "no", "nonblock");
        fputs(_(" -r, --read-only          forced open cfdisk in read-only mode\n"), out);
 
+       fputs(_(" -b, --sector-size <size> physical and logical sector size\n"), out);
+
        fputs(USAGE_SEPARATOR, out);
        fprintf(out, USAGE_HELP_OPTIONS(26));
 
@@ -2743,6 +2745,7 @@ int main(int argc, char *argv[])
        const char *diskpath = NULL, *lockmode = NULL;
        int rc, c, colormode = UL_COLORMODE_UNDEF;
        int read_only = 0;
+       size_t user_ss = 0;
        struct cfdisk _cf = { .lines_idx = 0 },
                      *cf = &_cf;
        enum {
@@ -2752,6 +2755,7 @@ int main(int argc, char *argv[])
                { "color",   optional_argument, NULL, 'L' },
                { "lock",    optional_argument, NULL, OPT_LOCK },
                { "help",    no_argument,       NULL, 'h' },
+               { "sector-size", required_argument, NULL, 'b' },
                { "version", no_argument,       NULL, 'V' },
                { "zero",    no_argument,       NULL, 'z' },
                { "read-only", no_argument,     NULL, 'r' },
@@ -2763,8 +2767,15 @@ int main(int argc, char *argv[])
        textdomain(PACKAGE);
        close_stdout_atexit();
 
-       while((c = getopt_long(argc, argv, "L::hVzr", longopts, NULL)) != -1) {
+       while((c = getopt_long(argc, argv, "b:L::hVzr", longopts, NULL)) != -1) {
                switch(c) {
+               case 'b':
+                       user_ss = strtou32_or_err(optarg,
+                                       _("invalid sector size argument"));
+                       if (user_ss != 512 && user_ss != 1024 &&
+                           user_ss != 2048 && user_ss != 4096)
+                               errx(EXIT_FAILURE, _("invalid sector size argument"));
+                       break;
                case 'h':
                        usage();
                        break;
@@ -2803,6 +2814,8 @@ int main(int argc, char *argv[])
        cf->cxt = fdisk_new_context();
        if (!cf->cxt)
                err(EXIT_FAILURE, _("failed to allocate libfdisk context"));
+       if (user_ss)
+               fdisk_save_user_sector_size(cf->cxt, user_ss, user_ss);
 
        fdisk_set_ask(cf->cxt, ask_callback, (void *) cf);