check if fs is mounted before running fsck

This commit is contained in:
Kent Overstreet 2019-03-24 21:07:45 -04:00
parent ddb58076ef
commit 9b08492bc7
4 changed files with 23 additions and 18 deletions

View File

@ -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);

View File

@ -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)));

View File

@ -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");
}

View File

@ -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 { \