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
[])
59 long long array_size
= -1;
62 char *layout_str
= NULL
;
65 struct mddev_ident ident
;
66 char *configfile
= NULL
;
74 char *bitmap_file
= NULL
;
75 char *backup_file
= NULL
;
76 int invalid_backup
= 0;
77 int bitmap_chunk
= UnSet
;
79 struct mddev_dev
*devlist
= NULL
;
80 struct mddev_dev
**devlistend
= & devlist
;
91 char *symlinks
= NULL
;
92 int grow_continue
= 0;
93 /* autof indicates whether and how to create device node.
94 * bottom 3 bits are style. Rest (when shifted) are number of parts
96 * 1 - don't create (no)
97 * 2 - if is_standard, then create (yes)
98 * 3 - create as 'md' - reject is_standard mdp (md)
99 * 4 - create as 'mdp' - reject is_standard md (mdp)
100 * 5 - default to md if not is_standard (md in config file)
101 * 6 - default to mdp if not is_standard (part, or mdp in config file)
105 char *homehost
= NULL
;
106 char sys_hostname
[256];
107 int require_homehost
= 1;
108 char *mailaddr
= NULL
;
109 char *program
= NULL
;
113 char *pidfile
= NULL
;
115 int spare_sharing
= 1;
116 struct supertype
*ss
= NULL
;
118 char *shortopt
= short_options
;
121 char *subarray
= NULL
;
122 char *remove_path
= NULL
;
123 char *udev_filename
= NULL
;
130 int freeze_reshape
= 0;
132 srandom(time(0) ^ getpid());
136 ident
.raid_disks
= UnSet
;
137 ident
.super_minor
= UnSet
;
139 ident
.spare_group
= NULL
;
142 ident
.bitmap_fd
= -1;
143 ident
.bitmap_file
= NULL
;
145 ident
.container
= NULL
;
148 while ((option_index
= -1) ,
149 (opt
=getopt_long(argc
, argv
,
150 shortopt
, long_options
,
151 &option_index
)) != -1) {
153 /* firstly, some mode-independent options */
163 fputs(Version
, stderr
);
173 if (mode
== ASSEMBLE
|| mode
== BUILD
|| mode
== CREATE
174 || mode
== GROW
|| mode
== INCREMENTAL
176 break; /* b means bitmap */
185 if (strcasecmp(optarg
, "<ignore>") == 0)
186 require_homehost
= 0;
192 * --offroot sets first char of argv[0] to @. This is used
193 * by systemd to signal that the task was launched from
194 * initrd/initramfs and should be preserved during shutdown
204 if (asprintf(&prefer
, "/%s/", optarg
) <= 0)
210 fputs(Usage
, stderr
);
213 /* second, figure out the mode.
214 * Some options force the mode. Others
215 * set the mode if it isn't already
221 shortopt
= short_bitmap_options
;
229 case ReAdd
: /* re-add */
232 shortopt
= short_bitmap_options
;
236 case 'A': newmode
= ASSEMBLE
;
237 shortopt
= short_bitmap_auto_options
;
239 case 'B': newmode
= BUILD
;
240 shortopt
= short_bitmap_auto_options
;
242 case 'C': newmode
= CREATE
;
243 shortopt
= short_bitmap_auto_options
;
245 case 'F': newmode
= MONITOR
;
247 case 'G': newmode
= GROW
;
248 shortopt
= short_bitmap_options
;
250 case 'I': newmode
= INCREMENTAL
;
251 shortopt
= short_bitmap_auto_options
;
254 newmode
= AUTODETECT
;
285 if (mode
&& newmode
== mode
) {
286 /* everybody happy ! */
287 } else if (mode
&& newmode
!= mode
) {
290 if (option_index
>= 0)
291 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
293 fprintf(stderr
, "-%c", opt
);
294 fprintf(stderr
, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
295 map_num(modes
, newmode
),
296 map_num(modes
, mode
));
298 } else if (!mode
&& newmode
) {
300 if (mode
== MISC
&& devs_found
) {
301 pr_err("No action given for %s in --misc mode\n",
303 fprintf(stderr
," Action options must come before device names\n");
307 /* special case of -c --help */
308 if ((opt
== 'c' || opt
== ConfigFile
) &&
309 ( strncmp(optarg
, "--h", 3)==0 ||
310 strncmp(optarg
, "-h", 2)==0)) {
311 fputs(Help_config
, stdout
);
315 /* If first option is a device, don't force the mode yet */
317 if (devs_found
== 0) {
318 dv
= xmalloc(sizeof(*dv
));
319 dv
->devname
= optarg
;
320 dv
->disposition
= devmode
;
321 dv
->writemostly
= writemostly
;
325 devlistend
= &dv
->next
;
330 /* No mode yet, and this is the second device ... */
331 pr_err("An option must be given to set the mode before a second device\n"
332 " (%s) is listed\n", optarg
);
335 if (option_index
>= 0)
336 pr_err("--%s", long_options
[option_index
].name
);
339 fprintf(stderr
, " does not set the mode, and so cannot be the first option.\n");
343 /* if we just set the mode, then done */
357 /* an undecorated option - must be a device name.
359 if (devs_found
> 0 && mode
== MANAGE
&& !devmode
) {
360 pr_err("Must give one of -a/-r/-f"
361 " for subsequent devices at %s\n", optarg
);
364 if (devs_found
> 0 && mode
== GROW
&& !devmode
) {
365 pr_err("Must give -a/--add for"
366 " devices to add: %s\n", optarg
);
369 dv
= xmalloc(sizeof(*dv
));
370 dv
->devname
= optarg
;
371 dv
->disposition
= devmode
;
372 dv
->writemostly
= writemostly
;
376 devlistend
= &dv
->next
;
382 /* We've got a mode, and opt is now something else which
383 * could depend on the mode */
384 #define O(a,b) ((a<<16)|b)
385 switch (O(mode
,opt
)) {
387 case O(GROW
,ChunkSize
):
389 case O(CREATE
,ChunkSize
):
390 case O(BUILD
,'c'): /* chunk or rounding */
391 case O(BUILD
,ChunkSize
): /* chunk or rounding */
393 pr_err("chunk/rounding may only be specified once. "
394 "Second value is %s.\n", optarg
);
397 chunk
= parse_size(optarg
);
398 if (chunk
< 8 || (chunk
&1)) {
399 pr_err("invalid chunk/rounding value: %s\n",
403 /* Convert sectors to K */
407 case O(INCREMENTAL
, 'e'):
409 case O(ASSEMBLE
,'e'):
410 case O(MISC
,'e'): /* set metadata (superblock) information */
412 pr_err("metadata information already given\n");
415 for(i
=0; !ss
&& superlist
[i
]; i
++)
416 ss
= superlist
[i
]->match_metadata_desc(optarg
);
419 pr_err("unrecognised metadata identifier: %s\n", optarg
);
425 case O(MANAGE
,WriteMostly
):
427 case O(BUILD
,WriteMostly
):
429 case O(CREATE
,WriteMostly
):
430 /* set write-mostly for following devices */
435 /* clear write-mostly for following devices */
442 case O(BUILD
,'z'): /* size */
444 pr_err("size may only be specified once. "
445 "Second value is %s.\n", optarg
);
448 if (strcmp(optarg
, "max")==0)
451 size
= parse_size(optarg
);
453 pr_err("invalid size: %s\n",
457 /* convert sectors to K */
462 case O(GROW
,'Z'): /* array size */
463 if (array_size
>= 0) {
464 pr_err("array-size may only be specified once. "
465 "Second value is %s.\n", optarg
);
468 if (strcmp(optarg
, "max") == 0)
471 array_size
= parse_size(optarg
);
472 if (array_size
<= 0) {
473 pr_err("invalid array size: %s\n",
482 case O(BUILD
,'l'): /* set raid level*/
483 if (level
!= UnSet
) {
484 pr_err("raid level may only be set once. "
485 "Second value is %s.\n", optarg
);
488 level
= map_name(pers
, optarg
);
489 if (level
== UnSet
) {
490 pr_err("invalid raid level: %s\n",
494 if (level
!= 0 && level
!= LEVEL_LINEAR
&& level
!= 1 &&
495 level
!= LEVEL_MULTIPATH
&& level
!= LEVEL_FAULTY
&&
498 pr_err("Raid level %s not permitted with --build.\n",
502 if (sparedisks
> 0 && level
< 1 && level
>= -1) {
503 pr_err("raid level %s is incompatible with spare-devices setting.\n",
510 case O(GROW
, 'p'): /* new layout */
511 case O(GROW
, Layout
):
513 pr_err("layout may only be sent once. "
514 "Second value was %s\n", optarg
);
518 /* 'Grow' will parse the value */
521 case O(CREATE
,'p'): /* raid5 layout */
522 case O(CREATE
,Layout
):
523 case O(BUILD
,'p'): /* faulty layout */
524 case O(BUILD
,Layout
):
525 if (layout
!= UnSet
) {
526 pr_err("layout may only be sent once. "
527 "Second value was %s\n", optarg
);
532 pr_err("layout not meaningful for %s arrays.\n",
533 map_num(pers
, level
));
536 pr_err("raid level must be given before layout.\n");
540 layout
= map_name(r5layout
, optarg
);
542 pr_err("layout %s not understood for raid5.\n",
548 layout
= map_name(r6layout
, optarg
);
550 pr_err("layout %s not understood for raid6.\n",
557 layout
= parse_layout_10(optarg
);
559 pr_err("layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg
);
567 layout
= parse_layout_faulty(optarg
);
569 pr_err("layout %s not understood for faulty.\n",
577 case O(CREATE
,AssumeClean
):
578 case O(BUILD
,AssumeClean
): /* assume clean */
579 case O(GROW
,AssumeClean
):
585 case O(BUILD
,'n'): /* number of raid disks */
587 pr_err("raid-devices set twice: %d and %s\n",
591 raiddisks
= parse_num(optarg
);
592 if (raiddisks
<= 0) {
593 pr_err("invalid number of raid devices: %s\n",
597 ident
.raid_disks
= raiddisks
;
600 case O(CREATE
,'x'): /* number of spare (eXtra) disks */
602 pr_err("spare-devices set twice: %d and %s\n",
606 if (level
!= UnSet
&& level
<= 0 && level
>= -1) {
607 pr_err("spare-devices setting is incompatible with raid level %d\n",
611 sparedisks
= parse_num(optarg
);
612 if (sparedisks
< 0) {
613 pr_err("invalid number of spare-devices: %s\n",
623 case O(INCREMENTAL
,'a'):
624 case O(INCREMENTAL
,Auto
):
625 case O(ASSEMBLE
,'a'):
626 case O(ASSEMBLE
,Auto
): /* auto-creation of device node */
627 autof
= parse_auto(optarg
, "--auto flag", 0);
630 case O(CREATE
,Symlinks
):
631 case O(BUILD
,Symlinks
):
632 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
636 case O(BUILD
,'f'): /* force honouring '-n 1' */
637 case O(BUILD
,Force
): /* force honouring '-n 1' */
638 case O(GROW
,'f'): /* ditto */
639 case O(GROW
,Force
): /* ditto */
640 case O(CREATE
,'f'): /* force honouring of device list */
641 case O(CREATE
,Force
): /* force honouring of device list */
642 case O(ASSEMBLE
,'f'): /* force assembly */
643 case O(ASSEMBLE
,Force
): /* force assembly */
644 case O(MISC
,'f'): /* force zero */
645 case O(MISC
,Force
): /* force zero */
646 case O(MANAGE
,Force
): /* add device which is too large */
649 /* now for the Assemble options */
650 case O(ASSEMBLE
, FreezeReshape
): /* Freeze reshape during
652 case O(INCREMENTAL
, FreezeReshape
):
655 case O(CREATE
,'u'): /* uuid of array */
656 case O(ASSEMBLE
,'u'): /* uuid of array */
657 if (ident
.uuid_set
) {
658 pr_err("uuid cannot be set twice. "
659 "Second value %s.\n", optarg
);
662 if (parse_uuid(optarg
, ident
.uuid
))
665 pr_err("Bad uuid: %s\n", optarg
);
671 case O(ASSEMBLE
,'N'):
674 pr_err("name cannot be set twice. "
675 "Second value %s.\n", optarg
);
678 if (mode
== MISC
&& !subarray
) {
679 pr_err("-N/--name only valid with --update-subarray in misc mode\n");
682 if (strlen(optarg
) > 32) {
683 pr_err("name '%s' is too long, 32 chars max.\n",
687 strcpy(ident
.name
, optarg
);
690 case O(ASSEMBLE
,'m'): /* super-minor for array */
691 case O(ASSEMBLE
,SuperMinor
):
692 if (ident
.super_minor
!= UnSet
) {
693 pr_err("super-minor cannot be set twice. "
694 "Second value: %s.\n", optarg
);
697 if (strcmp(optarg
, "dev")==0)
698 ident
.super_minor
= -2;
700 ident
.super_minor
= parse_num(optarg
);
701 if (ident
.super_minor
< 0) {
702 pr_err("Bad super-minor number: %s.\n", optarg
);
708 case O(ASSEMBLE
,'o'):
714 case O(ASSEMBLE
,'U'): /* update the superblock */
717 pr_err("Can only update one aspect"
718 " of superblock, both %s and %s given.\n",
722 if (mode
== MISC
&& !subarray
) {
723 pr_err("Only subarrays can be"
724 " updated in misc mode\n");
728 if (strcmp(update
, "sparc2.2")==0)
730 if (strcmp(update
, "super-minor") == 0)
732 if (strcmp(update
, "summaries")==0)
734 if (strcmp(update
, "resync")==0)
736 if (strcmp(update
, "uuid")==0)
738 if (strcmp(update
, "name")==0)
740 if (strcmp(update
, "homehost")==0)
742 if (strcmp(update
, "devicesize")==0)
744 if (strcmp(update
, "no-bitmap")==0)
746 if (strcmp(update
, "byteorder")==0) {
748 pr_err("must not set metadata"
749 " type with --update=byteorder.\n");
752 for(i
=0; !ss
&& superlist
[i
]; i
++)
753 ss
= superlist
[i
]->match_metadata_desc(
756 pr_err("INTERNAL ERROR"
757 " cannot find 0.swap\n");
763 if (strcmp(update
,"?") == 0 ||
764 strcmp(update
, "help") == 0) {
766 fprintf(outf
, Name
": ");
770 Name
": '--update=%s' is invalid. ",
773 fprintf(outf
, "Valid --update options are:\n"
774 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
775 " 'summaries', 'homehost', 'byteorder', 'devicesize',\n"
777 exit(outf
== stdout
? 0 : 2);
780 /* update=devicesize is allowed with --re-add */
781 if (devmode
!= 'A') {
782 pr_err("--update in Manage mode only"
783 " allowed with --re-add.\n");
787 pr_err("Can only update one aspect"
788 " of superblock, both %s and %s given.\n",
793 if (strcmp(update
, "devicesize") != 0) {
794 pr_err("only 'devicesize' can be"
795 " updated with --re-add\n");
800 case O(INCREMENTAL
,NoDegraded
):
801 pr_err("--no-degraded is deprecated in Incremental mode\n");
802 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
803 runstop
= -1; /* --stop isn't allowed for --assemble,
804 * so we overload slightly */
807 case O(ASSEMBLE
,'c'):
808 case O(ASSEMBLE
,ConfigFile
):
809 case O(INCREMENTAL
, 'c'):
810 case O(INCREMENTAL
, ConfigFile
):
812 case O(MISC
, ConfigFile
):
814 case O(MONITOR
,ConfigFile
):
816 pr_err("configfile cannot be set twice. "
817 "Second value is %s.\n", optarg
);
821 set_conffile(configfile
);
822 /* FIXME possibly check that config file exists. Even parse it */
824 case O(ASSEMBLE
,'s'): /* scan */
827 case O(INCREMENTAL
,'s'):
831 case O(MONITOR
,'m'): /* mail address */
832 case O(MONITOR
,EMail
):
834 pr_err("only specify one mailaddress. %s ignored.\n",
840 case O(MONITOR
,'p'): /* alert program */
841 case O(MONITOR
,ProgramOpt
): /* alert program */
843 pr_err("only specify one alter program. %s ignored.\n",
849 case O(MONITOR
,'r'): /* rebuild increments */
850 case O(MONITOR
,Increment
):
851 increments
= atoi(optarg
);
852 if (increments
> 99 || increments
< 1) {
853 pr_err("please specify positive integer between 1 and 99 as rebuild increments.\n");
858 case O(MONITOR
,'d'): /* delay in seconds */
860 case O(BUILD
,'d'): /* delay for bitmap updates */
863 pr_err("only specify delay once. %s ignored.\n",
866 delay
= parse_num(optarg
);
868 pr_err("invalid delay: %s\n",
874 case O(MONITOR
,'f'): /* daemonise */
875 case O(MONITOR
,Fork
):
878 case O(MONITOR
,'i'): /* pid */
880 pr_err("only specify one pid file. %s ignored.\n",
885 case O(MONITOR
,'1'): /* oneshot */
889 case O(MONITOR
,'t'): /* test */
892 case O(MONITOR
,'y'): /* log messages to syslog */
893 openlog("mdadm", LOG_PID
, SYSLOG_FACILITY
);
896 case O(MONITOR
, NoSharing
):
900 /* now the general management options. Some are applicable
901 * to other modes. None have arguments.
906 case O(MANAGE
,Add
): /* add a drive */
909 case O(MANAGE
,ReAdd
):
912 case O(MANAGE
,'r'): /* remove a drive */
913 case O(MANAGE
,Remove
):
916 case O(MANAGE
,'f'): /* set faulty */
918 case O(INCREMENTAL
,'f'):
919 case O(INCREMENTAL
,Remove
):
920 case O(INCREMENTAL
,Fail
): /* r for incremental is taken, use f
921 * even though we will both fail and
922 * remove the device */
925 case O(INCREMENTAL
,'R'):
927 case O(ASSEMBLE
,'R'):
929 case O(CREATE
,'R'): /* Run the array */
931 pr_err("Cannot both Stop and Run an array\n");
938 pr_err("Cannot both Run and Stop an array\n");
950 case O(MISC
,KillOpt
):
957 case O(MISC
, WaitOpt
):
958 case O(MISC
, Waitclean
):
959 case O(MISC
, DetailPlatform
):
960 case O(MISC
, KillSubarray
):
961 case O(MISC
, UpdateSubarray
):
962 if (opt
== KillSubarray
|| opt
== UpdateSubarray
) {
964 pr_err("subarray can only"
965 " be specified once\n");
970 if (devmode
&& devmode
!= opt
&&
971 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
972 pr_err("--examine/-E cannot be given with ");
973 if (devmode
== 'E') {
974 if (option_index
>= 0)
975 fprintf(stderr
, "--%s\n",
976 long_options
[option_index
].name
);
978 fprintf(stderr
, "-%c\n", opt
);
979 } else if (isalpha(devmode
))
980 fprintf(stderr
, "-%c\n", devmode
);
982 fprintf(stderr
, "previous option\n");
987 case O(MISC
, UdevRules
):
988 if (devmode
&& devmode
!= opt
) {
989 pr_err("--udev-rules must"
990 " be the only option.\n");
993 pr_err("only specify one udev "
994 "rule filename. %s ignored.\n",
997 udev_filename
= optarg
;
1005 case O(MISC
, Sparc22
):
1006 if (devmode
!= 'E') {
1007 pr_err("--sparc2.2 only allowed with --examine\n");
1013 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
1014 case O(ASSEMBLE
,Bitmap
):
1016 pr_err("bitmap file needed with -b in --assemble mode\n");
1019 if (strcmp(optarg
, "internal")==0) {
1020 pr_err("there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
1023 bitmap_fd
= open(optarg
, O_RDWR
);
1024 if (!*optarg
|| bitmap_fd
< 0) {
1025 pr_err("cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
1028 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
1031 case O(ASSEMBLE
, BackupFile
):
1032 case O(GROW
, BackupFile
):
1033 /* Specify a file into which grow might place a backup,
1034 * or from which assemble might recover a backup
1037 pr_err("backup file already specified, rejecting %s\n", optarg
);
1040 backup_file
= optarg
;
1043 case O(GROW
, Continue
):
1044 /* Continue interrupted grow
1048 case O(ASSEMBLE
, InvalidBackup
):
1049 /* Acknowledge that the backupfile is invalid, but ask
1050 * to continue anyway
1056 case O(BUILD
,Bitmap
):
1058 case O(CREATE
,Bitmap
): /* here we create the bitmap */
1059 if (strcmp(optarg
, "none") == 0) {
1060 pr_err("'--bitmap none' only"
1061 " supported for --grow\n");
1066 case O(GROW
,Bitmap
):
1067 if (strcmp(optarg
, "internal")== 0 ||
1068 strcmp(optarg
, "none")== 0 ||
1069 strchr(optarg
, '/') != NULL
) {
1070 bitmap_file
= optarg
;
1074 pr_err("bitmap file must contain a '/', or be 'internal', or 'none'\n"
1075 " not '%s'\n", optarg
);
1078 case O(GROW
,BitmapChunk
):
1079 case O(BUILD
,BitmapChunk
):
1080 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
1081 bitmap_chunk
= parse_size(optarg
);
1082 if (bitmap_chunk
<= 0 ||
1083 bitmap_chunk
& (bitmap_chunk
- 1)) {
1084 pr_err("invalid bitmap chunksize: %s\n",
1088 bitmap_chunk
= bitmap_chunk
* 512;
1091 case O(GROW
, WriteBehind
):
1092 case O(BUILD
, WriteBehind
):
1093 case O(CREATE
, WriteBehind
): /* write-behind mode */
1094 write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
1096 write_behind
= parse_num(optarg
);
1097 if (write_behind
< 0 ||
1098 write_behind
> 16383) {
1099 pr_err("Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
1105 case O(INCREMENTAL
, 'r'):
1106 case O(INCREMENTAL
, RebuildMapOpt
):
1109 case O(INCREMENTAL
, IncrementalPath
):
1110 remove_path
= optarg
;
1113 /* We have now processed all the valid options. Anything else is
1116 if (option_index
> 0)
1117 pr_err(":option --%s not valid in %s mode\n",
1118 long_options
[option_index
].name
,
1119 map_num(modes
, mode
));
1121 pr_err("option -%c not valid in %s mode\n",
1122 opt
, map_num(modes
, mode
));
1129 if (print_help
== 2)
1130 help_text
= OptionHelp
;
1132 help_text
= mode_help
[mode
];
1133 if (help_text
== NULL
)
1135 fputs(help_text
,stdout
);
1139 if (!mode
&& devs_found
) {
1142 if (devlist
->disposition
== 0)
1143 devlist
->disposition
= devmode
;
1146 fputs(Usage
, stderr
);
1151 struct createinfo
*ci
= conf_get_create_info();
1153 if (strcasecmp(symlinks
, "yes") == 0)
1155 else if (strcasecmp(symlinks
, "no") == 0)
1158 pr_err("option --symlinks must be 'no' or 'yes'\n");
1162 /* Ok, got the option parsing out of the way
1163 * hopefully it's mostly right but there might be some stuff
1166 * That is mosty checked in the per-mode stuff but...
1168 * For @,B,C and A without -s, the first device listed must be
1169 * an md device. We check that here and open it.
1172 if (mode
== MANAGE
|| mode
== BUILD
|| mode
== CREATE
1174 || (mode
== ASSEMBLE
&& ! scan
)) {
1175 if (devs_found
< 1) {
1176 pr_err("an md device must be given in this mode\n");
1179 if ((int)ident
.super_minor
== -2 && autof
) {
1180 pr_err("--super-minor=dev is incompatible with --auto\n");
1183 if (mode
== MANAGE
|| mode
== GROW
) {
1184 mdfd
= open_mddev(devlist
->devname
, 1);
1188 /* non-existent device is OK */
1189 mdfd
= open_mddev(devlist
->devname
, 0);
1191 pr_err("device %s exists but is not an "
1192 "md array.\n", devlist
->devname
);
1195 if ((int)ident
.super_minor
== -2) {
1198 pr_err("--super-minor=dev given, and "
1199 "listed device %s doesn't exist.\n",
1204 ident
.super_minor
= minor(stb
.st_rdev
);
1206 if (mdfd
>= 0 && mode
!= MANAGE
&& mode
!= GROW
) {
1207 /* We don't really want this open yet, we just might
1208 * have wanted to check some things
1216 if (raiddisks
== 1 && !force
&& level
!= LEVEL_FAULTY
) {
1217 pr_err("'1' is an unusual number of drives for an array, so it is probably\n"
1218 " a mistake. If you really mean it you will need to specify --force before\n"
1219 " setting the number of drives.\n");
1224 if (homehost
== NULL
)
1225 homehost
= conf_get_homehost(&require_homehost
);
1226 if (homehost
== NULL
|| strcasecmp(homehost
, "<system>")==0) {
1227 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
1228 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
1229 homehost
= sys_hostname
;
1232 if (homehost
&& (!homehost
[0] || strcasecmp(homehost
, "<none>") == 0)) {
1234 require_homehost
= 0;
1237 if ((mode
== MISC
&& devmode
== 'E')
1238 || (mode
== MONITOR
&& spare_sharing
== 0))
1239 /* Anyone may try this */;
1240 else if (geteuid() != 0) {
1241 pr_err("must be super-user to perform this action\n");
1245 ident
.autof
= autof
;
1250 /* readonly, add/remove, readwrite, runstop */
1252 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1253 if (!rv
&& devs_found
>1)
1254 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
1255 devlist
->next
, verbose
-quiet
, test
,
1257 if (!rv
&& readonly
< 0)
1258 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1260 rv
= Manage_runstop(devlist
->devname
, mdfd
, runstop
, quiet
);
1263 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1264 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !scan
) {
1265 /* Only a device has been given, so get details from config file */
1266 struct mddev_ident
*array_ident
= conf_get_ident(devlist
->devname
);
1267 if (array_ident
== NULL
) {
1268 pr_err("%s not identified in config file.\n",
1274 if (array_ident
->autof
== 0)
1275 array_ident
->autof
= autof
;
1276 rv
|= Assemble(ss
, devlist
->devname
, array_ident
,
1277 NULL
, backup_file
, invalid_backup
,
1278 readonly
, runstop
, update
,
1279 homehost
, require_homehost
,
1280 verbose
-quiet
, force
,
1284 rv
= Assemble(ss
, devlist
->devname
, &ident
,
1285 devlist
->next
, backup_file
, invalid_backup
,
1286 readonly
, runstop
, update
,
1287 homehost
, require_homehost
,
1288 verbose
-quiet
, force
,
1290 else if (devs_found
> 0) {
1291 if (update
&& devs_found
> 1) {
1292 pr_err("can only update a single array at a time\n");
1295 if (backup_file
&& devs_found
> 1) {
1296 pr_err("can only assemble a single array when providing a backup file.\n");
1299 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1300 struct mddev_ident
*array_ident
= conf_get_ident(dv
->devname
);
1301 if (array_ident
== NULL
) {
1302 pr_err("%s not identified in config file.\n",
1307 if (array_ident
->autof
== 0)
1308 array_ident
->autof
= autof
;
1309 rv
|= Assemble(ss
, dv
->devname
, array_ident
,
1310 NULL
, backup_file
, invalid_backup
,
1311 readonly
, runstop
, update
,
1312 homehost
, require_homehost
,
1313 verbose
-quiet
, force
,
1318 pr_err("--update not meaningful with a --scan assembly.\n");
1322 pr_err("--backup_file not meaningful with a --scan assembly.\n");
1325 rv
= scan_assemble(autof
, ss
, readonly
, runstop
,
1329 force
, freeze_reshape
);
1334 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1335 if (write_behind
&& !bitmap_file
) {
1336 pr_err("write-behind mode requires a bitmap.\n");
1340 if (raiddisks
== 0) {
1341 pr_err("no raid-devices specified.\n");
1347 if (strcmp(bitmap_file
, "internal")==0) {
1348 pr_err("'internal' bitmaps not supported with --build\n");
1353 rv
= Build(devlist
->devname
, chunk
, level
, layout
,
1354 raiddisks
, devlist
->next
, assume_clean
,
1355 bitmap_file
, bitmap_chunk
, write_behind
,
1356 delay
, verbose
-quiet
, autof
, size
);
1359 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1360 if (write_behind
&& !bitmap_file
) {
1361 pr_err("write-behind mode requires a bitmap.\n");
1365 if (raiddisks
== 0) {
1366 pr_err("no raid-devices specified.\n");
1371 rv
= Create(ss
, devlist
->devname
, chunk
, level
, layout
, size
<0 ? 0 : size
,
1372 raiddisks
, sparedisks
, ident
.name
, homehost
,
1373 ident
.uuid_set
? ident
.uuid
: NULL
,
1374 devs_found
-1, devlist
->next
, runstop
,
1375 readonly
, verbose
-quiet
, force
, assume_clean
,
1376 bitmap_file
, bitmap_chunk
, write_behind
, delay
, autof
);
1379 if (devmode
== 'E') {
1380 if (devlist
== NULL
&& !scan
) {
1381 pr_err("No devices to examine\n");
1384 if (devlist
== NULL
)
1385 devlist
= conf_get_devs();
1386 if (devlist
== NULL
) {
1387 pr_err("No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1390 if (brief
&& verbose
)
1392 rv
= Examine(devlist
, scan
?(verbose
>1?0:verbose
+1):brief
,
1394 SparcAdjust
, ss
, homehost
);
1395 } else if (devmode
== DetailPlatform
) {
1396 rv
= Detail_Platform(ss
? ss
->ss
: NULL
, ss
? scan
: 1, verbose
);
1397 } else if (devlist
== NULL
) {
1398 if (devmode
== 'S' && scan
)
1399 rv
= stop_scan(quiet
);
1400 else if ((devmode
== 'D' || devmode
== Waitclean
) && scan
)
1401 rv
= misc_scan(devmode
, verbose
, export
,
1402 test
, homehost
, prefer
);
1403 else if (devmode
== UdevRules
)
1404 rv
= Write_rules(udev_filename
);
1406 pr_err("No devices given.\n");
1410 rv
= misc_list(devlist
, brief
, verbose
, export
, test
,
1411 homehost
, prefer
, subarray
, update
,
1416 if (!devlist
&& !scan
) {
1417 pr_err("Cannot monitor: need --scan or at least one device\n");
1421 if (pidfile
&& !daemonise
) {
1422 pr_err("Cannot write a pid file when not in daemon mode\n");
1427 if (get_linux_version() > 2006016)
1428 /* mdstat responds to poll */
1433 rv
= Monitor(devlist
, mailaddr
, program
,
1434 delay
?delay
:60, daemonise
, scan
, oneshot
,
1435 dosyslog
, test
, pidfile
, increments
,
1436 spare_sharing
, prefer
);
1440 if (array_size
>= 0) {
1441 /* alway impose array size first, independent of
1443 * Do not allow level or raid_disks changes at the
1444 * same time as that can be irreversibly destructive.
1448 if (raiddisks
|| level
!= UnSet
) {
1449 pr_err("cannot change array size in same operation "
1450 "as changing raiddisks or level.\n"
1451 " Change size first, then check that data is still intact.\n");
1455 sysfs_init(&sra
, mdfd
, 0);
1456 if (array_size
== 0)
1457 err
= sysfs_set_str(&sra
, NULL
, "array_size", "default");
1459 err
= sysfs_set_num(&sra
, NULL
, "array_size", array_size
/ 2);
1462 pr_err("--array-size setting"
1463 " is too large.\n");
1465 pr_err("current kernel does"
1466 " not support setting --array-size\n");
1471 if (devs_found
> 1 && raiddisks
== 0) {
1473 if (size
>= 0 || chunk
|| layout_str
!= NULL
|| bitmap_file
) {
1474 pr_err("--add cannot be used with "
1475 "other geometry changes in --grow mode\n");
1479 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1480 rv
= Grow_Add_device(devlist
->devname
, mdfd
,
1485 } else if (bitmap_file
) {
1486 if (size
>= 0 || raiddisks
|| chunk
||
1487 layout_str
!= NULL
|| devs_found
> 1) {
1488 pr_err("--bitmap changes cannot be "
1489 "used with other geometry changes "
1490 "in --grow mode\n");
1495 delay
= DEFAULT_BITMAP_DELAY
;
1496 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, bitmap_file
,
1497 bitmap_chunk
, delay
, write_behind
, force
);
1498 } else if (grow_continue
)
1499 rv
= Grow_continue_command(devlist
->devname
,
1502 else if (size
>= 0 || raiddisks
!= 0 || layout_str
!= NULL
1503 || chunk
!= 0 || level
!= UnSet
) {
1504 rv
= Grow_reshape(devlist
->devname
, mdfd
, quiet
, backup_file
,
1505 size
, level
, layout_str
, chunk
, raiddisks
,
1507 assume_clean
, force
);
1508 } else if (array_size
< 0)
1509 pr_err("no changes to --grow\n");
1517 pr_err("--incremental --scan meaningless without --run.\n");
1520 if (devmode
== 'f') {
1521 pr_err("--incremental --scan --fail not supported.\n");
1524 rv
= IncrementalScan(verbose
);
1527 if (!rebuild_map
&& !scan
) {
1528 pr_err("--incremental requires a device.\n");
1533 if (devlist
->next
) {
1534 pr_err("--incremental can only handle one device.\n");
1539 rv
= IncrementalRemove(devlist
->devname
, remove_path
,
1542 rv
= Incremental(devlist
->devname
, verbose
-quiet
,
1543 runstop
, ss
, homehost
,
1544 require_homehost
, autof
,
1554 static int scan_assemble(int autof
, struct supertype
*ss
,
1555 int readonly
, int runstop
,
1556 struct mddev_ident
*ident
,
1557 char *homehost
, int require_homehost
,
1558 int verbose
, int force
,
1561 struct mddev_ident
*a
, *array_list
= conf_get_ident(NULL
);
1562 struct mddev_dev
*devlist
= conf_get_devs();
1563 struct map_ent
*map
= NULL
;
1566 int failures
, successes
;
1568 if (conf_verify_devnames(array_list
)) {
1569 pr_err("Duplicate MD device names in "
1570 "conf file were found.\n");
1573 if (devlist
== NULL
) {
1574 pr_err("No devices listed in conf file were found.\n");
1577 for (a
= array_list
; a
; a
= a
->next
) {
1583 pr_err("%s: failed to get "
1584 "exclusive lock on mapfile\n",
1590 for (a
= array_list
; a
; a
= a
->next
) {
1595 strcasecmp(a
->devname
, "<ignore>") == 0)
1598 r
= Assemble(ss
, a
->devname
,
1601 readonly
, runstop
, NULL
,
1602 homehost
, require_homehost
,
1613 } while (failures
&& successes
);
1614 if (homehost
&& cnt
== 0) {
1615 /* Maybe we can auto-assemble something.
1616 * Repeatedly call Assemble in auto-assemble mode
1621 ident
->autof
= autof
;
1623 struct mddev_dev
*devlist
= conf_get_devs();
1626 rv2
= Assemble(ss
, NULL
,
1641 /* Incase there are stacked devices, we need to go around again */
1643 if (cnt
== 0 && rv
== 0) {
1644 pr_err("No arrays found in config file or automatically\n");
1648 } else if (cnt
== 0 && rv
== 0) {
1649 pr_err("No arrays found in config file\n");
1656 static int misc_scan(char devmode
, int verbose
, int export
, int test
,
1657 char *homehost
, char *prefer
)
1659 /* apply --detail or --wait-clean to
1660 * all devices in /proc/mdstat
1662 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1663 struct mdstat_ent
*e
;
1664 struct map_ent
*map
= NULL
;
1666 int v
= verbose
>1?0:verbose
+1;
1669 for (members
= 0; members
<= 1; members
++) {
1670 for (e
=ms
; e
; e
=e
->next
) {
1673 int member
= e
->metadata_version
&&
1674 strncmp(e
->metadata_version
,
1675 "external:/", 10) == 0;
1676 if (members
!= member
)
1678 me
= map_by_devnum(&map
, e
->devnum
);
1680 && strcmp(me
->path
, "/unknown") != 0)
1683 name
= get_md_name(e
->devnum
);
1686 pr_err("cannot find device file for %s\n",
1691 rv
|= Detail(name
, v
,
1695 rv
|= WaitClean(name
, -1, v
);
1703 static int stop_scan(int quiet
)
1705 /* apply --stop to all devices in /proc/mdstat */
1706 /* Due to possible stacking of devices, repeat until
1707 * nothing more can be stopped
1709 int progress
=1, err
;
1713 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1714 struct mdstat_ent
*e
;
1716 if (!progress
) last
= 1;
1717 progress
= 0; err
= 0;
1718 for (e
=ms
; e
; e
=e
->next
) {
1719 char *name
= get_md_name(e
->devnum
);
1723 pr_err("cannot find device file for %s\n",
1727 mdfd
= open_mddev(name
, 1);
1729 if (Manage_runstop(name
, mdfd
, -1, quiet
?1:last
?0:-1))
1739 } while (!last
&& err
);
1745 static int misc_list(struct mddev_dev
*devlist
,
1746 int brief
, int verbose
, int export
, int test
,
1747 char *homehost
, char *prefer
, char *subarray
,
1748 char *update
, struct mddev_ident
*ident
,
1749 struct supertype
*ss
, int force
, int quiet
)
1751 struct mddev_dev
*dv
;
1754 for (dv
=devlist
; dv
; dv
=dv
->next
) {
1757 switch(dv
->disposition
) {
1759 rv
|= Detail(dv
->devname
,
1761 export
, test
, homehost
, prefer
);
1763 case KillOpt
: /* Zero superblock */
1765 rv
|= Kill(dv
->devname
, ss
, force
, quiet
,0);
1769 rv
|= Kill(dv
->devname
, NULL
, force
, q
, 0);
1776 rv
|= Query(dv
->devname
); continue;
1778 rv
|= ExamineBitmap(dv
->devname
, brief
, ss
); continue;
1781 rv
|= Wait(dv
->devname
); continue;
1783 rv
|= WaitClean(dv
->devname
, -1, verbose
-quiet
); continue;
1785 rv
|= Kill_subarray(dv
->devname
, subarray
, quiet
);
1787 case UpdateSubarray
:
1788 if (update
== NULL
) {
1789 pr_err("-U/--update must be specified with --update-subarray\n");
1793 rv
|= Update_subarray(dv
->devname
, subarray
,
1794 update
, ident
, quiet
);
1797 mdfd
= open_mddev(dv
->devname
, 1);
1799 switch(dv
->disposition
) {
1801 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1, quiet
); break;
1803 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1, quiet
); break;
1805 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1807 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;