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
;
49 int max_disks
= MD_SB_DISKS
; /* just a default */
51 struct mddev_ident ident
;
52 char *configfile
= NULL
;
61 char *bitmap_file
= NULL
;
62 char *backup_file
= NULL
;
63 int bitmap_chunk
= UnSet
;
65 struct mddev_dev
*devlist
= NULL
;
66 struct mddev_dev
**devlistend
= & devlist
;
76 char *symlinks
= NULL
;
77 /* autof indicates whether and how to create device node.
78 * bottom 3 bits are style. Rest (when shifted) are number of parts
80 * 1 - don't create (no)
81 * 2 - if is_standard, then create (yes)
82 * 3 - create as 'md' - reject is_standard mdp (md)
83 * 4 - create as 'mdp' - reject is_standard md (mdp)
84 * 5 - default to md if not is_standard (md in config file)
85 * 6 - default to mdp if not is_standard (part, or mdp in config file)
89 char *homehost
= NULL
;
90 char sys_hostname
[256];
91 int require_homehost
= 1;
92 char *mailaddr
= NULL
;
99 struct supertype
*ss
= NULL
;
102 char *shortopt
= short_options
;
105 int auto_update_home
= 0;
106 char *subarray
= NULL
;
107 char *remove_path
= NULL
;
114 srandom(time(0) ^ getpid());
118 ident
.raid_disks
= UnSet
;
119 ident
.super_minor
= UnSet
;
121 ident
.spare_group
= NULL
;
124 ident
.bitmap_fd
= -1;
125 ident
.bitmap_file
= NULL
;
127 ident
.container
= NULL
;
130 while ((option_index
= -1) ,
131 (opt
=getopt_long(argc
, argv
,
132 shortopt
, long_options
,
133 &option_index
)) != -1) {
135 /* firstly, some mode-independent options */
138 if (option_index
> 0 &&
139 strcmp(long_options
[option_index
].name
, "help-options")==0)
146 fputs(Version
, stderr
);
156 if (mode
== ASSEMBLE
|| mode
== BUILD
|| mode
== CREATE
|| mode
== GROW
||
157 mode
== INCREMENTAL
|| mode
== MANAGE
)
158 break; /* b means bitmap */
166 if (strcasecmp(optarg
, "<ignore>") == 0)
167 require_homehost
= 0;
174 fputs(Usage
, stderr
);
177 /* second, figure out the mode.
178 * Some options force the mode. Others
179 * set the mode if it isn't already
183 case '@': /* just incase they say --manage */
185 shortopt
= short_bitmap_options
;
190 case ReAdd
: /* re-add */
193 shortopt
= short_bitmap_options
;
197 case 'A': newmode
= ASSEMBLE
; shortopt
= short_bitmap_auto_options
; break;
198 case 'B': newmode
= BUILD
; shortopt
= short_bitmap_auto_options
; break;
199 case 'C': newmode
= CREATE
; shortopt
= short_bitmap_auto_options
; break;
200 case 'F': newmode
= MONITOR
;break;
201 case 'G': newmode
= GROW
;
202 shortopt
= short_bitmap_options
;
204 case 'I': newmode
= INCREMENTAL
;
205 shortopt
= short_bitmap_auto_options
; break;
207 newmode
= AUTODETECT
; break;
213 case 'Q': newmode
= MISC
; break;
223 if (opt
== KillSubarray
|| opt
== UpdateSubarray
) {
225 fprintf(stderr
, Name
": subarray can only be specified once\n");
230 case 'K': if (!mode
) newmode
= MISC
; break;
232 if (mode
&& newmode
== mode
) {
233 /* everybody happy ! */
234 } else if (mode
&& newmode
!= mode
) {
236 fprintf(stderr
, Name
": ");
237 if (option_index
>= 0)
238 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
240 fprintf(stderr
, "-%c", opt
);
241 fprintf(stderr
, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
242 map_num(modes
, newmode
),
243 map_num(modes
, mode
));
245 } else if (!mode
&& newmode
) {
248 /* special case of -c --help */
250 ( strncmp(optarg
, "--h", 3)==0 ||
251 strncmp(optarg
, "-h", 2)==0)) {
252 fputs(Help_config
, stdout
);
256 /* If first option is a device, don't force the mode yet */
258 if (devs_found
== 0) {
259 dv
= malloc(sizeof(*dv
));
261 fprintf(stderr
, Name
": malloc failed\n");
264 dv
->devname
= optarg
;
265 dv
->disposition
= devmode
;
266 dv
->writemostly
= writemostly
;
271 devlistend
= &dv
->next
;
276 /* No mode yet, and this is the second device ... */
277 fprintf(stderr
, Name
": An option must be given to set the mode before a second device\n"
278 " (%s) is listed\n", optarg
);
281 if (option_index
>= 0)
282 fprintf(stderr
, Name
": --%s", long_options
[option_index
].name
);
284 fprintf(stderr
, Name
": -%c", opt
);
285 fprintf(stderr
, " does not set the mode, and so cannot be the first option.\n");
289 /* if we just set the mode, then done */
303 /* an undecorated option - must be a device name.
305 if (devs_found
> 0 && mode
== '@' && !devmode
) {
306 fprintf(stderr
, Name
": Must give one of -a/-r/-f for subsequent devices at %s\n", optarg
);
309 if (devs_found
> 0 && mode
== 'G' && !devmode
) {
310 fprintf(stderr
, Name
": Must give one of -a for devices do add: %s\n", optarg
);
313 dv
= malloc(sizeof(*dv
));
315 fprintf(stderr
, Name
": malloc failed\n");
318 dv
->devname
= optarg
;
319 dv
->disposition
= devmode
;
320 dv
->writemostly
= writemostly
;
325 devlistend
= &dv
->next
;
331 /* We've got a mode, and opt is now something else which
332 * could depend on the mode */
333 #define O(a,b) ((a<<8)|b)
334 switch (O(mode
,opt
)) {
337 case O(BUILD
,'c'): /* chunk or rounding */
339 fprintf(stderr
, Name
": chunk/rounding may only be specified once. "
340 "Second value is %s.\n", optarg
);
343 chunk
= strtol(optarg
, &c
, 10);
344 if (!optarg
[0] || *c
|| chunk
<4 || ((chunk
-1)&chunk
)) {
345 fprintf(stderr
, Name
": invalid chunk/rounding value: %s\n",
352 case O(ASSEMBLE
,AutoHomeHost
):
353 auto_update_home
= 1;
356 case O(INCREMENTAL
, 'e'):
358 case O(ASSEMBLE
,'e'):
359 case O(MISC
,'e'): /* set metadata (superblock) information */
361 fprintf(stderr
, Name
": metadata information already given\n");
364 for(i
=0; !ss
&& superlist
[i
]; i
++)
365 ss
= superlist
[i
]->match_metadata_desc(optarg
);
368 fprintf(stderr
, Name
": unrecognised metadata identifier: %s\n", optarg
);
371 max_disks
= ss
->max_devs
;
377 /* set write-mostly for following devices */
382 /* clear write-mostly for following devices */
389 case O(BUILD
,'z'): /* size */
391 fprintf(stderr
, Name
": size may only be specified once. "
392 "Second value is %s.\n", optarg
);
395 if (strcmp(optarg
, "max")==0)
398 size
= parse_size(optarg
);
400 fprintf(stderr
, Name
": invalid size: %s\n",
404 /* convert sectors to K */
409 case O(GROW
,'Z'): /* array size */
410 if (array_size
>= 0) {
411 fprintf(stderr
, Name
": array-size may only be specified once. "
412 "Second value is %s.\n", optarg
);
415 if (strcmp(optarg
, "max") == 0)
418 array_size
= parse_size(optarg
);
419 if (array_size
<= 0) {
420 fprintf(stderr
, Name
": invalid array size: %s\n",
429 case O(BUILD
,'l'): /* set raid level*/
430 if (level
!= UnSet
) {
431 fprintf(stderr
, Name
": raid level may only be set once. "
432 "Second value is %s.\n", optarg
);
435 level
= map_name(pers
, optarg
);
436 if (level
== UnSet
) {
437 fprintf(stderr
, Name
": invalid raid level: %s\n",
441 if (level
!= 0 && level
!= LEVEL_LINEAR
&& level
!= 1 &&
442 level
!= LEVEL_MULTIPATH
&& level
!= LEVEL_FAULTY
&&
445 fprintf(stderr
, Name
": Raid level %s not permitted with --build.\n",
449 if (sparedisks
> 0 && level
< 1 && level
>= -1) {
450 fprintf(stderr
, Name
": raid level %s is incompatible with spare-devices setting.\n",
457 case O(GROW
, 'p'): /* new layout */
459 fprintf(stderr
,Name
": layout may only be sent once. "
460 "Second value was %s\n", optarg
);
464 /* 'Grow' will parse the value */
467 case O(CREATE
,'p'): /* raid5 layout */
468 case O(BUILD
,'p'): /* faulty layout */
469 if (layout
!= UnSet
) {
470 fprintf(stderr
,Name
": layout may only be sent once. "
471 "Second value was %s\n", optarg
);
476 fprintf(stderr
, Name
": layout not meaningful for %s arrays.\n",
477 map_num(pers
, level
));
480 fprintf(stderr
, Name
": raid level must be given before layout.\n");
484 layout
= map_name(r5layout
, optarg
);
486 fprintf(stderr
, Name
": layout %s not understood for raid5.\n",
492 layout
= map_name(r6layout
, optarg
);
494 fprintf(stderr
, Name
": layout %s not understood for raid6.\n",
501 layout
= parse_layout_10(optarg
);
503 fprintf(stderr
, Name
": layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg
);
511 layout
= parse_layout_faulty(optarg
);
513 fprintf(stderr
, Name
": layout %s not understood for faulty.\n",
521 case O(CREATE
,AssumeClean
):
522 case O(BUILD
,AssumeClean
): /* assume clean */
528 case O(BUILD
,'n'): /* number of raid disks */
530 fprintf(stderr
, Name
": raid-devices set twice: %d and %s\n",
534 raiddisks
= strtol(optarg
, &c
, 10);
535 if (!optarg
[0] || *c
|| raiddisks
<=0) {
536 fprintf(stderr
, Name
": invalid number of raid devices: %s\n",
540 ident
.raid_disks
= raiddisks
;
543 case O(CREATE
,'x'): /* number of spare (eXtra) discs */
545 fprintf(stderr
,Name
": spare-devices set twice: %d and %s\n",
549 if (level
!= UnSet
&& level
<= 0 && level
>= -1) {
550 fprintf(stderr
, Name
": spare-devices setting is incompatible with raid level %d\n",
554 sparedisks
= strtol(optarg
, &c
, 10);
555 if (!optarg
[0] || *c
|| sparedisks
< 0) {
556 fprintf(stderr
, Name
": invalid number of spare-devices: %s\n",
564 case O(INCREMENTAL
,'a'):
565 case O(ASSEMBLE
,'a'): /* auto-creation of device node */
566 autof
= parse_auto(optarg
, "--auto flag", 0);
569 case O(CREATE
,Symlinks
):
570 case O(BUILD
,Symlinks
):
571 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
575 case O(BUILD
,'f'): /* force honouring '-n 1' */
576 case O(GROW
,'f'): /* ditto */
577 case O(CREATE
,'f'): /* force honouring of device list */
578 case O(ASSEMBLE
,'f'): /* force assembly */
579 case O(MISC
,'f'): /* force zero */
583 /* now for the Assemble options */
584 case O(CREATE
,'u'): /* uuid of array */
585 case O(ASSEMBLE
,'u'): /* uuid of array */
586 if (ident
.uuid_set
) {
587 fprintf(stderr
, Name
": uuid cannot be set twice. "
588 "Second value %s.\n", optarg
);
591 if (parse_uuid(optarg
, ident
.uuid
))
594 fprintf(stderr
,Name
": Bad uuid: %s\n", optarg
);
600 case O(ASSEMBLE
,'N'):
603 fprintf(stderr
, Name
": name cannot be set twice. "
604 "Second value %s.\n", optarg
);
607 if (mode
== MISC
&& !subarray
) {
608 fprintf(stderr
, Name
": -N/--name only valid with --update-subarray in misc mode\n");
611 if (strlen(optarg
) > 32) {
612 fprintf(stderr
, Name
": name '%s' is too long, 32 chars max.\n",
616 strcpy(ident
.name
, optarg
);
619 case O(ASSEMBLE
,'m'): /* super-minor for array */
620 if (ident
.super_minor
!= UnSet
) {
621 fprintf(stderr
, Name
": super-minor cannot be set twice. "
622 "Second value: %s.\n", optarg
);
625 if (strcmp(optarg
, "dev")==0)
626 ident
.super_minor
= -2;
628 ident
.super_minor
= strtoul(optarg
, &cp
, 10);
629 if (!optarg
[0] || *cp
) {
630 fprintf(stderr
, Name
": Bad super-minor number: %s.\n", optarg
);
636 case O(ASSEMBLE
,'U'): /* update the superblock */
639 fprintf(stderr
, Name
": Can only update one aspect of superblock, both %s and %s given.\n",
643 if (mode
== MISC
&& !subarray
) {
644 fprintf(stderr
, Name
": Only subarrays can be updated in misc mode\n");
648 if (strcmp(update
, "sparc2.2")==0)
650 if (strcmp(update
, "super-minor") == 0)
652 if (strcmp(update
, "summaries")==0)
654 if (strcmp(update
, "resync")==0)
656 if (strcmp(update
, "uuid")==0)
658 if (strcmp(update
, "name")==0)
660 if (strcmp(update
, "homehost")==0)
662 if (strcmp(update
, "devicesize")==0)
664 if (strcmp(update
, "byteorder")==0) {
666 fprintf(stderr
, Name
": must not set metadata type with --update=byteorder.\n");
669 for(i
=0; !ss
&& superlist
[i
]; i
++)
670 ss
= superlist
[i
]->match_metadata_desc("0.swap");
672 fprintf(stderr
, Name
": INTERNAL ERROR cannot find 0.swap\n");
678 if (strcmp(update
,"?") == 0 ||
679 strcmp(update
, "help") == 0) {
681 fprintf(outf
, Name
": ");
685 Name
": '--update=%s' is invalid. ",
688 fprintf(outf
, "Valid --update options are:\n"
689 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
690 " 'summaries', 'homehost', 'byteorder', 'devicesize'.\n");
691 exit(outf
== stdout
? 0 : 2);
693 case O(INCREMENTAL
,NoDegraded
):
694 fprintf(stderr
, Name
": --no-degraded is deprecated in Incremental mode\n");
695 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
696 runstop
= -1; /* --stop isn't allowed for --assemble,
697 * so we overload slightly */
700 case O(ASSEMBLE
,'c'): /* config file */
701 case O(INCREMENTAL
, 'c'):
705 fprintf(stderr
, Name
": configfile cannot be set twice. "
706 "Second value is %s.\n", optarg
);
710 set_conffile(configfile
);
711 /* FIXME possibly check that config file exists. Even parse it */
713 case O(ASSEMBLE
,'s'): /* scan */
716 case O(INCREMENTAL
,'s'):
720 case O(MONITOR
,'m'): /* mail address */
722 fprintf(stderr
, Name
": only specify one mailaddress. %s ignored.\n",
728 case O(MONITOR
,'p'): /* alert program */
730 fprintf(stderr
, Name
": only specify one alter program. %s ignored.\n",
736 case O(MONITOR
,'r'): /* rebuild increments */
737 increments
= atoi(optarg
);
738 if (increments
>99 || increments
<1) {
739 fprintf(stderr
, Name
": please specify positive integer between 1 and 99 as rebuild increments.\n");
744 case O(MONITOR
,'d'): /* delay in seconds */
746 case O(BUILD
,'d'): /* delay for bitmap updates */
749 fprintf(stderr
, Name
": only specify delay once. %s ignored.\n",
752 delay
= strtol(optarg
, &c
, 10);
753 if (!optarg
[0] || *c
|| delay
<1) {
754 fprintf(stderr
, Name
": invalid delay: %s\n",
760 case O(MONITOR
,'f'): /* daemonise */
763 case O(MONITOR
,'i'): /* pid */
765 fprintf(stderr
, Name
": only specify one pid file. %s ignored.\n",
770 case O(MONITOR
,'1'): /* oneshot */
773 case O(MONITOR
,'t'): /* test */
776 case O(MONITOR
,'y'): /* log messages to syslog */
777 openlog("mdadm", LOG_PID
, SYSLOG_FACILITY
);
781 /* now the general management options. Some are applicable
782 * to other modes. None have arguments.
785 case O(MANAGE
,'a'): /* add a drive */
789 case O(MANAGE
,ReAdd
):
793 case O(MANAGE
,'r'): /* remove a drive */
796 case O(MANAGE
,'f'): /* set faulty */
797 case O(INCREMENTAL
,'f'): /* r for incremental is taken, use f
798 * even though we will both fail and
799 * remove the device */
802 case O(INCREMENTAL
,'R'):
804 case O(ASSEMBLE
,'R'):
806 case O(CREATE
,'R'): /* Run the array */
808 fprintf(stderr
, Name
": Cannot both Stop and Run an array\n");
815 fprintf(stderr
, Name
": Cannot both Run and Stop an array\n");
834 case O(MISC
, Waitclean
):
835 case O(MISC
, DetailPlatform
):
836 case O(MISC
, KillSubarray
):
837 case O(MISC
, UpdateSubarray
):
838 if (devmode
&& devmode
!= opt
&&
839 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
840 fprintf(stderr
, Name
": --examine/-E cannot be given with ");
841 if (devmode
== 'E') {
842 if (option_index
>= 0)
843 fprintf(stderr
, "--%s\n",
844 long_options
[option_index
].name
);
846 fprintf(stderr
, "-%c\n", opt
);
847 } else if (isalpha(devmode
))
848 fprintf(stderr
, "-%c\n", devmode
);
850 fprintf(stderr
, "previous option\n");
859 case O(MISC
, Sparc22
):
860 if (devmode
!= 'E') {
861 fprintf(stderr
, Name
": --sparc2.2 only allowed with --examine\n");
867 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
869 fprintf(stderr
, Name
": bitmap file needed with -b in --assemble mode\n");
872 if (strcmp(optarg
, "internal")==0) {
873 fprintf(stderr
, Name
": there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
876 bitmap_fd
= open(optarg
, O_RDWR
);
877 if (!*optarg
|| bitmap_fd
< 0) {
878 fprintf(stderr
, Name
": cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
881 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
884 case O(ASSEMBLE
, BackupFile
):
885 case O(GROW
, BackupFile
):
886 /* Specify a file into which grow might place a backup,
887 * or from which assemble might recover a backup
890 fprintf(stderr
, Name
": backup file already specified, rejecting %s\n", optarg
);
893 backup_file
= optarg
;
897 case O(CREATE
,'b'): /* here we create the bitmap */
898 if (strcmp(optarg
, "none") == 0) {
899 fprintf(stderr
, Name
": '--bitmap none' only"
900 " support for --grow\n");
905 if (strcmp(optarg
, "internal")== 0 ||
906 strcmp(optarg
, "none")== 0 ||
907 strchr(optarg
, '/') != NULL
) {
908 bitmap_file
= optarg
;
912 fprintf(stderr
, Name
": bitmap file must contain a '/', or be 'internal', or 'none'\n"
913 " not '%s'\n", optarg
);
916 case O(GROW
,BitmapChunk
):
917 case O(BUILD
,BitmapChunk
):
918 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
919 bitmap_chunk
= strtol(optarg
, &c
, 10);
920 if (!optarg
[0] || *c
|| bitmap_chunk
< 0 ||
921 bitmap_chunk
& (bitmap_chunk
- 1)) {
922 fprintf(stderr
, Name
": invalid bitmap chunksize: %s\n",
926 /* convert K to B, chunk of 0K means 512B */
927 bitmap_chunk
= bitmap_chunk
? bitmap_chunk
* 1024 : 512;
930 case O(GROW
, WriteBehind
):
931 case O(BUILD
, WriteBehind
):
932 case O(CREATE
, WriteBehind
): /* write-behind mode */
933 write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
935 write_behind
= strtol(optarg
, &c
, 10);
936 if (write_behind
< 0 || *c
||
937 write_behind
> 16383) {
938 fprintf(stderr
, Name
": Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
944 case O(INCREMENTAL
, 'r'):
947 case O(INCREMENTAL
, IncrementalPath
):
948 remove_path
= optarg
;
951 /* We have now processed all the valid options. Anything else is
954 if (option_index
> 0)
955 fprintf(stderr
, Name
":option --%s not valid in %s mode\n",
956 long_options
[option_index
].name
,
957 map_num(modes
, mode
));
959 fprintf(stderr
, Name
": option -%c not valid in %s mode\n",
960 opt
, map_num(modes
, mode
));
966 char *help_text
= Help
;
968 help_text
= OptionHelp
;
971 case ASSEMBLE
: help_text
= Help_assemble
; break;
972 case BUILD
: help_text
= Help_build
; break;
973 case CREATE
: help_text
= Help_create
; break;
974 case MANAGE
: help_text
= Help_manage
; break;
975 case MISC
: help_text
= Help_misc
; break;
976 case MONITOR
: help_text
= Help_monitor
; break;
977 case GROW
: help_text
= Help_grow
; break;
978 case INCREMENTAL
:help_text
= Help_incr
; break;
980 fputs(help_text
,stdout
);
984 if (!mode
&& devs_found
) {
987 if (devlist
->disposition
== 0)
988 devlist
->disposition
= devmode
;
991 fputs(Usage
, stderr
);
996 struct createinfo
*ci
= conf_get_create_info();
998 if (strcasecmp(symlinks
, "yes") == 0)
1000 else if (strcasecmp(symlinks
, "no") == 0)
1003 fprintf(stderr
, Name
": option --symlinks must be 'no' or 'yes'\n");
1007 /* Ok, got the option parsing out of the way
1008 * hopefully it's mostly right but there might be some stuff
1011 * That is mosty checked in the per-mode stuff but...
1013 * For @,B,C and A without -s, the first device listed must be an md device
1014 * we check that here and open it.
1017 if (mode
==MANAGE
|| mode
== BUILD
|| mode
== CREATE
|| mode
== GROW
||
1018 (mode
== ASSEMBLE
&& ! scan
)) {
1019 if (devs_found
< 1) {
1020 fprintf(stderr
, Name
": an md device must be given in this mode\n");
1023 if ((int)ident
.super_minor
== -2 && autof
) {
1024 fprintf(stderr
, Name
": --super-minor=dev is incompatible with --auto\n");
1027 if (mode
== MANAGE
|| mode
== GROW
) {
1028 mdfd
= open_mddev(devlist
->devname
, 1);
1032 /* non-existent device is OK */
1033 mdfd
= open_mddev(devlist
->devname
, 0);
1035 fprintf(stderr
, Name
": device %s exists but is not an "
1036 "md array.\n", devlist
->devname
);
1039 if ((int)ident
.super_minor
== -2) {
1042 fprintf(stderr
, Name
": --super-minor=dev given, and "
1043 "listed device %s doesn't exist.\n",
1048 ident
.super_minor
= minor(stb
.st_rdev
);
1050 if (mdfd
>= 0 && mode
!= MANAGE
&& mode
!= GROW
) {
1051 /* We don't really want this open yet, we just might
1052 * have wanted to check some things
1060 if (raiddisks
> max_disks
) {
1061 fprintf(stderr
, Name
": invalid number of raid devices: %d\n",
1065 if (raiddisks
== 1 && !force
&& level
!= -5) {
1066 fprintf(stderr
, Name
": '1' is an unusual number of drives for an array, so it is probably\n"
1067 " a mistake. If you really mean it you will need to specify --force before\n"
1068 " setting the number of drives.\n");
1073 if ( sparedisks
> max_disks
- raiddisks
) {
1074 fprintf(stderr
, Name
": invalid number of spare-devices: %d\n",
1080 if (homehost
== NULL
)
1081 homehost
= conf_get_homehost(&require_homehost
);
1082 if (homehost
== NULL
|| strcmp(homehost
, "<system>")==0) {
1083 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
1084 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
1085 homehost
= sys_hostname
;
1089 if ((mode
!= MISC
|| devmode
!= 'E') &&
1091 fprintf(stderr
, Name
": must be super-user to perform this action\n");
1095 ident
.autof
= autof
;
1100 /* readonly, add/remove, readwrite, runstop */
1102 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1103 if (!rv
&& devs_found
>1)
1104 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
1105 devlist
->next
, verbose
-quiet
, test
);
1106 if (!rv
&& readonly
< 0)
1107 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1109 rv
= Manage_runstop(devlist
->devname
, mdfd
, runstop
, quiet
);
1112 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1113 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !scan
) {
1114 /* Only a device has been given, so get details from config file */
1115 struct mddev_ident
*array_ident
= conf_get_ident(devlist
->devname
);
1116 if (array_ident
== NULL
) {
1117 fprintf(stderr
, Name
": %s not identified in config file.\n",
1123 if (array_ident
->autof
== 0)
1124 array_ident
->autof
= autof
;
1125 rv
|= Assemble(ss
, devlist
->devname
, array_ident
,
1127 readonly
, runstop
, update
,
1128 homehost
, require_homehost
,
1129 verbose
-quiet
, force
);
1132 rv
= Assemble(ss
, devlist
->devname
, &ident
,
1133 devlist
->next
, backup_file
,
1134 readonly
, runstop
, update
,
1135 homehost
, require_homehost
,
1136 verbose
-quiet
, force
);
1137 else if (devs_found
>0) {
1138 if (update
&& devs_found
> 1) {
1139 fprintf(stderr
, Name
": can only update a single array at a time\n");
1142 if (backup_file
&& devs_found
> 1) {
1143 fprintf(stderr
, Name
": can only assemble a single array when providing a backup file.\n");
1146 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1147 struct mddev_ident
*array_ident
= conf_get_ident(dv
->devname
);
1148 if (array_ident
== NULL
) {
1149 fprintf(stderr
, Name
": %s not identified in config file.\n",
1154 if (array_ident
->autof
== 0)
1155 array_ident
->autof
= autof
;
1156 rv
|= Assemble(ss
, dv
->devname
, array_ident
,
1158 readonly
, runstop
, update
,
1159 homehost
, require_homehost
,
1160 verbose
-quiet
, force
);
1163 struct mddev_ident
*a
, *array_list
= conf_get_ident(NULL
);
1164 struct mddev_dev
*devlist
= conf_get_devs();
1166 int failures
, successes
;
1167 if (devlist
== NULL
) {
1168 fprintf(stderr
, Name
": No devices listed in conf file were found.\n");
1172 fprintf(stderr
, Name
": --update not meaningful with a --scan assembly.\n");
1176 fprintf(stderr
, Name
": --backup_file not meaningful with a --scan assembly.\n");
1179 for (a
= array_list
; a
; a
= a
->next
) {
1188 for (a
= array_list
; a
; a
= a
->next
) {
1193 strcasecmp(a
->devname
, "<ignore>") == 0)
1196 r
= Assemble(ss
, a
->devname
,
1199 readonly
, runstop
, NULL
,
1200 homehost
, require_homehost
,
1201 verbose
-quiet
, force
);
1210 } while (failures
&& successes
);
1211 if (homehost
&& cnt
== 0) {
1212 /* Maybe we can auto-assemble something.
1213 * Repeatedly call Assemble in auto-assemble mode
1218 ident
.autof
= autof
;
1220 struct mddev_dev
*devlist
= conf_get_devs();
1223 rv2
= Assemble(ss
, NULL
,
1226 readonly
, runstop
, NULL
,
1227 homehost
, require_homehost
,
1228 verbose
-quiet
, force
);
1234 /* found something so even though assembly failed we
1235 * want to avoid auto-updates
1237 auto_update_home
= 0;
1239 /* Incase there are stacked devices, we need to go around again */
1242 if (cnt
== 0 && auto_update_home
&& homehost
) {
1243 /* Nothing found, maybe we need to bootstrap homehost info */
1247 rv2
= Assemble(ss
, NULL
,
1250 readonly
, runstop
, "homehost",
1251 homehost
, require_homehost
,
1252 verbose
-quiet
, force
);
1258 /* Incase there are stacked devices, we need to go around again */
1262 if (cnt
== 0 && rv
== 0) {
1263 fprintf(stderr
, Name
": No arrays found in config file or automatically\n");
1267 } else if (cnt
== 0 && rv
== 0) {
1268 fprintf(stderr
, Name
": No arrays found in config file\n");
1274 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1275 if (write_behind
&& !bitmap_file
) {
1276 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1280 if (raiddisks
== 0) {
1281 fprintf(stderr
, Name
": no raid-devices specified.\n");
1287 if (strcmp(bitmap_file
, "internal")==0) {
1288 fprintf(stderr
, Name
": 'internal' bitmaps not supported with --build\n");
1293 rv
= Build(devlist
->devname
, chunk
, level
, layout
,
1294 raiddisks
, devlist
->next
, assume_clean
,
1295 bitmap_file
, bitmap_chunk
, write_behind
,
1296 delay
, verbose
-quiet
, autof
, size
);
1299 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1300 if (write_behind
&& !bitmap_file
) {
1301 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1305 if (raiddisks
== 0) {
1306 fprintf(stderr
, Name
": no raid-devices specified.\n");
1311 rv
= Create(ss
, devlist
->devname
, chunk
, level
, layout
, size
<0 ? 0 : size
,
1312 raiddisks
, sparedisks
, ident
.name
, homehost
,
1313 ident
.uuid_set
? ident
.uuid
: NULL
,
1314 devs_found
-1, devlist
->next
, runstop
, verbose
-quiet
, force
, assume_clean
,
1315 bitmap_file
, bitmap_chunk
, write_behind
, delay
, autof
);
1318 if (devmode
== 'E') {
1319 if (devlist
== NULL
&& !scan
) {
1320 fprintf(stderr
, Name
": No devices to examine\n");
1323 if (devlist
== NULL
)
1324 devlist
= conf_get_devs();
1325 if (devlist
== NULL
) {
1326 fprintf(stderr
, Name
": No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1329 if (brief
&& verbose
)
1331 rv
= Examine(devlist
, scan
?(verbose
>1?0:verbose
+1):brief
,
1333 SparcAdjust
, ss
, homehost
);
1334 } else if (devmode
== DetailPlatform
) {
1335 rv
= Detail_Platform(ss
? ss
->ss
: NULL
, ss
? scan
: 1, verbose
);
1337 if (devlist
== NULL
) {
1338 if ((devmode
=='D' || devmode
== Waitclean
) && scan
) {
1339 /* apply --detail or --wait-clean to
1340 * all devices in /proc/mdstat
1342 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1343 struct mdstat_ent
*e
;
1344 struct map_ent
*map
= NULL
;
1346 int v
= verbose
>1?0:verbose
+1;
1348 for (members
= 0; members
<= 1; members
++) {
1349 for (e
=ms
; e
; e
=e
->next
) {
1352 int member
= e
->metadata_version
&&
1353 strncmp(e
->metadata_version
,
1354 "external:/", 10) == 0;
1355 if (members
!= member
)
1357 me
= map_by_devnum(&map
, e
->devnum
);
1359 && strcmp(me
->path
, "/unknown") != 0)
1362 name
= get_md_name(e
->devnum
);
1365 fprintf(stderr
, Name
": cannot find device file for %s\n",
1370 rv
|= Detail(name
, v
,
1374 rv
|= WaitClean(name
, -1, v
);
1379 } else if (devmode
== 'S' && scan
) {
1380 /* apply --stop to all devices in /proc/mdstat */
1381 /* Due to possible stacking of devices, repeat until
1382 * nothing more can be stopped
1384 int progress
=1, err
;
1387 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1388 struct mdstat_ent
*e
;
1390 if (!progress
) last
= 1;
1391 progress
= 0; err
= 0;
1392 for (e
=ms
; e
; e
=e
->next
) {
1393 char *name
= get_md_name(e
->devnum
);
1396 fprintf(stderr
, Name
": cannot find device file for %s\n",
1400 mdfd
= open_mddev(name
, 1);
1402 if (Manage_runstop(name
, mdfd
, -1, quiet
?1:last
?0:-1))
1412 } while (!last
&& err
);
1415 fprintf(stderr
, Name
": No devices given.\n");
1419 for (dv
=devlist
; dv
; dv
=dv
->next
) {
1420 switch(dv
->disposition
) {
1422 rv
|= Detail(dv
->devname
,
1424 export
, test
, homehost
);
1426 case 'K': /* Zero superblock */
1428 rv
|= Kill(dv
->devname
, ss
, force
, quiet
,0);
1432 rv
|= Kill(dv
->devname
, NULL
, force
, q
, 0);
1439 rv
|= Query(dv
->devname
); continue;
1441 rv
|= ExamineBitmap(dv
->devname
, brief
, ss
); continue;
1443 rv
|= Wait(dv
->devname
); continue;
1445 rv
|= WaitClean(dv
->devname
, -1, verbose
-quiet
); continue;
1447 rv
|= Kill_subarray(dv
->devname
, subarray
, quiet
);
1449 case UpdateSubarray
:
1450 if (update
== NULL
) {
1452 Name
": -U/--update must be specified with --update-subarray\n");
1456 rv
|= Update_subarray(dv
->devname
, subarray
, update
, &ident
, quiet
);
1459 mdfd
= open_mddev(dv
->devname
, 1);
1461 switch(dv
->disposition
) {
1463 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1, quiet
); break;
1465 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1, quiet
); break;
1467 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1469 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;
1478 if (!devlist
&& !scan
) {
1479 fprintf(stderr
, Name
": Cannot monitor: need --scan or at least one device\n");
1483 if (pidfile
&& !daemonise
) {
1484 fprintf(stderr
, Name
": Cannot write a pid file when not in daemon mode\n");
1489 if (get_linux_version() > 20616)
1490 /* mdstat responds to poll */
1495 rv
= Monitor(devlist
, mailaddr
, program
,
1496 delay
?delay
:60, daemonise
, scan
, oneshot
,
1497 dosyslog
, test
, pidfile
, increments
);
1501 if (array_size
>= 0) {
1502 /* alway impose array size first, independent of
1504 * Do not allow level or raid_disks changes at the
1505 * same time as that can be irreversibly destructive.
1509 if (raiddisks
|| level
!= UnSet
) {
1510 fprintf(stderr
, Name
": cannot change array size in same operation "
1511 "as changing raiddisks or level.\n"
1512 " Change size first, then check that data is still intact.\n");
1516 sysfs_init(&sra
, mdfd
, 0);
1517 if (array_size
== 0)
1518 err
= sysfs_set_str(&sra
, NULL
, "array_size", "default");
1520 err
= sysfs_set_num(&sra
, NULL
, "array_size", array_size
/ 2);
1523 fprintf(stderr
, Name
": --array-size setting"
1524 " is too large.\n");
1526 fprintf(stderr
, Name
": current kernel does"
1527 " not support setting --array-size\n");
1532 if (devs_found
> 1) {
1535 if (size
>= 0 || raiddisks
|| chunk
|| layout_str
!= NULL
|| bitmap_file
) {
1536 fprintf(stderr
, Name
": --add cannot be used with other geometry changes in --grow mode\n");
1540 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1541 rv
= Grow_Add_device(devlist
->devname
, mdfd
, dv
->devname
);
1545 } else if (bitmap_file
) {
1546 if (size
>= 0 || raiddisks
|| chunk
|| layout_str
!= NULL
) {
1547 fprintf(stderr
, Name
": --bitmap changes cannot be used with other geometry changes in --grow mode\n");
1552 delay
= DEFAULT_BITMAP_DELAY
;
1553 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, bitmap_file
,
1554 bitmap_chunk
, delay
, write_behind
, force
);
1555 } else if (size
>= 0 || raiddisks
!= 0 || layout_str
!= NULL
1556 || chunk
!= 0 || level
!= UnSet
) {
1557 rv
= Grow_reshape(devlist
->devname
, mdfd
, quiet
, backup_file
,
1558 size
, level
, layout_str
, chunk
, raiddisks
);
1559 } else if (array_size
< 0)
1560 fprintf(stderr
, Name
": no changes to --grow\n");
1568 fprintf(stderr
, Name
1569 ": --incremental --scan meaningless without --run.\n");
1572 if (devmode
== 'f') {
1573 fprintf(stderr
, Name
1574 ": --incremental --scan --fail not supported.\n");
1577 rv
= IncrementalScan(verbose
);
1580 if (!rebuild_map
&& !scan
) {
1581 fprintf(stderr
, Name
1582 ": --incremental requires a device.\n");
1587 if (devlist
->next
) {
1588 fprintf(stderr
, Name
1589 ": --incremental can only handle one device.\n");
1594 rv
= IncrementalRemove(devlist
->devname
, remove_path
,
1597 rv
= Incremental(devlist
->devname
, verbose
-quiet
,
1598 runstop
, ss
, homehost
,
1599 require_homehost
, autof
);