2 * Authors: Kent Overstreet <kmo@daterainc.com>
3 * Gabriel de Perthuis <g2p.code@gmail.com>
4 * Jacob Malevich <jam@datera.io>
9 #include <nih/option.h>
10 #include <nih/command.h>
12 #include <nih/logging.h>
25 #include <sys/ioctl.h>
26 #include <sys/types.h>
28 #include <uuid/uuid.h>
32 #include "bcacheadm-format.h"
33 #include "bcacheadm-assemble.h"
34 #include "bcacheadm-run.h"
35 #include "bcacheadm-query.h"
37 #define PACKAGE_NAME "bcacheadm"
38 #define PACKAGE_VERSION "1.0"
39 #define PACKAGE_BUGREPORT "linux-bcache@vger.kernel.org"
42 static bool modify_list_attrs = false;
43 static const char *modify_set_uuid = NULL;
44 static const char *modify_dev_uuid = NULL;
46 static NihOption bcache_modify_options[] = {
47 {'l', "list", N_("list attributes"), NULL, NULL, &modify_list_attrs, NULL},
48 {'u', "set", N_("cacheset uuid"), NULL, "UUID", &modify_set_uuid, NULL},
49 {'d', "dev", N_("device uuid"), NULL, "UUID", &modify_dev_uuid, NULL},
53 int bcache_modify(NihCommand *command, char *const *args)
61 if (modify_list_attrs) {
66 if (!modify_set_uuid) {
67 printf("Must provide a cacheset uuid\n");
71 snprintf(path, MAX_PATH, "%s/%s", cset_dir, modify_set_uuid);
74 printf("Must provide the name of an attribute to modify\n");
78 enum sysfs_attr type = sysfs_attr_type(attr);
82 else if(type == INTERNAL_ATTR)
83 strcat(path, "/internal");
84 else if(type == CACHE_ATTR) {
86 /* searches all cache# for a matching uuid,
87 * path gets modified to the correct cache path */
88 char subdir[10] = "/cache";
89 err = find_matching_uuid(path, subdir,
92 printf("Failed to find "
93 "matching dev %s\n", err);
99 printf("Must provide a device uuid\n");
102 /* SET_ATTRs are just in the current dir */
109 printf("Must provide a value to change the attribute to\n");
113 fd = open(path, O_WRONLY);
115 printf("Unable to open modify attr with path %s\n", path);
119 write(fd, val, strlen(val));
128 #define CMD(_command, _usage, _synopsis, _help) \
130 .command = #_command, \
132 .synopsis = _synopsis, \
135 .options = opts_##_command, \
136 .action = cmd_##_command, \
139 static NihCommand commands[] = {
140 CMD(format, N_("<list of devices>"),
141 "Create a new bcache volume from one or more devices",
142 N_("format drive[s] for bcache")),
144 CMD(assemble, N_("<devices>"),
145 "Assembles one or more devices into a bcache volume",
146 N_("Registers a list of devices")),
147 CMD(incremental, N_("<device"),
148 "Incremental assemble bcache volumes",
149 N_("Incrementally registers a single device")),
151 CMD(run, N_("<volume>"),
152 "Start a partially assembled volume",
153 N_("Registers a list of devices")),
154 CMD(stop, N_("<volume>"),
155 "Stops a running bcache volume",
156 N_("Unregisters a list of devices")),
157 CMD(add, N_("<volume> <devices>"),
158 "Adds a list of devices to a volume",
159 N_("Adds a list of devices to a volume")),
160 CMD(readd, N_("<volume> <devices>"),
161 "Adds previously used members of a volume",
162 N_("Adds a list of devices to a volume")),
163 CMD(remove, N_("<volume> <devices>"),
164 "Removes a device from its volume",
165 N_("Removes a device from its volume")),
166 CMD(fail, N_("<volume> <devices>"),
167 "Sets a device to the FAILED state",
168 N_("Sets a device to the FAILED state")),
171 CMD(modify, N_("<options>"),
172 "Modifies attributes related to the volume",
173 N_("Modifies attributes related to the volume")),
175 CMD(list, N_("list-cachesets"),
176 "Lists cachesets in /sys/fs/bcache",
177 N_("Lists cachesets in /sys/fs/bcache")),
178 CMD(query, N_("query <list of devices>"),
179 "Gives info about the superblock of a list of devices",
180 N_("show superblock on each of the listed drive")),
181 CMD(status, N_("status <list of devices>"),
182 "Finds the status of the most up to date superblock",
183 N_("Finds the status of the most up to date superblock")),
187 static NihOption options[] = {
191 int main(int argc, char *argv[])
193 nih_main_init(argv[0]);
194 nih_option_set_synopsis(_("Manage bcache devices"));
195 nih_option_set_help( _("Helps you manage bcache devices"));
197 int ret = nih_command_parser(NULL, argc, argv, options, commands);