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
;
202 case ReAdd
: /* re-add */
205 shortopt
= short_bitmap_options
;
209 case 'A': newmode
= ASSEMBLE
;
210 shortopt
= short_bitmap_auto_options
;
212 case 'B': newmode
= BUILD
;
213 shortopt
= short_bitmap_auto_options
;
215 case 'C': newmode
= CREATE
;
216 shortopt
= short_bitmap_auto_options
;
218 case 'F': newmode
= MONITOR
;
220 case 'G': newmode
= GROW
;
221 shortopt
= short_bitmap_options
;
223 case 'I': newmode
= INCREMENTAL
;
224 shortopt
= short_bitmap_auto_options
;
227 newmode
= AUTODETECT
;
258 if (mode
&& newmode
== mode
) {
259 /* everybody happy ! */
260 } else if (mode
&& newmode
!= mode
) {
263 if (option_index
>= 0)
264 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
266 fprintf(stderr
, "-%c", opt
);
267 fprintf(stderr
, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
268 map_num(modes
, newmode
),
269 map_num(modes
, mode
));
271 } else if (!mode
&& newmode
) {
273 if (mode
== MISC
&& devs_found
) {
274 pr_err("No action given for %s in --misc mode\n",
276 fprintf(stderr
," Action options must come before device names\n");
280 /* special case of -c --help */
281 if ((opt
== 'c' || opt
== ConfigFile
) &&
282 ( strncmp(optarg
, "--h", 3)==0 ||
283 strncmp(optarg
, "-h", 2)==0)) {
284 fputs(Help_config
, stdout
);
288 /* If first option is a device, don't force the mode yet */
290 if (devs_found
== 0) {
291 dv
= xmalloc(sizeof(*dv
));
292 dv
->devname
= optarg
;
293 dv
->disposition
= devmode
;
294 dv
->writemostly
= writemostly
;
298 devlistend
= &dv
->next
;
303 /* No mode yet, and this is the second device ... */
304 pr_err("An option must be given to set the mode before a second device\n"
305 " (%s) is listed\n", optarg
);
308 if (option_index
>= 0)
309 pr_err("--%s", long_options
[option_index
].name
);
312 fprintf(stderr
, " does not set the mode, and so cannot be the first option.\n");
316 /* if we just set the mode, then done */
330 /* an undecorated option - must be a device name.
333 if (devs_found
> 0 && devmode
== DetailPlatform
) {
334 pr_err("controller may only be specified once. %s ignored\n",
339 if (devs_found
> 0 && mode
== MANAGE
&& !devmode
) {
340 pr_err("Must give one of -a/-r/-f"
341 " for subsequent devices at %s\n", optarg
);
344 if (devs_found
> 0 && mode
== GROW
&& !devmode
) {
345 pr_err("Must give -a/--add for"
346 " devices to add: %s\n", optarg
);
349 dv
= xmalloc(sizeof(*dv
));
350 dv
->devname
= optarg
;
351 dv
->disposition
= devmode
;
352 dv
->writemostly
= writemostly
;
356 devlistend
= &dv
->next
;
362 /* We've got a mode, and opt is now something else which
363 * could depend on the mode */
364 #define O(a,b) ((a<<16)|b)
365 switch (O(mode
,opt
)) {
367 case O(GROW
,ChunkSize
):
369 case O(CREATE
,ChunkSize
):
370 case O(BUILD
,'c'): /* chunk or rounding */
371 case O(BUILD
,ChunkSize
): /* chunk or rounding */
373 pr_err("chunk/rounding may only be specified once. "
374 "Second value is %s.\n", optarg
);
377 s
.chunk
= parse_size(optarg
);
378 if (s
.chunk
== INVALID_SECTORS
||
379 s
.chunk
< 8 || (s
.chunk
&1)) {
380 pr_err("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 pr_err("metadata information already given\n");
396 for(i
=0; !ss
&& superlist
[i
]; i
++)
397 ss
= superlist
[i
]->match_metadata_desc(optarg
);
400 pr_err("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 pr_err("size may only be specified once. "
426 "Second value is %s.\n", optarg
);
429 if (strcmp(optarg
, "max")==0)
432 s
.size
= parse_size(optarg
);
433 if (s
.size
== INVALID_SECTORS
||
435 pr_err("invalid size: %s\n",
439 /* convert sectors to K */
444 case O(GROW
,'Z'): /* array size */
445 if (array_size
> 0) {
446 pr_err("array-size may only be specified once. "
447 "Second value is %s.\n", optarg
);
450 if (strcmp(optarg
, "max") == 0)
451 array_size
= MAX_SIZE
;
453 array_size
= parse_size(optarg
);
454 if (array_size
== 0 ||
455 array_size
== INVALID_SECTORS
) {
456 pr_err("invalid array size: %s\n",
463 case O(CREATE
,DataOffset
):
464 case O(GROW
,DataOffset
):
465 if (data_offset
!= INVALID_SECTORS
) {
466 fprintf(stderr
, Name
": data-offset may only be specified one. "
467 "Second value is %s.\n", optarg
);
470 if (mode
== CREATE
&&
471 strcmp(optarg
, "variable") == 0)
472 data_offset
= VARIABLE_OFFSET
;
474 data_offset
= parse_size(optarg
);
475 if (data_offset
== INVALID_SECTORS
) {
476 fprintf(stderr
, Name
": invalid data-offset: %s\n",
484 case O(BUILD
,'l'): /* set raid level*/
485 if (s
.level
!= UnSet
) {
486 pr_err("raid level may only be set once. "
487 "Second value is %s.\n", optarg
);
490 s
.level
= map_name(pers
, optarg
);
491 if (s
.level
== UnSet
) {
492 pr_err("invalid raid level: %s\n",
496 if (s
.level
!= 0 && s
.level
!= LEVEL_LINEAR
&& s
.level
!= 1 &&
497 s
.level
!= LEVEL_MULTIPATH
&& s
.level
!= LEVEL_FAULTY
&&
500 pr_err("Raid level %s not permitted with --build.\n",
504 if (s
.sparedisks
> 0 && s
.level
< 1 && s
.level
>= -1) {
505 pr_err("raid level %s is incompatible with spare-devices setting.\n",
509 ident
.level
= s
.level
;
512 case O(GROW
, 'p'): /* new layout */
513 case O(GROW
, Layout
):
515 pr_err("layout may only be sent once. "
516 "Second value was %s\n", optarg
);
519 s
.layout_str
= optarg
;
520 /* 'Grow' will parse the value */
523 case O(CREATE
,'p'): /* raid5 layout */
524 case O(CREATE
,Layout
):
525 case O(BUILD
,'p'): /* faulty layout */
526 case O(BUILD
,Layout
):
527 if (s
.layout
!= UnSet
) {
528 pr_err("layout may only be sent once. "
529 "Second value was %s\n", optarg
);
534 pr_err("layout not meaningful for %s arrays.\n",
535 map_num(pers
, s
.level
));
538 pr_err("raid level must be given before layout.\n");
542 s
.layout
= map_name(r5layout
, optarg
);
543 if (s
.layout
==UnSet
) {
544 pr_err("layout %s not understood for raid5.\n",
550 s
.layout
= map_name(r6layout
, optarg
);
551 if (s
.layout
==UnSet
) {
552 pr_err("layout %s not understood for raid6.\n",
559 s
.layout
= parse_layout_10(optarg
);
561 pr_err("layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg
);
569 s
.layout
= parse_layout_faulty(optarg
);
570 if (s
.layout
== -1) {
571 pr_err("layout %s not understood for faulty.\n",
579 case O(CREATE
,AssumeClean
):
580 case O(BUILD
,AssumeClean
): /* assume clean */
581 case O(GROW
,AssumeClean
):
587 case O(BUILD
,'n'): /* number of raid disks */
589 pr_err("raid-devices set twice: %d and %s\n",
590 s
.raiddisks
, optarg
);
593 s
.raiddisks
= parse_num(optarg
);
594 if (s
.raiddisks
<= 0) {
595 pr_err("invalid number of raid devices: %s\n",
599 ident
.raid_disks
= s
.raiddisks
;
602 case O(CREATE
,'x'): /* number of spare (eXtra) disks */
604 pr_err("spare-devices set twice: %d and %s\n",
605 s
.sparedisks
, optarg
);
608 if (s
.level
!= UnSet
&& s
.level
<= 0 && s
.level
>= -1) {
609 pr_err("spare-devices setting is incompatible with raid level %d\n",
613 s
.sparedisks
= parse_num(optarg
);
614 if (s
.sparedisks
< 0) {
615 pr_err("invalid number of spare-devices: %s\n",
625 case O(INCREMENTAL
,'a'):
626 case O(INCREMENTAL
,Auto
):
627 case O(ASSEMBLE
,'a'):
628 case O(ASSEMBLE
,Auto
): /* auto-creation of device node */
629 c
.autof
= parse_auto(optarg
, "--auto flag", 0);
632 case O(CREATE
,Symlinks
):
633 case O(BUILD
,Symlinks
):
634 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
638 case O(BUILD
,'f'): /* force honouring '-n 1' */
639 case O(BUILD
,Force
): /* force honouring '-n 1' */
640 case O(GROW
,'f'): /* ditto */
641 case O(GROW
,Force
): /* ditto */
642 case O(CREATE
,'f'): /* force honouring of device list */
643 case O(CREATE
,Force
): /* force honouring of device list */
644 case O(ASSEMBLE
,'f'): /* force assembly */
645 case O(ASSEMBLE
,Force
): /* force assembly */
646 case O(MISC
,'f'): /* force zero */
647 case O(MISC
,Force
): /* force zero */
648 case O(MANAGE
,Force
): /* add device which is too large */
651 /* now for the Assemble options */
652 case O(ASSEMBLE
, FreezeReshape
): /* Freeze reshape during
654 case O(INCREMENTAL
, FreezeReshape
):
655 c
.freeze_reshape
= 1;
657 case O(CREATE
,'u'): /* uuid of array */
658 case O(ASSEMBLE
,'u'): /* uuid of array */
659 if (ident
.uuid_set
) {
660 pr_err("uuid cannot be set twice. "
661 "Second value %s.\n", optarg
);
664 if (parse_uuid(optarg
, ident
.uuid
))
667 pr_err("Bad uuid: %s\n", optarg
);
673 case O(ASSEMBLE
,'N'):
676 pr_err("name cannot be set twice. "
677 "Second value %s.\n", optarg
);
680 if (mode
== MISC
&& !c
.subarray
) {
681 pr_err("-N/--name only valid with --update-subarray in misc mode\n");
684 if (strlen(optarg
) > 32) {
685 pr_err("name '%s' is too long, 32 chars max.\n",
689 strcpy(ident
.name
, optarg
);
692 case O(ASSEMBLE
,'m'): /* super-minor for array */
693 case O(ASSEMBLE
,SuperMinor
):
694 if (ident
.super_minor
!= UnSet
) {
695 pr_err("super-minor cannot be set twice. "
696 "Second value: %s.\n", optarg
);
699 if (strcmp(optarg
, "dev")==0)
700 ident
.super_minor
= -2;
702 ident
.super_minor
= parse_num(optarg
);
703 if (ident
.super_minor
< 0) {
704 pr_err("Bad super-minor number: %s.\n", optarg
);
710 case O(ASSEMBLE
,'o'):
716 case O(ASSEMBLE
,'U'): /* update the superblock */
719 pr_err("Can only update one aspect"
720 " of superblock, both %s and %s given.\n",
724 if (mode
== MISC
&& !c
.subarray
) {
725 pr_err("Only subarrays can be"
726 " updated in misc mode\n");
730 if (strcmp(c
.update
, "sparc2.2")==0)
732 if (strcmp(c
.update
, "super-minor") == 0)
734 if (strcmp(c
.update
, "summaries")==0)
736 if (strcmp(c
.update
, "resync")==0)
738 if (strcmp(c
.update
, "uuid")==0)
740 if (strcmp(c
.update
, "name")==0)
742 if (strcmp(c
.update
, "homehost")==0)
744 if (strcmp(c
.update
, "devicesize")==0)
746 if (strcmp(c
.update
, "no-bitmap")==0)
748 if (strcmp(c
.update
, "bbl") == 0)
750 if (strcmp(c
.update
, "no-bbl") == 0)
752 if (strcmp(c
.update
, "byteorder")==0) {
754 pr_err("must not set metadata"
755 " type with --update=byteorder.\n");
758 for(i
=0; !ss
&& superlist
[i
]; i
++)
759 ss
= superlist
[i
]->match_metadata_desc(
762 pr_err("INTERNAL ERROR"
763 " cannot find 0.swap\n");
769 if (strcmp(c
.update
,"?") == 0 ||
770 strcmp(c
.update
, "help") == 0) {
772 fprintf(outf
, Name
": ");
776 Name
": '--update=%s' is invalid. ",
779 fprintf(outf
, "Valid --update options are:\n"
780 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
781 " 'summaries', 'homehost', 'byteorder', 'devicesize',\n"
783 exit(outf
== stdout
? 0 : 2);
786 /* update=devicesize is allowed with --re-add */
787 if (devmode
!= 'A') {
788 pr_err("--update in Manage mode only"
789 " allowed with --re-add.\n");
793 pr_err("Can only update one aspect"
794 " of superblock, both %s and %s given.\n",
799 if (strcmp(c
.update
, "devicesize") != 0 &&
800 strcmp(c
.update
, "bbl") != 0 &&
801 strcmp(c
.update
, "no-bbl") != 0) {
802 pr_err("only 'devicesize', 'bbl' and 'no-bbl' can be"
803 " updated with --re-add\n");
808 case O(INCREMENTAL
,NoDegraded
):
809 pr_err("--no-degraded is deprecated in Incremental mode\n");
810 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
811 c
.runstop
= -1; /* --stop isn't allowed for --assemble,
812 * so we overload slightly */
815 case O(ASSEMBLE
,'c'):
816 case O(ASSEMBLE
,ConfigFile
):
817 case O(INCREMENTAL
, 'c'):
818 case O(INCREMENTAL
, ConfigFile
):
820 case O(MISC
, ConfigFile
):
822 case O(MONITOR
,ConfigFile
):
824 pr_err("configfile cannot be set twice. "
825 "Second value is %s.\n", optarg
);
829 set_conffile(configfile
);
830 /* FIXME possibly check that config file exists. Even parse it */
832 case O(ASSEMBLE
,'s'): /* scan */
835 case O(INCREMENTAL
,'s'):
839 case O(MONITOR
,'m'): /* mail address */
840 case O(MONITOR
,EMail
):
842 pr_err("only specify one mailaddress. %s ignored.\n",
848 case O(MONITOR
,'p'): /* alert program */
849 case O(MONITOR
,ProgramOpt
): /* alert program */
851 pr_err("only specify one alter program. %s ignored.\n",
857 case O(MONITOR
,'r'): /* rebuild increments */
858 case O(MONITOR
,Increment
):
859 increments
= atoi(optarg
);
860 if (increments
> 99 || increments
< 1) {
861 pr_err("please specify positive integer between 1 and 99 as rebuild increments.\n");
866 case O(MONITOR
,'d'): /* delay in seconds */
868 case O(BUILD
,'d'): /* delay for bitmap updates */
871 pr_err("only specify delay once. %s ignored.\n",
874 c
.delay
= parse_num(optarg
);
876 pr_err("invalid delay: %s\n",
882 case O(MONITOR
,'f'): /* daemonise */
883 case O(MONITOR
,Fork
):
886 case O(MONITOR
,'i'): /* pid */
888 pr_err("only specify one pid file. %s ignored.\n",
893 case O(MONITOR
,'1'): /* oneshot */
897 case O(MONITOR
,'t'): /* test */
900 case O(MONITOR
,'y'): /* log messages to syslog */
901 openlog("mdadm", LOG_PID
, SYSLOG_FACILITY
);
904 case O(MONITOR
, NoSharing
):
908 /* now the general management options. Some are applicable
909 * to other modes. None have arguments.
914 case O(MANAGE
,Add
): /* add a drive */
917 case O(MANAGE
,ReAdd
):
920 case O(MANAGE
,'r'): /* remove a drive */
921 case O(MANAGE
,Remove
):
924 case O(MANAGE
,'f'): /* set faulty */
926 case O(INCREMENTAL
,'f'):
927 case O(INCREMENTAL
,Remove
):
928 case O(INCREMENTAL
,Fail
): /* r for incremental is taken, use f
929 * even though we will both fail and
930 * remove the device */
933 case O(MANAGE
,Replace
):
934 /* Mark these devices for replacement */
938 /* These are the replacements to use */
939 if (devmode
!= 'R') {
940 pr_err("--with must follow --replace\n");
945 case O(INCREMENTAL
,'R'):
947 case O(ASSEMBLE
,'R'):
949 case O(CREATE
,'R'): /* Run the array */
951 pr_err("Cannot both Stop and Run an array\n");
958 pr_err("Cannot both Run and Stop an array\n");
970 case O(MISC
,KillOpt
):
977 case O(MISC
, WaitOpt
):
978 case O(MISC
, Waitclean
):
979 case O(MISC
, DetailPlatform
):
980 case O(MISC
, KillSubarray
):
981 case O(MISC
, UpdateSubarray
):
982 if (opt
== KillSubarray
|| opt
== UpdateSubarray
) {
984 pr_err("subarray can only"
985 " be specified once\n");
990 if (devmode
&& devmode
!= opt
&&
991 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
992 pr_err("--examine/-E cannot be given with ");
993 if (devmode
== 'E') {
994 if (option_index
>= 0)
995 fprintf(stderr
, "--%s\n",
996 long_options
[option_index
].name
);
998 fprintf(stderr
, "-%c\n", opt
);
999 } else if (isalpha(devmode
))
1000 fprintf(stderr
, "-%c\n", devmode
);
1002 fprintf(stderr
, "previous option\n");
1007 case O(MISC
, UdevRules
):
1008 if (devmode
&& devmode
!= opt
) {
1009 pr_err("--udev-rules must"
1010 " be the only option.\n");
1013 pr_err("only specify one udev "
1014 "rule filename. %s ignored.\n",
1017 udev_filename
= optarg
;
1025 case O(MISC
, Sparc22
):
1026 if (devmode
!= 'E') {
1027 pr_err("--sparc2.2 only allowed with --examine\n");
1033 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
1034 case O(ASSEMBLE
,Bitmap
):
1036 pr_err("bitmap file needed with -b in --assemble mode\n");
1039 if (strcmp(optarg
, "internal")==0) {
1040 pr_err("there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
1043 bitmap_fd
= open(optarg
, O_RDWR
);
1044 if (!*optarg
|| bitmap_fd
< 0) {
1045 pr_err("cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
1048 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
1051 case O(ASSEMBLE
, BackupFile
):
1052 case O(GROW
, BackupFile
):
1053 /* Specify a file into which grow might place a backup,
1054 * or from which assemble might recover a backup
1056 if (c
.backup_file
) {
1057 pr_err("backup file already specified, rejecting %s\n", optarg
);
1060 c
.backup_file
= optarg
;
1063 case O(GROW
, Continue
):
1064 /* Continue interrupted grow
1068 case O(ASSEMBLE
, InvalidBackup
):
1069 /* Acknowledge that the backupfile is invalid, but ask
1070 * to continue anyway
1072 c
.invalid_backup
= 1;
1076 case O(BUILD
,Bitmap
):
1078 case O(CREATE
,Bitmap
): /* here we create the bitmap */
1079 if (strcmp(optarg
, "none") == 0) {
1080 pr_err("'--bitmap none' only"
1081 " supported for --grow\n");
1086 case O(GROW
,Bitmap
):
1087 if (strcmp(optarg
, "internal")== 0 ||
1088 strcmp(optarg
, "none")== 0 ||
1089 strchr(optarg
, '/') != NULL
) {
1090 s
.bitmap_file
= optarg
;
1094 pr_err("bitmap file must contain a '/', or be 'internal', or 'none'\n"
1095 " not '%s'\n", optarg
);
1098 case O(GROW
,BitmapChunk
):
1099 case O(BUILD
,BitmapChunk
):
1100 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
1101 s
.bitmap_chunk
= parse_size(optarg
);
1102 if (s
.bitmap_chunk
== 0 ||
1103 s
.bitmap_chunk
== INVALID_SECTORS
||
1104 s
.bitmap_chunk
& (s
.bitmap_chunk
- 1)) {
1105 pr_err("invalid bitmap chunksize: %s\n",
1109 s
.bitmap_chunk
= s
.bitmap_chunk
* 512;
1112 case O(GROW
, WriteBehind
):
1113 case O(BUILD
, WriteBehind
):
1114 case O(CREATE
, WriteBehind
): /* write-behind mode */
1115 s
.write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
1117 s
.write_behind
= parse_num(optarg
);
1118 if (s
.write_behind
< 0 ||
1119 s
.write_behind
> 16383) {
1120 pr_err("Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
1126 case O(INCREMENTAL
, 'r'):
1127 case O(INCREMENTAL
, RebuildMapOpt
):
1130 case O(INCREMENTAL
, IncrementalPath
):
1131 remove_path
= optarg
;
1134 /* We have now processed all the valid options. Anything else is
1137 if (option_index
> 0)
1138 pr_err(":option --%s not valid in %s mode\n",
1139 long_options
[option_index
].name
,
1140 map_num(modes
, mode
));
1142 pr_err("option -%c not valid in %s mode\n",
1143 opt
, map_num(modes
, mode
));
1150 if (print_help
== 2)
1151 help_text
= OptionHelp
;
1153 help_text
= mode_help
[mode
];
1154 if (help_text
== NULL
)
1156 fputs(help_text
,stdout
);
1160 if (!mode
&& devs_found
) {
1163 if (devlist
->disposition
== 0)
1164 devlist
->disposition
= devmode
;
1167 fputs(Usage
, stderr
);
1172 struct createinfo
*ci
= conf_get_create_info();
1174 if (strcasecmp(symlinks
, "yes") == 0)
1176 else if (strcasecmp(symlinks
, "no") == 0)
1179 pr_err("option --symlinks must be 'no' or 'yes'\n");
1183 /* Ok, got the option parsing out of the way
1184 * hopefully it's mostly right but there might be some stuff
1187 * That is mosty checked in the per-mode stuff but...
1189 * For @,B,C and A without -s, the first device listed must be
1190 * an md device. We check that here and open it.
1193 if (mode
== MANAGE
|| mode
== BUILD
|| mode
== CREATE
1195 || (mode
== ASSEMBLE
&& ! c
.scan
)) {
1196 if (devs_found
< 1) {
1197 pr_err("an md device must be given in this mode\n");
1200 if ((int)ident
.super_minor
== -2 && c
.autof
) {
1201 pr_err("--super-minor=dev is incompatible with --auto\n");
1204 if (mode
== MANAGE
|| mode
== GROW
) {
1205 mdfd
= open_mddev(devlist
->devname
, 1);
1209 /* non-existent device is OK */
1210 mdfd
= open_mddev(devlist
->devname
, 0);
1212 pr_err("device %s exists but is not an "
1213 "md array.\n", devlist
->devname
);
1216 if ((int)ident
.super_minor
== -2) {
1219 pr_err("--super-minor=dev given, and "
1220 "listed device %s doesn't exist.\n",
1225 ident
.super_minor
= minor(stb
.st_rdev
);
1227 if (mdfd
>= 0 && mode
!= MANAGE
&& mode
!= GROW
) {
1228 /* We don't really want this open yet, we just might
1229 * have wanted to check some things
1237 if (s
.raiddisks
== 1 && !c
.force
&& s
.level
!= LEVEL_FAULTY
) {
1238 pr_err("'1' is an unusual number of drives for an array, so it is probably\n"
1239 " a mistake. If you really mean it you will need to specify --force before\n"
1240 " setting the number of drives.\n");
1245 if (c
.homehost
== NULL
)
1246 c
.homehost
= conf_get_homehost(&c
.require_homehost
);
1247 if (c
.homehost
== NULL
|| strcasecmp(c
.homehost
, "<system>")==0) {
1248 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
1249 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
1250 c
.homehost
= sys_hostname
;
1253 if (c
.homehost
&& (!c
.homehost
[0] || strcasecmp(c
.homehost
, "<none>") == 0)) {
1255 c
.require_homehost
= 0;
1258 if ((mode
== MISC
&& devmode
== 'E')
1259 || (mode
== MONITOR
&& spare_sharing
== 0))
1260 /* Anyone may try this */;
1261 else if (geteuid() != 0) {
1262 pr_err("must be super-user to perform this action\n");
1266 ident
.autof
= c
.autof
;
1268 if (c
.scan
&& c
.verbose
< 2)
1269 /* --scan implied --brief unless -vv */
1275 /* readonly, add/remove, readwrite, runstop */
1277 rv
= Manage_ro(devlist
->devname
, mdfd
, c
.readonly
);
1278 if (!rv
&& devs_found
>1)
1279 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
1280 devlist
->next
, c
.verbose
, c
.test
,
1282 if (!rv
&& c
.readonly
< 0)
1283 rv
= Manage_ro(devlist
->devname
, mdfd
, c
.readonly
);
1284 if (!rv
&& c
.runstop
)
1285 rv
= Manage_runstop(devlist
->devname
, mdfd
, c
.runstop
, c
.verbose
, 0);
1288 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1289 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !c
.scan
) {
1290 /* Only a device has been given, so get details from config file */
1291 struct mddev_ident
*array_ident
= conf_get_ident(devlist
->devname
);
1292 if (array_ident
== NULL
) {
1293 pr_err("%s not identified in config file.\n",
1299 if (array_ident
->autof
== 0)
1300 array_ident
->autof
= c
.autof
;
1301 rv
|= Assemble(ss
, devlist
->devname
, array_ident
,
1305 rv
= Assemble(ss
, devlist
->devname
, &ident
,
1307 else if (devs_found
> 0) {
1308 if (c
.update
&& devs_found
> 1) {
1309 pr_err("can only update a single array at a time\n");
1312 if (c
.backup_file
&& devs_found
> 1) {
1313 pr_err("can only assemble a single array when providing a backup file.\n");
1316 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1317 struct mddev_ident
*array_ident
= conf_get_ident(dv
->devname
);
1318 if (array_ident
== NULL
) {
1319 pr_err("%s not identified in config file.\n",
1324 if (array_ident
->autof
== 0)
1325 array_ident
->autof
= c
.autof
;
1326 rv
|= Assemble(ss
, dv
->devname
, array_ident
,
1331 pr_err("--update not meaningful with a --scan assembly.\n");
1334 if (c
.backup_file
) {
1335 pr_err("--backup_file not meaningful with a --scan assembly.\n");
1338 rv
= scan_assemble(ss
, &c
, &ident
);
1344 c
.delay
= DEFAULT_BITMAP_DELAY
;
1345 if (s
.write_behind
&& !s
.bitmap_file
) {
1346 pr_err("write-behind mode requires a bitmap.\n");
1350 if (s
.raiddisks
== 0) {
1351 pr_err("no raid-devices specified.\n");
1356 if (s
.bitmap_file
) {
1357 if (strcmp(s
.bitmap_file
, "internal")==0) {
1358 pr_err("'internal' bitmaps not supported with --build\n");
1363 rv
= Build(devlist
->devname
, devlist
->next
, &s
, &c
);
1367 c
.delay
= DEFAULT_BITMAP_DELAY
;
1368 if (s
.write_behind
&& !s
.bitmap_file
) {
1369 pr_err("write-behind mode requires a bitmap.\n");
1373 if (s
.raiddisks
== 0) {
1374 pr_err("no raid-devices specified.\n");
1379 rv
= Create(ss
, devlist
->devname
,
1380 ident
.name
, ident
.uuid_set
? ident
.uuid
: NULL
,
1381 devs_found
-1, devlist
->next
,
1382 &s
, &c
, data_offset
);
1385 if (devmode
== 'E') {
1386 if (devlist
== NULL
&& !c
.scan
) {
1387 pr_err("No devices to examine\n");
1390 if (devlist
== NULL
)
1391 devlist
= conf_get_devs();
1392 if (devlist
== NULL
) {
1393 pr_err("No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1396 rv
= Examine(devlist
, &c
, ss
);
1397 } else if (devmode
== DetailPlatform
) {
1398 rv
= Detail_Platform(ss
? ss
->ss
: NULL
, ss
? c
.scan
: 1,
1399 c
.verbose
, c
.export
,
1400 devlist
? devlist
->devname
: NULL
);
1401 } else if (devlist
== NULL
) {
1402 if (devmode
== 'S' && c
.scan
)
1403 rv
= stop_scan(c
.verbose
);
1404 else if ((devmode
== 'D' || devmode
== Waitclean
) && c
.scan
)
1405 rv
= misc_scan(devmode
, &c
);
1406 else if (devmode
== UdevRules
)
1407 rv
= Write_rules(udev_filename
);
1409 pr_err("No devices given.\n");
1413 rv
= misc_list(devlist
, &ident
, ss
, &c
);
1416 if (!devlist
&& !c
.scan
) {
1417 pr_err("Cannot monitor: need --scan or at least one device\n");
1421 if (pidfile
&& !daemonise
) {
1422 pr_err("Cannot write a pid file when not in daemon mode\n");
1427 if (get_linux_version() > 2006016)
1428 /* mdstat responds to poll */
1435 rv
= Monitor(devlist
, mailaddr
, program
,
1436 &c
, daemonise
, oneshot
,
1437 dosyslog
, pidfile
, increments
,
1442 if (array_size
> 0) {
1443 /* alway impose array size first, independent of
1445 * Do not allow level or raid_disks changes at the
1446 * same time as that can be irreversibly destructive.
1450 if (s
.raiddisks
|| s
.level
!= UnSet
) {
1451 pr_err("cannot change array size in same operation "
1452 "as changing raiddisks or level.\n"
1453 " Change size first, then check that data is still intact.\n");
1457 sysfs_init(&sra
, mdfd
, 0);
1458 if (array_size
== MAX_SIZE
)
1459 err
= sysfs_set_str(&sra
, NULL
, "array_size", "default");
1461 err
= sysfs_set_num(&sra
, NULL
, "array_size", array_size
/ 2);
1464 pr_err("--array-size setting"
1465 " is too large.\n");
1467 pr_err("current kernel does"
1468 " not support setting --array-size\n");
1473 if (devs_found
> 1 && s
.raiddisks
== 0) {
1475 if (s
.size
> 0 || s
.chunk
|| s
.layout_str
!= NULL
|| s
.bitmap_file
) {
1476 pr_err("--add cannot be used with "
1477 "other geometry changes in --grow mode\n");
1481 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1482 rv
= Grow_Add_device(devlist
->devname
, mdfd
,
1487 } else if (s
.bitmap_file
) {
1488 if (s
.size
> 0 || s
.raiddisks
|| s
.chunk
||
1489 s
.layout_str
!= NULL
|| devs_found
> 1) {
1490 pr_err("--bitmap changes cannot be "
1491 "used with other geometry changes "
1492 "in --grow mode\n");
1497 c
.delay
= DEFAULT_BITMAP_DELAY
;
1498 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, &c
, &s
);
1499 } else if (grow_continue
)
1500 rv
= Grow_continue_command(devlist
->devname
,
1501 mdfd
, c
.backup_file
,
1503 else if (s
.size
> 0 || s
.raiddisks
|| s
.layout_str
!= NULL
1504 || s
.chunk
!= 0 || s
.level
!= UnSet
) {
1505 rv
= Grow_reshape(devlist
->devname
, mdfd
,
1507 data_offset
, &c
, &s
);
1508 } else if (array_size
== 0)
1509 pr_err("no changes to --grow\n");
1516 if (c
.runstop
<= 0) {
1517 pr_err("--incremental --scan meaningless without --run.\n");
1520 if (devmode
== 'f') {
1521 pr_err("--incremental --scan --fail not supported.\n");
1524 rv
= IncrementalScan(c
.verbose
);
1527 if (!rebuild_map
&& !c
.scan
) {
1528 pr_err("--incremental requires a device.\n");
1533 if (devlist
->next
) {
1534 pr_err("--incremental can only handle one device.\n");
1539 rv
= IncrementalRemove(devlist
->devname
, remove_path
,
1542 rv
= Incremental(devlist
->devname
, &c
, ss
);
1551 static int scan_assemble(struct supertype
*ss
,
1553 struct mddev_ident
*ident
)
1555 struct mddev_ident
*a
, *array_list
= conf_get_ident(NULL
);
1556 struct mddev_dev
*devlist
= conf_get_devs();
1557 struct map_ent
*map
= NULL
;
1560 int failures
, successes
;
1562 if (conf_verify_devnames(array_list
)) {
1563 pr_err("Duplicate MD device names in "
1564 "conf file were found.\n");
1567 if (devlist
== NULL
) {
1568 pr_err("No devices listed in conf file were found.\n");
1571 for (a
= array_list
; a
; a
= a
->next
) {
1574 a
->autof
= c
->autof
;
1577 pr_err("%s: failed to get "
1578 "exclusive lock on mapfile\n",
1584 for (a
= array_list
; a
; a
= a
->next
) {
1589 strcasecmp(a
->devname
, "<ignore>") == 0)
1592 r
= Assemble(ss
, a
->devname
,
1602 } while (failures
&& successes
);
1603 if (c
->homehost
&& cnt
== 0) {
1604 /* Maybe we can auto-assemble something.
1605 * Repeatedly call Assemble in auto-assemble mode
1610 ident
->autof
= c
->autof
;
1612 struct mddev_dev
*devlist
= conf_get_devs();
1615 rv2
= Assemble(ss
, NULL
,
1623 /* Incase there are stacked devices, we need to go around again */
1625 if (cnt
== 0 && rv
== 0) {
1626 pr_err("No arrays found in config file or automatically\n");
1630 } else if (cnt
== 0 && rv
== 0) {
1631 pr_err("No arrays found in config file\n");
1638 static int misc_scan(char devmode
, struct context
*c
)
1640 /* apply --detail or --wait-clean to
1641 * all devices in /proc/mdstat
1643 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1644 struct mdstat_ent
*e
;
1645 struct map_ent
*map
= NULL
;
1649 for (members
= 0; members
<= 1; members
++) {
1650 for (e
=ms
; e
; e
=e
->next
) {
1653 int member
= e
->metadata_version
&&
1654 strncmp(e
->metadata_version
,
1655 "external:/", 10) == 0;
1656 if (members
!= member
)
1658 me
= map_by_devnum(&map
, e
->devnum
);
1660 && strcmp(me
->path
, "/unknown") != 0)
1663 name
= get_md_name(e
->devnum
);
1666 pr_err("cannot find device file for %s\n",
1671 rv
|= Detail(name
, c
);
1673 rv
|= WaitClean(name
, -1, c
->verbose
);
1681 static int stop_scan(int verbose
)
1683 /* apply --stop to all devices in /proc/mdstat */
1684 /* Due to possible stacking of devices, repeat until
1685 * nothing more can be stopped
1687 int progress
=1, err
;
1691 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1692 struct mdstat_ent
*e
;
1694 if (!progress
) last
= 1;
1695 progress
= 0; err
= 0;
1696 for (e
=ms
; e
; e
=e
->next
) {
1697 char *name
= get_md_name(e
->devnum
);
1701 pr_err("cannot find device file for %s\n",
1705 mdfd
= open_mddev(name
, 1);
1707 if (Manage_runstop(name
, mdfd
, -1, verbose
, !last
))
1717 } while (!last
&& err
);
1723 static int misc_list(struct mddev_dev
*devlist
,
1724 struct mddev_ident
*ident
,
1725 struct supertype
*ss
, struct context
*c
)
1727 struct mddev_dev
*dv
;
1730 for (dv
=devlist
; dv
; dv
=dv
->next
) {
1733 switch(dv
->disposition
) {
1735 rv
|= Detail(dv
->devname
, c
);
1737 case KillOpt
: /* Zero superblock */
1739 rv
|= Kill(dv
->devname
, ss
, c
->force
, c
->verbose
,0);
1743 rv
|= Kill(dv
->devname
, NULL
, c
->force
, v
, 0);
1750 rv
|= Query(dv
->devname
); continue;
1752 rv
|= ExamineBitmap(dv
->devname
, c
->brief
, ss
); continue;
1755 rv
|= Wait(dv
->devname
); continue;
1757 rv
|= WaitClean(dv
->devname
, -1, c
->verbose
); continue;
1759 rv
|= Kill_subarray(dv
->devname
, c
->subarray
, c
->verbose
);
1761 case UpdateSubarray
:
1762 if (c
->update
== NULL
) {
1763 pr_err("-U/--update must be specified with --update-subarray\n");
1767 rv
|= Update_subarray(dv
->devname
, c
->subarray
,
1768 c
->update
, ident
, c
->verbose
);
1771 mdfd
= open_mddev(dv
->devname
, 1);
1773 switch(dv
->disposition
) {
1775 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1, c
->verbose
, 0); break;
1777 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1, c
->verbose
, 0); break;
1779 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1781 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;