linux-f2fs-devel.lists.sourceforge.net archive mirror
 help / color / mirror / Atom feed
* [PATCH] mkfs.f2fs: check filesystem overwrite before formatting
@ 2017-02-26 13:29 Kinglong Mee
  0 siblings, 0 replies; only message in thread
From: Kinglong Mee @ 2017-02-26 13:29 UTC (permalink / raw)
  To: Jaegeuk Kim; +Cc: linux-f2fs-devel

Mkfs.f2fs doesn't check the overwrite of exist filesystem.
Avoid formatting an exist filesystem by mistake, a notice is important.
The code is modified from xfsprogs.

Signed-off-by: Kinglong Mee <kinglongmee@gmail.com>
---
 configure.ac            | 20 +++++++++--
 mkfs/Makefile.am        |  4 +--
 mkfs/f2fs_format_main.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 108 insertions(+), 5 deletions(-)

diff --git a/configure.ac b/configure.ac
index 6a3f7c4..d6de43b 100644
--- a/configure.ac
+++ b/configure.ac
@@ -48,8 +48,11 @@ AC_CHECK_HEADERS_ONCE([
 # Test configure options.
 AC_ARG_WITH([selinux],
 	AS_HELP_STRING([--without-selinux],
-	  [Ignore presence of libselinux and disable selinux support])
-)
+	  [Ignore presence of libselinux and disable selinux support]))
+
+AC_ARG_WITH([blkid],
+	AS_HELP_STRING([--without-blkid],
+	  [Ignore presence of libblkid and disable blkid support]))
 
 # Checks for programs.
 AC_PROG_CC
@@ -74,6 +77,19 @@ AS_IF([test "x$have_selinux" = "xyes"],
 	)]
 )
 
+AS_IF([test "x$with_blkid" != "xno"],
+	[PKG_CHECK_MODULES([libblkid], [blkid],
+	                   [have_blkid=yes], [have_blkid=no])],
+	[have_blkid=no]
+)
+
+AS_IF([test "x$have_blkid" = "xyes"],
+	[AC_DEFINE([HAVE_LIBBLKID], [1], [Use blkid])],
+	[AS_IF([test "x$with_blkid" = "xyes"],
+		[AC_MSG_ERROR([blkid support requested but libblkid not found])]
+	)]
+)
+
 # Checks for header files.
 AC_CHECK_HEADERS([linux/fs.h linux/blkzoned.h fcntl.h mntent.h stdlib.h string.h \
 		sys/ioctl.h sys/mount.h unistd.h linux/falloc.h byteswap.h])
diff --git a/mkfs/Makefile.am b/mkfs/Makefile.am
index 8b4c16c..162a0cf 100644
--- a/mkfs/Makefile.am
+++ b/mkfs/Makefile.am
@@ -1,10 +1,10 @@
 ## Makefile.am
 
-AM_CPPFLAGS = ${libuuid_CFLAGS} -I$(top_srcdir)/include
+AM_CPPFLAGS = ${libuuid_CFLAGS} ${libblkid_CFLAGS} -I$(top_srcdir)/include
 AM_CFLAGS = -Wall -DWITH_BLKDISCARD
 sbin_PROGRAMS = mkfs.f2fs
 mkfs_f2fs_SOURCES = f2fs_format_main.c f2fs_format.c f2fs_format_utils.c f2fs_format_utils.h $(top_srcdir)/include/f2fs_fs.h
-mkfs_f2fs_LDADD = ${libuuid_LIBS} $(top_builddir)/lib/libf2fs.la
+mkfs_f2fs_LDADD = ${libuuid_LIBS} ${libblkid_LIBS} $(top_builddir)/lib/libf2fs.la
 
 lib_LTLIBRARIES = libf2fs_format.la
 libf2fs_format_la_SOURCES = f2fs_format_main.c f2fs_format.c f2fs_format_utils.c
diff --git a/mkfs/f2fs_format_main.c b/mkfs/f2fs_format_main.c
index 5bb1faf..b26256d 100644
--- a/mkfs/f2fs_format_main.c
+++ b/mkfs/f2fs_format_main.c
@@ -18,10 +18,16 @@
 #include <time.h>
 #include <uuid/uuid.h>
 
