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
) {
257 /* special case of -c --help */
258 if ((opt
== 'c' || opt
== ConfigFile
) &&
259 ( strncmp(optarg
, "--h", 3)==0 ||
260 strncmp(optarg
, "-h", 2)==0)) {
261 fputs(Help_config
, stdout
);
265 /* If first option is a device, don't force the mode yet */
267 if (devs_found
== 0) {
268 dv
= malloc(sizeof(*dv
));
270 fprintf(stderr
, Name
": malloc failed\n");
273 dv
->devname
= optarg
;
274 dv
->disposition
= devmode
;
275 dv
->writemostly
= writemostly
;
280 devlistend
= &dv
->next
;
285 /* No mode yet, and this is the second device ... */
286 fprintf(stderr
, Name
": An option must be given to set the mode before a second device\n"
287 " (%s) is listed\n", optarg
);
290 if (option_index
>= 0)
291 fprintf(stderr
, Name
": --%s", long_options
[option_index
].name
);
293 fprintf(stderr
, Name
": -%c", opt
);
294 fprintf(stderr
, " does not set the mode, and so cannot be the first option.\n");
298 /* if we just set the mode, then done */
312 /* an undecorated option - must be a device name.
314 if (devs_found
> 0 && mode
== MANAGE
&& !devmode
) {
315 fprintf(stderr
, Name
": Must give one of -a/-r/-f"
316 " for subsequent devices at %s\n", optarg
);
319 if (devs_found
> 0 && mode
== GROW
&& !devmode
) {
320 fprintf(stderr
, Name
": Must give -a/--add for"
321 " devices to add: %s\n", optarg
);
324 dv
= malloc(sizeof(*dv
));
326 fprintf(stderr
, Name
": malloc failed\n");
329 dv
->devname
= optarg
;
330 dv
->disposition
= devmode
;
331 dv
->writemostly
= writemostly
;
336 devlistend
= &dv
->next
;
342 /* We've got a mode, and opt is now something else which
343 * could depend on the mode */
344 #define O(a,b) ((a<<16)|b)
345 switch (O(mode
,opt
)) {
347 case O(GROW
,ChunkSize
):
349 case O(CREATE
,ChunkSize
):
350 case O(BUILD
,'c'): /* chunk or rounding */
351 case O(BUILD
,ChunkSize
): /* chunk or rounding */
353 fprintf(stderr
, Name
": chunk/rounding may only be specified once. "
354 "Second value is %s.\n", optarg
);
357 chunk
= parse_size(optarg
);
358 if (chunk
< 8 || ((chunk
-1)&chunk
)) {
359 fprintf(stderr
, Name
": invalid chunk/rounding value: %s\n",
363 /* Covert sectors to K */
367 case O(INCREMENTAL
, 'e'):
369 case O(ASSEMBLE
,'e'):
370 case O(MISC
,'e'): /* set metadata (superblock) information */
372 fprintf(stderr
, Name
": metadata information already given\n");
375 for(i
=0; !ss
&& superlist
[i
]; i
++)
376 ss
= superlist
[i
]->match_metadata_desc(optarg
);
379 fprintf(stderr
, Name
": unrecognised metadata identifier: %s\n", optarg
);
385 case O(MANAGE
,WriteMostly
):
387 case O(BUILD
,WriteMostly
):
389 case O(CREATE
,WriteMostly
):
390 /* set write-mostly for following devices */
395 /* clear write-mostly for following devices */
402 case O(BUILD
,'z'): /* size */
404 fprintf(stderr
, Name
": size may only be specified once. "
405 "Second value is %s.\n", optarg
);
408 if (strcmp(optarg
, "max")==0)
411 size
= parse_size(optarg
);
413 fprintf(stderr
, Name
": invalid size: %s\n",
417 /* convert sectors to K */
422 case O(GROW
,'Z'): /* array size */
423 if (array_size
>= 0) {
424 fprintf(stderr
, Name
": array-size may only be specified once. "
425 "Second value is %s.\n", optarg
);
428 if (strcmp(optarg
, "max") == 0)
431 array_size
= parse_size(optarg
);
432 if (array_size
<= 0) {
433 fprintf(stderr
, Name
": invalid array size: %s\n",
442 case O(BUILD
,'l'): /* set raid level*/
443 if (level
!= UnSet
) {
444 fprintf(stderr
, Name
": raid level may only be set once. "
445 "Second value is %s.\n", optarg
);
448 level
= map_name(pers
, optarg
);
449 if (level
== UnSet
) {
450 fprintf(stderr
, Name
": invalid raid level: %s\n",
454 if (level
!= 0 && level
!= LEVEL_LINEAR
&& level
!= 1 &&
455 level
!= LEVEL_MULTIPATH
&& level
!= LEVEL_FAULTY
&&
458 fprintf(stderr
, Name
": Raid level %s not permitted with --build.\n",
462 if (sparedisks
> 0 && level
< 1 && level
>= -1) {
463 fprintf(stderr
, Name
": raid level %s is incompatible with spare-devices setting.\n",
470 case O(GROW
, 'p'): /* new layout */
471 case O(GROW
, Layout
):
473 fprintf(stderr
,Name
": layout may only be sent once. "
474 "Second value was %s\n", optarg
);
478 /* 'Grow' will parse the value */
481 case O(CREATE
,'p'): /* raid5 layout */
482 case O(CREATE
,Layout
):
483 case O(BUILD
,'p'): /* faulty layout */
484 case O(BUILD
,Layout
):
485 if (layout
!= UnSet
) {
486 fprintf(stderr
,Name
": layout may only be sent once. "
487 "Second value was %s\n", optarg
);
492 fprintf(stderr
, Name
": layout not meaningful for %s arrays.\n",
493 map_num(pers
, level
));
496 fprintf(stderr
, Name
": raid level must be given before layout.\n");
500 layout
= map_name(r5layout
, optarg
);
502 fprintf(stderr
, Name
": layout %s not understood for raid5.\n",
508 layout
= map_name(r6layout
, optarg
);
510 fprintf(stderr
, Name
": layout %s not understood for raid6.\n",
517 layout
= parse_layout_10(optarg
);
519 fprintf(stderr
, Name
": layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg
);
527 layout
= parse_layout_faulty(optarg
);
529 fprintf(stderr
, Name
": layout %s not understood for faulty.\n",
537 case O(CREATE
,AssumeClean
):
538 case O(BUILD
,AssumeClean
): /* assume clean */
544 case O(BUILD
,'n'): /* number of raid disks */
546 fprintf(stderr
, Name
": raid-devices set twice: %d and %s\n",
550 raiddisks
= strtol(optarg
, &c
, 10);
551 if (!optarg
[0] || *c
|| raiddisks
<=0) {
552 fprintf(stderr
, Name
": invalid number of raid devices: %s\n",
556 ident
.raid_disks
= raiddisks
;
559 case O(CREATE
,'x'): /* number of spare (eXtra) discs */
561 fprintf(stderr
,Name
": spare-devices set twice: %d and %s\n",
565 if (level
!= UnSet
&& level
<= 0 && level
>= -1) {
566 fprintf(stderr
, Name
": spare-devices setting is incompatible with raid level %d\n",
570 sparedisks
= strtol(optarg
, &c
, 10);
571 if (!optarg
[0] || *c
|| sparedisks
< 0) {
572 fprintf(stderr
, Name
": invalid number of spare-devices: %s\n",
582 case O(INCREMENTAL
,'a'):
583 case O(INCREMENTAL
,Auto
):
584 case O(ASSEMBLE
,'a'):
585 case O(ASSEMBLE
,Auto
): /* auto-creation of device node */
586 autof
= parse_auto(optarg
, "--auto flag", 0);
589 case O(CREATE
,Symlinks
):
590 case O(BUILD
,Symlinks
):
591 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
595 case O(BUILD
,'f'): /* force honouring '-n 1' */
596 case O(BUILD
,Force
): /* force honouring '-n 1' */
597 case O(GROW
,'f'): /* ditto */
598 case O(GROW
,Force
): /* ditto */
599 case O(CREATE
,'f'): /* force honouring of device list */
600 case O(CREATE
,Force
): /* force honouring of device list */
601 case O(ASSEMBLE
,'f'): /* force assembly */
602 case O(ASSEMBLE
,Force
): /* force assembly */
603 case O(MISC
,'f'): /* force zero */
604 case O(MISC
,Force
): /* force zero */
608 /* now for the Assemble options */
609 case O(CREATE
,'u'): /* uuid of array */
610 case O(ASSEMBLE
,'u'): /* uuid of array */
611 if (ident
.uuid_set
) {
612 fprintf(stderr
, Name
": uuid cannot be set twice. "
613 "Second value %s.\n", optarg
);
616 if (parse_uuid(optarg
, ident
.uuid
))
619 fprintf(stderr
,Name
": Bad uuid: %s\n", optarg
);
625 case O(ASSEMBLE
,'N'):
628 fprintf(stderr
, Name
": name cannot be set twice. "
629 "Second value %s.\n", optarg
);
632 if (mode
== MISC
&& !subarray
) {
633 fprintf(stderr
, Name
": -N/--name only valid with --update-subarray in misc mode\n");
636 if (strlen(optarg
) > 32) {
637 fprintf(stderr
, Name
": name '%s' is too long, 32 chars max.\n",
641 strcpy(ident
.name
, optarg
);
644 case O(ASSEMBLE
,'m'): /* super-minor for array */
645 case O(ASSEMBLE
,SuperMinor
):
646 if (ident
.super_minor
!= UnSet
) {
647 fprintf(stderr
, Name
": super-minor cannot be set twice. "
648 "Second value: %s.\n", optarg
);
651 if (strcmp(optarg
, "dev")==0)
652 ident
.super_minor
= -2;
654 ident
.super_minor
= strtoul(optarg
, &cp
, 10);
655 if (!optarg
[0] || *cp
) {
656 fprintf(stderr
, Name
": Bad super-minor number: %s.\n", optarg
);
662 case O(ASSEMBLE
,'U'): /* update the superblock */
665 fprintf(stderr
, Name
": Can only update one aspect"
666 " of superblock, both %s and %s given.\n",
670 if (mode
== MISC
&& !subarray
) {
671 fprintf(stderr
, Name
": Only subarrays can be"
672 " updated in misc mode\n");
676 if (strcmp(update
, "sparc2.2")==0)
678 if (strcmp(update
, "super-minor") == 0)
680 if (strcmp(update
, "summaries")==0)
682 if (strcmp(update
, "resync")==0)
684 if (strcmp(update
, "uuid")==0)
686 if (strcmp(update
, "name")==0)
688 if (strcmp(update
, "homehost")==0)
690 if (strcmp(update
, "devicesize")==0)
692 if (strcmp(update
, "no-bitmap")==0)
694 if (strcmp(update
, "byteorder")==0) {
697 Name
": must not set metadata"
698 " type with --update=byteorder.\n");
701 for(i
=0; !ss
&& superlist
[i
]; i
++)
702 ss
= superlist
[i
]->match_metadata_desc(
705 fprintf(stderr
, Name
": INTERNAL ERROR"
706 " cannot find 0.swap\n");
712 if (strcmp(update
,"?") == 0 ||
713 strcmp(update
, "help") == 0) {
715 fprintf(outf
, Name
": ");
719 Name
": '--update=%s' is invalid. ",
722 fprintf(outf
, "Valid --update options are:\n"
723 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
724 " 'summaries', 'homehost', 'byteorder', 'devicesize',\n"
726 exit(outf
== stdout
? 0 : 2);
729 /* update=devicesize is allowed with --re-add */
730 if (devmode
!= 'a' || re_add
!= 1) {
731 fprintf(stderr
, Name
"--update in Manage mode only"
732 " allowed with --re-add.\n");
736 fprintf(stderr
, Name
": Can only update one aspect"
737 " of superblock, both %s and %s given.\n",
742 if (strcmp(update
, "devicesize") != 0) {
743 fprintf(stderr
, Name
": only 'devicesize' can be"
744 " updated with --re-add\n");
749 case O(INCREMENTAL
,NoDegraded
):
750 fprintf(stderr
, Name
": --no-degraded is deprecated in Incremental mode\n");
751 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
752 runstop
= -1; /* --stop isn't allowed for --assemble,
753 * so we overload slightly */
756 case O(ASSEMBLE
,'c'):
757 case O(ASSEMBLE
,ConfigFile
):
758 case O(INCREMENTAL
, 'c'):
759 case O(INCREMENTAL
, ConfigFile
):
761 case O(MISC
, ConfigFile
):
763 case O(MONITOR
,ConfigFile
):
765 fprintf(stderr
, Name
": configfile cannot be set twice. "
766 "Second value is %s.\n", optarg
);
770 set_conffile(configfile
);
771 /* FIXME possibly check that config file exists. Even parse it */
773 case O(ASSEMBLE
,'s'): /* scan */
776 case O(INCREMENTAL
,'s'):
780 case O(MONITOR
,'m'): /* mail address */
781 case O(MONITOR
,EMail
):
783 fprintf(stderr
, Name
": only specify one mailaddress. %s ignored.\n",
789 case O(MONITOR
,'p'): /* alert program */
790 case O(MONITOR
,ProgramOpt
): /* alert program */
792 fprintf(stderr
, Name
": only specify one alter program. %s ignored.\n",
798 case O(MONITOR
,'r'): /* rebuild increments */
799 case O(MONITOR
,Increment
):
800 increments
= atoi(optarg
);
801 if (increments
>99 || increments
<1) {
802 fprintf(stderr
, Name
": please specify positive integer between 1 and 99 as rebuild increments.\n");
807 case O(MONITOR
,'d'): /* delay in seconds */
809 case O(BUILD
,'d'): /* delay for bitmap updates */
812 fprintf(stderr
, Name
": only specify delay once. %s ignored.\n",
815 delay
= strtol(optarg
, &c
, 10);
816 if (!optarg
[0] || *c
|| delay
<1) {
817 fprintf(stderr
, Name
": invalid delay: %s\n",
823 case O(MONITOR
,'f'): /* daemonise */
824 case O(MONITOR
,Fork
):
827 case O(MONITOR
,'i'): /* pid */
829 fprintf(stderr
, Name
": only specify one pid file. %s ignored.\n",
834 case O(MONITOR
,'1'): /* oneshot */
837 case O(MONITOR
,'t'): /* test */
840 case O(MONITOR
,'y'): /* log messages to syslog */
841 openlog("mdadm", LOG_PID
, SYSLOG_FACILITY
);
844 case O(MONITOR
, NoSharing
):
847 /* now the general management options. Some are applicable
848 * to other modes. None have arguments.
853 case O(MANAGE
,Add
): /* add a drive */
857 case O(MANAGE
,ReAdd
):
861 case O(MANAGE
,'r'): /* remove a drive */
862 case O(MANAGE
,Remove
):
865 case O(MANAGE
,'f'): /* set faulty */
867 case O(INCREMENTAL
,'f'):
868 case O(INCREMENTAL
,Remove
):
869 case O(INCREMENTAL
,Fail
): /* r for incremental is taken, use f
870 * even though we will both fail and
871 * remove the device */
874 case O(INCREMENTAL
,'R'):
876 case O(ASSEMBLE
,'R'):
878 case O(CREATE
,'R'): /* Run the array */
880 fprintf(stderr
, Name
": Cannot both Stop and Run an array\n");
887 fprintf(stderr
, Name
": Cannot both Run and Stop an array\n");
906 case O(MISC
, WaitOpt
):
907 case O(MISC
, Waitclean
):
908 case O(MISC
, DetailPlatform
):
909 case O(MISC
, KillSubarray
):
910 case O(MISC
, UpdateSubarray
):
911 if (devmode
&& devmode
!= opt
&&
912 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
913 fprintf(stderr
, Name
": --examine/-E cannot be given with ");
914 if (devmode
== 'E') {
915 if (option_index
>= 0)
916 fprintf(stderr
, "--%s\n",
917 long_options
[option_index
].name
);
919 fprintf(stderr
, "-%c\n", opt
);
920 } else if (isalpha(devmode
))
921 fprintf(stderr
, "-%c\n", devmode
);
923 fprintf(stderr
, "previous option\n");
928 case O(MISC
, UdevRules
):
929 if (devmode
&& devmode
!= opt
) {
930 fprintf(stderr
, Name
": --udev-rules must"
931 " be the only option.\n");
934 fprintf(stderr
, Name
": only specify one udev "
935 "rule filename. %s ignored.\n",
938 udev_filename
= optarg
;
946 case O(MISC
, Sparc22
):
947 if (devmode
!= 'E') {
948 fprintf(stderr
, Name
": --sparc2.2 only allowed with --examine\n");
954 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
955 case O(ASSEMBLE
,Bitmap
):
957 fprintf(stderr
, Name
": bitmap file needed with -b in --assemble mode\n");
960 if (strcmp(optarg
, "internal")==0) {
961 fprintf(stderr
, Name
": there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
964 bitmap_fd
= open(optarg
, O_RDWR
);
965 if (!*optarg
|| bitmap_fd
< 0) {
966 fprintf(stderr
, Name
": cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
969 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
972 case O(ASSEMBLE
, BackupFile
):
973 case O(GROW
, BackupFile
):
974 /* Specify a file into which grow might place a backup,
975 * or from which assemble might recover a backup
978 fprintf(stderr
, Name
": backup file already specified, rejecting %s\n", optarg
);
981 backup_file
= optarg
;
984 case O(ASSEMBLE
, InvalidBackup
):
985 /* Acknowledge that the backupfile is invalid, but ask
992 case O(BUILD
,Bitmap
):
994 case O(CREATE
,Bitmap
): /* here we create the bitmap */
995 if (strcmp(optarg
, "none") == 0) {
996 fprintf(stderr
, Name
": '--bitmap none' only"
997 " support for --grow\n");
1002 case O(GROW
,Bitmap
):
1003 if (strcmp(optarg
, "internal")== 0 ||
1004 strcmp(optarg
, "none")== 0 ||
1005 strchr(optarg
, '/') != NULL
) {
1006 bitmap_file
= optarg
;
1010 fprintf(stderr
, Name
": bitmap file must contain a '/', or be 'internal', or 'none'\n"
1011 " not '%s'\n", optarg
);
1014 case O(GROW
,BitmapChunk
):
1015 case O(BUILD
,BitmapChunk
):
1016 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
1017 bitmap_chunk
= parse_size(optarg
);
1018 if (bitmap_chunk
< 0 ||
1019 bitmap_chunk
& (bitmap_chunk
- 1)) {
1021 Name
": invalid bitmap chunksize: %s\n",
1025 /* convert sectors to B, chunk of 0 means 512B */
1026 bitmap_chunk
= bitmap_chunk
? bitmap_chunk
* 512 : 512;
1029 case O(GROW
, WriteBehind
):
1030 case O(BUILD
, WriteBehind
):
1031 case O(CREATE
, WriteBehind
): /* write-behind mode */
1032 write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
1034 write_behind
= strtol(optarg
, &c
, 10);
1035 if (write_behind
< 0 || *c
||
1036 write_behind
> 16383) {
1037 fprintf(stderr
, Name
": Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
1043 case O(INCREMENTAL
, 'r'):
1044 case O(INCREMENTAL
, RebuildMapOpt
):
1047 case O(INCREMENTAL
, IncrementalPath
):
1048 remove_path
= optarg
;
1051 /* We have now processed all the valid options. Anything else is
1054 if (option_index
> 0)
1055 fprintf(stderr
, Name
":option --%s not valid in %s mode\n",
1056 long_options
[option_index
].name
,
1057 map_num(modes
, mode
));
1059 fprintf(stderr
, Name
": option -%c not valid in %s mode\n",
1060 opt
, map_num(modes
, mode
));
1066 char *help_text
= Help
;
1067 if (print_help
== 2)
1068 help_text
= OptionHelp
;
1071 case ASSEMBLE
: help_text
= Help_assemble
; break;
1072 case BUILD
: help_text
= Help_build
; break;
1073 case CREATE
: help_text
= Help_create
; break;
1074 case MANAGE
: help_text
= Help_manage
; break;
1075 case MISC
: help_text
= Help_misc
; break;
1076 case MONITOR
: help_text
= Help_monitor
; break;
1077 case GROW
: help_text
= Help_grow
; break;
1078 case INCREMENTAL
:help_text
= Help_incr
; break;
1080 fputs(help_text
,stdout
);
1084 if (!mode
&& devs_found
) {
1087 if (devlist
->disposition
== 0)
1088 devlist
->disposition
= devmode
;
1091 fputs(Usage
, stderr
);
1096 struct createinfo
*ci
= conf_get_create_info();
1098 if (strcasecmp(symlinks
, "yes") == 0)
1100 else if (strcasecmp(symlinks
, "no") == 0)
1103 fprintf(stderr
, Name
": option --symlinks must be 'no' or 'yes'\n");
1107 /* Ok, got the option parsing out of the way
1108 * hopefully it's mostly right but there might be some stuff
1111 * That is mosty checked in the per-mode stuff but...
1113 * For @,B,C and A without -s, the first device listed must be an md device
1114 * we check that here and open it.
1117 if (mode
==MANAGE
|| mode
== BUILD
|| mode
== CREATE
|| mode
== GROW
||
1118 (mode
== ASSEMBLE
&& ! scan
)) {
1119 if (devs_found
< 1) {
1120 fprintf(stderr
, Name
": an md device must be given in this mode\n");
1123 if ((int)ident
.super_minor
== -2 && autof
) {
1124 fprintf(stderr
, Name
": --super-minor=dev is incompatible with --auto\n");
1127 if (mode
== MANAGE
|| mode
== GROW
) {
1128 mdfd
= open_mddev(devlist
->devname
, 1);
1132 /* non-existent device is OK */
1133 mdfd
= open_mddev(devlist
->devname
, 0);
1135 fprintf(stderr
, Name
": device %s exists but is not an "
1136 "md array.\n", devlist
->devname
);
1139 if ((int)ident
.super_minor
== -2) {
1142 fprintf(stderr
, Name
": --super-minor=dev given, and "
1143 "listed device %s doesn't exist.\n",
1148 ident
.super_minor
= minor(stb
.st_rdev
);
1150 if (mdfd
>= 0 && mode
!= MANAGE
&& mode
!= GROW
) {
1151 /* We don't really want this open yet, we just might
1152 * have wanted to check some things
1160 if (raiddisks
== 1 && !force
&& level
!= -5) {
1161 fprintf(stderr
, Name
": '1' is an unusual number of drives for an array, so it is probably\n"
1162 " a mistake. If you really mean it you will need to specify --force before\n"
1163 " setting the number of drives.\n");
1168 if (homehost
== NULL
)
1169 homehost
= conf_get_homehost(&require_homehost
);
1170 if (homehost
== NULL
|| strcmp(homehost
, "<system>")==0) {
1171 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
1172 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
1173 homehost
= sys_hostname
;
1177 if ((mode
!= MISC
|| devmode
!= 'E') &&
1179 fprintf(stderr
, Name
": must be super-user to perform this action\n");
1183 ident
.autof
= autof
;
1188 /* readonly, add/remove, readwrite, runstop */
1190 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1191 if (!rv
&& devs_found
>1)
1192 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
1193 devlist
->next
, verbose
-quiet
, test
,
1195 if (!rv
&& readonly
< 0)
1196 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1198 rv
= Manage_runstop(devlist
->devname
, mdfd
, runstop
, quiet
);
1201 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1202 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !scan
) {
1203 /* Only a device has been given, so get details from config file */
1204 struct mddev_ident
*array_ident
= conf_get_ident(devlist
->devname
);
1205 if (array_ident
== NULL
) {
1206 fprintf(stderr
, Name
": %s not identified in config file.\n",
1212 if (array_ident
->autof
== 0)
1213 array_ident
->autof
= autof
;
1214 rv
|= Assemble(ss
, devlist
->devname
, array_ident
,
1215 NULL
, backup_file
, invalid_backup
,
1216 readonly
, runstop
, update
,
1217 homehost
, require_homehost
,
1218 verbose
-quiet
, force
);
1221 rv
= Assemble(ss
, devlist
->devname
, &ident
,
1222 devlist
->next
, backup_file
, invalid_backup
,
1223 readonly
, runstop
, update
,
1224 homehost
, require_homehost
,
1225 verbose
-quiet
, force
);
1226 else if (devs_found
>0) {
1227 if (update
&& devs_found
> 1) {
1228 fprintf(stderr
, Name
": can only update a single array at a time\n");
1231 if (backup_file
&& devs_found
> 1) {
1232 fprintf(stderr
, Name
": can only assemble a single array when providing a backup file.\n");
1235 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1236 struct mddev_ident
*array_ident
= conf_get_ident(dv
->devname
);
1237 if (array_ident
== NULL
) {
1238 fprintf(stderr
, Name
": %s not identified in config file.\n",
1243 if (array_ident
->autof
== 0)
1244 array_ident
->autof
= autof
;
1245 rv
|= Assemble(ss
, dv
->devname
, array_ident
,
1246 NULL
, backup_file
, invalid_backup
,
1247 readonly
, runstop
, update
,
1248 homehost
, require_homehost
,
1249 verbose
-quiet
, force
);
1252 struct mddev_ident
*a
, *array_list
= conf_get_ident(NULL
);
1253 struct mddev_dev
*devlist
= conf_get_devs();
1255 int failures
, successes
;
1256 if (devlist
== NULL
) {
1257 fprintf(stderr
, Name
": No devices listed in conf file were found.\n");
1261 fprintf(stderr
, Name
": --update not meaningful with a --scan assembly.\n");
1265 fprintf(stderr
, Name
": --backup_file not meaningful with a --scan assembly.\n");
1268 for (a
= array_list
; a
; a
= a
->next
) {
1277 for (a
= array_list
; a
; a
= a
->next
) {
1282 strcasecmp(a
->devname
, "<ignore>") == 0)
1285 r
= Assemble(ss
, a
->devname
,
1288 readonly
, runstop
, NULL
,
1289 homehost
, require_homehost
,
1290 verbose
-quiet
, force
);
1299 } while (failures
&& successes
);
1300 if (homehost
&& cnt
== 0) {
1301 /* Maybe we can auto-assemble something.
1302 * Repeatedly call Assemble in auto-assemble mode
1307 ident
.autof
= autof
;
1309 struct mddev_dev
*devlist
= conf_get_devs();
1312 rv2
= Assemble(ss
, NULL
,
1315 readonly
, runstop
, NULL
,
1316 homehost
, require_homehost
,
1317 verbose
-quiet
, force
);
1323 /* Incase there are stacked devices, we need to go around again */
1325 if (cnt
== 0 && rv
== 0) {
1326 fprintf(stderr
, Name
": No arrays found in config file or automatically\n");
1330 } else if (cnt
== 0 && rv
== 0) {
1331 fprintf(stderr
, Name
": No arrays found in config file\n");
1337 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1338 if (write_behind
&& !bitmap_file
) {
1339 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1343 if (raiddisks
== 0) {
1344 fprintf(stderr
, Name
": no raid-devices specified.\n");
1350 if (strcmp(bitmap_file
, "internal")==0) {
1351 fprintf(stderr
, Name
": 'internal' bitmaps not supported with --build\n");
1356 rv
= Build(devlist
->devname
, chunk
, level
, layout
,
1357 raiddisks
, devlist
->next
, assume_clean
,
1358 bitmap_file
, bitmap_chunk
, write_behind
,
1359 delay
, verbose
-quiet
, autof
, size
);
1362 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1363 if (write_behind
&& !bitmap_file
) {
1364 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1368 if (raiddisks
== 0) {
1369 fprintf(stderr
, Name
": no raid-devices specified.\n");
1374 rv
= Create(ss
, devlist
->devname
, chunk
, level
, layout
, size
<0 ? 0 : size
,
1375 raiddisks
, sparedisks
, ident
.name
, homehost
,
1376 ident
.uuid_set
? ident
.uuid
: NULL
,
1377 devs_found
-1, devlist
->next
, runstop
, verbose
-quiet
, force
, assume_clean
,
1378 bitmap_file
, bitmap_chunk
, write_behind
, delay
, autof
);
1381 if (devmode
== 'E') {
1382 if (devlist
== NULL
&& !scan
) {
1383 fprintf(stderr
, Name
": No devices to examine\n");
1386 if (devlist
== NULL
)
1387 devlist
= conf_get_devs();
1388 if (devlist
== NULL
) {
1389 fprintf(stderr
, Name
": No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1392 if (brief
&& verbose
)
1394 rv
= Examine(devlist
, scan
?(verbose
>1?0:verbose
+1):brief
,
1396 SparcAdjust
, ss
, homehost
);
1397 } else if (devmode
== DetailPlatform
) {
1398 rv
= Detail_Platform(ss
? ss
->ss
: NULL
, ss
? scan
: 1, verbose
);
1400 if (devlist
== NULL
) {
1401 if ((devmode
=='D' || devmode
== Waitclean
) && scan
) {
1402 /* apply --detail or --wait-clean to
1403 * all devices in /proc/mdstat
1405 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1406 struct mdstat_ent
*e
;
1407 struct map_ent
*map
= NULL
;
1409 int v
= verbose
>1?0:verbose
+1;
1411 for (members
= 0; members
<= 1; members
++) {
1412 for (e
=ms
; e
; e
=e
->next
) {
1415 int member
= e
->metadata_version
&&
1416 strncmp(e
->metadata_version
,
1417 "external:/", 10) == 0;
1418 if (members
!= member
)
1420 me
= map_by_devnum(&map
, e
->devnum
);
1422 && strcmp(me
->path
, "/unknown") != 0)
1425 name
= get_md_name(e
->devnum
);
1428 fprintf(stderr
, Name
": cannot find device file for %s\n",
1433 rv
|= Detail(name
, v
,
1437 rv
|= WaitClean(name
, -1, v
);
1442 } else if (devmode
== 'S' && scan
) {
1443 /* apply --stop to all devices in /proc/mdstat */
1444 /* Due to possible stacking of devices, repeat until
1445 * nothing more can be stopped
1447 int progress
=1, err
;
1450 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1451 struct mdstat_ent
*e
;
1453 if (!progress
) last
= 1;
1454 progress
= 0; err
= 0;
1455 for (e
=ms
; e
; e
=e
->next
) {
1456 char *name
= get_md_name(e
->devnum
);
1459 fprintf(stderr
, Name
": cannot find device file for %s\n",
1463 mdfd
= open_mddev(name
, 1);
1465 if (Manage_runstop(name
, mdfd
, -1, quiet
?1:last
?0:-1))
1475 } while (!last
&& err
);
1477 } else if (devmode
== UdevRules
) {
1478 rv
= Write_rules(udev_filename
);
1480 fprintf(stderr
, Name
": No devices given.\n");
1484 for (dv
=devlist
; dv
; dv
=dv
->next
) {
1485 switch(dv
->disposition
) {
1487 rv
|= Detail(dv
->devname
,
1489 export
, test
, homehost
);
1491 case 'K': /* Zero superblock */
1493 rv
|= Kill(dv
->devname
, ss
, force
, quiet
,0);
1497 rv
|= Kill(dv
->devname
, NULL
, force
, q
, 0);
1504 rv
|= Query(dv
->devname
); continue;
1506 rv
|= ExamineBitmap(dv
->devname
, brief
, ss
); continue;
1509 rv
|= Wait(dv
->devname
); continue;
1511 rv
|= WaitClean(dv
->devname
, -1, verbose
-quiet
); continue;
1513 rv
|= Kill_subarray(dv
->devname
, subarray
, quiet
);
1515 case UpdateSubarray
:
1516 if (update
== NULL
) {
1518 Name
": -U/--update must be specified with --update-subarray\n");
1522 rv
|= Update_subarray(dv
->devname
, subarray
, update
, &ident
, quiet
);
1525 mdfd
= open_mddev(dv
->devname
, 1);
1527 switch(dv
->disposition
) {
1529 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1, quiet
); break;
1531 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1, quiet
); break;
1533 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1535 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;
1544 if (!devlist
&& !scan
) {
1545 fprintf(stderr
, Name
": Cannot monitor: need --scan or at least one device\n");
1549 if (pidfile
&& !daemonise
) {
1550 fprintf(stderr
, Name
": Cannot write a pid file when not in daemon mode\n");
1555 if (get_linux_version() > 2006016)
1556 /* mdstat responds to poll */
1561 rv
= Monitor(devlist
, mailaddr
, program
,
1562 delay
?delay
:60, daemonise
, scan
, oneshot
,
1563 dosyslog
, test
, pidfile
, increments
, spare_sharing
);
1567 if (array_size
>= 0) {
1568 /* alway impose array size first, independent of
1570 * Do not allow level or raid_disks changes at the
1571 * same time as that can be irreversibly destructive.
1575 if (raiddisks
|| level
!= UnSet
) {
1576 fprintf(stderr
, Name
": cannot change array size in same operation "
1577 "as changing raiddisks or level.\n"
1578 " Change size first, then check that data is still intact.\n");
1582 sysfs_init(&sra
, mdfd
, 0);
1583 if (array_size
== 0)
1584 err
= sysfs_set_str(&sra
, NULL
, "array_size", "default");
1586 err
= sysfs_set_num(&sra
, NULL
, "array_size", array_size
/ 2);
1589 fprintf(stderr
, Name
": --array-size setting"
1590 " is too large.\n");
1592 fprintf(stderr
, Name
": current kernel does"
1593 " not support setting --array-size\n");
1598 if (devs_found
> 1 && raiddisks
== 0) {
1600 if (size
>= 0 || chunk
|| layout_str
!= NULL
|| bitmap_file
) {
1601 fprintf(stderr
, Name
": --add cannot be used with "
1602 "other geometry changes in --grow mode\n");
1606 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1607 rv
= Grow_Add_device(devlist
->devname
, mdfd
,
1612 } else if (bitmap_file
) {
1613 if (size
>= 0 || raiddisks
|| chunk
||
1614 layout_str
!= NULL
|| devs_found
> 1) {
1615 fprintf(stderr
, Name
": --bitmap changes cannot be "
1616 "used with other geometry changes "
1617 "in --grow mode\n");
1622 delay
= DEFAULT_BITMAP_DELAY
;
1623 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, bitmap_file
,
1624 bitmap_chunk
, delay
, write_behind
, force
);
1625 } else if (size
>= 0 || raiddisks
!= 0 || layout_str
!= NULL
1626 || chunk
!= 0 || level
!= UnSet
) {
1627 rv
= Grow_reshape(devlist
->devname
, mdfd
, quiet
, backup_file
,
1628 size
, level
, layout_str
, chunk
, raiddisks
,
1631 } else if (array_size
< 0)
1632 fprintf(stderr
, Name
": no changes to --grow\n");
1640 fprintf(stderr
, Name
1641 ": --incremental --scan meaningless without --run.\n");
1644 if (devmode
== 'f') {
1645 fprintf(stderr
, Name
1646 ": --incremental --scan --fail not supported.\n");
1649 rv
= IncrementalScan(verbose
);
1652 if (!rebuild_map
&& !scan
) {
1653 fprintf(stderr
, Name
1654 ": --incremental requires a device.\n");
1659 if (devlist
->next
) {
1660 fprintf(stderr
, Name
1661 ": --incremental can only handle one device.\n");
1666 rv
= IncrementalRemove(devlist
->devname
, remove_path
,
1669 rv
= Incremental(devlist
->devname
, verbose
-quiet
,
1670 runstop
, ss
, homehost
,
1671 require_homehost
, autof
);