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
;
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");
237 case 'K': if (!mode
) newmode
= MISC
; break;
238 case NoSharing
: newmode
= MONITOR
; break;
240 if (mode
&& newmode
== mode
) {
241 /* everybody happy ! */
242 } else if (mode
&& newmode
!= mode
) {
244 fprintf(stderr
, Name
": ");
245 if (option_index
>= 0)
246 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
248 fprintf(stderr
, "-%c", opt
);
249 fprintf(stderr
, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
250 map_num(modes
, newmode
),
251 map_num(modes
, mode
));
253 } else if (!mode
&& newmode
) {
256 /* special case of -c --help */
257 if ((opt
== 'c' || opt
== ConfigFile
) &&
258 ( strncmp(optarg
, "--h", 3)==0 ||
259 strncmp(optarg
, "-h", 2)==0)) {
260 fputs(Help_config
, stdout
);
264 /* If first option is a device, don't force the mode yet */
266 if (devs_found
== 0) {
267 dv
= malloc(sizeof(*dv
));
269 fprintf(stderr
, Name
": malloc failed\n");
272 dv
->devname
= optarg
;
273 dv
->disposition
= devmode
;
274 dv
->writemostly
= writemostly
;
279 devlistend
= &dv
->next
;
284 /* No mode yet, and this is the second device ... */
285 fprintf(stderr
, Name
": An option must be given to set the mode before a second device\n"
286 " (%s) is listed\n", optarg
);
289 if (option_index
>= 0)
290 fprintf(stderr
, Name
": --%s", long_options
[option_index
].name
);
292 fprintf(stderr
, Name
": -%c", opt
);
293 fprintf(stderr
, " does not set the mode, and so cannot be the first option.\n");
297 /* if we just set the mode, then done */
311 /* an undecorated option - must be a device name.
313 if (devs_found
> 0 && mode
== MANAGE
&& !devmode
) {
314 fprintf(stderr
, Name
": Must give one of -a/-r/-f"
315 " for subsequent devices at %s\n", optarg
);
318 if (devs_found
> 0 && mode
== GROW
&& !devmode
) {
319 fprintf(stderr
, Name
": Must give -a/--add for"
320 " devices to add: %s\n", optarg
);
323 dv
= malloc(sizeof(*dv
));
325 fprintf(stderr
, Name
": malloc failed\n");
328 dv
->devname
= optarg
;
329 dv
->disposition
= devmode
;
330 dv
->writemostly
= writemostly
;
335 devlistend
= &dv
->next
;
341 /* We've got a mode, and opt is now something else which
342 * could depend on the mode */
343 #define O(a,b) ((a<<16)|b)
344 switch (O(mode
,opt
)) {
346 case O(GROW
,ChunkSize
):
348 case O(CREATE
,ChunkSize
):
349 case O(BUILD
,'c'): /* chunk or rounding */
350 case O(BUILD
,ChunkSize
): /* chunk or rounding */
352 fprintf(stderr
, Name
": chunk/rounding may only be specified once. "
353 "Second value is %s.\n", optarg
);
356 chunk
= parse_size(optarg
);
357 if (chunk
< 8 || ((chunk
-1)&chunk
)) {
358 fprintf(stderr
, Name
": invalid chunk/rounding value: %s\n",
362 /* Covert sectors to K */
367 case O(ASSEMBLE
,AutoHomeHost
):
368 auto_update_home
= 1;
371 case O(INCREMENTAL
, 'e'):
373 case O(ASSEMBLE
,'e'):
374 case O(MISC
,'e'): /* set metadata (superblock) information */
376 fprintf(stderr
, Name
": metadata information already given\n");
379 for(i
=0; !ss
&& superlist
[i
]; i
++)
380 ss
= superlist
[i
]->match_metadata_desc(optarg
);
383 fprintf(stderr
, Name
": unrecognised metadata identifier: %s\n", optarg
);
389 case O(MANAGE
,WriteMostly
):
391 case O(BUILD
,WriteMostly
):
393 case O(CREATE
,WriteMostly
):
394 /* set write-mostly for following devices */
399 /* clear write-mostly for following devices */
406 case O(BUILD
,'z'): /* size */
408 fprintf(stderr
, Name
": size may only be specified once. "
409 "Second value is %s.\n", optarg
);
412 if (strcmp(optarg
, "max")==0)
415 size
= parse_size(optarg
);
417 fprintf(stderr
, Name
": invalid size: %s\n",
421 /* convert sectors to K */
426 case O(GROW
,'Z'): /* array size */
427 if (array_size
>= 0) {
428 fprintf(stderr
, Name
": array-size may only be specified once. "
429 "Second value is %s.\n", optarg
);
432 if (strcmp(optarg
, "max") == 0)
435 array_size
= parse_size(optarg
);
436 if (array_size
<= 0) {
437 fprintf(stderr
, Name
": invalid array size: %s\n",
446 case O(BUILD
,'l'): /* set raid level*/
447 if (level
!= UnSet
) {
448 fprintf(stderr
, Name
": raid level may only be set once. "
449 "Second value is %s.\n", optarg
);
452 level
= map_name(pers
, optarg
);
453 if (level
== UnSet
) {
454 fprintf(stderr
, Name
": invalid raid level: %s\n",
458 if (level
!= 0 && level
!= LEVEL_LINEAR
&& level
!= 1 &&
459 level
!= LEVEL_MULTIPATH
&& level
!= LEVEL_FAULTY
&&
462 fprintf(stderr
, Name
": Raid level %s not permitted with --build.\n",
466 if (sparedisks
> 0 && level
< 1 && level
>= -1) {
467 fprintf(stderr
, Name
": raid level %s is incompatible with spare-devices setting.\n",
474 case O(GROW
, 'p'): /* new layout */
475 case O(GROW
, Layout
):
477 fprintf(stderr
,Name
": layout may only be sent once. "
478 "Second value was %s\n", optarg
);
482 /* 'Grow' will parse the value */
485 case O(CREATE
,'p'): /* raid5 layout */
486 case O(CREATE
,Layout
):
487 case O(BUILD
,'p'): /* faulty layout */
488 case O(BUILD
,Layout
):
489 if (layout
!= UnSet
) {
490 fprintf(stderr
,Name
": layout may only be sent once. "
491 "Second value was %s\n", optarg
);
496 fprintf(stderr
, Name
": layout not meaningful for %s arrays.\n",
497 map_num(pers
, level
));
500 fprintf(stderr
, Name
": raid level must be given before layout.\n");
504 layout
= map_name(r5layout
, optarg
);
506 fprintf(stderr
, Name
": layout %s not understood for raid5.\n",
512 layout
= map_name(r6layout
, optarg
);
514 fprintf(stderr
, Name
": layout %s not understood for raid6.\n",
521 layout
= parse_layout_10(optarg
);
523 fprintf(stderr
, Name
": layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg
);
531 layout
= parse_layout_faulty(optarg
);
533 fprintf(stderr
, Name
": layout %s not understood for faulty.\n",
541 case O(CREATE
,AssumeClean
):
542 case O(BUILD
,AssumeClean
): /* assume clean */
548 case O(BUILD
,'n'): /* number of raid disks */
550 fprintf(stderr
, Name
": raid-devices set twice: %d and %s\n",
554 raiddisks
= strtol(optarg
, &c
, 10);
555 if (!optarg
[0] || *c
|| raiddisks
<=0) {
556 fprintf(stderr
, Name
": invalid number of raid devices: %s\n",
560 ident
.raid_disks
= raiddisks
;
563 case O(CREATE
,'x'): /* number of spare (eXtra) discs */
565 fprintf(stderr
,Name
": spare-devices set twice: %d and %s\n",
569 if (level
!= UnSet
&& level
<= 0 && level
>= -1) {
570 fprintf(stderr
, Name
": spare-devices setting is incompatible with raid level %d\n",
574 sparedisks
= strtol(optarg
, &c
, 10);
575 if (!optarg
[0] || *c
|| sparedisks
< 0) {
576 fprintf(stderr
, Name
": invalid number of spare-devices: %s\n",
586 case O(INCREMENTAL
,'a'):
587 case O(INCREMENTAL
,Auto
):
588 case O(ASSEMBLE
,'a'):
589 case O(ASSEMBLE
,Auto
): /* auto-creation of device node */
590 autof
= parse_auto(optarg
, "--auto flag", 0);
593 case O(CREATE
,Symlinks
):
594 case O(BUILD
,Symlinks
):
595 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
599 case O(BUILD
,'f'): /* force honouring '-n 1' */
600 case O(BUILD
,Force
): /* force honouring '-n 1' */
601 case O(GROW
,'f'): /* ditto */
602 case O(GROW
,Force
): /* ditto */
603 case O(CREATE
,'f'): /* force honouring of device list */
604 case O(CREATE
,Force
): /* force honouring of device list */
605 case O(ASSEMBLE
,'f'): /* force assembly */
606 case O(ASSEMBLE
,Force
): /* force assembly */
607 case O(MISC
,'f'): /* force zero */
608 case O(MISC
,Force
): /* force zero */
612 /* now for the Assemble options */
613 case O(CREATE
,'u'): /* uuid of array */
614 case O(ASSEMBLE
,'u'): /* uuid of array */
615 if (ident
.uuid_set
) {
616 fprintf(stderr
, Name
": uuid cannot be set twice. "
617 "Second value %s.\n", optarg
);
620 if (parse_uuid(optarg
, ident
.uuid
))
623 fprintf(stderr
,Name
": Bad uuid: %s\n", optarg
);
629 case O(ASSEMBLE
,'N'):
632 fprintf(stderr
, Name
": name cannot be set twice. "
633 "Second value %s.\n", optarg
);
636 if (mode
== MISC
&& !subarray
) {
637 fprintf(stderr
, Name
": -N/--name only valid with --update-subarray in misc mode\n");
640 if (strlen(optarg
) > 32) {
641 fprintf(stderr
, Name
": name '%s' is too long, 32 chars max.\n",
645 strcpy(ident
.name
, optarg
);
648 case O(ASSEMBLE
,'m'): /* super-minor for array */
649 case O(ASSEMBLE
,SuperMinor
):
650 if (ident
.super_minor
!= UnSet
) {
651 fprintf(stderr
, Name
": super-minor cannot be set twice. "
652 "Second value: %s.\n", optarg
);
655 if (strcmp(optarg
, "dev")==0)
656 ident
.super_minor
= -2;
658 ident
.super_minor
= strtoul(optarg
, &cp
, 10);
659 if (!optarg
[0] || *cp
) {
660 fprintf(stderr
, Name
": Bad super-minor number: %s.\n", optarg
);
666 case O(ASSEMBLE
,'U'): /* update the superblock */
669 fprintf(stderr
, Name
": Can only update one aspect"
670 " of superblock, both %s and %s given.\n",
674 if (mode
== MISC
&& !subarray
) {
675 fprintf(stderr
, Name
": Only subarrays can be"
676 " updated in misc mode\n");
680 if (strcmp(update
, "sparc2.2")==0)
682 if (strcmp(update
, "super-minor") == 0)
684 if (strcmp(update
, "summaries")==0)
686 if (strcmp(update
, "resync")==0)
688 if (strcmp(update
, "uuid")==0)
690 if (strcmp(update
, "name")==0)
692 if (strcmp(update
, "homehost")==0)
694 if (strcmp(update
, "devicesize")==0)
696 if (strcmp(update
, "no-bitmap")==0)
698 if (strcmp(update
, "byteorder")==0) {
701 Name
": must not set metadata"
702 " type with --update=byteorder.\n");
705 for(i
=0; !ss
&& superlist
[i
]; i
++)
706 ss
= superlist
[i
]->match_metadata_desc(
709 fprintf(stderr
, Name
": INTERNAL ERROR"
710 " cannot find 0.swap\n");
716 if (strcmp(update
,"?") == 0 ||
717 strcmp(update
, "help") == 0) {
719 fprintf(outf
, Name
": ");
723 Name
": '--update=%s' is invalid. ",
726 fprintf(outf
, "Valid --update options are:\n"
727 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
728 " 'summaries', 'homehost', 'byteorder', 'devicesize',\n"
730 exit(outf
== stdout
? 0 : 2);
733 /* update=devicesize is allowed with --re-add */
734 if (devmode
!= 'a' || re_add
!= 1) {
735 fprintf(stderr
, Name
"--update in Manage mode only"
736 " allowed with --re-add.\n");
740 fprintf(stderr
, Name
": Can only update one aspect"
741 " of superblock, both %s and %s given.\n",
746 if (strcmp(update
, "devicesize") != 0) {
747 fprintf(stderr
, Name
": only 'devicesize' can be"
748 " updated with --re-add\n");
753 case O(INCREMENTAL
,NoDegraded
):
754 fprintf(stderr
, Name
": --no-degraded is deprecated in Incremental mode\n");
755 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
756 runstop
= -1; /* --stop isn't allowed for --assemble,
757 * so we overload slightly */
760 case O(ASSEMBLE
,'c'):
761 case O(ASSEMBLE
,ConfigFile
):
762 case O(INCREMENTAL
, 'c'):
763 case O(INCREMENTAL
, ConfigFile
):
765 case O(MISC
, ConfigFile
):
767 case O(MONITOR
,ConfigFile
):
769 fprintf(stderr
, Name
": configfile cannot be set twice. "
770 "Second value is %s.\n", optarg
);
774 set_conffile(configfile
);
775 /* FIXME possibly check that config file exists. Even parse it */
777 case O(ASSEMBLE
,'s'): /* scan */
780 case O(INCREMENTAL
,'s'):
784 case O(MONITOR
,'m'): /* mail address */
785 case O(MONITOR
,EMail
):
787 fprintf(stderr
, Name
": only specify one mailaddress. %s ignored.\n",
793 case O(MONITOR
,'p'): /* alert program */
794 case O(MONITOR
,ProgramOpt
): /* alert program */
796 fprintf(stderr
, Name
": only specify one alter program. %s ignored.\n",
802 case O(MONITOR
,'r'): /* rebuild increments */
803 case O(MONITOR
,Increment
):
804 increments
= atoi(optarg
);
805 if (increments
>99 || increments
<1) {
806 fprintf(stderr
, Name
": please specify positive integer between 1 and 99 as rebuild increments.\n");
811 case O(MONITOR
,'d'): /* delay in seconds */
813 case O(BUILD
,'d'): /* delay for bitmap updates */
816 fprintf(stderr
, Name
": only specify delay once. %s ignored.\n",
819 delay
= strtol(optarg
, &c
, 10);
820 if (!optarg
[0] || *c
|| delay
<1) {
821 fprintf(stderr
, Name
": invalid delay: %s\n",
827 case O(MONITOR
,'f'): /* daemonise */
828 case O(MONITOR
,Fork
):
831 case O(MONITOR
,'i'): /* pid */
833 fprintf(stderr
, Name
": only specify one pid file. %s ignored.\n",
838 case O(MONITOR
,'1'): /* oneshot */
841 case O(MONITOR
,'t'): /* test */
844 case O(MONITOR
,'y'): /* log messages to syslog */
845 openlog("mdadm", LOG_PID
, SYSLOG_FACILITY
);
848 case O(MONITOR
, NoSharing
):
851 /* now the general management options. Some are applicable
852 * to other modes. None have arguments.
857 case O(MANAGE
,Add
): /* add a drive */
861 case O(MANAGE
,ReAdd
):
865 case O(MANAGE
,'r'): /* remove a drive */
866 case O(MANAGE
,Remove
):
869 case O(MANAGE
,'f'): /* set faulty */
871 case O(INCREMENTAL
,'f'):
872 case O(INCREMENTAL
,Remove
):
873 case O(INCREMENTAL
,Fail
): /* r for incremental is taken, use f
874 * even though we will both fail and
875 * remove the device */
878 case O(INCREMENTAL
,'R'):
880 case O(ASSEMBLE
,'R'):
882 case O(CREATE
,'R'): /* Run the array */
884 fprintf(stderr
, Name
": Cannot both Stop and Run an array\n");
891 fprintf(stderr
, Name
": Cannot both Run and Stop an array\n");
910 case O(MISC
, WaitOpt
):
911 case O(MISC
, Waitclean
):
912 case O(MISC
, DetailPlatform
):
913 case O(MISC
, KillSubarray
):
914 case O(MISC
, UpdateSubarray
):
915 if (devmode
&& devmode
!= opt
&&
916 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
917 fprintf(stderr
, Name
": --examine/-E cannot be given with ");
918 if (devmode
== 'E') {
919 if (option_index
>= 0)
920 fprintf(stderr
, "--%s\n",
921 long_options
[option_index
].name
);
923 fprintf(stderr
, "-%c\n", opt
);
924 } else if (isalpha(devmode
))
925 fprintf(stderr
, "-%c\n", devmode
);
927 fprintf(stderr
, "previous option\n");
936 case O(MISC
, Sparc22
):
937 if (devmode
!= 'E') {
938 fprintf(stderr
, Name
": --sparc2.2 only allowed with --examine\n");
944 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
945 case O(ASSEMBLE
,Bitmap
):
947 fprintf(stderr
, Name
": bitmap file needed with -b in --assemble mode\n");
950 if (strcmp(optarg
, "internal")==0) {
951 fprintf(stderr
, Name
": there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
954 bitmap_fd
= open(optarg
, O_RDWR
);
955 if (!*optarg
|| bitmap_fd
< 0) {
956 fprintf(stderr
, Name
": cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
959 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
962 case O(ASSEMBLE
, BackupFile
):
963 case O(GROW
, BackupFile
):
964 /* Specify a file into which grow might place a backup,
965 * or from which assemble might recover a backup
968 fprintf(stderr
, Name
": backup file already specified, rejecting %s\n", optarg
);
971 backup_file
= optarg
;
974 case O(ASSEMBLE
, InvalidBackup
):
975 /* Acknowledge that the backupfile is invalid, but ask
982 case O(BUILD
,Bitmap
):
984 case O(CREATE
,Bitmap
): /* here we create the bitmap */
985 if (strcmp(optarg
, "none") == 0) {
986 fprintf(stderr
, Name
": '--bitmap none' only"
987 " support for --grow\n");
993 if (strcmp(optarg
, "internal")== 0 ||
994 strcmp(optarg
, "none")== 0 ||
995 strchr(optarg
, '/') != NULL
) {
996 bitmap_file
= optarg
;
1000 fprintf(stderr
, Name
": bitmap file must contain a '/', or be 'internal', or 'none'\n"
1001 " not '%s'\n", optarg
);
1004 case O(GROW
,BitmapChunk
):
1005 case O(BUILD
,BitmapChunk
):
1006 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
1007 bitmap_chunk
= parse_size(optarg
);
1008 if (bitmap_chunk
< 0 ||
1009 bitmap_chunk
& (bitmap_chunk
- 1)) {
1011 Name
": invalid bitmap chunksize: %s\n",
1015 /* convert sectors to B, chunk of 0 means 512B */
1016 bitmap_chunk
= bitmap_chunk
? bitmap_chunk
* 512 : 512;
1019 case O(GROW
, WriteBehind
):
1020 case O(BUILD
, WriteBehind
):
1021 case O(CREATE
, WriteBehind
): /* write-behind mode */
1022 write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
1024 write_behind
= strtol(optarg
, &c
, 10);
1025 if (write_behind
< 0 || *c
||
1026 write_behind
> 16383) {
1027 fprintf(stderr
, Name
": Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
1033 case O(INCREMENTAL
, 'r'):
1034 case O(INCREMENTAL
, RebuildMapOpt
):
1037 case O(INCREMENTAL
, IncrementalPath
):
1038 remove_path
= optarg
;
1041 /* We have now processed all the valid options. Anything else is
1044 if (option_index
> 0)
1045 fprintf(stderr
, Name
":option --%s not valid in %s mode\n",
1046 long_options
[option_index
].name
,
1047 map_num(modes
, mode
));
1049 fprintf(stderr
, Name
": option -%c not valid in %s mode\n",
1050 opt
, map_num(modes
, mode
));
1056 char *help_text
= Help
;
1057 if (print_help
== 2)
1058 help_text
= OptionHelp
;
1061 case ASSEMBLE
: help_text
= Help_assemble
; break;
1062 case BUILD
: help_text
= Help_build
; break;
1063 case CREATE
: help_text
= Help_create
; break;
1064 case MANAGE
: help_text
= Help_manage
; break;
1065 case MISC
: help_text
= Help_misc
; break;
1066 case MONITOR
: help_text
= Help_monitor
; break;
1067 case GROW
: help_text
= Help_grow
; break;
1068 case INCREMENTAL
:help_text
= Help_incr
; break;
1070 fputs(help_text
,stdout
);
1074 if (!mode
&& devs_found
) {
1077 if (devlist
->disposition
== 0)
1078 devlist
->disposition
= devmode
;
1081 fputs(Usage
, stderr
);
1086 struct createinfo
*ci
= conf_get_create_info();
1088 if (strcasecmp(symlinks
, "yes") == 0)
1090 else if (strcasecmp(symlinks
, "no") == 0)
1093 fprintf(stderr
, Name
": option --symlinks must be 'no' or 'yes'\n");
1097 /* Ok, got the option parsing out of the way
1098 * hopefully it's mostly right but there might be some stuff
1101 * That is mosty checked in the per-mode stuff but...
1103 * For @,B,C and A without -s, the first device listed must be an md device
1104 * we check that here and open it.
1107 if (mode
==MANAGE
|| mode
== BUILD
|| mode
== CREATE
|| mode
== GROW
||
1108 (mode
== ASSEMBLE
&& ! scan
)) {
1109 if (devs_found
< 1) {
1110 fprintf(stderr
, Name
": an md device must be given in this mode\n");
1113 if ((int)ident
.super_minor
== -2 && autof
) {
1114 fprintf(stderr
, Name
": --super-minor=dev is incompatible with --auto\n");
1117 if (mode
== MANAGE
|| mode
== GROW
) {
1118 mdfd
= open_mddev(devlist
->devname
, 1);
1122 /* non-existent device is OK */
1123 mdfd
= open_mddev(devlist
->devname
, 0);
1125 fprintf(stderr
, Name
": device %s exists but is not an "
1126 "md array.\n", devlist
->devname
);
1129 if ((int)ident
.super_minor
== -2) {
1132 fprintf(stderr
, Name
": --super-minor=dev given, and "
1133 "listed device %s doesn't exist.\n",
1138 ident
.super_minor
= minor(stb
.st_rdev
);
1140 if (mdfd
>= 0 && mode
!= MANAGE
&& mode
!= GROW
) {
1141 /* We don't really want this open yet, we just might
1142 * have wanted to check some things
1150 if (raiddisks
== 1 && !force
&& level
!= -5) {
1151 fprintf(stderr
, Name
": '1' is an unusual number of drives for an array, so it is probably\n"
1152 " a mistake. If you really mean it you will need to specify --force before\n"
1153 " setting the number of drives.\n");
1158 if (homehost
== NULL
)
1159 homehost
= conf_get_homehost(&require_homehost
);
1160 if (homehost
== NULL
|| strcmp(homehost
, "<system>")==0) {
1161 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
1162 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
1163 homehost
= sys_hostname
;
1167 if ((mode
!= MISC
|| devmode
!= 'E') &&
1169 fprintf(stderr
, Name
": must be super-user to perform this action\n");
1173 ident
.autof
= autof
;
1178 /* readonly, add/remove, readwrite, runstop */
1180 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1181 if (!rv
&& devs_found
>1)
1182 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
1183 devlist
->next
, verbose
-quiet
, test
,
1185 if (!rv
&& readonly
< 0)
1186 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1188 rv
= Manage_runstop(devlist
->devname
, mdfd
, runstop
, quiet
);
1191 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1192 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !scan
) {
1193 /* Only a device has been given, so get details from config file */
1194 struct mddev_ident
*array_ident
= conf_get_ident(devlist
->devname
);
1195 if (array_ident
== NULL
) {
1196 fprintf(stderr
, Name
": %s not identified in config file.\n",
1202 if (array_ident
->autof
== 0)
1203 array_ident
->autof
= autof
;
1204 rv
|= Assemble(ss
, devlist
->devname
, array_ident
,
1205 NULL
, backup_file
, invalid_backup
,
1206 readonly
, runstop
, update
,
1207 homehost
, require_homehost
,
1208 verbose
-quiet
, force
);
1211 rv
= Assemble(ss
, devlist
->devname
, &ident
,
1212 devlist
->next
, backup_file
, invalid_backup
,
1213 readonly
, runstop
, update
,
1214 homehost
, require_homehost
,
1215 verbose
-quiet
, force
);
1216 else if (devs_found
>0) {
1217 if (update
&& devs_found
> 1) {
1218 fprintf(stderr
, Name
": can only update a single array at a time\n");
1221 if (backup_file
&& devs_found
> 1) {
1222 fprintf(stderr
, Name
": can only assemble a single array when providing a backup file.\n");
1225 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1226 struct mddev_ident
*array_ident
= conf_get_ident(dv
->devname
);
1227 if (array_ident
== NULL
) {
1228 fprintf(stderr
, Name
": %s not identified in config file.\n",
1233 if (array_ident
->autof
== 0)
1234 array_ident
->autof
= autof
;
1235 rv
|= Assemble(ss
, dv
->devname
, array_ident
,
1236 NULL
, backup_file
, invalid_backup
,
1237 readonly
, runstop
, update
,
1238 homehost
, require_homehost
,
1239 verbose
-quiet
, force
);
1242 struct mddev_ident
*a
, *array_list
= conf_get_ident(NULL
);
1243 struct mddev_dev
*devlist
= conf_get_devs();
1245 int failures
, successes
;
1246 if (devlist
== NULL
) {
1247 fprintf(stderr
, Name
": No devices listed in conf file were found.\n");
1251 fprintf(stderr
, Name
": --update not meaningful with a --scan assembly.\n");
1255 fprintf(stderr
, Name
": --backup_file not meaningful with a --scan assembly.\n");
1258 for (a
= array_list
; a
; a
= a
->next
) {
1267 for (a
= array_list
; a
; a
= a
->next
) {
1272 strcasecmp(a
->devname
, "<ignore>") == 0)
1275 r
= Assemble(ss
, a
->devname
,
1278 readonly
, runstop
, NULL
,
1279 homehost
, require_homehost
,
1280 verbose
-quiet
, force
);
1289 } while (failures
&& successes
);
1290 if (homehost
&& cnt
== 0) {
1291 /* Maybe we can auto-assemble something.
1292 * Repeatedly call Assemble in auto-assemble mode
1297 ident
.autof
= autof
;
1299 struct mddev_dev
*devlist
= conf_get_devs();
1302 rv2
= Assemble(ss
, NULL
,
1305 readonly
, runstop
, NULL
,
1306 homehost
, require_homehost
,
1307 verbose
-quiet
, force
);
1313 /* found something so even though assembly failed we
1314 * want to avoid auto-updates
1316 auto_update_home
= 0;
1318 /* Incase there are stacked devices, we need to go around again */
1321 if (cnt
== 0 && auto_update_home
&& homehost
) {
1322 /* Nothing found, maybe we need to bootstrap homehost info */
1334 verbose
-quiet
, force
);
1340 /* Incase there are stacked devices, we need to go around again */
1344 if (cnt
== 0 && rv
== 0) {
1345 fprintf(stderr
, Name
": No arrays found in config file or automatically\n");
1349 } else if (cnt
== 0 && rv
== 0) {
1350 fprintf(stderr
, Name
": No arrays found in config file\n");
1356 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1357 if (write_behind
&& !bitmap_file
) {
1358 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1362 if (raiddisks
== 0) {
1363 fprintf(stderr
, Name
": no raid-devices specified.\n");
1369 if (strcmp(bitmap_file
, "internal")==0) {
1370 fprintf(stderr
, Name
": 'internal' bitmaps not supported with --build\n");
1375 rv
= Build(devlist
->devname
, chunk
, level
, layout
,
1376 raiddisks
, devlist
->next
, assume_clean
,
1377 bitmap_file
, bitmap_chunk
, write_behind
,
1378 delay
, verbose
-quiet
, autof
, size
);
1381 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1382 if (write_behind
&& !bitmap_file
) {
1383 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1387 if (raiddisks
== 0) {
1388 fprintf(stderr
, Name
": no raid-devices specified.\n");
1393 rv
= Create(ss
, devlist
->devname
, chunk
, level
, layout
, size
<0 ? 0 : size
,
1394 raiddisks
, sparedisks
, ident
.name
, homehost
,
1395 ident
.uuid_set
? ident
.uuid
: NULL
,
1396 devs_found
-1, devlist
->next
, runstop
, verbose
-quiet
, force
, assume_clean
,
1397 bitmap_file
, bitmap_chunk
, write_behind
, delay
, autof
);
1400 if (devmode
== 'E') {
1401 if (devlist
== NULL
&& !scan
) {
1402 fprintf(stderr
, Name
": No devices to examine\n");
1405 if (devlist
== NULL
)
1406 devlist
= conf_get_devs();
1407 if (devlist
== NULL
) {
1408 fprintf(stderr
, Name
": No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1411 if (brief
&& verbose
)
1413 rv
= Examine(devlist
, scan
?(verbose
>1?0:verbose
+1):brief
,
1415 SparcAdjust
, ss
, homehost
);
1416 } else if (devmode
== DetailPlatform
) {
1417 rv
= Detail_Platform(ss
? ss
->ss
: NULL
, ss
? scan
: 1, verbose
);
1419 if (devlist
== NULL
) {
1420 if ((devmode
=='D' || devmode
== Waitclean
) && scan
) {
1421 /* apply --detail or --wait-clean to
1422 * all devices in /proc/mdstat
1424 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1425 struct mdstat_ent
*e
;
1426 struct map_ent
*map
= NULL
;
1428 int v
= verbose
>1?0:verbose
+1;
1430 for (members
= 0; members
<= 1; members
++) {
1431 for (e
=ms
; e
; e
=e
->next
) {
1434 int member
= e
->metadata_version
&&
1435 strncmp(e
->metadata_version
,
1436 "external:/", 10) == 0;
1437 if (members
!= member
)
1439 me
= map_by_devnum(&map
, e
->devnum
);
1441 && strcmp(me
->path
, "/unknown") != 0)
1444 name
= get_md_name(e
->devnum
);
1447 fprintf(stderr
, Name
": cannot find device file for %s\n",
1452 rv
|= Detail(name
, v
,
1456 rv
|= WaitClean(name
, -1, v
);
1461 } else if (devmode
== 'S' && scan
) {
1462 /* apply --stop to all devices in /proc/mdstat */
1463 /* Due to possible stacking of devices, repeat until
1464 * nothing more can be stopped
1466 int progress
=1, err
;
1469 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1470 struct mdstat_ent
*e
;
1472 if (!progress
) last
= 1;
1473 progress
= 0; err
= 0;
1474 for (e
=ms
; e
; e
=e
->next
) {
1475 char *name
= get_md_name(e
->devnum
);
1478 fprintf(stderr
, Name
": cannot find device file for %s\n",
1482 mdfd
= open_mddev(name
, 1);
1484 if (Manage_runstop(name
, mdfd
, -1, quiet
?1:last
?0:-1))
1494 } while (!last
&& err
);
1497 fprintf(stderr
, Name
": No devices given.\n");
1501 for (dv
=devlist
; dv
; dv
=dv
->next
) {
1502 switch(dv
->disposition
) {
1504 rv
|= Detail(dv
->devname
,
1506 export
, test
, homehost
);
1508 case 'K': /* Zero superblock */
1510 rv
|= Kill(dv
->devname
, ss
, force
, quiet
,0);
1514 rv
|= Kill(dv
->devname
, NULL
, force
, q
, 0);
1521 rv
|= Query(dv
->devname
); continue;
1523 rv
|= ExamineBitmap(dv
->devname
, brief
, ss
); continue;
1526 rv
|= Wait(dv
->devname
); continue;
1528 rv
|= WaitClean(dv
->devname
, -1, verbose
-quiet
); continue;
1530 rv
|= Kill_subarray(dv
->devname
, subarray
, quiet
);
1532 case UpdateSubarray
:
1533 if (update
== NULL
) {
1535 Name
": -U/--update must be specified with --update-subarray\n");
1539 rv
|= Update_subarray(dv
->devname
, subarray
, update
, &ident
, quiet
);
1542 mdfd
= open_mddev(dv
->devname
, 1);
1544 switch(dv
->disposition
) {
1546 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1, quiet
); break;
1548 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1, quiet
); break;
1550 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1552 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;
1561 if (!devlist
&& !scan
) {
1562 fprintf(stderr
, Name
": Cannot monitor: need --scan or at least one device\n");
1566 if (pidfile
&& !daemonise
) {
1567 fprintf(stderr
, Name
": Cannot write a pid file when not in daemon mode\n");
1572 if (get_linux_version() > 2006016)
1573 /* mdstat responds to poll */
1578 rv
= Monitor(devlist
, mailaddr
, program
,
1579 delay
?delay
:60, daemonise
, scan
, oneshot
,
1580 dosyslog
, test
, pidfile
, increments
, spare_sharing
);
1584 if (array_size
>= 0) {
1585 /* alway impose array size first, independent of
1587 * Do not allow level or raid_disks changes at the
1588 * same time as that can be irreversibly destructive.
1592 if (raiddisks
|| level
!= UnSet
) {
1593 fprintf(stderr
, Name
": cannot change array size in same operation "
1594 "as changing raiddisks or level.\n"
1595 " Change size first, then check that data is still intact.\n");
1599 sysfs_init(&sra
, mdfd
, 0);
1600 if (array_size
== 0)
1601 err
= sysfs_set_str(&sra
, NULL
, "array_size", "default");
1603 err
= sysfs_set_num(&sra
, NULL
, "array_size", array_size
/ 2);
1606 fprintf(stderr
, Name
": --array-size setting"
1607 " is too large.\n");
1609 fprintf(stderr
, Name
": current kernel does"
1610 " not support setting --array-size\n");
1615 if (devs_found
> 1) {
1618 if (size
>= 0 || raiddisks
|| chunk
|| layout_str
!= NULL
|| bitmap_file
) {
1619 fprintf(stderr
, Name
": --add cannot be used with other geometry changes in --grow mode\n");
1623 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1624 rv
= Grow_Add_device(devlist
->devname
, mdfd
, dv
->devname
);
1628 } else if (bitmap_file
) {
1629 if (size
>= 0 || raiddisks
|| chunk
|| layout_str
!= NULL
) {
1630 fprintf(stderr
, Name
": --bitmap changes cannot be used with other geometry changes 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 } else if (array_size
< 0)
1644 fprintf(stderr
, Name
": no changes to --grow\n");
1652 fprintf(stderr
, Name
1653 ": --incremental --scan meaningless without --run.\n");
1656 if (devmode
== 'f') {
1657 fprintf(stderr
, Name
1658 ": --incremental --scan --fail not supported.\n");
1661 rv
= IncrementalScan(verbose
);
1664 if (!rebuild_map
&& !scan
) {
1665 fprintf(stderr
, Name
1666 ": --incremental requires a device.\n");
1671 if (devlist
->next
) {
1672 fprintf(stderr
, Name
1673 ": --incremental can only handle one device.\n");
1678 rv
= IncrementalRemove(devlist
->devname
, remove_path
,
1681 rv
= Incremental(devlist
->devname
, verbose
-quiet
,
1682 runstop
, ss
, homehost
,
1683 require_homehost
, autof
);