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 */
612 case O(MANAGE
,Force
): /* add device which is too large */
616 /* now for the Assemble options */
617 case O(CREATE
,'u'): /* uuid of array */
618 case O(ASSEMBLE
,'u'): /* uuid of array */
619 if (ident
.uuid_set
) {
620 fprintf(stderr
, Name
": uuid cannot be set twice. "
621 "Second value %s.\n", optarg
);
624 if (parse_uuid(optarg
, ident
.uuid
))
627 fprintf(stderr
,Name
": Bad uuid: %s\n", optarg
);
633 case O(ASSEMBLE
,'N'):
636 fprintf(stderr
, Name
": name cannot be set twice. "
637 "Second value %s.\n", optarg
);
640 if (mode
== MISC
&& !subarray
) {
641 fprintf(stderr
, Name
": -N/--name only valid with --update-subarray in misc mode\n");
644 if (strlen(optarg
) > 32) {
645 fprintf(stderr
, Name
": name '%s' is too long, 32 chars max.\n",
649 strcpy(ident
.name
, optarg
);
652 case O(ASSEMBLE
,'m'): /* super-minor for array */
653 case O(ASSEMBLE
,SuperMinor
):
654 if (ident
.super_minor
!= UnSet
) {
655 fprintf(stderr
, Name
": super-minor cannot be set twice. "
656 "Second value: %s.\n", optarg
);
659 if (strcmp(optarg
, "dev")==0)
660 ident
.super_minor
= -2;
662 ident
.super_minor
= strtoul(optarg
, &cp
, 10);
663 if (!optarg
[0] || *cp
) {
664 fprintf(stderr
, Name
": Bad super-minor number: %s.\n", optarg
);
670 case O(ASSEMBLE
,'U'): /* update the superblock */
673 fprintf(stderr
, Name
": Can only update one aspect"
674 " of superblock, both %s and %s given.\n",
678 if (mode
== MISC
&& !subarray
) {
679 fprintf(stderr
, Name
": Only subarrays can be"
680 " updated in misc mode\n");
684 if (strcmp(update
, "sparc2.2")==0)
686 if (strcmp(update
, "super-minor") == 0)
688 if (strcmp(update
, "summaries")==0)
690 if (strcmp(update
, "resync")==0)
692 if (strcmp(update
, "uuid")==0)
694 if (strcmp(update
, "name")==0)
696 if (strcmp(update
, "homehost")==0)
698 if (strcmp(update
, "devicesize")==0)
700 if (strcmp(update
, "no-bitmap")==0)
702 if (strcmp(update
, "byteorder")==0) {
705 Name
": must not set metadata"
706 " type with --update=byteorder.\n");
709 for(i
=0; !ss
&& superlist
[i
]; i
++)
710 ss
= superlist
[i
]->match_metadata_desc(
713 fprintf(stderr
, Name
": INTERNAL ERROR"
714 " cannot find 0.swap\n");
720 if (strcmp(update
,"?") == 0 ||
721 strcmp(update
, "help") == 0) {
723 fprintf(outf
, Name
": ");
727 Name
": '--update=%s' is invalid. ",
730 fprintf(outf
, "Valid --update options are:\n"
731 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
732 " 'summaries', 'homehost', 'byteorder', 'devicesize',\n"
734 exit(outf
== stdout
? 0 : 2);
737 /* update=devicesize is allowed with --re-add */
738 if (devmode
!= 'a' || re_add
!= 1) {
739 fprintf(stderr
, Name
"--update in Manage mode only"
740 " allowed with --re-add.\n");
744 fprintf(stderr
, Name
": Can only update one aspect"
745 " of superblock, both %s and %s given.\n",
750 if (strcmp(update
, "devicesize") != 0) {
751 fprintf(stderr
, Name
": only 'devicesize' can be"
752 " updated with --re-add\n");
757 case O(INCREMENTAL
,NoDegraded
):
758 fprintf(stderr
, Name
": --no-degraded is deprecated in Incremental mode\n");
759 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
760 runstop
= -1; /* --stop isn't allowed for --assemble,
761 * so we overload slightly */
764 case O(ASSEMBLE
,'c'):
765 case O(ASSEMBLE
,ConfigFile
):
766 case O(INCREMENTAL
, 'c'):
767 case O(INCREMENTAL
, ConfigFile
):
769 case O(MISC
, ConfigFile
):
771 case O(MONITOR
,ConfigFile
):
773 fprintf(stderr
, Name
": configfile cannot be set twice. "
774 "Second value is %s.\n", optarg
);
778 set_conffile(configfile
);
779 /* FIXME possibly check that config file exists. Even parse it */
781 case O(ASSEMBLE
,'s'): /* scan */
784 case O(INCREMENTAL
,'s'):
788 case O(MONITOR
,'m'): /* mail address */
789 case O(MONITOR
,EMail
):
791 fprintf(stderr
, Name
": only specify one mailaddress. %s ignored.\n",
797 case O(MONITOR
,'p'): /* alert program */
798 case O(MONITOR
,ProgramOpt
): /* alert program */
800 fprintf(stderr
, Name
": only specify one alter program. %s ignored.\n",
806 case O(MONITOR
,'r'): /* rebuild increments */
807 case O(MONITOR
,Increment
):
808 increments
= atoi(optarg
);
809 if (increments
>99 || increments
<1) {
810 fprintf(stderr
, Name
": please specify positive integer between 1 and 99 as rebuild increments.\n");
815 case O(MONITOR
,'d'): /* delay in seconds */
817 case O(BUILD
,'d'): /* delay for bitmap updates */
820 fprintf(stderr
, Name
": only specify delay once. %s ignored.\n",
823 delay
= strtol(optarg
, &c
, 10);
824 if (!optarg
[0] || *c
|| delay
<1) {
825 fprintf(stderr
, Name
": invalid delay: %s\n",
831 case O(MONITOR
,'f'): /* daemonise */
832 case O(MONITOR
,Fork
):
835 case O(MONITOR
,'i'): /* pid */
837 fprintf(stderr
, Name
": only specify one pid file. %s ignored.\n",
842 case O(MONITOR
,'1'): /* oneshot */
846 case O(MONITOR
,'t'): /* test */
849 case O(MONITOR
,'y'): /* log messages to syslog */
850 openlog("mdadm", LOG_PID
, SYSLOG_FACILITY
);
853 case O(MONITOR
, NoSharing
):
856 /* now the general management options. Some are applicable
857 * to other modes. None have arguments.
862 case O(MANAGE
,Add
): /* add a drive */
866 case O(MANAGE
,ReAdd
):
870 case O(MANAGE
,'r'): /* remove a drive */
871 case O(MANAGE
,Remove
):
874 case O(MANAGE
,'f'): /* set faulty */
876 case O(INCREMENTAL
,'f'):
877 case O(INCREMENTAL
,Remove
):
878 case O(INCREMENTAL
,Fail
): /* r for incremental is taken, use f
879 * even though we will both fail and
880 * remove the device */
883 case O(INCREMENTAL
,'R'):
885 case O(ASSEMBLE
,'R'):
887 case O(CREATE
,'R'): /* Run the array */
889 fprintf(stderr
, Name
": Cannot both Stop and Run an array\n");
896 fprintf(stderr
, Name
": Cannot both Run and Stop an array\n");
915 case O(MISC
, WaitOpt
):
916 case O(MISC
, Waitclean
):
917 case O(MISC
, DetailPlatform
):
918 case O(MISC
, KillSubarray
):
919 case O(MISC
, UpdateSubarray
):
920 if (devmode
&& devmode
!= opt
&&
921 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
922 fprintf(stderr
, Name
": --examine/-E cannot be given with ");
923 if (devmode
== 'E') {
924 if (option_index
>= 0)
925 fprintf(stderr
, "--%s\n",
926 long_options
[option_index
].name
);
928 fprintf(stderr
, "-%c\n", opt
);
929 } else if (isalpha(devmode
))
930 fprintf(stderr
, "-%c\n", devmode
);
932 fprintf(stderr
, "previous option\n");
937 case O(MISC
, UdevRules
):
938 if (devmode
&& devmode
!= opt
) {
939 fprintf(stderr
, Name
": --udev-rules must"
940 " be the only option.\n");
943 fprintf(stderr
, Name
": only specify one udev "
944 "rule filename. %s ignored.\n",
947 udev_filename
= optarg
;
955 case O(MISC
, Sparc22
):
956 if (devmode
!= 'E') {
957 fprintf(stderr
, Name
": --sparc2.2 only allowed with --examine\n");
963 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
964 case O(ASSEMBLE
,Bitmap
):
966 fprintf(stderr
, Name
": bitmap file needed with -b in --assemble mode\n");
969 if (strcmp(optarg
, "internal")==0) {
970 fprintf(stderr
, Name
": there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
973 bitmap_fd
= open(optarg
, O_RDWR
);
974 if (!*optarg
|| bitmap_fd
< 0) {
975 fprintf(stderr
, Name
": cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
978 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
981 case O(ASSEMBLE
, BackupFile
):
982 case O(GROW
, BackupFile
):
983 /* Specify a file into which grow might place a backup,
984 * or from which assemble might recover a backup
987 fprintf(stderr
, Name
": backup file already specified, rejecting %s\n", optarg
);
990 backup_file
= optarg
;
993 case O(ASSEMBLE
, InvalidBackup
):
994 /* Acknowledge that the backupfile is invalid, but ask
1001 case O(BUILD
,Bitmap
):
1003 case O(CREATE
,Bitmap
): /* here we create the bitmap */
1004 if (strcmp(optarg
, "none") == 0) {
1005 fprintf(stderr
, Name
": '--bitmap none' only"
1006 " support for --grow\n");
1011 case O(GROW
,Bitmap
):
1012 if (strcmp(optarg
, "internal")== 0 ||
1013 strcmp(optarg
, "none")== 0 ||
1014 strchr(optarg
, '/') != NULL
) {
1015 bitmap_file
= optarg
;
1019 fprintf(stderr
, Name
": bitmap file must contain a '/', or be 'internal', or 'none'\n"
1020 " not '%s'\n", optarg
);
1023 case O(GROW
,BitmapChunk
):
1024 case O(BUILD
,BitmapChunk
):
1025 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
1026 bitmap_chunk
= parse_size(optarg
);
1027 if (bitmap_chunk
< 0 ||
1028 bitmap_chunk
& (bitmap_chunk
- 1)) {
1030 Name
": invalid bitmap chunksize: %s\n",
1034 /* convert sectors to B, chunk of 0 means 512B */
1035 bitmap_chunk
= bitmap_chunk
? bitmap_chunk
* 512 : 512;
1038 case O(GROW
, WriteBehind
):
1039 case O(BUILD
, WriteBehind
):
1040 case O(CREATE
, WriteBehind
): /* write-behind mode */
1041 write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
1043 write_behind
= strtol(optarg
, &c
, 10);
1044 if (write_behind
< 0 || *c
||
1045 write_behind
> 16383) {
1046 fprintf(stderr
, Name
": Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
1052 case O(INCREMENTAL
, 'r'):
1053 case O(INCREMENTAL
, RebuildMapOpt
):
1056 case O(INCREMENTAL
, IncrementalPath
):
1057 remove_path
= optarg
;
1060 /* We have now processed all the valid options. Anything else is
1063 if (option_index
> 0)
1064 fprintf(stderr
, Name
":option --%s not valid in %s mode\n",
1065 long_options
[option_index
].name
,
1066 map_num(modes
, mode
));
1068 fprintf(stderr
, Name
": option -%c not valid in %s mode\n",
1069 opt
, map_num(modes
, mode
));
1075 char *help_text
= Help
;
1076 if (print_help
== 2)
1077 help_text
= OptionHelp
;
1080 case ASSEMBLE
: help_text
= Help_assemble
; break;
1081 case BUILD
: help_text
= Help_build
; break;
1082 case CREATE
: help_text
= Help_create
; break;
1083 case MANAGE
: help_text
= Help_manage
; break;
1084 case MISC
: help_text
= Help_misc
; break;
1085 case MONITOR
: help_text
= Help_monitor
; break;
1086 case GROW
: help_text
= Help_grow
; break;
1087 case INCREMENTAL
:help_text
= Help_incr
; break;
1089 fputs(help_text
,stdout
);
1093 if (!mode
&& devs_found
) {
1096 if (devlist
->disposition
== 0)
1097 devlist
->disposition
= devmode
;
1100 fputs(Usage
, stderr
);
1105 struct createinfo
*ci
= conf_get_create_info();
1107 if (strcasecmp(symlinks
, "yes") == 0)
1109 else if (strcasecmp(symlinks
, "no") == 0)
1112 fprintf(stderr
, Name
": option --symlinks must be 'no' or 'yes'\n");
1116 /* Ok, got the option parsing out of the way
1117 * hopefully it's mostly right but there might be some stuff
1120 * That is mosty checked in the per-mode stuff but...
1122 * For @,B,C and A without -s, the first device listed must be an md device
1123 * we check that here and open it.
1126 if (mode
==MANAGE
|| mode
== BUILD
|| mode
== CREATE
|| mode
== GROW
||
1127 (mode
== ASSEMBLE
&& ! scan
)) {
1128 if (devs_found
< 1) {
1129 fprintf(stderr
, Name
": an md device must be given in this mode\n");
1132 if ((int)ident
.super_minor
== -2 && autof
) {
1133 fprintf(stderr
, Name
": --super-minor=dev is incompatible with --auto\n");
1136 if (mode
== MANAGE
|| mode
== GROW
) {
1137 mdfd
= open_mddev(devlist
->devname
, 1);
1141 /* non-existent device is OK */
1142 mdfd
= open_mddev(devlist
->devname
, 0);
1144 fprintf(stderr
, Name
": device %s exists but is not an "
1145 "md array.\n", devlist
->devname
);
1148 if ((int)ident
.super_minor
== -2) {
1151 fprintf(stderr
, Name
": --super-minor=dev given, and "
1152 "listed device %s doesn't exist.\n",
1157 ident
.super_minor
= minor(stb
.st_rdev
);
1159 if (mdfd
>= 0 && mode
!= MANAGE
&& mode
!= GROW
) {
1160 /* We don't really want this open yet, we just might
1161 * have wanted to check some things
1169 if (raiddisks
== 1 && !force
&& level
!= -5) {
1170 fprintf(stderr
, Name
": '1' is an unusual number of drives for an array, so it is probably\n"
1171 " a mistake. If you really mean it you will need to specify --force before\n"
1172 " setting the number of drives.\n");
1177 if (homehost
== NULL
)
1178 homehost
= conf_get_homehost(&require_homehost
);
1179 if (homehost
== NULL
|| strcasecmp(homehost
, "<system>")==0) {
1180 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
1181 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
1182 homehost
= sys_hostname
;
1185 if (homehost
&& (!homehost
[0] || strcasecmp(homehost
, "<none>") == 0)) {
1187 require_homehost
= 0;
1190 if ((mode
!= MISC
|| devmode
!= 'E') &&
1192 fprintf(stderr
, Name
": must be super-user to perform this action\n");
1196 ident
.autof
= autof
;
1201 /* readonly, add/remove, readwrite, runstop */
1203 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1204 if (!rv
&& devs_found
>1)
1205 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
1206 devlist
->next
, verbose
-quiet
, test
,
1208 if (!rv
&& readonly
< 0)
1209 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1211 rv
= Manage_runstop(devlist
->devname
, mdfd
, runstop
, quiet
);
1214 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1215 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !scan
) {
1216 /* Only a device has been given, so get details from config file */
1217 struct mddev_ident
*array_ident
= conf_get_ident(devlist
->devname
);
1218 if (array_ident
== NULL
) {
1219 fprintf(stderr
, Name
": %s not identified in config file.\n",
1225 if (array_ident
->autof
== 0)
1226 array_ident
->autof
= autof
;
1227 rv
|= Assemble(ss
, devlist
->devname
, array_ident
,
1228 NULL
, backup_file
, invalid_backup
,
1229 readonly
, runstop
, update
,
1230 homehost
, require_homehost
,
1231 verbose
-quiet
, force
);
1234 rv
= Assemble(ss
, devlist
->devname
, &ident
,
1235 devlist
->next
, backup_file
, invalid_backup
,
1236 readonly
, runstop
, update
,
1237 homehost
, require_homehost
,
1238 verbose
-quiet
, force
);
1239 else if (devs_found
>0) {
1240 if (update
&& devs_found
> 1) {
1241 fprintf(stderr
, Name
": can only update a single array at a time\n");
1244 if (backup_file
&& devs_found
> 1) {
1245 fprintf(stderr
, Name
": can only assemble a single array when providing a backup file.\n");
1248 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1249 struct mddev_ident
*array_ident
= conf_get_ident(dv
->devname
);
1250 if (array_ident
== NULL
) {
1251 fprintf(stderr
, Name
": %s not identified in config file.\n",
1256 if (array_ident
->autof
== 0)
1257 array_ident
->autof
= autof
;
1258 rv
|= Assemble(ss
, dv
->devname
, array_ident
,
1259 NULL
, backup_file
, invalid_backup
,
1260 readonly
, runstop
, update
,
1261 homehost
, require_homehost
,
1262 verbose
-quiet
, force
);
1265 struct mddev_ident
*a
, *array_list
= conf_get_ident(NULL
);
1266 struct mddev_dev
*devlist
= conf_get_devs();
1268 int failures
, successes
;
1269 if (devlist
== NULL
) {
1270 fprintf(stderr
, Name
": No devices listed in conf file were found.\n");
1274 fprintf(stderr
, Name
": --update not meaningful with a --scan assembly.\n");
1278 fprintf(stderr
, Name
": --backup_file not meaningful with a --scan assembly.\n");
1281 for (a
= array_list
; a
; a
= a
->next
) {
1290 for (a
= array_list
; a
; a
= a
->next
) {
1295 strcasecmp(a
->devname
, "<ignore>") == 0)
1298 r
= Assemble(ss
, a
->devname
,
1301 readonly
, runstop
, NULL
,
1302 homehost
, require_homehost
,
1303 verbose
-quiet
, force
);
1312 } while (failures
&& successes
);
1313 if (homehost
&& cnt
== 0) {
1314 /* Maybe we can auto-assemble something.
1315 * Repeatedly call Assemble in auto-assemble mode
1320 ident
.autof
= autof
;
1322 struct mddev_dev
*devlist
= conf_get_devs();
1325 rv2
= Assemble(ss
, NULL
,
1328 readonly
, runstop
, NULL
,
1329 homehost
, require_homehost
,
1330 verbose
-quiet
, force
);
1336 /* Incase there are stacked devices, we need to go around again */
1338 if (cnt
== 0 && rv
== 0) {
1339 fprintf(stderr
, Name
": No arrays found in config file or automatically\n");
1343 } else if (cnt
== 0 && rv
== 0) {
1344 fprintf(stderr
, Name
": No arrays found in config file\n");
1350 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1351 if (write_behind
&& !bitmap_file
) {
1352 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1356 if (raiddisks
== 0) {
1357 fprintf(stderr
, Name
": no raid-devices specified.\n");
1363 if (strcmp(bitmap_file
, "internal")==0) {
1364 fprintf(stderr
, Name
": 'internal' bitmaps not supported with --build\n");
1369 rv
= Build(devlist
->devname
, chunk
, level
, layout
,
1370 raiddisks
, devlist
->next
, assume_clean
,
1371 bitmap_file
, bitmap_chunk
, write_behind
,
1372 delay
, verbose
-quiet
, autof
, size
);
1375 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1376 if (write_behind
&& !bitmap_file
) {
1377 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1381 if (raiddisks
== 0) {
1382 fprintf(stderr
, Name
": no raid-devices specified.\n");
1387 rv
= Create(ss
, devlist
->devname
, chunk
, level
, layout
, size
<0 ? 0 : size
,
1388 raiddisks
, sparedisks
, ident
.name
, homehost
,
1389 ident
.uuid_set
? ident
.uuid
: NULL
,
1390 devs_found
-1, devlist
->next
, runstop
, verbose
-quiet
, force
, assume_clean
,
1391 bitmap_file
, bitmap_chunk
, write_behind
, delay
, autof
);
1394 if (devmode
== 'E') {
1395 if (devlist
== NULL
&& !scan
) {
1396 fprintf(stderr
, Name
": No devices to examine\n");
1399 if (devlist
== NULL
)
1400 devlist
= conf_get_devs();
1401 if (devlist
== NULL
) {
1402 fprintf(stderr
, Name
": No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1405 if (brief
&& verbose
)
1407 rv
= Examine(devlist
, scan
?(verbose
>1?0:verbose
+1):brief
,
1409 SparcAdjust
, ss
, homehost
);
1410 } else if (devmode
== DetailPlatform
) {
1411 rv
= Detail_Platform(ss
? ss
->ss
: NULL
, ss
? scan
: 1, verbose
);
1413 if (devlist
== NULL
) {
1414 if ((devmode
=='D' || devmode
== Waitclean
) && scan
) {
1415 /* apply --detail or --wait-clean to
1416 * all devices in /proc/mdstat
1418 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1419 struct mdstat_ent
*e
;
1420 struct map_ent
*map
= NULL
;
1422 int v
= verbose
>1?0:verbose
+1;
1424 for (members
= 0; members
<= 1; members
++) {
1425 for (e
=ms
; e
; e
=e
->next
) {
1428 int member
= e
->metadata_version
&&
1429 strncmp(e
->metadata_version
,
1430 "external:/", 10) == 0;
1431 if (members
!= member
)
1433 me
= map_by_devnum(&map
, e
->devnum
);
1435 && strcmp(me
->path
, "/unknown") != 0)
1438 name
= get_md_name(e
->devnum
);
1441 fprintf(stderr
, Name
": cannot find device file for %s\n",
1446 rv
|= Detail(name
, v
,
1450 rv
|= WaitClean(name
, -1, v
);
1455 } else if (devmode
== 'S' && scan
) {
1456 /* apply --stop to all devices in /proc/mdstat */
1457 /* Due to possible stacking of devices, repeat until
1458 * nothing more can be stopped
1460 int progress
=1, err
;
1463 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1464 struct mdstat_ent
*e
;
1466 if (!progress
) last
= 1;
1467 progress
= 0; err
= 0;
1468 for (e
=ms
; e
; e
=e
->next
) {
1469 char *name
= get_md_name(e
->devnum
);
1472 fprintf(stderr
, Name
": cannot find device file for %s\n",
1476 mdfd
= open_mddev(name
, 1);
1478 if (Manage_runstop(name
, mdfd
, -1, quiet
?1:last
?0:-1))
1488 } while (!last
&& err
);
1490 } else if (devmode
== UdevRules
) {
1491 rv
= Write_rules(udev_filename
);
1493 fprintf(stderr
, Name
": No devices given.\n");
1497 for (dv
=devlist
; dv
; dv
=dv
->next
) {
1498 switch(dv
->disposition
) {
1500 rv
|= Detail(dv
->devname
,
1502 export
, test
, homehost
);
1504 case 'K': /* Zero superblock */
1506 rv
|= Kill(dv
->devname
, ss
, force
, quiet
,0);
1510 rv
|= Kill(dv
->devname
, NULL
, force
, q
, 0);
1517 rv
|= Query(dv
->devname
); continue;
1519 rv
|= ExamineBitmap(dv
->devname
, brief
, ss
); continue;
1522 rv
|= Wait(dv
->devname
); continue;
1524 rv
|= WaitClean(dv
->devname
, -1, verbose
-quiet
); continue;
1526 rv
|= Kill_subarray(dv
->devname
, subarray
, quiet
);
1528 case UpdateSubarray
:
1529 if (update
== NULL
) {
1531 Name
": -U/--update must be specified with --update-subarray\n");
1535 rv
|= Update_subarray(dv
->devname
, subarray
, update
, &ident
, quiet
);
1538 mdfd
= open_mddev(dv
->devname
, 1);
1540 switch(dv
->disposition
) {
1542 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1, quiet
); break;
1544 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1, quiet
); break;
1546 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1548 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;
1557 if (!devlist
&& !scan
) {
1558 fprintf(stderr
, Name
": Cannot monitor: need --scan or at least one device\n");
1562 if (pidfile
&& !daemonise
) {
1563 fprintf(stderr
, Name
": Cannot write a pid file when not in daemon mode\n");
1568 if (get_linux_version() > 2006016)
1569 /* mdstat responds to poll */
1574 rv
= Monitor(devlist
, mailaddr
, program
,
1575 delay
?delay
:60, daemonise
, scan
, oneshot
,
1576 dosyslog
, test
, pidfile
, increments
, spare_sharing
);
1580 if (array_size
>= 0) {
1581 /* alway impose array size first, independent of
1583 * Do not allow level or raid_disks changes at the
1584 * same time as that can be irreversibly destructive.
1588 if (raiddisks
|| level
!= UnSet
) {
1589 fprintf(stderr
, Name
": cannot change array size in same operation "
1590 "as changing raiddisks or level.\n"
1591 " Change size first, then check that data is still intact.\n");
1595 sysfs_init(&sra
, mdfd
, 0);
1596 if (array_size
== 0)
1597 err
= sysfs_set_str(&sra
, NULL
, "array_size", "default");
1599 err
= sysfs_set_num(&sra
, NULL
, "array_size", array_size
/ 2);
1602 fprintf(stderr
, Name
": --array-size setting"
1603 " is too large.\n");
1605 fprintf(stderr
, Name
": current kernel does"
1606 " not support setting --array-size\n");
1611 if (devs_found
> 1 && raiddisks
== 0) {
1613 if (size
>= 0 || chunk
|| layout_str
!= NULL
|| bitmap_file
) {
1614 fprintf(stderr
, Name
": --add cannot be used with "
1615 "other geometry changes in --grow mode\n");
1619 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1620 rv
= Grow_Add_device(devlist
->devname
, mdfd
,
1625 } else if (bitmap_file
) {
1626 if (size
>= 0 || raiddisks
|| chunk
||
1627 layout_str
!= NULL
|| devs_found
> 1) {
1628 fprintf(stderr
, Name
": --bitmap changes cannot be "
1629 "used with other geometry changes "
1630 "in --grow mode\n");
1635 delay
= DEFAULT_BITMAP_DELAY
;
1636 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, bitmap_file
,
1637 bitmap_chunk
, delay
, write_behind
, force
);
1638 } else if (size
>= 0 || raiddisks
!= 0 || layout_str
!= NULL
1639 || chunk
!= 0 || level
!= UnSet
) {
1640 rv
= Grow_reshape(devlist
->devname
, mdfd
, quiet
, backup_file
,
1641 size
, level
, layout_str
, chunk
, raiddisks
,
1643 assume_clean
, force
);
1644 } else if (array_size
< 0)
1645 fprintf(stderr
, Name
": no changes to --grow\n");
1653 fprintf(stderr
, Name
1654 ": --incremental --scan meaningless without --run.\n");
1657 if (devmode
== 'f') {
1658 fprintf(stderr
, Name
1659 ": --incremental --scan --fail not supported.\n");
1662 rv
= IncrementalScan(verbose
);
1665 if (!rebuild_map
&& !scan
) {
1666 fprintf(stderr
, Name
1667 ": --incremental requires a device.\n");
1672 if (devlist
->next
) {
1673 fprintf(stderr
, Name
1674 ": --incremental can only handle one device.\n");
1679 rv
= IncrementalRemove(devlist
->devname
, remove_path
,
1682 rv
= Incremental(devlist
->devname
, verbose
-quiet
,
1683 runstop
, ss
, homehost
,
1684 require_homehost
, autof
);