cfdisk: add --lock and LOCK_BLOCK_DEVICE

Addresses: https://github.com/karelzak/util-linux/issues/921
Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Karel Zak 2020-05-27 16:58:08 +02:00
parent 37b302046a
commit ec8f712157
2 changed files with 30 additions and 2 deletions

View 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>

View 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);
}