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 int grow_continue
= 0;
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 char *subarray
= NULL
;
108 char *remove_path
= NULL
;
109 char *udev_filename
= NULL
;
116 int freeze_reshape
= 0;
118 srandom(time(0) ^ getpid());
122 ident
.raid_disks
= UnSet
;
123 ident
.super_minor
= UnSet
;
125 ident
.spare_group
= NULL
;
128 ident
.bitmap_fd
= -1;
129 ident
.bitmap_file
= NULL
;
131 ident
.container
= NULL
;
134 while ((option_index
= -1) ,
135 (opt
=getopt_long(argc
, argv
,
136 shortopt
, long_options
,
137 &option_index
)) != -1) {
139 /* firstly, some mode-independent options */
149 fputs(Version
, stderr
);
159 if (mode
== ASSEMBLE
|| mode
== BUILD
|| mode
== CREATE
160 || mode
== GROW
|| mode
== INCREMENTAL
162 break; /* b means bitmap */
171 if (strcasecmp(optarg
, "<ignore>") == 0)
172 require_homehost
= 0;
178 * --offroot sets first char of argv[0] to @. This is used
179 * by systemd to signal that the tast was launched from
180 * initrd/initramfs and should be preserved during shutdown
188 fputs(Usage
, stderr
);
191 /* second, figure out the mode.
192 * Some options force the mode. Others
193 * set the mode if it isn't already
199 shortopt
= short_bitmap_options
;
207 case ReAdd
: /* re-add */
210 shortopt
= short_bitmap_options
;
214 case 'A': newmode
= ASSEMBLE
; shortopt
= short_bitmap_auto_options
; break;
215 case 'B': newmode
= BUILD
; shortopt
= short_bitmap_auto_options
; break;
216 case 'C': newmode
= CREATE
; shortopt
= short_bitmap_auto_options
; break;
217 case 'F': newmode
= MONITOR
;break;
218 case 'G': newmode
= GROW
;
219 shortopt
= short_bitmap_options
;
221 case 'I': newmode
= INCREMENTAL
;
222 shortopt
= short_bitmap_auto_options
; break;
224 newmode
= AUTODETECT
;
255 if (mode
&& newmode
== mode
) {
256 /* everybody happy ! */
257 } else if (mode
&& newmode
!= mode
) {
259 fprintf(stderr
, Name
": ");
260 if (option_index
>= 0)
261 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
263 fprintf(stderr
, "-%c", opt
);
264 fprintf(stderr
, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
265 map_num(modes
, newmode
),
266 map_num(modes
, mode
));
268 } else if (!mode
&& newmode
) {
270 if (mode
== MISC
&& devs_found
) {
271 fprintf(stderr
, Name
": No action given for %s in --misc mode\n",
273 fprintf(stderr
," Action options must come before device names\n");
277 /* special case of -c --help */
278 if ((opt
== 'c' || opt
== ConfigFile
) &&
279 ( strncmp(optarg
, "--h", 3)==0 ||
280 strncmp(optarg
, "-h", 2)==0)) {
281 fputs(Help_config
, stdout
);
285 /* If first option is a device, don't force the mode yet */
287 if (devs_found
== 0) {
288 dv
= malloc(sizeof(*dv
));
290 fprintf(stderr
, Name
": malloc failed\n");
293 dv
->devname
= optarg
;
294 dv
->disposition
= devmode
;
295 dv
->writemostly
= writemostly
;
300 devlistend
= &dv
->next
;
305 /* No mode yet, and this is the second device ... */
306 fprintf(stderr
, Name
": An option must be given to set the mode before a second device\n"
307 " (%s) is listed\n", optarg
);
310 if (option_index
>= 0)
311 fprintf(stderr
, Name
": --%s", long_options
[option_index
].name
);
313 fprintf(stderr
, Name
": -%c", opt
);
314 fprintf(stderr
, " does not set the mode, and so cannot be the first option.\n");
318 /* if we just set the mode, then done */
332 /* an undecorated option - must be a device name.
334 if (devs_found
> 0 && mode
== MANAGE
&& !devmode
) {
335 fprintf(stderr
, Name
": Must give one of -a/-r/-f"
336 " for subsequent devices at %s\n", optarg
);
339 if (devs_found
> 0 && mode
== GROW
&& !devmode
) {
340 fprintf(stderr
, Name
": Must give -a/--add for"
341 " devices to add: %s\n", optarg
);
344 dv
= malloc(sizeof(*dv
));
346 fprintf(stderr
, Name
": malloc failed\n");
349 dv
->devname
= optarg
;
350 dv
->disposition
= devmode
;
351 dv
->writemostly
= writemostly
;
356 devlistend
= &dv
->next
;
362 /* We've got a mode, and opt is now something else which
363 * could depend on the mode */
364 #define O(a,b) ((a<<16)|b)
365 switch (O(mode
,opt
)) {
367 case O(GROW
,ChunkSize
):
369 case O(CREATE
,ChunkSize
):
370 case O(BUILD
,'c'): /* chunk or rounding */
371 case O(BUILD
,ChunkSize
): /* chunk or rounding */
373 fprintf(stderr
, Name
": chunk/rounding may only be specified once. "
374 "Second value is %s.\n", optarg
);
377 chunk
= parse_size(optarg
);
378 if (chunk
< 8 || (chunk
&1)) {
379 fprintf(stderr
, Name
": invalid chunk/rounding value: %s\n",
383 /* Convert sectors to K */
387 case O(INCREMENTAL
, 'e'):
389 case O(ASSEMBLE
,'e'):
390 case O(MISC
,'e'): /* set metadata (superblock) information */
392 fprintf(stderr
, Name
": metadata information already given\n");
395 for(i
=0; !ss
&& superlist
[i
]; i
++)
396 ss
= superlist
[i
]->match_metadata_desc(optarg
);
399 fprintf(stderr
, Name
": unrecognised metadata identifier: %s\n", optarg
);
405 case O(MANAGE
,WriteMostly
):
407 case O(BUILD
,WriteMostly
):
409 case O(CREATE
,WriteMostly
):
410 /* set write-mostly for following devices */
415 /* clear write-mostly for following devices */
422 case O(BUILD
,'z'): /* size */
424 fprintf(stderr
, Name
": size may only be specified once. "
425 "Second value is %s.\n", optarg
);
428 if (strcmp(optarg
, "max")==0)
431 size
= parse_size(optarg
);
433 fprintf(stderr
, Name
": invalid size: %s\n",
437 /* convert sectors to K */
442 case O(GROW
,'Z'): /* array size */
443 if (array_size
>= 0) {
444 fprintf(stderr
, Name
": array-size may only be specified once. "
445 "Second value is %s.\n", optarg
);
448 if (strcmp(optarg
, "max") == 0)
451 array_size
= parse_size(optarg
);
452 if (array_size
<= 0) {
453 fprintf(stderr
, Name
": invalid array size: %s\n",
462 case O(BUILD
,'l'): /* set raid level*/
463 if (level
!= UnSet
) {
464 fprintf(stderr
, Name
": raid level may only be set once. "
465 "Second value is %s.\n", optarg
);
468 level
= map_name(pers
, optarg
);
469 if (level
== UnSet
) {
470 fprintf(stderr
, Name
": invalid raid level: %s\n",
474 if (level
!= 0 && level
!= LEVEL_LINEAR
&& level
!= 1 &&
475 level
!= LEVEL_MULTIPATH
&& level
!= LEVEL_FAULTY
&&
478 fprintf(stderr
, Name
": Raid level %s not permitted with --build.\n",
482 if (sparedisks
> 0 && level
< 1 && level
>= -1) {
483 fprintf(stderr
, Name
": raid level %s is incompatible with spare-devices setting.\n",
490 case O(GROW
, 'p'): /* new layout */
491 case O(GROW
, Layout
):
493 fprintf(stderr
,Name
": layout may only be sent once. "
494 "Second value was %s\n", optarg
);
498 /* 'Grow' will parse the value */
501 case O(CREATE
,'p'): /* raid5 layout */
502 case O(CREATE
,Layout
):
503 case O(BUILD
,'p'): /* faulty layout */
504 case O(BUILD
,Layout
):
505 if (layout
!= UnSet
) {
506 fprintf(stderr
,Name
": layout may only be sent once. "
507 "Second value was %s\n", optarg
);
512 fprintf(stderr
, Name
": layout not meaningful for %s arrays.\n",
513 map_num(pers
, level
));
516 fprintf(stderr
, Name
": raid level must be given before layout.\n");
520 layout
= map_name(r5layout
, optarg
);
522 fprintf(stderr
, Name
": layout %s not understood for raid5.\n",
528 layout
= map_name(r6layout
, optarg
);
530 fprintf(stderr
, Name
": layout %s not understood for raid6.\n",
537 layout
= parse_layout_10(optarg
);
539 fprintf(stderr
, Name
": layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg
);
547 layout
= parse_layout_faulty(optarg
);
549 fprintf(stderr
, Name
": layout %s not understood for faulty.\n",
557 case O(CREATE
,AssumeClean
):
558 case O(BUILD
,AssumeClean
): /* assume clean */
559 case O(GROW
,AssumeClean
):
565 case O(BUILD
,'n'): /* number of raid disks */
567 fprintf(stderr
, Name
": raid-devices set twice: %d and %s\n",
571 raiddisks
= strtol(optarg
, &c
, 10);
572 if (!optarg
[0] || *c
|| raiddisks
<=0) {
573 fprintf(stderr
, Name
": invalid number of raid devices: %s\n",
577 ident
.raid_disks
= raiddisks
;
580 case O(CREATE
,'x'): /* number of spare (eXtra) discs */
582 fprintf(stderr
,Name
": spare-devices set twice: %d and %s\n",
586 if (level
!= UnSet
&& level
<= 0 && level
>= -1) {
587 fprintf(stderr
, Name
": spare-devices setting is incompatible with raid level %d\n",
591 sparedisks
= strtol(optarg
, &c
, 10);
592 if (!optarg
[0] || *c
|| sparedisks
< 0) {
593 fprintf(stderr
, Name
": invalid number of spare-devices: %s\n",
603 case O(INCREMENTAL
,'a'):
604 case O(INCREMENTAL
,Auto
):
605 case O(ASSEMBLE
,'a'):
606 case O(ASSEMBLE
,Auto
): /* auto-creation of device node */
607 autof
= parse_auto(optarg
, "--auto flag", 0);
610 case O(CREATE
,Symlinks
):
611 case O(BUILD
,Symlinks
):
612 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
616 case O(BUILD
,'f'): /* force honouring '-n 1' */
617 case O(BUILD
,Force
): /* force honouring '-n 1' */
618 case O(GROW
,'f'): /* ditto */
619 case O(GROW
,Force
): /* ditto */
620 case O(CREATE
,'f'): /* force honouring of device list */
621 case O(CREATE
,Force
): /* force honouring of device list */
622 case O(ASSEMBLE
,'f'): /* force assembly */
623 case O(ASSEMBLE
,Force
): /* force assembly */
624 case O(MISC
,'f'): /* force zero */
625 case O(MISC
,Force
): /* force zero */
626 case O(MANAGE
,Force
): /* add device which is too large */
629 /* now for the Assemble options */
630 case O(ASSEMBLE
, FreezeReshape
): /* Freeze reshape during
632 case O(INCREMENTAL
, FreezeReshape
):
635 case O(CREATE
,'u'): /* uuid of array */
636 case O(ASSEMBLE
,'u'): /* uuid of array */
637 if (ident
.uuid_set
) {
638 fprintf(stderr
, Name
": uuid cannot be set twice. "
639 "Second value %s.\n", optarg
);
642 if (parse_uuid(optarg
, ident
.uuid
))
645 fprintf(stderr
,Name
": Bad uuid: %s\n", optarg
);
651 case O(ASSEMBLE
,'N'):
654 fprintf(stderr
, Name
": name cannot be set twice. "
655 "Second value %s.\n", optarg
);
658 if (mode
== MISC
&& !subarray
) {
659 fprintf(stderr
, Name
": -N/--name only valid with --update-subarray in misc mode\n");
662 if (strlen(optarg
) > 32) {
663 fprintf(stderr
, Name
": name '%s' is too long, 32 chars max.\n",
667 strcpy(ident
.name
, optarg
);
670 case O(ASSEMBLE
,'m'): /* super-minor for array */
671 case O(ASSEMBLE
,SuperMinor
):
672 if (ident
.super_minor
!= UnSet
) {
673 fprintf(stderr
, Name
": super-minor cannot be set twice. "
674 "Second value: %s.\n", optarg
);
677 if (strcmp(optarg
, "dev")==0)
678 ident
.super_minor
= -2;
680 ident
.super_minor
= strtoul(optarg
, &cp
, 10);
681 if (!optarg
[0] || *cp
) {
682 fprintf(stderr
, Name
": Bad super-minor number: %s.\n", optarg
);
688 case O(ASSEMBLE
,'U'): /* update the superblock */
691 fprintf(stderr
, Name
": Can only update one aspect"
692 " of superblock, both %s and %s given.\n",
696 if (mode
== MISC
&& !subarray
) {
697 fprintf(stderr
, Name
": Only subarrays can be"
698 " updated in misc mode\n");
702 if (strcmp(update
, "sparc2.2")==0)
704 if (strcmp(update
, "super-minor") == 0)
706 if (strcmp(update
, "summaries")==0)
708 if (strcmp(update
, "resync")==0)
710 if (strcmp(update
, "uuid")==0)
712 if (strcmp(update
, "name")==0)
714 if (strcmp(update
, "homehost")==0)
716 if (strcmp(update
, "devicesize")==0)
718 if (strcmp(update
, "no-bitmap")==0)
720 if (strcmp(update
, "byteorder")==0) {
723 Name
": must not set metadata"
724 " type with --update=byteorder.\n");
727 for(i
=0; !ss
&& superlist
[i
]; i
++)
728 ss
= superlist
[i
]->match_metadata_desc(
731 fprintf(stderr
, Name
": INTERNAL ERROR"
732 " cannot find 0.swap\n");
738 if (strcmp(update
,"?") == 0 ||
739 strcmp(update
, "help") == 0) {
741 fprintf(outf
, Name
": ");
745 Name
": '--update=%s' is invalid. ",
748 fprintf(outf
, "Valid --update options are:\n"
749 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
750 " 'summaries', 'homehost', 'byteorder', 'devicesize',\n"
752 exit(outf
== stdout
? 0 : 2);
755 /* update=devicesize is allowed with --re-add */
756 if (devmode
!= 'a' || re_add
!= 1) {
757 fprintf(stderr
, Name
"--update in Manage mode only"
758 " allowed with --re-add.\n");
762 fprintf(stderr
, Name
": Can only update one aspect"
763 " of superblock, both %s and %s given.\n",
768 if (strcmp(update
, "devicesize") != 0) {
769 fprintf(stderr
, Name
": only 'devicesize' can be"
770 " updated with --re-add\n");
775 case O(INCREMENTAL
,NoDegraded
):
776 fprintf(stderr
, Name
": --no-degraded is deprecated in Incremental mode\n");
777 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
778 runstop
= -1; /* --stop isn't allowed for --assemble,
779 * so we overload slightly */
782 case O(ASSEMBLE
,'c'):
783 case O(ASSEMBLE
,ConfigFile
):
784 case O(INCREMENTAL
, 'c'):
785 case O(INCREMENTAL
, ConfigFile
):
787 case O(MISC
, ConfigFile
):
789 case O(MONITOR
,ConfigFile
):
791 fprintf(stderr
, Name
": configfile cannot be set twice. "
792 "Second value is %s.\n", optarg
);
796 set_conffile(configfile
);
797 /* FIXME possibly check that config file exists. Even parse it */
799 case O(ASSEMBLE
,'s'): /* scan */
802 case O(INCREMENTAL
,'s'):
806 case O(MONITOR
,'m'): /* mail address */
807 case O(MONITOR
,EMail
):
809 fprintf(stderr
, Name
": only specify one mailaddress. %s ignored.\n",
815 case O(MONITOR
,'p'): /* alert program */
816 case O(MONITOR
,ProgramOpt
): /* alert program */
818 fprintf(stderr
, Name
": only specify one alter program. %s ignored.\n",
824 case O(MONITOR
,'r'): /* rebuild increments */
825 case O(MONITOR
,Increment
):
826 increments
= atoi(optarg
);
827 if (increments
>99 || increments
<1) {
828 fprintf(stderr
, Name
": please specify positive integer between 1 and 99 as rebuild increments.\n");
833 case O(MONITOR
,'d'): /* delay in seconds */
835 case O(BUILD
,'d'): /* delay for bitmap updates */
838 fprintf(stderr
, Name
": only specify delay once. %s ignored.\n",
841 delay
= strtol(optarg
, &c
, 10);
842 if (!optarg
[0] || *c
|| delay
<1) {
843 fprintf(stderr
, Name
": invalid delay: %s\n",
849 case O(MONITOR
,'f'): /* daemonise */
850 case O(MONITOR
,Fork
):
853 case O(MONITOR
,'i'): /* pid */
855 fprintf(stderr
, Name
": only specify one pid file. %s ignored.\n",
860 case O(MONITOR
,'1'): /* oneshot */
864 case O(MONITOR
,'t'): /* test */
867 case O(MONITOR
,'y'): /* log messages to syslog */
868 openlog("mdadm", LOG_PID
, SYSLOG_FACILITY
);
871 case O(MONITOR
, NoSharing
):
874 /* now the general management options. Some are applicable
875 * to other modes. None have arguments.
880 case O(MANAGE
,Add
): /* add a drive */
884 case O(MANAGE
,ReAdd
):
888 case O(MANAGE
,'r'): /* remove a drive */
889 case O(MANAGE
,Remove
):
892 case O(MANAGE
,'f'): /* set faulty */
894 case O(INCREMENTAL
,'f'):
895 case O(INCREMENTAL
,Remove
):
896 case O(INCREMENTAL
,Fail
): /* r for incremental is taken, use f
897 * even though we will both fail and
898 * remove the device */
901 case O(INCREMENTAL
,'R'):
903 case O(ASSEMBLE
,'R'):
905 case O(CREATE
,'R'): /* Run the array */
907 fprintf(stderr
, Name
": Cannot both Stop and Run an array\n");
914 fprintf(stderr
, Name
": Cannot both Run and Stop an array\n");
933 case O(MISC
, WaitOpt
):
934 case O(MISC
, Waitclean
):
935 case O(MISC
, DetailPlatform
):
936 case O(MISC
, KillSubarray
):
937 case O(MISC
, UpdateSubarray
):
938 if (opt
== KillSubarray
|| opt
== UpdateSubarray
) {
940 fprintf(stderr
, Name
": subarray can only"
941 " be specified once\n");
946 if (devmode
&& devmode
!= opt
&&
947 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
948 fprintf(stderr
, Name
": --examine/-E cannot be given with ");
949 if (devmode
== 'E') {
950 if (option_index
>= 0)
951 fprintf(stderr
, "--%s\n",
952 long_options
[option_index
].name
);
954 fprintf(stderr
, "-%c\n", opt
);
955 } else if (isalpha(devmode
))
956 fprintf(stderr
, "-%c\n", devmode
);
958 fprintf(stderr
, "previous option\n");
963 case O(MISC
, UdevRules
):
964 if (devmode
&& devmode
!= opt
) {
965 fprintf(stderr
, Name
": --udev-rules must"
966 " be the only option.\n");
969 fprintf(stderr
, Name
": only specify one udev "
970 "rule filename. %s ignored.\n",
973 udev_filename
= optarg
;
981 case O(MISC
, Sparc22
):
982 if (devmode
!= 'E') {
983 fprintf(stderr
, Name
": --sparc2.2 only allowed with --examine\n");
989 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
990 case O(ASSEMBLE
,Bitmap
):
992 fprintf(stderr
, Name
": bitmap file needed with -b in --assemble mode\n");
995 if (strcmp(optarg
, "internal")==0) {
996 fprintf(stderr
, Name
": there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
999 bitmap_fd
= open(optarg
, O_RDWR
);
1000 if (!*optarg
|| bitmap_fd
< 0) {
1001 fprintf(stderr
, Name
": cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
1004 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
1007 case O(ASSEMBLE
, BackupFile
):
1008 case O(GROW
, BackupFile
):
1009 /* Specify a file into which grow might place a backup,
1010 * or from which assemble might recover a backup
1013 fprintf(stderr
, Name
": backup file already specified, rejecting %s\n", optarg
);
1016 backup_file
= optarg
;
1019 case O(GROW
, Continue
):
1020 /* Continue interrupted grow
1024 case O(ASSEMBLE
, InvalidBackup
):
1025 /* Acknowledge that the backupfile is invalid, but ask
1026 * to continue anyway
1032 case O(BUILD
,Bitmap
):
1034 case O(CREATE
,Bitmap
): /* here we create the bitmap */
1035 if (strcmp(optarg
, "none") == 0) {
1036 fprintf(stderr
, Name
": '--bitmap none' only"
1037 " support for --grow\n");
1042 case O(GROW
,Bitmap
):
1043 if (strcmp(optarg
, "internal")== 0 ||
1044 strcmp(optarg
, "none")== 0 ||
1045 strchr(optarg
, '/') != NULL
) {
1046 bitmap_file
= optarg
;
1050 fprintf(stderr
, Name
": bitmap file must contain a '/', or be 'internal', or 'none'\n"
1051 " not '%s'\n", optarg
);
1054 case O(GROW
,BitmapChunk
):
1055 case O(BUILD
,BitmapChunk
):
1056 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
1057 bitmap_chunk
= parse_size(optarg
);
1058 if (bitmap_chunk
< 0 ||
1059 bitmap_chunk
& (bitmap_chunk
- 1)) {
1061 Name
": invalid bitmap chunksize: %s\n",
1065 /* convert sectors to B, chunk of 0 means 512B */
1066 bitmap_chunk
= bitmap_chunk
? bitmap_chunk
* 512 : 512;
1069 case O(GROW
, WriteBehind
):
1070 case O(BUILD
, WriteBehind
):
1071 case O(CREATE
, WriteBehind
): /* write-behind mode */
1072 write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
1074 write_behind
= strtol(optarg
, &c
, 10);
1075 if (write_behind
< 0 || *c
||
1076 write_behind
> 16383) {
1077 fprintf(stderr
, Name
": Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
1083 case O(INCREMENTAL
, 'r'):
1084 case O(INCREMENTAL
, RebuildMapOpt
):
1087 case O(INCREMENTAL
, IncrementalPath
):
1088 remove_path
= optarg
;
1091 /* We have now processed all the valid options. Anything else is
1094 if (option_index
> 0)
1095 fprintf(stderr
, Name
":option --%s not valid in %s mode\n",
1096 long_options
[option_index
].name
,
1097 map_num(modes
, mode
));
1099 fprintf(stderr
, Name
": option -%c not valid in %s mode\n",
1100 opt
, map_num(modes
, mode
));
1106 char *help_text
= Help
;
1107 if (print_help
== 2)
1108 help_text
= OptionHelp
;
1111 case ASSEMBLE
: help_text
= Help_assemble
; break;
1112 case BUILD
: help_text
= Help_build
; break;
1113 case CREATE
: help_text
= Help_create
; break;
1114 case MANAGE
: help_text
= Help_manage
; break;
1115 case MISC
: help_text
= Help_misc
; break;
1116 case MONITOR
: help_text
= Help_monitor
; break;
1117 case GROW
: help_text
= Help_grow
; break;
1118 case INCREMENTAL
:help_text
= Help_incr
; break;
1120 fputs(help_text
,stdout
);
1124 if (!mode
&& devs_found
) {
1127 if (devlist
->disposition
== 0)
1128 devlist
->disposition
= devmode
;
1131 fputs(Usage
, stderr
);
1136 struct createinfo
*ci
= conf_get_create_info();
1138 if (strcasecmp(symlinks
, "yes") == 0)
1140 else if (strcasecmp(symlinks
, "no") == 0)
1143 fprintf(stderr
, Name
": option --symlinks must be 'no' or 'yes'\n");
1147 /* Ok, got the option parsing out of the way
1148 * hopefully it's mostly right but there might be some stuff
1151 * That is mosty checked in the per-mode stuff but...
1153 * For @,B,C and A without -s, the first device listed must be an md device
1154 * we check that here and open it.
1157 if (mode
==MANAGE
|| mode
== BUILD
|| mode
== CREATE
|| mode
== GROW
||
1158 (mode
== ASSEMBLE
&& ! scan
)) {
1159 if (devs_found
< 1) {
1160 fprintf(stderr
, Name
": an md device must be given in this mode\n");
1163 if ((int)ident
.super_minor
== -2 && autof
) {
1164 fprintf(stderr
, Name
": --super-minor=dev is incompatible with --auto\n");
1167 if (mode
== MANAGE
|| mode
== GROW
) {
1168 mdfd
= open_mddev(devlist
->devname
, 1);
1172 /* non-existent device is OK */
1173 mdfd
= open_mddev(devlist
->devname
, 0);
1175 fprintf(stderr
, Name
": device %s exists but is not an "
1176 "md array.\n", devlist
->devname
);
1179 if ((int)ident
.super_minor
== -2) {
1182 fprintf(stderr
, Name
": --super-minor=dev given, and "
1183 "listed device %s doesn't exist.\n",
1188 ident
.super_minor
= minor(stb
.st_rdev
);
1190 if (mdfd
>= 0 && mode
!= MANAGE
&& mode
!= GROW
) {
1191 /* We don't really want this open yet, we just might
1192 * have wanted to check some things
1200 if (raiddisks
== 1 && !force
&& level
!= -5) {
1201 fprintf(stderr
, Name
": '1' is an unusual number of drives for an array, so it is probably\n"
1202 " a mistake. If you really mean it you will need to specify --force before\n"
1203 " setting the number of drives.\n");
1208 if (homehost
== NULL
)
1209 homehost
= conf_get_homehost(&require_homehost
);
1210 if (homehost
== NULL
|| strcasecmp(homehost
, "<system>")==0) {
1211 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
1212 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
1213 homehost
= sys_hostname
;
1216 if (homehost
&& (!homehost
[0] || strcasecmp(homehost
, "<none>") == 0)) {
1218 require_homehost
= 0;
1221 if (!((mode
== MISC
&& devmode
== 'E')
1222 || (mode
== MONITOR
&& spare_sharing
== 0)) &&
1224 fprintf(stderr
, Name
": must be super-user to perform this action\n");
1228 ident
.autof
= autof
;
1233 /* readonly, add/remove, readwrite, runstop */
1235 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1236 if (!rv
&& devs_found
>1)
1237 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
1238 devlist
->next
, verbose
-quiet
, test
,
1240 if (!rv
&& readonly
< 0)
1241 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1243 rv
= Manage_runstop(devlist
->devname
, mdfd
, runstop
, quiet
);
1246 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1247 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !scan
) {
1248 /* Only a device has been given, so get details from config file */
1249 struct mddev_ident
*array_ident
= conf_get_ident(devlist
->devname
);
1250 if (array_ident
== NULL
) {
1251 fprintf(stderr
, Name
": %s not identified in config file.\n",
1257 if (array_ident
->autof
== 0)
1258 array_ident
->autof
= autof
;
1259 rv
|= Assemble(ss
, devlist
->devname
, array_ident
,
1260 NULL
, backup_file
, invalid_backup
,
1261 readonly
, runstop
, update
,
1262 homehost
, require_homehost
,
1263 verbose
-quiet
, force
,
1267 rv
= Assemble(ss
, devlist
->devname
, &ident
,
1268 devlist
->next
, backup_file
, invalid_backup
,
1269 readonly
, runstop
, update
,
1270 homehost
, require_homehost
,
1271 verbose
-quiet
, force
,
1273 else if (devs_found
>0) {
1274 if (update
&& devs_found
> 1) {
1275 fprintf(stderr
, Name
": can only update a single array at a time\n");
1278 if (backup_file
&& devs_found
> 1) {
1279 fprintf(stderr
, Name
": can only assemble a single array when providing a backup file.\n");
1282 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1283 struct mddev_ident
*array_ident
= conf_get_ident(dv
->devname
);
1284 if (array_ident
== NULL
) {
1285 fprintf(stderr
, Name
": %s not identified in config file.\n",
1290 if (array_ident
->autof
== 0)
1291 array_ident
->autof
= autof
;
1292 rv
|= Assemble(ss
, dv
->devname
, array_ident
,
1293 NULL
, backup_file
, invalid_backup
,
1294 readonly
, runstop
, update
,
1295 homehost
, require_homehost
,
1296 verbose
-quiet
, force
,
1300 struct mddev_ident
*a
, *array_list
= conf_get_ident(NULL
);
1301 struct mddev_dev
*devlist
= conf_get_devs();
1302 struct map_ent
*map
= NULL
;
1304 int failures
, successes
;
1306 if (conf_verify_devnames(array_list
)) {
1307 fprintf(stderr
, Name
1308 ": Duplicate MD device names in "
1309 "conf file were found.\n");
1312 if (devlist
== NULL
) {
1313 fprintf(stderr
, Name
": No devices listed in conf file were found.\n");
1317 fprintf(stderr
, Name
": --update not meaningful with a --scan assembly.\n");
1321 fprintf(stderr
, Name
": --backup_file not meaningful with a --scan assembly.\n");
1324 for (a
= array_list
; a
; a
= a
->next
) {
1330 fprintf(stderr
, Name
" %s: failed to get "
1331 "exclusive lock on mapfile\n",
1337 for (a
= array_list
; a
; a
= a
->next
) {
1342 strcasecmp(a
->devname
, "<ignore>") == 0)
1345 r
= Assemble(ss
, a
->devname
,
1348 readonly
, runstop
, NULL
,
1349 homehost
, require_homehost
,
1350 verbose
-quiet
, force
,
1360 } while (failures
&& successes
);
1361 if (homehost
&& cnt
== 0) {
1362 /* Maybe we can auto-assemble something.
1363 * Repeatedly call Assemble in auto-assemble mode
1368 ident
.autof
= autof
;
1370 struct mddev_dev
*devlist
= conf_get_devs();
1373 rv2
= Assemble(ss
, NULL
,
1388 /* Incase there are stacked devices, we need to go around again */
1390 if (cnt
== 0 && rv
== 0) {
1391 fprintf(stderr
, Name
": No arrays found in config file or automatically\n");
1395 } else if (cnt
== 0 && rv
== 0) {
1396 fprintf(stderr
, Name
": No arrays found in config file\n");
1403 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1404 if (write_behind
&& !bitmap_file
) {
1405 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1409 if (raiddisks
== 0) {
1410 fprintf(stderr
, Name
": no raid-devices specified.\n");
1416 if (strcmp(bitmap_file
, "internal")==0) {
1417 fprintf(stderr
, Name
": 'internal' bitmaps not supported with --build\n");
1422 rv
= Build(devlist
->devname
, chunk
, level
, layout
,
1423 raiddisks
, devlist
->next
, assume_clean
,
1424 bitmap_file
, bitmap_chunk
, write_behind
,
1425 delay
, verbose
-quiet
, autof
, size
);
1428 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1429 if (write_behind
&& !bitmap_file
) {
1430 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1434 if (raiddisks
== 0) {
1435 fprintf(stderr
, Name
": no raid-devices specified.\n");
1440 rv
= Create(ss
, devlist
->devname
, chunk
, level
, layout
, size
<0 ? 0 : size
,
1441 raiddisks
, sparedisks
, ident
.name
, homehost
,
1442 ident
.uuid_set
? ident
.uuid
: NULL
,
1443 devs_found
-1, devlist
->next
, runstop
, verbose
-quiet
, force
, assume_clean
,
1444 bitmap_file
, bitmap_chunk
, write_behind
, delay
, autof
);
1447 if (devmode
== 'E') {
1448 if (devlist
== NULL
&& !scan
) {
1449 fprintf(stderr
, Name
": No devices to examine\n");
1452 if (devlist
== NULL
)
1453 devlist
= conf_get_devs();
1454 if (devlist
== NULL
) {
1455 fprintf(stderr
, Name
": No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1458 if (brief
&& verbose
)
1460 rv
= Examine(devlist
, scan
?(verbose
>1?0:verbose
+1):brief
,
1462 SparcAdjust
, ss
, homehost
);
1463 } else if (devmode
== DetailPlatform
) {
1464 rv
= Detail_Platform(ss
? ss
->ss
: NULL
, ss
? scan
: 1, verbose
);
1466 if (devlist
== NULL
) {
1467 if ((devmode
=='D' || devmode
== Waitclean
) && scan
) {
1468 /* apply --detail or --wait-clean to
1469 * all devices in /proc/mdstat
1471 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1472 struct mdstat_ent
*e
;
1473 struct map_ent
*map
= NULL
;
1475 int v
= verbose
>1?0:verbose
+1;
1477 for (members
= 0; members
<= 1; members
++) {
1478 for (e
=ms
; e
; e
=e
->next
) {
1481 int member
= e
->metadata_version
&&
1482 strncmp(e
->metadata_version
,
1483 "external:/", 10) == 0;
1484 if (members
!= member
)
1486 me
= map_by_devnum(&map
, e
->devnum
);
1488 && strcmp(me
->path
, "/unknown") != 0)
1491 name
= get_md_name(e
->devnum
);
1494 fprintf(stderr
, Name
": cannot find device file for %s\n",
1499 rv
|= Detail(name
, v
,
1503 rv
|= WaitClean(name
, -1, v
);
1508 } else if (devmode
== 'S' && scan
) {
1509 /* apply --stop to all devices in /proc/mdstat */
1510 /* Due to possible stacking of devices, repeat until
1511 * nothing more can be stopped
1513 int progress
=1, err
;
1516 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1517 struct mdstat_ent
*e
;
1519 if (!progress
) last
= 1;
1520 progress
= 0; err
= 0;
1521 for (e
=ms
; e
; e
=e
->next
) {
1522 char *name
= get_md_name(e
->devnum
);
1525 fprintf(stderr
, Name
": cannot find device file for %s\n",
1529 mdfd
= open_mddev(name
, 1);
1531 if (Manage_runstop(name
, mdfd
, -1, quiet
?1:last
?0:-1))
1541 } while (!last
&& err
);
1543 } else if (devmode
== UdevRules
) {
1544 rv
= Write_rules(udev_filename
);
1546 fprintf(stderr
, Name
": No devices given.\n");
1550 for (dv
=devlist
; dv
; dv
=dv
->next
) {
1551 switch(dv
->disposition
) {
1553 rv
|= Detail(dv
->devname
,
1555 export
, test
, homehost
);
1557 case 'K': /* Zero superblock */
1559 rv
|= Kill(dv
->devname
, ss
, force
, quiet
,0);
1563 rv
|= Kill(dv
->devname
, NULL
, force
, q
, 0);
1570 rv
|= Query(dv
->devname
); continue;
1572 rv
|= ExamineBitmap(dv
->devname
, brief
, ss
); continue;
1575 rv
|= Wait(dv
->devname
); continue;
1577 rv
|= WaitClean(dv
->devname
, -1, verbose
-quiet
); continue;
1579 rv
|= Kill_subarray(dv
->devname
, subarray
, quiet
);
1581 case UpdateSubarray
:
1582 if (update
== NULL
) {
1584 Name
": -U/--update must be specified with --update-subarray\n");
1588 rv
|= Update_subarray(dv
->devname
, subarray
, update
, &ident
, quiet
);
1591 mdfd
= open_mddev(dv
->devname
, 1);
1593 switch(dv
->disposition
) {
1595 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1, quiet
); break;
1597 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1, quiet
); break;
1599 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1601 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;
1610 if (!devlist
&& !scan
) {
1611 fprintf(stderr
, Name
": Cannot monitor: need --scan or at least one device\n");
1615 if (pidfile
&& !daemonise
) {
1616 fprintf(stderr
, Name
": Cannot write a pid file when not in daemon mode\n");
1621 if (get_linux_version() > 2006016)
1622 /* mdstat responds to poll */
1627 rv
= Monitor(devlist
, mailaddr
, program
,
1628 delay
?delay
:60, daemonise
, scan
, oneshot
,
1629 dosyslog
, test
, pidfile
, increments
, spare_sharing
);
1633 if (array_size
>= 0) {
1634 /* alway impose array size first, independent of
1636 * Do not allow level or raid_disks changes at the
1637 * same time as that can be irreversibly destructive.
1641 if (raiddisks
|| level
!= UnSet
) {
1642 fprintf(stderr
, Name
": cannot change array size in same operation "
1643 "as changing raiddisks or level.\n"
1644 " Change size first, then check that data is still intact.\n");
1648 sysfs_init(&sra
, mdfd
, 0);
1649 if (array_size
== 0)
1650 err
= sysfs_set_str(&sra
, NULL
, "array_size", "default");
1652 err
= sysfs_set_num(&sra
, NULL
, "array_size", array_size
/ 2);
1655 fprintf(stderr
, Name
": --array-size setting"
1656 " is too large.\n");
1658 fprintf(stderr
, Name
": current kernel does"
1659 " not support setting --array-size\n");
1664 if (devs_found
> 1 && raiddisks
== 0) {
1666 if (size
>= 0 || chunk
|| layout_str
!= NULL
|| bitmap_file
) {
1667 fprintf(stderr
, Name
": --add cannot be used with "
1668 "other geometry changes in --grow mode\n");
1672 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1673 rv
= Grow_Add_device(devlist
->devname
, mdfd
,
1678 } else if (bitmap_file
) {
1679 if (size
>= 0 || raiddisks
|| chunk
||
1680 layout_str
!= NULL
|| devs_found
> 1) {
1681 fprintf(stderr
, Name
": --bitmap changes cannot be "
1682 "used with other geometry changes "
1683 "in --grow mode\n");
1688 delay
= DEFAULT_BITMAP_DELAY
;
1689 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, bitmap_file
,
1690 bitmap_chunk
, delay
, write_behind
, force
);
1691 } else if (grow_continue
)
1692 rv
= Grow_continue_command(devlist
->devname
,
1695 else if (size
>= 0 || raiddisks
!= 0 || layout_str
!= NULL
1696 || chunk
!= 0 || level
!= UnSet
) {
1697 rv
= Grow_reshape(devlist
->devname
, mdfd
, quiet
, backup_file
,
1698 size
, level
, layout_str
, chunk
, raiddisks
,
1700 assume_clean
, force
);
1701 } else if (array_size
< 0)
1702 fprintf(stderr
, Name
": no changes to --grow\n");
1710 fprintf(stderr
, Name
1711 ": --incremental --scan meaningless without --run.\n");
1714 if (devmode
== 'f') {
1715 fprintf(stderr
, Name
1716 ": --incremental --scan --fail not supported.\n");
1719 rv
= IncrementalScan(verbose
);
1722 if (!rebuild_map
&& !scan
) {
1723 fprintf(stderr
, Name
1724 ": --incremental requires a device.\n");
1729 if (devlist
->next
) {
1730 fprintf(stderr
, Name
1731 ": --incremental can only handle one device.\n");
1736 rv
= IncrementalRemove(devlist
->devname
, remove_path
,
1739 rv
= Incremental(devlist
->devname
, verbose
-quiet
,
1740 runstop
, ss
, homehost
,
1741 require_homehost
, autof
,