2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2012 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.
33 static int scan_assemble(int autof
, struct supertype
*ss
,
34 int readonly
, int runstop
,
35 struct mddev_ident
*ident
,
36 char *homehost
, int require_homehost
,
37 int verbose
, int force
,
39 static int misc_scan(char devmode
, int verbose
, int export
, int test
,
40 char *homehost
, char *prefer
);
41 static int stop_scan(int quiet
);
42 static int misc_list(struct mddev_dev
*devlist
,
43 int brief
, int verbose
, int export
, int test
,
44 char *homehost
, char *prefer
, char *subarray
,
45 char *update
, struct mddev_ident
*ident
,
46 struct supertype
*ss
, int force
, int quiet
);
49 int main(int argc
, char *argv
[])
60 long long array_size
= -1;
63 char *layout_str
= NULL
;
66 struct mddev_ident ident
;
67 char *configfile
= NULL
;
76 char *bitmap_file
= NULL
;
77 char *backup_file
= NULL
;
78 int invalid_backup
= 0;
79 int bitmap_chunk
= UnSet
;
81 struct mddev_dev
*devlist
= NULL
;
82 struct mddev_dev
**devlistend
= & devlist
;
93 char *symlinks
= NULL
;
94 int grow_continue
= 0;
95 /* autof indicates whether and how to create device node.
96 * bottom 3 bits are style. Rest (when shifted) are number of parts
98 * 1 - don't create (no)
99 * 2 - if is_standard, then create (yes)
100 * 3 - create as 'md' - reject is_standard mdp (md)
101 * 4 - create as 'mdp' - reject is_standard md (mdp)
102 * 5 - default to md if not is_standard (md in config file)
103 * 6 - default to mdp if not is_standard (part, or mdp in config file)
107 char *homehost
= NULL
;
108 char sys_hostname
[256];
109 int require_homehost
= 1;
110 char *mailaddr
= NULL
;
111 char *program
= NULL
;
115 char *pidfile
= NULL
;
117 int spare_sharing
= 1;
118 struct supertype
*ss
= NULL
;
121 char *shortopt
= short_options
;
124 char *subarray
= NULL
;
125 char *remove_path
= NULL
;
126 char *udev_filename
= NULL
;
133 int freeze_reshape
= 0;
135 srandom(time(0) ^ getpid());
139 ident
.raid_disks
= UnSet
;
140 ident
.super_minor
= UnSet
;
142 ident
.spare_group
= NULL
;
145 ident
.bitmap_fd
= -1;
146 ident
.bitmap_file
= NULL
;
148 ident
.container
= NULL
;
151 while ((option_index
= -1) ,
152 (opt
=getopt_long(argc
, argv
,
153 shortopt
, long_options
,
154 &option_index
)) != -1) {
156 /* firstly, some mode-independent options */
166 fputs(Version
, stderr
);
176 if (mode
== ASSEMBLE
|| mode
== BUILD
|| mode
== CREATE
177 || mode
== GROW
|| mode
== INCREMENTAL
179 break; /* b means bitmap */
188 if (strcasecmp(optarg
, "<ignore>") == 0)
189 require_homehost
= 0;
195 * --offroot sets first char of argv[0] to @. This is used
196 * by systemd to signal that the task was launched from
197 * initrd/initramfs and should be preserved during shutdown
207 if (asprintf(&prefer
, "/%s/", optarg
) <= 0)
213 fputs(Usage
, stderr
);
216 /* second, figure out the mode.
217 * Some options force the mode. Others
218 * set the mode if it isn't already
224 shortopt
= short_bitmap_options
;
232 case ReAdd
: /* re-add */
235 shortopt
= short_bitmap_options
;
239 case 'A': newmode
= ASSEMBLE
;
240 shortopt
= short_bitmap_auto_options
;
242 case 'B': newmode
= BUILD
;
243 shortopt
= short_bitmap_auto_options
;
245 case 'C': newmode
= CREATE
;
246 shortopt
= short_bitmap_auto_options
;
248 case 'F': newmode
= MONITOR
;
250 case 'G': newmode
= GROW
;
251 shortopt
= short_bitmap_options
;
253 case 'I': newmode
= INCREMENTAL
;
254 shortopt
= short_bitmap_auto_options
;
257 newmode
= AUTODETECT
;
288 if (mode
&& newmode
== mode
) {
289 /* everybody happy ! */
290 } else if (mode
&& newmode
!= mode
) {
293 if (option_index
>= 0)
294 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
296 fprintf(stderr
, "-%c", opt
);
297 fprintf(stderr
, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
298 map_num(modes
, newmode
),
299 map_num(modes
, mode
));
301 } else if (!mode
&& newmode
) {
303 if (mode
== MISC
&& devs_found
) {
304 pr_err("No action given for %s in --misc mode\n",
306 fprintf(stderr
," Action options must come before device names\n");
310 /* special case of -c --help */
311 if ((opt
== 'c' || opt
== ConfigFile
) &&
312 ( strncmp(optarg
, "--h", 3)==0 ||
313 strncmp(optarg
, "-h", 2)==0)) {
314 fputs(Help_config
, stdout
);
318 /* If first option is a device, don't force the mode yet */
320 if (devs_found
== 0) {
321 dv
= malloc(sizeof(*dv
));
323 pr_err("malloc failed\n");
326 dv
->devname
= optarg
;
327 dv
->disposition
= devmode
;
328 dv
->writemostly
= writemostly
;
333 devlistend
= &dv
->next
;
338 /* No mode yet, and this is the second device ... */
339 pr_err("An option must be given to set the mode before a second device\n"
340 " (%s) is listed\n", optarg
);
343 if (option_index
>= 0)
344 pr_err("--%s", long_options
[option_index
].name
);
347 fprintf(stderr
, " does not set the mode, and so cannot be the first option.\n");
351 /* if we just set the mode, then done */
365 /* an undecorated option - must be a device name.
367 if (devs_found
> 0 && mode
== MANAGE
&& !devmode
) {
368 pr_err("Must give one of -a/-r/-f"
369 " for subsequent devices at %s\n", optarg
);
372 if (devs_found
> 0 && mode
== GROW
&& !devmode
) {
373 pr_err("Must give -a/--add for"
374 " devices to add: %s\n", optarg
);
377 dv
= malloc(sizeof(*dv
));
379 pr_err("malloc failed\n");
382 dv
->devname
= optarg
;
383 dv
->disposition
= devmode
;
384 dv
->writemostly
= writemostly
;
389 devlistend
= &dv
->next
;
395 /* We've got a mode, and opt is now something else which
396 * could depend on the mode */
397 #define O(a,b) ((a<<16)|b)
398 switch (O(mode
,opt
)) {
400 case O(GROW
,ChunkSize
):
402 case O(CREATE
,ChunkSize
):
403 case O(BUILD
,'c'): /* chunk or rounding */
404 case O(BUILD
,ChunkSize
): /* chunk or rounding */
406 pr_err("chunk/rounding may only be specified once. "
407 "Second value is %s.\n", optarg
);
410 chunk
= parse_size(optarg
);
411 if (chunk
< 8 || (chunk
&1)) {
412 pr_err("invalid chunk/rounding value: %s\n",
416 /* Convert sectors to K */
420 case O(INCREMENTAL
, 'e'):
422 case O(ASSEMBLE
,'e'):
423 case O(MISC
,'e'): /* set metadata (superblock) information */
425 pr_err("metadata information already given\n");
428 for(i
=0; !ss
&& superlist
[i
]; i
++)
429 ss
= superlist
[i
]->match_metadata_desc(optarg
);
432 pr_err("unrecognised metadata identifier: %s\n", optarg
);
438 case O(MANAGE
,WriteMostly
):
440 case O(BUILD
,WriteMostly
):
442 case O(CREATE
,WriteMostly
):
443 /* set write-mostly for following devices */
448 /* clear write-mostly for following devices */
455 case O(BUILD
,'z'): /* size */
457 pr_err("size may only be specified once. "
458 "Second value is %s.\n", optarg
);
461 if (strcmp(optarg
, "max")==0)
464 size
= parse_size(optarg
);
466 pr_err("invalid size: %s\n",
470 /* convert sectors to K */
475 case O(GROW
,'Z'): /* array size */
476 if (array_size
>= 0) {
477 pr_err("array-size may only be specified once. "
478 "Second value is %s.\n", optarg
);
481 if (strcmp(optarg
, "max") == 0)
484 array_size
= parse_size(optarg
);
485 if (array_size
<= 0) {
486 pr_err("invalid array size: %s\n",
495 case O(BUILD
,'l'): /* set raid level*/
496 if (level
!= UnSet
) {
497 pr_err("raid level may only be set once. "
498 "Second value is %s.\n", optarg
);
501 level
= map_name(pers
, optarg
);
502 if (level
== UnSet
) {
503 pr_err("invalid raid level: %s\n",
507 if (level
!= 0 && level
!= LEVEL_LINEAR
&& level
!= 1 &&
508 level
!= LEVEL_MULTIPATH
&& level
!= LEVEL_FAULTY
&&
511 pr_err("Raid level %s not permitted with --build.\n",
515 if (sparedisks
> 0 && level
< 1 && level
>= -1) {
516 pr_err("raid level %s is incompatible with spare-devices setting.\n",
523 case O(GROW
, 'p'): /* new layout */
524 case O(GROW
, Layout
):
526 pr_err("layout may only be sent once. "
527 "Second value was %s\n", optarg
);
531 /* 'Grow' will parse the value */
534 case O(CREATE
,'p'): /* raid5 layout */
535 case O(CREATE
,Layout
):
536 case O(BUILD
,'p'): /* faulty layout */
537 case O(BUILD
,Layout
):
538 if (layout
!= UnSet
) {
539 pr_err("layout may only be sent once. "
540 "Second value was %s\n", optarg
);
545 pr_err("layout not meaningful for %s arrays.\n",
546 map_num(pers
, level
));
549 pr_err("raid level must be given before layout.\n");
553 layout
= map_name(r5layout
, optarg
);
555 pr_err("layout %s not understood for raid5.\n",
561 layout
= map_name(r6layout
, optarg
);
563 pr_err("layout %s not understood for raid6.\n",
570 layout
= parse_layout_10(optarg
);
572 pr_err("layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg
);
580 layout
= parse_layout_faulty(optarg
);
582 pr_err("layout %s not understood for faulty.\n",
590 case O(CREATE
,AssumeClean
):
591 case O(BUILD
,AssumeClean
): /* assume clean */
592 case O(GROW
,AssumeClean
):
598 case O(BUILD
,'n'): /* number of raid disks */
600 pr_err("raid-devices set twice: %d and %s\n",
604 raiddisks
= strtol(optarg
, &c
, 10);
605 if (!optarg
[0] || *c
|| raiddisks
<=0) {
606 pr_err("invalid number of raid devices: %s\n",
610 ident
.raid_disks
= raiddisks
;
613 case O(CREATE
,'x'): /* number of spare (eXtra) disks */
615 pr_err("spare-devices set twice: %d and %s\n",
619 if (level
!= UnSet
&& level
<= 0 && level
>= -1) {
620 pr_err("spare-devices setting is incompatible with raid level %d\n",
624 sparedisks
= strtol(optarg
, &c
, 10);
625 if (!optarg
[0] || *c
|| sparedisks
< 0) {
626 pr_err("invalid number of spare-devices: %s\n",
636 case O(INCREMENTAL
,'a'):
637 case O(INCREMENTAL
,Auto
):
638 case O(ASSEMBLE
,'a'):
639 case O(ASSEMBLE
,Auto
): /* auto-creation of device node */
640 autof
= parse_auto(optarg
, "--auto flag", 0);
643 case O(CREATE
,Symlinks
):
644 case O(BUILD
,Symlinks
):
645 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
649 case O(BUILD
,'f'): /* force honouring '-n 1' */
650 case O(BUILD
,Force
): /* force honouring '-n 1' */
651 case O(GROW
,'f'): /* ditto */
652 case O(GROW
,Force
): /* ditto */
653 case O(CREATE
,'f'): /* force honouring of device list */
654 case O(CREATE
,Force
): /* force honouring of device list */
655 case O(ASSEMBLE
,'f'): /* force assembly */
656 case O(ASSEMBLE
,Force
): /* force assembly */
657 case O(MISC
,'f'): /* force zero */
658 case O(MISC
,Force
): /* force zero */
659 case O(MANAGE
,Force
): /* add device which is too large */
662 /* now for the Assemble options */
663 case O(ASSEMBLE
, FreezeReshape
): /* Freeze reshape during
665 case O(INCREMENTAL
, FreezeReshape
):
668 case O(CREATE
,'u'): /* uuid of array */
669 case O(ASSEMBLE
,'u'): /* uuid of array */
670 if (ident
.uuid_set
) {
671 pr_err("uuid cannot be set twice. "
672 "Second value %s.\n", optarg
);
675 if (parse_uuid(optarg
, ident
.uuid
))
678 pr_err("Bad uuid: %s\n", optarg
);
684 case O(ASSEMBLE
,'N'):
687 pr_err("name cannot be set twice. "
688 "Second value %s.\n", optarg
);
691 if (mode
== MISC
&& !subarray
) {
692 pr_err("-N/--name only valid with --update-subarray in misc mode\n");
695 if (strlen(optarg
) > 32) {
696 pr_err("name '%s' is too long, 32 chars max.\n",
700 strcpy(ident
.name
, optarg
);
703 case O(ASSEMBLE
,'m'): /* super-minor for array */
704 case O(ASSEMBLE
,SuperMinor
):
705 if (ident
.super_minor
!= UnSet
) {
706 pr_err("super-minor cannot be set twice. "
707 "Second value: %s.\n", optarg
);
710 if (strcmp(optarg
, "dev")==0)
711 ident
.super_minor
= -2;
713 ident
.super_minor
= strtoul(optarg
, &cp
, 10);
714 if (!optarg
[0] || *cp
) {
715 pr_err("Bad super-minor number: %s.\n", optarg
);
721 case O(ASSEMBLE
,'U'): /* update the superblock */
724 pr_err("Can only update one aspect"
725 " of superblock, both %s and %s given.\n",
729 if (mode
== MISC
&& !subarray
) {
730 pr_err("Only subarrays can be"
731 " updated in misc mode\n");
735 if (strcmp(update
, "sparc2.2")==0)
737 if (strcmp(update
, "super-minor") == 0)
739 if (strcmp(update
, "summaries")==0)
741 if (strcmp(update
, "resync")==0)
743 if (strcmp(update
, "uuid")==0)
745 if (strcmp(update
, "name")==0)
747 if (strcmp(update
, "homehost")==0)
749 if (strcmp(update
, "devicesize")==0)
751 if (strcmp(update
, "no-bitmap")==0)
753 if (strcmp(update
, "byteorder")==0) {
755 pr_err("must not set metadata"
756 " type with --update=byteorder.\n");
759 for(i
=0; !ss
&& superlist
[i
]; i
++)
760 ss
= superlist
[i
]->match_metadata_desc(
763 pr_err("INTERNAL ERROR"
764 " cannot find 0.swap\n");
770 if (strcmp(update
,"?") == 0 ||
771 strcmp(update
, "help") == 0) {
773 fprintf(outf
, Name
": ");
777 Name
": '--update=%s' is invalid. ",
780 fprintf(outf
, "Valid --update options are:\n"
781 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
782 " 'summaries', 'homehost', 'byteorder', 'devicesize',\n"
784 exit(outf
== stdout
? 0 : 2);
787 /* update=devicesize is allowed with --re-add */
788 if (devmode
!= 'a' || re_add
!= 1) {
789 pr_err("--update in Manage mode only"
790 " allowed with --re-add.\n");
794 pr_err("Can only update one aspect"
795 " of superblock, both %s and %s given.\n",
800 if (strcmp(update
, "devicesize") != 0) {
801 pr_err("only 'devicesize' can be"
802 " updated with --re-add\n");
807 case O(INCREMENTAL
,NoDegraded
):
808 pr_err("--no-degraded is deprecated in Incremental mode\n");
809 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
810 runstop
= -1; /* --stop isn't allowed for --assemble,
811 * so we overload slightly */
814 case O(ASSEMBLE
,'c'):
815 case O(ASSEMBLE
,ConfigFile
):
816 case O(INCREMENTAL
, 'c'):
817 case O(INCREMENTAL
, ConfigFile
):
819 case O(MISC
, ConfigFile
):
821 case O(MONITOR
,ConfigFile
):
823 pr_err("configfile cannot be set twice. "
824 "Second value is %s.\n", optarg
);
828 set_conffile(configfile
);
829 /* FIXME possibly check that config file exists. Even parse it */
831 case O(ASSEMBLE
,'s'): /* scan */
834 case O(INCREMENTAL
,'s'):
838 case O(MONITOR
,'m'): /* mail address */
839 case O(MONITOR
,EMail
):
841 pr_err("only specify one mailaddress. %s ignored.\n",
847 case O(MONITOR
,'p'): /* alert program */
848 case O(MONITOR
,ProgramOpt
): /* alert program */
850 pr_err("only specify one alter program. %s ignored.\n",
856 case O(MONITOR
,'r'): /* rebuild increments */
857 case O(MONITOR
,Increment
):
858 increments
= atoi(optarg
);
859 if (increments
> 99 || increments
< 1) {
860 pr_err("please specify positive integer between 1 and 99 as rebuild increments.\n");
865 case O(MONITOR
,'d'): /* delay in seconds */
867 case O(BUILD
,'d'): /* delay for bitmap updates */
870 pr_err("only specify delay once. %s ignored.\n",
873 delay
= strtol(optarg
, &c
, 10);
874 if (!optarg
[0] || *c
|| delay
<1) {
875 pr_err("invalid delay: %s\n",
881 case O(MONITOR
,'f'): /* daemonise */
882 case O(MONITOR
,Fork
):
885 case O(MONITOR
,'i'): /* pid */
887 pr_err("only specify one pid file. %s ignored.\n",
892 case O(MONITOR
,'1'): /* oneshot */
896 case O(MONITOR
,'t'): /* test */
899 case O(MONITOR
,'y'): /* log messages to syslog */
900 openlog("mdadm", LOG_PID
, SYSLOG_FACILITY
);
903 case O(MONITOR
, NoSharing
):
907 /* now the general management options. Some are applicable
908 * to other modes. None have arguments.
913 case O(MANAGE
,Add
): /* add a drive */
917 case O(MANAGE
,ReAdd
):
921 case O(MANAGE
,'r'): /* remove a drive */
922 case O(MANAGE
,Remove
):
925 case O(MANAGE
,'f'): /* set faulty */
927 case O(INCREMENTAL
,'f'):
928 case O(INCREMENTAL
,Remove
):
929 case O(INCREMENTAL
,Fail
): /* r for incremental is taken, use f
930 * even though we will both fail and
931 * remove the device */
934 case O(INCREMENTAL
,'R'):
936 case O(ASSEMBLE
,'R'):
938 case O(CREATE
,'R'): /* Run the array */
940 pr_err("Cannot both Stop and Run an array\n");
947 pr_err("Cannot both Run and Stop an array\n");
959 case O(MISC
,KillOpt
):
966 case O(MISC
, WaitOpt
):
967 case O(MISC
, Waitclean
):
968 case O(MISC
, DetailPlatform
):
969 case O(MISC
, KillSubarray
):
970 case O(MISC
, UpdateSubarray
):
971 if (opt
== KillSubarray
|| opt
== UpdateSubarray
) {
973 pr_err("subarray can only"
974 " be specified once\n");
979 if (devmode
&& devmode
!= opt
&&
980 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
981 pr_err("--examine/-E cannot be given with ");
982 if (devmode
== 'E') {
983 if (option_index
>= 0)
984 fprintf(stderr
, "--%s\n",
985 long_options
[option_index
].name
);
987 fprintf(stderr
, "-%c\n", opt
);
988 } else if (isalpha(devmode
))
989 fprintf(stderr
, "-%c\n", devmode
);
991 fprintf(stderr
, "previous option\n");
996 case O(MISC
, UdevRules
):
997 if (devmode
&& devmode
!= opt
) {
998 pr_err("--udev-rules must"
999 " be the only option.\n");
1002 pr_err("only specify one udev "
1003 "rule filename. %s ignored.\n",
1006 udev_filename
= optarg
;
1014 case O(MISC
, Sparc22
):
1015 if (devmode
!= 'E') {
1016 pr_err("--sparc2.2 only allowed with --examine\n");
1022 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
1023 case O(ASSEMBLE
,Bitmap
):
1025 pr_err("bitmap file needed with -b in --assemble mode\n");
1028 if (strcmp(optarg
, "internal")==0) {
1029 pr_err("there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
1032 bitmap_fd
= open(optarg
, O_RDWR
);
1033 if (!*optarg
|| bitmap_fd
< 0) {
1034 pr_err("cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
1037 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
1040 case O(ASSEMBLE
, BackupFile
):
1041 case O(GROW
, BackupFile
):
1042 /* Specify a file into which grow might place a backup,
1043 * or from which assemble might recover a backup
1046 pr_err("backup file already specified, rejecting %s\n", optarg
);
1049 backup_file
= optarg
;
1052 case O(GROW
, Continue
):
1053 /* Continue interrupted grow
1057 case O(ASSEMBLE
, InvalidBackup
):
1058 /* Acknowledge that the backupfile is invalid, but ask
1059 * to continue anyway
1065 case O(BUILD
,Bitmap
):
1067 case O(CREATE
,Bitmap
): /* here we create the bitmap */
1068 if (strcmp(optarg
, "none") == 0) {
1069 pr_err("'--bitmap none' only"
1070 " supported for --grow\n");
1075 case O(GROW
,Bitmap
):
1076 if (strcmp(optarg
, "internal")== 0 ||
1077 strcmp(optarg
, "none")== 0 ||
1078 strchr(optarg
, '/') != NULL
) {
1079 bitmap_file
= optarg
;
1083 pr_err("bitmap file must contain a '/', or be 'internal', or 'none'\n"
1084 " not '%s'\n", optarg
);
1087 case O(GROW
,BitmapChunk
):
1088 case O(BUILD
,BitmapChunk
):
1089 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
1090 bitmap_chunk
= parse_size(optarg
);
1091 if (bitmap_chunk
<= 0 ||
1092 bitmap_chunk
& (bitmap_chunk
- 1)) {
1093 pr_err("invalid bitmap chunksize: %s\n",
1097 bitmap_chunk
= bitmap_chunk
* 512;
1100 case O(GROW
, WriteBehind
):
1101 case O(BUILD
, WriteBehind
):
1102 case O(CREATE
, WriteBehind
): /* write-behind mode */
1103 write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
1105 write_behind
= strtol(optarg
, &c
, 10);
1106 if (write_behind
< 0 || *c
||
1107 write_behind
> 16383) {
1108 pr_err("Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
1114 case O(INCREMENTAL
, 'r'):
1115 case O(INCREMENTAL
, RebuildMapOpt
):
1118 case O(INCREMENTAL
, IncrementalPath
):
1119 remove_path
= optarg
;
1122 /* We have now processed all the valid options. Anything else is
1125 if (option_index
> 0)
1126 pr_err(":option --%s not valid in %s mode\n",
1127 long_options
[option_index
].name
,
1128 map_num(modes
, mode
));
1130 pr_err("option -%c not valid in %s mode\n",
1131 opt
, map_num(modes
, mode
));
1138 if (print_help
== 2)
1139 help_text
= OptionHelp
;
1141 help_text
= mode_help
[mode
];
1142 if (help_text
== NULL
)
1144 fputs(help_text
,stdout
);
1148 if (!mode
&& devs_found
) {
1151 if (devlist
->disposition
== 0)
1152 devlist
->disposition
= devmode
;
1155 fputs(Usage
, stderr
);
1160 struct createinfo
*ci
= conf_get_create_info();
1162 if (strcasecmp(symlinks
, "yes") == 0)
1164 else if (strcasecmp(symlinks
, "no") == 0)
1167 pr_err("option --symlinks must be 'no' or 'yes'\n");
1171 /* Ok, got the option parsing out of the way
1172 * hopefully it's mostly right but there might be some stuff
1175 * That is mosty checked in the per-mode stuff but...
1177 * For @,B,C and A without -s, the first device listed must be
1178 * an md device. We check that here and open it.
1181 if (mode
== MANAGE
|| mode
== BUILD
|| mode
== CREATE
1183 || (mode
== ASSEMBLE
&& ! scan
)) {
1184 if (devs_found
< 1) {
1185 pr_err("an md device must be given in this mode\n");
1188 if ((int)ident
.super_minor
== -2 && autof
) {
1189 pr_err("--super-minor=dev is incompatible with --auto\n");
1192 if (mode
== MANAGE
|| mode
== GROW
) {
1193 mdfd
= open_mddev(devlist
->devname
, 1);
1197 /* non-existent device is OK */
1198 mdfd
= open_mddev(devlist
->devname
, 0);
1200 pr_err("device %s exists but is not an "
1201 "md array.\n", devlist
->devname
);
1204 if ((int)ident
.super_minor
== -2) {
1207 pr_err("--super-minor=dev given, and "
1208 "listed device %s doesn't exist.\n",
1213 ident
.super_minor
= minor(stb
.st_rdev
);
1215 if (mdfd
>= 0 && mode
!= MANAGE
&& mode
!= GROW
) {
1216 /* We don't really want this open yet, we just might
1217 * have wanted to check some things
1225 if (raiddisks
== 1 && !force
&& level
!= LEVEL_FAULTY
) {
1226 pr_err("'1' is an unusual number of drives for an array, so it is probably\n"
1227 " a mistake. If you really mean it you will need to specify --force before\n"
1228 " setting the number of drives.\n");
1233 if (homehost
== NULL
)
1234 homehost
= conf_get_homehost(&require_homehost
);
1235 if (homehost
== NULL
|| strcasecmp(homehost
, "<system>")==0) {
1236 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
1237 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
1238 homehost
= sys_hostname
;
1241 if (homehost
&& (!homehost
[0] || strcasecmp(homehost
, "<none>") == 0)) {
1243 require_homehost
= 0;
1246 if ((mode
== MISC
&& devmode
== 'E')
1247 || (mode
== MONITOR
&& spare_sharing
== 0))
1248 /* Anyone may try this */;
1249 else if (geteuid() != 0) {
1250 pr_err("must be super-user to perform this action\n");
1254 ident
.autof
= autof
;
1259 /* readonly, add/remove, readwrite, runstop */
1261 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1262 if (!rv
&& devs_found
>1)
1263 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
1264 devlist
->next
, verbose
-quiet
, test
,
1266 if (!rv
&& readonly
< 0)
1267 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1269 rv
= Manage_runstop(devlist
->devname
, mdfd
, runstop
, quiet
);
1272 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1273 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !scan
) {
1274 /* Only a device has been given, so get details from config file */
1275 struct mddev_ident
*array_ident
= conf_get_ident(devlist
->devname
);
1276 if (array_ident
== NULL
) {
1277 pr_err("%s not identified in config file.\n",
1283 if (array_ident
->autof
== 0)
1284 array_ident
->autof
= autof
;
1285 rv
|= Assemble(ss
, devlist
->devname
, array_ident
,
1286 NULL
, backup_file
, invalid_backup
,
1287 readonly
, runstop
, update
,
1288 homehost
, require_homehost
,
1289 verbose
-quiet
, force
,
1293 rv
= Assemble(ss
, devlist
->devname
, &ident
,
1294 devlist
->next
, backup_file
, invalid_backup
,
1295 readonly
, runstop
, update
,
1296 homehost
, require_homehost
,
1297 verbose
-quiet
, force
,
1299 else if (devs_found
> 0) {
1300 if (update
&& devs_found
> 1) {
1301 pr_err("can only update a single array at a time\n");
1304 if (backup_file
&& devs_found
> 1) {
1305 pr_err("can only assemble a single array when providing a backup file.\n");
1308 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1309 struct mddev_ident
*array_ident
= conf_get_ident(dv
->devname
);
1310 if (array_ident
== NULL
) {
1311 pr_err("%s not identified in config file.\n",
1316 if (array_ident
->autof
== 0)
1317 array_ident
->autof
= autof
;
1318 rv
|= Assemble(ss
, dv
->devname
, array_ident
,
1319 NULL
, backup_file
, invalid_backup
,
1320 readonly
, runstop
, update
,
1321 homehost
, require_homehost
,
1322 verbose
-quiet
, force
,
1327 pr_err("--update not meaningful with a --scan assembly.\n");
1331 pr_err("--backup_file not meaningful with a --scan assembly.\n");
1334 rv
= scan_assemble(autof
, ss
, readonly
, runstop
,
1338 force
, freeze_reshape
);
1343 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1344 if (write_behind
&& !bitmap_file
) {
1345 pr_err("write-behind mode requires a bitmap.\n");
1349 if (raiddisks
== 0) {
1350 pr_err("no raid-devices specified.\n");
1356 if (strcmp(bitmap_file
, "internal")==0) {
1357 pr_err("'internal' bitmaps not supported with --build\n");
1362 rv
= Build(devlist
->devname
, chunk
, level
, layout
,
1363 raiddisks
, devlist
->next
, assume_clean
,
1364 bitmap_file
, bitmap_chunk
, write_behind
,
1365 delay
, verbose
-quiet
, autof
, size
);
1368 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1369 if (write_behind
&& !bitmap_file
) {
1370 pr_err("write-behind mode requires a bitmap.\n");
1374 if (raiddisks
== 0) {
1375 pr_err("no raid-devices specified.\n");
1380 rv
= Create(ss
, devlist
->devname
, chunk
, level
, layout
, size
<0 ? 0 : size
,
1381 raiddisks
, sparedisks
, ident
.name
, homehost
,
1382 ident
.uuid_set
? ident
.uuid
: NULL
,
1383 devs_found
-1, devlist
->next
, runstop
, verbose
-quiet
, force
, assume_clean
,
1384 bitmap_file
, bitmap_chunk
, write_behind
, delay
, autof
);
1387 if (devmode
== 'E') {
1388 if (devlist
== NULL
&& !scan
) {
1389 pr_err("No devices to examine\n");
1392 if (devlist
== NULL
)
1393 devlist
= conf_get_devs();
1394 if (devlist
== NULL
) {
1395 pr_err("No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1398 if (brief
&& verbose
)
1400 rv
= Examine(devlist
, scan
?(verbose
>1?0:verbose
+1):brief
,
1402 SparcAdjust
, ss
, homehost
);
1403 } else if (devmode
== DetailPlatform
) {
1404 rv
= Detail_Platform(ss
? ss
->ss
: NULL
, ss
? scan
: 1, verbose
);
1405 } else if (devlist
== NULL
) {
1406 if (devmode
== 'S' && scan
)
1407 rv
= stop_scan(quiet
);
1408 else if ((devmode
== 'D' || devmode
== Waitclean
) && scan
)
1409 rv
= misc_scan(devmode
, verbose
, export
,
1410 test
, homehost
, prefer
);
1411 else if (devmode
== UdevRules
)
1412 rv
= Write_rules(udev_filename
);
1414 pr_err("No devices given.\n");
1418 rv
= misc_list(devlist
, brief
, verbose
, export
, test
,
1419 homehost
, prefer
, subarray
, update
,
1424 if (!devlist
&& !scan
) {
1425 pr_err("Cannot monitor: need --scan or at least one device\n");
1429 if (pidfile
&& !daemonise
) {
1430 pr_err("Cannot write a pid file when not in daemon mode\n");
1435 if (get_linux_version() > 2006016)
1436 /* mdstat responds to poll */
1441 rv
= Monitor(devlist
, mailaddr
, program
,
1442 delay
?delay
:60, daemonise
, scan
, oneshot
,
1443 dosyslog
, test
, pidfile
, increments
,
1444 spare_sharing
, prefer
);
1448 if (array_size
>= 0) {
1449 /* alway impose array size first, independent of
1451 * Do not allow level or raid_disks changes at the
1452 * same time as that can be irreversibly destructive.
1456 if (raiddisks
|| level
!= UnSet
) {
1457 pr_err("cannot change array size in same operation "
1458 "as changing raiddisks or level.\n"
1459 " Change size first, then check that data is still intact.\n");
1463 sysfs_init(&sra
, mdfd
, 0);
1464 if (array_size
== 0)
1465 err
= sysfs_set_str(&sra
, NULL
, "array_size", "default");
1467 err
= sysfs_set_num(&sra
, NULL
, "array_size", array_size
/ 2);
1470 pr_err("--array-size setting"
1471 " is too large.\n");
1473 pr_err("current kernel does"
1474 " not support setting --array-size\n");
1479 if (devs_found
> 1 && raiddisks
== 0) {
1481 if (size
>= 0 || chunk
|| layout_str
!= NULL
|| bitmap_file
) {
1482 pr_err("--add cannot be used with "
1483 "other geometry changes in --grow mode\n");
1487 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1488 rv
= Grow_Add_device(devlist
->devname
, mdfd
,
1493 } else if (bitmap_file
) {
1494 if (size
>= 0 || raiddisks
|| chunk
||
1495 layout_str
!= NULL
|| devs_found
> 1) {
1496 pr_err("--bitmap changes cannot be "
1497 "used with other geometry changes "
1498 "in --grow mode\n");
1503 delay
= DEFAULT_BITMAP_DELAY
;
1504 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, bitmap_file
,
1505 bitmap_chunk
, delay
, write_behind
, force
);
1506 } else if (grow_continue
)
1507 rv
= Grow_continue_command(devlist
->devname
,
1510 else if (size
>= 0 || raiddisks
!= 0 || layout_str
!= NULL
1511 || chunk
!= 0 || level
!= UnSet
) {
1512 rv
= Grow_reshape(devlist
->devname
, mdfd
, quiet
, backup_file
,
1513 size
, level
, layout_str
, chunk
, raiddisks
,
1515 assume_clean
, force
);
1516 } else if (array_size
< 0)
1517 pr_err("no changes to --grow\n");
1525 pr_err("--incremental --scan meaningless without --run.\n");
1528 if (devmode
== 'f') {
1529 pr_err("--incremental --scan --fail not supported.\n");
1532 rv
= IncrementalScan(verbose
);
1535 if (!rebuild_map
&& !scan
) {
1536 pr_err("--incremental requires a device.\n");
1541 if (devlist
->next
) {
1542 pr_err("--incremental can only handle one device.\n");
1547 rv
= IncrementalRemove(devlist
->devname
, remove_path
,
1550 rv
= Incremental(devlist
->devname
, verbose
-quiet
,
1551 runstop
, ss
, homehost
,
1552 require_homehost
, autof
,
1562 static int scan_assemble(int autof
, struct supertype
*ss
,
1563 int readonly
, int runstop
,
1564 struct mddev_ident
*ident
,
1565 char *homehost
, int require_homehost
,
1566 int verbose
, int force
,
1569 struct mddev_ident
*a
, *array_list
= conf_get_ident(NULL
);
1570 struct mddev_dev
*devlist
= conf_get_devs();
1571 struct map_ent
*map
= NULL
;
1574 int failures
, successes
;
1576 if (conf_verify_devnames(array_list
)) {
1577 pr_err("Duplicate MD device names in "
1578 "conf file were found.\n");
1581 if (devlist
== NULL
) {
1582 pr_err("No devices listed in conf file were found.\n");
1585 for (a
= array_list
; a
; a
= a
->next
) {
1591 pr_err("%s: failed to get "
1592 "exclusive lock on mapfile\n",
1598 for (a
= array_list
; a
; a
= a
->next
) {
1603 strcasecmp(a
->devname
, "<ignore>") == 0)
1606 r
= Assemble(ss
, a
->devname
,
1609 readonly
, runstop
, NULL
,
1610 homehost
, require_homehost
,
1621 } while (failures
&& successes
);
1622 if (homehost
&& cnt
== 0) {
1623 /* Maybe we can auto-assemble something.
1624 * Repeatedly call Assemble in auto-assemble mode
1629 ident
->autof
= autof
;
1631 struct mddev_dev
*devlist
= conf_get_devs();
1634 rv2
= Assemble(ss
, NULL
,
1649 /* Incase there are stacked devices, we need to go around again */
1651 if (cnt
== 0 && rv
== 0) {
1652 pr_err("No arrays found in config file or automatically\n");
1656 } else if (cnt
== 0 && rv
== 0) {
1657 pr_err("No arrays found in config file\n");
1664 static int misc_scan(char devmode
, int verbose
, int export
, int test
,
1665 char *homehost
, char *prefer
)
1667 /* apply --detail or --wait-clean to
1668 * all devices in /proc/mdstat
1670 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1671 struct mdstat_ent
*e
;
1672 struct map_ent
*map
= NULL
;
1674 int v
= verbose
>1?0:verbose
+1;
1677 for (members
= 0; members
<= 1; members
++) {
1678 for (e
=ms
; e
; e
=e
->next
) {
1681 int member
= e
->metadata_version
&&
1682 strncmp(e
->metadata_version
,
1683 "external:/", 10) == 0;
1684 if (members
!= member
)
1686 me
= map_by_devnum(&map
, e
->devnum
);
1688 && strcmp(me
->path
, "/unknown") != 0)
1691 name
= get_md_name(e
->devnum
);
1694 pr_err("cannot find device file for %s\n",
1699 rv
|= Detail(name
, v
,
1703 rv
|= WaitClean(name
, -1, v
);
1711 static int stop_scan(int quiet
)
1713 /* apply --stop to all devices in /proc/mdstat */
1714 /* Due to possible stacking of devices, repeat until
1715 * nothing more can be stopped
1717 int progress
=1, err
;
1721 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1722 struct mdstat_ent
*e
;
1724 if (!progress
) last
= 1;
1725 progress
= 0; err
= 0;
1726 for (e
=ms
; e
; e
=e
->next
) {
1727 char *name
= get_md_name(e
->devnum
);
1731 pr_err("cannot find device file for %s\n",
1735 mdfd
= open_mddev(name
, 1);
1737 if (Manage_runstop(name
, mdfd
, -1, quiet
?1:last
?0:-1))
1747 } while (!last
&& err
);
1753 static int misc_list(struct mddev_dev
*devlist
,
1754 int brief
, int verbose
, int export
, int test
,
1755 char *homehost
, char *prefer
, char *subarray
,
1756 char *update
, struct mddev_ident
*ident
,
1757 struct supertype
*ss
, int force
, int quiet
)
1759 struct mddev_dev
*dv
;
1762 for (dv
=devlist
; dv
; dv
=dv
->next
) {
1765 switch(dv
->disposition
) {
1767 rv
|= Detail(dv
->devname
,
1769 export
, test
, homehost
, prefer
);
1771 case KillOpt
: /* Zero superblock */
1773 rv
|= Kill(dv
->devname
, ss
, force
, quiet
,0);
1777 rv
|= Kill(dv
->devname
, NULL
, force
, q
, 0);
1784 rv
|= Query(dv
->devname
); continue;
1786 rv
|= ExamineBitmap(dv
->devname
, brief
, ss
); continue;
1789 rv
|= Wait(dv
->devname
); continue;
1791 rv
|= WaitClean(dv
->devname
, -1, verbose
-quiet
); continue;
1793 rv
|= Kill_subarray(dv
->devname
, subarray
, quiet
);
1795 case UpdateSubarray
:
1796 if (update
== NULL
) {
1797 pr_err("-U/--update must be specified with --update-subarray\n");
1801 rv
|= Update_subarray(dv
->devname
, subarray
,
1802 update
, ident
, quiet
);
1805 mdfd
= open_mddev(dv
->devname
, 1);
1807 switch(dv
->disposition
) {
1809 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1, quiet
); break;
1811 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1, quiet
); break;
1813 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1815 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;