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
;
77 char *symlinks
= NULL
;
78 int grow_continue
= 0;
79 /* autof indicates whether and how to create device node.
80 * bottom 3 bits are style. Rest (when shifted) are number of parts
82 * 1 - don't create (no)
83 * 2 - if is_standard, then create (yes)
84 * 3 - create as 'md' - reject is_standard mdp (md)
85 * 4 - create as 'mdp' - reject is_standard md (mdp)
86 * 5 - default to md if not is_standard (md in config file)
87 * 6 - default to mdp if not is_standard (part, or mdp in config file)
91 char *homehost
= NULL
;
92 char sys_hostname
[256];
93 int require_homehost
= 1;
94 char *mailaddr
= NULL
;
101 int spare_sharing
= 1;
102 struct supertype
*ss
= NULL
;
105 char *shortopt
= short_options
;
108 char *subarray
= NULL
;
109 char *remove_path
= NULL
;
110 char *udev_filename
= NULL
;
117 int freeze_reshape
= 0;
119 srandom(time(0) ^ getpid());
123 ident
.raid_disks
= UnSet
;
124 ident
.super_minor
= UnSet
;
126 ident
.spare_group
= NULL
;
129 ident
.bitmap_fd
= -1;
130 ident
.bitmap_file
= NULL
;
132 ident
.container
= NULL
;
135 while ((option_index
= -1) ,
136 (opt
=getopt_long(argc
, argv
,
137 shortopt
, long_options
,
138 &option_index
)) != -1) {
140 /* firstly, some mode-independent options */
150 fputs(Version
, stderr
);
160 if (mode
== ASSEMBLE
|| mode
== BUILD
|| mode
== CREATE
161 || mode
== GROW
|| mode
== INCREMENTAL
163 break; /* b means bitmap */
172 if (strcasecmp(optarg
, "<ignore>") == 0)
173 require_homehost
= 0;
179 * --offroot sets first char of argv[0] to @. This is used
180 * by systemd to signal that the tast was launched from
181 * initrd/initramfs and should be preserved during shutdown
191 if (asprintf(&prefer
, "/%s/", optarg
) <= 0)
197 fputs(Usage
, stderr
);
200 /* second, figure out the mode.
201 * Some options force the mode. Others
202 * set the mode if it isn't already
208 shortopt
= short_bitmap_options
;
216 case ReAdd
: /* re-add */
219 shortopt
= short_bitmap_options
;
223 case 'A': newmode
= ASSEMBLE
; shortopt
= short_bitmap_auto_options
; break;
224 case 'B': newmode
= BUILD
; shortopt
= short_bitmap_auto_options
; break;
225 case 'C': newmode
= CREATE
; shortopt
= short_bitmap_auto_options
; break;
226 case 'F': newmode
= MONITOR
;break;
227 case 'G': newmode
= GROW
;
228 shortopt
= short_bitmap_options
;
230 case 'I': newmode
= INCREMENTAL
;
231 shortopt
= short_bitmap_auto_options
; break;
233 newmode
= AUTODETECT
;
264 if (mode
&& newmode
== mode
) {
265 /* everybody happy ! */
266 } else if (mode
&& newmode
!= mode
) {
268 fprintf(stderr
, Name
": ");
269 if (option_index
>= 0)
270 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
272 fprintf(stderr
, "-%c", opt
);
273 fprintf(stderr
, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
274 map_num(modes
, newmode
),
275 map_num(modes
, mode
));
277 } else if (!mode
&& newmode
) {
279 if (mode
== MISC
&& devs_found
) {
280 fprintf(stderr
, Name
": No action given for %s in --misc mode\n",
282 fprintf(stderr
," Action options must come before device names\n");
286 /* special case of -c --help */
287 if ((opt
== 'c' || opt
== ConfigFile
) &&
288 ( strncmp(optarg
, "--h", 3)==0 ||
289 strncmp(optarg
, "-h", 2)==0)) {
290 fputs(Help_config
, stdout
);
294 /* If first option is a device, don't force the mode yet */
296 if (devs_found
== 0) {
297 dv
= malloc(sizeof(*dv
));
299 fprintf(stderr
, Name
": malloc failed\n");
302 dv
->devname
= optarg
;
303 dv
->disposition
= devmode
;
304 dv
->writemostly
= writemostly
;
309 devlistend
= &dv
->next
;
314 /* No mode yet, and this is the second device ... */
315 fprintf(stderr
, Name
": An option must be given to set the mode before a second device\n"
316 " (%s) is listed\n", optarg
);
319 if (option_index
>= 0)
320 fprintf(stderr
, Name
": --%s", long_options
[option_index
].name
);
322 fprintf(stderr
, Name
": -%c", opt
);
323 fprintf(stderr
, " does not set the mode, and so cannot be the first option.\n");
327 /* if we just set the mode, then done */
341 /* an undecorated option - must be a device name.
343 if (devs_found
> 0 && mode
== MANAGE
&& !devmode
) {
344 fprintf(stderr
, Name
": Must give one of -a/-r/-f"
345 " for subsequent devices at %s\n", optarg
);
348 if (devs_found
> 0 && mode
== GROW
&& !devmode
) {
349 fprintf(stderr
, Name
": Must give -a/--add for"
350 " devices to add: %s\n", optarg
);
353 dv
= malloc(sizeof(*dv
));
355 fprintf(stderr
, Name
": malloc failed\n");
358 dv
->devname
= optarg
;
359 dv
->disposition
= devmode
;
360 dv
->writemostly
= writemostly
;
365 devlistend
= &dv
->next
;
371 /* We've got a mode, and opt is now something else which
372 * could depend on the mode */
373 #define O(a,b) ((a<<16)|b)
374 switch (O(mode
,opt
)) {
376 case O(GROW
,ChunkSize
):
378 case O(CREATE
,ChunkSize
):
379 case O(BUILD
,'c'): /* chunk or rounding */
380 case O(BUILD
,ChunkSize
): /* chunk or rounding */
382 fprintf(stderr
, Name
": chunk/rounding may only be specified once. "
383 "Second value is %s.\n", optarg
);
386 chunk
= parse_size(optarg
);
387 if (chunk
< 8 || (chunk
&1)) {
388 fprintf(stderr
, Name
": invalid chunk/rounding value: %s\n",
392 /* Convert sectors to K */
396 case O(INCREMENTAL
, 'e'):
398 case O(ASSEMBLE
,'e'):
399 case O(MISC
,'e'): /* set metadata (superblock) information */
401 fprintf(stderr
, Name
": metadata information already given\n");
404 for(i
=0; !ss
&& superlist
[i
]; i
++)
405 ss
= superlist
[i
]->match_metadata_desc(optarg
);
408 fprintf(stderr
, Name
": unrecognised metadata identifier: %s\n", optarg
);
414 case O(MANAGE
,WriteMostly
):
416 case O(BUILD
,WriteMostly
):
418 case O(CREATE
,WriteMostly
):
419 /* set write-mostly for following devices */
424 /* clear write-mostly for following devices */
431 case O(BUILD
,'z'): /* size */
433 fprintf(stderr
, Name
": size may only be specified once. "
434 "Second value is %s.\n", optarg
);
437 if (strcmp(optarg
, "max")==0)
440 size
= parse_size(optarg
);
442 fprintf(stderr
, Name
": invalid size: %s\n",
446 /* convert sectors to K */
451 case O(GROW
,'Z'): /* array size */
452 if (array_size
>= 0) {
453 fprintf(stderr
, Name
": array-size may only be specified once. "
454 "Second value is %s.\n", optarg
);
457 if (strcmp(optarg
, "max") == 0)
460 array_size
= parse_size(optarg
);
461 if (array_size
<= 0) {
462 fprintf(stderr
, Name
": invalid array size: %s\n",
471 case O(BUILD
,'l'): /* set raid level*/
472 if (level
!= UnSet
) {
473 fprintf(stderr
, Name
": raid level may only be set once. "
474 "Second value is %s.\n", optarg
);
477 level
= map_name(pers
, optarg
);
478 if (level
== UnSet
) {
479 fprintf(stderr
, Name
": invalid raid level: %s\n",
483 if (level
!= 0 && level
!= LEVEL_LINEAR
&& level
!= 1 &&
484 level
!= LEVEL_MULTIPATH
&& level
!= LEVEL_FAULTY
&&
487 fprintf(stderr
, Name
": Raid level %s not permitted with --build.\n",
491 if (sparedisks
> 0 && level
< 1 && level
>= -1) {
492 fprintf(stderr
, Name
": raid level %s is incompatible with spare-devices setting.\n",
499 case O(GROW
, 'p'): /* new layout */
500 case O(GROW
, Layout
):
502 fprintf(stderr
,Name
": layout may only be sent once. "
503 "Second value was %s\n", optarg
);
507 /* 'Grow' will parse the value */
510 case O(CREATE
,'p'): /* raid5 layout */
511 case O(CREATE
,Layout
):
512 case O(BUILD
,'p'): /* faulty layout */
513 case O(BUILD
,Layout
):
514 if (layout
!= UnSet
) {
515 fprintf(stderr
,Name
": layout may only be sent once. "
516 "Second value was %s\n", optarg
);
521 fprintf(stderr
, Name
": layout not meaningful for %s arrays.\n",
522 map_num(pers
, level
));
525 fprintf(stderr
, Name
": raid level must be given before layout.\n");
529 layout
= map_name(r5layout
, optarg
);
531 fprintf(stderr
, Name
": layout %s not understood for raid5.\n",
537 layout
= map_name(r6layout
, optarg
);
539 fprintf(stderr
, Name
": layout %s not understood for raid6.\n",
546 layout
= parse_layout_10(optarg
);
548 fprintf(stderr
, Name
": layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg
);
556 layout
= parse_layout_faulty(optarg
);
558 fprintf(stderr
, Name
": layout %s not understood for faulty.\n",
566 case O(CREATE
,AssumeClean
):
567 case O(BUILD
,AssumeClean
): /* assume clean */
568 case O(GROW
,AssumeClean
):
574 case O(BUILD
,'n'): /* number of raid disks */
576 fprintf(stderr
, Name
": raid-devices set twice: %d and %s\n",
580 raiddisks
= strtol(optarg
, &c
, 10);
581 if (!optarg
[0] || *c
|| raiddisks
<=0) {
582 fprintf(stderr
, Name
": invalid number of raid devices: %s\n",
586 ident
.raid_disks
= raiddisks
;
589 case O(CREATE
,'x'): /* number of spare (eXtra) discs */
591 fprintf(stderr
,Name
": spare-devices set twice: %d and %s\n",
595 if (level
!= UnSet
&& level
<= 0 && level
>= -1) {
596 fprintf(stderr
, Name
": spare-devices setting is incompatible with raid level %d\n",
600 sparedisks
= strtol(optarg
, &c
, 10);
601 if (!optarg
[0] || *c
|| sparedisks
< 0) {
602 fprintf(stderr
, Name
": invalid number of spare-devices: %s\n",
612 case O(INCREMENTAL
,'a'):
613 case O(INCREMENTAL
,Auto
):
614 case O(ASSEMBLE
,'a'):
615 case O(ASSEMBLE
,Auto
): /* auto-creation of device node */
616 autof
= parse_auto(optarg
, "--auto flag", 0);
619 case O(CREATE
,Symlinks
):
620 case O(BUILD
,Symlinks
):
621 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
625 case O(BUILD
,'f'): /* force honouring '-n 1' */
626 case O(BUILD
,Force
): /* force honouring '-n 1' */
627 case O(GROW
,'f'): /* ditto */
628 case O(GROW
,Force
): /* ditto */
629 case O(CREATE
,'f'): /* force honouring of device list */
630 case O(CREATE
,Force
): /* force honouring of device list */
631 case O(ASSEMBLE
,'f'): /* force assembly */
632 case O(ASSEMBLE
,Force
): /* force assembly */
633 case O(MISC
,'f'): /* force zero */
634 case O(MISC
,Force
): /* force zero */
635 case O(MANAGE
,Force
): /* add device which is too large */
638 /* now for the Assemble options */
639 case O(ASSEMBLE
, FreezeReshape
): /* Freeze reshape during
641 case O(INCREMENTAL
, FreezeReshape
):
644 case O(CREATE
,'u'): /* uuid of array */
645 case O(ASSEMBLE
,'u'): /* uuid of array */
646 if (ident
.uuid_set
) {
647 fprintf(stderr
, Name
": uuid cannot be set twice. "
648 "Second value %s.\n", optarg
);
651 if (parse_uuid(optarg
, ident
.uuid
))
654 fprintf(stderr
,Name
": Bad uuid: %s\n", optarg
);
660 case O(ASSEMBLE
,'N'):
663 fprintf(stderr
, Name
": name cannot be set twice. "
664 "Second value %s.\n", optarg
);
667 if (mode
== MISC
&& !subarray
) {
668 fprintf(stderr
, Name
": -N/--name only valid with --update-subarray in misc mode\n");
671 if (strlen(optarg
) > 32) {
672 fprintf(stderr
, Name
": name '%s' is too long, 32 chars max.\n",
676 strcpy(ident
.name
, optarg
);
679 case O(ASSEMBLE
,'m'): /* super-minor for array */
680 case O(ASSEMBLE
,SuperMinor
):
681 if (ident
.super_minor
!= UnSet
) {
682 fprintf(stderr
, Name
": super-minor cannot be set twice. "
683 "Second value: %s.\n", optarg
);
686 if (strcmp(optarg
, "dev")==0)
687 ident
.super_minor
= -2;
689 ident
.super_minor
= strtoul(optarg
, &cp
, 10);
690 if (!optarg
[0] || *cp
) {
691 fprintf(stderr
, Name
": Bad super-minor number: %s.\n", optarg
);
697 case O(ASSEMBLE
,'U'): /* update the superblock */
700 fprintf(stderr
, Name
": Can only update one aspect"
701 " of superblock, both %s and %s given.\n",
705 if (mode
== MISC
&& !subarray
) {
706 fprintf(stderr
, Name
": Only subarrays can be"
707 " updated in misc mode\n");
711 if (strcmp(update
, "sparc2.2")==0)
713 if (strcmp(update
, "super-minor") == 0)
715 if (strcmp(update
, "summaries")==0)
717 if (strcmp(update
, "resync")==0)
719 if (strcmp(update
, "uuid")==0)
721 if (strcmp(update
, "name")==0)
723 if (strcmp(update
, "homehost")==0)
725 if (strcmp(update
, "devicesize")==0)
727 if (strcmp(update
, "no-bitmap")==0)
729 if (strcmp(update
, "byteorder")==0) {
732 Name
": must not set metadata"
733 " type with --update=byteorder.\n");
736 for(i
=0; !ss
&& superlist
[i
]; i
++)
737 ss
= superlist
[i
]->match_metadata_desc(
740 fprintf(stderr
, Name
": INTERNAL ERROR"
741 " cannot find 0.swap\n");
747 if (strcmp(update
,"?") == 0 ||
748 strcmp(update
, "help") == 0) {
750 fprintf(outf
, Name
": ");
754 Name
": '--update=%s' is invalid. ",
757 fprintf(outf
, "Valid --update options are:\n"
758 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
759 " 'summaries', 'homehost', 'byteorder', 'devicesize',\n"
761 exit(outf
== stdout
? 0 : 2);
764 /* update=devicesize is allowed with --re-add */
765 if (devmode
!= 'a' || re_add
!= 1) {
766 fprintf(stderr
, Name
"--update in Manage mode only"
767 " allowed with --re-add.\n");
771 fprintf(stderr
, Name
": Can only update one aspect"
772 " of superblock, both %s and %s given.\n",
777 if (strcmp(update
, "devicesize") != 0) {
778 fprintf(stderr
, Name
": only 'devicesize' can be"
779 " updated with --re-add\n");
784 case O(INCREMENTAL
,NoDegraded
):
785 fprintf(stderr
, Name
": --no-degraded is deprecated in Incremental mode\n");
786 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
787 runstop
= -1; /* --stop isn't allowed for --assemble,
788 * so we overload slightly */
791 case O(ASSEMBLE
,'c'):
792 case O(ASSEMBLE
,ConfigFile
):
793 case O(INCREMENTAL
, 'c'):
794 case O(INCREMENTAL
, ConfigFile
):
796 case O(MISC
, ConfigFile
):
798 case O(MONITOR
,ConfigFile
):
800 fprintf(stderr
, Name
": configfile cannot be set twice. "
801 "Second value is %s.\n", optarg
);
805 set_conffile(configfile
);
806 /* FIXME possibly check that config file exists. Even parse it */
808 case O(ASSEMBLE
,'s'): /* scan */
811 case O(INCREMENTAL
,'s'):
815 case O(MONITOR
,'m'): /* mail address */
816 case O(MONITOR
,EMail
):
818 fprintf(stderr
, Name
": only specify one mailaddress. %s ignored.\n",
824 case O(MONITOR
,'p'): /* alert program */
825 case O(MONITOR
,ProgramOpt
): /* alert program */
827 fprintf(stderr
, Name
": only specify one alter program. %s ignored.\n",
833 case O(MONITOR
,'r'): /* rebuild increments */
834 case O(MONITOR
,Increment
):
835 increments
= atoi(optarg
);
836 if (increments
>99 || increments
<1) {
837 fprintf(stderr
, Name
": please specify positive integer between 1 and 99 as rebuild increments.\n");
842 case O(MONITOR
,'d'): /* delay in seconds */
844 case O(BUILD
,'d'): /* delay for bitmap updates */
847 fprintf(stderr
, Name
": only specify delay once. %s ignored.\n",
850 delay
= strtol(optarg
, &c
, 10);
851 if (!optarg
[0] || *c
|| delay
<1) {
852 fprintf(stderr
, Name
": invalid delay: %s\n",
858 case O(MONITOR
,'f'): /* daemonise */
859 case O(MONITOR
,Fork
):
862 case O(MONITOR
,'i'): /* pid */
864 fprintf(stderr
, Name
": only specify one pid file. %s ignored.\n",
869 case O(MONITOR
,'1'): /* oneshot */
873 case O(MONITOR
,'t'): /* test */
876 case O(MONITOR
,'y'): /* log messages to syslog */
877 openlog("mdadm", LOG_PID
, SYSLOG_FACILITY
);
880 case O(MONITOR
, NoSharing
):
883 /* now the general management options. Some are applicable
884 * to other modes. None have arguments.
889 case O(MANAGE
,Add
): /* add a drive */
893 case O(MANAGE
,ReAdd
):
897 case O(MANAGE
,'r'): /* remove a drive */
898 case O(MANAGE
,Remove
):
901 case O(MANAGE
,'f'): /* set faulty */
903 case O(INCREMENTAL
,'f'):
904 case O(INCREMENTAL
,Remove
):
905 case O(INCREMENTAL
,Fail
): /* r for incremental is taken, use f
906 * even though we will both fail and
907 * remove the device */
910 case O(INCREMENTAL
,'R'):
912 case O(ASSEMBLE
,'R'):
914 case O(CREATE
,'R'): /* Run the array */
916 fprintf(stderr
, Name
": Cannot both Stop and Run an array\n");
923 fprintf(stderr
, Name
": Cannot both Run and Stop an array\n");
942 case O(MISC
, WaitOpt
):
943 case O(MISC
, Waitclean
):
944 case O(MISC
, DetailPlatform
):
945 case O(MISC
, KillSubarray
):
946 case O(MISC
, UpdateSubarray
):
947 if (opt
== KillSubarray
|| opt
== UpdateSubarray
) {
949 fprintf(stderr
, Name
": subarray can only"
950 " be specified once\n");
955 if (devmode
&& devmode
!= opt
&&
956 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
957 fprintf(stderr
, Name
": --examine/-E cannot be given with ");
958 if (devmode
== 'E') {
959 if (option_index
>= 0)
960 fprintf(stderr
, "--%s\n",
961 long_options
[option_index
].name
);
963 fprintf(stderr
, "-%c\n", opt
);
964 } else if (isalpha(devmode
))
965 fprintf(stderr
, "-%c\n", devmode
);
967 fprintf(stderr
, "previous option\n");
972 case O(MISC
, UdevRules
):
973 if (devmode
&& devmode
!= opt
) {
974 fprintf(stderr
, Name
": --udev-rules must"
975 " be the only option.\n");
978 fprintf(stderr
, Name
": only specify one udev "
979 "rule filename. %s ignored.\n",
982 udev_filename
= optarg
;
990 case O(MISC
, Sparc22
):
991 if (devmode
!= 'E') {
992 fprintf(stderr
, Name
": --sparc2.2 only allowed with --examine\n");
998 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
999 case O(ASSEMBLE
,Bitmap
):
1001 fprintf(stderr
, Name
": bitmap file needed with -b in --assemble mode\n");
1004 if (strcmp(optarg
, "internal")==0) {
1005 fprintf(stderr
, Name
": there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
1008 bitmap_fd
= open(optarg
, O_RDWR
);
1009 if (!*optarg
|| bitmap_fd
< 0) {
1010 fprintf(stderr
, Name
": cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
1013 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
1016 case O(ASSEMBLE
, BackupFile
):
1017 case O(GROW
, BackupFile
):
1018 /* Specify a file into which grow might place a backup,
1019 * or from which assemble might recover a backup
1022 fprintf(stderr
, Name
": backup file already specified, rejecting %s\n", optarg
);
1025 backup_file
= optarg
;
1028 case O(GROW
, Continue
):
1029 /* Continue interrupted grow
1033 case O(ASSEMBLE
, InvalidBackup
):
1034 /* Acknowledge that the backupfile is invalid, but ask
1035 * to continue anyway
1041 case O(BUILD
,Bitmap
):
1043 case O(CREATE
,Bitmap
): /* here we create the bitmap */
1044 if (strcmp(optarg
, "none") == 0) {
1045 fprintf(stderr
, Name
": '--bitmap none' only"
1046 " support for --grow\n");
1051 case O(GROW
,Bitmap
):
1052 if (strcmp(optarg
, "internal")== 0 ||
1053 strcmp(optarg
, "none")== 0 ||
1054 strchr(optarg
, '/') != NULL
) {
1055 bitmap_file
= optarg
;
1059 fprintf(stderr
, Name
": bitmap file must contain a '/', or be 'internal', or 'none'\n"
1060 " not '%s'\n", optarg
);
1063 case O(GROW
,BitmapChunk
):
1064 case O(BUILD
,BitmapChunk
):
1065 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
1066 bitmap_chunk
= parse_size(optarg
);
1067 if (bitmap_chunk
<= 0 ||
1068 bitmap_chunk
& (bitmap_chunk
- 1)) {
1070 Name
": invalid bitmap chunksize: %s\n",
1074 bitmap_chunk
= bitmap_chunk
* 512;
1077 case O(GROW
, WriteBehind
):
1078 case O(BUILD
, WriteBehind
):
1079 case O(CREATE
, WriteBehind
): /* write-behind mode */
1080 write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
1082 write_behind
= strtol(optarg
, &c
, 10);
1083 if (write_behind
< 0 || *c
||
1084 write_behind
> 16383) {
1085 fprintf(stderr
, Name
": Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
1091 case O(INCREMENTAL
, 'r'):
1092 case O(INCREMENTAL
, RebuildMapOpt
):
1095 case O(INCREMENTAL
, IncrementalPath
):
1096 remove_path
= optarg
;
1099 /* We have now processed all the valid options. Anything else is
1102 if (option_index
> 0)
1103 fprintf(stderr
, Name
":option --%s not valid in %s mode\n",
1104 long_options
[option_index
].name
,
1105 map_num(modes
, mode
));
1107 fprintf(stderr
, Name
": option -%c not valid in %s mode\n",
1108 opt
, map_num(modes
, mode
));
1114 char *help_text
= Help
;
1115 if (print_help
== 2)
1116 help_text
= OptionHelp
;
1119 case ASSEMBLE
: help_text
= Help_assemble
; break;
1120 case BUILD
: help_text
= Help_build
; break;
1121 case CREATE
: help_text
= Help_create
; break;
1122 case MANAGE
: help_text
= Help_manage
; break;
1123 case MISC
: help_text
= Help_misc
; break;
1124 case MONITOR
: help_text
= Help_monitor
; break;
1125 case GROW
: help_text
= Help_grow
; break;
1126 case INCREMENTAL
:help_text
= Help_incr
; break;
1128 fputs(help_text
,stdout
);
1132 if (!mode
&& devs_found
) {
1135 if (devlist
->disposition
== 0)
1136 devlist
->disposition
= devmode
;
1139 fputs(Usage
, stderr
);
1144 struct createinfo
*ci
= conf_get_create_info();
1146 if (strcasecmp(symlinks
, "yes") == 0)
1148 else if (strcasecmp(symlinks
, "no") == 0)
1151 fprintf(stderr
, Name
": option --symlinks must be 'no' or 'yes'\n");
1155 /* Ok, got the option parsing out of the way
1156 * hopefully it's mostly right but there might be some stuff
1159 * That is mosty checked in the per-mode stuff but...
1161 * For @,B,C and A without -s, the first device listed must be an md device
1162 * we check that here and open it.
1165 if (mode
==MANAGE
|| mode
== BUILD
|| mode
== CREATE
|| mode
== GROW
||
1166 (mode
== ASSEMBLE
&& ! scan
)) {
1167 if (devs_found
< 1) {
1168 fprintf(stderr
, Name
": an md device must be given in this mode\n");
1171 if ((int)ident
.super_minor
== -2 && autof
) {
1172 fprintf(stderr
, Name
": --super-minor=dev is incompatible with --auto\n");
1175 if (mode
== MANAGE
|| mode
== GROW
) {
1176 mdfd
= open_mddev(devlist
->devname
, 1);
1180 /* non-existent device is OK */
1181 mdfd
= open_mddev(devlist
->devname
, 0);
1183 fprintf(stderr
, Name
": device %s exists but is not an "
1184 "md array.\n", devlist
->devname
);
1187 if ((int)ident
.super_minor
== -2) {
1190 fprintf(stderr
, Name
": --super-minor=dev given, and "
1191 "listed device %s doesn't exist.\n",
1196 ident
.super_minor
= minor(stb
.st_rdev
);
1198 if (mdfd
>= 0 && mode
!= MANAGE
&& mode
!= GROW
) {
1199 /* We don't really want this open yet, we just might
1200 * have wanted to check some things
1208 if (raiddisks
== 1 && !force
&& level
!= -5) {
1209 fprintf(stderr
, Name
": '1' is an unusual number of drives for an array, so it is probably\n"
1210 " a mistake. If you really mean it you will need to specify --force before\n"
1211 " setting the number of drives.\n");
1216 if (homehost
== NULL
)
1217 homehost
= conf_get_homehost(&require_homehost
);
1218 if (homehost
== NULL
|| strcasecmp(homehost
, "<system>")==0) {
1219 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
1220 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
1221 homehost
= sys_hostname
;
1224 if (homehost
&& (!homehost
[0] || strcasecmp(homehost
, "<none>") == 0)) {
1226 require_homehost
= 0;
1229 if (!((mode
== MISC
&& devmode
== 'E')
1230 || (mode
== MONITOR
&& spare_sharing
== 0)) &&
1232 fprintf(stderr
, Name
": must be super-user to perform this action\n");
1236 ident
.autof
= autof
;
1241 /* readonly, add/remove, readwrite, runstop */
1243 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1244 if (!rv
&& devs_found
>1)
1245 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
1246 devlist
->next
, verbose
-quiet
, test
,
1248 if (!rv
&& readonly
< 0)
1249 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1251 rv
= Manage_runstop(devlist
->devname
, mdfd
, runstop
, quiet
);
1254 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1255 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !scan
) {
1256 /* Only a device has been given, so get details from config file */
1257 struct mddev_ident
*array_ident
= conf_get_ident(devlist
->devname
);
1258 if (array_ident
== NULL
) {
1259 fprintf(stderr
, Name
": %s not identified in config file.\n",
1265 if (array_ident
->autof
== 0)
1266 array_ident
->autof
= autof
;
1267 rv
|= Assemble(ss
, devlist
->devname
, array_ident
,
1268 NULL
, backup_file
, invalid_backup
,
1269 readonly
, runstop
, update
,
1270 homehost
, require_homehost
,
1271 verbose
-quiet
, force
,
1275 rv
= Assemble(ss
, devlist
->devname
, &ident
,
1276 devlist
->next
, backup_file
, invalid_backup
,
1277 readonly
, runstop
, update
,
1278 homehost
, require_homehost
,
1279 verbose
-quiet
, force
,
1281 else if (devs_found
>0) {
1282 if (update
&& devs_found
> 1) {
1283 fprintf(stderr
, Name
": can only update a single array at a time\n");
1286 if (backup_file
&& devs_found
> 1) {
1287 fprintf(stderr
, Name
": can only assemble a single array when providing a backup file.\n");
1290 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1291 struct mddev_ident
*array_ident
= conf_get_ident(dv
->devname
);
1292 if (array_ident
== NULL
) {
1293 fprintf(stderr
, Name
": %s not identified in config file.\n",
1298 if (array_ident
->autof
== 0)
1299 array_ident
->autof
= autof
;
1300 rv
|= Assemble(ss
, dv
->devname
, array_ident
,
1301 NULL
, backup_file
, invalid_backup
,
1302 readonly
, runstop
, update
,
1303 homehost
, require_homehost
,
1304 verbose
-quiet
, force
,
1308 struct mddev_ident
*a
, *array_list
= conf_get_ident(NULL
);
1309 struct mddev_dev
*devlist
= conf_get_devs();
1310 struct map_ent
*map
= NULL
;
1312 int failures
, successes
;
1314 if (conf_verify_devnames(array_list
)) {
1315 fprintf(stderr
, Name
1316 ": Duplicate MD device names in "
1317 "conf file were found.\n");
1320 if (devlist
== NULL
) {
1321 fprintf(stderr
, Name
": No devices listed in conf file were found.\n");
1325 fprintf(stderr
, Name
": --update not meaningful with a --scan assembly.\n");
1329 fprintf(stderr
, Name
": --backup_file not meaningful with a --scan assembly.\n");
1332 for (a
= array_list
; a
; a
= a
->next
) {
1338 fprintf(stderr
, Name
" %s: failed to get "
1339 "exclusive lock on mapfile\n",
1345 for (a
= array_list
; a
; a
= a
->next
) {
1350 strcasecmp(a
->devname
, "<ignore>") == 0)
1353 r
= Assemble(ss
, a
->devname
,
1356 readonly
, runstop
, NULL
,
1357 homehost
, require_homehost
,
1358 verbose
-quiet
, force
,
1368 } while (failures
&& successes
);
1369 if (homehost
&& cnt
== 0) {
1370 /* Maybe we can auto-assemble something.
1371 * Repeatedly call Assemble in auto-assemble mode
1376 ident
.autof
= autof
;
1378 struct mddev_dev
*devlist
= conf_get_devs();
1381 rv2
= Assemble(ss
, NULL
,
1396 /* Incase there are stacked devices, we need to go around again */
1398 if (cnt
== 0 && rv
== 0) {
1399 fprintf(stderr
, Name
": No arrays found in config file or automatically\n");
1403 } else if (cnt
== 0 && rv
== 0) {
1404 fprintf(stderr
, Name
": No arrays found in config file\n");
1411 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1412 if (write_behind
&& !bitmap_file
) {
1413 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1417 if (raiddisks
== 0) {
1418 fprintf(stderr
, Name
": no raid-devices specified.\n");
1424 if (strcmp(bitmap_file
, "internal")==0) {
1425 fprintf(stderr
, Name
": 'internal' bitmaps not supported with --build\n");
1430 rv
= Build(devlist
->devname
, chunk
, level
, layout
,
1431 raiddisks
, devlist
->next
, assume_clean
,
1432 bitmap_file
, bitmap_chunk
, write_behind
,
1433 delay
, verbose
-quiet
, autof
, size
);
1436 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1437 if (write_behind
&& !bitmap_file
) {
1438 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1442 if (raiddisks
== 0) {
1443 fprintf(stderr
, Name
": no raid-devices specified.\n");
1448 rv
= Create(ss
, devlist
->devname
, chunk
, level
, layout
, size
<0 ? 0 : size
,
1449 raiddisks
, sparedisks
, ident
.name
, homehost
,
1450 ident
.uuid_set
? ident
.uuid
: NULL
,
1451 devs_found
-1, devlist
->next
, runstop
, verbose
-quiet
, force
, assume_clean
,
1452 bitmap_file
, bitmap_chunk
, write_behind
, delay
, autof
);
1455 if (devmode
== 'E') {
1456 if (devlist
== NULL
&& !scan
) {
1457 fprintf(stderr
, Name
": No devices to examine\n");
1460 if (devlist
== NULL
)
1461 devlist
= conf_get_devs();
1462 if (devlist
== NULL
) {
1463 fprintf(stderr
, Name
": No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1466 if (brief
&& verbose
)
1468 rv
= Examine(devlist
, scan
?(verbose
>1?0:verbose
+1):brief
,
1470 SparcAdjust
, ss
, homehost
);
1471 } else if (devmode
== DetailPlatform
) {
1472 rv
= Detail_Platform(ss
? ss
->ss
: NULL
, ss
? scan
: 1, verbose
);
1474 if (devlist
== NULL
) {
1475 if ((devmode
=='D' || devmode
== Waitclean
) && scan
) {
1476 /* apply --detail or --wait-clean to
1477 * all devices in /proc/mdstat
1479 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1480 struct mdstat_ent
*e
;
1481 struct map_ent
*map
= NULL
;
1483 int v
= verbose
>1?0:verbose
+1;
1485 for (members
= 0; members
<= 1; members
++) {
1486 for (e
=ms
; e
; e
=e
->next
) {
1489 int member
= e
->metadata_version
&&
1490 strncmp(e
->metadata_version
,
1491 "external:/", 10) == 0;
1492 if (members
!= member
)
1494 me
= map_by_devnum(&map
, e
->devnum
);
1496 && strcmp(me
->path
, "/unknown") != 0)
1499 name
= get_md_name(e
->devnum
);
1502 fprintf(stderr
, Name
": cannot find device file for %s\n",
1507 rv
|= Detail(name
, v
,
1511 rv
|= WaitClean(name
, -1, v
);
1516 } else if (devmode
== 'S' && scan
) {
1517 /* apply --stop to all devices in /proc/mdstat */
1518 /* Due to possible stacking of devices, repeat until
1519 * nothing more can be stopped
1521 int progress
=1, err
;
1524 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1525 struct mdstat_ent
*e
;
1527 if (!progress
) last
= 1;
1528 progress
= 0; err
= 0;
1529 for (e
=ms
; e
; e
=e
->next
) {
1530 char *name
= get_md_name(e
->devnum
);
1533 fprintf(stderr
, Name
": cannot find device file for %s\n",
1537 mdfd
= open_mddev(name
, 1);
1539 if (Manage_runstop(name
, mdfd
, -1, quiet
?1:last
?0:-1))
1549 } while (!last
&& err
);
1551 } else if (devmode
== UdevRules
) {
1552 rv
= Write_rules(udev_filename
);
1554 fprintf(stderr
, Name
": No devices given.\n");
1558 for (dv
=devlist
; dv
; dv
=dv
->next
) {
1559 switch(dv
->disposition
) {
1561 rv
|= Detail(dv
->devname
,
1563 export
, test
, homehost
, prefer
);
1565 case 'K': /* Zero superblock */
1567 rv
|= Kill(dv
->devname
, ss
, force
, quiet
,0);
1571 rv
|= Kill(dv
->devname
, NULL
, force
, q
, 0);
1578 rv
|= Query(dv
->devname
); continue;
1580 rv
|= ExamineBitmap(dv
->devname
, brief
, ss
); continue;
1583 rv
|= Wait(dv
->devname
); continue;
1585 rv
|= WaitClean(dv
->devname
, -1, verbose
-quiet
); continue;
1587 rv
|= Kill_subarray(dv
->devname
, subarray
, quiet
);
1589 case UpdateSubarray
:
1590 if (update
== NULL
) {
1592 Name
": -U/--update must be specified with --update-subarray\n");
1596 rv
|= Update_subarray(dv
->devname
, subarray
, update
, &ident
, quiet
);
1599 mdfd
= open_mddev(dv
->devname
, 1);
1601 switch(dv
->disposition
) {
1603 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1, quiet
); break;
1605 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1, quiet
); break;
1607 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1609 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;
1618 if (!devlist
&& !scan
) {
1619 fprintf(stderr
, Name
": Cannot monitor: need --scan or at least one device\n");
1623 if (pidfile
&& !daemonise
) {
1624 fprintf(stderr
, Name
": Cannot write a pid file when not in daemon mode\n");
1629 if (get_linux_version() > 2006016)
1630 /* mdstat responds to poll */
1635 rv
= Monitor(devlist
, mailaddr
, program
,
1636 delay
?delay
:60, daemonise
, scan
, oneshot
,
1637 dosyslog
, test
, pidfile
, increments
,
1638 spare_sharing
, prefer
);
1642 if (array_size
>= 0) {
1643 /* alway impose array size first, independent of
1645 * Do not allow level or raid_disks changes at the
1646 * same time as that can be irreversibly destructive.
1650 if (raiddisks
|| level
!= UnSet
) {
1651 fprintf(stderr
, Name
": cannot change array size in same operation "
1652 "as changing raiddisks or level.\n"
1653 " Change size first, then check that data is still intact.\n");
1657 sysfs_init(&sra
, mdfd
, 0);
1658 if (array_size
== 0)
1659 err
= sysfs_set_str(&sra
, NULL
, "array_size", "default");
1661 err
= sysfs_set_num(&sra
, NULL
, "array_size", array_size
/ 2);
1664 fprintf(stderr
, Name
": --array-size setting"
1665 " is too large.\n");
1667 fprintf(stderr
, Name
": current kernel does"
1668 " not support setting --array-size\n");
1673 if (devs_found
> 1 && raiddisks
== 0) {
1675 if (size
>= 0 || chunk
|| layout_str
!= NULL
|| bitmap_file
) {
1676 fprintf(stderr
, Name
": --add cannot be used with "
1677 "other geometry changes in --grow mode\n");
1681 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1682 rv
= Grow_Add_device(devlist
->devname
, mdfd
,
1687 } else if (bitmap_file
) {
1688 if (size
>= 0 || raiddisks
|| chunk
||
1689 layout_str
!= NULL
|| devs_found
> 1) {
1690 fprintf(stderr
, Name
": --bitmap changes cannot be "
1691 "used with other geometry changes "
1692 "in --grow mode\n");
1697 delay
= DEFAULT_BITMAP_DELAY
;
1698 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, bitmap_file
,
1699 bitmap_chunk
, delay
, write_behind
, force
);
1700 } else if (grow_continue
)
1701 rv
= Grow_continue_command(devlist
->devname
,
1704 else if (size
>= 0 || raiddisks
!= 0 || layout_str
!= NULL
1705 || chunk
!= 0 || level
!= UnSet
) {
1706 rv
= Grow_reshape(devlist
->devname
, mdfd
, quiet
, backup_file
,
1707 size
, level
, layout_str
, chunk
, raiddisks
,
1709 assume_clean
, force
);
1710 } else if (array_size
< 0)
1711 fprintf(stderr
, Name
": no changes to --grow\n");
1719 fprintf(stderr
, Name
1720 ": --incremental --scan meaningless without --run.\n");
1723 if (devmode
== 'f') {
1724 fprintf(stderr
, Name
1725 ": --incremental --scan --fail not supported.\n");
1728 rv
= IncrementalScan(verbose
);
1731 if (!rebuild_map
&& !scan
) {
1732 fprintf(stderr
, Name
1733 ": --incremental requires a device.\n");
1738 if (devlist
->next
) {
1739 fprintf(stderr
, Name
1740 ": --incremental can only handle one device.\n");
1745 rv
= IncrementalRemove(devlist
->devname
, remove_path
,
1748 rv
= Incremental(devlist
->devname
, verbose
-quiet
,
1749 runstop
, ss
, homehost
,
1750 require_homehost
, autof
,