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
189 fputs(Usage
, stderr
);
192 /* second, figure out the mode.
193 * Some options force the mode. Others
194 * set the mode if it isn't already
200 shortopt
= short_bitmap_options
;
208 case ReAdd
: /* re-add */
211 shortopt
= short_bitmap_options
;
215 case 'A': newmode
= ASSEMBLE
; shortopt
= short_bitmap_auto_options
; break;
216 case 'B': newmode
= BUILD
; shortopt
= short_bitmap_auto_options
; break;
217 case 'C': newmode
= CREATE
; shortopt
= short_bitmap_auto_options
; break;
218 case 'F': newmode
= MONITOR
;break;
219 case 'G': newmode
= GROW
;
220 shortopt
= short_bitmap_options
;
222 case 'I': newmode
= INCREMENTAL
;
223 shortopt
= short_bitmap_auto_options
; break;
225 newmode
= AUTODETECT
;
256 if (mode
&& newmode
== mode
) {
257 /* everybody happy ! */
258 } else if (mode
&& newmode
!= mode
) {
260 fprintf(stderr
, Name
": ");
261 if (option_index
>= 0)
262 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
264 fprintf(stderr
, "-%c", opt
);
265 fprintf(stderr
, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
266 map_num(modes
, newmode
),
267 map_num(modes
, mode
));
269 } else if (!mode
&& newmode
) {
271 if (mode
== MISC
&& devs_found
) {
272 fprintf(stderr
, Name
": No action given for %s in --misc mode\n",
274 fprintf(stderr
," Action options must come before device names\n");
278 /* special case of -c --help */
279 if ((opt
== 'c' || opt
== ConfigFile
) &&
280 ( strncmp(optarg
, "--h", 3)==0 ||
281 strncmp(optarg
, "-h", 2)==0)) {
282 fputs(Help_config
, stdout
);
286 /* If first option is a device, don't force the mode yet */
288 if (devs_found
== 0) {
289 dv
= malloc(sizeof(*dv
));
291 fprintf(stderr
, Name
": malloc failed\n");
294 dv
->devname
= optarg
;
295 dv
->disposition
= devmode
;
296 dv
->writemostly
= writemostly
;
301 devlistend
= &dv
->next
;
306 /* No mode yet, and this is the second device ... */
307 fprintf(stderr
, Name
": An option must be given to set the mode before a second device\n"
308 " (%s) is listed\n", optarg
);
311 if (option_index
>= 0)
312 fprintf(stderr
, Name
": --%s", long_options
[option_index
].name
);
314 fprintf(stderr
, Name
": -%c", opt
);
315 fprintf(stderr
, " does not set the mode, and so cannot be the first option.\n");
319 /* if we just set the mode, then done */
333 /* an undecorated option - must be a device name.
335 if (devs_found
> 0 && mode
== MANAGE
&& !devmode
) {
336 fprintf(stderr
, Name
": Must give one of -a/-r/-f"
337 " for subsequent devices at %s\n", optarg
);
340 if (devs_found
> 0 && mode
== GROW
&& !devmode
) {
341 fprintf(stderr
, Name
": Must give -a/--add for"
342 " devices to add: %s\n", optarg
);
345 dv
= malloc(sizeof(*dv
));
347 fprintf(stderr
, Name
": malloc failed\n");
350 dv
->devname
= optarg
;
351 dv
->disposition
= devmode
;
352 dv
->writemostly
= writemostly
;
357 devlistend
= &dv
->next
;
363 /* We've got a mode, and opt is now something else which
364 * could depend on the mode */
365 #define O(a,b) ((a<<16)|b)
366 switch (O(mode
,opt
)) {
368 case O(GROW
,ChunkSize
):
370 case O(CREATE
,ChunkSize
):
371 case O(BUILD
,'c'): /* chunk or rounding */
372 case O(BUILD
,ChunkSize
): /* chunk or rounding */
374 fprintf(stderr
, Name
": chunk/rounding may only be specified once. "
375 "Second value is %s.\n", optarg
);
378 chunk
= parse_size(optarg
);
379 if (chunk
< 8 || (chunk
&1)) {
380 fprintf(stderr
, Name
": invalid chunk/rounding value: %s\n",
384 /* Convert sectors to K */
388 case O(INCREMENTAL
, 'e'):
390 case O(ASSEMBLE
,'e'):
391 case O(MISC
,'e'): /* set metadata (superblock) information */
393 fprintf(stderr
, Name
": metadata information already given\n");
396 for(i
=0; !ss
&& superlist
[i
]; i
++)
397 ss
= superlist
[i
]->match_metadata_desc(optarg
);
400 fprintf(stderr
, Name
": unrecognised metadata identifier: %s\n", optarg
);
406 case O(MANAGE
,WriteMostly
):
408 case O(BUILD
,WriteMostly
):
410 case O(CREATE
,WriteMostly
):
411 /* set write-mostly for following devices */
416 /* clear write-mostly for following devices */
423 case O(BUILD
,'z'): /* size */
425 fprintf(stderr
, Name
": size may only be specified once. "
426 "Second value is %s.\n", optarg
);
429 if (strcmp(optarg
, "max")==0)
432 size
= parse_size(optarg
);
434 fprintf(stderr
, Name
": invalid size: %s\n",
438 /* convert sectors to K */
443 case O(GROW
,'Z'): /* array size */
444 if (array_size
>= 0) {
445 fprintf(stderr
, Name
": array-size may only be specified once. "
446 "Second value is %s.\n", optarg
);
449 if (strcmp(optarg
, "max") == 0)
452 array_size
= parse_size(optarg
);
453 if (array_size
<= 0) {
454 fprintf(stderr
, Name
": invalid array size: %s\n",
463 case O(BUILD
,'l'): /* set raid level*/
464 if (level
!= UnSet
) {
465 fprintf(stderr
, Name
": raid level may only be set once. "
466 "Second value is %s.\n", optarg
);
469 level
= map_name(pers
, optarg
);
470 if (level
== UnSet
) {
471 fprintf(stderr
, Name
": invalid raid level: %s\n",
475 if (level
!= 0 && level
!= LEVEL_LINEAR
&& level
!= 1 &&
476 level
!= LEVEL_MULTIPATH
&& level
!= LEVEL_FAULTY
&&
479 fprintf(stderr
, Name
": Raid level %s not permitted with --build.\n",
483 if (sparedisks
> 0 && level
< 1 && level
>= -1) {
484 fprintf(stderr
, Name
": raid level %s is incompatible with spare-devices setting.\n",
491 case O(GROW
, 'p'): /* new layout */
492 case O(GROW
, Layout
):
494 fprintf(stderr
,Name
": layout may only be sent once. "
495 "Second value was %s\n", optarg
);
499 /* 'Grow' will parse the value */
502 case O(CREATE
,'p'): /* raid5 layout */
503 case O(CREATE
,Layout
):
504 case O(BUILD
,'p'): /* faulty layout */
505 case O(BUILD
,Layout
):
506 if (layout
!= UnSet
) {
507 fprintf(stderr
,Name
": layout may only be sent once. "
508 "Second value was %s\n", optarg
);
513 fprintf(stderr
, Name
": layout not meaningful for %s arrays.\n",
514 map_num(pers
, level
));
517 fprintf(stderr
, Name
": raid level must be given before layout.\n");
521 layout
= map_name(r5layout
, optarg
);
523 fprintf(stderr
, Name
": layout %s not understood for raid5.\n",
529 layout
= map_name(r6layout
, optarg
);
531 fprintf(stderr
, Name
": layout %s not understood for raid6.\n",
538 layout
= parse_layout_10(optarg
);
540 fprintf(stderr
, Name
": layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg
);
548 layout
= parse_layout_faulty(optarg
);
550 fprintf(stderr
, Name
": layout %s not understood for faulty.\n",
558 case O(CREATE
,AssumeClean
):
559 case O(BUILD
,AssumeClean
): /* assume clean */
560 case O(GROW
,AssumeClean
):
566 case O(BUILD
,'n'): /* number of raid disks */
568 fprintf(stderr
, Name
": raid-devices set twice: %d and %s\n",
572 raiddisks
= strtol(optarg
, &c
, 10);
573 if (!optarg
[0] || *c
|| raiddisks
<=0) {
574 fprintf(stderr
, Name
": invalid number of raid devices: %s\n",
578 ident
.raid_disks
= raiddisks
;
581 case O(CREATE
,'x'): /* number of spare (eXtra) discs */
583 fprintf(stderr
,Name
": spare-devices set twice: %d and %s\n",
587 if (level
!= UnSet
&& level
<= 0 && level
>= -1) {
588 fprintf(stderr
, Name
": spare-devices setting is incompatible with raid level %d\n",
592 sparedisks
= strtol(optarg
, &c
, 10);
593 if (!optarg
[0] || *c
|| sparedisks
< 0) {
594 fprintf(stderr
, Name
": invalid number of spare-devices: %s\n",
604 case O(INCREMENTAL
,'a'):
605 case O(INCREMENTAL
,Auto
):
606 case O(ASSEMBLE
,'a'):
607 case O(ASSEMBLE
,Auto
): /* auto-creation of device node */
608 autof
= parse_auto(optarg
, "--auto flag", 0);
611 case O(CREATE
,Symlinks
):
612 case O(BUILD
,Symlinks
):
613 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
617 case O(BUILD
,'f'): /* force honouring '-n 1' */
618 case O(BUILD
,Force
): /* force honouring '-n 1' */
619 case O(GROW
,'f'): /* ditto */
620 case O(GROW
,Force
): /* ditto */
621 case O(CREATE
,'f'): /* force honouring of device list */
622 case O(CREATE
,Force
): /* force honouring of device list */
623 case O(ASSEMBLE
,'f'): /* force assembly */
624 case O(ASSEMBLE
,Force
): /* force assembly */
625 case O(MISC
,'f'): /* force zero */
626 case O(MISC
,Force
): /* force zero */
627 case O(MANAGE
,Force
): /* add device which is too large */
630 /* now for the Assemble options */
631 case O(ASSEMBLE
, FreezeReshape
): /* Freeze reshape during
633 case O(INCREMENTAL
, FreezeReshape
):
636 case O(CREATE
,'u'): /* uuid of array */
637 case O(ASSEMBLE
,'u'): /* uuid of array */
638 if (ident
.uuid_set
) {
639 fprintf(stderr
, Name
": uuid cannot be set twice. "
640 "Second value %s.\n", optarg
);
643 if (parse_uuid(optarg
, ident
.uuid
))
646 fprintf(stderr
,Name
": Bad uuid: %s\n", optarg
);
652 case O(ASSEMBLE
,'N'):
655 fprintf(stderr
, Name
": name cannot be set twice. "
656 "Second value %s.\n", optarg
);
659 if (mode
== MISC
&& !subarray
) {
660 fprintf(stderr
, Name
": -N/--name only valid with --update-subarray in misc mode\n");
663 if (strlen(optarg
) > 32) {
664 fprintf(stderr
, Name
": name '%s' is too long, 32 chars max.\n",
668 strcpy(ident
.name
, optarg
);
671 case O(ASSEMBLE
,'m'): /* super-minor for array */
672 case O(ASSEMBLE
,SuperMinor
):
673 if (ident
.super_minor
!= UnSet
) {
674 fprintf(stderr
, Name
": super-minor cannot be set twice. "
675 "Second value: %s.\n", optarg
);
678 if (strcmp(optarg
, "dev")==0)
679 ident
.super_minor
= -2;
681 ident
.super_minor
= strtoul(optarg
, &cp
, 10);
682 if (!optarg
[0] || *cp
) {
683 fprintf(stderr
, Name
": Bad super-minor number: %s.\n", optarg
);
689 case O(ASSEMBLE
,'U'): /* update the superblock */
692 fprintf(stderr
, Name
": Can only update one aspect"
693 " of superblock, both %s and %s given.\n",
697 if (mode
== MISC
&& !subarray
) {
698 fprintf(stderr
, Name
": Only subarrays can be"
699 " updated in misc mode\n");
703 if (strcmp(update
, "sparc2.2")==0)
705 if (strcmp(update
, "super-minor") == 0)
707 if (strcmp(update
, "summaries")==0)
709 if (strcmp(update
, "resync")==0)
711 if (strcmp(update
, "uuid")==0)
713 if (strcmp(update
, "name")==0)
715 if (strcmp(update
, "homehost")==0)
717 if (strcmp(update
, "devicesize")==0)
719 if (strcmp(update
, "no-bitmap")==0)
721 if (strcmp(update
, "byteorder")==0) {
724 Name
": must not set metadata"
725 " type with --update=byteorder.\n");
728 for(i
=0; !ss
&& superlist
[i
]; i
++)
729 ss
= superlist
[i
]->match_metadata_desc(
732 fprintf(stderr
, Name
": INTERNAL ERROR"
733 " cannot find 0.swap\n");
739 if (strcmp(update
,"?") == 0 ||
740 strcmp(update
, "help") == 0) {
742 fprintf(outf
, Name
": ");
746 Name
": '--update=%s' is invalid. ",
749 fprintf(outf
, "Valid --update options are:\n"
750 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
751 " 'summaries', 'homehost', 'byteorder', 'devicesize',\n"
753 exit(outf
== stdout
? 0 : 2);
756 /* update=devicesize is allowed with --re-add */
757 if (devmode
!= 'a' || re_add
!= 1) {
758 fprintf(stderr
, Name
"--update in Manage mode only"
759 " allowed with --re-add.\n");
763 fprintf(stderr
, Name
": Can only update one aspect"
764 " of superblock, both %s and %s given.\n",
769 if (strcmp(update
, "devicesize") != 0) {
770 fprintf(stderr
, Name
": only 'devicesize' can be"
771 " updated with --re-add\n");
776 case O(INCREMENTAL
,NoDegraded
):
777 fprintf(stderr
, Name
": --no-degraded is deprecated in Incremental mode\n");
778 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
779 runstop
= -1; /* --stop isn't allowed for --assemble,
780 * so we overload slightly */
783 case O(ASSEMBLE
,'c'):
784 case O(ASSEMBLE
,ConfigFile
):
785 case O(INCREMENTAL
, 'c'):
786 case O(INCREMENTAL
, ConfigFile
):
788 case O(MISC
, ConfigFile
):
790 case O(MONITOR
,ConfigFile
):
792 fprintf(stderr
, Name
": configfile cannot be set twice. "
793 "Second value is %s.\n", optarg
);
797 set_conffile(configfile
);
798 /* FIXME possibly check that config file exists. Even parse it */
800 case O(ASSEMBLE
,'s'): /* scan */
803 case O(INCREMENTAL
,'s'):
807 case O(MONITOR
,'m'): /* mail address */
808 case O(MONITOR
,EMail
):
810 fprintf(stderr
, Name
": only specify one mailaddress. %s ignored.\n",
816 case O(MONITOR
,'p'): /* alert program */
817 case O(MONITOR
,ProgramOpt
): /* alert program */
819 fprintf(stderr
, Name
": only specify one alter program. %s ignored.\n",
825 case O(MONITOR
,'r'): /* rebuild increments */
826 case O(MONITOR
,Increment
):
827 increments
= atoi(optarg
);
828 if (increments
>99 || increments
<1) {
829 fprintf(stderr
, Name
": please specify positive integer between 1 and 99 as rebuild increments.\n");
834 case O(MONITOR
,'d'): /* delay in seconds */
836 case O(BUILD
,'d'): /* delay for bitmap updates */
839 fprintf(stderr
, Name
": only specify delay once. %s ignored.\n",
842 delay
= strtol(optarg
, &c
, 10);
843 if (!optarg
[0] || *c
|| delay
<1) {
844 fprintf(stderr
, Name
": invalid delay: %s\n",
850 case O(MONITOR
,'f'): /* daemonise */
851 case O(MONITOR
,Fork
):
854 case O(MONITOR
,'i'): /* pid */
856 fprintf(stderr
, Name
": only specify one pid file. %s ignored.\n",
861 case O(MONITOR
,'1'): /* oneshot */
865 case O(MONITOR
,'t'): /* test */
868 case O(MONITOR
,'y'): /* log messages to syslog */
869 openlog("mdadm", LOG_PID
, SYSLOG_FACILITY
);
872 case O(MONITOR
, NoSharing
):
875 /* now the general management options. Some are applicable
876 * to other modes. None have arguments.
881 case O(MANAGE
,Add
): /* add a drive */
885 case O(MANAGE
,ReAdd
):
889 case O(MANAGE
,'r'): /* remove a drive */
890 case O(MANAGE
,Remove
):
893 case O(MANAGE
,'f'): /* set faulty */
895 case O(INCREMENTAL
,'f'):
896 case O(INCREMENTAL
,Remove
):
897 case O(INCREMENTAL
,Fail
): /* r for incremental is taken, use f
898 * even though we will both fail and
899 * remove the device */
902 case O(INCREMENTAL
,'R'):
904 case O(ASSEMBLE
,'R'):
906 case O(CREATE
,'R'): /* Run the array */
908 fprintf(stderr
, Name
": Cannot both Stop and Run an array\n");
915 fprintf(stderr
, Name
": Cannot both Run and Stop an array\n");
934 case O(MISC
, WaitOpt
):
935 case O(MISC
, Waitclean
):
936 case O(MISC
, DetailPlatform
):
937 case O(MISC
, KillSubarray
):
938 case O(MISC
, UpdateSubarray
):
939 if (opt
== KillSubarray
|| opt
== UpdateSubarray
) {
941 fprintf(stderr
, Name
": subarray can only"
942 " be specified once\n");
947 if (devmode
&& devmode
!= opt
&&
948 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
949 fprintf(stderr
, Name
": --examine/-E cannot be given with ");
950 if (devmode
== 'E') {
951 if (option_index
>= 0)
952 fprintf(stderr
, "--%s\n",
953 long_options
[option_index
].name
);
955 fprintf(stderr
, "-%c\n", opt
);
956 } else if (isalpha(devmode
))
957 fprintf(stderr
, "-%c\n", devmode
);
959 fprintf(stderr
, "previous option\n");
964 case O(MISC
, UdevRules
):
965 if (devmode
&& devmode
!= opt
) {
966 fprintf(stderr
, Name
": --udev-rules must"
967 " be the only option.\n");
970 fprintf(stderr
, Name
": only specify one udev "
971 "rule filename. %s ignored.\n",
974 udev_filename
= optarg
;
982 case O(MISC
, Sparc22
):
983 if (devmode
!= 'E') {
984 fprintf(stderr
, Name
": --sparc2.2 only allowed with --examine\n");
990 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
991 case O(ASSEMBLE
,Bitmap
):
993 fprintf(stderr
, Name
": bitmap file needed with -b in --assemble mode\n");
996 if (strcmp(optarg
, "internal")==0) {
997 fprintf(stderr
, Name
": there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
1000 bitmap_fd
= open(optarg
, O_RDWR
);
1001 if (!*optarg
|| bitmap_fd
< 0) {
1002 fprintf(stderr
, Name
": cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
1005 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
1008 case O(ASSEMBLE
, BackupFile
):
1009 case O(GROW
, BackupFile
):
1010 /* Specify a file into which grow might place a backup,
1011 * or from which assemble might recover a backup
1014 fprintf(stderr
, Name
": backup file already specified, rejecting %s\n", optarg
);
1017 backup_file
= optarg
;
1020 case O(GROW
, Continue
):
1021 /* Continue interrupted grow
1025 case O(ASSEMBLE
, InvalidBackup
):
1026 /* Acknowledge that the backupfile is invalid, but ask
1027 * to continue anyway
1033 case O(BUILD
,Bitmap
):
1035 case O(CREATE
,Bitmap
): /* here we create the bitmap */
1036 if (strcmp(optarg
, "none") == 0) {
1037 fprintf(stderr
, Name
": '--bitmap none' only"
1038 " support for --grow\n");
1043 case O(GROW
,Bitmap
):
1044 if (strcmp(optarg
, "internal")== 0 ||
1045 strcmp(optarg
, "none")== 0 ||
1046 strchr(optarg
, '/') != NULL
) {
1047 bitmap_file
= optarg
;
1051 fprintf(stderr
, Name
": bitmap file must contain a '/', or be 'internal', or 'none'\n"
1052 " not '%s'\n", optarg
);
1055 case O(GROW
,BitmapChunk
):
1056 case O(BUILD
,BitmapChunk
):
1057 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
1058 bitmap_chunk
= parse_size(optarg
);
1059 if (bitmap_chunk
<= 0 ||
1060 bitmap_chunk
& (bitmap_chunk
- 1)) {
1062 Name
": invalid bitmap chunksize: %s\n",
1066 bitmap_chunk
= bitmap_chunk
* 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
,