]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - cmd_device.c
Implement basic fuse mount tests.
[bcachefs-tools-debian] / cmd_device.c
index fa63e48d1b10adde910c2fc3e5ebc8b0f4ea7db4..87ede0f625578fe2147e8cdd60db9717759c75ef 100644 (file)
@@ -95,12 +95,20 @@ int cmd_device_add(int argc, char *argv[])
        dev_opts.path = dev_path;
        dev_opts.fd = open_for_format(dev_opts.path, force);
 
-       format_opts.block_size  =
-               read_file_u64(fs.sysfs_fd, "block_size") >> 9;
-       format_opts.btree_node_size =
-               read_file_u64(fs.sysfs_fd, "btree_node_size") >> 9;
+       struct bch_opt_strs fs_opt_strs;
+       memset(&fs_opt_strs, 0, sizeof(fs_opt_strs));
 
-       struct bch_sb *sb = bch2_format(format_opts, &dev_opts, 1);
+       struct bch_opts fs_opts = bch2_parse_opts(fs_opt_strs);
+
+       opt_set(fs_opts, block_size,
+               read_file_u64(fs.sysfs_fd, "block_size") >> 9);
+       opt_set(fs_opts, btree_node_size,
+               read_file_u64(fs.sysfs_fd, "btree_node_size") >> 9);
+
+       struct bch_sb *sb = bch2_format(fs_opt_strs,
+                                       fs_opts,
+                                       format_opts,
+                                       &dev_opts, 1);
        free(sb);
        fsync(dev_opts.fd);
        close(dev_opts.fd);
@@ -112,9 +120,12 @@ int cmd_device_add(int argc, char *argv[])
 static void device_remove_usage(void)
 {
        puts("bcachefs device_remove - remove a device from a filesystem\n"
-            "Usage: bcachefs device remove device\n"
+            "Usage:\n"
+            "  bcachefs device remove device\n"
+            "  bcachefs device remove --by-id path devid\n"
             "\n"
             "Options:\n"
+            "  -i, --by-id                 Remove device by device id\n"
             "  -f, --force                 Force removal, even if some data\n"
             "                              couldn't be migrated\n"
             "  -F, --force-metadata        Force removal, even if some metadata\n"
@@ -127,15 +138,22 @@ static void device_remove_usage(void)
 int cmd_device_remove(int argc, char *argv[])
 {
        static const struct option longopts[] = {
+               { "by-id",              0, NULL, 'i' },
                { "force",              0, NULL, 'f' },
                { "force-metadata",     0, NULL, 'F' },
                { "help",               0, NULL, 'h' },
                { NULL }
        };
+       struct bchfs_handle fs;
+       bool by_id = false;
        int opt, flags = BCH_FORCE_IF_DEGRADED;
+       unsigned dev_idx;
 
        while ((opt = getopt_long(argc, argv, "fh", longopts, NULL)) != -1)
                switch (opt) {
+               case 'i':
+                       by_id = true;
+                       break;
                case 'f':
                        flags |= BCH_FORCE_IF_DATA_LOST;
                        break;
@@ -147,15 +165,27 @@ int cmd_device_remove(int argc, char *argv[])
                }
        args_shift(optind);
 
-       char *dev = arg_pop();
-       if (!dev)
-               die("Please supply a device to remove");
+       if (by_id) {
+               char *path = arg_pop();
+               if (!path)
+                       die("Please supply filesystem to remove device from");
+
+               dev_idx = (intptr_t) arg_pop();
+               if (!dev_idx)
+                       die("Please supply device id");
+
+               fs = bcache_fs_open(path);
+       } else {
+               char *dev = arg_pop();
+               if (!dev)
+                       die("Please supply a device to remove");
+
+               fs = bchu_fs_open_by_dev(dev, &dev_idx);
+       }
 
        if (argc)
                die("too many arguments");
 
-       unsigned dev_idx;
-       struct bchfs_handle fs = bchu_fs_open_by_dev(dev, &dev_idx);
        bchu_disk_remove(fs, dev_idx, flags);
        return 0;
 }
@@ -377,14 +407,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);