2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2009 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 int main(int argc
, char *argv
[])
44 long long array_size
= -1;
47 char *layout_str
= NULL
;
50 struct mddev_ident ident
;
51 char *configfile
= NULL
;
60 char *bitmap_file
= NULL
;
61 char *backup_file
= NULL
;
62 int invalid_backup
= 0;
63 int bitmap_chunk
= UnSet
;
65 struct mddev_dev
*devlist
= NULL
;
66 struct mddev_dev
**devlistend
= & devlist
;
76 char *symlinks
= NULL
;
77 /* autof indicates whether and how to create device node.
78 * bottom 3 bits are style. Rest (when shifted) are number of parts
80 * 1 - don't create (no)
81 * 2 - if is_standard, then create (yes)
82 * 3 - create as 'md' - reject is_standard mdp (md)
83 * 4 - create as 'mdp' - reject is_standard md (mdp)
84 * 5 - default to md if not is_standard (md in config file)
85 * 6 - default to mdp if not is_standard (part, or mdp in config file)
89 char *homehost
= NULL
;
90 char sys_hostname
[256];
91 int require_homehost
= 1;
92 char *mailaddr
= NULL
;
99 int spare_sharing
= 1;
100 struct supertype
*ss
= NULL
;
103 char *shortopt
= short_options
;
106 int auto_update_home
= 0;
107 char *subarray
= NULL
;
108 char *remove_path
= NULL
;
109 char *udev_filename
= NULL
;
116 srandom(time(0) ^ getpid());
120 ident
.raid_disks
= UnSet
;
121 ident
.super_minor
= UnSet
;
123 ident
.spare_group
= NULL
;
126 ident
.bitmap_fd
= -1;
127 ident
.bitmap_file
= NULL
;
129 ident
.container
= NULL
;
132 while ((option_index
= -1) ,
133 (opt
=getopt_long(argc
, argv
,
134 shortopt
, long_options
,
135 &option_index
)) != -1) {
137 /* firstly, some mode-independent options */
147 fputs(Version
, stderr
);
157 if (mode
== ASSEMBLE
|| mode
== BUILD
|| mode
== CREATE
158 || mode
== GROW
|| mode
== INCREMENTAL
160 break; /* b means bitmap */
169 if (strcasecmp(optarg
, "<ignore>") == 0)
170 require_homehost
= 0;
177 fputs(Usage
, stderr
);
180 /* second, figure out the mode.
181 * Some options force the mode. Others
182 * set the mode if it isn't already
188 shortopt
= short_bitmap_options
;
196 case ReAdd
: /* re-add */
199 shortopt
= short_bitmap_options
;
203 case 'A': newmode
= ASSEMBLE
; shortopt
= short_bitmap_auto_options
; break;
204 case 'B': newmode
= BUILD
; shortopt
= short_bitmap_auto_options
; break;
205 case 'C': newmode
= CREATE
; shortopt
= short_bitmap_auto_options
; break;
206 case 'F': newmode
= MONITOR
;break;
207 case 'G': newmode
= GROW
;
208 shortopt
= short_bitmap_options
;
210 case 'I': newmode
= INCREMENTAL
;
211 shortopt
= short_bitmap_auto_options
; break;
213 newmode
= AUTODETECT
; break;
219 case 'Q': newmode
= MISC
; break;
230 if (opt
== KillSubarray
|| opt
== UpdateSubarray
) {
232 fprintf(stderr
, Name
": subarray can only"
233 " be specified once\n");
239 case 'K': if (!mode
) newmode
= MISC
; break;
240 case NoSharing
: newmode
= MONITOR
; break;
242 if (mode
&& newmode
== mode
) {
243 /* everybody happy ! */
244 } else if (mode
&& newmode
!= mode
) {
246 fprintf(stderr
, Name
": ");
247 if (option_index
>= 0)
248 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
250 fprintf(stderr
, "-%c", opt
);
251 fprintf(stderr
, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
252 map_num(modes
, newmode
),
253 map_num(modes
, mode
));
255 } else if (!mode
&& newmode
) {
258 /* special case of -c --help */
259 if ((opt
== 'c' || opt
== ConfigFile
) &&
260 ( strncmp(optarg
, "--h", 3)==0 ||
261 strncmp(optarg
, "-h", 2)==0)) {
262 fputs(Help_config
, stdout
);
266 /* If first option is a device, don't force the mode yet */
268 if (devs_found
== 0) {
269 dv
= malloc(sizeof(*dv
));
271 fprintf(stderr
, Name
": malloc failed\n");
274 dv
->devname
= optarg
;
275 dv
->disposition
= devmode
;
276 dv
->writemostly
= writemostly
;
281 devlistend
= &dv
->next
;
286 /* No mode yet, and this is the second device ... */
287 fprintf(stderr
, Name
": An option must be given to set the mode before a second device\n"
288 " (%s) is listed\n", optarg
);
291 if (option_index
>= 0)
292 fprintf(stderr
, Name
": --%s", long_options
[option_index
].name
);
294 fprintf(stderr
, Name
": -%c", opt
);
295 fprintf(stderr
, " does not set the mode, and so cannot be the first option.\n");
299 /* if we just set the mode, then done */
313 /* an undecorated option - must be a device name.
315 if (devs_found
> 0 && mode
== MANAGE
&& !devmode
) {
316 fprintf(stderr
, Name
": Must give one of -a/-r/-f"
317 " for subsequent devices at %s\n", optarg
);
320 if (devs_found
> 0 && mode
== GROW
&& !devmode
) {
321 fprintf(stderr
, Name
": Must give -a/--add for"
322 " devices to add: %s\n", optarg
);
325 dv
= malloc(sizeof(*dv
));
327 fprintf(stderr
, Name
": malloc failed\n");
330 dv
->devname
= optarg
;
331 dv
->disposition
= devmode
;
332 dv
->writemostly
= writemostly
;
337 devlistend
= &dv
->next
;
343 /* We've got a mode, and opt is now something else which
344 * could depend on the mode */
345 #define O(a,b) ((a<<16)|b)
346 switch (O(mode
,opt
)) {
348 case O(GROW
,ChunkSize
):
350 case O(CREATE
,ChunkSize
):
351 case O(BUILD
,'c'): /* chunk or rounding */
352 case O(BUILD
,ChunkSize
): /* chunk or rounding */
354 fprintf(stderr
, Name
": chunk/rounding may only be specified once. "
355 "Second value is %s.\n", optarg
);
358 chunk
= parse_size(optarg
);
359 if (chunk
< 8 || ((chunk
-1)&chunk
)) {
360 fprintf(stderr
, Name
": invalid chunk/rounding value: %s\n",
364 /* Covert sectors to K */
369 case O(ASSEMBLE
,AutoHomeHost
):
370 auto_update_home
= 1;
373 case O(INCREMENTAL
, 'e'):
375 case O(ASSEMBLE
,'e'):
376 case O(MISC
,'e'): /* set metadata (superblock) information */
378 fprintf(stderr
, Name
": metadata information already given\n");
381 for(i
=0; !ss
&& superlist
[i
]; i
++)
382 ss
= superlist
[i
]->match_metadata_desc(optarg
);
385 fprintf(stderr
, Name
": unrecognised metadata identifier: %s\n", optarg
);
391 case O(MANAGE
,WriteMostly
):
393 case O(BUILD
,WriteMostly
):
395 case O(CREATE
,WriteMostly
):
396 /* set write-mostly for following devices */
401 /* clear write-mostly for following devices */
408 case O(BUILD
,'z'): /* size */
410 fprintf(stderr
, Name
": size may only be specified once. "
411 "Second value is %s.\n", optarg
);
414 if (strcmp(optarg
, "max")==0)
417 size
= parse_size(optarg
);
419 fprintf(stderr
, Name
": invalid size: %s\n",
423 /* convert sectors to K */
428 case O(GROW
,'Z'): /* array size */
429 if (array_size
>= 0) {
430 fprintf(stderr
, Name
": array-size may only be specified once. "
431 "Second value is %s.\n", optarg
);
434 if (strcmp(optarg
, "max") == 0)
437 array_size
= parse_size(optarg
);
438 if (array_size
<= 0) {
439 fprintf(stderr
, Name
": invalid array size: %s\n",
448 case O(BUILD
,'l'): /* set raid level*/
449 if (level
!= UnSet
) {
450 fprintf(stderr
, Name
": raid level may only be set once. "
451 "Second value is %s.\n", optarg
);
454 level
= map_name(pers
, optarg
);
455 if (level
== UnSet
) {
456 fprintf(stderr
, Name
": invalid raid level: %s\n",
460 if (level
!= 0 && level
!= LEVEL_LINEAR
&& level
!= 1 &&
461 level
!= LEVEL_MULTIPATH
&& level
!= LEVEL_FAULTY
&&
464 fprintf(stderr
, Name
": Raid level %s not permitted with --build.\n",
468 if (sparedisks
> 0 && level
< 1 && level
>= -1) {
469 fprintf(stderr
, Name
": raid level %s is incompatible with spare-devices setting.\n",
476 case O(GROW
, 'p'): /* new layout */
477 case O(GROW
, Layout
):
479 fprintf(stderr
,Name
": layout may only be sent once. "
480 "Second value was %s\n", optarg
);
484 /* 'Grow' will parse the value */
487 case O(CREATE
,'p'): /* raid5 layout */
488 case O(CREATE
,Layout
):
489 case O(BUILD
,'p'): /* faulty layout */
490 case O(BUILD
,Layout
):
491 if (layout
!= UnSet
) {
492 fprintf(stderr
,Name
": layout may only be sent once. "
493 "Second value was %s\n", optarg
);
498 fprintf(stderr
, Name
": layout not meaningful for %s arrays.\n",
499 map_num(pers
, level
));
502 fprintf(stderr
, Name
": raid level must be given before layout.\n");
506 layout
= map_name(r5layout
, optarg
);
508 fprintf(stderr
, Name
": layout %s not understood for raid5.\n",
514 layout
= map_name(r6layout
, optarg
);
516 fprintf(stderr
, Name
": layout %s not understood for raid6.\n",
523 layout
= parse_layout_10(optarg
);
525 fprintf(stderr
, Name
": layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg
);
533 layout
= parse_layout_faulty(optarg
);
535 fprintf(stderr
, Name
": layout %s not understood for faulty.\n",
543 case O(CREATE
,AssumeClean
):
544 case O(BUILD
,AssumeClean
): /* assume clean */
550 case O(BUILD
,'n'): /* number of raid disks */
552 fprintf(stderr
, Name
": raid-devices set twice: %d and %s\n",
556 raiddisks
= strtol(optarg
, &c
, 10);
557 if (!optarg
[0] || *c
|| raiddisks
<=0) {
558 fprintf(stderr
, Name
": invalid number of raid devices: %s\n",
562 ident
.raid_disks
= raiddisks
;
565 case O(CREATE
,'x'): /* number of spare (eXtra) discs */
567 fprintf(stderr
,Name
": spare-devices set twice: %d and %s\n",
571 if (level
!= UnSet
&& level
<= 0 && level
>= -1) {
572 fprintf(stderr
, Name
": spare-devices setting is incompatible with raid level %d\n",
576 sparedisks
= strtol(optarg
, &c
, 10);
577 if (!optarg
[0] || *c
|| sparedisks
< 0) {
578 fprintf(stderr
, Name
": invalid number of spare-devices: %s\n",
588 case O(INCREMENTAL
,'a'):
589 case O(INCREMENTAL
,Auto
):
590 case O(ASSEMBLE
,'a'):
591 case O(ASSEMBLE
,Auto
): /* auto-creation of device node */
592 autof
= parse_auto(optarg
, "--auto flag", 0);
595 case O(CREATE
,Symlinks
):
596 case O(BUILD
,Symlinks
):
597 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
601 case O(BUILD
,'f'): /* force honouring '-n 1' */
602 case O(BUILD
,Force
): /* force honouring '-n 1' */
603 case O(GROW
,'f'): /* ditto */
604 case O(GROW
,Force
): /* ditto */
605 case O(CREATE
,'f'): /* force honouring of device list */
606 case O(CREATE
,Force
): /* force honouring of device list */
607 case O(ASSEMBLE
,'f'): /* force assembly */
608 case O(ASSEMBLE
,Force
): /* force assembly */
609 case O(MISC
,'f'): /* force zero */
610 case O(MISC
,Force
): /* force zero */
614 /* now for the Assemble options */
615 case O(CREATE
,'u'): /* uuid of array */
616 case O(ASSEMBLE
,'u'): /* uuid of array */
617 if (ident
.uuid_set
) {
618 fprintf(stderr
, Name
": uuid cannot be set twice. "
619 "Second value %s.\n", optarg
);
622 if (parse_uuid(optarg
, ident
.uuid
))
625 fprintf(stderr
,Name
": Bad uuid: %s\n", optarg
);
631 case O(ASSEMBLE
,'N'):
634 fprintf(stderr
, Name
": name cannot be set twice. "
635 "Second value %s.\n", optarg
);
638 if (mode
== MISC
&& !subarray
) {
639 fprintf(stderr
, Name
": -N/--name only valid with --update-subarray in misc mode\n");
642 if (strlen(optarg
) > 32) {
643 fprintf(stderr
, Name
": name '%s' is too long, 32 chars max.\n",
647 strcpy(ident
.name
, optarg
);
650 case O(ASSEMBLE
,'m'): /* super-minor for array */
651 case O(ASSEMBLE
,SuperMinor
):
652 if (ident
.super_minor
!= UnSet
) {
653 fprintf(stderr
, Name
": super-minor cannot be set twice. "
654 "Second value: %s.\n", optarg
);
657 if (strcmp(optarg
, "dev")==0)
658 ident
.super_minor
= -2;
660 ident
.super_minor
= strtoul(optarg
, &cp
, 10);
661 if (!optarg
[0] || *cp
) {
662 fprintf(stderr
, Name
": Bad super-minor number: %s.\n", optarg
);
668 case O(ASSEMBLE
,'U'): /* update the superblock */
671 fprintf(stderr
, Name
": Can only update one aspect"
672 " of superblock, both %s and %s given.\n",
676 if (mode
== MISC
&& !subarray
) {
677 fprintf(stderr
, Name
": Only subarrays can be"
678 " updated in misc mode\n");
682 if (strcmp(update
, "sparc2.2")==0)
684 if (strcmp(update
, "super-minor") == 0)
686 if (strcmp(update
, "summaries")==0)
688 if (strcmp(update
, "resync")==0)
690 if (strcmp(update
, "uuid")==0)
692 if (strcmp(update
, "name")==0)
694 if (strcmp(update
, "homehost")==0)
696 if (strcmp(update
, "devicesize")==0)
698 if (strcmp(update
, "no-bitmap")==0)
700 if (strcmp(update
, "byteorder")==0) {
703 Name
": must not set metadata"
704 " type with --update=byteorder.\n");
707 for(i
=0; !ss
&& superlist
[i
]; i
++)
708 ss
= superlist
[i
]->match_metadata_desc(
711 fprintf(stderr
, Name
": INTERNAL ERROR"
712 " cannot find 0.swap\n");
718 if (strcmp(update
,"?") == 0 ||
719 strcmp(update
, "help") == 0) {
721 fprintf(outf
, Name
": ");
725 Name
": '--update=%s' is invalid. ",
728 fprintf(outf
, "Valid --update options are:\n"
729 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
730 " 'summaries', 'homehost', 'byteorder', 'devicesize',\n"
732 exit(outf
== stdout
? 0 : 2);
735 /* update=devicesize is allowed with --re-add */
736 if (devmode
!= 'a' || re_add
!= 1) {
737 fprintf(stderr
, Name
"--update in Manage mode only"
738 " allowed with --re-add.\n");
742 fprintf(stderr
, Name
": Can only update one aspect"
743 " of superblock, both %s and %s given.\n",
748 if (strcmp(update
, "devicesize") != 0) {
749 fprintf(stderr
, Name
": only 'devicesize' can be"
750 " updated with --re-add\n");
755 case O(INCREMENTAL
,NoDegraded
):
756 fprintf(stderr
, Name
": --no-degraded is deprecated in Incremental mode\n");
757 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
758 runstop
= -1; /* --stop isn't allowed for --assemble,
759 * so we overload slightly */
762 case O(ASSEMBLE
,'c'):
763 case O(ASSEMBLE
,ConfigFile
):
764 case O(INCREMENTAL
, 'c'):
765 case O(INCREMENTAL
, ConfigFile
):
767 case O(MISC
, ConfigFile
):
769 case O(MONITOR
,ConfigFile
):
771 fprintf(stderr
, Name
": configfile cannot be set twice. "
772 "Second value is %s.\n", optarg
);
776 set_conffile(configfile
);
777 /* FIXME possibly check that config file exists. Even parse it */
779 case O(ASSEMBLE
,'s'): /* scan */
782 case O(INCREMENTAL
,'s'):
786 case O(MONITOR
,'m'): /* mail address */
787 case O(MONITOR
,EMail
):
789 fprintf(stderr
, Name
": only specify one mailaddress. %s ignored.\n",
795 case O(MONITOR
,'p'): /* alert program */
796 case O(MONITOR
,ProgramOpt
): /* alert program */
798 fprintf(stderr
, Name
": only specify one alter program. %s ignored.\n",
804 case O(MONITOR
,'r'): /* rebuild increments */
805 case O(MONITOR
,Increment
):
806 increments
= atoi(optarg
);
807 if (increments
>99 || increments
<1) {
808 fprintf(stderr
, Name
": please specify positive integer between 1 and 99 as rebuild increments.\n");
813 case O(MONITOR
,'d'): /* delay in seconds */
815 case O(BUILD
,'d'): /* delay for bitmap updates */
818 fprintf(stderr
, Name
": only specify delay once. %s ignored.\n",
821 delay
= strtol(optarg
, &c
, 10);
822 if (!optarg
[0] || *c
|| delay
<1) {
823 fprintf(stderr
, Name
": invalid delay: %s\n",
829 case O(MONITOR
,'f'): /* daemonise */
830 case O(MONITOR
,Fork
):
833 case O(MONITOR
,'i'): /* pid */
835 fprintf(stderr
, Name
": only specify one pid file. %s ignored.\n",
840 case O(MONITOR
,'1'): /* oneshot */
843 case O(MONITOR
,'t'): /* test */
846 case O(MONITOR
,'y'): /* log messages to syslog */
847 openlog("mdadm", LOG_PID
, SYSLOG_FACILITY
);
850 case O(MONITOR
, NoSharing
):
853 /* now the general management options. Some are applicable
854 * to other modes. None have arguments.
859 case O(MANAGE
,Add
): /* add a drive */
863 case O(MANAGE
,ReAdd
):
867 case O(MANAGE
,'r'): /* remove a drive */
868 case O(MANAGE
,Remove
):
871 case O(MANAGE
,'f'): /* set faulty */
873 case O(INCREMENTAL
,'f'):
874 case O(INCREMENTAL
,Remove
):
875 case O(INCREMENTAL
,Fail
): /* r for incremental is taken, use f
876 * even though we will both fail and
877 * remove the device */
880 case O(INCREMENTAL
,'R'):
882 case O(ASSEMBLE
,'R'):
884 case O(CREATE
,'R'): /* Run the array */
886 fprintf(stderr
, Name
": Cannot both Stop and Run an array\n");
893 fprintf(stderr
, Name
": Cannot both Run and Stop an array\n");
912 case O(MISC
, WaitOpt
):
913 case O(MISC
, Waitclean
):
914 case O(MISC
, DetailPlatform
):
915 case O(MISC
, KillSubarray
):
916 case O(MISC
, UpdateSubarray
):
917 if (devmode
&& devmode
!= opt
&&
918 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
919 fprintf(stderr
, Name
": --examine/-E cannot be given with ");
920 if (devmode
== 'E') {
921 if (option_index
>= 0)
922 fprintf(stderr
, "--%s\n",
923 long_options
[option_index
].name
);
925 fprintf(stderr
, "-%c\n", opt
);
926 } else if (isalpha(devmode
))
927 fprintf(stderr
, "-%c\n", devmode
);
929 fprintf(stderr
, "previous option\n");
934 case O(MISC
, UdevRules
):
935 if (devmode
&& devmode
!= opt
) {
936 fprintf(stderr
, Name
": --udev-rules must"
937 " be the only option.\n");
940 fprintf(stderr
, Name
": only specify one udev "
941 "rule filename. %s ignored.\n",
944 udev_filename
= optarg
;
952 case O(MISC
, Sparc22
):
953 if (devmode
!= 'E') {
954 fprintf(stderr
, Name
": --sparc2.2 only allowed with --examine\n");
960 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
961 case O(ASSEMBLE
,Bitmap
):
963 fprintf(stderr
, Name
": bitmap file needed with -b in --assemble mode\n");
966 if (strcmp(optarg
, "internal")==0) {
967 fprintf(stderr
, Name
": there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
970 bitmap_fd
= open(optarg
, O_RDWR
);
971 if (!*optarg
|| bitmap_fd
< 0) {
972 fprintf(stderr
, Name
": cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
975 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
978 case O(ASSEMBLE
, BackupFile
):
979 case O(GROW
, BackupFile
):
980 /* Specify a file into which grow might place a backup,
981 * or from which assemble might recover a backup
984 fprintf(stderr
, Name
": backup file already specified, rejecting %s\n", optarg
);
987 backup_file
= optarg
;
990 case O(ASSEMBLE
, InvalidBackup
):
991 /* Acknowledge that the backupfile is invalid, but ask
998 case O(BUILD
,Bitmap
):
1000 case O(CREATE
,Bitmap
): /* here we create the bitmap */
1001 if (strcmp(optarg
, "none") == 0) {
1002 fprintf(stderr
, Name
": '--bitmap none' only"
1003 " support for --grow\n");
1008 case O(GROW
,Bitmap
):
1009 if (strcmp(optarg
, "internal")== 0 ||
1010 strcmp(optarg
, "none")== 0 ||
1011 strchr(optarg
, '/') != NULL
) {
1012 bitmap_file
= optarg
;
1016 fprintf(stderr
, Name
": bitmap file must contain a '/', or be 'internal', or 'none'\n"
1017 " not '%s'\n", optarg
);
1020 case O(GROW
,BitmapChunk
):
1021 case O(BUILD
,BitmapChunk
):
1022 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
1023 bitmap_chunk
= parse_size(optarg
);
1024 if (bitmap_chunk
< 0 ||
1025 bitmap_chunk
& (bitmap_chunk
- 1)) {
1027 Name
": invalid bitmap chunksize: %s\n",
1031 /* convert sectors to B, chunk of 0 means 512B */
1032 bitmap_chunk
= bitmap_chunk
? bitmap_chunk
* 512 : 512;
1035 case O(GROW
, WriteBehind
):
1036 case O(BUILD
, WriteBehind
):
1037 case O(CREATE
, WriteBehind
): /* write-behind mode */
1038 write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
1040 write_behind
= strtol(optarg
, &c
, 10);
1041 if (write_behind
< 0 || *c
||
1042 write_behind
> 16383) {
1043 fprintf(stderr
, Name
": Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
1049 case O(INCREMENTAL
, 'r'):
1050 case O(INCREMENTAL
, RebuildMapOpt
):
1053 case O(INCREMENTAL
, IncrementalPath
):
1054 remove_path
= optarg
;
1057 /* We have now processed all the valid options. Anything else is
1060 if (option_index
> 0)
1061 fprintf(stderr
, Name
":option --%s not valid in %s mode\n",
1062 long_options
[option_index
].name
,
1063 map_num(modes
, mode
));
1065 fprintf(stderr
, Name
": option -%c not valid in %s mode\n",
1066 opt
, map_num(modes
, mode
));
1072 char *help_text
= Help
;
1073 if (print_help
== 2)
1074 help_text
= OptionHelp
;
1077 case ASSEMBLE
: help_text
= Help_assemble
; break;
1078 case BUILD
: help_text
= Help_build
; break;
1079 case CREATE
: help_text
= Help_create
; break;
1080 case MANAGE
: help_text
= Help_manage
; break;
1081 case MISC
: help_text
= Help_misc
; break;
1082 case MONITOR
: help_text
= Help_monitor
; break;
1083 case GROW
: help_text
= Help_grow
; break;
1084 case INCREMENTAL
:help_text
= Help_incr
; break;
1086 fputs(help_text
,stdout
);
1090 if (!mode
&& devs_found
) {
1093 if (devlist
->disposition
== 0)
1094 devlist
->disposition
= devmode
;
1097 fputs(Usage
, stderr
);
1102 struct createinfo
*ci
= conf_get_create_info();
1104 if (strcasecmp(symlinks
, "yes") == 0)
1106 else if (strcasecmp(symlinks
, "no") == 0)
1109 fprintf(stderr
, Name
": option --symlinks must be 'no' or 'yes'\n");
1113 /* Ok, got the option parsing out of the way
1114 * hopefully it's mostly right but there might be some stuff
1117 * That is mosty checked in the per-mode stuff but...
1119 * For @,B,C and A without -s, the first device listed must be an md device
1120 * we check that here and open it.
1123 if (mode
==MANAGE
|| mode
== BUILD
|| mode
== CREATE
|| mode
== GROW
||
1124 (mode
== ASSEMBLE
&& ! scan
)) {
1125 if (devs_found
< 1) {
1126 fprintf(stderr
, Name
": an md device must be given in this mode\n");
1129 if ((int)ident
.super_minor
== -2 && autof
) {
1130 fprintf(stderr
, Name
": --super-minor=dev is incompatible with --auto\n");
1133 if (mode
== MANAGE
|| mode
== GROW
) {
1134 mdfd
= open_mddev(devlist
->devname
, 1);
1138 /* non-existent device is OK */
1139 mdfd
= open_mddev(devlist
->devname
, 0);
1141 fprintf(stderr
, Name
": device %s exists but is not an "
1142 "md array.\n", devlist
->devname
);
1145 if ((int)ident
.super_minor
== -2) {
1148 fprintf(stderr
, Name
": --super-minor=dev given, and "
1149 "listed device %s doesn't exist.\n",
1154 ident
.super_minor
= minor(stb
.st_rdev
);
1156 if (mdfd
>= 0 && mode
!= MANAGE
&& mode
!= GROW
) {
1157 /* We don't really want this open yet, we just might
1158 * have wanted to check some things
1166 if (raiddisks
== 1 && !force
&& level
!= -5) {
1167 fprintf(stderr
, Name
": '1' is an unusual number of drives for an array, so it is probably\n"
1168 " a mistake. If you really mean it you will need to specify --force before\n"
1169 " setting the number of drives.\n");
1174 if (homehost
== NULL
)
1175 homehost
= conf_get_homehost(&require_homehost
);
1176 if (homehost
== NULL
|| strcmp(homehost
, "<system>")==0) {
1177 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
1178 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
1179 homehost
= sys_hostname
;
1183 if ((mode
!= MISC
|| devmode
!= 'E') &&
1185 fprintf(stderr
, Name
": must be super-user to perform this action\n");
1189 ident
.autof
= autof
;
1194 /* readonly, add/remove, readwrite, runstop */
1196 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1197 if (!rv
&& devs_found
>1)
1198 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
1199 devlist
->next
, verbose
-quiet
, test
,
1201 if (!rv
&& readonly
< 0)
1202 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1204 rv
= Manage_runstop(devlist
->devname
, mdfd
, runstop
, quiet
);
1207 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1208 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !scan
) {
1209 /* Only a device has been given, so get details from config file */
1210 struct mddev_ident
*array_ident
= conf_get_ident(devlist
->devname
);
1211 if (array_ident
== NULL
) {
1212 fprintf(stderr
, Name
": %s not identified in config file.\n",
1218 if (array_ident
->autof
== 0)
1219 array_ident
->autof
= autof
;
1220 rv
|= Assemble(ss
, devlist
->devname
, array_ident
,
1221 NULL
, backup_file
, invalid_backup
,
1222 readonly
, runstop
, update
,
1223 homehost
, require_homehost
,
1224 verbose
-quiet
, force
);
1227 rv
= Assemble(ss
, devlist
->devname
, &ident
,
1228 devlist
->next
, backup_file
, invalid_backup
,
1229 readonly
, runstop
, update
,
1230 homehost
, require_homehost
,
1231 verbose
-quiet
, force
);
1232 else if (devs_found
>0) {
1233 if (update
&& devs_found
> 1) {
1234 fprintf(stderr
, Name
": can only update a single array at a time\n");
1237 if (backup_file
&& devs_found
> 1) {
1238 fprintf(stderr
, Name
": can only assemble a single array when providing a backup file.\n");
1241 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1242 struct mddev_ident
*array_ident
= conf_get_ident(dv
->devname
);
1243 if (array_ident
== NULL
) {
1244 fprintf(stderr
, Name
": %s not identified in config file.\n",
1249 if (array_ident
->autof
== 0)
1250 array_ident
->autof
= autof
;
1251 rv
|= Assemble(ss
, dv
->devname
, array_ident
,
1252 NULL
, backup_file
, invalid_backup
,
1253 readonly
, runstop
, update
,
1254 homehost
, require_homehost
,
1255 verbose
-quiet
, force
);
1258 struct mddev_ident
*a
, *array_list
= conf_get_ident(NULL
);
1259 struct mddev_dev
*devlist
= conf_get_devs();
1261 int failures
, successes
;
1262 if (devlist
== NULL
) {
1263 fprintf(stderr
, Name
": No devices listed in conf file were found.\n");
1267 fprintf(stderr
, Name
": --update not meaningful with a --scan assembly.\n");
1271 fprintf(stderr
, Name
": --backup_file not meaningful with a --scan assembly.\n");
1274 for (a
= array_list
; a
; a
= a
->next
) {
1283 for (a
= array_list
; a
; a
= a
->next
) {
1288 strcasecmp(a
->devname
, "<ignore>") == 0)
1291 r
= Assemble(ss
, a
->devname
,
1294 readonly
, runstop
, NULL
,
1295 homehost
, require_homehost
,
1296 verbose
-quiet
, force
);
1305 } while (failures
&& successes
);
1306 if (homehost
&& cnt
== 0) {
1307 /* Maybe we can auto-assemble something.
1308 * Repeatedly call Assemble in auto-assemble mode
1313 ident
.autof
= autof
;
1315 struct mddev_dev
*devlist
= conf_get_devs();
1318 rv2
= Assemble(ss
, NULL
,
1321 readonly
, runstop
, NULL
,
1322 homehost
, require_homehost
,
1323 verbose
-quiet
, force
);
1329 /* found something so even though assembly failed we
1330 * want to avoid auto-updates
1332 auto_update_home
= 0;
1334 /* Incase there are stacked devices, we need to go around again */
1337 if (cnt
== 0 && auto_update_home
&& homehost
) {
1338 /* Nothing found, maybe we need to bootstrap homehost info */
1350 verbose
-quiet
, force
);
1356 /* Incase there are stacked devices, we need to go around again */
1360 if (cnt
== 0 && rv
== 0) {
1361 fprintf(stderr
, Name
": No arrays found in config file or automatically\n");
1365 } else if (cnt
== 0 && rv
== 0) {
1366 fprintf(stderr
, Name
": No arrays found in config file\n");
1372 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1373 if (write_behind
&& !bitmap_file
) {
1374 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1378 if (raiddisks
== 0) {
1379 fprintf(stderr
, Name
": no raid-devices specified.\n");
1385 if (strcmp(bitmap_file
, "internal")==0) {
1386 fprintf(stderr
, Name
": 'internal' bitmaps not supported with --build\n");
1391 rv
= Build(devlist
->devname
, chunk
, level
, layout
,
1392 raiddisks
, devlist
->next
, assume_clean
,
1393 bitmap_file
, bitmap_chunk
, write_behind
,
1394 delay
, verbose
-quiet
, autof
, size
);
1397 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1398 if (write_behind
&& !bitmap_file
) {
1399 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1403 if (raiddisks
== 0) {
1404 fprintf(stderr
, Name
": no raid-devices specified.\n");
1409 rv
= Create(ss
, devlist
->devname
, chunk
, level
, layout
, size
<0 ? 0 : size
,
1410 raiddisks
, sparedisks
, ident
.name
, homehost
,
1411 ident
.uuid_set
? ident
.uuid
: NULL
,
1412 devs_found
-1, devlist
->next
, runstop
, verbose
-quiet
, force
, assume_clean
,
1413 bitmap_file
, bitmap_chunk
, write_behind
, delay
, autof
);
1416 if (devmode
== 'E') {
1417 if (devlist
== NULL
&& !scan
) {
1418 fprintf(stderr
, Name
": No devices to examine\n");
1421 if (devlist
== NULL
)
1422 devlist
= conf_get_devs();
1423 if (devlist
== NULL
) {
1424 fprintf(stderr
, Name
": No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1427 if (brief
&& verbose
)
1429 rv
= Examine(devlist
, scan
?(verbose
>1?0:verbose
+1):brief
,
1431 SparcAdjust
, ss
, homehost
);
1432 } else if (devmode
== DetailPlatform
) {
1433 rv
= Detail_Platform(ss
? ss
->ss
: NULL
, ss
? scan
: 1, verbose
);
1435 if (devlist
== NULL
) {
1436 if ((devmode
=='D' || devmode
== Waitclean
) && scan
) {
1437 /* apply --detail or --wait-clean to
1438 * all devices in /proc/mdstat
1440 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1441 struct mdstat_ent
*e
;
1442 struct map_ent
*map
= NULL
;
1444 int v
= verbose
>1?0:verbose
+1;
1446 for (members
= 0; members
<= 1; members
++) {
1447 for (e
=ms
; e
; e
=e
->next
) {
1450 int member
= e
->metadata_version
&&
1451 strncmp(e
->metadata_version
,
1452 "external:/", 10) == 0;
1453 if (members
!= member
)
1455 me
= map_by_devnum(&map
, e
->devnum
);
1457 && strcmp(me
->path
, "/unknown") != 0)
1460 name
= get_md_name(e
->devnum
);
1463 fprintf(stderr
, Name
": cannot find device file for %s\n",
1468 rv
|= Detail(name
, v
,
1472 rv
|= WaitClean(name
, -1, v
);
1477 } else if (devmode
== 'S' && scan
) {
1478 /* apply --stop to all devices in /proc/mdstat */
1479 /* Due to possible stacking of devices, repeat until
1480 * nothing more can be stopped
1482 int progress
=1, err
;
1485 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1486 struct mdstat_ent
*e
;
1488 if (!progress
) last
= 1;
1489 progress
= 0; err
= 0;
1490 for (e
=ms
; e
; e
=e
->next
) {
1491 char *name
= get_md_name(e
->devnum
);
1494 fprintf(stderr
, Name
": cannot find device file for %s\n",
1498 mdfd
= open_mddev(name
, 1);
1500 if (Manage_runstop(name
, mdfd
, -1, quiet
?1:last
?0:-1))
1510 } while (!last
&& err
);
1512 } else if (devmode
== UdevRules
) {
1513 rv
= Write_rules(udev_filename
);
1515 fprintf(stderr
, Name
": No devices given.\n");
1519 for (dv
=devlist
; dv
; dv
=dv
->next
) {
1520 switch(dv
->disposition
) {
1522 rv
|= Detail(dv
->devname
,
1524 export
, test
, homehost
);
1526 case 'K': /* Zero superblock */
1528 rv
|= Kill(dv
->devname
, ss
, force
, quiet
,0);
1532 rv
|= Kill(dv
->devname
, NULL
, force
, q
, 0);
1539 rv
|= Query(dv
->devname
); continue;
1541 rv
|= ExamineBitmap(dv
->devname
, brief
, ss
); continue;
1544 rv
|= Wait(dv
->devname
); continue;
1546 rv
|= WaitClean(dv
->devname
, -1, verbose
-quiet
); continue;
1548 rv
|= Kill_subarray(dv
->devname
, subarray
, quiet
);
1550 case UpdateSubarray
:
1551 if (update
== NULL
) {
1553 Name
": -U/--update must be specified with --update-subarray\n");
1557 rv
|= Update_subarray(dv
->devname
, subarray
, update
, &ident
, quiet
);
1560 mdfd
= open_mddev(dv
->devname
, 1);
1562 switch(dv
->disposition
) {
1564 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1, quiet
); break;
1566 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1, quiet
); break;
1568 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1570 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;
1579 if (!devlist
&& !scan
) {
1580 fprintf(stderr
, Name
": Cannot monitor: need --scan or at least one device\n");
1584 if (pidfile
&& !daemonise
) {
1585 fprintf(stderr
, Name
": Cannot write a pid file when not in daemon mode\n");
1590 if (get_linux_version() > 2006016)
1591 /* mdstat responds to poll */
1596 rv
= Monitor(devlist
, mailaddr
, program
,
1597 delay
?delay
:60, daemonise
, scan
, oneshot
,
1598 dosyslog
, test
, pidfile
, increments
, spare_sharing
);
1602 if (array_size
>= 0) {
1603 /* alway impose array size first, independent of
1605 * Do not allow level or raid_disks changes at the
1606 * same time as that can be irreversibly destructive.
1610 if (raiddisks
|| level
!= UnSet
) {
1611 fprintf(stderr
, Name
": cannot change array size in same operation "
1612 "as changing raiddisks or level.\n"
1613 " Change size first, then check that data is still intact.\n");
1617 sysfs_init(&sra
, mdfd
, 0);
1618 if (array_size
== 0)
1619 err
= sysfs_set_str(&sra
, NULL
, "array_size", "default");
1621 err
= sysfs_set_num(&sra
, NULL
, "array_size", array_size
/ 2);
1624 fprintf(stderr
, Name
": --array-size setting"
1625 " is too large.\n");
1627 fprintf(stderr
, Name
": current kernel does"
1628 " not support setting --array-size\n");
1633 if (devs_found
> 1 && raiddisks
== 0) {
1635 if (size
>= 0 || chunk
|| layout_str
!= NULL
|| bitmap_file
) {
1636 fprintf(stderr
, Name
": --add cannot be used with "
1637 "other geometry changes in --grow mode\n");
1641 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1642 rv
= Grow_Add_device(devlist
->devname
, mdfd
,
1647 } else if (bitmap_file
) {
1648 if (size
>= 0 || raiddisks
|| chunk
||
1649 layout_str
!= NULL
|| devs_found
> 1) {
1650 fprintf(stderr
, Name
": --bitmap changes cannot be "
1651 "used with other geometry changes "
1652 "in --grow mode\n");
1657 delay
= DEFAULT_BITMAP_DELAY
;
1658 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, bitmap_file
,
1659 bitmap_chunk
, delay
, write_behind
, force
);
1660 } else if (size
>= 0 || raiddisks
!= 0 || layout_str
!= NULL
1661 || chunk
!= 0 || level
!= UnSet
) {
1662 rv
= Grow_reshape(devlist
->devname
, mdfd
, quiet
, backup_file
,
1663 size
, level
, layout_str
, chunk
, raiddisks
,
1666 } else if (array_size
< 0)
1667 fprintf(stderr
, Name
": no changes to --grow\n");
1675 fprintf(stderr
, Name
1676 ": --incremental --scan meaningless without --run.\n");
1679 if (devmode
== 'f') {
1680 fprintf(stderr
, Name
1681 ": --incremental --scan --fail not supported.\n");
1684 rv
= IncrementalScan(verbose
);
1687 if (!rebuild_map
&& !scan
) {
1688 fprintf(stderr
, Name
1689 ": --incremental requires a device.\n");
1694 if (devlist
->next
) {
1695 fprintf(stderr
, Name
1696 ": --incremental can only handle one device.\n");
1701 rv
= IncrementalRemove(devlist
->devname
, remove_path
,
1704 rv
= Incremental(devlist
->devname
, verbose
-quiet
,
1705 runstop
, ss
, homehost
,
1706 require_homehost
, autof
);