From ec8f712157fee9a23dead612627dc3851e325f6f Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Wed, 27 May 2020 16:58:08 +0200 Subject: [PATCH] cfdisk: add --lock and LOCK_BLOCK_DEVICE Addresses: https://github.com/karelzak/util-linux/issues/921 Signed-off-by: Karel Zak --- disk-utils/cfdisk.8 | 10 ++++++++++ disk-utils/cfdisk.c | 22 ++++++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/disk-utils/cfdisk.8 b/disk-utils/cfdisk.8 index 902861a5bf..a61aad71f3 100644 --- a/disk-utils/cfdisk.8 +++ b/disk-utils/cfdisk.8 @@ -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= +use exclusive BSD lock. The mode is "1" or "0". See \fB\-\-lock\fR for more details. .SH AUTHORS Karel Zak diff --git a/disk-utils/cfdisk.c b/disk-utils/cfdisk.c index 2fb442dc33..c783f9daa3 100644 --- a/disk-utils/cfdisk.c +++ b/disk-utils/cfdisk.c @@ -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[=] 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); } -- 2.39.2