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
;
49 int max_disks
= MD_SB_DISKS
; /* just a default */
51 struct mddev_ident ident
;
52 char *configfile
= NULL
;
61 char *bitmap_file
= NULL
;
62 char *backup_file
= NULL
;
63 int invalid_backup
= 0;
64 int bitmap_chunk
= UnSet
;
66 struct mddev_dev
*devlist
= NULL
;
67 struct mddev_dev
**devlistend
= & devlist
;
77 char *symlinks
= NULL
;
78 /* autof indicates whether and how to create device node.
79 * bottom 3 bits are style. Rest (when shifted) are number of parts
81 * 1 - don't create (no)
82 * 2 - if is_standard, then create (yes)
83 * 3 - create as 'md' - reject is_standard mdp (md)
84 * 4 - create as 'mdp' - reject is_standard md (mdp)
85 * 5 - default to md if not is_standard (md in config file)
86 * 6 - default to mdp if not is_standard (part, or mdp in config file)
90 char *homehost
= NULL
;
91 char sys_hostname
[256];
92 int require_homehost
= 1;
93 char *mailaddr
= NULL
;
100 int spare_sharing
= 1;
101 struct supertype
*ss
= NULL
;
104 char *shortopt
= short_options
;
107 int auto_update_home
= 0;
108 char *subarray
= NULL
;
109 char *remove_path
= NULL
;
116 srandom(time(0) ^ getpid());
120 ident
.raid_disks
= UnSet
;
121 ident
.super_minor
= UnSet
;
123 ident
.spare_group
= NULL
;
126 ident
.bitmap_fd
= -1;
127 ident
.bitmap_file
= NULL
;
129 ident
.container
= NULL
;
132 while ((option_index
= -1) ,
133 (opt
=getopt_long(argc
, argv
,
134 shortopt
, long_options
,
135 &option_index
)) != -1) {
137 /* firstly, some mode-independent options */
147 fputs(Version
, stderr
);
157 if (mode
== ASSEMBLE
|| mode
== BUILD
|| mode
== CREATE
158 || mode
== GROW
|| mode
== INCREMENTAL
160 break; /* b means bitmap */
169 if (strcasecmp(optarg
, "<ignore>") == 0)
170 require_homehost
= 0;
177 fputs(Usage
, stderr
);
180 /* second, figure out the mode.
181 * Some options force the mode. Others
182 * set the mode if it isn't already
188 shortopt
= short_bitmap_options
;
196 case ReAdd
: /* re-add */
199 shortopt
= short_bitmap_options
;
203 case 'A': newmode
= ASSEMBLE
; shortopt
= short_bitmap_auto_options
; break;
204 case 'B': newmode
= BUILD
; shortopt
= short_bitmap_auto_options
; break;
205 case 'C': newmode
= CREATE
; shortopt
= short_bitmap_auto_options
; break;
206 case 'F': newmode
= MONITOR
;break;
207 case 'G': newmode
= GROW
;
208 shortopt
= short_bitmap_options
;
210 case 'I': newmode
= INCREMENTAL
;
211 shortopt
= short_bitmap_auto_options
; break;
213 newmode
= AUTODETECT
; break;
219 case 'Q': newmode
= MISC
; break;
230 if (opt
== KillSubarray
|| opt
== UpdateSubarray
) {
232 fprintf(stderr
, Name
": subarray can only"
233 " be specified once\n");
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 */
368 case O(ASSEMBLE
,AutoHomeHost
):
369 auto_update_home
= 1;
372 case O(INCREMENTAL
, 'e'):
374 case O(ASSEMBLE
,'e'):
375 case O(MISC
,'e'): /* set metadata (superblock) information */
377 fprintf(stderr
, Name
": metadata information already given\n");
380 for(i
=0; !ss
&& superlist
[i
]; i
++)
381 ss
= superlist
[i
]->match_metadata_desc(optarg
);
384 fprintf(stderr
, Name
": unrecognised metadata identifier: %s\n", optarg
);
387 max_disks
= ss
->max_devs
;
391 case O(MANAGE
,WriteMostly
):
393 case O(BUILD
,WriteMostly
):
395 case O(CREATE
,WriteMostly
):
396 /* set write-mostly for following devices */
401 /* clear write-mostly for following devices */
408 case O(BUILD
,'z'): /* size */
410 fprintf(stderr
, Name
": size may only be specified once. "
411 "Second value is %s.\n", optarg
);
414 if (strcmp(optarg
, "max")==0)
417 size
= parse_size(optarg
);
419 fprintf(stderr
, Name
": invalid size: %s\n",
423 /* convert sectors to K */
428 case O(GROW
,'Z'): /* array size */
429 if (array_size
>= 0) {
430 fprintf(stderr
, Name
": array-size may only be specified once. "
431 "Second value is %s.\n", optarg
);
434 if (strcmp(optarg
, "max") == 0)
437 array_size
= parse_size(optarg
);
438 if (array_size
<= 0) {
439 fprintf(stderr
, Name
": invalid array size: %s\n",
448 case O(BUILD
,'l'): /* set raid level*/
449 if (level
!= UnSet
) {
450 fprintf(stderr
, Name
": raid level may only be set once. "
451 "Second value is %s.\n", optarg
);
454 level
= map_name(pers
, optarg
);
455 if (level
== UnSet
) {
456 fprintf(stderr
, Name
": invalid raid level: %s\n",
460 if (level
!= 0 && level
!= LEVEL_LINEAR
&& level
!= 1 &&
461 level
!= LEVEL_MULTIPATH
&& level
!= LEVEL_FAULTY
&&
464 fprintf(stderr
, Name
": Raid level %s not permitted with --build.\n",
468 if (sparedisks
> 0 && level
< 1 && level
>= -1) {
469 fprintf(stderr
, Name
": raid level %s is incompatible with spare-devices setting.\n",
476 case O(GROW
, 'p'): /* new layout */
477 case O(GROW
, Layout
):
479 fprintf(stderr
,Name
": layout may only be sent once. "
480 "Second value was %s\n", optarg
);
484 /* 'Grow' will parse the value */
487 case O(CREATE
,'p'): /* raid5 layout */
488 case O(CREATE
,Layout
):
489 case O(BUILD
,'p'): /* faulty layout */
490 case O(BUILD
,Layout
):
491 if (layout
!= UnSet
) {
492 fprintf(stderr
,Name
": layout may only be sent once. "
493 "Second value was %s\n", optarg
);
498 fprintf(stderr
, Name
": layout not meaningful for %s arrays.\n",
499 map_num(pers
, level
));
502 fprintf(stderr
, Name
": raid level must be given before layout.\n");
506 layout
= map_name(r5layout
, optarg
);
508 fprintf(stderr
, Name
": layout %s not understood for raid5.\n",
514 layout
= map_name(r6layout
, optarg
);
516 fprintf(stderr
, Name
": layout %s not understood for raid6.\n",
523 layout
= parse_layout_10(optarg
);
525 fprintf(stderr
, Name
": layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg
);
533 layout
= parse_layout_faulty(optarg
);
535 fprintf(stderr
, Name
": layout %s not understood for faulty.\n",
543 case O(CREATE
,AssumeClean
):
544 case O(BUILD
,AssumeClean
): /* assume clean */
550 case O(BUILD
,'n'): /* number of raid disks */
552 fprintf(stderr
, Name
": raid-devices set twice: %d and %s\n",
556 raiddisks
= strtol(optarg
, &c
, 10);
557 if (!optarg
[0] || *c
|| raiddisks
<=0) {
558 fprintf(stderr
, Name
": invalid number of raid devices: %s\n",
562 ident
.raid_disks
= raiddisks
;
565 case O(CREATE
,'x'): /* number of spare (eXtra) discs */
567 fprintf(stderr
,Name
": spare-devices set twice: %d and %s\n",
571 if (level
!= UnSet
&& level
<= 0 && level
>= -1) {
572 fprintf(stderr
, Name
": spare-devices setting is incompatible with raid level %d\n",
576 sparedisks
= strtol(optarg
, &c
, 10);
577 if (!optarg
[0] || *c
|| sparedisks
< 0) {
578 fprintf(stderr
, Name
": invalid number of spare-devices: %s\n",
588 case O(INCREMENTAL
,'a'):
589 case O(INCREMENTAL
,Auto
):
590 case O(ASSEMBLE
,'a'):
591 case O(ASSEMBLE
,Auto
): /* auto-creation of device node */
592 autof
= parse_auto(optarg
, "--auto flag", 0);
595 case O(CREATE
,Symlinks
):
596 case O(BUILD
,Symlinks
):
597 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
601 case O(BUILD
,'f'): /* force honouring '-n 1' */
602 case O(BUILD
,Force
): /* force honouring '-n 1' */
603 case O(GROW
,'f'): /* ditto */
604 case O(GROW
,Force
): /* ditto */
605 case O(CREATE
,'f'): /* force honouring of device list */
606 case O(CREATE
,Force
): /* force honouring of device list */
607 case O(ASSEMBLE
,'f'): /* force assembly */
608 case O(ASSEMBLE
,Force
): /* force assembly */
609 case O(MISC
,'f'): /* force zero */
610 case O(MISC
,Force
): /* force zero */
614 /* now for the Assemble options */
615 case O(CREATE
,'u'): /* uuid of array */
616 case O(ASSEMBLE
,'u'): /* uuid of array */
617 if (ident
.uuid_set
) {
618 fprintf(stderr
, Name
": uuid cannot be set twice. "
619 "Second value %s.\n", optarg
);
622 if (parse_uuid(optarg
, ident
.uuid
))
625 fprintf(stderr
,Name
": Bad uuid: %s\n", optarg
);
631 case O(ASSEMBLE
,'N'):
634 fprintf(stderr
, Name
": name cannot be set twice. "
635 "Second value %s.\n", optarg
);
638 if (mode
== MISC
&& !subarray
) {
639 fprintf(stderr
, Name
": -N/--name only valid with --update-subarray in misc mode\n");
642 if (strlen(optarg
) > 32) {
643 fprintf(stderr
, Name
": name '%s' is too long, 32 chars max.\n",
647 strcpy(ident
.name
, optarg
);
650 case O(ASSEMBLE
,'m'): /* super-minor for array */
651 case O(ASSEMBLE
,SuperMinor
):
652 if (ident
.super_minor
!= UnSet
) {
653 fprintf(stderr
, Name
": super-minor cannot be set twice. "
654 "Second value: %s.\n", optarg
);
657 if (strcmp(optarg
, "dev")==0)
658 ident
.super_minor
= -2;
660 ident
.super_minor
= strtoul(optarg
, &cp
, 10);
661 if (!optarg
[0] || *cp
) {
662 fprintf(stderr
, Name
": Bad super-minor number: %s.\n", optarg
);
668 case O(ASSEMBLE
,'U'): /* update the superblock */
671 fprintf(stderr
, Name
": Can only update one aspect of superblock, both %s and %s given.\n",
675 if (mode
== MISC
&& !subarray
) {
676 fprintf(stderr
, Name
": Only subarrays can be 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) {
700 fprintf(stderr
, Name
": must not set metadata type with --update=byteorder.\n");
703 for(i
=0; !ss
&& superlist
[i
]; i
++)
704 ss
= superlist
[i
]->match_metadata_desc("0.swap");
706 fprintf(stderr
, Name
": INTERNAL ERROR 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);
728 case O(INCREMENTAL
,NoDegraded
):
729 fprintf(stderr
, Name
": --no-degraded is deprecated in Incremental mode\n");
730 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
731 runstop
= -1; /* --stop isn't allowed for --assemble,
732 * so we overload slightly */
735 case O(ASSEMBLE
,'c'):
736 case O(ASSEMBLE
,ConfigFile
):
737 case O(INCREMENTAL
, 'c'):
738 case O(INCREMENTAL
, ConfigFile
):
740 case O(MISC
, ConfigFile
):
742 case O(MONITOR
,ConfigFile
):
744 fprintf(stderr
, Name
": configfile cannot be set twice. "
745 "Second value is %s.\n", optarg
);
749 set_conffile(configfile
);
750 /* FIXME possibly check that config file exists. Even parse it */
752 case O(ASSEMBLE
,'s'): /* scan */
755 case O(INCREMENTAL
,'s'):
759 case O(MONITOR
,'m'): /* mail address */
760 case O(MONITOR
,EMail
):
762 fprintf(stderr
, Name
": only specify one mailaddress. %s ignored.\n",
768 case O(MONITOR
,'p'): /* alert program */
769 case O(MONITOR
,ProgramOpt
): /* alert program */
771 fprintf(stderr
, Name
": only specify one alter program. %s ignored.\n",
777 case O(MONITOR
,'r'): /* rebuild increments */
778 case O(MONITOR
,Increment
):
779 increments
= atoi(optarg
);
780 if (increments
>99 || increments
<1) {
781 fprintf(stderr
, Name
": please specify positive integer between 1 and 99 as rebuild increments.\n");
786 case O(MONITOR
,'d'): /* delay in seconds */
788 case O(BUILD
,'d'): /* delay for bitmap updates */
791 fprintf(stderr
, Name
": only specify delay once. %s ignored.\n",
794 delay
= strtol(optarg
, &c
, 10);
795 if (!optarg
[0] || *c
|| delay
<1) {
796 fprintf(stderr
, Name
": invalid delay: %s\n",
802 case O(MONITOR
,'f'): /* daemonise */
803 case O(MONITOR
,Fork
):
806 case O(MONITOR
,'i'): /* pid */
808 fprintf(stderr
, Name
": only specify one pid file. %s ignored.\n",
813 case O(MONITOR
,'1'): /* oneshot */
816 case O(MONITOR
,'t'): /* test */
819 case O(MONITOR
,'y'): /* log messages to syslog */
820 openlog("mdadm", LOG_PID
, SYSLOG_FACILITY
);
823 case O(MONITOR
, NoSharing
):
826 /* now the general management options. Some are applicable
827 * to other modes. None have arguments.
832 case O(MANAGE
,Add
): /* add a drive */
836 case O(MANAGE
,ReAdd
):
840 case O(MANAGE
,'r'): /* remove a drive */
841 case O(MANAGE
,Remove
):
844 case O(MANAGE
,'f'): /* set faulty */
846 case O(INCREMENTAL
,'f'):
847 case O(INCREMENTAL
,Remove
):
848 case O(INCREMENTAL
,Fail
): /* r for incremental is taken, use f
849 * even though we will both fail and
850 * remove the device */
853 case O(INCREMENTAL
,'R'):
855 case O(ASSEMBLE
,'R'):
857 case O(CREATE
,'R'): /* Run the array */
859 fprintf(stderr
, Name
": Cannot both Stop and Run an array\n");
866 fprintf(stderr
, Name
": Cannot both Run and Stop an array\n");
885 case O(MISC
, WaitOpt
):
886 case O(MISC
, Waitclean
):
887 case O(MISC
, DetailPlatform
):
888 case O(MISC
, KillSubarray
):
889 case O(MISC
, UpdateSubarray
):
890 if (devmode
&& devmode
!= opt
&&
891 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
892 fprintf(stderr
, Name
": --examine/-E cannot be given with ");
893 if (devmode
== 'E') {
894 if (option_index
>= 0)
895 fprintf(stderr
, "--%s\n",
896 long_options
[option_index
].name
);
898 fprintf(stderr
, "-%c\n", opt
);
899 } else if (isalpha(devmode
))
900 fprintf(stderr
, "-%c\n", devmode
);
902 fprintf(stderr
, "previous option\n");
911 case O(MISC
, Sparc22
):
912 if (devmode
!= 'E') {
913 fprintf(stderr
, Name
": --sparc2.2 only allowed with --examine\n");
919 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
920 case O(ASSEMBLE
,Bitmap
):
922 fprintf(stderr
, Name
": bitmap file needed with -b in --assemble mode\n");
925 if (strcmp(optarg
, "internal")==0) {
926 fprintf(stderr
, Name
": there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
929 bitmap_fd
= open(optarg
, O_RDWR
);
930 if (!*optarg
|| bitmap_fd
< 0) {
931 fprintf(stderr
, Name
": cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
934 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
937 case O(ASSEMBLE
, BackupFile
):
938 case O(GROW
, BackupFile
):
939 /* Specify a file into which grow might place a backup,
940 * or from which assemble might recover a backup
943 fprintf(stderr
, Name
": backup file already specified, rejecting %s\n", optarg
);
946 backup_file
= optarg
;
949 case O(ASSEMBLE
, InvalidBackup
):
950 /* Acknowledge that the backupfile is invalid, but ask
957 case O(BUILD
,Bitmap
):
959 case O(CREATE
,Bitmap
): /* here we create the bitmap */
960 if (strcmp(optarg
, "none") == 0) {
961 fprintf(stderr
, Name
": '--bitmap none' only"
962 " support for --grow\n");
968 if (strcmp(optarg
, "internal")== 0 ||
969 strcmp(optarg
, "none")== 0 ||
970 strchr(optarg
, '/') != NULL
) {
971 bitmap_file
= optarg
;
975 fprintf(stderr
, Name
": bitmap file must contain a '/', or be 'internal', or 'none'\n"
976 " not '%s'\n", optarg
);
979 case O(GROW
,BitmapChunk
):
980 case O(BUILD
,BitmapChunk
):
981 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
982 bitmap_chunk
= parse_size(optarg
);
983 if (bitmap_chunk
< 0 ||
984 bitmap_chunk
& (bitmap_chunk
- 1)) {
986 Name
": invalid bitmap chunksize: %s\n",
990 /* convert sectors to B, chunk of 0 means 512B */
991 bitmap_chunk
= bitmap_chunk
? bitmap_chunk
* 512 : 512;
994 case O(GROW
, WriteBehind
):
995 case O(BUILD
, WriteBehind
):
996 case O(CREATE
, WriteBehind
): /* write-behind mode */
997 write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
999 write_behind
= strtol(optarg
, &c
, 10);
1000 if (write_behind
< 0 || *c
||
1001 write_behind
> 16383) {
1002 fprintf(stderr
, Name
": Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
1008 case O(INCREMENTAL
, 'r'):
1009 case O(INCREMENTAL
, RebuildMapOpt
):
1012 case O(INCREMENTAL
, IncrementalPath
):
1013 remove_path
= optarg
;
1016 /* We have now processed all the valid options. Anything else is
1019 if (option_index
> 0)
1020 fprintf(stderr
, Name
":option --%s not valid in %s mode\n",
1021 long_options
[option_index
].name
,
1022 map_num(modes
, mode
));
1024 fprintf(stderr
, Name
": option -%c not valid in %s mode\n",
1025 opt
, map_num(modes
, mode
));
1031 char *help_text
= Help
;
1032 if (print_help
== 2)
1033 help_text
= OptionHelp
;
1036 case ASSEMBLE
: help_text
= Help_assemble
; break;
1037 case BUILD
: help_text
= Help_build
; break;
1038 case CREATE
: help_text
= Help_create
; break;
1039 case MANAGE
: help_text
= Help_manage
; break;
1040 case MISC
: help_text
= Help_misc
; break;
1041 case MONITOR
: help_text
= Help_monitor
; break;
1042 case GROW
: help_text
= Help_grow
; break;
1043 case INCREMENTAL
:help_text
= Help_incr
; break;
1045 fputs(help_text
,stdout
);
1049 if (!mode
&& devs_found
) {
1052 if (devlist
->disposition
== 0)
1053 devlist
->disposition
= devmode
;
1056 fputs(Usage
, stderr
);
1061 struct createinfo
*ci
= conf_get_create_info();
1063 if (strcasecmp(symlinks
, "yes") == 0)
1065 else if (strcasecmp(symlinks
, "no") == 0)
1068 fprintf(stderr
, Name
": option --symlinks must be 'no' or 'yes'\n");
1072 /* Ok, got the option parsing out of the way
1073 * hopefully it's mostly right but there might be some stuff
1076 * That is mosty checked in the per-mode stuff but...
1078 * For @,B,C and A without -s, the first device listed must be an md device
1079 * we check that here and open it.
1082 if (mode
==MANAGE
|| mode
== BUILD
|| mode
== CREATE
|| mode
== GROW
||
1083 (mode
== ASSEMBLE
&& ! scan
)) {
1084 if (devs_found
< 1) {
1085 fprintf(stderr
, Name
": an md device must be given in this mode\n");
1088 if ((int)ident
.super_minor
== -2 && autof
) {
1089 fprintf(stderr
, Name
": --super-minor=dev is incompatible with --auto\n");
1092 if (mode
== MANAGE
|| mode
== GROW
) {
1093 mdfd
= open_mddev(devlist
->devname
, 1);
1097 /* non-existent device is OK */
1098 mdfd
= open_mddev(devlist
->devname
, 0);
1100 fprintf(stderr
, Name
": device %s exists but is not an "
1101 "md array.\n", devlist
->devname
);
1104 if ((int)ident
.super_minor
== -2) {
1107 fprintf(stderr
, Name
": --super-minor=dev given, and "
1108 "listed device %s doesn't exist.\n",
1113 ident
.super_minor
= minor(stb
.st_rdev
);
1115 if (mdfd
>= 0 && mode
!= MANAGE
&& mode
!= GROW
) {
1116 /* We don't really want this open yet, we just might
1117 * have wanted to check some things
1125 if (raiddisks
> max_disks
) {
1126 fprintf(stderr
, Name
": invalid number of raid devices: %d\n",
1130 if (raiddisks
== 1 && !force
&& level
!= -5) {
1131 fprintf(stderr
, Name
": '1' is an unusual number of drives for an array, so it is probably\n"
1132 " a mistake. If you really mean it you will need to specify --force before\n"
1133 " setting the number of drives.\n");
1138 if ( sparedisks
> max_disks
- raiddisks
) {
1139 fprintf(stderr
, Name
": invalid number of spare-devices: %d\n",
1145 if (homehost
== NULL
)
1146 homehost
= conf_get_homehost(&require_homehost
);
1147 if (homehost
== NULL
|| strcmp(homehost
, "<system>")==0) {
1148 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
1149 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
1150 homehost
= sys_hostname
;
1154 if ((mode
!= MISC
|| devmode
!= 'E') &&
1156 fprintf(stderr
, Name
": must be super-user to perform this action\n");
1160 ident
.autof
= autof
;
1165 /* readonly, add/remove, readwrite, runstop */
1167 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1168 if (!rv
&& devs_found
>1)
1169 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
1170 devlist
->next
, verbose
-quiet
, test
);
1171 if (!rv
&& readonly
< 0)
1172 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1174 rv
= Manage_runstop(devlist
->devname
, mdfd
, runstop
, quiet
);
1177 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1178 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !scan
) {
1179 /* Only a device has been given, so get details from config file */
1180 struct mddev_ident
*array_ident
= conf_get_ident(devlist
->devname
);
1181 if (array_ident
== NULL
) {
1182 fprintf(stderr
, Name
": %s not identified in config file.\n",
1188 if (array_ident
->autof
== 0)
1189 array_ident
->autof
= autof
;
1190 rv
|= Assemble(ss
, devlist
->devname
, array_ident
,
1191 NULL
, backup_file
, invalid_backup
,
1192 readonly
, runstop
, update
,
1193 homehost
, require_homehost
,
1194 verbose
-quiet
, force
);
1197 rv
= Assemble(ss
, devlist
->devname
, &ident
,
1198 devlist
->next
, backup_file
, invalid_backup
,
1199 readonly
, runstop
, update
,
1200 homehost
, require_homehost
,
1201 verbose
-quiet
, force
);
1202 else if (devs_found
>0) {
1203 if (update
&& devs_found
> 1) {
1204 fprintf(stderr
, Name
": can only update a single array at a time\n");
1207 if (backup_file
&& devs_found
> 1) {
1208 fprintf(stderr
, Name
": can only assemble a single array when providing a backup file.\n");
1211 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1212 struct mddev_ident
*array_ident
= conf_get_ident(dv
->devname
);
1213 if (array_ident
== NULL
) {
1214 fprintf(stderr
, Name
": %s not identified in config file.\n",
1219 if (array_ident
->autof
== 0)
1220 array_ident
->autof
= autof
;
1221 rv
|= Assemble(ss
, dv
->devname
, array_ident
,
1222 NULL
, backup_file
, invalid_backup
,
1223 readonly
, runstop
, update
,
1224 homehost
, require_homehost
,
1225 verbose
-quiet
, force
);
1228 struct mddev_ident
*a
, *array_list
= conf_get_ident(NULL
);
1229 struct mddev_dev
*devlist
= conf_get_devs();
1231 int failures
, successes
;
1232 if (devlist
== NULL
) {
1233 fprintf(stderr
, Name
": No devices listed in conf file were found.\n");
1237 fprintf(stderr
, Name
": --update not meaningful with a --scan assembly.\n");
1241 fprintf(stderr
, Name
": --backup_file not meaningful with a --scan assembly.\n");
1244 for (a
= array_list
; a
; a
= a
->next
) {
1253 for (a
= array_list
; a
; a
= a
->next
) {
1258 strcasecmp(a
->devname
, "<ignore>") == 0)
1261 r
= Assemble(ss
, a
->devname
,
1264 readonly
, runstop
, NULL
,
1265 homehost
, require_homehost
,
1266 verbose
-quiet
, force
);
1275 } while (failures
&& successes
);
1276 if (homehost
&& cnt
== 0) {
1277 /* Maybe we can auto-assemble something.
1278 * Repeatedly call Assemble in auto-assemble mode
1283 ident
.autof
= autof
;
1285 struct mddev_dev
*devlist
= conf_get_devs();
1288 rv2
= Assemble(ss
, NULL
,
1291 readonly
, runstop
, NULL
,
1292 homehost
, require_homehost
,
1293 verbose
-quiet
, force
);
1299 /* found something so even though assembly failed we
1300 * want to avoid auto-updates
1302 auto_update_home
= 0;
1304 /* Incase there are stacked devices, we need to go around again */
1307 if (cnt
== 0 && auto_update_home
&& homehost
) {
1308 /* Nothing found, maybe we need to bootstrap homehost info */
1320 verbose
-quiet
, force
);
1326 /* Incase there are stacked devices, we need to go around again */
1330 if (cnt
== 0 && rv
== 0) {
1331 fprintf(stderr
, Name
": No arrays found in config file or automatically\n");
1335 } else if (cnt
== 0 && rv
== 0) {
1336 fprintf(stderr
, Name
": No arrays found in config file\n");
1342 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1343 if (write_behind
&& !bitmap_file
) {
1344 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1348 if (raiddisks
== 0) {
1349 fprintf(stderr
, Name
": no raid-devices specified.\n");
1355 if (strcmp(bitmap_file
, "internal")==0) {
1356 fprintf(stderr
, Name
": 'internal' bitmaps not supported with --build\n");
1361 rv
= Build(devlist
->devname
, chunk
, level
, layout
,
1362 raiddisks
, devlist
->next
, assume_clean
,
1363 bitmap_file
, bitmap_chunk
, write_behind
,
1364 delay
, verbose
-quiet
, autof
, size
);
1367 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1368 if (write_behind
&& !bitmap_file
) {
1369 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1373 if (raiddisks
== 0) {
1374 fprintf(stderr
, Name
": no raid-devices specified.\n");
1379 rv
= Create(ss
, devlist
->devname
, chunk
, level
, layout
, size
<0 ? 0 : size
,
1380 raiddisks
, sparedisks
, ident
.name
, homehost
,
1381 ident
.uuid_set
? ident
.uuid
: NULL
,
1382 devs_found
-1, devlist
->next
, runstop
, verbose
-quiet
, force
, assume_clean
,
1383 bitmap_file
, bitmap_chunk
, write_behind
, delay
, autof
);
1386 if (devmode
== 'E') {
1387 if (devlist
== NULL
&& !scan
) {
1388 fprintf(stderr
, Name
": No devices to examine\n");
1391 if (devlist
== NULL
)
1392 devlist
= conf_get_devs();
1393 if (devlist
== NULL
) {
1394 fprintf(stderr
, Name
": No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1397 if (brief
&& verbose
)
1399 rv
= Examine(devlist
, scan
?(verbose
>1?0:verbose
+1):brief
,
1401 SparcAdjust
, ss
, homehost
);
1402 } else if (devmode
== DetailPlatform
) {
1403 rv
= Detail_Platform(ss
? ss
->ss
: NULL
, ss
? scan
: 1, verbose
);
1405 if (devlist
== NULL
) {
1406 if ((devmode
=='D' || devmode
== Waitclean
) && scan
) {
1407 /* apply --detail or --wait-clean to
1408 * all devices in /proc/mdstat
1410 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1411 struct mdstat_ent
*e
;
1412 struct map_ent
*map
= NULL
;
1414 int v
= verbose
>1?0:verbose
+1;
1416 for (members
= 0; members
<= 1; members
++) {
1417 for (e
=ms
; e
; e
=e
->next
) {
1420 int member
= e
->metadata_version
&&
1421 strncmp(e
->metadata_version
,
1422 "external:/", 10) == 0;
1423 if (members
!= member
)
1425 me
= map_by_devnum(&map
, e
->devnum
);
1427 && strcmp(me
->path
, "/unknown") != 0)
1430 name
= get_md_name(e
->devnum
);
1433 fprintf(stderr
, Name
": cannot find device file for %s\n",
1438 rv
|= Detail(name
, v
,
1442 rv
|= WaitClean(name
, -1, v
);
1447 } else if (devmode
== 'S' && scan
) {
1448 /* apply --stop to all devices in /proc/mdstat */
1449 /* Due to possible stacking of devices, repeat until
1450 * nothing more can be stopped
1452 int progress
=1, err
;
1455 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1456 struct mdstat_ent
*e
;
1458 if (!progress
) last
= 1;
1459 progress
= 0; err
= 0;
1460 for (e
=ms
; e
; e
=e
->next
) {
1461 char *name
= get_md_name(e
->devnum
);
1464 fprintf(stderr
, Name
": cannot find device file for %s\n",
1468 mdfd
= open_mddev(name
, 1);
1470 if (Manage_runstop(name
, mdfd
, -1, quiet
?1:last
?0:-1))
1480 } while (!last
&& err
);
1483 fprintf(stderr
, Name
": No devices given.\n");
1487 for (dv
=devlist
; dv
; dv
=dv
->next
) {
1488 switch(dv
->disposition
) {
1490 rv
|= Detail(dv
->devname
,
1492 export
, test
, homehost
);
1494 case 'K': /* Zero superblock */
1496 rv
|= Kill(dv
->devname
, ss
, force
, quiet
,0);
1500 rv
|= Kill(dv
->devname
, NULL
, force
, q
, 0);
1507 rv
|= Query(dv
->devname
); continue;
1509 rv
|= ExamineBitmap(dv
->devname
, brief
, ss
); continue;
1512 rv
|= Wait(dv
->devname
); continue;
1514 rv
|= WaitClean(dv
->devname
, -1, verbose
-quiet
); continue;
1516 rv
|= Kill_subarray(dv
->devname
, subarray
, quiet
);
1518 case UpdateSubarray
:
1519 if (update
== NULL
) {
1521 Name
": -U/--update must be specified with --update-subarray\n");
1525 rv
|= Update_subarray(dv
->devname
, subarray
, update
, &ident
, quiet
);
1528 mdfd
= open_mddev(dv
->devname
, 1);
1530 switch(dv
->disposition
) {
1532 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1, quiet
); break;
1534 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1, quiet
); break;
1536 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1538 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;
1547 if (!devlist
&& !scan
) {
1548 fprintf(stderr
, Name
": Cannot monitor: need --scan or at least one device\n");
1552 if (pidfile
&& !daemonise
) {
1553 fprintf(stderr
, Name
": Cannot write a pid file when not in daemon mode\n");
1558 if (get_linux_version() > 2006016)
1559 /* mdstat responds to poll */
1564 rv
= Monitor(devlist
, mailaddr
, program
,
1565 delay
?delay
:60, daemonise
, scan
, oneshot
,
1566 dosyslog
, test
, pidfile
, increments
, spare_sharing
);
1570 if (array_size
>= 0) {
1571 /* alway impose array size first, independent of
1573 * Do not allow level or raid_disks changes at the
1574 * same time as that can be irreversibly destructive.
1578 if (raiddisks
|| level
!= UnSet
) {
1579 fprintf(stderr
, Name
": cannot change array size in same operation "
1580 "as changing raiddisks or level.\n"
1581 " Change size first, then check that data is still intact.\n");
1585 sysfs_init(&sra
, mdfd
, 0);
1586 if (array_size
== 0)
1587 err
= sysfs_set_str(&sra
, NULL
, "array_size", "default");
1589 err
= sysfs_set_num(&sra
, NULL
, "array_size", array_size
/ 2);
1592 fprintf(stderr
, Name
": --array-size setting"
1593 " is too large.\n");
1595 fprintf(stderr
, Name
": current kernel does"
1596 " not support setting --array-size\n");
1601 if (devs_found
> 1) {
1604 if (size
>= 0 || raiddisks
|| chunk
|| layout_str
!= NULL
|| bitmap_file
) {
1605 fprintf(stderr
, Name
": --add cannot be used with other geometry changes in --grow mode\n");
1609 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1610 rv
= Grow_Add_device(devlist
->devname
, mdfd
, dv
->devname
);
1614 } else if (bitmap_file
) {
1615 if (size
>= 0 || raiddisks
|| chunk
|| layout_str
!= NULL
) {
1616 fprintf(stderr
, Name
": --bitmap changes cannot be used with other geometry changes in --grow mode\n");
1621 delay
= DEFAULT_BITMAP_DELAY
;
1622 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, bitmap_file
,
1623 bitmap_chunk
, delay
, write_behind
, force
);
1624 } else if (size
>= 0 || raiddisks
!= 0 || layout_str
!= NULL
1625 || chunk
!= 0 || level
!= UnSet
) {
1626 rv
= Grow_reshape(devlist
->devname
, mdfd
, quiet
, backup_file
,
1627 size
, level
, layout_str
, chunk
, raiddisks
);
1628 } else if (array_size
< 0)
1629 fprintf(stderr
, Name
": no changes to --grow\n");
1637 fprintf(stderr
, Name
1638 ": --incremental --scan meaningless without --run.\n");
1641 if (devmode
== 'f') {
1642 fprintf(stderr
, Name
1643 ": --incremental --scan --fail not supported.\n");
1646 rv
= IncrementalScan(verbose
);
1649 if (!rebuild_map
&& !scan
) {
1650 fprintf(stderr
, Name
1651 ": --incremental requires a device.\n");
1656 if (devlist
->next
) {
1657 fprintf(stderr
, Name
1658 ": --incremental can only handle one device.\n");
1663 rv
= IncrementalRemove(devlist
->devname
, remove_path
,
1666 rv
= Incremental(devlist
->devname
, verbose
-quiet
,
1667 runstop
, ss
, homehost
,
1668 require_homehost
, autof
);