2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2013 Neil Brown <neilb@suse.de>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * Email: <neilb@suse.de>
24 * Additions for bitmap and write-behind RAID options, Copyright (C) 2003-2004,
25 * Paul Clements, SteelEye Technology, Inc.
32 static int scan_assemble(struct supertype *ss,
34 struct mddev_ident *ident);
35 static int misc_scan(char devmode, struct context *c);
36 static int stop_scan(int verbose);
37 static int misc_list(struct mddev_dev *devlist,
38 struct mddev_ident *ident,
40 struct supertype *ss, struct context *c);
41 const char Name[] = "mdadm";
43 int main(int argc, char *argv[])
51 unsigned long long array_size = 0;
52 unsigned long long data_offset = INVALID_SECTORS;
53 struct mddev_ident ident;
54 char *configfile = NULL;
57 struct mddev_dev *devlist = NULL;
58 struct mddev_dev **devlistend = & devlist;
61 char *symlinks = NULL;
62 int grow_continue = 0;
63 /* autof indicates whether and how to create device node.
64 * bottom 3 bits are style. Rest (when shifted) are number of parts
66 * 1 - don't create (no)
67 * 2 - if is_standard, then create (yes)
68 * 3 - create as 'md' - reject is_standard mdp (md)
69 * 4 - create as 'mdp' - reject is_standard md (mdp)
70 * 5 - default to md if not is_standard (md in config file)
71 * 6 - default to mdp if not is_standard (part, or mdp in config file)
74 .require_homehost = 1,
80 .bitmap_chunk = UnSet,
83 char sys_hostname[256];
84 char *mailaddr = NULL;
90 int spare_sharing = 1;
91 struct supertype *ss = NULL;
92 enum flag_mode writemostly = FlagDefault;
93 enum flag_mode failfast = FlagDefault;
94 char *shortopt = short_options;
97 char *remove_path = NULL;
98 char *udev_filename = NULL;
99 char *dump_directory = NULL;
106 srandom(time(0) ^ getpid());
110 ident.raid_disks = UnSet;
111 ident.super_minor= UnSet;
113 ident.spare_group = NULL;
116 ident.bitmap_fd = -1;
117 ident.bitmap_file = NULL;
119 ident.container = NULL;
122 while ((option_index = -1) ,
123 (opt=getopt_long(argc, argv,
124 shortopt, long_options,
125 &option_index)) != -1) {
127 /* firstly, some mode-independent options */
137 fputs(Version, stderr);
140 case 'v': c.verbose++;
143 case 'q': c.verbose--;
147 if (mode == ASSEMBLE || mode == BUILD ||
148 mode == CREATE || mode == GROW ||
149 mode == INCREMENTAL || mode == MANAGE)
150 break; /* b means bitmap */
155 case 'Y': c.export++;
159 if (strcasecmp(optarg, "<ignore>") == 0)
160 c.require_homehost = 0;
166 /* Silently ignore old option */
172 if (asprintf(&c.prefer, "/%s/", optarg) <= 0)
178 fputs(Usage, stderr);
181 /* second, figure out the mode.
182 * Some options force the mode. Others
183 * set the mode if it isn't already
189 shortopt = short_bitmap_options;
201 case ReAdd: /* re-add */
205 shortopt = short_bitmap_options;
209 case 'A': newmode = ASSEMBLE;
210 shortopt = short_bitmap_auto_options;
212 case 'B': newmode = BUILD;
213 shortopt = short_bitmap_auto_options;
215 case 'C': newmode = CREATE;
216 shortopt = short_bitmap_auto_options;
218 case 'F': newmode = MONITOR;
220 case 'G': newmode = GROW;
221 shortopt = short_bitmap_options;
223 case 'I': newmode = INCREMENTAL;
224 shortopt = short_bitmap_auto_options;
227 newmode = AUTODETECT;
262 if (mode && newmode == mode) {
263 /* everybody happy ! */
264 } else if (mode && newmode != mode) {
267 if (option_index >= 0)
268 fprintf(stderr, "--%s", long_options[option_index].name);
270 fprintf(stderr, "-%c", opt);
271 fprintf(stderr, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
272 map_num(modes, newmode),
273 map_num(modes, mode));
275 } else if (!mode && newmode) {
277 if (mode == MISC && devs_found) {
278 pr_err("No action given for %s in --misc mode\n",
280 cont_err("Action options must come before device names\n");
284 /* special case of -c --help */
285 if ((opt == 'c' || opt == ConfigFile) &&
286 (strncmp(optarg, "--h", 3) == 0 ||
287 strncmp(optarg, "-h", 2) == 0)) {
288 fputs(Help_config, stdout);
292 /* If first option is a device, don't force the mode yet */
294 if (devs_found == 0) {
295 dv = xmalloc(sizeof(*dv));
296 dv->devname = optarg;
297 dv->disposition = devmode;
298 dv->writemostly = writemostly;
299 dv->failfast = failfast;
303 devlistend = &dv->next;
308 /* No mode yet, and this is the second device ... */
309 pr_err("An option must be given to set the mode before a second device\n"
310 " (%s) is listed\n", optarg);
313 if (option_index >= 0)
314 pr_err("--%s", long_options[option_index].name);
317 fprintf(stderr, " does not set the mode, and so cannot be the first option.\n");
321 /* if we just set the mode, then done */
335 /* an undecorated option - must be a device name.
338 if (devs_found > 0 && devmode == DetailPlatform) {
339 pr_err("controller may only be specified once. %s ignored\n",
344 if (devs_found > 0 && mode == MANAGE && !devmode) {
345 pr_err("Must give one of -a/-r/-f for subsequent devices at %s\n", optarg);
348 if (devs_found > 0 && mode == GROW && !devmode) {
349 pr_err("Must give -a/--add for devices to add: %s\n", optarg);
352 dv = xmalloc(sizeof(*dv));
353 dv->devname = optarg;
354 dv->disposition = devmode;
355 dv->writemostly = writemostly;
356 dv->failfast = failfast;
360 devlistend = &dv->next;
366 /* We've got a mode, and opt is now something else which
367 * could depend on the mode */
368 #define O(a,b) ((a<<16)|b)
369 switch (O(mode,opt)) {
371 case O(GROW,ChunkSize):
373 case O(CREATE,ChunkSize):
374 case O(BUILD,'c'): /* chunk or rounding */
375 case O(BUILD,ChunkSize): /* chunk or rounding */
377 pr_err("chunk/rounding may only be specified once. Second value is %s.\n", optarg);
380 s.chunk = parse_size(optarg);
381 if (s.chunk == INVALID_SECTORS ||
382 s.chunk < 8 || (s.chunk&1)) {
383 pr_err("invalid chunk/rounding value: %s\n",
387 /* Convert sectors to K */
391 case O(INCREMENTAL, 'e'):
393 case O(ASSEMBLE,'e'):
394 case O(MISC,'e'): /* set metadata (superblock) information */
396 pr_err("metadata information already given\n");
399 for(i=0; !ss && superlist[i]; i++)
400 ss = superlist[i]->match_metadata_desc(optarg);
403 pr_err("unrecognised metadata identifier: %s\n", optarg);
409 case O(MANAGE,WriteMostly):
411 case O(BUILD,WriteMostly):
413 case O(CREATE,WriteMostly):
414 /* set write-mostly for following devices */
415 writemostly = FlagSet;
419 /* clear write-mostly for following devices */
420 writemostly = FlagClear;
423 case O(MANAGE,FailFast):
424 case O(CREATE,FailFast):
427 case O(MANAGE,NoFailFast):
428 failfast = FlagClear;
433 case O(BUILD,'z'): /* size */
435 pr_err("size may only be specified once. Second value is %s.\n", optarg);
438 if (strcmp(optarg, "max") == 0)
441 s.size = parse_size(optarg);
442 if (s.size == INVALID_SECTORS || s.size < 8) {
443 pr_err("invalid size: %s\n", optarg);
446 /* convert sectors to K */
451 case O(GROW,'Z'): /* array size */
452 if (array_size > 0) {
453 pr_err("array-size may only be specified once. Second value is %s.\n", optarg);
456 if (strcmp(optarg, "max") == 0)
457 array_size = MAX_SIZE;
459 array_size = parse_size(optarg);
460 if (array_size == 0 ||
461 array_size == INVALID_SECTORS) {
462 pr_err("invalid array size: %s\n",
469 case O(CREATE,DataOffset):
470 case O(GROW,DataOffset):
471 if (data_offset != INVALID_SECTORS) {
472 pr_err("data-offset may only be specified one. Second value is %s.\n", optarg);
475 if (mode == CREATE && strcmp(optarg, "variable") == 0)
476 data_offset = VARIABLE_OFFSET;
478 data_offset = parse_size(optarg);
479 if (data_offset == INVALID_SECTORS) {
480 pr_err("invalid data-offset: %s\n",
488 case O(BUILD,'l'): /* set raid level*/
489 if (s.level != UnSet) {
490 pr_err("raid level may only be set once. Second value is %s.\n", optarg);
493 s.level = map_name(pers, optarg);
494 if (s.level == UnSet) {
495 pr_err("invalid raid level: %s\n",
499 if (s.level != 0 && s.level != LEVEL_LINEAR &&
500 s.level != 1 && s.level != LEVEL_MULTIPATH &&
501 s.level != LEVEL_FAULTY && s.level != 10 &&
503 pr_err("Raid level %s not permitted with --build.\n",
507 if (s.sparedisks > 0 && s.level < 1 && s.level >= -1) {
508 pr_err("raid level %s is incompatible with spare-devices setting.\n",
512 ident.level = s.level;
515 case O(GROW, 'p'): /* new layout */
516 case O(GROW, Layout):
518 pr_err("layout may only be sent once. Second value was %s\n", optarg);
521 s.layout_str = optarg;
522 /* 'Grow' will parse the value */
525 case O(CREATE,'p'): /* raid5 layout */
526 case O(CREATE,Layout):
527 case O(BUILD,'p'): /* faulty layout */
528 case O(BUILD,Layout):
529 if (s.layout != UnSet) {
530 pr_err("layout may only be sent once. Second value was %s\n", optarg);
535 pr_err("layout not meaningful for %s arrays.\n",
536 map_num(pers, s.level));
539 pr_err("raid level must be given before layout.\n");
543 s.layout = map_name(r5layout, optarg);
544 if (s.layout==UnSet) {
545 pr_err("layout %s not understood for raid5.\n",
551 s.layout = map_name(r6layout, optarg);
552 if (s.layout==UnSet) {
553 pr_err("layout %s not understood for raid6.\n",
560 s.layout = parse_layout_10(optarg);
562 pr_err("layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg);
570 s.layout = parse_layout_faulty(optarg);
571 if (s.layout == -1) {
572 pr_err("layout %s not understood for faulty.\n",
580 case O(CREATE,AssumeClean):
581 case O(BUILD,AssumeClean): /* assume clean */
582 case O(GROW,AssumeClean):
588 case O(BUILD,'n'): /* number of raid disks */
590 pr_err("raid-devices set twice: %d and %s\n",
591 s.raiddisks, optarg);
594 s.raiddisks = parse_num(optarg);
595 if (s.raiddisks <= 0) {
596 pr_err("invalid number of raid devices: %s\n",
600 ident.raid_disks = s.raiddisks;
602 case O(ASSEMBLE, Nodes):
604 case O(CREATE, Nodes):
605 c.nodes = parse_num(optarg);
607 pr_err("invalid number for the number of cluster nodes: %s\n",
612 case O(CREATE, ClusterName):
613 case O(ASSEMBLE, ClusterName):
614 c.homecluster = optarg;
615 if (strlen(c.homecluster) > 64) {
616 pr_err("Cluster name too big.\n");
620 case O(CREATE,'x'): /* number of spare (eXtra) disks */
622 pr_err("spare-devices set twice: %d and %s\n",
623 s.sparedisks, optarg);
626 if (s.level != UnSet && s.level <= 0 && s.level >= -1) {
627 pr_err("spare-devices setting is incompatible with raid level %d\n",
631 s.sparedisks = parse_num(optarg);
632 if (s.sparedisks < 0) {
633 pr_err("invalid number of spare-devices: %s\n",
643 case O(INCREMENTAL,'a'):
644 case O(INCREMENTAL,Auto):
645 case O(ASSEMBLE,'a'):
646 case O(ASSEMBLE,Auto): /* auto-creation of device node */
647 c.autof = parse_auto(optarg, "--auto flag", 0);
650 case O(CREATE,Symlinks):
651 case O(BUILD,Symlinks):
652 case O(ASSEMBLE,Symlinks): /* auto creation of symlinks in /dev to /dev/md */
656 case O(BUILD,'f'): /* force honouring '-n 1' */
657 case O(BUILD,Force): /* force honouring '-n 1' */
658 case O(GROW,'f'): /* ditto */
659 case O(GROW,Force): /* ditto */
660 case O(CREATE,'f'): /* force honouring of device list */
661 case O(CREATE,Force): /* force honouring of device list */
662 case O(ASSEMBLE,'f'): /* force assembly */
663 case O(ASSEMBLE,Force): /* force assembly */
664 case O(MISC,'f'): /* force zero */
665 case O(MISC,Force): /* force zero */
666 case O(MANAGE,Force): /* add device which is too large */
669 /* now for the Assemble options */
670 case O(ASSEMBLE, FreezeReshape): /* Freeze reshape during
672 case O(INCREMENTAL, FreezeReshape):
673 c.freeze_reshape = 1;
675 case O(CREATE,'u'): /* uuid of array */
676 case O(ASSEMBLE,'u'): /* uuid of array */
677 if (ident.uuid_set) {
678 pr_err("uuid cannot be set twice. Second value %s.\n", optarg);
681 if (parse_uuid(optarg, ident.uuid))
684 pr_err("Bad uuid: %s\n", optarg);
690 case O(ASSEMBLE,'N'):
693 pr_err("name cannot be set twice. Second value %s.\n", optarg);
696 if (mode == MISC && !c.subarray) {
697 pr_err("-N/--name only valid with --update-subarray in misc mode\n");
700 if (strlen(optarg) > 32) {
701 pr_err("name '%s' is too long, 32 chars max.\n",
705 strcpy(ident.name, optarg);
708 case O(ASSEMBLE,'m'): /* super-minor for array */
709 case O(ASSEMBLE,SuperMinor):
710 if (ident.super_minor != UnSet) {
711 pr_err("super-minor cannot be set twice. Second value: %s.\n", optarg);
714 if (strcmp(optarg, "dev") == 0)
715 ident.super_minor = -2;
717 ident.super_minor = parse_num(optarg);
718 if (ident.super_minor < 0) {
719 pr_err("Bad super-minor number: %s.\n", optarg);
725 case O(ASSEMBLE,'o'):
731 case O(ASSEMBLE,'U'): /* update the superblock */
734 pr_err("Can only update one aspect of superblock, both %s and %s given.\n",
738 if (mode == MISC && !c.subarray) {
739 pr_err("Only subarrays can be updated in misc mode\n");
743 if (strcmp(c.update, "sparc2.2") == 0)
745 if (strcmp(c.update, "super-minor") == 0)
747 if (strcmp(c.update, "summaries") == 0)
749 if (strcmp(c.update, "resync") == 0)
751 if (strcmp(c.update, "uuid") == 0)
753 if (strcmp(c.update, "name") == 0)
755 if (strcmp(c.update, "homehost") == 0)
757 if (strcmp(c.update, "home-cluster") == 0)
759 if (strcmp(c.update, "nodes") == 0)
761 if (strcmp(c.update, "devicesize") == 0)
763 if (strcmp(c.update, "no-bitmap") == 0)
765 if (strcmp(c.update, "bbl") == 0)
767 if (strcmp(c.update, "no-bbl") == 0)
769 if (strcmp(c.update, "force-no-bbl") == 0)
771 if (strcmp(c.update, "metadata") == 0)
773 if (strcmp(c.update, "revert-reshape") == 0)
775 if (strcmp(c.update, "byteorder")==0) {
777 pr_err("must not set metadata type with --update=byteorder.\n");
780 for(i=0; !ss && superlist[i]; i++)
781 ss = superlist[i]->match_metadata_desc(
784 pr_err("INTERNAL ERROR cannot find 0.swap\n");
790 if (strcmp(c.update,"?") == 0 ||
791 strcmp(c.update, "help") == 0) {
793 fprintf(outf, "%s: ", Name);
797 "%s: '--update=%s' is invalid. ",
800 fprintf(outf, "Valid --update options are:\n"
801 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'nodes', 'resync',\n"
802 " 'summaries', 'homehost', 'home-cluster', 'byteorder', 'devicesize',\n"
803 " 'no-bitmap', 'metadata', 'revert-reshape'\n"
804 " 'bbl', 'no-bbl', 'force-no-bbl'\n"
806 exit(outf == stdout ? 0 : 2);
809 /* update=devicesize is allowed with --re-add */
810 if (devmode != 'A') {
811 pr_err("--update in Manage mode only allowed with --re-add.\n");
815 pr_err("Can only update one aspect of superblock, both %s and %s given.\n",
820 if (strcmp(c.update, "devicesize") != 0 &&
821 strcmp(c.update, "bbl") != 0 &&
822 strcmp(c.update, "force-no-bbl") != 0 &&
823 strcmp(c.update, "no-bbl") != 0) {
824 pr_err("only 'devicesize', 'bbl', 'no-bbl', and 'force-no-bbl' can be updated with --re-add\n");
829 case O(INCREMENTAL,NoDegraded):
830 pr_err("--no-degraded is deprecated in Incremental mode\n");
831 case O(ASSEMBLE,NoDegraded): /* --no-degraded */
832 c.runstop = -1; /* --stop isn't allowed for --assemble,
833 * so we overload slightly */
836 case O(ASSEMBLE,'c'):
837 case O(ASSEMBLE,ConfigFile):
838 case O(INCREMENTAL, 'c'):
839 case O(INCREMENTAL, ConfigFile):
841 case O(MISC, ConfigFile):
843 case O(MONITOR,ConfigFile):
844 case O(CREATE,ConfigFile):
846 pr_err("configfile cannot be set twice. Second value is %s.\n", optarg);
850 set_conffile(configfile);
851 /* FIXME possibly check that config file exists. Even parse it */
853 case O(ASSEMBLE,'s'): /* scan */
856 case O(INCREMENTAL,'s'):
860 case O(MONITOR,'m'): /* mail address */
861 case O(MONITOR,EMail):
863 pr_err("only specify one mailaddress. %s ignored.\n",
869 case O(MONITOR,'p'): /* alert program */
870 case O(MONITOR,ProgramOpt): /* alert program */
872 pr_err("only specify one alter program. %s ignored.\n",
878 case O(MONITOR,'r'): /* rebuild increments */
879 case O(MONITOR,Increment):
880 increments = atoi(optarg);
881 if (increments > 99 || increments < 1) {
882 pr_err("please specify positive integer between 1 and 99 as rebuild increments.\n");
887 case O(MONITOR,'d'): /* delay in seconds */
889 case O(BUILD,'d'): /* delay for bitmap updates */
892 pr_err("only specify delay once. %s ignored.\n",
895 c.delay = parse_num(optarg);
897 pr_err("invalid delay: %s\n",
903 case O(MONITOR,'f'): /* daemonise */
904 case O(MONITOR,Fork):
907 case O(MONITOR,'i'): /* pid */
909 pr_err("only specify one pid file. %s ignored.\n",
914 case O(MONITOR,'1'): /* oneshot */
918 case O(MONITOR,'t'): /* test */
921 case O(MONITOR,'y'): /* log messages to syslog */
922 openlog("mdadm", LOG_PID, SYSLOG_FACILITY);
925 case O(MONITOR, NoSharing):
929 /* now the general management options. Some are applicable
930 * to other modes. None have arguments.
935 case O(MANAGE,Add): /* add a drive */
938 case O(MANAGE,AddSpare): /* add drive - never re-add */
941 case O(MANAGE,AddJournal): /* add journal */
942 if (s.journaldisks && (s.level < 4 || s.level > 6)) {
943 pr_err("--add-journal is only supported for RAID level 4/5/6.\n");
948 case O(MANAGE,ReAdd):
951 case O(MANAGE,'r'): /* remove a drive */
952 case O(MANAGE,Remove):
955 case O(MANAGE,'f'): /* set faulty */
957 case O(INCREMENTAL,'f'):
958 case O(INCREMENTAL,Remove):
959 case O(INCREMENTAL,Fail): /* r for incremental is taken, use f
960 * even though we will both fail and
961 * remove the device */
964 case O(MANAGE, ClusterConfirm):
967 case O(MANAGE,Replace):
968 /* Mark these devices for replacement */
972 /* These are the replacements to use */
973 if (devmode != 'R') {
974 pr_err("--with must follow --replace\n");
979 case O(INCREMENTAL,'R'):
981 case O(ASSEMBLE,'R'):
983 case O(CREATE,'R'): /* Run the array */
985 pr_err("Cannot both Stop and Run an array\n");
992 pr_err("Cannot both Run and Stop an array\n");
1004 case O(MISC,KillOpt):
1008 case O(MISC, ExamineBB):
1012 case O(MISC, WaitOpt):
1013 case O(MISC, Waitclean):
1014 case O(MISC, DetailPlatform):
1015 case O(MISC, KillSubarray):
1016 case O(MISC, UpdateSubarray):
1018 case O(MISC, Restore):
1019 case O(MISC ,Action):
1020 if (opt == KillSubarray || opt == UpdateSubarray) {
1022 pr_err("subarray can only be specified once\n");
1025 c.subarray = optarg;
1027 if (opt == Action) {
1029 pr_err("Only one --action can be specified\n");
1032 if (strcmp(optarg, "idle") == 0 ||
1033 strcmp(optarg, "frozen") == 0 ||
1034 strcmp(optarg, "check") == 0 ||
1035 strcmp(optarg, "repair") == 0)
1038 pr_err("action must be one of idle, frozen, check, repair\n");
1042 if (devmode && devmode != opt &&
1044 (opt == 'E' && devmode != 'Q'))) {
1045 pr_err("--examine/-E cannot be given with ");
1046 if (devmode == 'E') {
1047 if (option_index >= 0)
1048 fprintf(stderr, "--%s\n",
1049 long_options[option_index].name);
1051 fprintf(stderr, "-%c\n", opt);
1052 } else if (isalpha(devmode))
1053 fprintf(stderr, "-%c\n", devmode);
1055 fprintf(stderr, "previous option\n");
1059 if (opt == Dump || opt == Restore) {
1060 if (dump_directory != NULL) {
1061 pr_err("dump/restore directory specified twice: %s and %s\n",
1062 dump_directory, optarg);
1065 dump_directory = optarg;
1068 case O(MISC, UdevRules):
1069 if (devmode && devmode != opt) {
1070 pr_err("--udev-rules must be the only option.\n");
1073 pr_err("only specify one udev rule filename. %s ignored.\n",
1076 udev_filename = optarg;
1084 case O(MISC, Sparc22):
1085 if (devmode != 'E') {
1086 pr_err("--sparc2.2 only allowed with --examine\n");
1092 case O(ASSEMBLE,'b'): /* here we simply set the bitmap file */
1093 case O(ASSEMBLE,Bitmap):
1095 pr_err("bitmap file needed with -b in --assemble mode\n");
1098 if (strcmp(optarg, "internal") == 0) {
1099 pr_err("there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
1102 bitmap_fd = open(optarg, O_RDWR);
1103 if (!*optarg || bitmap_fd < 0) {
1104 pr_err("cannot open bitmap file %s: %s\n", optarg, strerror(errno));
1107 ident.bitmap_fd = bitmap_fd; /* for Assemble */
1110 case O(ASSEMBLE, BackupFile):
1111 case O(GROW, BackupFile):
1112 /* Specify a file into which grow might place a backup,
1113 * or from which assemble might recover a backup
1115 if (c.backup_file) {
1116 pr_err("backup file already specified, rejecting %s\n", optarg);
1119 c.backup_file = optarg;
1122 case O(GROW, Continue):
1123 /* Continue interrupted grow
1127 case O(ASSEMBLE, InvalidBackup):
1128 /* Acknowledge that the backupfile is invalid, but ask
1129 * to continue anyway
1131 c.invalid_backup = 1;
1135 case O(BUILD,Bitmap):
1137 case O(CREATE,Bitmap): /* here we create the bitmap */
1139 case O(GROW,Bitmap):
1140 if (strcmp(optarg, "internal") == 0 ||
1141 strcmp(optarg, "none") == 0 ||
1142 strchr(optarg, '/') != NULL) {
1143 s.bitmap_file = optarg;
1146 if (strcmp(optarg, "clustered") == 0) {
1147 s.bitmap_file = optarg;
1148 /* Set the default number of cluster nodes
1149 * to 4 if not already set by user
1156 pr_err("bitmap file must contain a '/', or be 'internal', or be 'clustered', or 'none'\n"
1157 " not '%s'\n", optarg);
1160 case O(GROW,BitmapChunk):
1161 case O(BUILD,BitmapChunk):
1162 case O(CREATE,BitmapChunk): /* bitmap chunksize */
1163 s.bitmap_chunk = parse_size(optarg);
1164 if (s.bitmap_chunk == 0 ||
1165 s.bitmap_chunk == INVALID_SECTORS ||
1166 s.bitmap_chunk & (s.bitmap_chunk - 1)) {
1167 pr_err("invalid bitmap chunksize: %s\n",
1171 s.bitmap_chunk = s.bitmap_chunk * 512;
1174 case O(GROW, WriteBehind):
1175 case O(BUILD, WriteBehind):
1176 case O(CREATE, WriteBehind): /* write-behind mode */
1177 s.write_behind = DEFAULT_MAX_WRITE_BEHIND;
1179 s.write_behind = parse_num(optarg);
1180 if (s.write_behind < 0 ||
1181 s.write_behind > 16383) {
1182 pr_err("Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg);
1188 case O(INCREMENTAL, 'r'):
1189 case O(INCREMENTAL, RebuildMapOpt):
1192 case O(INCREMENTAL, IncrementalPath):
1193 remove_path = optarg;
1195 case O(CREATE, WriteJournal):
1196 if (s.journaldisks) {
1197 pr_err("Please specify only one journal device for the array.\n");
1198 pr_err("Ignoring --write-journal %s...\n", optarg);
1201 dv = xmalloc(sizeof(*dv));
1202 dv->devname = optarg;
1203 dv->disposition = 'j'; /* WriteJournal */
1207 devlistend = &dv->next;
1213 /* We have now processed all the valid options. Anything else is
1216 if (option_index > 0)
1217 pr_err(":option --%s not valid in %s mode\n",
1218 long_options[option_index].name,
1219 map_num(modes, mode));
1221 pr_err("option -%c not valid in %s mode\n",
1222 opt, map_num(modes, mode));
1229 if (print_help == 2)
1230 help_text = OptionHelp;
1232 help_text = mode_help[mode];
1233 if (help_text == NULL)
1235 fputs(help_text,stdout);
1239 if (s.journaldisks && (s.level < 4 || s.level > 6)) {
1240 pr_err("--write-journal is only supported for RAID level 4/5/6.\n");
1244 if (!mode && devs_found) {
1247 if (devlist->disposition == 0)
1248 devlist->disposition = devmode;
1251 fputs(Usage, stderr);
1256 struct createinfo *ci = conf_get_create_info();
1258 if (strcasecmp(symlinks, "yes") == 0)
1260 else if (strcasecmp(symlinks, "no") == 0)
1263 pr_err("option --symlinks must be 'no' or 'yes'\n");
1267 /* Ok, got the option parsing out of the way
1268 * hopefully it's mostly right but there might be some stuff
1271 * That is mosty checked in the per-mode stuff but...
1273 * For @,B,C and A without -s, the first device listed must be
1274 * an md device. We check that here and open it.
1277 if (mode == MANAGE || mode == BUILD || mode == CREATE ||
1278 mode == GROW || (mode == ASSEMBLE && ! c.scan)) {
1279 if (devs_found < 1) {
1280 pr_err("an md device must be given in this mode\n");
1283 if ((int)ident.super_minor == -2 && c.autof) {
1284 pr_err("--super-minor=dev is incompatible with --auto\n");
1287 if (mode == MANAGE || mode == GROW) {
1288 mdfd = open_mddev(devlist->devname, 1);
1292 /* non-existent device is OK */
1293 mdfd = open_mddev(devlist->devname, 0);
1295 pr_err("device %s exists but is not an md array.\n", devlist->devname);
1298 if ((int)ident.super_minor == -2) {
1301 pr_err("--super-minor=dev given, and listed device %s doesn't exist.\n",
1306 ident.super_minor = minor(stb.st_rdev);
1308 if (mdfd >= 0 && mode != MANAGE && mode != GROW) {
1309 /* We don't really want this open yet, we just might
1310 * have wanted to check some things
1318 if (s.raiddisks == 1 && !c.force && s.level != LEVEL_FAULTY) {
1319 pr_err("'1' is an unusual number of drives for an array, so it is probably\n"
1320 " a mistake. If you really mean it you will need to specify --force before\n"
1321 " setting the number of drives.\n");
1326 if (c.homehost == NULL && c.require_homehost)
1327 c.homehost = conf_get_homehost(&c.require_homehost);
1328 if (c.homehost == NULL || strcasecmp(c.homehost, "<system>") == 0) {
1329 if (gethostname(sys_hostname, sizeof(sys_hostname)) == 0) {
1330 sys_hostname[sizeof(sys_hostname)-1] = 0;
1331 c.homehost = sys_hostname;
1335 (!c.homehost[0] || strcasecmp(c.homehost, "<none>") == 0)) {
1337 c.require_homehost = 0;
1342 set_hooks(); /* set hooks from libs */
1344 if (c.homecluster == NULL && (c.nodes > 0)) {
1345 c.homecluster = conf_get_homecluster();
1346 if (c.homecluster == NULL)
1347 rv = get_cluster_name(&c.homecluster);
1349 pr_err("The md can't get cluster name\n");
1354 if (c.backup_file && data_offset != INVALID_SECTORS) {
1355 pr_err("--backup-file and --data-offset are incompatible\n");
1359 if ((mode == MISC && devmode == 'E') ||
1360 (mode == MONITOR && spare_sharing == 0))
1361 /* Anyone may try this */;
1362 else if (geteuid() != 0) {
1363 pr_err("must be super-user to perform this action\n");
1367 ident.autof = c.autof;
1369 if (c.scan && c.verbose < 2)
1370 /* --scan implied --brief unless -vv */
1375 /* readonly, add/remove, readwrite, runstop */
1377 rv = Manage_ro(devlist->devname, mdfd, c.readonly);
1378 if (!rv && devs_found>1)
1379 rv = Manage_subdevs(devlist->devname, mdfd,
1380 devlist->next, c.verbose, c.test,
1382 if (!rv && c.readonly < 0)
1383 rv = Manage_ro(devlist->devname, mdfd, c.readonly);
1384 if (!rv && c.runstop > 0)
1385 rv = Manage_run(devlist->devname, mdfd, &c);
1386 if (!rv && c.runstop < 0)
1387 rv = Manage_stop(devlist->devname, mdfd, c.verbose, 0);
1390 if (devs_found == 1 && ident.uuid_set == 0 &&
1391 ident.super_minor == UnSet && ident.name[0] == 0 &&
1393 /* Only a device has been given, so get details from config file */
1394 struct mddev_ident *array_ident = conf_get_ident(devlist->devname);
1395 if (array_ident == NULL) {
1396 pr_err("%s not identified in config file.\n",
1402 if (array_ident->autof == 0)
1403 array_ident->autof = c.autof;
1404 rv |= Assemble(ss, devlist->devname, array_ident,
1408 rv = Assemble(ss, devlist->devname, &ident,
1410 else if (devs_found > 0) {
1411 if (c.update && devs_found > 1) {
1412 pr_err("can only update a single array at a time\n");
1415 if (c.backup_file && devs_found > 1) {
1416 pr_err("can only assemble a single array when providing a backup file.\n");
1419 for (dv = devlist ; dv ; dv=dv->next) {
1420 struct mddev_ident *array_ident = conf_get_ident(dv->devname);
1421 if (array_ident == NULL) {
1422 pr_err("%s not identified in config file.\n",
1427 if (array_ident->autof == 0)
1428 array_ident->autof = c.autof;
1429 rv |= Assemble(ss, dv->devname, array_ident,
1434 pr_err("--update not meaningful with a --scan assembly.\n");
1437 if (c.backup_file) {
1438 pr_err("--backup_file not meaningful with a --scan assembly.\n");
1441 rv = scan_assemble(ss, &c, &ident);
1447 c.delay = DEFAULT_BITMAP_DELAY;
1448 if (s.write_behind && !s.bitmap_file) {
1449 pr_err("write-behind mode requires a bitmap.\n");
1453 if (s.raiddisks == 0) {
1454 pr_err("no raid-devices specified.\n");
1459 if (s.bitmap_file) {
1460 if (strcmp(s.bitmap_file, "internal") == 0 ||
1461 strcmp(s.bitmap_file, "clustered") == 0) {
1462 pr_err("'internal' and 'clustered' bitmaps not supported with --build\n");
1467 rv = Build(devlist->devname, devlist->next, &s, &c);
1471 c.delay = DEFAULT_BITMAP_DELAY;
1474 if (!s.bitmap_file ||
1475 strcmp(s.bitmap_file, "clustered") != 0) {
1476 pr_err("--nodes argument only compatible with --bitmap=clustered\n");
1482 pr_err("--bitmap=clustered is currently supported with RAID mirror only\n");
1488 if (s.write_behind && !s.bitmap_file) {
1489 pr_err("write-behind mode requires a bitmap.\n");
1493 if (s.raiddisks == 0) {
1494 pr_err("no raid-devices specified.\n");
1499 rv = Create(ss, devlist->devname,
1500 ident.name, ident.uuid_set ? ident.uuid : NULL,
1501 devs_found-1, devlist->next,
1502 &s, &c, data_offset);
1505 if (devmode == 'E') {
1506 if (devlist == NULL && !c.scan) {
1507 pr_err("No devices to examine\n");
1510 if (devlist == NULL)
1511 devlist = conf_get_devs();
1512 if (devlist == NULL) {
1513 pr_err("No devices listed in %s\n", configfile?configfile:DefaultConfFile);
1516 rv = Examine(devlist, &c, ss);
1517 } else if (devmode == DetailPlatform) {
1518 rv = Detail_Platform(ss ? ss->ss : NULL, ss ? c.scan : 1,
1519 c.verbose, c.export,
1520 devlist ? devlist->devname : NULL);
1521 } else if (devlist == NULL) {
1522 if (devmode == 'S' && c.scan)
1523 rv = stop_scan(c.verbose);
1524 else if ((devmode == 'D' || devmode == Waitclean) &&
1526 rv = misc_scan(devmode, &c);
1527 else if (devmode == UdevRules)
1528 rv = Write_rules(udev_filename);
1530 pr_err("No devices given.\n");
1534 rv = misc_list(devlist, &ident, dump_directory, ss, &c);
1537 if (!devlist && !c.scan) {
1538 pr_err("Cannot monitor: need --scan or at least one device\n");
1542 if (pidfile && !daemonise) {
1543 pr_err("Cannot write a pid file when not in daemon mode\n");
1548 if (get_linux_version() > 2006016)
1549 /* mdstat responds to poll */
1554 rv= Monitor(devlist, mailaddr, program,
1555 &c, daemonise, oneshot,
1556 dosyslog, pidfile, increments,
1561 if (array_size > 0) {
1562 /* alway impose array size first, independent of
1564 * Do not allow level or raid_disks changes at the
1565 * same time as that can be irreversibly destructive.
1569 if (s.raiddisks || s.level != UnSet) {
1570 pr_err("cannot change array size in same operation as changing raiddisks or level.\n"
1571 " Change size first, then check that data is still intact.\n");
1575 sysfs_init(&sra, mdfd, NULL);
1576 if (array_size == MAX_SIZE)
1577 err = sysfs_set_str(&sra, NULL, "array_size", "default");
1579 err = sysfs_set_num(&sra, NULL, "array_size", array_size / 2);
1582 pr_err("--array-size setting is too large.\n");
1584 pr_err("current kernel does not support setting --array-size\n");
1589 if (devs_found > 1 && s.raiddisks == 0 && s.level == UnSet) {
1591 if (s.size > 0 || s.chunk ||
1592 s.layout_str || s.bitmap_file) {
1593 pr_err("--add cannot be used with other geometry changes in --grow mode\n");
1597 for (dv=devlist->next; dv ; dv=dv->next) {
1598 rv = Grow_Add_device(devlist->devname, mdfd,
1603 } else if (s.bitmap_file) {
1604 if (s.size > 0 || s.raiddisks || s.chunk ||
1605 s.layout_str || devs_found > 1) {
1606 pr_err("--bitmap changes cannot be used with other geometry changes in --grow mode\n");
1611 c.delay = DEFAULT_BITMAP_DELAY;
1612 rv = Grow_addbitmap(devlist->devname, mdfd, &c, &s);
1613 } else if (grow_continue)
1614 rv = Grow_continue_command(devlist->devname,
1615 mdfd, c.backup_file,
1617 else if (s.size > 0 || s.raiddisks || s.layout_str ||
1618 s.chunk != 0 || s.level != UnSet ||
1619 data_offset != INVALID_SECTORS) {
1620 rv = Grow_reshape(devlist->devname, mdfd,
1622 data_offset, &c, &s);
1623 } else if (array_size == 0)
1624 pr_err("no changes to --grow\n");
1633 pr_err("In --incremental mode, a device cannot be given with --scan.\n");
1636 if (c.runstop <= 0) {
1637 pr_err("--incremental --scan meaningless without --run.\n");
1640 if (devmode == 'f') {
1641 pr_err("--incremental --scan --fail not supported.\n");
1644 rv = IncrementalScan(&c, NULL);
1647 if (!rebuild_map && !c.scan) {
1648 pr_err("--incremental requires a device.\n");
1653 if (devmode == 'f') {
1654 if (devlist->next) {
1655 pr_err("'--incremental --fail' can only handle one device.\n");
1659 rv = IncrementalRemove(devlist->devname, remove_path,
1662 rv = Incremental(devlist, &c, ss);
1671 static int scan_assemble(struct supertype *ss,
1673 struct mddev_ident *ident)
1675 struct mddev_ident *a, *array_list = conf_get_ident(NULL);
1676 struct mddev_dev *devlist = conf_get_devs();
1677 struct map_ent *map = NULL;
1680 int failures, successes;
1682 if (conf_verify_devnames(array_list)) {
1683 pr_err("Duplicate MD device names in conf file were found.\n");
1686 if (devlist == NULL) {
1687 pr_err("No devices listed in conf file were found.\n");
1690 for (a = array_list; a ; a = a->next) {
1693 a->autof = c->autof;
1696 pr_err("failed to get exclusive lock on mapfile\n");
1701 for (a = array_list; a ; a = a->next) {
1706 strcasecmp(a->devname, "<ignore>") == 0)
1709 r = Assemble(ss, a->devname,
1719 } while (failures && successes);
1720 if (c->homehost && cnt == 0) {
1721 /* Maybe we can auto-assemble something.
1722 * Repeatedly call Assemble in auto-assemble mode
1727 ident->autof = c->autof;
1729 struct mddev_dev *devlist = conf_get_devs();
1732 rv2 = Assemble(ss, NULL,
1740 /* Incase there are stacked devices, we need to go around again */
1742 if (cnt == 0 && rv == 0) {
1743 pr_err("No arrays found in config file or automatically\n");
1747 } else if (cnt == 0 && rv == 0) {
1748 pr_err("No arrays found in config file\n");
1755 static int misc_scan(char devmode, struct context *c)
1757 /* apply --detail or --wait-clean to
1758 * all devices in /proc/mdstat
1760 struct mdstat_ent *ms = mdstat_read(0, 1);
1761 struct mdstat_ent *e;
1762 struct map_ent *map = NULL;
1766 for (members = 0; members <= 1; members++) {
1767 for (e=ms ; e ; e=e->next) {
1771 int member = e->metadata_version &&
1772 strncmp(e->metadata_version,
1773 "external:/", 10) == 0;
1774 if (members != member)
1776 me = map_by_devnm(&map, e->devnm);
1778 && strcmp(me->path, "/unknown") != 0)
1780 if (name == NULL || stat(name, &stb) != 0)
1781 name = get_md_name(e->devnm);
1784 pr_err("cannot find device file for %s\n",
1789 rv |= Detail(name, c);
1791 rv |= WaitClean(name, -1, c->verbose);
1799 static int stop_scan(int verbose)
1801 /* apply --stop to all devices in /proc/mdstat */
1802 /* Due to possible stacking of devices, repeat until
1803 * nothing more can be stopped
1805 int progress=1, err;
1809 struct mdstat_ent *ms = mdstat_read(0, 0);
1810 struct mdstat_ent *e;
1812 if (!progress) last = 1;
1813 progress = 0; err = 0;
1814 for (e=ms ; e ; e=e->next) {
1815 char *name = get_md_name(e->devnm);
1819 pr_err("cannot find device file for %s\n",
1823 mdfd = open_mddev(name, 1);
1825 if (Manage_stop(name, mdfd, verbose, !last))
1835 } while (!last && err);
1841 static int misc_list(struct mddev_dev *devlist,
1842 struct mddev_ident *ident,
1843 char *dump_directory,
1844 struct supertype *ss, struct context *c)
1846 struct mddev_dev *dv;
1849 for (dv=devlist ; dv; dv=(rv & 16) ? NULL : dv->next) {
1852 switch(dv->disposition) {
1854 rv |= Detail(dv->devname, c);
1856 case KillOpt: /* Zero superblock */
1858 rv |= Kill(dv->devname, ss, c->force, c->verbose,0);
1862 rv |= Kill(dv->devname, NULL, c->force, v, 0);
1869 rv |= Query(dv->devname); continue;
1871 rv |= ExamineBitmap(dv->devname, c->brief, ss); continue;
1873 rv |= ExamineBadblocks(dv->devname, c->brief, ss); continue;
1876 rv |= Wait(dv->devname); continue;
1878 rv |= WaitClean(dv->devname, -1, c->verbose); continue;
1880 rv |= Kill_subarray(dv->devname, c->subarray, c->verbose);
1882 case UpdateSubarray:
1883 if (c->update == NULL) {
1884 pr_err("-U/--update must be specified with --update-subarray\n");
1888 rv |= Update_subarray(dv->devname, c->subarray,
1889 c->update, ident, c->verbose);
1892 rv |= Dump_metadata(dv->devname, dump_directory, c, ss);
1895 rv |= Restore_metadata(dv->devname, dump_directory, c, ss,
1896 (dv == devlist && dv->next == NULL));
1899 rv |= SetAction(dv->devname, c->action);
1902 if (dv->devname[0] == '/')
1903 mdfd = open_mddev(dv->devname, 1);
1905 mdfd = open_dev(dv->devname);
1907 pr_err("Cannot open %s\n", dv->devname);
1910 switch(dv->disposition) {
1913 rv |= Manage_run(dv->devname, mdfd, c); break;
1915 rv |= Manage_stop(dv->devname, mdfd, c->verbose, 0); break;
1917 rv |= Manage_ro(dv->devname, mdfd, 1); break;
1919 rv |= Manage_ro(dv->devname, mdfd, -1); break;
1928 int SetAction(char *dev, char *action)
1930 int fd = open(dev, O_RDONLY);
1933 pr_err("Couldn't open %s: %s\n", dev, strerror(errno));
1936 sysfs_init(&mdi, fd, NULL);
1938 if (!mdi.sys_name[0]) {
1939 pr_err("%s is no an md array\n", dev);
1943 if (sysfs_set_str(&mdi, NULL, "sync_action", action) < 0) {
1944 pr_err("Count not set action for %s to %s: %s\n",
1945 dev, action, strerror(errno));