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(int, char **);
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 (modfind(GVINUMMOD) < 0) {
99 if (kldload(GVINUMKLD) < 0 && modfind(GVINUMMOD) < 0)
100 err(1, GVINUMKLD ": Kernel module not available");
103 /* Arguments given on the command line. */
107 parseline(argc, argv);
109 /* Interactive mode. */
112 inputline = readline("gvinum -> ");
113 if (inputline == NULL) {
115 err(1, "can't read input");
120 } else if (*inputline) {
121 add_history(inputline);
122 strcpy(buffer, inputline);
124 line++; /* count the lines */
125 tokens = gv_tokenize(buffer, token, GV_MAXARGS);
127 parseline(tokens, token);
134 /* Attach a plex to a volume or a subdisk to a plex. */
136 gvinum_attach(int argc, char **argv)
138 struct gctl_req *req;
146 warnx("usage:\tattach <subdisk> <plex> [rename] "
148 "\tattach <plex> <volume> [rename]");
152 if (!strcmp(argv[3], "rename")) {
155 offset = strtol(argv[4], NULL, 0);
157 offset = strtol(argv[3], NULL, 0);
159 req = gctl_get_handle();
160 gctl_ro_param(req, "class", -1, "VINUM");
161 gctl_ro_param(req, "verb", -1, "attach");
162 gctl_ro_param(req, "child", -1, argv[1]);
163 gctl_ro_param(req, "parent", -1, argv[2]);
164 gctl_ro_param(req, "offset", sizeof(off_t), &offset);
165 gctl_ro_param(req, "rename", sizeof(int), &rename);
166 errstr = gctl_issue(req);
168 warnx("attach failed: %s", errstr);
173 gvinum_create(int argc, char **argv)
175 struct gctl_req *req;
181 int drives, errors, fd, flags, i, line, plexes, plex_in_volume;
182 int sd_in_plex, status, subdisks, tokens, undeffd, volumes;
184 char buf[BUFSIZ], buf1[BUFSIZ], commandline[BUFSIZ], *ed, *sdname;
185 char original[BUFSIZ], tmpfile[20], *token[GV_MAXARGS];
186 char plex[GV_MAXPLEXNAME], volume[GV_MAXVOLNAME];
190 for (i = 1; i < argc; i++) {
191 /* Force flag used to ignore already created drives. */
192 if (!strcmp(argv[i], "-f")) {
194 /* Else it must be a file. */
196 if ((tmp = fopen(argv[i], "r")) == NULL) {
197 warn("can't open '%s' for reading", argv[i]);
203 /* We didn't get a file. */
205 snprintf(tmpfile, sizeof(tmpfile), "/tmp/gvinum.XXXXXX");
207 if ((fd = mkstemp(tmpfile)) == -1) {
208 warn("temporary file not accessible");
211 if ((tmp = fdopen(fd, "w")) == NULL) {
212 warn("can't open '%s' for writing", tmpfile);
215 printconfig(tmp, "# ");
218 ed = getenv("EDITOR");
222 snprintf(commandline, sizeof(commandline), "%s %s", ed,
224 status = system(commandline);
226 warn("couldn't exec %s; status: %d", ed, status);
230 if ((tmp = fopen(tmpfile, "r")) == NULL) {
231 warn("can't open '%s' for reading", tmpfile);
236 req = gctl_get_handle();
237 gctl_ro_param(req, "class", -1, "VINUM");
238 gctl_ro_param(req, "verb", -1, "create");
239 gctl_ro_param(req, "flags", sizeof(int), &flags);
241 drives = volumes = plexes = subdisks = 0;
242 plex_in_volume = sd_in_plex = undeffd = 0;
246 while ((fgets(buf, BUFSIZ, tmp)) != NULL) {
248 /* Skip empty lines and comments. */
249 if (*buf == '\0' || *buf == '#') {
254 /* Kill off the newline. */
255 buf[strlen(buf) - 1] = '\0';
258 * Copy the original input line in case we need it for error
261 strlcpy(original, buf, sizeof(original));
263 tokens = gv_tokenize(buf, token, GV_MAXARGS);
269 /* Volume definition. */
270 if (!strcmp(token[0], "volume")) {
271 v = gv_new_volume(tokens, token);
273 warnx("line %d: invalid volume definition",
275 warnx("line %d: '%s'", line, original);
281 /* Reset plex count for this volume. */
285 * Set default volume name for following plex
288 strlcpy(volume, v->name, sizeof(volume));
290 snprintf(buf1, sizeof(buf1), "volume%d", volumes);
291 gctl_ro_param(req, buf1, sizeof(*v), v);
294 /* Plex definition. */
295 } else if (!strcmp(token[0], "plex")) {
296 p = gv_new_plex(tokens, token);
298 warnx("line %d: invalid plex definition", line);
299 warnx("line %d: '%s'", line, original);
305 /* Reset subdisk count for this plex. */
309 if (strlen(p->name) == 0) {
310 snprintf(p->name, sizeof(p->name), "%s.p%d",
311 volume, plex_in_volume++);
314 /* Default volume. */
315 if (strlen(p->volume) == 0) {
316 snprintf(p->volume, sizeof(p->volume), "%s",
321 * Set default plex name for following subdisk
324 strlcpy(plex, p->name, sizeof(plex));
326 snprintf(buf1, sizeof(buf1), "plex%d", plexes);
327 gctl_ro_param(req, buf1, sizeof(*p), p);
330 /* Subdisk definition. */
331 } else if (!strcmp(token[0], "sd")) {
332 s = gv_new_sd(tokens, token);
334 warnx("line %d: invalid subdisk "
335 "definition:", line);
336 warnx("line %d: '%s'", line, original);
343 if (strlen(s->name) == 0) {
344 if (strlen(plex) == 0) {
345 sdname = find_name("gvinumsubdisk.p",
346 GV_TYPE_SD, GV_MAXSDNAME);
347 snprintf(s->name, sizeof(s->name),
348 "%s.s%d", sdname, undeffd++);
351 snprintf(s->name, sizeof(s->name),
352 "%s.s%d",plex, sd_in_plex++);
357 if (strlen(s->plex) == 0)
358 snprintf(s->plex, sizeof(s->plex), "%s", plex);
360 snprintf(buf1, sizeof(buf1), "sd%d", subdisks);
361 gctl_ro_param(req, buf1, sizeof(*s), s);
364 /* Subdisk definition. */
365 } else if (!strcmp(token[0], "drive")) {
366 d = gv_new_drive(tokens, token);
368 warnx("line %d: invalid drive definition:",
370 warnx("line %d: '%s'", line, original);
376 snprintf(buf1, sizeof(buf1), "drive%d", drives);
377 gctl_ro_param(req, buf1, sizeof(*d), d);
380 /* Everything else is bogus. */
382 warnx("line %d: invalid definition:", line);
383 warnx("line %d: '%s'", line, original);
392 if (!errors && (volumes || plexes || subdisks || drives)) {
393 gctl_ro_param(req, "volumes", sizeof(int), &volumes);
394 gctl_ro_param(req, "plexes", sizeof(int), &plexes);
395 gctl_ro_param(req, "subdisks", sizeof(int), &subdisks);
396 gctl_ro_param(req, "drives", sizeof(int), &drives);
397 errstr = gctl_issue(req);
399 warnx("create failed: %s", errstr);
404 /* Create a concatenated volume. */
406 gvinum_concat(int argc, char **argv)
410 warnx("usage:\tconcat [-fv] [-n name] drives\n");
413 create_volume(argc, argv, "concat");
417 /* Create a drive quick and dirty. */
419 create_drive(char *device)
422 struct gctl_req *req;
424 char *drivename, *dname;
425 int drives, i, flags, volumes, subdisks, plexes;
427 flags = plexes = subdisks = volumes = 0;
431 drivename = find_drive();
432 if (drivename == NULL)
435 req = gctl_get_handle();
436 gctl_ro_param(req, "class", -1, "VINUM");
437 gctl_ro_param(req, "verb", -1, "create");
438 d = gv_alloc_drive();
440 err(1, "unable to allocate for gv_drive object");
442 strlcpy(d->name, drivename, sizeof(d->name));
443 copy_device(d, device);
444 gctl_ro_param(req, "drive0", sizeof(*d), d);
445 gctl_ro_param(req, "flags", sizeof(int), &flags);
446 gctl_ro_param(req, "drives", sizeof(int), &drives);
447 gctl_ro_param(req, "volumes", sizeof(int), &volumes);
448 gctl_ro_param(req, "plexes", sizeof(int), &plexes);
449 gctl_ro_param(req, "subdisks", sizeof(int), &subdisks);
450 errstr = gctl_issue(req);
451 if (errstr != NULL) {
452 warnx("error creating drive: %s", errstr);
457 /* XXX: This is needed because we have to make sure the drives
458 * are created before we return. */
459 /* Loop until it's in the config. */
460 for (i = 0; i < 100000; i++) {
461 dname = find_name("gvinumdrive", GV_TYPE_DRIVE,
463 /* If we got a different name, quit. */
466 if (strcmp(dname, drivename)) {
472 usleep(100000); /* Sleep for 0.1s */
480 * General routine for creating a volume. Mainly for use by concat, mirror,
481 * raid5 and stripe commands.
484 create_volume(int argc, char **argv, char *verb)
486 struct gctl_req *req;
488 char buf[BUFSIZ], *drivename, *volname;
489 int drives, flags, i;
497 /* XXX: Should we check for argument length? */
499 req = gctl_get_handle();
500 gctl_ro_param(req, "class", -1, "VINUM");
502 for (i = 1; i < argc; i++) {
503 if (!strcmp(argv[i], "-f")) {
505 } else if (!strcmp(argv[i], "-n")) {
507 } else if (!strcmp(argv[i], "-v")) {
509 } else if (!strcmp(argv[i], "-s")) {
511 if (!strcmp(verb, "raid5"))
512 stripesize = gv_sizespec(argv[++i]);
514 /* Assume it's a drive. */
515 snprintf(buf, sizeof(buf), "drive%d", drives++);
517 /* First we create the drive. */
518 drivename = create_drive(argv[i]);
519 if (drivename == NULL)
521 /* Then we add it to the request. */
522 gctl_ro_param(req, buf, -1, drivename);
526 gctl_ro_param(req, "stripesize", sizeof(off_t), &stripesize);
528 /* Find a free volume name. */
530 volname = find_name("gvinumvolume", GV_TYPE_VOL, GV_MAXVOLNAME);
532 /* Then we send a request to actually create the volumes. */
533 gctl_ro_param(req, "verb", -1, verb);
534 gctl_ro_param(req, "flags", sizeof(int), &flags);
535 gctl_ro_param(req, "drives", sizeof(int), &drives);
536 gctl_ro_param(req, "name", -1, volname);
537 errstr = gctl_issue(req);
539 warnx("creating %s volume failed: %s", verb, errstr);
544 /* Parse a line of the config, return the word after <pattern>. */
546 find_pattern(char *line, char *pattern)
550 ptr = strsep(&line, " ");
551 while (ptr != NULL) {
552 if (!strcmp(ptr, pattern)) {
553 /* Return the next. */
554 ptr = strsep(&line, " ");
557 ptr = strsep(&line, " ");
562 /* Find a free name for an object given a prefix. */
564 find_name(const char *prefix, int type, int namelen)
566 struct gctl_req *req;
567 char comment[1], buf[GV_CFG_LEN - 1], *name, *sname, *ptr;
569 int i, n, begin, len, conflict;
574 /* Find a name. Fetch out configuration first. */
575 req = gctl_get_handle();
576 gctl_ro_param(req, "class", -1, "VINUM");
577 gctl_ro_param(req, "verb", -1, "getconfig");
578 gctl_ro_param(req, "comment", -1, comment);
579 gctl_rw_param(req, "config", sizeof(buf), buf);
580 errstr = gctl_issue(req);
581 if (errstr != NULL) {
582 warnx("can't get configuration: %s", errstr);
590 sname = malloc(namelen + 1);
592 /* XXX: Max object setting? */
593 for (n = 0; n < 10000; n++) {
594 snprintf(sname, namelen, "%s%d", prefix, n);
597 /* Loop through the configuration line by line. */
598 for (i = 0; i < len; i++) {
599 if (buf[i] == '\n' || buf[i] == '\0') {
601 strlcpy(line, ptr, (i - begin) + 1);
605 name = find_pattern(line, "drive");
608 name = find_pattern(line, "volume");
612 name = find_pattern(line, "name");
615 printf("Invalid type given\n");
620 if (!strcmp(sname, name)) {
622 /* XXX: Could quit the loop earlier. */
634 copy_device(struct gv_drive *d, const char *device)
636 if (strncmp(device, "/dev/", 5) == 0)
637 strlcpy(d->device, (device + 5), sizeof(d->device));
639 strlcpy(d->device, device, sizeof(d->device));
642 /* Detach a plex or subdisk from its parent. */
644 gvinum_detach(int argc, char **argv)
647 struct gctl_req *req;
653 while ((i = getopt(argc, argv, "f")) != -1) {
659 warn("invalid flag: %c", i);
666 warnx("usage: detach [-f] <subdisk> | <plex>");
670 req = gctl_get_handle();
671 gctl_ro_param(req, "class", -1, "VINUM");
672 gctl_ro_param(req, "verb", -1, "detach");
673 gctl_ro_param(req, "object", -1, argv[0]);
674 gctl_ro_param(req, "flags", sizeof(int), &flags);
676 errstr = gctl_issue(req);
678 warnx("detach failed: %s", errstr);
686 "checkparity [-f] plex\n"
687 " Check the parity blocks of a RAID-5 plex.\n"
688 "create [-f] description-file\n"
689 " Create as per description-file or open editor.\n"
690 "attach plex volume [rename]\n"
691 "attach subdisk plex [offset] [rename]\n"
692 " Attach a plex to a volume, or a subdisk to a plex\n"
693 "concat [-fv] [-n name] drives\n"
694 " Create a concatenated volume from the specified drives.\n"
695 "detach [-f] [plex | subdisk]\n"
696 " Detach a plex or a subdisk from the volume or plex to\n"
697 " which it is attached.\n"
699 " Grow plex by creating a properly sized subdisk on drive\n"
700 "l | list [-r] [-v] [-V] [volume | plex | subdisk]\n"
701 " List information about specified objects.\n"
702 "ld [-r] [-v] [-V] [volume]\n"
703 " List information about drives.\n"
704 "ls [-r] [-v] [-V] [subdisk]\n"
705 " List information about subdisks.\n"
706 "lp [-r] [-v] [-V] [plex]\n"
707 " List information about plexes.\n"
708 "lv [-r] [-v] [-V] [volume]\n"
709 " List information about volumes.\n"
710 "mirror [-fsv] [-n name] drives\n"
711 " Create a mirrored volume from the specified drives.\n"
712 "move | mv -f drive object ...\n"
713 " Move the object(s) to the specified drive.\n"
714 "quit Exit the vinum program when running in interactive mode."
716 " mally this would be done by entering the EOF character.\n"
717 "raid5 [-fv] [-s stripesize] [-n name] drives\n"
718 " Create a RAID-5 volume from the specified drives.\n"
719 "rename [-r] [drive | subdisk | plex | volume] newname\n"
720 " Change the name of the specified object.\n"
721 "rebuildparity plex [-f]\n"
722 " Rebuild the parity blocks of a RAID-5 plex.\n"
724 " Reset the complete gvinum configuration\n"
725 "rm [-r] [-f] volume | plex | subdisk | drive\n"
726 " Remove an object.\n"
728 " Save vinum configuration to disk after configuration"
730 "setstate [-f] state [volume | plex | subdisk | drive]\n"
731 " Set state without influencing other objects, for"
734 "start [-S size] volume | plex | subdisk\n"
735 " Allow the system to access the objects.\n"
736 "stripe [-fv] [-n name] drives\n"
737 " Create a striped volume from the specified drives.\n"
744 gvinum_setstate(int argc, char **argv)
746 struct gctl_req *req;
755 while ((i = getopt(argc, argv, "f")) != -1) {
762 warn("invalid flag: %c", i);
771 warnx("usage: setstate [-f] <state> <obj>");
776 * XXX: This hack is needed to avoid tripping over (now) invalid
777 * 'classic' vinum states and will go away later.
779 if (strcmp(argv[0], "up") && strcmp(argv[0], "down") &&
780 strcmp(argv[0], "stale")) {
781 warnx("invalid state '%s'", argv[0]);
785 req = gctl_get_handle();
786 gctl_ro_param(req, "class", -1, "VINUM");
787 gctl_ro_param(req, "verb", -1, "setstate");
788 gctl_ro_param(req, "state", -1, argv[0]);
789 gctl_ro_param(req, "object", -1, argv[1]);
790 gctl_ro_param(req, "flags", sizeof(int), &flags);
792 errstr = gctl_issue(req);
799 gvinum_list(int argc, char **argv)
801 struct gctl_req *req;
804 char buf[20], *cmd, config[GV_CFG_LEN + 1];
813 while ((j = getopt(argc, argv, "rsvV")) != -1) {
838 req = gctl_get_handle();
839 gctl_ro_param(req, "class", -1, "VINUM");
840 gctl_ro_param(req, "verb", -1, "list");
841 gctl_ro_param(req, "cmd", -1, cmd);
842 gctl_ro_param(req, "argc", sizeof(int), &argc);
843 gctl_ro_param(req, "flags", sizeof(int), &flags);
844 gctl_rw_param(req, "config", sizeof(config), config);
846 for (i = 0; i < argc; i++) {
847 snprintf(buf, sizeof(buf), "argv%d", i);
848 gctl_ro_param(req, buf, -1, argv[i]);
851 errstr = gctl_issue(req);
852 if (errstr != NULL) {
853 warnx("can't get configuration: %s", errstr);
858 printf("%s", config);
863 /* Create a mirrored volume. */
865 gvinum_mirror(int argc, char **argv)
869 warnx("usage\tmirror [-fsv] [-n name] drives\n");
872 create_volume(argc, argv, "mirror");
875 /* Note that move is currently of form '[-r] target object [...]' */
877 gvinum_move(int argc, char **argv)
879 struct gctl_req *req;
888 while ((j = getopt(argc, argv, "f")) != -1) {
904 warnx("no destination or object(s) to move specified");
907 warnx("no object(s) to move specified");
913 req = gctl_get_handle();
914 gctl_ro_param(req, "class", -1, "VINUM");
915 gctl_ro_param(req, "verb", -1, "move");
916 gctl_ro_param(req, "argc", sizeof(int), &argc);
917 gctl_ro_param(req, "flags", sizeof(int), &flags);
918 gctl_ro_param(req, "destination", -1, argv[0]);
919 for (i = 1; i < argc; i++) {
920 snprintf(buf, sizeof(buf), "argv%d", i);
921 gctl_ro_param(req, buf, -1, argv[i]);
923 errstr = gctl_issue(req);
925 warnx("can't move object(s): %s", errstr);
931 gvinum_printconfig(int argc, char **argv)
933 printconfig(stdout, "");
937 gvinum_parityop(int argc, char **argv, int rebuild)
939 struct gctl_req *req;
945 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;
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(int argc, char **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 **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);
1197 req = gctl_get_handle();
1198 gctl_ro_param(req, "class", -1, "VINUM");
1199 gctl_ro_param(req, "verb", -1, "start");
1200 gctl_ro_param(req, "argc", sizeof(int), &argc);
1201 gctl_ro_param(req, "initsize", sizeof(int), &initsize);
1203 for (i = 0; i < argc; i++) {
1204 snprintf(buf, sizeof(buf), "argv%d", i);
1205 gctl_ro_param(req, buf, -1, argv[i]);
1208 errstr = gctl_issue(req);
1209 if (errstr != NULL) {
1210 warnx("can't start: %s", errstr);
1219 gvinum_stop(int argc, char **argv)
1223 fileid = kldfind(GVINUMKLD);
1225 if (modfind(GVINUMMOD) < 0)
1226 warn("cannot find " GVINUMKLD);
1231 * This little hack prevents that we end up in an infinite loop in
1232 * g_unload_class(). gv_unload() will return EAGAIN so that the GEOM
1233 * event thread will be free for the g_wither_geom() call from
1234 * gv_unload(). It's silly, but it works.
1236 printf("unloading " GVINUMKLD " kernel module... ");
1238 if ((err = kldunload(fileid)) != 0 && (errno == EAGAIN)) {
1240 err = kldunload(fileid);
1243 printf(" failed!\n");
1244 warn("cannot unload " GVINUMKLD);
1252 /* Create a striped volume. */
1254 gvinum_stripe(int argc, char **argv)
1258 warnx("usage:\tstripe [-fv] [-n name] drives\n");
1261 create_volume(argc, argv, "stripe");
1264 /* Grow a subdisk by adding disk backed by provider. */
1266 gvinum_grow(int argc, char **argv)
1268 struct gctl_req *req;
1269 char *drive, *sdname;
1270 char sdprefix[GV_MAXSDNAME];
1274 int drives, volumes, plexes, subdisks, flags;
1277 drives = volumes = plexes = subdisks = 0;
1279 warnx("usage:\tgrow plex drive\n");
1285 warn("unable to create subdisk");
1288 d = gv_alloc_drive();
1290 warn("unable to create drive");
1294 /* Lookup device and set an appropriate drive name. */
1295 drive = find_drive();
1296 if (drive == NULL) {
1297 warn("unable to find an appropriate drive name");
1302 strlcpy(d->name, drive, sizeof(d->name));
1303 copy_device(d, argv[2]);
1307 /* We try to use the plex name as basis for the subdisk name. */
1308 snprintf(sdprefix, sizeof(sdprefix), "%s.s", argv[1]);
1309 sdname = find_name(sdprefix, GV_TYPE_SD, GV_MAXSDNAME);
1310 if (sdname == NULL) {
1311 warn("unable to find an appropriate subdisk name");
1317 strlcpy(s->name, sdname, sizeof(s->name));
1319 strlcpy(s->plex, argv[1], sizeof(s->plex));
1320 strlcpy(s->drive, d->name, sizeof(s->drive));
1323 req = gctl_get_handle();
1324 gctl_ro_param(req, "class", -1, "VINUM");
1325 gctl_ro_param(req, "verb", -1, "create");
1326 gctl_ro_param(req, "flags", sizeof(int), &flags);
1327 gctl_ro_param(req, "volumes", sizeof(int), &volumes);
1328 gctl_ro_param(req, "plexes", sizeof(int), &plexes);
1329 gctl_ro_param(req, "subdisks", sizeof(int), &subdisks);
1330 gctl_ro_param(req, "drives", sizeof(int), &drives);
1331 gctl_ro_param(req, "drive0", sizeof(*d), d);
1332 gctl_ro_param(req, "sd0", sizeof(*s), s);
1333 errstr = gctl_issue(req);
1335 if (errstr != NULL) {
1336 warnx("unable to grow plex: %s", errstr);
1345 parseline(int argc, char **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]);
1411 * The guts of printconfig. This is called from gvinum_printconfig and from
1412 * gvinum_create when called without an argument, in order to give the user
1413 * something to edit.
1416 printconfig(FILE *of, char *comment)
1418 struct gctl_req *req;
1419 struct utsname uname_s;
1422 char buf[GV_CFG_LEN + 1];
1427 req = gctl_get_handle();
1428 gctl_ro_param(req, "class", -1, "VINUM");
1429 gctl_ro_param(req, "verb", -1, "getconfig");
1430 gctl_ro_param(req, "comment", -1, comment);
1431 gctl_rw_param(req, "config", sizeof(buf), buf);
1432 errstr = gctl_issue(req);
1433 if (errstr != NULL) {
1434 warnx("can't get configuration: %s", errstr);
1439 fprintf(of, "# Vinum configuration of %s, saved at %s",
1443 if (*comment != '\0')
1444 fprintf(of, "# Current configuration:\n");
1446 fprintf(of, "%s", buf);