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 tokens = gv_tokenize(buffer, token, GV_MAXARGS);
129 parseline(tokens, token);
136 /* Attach a plex to a volume or a subdisk to a plex. */
138 gvinum_attach(int argc, char * const *argv)
140 struct gctl_req *req;
148 warnx("usage:\tattach <subdisk> <plex> [rename] "
150 "\tattach <plex> <volume> [rename]");
154 if (!strcmp(argv[3], "rename")) {
157 offset = strtol(argv[4], NULL, 0);
159 offset = strtol(argv[3], NULL, 0);
161 req = gctl_get_handle();
162 gctl_ro_param(req, "class", -1, "VINUM");
163 gctl_ro_param(req, "verb", -1, "attach");
164 gctl_ro_param(req, "child", -1, argv[1]);
165 gctl_ro_param(req, "parent", -1, argv[2]);
166 gctl_ro_param(req, "offset", sizeof(off_t), &offset);
167 gctl_ro_param(req, "rename", sizeof(int), &rename);
168 errstr = gctl_issue(req);
170 warnx("attach failed: %s", errstr);
175 gvinum_create(int argc, char * const *argv)
177 struct gctl_req *req;
183 int drives, errors, fd, flags, i, line, plexes, plex_in_volume;
184 int sd_in_plex, status, subdisks, tokens, undeffd, volumes;
186 char buf[BUFSIZ], buf1[BUFSIZ], commandline[BUFSIZ], *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], config[GV_CFG_LEN + 1];
819 while ((j = getopt(argc, argv, "rsvV")) != -1) {
844 req = gctl_get_handle();
845 gctl_ro_param(req, "class", -1, "VINUM");
846 gctl_ro_param(req, "verb", -1, "list");
847 gctl_ro_param(req, "cmd", -1, cmd);
848 gctl_ro_param(req, "argc", sizeof(int), &argc);
849 gctl_ro_param(req, "flags", sizeof(int), &flags);
850 gctl_rw_param(req, "config", sizeof(config), config);
852 for (i = 0; i < argc; i++) {
853 snprintf(buf, sizeof(buf), "argv%d", i);
854 gctl_ro_param(req, buf, -1, argv[i]);
857 errstr = gctl_issue(req);
858 if (errstr != NULL) {
859 warnx("can't get configuration: %s", errstr);
864 printf("%s", config);
868 /* Create a mirrored volume. */
870 gvinum_mirror(int argc, char * const *argv)
874 warnx("usage\tmirror [-fsv] [-n name] drives\n");
877 create_volume(argc, argv, "mirror");
880 /* Note that move is currently of form '[-r] target object [...]' */
882 gvinum_move(int argc, char * const *argv)
884 struct gctl_req *req;
893 while ((j = getopt(argc, argv, "f")) != -1) {
909 warnx("no destination or object(s) to move specified");
912 warnx("no object(s) to move specified");
918 req = gctl_get_handle();
919 gctl_ro_param(req, "class", -1, "VINUM");
920 gctl_ro_param(req, "verb", -1, "move");
921 gctl_ro_param(req, "argc", sizeof(int), &argc);
922 gctl_ro_param(req, "flags", sizeof(int), &flags);
923 gctl_ro_param(req, "destination", -1, argv[0]);
924 for (i = 1; i < argc; i++) {
925 snprintf(buf, sizeof(buf), "argv%d", i);
926 gctl_ro_param(req, buf, -1, argv[i]);
928 errstr = gctl_issue(req);
930 warnx("can't move object(s): %s", errstr);
935 gvinum_printconfig(int argc __unused, char * const *argv __unused)
938 printconfig(stdout, "");
942 gvinum_parityop(int argc, char * const *argv, int rebuild)
944 struct gctl_req *req;
950 op = "rebuildparity";
958 while ((i = getopt(argc, argv, "fv")) != -1) {
967 warnx("invalid flag '%c'", i);
975 warn("usage: %s [-f] [-v] <plex>", op);
979 req = gctl_get_handle();
980 gctl_ro_param(req, "class", -1, "VINUM");
981 gctl_ro_param(req, "verb", -1, op);
982 gctl_ro_param(req, "rebuild", sizeof(int), &rebuild);
983 gctl_ro_param(req, "flags", sizeof(int), &flags);
984 gctl_ro_param(req, "plex", -1, argv[0]);
986 errstr = gctl_issue(req);
988 warnx("%s\n", errstr);
992 /* Create a RAID-5 volume. */
994 gvinum_raid5(int argc, char * const *argv)
998 warnx("usage:\traid5 [-fv] [-s stripesize] [-n name] drives\n");
1001 create_volume(argc, argv, "raid5");
1005 gvinum_rename(int argc, char * const *argv)
1007 struct gctl_req *req;
1016 while ((j = getopt(argc, argv, "r")) != -1) {
1031 warnx("no object to rename specified");
1034 warnx("no new name specified");
1039 warnx("more than one new name specified");
1043 req = gctl_get_handle();
1044 gctl_ro_param(req, "class", -1, "VINUM");
1045 gctl_ro_param(req, "verb", -1, "rename");
1046 gctl_ro_param(req, "flags", sizeof(int), &flags);
1047 gctl_ro_param(req, "object", -1, argv[0]);
1048 gctl_ro_param(req, "newname", -1, argv[1]);
1049 errstr = gctl_issue(req);
1051 warnx("can't rename object: %s", errstr);
1056 gvinum_rm(int argc, char * const *argv)
1058 struct gctl_req *req;
1066 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(int argc, char * const *argv)
1104 struct gctl_req *req;
1110 while ((i = getopt(argc, argv, "f")) != -1) {
1116 warn("invalid flag: %c", i);
1120 if ((flags & GV_FLAG_F) == 0) {
1121 if (!isatty(STDIN_FILENO)) {
1122 warn("Please enter this command from a tty device\n");
1125 printf(" WARNING! This command will completely wipe out"
1126 " your gvinum configuration.\n"
1127 " All data will be lost. If you really want to do this,"
1128 " enter the text\n\n"
1131 fgets(reply, sizeof(reply), stdin);
1132 if (strcmp(reply, "NO FUTURE\n")) {
1133 printf("\n No change\n");
1137 req = gctl_get_handle();
1138 gctl_ro_param(req, "class", -1, "VINUM");
1139 gctl_ro_param(req, "verb", -1, "resetconfig");
1140 errstr = gctl_issue(req);
1141 if (errstr != NULL) {
1142 warnx("can't reset config: %s", errstr);
1147 printf("gvinum configuration obliterated\n");
1151 gvinum_saveconfig(void)
1153 struct gctl_req *req;
1156 req = gctl_get_handle();
1157 gctl_ro_param(req, "class", -1, "VINUM");
1158 gctl_ro_param(req, "verb", -1, "saveconfig");
1159 errstr = gctl_issue(req);
1161 warnx("can't save configuration: %s", errstr);
1166 gvinum_start(int argc, char * const *argv)
1168 struct gctl_req *req;
1173 /* 'start' with no arguments is a no-op. */
1181 while ((j = getopt(argc, argv, "S")) != -1) {
1184 initsize = atoi(optarg);
1196 req = gctl_get_handle();
1197 gctl_ro_param(req, "class", -1, "VINUM");
1198 gctl_ro_param(req, "verb", -1, "start");
1199 gctl_ro_param(req, "argc", sizeof(int), &argc);
1200 gctl_ro_param(req, "initsize", sizeof(int), &initsize);
1202 for (i = 0; i < argc; i++) {
1203 snprintf(buf, sizeof(buf), "argv%d", i);
1204 gctl_ro_param(req, buf, -1, argv[i]);
1207 errstr = gctl_issue(req);
1208 if (errstr != NULL) {
1209 warnx("can't start: %s", errstr);
1218 gvinum_stop(int argc __unused, char * const *argv __unused)
1222 fileid = kldfind(GVINUMKLD);
1224 if (modfind(GVINUMMOD) < 0)
1225 warn("cannot find " GVINUMKLD);
1230 * This little hack prevents that we end up in an infinite loop in
1231 * g_unload_class(). gv_unload() will return EAGAIN so that the GEOM
1232 * event thread will be free for the g_wither_geom() call from
1233 * gv_unload(). It's silly, but it works.
1235 printf("unloading " GVINUMKLD " kernel module... ");
1237 if ((err = kldunload(fileid)) != 0 && (errno == EAGAIN)) {
1239 err = kldunload(fileid);
1242 printf(" failed!\n");
1243 warn("cannot unload " GVINUMKLD);
1251 /* Create a striped volume. */
1253 gvinum_stripe(int argc, char * const *argv)
1257 warnx("usage:\tstripe [-fv] [-n name] drives\n");
1260 create_volume(argc, argv, "stripe");
1263 /* Grow a subdisk by adding disk backed by provider. */
1265 gvinum_grow(int argc, char * const *argv)
1267 struct gctl_req *req;
1268 char *drive, *sdname;
1269 char sdprefix[GV_MAXSDNAME];
1273 int drives, volumes, plexes, subdisks, flags;
1276 drives = volumes = plexes = subdisks = 0;
1278 warnx("usage:\tgrow plex drive\n");
1284 warn("unable to create subdisk");
1287 d = gv_alloc_drive();
1289 warn("unable to create drive");
1293 /* Lookup device and set an appropriate drive name. */
1294 drive = find_drive();
1295 if (drive == NULL) {
1296 warn("unable to find an appropriate drive name");
1301 strlcpy(d->name, drive, sizeof(d->name));
1302 copy_device(d, argv[2]);
1306 /* We try to use the plex name as basis for the subdisk name. */
1307 snprintf(sdprefix, sizeof(sdprefix), "%s.s", argv[1]);
1308 sdname = find_name(sdprefix, GV_TYPE_SD, GV_MAXSDNAME);
1309 if (sdname == NULL) {
1310 warn("unable to find an appropriate subdisk name");
1316 strlcpy(s->name, sdname, sizeof(s->name));
1318 strlcpy(s->plex, argv[1], sizeof(s->plex));
1319 strlcpy(s->drive, d->name, sizeof(s->drive));
1322 req = gctl_get_handle();
1323 gctl_ro_param(req, "class", -1, "VINUM");
1324 gctl_ro_param(req, "verb", -1, "create");
1325 gctl_ro_param(req, "flags", sizeof(int), &flags);
1326 gctl_ro_param(req, "volumes", sizeof(int), &volumes);
1327 gctl_ro_param(req, "plexes", sizeof(int), &plexes);
1328 gctl_ro_param(req, "subdisks", sizeof(int), &subdisks);
1329 gctl_ro_param(req, "drives", sizeof(int), &drives);
1330 gctl_ro_param(req, "drive0", sizeof(*d), d);
1331 gctl_ro_param(req, "sd0", sizeof(*s), s);
1332 errstr = gctl_issue(req);
1334 if (errstr != NULL) {
1335 warnx("unable to grow plex: %s", errstr);
1344 parseline(int argc, char * const *argv)
1350 if (!strcmp(argv[0], "create"))
1351 gvinum_create(argc, argv);
1352 else if (!strcmp(argv[0], "exit") || !strcmp(argv[0], "quit"))
1354 else if (!strcmp(argv[0], "attach"))
1355 gvinum_attach(argc, argv);
1356 else if (!strcmp(argv[0], "detach"))
1357 gvinum_detach(argc, argv);
1358 else if (!strcmp(argv[0], "concat"))
1359 gvinum_concat(argc, argv);
1360 else if (!strcmp(argv[0], "grow"))
1361 gvinum_grow(argc, argv);
1362 else if (!strcmp(argv[0], "help"))
1364 else if (!strcmp(argv[0], "list") || !strcmp(argv[0], "l"))
1365 gvinum_list(argc, argv);
1366 else if (!strcmp(argv[0], "ld"))
1367 gvinum_list(argc, argv);
1368 else if (!strcmp(argv[0], "lp"))
1369 gvinum_list(argc, argv);
1370 else if (!strcmp(argv[0], "ls"))
1371 gvinum_list(argc, argv);
1372 else if (!strcmp(argv[0], "lv"))
1373 gvinum_list(argc, argv);
1374 else if (!strcmp(argv[0], "mirror"))
1375 gvinum_mirror(argc, argv);
1376 else if (!strcmp(argv[0], "move"))
1377 gvinum_move(argc, argv);
1378 else if (!strcmp(argv[0], "mv"))
1379 gvinum_move(argc, argv);
1380 else if (!strcmp(argv[0], "printconfig"))
1381 gvinum_printconfig(argc, argv);
1382 else if (!strcmp(argv[0], "raid5"))
1383 gvinum_raid5(argc, argv);
1384 else if (!strcmp(argv[0], "rename"))
1385 gvinum_rename(argc, argv);
1386 else if (!strcmp(argv[0], "resetconfig"))
1387 gvinum_resetconfig(argc, argv);
1388 else if (!strcmp(argv[0], "rm"))
1389 gvinum_rm(argc, argv);
1390 else if (!strcmp(argv[0], "saveconfig"))
1391 gvinum_saveconfig();
1392 else if (!strcmp(argv[0], "setstate"))
1393 gvinum_setstate(argc, argv);
1394 else if (!strcmp(argv[0], "start"))
1395 gvinum_start(argc, argv);
1396 else if (!strcmp(argv[0], "stop"))
1397 gvinum_stop(argc, argv);
1398 else if (!strcmp(argv[0], "stripe"))
1399 gvinum_stripe(argc, argv);
1400 else if (!strcmp(argv[0], "checkparity"))
1401 gvinum_parityop(argc, argv, 0);
1402 else if (!strcmp(argv[0], "rebuildparity"))
1403 gvinum_parityop(argc, argv, 1);
1405 printf("unknown command '%s'\n", argv[0]);
1409 * The guts of printconfig. This is called from gvinum_printconfig and from
1410 * gvinum_create when called without an argument, in order to give the user
1411 * something to edit.
1414 printconfig(FILE *of, const char *comment)
1416 struct gctl_req *req;
1417 struct utsname uname_s;
1420 char buf[GV_CFG_LEN + 1];
1425 req = gctl_get_handle();
1426 gctl_ro_param(req, "class", -1, "VINUM");
1427 gctl_ro_param(req, "verb", -1, "getconfig");
1428 gctl_ro_param(req, "comment", -1, comment);
1429 gctl_rw_param(req, "config", sizeof(buf), buf);
1430 errstr = gctl_issue(req);
1431 if (errstr != NULL) {
1432 warnx("can't get configuration: %s", errstr);
1437 fprintf(of, "# Vinum configuration of %s, saved at %s",
1441 if (*comment != '\0')
1442 fprintf(of, "# Current configuration:\n");
1444 fprintf(of, "%s", buf);