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
;
120 char *shortopt
= short_options
;
123 char *subarray
= NULL
;
124 char *remove_path
= NULL
;
125 char *udev_filename
= NULL
;
132 int freeze_reshape
= 0;
134 srandom(time(0) ^ getpid());
138 ident
.raid_disks
= UnSet
;
139 ident
.super_minor
= UnSet
;
141 ident
.spare_group
= NULL
;
144 ident
.bitmap_fd
= -1;
145 ident
.bitmap_file
= NULL
;
147 ident
.container
= NULL
;
150 while ((option_index
= -1) ,
151 (opt
=getopt_long(argc
, argv
,
152 shortopt
, long_options
,
153 &option_index
)) != -1) {
155 /* firstly, some mode-independent options */
165 fputs(Version
, stderr
);
175 if (mode
== ASSEMBLE
|| mode
== BUILD
|| mode
== CREATE
176 || mode
== GROW
|| mode
== INCREMENTAL
178 break; /* b means bitmap */
187 if (strcasecmp(optarg
, "<ignore>") == 0)
188 require_homehost
= 0;
194 * --offroot sets first char of argv[0] to @. This is used
195 * by systemd to signal that the task was launched from
196 * initrd/initramfs and should be preserved during shutdown
206 if (asprintf(&prefer
, "/%s/", optarg
) <= 0)
212 fputs(Usage
, stderr
);
215 /* second, figure out the mode.
216 * Some options force the mode. Others
217 * set the mode if it isn't already
223 shortopt
= short_bitmap_options
;
231 case ReAdd
: /* re-add */
234 shortopt
= short_bitmap_options
;
238 case 'A': newmode
= ASSEMBLE
;
239 shortopt
= short_bitmap_auto_options
;
241 case 'B': newmode
= BUILD
;
242 shortopt
= short_bitmap_auto_options
;
244 case 'C': newmode
= CREATE
;
245 shortopt
= short_bitmap_auto_options
;
247 case 'F': newmode
= MONITOR
;
249 case 'G': newmode
= GROW
;
250 shortopt
= short_bitmap_options
;
252 case 'I': newmode
= INCREMENTAL
;
253 shortopt
= short_bitmap_auto_options
;
256 newmode
= AUTODETECT
;
287 if (mode
&& newmode
== mode
) {
288 /* everybody happy ! */
289 } else if (mode
&& newmode
!= mode
) {
292 if (option_index
>= 0)
293 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
295 fprintf(stderr
, "-%c", opt
);
296 fprintf(stderr
, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
297 map_num(modes
, newmode
),
298 map_num(modes
, mode
));
300 } else if (!mode
&& newmode
) {
302 if (mode
== MISC
&& devs_found
) {
303 pr_err("No action given for %s in --misc mode\n",
305 fprintf(stderr
," Action options must come before device names\n");
309 /* special case of -c --help */
310 if ((opt
== 'c' || opt
== ConfigFile
) &&
311 ( strncmp(optarg
, "--h", 3)==0 ||
312 strncmp(optarg
, "-h", 2)==0)) {
313 fputs(Help_config
, stdout
);
317 /* If first option is a device, don't force the mode yet */
319 if (devs_found
== 0) {
320 dv
= malloc(sizeof(*dv
));
322 pr_err("malloc failed\n");
325 dv
->devname
= optarg
;
326 dv
->disposition
= devmode
;
327 dv
->writemostly
= writemostly
;
331 devlistend
= &dv
->next
;
336 /* No mode yet, and this is the second device ... */
337 pr_err("An option must be given to set the mode before a second device\n"
338 " (%s) is listed\n", optarg
);
341 if (option_index
>= 0)
342 pr_err("--%s", long_options
[option_index
].name
);
345 fprintf(stderr
, " does not set the mode, and so cannot be the first option.\n");
349 /* if we just set the mode, then done */
363 /* an undecorated option - must be a device name.
365 if (devs_found
> 0 && mode
== MANAGE
&& !devmode
) {
366 pr_err("Must give one of -a/-r/-f"
367 " for subsequent devices at %s\n", optarg
);
370 if (devs_found
> 0 && mode
== GROW
&& !devmode
) {
371 pr_err("Must give -a/--add for"
372 " devices to add: %s\n", optarg
);
375 dv
= malloc(sizeof(*dv
));
377 pr_err("malloc failed\n");
380 dv
->devname
= optarg
;
381 dv
->disposition
= devmode
;
382 dv
->writemostly
= writemostly
;
386 devlistend
= &dv
->next
;
392 /* We've got a mode, and opt is now something else which
393 * could depend on the mode */
394 #define O(a,b) ((a<<16)|b)
395 switch (O(mode
,opt
)) {
397 case O(GROW
,ChunkSize
):
399 case O(CREATE
,ChunkSize
):
400 case O(BUILD
,'c'): /* chunk or rounding */
401 case O(BUILD
,ChunkSize
): /* chunk or rounding */
403 pr_err("chunk/rounding may only be specified once. "
404 "Second value is %s.\n", optarg
);
407 chunk
= parse_size(optarg
);
408 if (chunk
< 8 || (chunk
&1)) {
409 pr_err("invalid chunk/rounding value: %s\n",
413 /* Convert sectors to K */
417 case O(INCREMENTAL
, 'e'):
419 case O(ASSEMBLE
,'e'):
420 case O(MISC
,'e'): /* set metadata (superblock) information */
422 pr_err("metadata information already given\n");
425 for(i
=0; !ss
&& superlist
[i
]; i
++)
426 ss
= superlist
[i
]->match_metadata_desc(optarg
);
429 pr_err("unrecognised metadata identifier: %s\n", optarg
);
435 case O(MANAGE
,WriteMostly
):
437 case O(BUILD
,WriteMostly
):
439 case O(CREATE
,WriteMostly
):
440 /* set write-mostly for following devices */
445 /* clear write-mostly for following devices */
452 case O(BUILD
,'z'): /* size */
454 pr_err("size may only be specified once. "
455 "Second value is %s.\n", optarg
);
458 if (strcmp(optarg
, "max")==0)
461 size
= parse_size(optarg
);
463 pr_err("invalid size: %s\n",
467 /* convert sectors to K */
472 case O(GROW
,'Z'): /* array size */
473 if (array_size
>= 0) {
474 pr_err("array-size may only be specified once. "
475 "Second value is %s.\n", optarg
);
478 if (strcmp(optarg
, "max") == 0)
481 array_size
= parse_size(optarg
);
482 if (array_size
<= 0) {
483 pr_err("invalid array size: %s\n",
492 case O(BUILD
,'l'): /* set raid level*/
493 if (level
!= UnSet
) {
494 pr_err("raid level may only be set once. "
495 "Second value is %s.\n", optarg
);
498 level
= map_name(pers
, optarg
);
499 if (level
== UnSet
) {
500 pr_err("invalid raid level: %s\n",
504 if (level
!= 0 && level
!= LEVEL_LINEAR
&& level
!= 1 &&
505 level
!= LEVEL_MULTIPATH
&& level
!= LEVEL_FAULTY
&&
508 pr_err("Raid level %s not permitted with --build.\n",
512 if (sparedisks
> 0 && level
< 1 && level
>= -1) {
513 pr_err("raid level %s is incompatible with spare-devices setting.\n",
520 case O(GROW
, 'p'): /* new layout */
521 case O(GROW
, Layout
):
523 pr_err("layout may only be sent once. "
524 "Second value was %s\n", optarg
);
528 /* 'Grow' will parse the value */
531 case O(CREATE
,'p'): /* raid5 layout */
532 case O(CREATE
,Layout
):
533 case O(BUILD
,'p'): /* faulty layout */
534 case O(BUILD
,Layout
):
535 if (layout
!= UnSet
) {
536 pr_err("layout may only be sent once. "
537 "Second value was %s\n", optarg
);
542 pr_err("layout not meaningful for %s arrays.\n",
543 map_num(pers
, level
));
546 pr_err("raid level must be given before layout.\n");
550 layout
= map_name(r5layout
, optarg
);
552 pr_err("layout %s not understood for raid5.\n",
558 layout
= map_name(r6layout
, optarg
);
560 pr_err("layout %s not understood for raid6.\n",
567 layout
= parse_layout_10(optarg
);
569 pr_err("layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg
);
577 layout
= parse_layout_faulty(optarg
);
579 pr_err("layout %s not understood for faulty.\n",
587 case O(CREATE
,AssumeClean
):
588 case O(BUILD
,AssumeClean
): /* assume clean */
589 case O(GROW
,AssumeClean
):
595 case O(BUILD
,'n'): /* number of raid disks */
597 pr_err("raid-devices set twice: %d and %s\n",
601 raiddisks
= strtol(optarg
, &c
, 10);
602 if (!optarg
[0] || *c
|| raiddisks
<=0) {
603 pr_err("invalid number of raid devices: %s\n",
607 ident
.raid_disks
= raiddisks
;
610 case O(CREATE
,'x'): /* number of spare (eXtra) disks */
612 pr_err("spare-devices set twice: %d and %s\n",
616 if (level
!= UnSet
&& level
<= 0 && level
>= -1) {
617 pr_err("spare-devices setting is incompatible with raid level %d\n",
621 sparedisks
= strtol(optarg
, &c
, 10);
622 if (!optarg
[0] || *c
|| sparedisks
< 0) {
623 pr_err("invalid number of spare-devices: %s\n",
633 case O(INCREMENTAL
,'a'):
634 case O(INCREMENTAL
,Auto
):
635 case O(ASSEMBLE
,'a'):
636 case O(ASSEMBLE
,Auto
): /* auto-creation of device node */
637 autof
= parse_auto(optarg
, "--auto flag", 0);
640 case O(CREATE
,Symlinks
):
641 case O(BUILD
,Symlinks
):
642 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
646 case O(BUILD
,'f'): /* force honouring '-n 1' */
647 case O(BUILD
,Force
): /* force honouring '-n 1' */
648 case O(GROW
,'f'): /* ditto */
649 case O(GROW
,Force
): /* ditto */
650 case O(CREATE
,'f'): /* force honouring of device list */
651 case O(CREATE
,Force
): /* force honouring of device list */
652 case O(ASSEMBLE
,'f'): /* force assembly */
653 case O(ASSEMBLE
,Force
): /* force assembly */
654 case O(MISC
,'f'): /* force zero */
655 case O(MISC
,Force
): /* force zero */
656 case O(MANAGE
,Force
): /* add device which is too large */
659 /* now for the Assemble options */
660 case O(ASSEMBLE
, FreezeReshape
): /* Freeze reshape during
662 case O(INCREMENTAL
, FreezeReshape
):
665 case O(CREATE
,'u'): /* uuid of array */
666 case O(ASSEMBLE
,'u'): /* uuid of array */
667 if (ident
.uuid_set
) {
668 pr_err("uuid cannot be set twice. "
669 "Second value %s.\n", optarg
);
672 if (parse_uuid(optarg
, ident
.uuid
))
675 pr_err("Bad uuid: %s\n", optarg
);
681 case O(ASSEMBLE
,'N'):
684 pr_err("name cannot be set twice. "
685 "Second value %s.\n", optarg
);
688 if (mode
== MISC
&& !subarray
) {
689 pr_err("-N/--name only valid with --update-subarray in misc mode\n");
692 if (strlen(optarg
) > 32) {
693 pr_err("name '%s' is too long, 32 chars max.\n",
697 strcpy(ident
.name
, optarg
);
700 case O(ASSEMBLE
,'m'): /* super-minor for array */
701 case O(ASSEMBLE
,SuperMinor
):
702 if (ident
.super_minor
!= UnSet
) {
703 pr_err("super-minor cannot be set twice. "
704 "Second value: %s.\n", optarg
);
707 if (strcmp(optarg
, "dev")==0)
708 ident
.super_minor
= -2;
710 ident
.super_minor
= strtoul(optarg
, &cp
, 10);
711 if (!optarg
[0] || *cp
) {
712 pr_err("Bad super-minor number: %s.\n", optarg
);
718 case O(ASSEMBLE
,'U'): /* update the superblock */
721 pr_err("Can only update one aspect"
722 " of superblock, both %s and %s given.\n",
726 if (mode
== MISC
&& !subarray
) {
727 pr_err("Only subarrays can be"
728 " updated in misc mode\n");
732 if (strcmp(update
, "sparc2.2")==0)
734 if (strcmp(update
, "super-minor") == 0)
736 if (strcmp(update
, "summaries")==0)
738 if (strcmp(update
, "resync")==0)
740 if (strcmp(update
, "uuid")==0)
742 if (strcmp(update
, "name")==0)
744 if (strcmp(update
, "homehost")==0)
746 if (strcmp(update
, "devicesize")==0)
748 if (strcmp(update
, "no-bitmap")==0)
750 if (strcmp(update
, "byteorder")==0) {
752 pr_err("must not set metadata"
753 " type with --update=byteorder.\n");
756 for(i
=0; !ss
&& superlist
[i
]; i
++)
757 ss
= superlist
[i
]->match_metadata_desc(
760 pr_err("INTERNAL ERROR"
761 " cannot find 0.swap\n");
767 if (strcmp(update
,"?") == 0 ||
768 strcmp(update
, "help") == 0) {
770 fprintf(outf
, Name
": ");
774 Name
": '--update=%s' is invalid. ",
777 fprintf(outf
, "Valid --update options are:\n"
778 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
779 " 'summaries', 'homehost', 'byteorder', 'devicesize',\n"
781 exit(outf
== stdout
? 0 : 2);
784 /* update=devicesize is allowed with --re-add */
785 if (devmode
!= 'A') {
786 pr_err("--update in Manage mode only"
787 " allowed with --re-add.\n");
791 pr_err("Can only update one aspect"
792 " of superblock, both %s and %s given.\n",
797 if (strcmp(update
, "devicesize") != 0) {
798 pr_err("only 'devicesize' can be"
799 " updated with --re-add\n");
804 case O(INCREMENTAL
,NoDegraded
):
805 pr_err("--no-degraded is deprecated in Incremental mode\n");
806 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
807 runstop
= -1; /* --stop isn't allowed for --assemble,
808 * so we overload slightly */
811 case O(ASSEMBLE
,'c'):
812 case O(ASSEMBLE
,ConfigFile
):
813 case O(INCREMENTAL
, 'c'):
814 case O(INCREMENTAL
, ConfigFile
):
816 case O(MISC
, ConfigFile
):
818 case O(MONITOR
,ConfigFile
):
820 pr_err("configfile cannot be set twice. "
821 "Second value is %s.\n", optarg
);
825 set_conffile(configfile
);
826 /* FIXME possibly check that config file exists. Even parse it */
828 case O(ASSEMBLE
,'s'): /* scan */
831 case O(INCREMENTAL
,'s'):
835 case O(MONITOR
,'m'): /* mail address */
836 case O(MONITOR
,EMail
):
838 pr_err("only specify one mailaddress. %s ignored.\n",
844 case O(MONITOR
,'p'): /* alert program */
845 case O(MONITOR
,ProgramOpt
): /* alert program */
847 pr_err("only specify one alter program. %s ignored.\n",
853 case O(MONITOR
,'r'): /* rebuild increments */
854 case O(MONITOR
,Increment
):
855 increments
= atoi(optarg
);
856 if (increments
> 99 || increments
< 1) {
857 pr_err("please specify positive integer between 1 and 99 as rebuild increments.\n");
862 case O(MONITOR
,'d'): /* delay in seconds */
864 case O(BUILD
,'d'): /* delay for bitmap updates */
867 pr_err("only specify delay once. %s ignored.\n",
870 delay
= strtol(optarg
, &c
, 10);
871 if (!optarg
[0] || *c
|| delay
<1) {
872 pr_err("invalid delay: %s\n",
878 case O(MONITOR
,'f'): /* daemonise */
879 case O(MONITOR
,Fork
):
882 case O(MONITOR
,'i'): /* pid */
884 pr_err("only specify one pid file. %s ignored.\n",
889 case O(MONITOR
,'1'): /* oneshot */
893 case O(MONITOR
,'t'): /* test */
896 case O(MONITOR
,'y'): /* log messages to syslog */
897 openlog("mdadm", LOG_PID
, SYSLOG_FACILITY
);
900 case O(MONITOR
, NoSharing
):
904 /* now the general management options. Some are applicable
905 * to other modes. None have arguments.
910 case O(MANAGE
,Add
): /* add a drive */
913 case O(MANAGE
,ReAdd
):
916 case O(MANAGE
,'r'): /* remove a drive */
917 case O(MANAGE
,Remove
):
920 case O(MANAGE
,'f'): /* set faulty */
922 case O(INCREMENTAL
,'f'):
923 case O(INCREMENTAL
,Remove
):
924 case O(INCREMENTAL
,Fail
): /* r for incremental is taken, use f
925 * even though we will both fail and
926 * remove the device */
929 case O(INCREMENTAL
,'R'):
931 case O(ASSEMBLE
,'R'):
933 case O(CREATE
,'R'): /* Run the array */
935 pr_err("Cannot both Stop and Run an array\n");
942 pr_err("Cannot both Run and Stop an array\n");
954 case O(MISC
,KillOpt
):
961 case O(MISC
, WaitOpt
):
962 case O(MISC
, Waitclean
):
963 case O(MISC
, DetailPlatform
):
964 case O(MISC
, KillSubarray
):
965 case O(MISC
, UpdateSubarray
):
966 if (opt
== KillSubarray
|| opt
== UpdateSubarray
) {
968 pr_err("subarray can only"
969 " be specified once\n");
974 if (devmode
&& devmode
!= opt
&&
975 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
976 pr_err("--examine/-E cannot be given with ");
977 if (devmode
== 'E') {
978 if (option_index
>= 0)
979 fprintf(stderr
, "--%s\n",
980 long_options
[option_index
].name
);
982 fprintf(stderr
, "-%c\n", opt
);
983 } else if (isalpha(devmode
))
984 fprintf(stderr
, "-%c\n", devmode
);
986 fprintf(stderr
, "previous option\n");
991 case O(MISC
, UdevRules
):
992 if (devmode
&& devmode
!= opt
) {
993 pr_err("--udev-rules must"
994 " be the only option.\n");
997 pr_err("only specify one udev "
998 "rule filename. %s ignored.\n",
1001 udev_filename
= optarg
;
1009 case O(MISC
, Sparc22
):
1010 if (devmode
!= 'E') {
1011 pr_err("--sparc2.2 only allowed with --examine\n");
1017 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
1018 case O(ASSEMBLE
,Bitmap
):
1020 pr_err("bitmap file needed with -b in --assemble mode\n");
1023 if (strcmp(optarg
, "internal")==0) {
1024 pr_err("there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
1027 bitmap_fd
= open(optarg
, O_RDWR
);
1028 if (!*optarg
|| bitmap_fd
< 0) {
1029 pr_err("cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
1032 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
1035 case O(ASSEMBLE
, BackupFile
):
1036 case O(GROW
, BackupFile
):
1037 /* Specify a file into which grow might place a backup,
1038 * or from which assemble might recover a backup
1041 pr_err("backup file already specified, rejecting %s\n", optarg
);
1044 backup_file
= optarg
;
1047 case O(GROW
, Continue
):
1048 /* Continue interrupted grow
1052 case O(ASSEMBLE
, InvalidBackup
):
1053 /* Acknowledge that the backupfile is invalid, but ask
1054 * to continue anyway
1060 case O(BUILD
,Bitmap
):
1062 case O(CREATE
,Bitmap
): /* here we create the bitmap */
1063 if (strcmp(optarg
, "none") == 0) {
1064 pr_err("'--bitmap none' only"
1065 " supported for --grow\n");
1070 case O(GROW
,Bitmap
):
1071 if (strcmp(optarg
, "internal")== 0 ||
1072 strcmp(optarg
, "none")== 0 ||
1073 strchr(optarg
, '/') != NULL
) {
1074 bitmap_file
= optarg
;
1078 pr_err("bitmap file must contain a '/', or be 'internal', or 'none'\n"
1079 " not '%s'\n", optarg
);
1082 case O(GROW
,BitmapChunk
):
1083 case O(BUILD
,BitmapChunk
):
1084 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
1085 bitmap_chunk
= parse_size(optarg
);
1086 if (bitmap_chunk
<= 0 ||
1087 bitmap_chunk
& (bitmap_chunk
- 1)) {
1088 pr_err("invalid bitmap chunksize: %s\n",
1092 bitmap_chunk
= bitmap_chunk
* 512;
1095 case O(GROW
, WriteBehind
):
1096 case O(BUILD
, WriteBehind
):
1097 case O(CREATE
, WriteBehind
): /* write-behind mode */
1098 write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
1100 write_behind
= strtol(optarg
, &c
, 10);
1101 if (write_behind
< 0 || *c
||
1102 write_behind
> 16383) {
1103 pr_err("Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
1109 case O(INCREMENTAL
, 'r'):
1110 case O(INCREMENTAL
, RebuildMapOpt
):
1113 case O(INCREMENTAL
, IncrementalPath
):
1114 remove_path
= optarg
;
1117 /* We have now processed all the valid options. Anything else is
1120 if (option_index
> 0)
1121 pr_err(":option --%s not valid in %s mode\n",
1122 long_options
[option_index
].name
,
1123 map_num(modes
, mode
));
1125 pr_err("option -%c not valid in %s mode\n",
1126 opt
, map_num(modes
, mode
));
1133 if (print_help
== 2)
1134 help_text
= OptionHelp
;
1136 help_text
= mode_help
[mode
];
1137 if (help_text
== NULL
)
1139 fputs(help_text
,stdout
);
1143 if (!mode
&& devs_found
) {
1146 if (devlist
->disposition
== 0)
1147 devlist
->disposition
= devmode
;
1150 fputs(Usage
, stderr
);
1155 struct createinfo
*ci
= conf_get_create_info();
1157 if (strcasecmp(symlinks
, "yes") == 0)
1159 else if (strcasecmp(symlinks
, "no") == 0)
1162 pr_err("option --symlinks must be 'no' or 'yes'\n");
1166 /* Ok, got the option parsing out of the way
1167 * hopefully it's mostly right but there might be some stuff
1170 * That is mosty checked in the per-mode stuff but...
1172 * For @,B,C and A without -s, the first device listed must be
1173 * an md device. We check that here and open it.
1176 if (mode
== MANAGE
|| mode
== BUILD
|| mode
== CREATE
1178 || (mode
== ASSEMBLE
&& ! scan
)) {
1179 if (devs_found
< 1) {
1180 pr_err("an md device must be given in this mode\n");
1183 if ((int)ident
.super_minor
== -2 && autof
) {
1184 pr_err("--super-minor=dev is incompatible with --auto\n");
1187 if (mode
== MANAGE
|| mode
== GROW
) {
1188 mdfd
= open_mddev(devlist
->devname
, 1);
1192 /* non-existent device is OK */
1193 mdfd
= open_mddev(devlist
->devname
, 0);
1195 pr_err("device %s exists but is not an "
1196 "md array.\n", devlist
->devname
);
1199 if ((int)ident
.super_minor
== -2) {
1202 pr_err("--super-minor=dev given, and "
1203 "listed device %s doesn't exist.\n",
1208 ident
.super_minor
= minor(stb
.st_rdev
);
1210 if (mdfd
>= 0 && mode
!= MANAGE
&& mode
!= GROW
) {
1211 /* We don't really want this open yet, we just might
1212 * have wanted to check some things
1220 if (raiddisks
== 1 && !force
&& level
!= LEVEL_FAULTY
) {
1221 pr_err("'1' is an unusual number of drives for an array, so it is probably\n"
1222 " a mistake. If you really mean it you will need to specify --force before\n"
1223 " setting the number of drives.\n");
1228 if (homehost
== NULL
)
1229 homehost
= conf_get_homehost(&require_homehost
);
1230 if (homehost
== NULL
|| strcasecmp(homehost
, "<system>")==0) {
1231 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
1232 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
1233 homehost
= sys_hostname
;
1236 if (homehost
&& (!homehost
[0] || strcasecmp(homehost
, "<none>") == 0)) {
1238 require_homehost
= 0;
1241 if ((mode
== MISC
&& devmode
== 'E')
1242 || (mode
== MONITOR
&& spare_sharing
== 0))
1243 /* Anyone may try this */;
1244 else if (geteuid() != 0) {
1245 pr_err("must be super-user to perform this action\n");
1249 ident
.autof
= autof
;
1254 /* readonly, add/remove, readwrite, runstop */
1256 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1257 if (!rv
&& devs_found
>1)
1258 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
1259 devlist
->next
, verbose
-quiet
, test
,
1261 if (!rv
&& readonly
< 0)
1262 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1264 rv
= Manage_runstop(devlist
->devname
, mdfd
, runstop
, quiet
);
1267 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1268 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !scan
) {
1269 /* Only a device has been given, so get details from config file */
1270 struct mddev_ident
*array_ident
= conf_get_ident(devlist
->devname
);
1271 if (array_ident
== NULL
) {
1272 pr_err("%s not identified in config file.\n",
1278 if (array_ident
->autof
== 0)
1279 array_ident
->autof
= autof
;
1280 rv
|= Assemble(ss
, devlist
->devname
, array_ident
,
1281 NULL
, backup_file
, invalid_backup
,
1282 readonly
, runstop
, update
,
1283 homehost
, require_homehost
,
1284 verbose
-quiet
, force
,
1288 rv
= Assemble(ss
, devlist
->devname
, &ident
,
1289 devlist
->next
, backup_file
, invalid_backup
,
1290 readonly
, runstop
, update
,
1291 homehost
, require_homehost
,
1292 verbose
-quiet
, force
,
1294 else if (devs_found
> 0) {
1295 if (update
&& devs_found
> 1) {
1296 pr_err("can only update a single array at a time\n");
1299 if (backup_file
&& devs_found
> 1) {
1300 pr_err("can only assemble a single array when providing a backup file.\n");
1303 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1304 struct mddev_ident
*array_ident
= conf_get_ident(dv
->devname
);
1305 if (array_ident
== NULL
) {
1306 pr_err("%s not identified in config file.\n",
1311 if (array_ident
->autof
== 0)
1312 array_ident
->autof
= autof
;
1313 rv
|= Assemble(ss
, dv
->devname
, array_ident
,
1314 NULL
, backup_file
, invalid_backup
,
1315 readonly
, runstop
, update
,
1316 homehost
, require_homehost
,
1317 verbose
-quiet
, force
,
1322 pr_err("--update not meaningful with a --scan assembly.\n");
1326 pr_err("--backup_file not meaningful with a --scan assembly.\n");
1329 rv
= scan_assemble(autof
, ss
, readonly
, runstop
,
1333 force
, freeze_reshape
);
1338 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1339 if (write_behind
&& !bitmap_file
) {
1340 pr_err("write-behind mode requires a bitmap.\n");
1344 if (raiddisks
== 0) {
1345 pr_err("no raid-devices specified.\n");
1351 if (strcmp(bitmap_file
, "internal")==0) {
1352 pr_err("'internal' bitmaps not supported with --build\n");
1357 rv
= Build(devlist
->devname
, chunk
, level
, layout
,
1358 raiddisks
, devlist
->next
, assume_clean
,
1359 bitmap_file
, bitmap_chunk
, write_behind
,
1360 delay
, verbose
-quiet
, autof
, size
);
1363 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1364 if (write_behind
&& !bitmap_file
) {
1365 pr_err("write-behind mode requires a bitmap.\n");
1369 if (raiddisks
== 0) {
1370 pr_err("no raid-devices specified.\n");
1375 rv
= Create(ss
, devlist
->devname
, chunk
, level
, layout
, size
<0 ? 0 : size
,
1376 raiddisks
, sparedisks
, ident
.name
, homehost
,
1377 ident
.uuid_set
? ident
.uuid
: NULL
,
1378 devs_found
-1, devlist
->next
, runstop
, verbose
-quiet
, force
, assume_clean
,
1379 bitmap_file
, bitmap_chunk
, write_behind
, delay
, autof
);
1382 if (devmode
== 'E') {
1383 if (devlist
== NULL
&& !scan
) {
1384 pr_err("No devices to examine\n");
1387 if (devlist
== NULL
)
1388 devlist
= conf_get_devs();
1389 if (devlist
== NULL
) {
1390 pr_err("No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1393 if (brief
&& verbose
)
1395 rv
= Examine(devlist
, scan
?(verbose
>1?0:verbose
+1):brief
,
1397 SparcAdjust
, ss
, homehost
);
1398 } else if (devmode
== DetailPlatform
) {
1399 rv
= Detail_Platform(ss
? ss
->ss
: NULL
, ss
? scan
: 1, verbose
);
1400 } else if (devlist
== NULL
) {
1401 if (devmode
== 'S' && scan
)
1402 rv
= stop_scan(quiet
);
1403 else if ((devmode
== 'D' || devmode
== Waitclean
) && scan
)
1404 rv
= misc_scan(devmode
, verbose
, export
,
1405 test
, homehost
, prefer
);
1406 else if (devmode
== UdevRules
)
1407 rv
= Write_rules(udev_filename
);
1409 pr_err("No devices given.\n");
1413 rv
= misc_list(devlist
, brief
, verbose
, export
, test
,
1414 homehost
, prefer
, subarray
, update
,
1419 if (!devlist
&& !scan
) {
1420 pr_err("Cannot monitor: need --scan or at least one device\n");
1424 if (pidfile
&& !daemonise
) {
1425 pr_err("Cannot write a pid file when not in daemon mode\n");
1430 if (get_linux_version() > 2006016)
1431 /* mdstat responds to poll */
1436 rv
= Monitor(devlist
, mailaddr
, program
,
1437 delay
?delay
:60, daemonise
, scan
, oneshot
,
1438 dosyslog
, test
, pidfile
, increments
,
1439 spare_sharing
, prefer
);
1443 if (array_size
>= 0) {
1444 /* alway impose array size first, independent of
1446 * Do not allow level or raid_disks changes at the
1447 * same time as that can be irreversibly destructive.
1451 if (raiddisks
|| level
!= UnSet
) {
1452 pr_err("cannot change array size in same operation "
1453 "as changing raiddisks or level.\n"
1454 " Change size first, then check that data is still intact.\n");
1458 sysfs_init(&sra
, mdfd
, 0);
1459 if (array_size
== 0)
1460 err
= sysfs_set_str(&sra
, NULL
, "array_size", "default");
1462 err
= sysfs_set_num(&sra
, NULL
, "array_size", array_size
/ 2);
1465 pr_err("--array-size setting"
1466 " is too large.\n");
1468 pr_err("current kernel does"
1469 " not support setting --array-size\n");
1474 if (devs_found
> 1 && raiddisks
== 0) {
1476 if (size
>= 0 || chunk
|| layout_str
!= NULL
|| bitmap_file
) {
1477 pr_err("--add cannot be used with "
1478 "other geometry changes in --grow mode\n");
1482 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1483 rv
= Grow_Add_device(devlist
->devname
, mdfd
,
1488 } else if (bitmap_file
) {
1489 if (size
>= 0 || raiddisks
|| chunk
||
1490 layout_str
!= NULL
|| devs_found
> 1) {
1491 pr_err("--bitmap changes cannot be "
1492 "used with other geometry changes "
1493 "in --grow mode\n");
1498 delay
= DEFAULT_BITMAP_DELAY
;
1499 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, bitmap_file
,
1500 bitmap_chunk
, delay
, write_behind
, force
);
1501 } else if (grow_continue
)
1502 rv
= Grow_continue_command(devlist
->devname
,
1505 else if (size
>= 0 || raiddisks
!= 0 || layout_str
!= NULL
1506 || chunk
!= 0 || level
!= UnSet
) {
1507 rv
= Grow_reshape(devlist
->devname
, mdfd
, quiet
, backup_file
,
1508 size
, level
, layout_str
, chunk
, raiddisks
,
1510 assume_clean
, force
);
1511 } else if (array_size
< 0)
1512 pr_err("no changes to --grow\n");
1520 pr_err("--incremental --scan meaningless without --run.\n");
1523 if (devmode
== 'f') {
1524 pr_err("--incremental --scan --fail not supported.\n");
1527 rv
= IncrementalScan(verbose
);
1530 if (!rebuild_map
&& !scan
) {
1531 pr_err("--incremental requires a device.\n");
1536 if (devlist
->next
) {
1537 pr_err("--incremental can only handle one device.\n");
1542 rv
= IncrementalRemove(devlist
->devname
, remove_path
,
1545 rv
= Incremental(devlist
->devname
, verbose
-quiet
,
1546 runstop
, ss
, homehost
,
1547 require_homehost
, autof
,
1557 static int scan_assemble(int autof
, struct supertype
*ss
,
1558 int readonly
, int runstop
,
1559 struct mddev_ident
*ident
,
1560 char *homehost
, int require_homehost
,
1561 int verbose
, int force
,
1564 struct mddev_ident
*a
, *array_list
= conf_get_ident(NULL
);
1565 struct mddev_dev
*devlist
= conf_get_devs();
1566 struct map_ent
*map
= NULL
;
1569 int failures
, successes
;
1571 if (conf_verify_devnames(array_list
)) {
1572 pr_err("Duplicate MD device names in "
1573 "conf file were found.\n");
1576 if (devlist
== NULL
) {
1577 pr_err("No devices listed in conf file were found.\n");
1580 for (a
= array_list
; a
; a
= a
->next
) {
1586 pr_err("%s: failed to get "
1587 "exclusive lock on mapfile\n",
1593 for (a
= array_list
; a
; a
= a
->next
) {
1598 strcasecmp(a
->devname
, "<ignore>") == 0)
1601 r
= Assemble(ss
, a
->devname
,
1604 readonly
, runstop
, NULL
,
1605 homehost
, require_homehost
,
1616 } while (failures
&& successes
);
1617 if (homehost
&& cnt
== 0) {
1618 /* Maybe we can auto-assemble something.
1619 * Repeatedly call Assemble in auto-assemble mode
1624 ident
->autof
= autof
;
1626 struct mddev_dev
*devlist
= conf_get_devs();
1629 rv2
= Assemble(ss
, NULL
,
1644 /* Incase there are stacked devices, we need to go around again */
1646 if (cnt
== 0 && rv
== 0) {
1647 pr_err("No arrays found in config file or automatically\n");
1651 } else if (cnt
== 0 && rv
== 0) {
1652 pr_err("No arrays found in config file\n");
1659 static int misc_scan(char devmode
, int verbose
, int export
, int test
,
1660 char *homehost
, char *prefer
)
1662 /* apply --detail or --wait-clean to
1663 * all devices in /proc/mdstat
1665 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1666 struct mdstat_ent
*e
;
1667 struct map_ent
*map
= NULL
;
1669 int v
= verbose
>1?0:verbose
+1;
1672 for (members
= 0; members
<= 1; members
++) {
1673 for (e
=ms
; e
; e
=e
->next
) {
1676 int member
= e
->metadata_version
&&
1677 strncmp(e
->metadata_version
,
1678 "external:/", 10) == 0;
1679 if (members
!= member
)
1681 me
= map_by_devnum(&map
, e
->devnum
);
1683 && strcmp(me
->path
, "/unknown") != 0)
1686 name
= get_md_name(e
->devnum
);
1689 pr_err("cannot find device file for %s\n",
1694 rv
|= Detail(name
, v
,
1698 rv
|= WaitClean(name
, -1, v
);
1706 static int stop_scan(int quiet
)
1708 /* apply --stop to all devices in /proc/mdstat */
1709 /* Due to possible stacking of devices, repeat until
1710 * nothing more can be stopped
1712 int progress
=1, err
;
1716 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1717 struct mdstat_ent
*e
;
1719 if (!progress
) last
= 1;
1720 progress
= 0; err
= 0;
1721 for (e
=ms
; e
; e
=e
->next
) {
1722 char *name
= get_md_name(e
->devnum
);
1726 pr_err("cannot find device file for %s\n",
1730 mdfd
= open_mddev(name
, 1);
1732 if (Manage_runstop(name
, mdfd
, -1, quiet
?1:last
?0:-1))
1742 } while (!last
&& err
);
1748 static int misc_list(struct mddev_dev
*devlist
,
1749 int brief
, int verbose
, int export
, int test
,
1750 char *homehost
, char *prefer
, char *subarray
,
1751 char *update
, struct mddev_ident
*ident
,
1752 struct supertype
*ss
, int force
, int quiet
)
1754 struct mddev_dev
*dv
;
1757 for (dv
=devlist
; dv
; dv
=dv
->next
) {
1760 switch(dv
->disposition
) {
1762 rv
|= Detail(dv
->devname
,
1764 export
, test
, homehost
, prefer
);
1766 case KillOpt
: /* Zero superblock */
1768 rv
|= Kill(dv
->devname
, ss
, force
, quiet
,0);
1772 rv
|= Kill(dv
->devname
, NULL
, force
, q
, 0);
1779 rv
|= Query(dv
->devname
); continue;
1781 rv
|= ExamineBitmap(dv
->devname
, brief
, ss
); continue;
1784 rv
|= Wait(dv
->devname
); continue;
1786 rv
|= WaitClean(dv
->devname
, -1, verbose
-quiet
); continue;
1788 rv
|= Kill_subarray(dv
->devname
, subarray
, quiet
);
1790 case UpdateSubarray
:
1791 if (update
== NULL
) {
1792 pr_err("-U/--update must be specified with --update-subarray\n");
1796 rv
|= Update_subarray(dv
->devname
, subarray
,
1797 update
, ident
, quiet
);
1800 mdfd
= open_mddev(dv
->devname
, 1);
1802 switch(dv
->disposition
) {
1804 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1, quiet
); break;
1806 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1, quiet
); break;
1808 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1810 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;