From e57cbfe0aada02ad8556a3c6dcd6a5018bde9c01 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Wed, 14 Oct 2020 11:33:13 +0200 Subject: [PATCH] mkfs.minix: add --lock and LOCK_BLOCK_DEVICE Addresses: https://github.com/karelzak/util-linux/issues/921 Signed-off-by: Karel Zak --- disk-utils/mkfs.minix.8 | 11 +++++++++++ disk-utils/mkfs.minix.c | 17 +++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/disk-utils/mkfs.minix.8 b/disk-utils/mkfs.minix.8 index a33d844aa5..476fa8cd61 100644 --- a/disk-utils/mkfs.minix.8 +++ b/disk-utils/mkfs.minix.8 @@ -46,6 +46,14 @@ Specify the maximum length of filenames. Currently, the only allowable values are 14 and 30 for file system versions 1 and 2. Version 3 allows only value 60. The default is 30. .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 \fB\-i\fR, \fB\-\-inodes\fR \fInumber\fR Specify the number of inodes for the filesystem. .TP @@ -70,6 +78,9 @@ with other options. .TP \fB\-h\fR, \fB\-\-help\fR Display help text and exit. +.SH ENVIRONMENT +.IP LOCK_BLOCK_DEVICE= +use exclusive BSD lock. The mode is "1" or "0". See \fB\-\-lock\fR for more details. .SH EXIT STATUS The exit status returned by .B mkfs.minix diff --git a/disk-utils/mkfs.minix.c b/disk-utils/mkfs.minix.c index d6f5756a6c..1710616bdf 100644 --- a/disk-utils/mkfs.minix.c +++ b/disk-utils/mkfs.minix.c @@ -105,6 +105,7 @@ static char *inode_buffer = NULL; struct fs_control { char *device_name; /* device on a Minix file system is created */ int device_fd; /* open file descriptor of the device */ + char *lockmode; /* as specified by --lock */ unsigned long long fs_blocks; /* device block count for the file system */ int fs_used_blocks; /* used blocks on a device */ int fs_bad_blocks; /* number of bad blocks found from device */ @@ -144,6 +145,8 @@ static void __attribute__((__noreturn__)) usage(void) fputs(_(" -i, --inodes number of inodes for the filesystem\n"), out); fputs(_(" -c, --check check the device for bad blocks\n"), out); fputs(_(" -l, --badblocks list of bad blocks from file\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(25)); printf(USAGE_MAN_TAIL("mkfs.minix(8)")); @@ -745,6 +748,9 @@ int main(int argc, char ** argv) int i; struct stat statbuf; char * listfile = NULL; + enum { + OPT_LOCK = CHAR_MAX + 1 + }; static const struct option longopts[] = { {"namelength", required_argument, NULL, 'n'}, {"inodes", required_argument, NULL, 'i'}, @@ -752,6 +758,7 @@ int main(int argc, char ** argv) {"badblocks", required_argument, NULL, 'l'}, {"version", no_argument, NULL, 'V'}, {"help", no_argument, NULL, 'h'}, + {"lock",optional_argument, NULL, OPT_LOCK}, {NULL, 0, NULL, 0} }; @@ -791,6 +798,14 @@ int main(int argc, char ** argv) case 'l': listfile = optarg; break; + case OPT_LOCK: + ctl.lockmode = "1"; + if (optarg) { + if (*optarg == '=') + optarg++; + ctl.lockmode = optarg; + } + break; case 'V': print_version(MKFS_EX_OK); case 'h': @@ -821,6 +836,8 @@ int main(int argc, char ** argv) ctl.device_fd = open_blkdev_or_file(&statbuf, ctl.device_name, O_RDWR); if (ctl.device_fd < 0) err(MKFS_EX_ERROR, _("cannot open %s"), ctl.device_name); + if (blkdev_lock(ctl.device_fd, ctl.device_name, ctl.lockmode) != 0) + exit(MKFS_EX_ERROR); determine_device_blocks(&ctl, &statbuf); setup_tables(&ctl); if (ctl.check_blocks) -- 2.47.2