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 void gvinum_attach(int, char **);
62 void gvinum_concat(int, char **);
63 void gvinum_create(int, char **);
64 void gvinum_detach(int, char **);
65 void gvinum_grow(int, char **);
66 void gvinum_help(void);
67 void gvinum_list(int, char **);
68 void gvinum_move(int, char **);
69 void gvinum_mirror(int, char **);
70 void gvinum_parityop(int, char **, int);
71 void gvinum_printconfig(int, char **);
72 void gvinum_raid5(int, char **);
73 void gvinum_rename(int, char **);
74 void gvinum_resetconfig(void);
75 void gvinum_rm(int, char **);
76 void gvinum_saveconfig(void);
77 void gvinum_setstate(int, char **);
78 void gvinum_start(int, char **);
79 void gvinum_stop(int, char **);
80 void gvinum_stripe(int, char **);
81 void parseline(int, char **);
82 void printconfig(FILE *, char *);
84 char *create_drive(char *);
85 void create_volume(int, char **, char *);
86 char *find_name(const char *, int, int);
87 char *find_pattern(char *, char *);
88 void copy_device(struct gv_drive *, const char *);
89 #define find_drive() find_name("gvinumdrive", GV_TYPE_DRIVE, GV_MAXDRIVENAME)
92 main(int argc, char **argv)
95 char buffer[BUFSIZ], *inputline, *token[GV_MAXARGS];
97 /* Load the module if necessary. */
98 if (kldfind(GVINUMMOD) < 0 && kldload(GVINUMMOD) < 0)
99 err(1, GVINUMMOD ": Kernel module not available");
101 /* Arguments given on the command line. */
105 parseline(argc, argv);
107 /* Interactive mode. */
110 inputline = readline("gvinum -> ");
111 if (inputline == NULL) {
113 err(1, "can't read input");
118 } else if (*inputline) {
119 add_history(inputline);
120 strcpy(buffer, inputline);
122 line++; /* count the lines */
123 tokens = gv_tokenize(buffer, token, GV_MAXARGS);
125 parseline(tokens, token);
132 /* Attach a plex to a volume or a subdisk to a plex. */
134 gvinum_attach(int argc, char **argv)
136 struct gctl_req *req;
144 warnx("usage:\tattach <subdisk> <plex> [rename] "
146 "\tattach <plex> <volume> [rename]");
150 if (!strcmp(argv[3], "rename")) {
153 offset = strtol(argv[4], NULL, 0);
155 offset = strtol(argv[3], NULL, 0);
157 req = gctl_get_handle();
158 gctl_ro_param(req, "class", -1, "VINUM");
159 gctl_ro_param(req, "verb", -1, "attach");
160 gctl_ro_param(req, "child", -1, argv[1]);
161 gctl_ro_param(req, "parent", -1, argv[2]);
162 gctl_ro_param(req, "offset", sizeof(off_t), &offset);
163 gctl_ro_param(req, "rename", sizeof(int), &rename);
164 errstr = gctl_issue(req);
166 warnx("attach failed: %s", errstr);
171 gvinum_create(int argc, char **argv)
173 struct gctl_req *req;
179 int drives, errors, fd, flags, i, line, plexes, plex_in_volume;
180 int sd_in_plex, status, subdisks, tokens, undeffd, volumes;
182 char buf[BUFSIZ], buf1[BUFSIZ], commandline[BUFSIZ], *ed, *sdname;
183 char original[BUFSIZ], tmpfile[20], *token[GV_MAXARGS];
184 char plex[GV_MAXPLEXNAME], volume[GV_MAXVOLNAME];
188 for (i = 1; i < argc; i++) {
189 /* Force flag used to ignore already created drives. */
190 if (!strcmp(argv[i], "-f")) {
192 /* Else it must be a file. */
194 if ((tmp = fopen(argv[1], "r")) == NULL) {
195 warn("can't open '%s' for reading", argv[1]);
201 /* We didn't get a file. */
203 snprintf(tmpfile, sizeof(tmpfile), "/tmp/gvinum.XXXXXX");
205 if ((fd = mkstemp(tmpfile)) == -1) {
206 warn("temporary file not accessible");
209 if ((tmp = fdopen(fd, "w")) == NULL) {
210 warn("can't open '%s' for writing", tmpfile);
213 printconfig(tmp, "# ");
216 ed = getenv("EDITOR");
220 snprintf(commandline, sizeof(commandline), "%s %s", ed,
222 status = system(commandline);
224 warn("couldn't exec %s; status: %d", ed, status);
228 if ((tmp = fopen(tmpfile, "r")) == NULL) {
229 warn("can't open '%s' for reading", tmpfile);
234 req = gctl_get_handle();
235 gctl_ro_param(req, "class", -1, "VINUM");
236 gctl_ro_param(req, "verb", -1, "create");
237 gctl_ro_param(req, "flags", sizeof(int), &flags);
239 drives = volumes = plexes = subdisks = 0;
240 plex_in_volume = sd_in_plex = undeffd = 0;
244 while ((fgets(buf, BUFSIZ, tmp)) != NULL) {
246 /* Skip empty lines and comments. */
247 if (*buf == '\0' || *buf == '#') {
252 /* Kill off the newline. */
253 buf[strlen(buf) - 1] = '\0';
256 * Copy the original input line in case we need it for error
259 strlcpy(original, buf, sizeof(original));
261 tokens = gv_tokenize(buf, token, GV_MAXARGS);
267 /* Volume definition. */
268 if (!strcmp(token[0], "volume")) {
269 v = gv_new_volume(tokens, token);
271 warnx("line %d: invalid volume definition",
273 warnx("line %d: '%s'", line, original);
279 /* Reset plex count for this volume. */
283 * Set default volume name for following plex
286 strlcpy(volume, v->name, sizeof(volume));
288 snprintf(buf1, sizeof(buf1), "volume%d", volumes);
289 gctl_ro_param(req, buf1, sizeof(*v), v);
292 /* Plex definition. */
293 } else if (!strcmp(token[0], "plex")) {
294 p = gv_new_plex(tokens, token);
296 warnx("line %d: invalid plex definition", line);
297 warnx("line %d: '%s'", line, original);
303 /* Reset subdisk count for this plex. */
307 if (strlen(p->name) == 0) {
308 snprintf(p->name, sizeof(p->name), "%s.p%d",
309 volume, plex_in_volume++);
312 /* Default volume. */
313 if (strlen(p->volume) == 0) {
314 snprintf(p->volume, sizeof(p->volume), "%s",
319 * Set default plex name for following subdisk
322 strlcpy(plex, p->name, sizeof(plex));
324 snprintf(buf1, sizeof(buf1), "plex%d", plexes);
325 gctl_ro_param(req, buf1, sizeof(*p), p);
328 /* Subdisk definition. */
329 } else if (!strcmp(token[0], "sd")) {
330 s = gv_new_sd(tokens, token);
332 warnx("line %d: invalid subdisk "
333 "definition:", line);
334 warnx("line %d: '%s'", line, original);
341 if (strlen(s->name) == 0) {
342 if (strlen(plex) == 0) {
343 sdname = find_name("gvinumsubdisk.p",
344 GV_TYPE_SD, GV_MAXSDNAME);
345 snprintf(s->name, sizeof(s->name),
346 "%s.s%d", sdname, undeffd++);
349 snprintf(s->name, sizeof(s->name),
350 "%s.s%d",plex, sd_in_plex++);
355 if (strlen(s->plex) == 0)
356 snprintf(s->plex, sizeof(s->plex), "%s", plex);
358 snprintf(buf1, sizeof(buf1), "sd%d", subdisks);
359 gctl_ro_param(req, buf1, sizeof(*s), s);
362 /* Subdisk definition. */
363 } else if (!strcmp(token[0], "drive")) {
364 d = gv_new_drive(tokens, token);
366 warnx("line %d: invalid drive definition:",
368 warnx("line %d: '%s'", line, original);
374 snprintf(buf1, sizeof(buf1), "drive%d", drives);
375 gctl_ro_param(req, buf1, sizeof(*d), d);
378 /* Everything else is bogus. */
380 warnx("line %d: invalid definition:", line);
381 warnx("line %d: '%s'", line, original);
390 if (!errors && (volumes || plexes || subdisks || drives)) {
391 gctl_ro_param(req, "volumes", sizeof(int), &volumes);
392 gctl_ro_param(req, "plexes", sizeof(int), &plexes);
393 gctl_ro_param(req, "subdisks", sizeof(int), &subdisks);
394 gctl_ro_param(req, "drives", sizeof(int), &drives);
395 errstr = gctl_issue(req);
397 warnx("create failed: %s", errstr);
402 /* Create a concatenated volume. */
404 gvinum_concat(int argc, char **argv)
408 warnx("usage:\tconcat [-fv] [-n name] drives\n");
411 create_volume(argc, argv, "concat");
415 /* Create a drive quick and dirty. */
417 create_drive(char *device)
420 struct gctl_req *req;
422 char *drivename, *dname;
423 int drives, i, flags, volumes, subdisks, plexes;
425 flags = plexes = subdisks = volumes = 0;
429 drivename = find_drive();
430 if (drivename == NULL)
433 req = gctl_get_handle();
434 gctl_ro_param(req, "class", -1, "VINUM");
435 gctl_ro_param(req, "verb", -1, "create");
436 d = gv_alloc_drive();
438 err(1, "unable to allocate for gv_drive object");
440 strlcpy(d->name, drivename, sizeof(d->name));
441 copy_device(d, device);
442 gctl_ro_param(req, "drive0", sizeof(*d), d);
443 gctl_ro_param(req, "flags", sizeof(int), &flags);
444 gctl_ro_param(req, "drives", sizeof(int), &drives);
445 gctl_ro_param(req, "volumes", sizeof(int), &volumes);
446 gctl_ro_param(req, "plexes", sizeof(int), &plexes);
447 gctl_ro_param(req, "subdisks", sizeof(int), &subdisks);
448 errstr = gctl_issue(req);
449 if (errstr != NULL) {
450 warnx("error creating drive: %s", errstr);
455 /* XXX: This is needed because we have to make sure the drives
456 * are created before we return. */
457 /* Loop until it's in the config. */
458 for (i = 0; i < 100000; i++) {
459 dname = find_name("gvinumdrive", GV_TYPE_DRIVE,
461 /* If we got a different name, quit. */
464 if (strcmp(dname, drivename)) {
470 usleep(100000); /* Sleep for 0.1s */
478 * General routine for creating a volume. Mainly for use by concat, mirror,
479 * raid5 and stripe commands.
482 create_volume(int argc, char **argv, char *verb)
484 struct gctl_req *req;
486 char buf[BUFSIZ], *drivename, *volname;
487 int drives, flags, i;
495 /* XXX: Should we check for argument length? */
497 req = gctl_get_handle();
498 gctl_ro_param(req, "class", -1, "VINUM");
500 for (i = 1; i < argc; i++) {
501 if (!strcmp(argv[i], "-f")) {
503 } else if (!strcmp(argv[i], "-n")) {
505 } else if (!strcmp(argv[i], "-v")) {
507 } else if (!strcmp(argv[i], "-s")) {
509 if (!strcmp(verb, "raid5"))
510 stripesize = gv_sizespec(argv[++i]);
512 /* Assume it's a drive. */
513 snprintf(buf, sizeof(buf), "drive%d", drives++);
515 /* First we create the drive. */
516 drivename = create_drive(argv[i]);
517 if (drivename == NULL)
519 /* Then we add it to the request. */
520 gctl_ro_param(req, buf, -1, drivename);
524 gctl_ro_param(req, "stripesize", sizeof(off_t), &stripesize);
526 /* Find a free volume name. */
528 volname = find_name("gvinumvolume", GV_TYPE_VOL, GV_MAXVOLNAME);
530 /* Then we send a request to actually create the volumes. */
531 gctl_ro_param(req, "verb", -1, verb);
532 gctl_ro_param(req, "flags", sizeof(int), &flags);
533 gctl_ro_param(req, "drives", sizeof(int), &drives);
534 gctl_ro_param(req, "name", -1, volname);
535 errstr = gctl_issue(req);
537 warnx("creating %s volume failed: %s", verb, errstr);
542 /* Parse a line of the config, return the word after <pattern>. */
544 find_pattern(char *line, char *pattern)
548 ptr = strsep(&line, " ");
549 while (ptr != NULL) {
550 if (!strcmp(ptr, pattern)) {
551 /* Return the next. */
552 ptr = strsep(&line, " ");
555 ptr = strsep(&line, " ");
560 /* Find a free name for an object given a a prefix. */
562 find_name(const char *prefix, int type, int namelen)
564 struct gctl_req *req;
565 char comment[1], buf[GV_CFG_LEN - 1], *name, *sname, *ptr;
567 int i, n, begin, len, conflict;
572 /* Find a name. Fetch out configuration first. */
573 req = gctl_get_handle();
574 gctl_ro_param(req, "class", -1, "VINUM");
575 gctl_ro_param(req, "verb", -1, "getconfig");
576 gctl_ro_param(req, "comment", -1, comment);
577 gctl_rw_param(req, "config", sizeof(buf), buf);
578 errstr = gctl_issue(req);
579 if (errstr != NULL) {
580 warnx("can't get configuration: %s", errstr);
588 sname = malloc(namelen + 1);
590 /* XXX: Max object setting? */
591 for (n = 0; n < 10000; n++) {
592 snprintf(sname, namelen, "%s%d", prefix, n);
595 /* Loop through the configuration line by line. */
596 for (i = 0; i < len; i++) {
597 if (buf[i] == '\n' || buf[i] == '\0') {
599 strlcpy(line, ptr, (i - begin) + 1);
603 name = find_pattern(line, "drive");
606 name = find_pattern(line, "volume");
610 name = find_pattern(line, "name");
613 printf("Invalid type given\n");
618 if (!strcmp(sname, name)) {
620 /* XXX: Could quit the loop earlier. */
632 copy_device(struct gv_drive *d, const char *device)
634 if (strncmp(device, "/dev/", 5) == 0)
635 strlcpy(d->device, (device + 5), sizeof(d->device));
637 strlcpy(d->device, device, sizeof(d->device));
640 /* Detach a plex or subdisk from its parent. */
642 gvinum_detach(int argc, char **argv)
645 struct gctl_req *req;
651 while ((i = getopt(argc, argv, "f")) != -1) {
657 warn("invalid flag: %c", i);
664 warnx("usage: detach [-f] <subdisk> | <plex>");
668 req = gctl_get_handle();
669 gctl_ro_param(req, "class", -1, "VINUM");
670 gctl_ro_param(req, "verb", -1, "detach");
671 gctl_ro_param(req, "object", -1, argv[0]);
672 gctl_ro_param(req, "flags", sizeof(int), &flags);
674 errstr = gctl_issue(req);
676 warnx("detach failed: %s", errstr);
684 "checkparity [-f] plex\n"
685 " Check the parity blocks of a RAID-5 plex.\n"
686 "create [-f] description-file\n"
687 " Create as per description-file or open editor.\n"
688 "attach plex volume [rename]\n"
689 "attach subdisk plex [offset] [rename]\n"
690 " Attach a plex to a volume, or a subdisk to a plex\n"
691 "concat [-fv] [-n name] drives\n"
692 " Create a concatenated volume from the specified drives.\n"
693 "detach [-f] [plex | subdisk]\n"
694 " Detach a plex or a subdisk from the volume or plex to\n"
695 " which it is attached.\n"
697 " Grow plex by creating a properly sized subdisk on drive\n"
698 "l | list [-r] [-v] [-V] [volume | plex | subdisk]\n"
699 " List information about specified objects.\n"
700 "ld [-r] [-v] [-V] [volume]\n"
701 " List information about drives.\n"
702 "ls [-r] [-v] [-V] [subdisk]\n"
703 " List information about subdisks.\n"
704 "lp [-r] [-v] [-V] [plex]\n"
705 " List information about plexes.\n"
706 "lv [-r] [-v] [-V] [volume]\n"
707 " List information about volumes.\n"
708 "mirror [-fsv] [-n name] drives\n"
709 " Create a mirrored volume from the specified drives.\n"
710 "move | mv -f drive object ...\n"
711 " Move the object(s) to the specified drive.\n"
712 "quit Exit the vinum program when running in interactive mode."
714 " mally this would be done by entering the EOF character.\n"
715 "raid5 [-fv] [-s stripesize] [-n name] drives\n"
716 " Create a RAID-5 volume from the specified drives.\n"
717 "rename [-r] [drive | subdisk | plex | volume] newname\n"
718 " Change the name of the specified object.\n"
719 "rebuildparity plex [-f]\n"
720 " Rebuild the parity blocks of a RAID-5 plex.\n"
722 " Reset the complete gvinum configuration\n"
723 "rm [-r] [-f] volume | plex | subdisk | drive\n"
724 " Remove an object.\n"
726 " Save vinum configuration to disk after configuration"
728 "setstate [-f] state [volume | plex | subdisk | drive]\n"
729 " Set state without influencing other objects, for"
732 "start [-S size] volume | plex | subdisk\n"
733 " Allow the system to access the objects.\n"
734 "stripe [-fv] [-n name] drives\n"
735 " Create a striped volume from the specified drives.\n"
742 gvinum_setstate(int argc, char **argv)
744 struct gctl_req *req;
753 while ((i = getopt(argc, argv, "f")) != -1) {
760 warn("invalid flag: %c", i);
769 warnx("usage: setstate [-f] <state> <obj>");
774 * XXX: This hack is needed to avoid tripping over (now) invalid
775 * 'classic' vinum states and will go away later.
777 if (strcmp(argv[0], "up") && strcmp(argv[0], "down") &&
778 strcmp(argv[0], "stale")) {
779 warnx("invalid state '%s'", argv[0]);
783 req = gctl_get_handle();
784 gctl_ro_param(req, "class", -1, "VINUM");
785 gctl_ro_param(req, "verb", -1, "setstate");
786 gctl_ro_param(req, "state", -1, argv[0]);
787 gctl_ro_param(req, "object", -1, argv[1]);
788 gctl_ro_param(req, "flags", sizeof(int), &flags);
790 errstr = gctl_issue(req);
797 gvinum_list(int argc, char **argv)
799 struct gctl_req *req;
802 char buf[20], *cmd, config[GV_CFG_LEN + 1];
811 while ((j = getopt(argc, argv, "rsvV")) != -1) {
836 req = gctl_get_handle();
837 gctl_ro_param(req, "class", -1, "VINUM");
838 gctl_ro_param(req, "verb", -1, "list");
839 gctl_ro_param(req, "cmd", -1, cmd);
840 gctl_ro_param(req, "argc", sizeof(int), &argc);
841 gctl_ro_param(req, "flags", sizeof(int), &flags);
842 gctl_rw_param(req, "config", sizeof(config), config);
844 for (i = 0; i < argc; i++) {
845 snprintf(buf, sizeof(buf), "argv%d", i);
846 gctl_ro_param(req, buf, -1, argv[i]);
849 errstr = gctl_issue(req);
850 if (errstr != NULL) {
851 warnx("can't get configuration: %s", errstr);
856 printf("%s", config);
861 /* Create a mirrored volume. */
863 gvinum_mirror(int argc, char **argv)
867 warnx("usage\tmirror [-fsv] [-n name] drives\n");
870 create_volume(argc, argv, "mirror");
873 /* Note that move is currently of form '[-r] target object [...]' */
875 gvinum_move(int argc, char **argv)
877 struct gctl_req *req;
886 while ((j = getopt(argc, argv, "f")) != -1) {
902 warnx("no destination or object(s) to move specified");
905 warnx("no object(s) to move specified");
911 req = gctl_get_handle();
912 gctl_ro_param(req, "class", -1, "VINUM");
913 gctl_ro_param(req, "verb", -1, "move");
914 gctl_ro_param(req, "argc", sizeof(int), &argc);
915 gctl_ro_param(req, "flags", sizeof(int), &flags);
916 gctl_ro_param(req, "destination", -1, argv[0]);
917 for (i = 1; i < argc; i++) {
918 snprintf(buf, sizeof(buf), "argv%d", i);
919 gctl_ro_param(req, buf, -1, argv[i]);
921 errstr = gctl_issue(req);
923 warnx("can't move object(s): %s", errstr);
929 gvinum_printconfig(int argc, char **argv)
931 printconfig(stdout, "");
935 gvinum_parityop(int argc, char **argv, int rebuild)
937 struct gctl_req *req;
943 op = "rebuildparity";
953 while ((i = getopt(argc, argv, "fv")) != -1) {
963 warnx("invalid flag '%c'", i);
971 warn("usage: %s [-f] [-v] <plex>", op);
975 req = gctl_get_handle();
976 gctl_ro_param(req, "class", -1, "VINUM");
977 gctl_ro_param(req, "verb", -1, op);
978 gctl_ro_param(req, "rebuild", sizeof(int), &rebuild);
979 gctl_ro_param(req, "flags", sizeof(int), &flags);
980 gctl_ro_param(req, "plex", -1, argv[0]);
982 errstr = gctl_issue(req);
984 warnx("%s\n", errstr);
988 /* Create a RAID-5 volume. */
990 gvinum_raid5(int argc, char **argv)
994 warnx("usage:\traid5 [-fv] [-s stripesize] [-n name] drives\n");
997 create_volume(argc, argv, "raid5");
1002 gvinum_rename(int argc, char **argv)
1004 struct gctl_req *req;
1013 while ((j = getopt(argc, argv, "r")) != -1) {
1029 warnx("no object to rename specified");
1032 warnx("no new name specified");
1037 warnx("more than one new name specified");
1041 req = gctl_get_handle();
1042 gctl_ro_param(req, "class", -1, "VINUM");
1043 gctl_ro_param(req, "verb", -1, "rename");
1044 gctl_ro_param(req, "flags", sizeof(int), &flags);
1045 gctl_ro_param(req, "object", -1, argv[0]);
1046 gctl_ro_param(req, "newname", -1, argv[1]);
1047 errstr = gctl_issue(req);
1049 warnx("can't rename object: %s", errstr);
1055 gvinum_rm(int argc, char **argv)
1057 struct gctl_req *req;
1066 while ((j = getopt(argc, argv, "rf")) != -1) {
1082 req = gctl_get_handle();
1083 gctl_ro_param(req, "class", -1, "VINUM");
1084 gctl_ro_param(req, "verb", -1, "remove");
1085 gctl_ro_param(req, "argc", sizeof(int), &argc);
1086 gctl_ro_param(req, "flags", sizeof(int), &flags);
1088 for (i = 0; i < argc; i++) {
1089 snprintf(buf, sizeof(buf), "argv%d", i);
1090 gctl_ro_param(req, buf, -1, argv[i]);
1093 errstr = gctl_issue(req);
1094 if (errstr != NULL) {
1095 warnx("can't remove: %s", errstr);
1103 gvinum_resetconfig(void)
1105 struct gctl_req *req;
1109 if (!isatty(STDIN_FILENO)) {
1110 warn("Please enter this command from a tty device\n");
1113 printf(" WARNING! This command will completely wipe out your gvinum"
1115 " All data will be lost. If you really want to do this,"
1116 " enter the text\n\n"
1119 fgets(reply, sizeof(reply), stdin);
1120 if (strcmp(reply, "NO FUTURE\n")) {
1121 printf("\n No change\n");
1124 req = gctl_get_handle();
1125 gctl_ro_param(req, "class", -1, "VINUM");
1126 gctl_ro_param(req, "verb", -1, "resetconfig");
1127 errstr = gctl_issue(req);
1128 if (errstr != NULL) {
1129 warnx("can't reset config: %s", errstr);
1134 printf("gvinum configuration obliterated\n");
1138 gvinum_saveconfig(void)
1140 struct gctl_req *req;
1143 req = gctl_get_handle();
1144 gctl_ro_param(req, "class", -1, "VINUM");
1145 gctl_ro_param(req, "verb", -1, "saveconfig");
1146 errstr = gctl_issue(req);
1148 warnx("can't save configuration: %s", errstr);
1153 gvinum_start(int argc, char **argv)
1155 struct gctl_req *req;
1160 /* 'start' with no arguments is a no-op. */
1168 while ((j = getopt(argc, argv, "S")) != -1) {
1171 initsize = atoi(optarg);
1184 req = gctl_get_handle();
1185 gctl_ro_param(req, "class", -1, "VINUM");
1186 gctl_ro_param(req, "verb", -1, "start");
1187 gctl_ro_param(req, "argc", sizeof(int), &argc);
1188 gctl_ro_param(req, "initsize", sizeof(int), &initsize);
1190 for (i = 0; i < argc; i++) {
1191 snprintf(buf, sizeof(buf), "argv%d", i);
1192 gctl_ro_param(req, buf, -1, argv[i]);
1195 errstr = gctl_issue(req);
1196 if (errstr != NULL) {
1197 warnx("can't start: %s", errstr);
1206 gvinum_stop(int argc, char **argv)
1210 fileid = kldfind(GVINUMMOD);
1212 warn("cannot find " GVINUMMOD);
1217 * This little hack prevents that we end up in an infinite loop in
1218 * g_unload_class(). gv_unload() will return EAGAIN so that the GEOM
1219 * event thread will be free for the g_wither_geom() call from
1220 * gv_unload(). It's silly, but it works.
1222 printf("unloading " GVINUMMOD " kernel module... ");
1224 if ((err = kldunload(fileid)) != 0 && (errno == EAGAIN)) {
1226 err = kldunload(fileid);
1229 printf(" failed!\n");
1230 warn("cannot unload " GVINUMMOD);
1238 /* Create a striped volume. */
1240 gvinum_stripe(int argc, char **argv)
1244 warnx("usage:\tstripe [-fv] [-n name] drives\n");
1247 create_volume(argc, argv, "stripe");
1250 /* Grow a subdisk by adding disk backed by provider. */
1252 gvinum_grow(int argc, char **argv)
1254 struct gctl_req *req;
1255 char *drive, *sdname;
1256 char sdprefix[GV_MAXSDNAME];
1260 int drives, volumes, plexes, subdisks, flags;
1262 drives = volumes = plexes = subdisks = 0;
1264 warnx("usage:\tgrow plex drive\n");
1270 warn("unable to create subdisk");
1273 d = gv_alloc_drive();
1275 warn("unable to create drive");
1279 /* Lookup device and set an appropriate drive name. */
1280 drive = find_drive();
1281 if (drive == NULL) {
1282 warn("unable to find an appropriate drive name");
1287 strlcpy(d->name, drive, sizeof(d->name));
1288 copy_device(d, argv[2]);
1292 /* We try to use the plex name as basis for the subdisk name. */
1293 snprintf(sdprefix, sizeof(sdprefix), "%s.s", argv[1]);
1294 sdname = find_name(sdprefix, GV_TYPE_SD, GV_MAXSDNAME);
1295 if (sdname == NULL) {
1296 warn("unable to find an appropriate subdisk name");
1302 strlcpy(s->name, sdname, sizeof(s->name));
1304 strlcpy(s->plex, argv[1], sizeof(s->plex));
1305 strlcpy(s->drive, d->name, sizeof(s->drive));
1308 req = gctl_get_handle();
1309 gctl_ro_param(req, "class", -1, "VINUM");
1310 gctl_ro_param(req, "verb", -1, "create");
1311 gctl_ro_param(req, "flags", sizeof(int), &flags);
1312 gctl_ro_param(req, "volumes", sizeof(int), &volumes);
1313 gctl_ro_param(req, "plexes", sizeof(int), &plexes);
1314 gctl_ro_param(req, "subdisks", sizeof(int), &subdisks);
1315 gctl_ro_param(req, "drives", sizeof(int), &drives);
1316 gctl_ro_param(req, "drive0", sizeof(*d), d);
1317 gctl_ro_param(req, "sd0", sizeof(*s), s);
1318 errstr = gctl_issue(req);
1320 if (errstr != NULL) {
1321 warnx("unable to grow plex: %s", errstr);
1330 parseline(int argc, char **argv)
1335 if (!strcmp(argv[0], "create"))
1336 gvinum_create(argc, argv);
1337 else if (!strcmp(argv[0], "exit") || !strcmp(argv[0], "quit"))
1339 else if (!strcmp(argv[0], "attach"))
1340 gvinum_attach(argc, argv);
1341 else if (!strcmp(argv[0], "detach"))
1342 gvinum_detach(argc, argv);
1343 else if (!strcmp(argv[0], "concat"))
1344 gvinum_concat(argc, argv);
1345 else if (!strcmp(argv[0], "grow"))
1346 gvinum_grow(argc, argv);
1347 else if (!strcmp(argv[0], "help"))
1349 else if (!strcmp(argv[0], "list") || !strcmp(argv[0], "l"))
1350 gvinum_list(argc, argv);
1351 else if (!strcmp(argv[0], "ld"))
1352 gvinum_list(argc, argv);
1353 else if (!strcmp(argv[0], "lp"))
1354 gvinum_list(argc, argv);
1355 else if (!strcmp(argv[0], "ls"))
1356 gvinum_list(argc, argv);
1357 else if (!strcmp(argv[0], "lv"))
1358 gvinum_list(argc, argv);
1359 else if (!strcmp(argv[0], "mirror"))
1360 gvinum_mirror(argc, argv);
1361 else if (!strcmp(argv[0], "move"))
1362 gvinum_move(argc, argv);
1363 else if (!strcmp(argv[0], "mv"))
1364 gvinum_move(argc, argv);
1365 else if (!strcmp(argv[0], "printconfig"))
1366 gvinum_printconfig(argc, argv);
1367 else if (!strcmp(argv[0], "raid5"))
1368 gvinum_raid5(argc, argv);
1369 else if (!strcmp(argv[0], "rename"))
1370 gvinum_rename(argc, argv);
1371 else if (!strcmp(argv[0], "resetconfig"))
1372 gvinum_resetconfig();
1373 else if (!strcmp(argv[0], "rm"))
1374 gvinum_rm(argc, argv);
1375 else if (!strcmp(argv[0], "saveconfig"))
1376 gvinum_saveconfig();
1377 else if (!strcmp(argv[0], "setstate"))
1378 gvinum_setstate(argc, argv);
1379 else if (!strcmp(argv[0], "start"))
1380 gvinum_start(argc, argv);
1381 else if (!strcmp(argv[0], "stop"))
1382 gvinum_stop(argc, argv);
1383 else if (!strcmp(argv[0], "stripe"))
1384 gvinum_stripe(argc, argv);
1385 else if (!strcmp(argv[0], "checkparity"))
1386 gvinum_parityop(argc, argv, 0);
1387 else if (!strcmp(argv[0], "rebuildparity"))
1388 gvinum_parityop(argc, argv, 1);
1390 printf("unknown command '%s'\n", argv[0]);
1396 * The guts of printconfig. This is called from gvinum_printconfig and from
1397 * gvinum_create when called without an argument, in order to give the user
1398 * something to edit.
1401 printconfig(FILE *of, char *comment)
1403 struct gctl_req *req;
1404 struct utsname uname_s;
1407 char buf[GV_CFG_LEN + 1];
1412 req = gctl_get_handle();
1413 gctl_ro_param(req, "class", -1, "VINUM");
1414 gctl_ro_param(req, "verb", -1, "getconfig");
1415 gctl_ro_param(req, "comment", -1, comment);
1416 gctl_rw_param(req, "config", sizeof(buf), buf);
1417 errstr = gctl_issue(req);
1418 if (errstr != NULL) {
1419 warnx("can't get configuration: %s", errstr);
1424 fprintf(of, "# Vinum configuration of %s, saved at %s",
1428 if (*comment != '\0')
1429 fprintf(of, "# Current configuration:\n");
1431 fprintf(of, "%s", buf);