]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
wipefs: 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 15:00:08 +0000 (17:00 +0200)
Addresses: https://github.com/karelzak/util-linux/issues/921
Signed-off-by: Karel Zak <kzak@redhat.com>
misc-utils/wipefs.8
misc-utils/wipefs.c

index 01081fa8e74d1b8f0b9ab377d78278560ffc580c..cdc6b281192fddd62bf8fdd0f0ef28fc02efa994 100644 (file)
@@ -77,6 +77,14 @@ Display help text and exit.
 .BR \-J , " \-\-json"
 Use JSON output format.
 .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 \-i , " \-\-noheadings"
 Do not print a header line.
 .TP
@@ -115,6 +123,8 @@ Display version information and exit.
 .SH ENVIRONMENT
 .IP LIBBLKID_DEBUG=all
 enables libblkid debug output.
+.IP LOCK_BLOCK_DEVICE=<mode>
+use exclusive BSD lock.  The mode is "1" or "0".  See \fB\-\-lock\fR for more details.
 .SH EXAMPLE
 .TP
 .B wipefs /dev/sda*
index 2a16d62229cef7f7f5ee6858ba8a3ce05e8a56cb..77dfe6a28fdd08f8005bfe336e145baced1776aa 100644 (file)
@@ -64,6 +64,7 @@ struct wipe_desc {
 struct wipe_control {
        char            *devname;
        const char      *type_pattern;          /* -t <pattern> */
+       const char      *lockmode;
 
        struct libscols_table *outtab;
        struct wipe_desc *offsets;              /* -o <offset> -o <offset> ... */
@@ -546,6 +547,12 @@ static int do_wipe(struct wipe_control *ctl)
        if (!pr)
                return -errno;
 
+       if (blkdev_lock(blkid_probe_get_fd(pr),
+                       ctl->devname, ctl->lockmode) != 0) {
+               blkid_free_probe(pr);
+               return -1;
+       }
+
        if (ctl->backup) {
                const char *home = getenv ("HOME");
                char *tmp = xstrdup(ctl->devname);
@@ -655,6 +662,8 @@ usage(void)
        puts(_(" -p, --parsable      print out in parsable instead of printable format"));
        puts(_(" -q, --quiet         suppress output messages"));
        puts(_(" -t, --types <list>  limit the set of filesystem, RAIDs or partition tables"));
+       printf(
+            _("     --lock[=<mode>] use exclusive device lock (%s, %s or %s)\n"), "yes", "no", "nonblock");
 
        printf(USAGE_HELP_OPTIONS(21));
 
@@ -676,12 +685,15 @@ main(int argc, char **argv)
        struct wipe_control ctl = { .devname = NULL };
        int c;
        char *outarg = NULL;
-
+       enum {
+               OPT_LOCK = CHAR_MAX + 1,
+       };
        static const struct option longopts[] = {
            { "all",       no_argument,       NULL, 'a' },
            { "backup",    no_argument,       NULL, 'b' },
            { "force",     no_argument,       NULL, 'f' },
            { "help",      no_argument,       NULL, 'h' },
+           { "lock",      optional_argument, NULL, OPT_LOCK },
            { "no-act",    no_argument,       NULL, 'n' },
            { "offset",    required_argument, NULL, 'o' },
            { "parsable",  no_argument,       NULL, 'p' },
@@ -745,7 +757,14 @@ main(int argc, char **argv)
                case 't':
                        ctl.type_pattern = optarg;
                        break;
-
+               case OPT_LOCK:
+                       ctl.lockmode = "1";
+                       if (optarg) {
+                               if (*optarg == '=')
+                                       optarg++;
+                               ctl.lockmode = optarg;
+                       }
+                       break;
                case 'h':
                        usage();
                case 'V':