summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKent Overstreet <kent.overstreet@gmail.com>2019-03-24 21:07:45 -0400
committerKent Overstreet <kent.overstreet@gmail.com>2019-03-24 21:07:45 -0400
commit9b08492bc733f3b1b1eefc87a4c52d9673432e42 (patch)
tree1f4d9deb4d8f92baa601dc192201672558a68b32
parentddb58076ef4fe4572ab8537785fc67052f47bf5b (diff)
check if fs is mounted before running fsck
-rw-r--r--cmd_device.c4
-rw-r--r--cmd_fsck.c5
-rw-r--r--tools-util.c28
-rw-r--r--tools-util.h4
4 files changed, 23 insertions, 18 deletions
diff --git a/cmd_device.c b/cmd_device.c
index 797b958c..92d3125b 100644
--- a/cmd_device.c
+++ b/cmd_device.c
@@ -385,14 +385,14 @@ int cmd_device_resize(int argc, char *argv[])
struct stat dev_stat = xfstat(dev_fd);
- char *mount = dev_to_mount(dev);
+ struct mntent *mount = dev_to_mount(dev);
if (mount) {
if (!S_ISBLK(dev_stat.st_mode))
die("%s is mounted but isn't a block device?!", dev);
printf("Doing online resize of %s\n", dev);
- struct bchfs_handle fs = bcache_fs_open(mount);
+ struct bchfs_handle fs = bcache_fs_open(mount->mnt_dir);
unsigned idx = bchu_disk_get_idx(fs, dev_stat.st_rdev);
diff --git a/cmd_fsck.c b/cmd_fsck.c
index 617cf252..e0961b99 100644
--- a/cmd_fsck.c
+++ b/cmd_fsck.c
@@ -23,6 +23,7 @@ static void usage(void)
int cmd_fsck(int argc, char *argv[])
{
struct bch_opts opts = bch2_opts_empty();
+ unsigned i;
int opt, ret = 0;
opt_set(opts, degraded, true);
@@ -56,6 +57,10 @@ int cmd_fsck(int argc, char *argv[])
if (!argc)
die("Please supply device(s) to check");
+ for (i = 0; i < argc; i++)
+ if (dev_mounted_rw(argv[i]))
+ die("%s is mounted read-write - aborting", argv[i]);
+
struct bch_fs *c = bch2_fs_open(argv, argc, opts);
if (IS_ERR(c))
die("error opening %s: %s", argv[0], strerror(-PTR_ERR(c)));
diff --git a/tools-util.c b/tools-util.c
index 486bbacf..e4d2beae 100644
--- a/tools-util.c
+++ b/tools-util.c
@@ -610,26 +610,18 @@ char *dev_to_path(dev_t dev)
return path;
}
-char *dev_to_mount(char *dev)
+struct mntent *dev_to_mount(char *dev)
{
- char *line = NULL, *ret = NULL;
- size_t n = 0;
-
- FILE *f = fopen("/proc/mounts", "r");
+ struct mntent *mnt, *ret = NULL;
+ FILE *f = setmntent("/proc/mounts", "r");
if (!f)
die("error opening /proc/mounts: %m");
struct stat d1 = xstat(dev);
- while (getline(&line, &n, f) != -1) {
- char *d, *p = line;
- char *devs = strsep(&p, " ");
- char *mount = strsep(&p, " ");
+ while ((mnt = getmntent(f))) {
+ char *d, *p = mnt->mnt_fsname;
- if (!devs || !mount)
- continue;
-
- p = devs;
while ((d = strsep(&p, ":"))) {
struct stat d2;
@@ -648,12 +640,18 @@ char *dev_to_mount(char *dev)
continue;
}
- ret = strdup(mount);
+ ret = mnt;
goto found;
}
}
found:
fclose(f);
- free(line);
return ret;
}
+
+bool dev_mounted_rw(char *dev)
+{
+ struct mntent *mnt = dev_to_mount(dev);
+
+ return mnt && !hasmntopt(mnt, "ro");
+}
diff --git a/tools-util.h b/tools-util.h
index ae63f723..e5c35084 100644
--- a/tools-util.h
+++ b/tools-util.h
@@ -2,6 +2,7 @@
#define _TOOLS_UTIL_H
#include <errno.h>
+#include <mntent.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
@@ -153,7 +154,8 @@ u32 crc32c(u32, const void *, size_t);
char *dev_to_name(dev_t);
char *dev_to_path(dev_t);
-char *dev_to_mount(char *);
+struct mntent *dev_to_mount(char *);
+bool dev_mounted_rw(char *);
#define args_shift(_nr) \
do { \