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;
179 fputs(Usage
, stderr
);
182 /* second, figure out the mode.
183 * Some options force the mode. Others
184 * set the mode if it isn't already
190 shortopt
= short_bitmap_options
;
198 case ReAdd
: /* re-add */
201 shortopt
= short_bitmap_options
;
205 case 'A': newmode
= ASSEMBLE
; shortopt
= short_bitmap_auto_options
; break;
206 case 'B': newmode
= BUILD
; shortopt
= short_bitmap_auto_options
; break;
207 case 'C': newmode
= CREATE
; shortopt
= short_bitmap_auto_options
; break;
208 case 'F': newmode
= MONITOR
;break;
209 case 'G': newmode
= GROW
;
210 shortopt
= short_bitmap_options
;
212 case 'I': newmode
= INCREMENTAL
;
213 shortopt
= short_bitmap_auto_options
; break;
215 newmode
= AUTODETECT
;
246 if (mode
&& newmode
== mode
) {
247 /* everybody happy ! */
248 } else if (mode
&& newmode
!= mode
) {
250 fprintf(stderr
, Name
": ");
251 if (option_index
>= 0)
252 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
254 fprintf(stderr
, "-%c", opt
);
255 fprintf(stderr
, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
256 map_num(modes
, newmode
),
257 map_num(modes
, mode
));
259 } else if (!mode
&& newmode
) {
261 if (mode
== MISC
&& devs_found
) {
262 fprintf(stderr
, Name
": No action given for %s in --misc mode\n",
264 fprintf(stderr
," Action options must come before device names\n");
268 /* special case of -c --help */
269 if ((opt
== 'c' || opt
== ConfigFile
) &&
270 ( strncmp(optarg
, "--h", 3)==0 ||
271 strncmp(optarg
, "-h", 2)==0)) {
272 fputs(Help_config
, stdout
);
276 /* If first option is a device, don't force the mode yet */
278 if (devs_found
== 0) {
279 dv
= malloc(sizeof(*dv
));
281 fprintf(stderr
, Name
": malloc failed\n");
284 dv
->devname
= optarg
;
285 dv
->disposition
= devmode
;
286 dv
->writemostly
= writemostly
;
291 devlistend
= &dv
->next
;
296 /* No mode yet, and this is the second device ... */
297 fprintf(stderr
, Name
": An option must be given to set the mode before a second device\n"
298 " (%s) is listed\n", optarg
);
301 if (option_index
>= 0)
302 fprintf(stderr
, Name
": --%s", long_options
[option_index
].name
);
304 fprintf(stderr
, Name
": -%c", opt
);
305 fprintf(stderr
, " does not set the mode, and so cannot be the first option.\n");
309 /* if we just set the mode, then done */
323 /* an undecorated option - must be a device name.
325 if (devs_found
> 0 && mode
== MANAGE
&& !devmode
) {
326 fprintf(stderr
, Name
": Must give one of -a/-r/-f"
327 " for subsequent devices at %s\n", optarg
);
330 if (devs_found
> 0 && mode
== GROW
&& !devmode
) {
331 fprintf(stderr
, Name
": Must give -a/--add for"
332 " devices to add: %s\n", optarg
);
335 dv
= malloc(sizeof(*dv
));
337 fprintf(stderr
, Name
": malloc failed\n");
340 dv
->devname
= optarg
;
341 dv
->disposition
= devmode
;
342 dv
->writemostly
= writemostly
;
347 devlistend
= &dv
->next
;
353 /* We've got a mode, and opt is now something else which
354 * could depend on the mode */
355 #define O(a,b) ((a<<16)|b)
356 switch (O(mode
,opt
)) {
358 case O(GROW
,ChunkSize
):
360 case O(CREATE
,ChunkSize
):
361 case O(BUILD
,'c'): /* chunk or rounding */
362 case O(BUILD
,ChunkSize
): /* chunk or rounding */
364 fprintf(stderr
, Name
": chunk/rounding may only be specified once. "
365 "Second value is %s.\n", optarg
);
368 chunk
= parse_size(optarg
);
369 if (chunk
< 8 || (chunk
&1)) {
370 fprintf(stderr
, Name
": invalid chunk/rounding value: %s\n",
374 /* Convert sectors to K */
378 case O(INCREMENTAL
, 'e'):
380 case O(ASSEMBLE
,'e'):
381 case O(MISC
,'e'): /* set metadata (superblock) information */
383 fprintf(stderr
, Name
": metadata information already given\n");
386 for(i
=0; !ss
&& superlist
[i
]; i
++)
387 ss
= superlist
[i
]->match_metadata_desc(optarg
);
390 fprintf(stderr
, Name
": unrecognised metadata identifier: %s\n", optarg
);
396 case O(MANAGE
,WriteMostly
):
398 case O(BUILD
,WriteMostly
):
400 case O(CREATE
,WriteMostly
):
401 /* set write-mostly for following devices */
406 /* clear write-mostly for following devices */
413 case O(BUILD
,'z'): /* size */
415 fprintf(stderr
, Name
": size may only be specified once. "
416 "Second value is %s.\n", optarg
);
419 if (strcmp(optarg
, "max")==0)
422 size
= parse_size(optarg
);
424 fprintf(stderr
, Name
": invalid size: %s\n",
428 /* convert sectors to K */
433 case O(GROW
,'Z'): /* array size */
434 if (array_size
>= 0) {
435 fprintf(stderr
, Name
": array-size may only be specified once. "
436 "Second value is %s.\n", optarg
);
439 if (strcmp(optarg
, "max") == 0)
442 array_size
= parse_size(optarg
);
443 if (array_size
<= 0) {
444 fprintf(stderr
, Name
": invalid array size: %s\n",
453 case O(BUILD
,'l'): /* set raid level*/
454 if (level
!= UnSet
) {
455 fprintf(stderr
, Name
": raid level may only be set once. "
456 "Second value is %s.\n", optarg
);
459 level
= map_name(pers
, optarg
);
460 if (level
== UnSet
) {
461 fprintf(stderr
, Name
": invalid raid level: %s\n",
465 if (level
!= 0 && level
!= LEVEL_LINEAR
&& level
!= 1 &&
466 level
!= LEVEL_MULTIPATH
&& level
!= LEVEL_FAULTY
&&
469 fprintf(stderr
, Name
": Raid level %s not permitted with --build.\n",
473 if (sparedisks
> 0 && level
< 1 && level
>= -1) {
474 fprintf(stderr
, Name
": raid level %s is incompatible with spare-devices setting.\n",
481 case O(GROW
, 'p'): /* new layout */
482 case O(GROW
, Layout
):
484 fprintf(stderr
,Name
": layout may only be sent once. "
485 "Second value was %s\n", optarg
);
489 /* 'Grow' will parse the value */
492 case O(CREATE
,'p'): /* raid5 layout */
493 case O(CREATE
,Layout
):
494 case O(BUILD
,'p'): /* faulty layout */
495 case O(BUILD
,Layout
):
496 if (layout
!= UnSet
) {
497 fprintf(stderr
,Name
": layout may only be sent once. "
498 "Second value was %s\n", optarg
);
503 fprintf(stderr
, Name
": layout not meaningful for %s arrays.\n",
504 map_num(pers
, level
));
507 fprintf(stderr
, Name
": raid level must be given before layout.\n");
511 layout
= map_name(r5layout
, optarg
);
513 fprintf(stderr
, Name
": layout %s not understood for raid5.\n",
519 layout
= map_name(r6layout
, optarg
);
521 fprintf(stderr
, Name
": layout %s not understood for raid6.\n",
528 layout
= parse_layout_10(optarg
);
530 fprintf(stderr
, Name
": layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg
);
538 layout
= parse_layout_faulty(optarg
);
540 fprintf(stderr
, Name
": layout %s not understood for faulty.\n",
548 case O(CREATE
,AssumeClean
):
549 case O(BUILD
,AssumeClean
): /* assume clean */
550 case O(GROW
,AssumeClean
):
556 case O(BUILD
,'n'): /* number of raid disks */
558 fprintf(stderr
, Name
": raid-devices set twice: %d and %s\n",
562 raiddisks
= strtol(optarg
, &c
, 10);
563 if (!optarg
[0] || *c
|| raiddisks
<=0) {
564 fprintf(stderr
, Name
": invalid number of raid devices: %s\n",
568 ident
.raid_disks
= raiddisks
;
571 case O(CREATE
,'x'): /* number of spare (eXtra) discs */
573 fprintf(stderr
,Name
": spare-devices set twice: %d and %s\n",
577 if (level
!= UnSet
&& level
<= 0 && level
>= -1) {
578 fprintf(stderr
, Name
": spare-devices setting is incompatible with raid level %d\n",
582 sparedisks
= strtol(optarg
, &c
, 10);
583 if (!optarg
[0] || *c
|| sparedisks
< 0) {
584 fprintf(stderr
, Name
": invalid number of spare-devices: %s\n",
594 case O(INCREMENTAL
,'a'):
595 case O(INCREMENTAL
,Auto
):
596 case O(ASSEMBLE
,'a'):
597 case O(ASSEMBLE
,Auto
): /* auto-creation of device node */
598 autof
= parse_auto(optarg
, "--auto flag", 0);
601 case O(CREATE
,Symlinks
):
602 case O(BUILD
,Symlinks
):
603 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
607 case O(BUILD
,'f'): /* force honouring '-n 1' */
608 case O(BUILD
,Force
): /* force honouring '-n 1' */
609 case O(GROW
,'f'): /* ditto */
610 case O(GROW
,Force
): /* ditto */
611 case O(CREATE
,'f'): /* force honouring of device list */
612 case O(CREATE
,Force
): /* force honouring of device list */
613 case O(ASSEMBLE
,'f'): /* force assembly */
614 case O(ASSEMBLE
,Force
): /* force assembly */
615 case O(MISC
,'f'): /* force zero */
616 case O(MISC
,Force
): /* force zero */
617 case O(MANAGE
,Force
): /* add device which is too large */
620 /* now for the Assemble options */
621 case O(ASSEMBLE
, FreezeReshape
): /* Freeze reshape during
623 case O(INCREMENTAL
, FreezeReshape
):
626 case O(CREATE
,'u'): /* uuid of array */
627 case O(ASSEMBLE
,'u'): /* uuid of array */
628 if (ident
.uuid_set
) {
629 fprintf(stderr
, Name
": uuid cannot be set twice. "
630 "Second value %s.\n", optarg
);
633 if (parse_uuid(optarg
, ident
.uuid
))
636 fprintf(stderr
,Name
": Bad uuid: %s\n", optarg
);
642 case O(ASSEMBLE
,'N'):
645 fprintf(stderr
, Name
": name cannot be set twice. "
646 "Second value %s.\n", optarg
);
649 if (mode
== MISC
&& !subarray
) {
650 fprintf(stderr
, Name
": -N/--name only valid with --update-subarray in misc mode\n");
653 if (strlen(optarg
) > 32) {
654 fprintf(stderr
, Name
": name '%s' is too long, 32 chars max.\n",
658 strcpy(ident
.name
, optarg
);
661 case O(ASSEMBLE
,'m'): /* super-minor for array */
662 case O(ASSEMBLE
,SuperMinor
):
663 if (ident
.super_minor
!= UnSet
) {
664 fprintf(stderr
, Name
": super-minor cannot be set twice. "
665 "Second value: %s.\n", optarg
);
668 if (strcmp(optarg
, "dev")==0)
669 ident
.super_minor
= -2;
671 ident
.super_minor
= strtoul(optarg
, &cp
, 10);
672 if (!optarg
[0] || *cp
) {
673 fprintf(stderr
, Name
": Bad super-minor number: %s.\n", optarg
);
679 case O(ASSEMBLE
,'U'): /* update the superblock */
682 fprintf(stderr
, Name
": Can only update one aspect"
683 " of superblock, both %s and %s given.\n",
687 if (mode
== MISC
&& !subarray
) {
688 fprintf(stderr
, Name
": Only subarrays can be"
689 " updated in misc mode\n");
693 if (strcmp(update
, "sparc2.2")==0)
695 if (strcmp(update
, "super-minor") == 0)
697 if (strcmp(update
, "summaries")==0)
699 if (strcmp(update
, "resync")==0)
701 if (strcmp(update
, "uuid")==0)
703 if (strcmp(update
, "name")==0)
705 if (strcmp(update
, "homehost")==0)
707 if (strcmp(update
, "devicesize")==0)
709 if (strcmp(update
, "no-bitmap")==0)
711 if (strcmp(update
, "byteorder")==0) {
714 Name
": must not set metadata"
715 " type with --update=byteorder.\n");
718 for(i
=0; !ss
&& superlist
[i
]; i
++)
719 ss
= superlist
[i
]->match_metadata_desc(
722 fprintf(stderr
, Name
": INTERNAL ERROR"
723 " cannot find 0.swap\n");
729 if (strcmp(update
,"?") == 0 ||
730 strcmp(update
, "help") == 0) {
732 fprintf(outf
, Name
": ");
736 Name
": '--update=%s' is invalid. ",
739 fprintf(outf
, "Valid --update options are:\n"
740 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
741 " 'summaries', 'homehost', 'byteorder', 'devicesize',\n"
743 exit(outf
== stdout
? 0 : 2);
746 /* update=devicesize is allowed with --re-add */
747 if (devmode
!= 'a' || re_add
!= 1) {
748 fprintf(stderr
, Name
"--update in Manage mode only"
749 " allowed with --re-add.\n");
753 fprintf(stderr
, Name
": Can only update one aspect"
754 " of superblock, both %s and %s given.\n",
759 if (strcmp(update
, "devicesize") != 0) {
760 fprintf(stderr
, Name
": only 'devicesize' can be"
761 " updated with --re-add\n");
766 case O(INCREMENTAL
,NoDegraded
):
767 fprintf(stderr
, Name
": --no-degraded is deprecated in Incremental mode\n");
768 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
769 runstop
= -1; /* --stop isn't allowed for --assemble,
770 * so we overload slightly */
773 case O(ASSEMBLE
,'c'):
774 case O(ASSEMBLE
,ConfigFile
):
775 case O(INCREMENTAL
, 'c'):
776 case O(INCREMENTAL
, ConfigFile
):
778 case O(MISC
, ConfigFile
):
780 case O(MONITOR
,ConfigFile
):
782 fprintf(stderr
, Name
": configfile cannot be set twice. "
783 "Second value is %s.\n", optarg
);
787 set_conffile(configfile
);
788 /* FIXME possibly check that config file exists. Even parse it */
790 case O(ASSEMBLE
,'s'): /* scan */
793 case O(INCREMENTAL
,'s'):
797 case O(MONITOR
,'m'): /* mail address */
798 case O(MONITOR
,EMail
):
800 fprintf(stderr
, Name
": only specify one mailaddress. %s ignored.\n",
806 case O(MONITOR
,'p'): /* alert program */
807 case O(MONITOR
,ProgramOpt
): /* alert program */
809 fprintf(stderr
, Name
": only specify one alter program. %s ignored.\n",
815 case O(MONITOR
,'r'): /* rebuild increments */
816 case O(MONITOR
,Increment
):
817 increments
= atoi(optarg
);
818 if (increments
>99 || increments
<1) {
819 fprintf(stderr
, Name
": please specify positive integer between 1 and 99 as rebuild increments.\n");
824 case O(MONITOR
,'d'): /* delay in seconds */
826 case O(BUILD
,'d'): /* delay for bitmap updates */
829 fprintf(stderr
, Name
": only specify delay once. %s ignored.\n",
832 delay
= strtol(optarg
, &c
, 10);
833 if (!optarg
[0] || *c
|| delay
<1) {
834 fprintf(stderr
, Name
": invalid delay: %s\n",
840 case O(MONITOR
,'f'): /* daemonise */
841 case O(MONITOR
,Fork
):
844 case O(MONITOR
,'i'): /* pid */
846 fprintf(stderr
, Name
": only specify one pid file. %s ignored.\n",
851 case O(MONITOR
,'1'): /* oneshot */
855 case O(MONITOR
,'t'): /* test */
858 case O(MONITOR
,'y'): /* log messages to syslog */
859 openlog("mdadm", LOG_PID
, SYSLOG_FACILITY
);
862 case O(MONITOR
, NoSharing
):
865 /* now the general management options. Some are applicable
866 * to other modes. None have arguments.
871 case O(MANAGE
,Add
): /* add a drive */
875 case O(MANAGE
,ReAdd
):
879 case O(MANAGE
,'r'): /* remove a drive */
880 case O(MANAGE
,Remove
):
883 case O(MANAGE
,'f'): /* set faulty */
885 case O(INCREMENTAL
,'f'):
886 case O(INCREMENTAL
,Remove
):
887 case O(INCREMENTAL
,Fail
): /* r for incremental is taken, use f
888 * even though we will both fail and
889 * remove the device */
892 case O(INCREMENTAL
,'R'):
894 case O(ASSEMBLE
,'R'):
896 case O(CREATE
,'R'): /* Run the array */
898 fprintf(stderr
, Name
": Cannot both Stop and Run an array\n");
905 fprintf(stderr
, Name
": Cannot both Run and Stop an array\n");
924 case O(MISC
, WaitOpt
):
925 case O(MISC
, Waitclean
):
926 case O(MISC
, DetailPlatform
):
927 case O(MISC
, KillSubarray
):
928 case O(MISC
, UpdateSubarray
):
929 if (opt
== KillSubarray
|| opt
== UpdateSubarray
) {
931 fprintf(stderr
, Name
": subarray can only"
932 " be specified once\n");
937 if (devmode
&& devmode
!= opt
&&
938 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
939 fprintf(stderr
, Name
": --examine/-E cannot be given with ");
940 if (devmode
== 'E') {
941 if (option_index
>= 0)
942 fprintf(stderr
, "--%s\n",
943 long_options
[option_index
].name
);
945 fprintf(stderr
, "-%c\n", opt
);
946 } else if (isalpha(devmode
))
947 fprintf(stderr
, "-%c\n", devmode
);
949 fprintf(stderr
, "previous option\n");
954 case O(MISC
, UdevRules
):
955 if (devmode
&& devmode
!= opt
) {
956 fprintf(stderr
, Name
": --udev-rules must"
957 " be the only option.\n");
960 fprintf(stderr
, Name
": only specify one udev "
961 "rule filename. %s ignored.\n",
964 udev_filename
= optarg
;
972 case O(MISC
, Sparc22
):
973 if (devmode
!= 'E') {
974 fprintf(stderr
, Name
": --sparc2.2 only allowed with --examine\n");
980 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
981 case O(ASSEMBLE
,Bitmap
):
983 fprintf(stderr
, Name
": bitmap file needed with -b in --assemble mode\n");
986 if (strcmp(optarg
, "internal")==0) {
987 fprintf(stderr
, Name
": there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
990 bitmap_fd
= open(optarg
, O_RDWR
);
991 if (!*optarg
|| bitmap_fd
< 0) {
992 fprintf(stderr
, Name
": cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
995 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
998 case O(ASSEMBLE
, BackupFile
):
999 case O(GROW
, BackupFile
):
1000 /* Specify a file into which grow might place a backup,
1001 * or from which assemble might recover a backup
1004 fprintf(stderr
, Name
": backup file already specified, rejecting %s\n", optarg
);
1007 backup_file
= optarg
;
1010 case O(GROW
, Continue
):
1011 /* Continue interrupted grow
1015 case O(ASSEMBLE
, InvalidBackup
):
1016 /* Acknowledge that the backupfile is invalid, but ask
1017 * to continue anyway
1023 case O(BUILD
,Bitmap
):
1025 case O(CREATE
,Bitmap
): /* here we create the bitmap */
1026 if (strcmp(optarg
, "none") == 0) {
1027 fprintf(stderr
, Name
": '--bitmap none' only"
1028 " support for --grow\n");
1033 case O(GROW
,Bitmap
):
1034 if (strcmp(optarg
, "internal")== 0 ||
1035 strcmp(optarg
, "none")== 0 ||
1036 strchr(optarg
, '/') != NULL
) {
1037 bitmap_file
= optarg
;
1041 fprintf(stderr
, Name
": bitmap file must contain a '/', or be 'internal', or 'none'\n"
1042 " not '%s'\n", optarg
);
1045 case O(GROW
,BitmapChunk
):
1046 case O(BUILD
,BitmapChunk
):
1047 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
1048 bitmap_chunk
= parse_size(optarg
);
1049 if (bitmap_chunk
< 0 ||
1050 bitmap_chunk
& (bitmap_chunk
- 1)) {
1052 Name
": invalid bitmap chunksize: %s\n",
1056 /* convert sectors to B, chunk of 0 means 512B */
1057 bitmap_chunk
= bitmap_chunk
? bitmap_chunk
* 512 : 512;
1060 case O(GROW
, WriteBehind
):
1061 case O(BUILD
, WriteBehind
):
1062 case O(CREATE
, WriteBehind
): /* write-behind mode */
1063 write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
1065 write_behind
= strtol(optarg
, &c
, 10);
1066 if (write_behind
< 0 || *c
||
1067 write_behind
> 16383) {
1068 fprintf(stderr
, Name
": Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
1074 case O(INCREMENTAL
, 'r'):
1075 case O(INCREMENTAL
, RebuildMapOpt
):
1078 case O(INCREMENTAL
, IncrementalPath
):
1079 remove_path
= optarg
;
1082 /* We have now processed all the valid options. Anything else is
1085 if (option_index
> 0)
1086 fprintf(stderr
, Name
":option --%s not valid in %s mode\n",
1087 long_options
[option_index
].name
,
1088 map_num(modes
, mode
));
1090 fprintf(stderr
, Name
": option -%c not valid in %s mode\n",
1091 opt
, map_num(modes
, mode
));
1097 char *help_text
= Help
;
1098 if (print_help
== 2)
1099 help_text
= OptionHelp
;
1102 case ASSEMBLE
: help_text
= Help_assemble
; break;
1103 case BUILD
: help_text
= Help_build
; break;
1104 case CREATE
: help_text
= Help_create
; break;
1105 case MANAGE
: help_text
= Help_manage
; break;
1106 case MISC
: help_text
= Help_misc
; break;
1107 case MONITOR
: help_text
= Help_monitor
; break;
1108 case GROW
: help_text
= Help_grow
; break;
1109 case INCREMENTAL
:help_text
= Help_incr
; break;
1111 fputs(help_text
,stdout
);
1115 if (!mode
&& devs_found
) {
1118 if (devlist
->disposition
== 0)
1119 devlist
->disposition
= devmode
;
1122 fputs(Usage
, stderr
);
1127 struct createinfo
*ci
= conf_get_create_info();
1129 if (strcasecmp(symlinks
, "yes") == 0)
1131 else if (strcasecmp(symlinks
, "no") == 0)
1134 fprintf(stderr
, Name
": option --symlinks must be 'no' or 'yes'\n");
1138 /* Ok, got the option parsing out of the way
1139 * hopefully it's mostly right but there might be some stuff
1142 * That is mosty checked in the per-mode stuff but...
1144 * For @,B,C and A without -s, the first device listed must be an md device
1145 * we check that here and open it.
1148 if (mode
==MANAGE
|| mode
== BUILD
|| mode
== CREATE
|| mode
== GROW
||
1149 (mode
== ASSEMBLE
&& ! scan
)) {
1150 if (devs_found
< 1) {
1151 fprintf(stderr
, Name
": an md device must be given in this mode\n");
1154 if ((int)ident
.super_minor
== -2 && autof
) {
1155 fprintf(stderr
, Name
": --super-minor=dev is incompatible with --auto\n");
1158 if (mode
== MANAGE
|| mode
== GROW
) {
1159 mdfd
= open_mddev(devlist
->devname
, 1);
1163 /* non-existent device is OK */
1164 mdfd
= open_mddev(devlist
->devname
, 0);
1166 fprintf(stderr
, Name
": device %s exists but is not an "
1167 "md array.\n", devlist
->devname
);
1170 if ((int)ident
.super_minor
== -2) {
1173 fprintf(stderr
, Name
": --super-minor=dev given, and "
1174 "listed device %s doesn't exist.\n",
1179 ident
.super_minor
= minor(stb
.st_rdev
);
1181 if (mdfd
>= 0 && mode
!= MANAGE
&& mode
!= GROW
) {
1182 /* We don't really want this open yet, we just might
1183 * have wanted to check some things
1191 if (raiddisks
== 1 && !force
&& level
!= -5) {
1192 fprintf(stderr
, Name
": '1' is an unusual number of drives for an array, so it is probably\n"
1193 " a mistake. If you really mean it you will need to specify --force before\n"
1194 " setting the number of drives.\n");
1199 if (homehost
== NULL
)
1200 homehost
= conf_get_homehost(&require_homehost
);
1201 if (homehost
== NULL
|| strcasecmp(homehost
, "<system>")==0) {
1202 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
1203 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
1204 homehost
= sys_hostname
;
1207 if (homehost
&& (!homehost
[0] || strcasecmp(homehost
, "<none>") == 0)) {
1209 require_homehost
= 0;
1212 if ((mode
!= MISC
|| devmode
!= 'E') &&
1214 fprintf(stderr
, Name
": must be super-user to perform this action\n");
1218 ident
.autof
= autof
;
1223 /* readonly, add/remove, readwrite, runstop */
1225 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1226 if (!rv
&& devs_found
>1)
1227 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
1228 devlist
->next
, verbose
-quiet
, test
,
1230 if (!rv
&& readonly
< 0)
1231 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1233 rv
= Manage_runstop(devlist
->devname
, mdfd
, runstop
, quiet
);
1236 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1237 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !scan
) {
1238 /* Only a device has been given, so get details from config file */
1239 struct mddev_ident
*array_ident
= conf_get_ident(devlist
->devname
);
1240 if (array_ident
== NULL
) {
1241 fprintf(stderr
, Name
": %s not identified in config file.\n",
1247 if (array_ident
->autof
== 0)
1248 array_ident
->autof
= autof
;
1249 rv
|= Assemble(ss
, devlist
->devname
, array_ident
,
1250 NULL
, backup_file
, invalid_backup
,
1251 readonly
, runstop
, update
,
1252 homehost
, require_homehost
,
1253 verbose
-quiet
, force
,
1257 rv
= Assemble(ss
, devlist
->devname
, &ident
,
1258 devlist
->next
, backup_file
, invalid_backup
,
1259 readonly
, runstop
, update
,
1260 homehost
, require_homehost
,
1261 verbose
-quiet
, force
,
1263 else if (devs_found
>0) {
1264 if (update
&& devs_found
> 1) {
1265 fprintf(stderr
, Name
": can only update a single array at a time\n");
1268 if (backup_file
&& devs_found
> 1) {
1269 fprintf(stderr
, Name
": can only assemble a single array when providing a backup file.\n");
1272 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1273 struct mddev_ident
*array_ident
= conf_get_ident(dv
->devname
);
1274 if (array_ident
== NULL
) {
1275 fprintf(stderr
, Name
": %s not identified in config file.\n",
1280 if (array_ident
->autof
== 0)
1281 array_ident
->autof
= autof
;
1282 rv
|= Assemble(ss
, dv
->devname
, array_ident
,
1283 NULL
, backup_file
, invalid_backup
,
1284 readonly
, runstop
, update
,
1285 homehost
, require_homehost
,
1286 verbose
-quiet
, force
,
1290 struct mddev_ident
*a
, *array_list
= conf_get_ident(NULL
);
1291 struct mddev_dev
*devlist
= conf_get_devs();
1292 struct map_ent
*map
= NULL
;
1294 int failures
, successes
;
1296 if (conf_verify_devnames(array_list
)) {
1297 fprintf(stderr
, Name
1298 ": Duplicate MD device names in "
1299 "conf file were found.\n");
1302 if (devlist
== NULL
) {
1303 fprintf(stderr
, Name
": No devices listed in conf file were found.\n");
1307 fprintf(stderr
, Name
": --update not meaningful with a --scan assembly.\n");
1311 fprintf(stderr
, Name
": --backup_file not meaningful with a --scan assembly.\n");
1314 for (a
= array_list
; a
; a
= a
->next
) {
1320 fprintf(stderr
, Name
" %s: failed to get "
1321 "exclusive lock on mapfile\n",
1327 for (a
= array_list
; a
; a
= a
->next
) {
1332 strcasecmp(a
->devname
, "<ignore>") == 0)
1335 r
= Assemble(ss
, a
->devname
,
1338 readonly
, runstop
, NULL
,
1339 homehost
, require_homehost
,
1340 verbose
-quiet
, force
,
1350 } while (failures
&& successes
);
1351 if (homehost
&& cnt
== 0) {
1352 /* Maybe we can auto-assemble something.
1353 * Repeatedly call Assemble in auto-assemble mode
1358 ident
.autof
= autof
;
1360 struct mddev_dev
*devlist
= conf_get_devs();
1363 rv2
= Assemble(ss
, NULL
,
1378 /* Incase there are stacked devices, we need to go around again */
1380 if (cnt
== 0 && rv
== 0) {
1381 fprintf(stderr
, Name
": No arrays found in config file or automatically\n");
1385 } else if (cnt
== 0 && rv
== 0) {
1386 fprintf(stderr
, Name
": No arrays found in config file\n");
1393 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1394 if (write_behind
&& !bitmap_file
) {
1395 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1399 if (raiddisks
== 0) {
1400 fprintf(stderr
, Name
": no raid-devices specified.\n");
1406 if (strcmp(bitmap_file
, "internal")==0) {
1407 fprintf(stderr
, Name
": 'internal' bitmaps not supported with --build\n");
1412 rv
= Build(devlist
->devname
, chunk
, level
, layout
,
1413 raiddisks
, devlist
->next
, assume_clean
,
1414 bitmap_file
, bitmap_chunk
, write_behind
,
1415 delay
, verbose
-quiet
, autof
, size
);
1418 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1419 if (write_behind
&& !bitmap_file
) {
1420 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1424 if (raiddisks
== 0) {
1425 fprintf(stderr
, Name
": no raid-devices specified.\n");
1430 rv
= Create(ss
, devlist
->devname
, chunk
, level
, layout
, size
<0 ? 0 : size
,
1431 raiddisks
, sparedisks
, ident
.name
, homehost
,
1432 ident
.uuid_set
? ident
.uuid
: NULL
,
1433 devs_found
-1, devlist
->next
, runstop
, verbose
-quiet
, force
, assume_clean
,
1434 bitmap_file
, bitmap_chunk
, write_behind
, delay
, autof
);
1437 if (devmode
== 'E') {
1438 if (devlist
== NULL
&& !scan
) {
1439 fprintf(stderr
, Name
": No devices to examine\n");
1442 if (devlist
== NULL
)
1443 devlist
= conf_get_devs();
1444 if (devlist
== NULL
) {
1445 fprintf(stderr
, Name
": No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1448 if (brief
&& verbose
)
1450 rv
= Examine(devlist
, scan
?(verbose
>1?0:verbose
+1):brief
,
1452 SparcAdjust
, ss
, homehost
);
1453 } else if (devmode
== DetailPlatform
) {
1454 rv
= Detail_Platform(ss
? ss
->ss
: NULL
, ss
? scan
: 1, verbose
);
1456 if (devlist
== NULL
) {
1457 if ((devmode
=='D' || devmode
== Waitclean
) && scan
) {
1458 /* apply --detail or --wait-clean to
1459 * all devices in /proc/mdstat
1461 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1462 struct mdstat_ent
*e
;
1463 struct map_ent
*map
= NULL
;
1465 int v
= verbose
>1?0:verbose
+1;
1467 for (members
= 0; members
<= 1; members
++) {
1468 for (e
=ms
; e
; e
=e
->next
) {
1471 int member
= e
->metadata_version
&&
1472 strncmp(e
->metadata_version
,
1473 "external:/", 10) == 0;
1474 if (members
!= member
)
1476 me
= map_by_devnum(&map
, e
->devnum
);
1478 && strcmp(me
->path
, "/unknown") != 0)
1481 name
= get_md_name(e
->devnum
);
1484 fprintf(stderr
, Name
": cannot find device file for %s\n",
1489 rv
|= Detail(name
, v
,
1493 rv
|= WaitClean(name
, -1, v
);
1498 } else if (devmode
== 'S' && scan
) {
1499 /* apply --stop to all devices in /proc/mdstat */
1500 /* Due to possible stacking of devices, repeat until
1501 * nothing more can be stopped
1503 int progress
=1, err
;
1506 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1507 struct mdstat_ent
*e
;
1509 if (!progress
) last
= 1;
1510 progress
= 0; err
= 0;
1511 for (e
=ms
; e
; e
=e
->next
) {
1512 char *name
= get_md_name(e
->devnum
);
1515 fprintf(stderr
, Name
": cannot find device file for %s\n",
1519 mdfd
= open_mddev(name
, 1);
1521 if (Manage_runstop(name
, mdfd
, -1, quiet
?1:last
?0:-1))
1531 } while (!last
&& err
);
1533 } else if (devmode
== UdevRules
) {
1534 rv
= Write_rules(udev_filename
);
1536 fprintf(stderr
, Name
": No devices given.\n");
1540 for (dv
=devlist
; dv
; dv
=dv
->next
) {
1541 switch(dv
->disposition
) {
1543 rv
|= Detail(dv
->devname
,
1545 export
, test
, homehost
);
1547 case 'K': /* Zero superblock */
1549 rv
|= Kill(dv
->devname
, ss
, force
, quiet
,0);
1553 rv
|= Kill(dv
->devname
, NULL
, force
, q
, 0);
1560 rv
|= Query(dv
->devname
); continue;
1562 rv
|= ExamineBitmap(dv
->devname
, brief
, ss
); continue;
1565 rv
|= Wait(dv
->devname
); continue;
1567 rv
|= WaitClean(dv
->devname
, -1, verbose
-quiet
); continue;
1569 rv
|= Kill_subarray(dv
->devname
, subarray
, quiet
);
1571 case UpdateSubarray
:
1572 if (update
== NULL
) {
1574 Name
": -U/--update must be specified with --update-subarray\n");
1578 rv
|= Update_subarray(dv
->devname
, subarray
, update
, &ident
, quiet
);
1581 mdfd
= open_mddev(dv
->devname
, 1);
1583 switch(dv
->disposition
) {
1585 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1, quiet
); break;
1587 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1, quiet
); break;
1589 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1591 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;
1600 if (!devlist
&& !scan
) {
1601 fprintf(stderr
, Name
": Cannot monitor: need --scan or at least one device\n");
1605 if (pidfile
&& !daemonise
) {
1606 fprintf(stderr
, Name
": Cannot write a pid file when not in daemon mode\n");
1611 if (get_linux_version() > 2006016)
1612 /* mdstat responds to poll */
1617 rv
= Monitor(devlist
, mailaddr
, program
,
1618 delay
?delay
:60, daemonise
, scan
, oneshot
,
1619 dosyslog
, test
, pidfile
, increments
, spare_sharing
);
1623 if (array_size
>= 0) {
1624 /* alway impose array size first, independent of
1626 * Do not allow level or raid_disks changes at the
1627 * same time as that can be irreversibly destructive.
1631 if (raiddisks
|| level
!= UnSet
) {
1632 fprintf(stderr
, Name
": cannot change array size in same operation "
1633 "as changing raiddisks or level.\n"
1634 " Change size first, then check that data is still intact.\n");
1638 sysfs_init(&sra
, mdfd
, 0);
1639 if (array_size
== 0)
1640 err
= sysfs_set_str(&sra
, NULL
, "array_size", "default");
1642 err
= sysfs_set_num(&sra
, NULL
, "array_size", array_size
/ 2);
1645 fprintf(stderr
, Name
": --array-size setting"
1646 " is too large.\n");
1648 fprintf(stderr
, Name
": current kernel does"
1649 " not support setting --array-size\n");
1654 if (devs_found
> 1 && raiddisks
== 0) {
1656 if (size
>= 0 || chunk
|| layout_str
!= NULL
|| bitmap_file
) {
1657 fprintf(stderr
, Name
": --add cannot be used with "
1658 "other geometry changes in --grow mode\n");
1662 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1663 rv
= Grow_Add_device(devlist
->devname
, mdfd
,
1668 } else if (bitmap_file
) {
1669 if (size
>= 0 || raiddisks
|| chunk
||
1670 layout_str
!= NULL
|| devs_found
> 1) {
1671 fprintf(stderr
, Name
": --bitmap changes cannot be "
1672 "used with other geometry changes "
1673 "in --grow mode\n");
1678 delay
= DEFAULT_BITMAP_DELAY
;
1679 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, bitmap_file
,
1680 bitmap_chunk
, delay
, write_behind
, force
);
1681 } else if (grow_continue
)
1682 rv
= Grow_continue_command(devlist
->devname
,
1685 else if (size
>= 0 || raiddisks
!= 0 || layout_str
!= NULL
1686 || chunk
!= 0 || level
!= UnSet
) {
1687 rv
= Grow_reshape(devlist
->devname
, mdfd
, quiet
, backup_file
,
1688 size
, level
, layout_str
, chunk
, raiddisks
,
1690 assume_clean
, force
);
1691 } else if (array_size
< 0)
1692 fprintf(stderr
, Name
": no changes to --grow\n");
1700 fprintf(stderr
, Name
1701 ": --incremental --scan meaningless without --run.\n");
1704 if (devmode
== 'f') {
1705 fprintf(stderr
, Name
1706 ": --incremental --scan --fail not supported.\n");
1709 rv
= IncrementalScan(verbose
);
1712 if (!rebuild_map
&& !scan
) {
1713 fprintf(stderr
, Name
1714 ": --incremental requires a device.\n");
1719 if (devlist
->next
) {
1720 fprintf(stderr
, Name
1721 ": --incremental can only handle one device.\n");
1726 rv
= IncrementalRemove(devlist
->devname
, remove_path
,
1729 rv
= Incremental(devlist
->devname
, verbose
-quiet
,
1730 runstop
, ss
, homehost
,
1731 require_homehost
, autof
,