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 /* convert sectors to B, chunk of 0 means 512B */
1067 bitmap_chunk
= bitmap_chunk
? bitmap_chunk
* 512 : 512;
1070 case O(GROW
, WriteBehind
):
1071 case O(BUILD
, WriteBehind
):
1072 case O(CREATE
, WriteBehind
): /* write-behind mode */
1073 write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
1075 write_behind
= strtol(optarg
, &c
, 10);
1076 if (write_behind
< 0 || *c
||
1077 write_behind
> 16383) {
1078 fprintf(stderr
, Name
": Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
1084 case O(INCREMENTAL
, 'r'):
1085 case O(INCREMENTAL
, RebuildMapOpt
):
1088 case O(INCREMENTAL
, IncrementalPath
):
1089 remove_path
= optarg
;
1092 /* We have now processed all the valid options. Anything else is
1095 if (option_index
> 0)
1096 fprintf(stderr
, Name
":option --%s not valid in %s mode\n",
1097 long_options
[option_index
].name
,
1098 map_num(modes
, mode
));
1100 fprintf(stderr
, Name
": option -%c not valid in %s mode\n",
1101 opt
, map_num(modes
, mode
));
1107 char *help_text
= Help
;
1108 if (print_help
== 2)
1109 help_text
= OptionHelp
;
1112 case ASSEMBLE
: help_text
= Help_assemble
; break;
1113 case BUILD
: help_text
= Help_build
; break;
1114 case CREATE
: help_text
= Help_create
; break;
1115 case MANAGE
: help_text
= Help_manage
; break;
1116 case MISC
: help_text
= Help_misc
; break;
1117 case MONITOR
: help_text
= Help_monitor
; break;
1118 case GROW
: help_text
= Help_grow
; break;
1119 case INCREMENTAL
:help_text
= Help_incr
; break;
1121 fputs(help_text
,stdout
);
1125 if (!mode
&& devs_found
) {
1128 if (devlist
->disposition
== 0)
1129 devlist
->disposition
= devmode
;
1132 fputs(Usage
, stderr
);
1137 struct createinfo
*ci
= conf_get_create_info();
1139 if (strcasecmp(symlinks
, "yes") == 0)
1141 else if (strcasecmp(symlinks
, "no") == 0)
1144 fprintf(stderr
, Name
": option --symlinks must be 'no' or 'yes'\n");
1148 /* Ok, got the option parsing out of the way
1149 * hopefully it's mostly right but there might be some stuff
1152 * That is mosty checked in the per-mode stuff but...
1154 * For @,B,C and A without -s, the first device listed must be an md device
1155 * we check that here and open it.
1158 if (mode
==MANAGE
|| mode
== BUILD
|| mode
== CREATE
|| mode
== GROW
||
1159 (mode
== ASSEMBLE
&& ! scan
)) {
1160 if (devs_found
< 1) {
1161 fprintf(stderr
, Name
": an md device must be given in this mode\n");
1164 if ((int)ident
.super_minor
== -2 && autof
) {
1165 fprintf(stderr
, Name
": --super-minor=dev is incompatible with --auto\n");
1168 if (mode
== MANAGE
|| mode
== GROW
) {
1169 mdfd
= open_mddev(devlist
->devname
, 1);
1173 /* non-existent device is OK */
1174 mdfd
= open_mddev(devlist
->devname
, 0);
1176 fprintf(stderr
, Name
": device %s exists but is not an "
1177 "md array.\n", devlist
->devname
);
1180 if ((int)ident
.super_minor
== -2) {
1183 fprintf(stderr
, Name
": --super-minor=dev given, and "
1184 "listed device %s doesn't exist.\n",
1189 ident
.super_minor
= minor(stb
.st_rdev
);
1191 if (mdfd
>= 0 && mode
!= MANAGE
&& mode
!= GROW
) {
1192 /* We don't really want this open yet, we just might
1193 * have wanted to check some things
1201 if (raiddisks
== 1 && !force
&& level
!= -5) {
1202 fprintf(stderr
, Name
": '1' is an unusual number of drives for an array, so it is probably\n"
1203 " a mistake. If you really mean it you will need to specify --force before\n"
1204 " setting the number of drives.\n");
1209 if (homehost
== NULL
)
1210 homehost
= conf_get_homehost(&require_homehost
);
1211 if (homehost
== NULL
|| strcasecmp(homehost
, "<system>")==0) {
1212 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
1213 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
1214 homehost
= sys_hostname
;
1217 if (homehost
&& (!homehost
[0] || strcasecmp(homehost
, "<none>") == 0)) {
1219 require_homehost
= 0;
1222 if (!((mode
== MISC
&& devmode
== 'E')
1223 || (mode
== MONITOR
&& spare_sharing
== 0)) &&
1225 fprintf(stderr
, Name
": must be super-user to perform this action\n");
1229 ident
.autof
= autof
;
1234 /* readonly, add/remove, readwrite, runstop */
1236 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1237 if (!rv
&& devs_found
>1)
1238 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
1239 devlist
->next
, verbose
-quiet
, test
,
1241 if (!rv
&& readonly
< 0)
1242 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1244 rv
= Manage_runstop(devlist
->devname
, mdfd
, runstop
, quiet
);
1247 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1248 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !scan
) {
1249 /* Only a device has been given, so get details from config file */
1250 struct mddev_ident
*array_ident
= conf_get_ident(devlist
->devname
);
1251 if (array_ident
== NULL
) {
1252 fprintf(stderr
, Name
": %s not identified in config file.\n",
1258 if (array_ident
->autof
== 0)
1259 array_ident
->autof
= autof
;
1260 rv
|= Assemble(ss
, devlist
->devname
, array_ident
,
1261 NULL
, backup_file
, invalid_backup
,
1262 readonly
, runstop
, update
,
1263 homehost
, require_homehost
,
1264 verbose
-quiet
, force
,
1268 rv
= Assemble(ss
, devlist
->devname
, &ident
,
1269 devlist
->next
, backup_file
, invalid_backup
,
1270 readonly
, runstop
, update
,
1271 homehost
, require_homehost
,
1272 verbose
-quiet
, force
,
1274 else if (devs_found
>0) {
1275 if (update
&& devs_found
> 1) {
1276 fprintf(stderr
, Name
": can only update a single array at a time\n");
1279 if (backup_file
&& devs_found
> 1) {
1280 fprintf(stderr
, Name
": can only assemble a single array when providing a backup file.\n");
1283 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1284 struct mddev_ident
*array_ident
= conf_get_ident(dv
->devname
);
1285 if (array_ident
== NULL
) {
1286 fprintf(stderr
, Name
": %s not identified in config file.\n",
1291 if (array_ident
->autof
== 0)
1292 array_ident
->autof
= autof
;
1293 rv
|= Assemble(ss
, dv
->devname
, array_ident
,
1294 NULL
, backup_file
, invalid_backup
,
1295 readonly
, runstop
, update
,
1296 homehost
, require_homehost
,
1297 verbose
-quiet
, force
,
1301 struct mddev_ident
*a
, *array_list
= conf_get_ident(NULL
);
1302 struct mddev_dev
*devlist
= conf_get_devs();
1303 struct map_ent
*map
= NULL
;
1305 int failures
, successes
;
1307 if (conf_verify_devnames(array_list
)) {
1308 fprintf(stderr
, Name
1309 ": Duplicate MD device names in "
1310 "conf file were found.\n");
1313 if (devlist
== NULL
) {
1314 fprintf(stderr
, Name
": No devices listed in conf file were found.\n");
1318 fprintf(stderr
, Name
": --update not meaningful with a --scan assembly.\n");
1322 fprintf(stderr
, Name
": --backup_file not meaningful with a --scan assembly.\n");
1325 for (a
= array_list
; a
; a
= a
->next
) {
1331 fprintf(stderr
, Name
" %s: failed to get "
1332 "exclusive lock on mapfile\n",
1338 for (a
= array_list
; a
; a
= a
->next
) {
1343 strcasecmp(a
->devname
, "<ignore>") == 0)
1346 r
= Assemble(ss
, a
->devname
,
1349 readonly
, runstop
, NULL
,
1350 homehost
, require_homehost
,
1351 verbose
-quiet
, force
,
1361 } while (failures
&& successes
);
1362 if (homehost
&& cnt
== 0) {
1363 /* Maybe we can auto-assemble something.
1364 * Repeatedly call Assemble in auto-assemble mode
1369 ident
.autof
= autof
;
1371 struct mddev_dev
*devlist
= conf_get_devs();
1374 rv2
= Assemble(ss
, NULL
,
1389 /* Incase there are stacked devices, we need to go around again */
1391 if (cnt
== 0 && rv
== 0) {
1392 fprintf(stderr
, Name
": No arrays found in config file or automatically\n");
1396 } else if (cnt
== 0 && rv
== 0) {
1397 fprintf(stderr
, Name
": No arrays found in config file\n");
1404 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1405 if (write_behind
&& !bitmap_file
) {
1406 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1410 if (raiddisks
== 0) {
1411 fprintf(stderr
, Name
": no raid-devices specified.\n");
1417 if (strcmp(bitmap_file
, "internal")==0) {
1418 fprintf(stderr
, Name
": 'internal' bitmaps not supported with --build\n");
1423 rv
= Build(devlist
->devname
, chunk
, level
, layout
,
1424 raiddisks
, devlist
->next
, assume_clean
,
1425 bitmap_file
, bitmap_chunk
, write_behind
,
1426 delay
, verbose
-quiet
, autof
, size
);
1429 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1430 if (write_behind
&& !bitmap_file
) {
1431 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1435 if (raiddisks
== 0) {
1436 fprintf(stderr
, Name
": no raid-devices specified.\n");
1441 rv
= Create(ss
, devlist
->devname
, chunk
, level
, layout
, size
<0 ? 0 : size
,
1442 raiddisks
, sparedisks
, ident
.name
, homehost
,
1443 ident
.uuid_set
? ident
.uuid
: NULL
,
1444 devs_found
-1, devlist
->next
, runstop
, verbose
-quiet
, force
, assume_clean
,
1445 bitmap_file
, bitmap_chunk
, write_behind
, delay
, autof
);
1448 if (devmode
== 'E') {
1449 if (devlist
== NULL
&& !scan
) {
1450 fprintf(stderr
, Name
": No devices to examine\n");
1453 if (devlist
== NULL
)
1454 devlist
= conf_get_devs();
1455 if (devlist
== NULL
) {
1456 fprintf(stderr
, Name
": No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1459 if (brief
&& verbose
)
1461 rv
= Examine(devlist
, scan
?(verbose
>1?0:verbose
+1):brief
,
1463 SparcAdjust
, ss
, homehost
);
1464 } else if (devmode
== DetailPlatform
) {
1465 rv
= Detail_Platform(ss
? ss
->ss
: NULL
, ss
? scan
: 1, verbose
);
1467 if (devlist
== NULL
) {
1468 if ((devmode
=='D' || devmode
== Waitclean
) && scan
) {
1469 /* apply --detail or --wait-clean to
1470 * all devices in /proc/mdstat
1472 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1473 struct mdstat_ent
*e
;
1474 struct map_ent
*map
= NULL
;
1476 int v
= verbose
>1?0:verbose
+1;
1478 for (members
= 0; members
<= 1; members
++) {
1479 for (e
=ms
; e
; e
=e
->next
) {
1482 int member
= e
->metadata_version
&&
1483 strncmp(e
->metadata_version
,
1484 "external:/", 10) == 0;
1485 if (members
!= member
)
1487 me
= map_by_devnum(&map
, e
->devnum
);
1489 && strcmp(me
->path
, "/unknown") != 0)
1492 name
= get_md_name(e
->devnum
);
1495 fprintf(stderr
, Name
": cannot find device file for %s\n",
1500 rv
|= Detail(name
, v
,
1504 rv
|= WaitClean(name
, -1, v
);
1509 } else if (devmode
== 'S' && scan
) {
1510 /* apply --stop to all devices in /proc/mdstat */
1511 /* Due to possible stacking of devices, repeat until
1512 * nothing more can be stopped
1514 int progress
=1, err
;
1517 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1518 struct mdstat_ent
*e
;
1520 if (!progress
) last
= 1;
1521 progress
= 0; err
= 0;
1522 for (e
=ms
; e
; e
=e
->next
) {
1523 char *name
= get_md_name(e
->devnum
);
1526 fprintf(stderr
, Name
": cannot find device file for %s\n",
1530 mdfd
= open_mddev(name
, 1);
1532 if (Manage_runstop(name
, mdfd
, -1, quiet
?1:last
?0:-1))
1542 } while (!last
&& err
);
1544 } else if (devmode
== UdevRules
) {
1545 rv
= Write_rules(udev_filename
);
1547 fprintf(stderr
, Name
": No devices given.\n");
1551 for (dv
=devlist
; dv
; dv
=dv
->next
) {
1552 switch(dv
->disposition
) {
1554 rv
|= Detail(dv
->devname
,
1556 export
, test
, homehost
);
1558 case 'K': /* Zero superblock */
1560 rv
|= Kill(dv
->devname
, ss
, force
, quiet
,0);
1564 rv
|= Kill(dv
->devname
, NULL
, force
, q
, 0);
1571 rv
|= Query(dv
->devname
); continue;
1573 rv
|= ExamineBitmap(dv
->devname
, brief
, ss
); continue;
1576 rv
|= Wait(dv
->devname
); continue;
1578 rv
|= WaitClean(dv
->devname
, -1, verbose
-quiet
); continue;
1580 rv
|= Kill_subarray(dv
->devname
, subarray
, quiet
);
1582 case UpdateSubarray
:
1583 if (update
== NULL
) {
1585 Name
": -U/--update must be specified with --update-subarray\n");
1589 rv
|= Update_subarray(dv
->devname
, subarray
, update
, &ident
, quiet
);
1592 mdfd
= open_mddev(dv
->devname
, 1);
1594 switch(dv
->disposition
) {
1596 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1, quiet
); break;
1598 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1, quiet
); break;
1600 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1602 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;
1611 if (!devlist
&& !scan
) {
1612 fprintf(stderr
, Name
": Cannot monitor: need --scan or at least one device\n");
1616 if (pidfile
&& !daemonise
) {
1617 fprintf(stderr
, Name
": Cannot write a pid file when not in daemon mode\n");
1622 if (get_linux_version() > 2006016)
1623 /* mdstat responds to poll */
1628 rv
= Monitor(devlist
, mailaddr
, program
,
1629 delay
?delay
:60, daemonise
, scan
, oneshot
,
1630 dosyslog
, test
, pidfile
, increments
, spare_sharing
);
1634 if (array_size
>= 0) {
1635 /* alway impose array size first, independent of
1637 * Do not allow level or raid_disks changes at the
1638 * same time as that can be irreversibly destructive.
1642 if (raiddisks
|| level
!= UnSet
) {
1643 fprintf(stderr
, Name
": cannot change array size in same operation "
1644 "as changing raiddisks or level.\n"
1645 " Change size first, then check that data is still intact.\n");
1649 sysfs_init(&sra
, mdfd
, 0);
1650 if (array_size
== 0)
1651 err
= sysfs_set_str(&sra
, NULL
, "array_size", "default");
1653 err
= sysfs_set_num(&sra
, NULL
, "array_size", array_size
/ 2);
1656 fprintf(stderr
, Name
": --array-size setting"
1657 " is too large.\n");
1659 fprintf(stderr
, Name
": current kernel does"
1660 " not support setting --array-size\n");
1665 if (devs_found
> 1 && raiddisks
== 0) {
1667 if (size
>= 0 || chunk
|| layout_str
!= NULL
|| bitmap_file
) {
1668 fprintf(stderr
, Name
": --add cannot be used with "
1669 "other geometry changes in --grow mode\n");
1673 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1674 rv
= Grow_Add_device(devlist
->devname
, mdfd
,
1679 } else if (bitmap_file
) {
1680 if (size
>= 0 || raiddisks
|| chunk
||
1681 layout_str
!= NULL
|| devs_found
> 1) {
1682 fprintf(stderr
, Name
": --bitmap changes cannot be "
1683 "used with other geometry changes "
1684 "in --grow mode\n");
1689 delay
= DEFAULT_BITMAP_DELAY
;
1690 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, bitmap_file
,
1691 bitmap_chunk
, delay
, write_behind
, force
);
1692 } else if (grow_continue
)
1693 rv
= Grow_continue_command(devlist
->devname
,
1696 else if (size
>= 0 || raiddisks
!= 0 || layout_str
!= NULL
1697 || chunk
!= 0 || level
!= UnSet
) {
1698 rv
= Grow_reshape(devlist
->devname
, mdfd
, quiet
, backup_file
,
1699 size
, level
, layout_str
, chunk
, raiddisks
,
1701 assume_clean
, force
);
1702 } else if (array_size
< 0)
1703 fprintf(stderr
, Name
": no changes to --grow\n");
1711 fprintf(stderr
, Name
1712 ": --incremental --scan meaningless without --run.\n");
1715 if (devmode
== 'f') {
1716 fprintf(stderr
, Name
1717 ": --incremental --scan --fail not supported.\n");
1720 rv
= IncrementalScan(verbose
);
1723 if (!rebuild_map
&& !scan
) {
1724 fprintf(stderr
, Name
1725 ": --incremental requires a device.\n");
1730 if (devlist
->next
) {
1731 fprintf(stderr
, Name
1732 ": --incremental can only handle one device.\n");
1737 rv
= IncrementalRemove(devlist
->devname
, remove_path
,
1740 rv
= Incremental(devlist
->devname
, verbose
-quiet
,
1741 runstop
, ss
, homehost
,
1742 require_homehost
, autof
,