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.
32 int main(int argc
, char *argv
[])
43 long long array_size
= -1;
46 char *layout_str
= NULL
;
49 struct mddev_ident ident
;
50 char *configfile
= NULL
;
59 char *bitmap_file
= NULL
;
60 char *backup_file
= NULL
;
61 int invalid_backup
= 0;
62 int bitmap_chunk
= UnSet
;
64 struct mddev_dev
*devlist
= NULL
;
65 struct mddev_dev
**devlistend
= & devlist
;
76 char *symlinks
= NULL
;
77 int grow_continue
= 0;
78 /* autof indicates whether and how to create device node.
79 * bottom 3 bits are style. Rest (when shifted) are number of parts
81 * 1 - don't create (no)
82 * 2 - if is_standard, then create (yes)
83 * 3 - create as 'md' - reject is_standard mdp (md)
84 * 4 - create as 'mdp' - reject is_standard md (mdp)
85 * 5 - default to md if not is_standard (md in config file)
86 * 6 - default to mdp if not is_standard (part, or mdp in config file)
90 char *homehost
= NULL
;
91 char sys_hostname
[256];
92 int require_homehost
= 1;
93 char *mailaddr
= NULL
;
100 int spare_sharing
= 1;
101 struct supertype
*ss
= NULL
;
104 char *shortopt
= short_options
;
107 char *subarray
= NULL
;
108 char *remove_path
= NULL
;
109 char *udev_filename
= NULL
;
116 int freeze_reshape
= 0;
118 srandom(time(0) ^ getpid());
122 ident
.raid_disks
= UnSet
;
123 ident
.super_minor
= UnSet
;
125 ident
.spare_group
= NULL
;
128 ident
.bitmap_fd
= -1;
129 ident
.bitmap_file
= NULL
;
131 ident
.container
= NULL
;
134 while ((option_index
= -1) ,
135 (opt
=getopt_long(argc
, argv
,
136 shortopt
, long_options
,
137 &option_index
)) != -1) {
139 /* firstly, some mode-independent options */
149 fputs(Version
, stderr
);
159 if (mode
== ASSEMBLE
|| mode
== BUILD
|| mode
== CREATE
160 || mode
== GROW
|| mode
== INCREMENTAL
162 break; /* b means bitmap */
171 if (strcasecmp(optarg
, "<ignore>") == 0)
172 require_homehost
= 0;
178 * --offroot sets first char of argv[0] to @. This is used
179 * by systemd to signal that the task was launched from
180 * initrd/initramfs and should be preserved during shutdown
190 if (asprintf(&prefer
, "/%s/", optarg
) <= 0)
196 fputs(Usage
, stderr
);
199 /* second, figure out the mode.
200 * Some options force the mode. Others
201 * set the mode if it isn't already
207 shortopt
= short_bitmap_options
;
215 case ReAdd
: /* re-add */
218 shortopt
= short_bitmap_options
;
222 case 'A': newmode
= ASSEMBLE
;
223 shortopt
= short_bitmap_auto_options
;
225 case 'B': newmode
= BUILD
;
226 shortopt
= short_bitmap_auto_options
;
228 case 'C': newmode
= CREATE
;
229 shortopt
= short_bitmap_auto_options
;
231 case 'F': newmode
= MONITOR
;
233 case 'G': newmode
= GROW
;
234 shortopt
= short_bitmap_options
;
236 case 'I': newmode
= INCREMENTAL
;
237 shortopt
= short_bitmap_auto_options
;
240 newmode
= AUTODETECT
;
271 if (mode
&& newmode
== mode
) {
272 /* everybody happy ! */
273 } else if (mode
&& newmode
!= mode
) {
275 fprintf(stderr
, Name
": ");
276 if (option_index
>= 0)
277 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
279 fprintf(stderr
, "-%c", opt
);
280 fprintf(stderr
, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
281 map_num(modes
, newmode
),
282 map_num(modes
, mode
));
284 } else if (!mode
&& newmode
) {
286 if (mode
== MISC
&& devs_found
) {
287 fprintf(stderr
, Name
": No action given for %s in --misc mode\n",
289 fprintf(stderr
," Action options must come before device names\n");
293 /* special case of -c --help */
294 if ((opt
== 'c' || opt
== ConfigFile
) &&
295 ( strncmp(optarg
, "--h", 3)==0 ||
296 strncmp(optarg
, "-h", 2)==0)) {
297 fputs(Help_config
, stdout
);
301 /* If first option is a device, don't force the mode yet */
303 if (devs_found
== 0) {
304 dv
= malloc(sizeof(*dv
));
306 fprintf(stderr
, Name
": malloc failed\n");
309 dv
->devname
= optarg
;
310 dv
->disposition
= devmode
;
311 dv
->writemostly
= writemostly
;
316 devlistend
= &dv
->next
;
321 /* No mode yet, and this is the second device ... */
322 fprintf(stderr
, Name
": An option must be given to set the mode before a second device\n"
323 " (%s) is listed\n", optarg
);
326 if (option_index
>= 0)
327 fprintf(stderr
, Name
": --%s", long_options
[option_index
].name
);
329 fprintf(stderr
, Name
": -%c", opt
);
330 fprintf(stderr
, " does not set the mode, and so cannot be the first option.\n");
334 /* if we just set the mode, then done */
348 /* an undecorated option - must be a device name.
350 if (devs_found
> 0 && mode
== MANAGE
&& !devmode
) {
351 fprintf(stderr
, Name
": Must give one of -a/-r/-f"
352 " for subsequent devices at %s\n", optarg
);
355 if (devs_found
> 0 && mode
== GROW
&& !devmode
) {
356 fprintf(stderr
, Name
": Must give -a/--add for"
357 " devices to add: %s\n", optarg
);
360 dv
= malloc(sizeof(*dv
));
362 fprintf(stderr
, Name
": malloc failed\n");
365 dv
->devname
= optarg
;
366 dv
->disposition
= devmode
;
367 dv
->writemostly
= writemostly
;
372 devlistend
= &dv
->next
;
378 /* We've got a mode, and opt is now something else which
379 * could depend on the mode */
380 #define O(a,b) ((a<<16)|b)
381 switch (O(mode
,opt
)) {
383 case O(GROW
,ChunkSize
):
385 case O(CREATE
,ChunkSize
):
386 case O(BUILD
,'c'): /* chunk or rounding */
387 case O(BUILD
,ChunkSize
): /* chunk or rounding */
389 fprintf(stderr
, Name
": chunk/rounding may only be specified once. "
390 "Second value is %s.\n", optarg
);
393 chunk
= parse_size(optarg
);
394 if (chunk
< 8 || (chunk
&1)) {
395 fprintf(stderr
, Name
": invalid chunk/rounding value: %s\n",
399 /* Convert sectors to K */
403 case O(INCREMENTAL
, 'e'):
405 case O(ASSEMBLE
,'e'):
406 case O(MISC
,'e'): /* set metadata (superblock) information */
408 fprintf(stderr
, Name
": metadata information already given\n");
411 for(i
=0; !ss
&& superlist
[i
]; i
++)
412 ss
= superlist
[i
]->match_metadata_desc(optarg
);
415 fprintf(stderr
, Name
": unrecognised metadata identifier: %s\n", optarg
);
421 case O(MANAGE
,WriteMostly
):
423 case O(BUILD
,WriteMostly
):
425 case O(CREATE
,WriteMostly
):
426 /* set write-mostly for following devices */
431 /* clear write-mostly for following devices */
438 case O(BUILD
,'z'): /* size */
440 fprintf(stderr
, Name
": size may only be specified once. "
441 "Second value is %s.\n", optarg
);
444 if (strcmp(optarg
, "max")==0)
447 size
= parse_size(optarg
);
449 fprintf(stderr
, Name
": invalid size: %s\n",
453 /* convert sectors to K */
458 case O(GROW
,'Z'): /* array size */
459 if (array_size
>= 0) {
460 fprintf(stderr
, Name
": array-size may only be specified once. "
461 "Second value is %s.\n", optarg
);
464 if (strcmp(optarg
, "max") == 0)
467 array_size
= parse_size(optarg
);
468 if (array_size
<= 0) {
469 fprintf(stderr
, Name
": invalid array size: %s\n",
478 case O(BUILD
,'l'): /* set raid level*/
479 if (level
!= UnSet
) {
480 fprintf(stderr
, Name
": raid level may only be set once. "
481 "Second value is %s.\n", optarg
);
484 level
= map_name(pers
, optarg
);
485 if (level
== UnSet
) {
486 fprintf(stderr
, Name
": invalid raid level: %s\n",
490 if (level
!= 0 && level
!= LEVEL_LINEAR
&& level
!= 1 &&
491 level
!= LEVEL_MULTIPATH
&& level
!= LEVEL_FAULTY
&&
494 fprintf(stderr
, Name
": Raid level %s not permitted with --build.\n",
498 if (sparedisks
> 0 && level
< 1 && level
>= -1) {
499 fprintf(stderr
, Name
": raid level %s is incompatible with spare-devices setting.\n",
506 case O(GROW
, 'p'): /* new layout */
507 case O(GROW
, Layout
):
509 fprintf(stderr
,Name
": layout may only be sent once. "
510 "Second value was %s\n", optarg
);
514 /* 'Grow' will parse the value */
517 case O(CREATE
,'p'): /* raid5 layout */
518 case O(CREATE
,Layout
):
519 case O(BUILD
,'p'): /* faulty layout */
520 case O(BUILD
,Layout
):
521 if (layout
!= UnSet
) {
522 fprintf(stderr
,Name
": layout may only be sent once. "
523 "Second value was %s\n", optarg
);
528 fprintf(stderr
, Name
": layout not meaningful for %s arrays.\n",
529 map_num(pers
, level
));
532 fprintf(stderr
, Name
": raid level must be given before layout.\n");
536 layout
= map_name(r5layout
, optarg
);
538 fprintf(stderr
, Name
": layout %s not understood for raid5.\n",
544 layout
= map_name(r6layout
, optarg
);
546 fprintf(stderr
, Name
": layout %s not understood for raid6.\n",
553 layout
= parse_layout_10(optarg
);
555 fprintf(stderr
, Name
": layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg
);
563 layout
= parse_layout_faulty(optarg
);
565 fprintf(stderr
, Name
": layout %s not understood for faulty.\n",
573 case O(CREATE
,AssumeClean
):
574 case O(BUILD
,AssumeClean
): /* assume clean */
575 case O(GROW
,AssumeClean
):
581 case O(BUILD
,'n'): /* number of raid disks */
583 fprintf(stderr
, Name
": raid-devices set twice: %d and %s\n",
587 raiddisks
= strtol(optarg
, &c
, 10);
588 if (!optarg
[0] || *c
|| raiddisks
<=0) {
589 fprintf(stderr
, Name
": invalid number of raid devices: %s\n",
593 ident
.raid_disks
= raiddisks
;
596 case O(CREATE
,'x'): /* number of spare (eXtra) disks */
598 fprintf(stderr
,Name
": spare-devices set twice: %d and %s\n",
602 if (level
!= UnSet
&& level
<= 0 && level
>= -1) {
603 fprintf(stderr
, Name
": spare-devices setting is incompatible with raid level %d\n",
607 sparedisks
= strtol(optarg
, &c
, 10);
608 if (!optarg
[0] || *c
|| sparedisks
< 0) {
609 fprintf(stderr
, Name
": invalid number of spare-devices: %s\n",
619 case O(INCREMENTAL
,'a'):
620 case O(INCREMENTAL
,Auto
):
621 case O(ASSEMBLE
,'a'):
622 case O(ASSEMBLE
,Auto
): /* auto-creation of device node */
623 autof
= parse_auto(optarg
, "--auto flag", 0);
626 case O(CREATE
,Symlinks
):
627 case O(BUILD
,Symlinks
):
628 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
632 case O(BUILD
,'f'): /* force honouring '-n 1' */
633 case O(BUILD
,Force
): /* force honouring '-n 1' */
634 case O(GROW
,'f'): /* ditto */
635 case O(GROW
,Force
): /* ditto */
636 case O(CREATE
,'f'): /* force honouring of device list */
637 case O(CREATE
,Force
): /* force honouring of device list */
638 case O(ASSEMBLE
,'f'): /* force assembly */
639 case O(ASSEMBLE
,Force
): /* force assembly */
640 case O(MISC
,'f'): /* force zero */
641 case O(MISC
,Force
): /* force zero */
642 case O(MANAGE
,Force
): /* add device which is too large */
645 /* now for the Assemble options */
646 case O(ASSEMBLE
, FreezeReshape
): /* Freeze reshape during
648 case O(INCREMENTAL
, FreezeReshape
):
651 case O(CREATE
,'u'): /* uuid of array */
652 case O(ASSEMBLE
,'u'): /* uuid of array */
653 if (ident
.uuid_set
) {
654 fprintf(stderr
, Name
": uuid cannot be set twice. "
655 "Second value %s.\n", optarg
);
658 if (parse_uuid(optarg
, ident
.uuid
))
661 fprintf(stderr
,Name
": Bad uuid: %s\n", optarg
);
667 case O(ASSEMBLE
,'N'):
670 fprintf(stderr
, Name
": name cannot be set twice. "
671 "Second value %s.\n", optarg
);
674 if (mode
== MISC
&& !subarray
) {
675 fprintf(stderr
, Name
": -N/--name only valid with --update-subarray in misc mode\n");
678 if (strlen(optarg
) > 32) {
679 fprintf(stderr
, Name
": name '%s' is too long, 32 chars max.\n",
683 strcpy(ident
.name
, optarg
);
686 case O(ASSEMBLE
,'m'): /* super-minor for array */
687 case O(ASSEMBLE
,SuperMinor
):
688 if (ident
.super_minor
!= UnSet
) {
689 fprintf(stderr
, Name
": super-minor cannot be set twice. "
690 "Second value: %s.\n", optarg
);
693 if (strcmp(optarg
, "dev")==0)
694 ident
.super_minor
= -2;
696 ident
.super_minor
= strtoul(optarg
, &cp
, 10);
697 if (!optarg
[0] || *cp
) {
698 fprintf(stderr
, Name
": Bad super-minor number: %s.\n", optarg
);
704 case O(ASSEMBLE
,'U'): /* update the superblock */
707 fprintf(stderr
, Name
": Can only update one aspect"
708 " of superblock, both %s and %s given.\n",
712 if (mode
== MISC
&& !subarray
) {
713 fprintf(stderr
, Name
": Only subarrays can be"
714 " updated in misc mode\n");
718 if (strcmp(update
, "sparc2.2")==0)
720 if (strcmp(update
, "super-minor") == 0)
722 if (strcmp(update
, "summaries")==0)
724 if (strcmp(update
, "resync")==0)
726 if (strcmp(update
, "uuid")==0)
728 if (strcmp(update
, "name")==0)
730 if (strcmp(update
, "homehost")==0)
732 if (strcmp(update
, "devicesize")==0)
734 if (strcmp(update
, "no-bitmap")==0)
736 if (strcmp(update
, "byteorder")==0) {
739 Name
": must not set metadata"
740 " type with --update=byteorder.\n");
743 for(i
=0; !ss
&& superlist
[i
]; i
++)
744 ss
= superlist
[i
]->match_metadata_desc(
747 fprintf(stderr
, Name
": INTERNAL ERROR"
748 " cannot find 0.swap\n");
754 if (strcmp(update
,"?") == 0 ||
755 strcmp(update
, "help") == 0) {
757 fprintf(outf
, Name
": ");
761 Name
": '--update=%s' is invalid. ",
764 fprintf(outf
, "Valid --update options are:\n"
765 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
766 " 'summaries', 'homehost', 'byteorder', 'devicesize',\n"
768 exit(outf
== stdout
? 0 : 2);
771 /* update=devicesize is allowed with --re-add */
772 if (devmode
!= 'a' || re_add
!= 1) {
773 fprintf(stderr
, Name
"--update in Manage mode only"
774 " allowed with --re-add.\n");
778 fprintf(stderr
, Name
": Can only update one aspect"
779 " of superblock, both %s and %s given.\n",
784 if (strcmp(update
, "devicesize") != 0) {
785 fprintf(stderr
, Name
": only 'devicesize' can be"
786 " updated with --re-add\n");
791 case O(INCREMENTAL
,NoDegraded
):
792 fprintf(stderr
, Name
": --no-degraded is deprecated in Incremental mode\n");
793 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
794 runstop
= -1; /* --stop isn't allowed for --assemble,
795 * so we overload slightly */
798 case O(ASSEMBLE
,'c'):
799 case O(ASSEMBLE
,ConfigFile
):
800 case O(INCREMENTAL
, 'c'):
801 case O(INCREMENTAL
, ConfigFile
):
803 case O(MISC
, ConfigFile
):
805 case O(MONITOR
,ConfigFile
):
807 fprintf(stderr
, Name
": configfile cannot be set twice. "
808 "Second value is %s.\n", optarg
);
812 set_conffile(configfile
);
813 /* FIXME possibly check that config file exists. Even parse it */
815 case O(ASSEMBLE
,'s'): /* scan */
818 case O(INCREMENTAL
,'s'):
822 case O(MONITOR
,'m'): /* mail address */
823 case O(MONITOR
,EMail
):
825 fprintf(stderr
, Name
": only specify one mailaddress. %s ignored.\n",
831 case O(MONITOR
,'p'): /* alert program */
832 case O(MONITOR
,ProgramOpt
): /* alert program */
834 fprintf(stderr
, Name
": only specify one alter program. %s ignored.\n",
840 case O(MONITOR
,'r'): /* rebuild increments */
841 case O(MONITOR
,Increment
):
842 increments
= atoi(optarg
);
843 if (increments
> 99 || increments
< 1) {
844 fprintf(stderr
, Name
": please specify positive integer between 1 and 99 as rebuild increments.\n");
849 case O(MONITOR
,'d'): /* delay in seconds */
851 case O(BUILD
,'d'): /* delay for bitmap updates */
854 fprintf(stderr
, Name
": only specify delay once. %s ignored.\n",
857 delay
= strtol(optarg
, &c
, 10);
858 if (!optarg
[0] || *c
|| delay
<1) {
859 fprintf(stderr
, Name
": invalid delay: %s\n",
865 case O(MONITOR
,'f'): /* daemonise */
866 case O(MONITOR
,Fork
):
869 case O(MONITOR
,'i'): /* pid */
871 fprintf(stderr
, Name
": only specify one pid file. %s ignored.\n",
876 case O(MONITOR
,'1'): /* oneshot */
880 case O(MONITOR
,'t'): /* test */
883 case O(MONITOR
,'y'): /* log messages to syslog */
884 openlog("mdadm", LOG_PID
, SYSLOG_FACILITY
);
887 case O(MONITOR
, NoSharing
):
891 /* now the general management options. Some are applicable
892 * to other modes. None have arguments.
897 case O(MANAGE
,Add
): /* add a drive */
901 case O(MANAGE
,ReAdd
):
905 case O(MANAGE
,'r'): /* remove a drive */
906 case O(MANAGE
,Remove
):
909 case O(MANAGE
,'f'): /* set faulty */
911 case O(INCREMENTAL
,'f'):
912 case O(INCREMENTAL
,Remove
):
913 case O(INCREMENTAL
,Fail
): /* r for incremental is taken, use f
914 * even though we will both fail and
915 * remove the device */
918 case O(INCREMENTAL
,'R'):
920 case O(ASSEMBLE
,'R'):
922 case O(CREATE
,'R'): /* Run the array */
924 fprintf(stderr
, Name
": Cannot both Stop and Run an array\n");
931 fprintf(stderr
, Name
": Cannot both Run and Stop an array\n");
943 case O(MISC
,KillOpt
):
950 case O(MISC
, WaitOpt
):
951 case O(MISC
, Waitclean
):
952 case O(MISC
, DetailPlatform
):
953 case O(MISC
, KillSubarray
):
954 case O(MISC
, UpdateSubarray
):
955 if (opt
== KillSubarray
|| opt
== UpdateSubarray
) {
957 fprintf(stderr
, Name
": subarray can only"
958 " be specified once\n");
963 if (devmode
&& devmode
!= opt
&&
964 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
965 fprintf(stderr
, Name
": --examine/-E cannot be given with ");
966 if (devmode
== 'E') {
967 if (option_index
>= 0)
968 fprintf(stderr
, "--%s\n",
969 long_options
[option_index
].name
);
971 fprintf(stderr
, "-%c\n", opt
);
972 } else if (isalpha(devmode
))
973 fprintf(stderr
, "-%c\n", devmode
);
975 fprintf(stderr
, "previous option\n");
980 case O(MISC
, UdevRules
):
981 if (devmode
&& devmode
!= opt
) {
982 fprintf(stderr
, Name
": --udev-rules must"
983 " be the only option.\n");
986 fprintf(stderr
, Name
": only specify one udev "
987 "rule filename. %s ignored.\n",
990 udev_filename
= optarg
;
998 case O(MISC
, Sparc22
):
999 if (devmode
!= 'E') {
1000 fprintf(stderr
, Name
": --sparc2.2 only allowed with --examine\n");
1006 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
1007 case O(ASSEMBLE
,Bitmap
):
1009 fprintf(stderr
, Name
": bitmap file needed with -b in --assemble mode\n");
1012 if (strcmp(optarg
, "internal")==0) {
1013 fprintf(stderr
, Name
": there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
1016 bitmap_fd
= open(optarg
, O_RDWR
);
1017 if (!*optarg
|| bitmap_fd
< 0) {
1018 fprintf(stderr
, Name
": cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
1021 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
1024 case O(ASSEMBLE
, BackupFile
):
1025 case O(GROW
, BackupFile
):
1026 /* Specify a file into which grow might place a backup,
1027 * or from which assemble might recover a backup
1030 fprintf(stderr
, Name
": backup file already specified, rejecting %s\n", optarg
);
1033 backup_file
= optarg
;
1036 case O(GROW
, Continue
):
1037 /* Continue interrupted grow
1041 case O(ASSEMBLE
, InvalidBackup
):
1042 /* Acknowledge that the backupfile is invalid, but ask
1043 * to continue anyway
1049 case O(BUILD
,Bitmap
):
1051 case O(CREATE
,Bitmap
): /* here we create the bitmap */
1052 if (strcmp(optarg
, "none") == 0) {
1053 fprintf(stderr
, Name
": '--bitmap none' only"
1054 " supported for --grow\n");
1059 case O(GROW
,Bitmap
):
1060 if (strcmp(optarg
, "internal")== 0 ||
1061 strcmp(optarg
, "none")== 0 ||
1062 strchr(optarg
, '/') != NULL
) {
1063 bitmap_file
= optarg
;
1067 fprintf(stderr
, Name
": bitmap file must contain a '/', or be 'internal', or 'none'\n"
1068 " not '%s'\n", optarg
);
1071 case O(GROW
,BitmapChunk
):
1072 case O(BUILD
,BitmapChunk
):
1073 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
1074 bitmap_chunk
= parse_size(optarg
);
1075 if (bitmap_chunk
<= 0 ||
1076 bitmap_chunk
& (bitmap_chunk
- 1)) {
1078 Name
": invalid bitmap chunksize: %s\n",
1082 bitmap_chunk
= bitmap_chunk
* 512;
1085 case O(GROW
, WriteBehind
):
1086 case O(BUILD
, WriteBehind
):
1087 case O(CREATE
, WriteBehind
): /* write-behind mode */
1088 write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
1090 write_behind
= strtol(optarg
, &c
, 10);
1091 if (write_behind
< 0 || *c
||
1092 write_behind
> 16383) {
1093 fprintf(stderr
, Name
": Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
1099 case O(INCREMENTAL
, 'r'):
1100 case O(INCREMENTAL
, RebuildMapOpt
):
1103 case O(INCREMENTAL
, IncrementalPath
):
1104 remove_path
= optarg
;
1107 /* We have now processed all the valid options. Anything else is
1110 if (option_index
> 0)
1111 fprintf(stderr
, Name
":option --%s not valid in %s mode\n",
1112 long_options
[option_index
].name
,
1113 map_num(modes
, mode
));
1115 fprintf(stderr
, Name
": option -%c not valid in %s mode\n",
1116 opt
, map_num(modes
, mode
));
1122 char *help_text
= Help
;
1123 if (print_help
== 2)
1124 help_text
= OptionHelp
;
1127 case ASSEMBLE
: help_text
= Help_assemble
; break;
1128 case BUILD
: help_text
= Help_build
; break;
1129 case CREATE
: help_text
= Help_create
; break;
1130 case MANAGE
: help_text
= Help_manage
; break;
1131 case MISC
: help_text
= Help_misc
; break;
1132 case MONITOR
: help_text
= Help_monitor
; break;
1133 case GROW
: help_text
= Help_grow
; break;
1134 case INCREMENTAL
:help_text
= Help_incr
; break;
1136 fputs(help_text
,stdout
);
1140 if (!mode
&& devs_found
) {
1143 if (devlist
->disposition
== 0)
1144 devlist
->disposition
= devmode
;
1147 fputs(Usage
, stderr
);
1152 struct createinfo
*ci
= conf_get_create_info();
1154 if (strcasecmp(symlinks
, "yes") == 0)
1156 else if (strcasecmp(symlinks
, "no") == 0)
1159 fprintf(stderr
, Name
": option --symlinks must be 'no' or 'yes'\n");
1163 /* Ok, got the option parsing out of the way
1164 * hopefully it's mostly right but there might be some stuff
1167 * That is mosty checked in the per-mode stuff but...
1169 * For @,B,C and A without -s, the first device listed must be
1170 * an md device. We check that here and open it.
1173 if (mode
== MANAGE
|| mode
== BUILD
|| mode
== CREATE
1175 || (mode
== ASSEMBLE
&& ! scan
)) {
1176 if (devs_found
< 1) {
1177 fprintf(stderr
, Name
": an md device must be given in this mode\n");
1180 if ((int)ident
.super_minor
== -2 && autof
) {
1181 fprintf(stderr
, Name
": --super-minor=dev is incompatible with --auto\n");
1184 if (mode
== MANAGE
|| mode
== GROW
) {
1185 mdfd
= open_mddev(devlist
->devname
, 1);
1189 /* non-existent device is OK */
1190 mdfd
= open_mddev(devlist
->devname
, 0);
1192 fprintf(stderr
, Name
": device %s exists but is not an "
1193 "md array.\n", devlist
->devname
);
1196 if ((int)ident
.super_minor
== -2) {
1199 fprintf(stderr
, Name
": --super-minor=dev given, and "
1200 "listed device %s doesn't exist.\n",
1205 ident
.super_minor
= minor(stb
.st_rdev
);
1207 if (mdfd
>= 0 && mode
!= MANAGE
&& mode
!= GROW
) {
1208 /* We don't really want this open yet, we just might
1209 * have wanted to check some things
1217 if (raiddisks
== 1 && !force
&& level
!= LEVEL_FAULTY
) {
1218 fprintf(stderr
, Name
": '1' is an unusual number of drives for an array, so it is probably\n"
1219 " a mistake. If you really mean it you will need to specify --force before\n"
1220 " setting the number of drives.\n");
1225 if (homehost
== NULL
)
1226 homehost
= conf_get_homehost(&require_homehost
);
1227 if (homehost
== NULL
|| strcasecmp(homehost
, "<system>")==0) {
1228 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
1229 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
1230 homehost
= sys_hostname
;
1233 if (homehost
&& (!homehost
[0] || strcasecmp(homehost
, "<none>") == 0)) {
1235 require_homehost
= 0;
1238 if ((mode
== MISC
&& devmode
== 'E')
1239 || (mode
== MONITOR
&& spare_sharing
== 0))
1240 /* Anyone may try this */;
1241 else if (geteuid() != 0) {
1242 fprintf(stderr
, Name
": must be super-user to perform this action\n");
1246 ident
.autof
= autof
;
1251 /* readonly, add/remove, readwrite, runstop */
1253 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1254 if (!rv
&& devs_found
>1)
1255 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
1256 devlist
->next
, verbose
-quiet
, test
,
1258 if (!rv
&& readonly
< 0)
1259 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1261 rv
= Manage_runstop(devlist
->devname
, mdfd
, runstop
, quiet
);
1264 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1265 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !scan
) {
1266 /* Only a device has been given, so get details from config file */
1267 struct mddev_ident
*array_ident
= conf_get_ident(devlist
->devname
);
1268 if (array_ident
== NULL
) {
1269 fprintf(stderr
, Name
": %s not identified in config file.\n",
1275 if (array_ident
->autof
== 0)
1276 array_ident
->autof
= autof
;
1277 rv
|= Assemble(ss
, devlist
->devname
, array_ident
,
1278 NULL
, backup_file
, invalid_backup
,
1279 readonly
, runstop
, update
,
1280 homehost
, require_homehost
,
1281 verbose
-quiet
, force
,
1285 rv
= Assemble(ss
, devlist
->devname
, &ident
,
1286 devlist
->next
, backup_file
, invalid_backup
,
1287 readonly
, runstop
, update
,
1288 homehost
, require_homehost
,
1289 verbose
-quiet
, force
,
1291 else if (devs_found
> 0) {
1292 if (update
&& devs_found
> 1) {
1293 fprintf(stderr
, Name
": can only update a single array at a time\n");
1296 if (backup_file
&& devs_found
> 1) {
1297 fprintf(stderr
, Name
": can only assemble a single array when providing a backup file.\n");
1300 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1301 struct mddev_ident
*array_ident
= conf_get_ident(dv
->devname
);
1302 if (array_ident
== NULL
) {
1303 fprintf(stderr
, Name
": %s not identified in config file.\n",
1308 if (array_ident
->autof
== 0)
1309 array_ident
->autof
= autof
;
1310 rv
|= Assemble(ss
, dv
->devname
, array_ident
,
1311 NULL
, backup_file
, invalid_backup
,
1312 readonly
, runstop
, update
,
1313 homehost
, require_homehost
,
1314 verbose
-quiet
, force
,
1318 struct mddev_ident
*a
, *array_list
= conf_get_ident(NULL
);
1319 struct mddev_dev
*devlist
= conf_get_devs();
1320 struct map_ent
*map
= NULL
;
1322 int failures
, successes
;
1324 if (conf_verify_devnames(array_list
)) {
1325 fprintf(stderr
, Name
1326 ": Duplicate MD device names in "
1327 "conf file were found.\n");
1330 if (devlist
== NULL
) {
1331 fprintf(stderr
, Name
": No devices listed in conf file were found.\n");
1335 fprintf(stderr
, Name
": --update not meaningful with a --scan assembly.\n");
1339 fprintf(stderr
, Name
": --backup_file not meaningful with a --scan assembly.\n");
1342 for (a
= array_list
; a
; a
= a
->next
) {
1348 fprintf(stderr
, Name
" %s: failed to get "
1349 "exclusive lock on mapfile\n",
1355 for (a
= array_list
; a
; a
= a
->next
) {
1360 strcasecmp(a
->devname
, "<ignore>") == 0)
1363 r
= Assemble(ss
, a
->devname
,
1366 readonly
, runstop
, NULL
,
1367 homehost
, require_homehost
,
1368 verbose
-quiet
, force
,
1378 } while (failures
&& successes
);
1379 if (homehost
&& cnt
== 0) {
1380 /* Maybe we can auto-assemble something.
1381 * Repeatedly call Assemble in auto-assemble mode
1386 ident
.autof
= autof
;
1388 struct mddev_dev
*devlist
= conf_get_devs();
1391 rv2
= Assemble(ss
, NULL
,
1406 /* Incase there are stacked devices, we need to go around again */
1408 if (cnt
== 0 && rv
== 0) {
1409 fprintf(stderr
, Name
": No arrays found in config file or automatically\n");
1413 } else if (cnt
== 0 && rv
== 0) {
1414 fprintf(stderr
, Name
": No arrays found in config file\n");
1421 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1422 if (write_behind
&& !bitmap_file
) {
1423 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1427 if (raiddisks
== 0) {
1428 fprintf(stderr
, Name
": no raid-devices specified.\n");
1434 if (strcmp(bitmap_file
, "internal")==0) {
1435 fprintf(stderr
, Name
": 'internal' bitmaps not supported with --build\n");
1440 rv
= Build(devlist
->devname
, chunk
, level
, layout
,
1441 raiddisks
, devlist
->next
, assume_clean
,
1442 bitmap_file
, bitmap_chunk
, write_behind
,
1443 delay
, verbose
-quiet
, autof
, size
);
1446 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1447 if (write_behind
&& !bitmap_file
) {
1448 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1452 if (raiddisks
== 0) {
1453 fprintf(stderr
, Name
": no raid-devices specified.\n");
1458 rv
= Create(ss
, devlist
->devname
, chunk
, level
, layout
, size
<0 ? 0 : size
,
1459 raiddisks
, sparedisks
, ident
.name
, homehost
,
1460 ident
.uuid_set
? ident
.uuid
: NULL
,
1461 devs_found
-1, devlist
->next
, runstop
, verbose
-quiet
, force
, assume_clean
,
1462 bitmap_file
, bitmap_chunk
, write_behind
, delay
, autof
);
1465 if (devmode
== 'E') {
1466 if (devlist
== NULL
&& !scan
) {
1467 fprintf(stderr
, Name
": No devices to examine\n");
1470 if (devlist
== NULL
)
1471 devlist
= conf_get_devs();
1472 if (devlist
== NULL
) {
1473 fprintf(stderr
, Name
": No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1476 if (brief
&& verbose
)
1478 rv
= Examine(devlist
, scan
?(verbose
>1?0:verbose
+1):brief
,
1480 SparcAdjust
, ss
, homehost
);
1481 } else if (devmode
== DetailPlatform
) {
1482 rv
= Detail_Platform(ss
? ss
->ss
: NULL
, ss
? scan
: 1, verbose
);
1484 if (devlist
== NULL
) {
1485 if ((devmode
=='D' || devmode
== Waitclean
) && scan
) {
1486 /* apply --detail or --wait-clean to
1487 * all devices in /proc/mdstat
1489 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1490 struct mdstat_ent
*e
;
1491 struct map_ent
*map
= NULL
;
1493 int v
= verbose
>1?0:verbose
+1;
1495 for (members
= 0; members
<= 1; members
++) {
1496 for (e
=ms
; e
; e
=e
->next
) {
1499 int member
= e
->metadata_version
&&
1500 strncmp(e
->metadata_version
,
1501 "external:/", 10) == 0;
1502 if (members
!= member
)
1504 me
= map_by_devnum(&map
, e
->devnum
);
1506 && strcmp(me
->path
, "/unknown") != 0)
1509 name
= get_md_name(e
->devnum
);
1512 fprintf(stderr
, Name
": cannot find device file for %s\n",
1517 rv
|= Detail(name
, v
,
1521 rv
|= WaitClean(name
, -1, v
);
1526 } else if (devmode
== 'S' && scan
) {
1527 /* apply --stop to all devices in /proc/mdstat */
1528 /* Due to possible stacking of devices, repeat until
1529 * nothing more can be stopped
1531 int progress
=1, err
;
1534 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1535 struct mdstat_ent
*e
;
1537 if (!progress
) last
= 1;
1538 progress
= 0; err
= 0;
1539 for (e
=ms
; e
; e
=e
->next
) {
1540 char *name
= get_md_name(e
->devnum
);
1543 fprintf(stderr
, Name
": cannot find device file for %s\n",
1547 mdfd
= open_mddev(name
, 1);
1549 if (Manage_runstop(name
, mdfd
, -1, quiet
?1:last
?0:-1))
1559 } while (!last
&& err
);
1561 } else if (devmode
== UdevRules
) {
1562 rv
= Write_rules(udev_filename
);
1564 fprintf(stderr
, Name
": No devices given.\n");
1568 for (dv
=devlist
; dv
; dv
=dv
->next
) {
1569 switch(dv
->disposition
) {
1571 rv
|= Detail(dv
->devname
,
1573 export
, test
, homehost
, prefer
);
1575 case KillOpt
: /* Zero superblock */
1577 rv
|= Kill(dv
->devname
, ss
, force
, quiet
,0);
1581 rv
|= Kill(dv
->devname
, NULL
, force
, q
, 0);
1588 rv
|= Query(dv
->devname
); continue;
1590 rv
|= ExamineBitmap(dv
->devname
, brief
, ss
); continue;
1593 rv
|= Wait(dv
->devname
); continue;
1595 rv
|= WaitClean(dv
->devname
, -1, verbose
-quiet
); continue;
1597 rv
|= Kill_subarray(dv
->devname
, subarray
, quiet
);
1599 case UpdateSubarray
:
1600 if (update
== NULL
) {
1602 Name
": -U/--update must be specified with --update-subarray\n");
1606 rv
|= Update_subarray(dv
->devname
, subarray
, update
, &ident
, quiet
);
1609 mdfd
= open_mddev(dv
->devname
, 1);
1611 switch(dv
->disposition
) {
1613 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1, quiet
); break;
1615 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1, quiet
); break;
1617 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1619 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;
1628 if (!devlist
&& !scan
) {
1629 fprintf(stderr
, Name
": Cannot monitor: need --scan or at least one device\n");
1633 if (pidfile
&& !daemonise
) {
1634 fprintf(stderr
, Name
": Cannot write a pid file when not in daemon mode\n");
1639 if (get_linux_version() > 2006016)
1640 /* mdstat responds to poll */
1645 rv
= Monitor(devlist
, mailaddr
, program
,
1646 delay
?delay
:60, daemonise
, scan
, oneshot
,
1647 dosyslog
, test
, pidfile
, increments
,
1648 spare_sharing
, prefer
);
1652 if (array_size
>= 0) {
1653 /* alway impose array size first, independent of
1655 * Do not allow level or raid_disks changes at the
1656 * same time as that can be irreversibly destructive.
1660 if (raiddisks
|| level
!= UnSet
) {
1661 fprintf(stderr
, Name
": cannot change array size in same operation "
1662 "as changing raiddisks or level.\n"
1663 " Change size first, then check that data is still intact.\n");
1667 sysfs_init(&sra
, mdfd
, 0);
1668 if (array_size
== 0)
1669 err
= sysfs_set_str(&sra
, NULL
, "array_size", "default");
1671 err
= sysfs_set_num(&sra
, NULL
, "array_size", array_size
/ 2);
1674 fprintf(stderr
, Name
": --array-size setting"
1675 " is too large.\n");
1677 fprintf(stderr
, Name
": current kernel does"
1678 " not support setting --array-size\n");
1683 if (devs_found
> 1 && raiddisks
== 0) {
1685 if (size
>= 0 || chunk
|| layout_str
!= NULL
|| bitmap_file
) {
1686 fprintf(stderr
, Name
": --add cannot be used with "
1687 "other geometry changes in --grow mode\n");
1691 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1692 rv
= Grow_Add_device(devlist
->devname
, mdfd
,
1697 } else if (bitmap_file
) {
1698 if (size
>= 0 || raiddisks
|| chunk
||
1699 layout_str
!= NULL
|| devs_found
> 1) {
1700 fprintf(stderr
, Name
": --bitmap changes cannot be "
1701 "used with other geometry changes "
1702 "in --grow mode\n");
1707 delay
= DEFAULT_BITMAP_DELAY
;
1708 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, bitmap_file
,
1709 bitmap_chunk
, delay
, write_behind
, force
);
1710 } else if (grow_continue
)
1711 rv
= Grow_continue_command(devlist
->devname
,
1714 else if (size
>= 0 || raiddisks
!= 0 || layout_str
!= NULL
1715 || chunk
!= 0 || level
!= UnSet
) {
1716 rv
= Grow_reshape(devlist
->devname
, mdfd
, quiet
, backup_file
,
1717 size
, level
, layout_str
, chunk
, raiddisks
,
1719 assume_clean
, force
);
1720 } else if (array_size
< 0)
1721 fprintf(stderr
, Name
": no changes to --grow\n");
1729 fprintf(stderr
, Name
1730 ": --incremental --scan meaningless without --run.\n");
1733 if (devmode
== 'f') {
1734 fprintf(stderr
, Name
1735 ": --incremental --scan --fail not supported.\n");
1738 rv
= IncrementalScan(verbose
);
1741 if (!rebuild_map
&& !scan
) {
1742 fprintf(stderr
, Name
1743 ": --incremental requires a device.\n");
1748 if (devlist
->next
) {
1749 fprintf(stderr
, Name
1750 ": --incremental can only handle one device.\n");
1755 rv
= IncrementalRemove(devlist
->devname
, remove_path
,
1758 rv
= Incremental(devlist
->devname
, verbose
-quiet
,
1759 runstop
, ss
, homehost
,
1760 require_homehost
, autof
,