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 char *subarray
= NULL
;
107 char *remove_path
= NULL
;
108 char *udev_filename
= NULL
;
115 srandom(time(0) ^ getpid());
119 ident
.raid_disks
= UnSet
;
120 ident
.super_minor
= UnSet
;
122 ident
.spare_group
= NULL
;
125 ident
.bitmap_fd
= -1;
126 ident
.bitmap_file
= NULL
;
128 ident
.container
= NULL
;
131 while ((option_index
= -1) ,
132 (opt
=getopt_long(argc
, argv
,
133 shortopt
, long_options
,
134 &option_index
)) != -1) {
136 /* firstly, some mode-independent options */
146 fputs(Version
, stderr
);
156 if (mode
== ASSEMBLE
|| mode
== BUILD
|| mode
== CREATE
157 || mode
== GROW
|| mode
== INCREMENTAL
159 break; /* b means bitmap */
168 if (strcasecmp(optarg
, "<ignore>") == 0)
169 require_homehost
= 0;
176 fputs(Usage
, stderr
);
179 /* second, figure out the mode.
180 * Some options force the mode. Others
181 * set the mode if it isn't already
187 shortopt
= short_bitmap_options
;
195 case ReAdd
: /* re-add */
198 shortopt
= short_bitmap_options
;
202 case 'A': newmode
= ASSEMBLE
; shortopt
= short_bitmap_auto_options
; break;
203 case 'B': newmode
= BUILD
; shortopt
= short_bitmap_auto_options
; break;
204 case 'C': newmode
= CREATE
; shortopt
= short_bitmap_auto_options
; break;
205 case 'F': newmode
= MONITOR
;break;
206 case 'G': newmode
= GROW
;
207 shortopt
= short_bitmap_options
;
209 case 'I': newmode
= INCREMENTAL
;
210 shortopt
= short_bitmap_auto_options
; break;
212 newmode
= AUTODETECT
; break;
218 case 'Q': newmode
= MISC
; break;
229 if (opt
== KillSubarray
|| opt
== UpdateSubarray
) {
231 fprintf(stderr
, Name
": subarray can only"
232 " be specified once\n");
238 case 'K': if (!mode
) newmode
= MISC
; break;
239 case NoSharing
: newmode
= MONITOR
; break;
241 if (mode
&& newmode
== mode
) {
242 /* everybody happy ! */
243 } else if (mode
&& newmode
!= mode
) {
245 fprintf(stderr
, Name
": ");
246 if (option_index
>= 0)
247 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
249 fprintf(stderr
, "-%c", opt
);
250 fprintf(stderr
, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
251 map_num(modes
, newmode
),
252 map_num(modes
, mode
));
254 } else if (!mode
&& newmode
) {
256 if (mode
== MISC
&& devs_found
) {
257 fprintf(stderr
, Name
": No action given for %s in --misc mode\n",
259 fprintf(stderr
," Action options must come before device names\n");
263 /* special case of -c --help */
264 if ((opt
== 'c' || opt
== ConfigFile
) &&
265 ( strncmp(optarg
, "--h", 3)==0 ||
266 strncmp(optarg
, "-h", 2)==0)) {
267 fputs(Help_config
, stdout
);
271 /* If first option is a device, don't force the mode yet */
273 if (devs_found
== 0) {
274 dv
= malloc(sizeof(*dv
));
276 fprintf(stderr
, Name
": malloc failed\n");
279 dv
->devname
= optarg
;
280 dv
->disposition
= devmode
;
281 dv
->writemostly
= writemostly
;
286 devlistend
= &dv
->next
;
291 /* No mode yet, and this is the second device ... */
292 fprintf(stderr
, Name
": An option must be given to set the mode before a second device\n"
293 " (%s) is listed\n", optarg
);
296 if (option_index
>= 0)
297 fprintf(stderr
, Name
": --%s", long_options
[option_index
].name
);
299 fprintf(stderr
, Name
": -%c", opt
);
300 fprintf(stderr
, " does not set the mode, and so cannot be the first option.\n");
304 /* if we just set the mode, then done */
318 /* an undecorated option - must be a device name.
320 if (devs_found
> 0 && mode
== MANAGE
&& !devmode
) {
321 fprintf(stderr
, Name
": Must give one of -a/-r/-f"
322 " for subsequent devices at %s\n", optarg
);
325 if (devs_found
> 0 && mode
== GROW
&& !devmode
) {
326 fprintf(stderr
, Name
": Must give -a/--add for"
327 " devices to add: %s\n", optarg
);
330 dv
= malloc(sizeof(*dv
));
332 fprintf(stderr
, Name
": malloc failed\n");
335 dv
->devname
= optarg
;
336 dv
->disposition
= devmode
;
337 dv
->writemostly
= writemostly
;
342 devlistend
= &dv
->next
;
348 /* We've got a mode, and opt is now something else which
349 * could depend on the mode */
350 #define O(a,b) ((a<<16)|b)
351 switch (O(mode
,opt
)) {
353 case O(GROW
,ChunkSize
):
355 case O(CREATE
,ChunkSize
):
356 case O(BUILD
,'c'): /* chunk or rounding */
357 case O(BUILD
,ChunkSize
): /* chunk or rounding */
359 fprintf(stderr
, Name
": chunk/rounding may only be specified once. "
360 "Second value is %s.\n", optarg
);
363 chunk
= parse_size(optarg
);
364 if (chunk
< 8 || (chunk
&1)) {
365 fprintf(stderr
, Name
": invalid chunk/rounding value: %s\n",
369 /* Convert sectors to K */
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 */
545 case O(GROW
,AssumeClean
):
551 case O(BUILD
,'n'): /* number of raid disks */
553 fprintf(stderr
, Name
": raid-devices set twice: %d and %s\n",
557 raiddisks
= strtol(optarg
, &c
, 10);
558 if (!optarg
[0] || *c
|| raiddisks
<=0) {
559 fprintf(stderr
, Name
": invalid number of raid devices: %s\n",
563 ident
.raid_disks
= raiddisks
;
566 case O(CREATE
,'x'): /* number of spare (eXtra) discs */
568 fprintf(stderr
,Name
": spare-devices set twice: %d and %s\n",
572 if (level
!= UnSet
&& level
<= 0 && level
>= -1) {
573 fprintf(stderr
, Name
": spare-devices setting is incompatible with raid level %d\n",
577 sparedisks
= strtol(optarg
, &c
, 10);
578 if (!optarg
[0] || *c
|| sparedisks
< 0) {
579 fprintf(stderr
, Name
": invalid number of spare-devices: %s\n",
589 case O(INCREMENTAL
,'a'):
590 case O(INCREMENTAL
,Auto
):
591 case O(ASSEMBLE
,'a'):
592 case O(ASSEMBLE
,Auto
): /* auto-creation of device node */
593 autof
= parse_auto(optarg
, "--auto flag", 0);
596 case O(CREATE
,Symlinks
):
597 case O(BUILD
,Symlinks
):
598 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
602 case O(BUILD
,'f'): /* force honouring '-n 1' */
603 case O(BUILD
,Force
): /* force honouring '-n 1' */
604 case O(GROW
,'f'): /* ditto */
605 case O(GROW
,Force
): /* ditto */
606 case O(CREATE
,'f'): /* force honouring of device list */
607 case O(CREATE
,Force
): /* force honouring of device list */
608 case O(ASSEMBLE
,'f'): /* force assembly */
609 case O(ASSEMBLE
,Force
): /* force assembly */
610 case O(MISC
,'f'): /* force zero */
611 case O(MISC
,Force
): /* force zero */
615 /* now for the Assemble options */
616 case O(CREATE
,'u'): /* uuid of array */
617 case O(ASSEMBLE
,'u'): /* uuid of array */
618 if (ident
.uuid_set
) {
619 fprintf(stderr
, Name
": uuid cannot be set twice. "
620 "Second value %s.\n", optarg
);
623 if (parse_uuid(optarg
, ident
.uuid
))
626 fprintf(stderr
,Name
": Bad uuid: %s\n", optarg
);
632 case O(ASSEMBLE
,'N'):
635 fprintf(stderr
, Name
": name cannot be set twice. "
636 "Second value %s.\n", optarg
);
639 if (mode
== MISC
&& !subarray
) {
640 fprintf(stderr
, Name
": -N/--name only valid with --update-subarray in misc mode\n");
643 if (strlen(optarg
) > 32) {
644 fprintf(stderr
, Name
": name '%s' is too long, 32 chars max.\n",
648 strcpy(ident
.name
, optarg
);
651 case O(ASSEMBLE
,'m'): /* super-minor for array */
652 case O(ASSEMBLE
,SuperMinor
):
653 if (ident
.super_minor
!= UnSet
) {
654 fprintf(stderr
, Name
": super-minor cannot be set twice. "
655 "Second value: %s.\n", optarg
);
658 if (strcmp(optarg
, "dev")==0)
659 ident
.super_minor
= -2;
661 ident
.super_minor
= strtoul(optarg
, &cp
, 10);
662 if (!optarg
[0] || *cp
) {
663 fprintf(stderr
, Name
": Bad super-minor number: %s.\n", optarg
);
669 case O(ASSEMBLE
,'U'): /* update the superblock */
672 fprintf(stderr
, Name
": Can only update one aspect"
673 " of superblock, both %s and %s given.\n",
677 if (mode
== MISC
&& !subarray
) {
678 fprintf(stderr
, Name
": Only subarrays can be"
679 " updated in misc mode\n");
683 if (strcmp(update
, "sparc2.2")==0)
685 if (strcmp(update
, "super-minor") == 0)
687 if (strcmp(update
, "summaries")==0)
689 if (strcmp(update
, "resync")==0)
691 if (strcmp(update
, "uuid")==0)
693 if (strcmp(update
, "name")==0)
695 if (strcmp(update
, "homehost")==0)
697 if (strcmp(update
, "devicesize")==0)
699 if (strcmp(update
, "no-bitmap")==0)
701 if (strcmp(update
, "byteorder")==0) {
704 Name
": must not set metadata"
705 " type with --update=byteorder.\n");
708 for(i
=0; !ss
&& superlist
[i
]; i
++)
709 ss
= superlist
[i
]->match_metadata_desc(
712 fprintf(stderr
, Name
": INTERNAL ERROR"
713 " cannot find 0.swap\n");
719 if (strcmp(update
,"?") == 0 ||
720 strcmp(update
, "help") == 0) {
722 fprintf(outf
, Name
": ");
726 Name
": '--update=%s' is invalid. ",
729 fprintf(outf
, "Valid --update options are:\n"
730 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
731 " 'summaries', 'homehost', 'byteorder', 'devicesize',\n"
733 exit(outf
== stdout
? 0 : 2);
736 /* update=devicesize is allowed with --re-add */
737 if (devmode
!= 'a' || re_add
!= 1) {
738 fprintf(stderr
, Name
"--update in Manage mode only"
739 " allowed with --re-add.\n");
743 fprintf(stderr
, Name
": Can only update one aspect"
744 " of superblock, both %s and %s given.\n",
749 if (strcmp(update
, "devicesize") != 0) {
750 fprintf(stderr
, Name
": only 'devicesize' can be"
751 " updated with --re-add\n");
756 case O(INCREMENTAL
,NoDegraded
):
757 fprintf(stderr
, Name
": --no-degraded is deprecated in Incremental mode\n");
758 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
759 runstop
= -1; /* --stop isn't allowed for --assemble,
760 * so we overload slightly */
763 case O(ASSEMBLE
,'c'):
764 case O(ASSEMBLE
,ConfigFile
):
765 case O(INCREMENTAL
, 'c'):
766 case O(INCREMENTAL
, ConfigFile
):
768 case O(MISC
, ConfigFile
):
770 case O(MONITOR
,ConfigFile
):
772 fprintf(stderr
, Name
": configfile cannot be set twice. "
773 "Second value is %s.\n", optarg
);
777 set_conffile(configfile
);
778 /* FIXME possibly check that config file exists. Even parse it */
780 case O(ASSEMBLE
,'s'): /* scan */
783 case O(INCREMENTAL
,'s'):
787 case O(MONITOR
,'m'): /* mail address */
788 case O(MONITOR
,EMail
):
790 fprintf(stderr
, Name
": only specify one mailaddress. %s ignored.\n",
796 case O(MONITOR
,'p'): /* alert program */
797 case O(MONITOR
,ProgramOpt
): /* alert program */
799 fprintf(stderr
, Name
": only specify one alter program. %s ignored.\n",
805 case O(MONITOR
,'r'): /* rebuild increments */
806 case O(MONITOR
,Increment
):
807 increments
= atoi(optarg
);
808 if (increments
>99 || increments
<1) {
809 fprintf(stderr
, Name
": please specify positive integer between 1 and 99 as rebuild increments.\n");
814 case O(MONITOR
,'d'): /* delay in seconds */
816 case O(BUILD
,'d'): /* delay for bitmap updates */
819 fprintf(stderr
, Name
": only specify delay once. %s ignored.\n",
822 delay
= strtol(optarg
, &c
, 10);
823 if (!optarg
[0] || *c
|| delay
<1) {
824 fprintf(stderr
, Name
": invalid delay: %s\n",
830 case O(MONITOR
,'f'): /* daemonise */
831 case O(MONITOR
,Fork
):
834 case O(MONITOR
,'i'): /* pid */
836 fprintf(stderr
, Name
": only specify one pid file. %s ignored.\n",
841 case O(MONITOR
,'1'): /* oneshot */
844 case O(MONITOR
,'t'): /* test */
847 case O(MONITOR
,'y'): /* log messages to syslog */
848 openlog("mdadm", LOG_PID
, SYSLOG_FACILITY
);
851 case O(MONITOR
, NoSharing
):
854 /* now the general management options. Some are applicable
855 * to other modes. None have arguments.
860 case O(MANAGE
,Add
): /* add a drive */
864 case O(MANAGE
,ReAdd
):
868 case O(MANAGE
,'r'): /* remove a drive */
869 case O(MANAGE
,Remove
):
872 case O(MANAGE
,'f'): /* set faulty */
874 case O(INCREMENTAL
,'f'):
875 case O(INCREMENTAL
,Remove
):
876 case O(INCREMENTAL
,Fail
): /* r for incremental is taken, use f
877 * even though we will both fail and
878 * remove the device */
881 case O(INCREMENTAL
,'R'):
883 case O(ASSEMBLE
,'R'):
885 case O(CREATE
,'R'): /* Run the array */
887 fprintf(stderr
, Name
": Cannot both Stop and Run an array\n");
894 fprintf(stderr
, Name
": Cannot both Run and Stop an array\n");
913 case O(MISC
, WaitOpt
):
914 case O(MISC
, Waitclean
):
915 case O(MISC
, DetailPlatform
):
916 case O(MISC
, KillSubarray
):
917 case O(MISC
, UpdateSubarray
):
918 if (devmode
&& devmode
!= opt
&&
919 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
920 fprintf(stderr
, Name
": --examine/-E cannot be given with ");
921 if (devmode
== 'E') {
922 if (option_index
>= 0)
923 fprintf(stderr
, "--%s\n",
924 long_options
[option_index
].name
);
926 fprintf(stderr
, "-%c\n", opt
);
927 } else if (isalpha(devmode
))
928 fprintf(stderr
, "-%c\n", devmode
);
930 fprintf(stderr
, "previous option\n");
935 case O(MISC
, UdevRules
):
936 if (devmode
&& devmode
!= opt
) {
937 fprintf(stderr
, Name
": --udev-rules must"
938 " be the only option.\n");
941 fprintf(stderr
, Name
": only specify one udev "
942 "rule filename. %s ignored.\n",
945 udev_filename
= optarg
;
953 case O(MISC
, Sparc22
):
954 if (devmode
!= 'E') {
955 fprintf(stderr
, Name
": --sparc2.2 only allowed with --examine\n");
961 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
962 case O(ASSEMBLE
,Bitmap
):
964 fprintf(stderr
, Name
": bitmap file needed with -b in --assemble mode\n");
967 if (strcmp(optarg
, "internal")==0) {
968 fprintf(stderr
, Name
": there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
971 bitmap_fd
= open(optarg
, O_RDWR
);
972 if (!*optarg
|| bitmap_fd
< 0) {
973 fprintf(stderr
, Name
": cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
976 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
979 case O(ASSEMBLE
, BackupFile
):
980 case O(GROW
, BackupFile
):
981 /* Specify a file into which grow might place a backup,
982 * or from which assemble might recover a backup
985 fprintf(stderr
, Name
": backup file already specified, rejecting %s\n", optarg
);
988 backup_file
= optarg
;
991 case O(ASSEMBLE
, InvalidBackup
):
992 /* Acknowledge that the backupfile is invalid, but ask
999 case O(BUILD
,Bitmap
):
1001 case O(CREATE
,Bitmap
): /* here we create the bitmap */
1002 if (strcmp(optarg
, "none") == 0) {
1003 fprintf(stderr
, Name
": '--bitmap none' only"
1004 " support for --grow\n");
1009 case O(GROW
,Bitmap
):
1010 if (strcmp(optarg
, "internal")== 0 ||
1011 strcmp(optarg
, "none")== 0 ||
1012 strchr(optarg
, '/') != NULL
) {
1013 bitmap_file
= optarg
;
1017 fprintf(stderr
, Name
": bitmap file must contain a '/', or be 'internal', or 'none'\n"
1018 " not '%s'\n", optarg
);
1021 case O(GROW
,BitmapChunk
):
1022 case O(BUILD
,BitmapChunk
):
1023 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
1024 bitmap_chunk
= parse_size(optarg
);
1025 if (bitmap_chunk
< 0 ||
1026 bitmap_chunk
& (bitmap_chunk
- 1)) {
1028 Name
": invalid bitmap chunksize: %s\n",
1032 /* convert sectors to B, chunk of 0 means 512B */
1033 bitmap_chunk
= bitmap_chunk
? bitmap_chunk
* 512 : 512;
1036 case O(GROW
, WriteBehind
):
1037 case O(BUILD
, WriteBehind
):
1038 case O(CREATE
, WriteBehind
): /* write-behind mode */
1039 write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
1041 write_behind
= strtol(optarg
, &c
, 10);
1042 if (write_behind
< 0 || *c
||
1043 write_behind
> 16383) {
1044 fprintf(stderr
, Name
": Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
1050 case O(INCREMENTAL
, 'r'):
1051 case O(INCREMENTAL
, RebuildMapOpt
):
1054 case O(INCREMENTAL
, IncrementalPath
):
1055 remove_path
= optarg
;
1058 /* We have now processed all the valid options. Anything else is
1061 if (option_index
> 0)
1062 fprintf(stderr
, Name
":option --%s not valid in %s mode\n",
1063 long_options
[option_index
].name
,
1064 map_num(modes
, mode
));
1066 fprintf(stderr
, Name
": option -%c not valid in %s mode\n",
1067 opt
, map_num(modes
, mode
));
1073 char *help_text
= Help
;
1074 if (print_help
== 2)
1075 help_text
= OptionHelp
;
1078 case ASSEMBLE
: help_text
= Help_assemble
; break;
1079 case BUILD
: help_text
= Help_build
; break;
1080 case CREATE
: help_text
= Help_create
; break;
1081 case MANAGE
: help_text
= Help_manage
; break;
1082 case MISC
: help_text
= Help_misc
; break;
1083 case MONITOR
: help_text
= Help_monitor
; break;
1084 case GROW
: help_text
= Help_grow
; break;
1085 case INCREMENTAL
:help_text
= Help_incr
; break;
1087 fputs(help_text
,stdout
);
1091 if (!mode
&& devs_found
) {
1094 if (devlist
->disposition
== 0)
1095 devlist
->disposition
= devmode
;
1098 fputs(Usage
, stderr
);
1103 struct createinfo
*ci
= conf_get_create_info();
1105 if (strcasecmp(symlinks
, "yes") == 0)
1107 else if (strcasecmp(symlinks
, "no") == 0)
1110 fprintf(stderr
, Name
": option --symlinks must be 'no' or 'yes'\n");
1114 /* Ok, got the option parsing out of the way
1115 * hopefully it's mostly right but there might be some stuff
1118 * That is mosty checked in the per-mode stuff but...
1120 * For @,B,C and A without -s, the first device listed must be an md device
1121 * we check that here and open it.
1124 if (mode
==MANAGE
|| mode
== BUILD
|| mode
== CREATE
|| mode
== GROW
||
1125 (mode
== ASSEMBLE
&& ! scan
)) {
1126 if (devs_found
< 1) {
1127 fprintf(stderr
, Name
": an md device must be given in this mode\n");
1130 if ((int)ident
.super_minor
== -2 && autof
) {
1131 fprintf(stderr
, Name
": --super-minor=dev is incompatible with --auto\n");
1134 if (mode
== MANAGE
|| mode
== GROW
) {
1135 mdfd
= open_mddev(devlist
->devname
, 1);
1139 /* non-existent device is OK */
1140 mdfd
= open_mddev(devlist
->devname
, 0);
1142 fprintf(stderr
, Name
": device %s exists but is not an "
1143 "md array.\n", devlist
->devname
);
1146 if ((int)ident
.super_minor
== -2) {
1149 fprintf(stderr
, Name
": --super-minor=dev given, and "
1150 "listed device %s doesn't exist.\n",
1155 ident
.super_minor
= minor(stb
.st_rdev
);
1157 if (mdfd
>= 0 && mode
!= MANAGE
&& mode
!= GROW
) {
1158 /* We don't really want this open yet, we just might
1159 * have wanted to check some things
1167 if (raiddisks
== 1 && !force
&& level
!= -5) {
1168 fprintf(stderr
, Name
": '1' is an unusual number of drives for an array, so it is probably\n"
1169 " a mistake. If you really mean it you will need to specify --force before\n"
1170 " setting the number of drives.\n");
1175 if (homehost
== NULL
)
1176 homehost
= conf_get_homehost(&require_homehost
);
1177 if (homehost
== NULL
|| strcasecmp(homehost
, "<system>")==0) {
1178 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
1179 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
1180 homehost
= sys_hostname
;
1183 if (homehost
&& (!homehost
[0] || strcasecmp(homehost
, "<none>") == 0)) {
1185 require_homehost
= 0;
1188 if ((mode
!= MISC
|| devmode
!= 'E') &&
1190 fprintf(stderr
, Name
": must be super-user to perform this action\n");
1194 ident
.autof
= autof
;
1199 /* readonly, add/remove, readwrite, runstop */
1201 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1202 if (!rv
&& devs_found
>1)
1203 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
1204 devlist
->next
, verbose
-quiet
, test
,
1206 if (!rv
&& readonly
< 0)
1207 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1209 rv
= Manage_runstop(devlist
->devname
, mdfd
, runstop
, quiet
);
1212 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1213 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !scan
) {
1214 /* Only a device has been given, so get details from config file */
1215 struct mddev_ident
*array_ident
= conf_get_ident(devlist
->devname
);
1216 if (array_ident
== NULL
) {
1217 fprintf(stderr
, Name
": %s not identified in config file.\n",
1223 if (array_ident
->autof
== 0)
1224 array_ident
->autof
= autof
;
1225 rv
|= Assemble(ss
, devlist
->devname
, array_ident
,
1226 NULL
, backup_file
, invalid_backup
,
1227 readonly
, runstop
, update
,
1228 homehost
, require_homehost
,
1229 verbose
-quiet
, force
);
1232 rv
= Assemble(ss
, devlist
->devname
, &ident
,
1233 devlist
->next
, backup_file
, invalid_backup
,
1234 readonly
, runstop
, update
,
1235 homehost
, require_homehost
,
1236 verbose
-quiet
, force
);
1237 else if (devs_found
>0) {
1238 if (update
&& devs_found
> 1) {
1239 fprintf(stderr
, Name
": can only update a single array at a time\n");
1242 if (backup_file
&& devs_found
> 1) {
1243 fprintf(stderr
, Name
": can only assemble a single array when providing a backup file.\n");
1246 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1247 struct mddev_ident
*array_ident
= conf_get_ident(dv
->devname
);
1248 if (array_ident
== NULL
) {
1249 fprintf(stderr
, Name
": %s not identified in config file.\n",
1254 if (array_ident
->autof
== 0)
1255 array_ident
->autof
= autof
;
1256 rv
|= Assemble(ss
, dv
->devname
, array_ident
,
1257 NULL
, backup_file
, invalid_backup
,
1258 readonly
, runstop
, update
,
1259 homehost
, require_homehost
,
1260 verbose
-quiet
, force
);
1263 struct mddev_ident
*a
, *array_list
= conf_get_ident(NULL
);
1264 struct mddev_dev
*devlist
= conf_get_devs();
1266 int failures
, successes
;
1267 if (devlist
== NULL
) {
1268 fprintf(stderr
, Name
": No devices listed in conf file were found.\n");
1272 fprintf(stderr
, Name
": --update not meaningful with a --scan assembly.\n");
1276 fprintf(stderr
, Name
": --backup_file not meaningful with a --scan assembly.\n");
1279 for (a
= array_list
; a
; a
= a
->next
) {
1288 for (a
= array_list
; a
; a
= a
->next
) {
1293 strcasecmp(a
->devname
, "<ignore>") == 0)
1296 r
= Assemble(ss
, a
->devname
,
1299 readonly
, runstop
, NULL
,
1300 homehost
, require_homehost
,
1301 verbose
-quiet
, force
);
1310 } while (failures
&& successes
);
1311 if (homehost
&& cnt
== 0) {
1312 /* Maybe we can auto-assemble something.
1313 * Repeatedly call Assemble in auto-assemble mode
1318 ident
.autof
= autof
;
1320 struct mddev_dev
*devlist
= conf_get_devs();
1323 rv2
= Assemble(ss
, NULL
,
1326 readonly
, runstop
, NULL
,
1327 homehost
, require_homehost
,
1328 verbose
-quiet
, force
);
1334 /* Incase there are stacked devices, we need to go around again */
1336 if (cnt
== 0 && rv
== 0) {
1337 fprintf(stderr
, Name
": No arrays found in config file or automatically\n");
1341 } else if (cnt
== 0 && rv
== 0) {
1342 fprintf(stderr
, Name
": No arrays found in config file\n");
1348 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1349 if (write_behind
&& !bitmap_file
) {
1350 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1354 if (raiddisks
== 0) {
1355 fprintf(stderr
, Name
": no raid-devices specified.\n");
1361 if (strcmp(bitmap_file
, "internal")==0) {
1362 fprintf(stderr
, Name
": 'internal' bitmaps not supported with --build\n");
1367 rv
= Build(devlist
->devname
, chunk
, level
, layout
,
1368 raiddisks
, devlist
->next
, assume_clean
,
1369 bitmap_file
, bitmap_chunk
, write_behind
,
1370 delay
, verbose
-quiet
, autof
, size
);
1373 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1374 if (write_behind
&& !bitmap_file
) {
1375 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1379 if (raiddisks
== 0) {
1380 fprintf(stderr
, Name
": no raid-devices specified.\n");
1385 rv
= Create(ss
, devlist
->devname
, chunk
, level
, layout
, size
<0 ? 0 : size
,
1386 raiddisks
, sparedisks
, ident
.name
, homehost
,
1387 ident
.uuid_set
? ident
.uuid
: NULL
,
1388 devs_found
-1, devlist
->next
, runstop
, verbose
-quiet
, force
, assume_clean
,
1389 bitmap_file
, bitmap_chunk
, write_behind
, delay
, autof
);
1392 if (devmode
== 'E') {
1393 if (devlist
== NULL
&& !scan
) {
1394 fprintf(stderr
, Name
": No devices to examine\n");
1397 if (devlist
== NULL
)
1398 devlist
= conf_get_devs();
1399 if (devlist
== NULL
) {
1400 fprintf(stderr
, Name
": No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1403 if (brief
&& verbose
)
1405 rv
= Examine(devlist
, scan
?(verbose
>1?0:verbose
+1):brief
,
1407 SparcAdjust
, ss
, homehost
);
1408 } else if (devmode
== DetailPlatform
) {
1409 rv
= Detail_Platform(ss
? ss
->ss
: NULL
, ss
? scan
: 1, verbose
);
1411 if (devlist
== NULL
) {
1412 if ((devmode
=='D' || devmode
== Waitclean
) && scan
) {
1413 /* apply --detail or --wait-clean to
1414 * all devices in /proc/mdstat
1416 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1417 struct mdstat_ent
*e
;
1418 struct map_ent
*map
= NULL
;
1420 int v
= verbose
>1?0:verbose
+1;
1422 for (members
= 0; members
<= 1; members
++) {
1423 for (e
=ms
; e
; e
=e
->next
) {
1426 int member
= e
->metadata_version
&&
1427 strncmp(e
->metadata_version
,
1428 "external:/", 10) == 0;
1429 if (members
!= member
)
1431 me
= map_by_devnum(&map
, e
->devnum
);
1433 && strcmp(me
->path
, "/unknown") != 0)
1436 name
= get_md_name(e
->devnum
);
1439 fprintf(stderr
, Name
": cannot find device file for %s\n",
1444 rv
|= Detail(name
, v
,
1448 rv
|= WaitClean(name
, -1, v
);
1453 } else if (devmode
== 'S' && scan
) {
1454 /* apply --stop to all devices in /proc/mdstat */
1455 /* Due to possible stacking of devices, repeat until
1456 * nothing more can be stopped
1458 int progress
=1, err
;
1461 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1462 struct mdstat_ent
*e
;
1464 if (!progress
) last
= 1;
1465 progress
= 0; err
= 0;
1466 for (e
=ms
; e
; e
=e
->next
) {
1467 char *name
= get_md_name(e
->devnum
);
1470 fprintf(stderr
, Name
": cannot find device file for %s\n",
1474 mdfd
= open_mddev(name
, 1);
1476 if (Manage_runstop(name
, mdfd
, -1, quiet
?1:last
?0:-1))
1486 } while (!last
&& err
);
1488 } else if (devmode
== UdevRules
) {
1489 rv
= Write_rules(udev_filename
);
1491 fprintf(stderr
, Name
": No devices given.\n");
1495 for (dv
=devlist
; dv
; dv
=dv
->next
) {
1496 switch(dv
->disposition
) {
1498 rv
|= Detail(dv
->devname
,
1500 export
, test
, homehost
);
1502 case 'K': /* Zero superblock */
1504 rv
|= Kill(dv
->devname
, ss
, force
, quiet
,0);
1508 rv
|= Kill(dv
->devname
, NULL
, force
, q
, 0);
1515 rv
|= Query(dv
->devname
); continue;
1517 rv
|= ExamineBitmap(dv
->devname
, brief
, ss
); continue;
1520 rv
|= Wait(dv
->devname
); continue;
1522 rv
|= WaitClean(dv
->devname
, -1, verbose
-quiet
); continue;
1524 rv
|= Kill_subarray(dv
->devname
, subarray
, quiet
);
1526 case UpdateSubarray
:
1527 if (update
== NULL
) {
1529 Name
": -U/--update must be specified with --update-subarray\n");
1533 rv
|= Update_subarray(dv
->devname
, subarray
, update
, &ident
, quiet
);
1536 mdfd
= open_mddev(dv
->devname
, 1);
1538 switch(dv
->disposition
) {
1540 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1, quiet
); break;
1542 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1, quiet
); break;
1544 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1546 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;
1555 if (!devlist
&& !scan
) {
1556 fprintf(stderr
, Name
": Cannot monitor: need --scan or at least one device\n");
1560 if (pidfile
&& !daemonise
) {
1561 fprintf(stderr
, Name
": Cannot write a pid file when not in daemon mode\n");
1566 if (get_linux_version() > 2006016)
1567 /* mdstat responds to poll */
1572 rv
= Monitor(devlist
, mailaddr
, program
,
1573 delay
?delay
:60, daemonise
, scan
, oneshot
,
1574 dosyslog
, test
, pidfile
, increments
, spare_sharing
);
1578 if (array_size
>= 0) {
1579 /* alway impose array size first, independent of
1581 * Do not allow level or raid_disks changes at the
1582 * same time as that can be irreversibly destructive.
1586 if (raiddisks
|| level
!= UnSet
) {
1587 fprintf(stderr
, Name
": cannot change array size in same operation "
1588 "as changing raiddisks or level.\n"
1589 " Change size first, then check that data is still intact.\n");
1593 sysfs_init(&sra
, mdfd
, 0);
1594 if (array_size
== 0)
1595 err
= sysfs_set_str(&sra
, NULL
, "array_size", "default");
1597 err
= sysfs_set_num(&sra
, NULL
, "array_size", array_size
/ 2);
1600 fprintf(stderr
, Name
": --array-size setting"
1601 " is too large.\n");
1603 fprintf(stderr
, Name
": current kernel does"
1604 " not support setting --array-size\n");
1609 if (devs_found
> 1 && raiddisks
== 0) {
1611 if (size
>= 0 || chunk
|| layout_str
!= NULL
|| bitmap_file
) {
1612 fprintf(stderr
, Name
": --add cannot be used with "
1613 "other geometry changes in --grow mode\n");
1617 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1618 rv
= Grow_Add_device(devlist
->devname
, mdfd
,
1623 } else if (bitmap_file
) {
1624 if (size
>= 0 || raiddisks
|| chunk
||
1625 layout_str
!= NULL
|| devs_found
> 1) {
1626 fprintf(stderr
, Name
": --bitmap changes cannot be "
1627 "used with other geometry changes "
1628 "in --grow mode\n");
1633 delay
= DEFAULT_BITMAP_DELAY
;
1634 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, bitmap_file
,
1635 bitmap_chunk
, delay
, write_behind
, force
);
1636 } else if (size
>= 0 || raiddisks
!= 0 || layout_str
!= NULL
1637 || chunk
!= 0 || level
!= UnSet
) {
1638 rv
= Grow_reshape(devlist
->devname
, mdfd
, quiet
, backup_file
,
1639 size
, level
, layout_str
, chunk
, raiddisks
,
1641 assume_clean
, force
);
1642 } else if (array_size
< 0)
1643 fprintf(stderr
, Name
": no changes to --grow\n");
1651 fprintf(stderr
, Name
1652 ": --incremental --scan meaningless without --run.\n");
1655 if (devmode
== 'f') {
1656 fprintf(stderr
, Name
1657 ": --incremental --scan --fail not supported.\n");
1660 rv
= IncrementalScan(verbose
);
1663 if (!rebuild_map
&& !scan
) {
1664 fprintf(stderr
, Name
1665 ": --incremental requires a device.\n");
1670 if (devlist
->next
) {
1671 fprintf(stderr
, Name
1672 ": --incremental can only handle one device.\n");
1677 rv
= IncrementalRemove(devlist
->devname
, remove_path
,
1680 rv
= Incremental(devlist
->devname
, verbose
-quiet
,
1681 runstop
, ss
, homehost
,
1682 require_homehost
, autof
);