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
= xmalloc(sizeof(*dv
));
321 dv
->devname
= optarg
;
322 dv
->disposition
= devmode
;
323 dv
->writemostly
= writemostly
;
327 devlistend
= &dv
->next
;
332 /* No mode yet, and this is the second device ... */
333 pr_err("An option must be given to set the mode before a second device\n"
334 " (%s) is listed\n", optarg
);
337 if (option_index
>= 0)
338 pr_err("--%s", long_options
[option_index
].name
);
341 fprintf(stderr
, " does not set the mode, and so cannot be the first option.\n");
345 /* if we just set the mode, then done */
359 /* an undecorated option - must be a device name.
361 if (devs_found
> 0 && mode
== MANAGE
&& !devmode
) {
362 pr_err("Must give one of -a/-r/-f"
363 " for subsequent devices at %s\n", optarg
);
366 if (devs_found
> 0 && mode
== GROW
&& !devmode
) {
367 pr_err("Must give -a/--add for"
368 " devices to add: %s\n", optarg
);
371 dv
= xmalloc(sizeof(*dv
));
372 dv
->devname
= optarg
;
373 dv
->disposition
= devmode
;
374 dv
->writemostly
= writemostly
;
378 devlistend
= &dv
->next
;
384 /* We've got a mode, and opt is now something else which
385 * could depend on the mode */
386 #define O(a,b) ((a<<16)|b)
387 switch (O(mode
,opt
)) {
389 case O(GROW
,ChunkSize
):
391 case O(CREATE
,ChunkSize
):
392 case O(BUILD
,'c'): /* chunk or rounding */
393 case O(BUILD
,ChunkSize
): /* chunk or rounding */
395 pr_err("chunk/rounding may only be specified once. "
396 "Second value is %s.\n", optarg
);
399 chunk
= parse_size(optarg
);
400 if (chunk
< 8 || (chunk
&1)) {
401 pr_err("invalid chunk/rounding value: %s\n",
405 /* Convert sectors to K */
409 case O(INCREMENTAL
, 'e'):
411 case O(ASSEMBLE
,'e'):
412 case O(MISC
,'e'): /* set metadata (superblock) information */
414 pr_err("metadata information already given\n");
417 for(i
=0; !ss
&& superlist
[i
]; i
++)
418 ss
= superlist
[i
]->match_metadata_desc(optarg
);
421 pr_err("unrecognised metadata identifier: %s\n", optarg
);
427 case O(MANAGE
,WriteMostly
):
429 case O(BUILD
,WriteMostly
):
431 case O(CREATE
,WriteMostly
):
432 /* set write-mostly for following devices */
437 /* clear write-mostly for following devices */
444 case O(BUILD
,'z'): /* size */
446 pr_err("size may only be specified once. "
447 "Second value is %s.\n", optarg
);
450 if (strcmp(optarg
, "max")==0)
453 size
= parse_size(optarg
);
455 pr_err("invalid size: %s\n",
459 /* convert sectors to K */
464 case O(GROW
,'Z'): /* array size */
465 if (array_size
>= 0) {
466 pr_err("array-size may only be specified once. "
467 "Second value is %s.\n", optarg
);
470 if (strcmp(optarg
, "max") == 0)
473 array_size
= parse_size(optarg
);
474 if (array_size
<= 0) {
475 pr_err("invalid array size: %s\n",
484 case O(BUILD
,'l'): /* set raid level*/
485 if (level
!= UnSet
) {
486 pr_err("raid level may only be set once. "
487 "Second value is %s.\n", optarg
);
490 level
= map_name(pers
, optarg
);
491 if (level
== UnSet
) {
492 pr_err("invalid raid level: %s\n",
496 if (level
!= 0 && level
!= LEVEL_LINEAR
&& level
!= 1 &&
497 level
!= LEVEL_MULTIPATH
&& level
!= LEVEL_FAULTY
&&
500 pr_err("Raid level %s not permitted with --build.\n",
504 if (sparedisks
> 0 && level
< 1 && level
>= -1) {
505 pr_err("raid level %s is incompatible with spare-devices setting.\n",
512 case O(GROW
, 'p'): /* new layout */
513 case O(GROW
, Layout
):
515 pr_err("layout may only be sent once. "
516 "Second value was %s\n", optarg
);
520 /* 'Grow' will parse the value */
523 case O(CREATE
,'p'): /* raid5 layout */
524 case O(CREATE
,Layout
):
525 case O(BUILD
,'p'): /* faulty layout */
526 case O(BUILD
,Layout
):
527 if (layout
!= UnSet
) {
528 pr_err("layout may only be sent once. "
529 "Second value was %s\n", optarg
);
534 pr_err("layout not meaningful for %s arrays.\n",
535 map_num(pers
, level
));
538 pr_err("raid level must be given before layout.\n");
542 layout
= map_name(r5layout
, optarg
);
544 pr_err("layout %s not understood for raid5.\n",
550 layout
= map_name(r6layout
, optarg
);
552 pr_err("layout %s not understood for raid6.\n",
559 layout
= parse_layout_10(optarg
);
561 pr_err("layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg
);
569 layout
= parse_layout_faulty(optarg
);
571 pr_err("layout %s not understood for faulty.\n",
579 case O(CREATE
,AssumeClean
):
580 case O(BUILD
,AssumeClean
): /* assume clean */
581 case O(GROW
,AssumeClean
):
587 case O(BUILD
,'n'): /* number of raid disks */
589 pr_err("raid-devices set twice: %d and %s\n",
593 raiddisks
= strtol(optarg
, &c
, 10);
594 if (!optarg
[0] || *c
|| raiddisks
<=0) {
595 pr_err("invalid number of raid devices: %s\n",
599 ident
.raid_disks
= raiddisks
;
602 case O(CREATE
,'x'): /* number of spare (eXtra) disks */
604 pr_err("spare-devices set twice: %d and %s\n",
608 if (level
!= UnSet
&& level
<= 0 && level
>= -1) {
609 pr_err("spare-devices setting is incompatible with raid level %d\n",
613 sparedisks
= strtol(optarg
, &c
, 10);
614 if (!optarg
[0] || *c
|| sparedisks
< 0) {
615 pr_err("invalid number of spare-devices: %s\n",
625 case O(INCREMENTAL
,'a'):
626 case O(INCREMENTAL
,Auto
):
627 case O(ASSEMBLE
,'a'):
628 case O(ASSEMBLE
,Auto
): /* auto-creation of device node */
629 autof
= parse_auto(optarg
, "--auto flag", 0);
632 case O(CREATE
,Symlinks
):
633 case O(BUILD
,Symlinks
):
634 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
638 case O(BUILD
,'f'): /* force honouring '-n 1' */
639 case O(BUILD
,Force
): /* force honouring '-n 1' */
640 case O(GROW
,'f'): /* ditto */
641 case O(GROW
,Force
): /* ditto */
642 case O(CREATE
,'f'): /* force honouring of device list */
643 case O(CREATE
,Force
): /* force honouring of device list */
644 case O(ASSEMBLE
,'f'): /* force assembly */
645 case O(ASSEMBLE
,Force
): /* force assembly */
646 case O(MISC
,'f'): /* force zero */
647 case O(MISC
,Force
): /* force zero */
648 case O(MANAGE
,Force
): /* add device which is too large */
651 /* now for the Assemble options */
652 case O(ASSEMBLE
, FreezeReshape
): /* Freeze reshape during
654 case O(INCREMENTAL
, FreezeReshape
):
657 case O(CREATE
,'u'): /* uuid of array */
658 case O(ASSEMBLE
,'u'): /* uuid of array */
659 if (ident
.uuid_set
) {
660 pr_err("uuid cannot be set twice. "
661 "Second value %s.\n", optarg
);
664 if (parse_uuid(optarg
, ident
.uuid
))
667 pr_err("Bad uuid: %s\n", optarg
);
673 case O(ASSEMBLE
,'N'):
676 pr_err("name cannot be set twice. "
677 "Second value %s.\n", optarg
);
680 if (mode
== MISC
&& !subarray
) {
681 pr_err("-N/--name only valid with --update-subarray in misc mode\n");
684 if (strlen(optarg
) > 32) {
685 pr_err("name '%s' is too long, 32 chars max.\n",
689 strcpy(ident
.name
, optarg
);
692 case O(ASSEMBLE
,'m'): /* super-minor for array */
693 case O(ASSEMBLE
,SuperMinor
):
694 if (ident
.super_minor
!= UnSet
) {
695 pr_err("super-minor cannot be set twice. "
696 "Second value: %s.\n", optarg
);
699 if (strcmp(optarg
, "dev")==0)
700 ident
.super_minor
= -2;
702 ident
.super_minor
= strtoul(optarg
, &cp
, 10);
703 if (!optarg
[0] || *cp
) {
704 pr_err("Bad super-minor number: %s.\n", optarg
);
710 case O(ASSEMBLE
,'o'):
716 case O(ASSEMBLE
,'U'): /* update the superblock */
719 pr_err("Can only update one aspect"
720 " of superblock, both %s and %s given.\n",
724 if (mode
== MISC
&& !subarray
) {
725 pr_err("Only subarrays can be"
726 " updated in misc mode\n");
730 if (strcmp(update
, "sparc2.2")==0)
732 if (strcmp(update
, "super-minor") == 0)
734 if (strcmp(update
, "summaries")==0)
736 if (strcmp(update
, "resync")==0)
738 if (strcmp(update
, "uuid")==0)
740 if (strcmp(update
, "name")==0)
742 if (strcmp(update
, "homehost")==0)
744 if (strcmp(update
, "devicesize")==0)
746 if (strcmp(update
, "no-bitmap")==0)
748 if (strcmp(update
, "byteorder")==0) {
750 pr_err("must not set metadata"
751 " type with --update=byteorder.\n");
754 for(i
=0; !ss
&& superlist
[i
]; i
++)
755 ss
= superlist
[i
]->match_metadata_desc(
758 pr_err("INTERNAL ERROR"
759 " cannot find 0.swap\n");
765 if (strcmp(update
,"?") == 0 ||
766 strcmp(update
, "help") == 0) {
768 fprintf(outf
, Name
": ");
772 Name
": '--update=%s' is invalid. ",
775 fprintf(outf
, "Valid --update options are:\n"
776 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
777 " 'summaries', 'homehost', 'byteorder', 'devicesize',\n"
779 exit(outf
== stdout
? 0 : 2);
782 /* update=devicesize is allowed with --re-add */
783 if (devmode
!= 'A') {
784 pr_err("--update in Manage mode only"
785 " allowed with --re-add.\n");
789 pr_err("Can only update one aspect"
790 " of superblock, both %s and %s given.\n",
795 if (strcmp(update
, "devicesize") != 0) {
796 pr_err("only 'devicesize' can be"
797 " updated with --re-add\n");
802 case O(INCREMENTAL
,NoDegraded
):
803 pr_err("--no-degraded is deprecated in Incremental mode\n");
804 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
805 runstop
= -1; /* --stop isn't allowed for --assemble,
806 * so we overload slightly */
809 case O(ASSEMBLE
,'c'):
810 case O(ASSEMBLE
,ConfigFile
):
811 case O(INCREMENTAL
, 'c'):
812 case O(INCREMENTAL
, ConfigFile
):
814 case O(MISC
, ConfigFile
):
816 case O(MONITOR
,ConfigFile
):
818 pr_err("configfile cannot be set twice. "
819 "Second value is %s.\n", optarg
);
823 set_conffile(configfile
);
824 /* FIXME possibly check that config file exists. Even parse it */
826 case O(ASSEMBLE
,'s'): /* scan */
829 case O(INCREMENTAL
,'s'):
833 case O(MONITOR
,'m'): /* mail address */
834 case O(MONITOR
,EMail
):
836 pr_err("only specify one mailaddress. %s ignored.\n",
842 case O(MONITOR
,'p'): /* alert program */
843 case O(MONITOR
,ProgramOpt
): /* alert program */
845 pr_err("only specify one alter program. %s ignored.\n",
851 case O(MONITOR
,'r'): /* rebuild increments */
852 case O(MONITOR
,Increment
):
853 increments
= atoi(optarg
);
854 if (increments
> 99 || increments
< 1) {
855 pr_err("please specify positive integer between 1 and 99 as rebuild increments.\n");
860 case O(MONITOR
,'d'): /* delay in seconds */
862 case O(BUILD
,'d'): /* delay for bitmap updates */
865 pr_err("only specify delay once. %s ignored.\n",
868 delay
= strtol(optarg
, &c
, 10);
869 if (!optarg
[0] || *c
|| delay
<1) {
870 pr_err("invalid delay: %s\n",
876 case O(MONITOR
,'f'): /* daemonise */
877 case O(MONITOR
,Fork
):
880 case O(MONITOR
,'i'): /* pid */
882 pr_err("only specify one pid file. %s ignored.\n",
887 case O(MONITOR
,'1'): /* oneshot */
891 case O(MONITOR
,'t'): /* test */
894 case O(MONITOR
,'y'): /* log messages to syslog */
895 openlog("mdadm", LOG_PID
, SYSLOG_FACILITY
);
898 case O(MONITOR
, NoSharing
):
902 /* now the general management options. Some are applicable
903 * to other modes. None have arguments.
908 case O(MANAGE
,Add
): /* add a drive */
911 case O(MANAGE
,ReAdd
):
914 case O(MANAGE
,'r'): /* remove a drive */
915 case O(MANAGE
,Remove
):
918 case O(MANAGE
,'f'): /* set faulty */
920 case O(INCREMENTAL
,'f'):
921 case O(INCREMENTAL
,Remove
):
922 case O(INCREMENTAL
,Fail
): /* r for incremental is taken, use f
923 * even though we will both fail and
924 * remove the device */
927 case O(INCREMENTAL
,'R'):
929 case O(ASSEMBLE
,'R'):
931 case O(CREATE
,'R'): /* Run the array */
933 pr_err("Cannot both Stop and Run an array\n");
940 pr_err("Cannot both Run and Stop an array\n");
952 case O(MISC
,KillOpt
):
959 case O(MISC
, WaitOpt
):
960 case O(MISC
, Waitclean
):
961 case O(MISC
, DetailPlatform
):
962 case O(MISC
, KillSubarray
):
963 case O(MISC
, UpdateSubarray
):
964 if (opt
== KillSubarray
|| opt
== UpdateSubarray
) {
966 pr_err("subarray can only"
967 " be specified once\n");
972 if (devmode
&& devmode
!= opt
&&
973 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
974 pr_err("--examine/-E cannot be given with ");
975 if (devmode
== 'E') {
976 if (option_index
>= 0)
977 fprintf(stderr
, "--%s\n",
978 long_options
[option_index
].name
);
980 fprintf(stderr
, "-%c\n", opt
);
981 } else if (isalpha(devmode
))
982 fprintf(stderr
, "-%c\n", devmode
);
984 fprintf(stderr
, "previous option\n");
989 case O(MISC
, UdevRules
):
990 if (devmode
&& devmode
!= opt
) {
991 pr_err("--udev-rules must"
992 " be the only option.\n");
995 pr_err("only specify one udev "
996 "rule filename. %s ignored.\n",
999 udev_filename
= optarg
;
1007 case O(MISC
, Sparc22
):
1008 if (devmode
!= 'E') {
1009 pr_err("--sparc2.2 only allowed with --examine\n");
1015 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
1016 case O(ASSEMBLE
,Bitmap
):
1018 pr_err("bitmap file needed with -b in --assemble mode\n");
1021 if (strcmp(optarg
, "internal")==0) {
1022 pr_err("there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
1025 bitmap_fd
= open(optarg
, O_RDWR
);
1026 if (!*optarg
|| bitmap_fd
< 0) {
1027 pr_err("cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
1030 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
1033 case O(ASSEMBLE
, BackupFile
):
1034 case O(GROW
, BackupFile
):
1035 /* Specify a file into which grow might place a backup,
1036 * or from which assemble might recover a backup
1039 pr_err("backup file already specified, rejecting %s\n", optarg
);
1042 backup_file
= optarg
;
1045 case O(GROW
, Continue
):
1046 /* Continue interrupted grow
1050 case O(ASSEMBLE
, InvalidBackup
):
1051 /* Acknowledge that the backupfile is invalid, but ask
1052 * to continue anyway
1058 case O(BUILD
,Bitmap
):
1060 case O(CREATE
,Bitmap
): /* here we create the bitmap */
1061 if (strcmp(optarg
, "none") == 0) {
1062 pr_err("'--bitmap none' only"
1063 " supported for --grow\n");
1068 case O(GROW
,Bitmap
):
1069 if (strcmp(optarg
, "internal")== 0 ||
1070 strcmp(optarg
, "none")== 0 ||
1071 strchr(optarg
, '/') != NULL
) {
1072 bitmap_file
= optarg
;
1076 pr_err("bitmap file must contain a '/', or be 'internal', or 'none'\n"
1077 " not '%s'\n", optarg
);
1080 case O(GROW
,BitmapChunk
):
1081 case O(BUILD
,BitmapChunk
):
1082 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
1083 bitmap_chunk
= parse_size(optarg
);
1084 if (bitmap_chunk
<= 0 ||
1085 bitmap_chunk
& (bitmap_chunk
- 1)) {
1086 pr_err("invalid bitmap chunksize: %s\n",
1090 bitmap_chunk
= bitmap_chunk
* 512;
1093 case O(GROW
, WriteBehind
):
1094 case O(BUILD
, WriteBehind
):
1095 case O(CREATE
, WriteBehind
): /* write-behind mode */
1096 write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
1098 write_behind
= strtol(optarg
, &c
, 10);
1099 if (write_behind
< 0 || *c
||
1100 write_behind
> 16383) {
1101 pr_err("Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
1107 case O(INCREMENTAL
, 'r'):
1108 case O(INCREMENTAL
, RebuildMapOpt
):
1111 case O(INCREMENTAL
, IncrementalPath
):
1112 remove_path
= optarg
;
1115 /* We have now processed all the valid options. Anything else is
1118 if (option_index
> 0)
1119 pr_err(":option --%s not valid in %s mode\n",
1120 long_options
[option_index
].name
,
1121 map_num(modes
, mode
));
1123 pr_err("option -%c not valid in %s mode\n",
1124 opt
, map_num(modes
, mode
));
1131 if (print_help
== 2)
1132 help_text
= OptionHelp
;
1134 help_text
= mode_help
[mode
];
1135 if (help_text
== NULL
)
1137 fputs(help_text
,stdout
);
1141 if (!mode
&& devs_found
) {
1144 if (devlist
->disposition
== 0)
1145 devlist
->disposition
= devmode
;
1148 fputs(Usage
, stderr
);
1153 struct createinfo
*ci
= conf_get_create_info();
1155 if (strcasecmp(symlinks
, "yes") == 0)
1157 else if (strcasecmp(symlinks
, "no") == 0)
1160 pr_err("option --symlinks must be 'no' or 'yes'\n");
1164 /* Ok, got the option parsing out of the way
1165 * hopefully it's mostly right but there might be some stuff
1168 * That is mosty checked in the per-mode stuff but...
1170 * For @,B,C and A without -s, the first device listed must be
1171 * an md device. We check that here and open it.
1174 if (mode
== MANAGE
|| mode
== BUILD
|| mode
== CREATE
1176 || (mode
== ASSEMBLE
&& ! scan
)) {
1177 if (devs_found
< 1) {
1178 pr_err("an md device must be given in this mode\n");
1181 if ((int)ident
.super_minor
== -2 && autof
) {
1182 pr_err("--super-minor=dev is incompatible with --auto\n");
1185 if (mode
== MANAGE
|| mode
== GROW
) {
1186 mdfd
= open_mddev(devlist
->devname
, 1);
1190 /* non-existent device is OK */
1191 mdfd
= open_mddev(devlist
->devname
, 0);
1193 pr_err("device %s exists but is not an "
1194 "md array.\n", devlist
->devname
);
1197 if ((int)ident
.super_minor
== -2) {
1200 pr_err("--super-minor=dev given, and "
1201 "listed device %s doesn't exist.\n",
1206 ident
.super_minor
= minor(stb
.st_rdev
);
1208 if (mdfd
>= 0 && mode
!= MANAGE
&& mode
!= GROW
) {
1209 /* We don't really want this open yet, we just might
1210 * have wanted to check some things
1218 if (raiddisks
== 1 && !force
&& level
!= LEVEL_FAULTY
) {
1219 pr_err("'1' is an unusual number of drives for an array, so it is probably\n"
1220 " a mistake. If you really mean it you will need to specify --force before\n"
1221 " setting the number of drives.\n");
1226 if (homehost
== NULL
)
1227 homehost
= conf_get_homehost(&require_homehost
);
1228 if (homehost
== NULL
|| strcasecmp(homehost
, "<system>")==0) {
1229 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
1230 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
1231 homehost
= sys_hostname
;
1234 if (homehost
&& (!homehost
[0] || strcasecmp(homehost
, "<none>") == 0)) {
1236 require_homehost
= 0;
1239 if ((mode
== MISC
&& devmode
== 'E')
1240 || (mode
== MONITOR
&& spare_sharing
== 0))
1241 /* Anyone may try this */;
1242 else if (geteuid() != 0) {
1243 pr_err("must be super-user to perform this action\n");
1247 ident
.autof
= autof
;
1252 /* readonly, add/remove, readwrite, runstop */
1254 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1255 if (!rv
&& devs_found
>1)
1256 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
1257 devlist
->next
, verbose
-quiet
, test
,
1259 if (!rv
&& readonly
< 0)
1260 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1262 rv
= Manage_runstop(devlist
->devname
, mdfd
, runstop
, quiet
);
1265 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1266 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !scan
) {
1267 /* Only a device has been given, so get details from config file */
1268 struct mddev_ident
*array_ident
= conf_get_ident(devlist
->devname
);
1269 if (array_ident
== NULL
) {
1270 pr_err("%s not identified in config file.\n",
1276 if (array_ident
->autof
== 0)
1277 array_ident
->autof
= autof
;
1278 rv
|= Assemble(ss
, devlist
->devname
, array_ident
,
1279 NULL
, backup_file
, invalid_backup
,
1280 readonly
, runstop
, update
,
1281 homehost
, require_homehost
,
1282 verbose
-quiet
, force
,
1286 rv
= Assemble(ss
, devlist
->devname
, &ident
,
1287 devlist
->next
, backup_file
, invalid_backup
,
1288 readonly
, runstop
, update
,
1289 homehost
, require_homehost
,
1290 verbose
-quiet
, force
,
1292 else if (devs_found
> 0) {
1293 if (update
&& devs_found
> 1) {
1294 pr_err("can only update a single array at a time\n");
1297 if (backup_file
&& devs_found
> 1) {
1298 pr_err("can only assemble a single array when providing a backup file.\n");
1301 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1302 struct mddev_ident
*array_ident
= conf_get_ident(dv
->devname
);
1303 if (array_ident
== NULL
) {
1304 pr_err("%s not identified in config file.\n",
1309 if (array_ident
->autof
== 0)
1310 array_ident
->autof
= autof
;
1311 rv
|= Assemble(ss
, dv
->devname
, array_ident
,
1312 NULL
, backup_file
, invalid_backup
,
1313 readonly
, runstop
, update
,
1314 homehost
, require_homehost
,
1315 verbose
-quiet
, force
,
1320 pr_err("--update not meaningful with a --scan assembly.\n");
1324 pr_err("--backup_file not meaningful with a --scan assembly.\n");
1327 rv
= scan_assemble(autof
, ss
, readonly
, runstop
,
1331 force
, freeze_reshape
);
1336 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1337 if (write_behind
&& !bitmap_file
) {
1338 pr_err("write-behind mode requires a bitmap.\n");
1342 if (raiddisks
== 0) {
1343 pr_err("no raid-devices specified.\n");
1349 if (strcmp(bitmap_file
, "internal")==0) {
1350 pr_err("'internal' bitmaps not supported with --build\n");
1355 rv
= Build(devlist
->devname
, chunk
, level
, layout
,
1356 raiddisks
, devlist
->next
, assume_clean
,
1357 bitmap_file
, bitmap_chunk
, write_behind
,
1358 delay
, verbose
-quiet
, autof
, size
);
1361 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1362 if (write_behind
&& !bitmap_file
) {
1363 pr_err("write-behind mode requires a bitmap.\n");
1367 if (raiddisks
== 0) {
1368 pr_err("no raid-devices specified.\n");
1373 rv
= Create(ss
, devlist
->devname
, chunk
, level
, layout
, size
<0 ? 0 : size
,
1374 raiddisks
, sparedisks
, ident
.name
, homehost
,
1375 ident
.uuid_set
? ident
.uuid
: NULL
,
1376 devs_found
-1, devlist
->next
, runstop
, verbose
-quiet
, force
, assume_clean
,
1377 bitmap_file
, bitmap_chunk
, write_behind
, delay
, autof
);
1380 if (devmode
== 'E') {
1381 if (devlist
== NULL
&& !scan
) {
1382 pr_err("No devices to examine\n");
1385 if (devlist
== NULL
)
1386 devlist
= conf_get_devs();
1387 if (devlist
== NULL
) {
1388 pr_err("No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1391 if (brief
&& verbose
)
1393 rv
= Examine(devlist
, scan
?(verbose
>1?0:verbose
+1):brief
,
1395 SparcAdjust
, ss
, homehost
);
1396 } else if (devmode
== DetailPlatform
) {
1397 rv
= Detail_Platform(ss
? ss
->ss
: NULL
, ss
? scan
: 1, verbose
);
1398 } else if (devlist
== NULL
) {
1399 if (devmode
== 'S' && scan
)
1400 rv
= stop_scan(quiet
);
1401 else if ((devmode
== 'D' || devmode
== Waitclean
) && scan
)
1402 rv
= misc_scan(devmode
, verbose
, export
,
1403 test
, homehost
, prefer
);
1404 else if (devmode
== UdevRules
)
1405 rv
= Write_rules(udev_filename
);
1407 pr_err("No devices given.\n");
1411 rv
= misc_list(devlist
, brief
, verbose
, export
, test
,
1412 homehost
, prefer
, subarray
, update
,
1417 if (!devlist
&& !scan
) {
1418 pr_err("Cannot monitor: need --scan or at least one device\n");
1422 if (pidfile
&& !daemonise
) {
1423 pr_err("Cannot write a pid file when not in daemon mode\n");
1428 if (get_linux_version() > 2006016)
1429 /* mdstat responds to poll */
1434 rv
= Monitor(devlist
, mailaddr
, program
,
1435 delay
?delay
:60, daemonise
, scan
, oneshot
,
1436 dosyslog
, test
, pidfile
, increments
,
1437 spare_sharing
, prefer
);
1441 if (array_size
>= 0) {
1442 /* alway impose array size first, independent of
1444 * Do not allow level or raid_disks changes at the
1445 * same time as that can be irreversibly destructive.
1449 if (raiddisks
|| level
!= UnSet
) {
1450 pr_err("cannot change array size in same operation "
1451 "as changing raiddisks or level.\n"
1452 " Change size first, then check that data is still intact.\n");
1456 sysfs_init(&sra
, mdfd
, 0);
1457 if (array_size
== 0)
1458 err
= sysfs_set_str(&sra
, NULL
, "array_size", "default");
1460 err
= sysfs_set_num(&sra
, NULL
, "array_size", array_size
/ 2);
1463 pr_err("--array-size setting"
1464 " is too large.\n");
1466 pr_err("current kernel does"
1467 " not support setting --array-size\n");
1472 if (devs_found
> 1 && raiddisks
== 0) {
1474 if (size
>= 0 || chunk
|| layout_str
!= NULL
|| bitmap_file
) {
1475 pr_err("--add cannot be used with "
1476 "other geometry changes in --grow mode\n");
1480 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1481 rv
= Grow_Add_device(devlist
->devname
, mdfd
,
1486 } else if (bitmap_file
) {
1487 if (size
>= 0 || raiddisks
|| chunk
||
1488 layout_str
!= NULL
|| devs_found
> 1) {
1489 pr_err("--bitmap changes cannot be "
1490 "used with other geometry changes "
1491 "in --grow mode\n");
1496 delay
= DEFAULT_BITMAP_DELAY
;
1497 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, bitmap_file
,
1498 bitmap_chunk
, delay
, write_behind
, force
);
1499 } else if (grow_continue
)
1500 rv
= Grow_continue_command(devlist
->devname
,
1503 else if (size
>= 0 || raiddisks
!= 0 || layout_str
!= NULL
1504 || chunk
!= 0 || level
!= UnSet
) {
1505 rv
= Grow_reshape(devlist
->devname
, mdfd
, quiet
, backup_file
,
1506 size
, level
, layout_str
, chunk
, raiddisks
,
1508 assume_clean
, force
);
1509 } else if (array_size
< 0)
1510 pr_err("no changes to --grow\n");
1518 pr_err("--incremental --scan meaningless without --run.\n");
1521 if (devmode
== 'f') {
1522 pr_err("--incremental --scan --fail not supported.\n");
1525 rv
= IncrementalScan(verbose
);
1528 if (!rebuild_map
&& !scan
) {
1529 pr_err("--incremental requires a device.\n");
1534 if (devlist
->next
) {
1535 pr_err("--incremental can only handle one device.\n");
1540 rv
= IncrementalRemove(devlist
->devname
, remove_path
,
1543 rv
= Incremental(devlist
->devname
, verbose
-quiet
,
1544 runstop
, ss
, homehost
,
1545 require_homehost
, autof
,
1555 static int scan_assemble(int autof
, struct supertype
*ss
,
1556 int readonly
, int runstop
,
1557 struct mddev_ident
*ident
,
1558 char *homehost
, int require_homehost
,
1559 int verbose
, int force
,
1562 struct mddev_ident
*a
, *array_list
= conf_get_ident(NULL
);
1563 struct mddev_dev
*devlist
= conf_get_devs();
1564 struct map_ent
*map
= NULL
;
1567 int failures
, successes
;
1569 if (conf_verify_devnames(array_list
)) {
1570 pr_err("Duplicate MD device names in "
1571 "conf file were found.\n");
1574 if (devlist
== NULL
) {
1575 pr_err("No devices listed in conf file were found.\n");
1578 for (a
= array_list
; a
; a
= a
->next
) {
1584 pr_err("%s: failed to get "
1585 "exclusive lock on mapfile\n",
1591 for (a
= array_list
; a
; a
= a
->next
) {
1596 strcasecmp(a
->devname
, "<ignore>") == 0)
1599 r
= Assemble(ss
, a
->devname
,
1602 readonly
, runstop
, NULL
,
1603 homehost
, require_homehost
,
1614 } while (failures
&& successes
);
1615 if (homehost
&& cnt
== 0) {
1616 /* Maybe we can auto-assemble something.
1617 * Repeatedly call Assemble in auto-assemble mode
1622 ident
->autof
= autof
;
1624 struct mddev_dev
*devlist
= conf_get_devs();
1627 rv2
= Assemble(ss
, NULL
,
1642 /* Incase there are stacked devices, we need to go around again */
1644 if (cnt
== 0 && rv
== 0) {
1645 pr_err("No arrays found in config file or automatically\n");
1649 } else if (cnt
== 0 && rv
== 0) {
1650 pr_err("No arrays found in config file\n");
1657 static int misc_scan(char devmode
, int verbose
, int export
, int test
,
1658 char *homehost
, char *prefer
)
1660 /* apply --detail or --wait-clean to
1661 * all devices in /proc/mdstat
1663 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1664 struct mdstat_ent
*e
;
1665 struct map_ent
*map
= NULL
;
1667 int v
= verbose
>1?0:verbose
+1;
1670 for (members
= 0; members
<= 1; members
++) {
1671 for (e
=ms
; e
; e
=e
->next
) {
1674 int member
= e
->metadata_version
&&
1675 strncmp(e
->metadata_version
,
1676 "external:/", 10) == 0;
1677 if (members
!= member
)
1679 me
= map_by_devnum(&map
, e
->devnum
);
1681 && strcmp(me
->path
, "/unknown") != 0)
1684 name
= get_md_name(e
->devnum
);
1687 pr_err("cannot find device file for %s\n",
1692 rv
|= Detail(name
, v
,
1696 rv
|= WaitClean(name
, -1, v
);
1704 static int stop_scan(int quiet
)
1706 /* apply --stop to all devices in /proc/mdstat */
1707 /* Due to possible stacking of devices, repeat until
1708 * nothing more can be stopped
1710 int progress
=1, err
;
1714 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1715 struct mdstat_ent
*e
;
1717 if (!progress
) last
= 1;
1718 progress
= 0; err
= 0;
1719 for (e
=ms
; e
; e
=e
->next
) {
1720 char *name
= get_md_name(e
->devnum
);
1724 pr_err("cannot find device file for %s\n",
1728 mdfd
= open_mddev(name
, 1);
1730 if (Manage_runstop(name
, mdfd
, -1, quiet
?1:last
?0:-1))
1740 } while (!last
&& err
);
1746 static int misc_list(struct mddev_dev
*devlist
,
1747 int brief
, int verbose
, int export
, int test
,
1748 char *homehost
, char *prefer
, char *subarray
,
1749 char *update
, struct mddev_ident
*ident
,
1750 struct supertype
*ss
, int force
, int quiet
)
1752 struct mddev_dev
*dv
;
1755 for (dv
=devlist
; dv
; dv
=dv
->next
) {
1758 switch(dv
->disposition
) {
1760 rv
|= Detail(dv
->devname
,
1762 export
, test
, homehost
, prefer
);
1764 case KillOpt
: /* Zero superblock */
1766 rv
|= Kill(dv
->devname
, ss
, force
, quiet
,0);
1770 rv
|= Kill(dv
->devname
, NULL
, force
, q
, 0);
1777 rv
|= Query(dv
->devname
); continue;
1779 rv
|= ExamineBitmap(dv
->devname
, brief
, ss
); continue;
1782 rv
|= Wait(dv
->devname
); continue;
1784 rv
|= WaitClean(dv
->devname
, -1, verbose
-quiet
); continue;
1786 rv
|= Kill_subarray(dv
->devname
, subarray
, quiet
);
1788 case UpdateSubarray
:
1789 if (update
== NULL
) {
1790 pr_err("-U/--update must be specified with --update-subarray\n");
1794 rv
|= Update_subarray(dv
->devname
, subarray
,
1795 update
, ident
, quiet
);
1798 mdfd
= open_mddev(dv
->devname
, 1);
1800 switch(dv
->disposition
) {
1802 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1, quiet
); break;
1804 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1, quiet
); break;
1806 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1808 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;