+#include "config.h"
+#ifdef HAVE_LIBBLKID
+#  include <blkid/blkid.h>
+#endif
+
 #include "f2fs_fs.h"
 #include "f2fs_format_utils.h"
 
 extern struct f2fs_configuration c;
+static int force_overwrite = 0;
 
 static void mkfs_usage()
 {
@@ -72,7 +78,7 @@ static void parse_feature(const char *features)
 
 static void f2fs_parse_options(int argc, char *argv[])
 {
-	static const char *option_string = "qa:c:d:e:l:mo:O:s:z:t:";
+	static const char *option_string = "qa:c:d:e:l:mo:O:s:z:t:f";
 	int32_t option=0;
 
 	while ((option = getopt(argc,argv,option_string)) != EOF) {
@@ -128,6 +134,9 @@ static void f2fs_parse_options(int argc, char *argv[])
 		case 't':
 			c.trim = atoi(optarg);
 			break;
+		case 'f':
+			force_overwrite = 1;
+			break;
 		default:
 			MSG(0, "\tError: Unknown option %c\n",option);
 			mkfs_usage();
@@ -155,6 +164,79 @@ static void f2fs_parse_options(int argc, char *argv[])
 		c.feature |= cpu_to_le32(F2FS_FEATURE_BLKZONED);
 }
 
+#ifdef HAVE_LIBBLKID
+static int f2fs_dev_is_overwrite(const char *device)
+{
+	const char	*type;
+	blkid_probe	pr = NULL;
+	int		ret = -1;
+
+	if (!device || !*device)
+		return 0;
+
+	pr = blkid_new_probe_from_filename(device);
+	if (!pr)
+		goto out;
+
+	ret = blkid_probe_enable_partitions(pr, 1);
+	if (ret < 0)
+		goto out;
+
+	ret = blkid_do_fullprobe(pr);
+	if (ret < 0)
+		goto out;
+
+	/*
+	 * Blkid returns 1 for nothing found and 0 when it finds a signature,
+	 * but we want the exact opposite, so reverse the return value here.
+	 *
+	 * In addition print some useful diagnostics about what actually is
+	 * on the device.
+	 */
+	if (ret) {
+		ret = 0;
+		goto out;
+	}
+
+	if (!blkid_probe_lookup_value(pr, "TYPE", &type, NULL)) {
+		MSG(0, "\t%s appears to contain an existing filesystem (%s).\n",
+			device, type);
+	} else if (!blkid_probe_lookup_value(pr, "PTTYPE", &type, NULL)) {
+		MSG(0, "\t%s appears to contain a partition table (%s).\n",
+			device, type);
+	} else {
+		MSG(0, "\t%s appears to contain something weird according to blkid\n",
+			device);
+	}
+	ret = 1;
+out:
+	if (pr)
+		blkid_free_probe(pr);
+	if (ret == -1)
+		MSG(0, "\tprobe of %s failed, cannot detect existing filesystem.\n",
+			device);
+	return ret;
+}
+
+static int f2fs_check_overwrite(void)
+{
+	int i;
+
+	for (i = 0; i < c.ndevs; i++)
+		if (f2fs_dev_is_overwrite((char *)c.devices[i].path))
+			return -1;
+	return 0;
+}
+
+#else
+
+static int f2fs_check_overwrite(void)
+{
+	return 0;
+}
+
+#endif /* HAVE_LIBBLKID */
+
 int main(int argc, char *argv[])
 {
 	f2fs_init_configuration();
@@ -163,6 +245,11 @@ int main(int argc, char *argv[])
 
 	f2fs_show_info();
 
+	if (!force_overwrite && f2fs_check_overwrite()) {
+		MSG(0, "\tUse the -f option to force overwrite.\n");
+		return -1;
+	}
+
 	if (f2fs_devs_are_umounted() < 0) {
 		MSG(0, "\tError: Not available on mounted device!\n");
 		return -1;
-- 
2.9.3


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2017-02-26 13:30 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-02-26 13:29 [PATCH] mkfs.f2fs: check filesystem overwrite before formatting Kinglong Mee

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).