Add start of mount check
authorJens Axboe <axboe@fb.com>
Fri, 22 May 2015 03:19:14 +0000 (21:19 -0600)
committerJens Axboe <axboe@fb.com>
Fri, 22 May 2015 03:19:14 +0000 (21:19 -0600)
For raw block devices, lets add some safety check that will check for
destructive tests whether a device is mounted or not. Will plumb this
in soon, and add an override option to continue regardless.

Signed-off-by: Jens Axboe <axboe@fb.com>
Makefile
configure
lib/mountcheck.c [new file with mode: 0644]

index d9aedf52c5a1126b9e6b811e397b5c78b742fd9e..b3acbcfe3c762529721be970de0a18be7c519a2f 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -36,7 +36,7 @@ SOURCE := gettime.c ioengines.c init.c stat.c log.c time.c filesetup.c \
                lib/lfsr.c gettime-thread.c helpers.c lib/flist_sort.c \
                lib/hweight.c lib/getrusage.c idletime.c td_error.c \
                profiles/tiobench.c profiles/act.c io_u_queue.c filelock.c \
                lib/lfsr.c gettime-thread.c helpers.c lib/flist_sort.c \
                lib/hweight.c lib/getrusage.c idletime.c td_error.c \
                profiles/tiobench.c profiles/act.c io_u_queue.c filelock.c \
-               lib/tp.c lib/bloom.c lib/gauss.c workqueue.c
+               lib/tp.c lib/bloom.c lib/gauss.c lib/mountcheck.c workqueue.c
 
 ifdef CONFIG_LIBHDFS
   HDFSFLAGS= -I $(JAVA_HOME)/include -I $(JAVA_HOME)/include/linux -I $(FIO_LIBHDFS_INCLUDE)
 
 ifdef CONFIG_LIBHDFS
   HDFSFLAGS= -I $(JAVA_HOME)/include -I $(JAVA_HOME)/include/linux -I $(FIO_LIBHDFS_INCLUDE)
index 3b871efa302acb522ed25f1f51da11fc838b0cb0..5c6129dc9331983a2795a7b13236c1768416d47d 100755 (executable)
--- a/configure
+++ b/configure
@@ -1453,6 +1453,25 @@ fi
 
 echo "lex/yacc for arithmetic       $arith"
 
 
 echo "lex/yacc for arithmetic       $arith"
 
+##########################################
+# Check whether we have setmntent/getmntent
+getmntent="no"
+cat > $TMPC << EOF
+#include <stdio.h>
+#include <mntent.h>
+int main(int argc, char **argv)
+{
+  FILE *mtab = setmntent(NULL, "r");
+  struct mntent *mnt = getmntent(mtab);
+  endmntent(mnt);
+  return 0;
+}
+EOF
+if compile_prog "" "" "getmntent"; then
+  getmntent="yes"
+fi
+echo "getmntent                     $getmntent"
+
 #############################################################################
 
 if test "$wordsize" = "64" ; then
 #############################################################################
 
 if test "$wordsize" = "64" ; then
@@ -1626,6 +1645,9 @@ if test "$arith" = "yes" ; then
     echo "YACC=$YACC" >> $config_host_mak
   fi
 fi
     echo "YACC=$YACC" >> $config_host_mak
   fi
 fi
+if test "$getmntent" = "yes" ; then
+  output_sym "CONFIG_GETMNTENT"
+fi
 
 if test "$zlib" = "no" ; then
   echo "Consider installing zlib-dev (zlib-devel), some fio features depend on it."
 
 if test "$zlib" = "no" ; then
   echo "Consider installing zlib-dev (zlib-devel), some fio features depend on it."
diff --git a/lib/mountcheck.c b/lib/mountcheck.c
new file mode 100644 (file)
index 0000000..dea2746
--- /dev/null
@@ -0,0 +1,39 @@
+#include <stdio.h>
+#include <string.h>
+#include <mntent.h>
+
+#ifdef CONFIG_GETMNTENT
+
+#define MTAB   "/etc/mtab"
+
+int device_is_mounted(const char *dev)
+{
+       FILE *mtab;
+       struct mntent *mnt;
+       int ret = 0;
+
+       mtab = setmntent(MTAB, "r");
+       if (!mtab)
+               return 0;
+
+       while ((mnt = getmntent(mtab)) != NULL) {
+               if (!mnt->mnt_fsname)
+                       continue;
+               if (!strcmp(mnt->mnt_fsname, dev)) {
+                       ret = 1;
+                       break;
+               }
+       }
+
+       endmntent(mtab);
+       return ret;
+}
+
+#else
+
+int device_is_mounted(const char *dev)
+{
+       return 0;
+}
+
+#endif