]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
cfdisk: add --lock and LOCK_BLOCK_DEVICE
authorKarel Zak <kzak@redhat.com>
Wed, 27 May 2020 14:58:08 +0000 (16:58 +0200)
committerKarel Zak <kzak@redhat.com>
Wed, 27 May 2020 14:58:08 +0000 (16:58 +0200)
Addresses: https://github.com/karelzak/util-linux/issues/921
Signed-off-by: Karel Zak <kzak@redhat.com>
disk-utils/cfdisk.8
disk-utils/cfdisk.c

index 902861a5bf15ffa38fd660eaedcf8eba4d5f9510..a61aad71f31df03a473ddb23e16d9c76dfcfd7dc 100644 (file)
@@ -61,6 +61,14 @@ can be \fBauto\fR, \fBnever\fR or \fBalways\fR.  If the \fIwhen\fR argument is o
 it defaults to \fBauto\fR.  The colors can be disabled, for the current built-in default
 see \fB\-\-help\fR output. See also the COLORS section.
 .TP
+\fB\-\-lock\fR[=\fImode\fR]
+Use exclusive BSD lock for device or file it operates.  The optional argument
+\fImode\fP can be \fByes\fR, \fBno\fR (or 1 and 0) or \fBnonblock\fR.  If the \fImode\fR
+argument is omitted, it defaults to \fB"yes"\fR.  This option overwrites
+environment variable \fB$LOCK_BLOCK_DEVICE\fR.  The default is not to use any
+lock at all, but it's recommended to avoid collisions with udevd or other
+tools.
+.TP
 .BR \-V , " \-\-version"
 Display version information and exit.
 .TP
@@ -186,6 +194,8 @@ enables libblkid debug output.
 enables libsmartcols debug output.
 .IP LIBSMARTCOLS_DEBUG_PADDING=on
 use visible padding characters. Requires enabled LIBSMARTCOLS_DEBUG.
+.IP LOCK_BLOCK_DEVICE=<mode>
+use exclusive BSD lock.  The mode is "1" or "0".  See \fB\-\-lock\fR for more details.
 
 .SH AUTHORS
 Karel Zak <kzak@redhat.com>
index 2fb442dc33a5987f54797a53ccb9992d20fcace6..c783f9daa3154143303f6effb1d69d0603b55ead 100644 (file)
@@ -64,6 +64,7 @@
 #include "colors.h"
 #include "debug.h"
 #include "list.h"
+#include "blkdev.h"
 
 static const char *default_disks[] = {
 #ifdef __GNU__
@@ -1725,6 +1726,7 @@ static int ui_refresh(struct cfdisk *cf)
        else
                ui_center(2, _("Label: %s"), fdisk_label_get_name(lb));
        free(strsz);
+       free(id);
 
        ui_draw_table(cf);
        ui_draw_menu(cf);
@@ -2647,6 +2649,8 @@ static void __attribute__((__noreturn__)) usage(void)
        fprintf(out,
                "                            %s\n", USAGE_COLORS_DEFAULT);
        fputs(_(" -z, --zero               start with zeroed partition table\n"), out);
+       fprintf(out,
+             _("     --lock[=<mode>]      use exclusive device lock (%s, %s or %s)\n"), "yes", "no", "nonblock");
 
        fputs(USAGE_SEPARATOR, out);
        printf(USAGE_HELP_OPTIONS(26));
@@ -2657,13 +2661,16 @@ static void __attribute__((__noreturn__)) usage(void)
 
 int main(int argc, char *argv[])
 {
-       const char *diskpath = NULL;
+       const char *diskpath = NULL, *lockmode = NULL;
        int rc, c, colormode = UL_COLORMODE_UNDEF;
        struct cfdisk _cf = { .lines_idx = 0 },
                      *cf = &_cf;
-
+       enum {
+               OPT_LOCK        = CHAR_MAX + 1
+       };
        static const struct option longopts[] = {
                { "color",   optional_argument, NULL, 'L' },
+               { "lock",    optional_argument, NULL, OPT_LOCK },
                { "help",    no_argument,       NULL, 'h' },
                { "version", no_argument,       NULL, 'V' },
                { "zero",    no_argument,       NULL, 'z' },
@@ -2691,6 +2698,14 @@ int main(int argc, char *argv[])
                case 'z':
                        cf->zero_start = 1;
                        break;
+               case OPT_LOCK:
+                       lockmode = "1";
+                       if (optarg) {
+                               if (*optarg == '=')
+                                       optarg++;
+                               lockmode = optarg;
+                       }
+                       break;
                default:
                        errtryhelp(EXIT_FAILURE);
                }
@@ -2728,6 +2743,9 @@ int main(int argc, char *argv[])
                err(EXIT_FAILURE, _("cannot open %s"), diskpath);
 
        if (!fdisk_is_readonly(cf->cxt)) {
+               if (blkdev_lock(fdisk_get_devfd(cf->cxt), diskpath, lockmode) != 0)
+                       return EXIT_FAILURE;
+
                cf->device_is_used = fdisk_device_is_used(cf->cxt);
                fdisk_get_partitions(cf->cxt, &cf->original_layout);
        }