2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2012 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 static int scan_assemble(struct supertype
*ss
,
35 struct mddev_ident
*ident
);
36 static int misc_scan(char devmode
, struct context
*c
);
37 static int stop_scan(int verbose
);
38 static int misc_list(struct mddev_dev
*devlist
,
39 struct mddev_ident
*ident
,
40 struct supertype
*ss
, struct context
*c
);
43 int main(int argc
, char *argv
[])
51 unsigned long long array_size
= 0;
52 unsigned long long data_offset
= INVALID_SECTORS
;
53 struct mddev_ident ident
;
54 char *configfile
= NULL
;
57 struct mddev_dev
*devlist
= NULL
;
58 struct mddev_dev
**devlistend
= & devlist
;
61 char *symlinks
= NULL
;
62 int grow_continue
= 0;
63 /* autof indicates whether and how to create device node.
64 * bottom 3 bits are style. Rest (when shifted) are number of parts
66 * 1 - don't create (no)
67 * 2 - if is_standard, then create (yes)
68 * 3 - create as 'md' - reject is_standard mdp (md)
69 * 4 - create as 'mdp' - reject is_standard md (mdp)
70 * 5 - default to md if not is_standard (md in config file)
71 * 6 - default to mdp if not is_standard (part, or mdp in config file)
74 .require_homehost
= 1,
79 .bitmap_chunk
= UnSet
,
82 char sys_hostname
[256];
83 char *mailaddr
= NULL
;
89 int spare_sharing
= 1;
90 struct supertype
*ss
= NULL
;
92 char *shortopt
= short_options
;
95 char *remove_path
= NULL
;
96 char *udev_filename
= NULL
;
103 srandom(time(0) ^ getpid());
107 ident
.raid_disks
= UnSet
;
108 ident
.super_minor
= UnSet
;
110 ident
.spare_group
= NULL
;
113 ident
.bitmap_fd
= -1;
114 ident
.bitmap_file
= NULL
;
116 ident
.container
= NULL
;
119 while ((option_index
= -1) ,
120 (opt
=getopt_long(argc
, argv
,
121 shortopt
, long_options
,
122 &option_index
)) != -1) {
124 /* firstly, some mode-independent options */
134 fputs(Version
, stderr
);
137 case 'v': c
.verbose
++;
140 case 'q': c
.verbose
--;
144 if (mode
== ASSEMBLE
|| mode
== BUILD
|| mode
== CREATE
145 || mode
== GROW
|| mode
== INCREMENTAL
147 break; /* b means bitmap */
152 case 'Y': c
.export
++;
156 if (strcasecmp(optarg
, "<ignore>") == 0)
157 c
.require_homehost
= 0;
163 * --offroot sets first char of argv[0] to @. This is used
164 * by systemd to signal that the task was launched from
165 * initrd/initramfs and should be preserved during shutdown
175 if (asprintf(&c
.prefer
, "/%s/", optarg
) <= 0)
181 fputs(Usage
, stderr
);
184 /* second, figure out the mode.
185 * Some options force the mode. Others
186 * set the mode if it isn't already
192 shortopt
= short_bitmap_options
;
200 case ReAdd
: /* re-add */
203 shortopt
= short_bitmap_options
;
207 case 'A': newmode
= ASSEMBLE
;
208 shortopt
= short_bitmap_auto_options
;
210 case 'B': newmode
= BUILD
;
211 shortopt
= short_bitmap_auto_options
;
213 case 'C': newmode
= CREATE
;
214 shortopt
= short_bitmap_auto_options
;
216 case 'F': newmode
= MONITOR
;
218 case 'G': newmode
= GROW
;
219 shortopt
= short_bitmap_options
;
221 case 'I': newmode
= INCREMENTAL
;
222 shortopt
= short_bitmap_auto_options
;
225 newmode
= AUTODETECT
;
256 if (mode
&& newmode
== mode
) {
257 /* everybody happy ! */
258 } else if (mode
&& newmode
!= mode
) {
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 pr_err("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
= xmalloc(sizeof(*dv
));
290 dv
->devname
= optarg
;
291 dv
->disposition
= devmode
;
292 dv
->writemostly
= writemostly
;
296 devlistend
= &dv
->next
;
301 /* No mode yet, and this is the second device ... */
302 pr_err("An option must be given to set the mode before a second device\n"
303 " (%s) is listed\n", optarg
);
306 if (option_index
>= 0)
307 pr_err("--%s", long_options
[option_index
].name
);
310 fprintf(stderr
, " does not set the mode, and so cannot be the first option.\n");
314 /* if we just set the mode, then done */
328 /* an undecorated option - must be a device name.
331 if (devs_found
> 0 && devmode
== DetailPlatform
) {
332 pr_err("controller may only be specified once. %s ignored\n",
337 if (devs_found
> 0 && mode
== MANAGE
&& !devmode
) {
338 pr_err("Must give one of -a/-r/-f"
339 " for subsequent devices at %s\n", optarg
);
342 if (devs_found
> 0 && mode
== GROW
&& !devmode
) {
343 pr_err("Must give -a/--add for"
344 " devices to add: %s\n", optarg
);
347 dv
= xmalloc(sizeof(*dv
));
348 dv
->devname
= optarg
;
349 dv
->disposition
= devmode
;
350 dv
->writemostly
= writemostly
;
354 devlistend
= &dv
->next
;
360 /* We've got a mode, and opt is now something else which
361 * could depend on the mode */
362 #define O(a,b) ((a<<16)|b)
363 switch (O(mode
,opt
)) {
365 case O(GROW
,ChunkSize
):
367 case O(CREATE
,ChunkSize
):
368 case O(BUILD
,'c'): /* chunk or rounding */
369 case O(BUILD
,ChunkSize
): /* chunk or rounding */
371 pr_err("chunk/rounding may only be specified once. "
372 "Second value is %s.\n", optarg
);
375 s
.chunk
= parse_size(optarg
);
376 if (s
.chunk
== INVALID_SECTORS
||
377 s
.chunk
< 8 || (s
.chunk
&1)) {
378 pr_err("invalid chunk/rounding value: %s\n",
382 /* Convert sectors to K */
386 case O(INCREMENTAL
, 'e'):
388 case O(ASSEMBLE
,'e'):
389 case O(MISC
,'e'): /* set metadata (superblock) information */
391 pr_err("metadata information already given\n");
394 for(i
=0; !ss
&& superlist
[i
]; i
++)
395 ss
= superlist
[i
]->match_metadata_desc(optarg
);
398 pr_err("unrecognised metadata identifier: %s\n", optarg
);
404 case O(MANAGE
,WriteMostly
):
406 case O(BUILD
,WriteMostly
):
408 case O(CREATE
,WriteMostly
):
409 /* set write-mostly for following devices */
414 /* clear write-mostly for following devices */
421 case O(BUILD
,'z'): /* size */
423 pr_err("size may only be specified once. "
424 "Second value is %s.\n", optarg
);
427 if (strcmp(optarg
, "max")==0)
430 s
.size
= parse_size(optarg
);
431 if (s
.size
== INVALID_SECTORS
||
433 pr_err("invalid size: %s\n",
437 /* convert sectors to K */
442 case O(GROW
,'Z'): /* array size */
443 if (array_size
> 0) {
444 pr_err("array-size may only be specified once. "
445 "Second value is %s.\n", optarg
);
448 if (strcmp(optarg
, "max") == 0)
449 array_size
= MAX_SIZE
;
451 array_size
= parse_size(optarg
);
452 if (array_size
== 0 ||
453 array_size
== INVALID_SECTORS
) {
454 pr_err("invalid array size: %s\n",
461 case O(CREATE
,DataOffset
):
462 case O(GROW
,DataOffset
):
463 if (data_offset
!= INVALID_SECTORS
) {
464 fprintf(stderr
, Name
": data-offset may only be specified one. "
465 "Second value is %s.\n", optarg
);
468 if (mode
== CREATE
&&
469 strcmp(optarg
, "variable") == 0)
470 data_offset
= VARIABLE_OFFSET
;
472 data_offset
= parse_size(optarg
);
473 if (data_offset
== INVALID_SECTORS
) {
474 fprintf(stderr
, Name
": invalid data-offset: %s\n",
482 case O(BUILD
,'l'): /* set raid level*/
483 if (s
.level
!= UnSet
) {
484 pr_err("raid level may only be set once. "
485 "Second value is %s.\n", optarg
);
488 s
.level
= map_name(pers
, optarg
);
489 if (s
.level
== UnSet
) {
490 pr_err("invalid raid level: %s\n",
494 if (s
.level
!= 0 && s
.level
!= LEVEL_LINEAR
&& s
.level
!= 1 &&
495 s
.level
!= LEVEL_MULTIPATH
&& s
.level
!= LEVEL_FAULTY
&&
498 pr_err("Raid level %s not permitted with --build.\n",
502 if (s
.sparedisks
> 0 && s
.level
< 1 && s
.level
>= -1) {
503 pr_err("raid level %s is incompatible with spare-devices setting.\n",
507 ident
.level
= s
.level
;
510 case O(GROW
, 'p'): /* new layout */
511 case O(GROW
, Layout
):
513 pr_err("layout may only be sent once. "
514 "Second value was %s\n", optarg
);
517 s
.layout_str
= optarg
;
518 /* 'Grow' will parse the value */
521 case O(CREATE
,'p'): /* raid5 layout */
522 case O(CREATE
,Layout
):
523 case O(BUILD
,'p'): /* faulty layout */
524 case O(BUILD
,Layout
):
525 if (s
.layout
!= UnSet
) {
526 pr_err("layout may only be sent once. "
527 "Second value was %s\n", optarg
);
532 pr_err("layout not meaningful for %s arrays.\n",
533 map_num(pers
, s
.level
));
536 pr_err("raid level must be given before layout.\n");
540 s
.layout
= map_name(r5layout
, optarg
);
541 if (s
.layout
==UnSet
) {
542 pr_err("layout %s not understood for raid5.\n",
548 s
.layout
= map_name(r6layout
, optarg
);
549 if (s
.layout
==UnSet
) {
550 pr_err("layout %s not understood for raid6.\n",
557 s
.layout
= parse_layout_10(optarg
);
559 pr_err("layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg
);
567 s
.layout
= parse_layout_faulty(optarg
);
568 if (s
.layout
== -1) {
569 pr_err("layout %s not understood for faulty.\n",
577 case O(CREATE
,AssumeClean
):
578 case O(BUILD
,AssumeClean
): /* assume clean */
579 case O(GROW
,AssumeClean
):
585 case O(BUILD
,'n'): /* number of raid disks */
587 pr_err("raid-devices set twice: %d and %s\n",
588 s
.raiddisks
, optarg
);
591 s
.raiddisks
= parse_num(optarg
);
592 if (s
.raiddisks
<= 0) {
593 pr_err("invalid number of raid devices: %s\n",
597 ident
.raid_disks
= s
.raiddisks
;
600 case O(CREATE
,'x'): /* number of spare (eXtra) disks */
602 pr_err("spare-devices set twice: %d and %s\n",
603 s
.sparedisks
, optarg
);
606 if (s
.level
!= UnSet
&& s
.level
<= 0 && s
.level
>= -1) {
607 pr_err("spare-devices setting is incompatible with raid level %d\n",
611 s
.sparedisks
= parse_num(optarg
);
612 if (s
.sparedisks
< 0) {
613 pr_err("invalid number of spare-devices: %s\n",
623 case O(INCREMENTAL
,'a'):
624 case O(INCREMENTAL
,Auto
):
625 case O(ASSEMBLE
,'a'):
626 case O(ASSEMBLE
,Auto
): /* auto-creation of device node */
627 c
.autof
= parse_auto(optarg
, "--auto flag", 0);
630 case O(CREATE
,Symlinks
):
631 case O(BUILD
,Symlinks
):
632 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
636 case O(BUILD
,'f'): /* force honouring '-n 1' */
637 case O(BUILD
,Force
): /* force honouring '-n 1' */
638 case O(GROW
,'f'): /* ditto */
639 case O(GROW
,Force
): /* ditto */
640 case O(CREATE
,'f'): /* force honouring of device list */
641 case O(CREATE
,Force
): /* force honouring of device list */
642 case O(ASSEMBLE
,'f'): /* force assembly */
643 case O(ASSEMBLE
,Force
): /* force assembly */
644 case O(MISC
,'f'): /* force zero */
645 case O(MISC
,Force
): /* force zero */
646 case O(MANAGE
,Force
): /* add device which is too large */
649 /* now for the Assemble options */
650 case O(ASSEMBLE
, FreezeReshape
): /* Freeze reshape during
652 case O(INCREMENTAL
, FreezeReshape
):
653 c
.freeze_reshape
= 1;
655 case O(CREATE
,'u'): /* uuid of array */
656 case O(ASSEMBLE
,'u'): /* uuid of array */
657 if (ident
.uuid_set
) {
658 pr_err("uuid cannot be set twice. "
659 "Second value %s.\n", optarg
);
662 if (parse_uuid(optarg
, ident
.uuid
))
665 pr_err("Bad uuid: %s\n", optarg
);
671 case O(ASSEMBLE
,'N'):
674 pr_err("name cannot be set twice. "
675 "Second value %s.\n", optarg
);
678 if (mode
== MISC
&& !c
.subarray
) {
679 pr_err("-N/--name only valid with --update-subarray in misc mode\n");
682 if (strlen(optarg
) > 32) {
683 pr_err("name '%s' is too long, 32 chars max.\n",
687 strcpy(ident
.name
, optarg
);
690 case O(ASSEMBLE
,'m'): /* super-minor for array */
691 case O(ASSEMBLE
,SuperMinor
):
692 if (ident
.super_minor
!= UnSet
) {
693 pr_err("super-minor cannot be set twice. "
694 "Second value: %s.\n", optarg
);
697 if (strcmp(optarg
, "dev")==0)
698 ident
.super_minor
= -2;
700 ident
.super_minor
= parse_num(optarg
);
701 if (ident
.super_minor
< 0) {
702 pr_err("Bad super-minor number: %s.\n", optarg
);
708 case O(ASSEMBLE
,'o'):
714 case O(ASSEMBLE
,'U'): /* update the superblock */
717 pr_err("Can only update one aspect"
718 " of superblock, both %s and %s given.\n",
722 if (mode
== MISC
&& !c
.subarray
) {
723 pr_err("Only subarrays can be"
724 " updated in misc mode\n");
728 if (strcmp(c
.update
, "sparc2.2")==0)
730 if (strcmp(c
.update
, "super-minor") == 0)
732 if (strcmp(c
.update
, "summaries")==0)
734 if (strcmp(c
.update
, "resync")==0)
736 if (strcmp(c
.update
, "uuid")==0)
738 if (strcmp(c
.update
, "name")==0)
740 if (strcmp(c
.update
, "homehost")==0)
742 if (strcmp(c
.update
, "devicesize")==0)
744 if (strcmp(c
.update
, "no-bitmap")==0)
746 if (strcmp(c
.update
, "bbl") == 0)
748 if (strcmp(c
.update
, "no-bbl") == 0)
750 if (strcmp(c
.update
, "byteorder")==0) {
752 pr_err("must not set metadata"
753 " type with --update=byteorder.\n");
756 for(i
=0; !ss
&& superlist
[i
]; i
++)
757 ss
= superlist
[i
]->match_metadata_desc(
760 pr_err("INTERNAL ERROR"
761 " cannot find 0.swap\n");
767 if (strcmp(c
.update
,"?") == 0 ||
768 strcmp(c
.update
, "help") == 0) {
770 fprintf(outf
, Name
": ");
774 Name
": '--update=%s' is invalid. ",
777 fprintf(outf
, "Valid --update options are:\n"
778 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
779 " 'summaries', 'homehost', 'byteorder', 'devicesize',\n"
781 exit(outf
== stdout
? 0 : 2);
784 /* update=devicesize is allowed with --re-add */
785 if (devmode
!= 'A') {
786 pr_err("--update in Manage mode only"
787 " allowed with --re-add.\n");
791 pr_err("Can only update one aspect"
792 " of superblock, both %s and %s given.\n",
797 if (strcmp(c
.update
, "devicesize") != 0 &&
798 strcmp(c
.update
, "bbl") != 0 &&
799 strcmp(c
.update
, "no-bbl") != 0) {
800 pr_err("only 'devicesize', 'bbl' and 'no-bbl' can be"
801 " updated with --re-add\n");
806 case O(INCREMENTAL
,NoDegraded
):
807 pr_err("--no-degraded is deprecated in Incremental mode\n");
808 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
809 c
.runstop
= -1; /* --stop isn't allowed for --assemble,
810 * so we overload slightly */
813 case O(ASSEMBLE
,'c'):
814 case O(ASSEMBLE
,ConfigFile
):
815 case O(INCREMENTAL
, 'c'):
816 case O(INCREMENTAL
, ConfigFile
):
818 case O(MISC
, ConfigFile
):
820 case O(MONITOR
,ConfigFile
):
822 pr_err("configfile cannot be set twice. "
823 "Second value is %s.\n", optarg
);
827 set_conffile(configfile
);
828 /* FIXME possibly check that config file exists. Even parse it */
830 case O(ASSEMBLE
,'s'): /* scan */
833 case O(INCREMENTAL
,'s'):
837 case O(MONITOR
,'m'): /* mail address */
838 case O(MONITOR
,EMail
):
840 pr_err("only specify one mailaddress. %s ignored.\n",
846 case O(MONITOR
,'p'): /* alert program */
847 case O(MONITOR
,ProgramOpt
): /* alert program */
849 pr_err("only specify one alter program. %s ignored.\n",
855 case O(MONITOR
,'r'): /* rebuild increments */
856 case O(MONITOR
,Increment
):
857 increments
= atoi(optarg
);
858 if (increments
> 99 || increments
< 1) {
859 pr_err("please specify positive integer between 1 and 99 as rebuild increments.\n");
864 case O(MONITOR
,'d'): /* delay in seconds */
866 case O(BUILD
,'d'): /* delay for bitmap updates */
869 pr_err("only specify delay once. %s ignored.\n",
872 c
.delay
= parse_num(optarg
);
874 pr_err("invalid delay: %s\n",
880 case O(MONITOR
,'f'): /* daemonise */
881 case O(MONITOR
,Fork
):
884 case O(MONITOR
,'i'): /* pid */
886 pr_err("only specify one pid file. %s ignored.\n",
891 case O(MONITOR
,'1'): /* oneshot */
895 case O(MONITOR
,'t'): /* test */
898 case O(MONITOR
,'y'): /* log messages to syslog */
899 openlog("mdadm", LOG_PID
, SYSLOG_FACILITY
);
902 case O(MONITOR
, NoSharing
):
906 /* now the general management options. Some are applicable
907 * to other modes. None have arguments.
912 case O(MANAGE
,Add
): /* add a drive */
915 case O(MANAGE
,ReAdd
):
918 case O(MANAGE
,'r'): /* remove a drive */
919 case O(MANAGE
,Remove
):
922 case O(MANAGE
,'f'): /* set faulty */
924 case O(INCREMENTAL
,'f'):
925 case O(INCREMENTAL
,Remove
):
926 case O(INCREMENTAL
,Fail
): /* r for incremental is taken, use f
927 * even though we will both fail and
928 * remove the device */
931 case O(INCREMENTAL
,'R'):
933 case O(ASSEMBLE
,'R'):
935 case O(CREATE
,'R'): /* Run the array */
937 pr_err("Cannot both Stop and Run an array\n");
944 pr_err("Cannot both Run and Stop an array\n");
956 case O(MISC
,KillOpt
):
963 case O(MISC
, WaitOpt
):
964 case O(MISC
, Waitclean
):
965 case O(MISC
, DetailPlatform
):
966 case O(MISC
, KillSubarray
):
967 case O(MISC
, UpdateSubarray
):
968 if (opt
== KillSubarray
|| opt
== UpdateSubarray
) {
970 pr_err("subarray can only"
971 " be specified once\n");
976 if (devmode
&& devmode
!= opt
&&
977 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
978 pr_err("--examine/-E cannot be given with ");
979 if (devmode
== 'E') {
980 if (option_index
>= 0)
981 fprintf(stderr
, "--%s\n",
982 long_options
[option_index
].name
);
984 fprintf(stderr
, "-%c\n", opt
);
985 } else if (isalpha(devmode
))
986 fprintf(stderr
, "-%c\n", devmode
);
988 fprintf(stderr
, "previous option\n");
993 case O(MISC
, UdevRules
):
994 if (devmode
&& devmode
!= opt
) {
995 pr_err("--udev-rules must"
996 " be the only option.\n");
999 pr_err("only specify one udev "
1000 "rule filename. %s ignored.\n",
1003 udev_filename
= optarg
;
1011 case O(MISC
, Sparc22
):
1012 if (devmode
!= 'E') {
1013 pr_err("--sparc2.2 only allowed with --examine\n");
1019 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
1020 case O(ASSEMBLE
,Bitmap
):
1022 pr_err("bitmap file needed with -b in --assemble mode\n");
1025 if (strcmp(optarg
, "internal")==0) {
1026 pr_err("there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
1029 bitmap_fd
= open(optarg
, O_RDWR
);
1030 if (!*optarg
|| bitmap_fd
< 0) {
1031 pr_err("cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
1034 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
1037 case O(ASSEMBLE
, BackupFile
):
1038 case O(GROW
, BackupFile
):
1039 /* Specify a file into which grow might place a backup,
1040 * or from which assemble might recover a backup
1042 if (c
.backup_file
) {
1043 pr_err("backup file already specified, rejecting %s\n", optarg
);
1046 c
.backup_file
= optarg
;
1049 case O(GROW
, Continue
):
1050 /* Continue interrupted grow
1054 case O(ASSEMBLE
, InvalidBackup
):
1055 /* Acknowledge that the backupfile is invalid, but ask
1056 * to continue anyway
1058 c
.invalid_backup
= 1;
1062 case O(BUILD
,Bitmap
):
1064 case O(CREATE
,Bitmap
): /* here we create the bitmap */
1065 if (strcmp(optarg
, "none") == 0) {
1066 pr_err("'--bitmap none' only"
1067 " supported for --grow\n");
1072 case O(GROW
,Bitmap
):
1073 if (strcmp(optarg
, "internal")== 0 ||
1074 strcmp(optarg
, "none")== 0 ||
1075 strchr(optarg
, '/') != NULL
) {
1076 s
.bitmap_file
= optarg
;
1080 pr_err("bitmap file must contain a '/', or be 'internal', or 'none'\n"
1081 " not '%s'\n", optarg
);
1084 case O(GROW
,BitmapChunk
):
1085 case O(BUILD
,BitmapChunk
):
1086 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
1087 s
.bitmap_chunk
= parse_size(optarg
);
1088 if (s
.bitmap_chunk
== 0 ||
1089 s
.bitmap_chunk
== INVALID_SECTORS
||
1090 s
.bitmap_chunk
& (s
.bitmap_chunk
- 1)) {
1091 pr_err("invalid bitmap chunksize: %s\n",
1095 s
.bitmap_chunk
= s
.bitmap_chunk
* 512;
1098 case O(GROW
, WriteBehind
):
1099 case O(BUILD
, WriteBehind
):
1100 case O(CREATE
, WriteBehind
): /* write-behind mode */
1101 s
.write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
1103 s
.write_behind
= parse_num(optarg
);
1104 if (s
.write_behind
< 0 ||
1105 s
.write_behind
> 16383) {
1106 pr_err("Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
1112 case O(INCREMENTAL
, 'r'):
1113 case O(INCREMENTAL
, RebuildMapOpt
):
1116 case O(INCREMENTAL
, IncrementalPath
):
1117 remove_path
= optarg
;
1120 /* We have now processed all the valid options. Anything else is
1123 if (option_index
> 0)
1124 pr_err(":option --%s not valid in %s mode\n",
1125 long_options
[option_index
].name
,
1126 map_num(modes
, mode
));
1128 pr_err("option -%c not valid in %s mode\n",
1129 opt
, map_num(modes
, mode
));
1136 if (print_help
== 2)
1137 help_text
= OptionHelp
;
1139 help_text
= mode_help
[mode
];
1140 if (help_text
== NULL
)
1142 fputs(help_text
,stdout
);
1146 if (!mode
&& devs_found
) {
1149 if (devlist
->disposition
== 0)
1150 devlist
->disposition
= devmode
;
1153 fputs(Usage
, stderr
);
1158 struct createinfo
*ci
= conf_get_create_info();
1160 if (strcasecmp(symlinks
, "yes") == 0)
1162 else if (strcasecmp(symlinks
, "no") == 0)
1165 pr_err("option --symlinks must be 'no' or 'yes'\n");
1169 /* Ok, got the option parsing out of the way
1170 * hopefully it's mostly right but there might be some stuff
1173 * That is mosty checked in the per-mode stuff but...
1175 * For @,B,C and A without -s, the first device listed must be
1176 * an md device. We check that here and open it.
1179 if (mode
== MANAGE
|| mode
== BUILD
|| mode
== CREATE
1181 || (mode
== ASSEMBLE
&& ! c
.scan
)) {
1182 if (devs_found
< 1) {
1183 pr_err("an md device must be given in this mode\n");
1186 if ((int)ident
.super_minor
== -2 && c
.autof
) {
1187 pr_err("--super-minor=dev is incompatible with --auto\n");
1190 if (mode
== MANAGE
|| mode
== GROW
) {
1191 mdfd
= open_mddev(devlist
->devname
, 1);
1195 /* non-existent device is OK */
1196 mdfd
= open_mddev(devlist
->devname
, 0);
1198 pr_err("device %s exists but is not an "
1199 "md array.\n", devlist
->devname
);
1202 if ((int)ident
.super_minor
== -2) {
1205 pr_err("--super-minor=dev given, and "
1206 "listed device %s doesn't exist.\n",
1211 ident
.super_minor
= minor(stb
.st_rdev
);
1213 if (mdfd
>= 0 && mode
!= MANAGE
&& mode
!= GROW
) {
1214 /* We don't really want this open yet, we just might
1215 * have wanted to check some things
1223 if (s
.raiddisks
== 1 && !c
.force
&& s
.level
!= LEVEL_FAULTY
) {
1224 pr_err("'1' is an unusual number of drives for an array, so it is probably\n"
1225 " a mistake. If you really mean it you will need to specify --force before\n"
1226 " setting the number of drives.\n");
1231 if (c
.homehost
== NULL
)
1232 c
.homehost
= conf_get_homehost(&c
.require_homehost
);
1233 if (c
.homehost
== NULL
|| strcasecmp(c
.homehost
, "<system>")==0) {
1234 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
1235 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
1236 c
.homehost
= sys_hostname
;
1239 if (c
.homehost
&& (!c
.homehost
[0] || strcasecmp(c
.homehost
, "<none>") == 0)) {
1241 c
.require_homehost
= 0;
1244 if ((mode
== MISC
&& devmode
== 'E')
1245 || (mode
== MONITOR
&& spare_sharing
== 0))
1246 /* Anyone may try this */;
1247 else if (geteuid() != 0) {
1248 pr_err("must be super-user to perform this action\n");
1252 ident
.autof
= c
.autof
;
1254 if (c
.scan
&& c
.verbose
< 2)
1255 /* --scan implied --brief unless -vv */
1261 /* readonly, add/remove, readwrite, runstop */
1263 rv
= Manage_ro(devlist
->devname
, mdfd
, c
.readonly
);
1264 if (!rv
&& devs_found
>1)
1265 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
1266 devlist
->next
, c
.verbose
, c
.test
,
1268 if (!rv
&& c
.readonly
< 0)
1269 rv
= Manage_ro(devlist
->devname
, mdfd
, c
.readonly
);
1270 if (!rv
&& c
.runstop
)
1271 rv
= Manage_runstop(devlist
->devname
, mdfd
, c
.runstop
, c
.verbose
, 0);
1274 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1275 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !c
.scan
) {
1276 /* Only a device has been given, so get details from config file */
1277 struct mddev_ident
*array_ident
= conf_get_ident(devlist
->devname
);
1278 if (array_ident
== NULL
) {
1279 pr_err("%s not identified in config file.\n",
1285 if (array_ident
->autof
== 0)
1286 array_ident
->autof
= c
.autof
;
1287 rv
|= Assemble(ss
, devlist
->devname
, array_ident
,
1291 rv
= Assemble(ss
, devlist
->devname
, &ident
,
1293 else if (devs_found
> 0) {
1294 if (c
.update
&& devs_found
> 1) {
1295 pr_err("can only update a single array at a time\n");
1298 if (c
.backup_file
&& devs_found
> 1) {
1299 pr_err("can only assemble a single array when providing a backup file.\n");
1302 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1303 struct mddev_ident
*array_ident
= conf_get_ident(dv
->devname
);
1304 if (array_ident
== NULL
) {
1305 pr_err("%s not identified in config file.\n",
1310 if (array_ident
->autof
== 0)
1311 array_ident
->autof
= c
.autof
;
1312 rv
|= Assemble(ss
, dv
->devname
, array_ident
,
1317 pr_err("--update not meaningful with a --scan assembly.\n");
1320 if (c
.backup_file
) {
1321 pr_err("--backup_file not meaningful with a --scan assembly.\n");
1324 rv
= scan_assemble(ss
, &c
, &ident
);
1330 c
.delay
= DEFAULT_BITMAP_DELAY
;
1331 if (s
.write_behind
&& !s
.bitmap_file
) {
1332 pr_err("write-behind mode requires a bitmap.\n");
1336 if (s
.raiddisks
== 0) {
1337 pr_err("no raid-devices specified.\n");
1342 if (s
.bitmap_file
) {
1343 if (strcmp(s
.bitmap_file
, "internal")==0) {
1344 pr_err("'internal' bitmaps not supported with --build\n");
1349 rv
= Build(devlist
->devname
, devlist
->next
, &s
, &c
);
1353 c
.delay
= DEFAULT_BITMAP_DELAY
;
1354 if (s
.write_behind
&& !s
.bitmap_file
) {
1355 pr_err("write-behind mode requires a bitmap.\n");
1359 if (s
.raiddisks
== 0) {
1360 pr_err("no raid-devices specified.\n");
1365 rv
= Create(ss
, devlist
->devname
,
1366 ident
.name
, ident
.uuid_set
? ident
.uuid
: NULL
,
1367 devs_found
-1, devlist
->next
,
1368 &s
, &c
, data_offset
);
1371 if (devmode
== 'E') {
1372 if (devlist
== NULL
&& !c
.scan
) {
1373 pr_err("No devices to examine\n");
1376 if (devlist
== NULL
)
1377 devlist
= conf_get_devs();
1378 if (devlist
== NULL
) {
1379 pr_err("No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1382 rv
= Examine(devlist
, &c
, ss
);
1383 } else if (devmode
== DetailPlatform
) {
1384 rv
= Detail_Platform(ss
? ss
->ss
: NULL
, ss
? c
.scan
: 1,
1385 c
.verbose
, c
.export
,
1386 devlist
? devlist
->devname
: NULL
);
1387 } else if (devlist
== NULL
) {
1388 if (devmode
== 'S' && c
.scan
)
1389 rv
= stop_scan(c
.verbose
);
1390 else if ((devmode
== 'D' || devmode
== Waitclean
) && c
.scan
)
1391 rv
= misc_scan(devmode
, &c
);
1392 else if (devmode
== UdevRules
)
1393 rv
= Write_rules(udev_filename
);
1395 pr_err("No devices given.\n");
1399 rv
= misc_list(devlist
, &ident
, ss
, &c
);
1402 if (!devlist
&& !c
.scan
) {
1403 pr_err("Cannot monitor: need --scan or at least one device\n");
1407 if (pidfile
&& !daemonise
) {
1408 pr_err("Cannot write a pid file when not in daemon mode\n");
1413 if (get_linux_version() > 2006016)
1414 /* mdstat responds to poll */
1421 rv
= Monitor(devlist
, mailaddr
, program
,
1422 &c
, daemonise
, oneshot
,
1423 dosyslog
, pidfile
, increments
,
1428 if (array_size
> 0) {
1429 /* alway impose array size first, independent of
1431 * Do not allow level or raid_disks changes at the
1432 * same time as that can be irreversibly destructive.
1436 if (s
.raiddisks
|| s
.level
!= UnSet
) {
1437 pr_err("cannot change array size in same operation "
1438 "as changing raiddisks or level.\n"
1439 " Change size first, then check that data is still intact.\n");
1443 sysfs_init(&sra
, mdfd
, 0);
1444 if (array_size
== MAX_SIZE
)
1445 err
= sysfs_set_str(&sra
, NULL
, "array_size", "default");
1447 err
= sysfs_set_num(&sra
, NULL
, "array_size", array_size
/ 2);
1450 pr_err("--array-size setting"
1451 " is too large.\n");
1453 pr_err("current kernel does"
1454 " not support setting --array-size\n");
1459 if (devs_found
> 1 && s
.raiddisks
== 0) {
1461 if (s
.size
> 0 || s
.chunk
|| s
.layout_str
!= NULL
|| s
.bitmap_file
) {
1462 pr_err("--add cannot be used with "
1463 "other geometry changes in --grow mode\n");
1467 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1468 rv
= Grow_Add_device(devlist
->devname
, mdfd
,
1473 } else if (s
.bitmap_file
) {
1474 if (s
.size
> 0 || s
.raiddisks
|| s
.chunk
||
1475 s
.layout_str
!= NULL
|| devs_found
> 1) {
1476 pr_err("--bitmap changes cannot be "
1477 "used with other geometry changes "
1478 "in --grow mode\n");
1483 c
.delay
= DEFAULT_BITMAP_DELAY
;
1484 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, &c
, &s
);
1485 } else if (grow_continue
)
1486 rv
= Grow_continue_command(devlist
->devname
,
1487 mdfd
, c
.backup_file
,
1489 else if (s
.size
> 0 || s
.raiddisks
|| s
.layout_str
!= NULL
1490 || s
.chunk
!= 0 || s
.level
!= UnSet
) {
1491 rv
= Grow_reshape(devlist
->devname
, mdfd
,
1493 data_offset
, &c
, &s
);
1494 } else if (array_size
== 0)
1495 pr_err("no changes to --grow\n");
1502 if (c
.runstop
<= 0) {
1503 pr_err("--incremental --scan meaningless without --run.\n");
1506 if (devmode
== 'f') {
1507 pr_err("--incremental --scan --fail not supported.\n");
1510 rv
= IncrementalScan(c
.verbose
);
1513 if (!rebuild_map
&& !c
.scan
) {
1514 pr_err("--incremental requires a device.\n");
1519 if (devlist
->next
) {
1520 pr_err("--incremental can only handle one device.\n");
1525 rv
= IncrementalRemove(devlist
->devname
, remove_path
,
1528 rv
= Incremental(devlist
->devname
, &c
, ss
);
1537 static int scan_assemble(struct supertype
*ss
,
1539 struct mddev_ident
*ident
)
1541 struct mddev_ident
*a
, *array_list
= conf_get_ident(NULL
);
1542 struct mddev_dev
*devlist
= conf_get_devs();
1543 struct map_ent
*map
= NULL
;
1546 int failures
, successes
;
1548 if (conf_verify_devnames(array_list
)) {
1549 pr_err("Duplicate MD device names in "
1550 "conf file were found.\n");
1553 if (devlist
== NULL
) {
1554 pr_err("No devices listed in conf file were found.\n");
1557 for (a
= array_list
; a
; a
= a
->next
) {
1560 a
->autof
= c
->autof
;
1563 pr_err("%s: failed to get "
1564 "exclusive lock on mapfile\n",
1570 for (a
= array_list
; a
; a
= a
->next
) {
1575 strcasecmp(a
->devname
, "<ignore>") == 0)
1578 r
= Assemble(ss
, a
->devname
,
1588 } while (failures
&& successes
);
1589 if (c
->homehost
&& cnt
== 0) {
1590 /* Maybe we can auto-assemble something.
1591 * Repeatedly call Assemble in auto-assemble mode
1596 ident
->autof
= c
->autof
;
1598 struct mddev_dev
*devlist
= conf_get_devs();
1601 rv2
= Assemble(ss
, NULL
,
1609 /* Incase there are stacked devices, we need to go around again */
1611 if (cnt
== 0 && rv
== 0) {
1612 pr_err("No arrays found in config file or automatically\n");
1616 } else if (cnt
== 0 && rv
== 0) {
1617 pr_err("No arrays found in config file\n");
1624 static int misc_scan(char devmode
, struct context
*c
)
1626 /* apply --detail or --wait-clean to
1627 * all devices in /proc/mdstat
1629 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1630 struct mdstat_ent
*e
;
1631 struct map_ent
*map
= NULL
;
1635 for (members
= 0; members
<= 1; members
++) {
1636 for (e
=ms
; e
; e
=e
->next
) {
1639 int member
= e
->metadata_version
&&
1640 strncmp(e
->metadata_version
,
1641 "external:/", 10) == 0;
1642 if (members
!= member
)
1644 me
= map_by_devnum(&map
, e
->devnum
);
1646 && strcmp(me
->path
, "/unknown") != 0)
1649 name
= get_md_name(e
->devnum
);
1652 pr_err("cannot find device file for %s\n",
1657 rv
|= Detail(name
, c
);
1659 rv
|= WaitClean(name
, -1, c
->verbose
);
1667 static int stop_scan(int verbose
)
1669 /* apply --stop to all devices in /proc/mdstat */
1670 /* Due to possible stacking of devices, repeat until
1671 * nothing more can be stopped
1673 int progress
=1, err
;
1677 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1678 struct mdstat_ent
*e
;
1680 if (!progress
) last
= 1;
1681 progress
= 0; err
= 0;
1682 for (e
=ms
; e
; e
=e
->next
) {
1683 char *name
= get_md_name(e
->devnum
);
1687 pr_err("cannot find device file for %s\n",
1691 mdfd
= open_mddev(name
, 1);
1693 if (Manage_runstop(name
, mdfd
, -1, verbose
, !last
))
1703 } while (!last
&& err
);
1709 static int misc_list(struct mddev_dev
*devlist
,
1710 struct mddev_ident
*ident
,
1711 struct supertype
*ss
, struct context
*c
)
1713 struct mddev_dev
*dv
;
1716 for (dv
=devlist
; dv
; dv
=dv
->next
) {
1719 switch(dv
->disposition
) {
1721 rv
|= Detail(dv
->devname
, c
);
1723 case KillOpt
: /* Zero superblock */
1725 rv
|= Kill(dv
->devname
, ss
, c
->force
, c
->verbose
,0);
1729 rv
|= Kill(dv
->devname
, NULL
, c
->force
, v
, 0);
1736 rv
|= Query(dv
->devname
); continue;
1738 rv
|= ExamineBitmap(dv
->devname
, c
->brief
, ss
); continue;
1741 rv
|= Wait(dv
->devname
); continue;
1743 rv
|= WaitClean(dv
->devname
, -1, c
->verbose
); continue;
1745 rv
|= Kill_subarray(dv
->devname
, c
->subarray
, c
->verbose
);
1747 case UpdateSubarray
:
1748 if (c
->update
== NULL
) {
1749 pr_err("-U/--update must be specified with --update-subarray\n");
1753 rv
|= Update_subarray(dv
->devname
, c
->subarray
,
1754 c
->update
, ident
, c
->verbose
);
1757 mdfd
= open_mddev(dv
->devname
, 1);
1759 switch(dv
->disposition
) {
1761 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1, c
->verbose
, 0); break;
1763 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1, c
->verbose
, 0); break;
1765 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1767 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;