check if partition is mounted before mkfs

This saves from the blunder of formatting a live mounted filesystem.
This can be extended to get the mount flags of the filesystem
mounted.

Signed-off-by: Goldwyn Rodrigues <rgoldwyn@gmail.com>
This commit is contained in:
Goldwyn Rodrigues 2008-04-01 10:36:46 -04:00 committed by David Woodhouse
parent ad67cd73b7
commit c2c5e3e7e5
3 changed files with 73 additions and 0 deletions

19
mkfs.c
View File

@ -197,6 +197,15 @@ int main(int ac, char **av)
print_usage(); print_usage();
file = av[optind++]; file = av[optind++];
ret = check_mounted(file);
if (ret < 0) {
fprintf(stderr, "error checking %s mount status\n", file);
exit(1);
}
if (ret == 1) {
fprintf(stderr, "%s is mounted\n", file);
exit(1);
}
ac--; ac--;
fd = open(file, O_RDWR); fd = open(file, O_RDWR);
if (fd < 0) { if (fd < 0) {
@ -241,6 +250,16 @@ int main(int ac, char **av)
zero_end = 1; zero_end = 1;
while(ac-- > 0) { while(ac-- > 0) {
file = av[optind++]; file = av[optind++];
ret = check_mounted(file);
if (ret < 0) {
fprintf(stderr, "error checking %s mount status\n",
file);
exit(1);
}
if (ret == 1) {
fprintf(stderr, "%s is mounted\n", file);
exit(1);
}
fd = open(file, O_RDWR); fd = open(file, O_RDWR);
if (fd < 0) { if (fd < 0) {
fprintf(stderr, "unable to open %s\n", file); fprintf(stderr, "unable to open %s\n", file);

53
utils.c
View File

@ -30,6 +30,7 @@
#include <dirent.h> #include <dirent.h>
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include <mntent.h>
#include "kerncompat.h" #include "kerncompat.h"
#include "radix-tree.h" #include "radix-tree.h"
#include "ctree.h" #include "ctree.h"
@ -525,6 +526,58 @@ error:
return ret; return ret;
} }
/*
* returns 1 if the device was mounted, < 0 on error or 0 if everything
* is safe to continue. TODO, this should also scan multi-device filesystems
*/
int check_mounted(char *file)
{
struct mntent *mnt;
struct stat st_buf;
dev_t file_dev = 0;
dev_t file_rdev = 0;
ino_t file_ino = 0;
FILE *f;
int ret = 0;
if ((f = setmntent ("/proc/mounts", "r")) == NULL)
return -errno;
if (stat(file, &st_buf) < 0) {
return -errno;
} else {
if (S_ISBLK(st_buf.st_mode)) {
file_rdev = st_buf.st_rdev;
} else {
file_dev = st_buf.st_dev;
file_ino = st_buf.st_ino;
}
}
while ((mnt = getmntent (f)) != NULL) {
if (strcmp(file, mnt->mnt_fsname) == 0)
break;
if (stat(mnt->mnt_fsname, &st_buf) == 0) {
if (S_ISBLK(st_buf.st_mode)) {
if (file_rdev && (file_rdev == st_buf.st_rdev))
break;
} else if (file_dev && ((file_dev == st_buf.st_dev) &&
(file_ino == st_buf.st_ino))) {
break;
}
}
}
if (mnt) {
/* found an entry in mnt table */
ret = 1;
}
endmntent (f);
return ret;
}
struct pending_dir { struct pending_dir {
struct list_head list; struct list_head list;
char name[256]; char name[256];

View File

@ -35,4 +35,5 @@ int btrfs_scan_for_fsid(struct btrfs_fs_devices *fs_devices, u64 total_devs,
int run_ioctls); int run_ioctls);
int btrfs_register_one_device(char *fname); int btrfs_register_one_device(char *fname);
int btrfs_scan_one_dir(char *dirname, int run_ioctl); int btrfs_scan_one_dir(char *dirname, int run_ioctl);
int check_mounted(char *devicename);
#endif #endif