summaryrefslogtreecommitdiff
path: root/cmds-device.c
diff options
context:
space:
mode:
authorAnand Jain <anand.jain@oracle.com>2013-09-28 01:30:05 +0800
committerChris Mason <chris.mason@fusionio.com>2013-10-16 08:23:11 -0400
commit1fdf85a3a30b139c02865bcf599fe5c461857f70 (patch)
treea79427040cb3f2fa958e1624fb9b342dee8aa671 /cmds-device.c
parentc31c50f3309110162f345b6b07389ff6e7e929d4 (diff)
downloadbtrfs-progs-1fdf85a3a30b139c02865bcf599fe5c461857f70.tar.gz
btrfs-progs: device add should check existing FS before adding
as of now, when 'btrfs device add' adds a device it doesn't check if the given device contains an existing FS. This patch will change that to check the same. which when true add will fail, and ask user to use -f option to overwrite. further, since now we have test_dev_for_mkfs() function to check if a disk can be used, so this patch will also use this function to test the given device before adding. Signed-off-by: Anand Jain <anand.jain@oracle.com> Signed-off-by: David Sterba <dsterba@suse.cz> Signed-off-by: Chris Mason <chris.mason@fusionio.com>
Diffstat (limited to 'cmds-device.c')
-rw-r--r--cmds-device.c36
1 files changed, 11 insertions, 25 deletions
diff --git a/cmds-device.c b/cmds-device.c
index 12c802e..7cfc347 100644
--- a/cmds-device.c
+++ b/cmds-device.c
@@ -40,6 +40,7 @@ static const char * const cmd_add_dev_usage[] = {
"btrfs device add [options] <device> [<device>...] <path>",
"Add a device to a filesystem",
"-K|--nodiscard do not perform whole device TRIM",
+ "-f|--force force overwrite existing filesystem on the disk",
NULL
};
@@ -49,14 +50,17 @@ static int cmd_add_dev(int argc, char **argv)
int i, fdmnt, ret=0, e;
DIR *dirstream = NULL;
int discard = 1;
+ int force = 0;
+ char estr[100];
while (1) {
int long_index;
static struct option long_options[] = {
{ "nodiscard", optional_argument, NULL, 'K'},
+ { "force", no_argument, NULL, 'f'},
{ 0, 0, 0, 0 }
};
- int c = getopt_long(argc, argv, "K", long_options,
+ int c = getopt_long(argc, argv, "Kf", long_options,
&long_index);
if (c < 0)
break;
@@ -64,6 +68,9 @@ static int cmd_add_dev(int argc, char **argv)
case 'K':
discard = 0;
break;
+ case 'f':
+ force = 1;
+ break;
default:
usage(cmd_add_dev_usage);
}
@@ -86,19 +93,11 @@ static int cmd_add_dev(int argc, char **argv)
struct btrfs_ioctl_vol_args ioctl_args;
int devfd, res;
u64 dev_block_count = 0;
- struct stat st;
int mixed = 0;
- res = check_mounted(argv[i]);
- if (res < 0) {
- fprintf(stderr, "error checking %s mount status\n",
- argv[i]);
- ret++;
- continue;
- }
- if (res == 1) {
- fprintf(stderr, "%s is mounted\n", argv[i]);
- ret++;
+ res = test_dev_for_mkfs(argv[i], force, estr);
+ if (res) {
+ fprintf(stderr, "%s", estr);
continue;
}
@@ -108,19 +107,6 @@ static int cmd_add_dev(int argc, char **argv)
ret++;
continue;
}
- res = fstat(devfd, &st);
- if (res) {
- fprintf(stderr, "ERROR: Unable to stat '%s'\n", argv[i]);
- close(devfd);
- ret++;
- continue;
- }
- if (!S_ISBLK(st.st_mode)) {
- fprintf(stderr, "ERROR: '%s' is not a block device\n", argv[i]);
- close(devfd);
- ret++;
- continue;
- }
res = btrfs_prepare_device(devfd, argv[i], 1, &dev_block_count,
0, &mixed, discard);