2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004 Lukas Ertl
5 * Copyright (c) 2005 Chris Jones
6 * Copyright (c) 2007 Ulf Lilleengen
9 * Portions of this software were developed for the FreeBSD Project
10 * by Chris Jones thanks to the support of Google's Summer of Code
11 * program and mentoring by Lukas Ertl.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include <sys/param.h>
38 #include <sys/linker.h>
40 #include <sys/module.h>
41 #include <sys/mutex.h>
42 #include <sys/queue.h>
43 #include <sys/utsname.h>
45 #include <geom/vinum/geom_vinum_var.h>
46 #include <geom/vinum/geom_vinum_share.h>
57 #include <readline/readline.h>
58 #include <readline/history.h>
63 static void gvinum_attach(int, char * const *);
64 static void gvinum_concat(int, char * const *);
65 static void gvinum_create(int, char * const *);
66 static void gvinum_detach(int, char * const *);
67 static void gvinum_grow(int, char * const *);
68 static void gvinum_help(void);
69 static void gvinum_list(int, char * const *);
70 static void gvinum_move(int, char * const *);
71 static void gvinum_mirror(int, char * const *);
72 static void gvinum_parityop(int, char * const * , int);
73 static void gvinum_printconfig(int, char * const *);
74 static void gvinum_raid5(int, char * const *);
75 static void gvinum_rename(int, char * const *);
76 static void gvinum_resetconfig(int, char * const *);
77 static void gvinum_rm(int, char * const *);
78 static void gvinum_saveconfig(void);
79 static void gvinum_setstate(int, char * const *);
80 static void gvinum_start(int, char * const *);
81 static void gvinum_stop(int, char * const *);
82 static void gvinum_stripe(int, char * const *);
83 static void parseline(int, char * const *);
84 static void printconfig(FILE *, const char *);
86 static char *create_drive(const char *);
87 static void create_volume(int, char * const * , const char *);
88 static char *find_name(const char *, int, int);
89 static const char *find_pattern(char *, const char *);
90 static void copy_device(struct gv_drive *, const char *);
91 #define find_drive() \
92 find_name("gvinumdrive", GV_TYPE_DRIVE, GV_MAXDRIVENAME)
95 main(int argc, char **argv)
98 char buffer[BUFSIZ], *inputline, *token[GV_MAXARGS];
100 /* Load the module if necessary. */
101 if (modfind(GVINUMMOD) < 0) {
102 if (kldload(GVINUMKLD) < 0 && modfind(GVINUMMOD) < 0)
103 err(1, GVINUMKLD ": Kernel module not available");
106 /* Arguments given on the command line. */
110 parseline(argc, argv);
112 /* Interactive mode. */
115 inputline = readline("gvinum -> ");
116 if (inputline == NULL) {
118 err(1, "can't read input");
123 } else if (*inputline) {
124 add_history(inputline);
125 strcpy(buffer, inputline);
127 line++; /* count the lines */
128 tokens = gv_tokenize(buffer, token, GV_MAXARGS);
130 parseline(tokens, token);
137 /* Attach a plex to a volume or a subdisk to a plex. */
139 gvinum_attach(int argc, char * const *argv)
141 struct gctl_req *req;
149 warnx("usage:\tattach <subdisk> <plex> [rename] "
151 "\tattach <plex> <volume> [rename]");
155 if (!strcmp(argv[3], "rename")) {
158 offset = strtol(argv[4], NULL, 0);
160 offset = strtol(argv[3], NULL, 0);
162 req = gctl_get_handle();
163 gctl_ro_param(req, "class", -1, "VINUM");
164 gctl_ro_param(req, "verb", -1, "attach");
165 gctl_ro_param(req, "child", -1, argv[1]);
166 gctl_ro_param(req, "parent", -1, argv[2]);
167 gctl_ro_param(req, "offset", sizeof(off_t), &offset);
168 gctl_ro_param(req, "rename", sizeof(int), &rename);
169 errstr = gctl_issue(req);
171 warnx("attach failed: %s", errstr);
176 gvinum_create(int argc, char * const *argv)
178 struct gctl_req *req;
184 int drives, errors, fd, flags, i, line, plexes, plex_in_volume;
185 int sd_in_plex, status, subdisks, tokens, undeffd, volumes;
187 char buf[BUFSIZ], buf1[BUFSIZ], commandline[BUFSIZ], *ed, *sdname;
188 char original[BUFSIZ], tmpfile[20], *token[GV_MAXARGS];
189 char plex[GV_MAXPLEXNAME], volume[GV_MAXVOLNAME];
193 for (i = 1; i < argc; i++) {
194 /* Force flag used to ignore already created drives. */
195 if (!strcmp(argv[i], "-f")) {
197 /* Else it must be a file. */
199 if ((tmp = fopen(argv[i], "r")) == NULL) {
200 warn("can't open '%s' for reading", argv[i]);
206 /* We didn't get a file. */
208 snprintf(tmpfile, sizeof(tmpfile), "/tmp/gvinum.XXXXXX");
210 if ((fd = mkstemp(tmpfile)) == -1) {
211 warn("temporary file not accessible");
214 if ((tmp = fdopen(fd, "w")) == NULL) {
215 warn("can't open '%s' for writing", tmpfile);
218 printconfig(tmp, "# ");
221 ed = getenv("EDITOR");
225 snprintf(commandline, sizeof(commandline), "%s %s", ed,
227 status = system(commandline);
229 warn("couldn't exec %s; status: %d", ed, status);
233 if ((tmp = fopen(tmpfile, "r")) == NULL) {
234 warn("can't open '%s' for reading", tmpfile);
239 req = gctl_get_handle();
240 gctl_ro_param(req, "class", -1, "VINUM");
241 gctl_ro_param(req, "verb", -1, "create");
242 gctl_ro_param(req, "flags", sizeof(int), &flags);
244 drives = volumes = plexes = subdisks = 0;
245 plex_in_volume = sd_in_plex = undeffd = 0;
249 while ((fgets(buf, BUFSIZ, tmp)) != NULL) {
251 /* Skip empty lines and comments. */
252 if (*buf == '\0' || *buf == '#') {
257 /* Kill off the newline. */
258 buf[strlen(buf) - 1] = '\0';
261 * Copy the original input line in case we need it for error
264 strlcpy(original, buf, sizeof(original));
266 tokens = gv_tokenize(buf, token, GV_MAXARGS);
272 /* Volume definition. */
273 if (!strcmp(token[0], "volume")) {
274 v = gv_new_volume(tokens, token);
276 warnx("line %d: invalid volume definition",
278 warnx("line %d: '%s'", line, original);
284 /* Reset plex count for this volume. */
288 * Set default volume name for following plex
291 strlcpy(volume, v->name, sizeof(volume));
293 snprintf(buf1, sizeof(buf1), "volume%d", volumes);
294 gctl_ro_param(req, buf1, sizeof(*v), v);
297 /* Plex definition. */
298 } else if (!strcmp(token[0], "plex")) {
299 p = gv_new_plex(tokens, token);
301 warnx("line %d: invalid plex definition", line);
302 warnx("line %d: '%s'", line, original);
308 /* Reset subdisk count for this plex. */
312 if (strlen(p->name) == 0) {
313 snprintf(p->name, sizeof(p->name), "%s.p%d",
314 volume, plex_in_volume++);
317 /* Default volume. */
318 if (strlen(p->volume) == 0) {
319 snprintf(p->volume, sizeof(p->volume), "%s",
324 * Set default plex name for following subdisk
327 strlcpy(plex, p->name, sizeof(plex));
329 snprintf(buf1, sizeof(buf1), "plex%d", plexes);
330 gctl_ro_param(req, buf1, sizeof(*p), p);
333 /* Subdisk definition. */
334 } else if (!strcmp(token[0], "sd")) {
335 s = gv_new_sd(tokens, token);
337 warnx("line %d: invalid subdisk "
338 "definition:", line);
339 warnx("line %d: '%s'", line, original);
346 if (strlen(s->name) == 0) {
347 if (strlen(plex) == 0) {
348 sdname = find_name("gvinumsubdisk.p",
349 GV_TYPE_SD, GV_MAXSDNAME);
350 snprintf(s->name, sizeof(s->name),
351 "%s.s%d", sdname, undeffd++);
354 snprintf(s->name, sizeof(s->name),
355 "%s.s%d",plex, sd_in_plex++);
360 if (strlen(s->plex) == 0)
361 snprintf(s->plex, sizeof(s->plex), "%s", plex);
363 snprintf(buf1, sizeof(buf1), "sd%d", subdisks);
364 gctl_ro_param(req, buf1, sizeof(*s), s);
367 /* Subdisk definition. */
368 } else if (!strcmp(token[0], "drive")) {
369 d = gv_new_drive(tokens, token);
371 warnx("line %d: invalid drive definition:",
373 warnx("line %d: '%s'", line, original);
379 snprintf(buf1, sizeof(buf1), "drive%d", drives);
380 gctl_ro_param(req, buf1, sizeof(*d), d);
383 /* Everything else is bogus. */
385 warnx("line %d: invalid definition:", line);
386 warnx("line %d: '%s'", line, original);
395 if (!errors && (volumes || plexes || subdisks || drives)) {
396 gctl_ro_param(req, "volumes", sizeof(int), &volumes);
397 gctl_ro_param(req, "plexes", sizeof(int), &plexes);
398 gctl_ro_param(req, "subdisks", sizeof(int), &subdisks);
399 gctl_ro_param(req, "drives", sizeof(int), &drives);
400 errstr = gctl_issue(req);
402 warnx("create failed: %s", errstr);
407 /* Create a concatenated volume. */
409 gvinum_concat(int argc, char * const *argv)
413 warnx("usage:\tconcat [-fv] [-n name] drives\n");
416 create_volume(argc, argv, "concat");
419 /* Create a drive quick and dirty. */
421 create_drive(const char *device)
424 struct gctl_req *req;
426 char *drivename, *dname;
427 int drives, i, flags, volumes, subdisks, plexes;
430 flags = plexes = subdisks = volumes = 0;
434 drivename = find_drive();
435 if (drivename == NULL)
438 req = gctl_get_handle();
439 gctl_ro_param(req, "class", -1, "VINUM");
440 gctl_ro_param(req, "verb", -1, "create");
441 d = gv_alloc_drive();
443 err(1, "unable to allocate for gv_drive object");
445 strlcpy(d->name, drivename, sizeof(d->name));
446 copy_device(d, device);
447 gctl_ro_param(req, "drive0", sizeof(*d), d);
448 gctl_ro_param(req, "flags", sizeof(int), &flags);
449 gctl_ro_param(req, "drives", sizeof(int), &drives);
450 gctl_ro_param(req, "volumes", sizeof(int), &volumes);
451 gctl_ro_param(req, "plexes", sizeof(int), &plexes);
452 gctl_ro_param(req, "subdisks", sizeof(int), &subdisks);
453 errstr = gctl_issue(req);
454 if (errstr != NULL) {
455 warnx("error creating drive: %s", errstr);
458 /* XXX: This is needed because we have to make sure the drives
459 * are created before we return. */
460 /* Loop until it's in the config. */
461 for (i = 0; i < 100000; i++) {
462 dname = find_name("gvinumdrive", GV_TYPE_DRIVE,
464 /* If we got a different name, quit. */
467 if (strcmp(dname, drivename))
473 usleep(100000); /* Sleep for 0.1s */
476 warnx("error creating drive");
485 * General routine for creating a volume. Mainly for use by concat, mirror,
486 * raid5 and stripe commands.
489 create_volume(int argc, char * const *argv, const char *verb)
491 struct gctl_req *req;
493 char buf[BUFSIZ], *drivename, *volname;
494 int drives, flags, i;
502 /* XXX: Should we check for argument length? */
504 req = gctl_get_handle();
505 gctl_ro_param(req, "class", -1, "VINUM");
507 for (i = 1; i < argc; i++) {
508 if (!strcmp(argv[i], "-f")) {
510 } else if (!strcmp(argv[i], "-n")) {
512 } else if (!strcmp(argv[i], "-v")) {
514 } else if (!strcmp(argv[i], "-s")) {
516 if (!strcmp(verb, "raid5"))
517 stripesize = gv_sizespec(argv[++i]);
519 /* Assume it's a drive. */
520 snprintf(buf, sizeof(buf), "drive%d", drives++);
522 /* First we create the drive. */
523 drivename = create_drive(argv[i]);
524 if (drivename == NULL)
526 /* Then we add it to the request. */
527 gctl_ro_param(req, buf, -1, drivename);
531 gctl_ro_param(req, "stripesize", sizeof(off_t), &stripesize);
533 /* Find a free volume name. */
535 volname = find_name("gvinumvolume", GV_TYPE_VOL, GV_MAXVOLNAME);
537 /* Then we send a request to actually create the volumes. */
538 gctl_ro_param(req, "verb", -1, verb);
539 gctl_ro_param(req, "flags", sizeof(int), &flags);
540 gctl_ro_param(req, "drives", sizeof(int), &drives);
541 gctl_ro_param(req, "name", -1, volname);
542 errstr = gctl_issue(req);
544 warnx("creating %s volume failed: %s", verb, errstr);
549 /* Parse a line of the config, return the word after <pattern>. */
551 find_pattern(char *line, const char *pattern)
555 ptr = strsep(&line, " ");
556 while (ptr != NULL) {
557 if (!strcmp(ptr, pattern)) {
558 /* Return the next. */
559 ptr = strsep(&line, " ");
562 ptr = strsep(&line, " ");
567 /* Find a free name for an object given a prefix. */
569 find_name(const char *prefix, int type, int namelen)
571 struct gctl_req *req;
572 char comment[1], buf[GV_CFG_LEN - 1], *sname, *ptr;
573 const char *errstr, *name;
574 int i, n, begin, len, conflict;
579 /* Find a name. Fetch out configuration first. */
580 req = gctl_get_handle();
581 gctl_ro_param(req, "class", -1, "VINUM");
582 gctl_ro_param(req, "verb", -1, "getconfig");
583 gctl_ro_param(req, "comment", -1, comment);
584 gctl_rw_param(req, "config", sizeof(buf), buf);
585 errstr = gctl_issue(req);
586 if (errstr != NULL) {
587 warnx("can't get configuration: %s", errstr);
595 sname = malloc(namelen + 1);
597 /* XXX: Max object setting? */
598 for (n = 0; n < 10000; n++) {
599 snprintf(sname, namelen, "%s%d", prefix, n);
602 /* Loop through the configuration line by line. */
603 for (i = 0; i < len; i++) {
604 if (buf[i] == '\n' || buf[i] == '\0') {
606 strlcpy(line, ptr, (i - begin) + 1);
610 name = find_pattern(line, "drive");
613 name = find_pattern(line, "volume");
617 name = find_pattern(line, "name");
620 printf("Invalid type given\n");
625 if (!strcmp(sname, name)) {
627 /* XXX: Could quit the loop earlier. */
639 copy_device(struct gv_drive *d, const char *device)
642 if (strncmp(device, "/dev/", 5) == 0)
643 strlcpy(d->device, (device + 5), sizeof(d->device));
645 strlcpy(d->device, device, sizeof(d->device));
648 /* Detach a plex or subdisk from its parent. */
650 gvinum_detach(int argc, char * const *argv)
653 struct gctl_req *req;
659 while ((i = getopt(argc, argv, "f")) != -1) {
665 warn("invalid flag: %c", i);
672 warnx("usage: detach [-f] <subdisk> | <plex>");
676 req = gctl_get_handle();
677 gctl_ro_param(req, "class", -1, "VINUM");
678 gctl_ro_param(req, "verb", -1, "detach");
679 gctl_ro_param(req, "object", -1, argv[0]);
680 gctl_ro_param(req, "flags", sizeof(int), &flags);
682 errstr = gctl_issue(req);
684 warnx("detach failed: %s", errstr);
693 "checkparity [-f] plex\n"
694 " Check the parity blocks of a RAID-5 plex.\n"
695 "create [-f] description-file\n"
696 " Create as per description-file or open editor.\n"
697 "attach plex volume [rename]\n"
698 "attach subdisk plex [offset] [rename]\n"
699 " Attach a plex to a volume, or a subdisk to a plex\n"
700 "concat [-fv] [-n name] drives\n"
701 " Create a concatenated volume from the specified drives.\n"
702 "detach [-f] [plex | subdisk]\n"
703 " Detach a plex or a subdisk from the volume or plex to\n"
704 " which it is attached.\n"
706 " Grow plex by creating a properly sized subdisk on drive\n"
707 "l | list [-r] [-v] [-V] [volume | plex | subdisk]\n"
708 " List information about specified objects.\n"
709 "ld [-r] [-v] [-V] [volume]\n"
710 " List information about drives.\n"
711 "ls [-r] [-v] [-V] [subdisk]\n"
712 " List information about subdisks.\n"
713 "lp [-r] [-v] [-V] [plex]\n"
714 " List information about plexes.\n"
715 "lv [-r] [-v] [-V] [volume]\n"
716 " List information about volumes.\n"
717 "mirror [-fsv] [-n name] drives\n"
718 " Create a mirrored volume from the specified drives.\n"
719 "move | mv -f drive object ...\n"
720 " Move the object(s) to the specified drive.\n"
721 "quit Exit the vinum program when running in interactive mode."
723 " mally this would be done by entering the EOF character.\n"
724 "raid5 [-fv] [-s stripesize] [-n name] drives\n"
725 " Create a RAID-5 volume from the specified drives.\n"
726 "rename [-r] [drive | subdisk | plex | volume] newname\n"
727 " Change the name of the specified object.\n"
728 "rebuildparity plex [-f]\n"
729 " Rebuild the parity blocks of a RAID-5 plex.\n"
731 " Reset the complete gvinum configuration\n"
732 "rm [-r] [-f] volume | plex | subdisk | drive\n"
733 " Remove an object.\n"
735 " Save vinum configuration to disk after configuration"
737 "setstate [-f] state [volume | plex | subdisk | drive]\n"
738 " Set state without influencing other objects, for"
741 "start [-S size] volume | plex | subdisk\n"
742 " Allow the system to access the objects.\n"
743 "stripe [-fv] [-n name] drives\n"
744 " Create a striped volume from the specified drives.\n"
749 gvinum_setstate(int argc, char * const *argv)
751 struct gctl_req *req;
760 while ((i = getopt(argc, argv, "f")) != -1) {
767 warn("invalid flag: %c", i);
776 warnx("usage: setstate [-f] <state> <obj>");
781 * XXX: This hack is needed to avoid tripping over (now) invalid
782 * 'classic' vinum states and will go away later.
784 if (strcmp(argv[0], "up") && strcmp(argv[0], "down") &&
785 strcmp(argv[0], "stale")) {
786 warnx("invalid state '%s'", argv[0]);
790 req = gctl_get_handle();
791 gctl_ro_param(req, "class", -1, "VINUM");
792 gctl_ro_param(req, "verb", -1, "setstate");
793 gctl_ro_param(req, "state", -1, argv[0]);
794 gctl_ro_param(req, "object", -1, argv[1]);
795 gctl_ro_param(req, "flags", sizeof(int), &flags);
797 errstr = gctl_issue(req);
804 gvinum_list(int argc, char * const *argv)
806 struct gctl_req *req;
809 char buf[20], *cmd, config[GV_CFG_LEN + 1];
818 while ((j = getopt(argc, argv, "rsvV")) != -1) {
843 req = gctl_get_handle();
844 gctl_ro_param(req, "class", -1, "VINUM");
845 gctl_ro_param(req, "verb", -1, "list");
846 gctl_ro_param(req, "cmd", -1, cmd);
847 gctl_ro_param(req, "argc", sizeof(int), &argc);
848 gctl_ro_param(req, "flags", sizeof(int), &flags);
849 gctl_rw_param(req, "config", sizeof(config), config);
851 for (i = 0; i < argc; i++) {
852 snprintf(buf, sizeof(buf), "argv%d", i);
853 gctl_ro_param(req, buf, -1, argv[i]);
856 errstr = gctl_issue(req);
857 if (errstr != NULL) {
858 warnx("can't get configuration: %s", errstr);
863 printf("%s", config);
867 /* Create a mirrored volume. */
869 gvinum_mirror(int argc, char * const *argv)
873 warnx("usage\tmirror [-fsv] [-n name] drives\n");
876 create_volume(argc, argv, "mirror");
879 /* Note that move is currently of form '[-r] target object [...]' */
881 gvinum_move(int argc, char * const *argv)
883 struct gctl_req *req;
892 while ((j = getopt(argc, argv, "f")) != -1) {
908 warnx("no destination or object(s) to move specified");
911 warnx("no object(s) to move specified");
917 req = gctl_get_handle();
918 gctl_ro_param(req, "class", -1, "VINUM");
919 gctl_ro_param(req, "verb", -1, "move");
920 gctl_ro_param(req, "argc", sizeof(int), &argc);
921 gctl_ro_param(req, "flags", sizeof(int), &flags);
922 gctl_ro_param(req, "destination", -1, argv[0]);
923 for (i = 1; i < argc; i++) {
924 snprintf(buf, sizeof(buf), "argv%d", i);
925 gctl_ro_param(req, buf, -1, argv[i]);
927 errstr = gctl_issue(req);
929 warnx("can't move object(s): %s", errstr);
934 gvinum_printconfig(int argc, char * const *argv)
937 printconfig(stdout, "");
941 gvinum_parityop(int argc, char * const *argv, int rebuild)
943 struct gctl_req *req;
949 op = "rebuildparity";
957 while ((i = getopt(argc, argv, "fv")) != -1) {
966 warnx("invalid flag '%c'", i);
974 warn("usage: %s [-f] [-v] <plex>", op);
978 req = gctl_get_handle();
979 gctl_ro_param(req, "class", -1, "VINUM");
980 gctl_ro_param(req, "verb", -1, op);
981 gctl_ro_param(req, "rebuild", sizeof(int), &rebuild);
982 gctl_ro_param(req, "flags", sizeof(int), &flags);
983 gctl_ro_param(req, "plex", -1, argv[0]);
985 errstr = gctl_issue(req);
987 warnx("%s\n", errstr);
991 /* Create a RAID-5 volume. */
993 gvinum_raid5(int argc, char * const *argv)
997 warnx("usage:\traid5 [-fv] [-s stripesize] [-n name] drives\n");
1000 create_volume(argc, argv, "raid5");
1004 gvinum_rename(int argc, char * const *argv)
1006 struct gctl_req *req;
1015 while ((j = getopt(argc, argv, "r")) != -1) {
1030 warnx("no object to rename specified");
1033 warnx("no new name specified");
1038 warnx("more than one new name specified");
1042 req = gctl_get_handle();
1043 gctl_ro_param(req, "class", -1, "VINUM");
1044 gctl_ro_param(req, "verb", -1, "rename");
1045 gctl_ro_param(req, "flags", sizeof(int), &flags);
1046 gctl_ro_param(req, "object", -1, argv[0]);
1047 gctl_ro_param(req, "newname", -1, argv[1]);
1048 errstr = gctl_issue(req);
1050 warnx("can't rename object: %s", errstr);
1055 gvinum_rm(int argc, char * const *argv)
1057 struct gctl_req *req;
1065 while ((j = getopt(argc, argv, "rf")) != -1) {
1080 req = gctl_get_handle();
1081 gctl_ro_param(req, "class", -1, "VINUM");
1082 gctl_ro_param(req, "verb", -1, "remove");
1083 gctl_ro_param(req, "argc", sizeof(int), &argc);
1084 gctl_ro_param(req, "flags", sizeof(int), &flags);
1086 for (i = 0; i < argc; i++) {
1087 snprintf(buf, sizeof(buf), "argv%d", i);
1088 gctl_ro_param(req, buf, -1, argv[i]);
1091 errstr = gctl_issue(req);
1092 if (errstr != NULL) {
1093 warnx("can't remove: %s", errstr);
1101 gvinum_resetconfig(int argc, char * const *argv)
1103 struct gctl_req *req;
1109 while ((i = getopt(argc, argv, "f")) != -1) {
1115 warn("invalid flag: %c", i);
1119 if ((flags & GV_FLAG_F) == 0) {
1120 if (!isatty(STDIN_FILENO)) {
1121 warn("Please enter this command from a tty device\n");
1124 printf(" WARNING! This command will completely wipe out"
1125 " your gvinum configuration.\n"
1126 " All data will be lost. If you really want to do this,"
1127 " enter the text\n\n"
1130 fgets(reply, sizeof(reply), stdin);
1131 if (strcmp(reply, "NO FUTURE\n")) {
1132 printf("\n No change\n");
1136 req = gctl_get_handle();
1137 gctl_ro_param(req, "class", -1, "VINUM");
1138 gctl_ro_param(req, "verb", -1, "resetconfig");
1139 errstr = gctl_issue(req);
1140 if (errstr != NULL) {
1141 warnx("can't reset config: %s", errstr);
1146 printf("gvinum configuration obliterated\n");
1150 gvinum_saveconfig(void)
1152 struct gctl_req *req;
1155 req = gctl_get_handle();
1156 gctl_ro_param(req, "class", -1, "VINUM");
1157 gctl_ro_param(req, "verb", -1, "saveconfig");
1158 errstr = gctl_issue(req);
1160 warnx("can't save configuration: %s", errstr);
1165 gvinum_start(int argc, char * const *argv)
1167 struct gctl_req *req;
1172 /* 'start' with no arguments is a no-op. */
1180 while ((j = getopt(argc, argv, "S")) != -1) {
1183 initsize = atoi(optarg);
1195 req = gctl_get_handle();
1196 gctl_ro_param(req, "class", -1, "VINUM");
1197 gctl_ro_param(req, "verb", -1, "start");
1198 gctl_ro_param(req, "argc", sizeof(int), &argc);
1199 gctl_ro_param(req, "initsize", sizeof(int), &initsize);
1201 for (i = 0; i < argc; i++) {
1202 snprintf(buf, sizeof(buf), "argv%d", i);
1203 gctl_ro_param(req, buf, -1, argv[i]);
1206 errstr = gctl_issue(req);
1207 if (errstr != NULL) {
1208 warnx("can't start: %s", errstr);
1217 gvinum_stop(int argc, char * const *argv)
1221 fileid = kldfind(GVINUMKLD);
1223 if (modfind(GVINUMMOD) < 0)
1224 warn("cannot find " GVINUMKLD);
1229 * This little hack prevents that we end up in an infinite loop in
1230 * g_unload_class(). gv_unload() will return EAGAIN so that the GEOM
1231 * event thread will be free for the g_wither_geom() call from
1232 * gv_unload(). It's silly, but it works.
1234 printf("unloading " GVINUMKLD " kernel module... ");
1236 if ((err = kldunload(fileid)) != 0 && (errno == EAGAIN)) {
1238 err = kldunload(fileid);
1241 printf(" failed!\n");
1242 warn("cannot unload " GVINUMKLD);
1250 /* Create a striped volume. */
1252 gvinum_stripe(int argc, char * const *argv)
1256 warnx("usage:\tstripe [-fv] [-n name] drives\n");
1259 create_volume(argc, argv, "stripe");
1262 /* Grow a subdisk by adding disk backed by provider. */
1264 gvinum_grow(int argc, char * const *argv)
1266 struct gctl_req *req;
1267 char *drive, *sdname;
1268 char sdprefix[GV_MAXSDNAME];
1272 int drives, volumes, plexes, subdisks, flags;
1275 drives = volumes = plexes = subdisks = 0;
1277 warnx("usage:\tgrow plex drive\n");
1283 warn("unable to create subdisk");
1286 d = gv_alloc_drive();
1288 warn("unable to create drive");
1292 /* Lookup device and set an appropriate drive name. */
1293 drive = find_drive();
1294 if (drive == NULL) {
1295 warn("unable to find an appropriate drive name");
1300 strlcpy(d->name, drive, sizeof(d->name));
1301 copy_device(d, argv[2]);
1305 /* We try to use the plex name as basis for the subdisk name. */
1306 snprintf(sdprefix, sizeof(sdprefix), "%s.s", argv[1]);
1307 sdname = find_name(sdprefix, GV_TYPE_SD, GV_MAXSDNAME);
1308 if (sdname == NULL) {
1309 warn("unable to find an appropriate subdisk name");
1315 strlcpy(s->name, sdname, sizeof(s->name));
1317 strlcpy(s->plex, argv[1], sizeof(s->plex));
1318 strlcpy(s->drive, d->name, sizeof(s->drive));
1321 req = gctl_get_handle();
1322 gctl_ro_param(req, "class", -1, "VINUM");
1323 gctl_ro_param(req, "verb", -1, "create");
1324 gctl_ro_param(req, "flags", sizeof(int), &flags);
1325 gctl_ro_param(req, "volumes", sizeof(int), &volumes);
1326 gctl_ro_param(req, "plexes", sizeof(int), &plexes);
1327 gctl_ro_param(req, "subdisks", sizeof(int), &subdisks);
1328 gctl_ro_param(req, "drives", sizeof(int), &drives);
1329 gctl_ro_param(req, "drive0", sizeof(*d), d);
1330 gctl_ro_param(req, "sd0", sizeof(*s), s);
1331 errstr = gctl_issue(req);
1333 if (errstr != NULL) {
1334 warnx("unable to grow plex: %s", errstr);
1343 parseline(int argc, char * const *argv)
1349 if (!strcmp(argv[0], "create"))
1350 gvinum_create(argc, argv);
1351 else if (!strcmp(argv[0], "exit") || !strcmp(argv[0], "quit"))
1353 else if (!strcmp(argv[0], "attach"))
1354 gvinum_attach(argc, argv);
1355 else if (!strcmp(argv[0], "detach"))
1356 gvinum_detach(argc, argv);
1357 else if (!strcmp(argv[0], "concat"))
1358 gvinum_concat(argc, argv);
1359 else if (!strcmp(argv[0], "grow"))
1360 gvinum_grow(argc, argv);
1361 else if (!strcmp(argv[0], "help"))
1363 else if (!strcmp(argv[0], "list") || !strcmp(argv[0], "l"))
1364 gvinum_list(argc, argv);
1365 else if (!strcmp(argv[0], "ld"))
1366 gvinum_list(argc, argv);
1367 else if (!strcmp(argv[0], "lp"))
1368 gvinum_list(argc, argv);
1369 else if (!strcmp(argv[0], "ls"))
1370 gvinum_list(argc, argv);
1371 else if (!strcmp(argv[0], "lv"))
1372 gvinum_list(argc, argv);
1373 else if (!strcmp(argv[0], "mirror"))
1374 gvinum_mirror(argc, argv);
1375 else if (!strcmp(argv[0], "move"))
1376 gvinum_move(argc, argv);
1377 else if (!strcmp(argv[0], "mv"))
1378 gvinum_move(argc, argv);
1379 else if (!strcmp(argv[0], "printconfig"))
1380 gvinum_printconfig(argc, argv);
1381 else if (!strcmp(argv[0], "raid5"))
1382 gvinum_raid5(argc, argv);
1383 else if (!strcmp(argv[0], "rename"))
1384 gvinum_rename(argc, argv);
1385 else if (!strcmp(argv[0], "resetconfig"))
1386 gvinum_resetconfig(argc, argv);
1387 else if (!strcmp(argv[0], "rm"))
1388 gvinum_rm(argc, argv);
1389 else if (!strcmp(argv[0], "saveconfig"))
1390 gvinum_saveconfig();
1391 else if (!strcmp(argv[0], "setstate"))
1392 gvinum_setstate(argc, argv);
1393 else if (!strcmp(argv[0], "start"))
1394 gvinum_start(argc, argv);
1395 else if (!strcmp(argv[0], "stop"))
1396 gvinum_stop(argc, argv);
1397 else if (!strcmp(argv[0], "stripe"))
1398 gvinum_stripe(argc, argv);
1399 else if (!strcmp(argv[0], "checkparity"))
1400 gvinum_parityop(argc, argv, 0);
1401 else if (!strcmp(argv[0], "rebuildparity"))
1402 gvinum_parityop(argc, argv, 1);
1404 printf("unknown command '%s'\n", argv[0]);
1408 * The guts of printconfig. This is called from gvinum_printconfig and from
1409 * gvinum_create when called without an argument, in order to give the user
1410 * something to edit.
1413 printconfig(FILE *of, const char *comment)
1415 struct gctl_req *req;
1416 struct utsname uname_s;
1419 char buf[GV_CFG_LEN + 1];
1424 req = gctl_get_handle();
1425 gctl_ro_param(req, "class", -1, "VINUM");
1426 gctl_ro_param(req, "verb", -1, "getconfig");
1427 gctl_ro_param(req, "comment", -1, comment);
1428 gctl_rw_param(req, "config", sizeof(buf), buf);
1429 errstr = gctl_issue(req);
1430 if (errstr != NULL) {
1431 warnx("can't get configuration: %s", errstr);
1436 fprintf(of, "# Vinum configuration of %s, saved at %s",
1440 if (*comment != '\0')
1441 fprintf(of, "# Current configuration:\n");
1443 fprintf(of, "%s", buf);