2 * Copyright (c) 2004 Lukas Ertl
3 * Copyright (c) 2005 Chris Jones
4 * Copyright (c) 2007 Ulf Lilleengen
7 * Portions of this software were developed for the FreeBSD Project
8 * by Chris Jones thanks to the support of Google's Summer of Code
9 * program and mentoring by Lukas Ertl.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/param.h>
36 #include <sys/linker.h>
38 #include <sys/module.h>
39 #include <sys/mutex.h>
40 #include <sys/queue.h>
41 #include <sys/utsname.h>
43 #include <geom/vinum/geom_vinum_var.h>
44 #include <geom/vinum/geom_vinum_share.h>
55 #include <readline/readline.h>
56 #include <readline/history.h>
61 static void gvinum_attach(int, char * const *);
62 static void gvinum_concat(int, char * const *);
63 static void gvinum_create(int, char * const *);
64 static void gvinum_detach(int, char * const *);
65 static void gvinum_grow(int, char * const *);
66 static void gvinum_help(void);
67 static void gvinum_list(int, char * const *);
68 static void gvinum_move(int, char * const *);
69 static void gvinum_mirror(int, char * const *);
70 static void gvinum_parityop(int, char * const * , int);
71 static void gvinum_printconfig(int, char * const *);
72 static void gvinum_raid5(int, char * const *);
73 static void gvinum_rename(int, char * const *);
74 static void gvinum_resetconfig(int, char * const *);
75 static void gvinum_rm(int, char * const *);
76 static void gvinum_saveconfig(void);
77 static void gvinum_setstate(int, char * const *);
78 static void gvinum_start(int, char * const *);
79 static void gvinum_stop(int, char * const *);
80 static void gvinum_stripe(int, char * const *);
81 static void parseline(int, char * const *);
82 static void printconfig(FILE *, const char *);
84 static char *create_drive(const char *);
85 static void create_volume(int, char * const * , const char *);
86 static char *find_name(const char *, int, int);
87 static const char *find_pattern(char *, const char *);
88 static void copy_device(struct gv_drive *, const char *);
89 #define find_drive() \
90 find_name("gvinumdrive", GV_TYPE_DRIVE, GV_MAXDRIVENAME)
93 main(int argc, char **argv)
96 char buffer[BUFSIZ], *inputline, *token[GV_MAXARGS];
98 /* Load the module if necessary. */
99 if (modfind(GVINUMMOD) < 0) {
100 if (kldload(GVINUMKLD) < 0 && modfind(GVINUMMOD) < 0)
101 err(1, GVINUMKLD ": Kernel module not available");
104 /* Arguments given on the command line. */
108 parseline(argc, argv);
110 /* Interactive mode. */
113 inputline = readline("gvinum -> ");
114 if (inputline == NULL) {
116 err(1, "can't read input");
121 } else if (*inputline) {
122 add_history(inputline);
123 strcpy(buffer, inputline);
125 line++; /* count the lines */
126 tokens = gv_tokenize(buffer, token, GV_MAXARGS);
128 parseline(tokens, token);
135 /* Attach a plex to a volume or a subdisk to a plex. */
137 gvinum_attach(int argc, char * const *argv)
139 struct gctl_req *req;
147 warnx("usage:\tattach <subdisk> <plex> [rename] "
149 "\tattach <plex> <volume> [rename]");
153 if (!strcmp(argv[3], "rename")) {
156 offset = strtol(argv[4], NULL, 0);
158 offset = strtol(argv[3], NULL, 0);
160 req = gctl_get_handle();
161 gctl_ro_param(req, "class", -1, "VINUM");
162 gctl_ro_param(req, "verb", -1, "attach");
163 gctl_ro_param(req, "child", -1, argv[1]);
164 gctl_ro_param(req, "parent", -1, argv[2]);
165 gctl_ro_param(req, "offset", sizeof(off_t), &offset);
166 gctl_ro_param(req, "rename", sizeof(int), &rename);
167 errstr = gctl_issue(req);
169 warnx("attach failed: %s", errstr);
174 gvinum_create(int argc, char * const *argv)
176 struct gctl_req *req;
182 int drives, errors, fd, flags, i, line, plexes, plex_in_volume;
183 int sd_in_plex, status, subdisks, tokens, undeffd, volumes;
185 char buf[BUFSIZ], buf1[BUFSIZ], commandline[BUFSIZ], *ed, *sdname;
186 char original[BUFSIZ], tmpfile[20], *token[GV_MAXARGS];
187 char plex[GV_MAXPLEXNAME], volume[GV_MAXVOLNAME];
191 for (i = 1; i < argc; i++) {
192 /* Force flag used to ignore already created drives. */
193 if (!strcmp(argv[i], "-f")) {
195 /* Else it must be a file. */
197 if ((tmp = fopen(argv[i], "r")) == NULL) {
198 warn("can't open '%s' for reading", argv[i]);
204 /* We didn't get a file. */
206 snprintf(tmpfile, sizeof(tmpfile), "/tmp/gvinum.XXXXXX");
208 if ((fd = mkstemp(tmpfile)) == -1) {
209 warn("temporary file not accessible");
212 if ((tmp = fdopen(fd, "w")) == NULL) {
213 warn("can't open '%s' for writing", tmpfile);
216 printconfig(tmp, "# ");
219 ed = getenv("EDITOR");
223 snprintf(commandline, sizeof(commandline), "%s %s", ed,
225 status = system(commandline);
227 warn("couldn't exec %s; status: %d", ed, status);
231 if ((tmp = fopen(tmpfile, "r")) == NULL) {
232 warn("can't open '%s' for reading", tmpfile);
237 req = gctl_get_handle();
238 gctl_ro_param(req, "class", -1, "VINUM");
239 gctl_ro_param(req, "verb", -1, "create");
240 gctl_ro_param(req, "flags", sizeof(int), &flags);
242 drives = volumes = plexes = subdisks = 0;
243 plex_in_volume = sd_in_plex = undeffd = 0;
247 while ((fgets(buf, BUFSIZ, tmp)) != NULL) {
249 /* Skip empty lines and comments. */
250 if (*buf == '\0' || *buf == '#') {
255 /* Kill off the newline. */
256 buf[strlen(buf) - 1] = '\0';
259 * Copy the original input line in case we need it for error
262 strlcpy(original, buf, sizeof(original));
264 tokens = gv_tokenize(buf, token, GV_MAXARGS);
270 /* Volume definition. */
271 if (!strcmp(token[0], "volume")) {
272 v = gv_new_volume(tokens, token);
274 warnx("line %d: invalid volume definition",
276 warnx("line %d: '%s'", line, original);
282 /* Reset plex count for this volume. */
286 * Set default volume name for following plex
289 strlcpy(volume, v->name, sizeof(volume));
291 snprintf(buf1, sizeof(buf1), "volume%d", volumes);
292 gctl_ro_param(req, buf1, sizeof(*v), v);
295 /* Plex definition. */
296 } else if (!strcmp(token[0], "plex")) {
297 p = gv_new_plex(tokens, token);
299 warnx("line %d: invalid plex definition", line);
300 warnx("line %d: '%s'", line, original);
306 /* Reset subdisk count for this plex. */
310 if (strlen(p->name) == 0) {
311 snprintf(p->name, sizeof(p->name), "%s.p%d",
312 volume, plex_in_volume++);
315 /* Default volume. */
316 if (strlen(p->volume) == 0) {
317 snprintf(p->volume, sizeof(p->volume), "%s",
322 * Set default plex name for following subdisk
325 strlcpy(plex, p->name, sizeof(plex));
327 snprintf(buf1, sizeof(buf1), "plex%d", plexes);
328 gctl_ro_param(req, buf1, sizeof(*p), p);
331 /* Subdisk definition. */
332 } else if (!strcmp(token[0], "sd")) {
333 s = gv_new_sd(tokens, token);
335 warnx("line %d: invalid subdisk "
336 "definition:", line);
337 warnx("line %d: '%s'", line, original);
344 if (strlen(s->name) == 0) {
345 if (strlen(plex) == 0) {
346 sdname = find_name("gvinumsubdisk.p",
347 GV_TYPE_SD, GV_MAXSDNAME);
348 snprintf(s->name, sizeof(s->name),
349 "%s.s%d", sdname, undeffd++);
352 snprintf(s->name, sizeof(s->name),
353 "%s.s%d",plex, sd_in_plex++);
358 if (strlen(s->plex) == 0)
359 snprintf(s->plex, sizeof(s->plex), "%s", plex);
361 snprintf(buf1, sizeof(buf1), "sd%d", subdisks);
362 gctl_ro_param(req, buf1, sizeof(*s), s);
365 /* Subdisk definition. */
366 } else if (!strcmp(token[0], "drive")) {
367 d = gv_new_drive(tokens, token);
369 warnx("line %d: invalid drive definition:",
371 warnx("line %d: '%s'", line, original);
377 snprintf(buf1, sizeof(buf1), "drive%d", drives);
378 gctl_ro_param(req, buf1, sizeof(*d), d);
381 /* Everything else is bogus. */
383 warnx("line %d: invalid definition:", line);
384 warnx("line %d: '%s'", line, original);
393 if (!errors && (volumes || plexes || subdisks || drives)) {
394 gctl_ro_param(req, "volumes", sizeof(int), &volumes);
395 gctl_ro_param(req, "plexes", sizeof(int), &plexes);
396 gctl_ro_param(req, "subdisks", sizeof(int), &subdisks);
397 gctl_ro_param(req, "drives", sizeof(int), &drives);
398 errstr = gctl_issue(req);
400 warnx("create failed: %s", errstr);
405 /* Create a concatenated volume. */
407 gvinum_concat(int argc, char * const *argv)
411 warnx("usage:\tconcat [-fv] [-n name] drives\n");
414 create_volume(argc, argv, "concat");
417 /* Create a drive quick and dirty. */
419 create_drive(const char *device)
422 struct gctl_req *req;
424 char *drivename, *dname;
425 int drives, i, flags, volumes, subdisks, plexes;
428 flags = plexes = subdisks = volumes = 0;
432 drivename = find_drive();
433 if (drivename == NULL)
436 req = gctl_get_handle();
437 gctl_ro_param(req, "class", -1, "VINUM");
438 gctl_ro_param(req, "verb", -1, "create");
439 d = gv_alloc_drive();
441 err(1, "unable to allocate for gv_drive object");
443 strlcpy(d->name, drivename, sizeof(d->name));
444 copy_device(d, device);
445 gctl_ro_param(req, "drive0", sizeof(*d), d);
446 gctl_ro_param(req, "flags", sizeof(int), &flags);
447 gctl_ro_param(req, "drives", sizeof(int), &drives);
448 gctl_ro_param(req, "volumes", sizeof(int), &volumes);
449 gctl_ro_param(req, "plexes", sizeof(int), &plexes);
450 gctl_ro_param(req, "subdisks", sizeof(int), &subdisks);
451 errstr = gctl_issue(req);
452 if (errstr != NULL) {
453 warnx("error creating drive: %s", errstr);
456 /* XXX: This is needed because we have to make sure the drives
457 * are created before we return. */
458 /* Loop until it's in the config. */
459 for (i = 0; i < 100000; i++) {
460 dname = find_name("gvinumdrive", GV_TYPE_DRIVE,
462 /* If we got a different name, quit. */
465 if (strcmp(dname, drivename))
471 usleep(100000); /* Sleep for 0.1s */
474 warnx("error creating drive");
483 * General routine for creating a volume. Mainly for use by concat, mirror,
484 * raid5 and stripe commands.
487 create_volume(int argc, char * const *argv, const char *verb)
489 struct gctl_req *req;
491 char buf[BUFSIZ], *drivename, *volname;
492 int drives, flags, i;
500 /* XXX: Should we check for argument length? */
502 req = gctl_get_handle();
503 gctl_ro_param(req, "class", -1, "VINUM");
505 for (i = 1; i < argc; i++) {
506 if (!strcmp(argv[i], "-f")) {
508 } else if (!strcmp(argv[i], "-n")) {
510 } else if (!strcmp(argv[i], "-v")) {
512 } else if (!strcmp(argv[i], "-s")) {
514 if (!strcmp(verb, "raid5"))
515 stripesize = gv_sizespec(argv[++i]);
517 /* Assume it's a drive. */
518 snprintf(buf, sizeof(buf), "drive%d", drives++);
520 /* First we create the drive. */
521 drivename = create_drive(argv[i]);
522 if (drivename == NULL)
524 /* Then we add it to the request. */
525 gctl_ro_param(req, buf, -1, drivename);
529 gctl_ro_param(req, "stripesize", sizeof(off_t), &stripesize);
531 /* Find a free volume name. */
533 volname = find_name("gvinumvolume", GV_TYPE_VOL, GV_MAXVOLNAME);
535 /* Then we send a request to actually create the volumes. */
536 gctl_ro_param(req, "verb", -1, verb);
537 gctl_ro_param(req, "flags", sizeof(int), &flags);
538 gctl_ro_param(req, "drives", sizeof(int), &drives);
539 gctl_ro_param(req, "name", -1, volname);
540 errstr = gctl_issue(req);
542 warnx("creating %s volume failed: %s", verb, errstr);
547 /* Parse a line of the config, return the word after <pattern>. */
549 find_pattern(char *line, const char *pattern)
553 ptr = strsep(&line, " ");
554 while (ptr != NULL) {
555 if (!strcmp(ptr, pattern)) {
556 /* Return the next. */
557 ptr = strsep(&line, " ");
560 ptr = strsep(&line, " ");
565 /* Find a free name for an object given a prefix. */
567 find_name(const char *prefix, int type, int namelen)
569 struct gctl_req *req;
570 char comment[1], buf[GV_CFG_LEN - 1], *sname, *ptr;
571 const char *errstr, *name;
572 int i, n, begin, len, conflict;
577 /* Find a name. Fetch out configuration first. */
578 req = gctl_get_handle();
579 gctl_ro_param(req, "class", -1, "VINUM");
580 gctl_ro_param(req, "verb", -1, "getconfig");
581 gctl_ro_param(req, "comment", -1, comment);
582 gctl_rw_param(req, "config", sizeof(buf), buf);
583 errstr = gctl_issue(req);
584 if (errstr != NULL) {
585 warnx("can't get configuration: %s", errstr);
593 sname = malloc(namelen + 1);
595 /* XXX: Max object setting? */
596 for (n = 0; n < 10000; n++) {
597 snprintf(sname, namelen, "%s%d", prefix, n);
600 /* Loop through the configuration line by line. */
601 for (i = 0; i < len; i++) {
602 if (buf[i] == '\n' || buf[i] == '\0') {
604 strlcpy(line, ptr, (i - begin) + 1);
608 name = find_pattern(line, "drive");
611 name = find_pattern(line, "volume");
615 name = find_pattern(line, "name");
618 printf("Invalid type given\n");
623 if (!strcmp(sname, name)) {
625 /* XXX: Could quit the loop earlier. */
637 copy_device(struct gv_drive *d, const char *device)
640 if (strncmp(device, "/dev/", 5) == 0)
641 strlcpy(d->device, (device + 5), sizeof(d->device));
643 strlcpy(d->device, device, sizeof(d->device));
646 /* Detach a plex or subdisk from its parent. */
648 gvinum_detach(int argc, char * const *argv)
651 struct gctl_req *req;
657 while ((i = getopt(argc, argv, "f")) != -1) {
663 warn("invalid flag: %c", i);
670 warnx("usage: detach [-f] <subdisk> | <plex>");
674 req = gctl_get_handle();
675 gctl_ro_param(req, "class", -1, "VINUM");
676 gctl_ro_param(req, "verb", -1, "detach");
677 gctl_ro_param(req, "object", -1, argv[0]);
678 gctl_ro_param(req, "flags", sizeof(int), &flags);
680 errstr = gctl_issue(req);
682 warnx("detach failed: %s", errstr);
691 "checkparity [-f] plex\n"
692 " Check the parity blocks of a RAID-5 plex.\n"
693 "create [-f] description-file\n"
694 " Create as per description-file or open editor.\n"
695 "attach plex volume [rename]\n"
696 "attach subdisk plex [offset] [rename]\n"
697 " Attach a plex to a volume, or a subdisk to a plex\n"
698 "concat [-fv] [-n name] drives\n"
699 " Create a concatenated volume from the specified drives.\n"
700 "detach [-f] [plex | subdisk]\n"
701 " Detach a plex or a subdisk from the volume or plex to\n"
702 " which it is attached.\n"
704 " Grow plex by creating a properly sized subdisk on drive\n"
705 "l | list [-r] [-v] [-V] [volume | plex | subdisk]\n"
706 " List information about specified objects.\n"
707 "ld [-r] [-v] [-V] [volume]\n"
708 " List information about drives.\n"
709 "ls [-r] [-v] [-V] [subdisk]\n"
710 " List information about subdisks.\n"
711 "lp [-r] [-v] [-V] [plex]\n"
712 " List information about plexes.\n"
713 "lv [-r] [-v] [-V] [volume]\n"
714 " List information about volumes.\n"
715 "mirror [-fsv] [-n name] drives\n"
716 " Create a mirrored volume from the specified drives.\n"
717 "move | mv -f drive object ...\n"
718 " Move the object(s) to the specified drive.\n"
719 "quit Exit the vinum program when running in interactive mode."
721 " mally this would be done by entering the EOF character.\n"
722 "raid5 [-fv] [-s stripesize] [-n name] drives\n"
723 " Create a RAID-5 volume from the specified drives.\n"
724 "rename [-r] [drive | subdisk | plex | volume] newname\n"
725 " Change the name of the specified object.\n"
726 "rebuildparity plex [-f]\n"
727 " Rebuild the parity blocks of a RAID-5 plex.\n"
729 " Reset the complete gvinum configuration\n"
730 "rm [-r] [-f] volume | plex | subdisk | drive\n"
731 " Remove an object.\n"
733 " Save vinum configuration to disk after configuration"
735 "setstate [-f] state [volume | plex | subdisk | drive]\n"
736 " Set state without influencing other objects, for"
739 "start [-S size] volume | plex | subdisk\n"
740 " Allow the system to access the objects.\n"
741 "stripe [-fv] [-n name] drives\n"
742 " Create a striped volume from the specified drives.\n"
747 gvinum_setstate(int argc, char * const *argv)
749 struct gctl_req *req;
758 while ((i = getopt(argc, argv, "f")) != -1) {
765 warn("invalid flag: %c", i);
774 warnx("usage: setstate [-f] <state> <obj>");
779 * XXX: This hack is needed to avoid tripping over (now) invalid
780 * 'classic' vinum states and will go away later.
782 if (strcmp(argv[0], "up") && strcmp(argv[0], "down") &&
783 strcmp(argv[0], "stale")) {
784 warnx("invalid state '%s'", argv[0]);
788 req = gctl_get_handle();
789 gctl_ro_param(req, "class", -1, "VINUM");
790 gctl_ro_param(req, "verb", -1, "setstate");
791 gctl_ro_param(req, "state", -1, argv[0]);
792 gctl_ro_param(req, "object", -1, argv[1]);
793 gctl_ro_param(req, "flags", sizeof(int), &flags);
795 errstr = gctl_issue(req);
802 gvinum_list(int argc, char * const *argv)
804 struct gctl_req *req;
807 char buf[20], *cmd, config[GV_CFG_LEN + 1];
816 while ((j = getopt(argc, argv, "rsvV")) != -1) {
841 req = gctl_get_handle();
842 gctl_ro_param(req, "class", -1, "VINUM");
843 gctl_ro_param(req, "verb", -1, "list");
844 gctl_ro_param(req, "cmd", -1, cmd);
845 gctl_ro_param(req, "argc", sizeof(int), &argc);
846 gctl_ro_param(req, "flags", sizeof(int), &flags);
847 gctl_rw_param(req, "config", sizeof(config), config);
849 for (i = 0; i < argc; i++) {
850 snprintf(buf, sizeof(buf), "argv%d", i);
851 gctl_ro_param(req, buf, -1, argv[i]);
854 errstr = gctl_issue(req);
855 if (errstr != NULL) {
856 warnx("can't get configuration: %s", errstr);
861 printf("%s", config);
865 /* Create a mirrored volume. */
867 gvinum_mirror(int argc, char * const *argv)
871 warnx("usage\tmirror [-fsv] [-n name] drives\n");
874 create_volume(argc, argv, "mirror");
877 /* Note that move is currently of form '[-r] target object [...]' */
879 gvinum_move(int argc, char * const *argv)
881 struct gctl_req *req;
890 while ((j = getopt(argc, argv, "f")) != -1) {
906 warnx("no destination or object(s) to move specified");
909 warnx("no object(s) to move specified");
915 req = gctl_get_handle();
916 gctl_ro_param(req, "class", -1, "VINUM");
917 gctl_ro_param(req, "verb", -1, "move");
918 gctl_ro_param(req, "argc", sizeof(int), &argc);
919 gctl_ro_param(req, "flags", sizeof(int), &flags);
920 gctl_ro_param(req, "destination", -1, argv[0]);
921 for (i = 1; i < argc; i++) {
922 snprintf(buf, sizeof(buf), "argv%d", i);
923 gctl_ro_param(req, buf, -1, argv[i]);
925 errstr = gctl_issue(req);
927 warnx("can't move object(s): %s", errstr);
932 gvinum_printconfig(int argc, char * const *argv)
935 printconfig(stdout, "");
939 gvinum_parityop(int argc, char * const *argv, int rebuild)
941 struct gctl_req *req;
947 op = "rebuildparity";
955 while ((i = getopt(argc, argv, "fv")) != -1) {
964 warnx("invalid flag '%c'", i);
972 warn("usage: %s [-f] [-v] <plex>", op);
976 req = gctl_get_handle();
977 gctl_ro_param(req, "class", -1, "VINUM");
978 gctl_ro_param(req, "verb", -1, op);
979 gctl_ro_param(req, "rebuild", sizeof(int), &rebuild);
980 gctl_ro_param(req, "flags", sizeof(int), &flags);
981 gctl_ro_param(req, "plex", -1, argv[0]);
983 errstr = gctl_issue(req);
985 warnx("%s\n", errstr);
989 /* Create a RAID-5 volume. */
991 gvinum_raid5(int argc, char * const *argv)
995 warnx("usage:\traid5 [-fv] [-s stripesize] [-n name] drives\n");
998 create_volume(argc, argv, "raid5");
1002 gvinum_rename(int argc, char * const *argv)
1004 struct gctl_req *req;
1013 while ((j = getopt(argc, argv, "r")) != -1) {
1028 warnx("no object to rename specified");
1031 warnx("no new name specified");
1036 warnx("more than one new name specified");
1040 req = gctl_get_handle();
1041 gctl_ro_param(req, "class", -1, "VINUM");
1042 gctl_ro_param(req, "verb", -1, "rename");
1043 gctl_ro_param(req, "flags", sizeof(int), &flags);
1044 gctl_ro_param(req, "object", -1, argv[0]);
1045 gctl_ro_param(req, "newname", -1, argv[1]);
1046 errstr = gctl_issue(req);
1048 warnx("can't rename object: %s", errstr);
1053 gvinum_rm(int argc, char * const *argv)
1055 struct gctl_req *req;
1063 while ((j = getopt(argc, argv, "rf")) != -1) {
1078 req = gctl_get_handle();
1079 gctl_ro_param(req, "class", -1, "VINUM");
1080 gctl_ro_param(req, "verb", -1, "remove");
1081 gctl_ro_param(req, "argc", sizeof(int), &argc);
1082 gctl_ro_param(req, "flags", sizeof(int), &flags);
1084 for (i = 0; i < argc; i++) {
1085 snprintf(buf, sizeof(buf), "argv%d", i);
1086 gctl_ro_param(req, buf, -1, argv[i]);
1089 errstr = gctl_issue(req);
1090 if (errstr != NULL) {
1091 warnx("can't remove: %s", errstr);
1099 gvinum_resetconfig(int argc, char * const *argv)
1101 struct gctl_req *req;
1107 while ((i = getopt(argc, argv, "f")) != -1) {
1113 warn("invalid flag: %c", i);
1117 if ((flags & GV_FLAG_F) == 0) {
1118 if (!isatty(STDIN_FILENO)) {
1119 warn("Please enter this command from a tty device\n");
1122 printf(" WARNING! This command will completely wipe out"
1123 " your gvinum configuration.\n"
1124 " All data will be lost. If you really want to do this,"
1125 " enter the text\n\n"
1128 fgets(reply, sizeof(reply), stdin);
1129 if (strcmp(reply, "NO FUTURE\n")) {
1130 printf("\n No change\n");
1134 req = gctl_get_handle();
1135 gctl_ro_param(req, "class", -1, "VINUM");
1136 gctl_ro_param(req, "verb", -1, "resetconfig");
1137 errstr = gctl_issue(req);
1138 if (errstr != NULL) {
1139 warnx("can't reset config: %s", errstr);
1144 printf("gvinum configuration obliterated\n");
1148 gvinum_saveconfig(void)
1150 struct gctl_req *req;
1153 req = gctl_get_handle();
1154 gctl_ro_param(req, "class", -1, "VINUM");
1155 gctl_ro_param(req, "verb", -1, "saveconfig");
1156 errstr = gctl_issue(req);
1158 warnx("can't save configuration: %s", errstr);
1163 gvinum_start(int argc, char * const *argv)
1165 struct gctl_req *req;
1170 /* 'start' with no arguments is a no-op. */
1178 while ((j = getopt(argc, argv, "S")) != -1) {
1181 initsize = atoi(optarg);
1193 req = gctl_get_handle();
1194 gctl_ro_param(req, "class", -1, "VINUM");
1195 gctl_ro_param(req, "verb", -1, "start");
1196 gctl_ro_param(req, "argc", sizeof(int), &argc);
1197 gctl_ro_param(req, "initsize", sizeof(int), &initsize);
1199 for (i = 0; i < argc; i++) {
1200 snprintf(buf, sizeof(buf), "argv%d", i);
1201 gctl_ro_param(req, buf, -1, argv[i]);
1204 errstr = gctl_issue(req);
1205 if (errstr != NULL) {
1206 warnx("can't start: %s", errstr);
1215 gvinum_stop(int argc, char * const *argv)
1219 fileid = kldfind(GVINUMKLD);
1221 if (modfind(GVINUMMOD) < 0)
1222 warn("cannot find " GVINUMKLD);
1227 * This little hack prevents that we end up in an infinite loop in
1228 * g_unload_class(). gv_unload() will return EAGAIN so that the GEOM
1229 * event thread will be free for the g_wither_geom() call from
1230 * gv_unload(). It's silly, but it works.
1232 printf("unloading " GVINUMKLD " kernel module... ");
1234 if ((err = kldunload(fileid)) != 0 && (errno == EAGAIN)) {
1236 err = kldunload(fileid);
1239 printf(" failed!\n");
1240 warn("cannot unload " GVINUMKLD);
1248 /* Create a striped volume. */
1250 gvinum_stripe(int argc, char * const *argv)
1254 warnx("usage:\tstripe [-fv] [-n name] drives\n");
1257 create_volume(argc, argv, "stripe");
1260 /* Grow a subdisk by adding disk backed by provider. */
1262 gvinum_grow(int argc, char * const *argv)
1264 struct gctl_req *req;
1265 char *drive, *sdname;
1266 char sdprefix[GV_MAXSDNAME];
1270 int drives, volumes, plexes, subdisks, flags;
1273 drives = volumes = plexes = subdisks = 0;
1275 warnx("usage:\tgrow plex drive\n");
1281 warn("unable to create subdisk");
1284 d = gv_alloc_drive();
1286 warn("unable to create drive");
1290 /* Lookup device and set an appropriate drive name. */
1291 drive = find_drive();
1292 if (drive == NULL) {
1293 warn("unable to find an appropriate drive name");
1298 strlcpy(d->name, drive, sizeof(d->name));
1299 copy_device(d, argv[2]);
1303 /* We try to use the plex name as basis for the subdisk name. */
1304 snprintf(sdprefix, sizeof(sdprefix), "%s.s", argv[1]);
1305 sdname = find_name(sdprefix, GV_TYPE_SD, GV_MAXSDNAME);
1306 if (sdname == NULL) {
1307 warn("unable to find an appropriate subdisk name");
1313 strlcpy(s->name, sdname, sizeof(s->name));
1315 strlcpy(s->plex, argv[1], sizeof(s->plex));
1316 strlcpy(s->drive, d->name, sizeof(s->drive));
1319 req = gctl_get_handle();
1320 gctl_ro_param(req, "class", -1, "VINUM");
1321 gctl_ro_param(req, "verb", -1, "create");
1322 gctl_ro_param(req, "flags", sizeof(int), &flags);
1323 gctl_ro_param(req, "volumes", sizeof(int), &volumes);
1324 gctl_ro_param(req, "plexes", sizeof(int), &plexes);
1325 gctl_ro_param(req, "subdisks", sizeof(int), &subdisks);
1326 gctl_ro_param(req, "drives", sizeof(int), &drives);
1327 gctl_ro_param(req, "drive0", sizeof(*d), d);
1328 gctl_ro_param(req, "sd0", sizeof(*s), s);
1329 errstr = gctl_issue(req);
1331 if (errstr != NULL) {
1332 warnx("unable to grow plex: %s", errstr);
1341 parseline(int argc, char * const *argv)
1347 if (!strcmp(argv[0], "create"))
1348 gvinum_create(argc, argv);
1349 else if (!strcmp(argv[0], "exit") || !strcmp(argv[0], "quit"))
1351 else if (!strcmp(argv[0], "attach"))
1352 gvinum_attach(argc, argv);
1353 else if (!strcmp(argv[0], "detach"))
1354 gvinum_detach(argc, argv);
1355 else if (!strcmp(argv[0], "concat"))
1356 gvinum_concat(argc, argv);
1357 else if (!strcmp(argv[0], "grow"))
1358 gvinum_grow(argc, argv);
1359 else if (!strcmp(argv[0], "help"))
1361 else if (!strcmp(argv[0], "list") || !strcmp(argv[0], "l"))
1362 gvinum_list(argc, argv);
1363 else if (!strcmp(argv[0], "ld"))
1364 gvinum_list(argc, argv);
1365 else if (!strcmp(argv[0], "lp"))
1366 gvinum_list(argc, argv);
1367 else if (!strcmp(argv[0], "ls"))
1368 gvinum_list(argc, argv);
1369 else if (!strcmp(argv[0], "lv"))
1370 gvinum_list(argc, argv);
1371 else if (!strcmp(argv[0], "mirror"))
1372 gvinum_mirror(argc, argv);
1373 else if (!strcmp(argv[0], "move"))
1374 gvinum_move(argc, argv);
1375 else if (!strcmp(argv[0], "mv"))
1376 gvinum_move(argc, argv);
1377 else if (!strcmp(argv[0], "printconfig"))
1378 gvinum_printconfig(argc, argv);
1379 else if (!strcmp(argv[0], "raid5"))
1380 gvinum_raid5(argc, argv);
1381 else if (!strcmp(argv[0], "rename"))
1382 gvinum_rename(argc, argv);
1383 else if (!strcmp(argv[0], "resetconfig"))
1384 gvinum_resetconfig(argc, argv);
1385 else if (!strcmp(argv[0], "rm"))
1386 gvinum_rm(argc, argv);
1387 else if (!strcmp(argv[0], "saveconfig"))
1388 gvinum_saveconfig();
1389 else if (!strcmp(argv[0], "setstate"))
1390 gvinum_setstate(argc, argv);
1391 else if (!strcmp(argv[0], "start"))
1392 gvinum_start(argc, argv);
1393 else if (!strcmp(argv[0], "stop"))
1394 gvinum_stop(argc, argv);
1395 else if (!strcmp(argv[0], "stripe"))
1396 gvinum_stripe(argc, argv);
1397 else if (!strcmp(argv[0], "checkparity"))
1398 gvinum_parityop(argc, argv, 0);
1399 else if (!strcmp(argv[0], "rebuildparity"))
1400 gvinum_parityop(argc, argv, 1);
1402 printf("unknown command '%s'\n", argv[0]);
1406 * The guts of printconfig. This is called from gvinum_printconfig and from
1407 * gvinum_create when called without an argument, in order to give the user
1408 * something to edit.
1411 printconfig(FILE *of, const char *comment)
1413 struct gctl_req *req;
1414 struct utsname uname_s;
1417 char buf[GV_CFG_LEN + 1];
1422 req = gctl_get_handle();
1423 gctl_ro_param(req, "class", -1, "VINUM");
1424 gctl_ro_param(req, "verb", -1, "getconfig");
1425 gctl_ro_param(req, "comment", -1, comment);
1426 gctl_rw_param(req, "config", sizeof(buf), buf);
1427 errstr = gctl_issue(req);
1428 if (errstr != NULL) {
1429 warnx("can't get configuration: %s", errstr);
1434 fprintf(of, "# Vinum configuration of %s, saved at %s",
1438 if (*comment != '\0')
1439 fprintf(of, "# Current configuration:\n");
1441 fprintf(of, "%s", buf);