12 #include <nih/command.h>
13 #include <nih/option.h>
15 #include <uuid/uuid.h>
18 #include "bcacheadm-run.h"
20 static bool force_data = false;
21 static bool force_metadata = false;
23 NihOption opts_run[] = {
27 int cmd_run(NihCommand *command, char *const *args)
32 NihOption opts_stop[] = {
36 int cmd_stop(NihCommand *command, char *const *args)
38 int bcachefd = open("/dev/bcache_extent0", O_RDWR);
40 die("Can't open bcache device");
42 int ret = ioctl(bcachefd, BCH_IOCTL_STOP);
44 die("BCH_IOCTL_STOP error: %s", strerror(errno));
50 NihOption opts_add[] = {
54 int cmd_add(NihCommand *command, char *const *args)
56 if (nr_args(args) != 1)
57 die("Please supply exactly one device");
61 bcachefd = open("/dev/bcache_extent0", O_RDWR);
63 die("Can't open bcache device: %s", strerror(errno));
65 struct bch_ioctl_disk_add ia = {
66 .dev = (__u64) args[0],
69 ret = ioctl(bcachefd, BCH_IOCTL_DISK_ADD, &ia);
71 die("BCH_IOCTL_DISK_ADD error: %s", strerror(ret));
77 NihOption opts_readd[] = {
81 int cmd_readd(NihCommand *command, char *const *args)
83 if (nr_args(args) != 1)
84 die("Please supply exactly one device");
89 NihOption opts_remove[] = {
91 'f', "force", N_("force if data present"),
92 NULL, NULL, &force_data, NULL
95 '\0', "force-metadata", N_("force if metadata present"),
96 NULL, NULL, &force_metadata, NULL},
100 int cmd_remove(NihCommand *command, char *const *args)
102 if (nr_args(args) != 1)
103 die("Please supply exactly one device");
105 int bcachefd = open("/dev/bcache_extent0", O_RDWR);
107 die("Can't open bcache device");
109 struct bch_ioctl_disk_remove ir = {
110 .dev = (__u64) args[0],
114 ir.flags |= BCH_FORCE_IF_DATA_MISSING;
116 ir.flags |= BCH_FORCE_IF_METADATA_MISSING;
118 int ret = ioctl(bcachefd, BCH_IOCTL_DISK_REMOVE, &ir);
120 die("BCH_IOCTL_DISK_REMOVE error: %s\n", strerror(errno));
126 static const char *dev_failed_uuid = NULL;
128 NihOption opts_fail[] = {
129 {'d', "dev", N_("dev UUID"), NULL, "UUID", &dev_failed_uuid, NULL},
133 int cmd_fail(NihCommand *command, char *const *args)
135 if (nr_args(args) != 1)
136 die("Please supply exactly one device");
138 int bcachefd = open("/dev/bcache_extent0", O_RDWR);
140 die("Can't open bcache device");
142 struct bch_ioctl_disk_fail df = {
143 .dev = (__u64) args[0],
146 int ret = ioctl(bcachefd, BCH_IOCTL_DISK_FAIL, &df);
148 die("BCH_IOCTL_DISK_FAIL error: %s\n", strerror(errno));