]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - Create.c
2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2006 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@cse.unsw.edu.au>
24 * School of Computer Science and Engineering
25 * The University of New South Wales
35 static int default_layout(struct supertype
*st
, int level
, int verbose
)
39 if (st
&& st
->ss
->default_layout
)
40 layout
= st
->ss
->default_layout(level
);
44 default: /* no layout */
48 layout
= 0x102; /* near=2, far=1 */
51 Name
": layout defaults to n1\n");
55 layout
= map_name(r5layout
, "default");
58 Name
": layout defaults to %s\n", map_num(r5layout
, layout
));
61 layout
= map_name(faultylayout
, "default");
65 Name
": layout defaults to %s\n", map_num(faultylayout
, layout
));
73 int Create(struct supertype
*st
, char *mddev
,
74 int chunk
, int level
, int layout
, unsigned long long size
, int raiddisks
, int sparedisks
,
75 char *name
, char *homehost
, int *uuid
,
76 int subdevs
, mddev_dev_t devlist
,
77 int runstop
, int verbose
, int force
, int assume_clean
,
78 char *bitmap_file
, int bitmap_chunk
, int write_behind
, int delay
, int autof
)
81 * Create a new raid array.
83 * First check that necessary details are available
84 * (i.e. level, raid-disks)
86 * Then check each disk to see what might be on it
87 * and report anything interesting.
89 * If anything looks odd, and runstop not set,
92 * SET_ARRAY_INFO and ADD_NEW_DISK, and
93 * if runstop==run, or raiddisks disks were used,
97 unsigned long long minsize
=0, maxsize
=0;
104 int first_missing
= subdevs
* 2;
105 int second_missing
= subdevs
* 2;
106 int missing_disks
= 0;
107 int insert_point
= subdevs
* 2; /* where to insert a missing drive */
113 int have_container
= 0;
114 int container_fd
= -1;
116 unsigned long long bitmapsize
;
117 struct mdinfo info
, *infos
;
119 int do_default_layout
= 0;
120 unsigned long safe_mode_delay
= 0;
121 char chosen_name
[1024];
122 struct map_ent
*map
= NULL
;
123 unsigned long long newsize
;
125 int major_num
= BITMAP_MAJOR_HI
;
127 memset(&info
, 0, sizeof(info
));
129 if (level
== UnSet
) {
130 /* "ddf" and "imsm" metadata only supports one level - should possibly
131 * push this into metadata handler??
133 if (st
&& (st
->ss
== &super_ddf
|| st
->ss
== &super_imsm
))
134 level
= LEVEL_CONTAINER
;
137 if (level
== UnSet
) {
139 Name
": a RAID level is needed to create an array.\n");
142 if (raiddisks
< 4 && level
== 6) {
144 Name
": at least 4 raid-devices needed for level 6\n");
147 if (raiddisks
> 256 && level
== 6) {
149 Name
": no more than 256 raid-devices supported for level 6\n");
152 if (raiddisks
< 2 && level
>= 4) {
154 Name
": at least 2 raid-devices needed for level 4 or 5\n");
157 if (level
<= 0 && sparedisks
) {
159 Name
": This level does not support spare devices\n");
163 if (subdevs
== 1 && strcmp(devlist
->devname
, "missing") != 0) {
164 /* If given a single device, it might be a container, and we can
165 * extract a device list from there
167 mdu_array_info_t inf
;
170 memset(&inf
, 0, sizeof(inf
));
171 fd
= open(devlist
->devname
, O_RDONLY
);
173 ioctl(fd
, GET_ARRAY_INFO
, &inf
) == 0 &&
174 inf
.raid_disks
== 0) {
175 /* yep, looks like a container */
177 rv
= st
->ss
->load_super(st
, fd
,
182 st
= guess_super(fd
);
183 if (st
&& !(rv
= st
->ss
->
190 if (have_container
) {
192 first_missing
= subdevs
* 2;
193 second_missing
= subdevs
* 2;
194 insert_point
= subdevs
* 2;
200 if (st
&& st
->ss
->external
&& sparedisks
) {
202 Name
": This metadata type does not support "
203 "spare disks are create time\n");
206 if (subdevs
> raiddisks
+sparedisks
) {
207 fprintf(stderr
, Name
": You have listed more devices (%d) than are in the array(%d)!\n", subdevs
, raiddisks
+sparedisks
);
210 if (!have_container
&& subdevs
< raiddisks
+sparedisks
) {
211 fprintf(stderr
, Name
": You haven't given enough devices (real or missing) to create this array\n");
214 if (bitmap_file
&& level
<= 0) {
215 fprintf(stderr
, Name
": bitmaps not meaningful with level %s\n",
216 map_num(pers
, level
)?:"given");
220 /* now set some defaults */
223 if (layout
== UnSet
) {
224 do_default_layout
= 1;
225 layout
= default_layout(st
, level
, verbose
);
229 /* check layout fits in array*/
230 if ((layout
&255) * ((layout
>>8)&255) > raiddisks
) {
231 fprintf(stderr
, Name
": that layout requires at least %d devices\n",
232 (layout
&255) * ((layout
>>8)&255));
242 case LEVEL_LINEAR
: /* linear */
246 fprintf(stderr
, Name
": chunk size defaults to 64K\n");
251 case LEVEL_MULTIPATH
:
252 case LEVEL_CONTAINER
:
256 fprintf(stderr
, Name
": chunk size ignored for this level\n");
260 fprintf(stderr
, Name
": unknown level %d\n", level
);
265 if (st
&& ! st
->ss
->validate_geometry(st
, level
, layout
, raiddisks
,
266 chunk
, size
*2, NULL
, &newsize
, verbose
>=0))
270 if (size
&& verbose
> 0)
271 fprintf(stderr
, Name
": setting size to %lluK\n",
272 (unsigned long long)size
);
275 /* now look at the subdevs */
276 info
.array
.active_disks
= 0;
277 info
.array
.working_disks
= 0;
279 for (dv
=devlist
; dv
&& !have_container
; dv
=dv
->next
, dnum
++) {
280 char *dname
= dv
->devname
;
281 unsigned long long freesize
;
282 if (strcasecmp(dname
, "missing")==0) {
283 if (first_missing
> dnum
)
284 first_missing
= dnum
;
285 if (second_missing
> dnum
&& dnum
> first_missing
)
286 second_missing
= dnum
;
290 info
.array
.working_disks
++;
291 if (dnum
< raiddisks
)
292 info
.array
.active_disks
++;
294 struct createinfo
*ci
= conf_get_create_info();
299 /* Need to choose a default metadata, which is different
300 * depending on geometry of array.
303 char *name
= "default";
304 for(i
=0; !st
&& superlist
[i
]; i
++) {
305 st
= superlist
[i
]->match_metadata_desc(name
);
306 if (do_default_layout
)
307 layout
= default_layout(st
, level
, verbose
);
308 if (st
&& !st
->ss
->validate_geometry
309 (st
, level
, layout
, raiddisks
,
310 chunk
, size
*2, dname
, &freesize
,
316 fprintf(stderr
, Name
": device %s not suitable "
317 "for any style of array\n",
321 if (st
->ss
!= &super0
||
322 st
->minor_version
!= 90)
325 if (do_default_layout
)
326 layout
= default_layout(st
, level
, verbose
);
327 if (!st
->ss
->validate_geometry(st
, level
, layout
,
329 chunk
, size
*2, dname
,
334 Name
": %s is not suitable for "
342 freesize
/= 2; /* convert to K */
344 /* round to chunk size */
345 freesize
= freesize
& ~(chunk
-1);
348 if (size
&& freesize
< size
) {
349 fprintf(stderr
, Name
": %s is smaller than given size."
350 " %lluK < %lluK + metadata\n",
351 dname
, freesize
, size
);
355 if (maxdisc
== NULL
|| (maxdisc
&& freesize
> maxsize
)) {
359 if (mindisc
==NULL
|| (mindisc
&& freesize
< minsize
)) {
363 if (runstop
!= 1 || verbose
>= 0) {
364 int fd
= open(dname
, O_RDONLY
);
366 fprintf(stderr
, Name
": Cannot open %s: %s\n",
367 dname
, strerror(errno
));
371 warn
|= check_ext2(fd
, dname
);
372 warn
|= check_reiser(fd
, dname
);
373 warn
|= check_raid(fd
, dname
);
378 info
.array
.working_disks
= raiddisks
;
380 fprintf(stderr
, Name
": create aborted\n");
384 if (mindisc
== NULL
&& !have_container
) {
385 fprintf(stderr
, Name
": no size and no drives given - aborting create.\n");
388 if (level
> 0 || level
== LEVEL_MULTIPATH
389 || level
== LEVEL_FAULTY
390 || st
->ss
->external
) {
391 /* size is meaningful */
392 if (!st
->ss
->validate_geometry(st
, level
, layout
,
396 fprintf(stderr
, Name
": devices too large for RAID level %d\n", level
);
401 fprintf(stderr
, Name
": size set to %lluK\n", size
);
404 if (!have_container
&& level
> 0 && ((maxsize
-size
)*100 > maxsize
)) {
405 if (runstop
!= 1 || verbose
>= 0)
406 fprintf(stderr
, Name
": largest drive (%s) exceeds size (%lluK) by more than 1%%\n",
411 if (st
->ss
->detail_platform
&& st
->ss
->detail_platform(0, 1) != 0) {
412 if (runstop
!= 1 || verbose
>= 0)
413 fprintf(stderr
, Name
": %s unable to enumerate platform support\n"
414 " array may not be compatible with hardware/firmware\n",
421 if (!ask("Continue creating array? ")) {
422 fprintf(stderr
, Name
": create aborted.\n");
427 fprintf(stderr
, Name
": creation continuing despite oddities due to --run\n");
431 /* If this is raid4/5, we want to configure the last active slot
432 * as missing, so that a reconstruct happens (faster than re-parity)
433 * FIX: Can we do this for raid6 as well?
435 if (st
->ss
->external
== 0 &&
436 assume_clean
==0 && force
== 0 && first_missing
>= raiddisks
) {
440 insert_point
= raiddisks
-1;
442 info
.array
.active_disks
--;
449 /* For raid6, if creating with 1 missing drive, make a good drive
450 * into a spare, else the create will fail
452 if (assume_clean
== 0 && force
== 0 && first_missing
< raiddisks
&&
453 st
->ss
->external
== 0 &&
454 second_missing
>= raiddisks
&& level
== 6) {
455 insert_point
= raiddisks
- 1;
456 if (insert_point
== first_missing
)
459 info
.array
.active_disks
--;
463 if (level
<= 0 && first_missing
< subdevs
* 2) {
465 Name
": This level does not support missing devices\n");
469 /* We need to create the device */
471 mdfd
= create_mddev(mddev
, name
, autof
, LOCAL
, chosen_name
);
476 vers
= md_get_version(mdfd
);
478 fprintf(stderr
, Name
": Create requires md driver version 0.90.0 or later\n");
481 mdu_array_info_t inf
;
482 memset(&inf
, 0, sizeof(inf
));
483 ioctl(mdfd
, GET_ARRAY_INFO
, &inf
);
484 if (inf
.working_disks
!= 0) {
485 fprintf(stderr
, Name
": another array by this name"
486 " is already running.\n");
491 /* Ok, lets try some ioctls */
493 info
.array
.level
= level
;
494 info
.array
.size
= size
;
495 info
.array
.raid_disks
= raiddisks
;
496 /* The kernel should *know* what md_minor we are dealing
497 * with, but it chooses to trust me instead. Sigh
499 info
.array
.md_minor
= 0;
500 if (fstat(mdfd
, &stb
)==0)
501 info
.array
.md_minor
= minor(stb
.st_rdev
);
502 info
.array
.not_persistent
= 0;
504 if ( ( (level
== 4 || level
== 5) &&
505 (insert_point
< raiddisks
|| first_missing
< raiddisks
) )
507 ( level
== 6 && (insert_point
< raiddisks
508 || second_missing
< raiddisks
))
514 info
.array
.state
= 1; /* clean, but one+ drive will be missing*/
515 info
.resync_start
= ~0ULL;
517 info
.array
.state
= 0; /* not clean, but no errors */
518 info
.resync_start
= 0;
521 /* for raid10, the bitmap size is the capacity of the array,
522 * which is array.size * raid_disks / ncopies;
523 * .. but convert to sectors.
525 int ncopies
= ((layout
>>8) & 255) * (layout
& 255);
526 bitmapsize
= (unsigned long long)size
* raiddisks
/ ncopies
* 2;
527 /* printf("bms=%llu as=%d rd=%d nc=%d\n", bitmapsize, size, raiddisks, ncopies);*/
529 bitmapsize
= (unsigned long long)size
* 2;
531 /* There is lots of redundancy in these disk counts,
532 * raid_disks is the most meaningful value
533 * it describes the geometry of the array
535 * nr_disks is total number of used slots.
536 * it should be raid_disks+spare_disks
537 * spare_disks is the number of extra disks present
539 * active_disks is the number of working disks in
540 * active slots. (With raid_disks)
541 * working_disks is the total number of working disks,
543 * failed_disks is the number of disks marked failed
545 * Ideally, the kernel would keep these (except raid_disks)
546 * up-to-date as we ADD_NEW_DISK, but it doesn't (yet).
547 * So for now, we assume that all raid and spare
548 * devices will be given.
550 info
.array
.spare_disks
=sparedisks
;
551 info
.array
.failed_disks
=missing_disks
;
552 info
.array
.nr_disks
= info
.array
.working_disks
553 + info
.array
.failed_disks
;
554 info
.array
.layout
= layout
;
555 info
.array
.chunk_size
= chunk
*1024;
557 if (name
== NULL
|| *name
== 0) {
558 /* base name on mddev */
563 * /dev/md/home -> home
564 * /dev/mdhome -> home
566 /* FIXME compare this with rules in create_mddev */
567 name
= strrchr(mddev
, '/');
570 if (strncmp(name
, "md_d", 4)==0 &&
573 (name
-mddev
) == 5 /* /dev/ */)
575 else if (strncmp(name
, "md", 2)==0 &&
578 (name
-mddev
) == 5 /* /dev/ */)
582 if (!st
->ss
->init_super(st
, &info
.array
, size
, name
, homehost
, uuid
))
585 total_slots
= info
.array
.nr_disks
;
586 sysfs_init(&info
, mdfd
, 0);
587 st
->ss
->getinfo_super(st
, &info
);
589 if (did_default
&& verbose
>= 0) {
590 if (is_subarray(info
.text_version
)) {
591 int dnum
= devname2devnum(info
.text_version
+1);
593 int mdp
= get_mdp_major();
596 path
= map_dev(MD_MAJOR
, dnum
, 1);
598 path
= map_dev(mdp
, (-1-dnum
)<< 6, 1);
600 mdi
= sysfs_read(-1, dnum
, GET_VERSION
);
602 fprintf(stderr
, Name
": Creating array inside "
604 mdi
?mdi
->text_version
:"managed", path
);
607 fprintf(stderr
, Name
": Defaulting to version"
608 " %s metadata\n", info
.text_version
);
611 map_update(&map
, fd2devnum(mdfd
), info
.text_version
,
612 info
.uuid
, chosen_name
);
615 if (bitmap_file
&& vers
< 9003) {
616 major_num
= BITMAP_MAJOR_HOSTENDIAN
;
618 fprintf(stderr
, Name
": Warning - bitmaps created on this kernel are not portable\n"
619 " between different architectured. Consider upgrading the Linux kernel.\n");
623 if (bitmap_file
&& strcmp(bitmap_file
, "internal")==0) {
624 if ((vers
%100) < 2) {
625 fprintf(stderr
, Name
": internal bitmaps not supported by this kernel.\n");
628 if (!st
->ss
->add_internal_bitmap(st
, &bitmap_chunk
,
630 bitmapsize
, 1, major_num
)) {
631 fprintf(stderr
, Name
": Given bitmap chunk size not supported.\n");
638 sysfs_init(&info
, mdfd
, 0);
640 if (st
->ss
->external
&& st
->subarray
[0]) {
643 /* When creating a member, we need to be careful
644 * to negotiate with mdmon properly.
645 * If it is already running, we cannot write to
646 * the devices and must ask it to do that part.
647 * If it isn't running, we write to the devices,
649 * We hold an exclusive open on the container
650 * device to make sure mdmon doesn't exit after
651 * we checked that it is running.
653 * For now, fail if it is already running.
655 container_fd
= open_dev_excl(st
->container_dev
);
656 if (container_fd
< 0) {
657 fprintf(stderr
, Name
": Cannot get exclusive "
658 "open on container - weird.\n");
661 if (mdmon_running(st
->container_dev
)) {
663 fprintf(stderr
, Name
": reusing mdmon "
665 devnum2devname(st
->container_dev
));
666 st
->update_tail
= &st
->updates
;
670 rv
= set_array_info(mdfd
, st
, &info
);
672 fprintf(stderr
, Name
": failed to set array info for %s: %s\n",
673 mddev
, strerror(errno
));
680 st
->ss
->uuid_from_super(st
, uuid
);
681 if (CreateBitmap(bitmap_file
, force
, (char*)uuid
, bitmap_chunk
,
687 bitmap_fd
= open(bitmap_file
, O_RDWR
);
689 fprintf(stderr
, Name
": weird: %s cannot be openned\n",
693 if (ioctl(mdfd
, SET_BITMAP_FILE
, bitmap_fd
) < 0) {
694 fprintf(stderr
, Name
": Cannot set bitmap file for %s: %s\n",
695 mddev
, strerror(errno
));
700 infos
= malloc(sizeof(*infos
) * total_slots
);
702 for (pass
=1; pass
<=2 ; pass
++) {
703 mddev_dev_t moved_disk
= NULL
; /* the disk that was moved out of the insert point */
705 for (dnum
=0, dv
= devlist
; dv
;
706 dv
=(dv
->next
)?(dv
->next
):moved_disk
, dnum
++) {
709 struct mdinfo
*inf
= &infos
[dnum
];
711 if (dnum
>= total_slots
)
713 if (dnum
== insert_point
) {
717 if (strcasecmp(dv
->devname
, "missing")==0)
721 if (have_container
&& dnum
< info
.array
.raid_disks
- 1)
722 /* repeatedly use the container */
729 inf
->disk
.number
= dnum
;
730 inf
->disk
.raid_disk
= dnum
;
731 if (inf
->disk
.raid_disk
< raiddisks
)
732 inf
->disk
.state
= (1<<MD_DISK_ACTIVE
) |
737 if (dv
->writemostly
== 1)
738 inf
->disk
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
743 if (st
->ss
->external
&& st
->subarray
[0])
744 fd
= open(dv
->devname
, O_RDWR
);
746 fd
= open(dv
->devname
, O_RDWR
|O_EXCL
);
749 fprintf(stderr
, Name
": failed to open %s "
750 "after earlier success - aborting\n",
755 inf
->disk
.major
= major(stb
.st_rdev
);
756 inf
->disk
.minor
= minor(stb
.st_rdev
);
759 remove_partitions(fd
);
760 if (st
->ss
->add_to_super(st
, &inf
->disk
,
763 st
->ss
->getinfo_super(st
, inf
);
764 safe_mode_delay
= inf
->safe_mode_delay
;
766 if (have_container
&& verbose
> 0)
767 fprintf(stderr
, Name
": Using %s for device %d\n",
768 map_dev(inf
->disk
.major
,
772 if (!have_container
) {
773 /* getinfo_super might have lost these ... */
774 inf
->disk
.major
= major(stb
.st_rdev
);
775 inf
->disk
.minor
= minor(stb
.st_rdev
);
782 rv
= add_disk(mdfd
, st
, &info
, inf
);
786 Name
": ADD_NEW_DISK for %s "
788 dv
->devname
, strerror(errno
));
789 st
->ss
->free_super(st
);
794 if (!have_container
&&
795 dv
== moved_disk
&& dnum
!= insert_point
) break;
798 st
->ss
->write_init_super(st
);
799 flush_metadata_updates(st
);
803 st
->ss
->free_super(st
);
805 if (level
== LEVEL_CONTAINER
) {
806 /* No need to start. But we should signal udev to
808 sysfs_uevent(&info
, "change");
810 fprintf(stderr
, Name
": container %s prepared.\n", mddev
);
811 wait_for(chosen_name
);
812 } else if (runstop
== 1 || subdevs
>= raiddisks
) {
813 if (st
->ss
->external
) {
816 case LEVEL_MULTIPATH
:
818 sysfs_set_str(&info
, NULL
, "array_state",
823 sysfs_set_str(&info
, NULL
, "array_state",
827 sysfs_set_safemode(&info
, safe_mode_delay
);
829 /* param is not actually used */
831 if (ioctl(mdfd
, RUN_ARRAY
, ¶m
)) {
832 fprintf(stderr
, Name
": RUN_ARRAY failed: %s\n",
834 Manage_runstop(mddev
, mdfd
, -1, 0);
839 fprintf(stderr
, Name
": array %s started.\n", mddev
);
840 if (st
->ss
->external
&& st
->subarray
[0]) {
842 start_mdmon(st
->container_dev
);
844 ping_monitor(devnum2devname(st
->container_dev
));
847 wait_for(chosen_name
);
849 fprintf(stderr
, Name
": not starting array - not enough devices.\n");