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>
54 #include <readline/readline.h>
55 #include <readline/history.h>
60 void gvinum_attach(int, char **);
61 void gvinum_concat(int, char **);
62 void gvinum_create(int, char **);
63 void gvinum_detach(int, char **);
64 void gvinum_grow(int, char **);
65 void gvinum_help(void);
66 void gvinum_list(int, char **);
67 void gvinum_move(int, char **);
68 void gvinum_mirror(int, char **);
69 void gvinum_parityop(int, char **, int);
70 void gvinum_printconfig(int, char **);
71 void gvinum_raid5(int, char **);
72 void gvinum_rename(int, char **);
73 void gvinum_resetconfig(void);
74 void gvinum_rm(int, char **);
75 void gvinum_saveconfig(void);
76 void gvinum_setstate(int, char **);
77 void gvinum_start(int, char **);
78 void gvinum_stop(int, char **);
79 void gvinum_stripe(int, char **);
80 void parseline(int, char **);
81 void printconfig(FILE *, char *);
83 char *create_drive(char *);
84 void create_volume(int, char **, char *);
85 char *find_name(const char *, int, int);
86 char *find_pattern(char *, char *);
87 void copy_device(struct gv_drive *, const char *);
88 #define find_drive() find_name("gvinumdrive", GV_TYPE_DRIVE, GV_MAXDRIVENAME)
91 main(int argc, char **argv)
94 char buffer[BUFSIZ], *inputline, *token[GV_MAXARGS];
96 /* Load the module if necessary. */
97 if (kldfind(GVINUMMOD) < 0 && kldload(GVINUMMOD) < 0)
98 err(1, GVINUMMOD ": Kernel module not available");
100 /* Arguments given on the command line. */
104 parseline(argc, argv);
106 /* Interactive mode. */
109 inputline = readline("gvinum -> ");
110 if (inputline == NULL) {
112 err(1, "can't read input");
117 } else if (*inputline) {
118 add_history(inputline);
119 strcpy(buffer, inputline);
121 line++; /* count the lines */
122 tokens = gv_tokenize(buffer, token, GV_MAXARGS);
124 parseline(tokens, token);
131 /* Attach a plex to a volume or a subdisk to a plex. */
133 gvinum_attach(int argc, char **argv)
135 struct gctl_req *req;
143 warnx("usage:\tattach <subdisk> <plex> [rename] "
145 "\tattach <plex> <volume> [rename]");
149 if (!strcmp(argv[3], "rename")) {
152 offset = strtol(argv[4], NULL, 0);
154 offset = strtol(argv[3], NULL, 0);
156 req = gctl_get_handle();
157 gctl_ro_param(req, "class", -1, "VINUM");
158 gctl_ro_param(req, "verb", -1, "attach");
159 gctl_ro_param(req, "child", -1, argv[1]);
160 gctl_ro_param(req, "parent", -1, argv[2]);
161 gctl_ro_param(req, "offset", sizeof(off_t), &offset);
162 gctl_ro_param(req, "rename", sizeof(int), &rename);
163 errstr = gctl_issue(req);
165 warnx("attach failed: %s", errstr);
170 gvinum_create(int argc, char **argv)
172 struct gctl_req *req;
178 int drives, errors, fd, flags, i, line, plexes, plex_in_volume;
179 int sd_in_plex, status, subdisks, tokens, undeffd, volumes;
181 char buf[BUFSIZ], buf1[BUFSIZ], commandline[BUFSIZ], *ed, *sdname;
182 char original[BUFSIZ], tmpfile[20], *token[GV_MAXARGS];
183 char plex[GV_MAXPLEXNAME], volume[GV_MAXVOLNAME];
187 for (i = 1; i < argc; i++) {
188 /* Force flag used to ignore already created drives. */
189 if (!strcmp(argv[i], "-f")) {
191 /* Else it must be a file. */
193 if ((tmp = fopen(argv[1], "r")) == NULL) {
194 warn("can't open '%s' for reading", argv[1]);
200 /* We didn't get a file. */
202 snprintf(tmpfile, sizeof(tmpfile), "/tmp/gvinum.XXXXXX");
204 if ((fd = mkstemp(tmpfile)) == -1) {
205 warn("temporary file not accessible");
208 if ((tmp = fdopen(fd, "w")) == NULL) {
209 warn("can't open '%s' for writing", tmpfile);
212 printconfig(tmp, "# ");
215 ed = getenv("EDITOR");
219 snprintf(commandline, sizeof(commandline), "%s %s", ed,
221 status = system(commandline);
223 warn("couldn't exec %s; status: %d", ed, status);
227 if ((tmp = fopen(tmpfile, "r")) == NULL) {
228 warn("can't open '%s' for reading", tmpfile);
233 req = gctl_get_handle();
234 gctl_ro_param(req, "class", -1, "VINUM");
235 gctl_ro_param(req, "verb", -1, "create");
236 gctl_ro_param(req, "flags", sizeof(int), &flags);
238 drives = volumes = plexes = subdisks = 0;
239 plex_in_volume = sd_in_plex = undeffd = 0;
243 while ((fgets(buf, BUFSIZ, tmp)) != NULL) {
245 /* Skip empty lines and comments. */
246 if (*buf == '\0' || *buf == '#') {
251 /* Kill off the newline. */
252 buf[strlen(buf) - 1] = '\0';
255 * Copy the original input line in case we need it for error
258 strlcpy(original, buf, sizeof(original));
260 tokens = gv_tokenize(buf, token, GV_MAXARGS);
266 /* Volume definition. */
267 if (!strcmp(token[0], "volume")) {
268 v = gv_new_volume(tokens, token);
270 warnx("line %d: invalid volume definition",
272 warnx("line %d: '%s'", line, original);
278 /* Reset plex count for this volume. */
282 * Set default volume name for following plex
285 strlcpy(volume, v->name, sizeof(volume));
287 snprintf(buf1, sizeof(buf1), "volume%d", volumes);
288 gctl_ro_param(req, buf1, sizeof(*v), v);
291 /* Plex definition. */
292 } else if (!strcmp(token[0], "plex")) {
293 p = gv_new_plex(tokens, token);
295 warnx("line %d: invalid plex definition", line);
296 warnx("line %d: '%s'", line, original);
302 /* Reset subdisk count for this plex. */
306 if (strlen(p->name) == 0) {
307 snprintf(p->name, sizeof(p->name), "%s.p%d",
308 volume, plex_in_volume++);
311 /* Default volume. */
312 if (strlen(p->volume) == 0) {
313 snprintf(p->volume, sizeof(p->volume), "%s",
318 * Set default plex name for following subdisk
321 strlcpy(plex, p->name, sizeof(plex));
323 snprintf(buf1, sizeof(buf1), "plex%d", plexes);
324 gctl_ro_param(req, buf1, sizeof(*p), p);
327 /* Subdisk definition. */
328 } else if (!strcmp(token[0], "sd")) {
329 s = gv_new_sd(tokens, token);
331 warnx("line %d: invalid subdisk "
332 "definition:", line);
333 warnx("line %d: '%s'", line, original);
340 if (strlen(s->name) == 0) {
341 if (strlen(plex) == 0) {
342 sdname = find_name("gvinumsubdisk.p",
343 GV_TYPE_SD, GV_MAXSDNAME);
344 snprintf(s->name, sizeof(s->name),
345 "%s.s%d", sdname, undeffd++);
348 snprintf(s->name, sizeof(s->name),
349 "%s.s%d",plex, sd_in_plex++);
354 if (strlen(s->plex) == 0)
355 snprintf(s->plex, sizeof(s->plex), "%s", plex);
357 snprintf(buf1, sizeof(buf1), "sd%d", subdisks);
358 gctl_ro_param(req, buf1, sizeof(*s), s);
361 /* Subdisk definition. */
362 } else if (!strcmp(token[0], "drive")) {
363 d = gv_new_drive(tokens, token);
365 warnx("line %d: invalid drive definition:",
367 warnx("line %d: '%s'", line, original);
373 snprintf(buf1, sizeof(buf1), "drive%d", drives);
374 gctl_ro_param(req, buf1, sizeof(*d), d);
377 /* Everything else is bogus. */
379 warnx("line %d: invalid definition:", line);
380 warnx("line %d: '%s'", line, original);
389 if (!errors && (volumes || plexes || subdisks || drives)) {
390 gctl_ro_param(req, "volumes", sizeof(int), &volumes);
391 gctl_ro_param(req, "plexes", sizeof(int), &plexes);
392 gctl_ro_param(req, "subdisks", sizeof(int), &subdisks);
393 gctl_ro_param(req, "drives", sizeof(int), &drives);
394 errstr = gctl_issue(req);
396 warnx("create failed: %s", errstr);
401 /* Create a concatenated volume. */
403 gvinum_concat(int argc, char **argv)
407 warnx("usage:\tconcat [-fv] [-n name] drives\n");
410 create_volume(argc, argv, "concat");
414 /* Create a drive quick and dirty. */
416 create_drive(char *device)
419 struct gctl_req *req;
421 char *drivename, *dname;
422 int drives, i, flags, volumes, subdisks, plexes;
424 flags = plexes = subdisks = volumes = 0;
428 drivename = find_drive();
429 if (drivename == NULL)
432 req = gctl_get_handle();
433 gctl_ro_param(req, "class", -1, "VINUM");
434 gctl_ro_param(req, "verb", -1, "create");
435 d = gv_alloc_drive();
437 err(1, "unable to allocate for gv_drive object");
439 strlcpy(d->name, drivename, sizeof(d->name));
440 copy_device(d, device);
441 gctl_ro_param(req, "drive0", sizeof(*d), d);
442 gctl_ro_param(req, "flags", sizeof(int), &flags);
443 gctl_ro_param(req, "drives", sizeof(int), &drives);
444 gctl_ro_param(req, "volumes", sizeof(int), &volumes);
445 gctl_ro_param(req, "plexes", sizeof(int), &plexes);
446 gctl_ro_param(req, "subdisks", sizeof(int), &subdisks);
447 errstr = gctl_issue(req);
448 if (errstr != NULL) {
449 warnx("error creating drive: %s", errstr);
454 /* XXX: This is needed because we have to make sure the drives
455 * are created before we return. */
456 /* Loop until it's in the config. */
457 for (i = 0; i < 100000; i++) {
458 dname = find_name("gvinumdrive", GV_TYPE_DRIVE,
460 /* If we got a different name, quit. */
463 if (strcmp(dname, drivename)) {
469 usleep(100000); /* Sleep for 0.1s */
477 * General routine for creating a volume. Mainly for use by concat, mirror,
478 * raid5 and stripe commands.
481 create_volume(int argc, char **argv, char *verb)
483 struct gctl_req *req;
485 char buf[BUFSIZ], *drivename, *volname;
486 int drives, flags, i;
494 /* XXX: Should we check for argument length? */
496 req = gctl_get_handle();
497 gctl_ro_param(req, "class", -1, "VINUM");
499 for (i = 1; i < argc; i++) {
500 if (!strcmp(argv[i], "-f")) {
502 } else if (!strcmp(argv[i], "-n")) {
504 } else if (!strcmp(argv[i], "-v")) {
506 } else if (!strcmp(argv[i], "-s")) {
508 if (!strcmp(verb, "raid5"))
509 stripesize = gv_sizespec(argv[++i]);
511 /* Assume it's a drive. */
512 snprintf(buf, sizeof(buf), "drive%d", drives++);
514 /* First we create the drive. */
515 drivename = create_drive(argv[i]);
516 if (drivename == NULL)
518 /* Then we add it to the request. */
519 gctl_ro_param(req, buf, -1, drivename);
523 gctl_ro_param(req, "stripesize", sizeof(off_t), &stripesize);
525 /* Find a free volume name. */
527 volname = find_name("gvinumvolume", GV_TYPE_VOL, GV_MAXVOLNAME);
529 /* Then we send a request to actually create the volumes. */
530 gctl_ro_param(req, "verb", -1, verb);
531 gctl_ro_param(req, "flags", sizeof(int), &flags);
532 gctl_ro_param(req, "drives", sizeof(int), &drives);
533 gctl_ro_param(req, "name", -1, volname);
534 errstr = gctl_issue(req);
536 warnx("creating %s volume failed: %s", verb, errstr);
541 /* Parse a line of the config, return the word after <pattern>. */
543 find_pattern(char *line, char *pattern)
547 ptr = strsep(&line, " ");
548 while (ptr != NULL) {
549 if (!strcmp(ptr, pattern)) {
550 /* Return the next. */
551 ptr = strsep(&line, " ");
554 ptr = strsep(&line, " ");
559 /* Find a free name for an object given a a prefix. */
561 find_name(const char *prefix, int type, int namelen)
563 struct gctl_req *req;
564 char comment[1], buf[GV_CFG_LEN - 1], *name, *sname, *ptr;
566 int i, n, begin, len, conflict;
571 /* Find a name. Fetch out configuration first. */
572 req = gctl_get_handle();
573 gctl_ro_param(req, "class", -1, "VINUM");
574 gctl_ro_param(req, "verb", -1, "getconfig");
575 gctl_ro_param(req, "comment", -1, comment);
576 gctl_rw_param(req, "config", sizeof(buf), buf);
577 errstr = gctl_issue(req);
578 if (errstr != NULL) {
579 warnx("can't get configuration: %s", errstr);
587 sname = malloc(namelen + 1);
589 /* XXX: Max object setting? */
590 for (n = 0; n < 10000; n++) {
591 snprintf(sname, namelen, "%s%d", prefix, n);
594 /* Loop through the configuration line by line. */
595 for (i = 0; i < len; i++) {
596 if (buf[i] == '\n' || buf[i] == '\0') {
598 strlcpy(line, ptr, (i - begin) + 1);
602 name = find_pattern(line, "drive");
605 name = find_pattern(line, "volume");
609 name = find_pattern(line, "name");
612 printf("Invalid type given\n");
617 if (!strcmp(sname, name)) {
619 /* XXX: Could quit the loop earlier. */
631 copy_device(struct gv_drive *d, const char *device)
633 if (strncmp(device, "/dev/", 5) == 0)
634 strlcpy(d->device, (device + 5), sizeof(d->device));
636 strlcpy(d->device, device, sizeof(d->device));
639 /* Detach a plex or subdisk from its parent. */
641 gvinum_detach(int argc, char **argv)
644 struct gctl_req *req;
650 while ((i = getopt(argc, argv, "f")) != -1) {
656 warn("invalid flag: %c", i);
663 warnx("usage: detach [-f] <subdisk> | <plex>");
667 req = gctl_get_handle();
668 gctl_ro_param(req, "class", -1, "VINUM");
669 gctl_ro_param(req, "verb", -1, "detach");
670 gctl_ro_param(req, "object", -1, argv[0]);
671 gctl_ro_param(req, "flags", sizeof(int), &flags);
673 errstr = gctl_issue(req);
675 warnx("detach failed: %s", errstr);
683 "checkparity [-f] plex\n"
684 " Check the parity blocks of a RAID-5 plex.\n"
685 "create [-f] description-file\n"
686 " Create as per description-file or open editor.\n"
687 "attach plex volume [rename]\n"
688 "attach subdisk plex [offset] [rename]\n"
689 " Attach a plex to a volume, or a subdisk to a plex\n"
690 "concat [-fv] [-n name] drives\n"
691 " Create a concatenated volume from the specified drives.\n"
692 "detach [-f] [plex | subdisk]\n"
693 " Detach a plex or a subdisk from the volume or plex to\n"
694 " which it is attached.\n"
696 " Grow plex by creating a properly sized subdisk on drive\n"
697 "l | list [-r] [-v] [-V] [volume | plex | subdisk]\n"
698 " List information about specified objects.\n"
699 "ld [-r] [-v] [-V] [volume]\n"
700 " List information about drives.\n"
701 "ls [-r] [-v] [-V] [subdisk]\n"
702 " List information about subdisks.\n"
703 "lp [-r] [-v] [-V] [plex]\n"
704 " List information about plexes.\n"
705 "lv [-r] [-v] [-V] [volume]\n"
706 " List information about volumes.\n"
707 "mirror [-fsv] [-n name] drives\n"
708 " Create a mirrored volume from the specified drives.\n"
709 "move | mv -f drive object ...\n"
710 " Move the object(s) to the specified drive.\n"
711 "quit Exit the vinum program when running in interactive mode."
713 " mally this would be done by entering the EOF character.\n"
714 "raid5 [-fv] [-s stripesize] [-n name] drives\n"
715 " Create a RAID-5 volume from the specified drives.\n"
716 "rename [-r] [drive | subdisk | plex | volume] newname\n"
717 " Change the name of the specified object.\n"
718 "rebuildparity plex [-f]\n"
719 " Rebuild the parity blocks of a RAID-5 plex.\n"
721 " Reset the complete gvinum configuration\n"
722 "rm [-r] [-f] volume | plex | subdisk | drive\n"
723 " Remove an object.\n"
725 " Save vinum configuration to disk after configuration"
727 "setstate [-f] state [volume | plex | subdisk | drive]\n"
728 " Set state without influencing other objects, for"
731 "start [-S size] volume | plex | subdisk\n"
732 " Allow the system to access the objects.\n"
733 "stripe [-fv] [-n name] drives\n"
734 " Create a striped volume from the specified drives.\n"
741 gvinum_setstate(int argc, char **argv)
743 struct gctl_req *req;
752 while ((i = getopt(argc, argv, "f")) != -1) {
759 warn("invalid flag: %c", i);
768 warnx("usage: setstate [-f] <state> <obj>");
773 * XXX: This hack is needed to avoid tripping over (now) invalid
774 * 'classic' vinum states and will go away later.
776 if (strcmp(argv[0], "up") && strcmp(argv[0], "down") &&
777 strcmp(argv[0], "stale")) {
778 warnx("invalid state '%s'", argv[0]);
782 req = gctl_get_handle();
783 gctl_ro_param(req, "class", -1, "VINUM");
784 gctl_ro_param(req, "verb", -1, "setstate");
785 gctl_ro_param(req, "state", -1, argv[0]);
786 gctl_ro_param(req, "object", -1, argv[1]);
787 gctl_ro_param(req, "flags", sizeof(int), &flags);
789 errstr = gctl_issue(req);
796 gvinum_list(int argc, char **argv)
798 struct gctl_req *req;
801 char buf[20], *cmd, config[GV_CFG_LEN + 1];
810 while ((j = getopt(argc, argv, "rsvV")) != -1) {
835 req = gctl_get_handle();
836 gctl_ro_param(req, "class", -1, "VINUM");
837 gctl_ro_param(req, "verb", -1, "list");
838 gctl_ro_param(req, "cmd", -1, cmd);
839 gctl_ro_param(req, "argc", sizeof(int), &argc);
840 gctl_ro_param(req, "flags", sizeof(int), &flags);
841 gctl_rw_param(req, "config", sizeof(config), config);
843 for (i = 0; i < argc; i++) {
844 snprintf(buf, sizeof(buf), "argv%d", i);
845 gctl_ro_param(req, buf, -1, argv[i]);
848 errstr = gctl_issue(req);
849 if (errstr != NULL) {
850 warnx("can't get configuration: %s", errstr);
855 printf("%s", config);
860 /* Create a mirrored volume. */
862 gvinum_mirror(int argc, char **argv)
866 warnx("usage\tmirror [-fsv] [-n name] drives\n");
869 create_volume(argc, argv, "mirror");
872 /* Note that move is currently of form '[-r] target object [...]' */
874 gvinum_move(int argc, char **argv)
876 struct gctl_req *req;
885 while ((j = getopt(argc, argv, "f")) != -1) {
901 warnx("no destination or object(s) to move specified");
904 warnx("no object(s) to move specified");
910 req = gctl_get_handle();
911 gctl_ro_param(req, "class", -1, "VINUM");
912 gctl_ro_param(req, "verb", -1, "move");
913 gctl_ro_param(req, "argc", sizeof(int), &argc);
914 gctl_ro_param(req, "flags", sizeof(int), &flags);
915 gctl_ro_param(req, "destination", -1, argv[0]);
916 for (i = 1; i < argc; i++) {
917 snprintf(buf, sizeof(buf), "argv%d", i);
918 gctl_ro_param(req, buf, -1, argv[i]);
920 errstr = gctl_issue(req);
922 warnx("can't move object(s): %s", errstr);
928 gvinum_printconfig(int argc, char **argv)
930 printconfig(stdout, "");
934 gvinum_parityop(int argc, char **argv, int rebuild)
936 struct gctl_req *req;
942 op = "rebuildparity";
952 while ((i = getopt(argc, argv, "fv")) != -1) {
962 warnx("invalid flag '%c'", i);
970 warn("usage: %s [-f] [-v] <plex>", op);
974 req = gctl_get_handle();
975 gctl_ro_param(req, "class", -1, "VINUM");
976 gctl_ro_param(req, "verb", -1, op);
977 gctl_ro_param(req, "rebuild", sizeof(int), &rebuild);
978 gctl_ro_param(req, "flags", sizeof(int), &flags);
979 gctl_ro_param(req, "plex", -1, argv[0]);
981 errstr = gctl_issue(req);
983 warnx("%s\n", errstr);
987 /* Create a RAID-5 volume. */
989 gvinum_raid5(int argc, char **argv)
993 warnx("usage:\traid5 [-fv] [-s stripesize] [-n name] drives\n");
996 create_volume(argc, argv, "raid5");
1001 gvinum_rename(int argc, char **argv)
1003 struct gctl_req *req;
1012 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);
1054 gvinum_rm(int argc, char **argv)
1056 struct gctl_req *req;
1065 while ((j = getopt(argc, argv, "rf")) != -1) {
1081 req = gctl_get_handle();
1082 gctl_ro_param(req, "class", -1, "VINUM");
1083 gctl_ro_param(req, "verb", -1, "remove");
1084 gctl_ro_param(req, "argc", sizeof(int), &argc);
1085 gctl_ro_param(req, "flags", sizeof(int), &flags);
1087 for (i = 0; i < argc; i++) {
1088 snprintf(buf, sizeof(buf), "argv%d", i);
1089 gctl_ro_param(req, buf, -1, argv[i]);
1092 errstr = gctl_issue(req);
1093 if (errstr != NULL) {
1094 warnx("can't remove: %s", errstr);
1102 gvinum_resetconfig(void)
1104 struct gctl_req *req;
1108 if (!isatty(STDIN_FILENO)) {
1109 warn("Please enter this command from a tty device\n");
1112 printf(" WARNING! This command will completely wipe out your gvinum"
1114 " All data will be lost. If you really want to do this,"
1115 " enter the text\n\n"
1118 fgets(reply, sizeof(reply), stdin);
1119 if (strcmp(reply, "NO FUTURE\n")) {
1120 printf("\n No change\n");
1123 req = gctl_get_handle();
1124 gctl_ro_param(req, "class", -1, "VINUM");
1125 gctl_ro_param(req, "verb", -1, "resetconfig");
1126 errstr = gctl_issue(req);
1127 if (errstr != NULL) {
1128 warnx("can't reset config: %s", errstr);
1133 printf("gvinum configuration obliterated\n");
1137 gvinum_saveconfig(void)
1139 struct gctl_req *req;
1142 req = gctl_get_handle();
1143 gctl_ro_param(req, "class", -1, "VINUM");
1144 gctl_ro_param(req, "verb", -1, "saveconfig");
1145 errstr = gctl_issue(req);
1147 warnx("can't save configuration: %s", errstr);
1152 gvinum_start(int argc, char **argv)
1154 struct gctl_req *req;
1159 /* 'start' with no arguments is a no-op. */
1167 while ((j = getopt(argc, argv, "S")) != -1) {
1170 initsize = atoi(optarg);
1183 req = gctl_get_handle();
1184 gctl_ro_param(req, "class", -1, "VINUM");
1185 gctl_ro_param(req, "verb", -1, "start");
1186 gctl_ro_param(req, "argc", sizeof(int), &argc);
1187 gctl_ro_param(req, "initsize", sizeof(int), &initsize);
1189 for (i = 0; i < argc; i++) {
1190 snprintf(buf, sizeof(buf), "argv%d", i);
1191 gctl_ro_param(req, buf, -1, argv[i]);
1194 errstr = gctl_issue(req);
1195 if (errstr != NULL) {
1196 warnx("can't start: %s", errstr);
1205 gvinum_stop(int argc, char **argv)
1209 fileid = kldfind(GVINUMMOD);
1211 warn("cannot find " GVINUMMOD);
1216 * This little hack prevents that we end up in an infinite loop in
1217 * g_unload_class(). gv_unload() will return EAGAIN so that the GEOM
1218 * event thread will be free for the g_wither_geom() call from
1219 * gv_unload(). It's silly, but it works.
1221 printf("unloading " GVINUMMOD " kernel module... ");
1223 if ((err = kldunload(fileid)) != 0 && (errno == EAGAIN)) {
1225 err = kldunload(fileid);
1228 printf(" failed!\n");
1229 warn("cannot unload " GVINUMMOD);
1237 /* Create a striped volume. */
1239 gvinum_stripe(int argc, char **argv)
1243 warnx("usage:\tstripe [-fv] [-n name] drives\n");
1246 create_volume(argc, argv, "stripe");
1249 /* Grow a subdisk by adding disk backed by provider. */
1251 gvinum_grow(int argc, char **argv)
1253 struct gctl_req *req;
1254 char *drive, *sdname;
1255 char sdprefix[GV_MAXSDNAME];
1259 int drives, volumes, plexes, subdisks, flags;
1261 drives = volumes = plexes = subdisks = 0;
1263 warnx("usage:\tgrow plex drive\n");
1269 warn("unable to create subdisk");
1272 d = gv_alloc_drive();
1274 warn("unable to create drive");
1278 /* Lookup device and set an appropriate drive name. */
1279 drive = find_drive();
1280 if (drive == NULL) {
1281 warn("unable to find an appropriate drive name");
1286 strlcpy(d->name, drive, sizeof(d->name));
1287 copy_device(d, argv[2]);
1291 /* We try to use the plex name as basis for the subdisk name. */
1292 snprintf(sdprefix, sizeof(sdprefix), "%s.s", argv[1]);
1293 sdname = find_name(sdprefix, GV_TYPE_SD, GV_MAXSDNAME);
1294 if (sdname == NULL) {
1295 warn("unable to find an appropriate subdisk name");
1301 strlcpy(s->name, sdname, sizeof(s->name));
1303 strlcpy(s->plex, argv[1], sizeof(s->plex));
1304 strlcpy(s->drive, d->name, sizeof(s->drive));
1307 req = gctl_get_handle();
1308 gctl_ro_param(req, "class", -1, "VINUM");
1309 gctl_ro_param(req, "verb", -1, "create");
1310 gctl_ro_param(req, "flags", sizeof(int), &flags);
1311 gctl_ro_param(req, "volumes", sizeof(int), &volumes);
1312 gctl_ro_param(req, "plexes", sizeof(int), &plexes);
1313 gctl_ro_param(req, "subdisks", sizeof(int), &subdisks);
1314 gctl_ro_param(req, "drives", sizeof(int), &drives);
1315 gctl_ro_param(req, "drive0", sizeof(*d), d);
1316 gctl_ro_param(req, "sd0", sizeof(*s), s);
1317 errstr = gctl_issue(req);
1319 if (errstr != NULL) {
1320 warnx("unable to grow plex: %s", errstr);
1329 parseline(int argc, char **argv)
1334 if (!strcmp(argv[0], "create"))
1335 gvinum_create(argc, argv);
1336 else if (!strcmp(argv[0], "exit") || !strcmp(argv[0], "quit"))
1338 else if (!strcmp(argv[0], "attach"))
1339 gvinum_attach(argc, argv);
1340 else if (!strcmp(argv[0], "detach"))
1341 gvinum_detach(argc, argv);
1342 else if (!strcmp(argv[0], "concat"))
1343 gvinum_concat(argc, argv);
1344 else if (!strcmp(argv[0], "grow"))
1345 gvinum_grow(argc, argv);
1346 else if (!strcmp(argv[0], "help"))
1348 else if (!strcmp(argv[0], "list") || !strcmp(argv[0], "l"))
1349 gvinum_list(argc, argv);
1350 else if (!strcmp(argv[0], "ld"))
1351 gvinum_list(argc, argv);
1352 else if (!strcmp(argv[0], "lp"))
1353 gvinum_list(argc, argv);
1354 else if (!strcmp(argv[0], "ls"))
1355 gvinum_list(argc, argv);
1356 else if (!strcmp(argv[0], "lv"))
1357 gvinum_list(argc, argv);
1358 else if (!strcmp(argv[0], "mirror"))
1359 gvinum_mirror(argc, argv);
1360 else if (!strcmp(argv[0], "move"))
1361 gvinum_move(argc, argv);
1362 else if (!strcmp(argv[0], "mv"))
1363 gvinum_move(argc, argv);
1364 else if (!strcmp(argv[0], "printconfig"))
1365 gvinum_printconfig(argc, argv);
1366 else if (!strcmp(argv[0], "raid5"))
1367 gvinum_raid5(argc, argv);
1368 else if (!strcmp(argv[0], "rename"))
1369 gvinum_rename(argc, argv);
1370 else if (!strcmp(argv[0], "resetconfig"))
1371 gvinum_resetconfig();
1372 else if (!strcmp(argv[0], "rm"))
1373 gvinum_rm(argc, argv);
1374 else if (!strcmp(argv[0], "saveconfig"))
1375 gvinum_saveconfig();
1376 else if (!strcmp(argv[0], "setstate"))
1377 gvinum_setstate(argc, argv);
1378 else if (!strcmp(argv[0], "start"))
1379 gvinum_start(argc, argv);
1380 else if (!strcmp(argv[0], "stop"))
1381 gvinum_stop(argc, argv);
1382 else if (!strcmp(argv[0], "stripe"))
1383 gvinum_stripe(argc, argv);
1384 else if (!strcmp(argv[0], "checkparity"))
1385 gvinum_parityop(argc, argv, 0);
1386 else if (!strcmp(argv[0], "rebuildparity"))
1387 gvinum_parityop(argc, argv, 1);
1389 printf("unknown command '%s'\n", argv[0]);
1395 * The guts of printconfig. This is called from gvinum_printconfig and from
1396 * gvinum_create when called without an argument, in order to give the user
1397 * something to edit.
1400 printconfig(FILE *of, char *comment)
1402 struct gctl_req *req;
1403 struct utsname uname_s;
1406 char buf[GV_CFG_LEN + 1];
1411 req = gctl_get_handle();
1412 gctl_ro_param(req, "class", -1, "VINUM");
1413 gctl_ro_param(req, "verb", -1, "getconfig");
1414 gctl_ro_param(req, "comment", -1, comment);
1415 gctl_rw_param(req, "config", sizeof(buf), buf);
1416 errstr = gctl_issue(req);
1417 if (errstr != NULL) {
1418 warnx("can't get configuration: %s", errstr);
1423 fprintf(of, "# Vinum configuration of %s, saved at %s",
1427 if (*comment != '\0')
1428 fprintf(of, "# Current configuration:\n");