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);
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);
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)));
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;
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");
+}
#define _TOOLS_UTIL_H
#include <errno.h>
+#include <mntent.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
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 { \