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 int freeze_reshape
= 0;
117 srandom(time(0) ^ getpid());
121 ident
.raid_disks
= UnSet
;
122 ident
.super_minor
= UnSet
;
124 ident
.spare_group
= NULL
;
127 ident
.bitmap_fd
= -1;
128 ident
.bitmap_file
= NULL
;
130 ident
.container
= NULL
;
133 while ((option_index
= -1) ,
134 (opt
=getopt_long(argc
, argv
,
135 shortopt
, long_options
,
136 &option_index
)) != -1) {
138 /* firstly, some mode-independent options */
148 fputs(Version
, stderr
);
158 if (mode
== ASSEMBLE
|| mode
== BUILD
|| mode
== CREATE
159 || mode
== GROW
|| mode
== INCREMENTAL
161 break; /* b means bitmap */
170 if (strcasecmp(optarg
, "<ignore>") == 0)
171 require_homehost
= 0;
178 fputs(Usage
, stderr
);
181 /* second, figure out the mode.
182 * Some options force the mode. Others
183 * set the mode if it isn't already
189 shortopt
= short_bitmap_options
;
197 case ReAdd
: /* re-add */
200 shortopt
= short_bitmap_options
;
204 case 'A': newmode
= ASSEMBLE
; shortopt
= short_bitmap_auto_options
; break;
205 case 'B': newmode
= BUILD
; shortopt
= short_bitmap_auto_options
; break;
206 case 'C': newmode
= CREATE
; shortopt
= short_bitmap_auto_options
; break;
207 case 'F': newmode
= MONITOR
;break;
208 case 'G': newmode
= GROW
;
209 shortopt
= short_bitmap_options
;
211 case 'I': newmode
= INCREMENTAL
;
212 shortopt
= short_bitmap_auto_options
; break;
214 newmode
= AUTODETECT
; break;
220 case 'Q': newmode
= MISC
; break;
231 if (opt
== KillSubarray
|| opt
== UpdateSubarray
) {
233 fprintf(stderr
, Name
": subarray can only"
234 " be specified once\n");
240 case 'K': if (!mode
) newmode
= MISC
; break;
241 case NoSharing
: newmode
= MONITOR
; break;
243 if (mode
&& newmode
== mode
) {
244 /* everybody happy ! */
245 } else if (mode
&& newmode
!= mode
) {
247 fprintf(stderr
, Name
": ");
248 if (option_index
>= 0)
249 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
251 fprintf(stderr
, "-%c", opt
);
252 fprintf(stderr
, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
253 map_num(modes
, newmode
),
254 map_num(modes
, mode
));
256 } else if (!mode
&& newmode
) {
258 if (mode
== MISC
&& devs_found
) {
259 fprintf(stderr
, Name
": No action given for %s in --misc mode\n",
261 fprintf(stderr
," Action options must come before device names\n");
265 /* special case of -c --help */
266 if ((opt
== 'c' || opt
== ConfigFile
) &&
267 ( strncmp(optarg
, "--h", 3)==0 ||
268 strncmp(optarg
, "-h", 2)==0)) {
269 fputs(Help_config
, stdout
);
273 /* If first option is a device, don't force the mode yet */
275 if (devs_found
== 0) {
276 dv
= malloc(sizeof(*dv
));
278 fprintf(stderr
, Name
": malloc failed\n");
281 dv
->devname
= optarg
;
282 dv
->disposition
= devmode
;
283 dv
->writemostly
= writemostly
;
288 devlistend
= &dv
->next
;
293 /* No mode yet, and this is the second device ... */
294 fprintf(stderr
, Name
": An option must be given to set the mode before a second device\n"
295 " (%s) is listed\n", optarg
);
298 if (option_index
>= 0)
299 fprintf(stderr
, Name
": --%s", long_options
[option_index
].name
);
301 fprintf(stderr
, Name
": -%c", opt
);
302 fprintf(stderr
, " does not set the mode, and so cannot be the first option.\n");
306 /* if we just set the mode, then done */
320 /* an undecorated option - must be a device name.
322 if (devs_found
> 0 && mode
== MANAGE
&& !devmode
) {
323 fprintf(stderr
, Name
": Must give one of -a/-r/-f"
324 " for subsequent devices at %s\n", optarg
);
327 if (devs_found
> 0 && mode
== GROW
&& !devmode
) {
328 fprintf(stderr
, Name
": Must give -a/--add for"
329 " devices to add: %s\n", optarg
);
332 dv
= malloc(sizeof(*dv
));
334 fprintf(stderr
, Name
": malloc failed\n");
337 dv
->devname
= optarg
;
338 dv
->disposition
= devmode
;
339 dv
->writemostly
= writemostly
;
344 devlistend
= &dv
->next
;
350 /* We've got a mode, and opt is now something else which
351 * could depend on the mode */
352 #define O(a,b) ((a<<16)|b)
353 switch (O(mode
,opt
)) {
355 case O(GROW
,ChunkSize
):
357 case O(CREATE
,ChunkSize
):
358 case O(BUILD
,'c'): /* chunk or rounding */
359 case O(BUILD
,ChunkSize
): /* chunk or rounding */
361 fprintf(stderr
, Name
": chunk/rounding may only be specified once. "
362 "Second value is %s.\n", optarg
);
365 chunk
= parse_size(optarg
);
366 if (chunk
< 8 || (chunk
&1)) {
367 fprintf(stderr
, Name
": invalid chunk/rounding value: %s\n",
371 /* Convert sectors to K */
375 case O(INCREMENTAL
, 'e'):
377 case O(ASSEMBLE
,'e'):
378 case O(MISC
,'e'): /* set metadata (superblock) information */
380 fprintf(stderr
, Name
": metadata information already given\n");
383 for(i
=0; !ss
&& superlist
[i
]; i
++)
384 ss
= superlist
[i
]->match_metadata_desc(optarg
);
387 fprintf(stderr
, Name
": unrecognised metadata identifier: %s\n", optarg
);
393 case O(MANAGE
,WriteMostly
):
395 case O(BUILD
,WriteMostly
):
397 case O(CREATE
,WriteMostly
):
398 /* set write-mostly for following devices */
403 /* clear write-mostly for following devices */
410 case O(BUILD
,'z'): /* size */
412 fprintf(stderr
, Name
": size may only be specified once. "
413 "Second value is %s.\n", optarg
);
416 if (strcmp(optarg
, "max")==0)
419 size
= parse_size(optarg
);
421 fprintf(stderr
, Name
": invalid size: %s\n",
425 /* convert sectors to K */
430 case O(GROW
,'Z'): /* array size */
431 if (array_size
>= 0) {
432 fprintf(stderr
, Name
": array-size may only be specified once. "
433 "Second value is %s.\n", optarg
);
436 if (strcmp(optarg
, "max") == 0)
439 array_size
= parse_size(optarg
);
440 if (array_size
<= 0) {
441 fprintf(stderr
, Name
": invalid array size: %s\n",
450 case O(BUILD
,'l'): /* set raid level*/
451 if (level
!= UnSet
) {
452 fprintf(stderr
, Name
": raid level may only be set once. "
453 "Second value is %s.\n", optarg
);
456 level
= map_name(pers
, optarg
);
457 if (level
== UnSet
) {
458 fprintf(stderr
, Name
": invalid raid level: %s\n",
462 if (level
!= 0 && level
!= LEVEL_LINEAR
&& level
!= 1 &&
463 level
!= LEVEL_MULTIPATH
&& level
!= LEVEL_FAULTY
&&
466 fprintf(stderr
, Name
": Raid level %s not permitted with --build.\n",
470 if (sparedisks
> 0 && level
< 1 && level
>= -1) {
471 fprintf(stderr
, Name
": raid level %s is incompatible with spare-devices setting.\n",
478 case O(GROW
, 'p'): /* new layout */
479 case O(GROW
, Layout
):
481 fprintf(stderr
,Name
": layout may only be sent once. "
482 "Second value was %s\n", optarg
);
486 /* 'Grow' will parse the value */
489 case O(CREATE
,'p'): /* raid5 layout */
490 case O(CREATE
,Layout
):
491 case O(BUILD
,'p'): /* faulty layout */
492 case O(BUILD
,Layout
):
493 if (layout
!= UnSet
) {
494 fprintf(stderr
,Name
": layout may only be sent once. "
495 "Second value was %s\n", optarg
);
500 fprintf(stderr
, Name
": layout not meaningful for %s arrays.\n",
501 map_num(pers
, level
));
504 fprintf(stderr
, Name
": raid level must be given before layout.\n");
508 layout
= map_name(r5layout
, optarg
);
510 fprintf(stderr
, Name
": layout %s not understood for raid5.\n",
516 layout
= map_name(r6layout
, optarg
);
518 fprintf(stderr
, Name
": layout %s not understood for raid6.\n",
525 layout
= parse_layout_10(optarg
);
527 fprintf(stderr
, Name
": layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg
);
535 layout
= parse_layout_faulty(optarg
);
537 fprintf(stderr
, Name
": layout %s not understood for faulty.\n",
545 case O(CREATE
,AssumeClean
):
546 case O(BUILD
,AssumeClean
): /* assume clean */
547 case O(GROW
,AssumeClean
):
553 case O(BUILD
,'n'): /* number of raid disks */
555 fprintf(stderr
, Name
": raid-devices set twice: %d and %s\n",
559 raiddisks
= strtol(optarg
, &c
, 10);
560 if (!optarg
[0] || *c
|| raiddisks
<=0) {
561 fprintf(stderr
, Name
": invalid number of raid devices: %s\n",
565 ident
.raid_disks
= raiddisks
;
568 case O(CREATE
,'x'): /* number of spare (eXtra) discs */
570 fprintf(stderr
,Name
": spare-devices set twice: %d and %s\n",
574 if (level
!= UnSet
&& level
<= 0 && level
>= -1) {
575 fprintf(stderr
, Name
": spare-devices setting is incompatible with raid level %d\n",
579 sparedisks
= strtol(optarg
, &c
, 10);
580 if (!optarg
[0] || *c
|| sparedisks
< 0) {
581 fprintf(stderr
, Name
": invalid number of spare-devices: %s\n",
591 case O(INCREMENTAL
,'a'):
592 case O(INCREMENTAL
,Auto
):
593 case O(ASSEMBLE
,'a'):
594 case O(ASSEMBLE
,Auto
): /* auto-creation of device node */
595 autof
= parse_auto(optarg
, "--auto flag", 0);
598 case O(CREATE
,Symlinks
):
599 case O(BUILD
,Symlinks
):
600 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
604 case O(BUILD
,'f'): /* force honouring '-n 1' */
605 case O(BUILD
,Force
): /* force honouring '-n 1' */
606 case O(GROW
,'f'): /* ditto */
607 case O(GROW
,Force
): /* ditto */
608 case O(CREATE
,'f'): /* force honouring of device list */
609 case O(CREATE
,Force
): /* force honouring of device list */
610 case O(ASSEMBLE
,'f'): /* force assembly */
611 case O(ASSEMBLE
,Force
): /* force assembly */
612 case O(MISC
,'f'): /* force zero */
613 case O(MISC
,Force
): /* force zero */
614 case O(MANAGE
,Force
): /* add device which is too large */
617 /* now for the Assemble options */
618 case O(ASSEMBLE
, FreezeReshape
): /* Freeze reshape during
620 case O(INCREMENTAL
, FreezeReshape
):
623 case O(CREATE
,'u'): /* uuid of array */
624 case O(ASSEMBLE
,'u'): /* uuid of array */
625 if (ident
.uuid_set
) {
626 fprintf(stderr
, Name
": uuid cannot be set twice. "
627 "Second value %s.\n", optarg
);
630 if (parse_uuid(optarg
, ident
.uuid
))
633 fprintf(stderr
,Name
": Bad uuid: %s\n", optarg
);
639 case O(ASSEMBLE
,'N'):
642 fprintf(stderr
, Name
": name cannot be set twice. "
643 "Second value %s.\n", optarg
);
646 if (mode
== MISC
&& !subarray
) {
647 fprintf(stderr
, Name
": -N/--name only valid with --update-subarray in misc mode\n");
650 if (strlen(optarg
) > 32) {
651 fprintf(stderr
, Name
": name '%s' is too long, 32 chars max.\n",
655 strcpy(ident
.name
, optarg
);
658 case O(ASSEMBLE
,'m'): /* super-minor for array */
659 case O(ASSEMBLE
,SuperMinor
):
660 if (ident
.super_minor
!= UnSet
) {
661 fprintf(stderr
, Name
": super-minor cannot be set twice. "
662 "Second value: %s.\n", optarg
);
665 if (strcmp(optarg
, "dev")==0)
666 ident
.super_minor
= -2;
668 ident
.super_minor
= strtoul(optarg
, &cp
, 10);
669 if (!optarg
[0] || *cp
) {
670 fprintf(stderr
, Name
": Bad super-minor number: %s.\n", optarg
);
676 case O(ASSEMBLE
,'U'): /* update the superblock */
679 fprintf(stderr
, Name
": Can only update one aspect"
680 " of superblock, both %s and %s given.\n",
684 if (mode
== MISC
&& !subarray
) {
685 fprintf(stderr
, Name
": Only subarrays can be"
686 " updated in misc mode\n");
690 if (strcmp(update
, "sparc2.2")==0)
692 if (strcmp(update
, "super-minor") == 0)
694 if (strcmp(update
, "summaries")==0)
696 if (strcmp(update
, "resync")==0)
698 if (strcmp(update
, "uuid")==0)
700 if (strcmp(update
, "name")==0)
702 if (strcmp(update
, "homehost")==0)
704 if (strcmp(update
, "devicesize")==0)
706 if (strcmp(update
, "no-bitmap")==0)
708 if (strcmp(update
, "byteorder")==0) {
711 Name
": must not set metadata"
712 " type with --update=byteorder.\n");
715 for(i
=0; !ss
&& superlist
[i
]; i
++)
716 ss
= superlist
[i
]->match_metadata_desc(
719 fprintf(stderr
, Name
": INTERNAL ERROR"
720 " cannot find 0.swap\n");
726 if (strcmp(update
,"?") == 0 ||
727 strcmp(update
, "help") == 0) {
729 fprintf(outf
, Name
": ");
733 Name
": '--update=%s' is invalid. ",
736 fprintf(outf
, "Valid --update options are:\n"
737 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
738 " 'summaries', 'homehost', 'byteorder', 'devicesize',\n"
740 exit(outf
== stdout
? 0 : 2);
743 /* update=devicesize is allowed with --re-add */
744 if (devmode
!= 'a' || re_add
!= 1) {
745 fprintf(stderr
, Name
"--update in Manage mode only"
746 " allowed with --re-add.\n");
750 fprintf(stderr
, Name
": Can only update one aspect"
751 " of superblock, both %s and %s given.\n",
756 if (strcmp(update
, "devicesize") != 0) {
757 fprintf(stderr
, Name
": only 'devicesize' can be"
758 " updated with --re-add\n");
763 case O(INCREMENTAL
,NoDegraded
):
764 fprintf(stderr
, Name
": --no-degraded is deprecated in Incremental mode\n");
765 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
766 runstop
= -1; /* --stop isn't allowed for --assemble,
767 * so we overload slightly */
770 case O(ASSEMBLE
,'c'):
771 case O(ASSEMBLE
,ConfigFile
):
772 case O(INCREMENTAL
, 'c'):
773 case O(INCREMENTAL
, ConfigFile
):
775 case O(MISC
, ConfigFile
):
777 case O(MONITOR
,ConfigFile
):
779 fprintf(stderr
, Name
": configfile cannot be set twice. "
780 "Second value is %s.\n", optarg
);
784 set_conffile(configfile
);
785 /* FIXME possibly check that config file exists. Even parse it */
787 case O(ASSEMBLE
,'s'): /* scan */
790 case O(INCREMENTAL
,'s'):
794 case O(MONITOR
,'m'): /* mail address */
795 case O(MONITOR
,EMail
):
797 fprintf(stderr
, Name
": only specify one mailaddress. %s ignored.\n",
803 case O(MONITOR
,'p'): /* alert program */
804 case O(MONITOR
,ProgramOpt
): /* alert program */
806 fprintf(stderr
, Name
": only specify one alter program. %s ignored.\n",
812 case O(MONITOR
,'r'): /* rebuild increments */
813 case O(MONITOR
,Increment
):
814 increments
= atoi(optarg
);
815 if (increments
>99 || increments
<1) {
816 fprintf(stderr
, Name
": please specify positive integer between 1 and 99 as rebuild increments.\n");
821 case O(MONITOR
,'d'): /* delay in seconds */
823 case O(BUILD
,'d'): /* delay for bitmap updates */
826 fprintf(stderr
, Name
": only specify delay once. %s ignored.\n",
829 delay
= strtol(optarg
, &c
, 10);
830 if (!optarg
[0] || *c
|| delay
<1) {
831 fprintf(stderr
, Name
": invalid delay: %s\n",
837 case O(MONITOR
,'f'): /* daemonise */
838 case O(MONITOR
,Fork
):
841 case O(MONITOR
,'i'): /* pid */
843 fprintf(stderr
, Name
": only specify one pid file. %s ignored.\n",
848 case O(MONITOR
,'1'): /* oneshot */
852 case O(MONITOR
,'t'): /* test */
855 case O(MONITOR
,'y'): /* log messages to syslog */
856 openlog("mdadm", LOG_PID
, SYSLOG_FACILITY
);
859 case O(MONITOR
, NoSharing
):
862 /* now the general management options. Some are applicable
863 * to other modes. None have arguments.
868 case O(MANAGE
,Add
): /* add a drive */
872 case O(MANAGE
,ReAdd
):
876 case O(MANAGE
,'r'): /* remove a drive */
877 case O(MANAGE
,Remove
):
880 case O(MANAGE
,'f'): /* set faulty */
882 case O(INCREMENTAL
,'f'):
883 case O(INCREMENTAL
,Remove
):
884 case O(INCREMENTAL
,Fail
): /* r for incremental is taken, use f
885 * even though we will both fail and
886 * remove the device */
889 case O(INCREMENTAL
,'R'):
891 case O(ASSEMBLE
,'R'):
893 case O(CREATE
,'R'): /* Run the array */
895 fprintf(stderr
, Name
": Cannot both Stop and Run an array\n");
902 fprintf(stderr
, Name
": Cannot both Run and Stop an array\n");
921 case O(MISC
, WaitOpt
):
922 case O(MISC
, Waitclean
):
923 case O(MISC
, DetailPlatform
):
924 case O(MISC
, KillSubarray
):
925 case O(MISC
, UpdateSubarray
):
926 if (devmode
&& devmode
!= opt
&&
927 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
928 fprintf(stderr
, Name
": --examine/-E cannot be given with ");
929 if (devmode
== 'E') {
930 if (option_index
>= 0)
931 fprintf(stderr
, "--%s\n",
932 long_options
[option_index
].name
);
934 fprintf(stderr
, "-%c\n", opt
);
935 } else if (isalpha(devmode
))
936 fprintf(stderr
, "-%c\n", devmode
);
938 fprintf(stderr
, "previous option\n");
943 case O(MISC
, UdevRules
):
944 if (devmode
&& devmode
!= opt
) {
945 fprintf(stderr
, Name
": --udev-rules must"
946 " be the only option.\n");
949 fprintf(stderr
, Name
": only specify one udev "
950 "rule filename. %s ignored.\n",
953 udev_filename
= optarg
;
961 case O(MISC
, Sparc22
):
962 if (devmode
!= 'E') {
963 fprintf(stderr
, Name
": --sparc2.2 only allowed with --examine\n");
969 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
970 case O(ASSEMBLE
,Bitmap
):
972 fprintf(stderr
, Name
": bitmap file needed with -b in --assemble mode\n");
975 if (strcmp(optarg
, "internal")==0) {
976 fprintf(stderr
, Name
": there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
979 bitmap_fd
= open(optarg
, O_RDWR
);
980 if (!*optarg
|| bitmap_fd
< 0) {
981 fprintf(stderr
, Name
": cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
984 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
987 case O(ASSEMBLE
, BackupFile
):
988 case O(GROW
, BackupFile
):
989 /* Specify a file into which grow might place a backup,
990 * or from which assemble might recover a backup
993 fprintf(stderr
, Name
": backup file already specified, rejecting %s\n", optarg
);
996 backup_file
= optarg
;
999 case O(ASSEMBLE
, InvalidBackup
):
1000 /* Acknowledge that the backupfile is invalid, but ask
1001 * to continue anyway
1007 case O(BUILD
,Bitmap
):
1009 case O(CREATE
,Bitmap
): /* here we create the bitmap */
1010 if (strcmp(optarg
, "none") == 0) {
1011 fprintf(stderr
, Name
": '--bitmap none' only"
1012 " support for --grow\n");
1017 case O(GROW
,Bitmap
):
1018 if (strcmp(optarg
, "internal")== 0 ||
1019 strcmp(optarg
, "none")== 0 ||
1020 strchr(optarg
, '/') != NULL
) {
1021 bitmap_file
= optarg
;
1025 fprintf(stderr
, Name
": bitmap file must contain a '/', or be 'internal', or 'none'\n"
1026 " not '%s'\n", optarg
);
1029 case O(GROW
,BitmapChunk
):
1030 case O(BUILD
,BitmapChunk
):
1031 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
1032 bitmap_chunk
= parse_size(optarg
);
1033 if (bitmap_chunk
< 0 ||
1034 bitmap_chunk
& (bitmap_chunk
- 1)) {
1036 Name
": invalid bitmap chunksize: %s\n",
1040 /* convert sectors to B, chunk of 0 means 512B */
1041 bitmap_chunk
= bitmap_chunk
? bitmap_chunk
* 512 : 512;
1044 case O(GROW
, WriteBehind
):
1045 case O(BUILD
, WriteBehind
):
1046 case O(CREATE
, WriteBehind
): /* write-behind mode */
1047 write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
1049 write_behind
= strtol(optarg
, &c
, 10);
1050 if (write_behind
< 0 || *c
||
1051 write_behind
> 16383) {
1052 fprintf(stderr
, Name
": Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
1058 case O(INCREMENTAL
, 'r'):
1059 case O(INCREMENTAL
, RebuildMapOpt
):
1062 case O(INCREMENTAL
, IncrementalPath
):
1063 remove_path
= optarg
;
1066 /* We have now processed all the valid options. Anything else is
1069 if (option_index
> 0)
1070 fprintf(stderr
, Name
":option --%s not valid in %s mode\n",
1071 long_options
[option_index
].name
,
1072 map_num(modes
, mode
));
1074 fprintf(stderr
, Name
": option -%c not valid in %s mode\n",
1075 opt
, map_num(modes
, mode
));
1081 char *help_text
= Help
;
1082 if (print_help
== 2)
1083 help_text
= OptionHelp
;
1086 case ASSEMBLE
: help_text
= Help_assemble
; break;
1087 case BUILD
: help_text
= Help_build
; break;
1088 case CREATE
: help_text
= Help_create
; break;
1089 case MANAGE
: help_text
= Help_manage
; break;
1090 case MISC
: help_text
= Help_misc
; break;
1091 case MONITOR
: help_text
= Help_monitor
; break;
1092 case GROW
: help_text
= Help_grow
; break;
1093 case INCREMENTAL
:help_text
= Help_incr
; break;
1095 fputs(help_text
,stdout
);
1099 if (!mode
&& devs_found
) {
1102 if (devlist
->disposition
== 0)
1103 devlist
->disposition
= devmode
;
1106 fputs(Usage
, stderr
);
1111 struct createinfo
*ci
= conf_get_create_info();
1113 if (strcasecmp(symlinks
, "yes") == 0)
1115 else if (strcasecmp(symlinks
, "no") == 0)
1118 fprintf(stderr
, Name
": option --symlinks must be 'no' or 'yes'\n");
1122 /* Ok, got the option parsing out of the way
1123 * hopefully it's mostly right but there might be some stuff
1126 * That is mosty checked in the per-mode stuff but...
1128 * For @,B,C and A without -s, the first device listed must be an md device
1129 * we check that here and open it.
1132 if (mode
==MANAGE
|| mode
== BUILD
|| mode
== CREATE
|| mode
== GROW
||
1133 (mode
== ASSEMBLE
&& ! scan
)) {
1134 if (devs_found
< 1) {
1135 fprintf(stderr
, Name
": an md device must be given in this mode\n");
1138 if ((int)ident
.super_minor
== -2 && autof
) {
1139 fprintf(stderr
, Name
": --super-minor=dev is incompatible with --auto\n");
1142 if (mode
== MANAGE
|| mode
== GROW
) {
1143 mdfd
= open_mddev(devlist
->devname
, 1);
1147 /* non-existent device is OK */
1148 mdfd
= open_mddev(devlist
->devname
, 0);
1150 fprintf(stderr
, Name
": device %s exists but is not an "
1151 "md array.\n", devlist
->devname
);
1154 if ((int)ident
.super_minor
== -2) {
1157 fprintf(stderr
, Name
": --super-minor=dev given, and "
1158 "listed device %s doesn't exist.\n",
1163 ident
.super_minor
= minor(stb
.st_rdev
);
1165 if (mdfd
>= 0 && mode
!= MANAGE
&& mode
!= GROW
) {
1166 /* We don't really want this open yet, we just might
1167 * have wanted to check some things
1175 if (raiddisks
== 1 && !force
&& level
!= -5) {
1176 fprintf(stderr
, Name
": '1' is an unusual number of drives for an array, so it is probably\n"
1177 " a mistake. If you really mean it you will need to specify --force before\n"
1178 " setting the number of drives.\n");
1183 if (homehost
== NULL
)
1184 homehost
= conf_get_homehost(&require_homehost
);
1185 if (homehost
== NULL
|| strcasecmp(homehost
, "<system>")==0) {
1186 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
1187 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
1188 homehost
= sys_hostname
;
1191 if (homehost
&& (!homehost
[0] || strcasecmp(homehost
, "<none>") == 0)) {
1193 require_homehost
= 0;
1196 if ((mode
!= MISC
|| devmode
!= 'E') &&
1198 fprintf(stderr
, Name
": must be super-user to perform this action\n");
1202 ident
.autof
= autof
;
1207 /* readonly, add/remove, readwrite, runstop */
1209 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1210 if (!rv
&& devs_found
>1)
1211 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
1212 devlist
->next
, verbose
-quiet
, test
,
1214 if (!rv
&& readonly
< 0)
1215 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1217 rv
= Manage_runstop(devlist
->devname
, mdfd
, runstop
, quiet
);
1220 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1221 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !scan
) {
1222 /* Only a device has been given, so get details from config file */
1223 struct mddev_ident
*array_ident
= conf_get_ident(devlist
->devname
);
1224 if (array_ident
== NULL
) {
1225 fprintf(stderr
, Name
": %s not identified in config file.\n",
1231 if (array_ident
->autof
== 0)
1232 array_ident
->autof
= autof
;
1233 rv
|= Assemble(ss
, devlist
->devname
, array_ident
,
1234 NULL
, backup_file
, invalid_backup
,
1235 readonly
, runstop
, update
,
1236 homehost
, require_homehost
,
1237 verbose
-quiet
, force
,
1241 rv
= Assemble(ss
, devlist
->devname
, &ident
,
1242 devlist
->next
, backup_file
, invalid_backup
,
1243 readonly
, runstop
, update
,
1244 homehost
, require_homehost
,
1245 verbose
-quiet
, force
,
1247 else if (devs_found
>0) {
1248 if (update
&& devs_found
> 1) {
1249 fprintf(stderr
, Name
": can only update a single array at a time\n");
1252 if (backup_file
&& devs_found
> 1) {
1253 fprintf(stderr
, Name
": can only assemble a single array when providing a backup file.\n");
1256 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1257 struct mddev_ident
*array_ident
= conf_get_ident(dv
->devname
);
1258 if (array_ident
== NULL
) {
1259 fprintf(stderr
, Name
": %s not identified in config file.\n",
1264 if (array_ident
->autof
== 0)
1265 array_ident
->autof
= autof
;
1266 rv
|= Assemble(ss
, dv
->devname
, array_ident
,
1267 NULL
, backup_file
, invalid_backup
,
1268 readonly
, runstop
, update
,
1269 homehost
, require_homehost
,
1270 verbose
-quiet
, force
,
1274 struct mddev_ident
*a
, *array_list
= conf_get_ident(NULL
);
1275 struct mddev_dev
*devlist
= conf_get_devs();
1277 int failures
, successes
;
1278 if (devlist
== NULL
) {
1279 fprintf(stderr
, Name
": No devices listed in conf file were found.\n");
1283 fprintf(stderr
, Name
": --update not meaningful with a --scan assembly.\n");
1287 fprintf(stderr
, Name
": --backup_file not meaningful with a --scan assembly.\n");
1290 for (a
= array_list
; a
; a
= a
->next
) {
1299 for (a
= array_list
; a
; a
= a
->next
) {
1304 strcasecmp(a
->devname
, "<ignore>") == 0)
1307 r
= Assemble(ss
, a
->devname
,
1310 readonly
, runstop
, NULL
,
1311 homehost
, require_homehost
,
1312 verbose
-quiet
, force
,
1322 } while (failures
&& successes
);
1323 if (homehost
&& cnt
== 0) {
1324 /* Maybe we can auto-assemble something.
1325 * Repeatedly call Assemble in auto-assemble mode
1330 ident
.autof
= autof
;
1332 struct mddev_dev
*devlist
= conf_get_devs();
1335 rv2
= Assemble(ss
, NULL
,
1350 /* Incase there are stacked devices, we need to go around again */
1352 if (cnt
== 0 && rv
== 0) {
1353 fprintf(stderr
, Name
": No arrays found in config file or automatically\n");
1357 } else if (cnt
== 0 && rv
== 0) {
1358 fprintf(stderr
, Name
": No arrays found in config file\n");
1364 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1365 if (write_behind
&& !bitmap_file
) {
1366 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1370 if (raiddisks
== 0) {
1371 fprintf(stderr
, Name
": no raid-devices specified.\n");
1377 if (strcmp(bitmap_file
, "internal")==0) {
1378 fprintf(stderr
, Name
": 'internal' bitmaps not supported with --build\n");
1383 rv
= Build(devlist
->devname
, chunk
, level
, layout
,
1384 raiddisks
, devlist
->next
, assume_clean
,
1385 bitmap_file
, bitmap_chunk
, write_behind
,
1386 delay
, verbose
-quiet
, autof
, size
);
1389 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1390 if (write_behind
&& !bitmap_file
) {
1391 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1395 if (raiddisks
== 0) {
1396 fprintf(stderr
, Name
": no raid-devices specified.\n");
1401 rv
= Create(ss
, devlist
->devname
, chunk
, level
, layout
, size
<0 ? 0 : size
,
1402 raiddisks
, sparedisks
, ident
.name
, homehost
,
1403 ident
.uuid_set
? ident
.uuid
: NULL
,
1404 devs_found
-1, devlist
->next
, runstop
, verbose
-quiet
, force
, assume_clean
,
1405 bitmap_file
, bitmap_chunk
, write_behind
, delay
, autof
);
1408 if (devmode
== 'E') {
1409 if (devlist
== NULL
&& !scan
) {
1410 fprintf(stderr
, Name
": No devices to examine\n");
1413 if (devlist
== NULL
)
1414 devlist
= conf_get_devs();
1415 if (devlist
== NULL
) {
1416 fprintf(stderr
, Name
": No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1419 if (brief
&& verbose
)
1421 rv
= Examine(devlist
, scan
?(verbose
>1?0:verbose
+1):brief
,
1423 SparcAdjust
, ss
, homehost
);
1424 } else if (devmode
== DetailPlatform
) {
1425 rv
= Detail_Platform(ss
? ss
->ss
: NULL
, ss
? scan
: 1, verbose
);
1427 if (devlist
== NULL
) {
1428 if ((devmode
=='D' || devmode
== Waitclean
) && scan
) {
1429 /* apply --detail or --wait-clean to
1430 * all devices in /proc/mdstat
1432 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1433 struct mdstat_ent
*e
;
1434 struct map_ent
*map
= NULL
;
1436 int v
= verbose
>1?0:verbose
+1;
1438 for (members
= 0; members
<= 1; members
++) {
1439 for (e
=ms
; e
; e
=e
->next
) {
1442 int member
= e
->metadata_version
&&
1443 strncmp(e
->metadata_version
,
1444 "external:/", 10) == 0;
1445 if (members
!= member
)
1447 me
= map_by_devnum(&map
, e
->devnum
);
1449 && strcmp(me
->path
, "/unknown") != 0)
1452 name
= get_md_name(e
->devnum
);
1455 fprintf(stderr
, Name
": cannot find device file for %s\n",
1460 rv
|= Detail(name
, v
,
1464 rv
|= WaitClean(name
, -1, v
);
1469 } else if (devmode
== 'S' && scan
) {
1470 /* apply --stop to all devices in /proc/mdstat */
1471 /* Due to possible stacking of devices, repeat until
1472 * nothing more can be stopped
1474 int progress
=1, err
;
1477 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1478 struct mdstat_ent
*e
;
1480 if (!progress
) last
= 1;
1481 progress
= 0; err
= 0;
1482 for (e
=ms
; e
; e
=e
->next
) {
1483 char *name
= get_md_name(e
->devnum
);
1486 fprintf(stderr
, Name
": cannot find device file for %s\n",
1490 mdfd
= open_mddev(name
, 1);
1492 if (Manage_runstop(name
, mdfd
, -1, quiet
?1:last
?0:-1))
1502 } while (!last
&& err
);
1504 } else if (devmode
== UdevRules
) {
1505 rv
= Write_rules(udev_filename
);
1507 fprintf(stderr
, Name
": No devices given.\n");
1511 for (dv
=devlist
; dv
; dv
=dv
->next
) {
1512 switch(dv
->disposition
) {
1514 rv
|= Detail(dv
->devname
,
1516 export
, test
, homehost
);
1518 case 'K': /* Zero superblock */
1520 rv
|= Kill(dv
->devname
, ss
, force
, quiet
,0);
1524 rv
|= Kill(dv
->devname
, NULL
, force
, q
, 0);
1531 rv
|= Query(dv
->devname
); continue;
1533 rv
|= ExamineBitmap(dv
->devname
, brief
, ss
); continue;
1536 rv
|= Wait(dv
->devname
); continue;
1538 rv
|= WaitClean(dv
->devname
, -1, verbose
-quiet
); continue;
1540 rv
|= Kill_subarray(dv
->devname
, subarray
, quiet
);
1542 case UpdateSubarray
:
1543 if (update
== NULL
) {
1545 Name
": -U/--update must be specified with --update-subarray\n");
1549 rv
|= Update_subarray(dv
->devname
, subarray
, update
, &ident
, quiet
);
1552 mdfd
= open_mddev(dv
->devname
, 1);
1554 switch(dv
->disposition
) {
1556 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1, quiet
); break;
1558 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1, quiet
); break;
1560 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1562 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;
1571 if (!devlist
&& !scan
) {
1572 fprintf(stderr
, Name
": Cannot monitor: need --scan or at least one device\n");
1576 if (pidfile
&& !daemonise
) {
1577 fprintf(stderr
, Name
": Cannot write a pid file when not in daemon mode\n");
1582 if (get_linux_version() > 2006016)
1583 /* mdstat responds to poll */
1588 rv
= Monitor(devlist
, mailaddr
, program
,
1589 delay
?delay
:60, daemonise
, scan
, oneshot
,
1590 dosyslog
, test
, pidfile
, increments
, spare_sharing
);
1594 if (array_size
>= 0) {
1595 /* alway impose array size first, independent of
1597 * Do not allow level or raid_disks changes at the
1598 * same time as that can be irreversibly destructive.
1602 if (raiddisks
|| level
!= UnSet
) {
1603 fprintf(stderr
, Name
": cannot change array size in same operation "
1604 "as changing raiddisks or level.\n"
1605 " Change size first, then check that data is still intact.\n");
1609 sysfs_init(&sra
, mdfd
, 0);
1610 if (array_size
== 0)
1611 err
= sysfs_set_str(&sra
, NULL
, "array_size", "default");
1613 err
= sysfs_set_num(&sra
, NULL
, "array_size", array_size
/ 2);
1616 fprintf(stderr
, Name
": --array-size setting"
1617 " is too large.\n");
1619 fprintf(stderr
, Name
": current kernel does"
1620 " not support setting --array-size\n");
1625 if (devs_found
> 1 && raiddisks
== 0) {
1627 if (size
>= 0 || chunk
|| layout_str
!= NULL
|| bitmap_file
) {
1628 fprintf(stderr
, Name
": --add cannot be used with "
1629 "other geometry changes in --grow mode\n");
1633 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1634 rv
= Grow_Add_device(devlist
->devname
, mdfd
,
1639 } else if (bitmap_file
) {
1640 if (size
>= 0 || raiddisks
|| chunk
||
1641 layout_str
!= NULL
|| devs_found
> 1) {
1642 fprintf(stderr
, Name
": --bitmap changes cannot be "
1643 "used with other geometry changes "
1644 "in --grow mode\n");
1649 delay
= DEFAULT_BITMAP_DELAY
;
1650 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, bitmap_file
,
1651 bitmap_chunk
, delay
, write_behind
, force
);
1652 } else if (size
>= 0 || raiddisks
!= 0 || layout_str
!= NULL
1653 || chunk
!= 0 || level
!= UnSet
) {
1654 rv
= Grow_reshape(devlist
->devname
, mdfd
, quiet
, backup_file
,
1655 size
, level
, layout_str
, chunk
, raiddisks
,
1657 assume_clean
, force
);
1658 } else if (array_size
< 0)
1659 fprintf(stderr
, Name
": no changes to --grow\n");
1667 fprintf(stderr
, Name
1668 ": --incremental --scan meaningless without --run.\n");
1671 if (devmode
== 'f') {
1672 fprintf(stderr
, Name
1673 ": --incremental --scan --fail not supported.\n");
1676 rv
= IncrementalScan(verbose
);
1679 if (!rebuild_map
&& !scan
) {
1680 fprintf(stderr
, Name
1681 ": --incremental requires a device.\n");
1686 if (devlist
->next
) {
1687 fprintf(stderr
, Name
1688 ": --incremental can only handle one device.\n");
1693 rv
= IncrementalRemove(devlist
->devname
, remove_path
,
1696 rv
= Incremental(devlist
->devname
, verbose
-quiet
,
1697 runstop
, ss
, homehost
,
1698 require_homehost
, autof
,