blkdiscard: Refuse to proceed if signatures are found
PATCH 1/1 blkdiscard: Refuse to proceed if signatures are found
Currently the blkdiscard has the ability to wipe out entere device in a matter of seconds. This is fine as long as it's intentional, it is potentially catastrophic if it's not.
With this commit blkdiscard will check for existing signatures on the device and refuse to continue if any are found unless the operation is forced with the -f option.
In an attempt to avoid breaking existing automation scripts the force is only required when stdin refers to a terminal.
--- sys-utils/Makemodule.am | 2 +- sys-utils/blkdiscard.c | 54 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 53 insertions(+), 3 deletions(-)
sys-utils/Makemodule.am +1 -1
@@ -165,7 +165,7 @@ if BUILD_BLKDISCARD sbin_PROGRAMS += blkdiscard dist_man_MANS += sys-utils/blkdiscard.8 blkdiscard_SOURCES = sys-utils/blkdiscard.c lib/monotonic.c -blkdiscard_LDADD = $(LDADD) libcommon.la $(REALTIME_LIBS) +blkdiscard_LDADD = $(LDADD) libblkid.la libcommon.la $(REALTIME_LIBS) endif if BUILD_BLKZONE
sys-utils/blkdiscard.c +52 -2
82 lines not on this page · raw message
Signature · 1 line
2.21.3
Karel Zak
maintainerQuoted 3 lines · Lukas Czerner
With this commit blkdiscard will check for existing signatures on the device and refuse to continue if any are found unless the operation is forced with the -f option.
Good idea.
Quoted 4 lines
+/*
+ * Check existing signature on the open fd
+ * Returns 0 if no signature was found
+ * 1 if a signature was found
this is not true, 0 means detected, 1 not found
Quoted 3 lines
+ * <0 on error
+ */
+static intdevice_empty(int fd, char *path)
This is difficult for to read, at first glance it seems according to function name that 1 means "yes, it's empty".
Maybe rename it to probe_device().
Quoted 5 lines
sys-utils/blkdiscard.c +5
-0+{ +const char *type; +blkid_probe pr =NULL ; +int ret =-1 ; +
sys-utils/blkdiscard.c static void __attribute__((__noreturn__)) usage(void) · line 131
Quoted 10 lines
sys-utils/blkdiscard.c +10
-0+ pr =blkid_new_probe (); +if (!pr ||blkid_probe_set_device (pr, fd,0 ,0 )) +return ret; + +blkid_probe_enable_superblocks (pr, TRUE); +blkid_probe_enable_partitions (pr, TRUE); + + ret =blkid_do_fullprobe (pr); +if (ret) +return ret;
yes, blkid_do_fullprobe() returns: 0 on success, 1 if nothing is detected or -1 on case of error.
sys-utils/blkdiscard.c static void __attribute__((__noreturn__)) usage(void) · line 142
Quoted 11 lines
sys-utils/blkdiscard.c +11
-0+ +if (!blkid_probe_lookup_value (pr,"TYPE" , &type,NULL )) { +warnx ("%s contains existing file system (%s)." ,path ,type); + }else if (!blkid_probe_lookup_value (pr,"PTTYPE" , &type,NULL )) { +warnx ("%s contains existing partition (%s)." ,path ,type); + }else { +warnx ("%s contains existing signature." , path); + } + +blkid_free_probe (pr); +return ret;
This is always 0.
Quoted 20 lines
sys-utils/blkdiscard.c +3
-2+}int main (int argc,char **argv) {char *path; -int c, fd, verbose =0 , secsize, force =0 ; +int c, fd, ret, verbose =0 , secsize, force =0 ;uint64_t end, blksize, step, range[2 ], stats[2 ];struct stat sb;struct timeval now, last;@@ -184,7 +219,7 @@ int main(int argc, char **argv)errtryhelp (EXIT_FAILURE ); } - fd =open (path,O_WRONLY | (force ?0 :O_EXCL )); + fd =open (path,O_RDWR | (force ?0 :O_EXCL ));if (fd <0 )err (EXIT_FAILURE ,_ ("cannot open %s" ), path);
sys-utils/blkdiscard.c int main(int argc, char **argv) · line 256
Quoted 5 lines
sys-utils/blkdiscard.c +2
-0@@ -217,6 +252,21 @@ int main(int argc, char **argv)errx (EXIT_FAILURE ,_ ("%s: length %" PRIu64" is not aligned " "to sector size %i" ), path, range[1 ], secsize); +/* Check for existing signatures on the device */ +if ((ret =device_empty (fd, path)) ==0 ) {
What about:
switch (probe_device(fd, path)) {
case 0: /* signature detected */
sys-utils/blkdiscard.c int main(int argc, char **argv) · line 266
Quoted 10 lines
sys-utils/blkdiscard.c +10
-0+/* +* Only require force in interactive mode to avoid +* breaking existing scripts +*/ +if (!force &&isatty (STDIN_FILENO )) { +errx (EXIT_FAILURE , +_ ("This is destructive operation, data will " \ +"be lost! Use the -f option to override." )); + } +warnx (_ ("Operation forced, data will be lost!" ));
break;
case 1: /* no signature */
break;
default: /* error */
sys-utils/blkdiscard.c int main(int argc, char **argv) · line 268
Quoted 1 line
sys-utils/blkdiscard.c +1
-0+err (EXIT_FAILURE ,_ ("failed to probe the device" ));
break;
}
I think it's more readable ;-)
Karel
Signature · 2 lines
Karel Zak <[email protected]> http://karelzak.blogspot.com
Quoted 100 lines · Karel Zak
Quoted 3 lines · Lukas Czerner
With this commit blkdiscard will check for existing signatures on the device and refuse to continue if any are found unless the operation is forced with the -f option.
Good idea.
Quoted 4 lines
+/*
+ * Check existing signature on the open fd
+ * Returns 0 if no signature was found
+ * 1 if a signature was foundthis is not true, 0 means detected, 1 not found
Quoted 3 lines
+ * <0 on error
+ */
+static intdevice_empty(int fd, char *path)This is difficult for to read, at first glance it seems according to function name that 1 means "yes, it's empty".
Maybe rename it to
probe_device().Quoted 5 lines
sys-utils/blkdiscard.c +5
-0+{ +const char *type; +blkid_probe pr =NULL ; +int ret =-1 ; +sys-utils/blkdiscard.c static void __attribute__((__noreturn__)) usage(void) · line 131
Quoted 10 lines
sys-utils/blkdiscard.c +10
-0+ pr =blkid_new_probe (); +if (!pr ||blkid_probe_set_device (pr, fd,0 ,0 )) +return ret; + +blkid_probe_enable_superblocks (pr, TRUE); +blkid_probe_enable_partitions (pr, TRUE); + + ret =blkid_do_fullprobe (pr); +if (ret) +return ret;yes,
blkid_do_fullprobe()returns: 0 on success, 1 if nothing is detected or -1 on case of error.sys-utils/blkdiscard.c static void __attribute__((__noreturn__)) usage(void) · line 142
Quoted 11 lines
sys-utils/blkdiscard.c +11
-0+ +if (!blkid_probe_lookup_value (pr,"TYPE" , &type,NULL )) { +warnx ("%s contains existing file system (%s)." ,path ,type); + }else if (!blkid_probe_lookup_value (pr,"PTTYPE" , &type,NULL )) { +warnx ("%s contains existing partition (%s)." ,path ,type); + }else { +warnx ("%s contains existing signature." , path); + } + +blkid_free_probe (pr); +return ret;This is always 0.
Quoted 20 lines
sys-utils/blkdiscard.c +3
-2+}int main (int argc,char **argv) {char *path; -int c, fd, verbose =0 , secsize, force =0 ; +int c, fd, ret, verbose =0 , secsize, force =0 ;uint64_t end, blksize, step, range[2 ], stats[2 ];struct stat sb;struct timeval now, last;@@ -184,7 +219,7 @@ int main(int argc, char **argv)errtryhelp (EXIT_FAILURE ); } - fd =open (path,O_WRONLY | (force ?0 :O_EXCL )); + fd =open (path,O_RDWR | (force ?0 :O_EXCL ));if (fd <0 )err (EXIT_FAILURE ,_ ("cannot open %s" ), path);sys-utils/blkdiscard.c int main(int argc, char **argv) · line 256
Quoted 5 lines
sys-utils/blkdiscard.c +2
-0@@ -217,6 +252,21 @@ int main(int argc, char **argv)errx (EXIT_FAILURE ,_ ("%s: length %" PRIu64" is not aligned " "to sector size %i" ), path, range[1 ], secsize); +/* Check for existing signatures on the device */ +if ((ret =device_empty (fd, path)) ==0 ) {What about:
switch (
probe_device(fd, path)) {
case 0: /* signature detected */sys-utils/blkdiscard.c int main(int argc, char **argv) · line 266
Quoted 10 lines
sys-utils/blkdiscard.c +10
-0+/* +* Only require force in interactive mode to avoid +* breaking existing scripts +*/ +if (!force &&isatty (STDIN_FILENO )) { +errx (EXIT_FAILURE , +_ ("This is destructive operation, data will " \ +"be lost! Use the -f option to override." )); + } +warnx (_ ("Operation forced, data will be lost!" ));break;case 1: /* no signature */
break;
Quoted 7 lines
default: /* error */
sys-utils/blkdiscard.c int main(int argc, char **argv) · line 268
Quoted 1 line
sys-utils/blkdiscard.c +1
-0+err (EXIT_FAILURE ,_ ("failed to probe the device" ));break;}
I think it's more readable ;-)
Sure, I can do that. The bad comment definitelly makes it more confusing that it needed to be :)
if (!device_empty()) was the intention but then I still have to deal with the error. I'll do the switch change.
-Lukas
Quoted 6 lines
Karel
Signature · 2 lines
Karel Zak <[email protected]> http://karelzak.blogspot.com