]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - Create.c
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>
30 static int default_layout(struct supertype
*st
, int level
, int verbose
)
34 if (st
&& st
->ss
->default_geometry
)
35 st
->ss
->default_geometry(st
, &level
, &layout
, NULL
);
39 default: /* no layout */
43 layout
= 0x102; /* near=2, far=1 */
46 Name
": layout defaults to n2\n");
50 layout
= map_name(r5layout
, "default");
53 Name
": layout defaults to %s\n", map_num(r5layout
, layout
));
56 layout
= map_name(faultylayout
, "default");
60 Name
": layout defaults to %s\n", map_num(faultylayout
, layout
));
68 int Create(struct supertype
*st
, char *mddev
,
69 int chunk
, int level
, int layout
, unsigned long long size
, int raiddisks
, int sparedisks
,
70 char *name
, char *homehost
, int *uuid
,
71 int subdevs
, struct mddev_dev
*devlist
,
72 int runstop
, int verbose
, int force
, int assume_clean
,
73 char *bitmap_file
, int bitmap_chunk
, int write_behind
, int delay
, int autof
)
76 * Create a new raid array.
78 * First check that necessary details are available
79 * (i.e. level, raid-disks)
81 * Then check each disk to see what might be on it
82 * and report anything interesting.
84 * If anything looks odd, and runstop not set,
87 * SET_ARRAY_INFO and ADD_NEW_DISK, and
88 * if runstop==run, or raiddisks disks were used,
92 unsigned long long minsize
=0, maxsize
=0;
99 int first_missing
= subdevs
* 2;
100 int second_missing
= subdevs
* 2;
101 int missing_disks
= 0;
102 int insert_point
= subdevs
* 2; /* where to insert a missing drive */
108 int have_container
= 0;
109 int container_fd
= -1;
111 unsigned long long bitmapsize
;
112 struct mdinfo info
, *infos
;
114 int do_default_layout
= 0;
115 unsigned long safe_mode_delay
= 0;
116 char chosen_name
[1024];
117 struct map_ent
*map
= NULL
;
118 unsigned long long newsize
;
120 int major_num
= BITMAP_MAJOR_HI
;
122 memset(&info
, 0, sizeof(info
));
123 if (level
== UnSet
&& st
&& st
->ss
->default_geometry
)
124 st
->ss
->default_geometry(st
, &level
, NULL
, NULL
);
125 if (level
== UnSet
) {
127 Name
": a RAID level is needed to create an array.\n");
130 if (raiddisks
< 4 && level
== 6) {
132 Name
": at least 4 raid-devices needed for level 6\n");
135 if (raiddisks
> 256 && level
== 6) {
137 Name
": no more than 256 raid-devices supported for level 6\n");
140 if (raiddisks
< 2 && level
>= 4) {
142 Name
": at least 2 raid-devices needed for level 4 or 5\n");
145 if (level
<= 0 && sparedisks
) {
147 Name
": This level does not support spare devices\n");
151 if (subdevs
== 1 && strcmp(devlist
->devname
, "missing") != 0) {
152 /* If given a single device, it might be a container, and we can
153 * extract a device list from there
155 mdu_array_info_t inf
;
158 memset(&inf
, 0, sizeof(inf
));
159 fd
= open(devlist
->devname
, O_RDONLY
);
161 ioctl(fd
, GET_ARRAY_INFO
, &inf
) == 0 &&
162 inf
.raid_disks
== 0) {
163 /* yep, looks like a container */
165 rv
= st
->ss
->load_container(st
, fd
,
170 st
= super_by_fd(fd
, NULL
);
171 if (st
&& !(rv
= st
->ss
->
172 load_container(st
, fd
,
178 if (have_container
) {
180 first_missing
= subdevs
* 2;
181 second_missing
= subdevs
* 2;
182 insert_point
= subdevs
* 2;
188 if (st
&& st
->ss
->external
&& sparedisks
) {
190 Name
": This metadata type does not support "
191 "spare disks at create time\n");
194 if (subdevs
> raiddisks
+sparedisks
) {
195 fprintf(stderr
, Name
": You have listed more devices (%d) than are in the array(%d)!\n", subdevs
, raiddisks
+sparedisks
);
198 if (!have_container
&& subdevs
< raiddisks
+sparedisks
) {
199 fprintf(stderr
, Name
": You haven't given enough devices (real or missing) to create this array\n");
202 if (bitmap_file
&& level
<= 0) {
203 fprintf(stderr
, Name
": bitmaps not meaningful with level %s\n",
204 map_num(pers
, level
)?:"given");
208 /* now set some defaults */
211 if (layout
== UnSet
) {
212 do_default_layout
= 1;
213 layout
= default_layout(st
, level
, verbose
);
217 /* check layout fits in array*/
218 if ((layout
&255) * ((layout
>>8)&255) > raiddisks
) {
219 fprintf(stderr
, Name
": that layout requires at least %d devices\n",
220 (layout
&255) * ((layout
>>8)&255));
231 if (st
&& st
->ss
->default_geometry
)
232 st
->ss
->default_geometry(st
, NULL
, NULL
, &chunk
);
233 chunk
= chunk
? : 512;
235 fprintf(stderr
, Name
": chunk size defaults to %dK\n", chunk
);
239 /* a chunksize of zero 0s perfectly valid (and preferred) since 2.6.16 */
240 if (get_linux_version() < 2006016 && chunk
== 0) {
243 fprintf(stderr
, Name
": chunk size defaults to 64K\n");
248 case LEVEL_MULTIPATH
:
249 case LEVEL_CONTAINER
:
253 fprintf(stderr
, Name
": chunk size ignored for this level\n");
257 fprintf(stderr
, Name
": unknown level %d\n", level
);
262 size
&= ~(unsigned long long)(chunk
- 1);
264 if (st
&& ! st
->ss
->validate_geometry(st
, level
, layout
, raiddisks
,
265 chunk
, size
*2, NULL
, &newsize
, verbose
>=0))
269 if (size
&& verbose
> 0)
270 fprintf(stderr
, Name
": setting size to %lluK\n",
271 (unsigned long long)size
);
274 /* now look at the subdevs */
275 info
.array
.active_disks
= 0;
276 info
.array
.working_disks
= 0;
278 for (dv
=devlist
; dv
&& !have_container
; dv
=dv
->next
, dnum
++) {
279 char *dname
= dv
->devname
;
280 unsigned long long freesize
;
281 if (strcasecmp(dname
, "missing")==0) {
282 if (first_missing
> dnum
)
283 first_missing
= dnum
;
284 if (second_missing
> dnum
&& dnum
> first_missing
)
285 second_missing
= dnum
;
289 info
.array
.working_disks
++;
290 if (dnum
< raiddisks
)
291 info
.array
.active_disks
++;
293 struct createinfo
*ci
= conf_get_create_info();
298 /* Need to choose a default metadata, which is different
299 * depending on geometry of array.
302 char *name
= "default";
303 for(i
=0; !st
&& superlist
[i
]; i
++) {
304 st
= superlist
[i
]->match_metadata_desc(name
);
305 if (do_default_layout
)
306 layout
= default_layout(st
, level
, verbose
);
307 if (st
&& !st
->ss
->validate_geometry
308 (st
, level
, layout
, raiddisks
,
309 chunk
, size
*2, dname
, &freesize
,
315 fprintf(stderr
, Name
": device %s not suitable "
316 "for any style of array\n",
320 if (st
->ss
!= &super0
||
321 st
->minor_version
!= 90)
324 if (do_default_layout
)
325 layout
= default_layout(st
, level
, verbose
);
326 if (!st
->ss
->validate_geometry(st
, level
, layout
,
328 chunk
, size
*2, dname
,
333 Name
": %s is not suitable for "
341 freesize
/= 2; /* convert to K */
343 /* round to chunk size */
344 freesize
= freesize
& ~(chunk
-1);
347 if (size
&& freesize
< size
) {
348 fprintf(stderr
, Name
": %s is smaller than given size."
349 " %lluK < %lluK + metadata\n",
350 dname
, freesize
, size
);
354 if (maxdisc
== NULL
|| (maxdisc
&& freesize
> maxsize
)) {
358 if (mindisc
==NULL
|| (mindisc
&& freesize
< minsize
)) {
362 if (runstop
!= 1 || verbose
>= 0) {
363 int fd
= open(dname
, O_RDONLY
);
365 fprintf(stderr
, Name
": Cannot open %s: %s\n",
366 dname
, strerror(errno
));
370 warn
|= check_ext2(fd
, dname
);
371 warn
|= check_reiser(fd
, dname
);
372 warn
|= check_raid(fd
, dname
);
373 if (strcmp(st
->ss
->name
, "1.x") == 0 &&
374 st
->minor_version
>= 1)
375 /* metadata at front */
376 warn
|= check_partitions(fd
, dname
, 0);
377 else if (level
== 1 || level
== LEVEL_CONTAINER
)
378 /* partitions could be meaningful */
379 warn
|= check_partitions(fd
, dname
, freesize
*2);
381 /* partitions cannot be meaningful */
382 warn
|= check_partitions(fd
, dname
, 0);
383 if (strcmp(st
->ss
->name
, "1.x") == 0 &&
384 st
->minor_version
>= 1 &&
387 (warn
& 1024) == 0) {
389 fprintf(stderr
, Name
": Note: this array has metadata at the start and\n"
390 " may not be suitable as a boot device. If you plan to\n"
391 " store '/boot' on this device please ensure that\n"
392 " your boot-loader understands md/v1.x metadata, or use\n"
393 " --metadata=0.90\n");
399 info
.array
.working_disks
= raiddisks
;
401 fprintf(stderr
, Name
": create aborted\n");
405 if (mindisc
== NULL
&& !have_container
) {
406 fprintf(stderr
, Name
": no size and no drives given - aborting create.\n");
409 if (level
> 0 || level
== LEVEL_MULTIPATH
410 || level
== LEVEL_FAULTY
411 || st
->ss
->external
) {
412 /* size is meaningful */
413 if (!st
->ss
->validate_geometry(st
, level
, layout
,
417 fprintf(stderr
, Name
": devices too large for RAID level %d\n", level
);
422 fprintf(stderr
, Name
": size set to %lluK\n", size
);
425 if (!have_container
&& level
> 0 && ((maxsize
-size
)*100 > maxsize
)) {
426 if (runstop
!= 1 || verbose
>= 0)
427 fprintf(stderr
, Name
": largest drive (%s) exceeds size (%lluK) by more than 1%%\n",
432 if (st
->ss
->detail_platform
&& st
->ss
->detail_platform(0, 1) != 0) {
433 if (runstop
!= 1 || verbose
>= 0)
434 fprintf(stderr
, Name
": %s unable to enumerate platform support\n"
435 " array may not be compatible with hardware/firmware\n",
442 if (!ask("Continue creating array? ")) {
443 fprintf(stderr
, Name
": create aborted.\n");
448 fprintf(stderr
, Name
": creation continuing despite oddities due to --run\n");
452 /* If this is raid4/5, we want to configure the last active slot
453 * as missing, so that a reconstruct happens (faster than re-parity)
454 * FIX: Can we do this for raid6 as well?
456 if (st
->ss
->external
== 0 &&
457 assume_clean
==0 && force
== 0 && first_missing
>= raiddisks
) {
461 insert_point
= raiddisks
-1;
463 info
.array
.active_disks
--;
470 /* For raid6, if creating with 1 missing drive, make a good drive
471 * into a spare, else the create will fail
473 if (assume_clean
== 0 && force
== 0 && first_missing
< raiddisks
&&
474 st
->ss
->external
== 0 &&
475 second_missing
>= raiddisks
&& level
== 6) {
476 insert_point
= raiddisks
- 1;
477 if (insert_point
== first_missing
)
480 info
.array
.active_disks
--;
484 if (level
<= 0 && first_missing
< subdevs
* 2) {
486 Name
": This level does not support missing devices\n");
490 /* We need to create the device */
492 mdfd
= create_mddev(mddev
, name
, autof
, LOCAL
, chosen_name
);
497 vers
= md_get_version(mdfd
);
499 fprintf(stderr
, Name
": Create requires md driver version 0.90.0 or later\n");
502 mdu_array_info_t inf
;
503 memset(&inf
, 0, sizeof(inf
));
504 ioctl(mdfd
, GET_ARRAY_INFO
, &inf
);
505 if (inf
.working_disks
!= 0) {
506 fprintf(stderr
, Name
": another array by this name"
507 " is already running.\n");
512 /* Ok, lets try some ioctls */
514 info
.array
.level
= level
;
515 info
.array
.size
= size
;
516 info
.array
.raid_disks
= raiddisks
;
517 /* The kernel should *know* what md_minor we are dealing
518 * with, but it chooses to trust me instead. Sigh
520 info
.array
.md_minor
= 0;
521 if (fstat(mdfd
, &stb
)==0)
522 info
.array
.md_minor
= minor(stb
.st_rdev
);
523 info
.array
.not_persistent
= 0;
525 if ( ( (level
== 4 || level
== 5) &&
526 (insert_point
< raiddisks
|| first_missing
< raiddisks
) )
528 ( level
== 6 && (insert_point
< raiddisks
529 || second_missing
< raiddisks
))
535 info
.array
.state
= 1; /* clean, but one+ drive will be missing*/
536 info
.resync_start
= MaxSector
;
538 info
.array
.state
= 0; /* not clean, but no errors */
539 info
.resync_start
= 0;
542 /* for raid10, the bitmap size is the capacity of the array,
543 * which is array.size * raid_disks / ncopies;
544 * .. but convert to sectors.
546 int ncopies
= ((layout
>>8) & 255) * (layout
& 255);
547 bitmapsize
= (unsigned long long)size
* raiddisks
/ ncopies
* 2;
548 /* printf("bms=%llu as=%d rd=%d nc=%d\n", bitmapsize, size, raiddisks, ncopies);*/
550 bitmapsize
= (unsigned long long)size
* 2;
552 /* There is lots of redundancy in these disk counts,
553 * raid_disks is the most meaningful value
554 * it describes the geometry of the array
556 * nr_disks is total number of used slots.
557 * it should be raid_disks+spare_disks
558 * spare_disks is the number of extra disks present
560 * active_disks is the number of working disks in
561 * active slots. (With raid_disks)
562 * working_disks is the total number of working disks,
564 * failed_disks is the number of disks marked failed
566 * Ideally, the kernel would keep these (except raid_disks)
567 * up-to-date as we ADD_NEW_DISK, but it doesn't (yet).
568 * So for now, we assume that all raid and spare
569 * devices will be given.
571 info
.array
.spare_disks
=sparedisks
;
572 info
.array
.failed_disks
=missing_disks
;
573 info
.array
.nr_disks
= info
.array
.working_disks
574 + info
.array
.failed_disks
;
575 info
.array
.layout
= layout
;
576 info
.array
.chunk_size
= chunk
*1024;
578 if (name
== NULL
|| *name
== 0) {
579 /* base name on mddev */
584 * /dev/md/home -> home
585 * /dev/mdhome -> home
587 /* FIXME compare this with rules in create_mddev */
588 name
= strrchr(mddev
, '/');
591 if (strncmp(name
, "md_d", 4)==0 &&
594 (name
-mddev
) == 5 /* /dev/ */)
596 else if (strncmp(name
, "md", 2)==0 &&
599 (name
-mddev
) == 5 /* /dev/ */)
603 if (!st
->ss
->init_super(st
, &info
.array
, size
, name
, homehost
, uuid
))
606 total_slots
= info
.array
.nr_disks
;
607 sysfs_init(&info
, mdfd
, 0);
608 st
->ss
->getinfo_super(st
, &info
, NULL
);
610 if (did_default
&& verbose
>= 0) {
611 if (is_subarray(info
.text_version
)) {
612 int dnum
= devname2devnum(info
.text_version
+1);
614 int mdp
= get_mdp_major();
617 path
= map_dev(MD_MAJOR
, dnum
, 1);
619 path
= map_dev(mdp
, (-1-dnum
)<< 6, 1);
621 mdi
= sysfs_read(-1, dnum
, GET_VERSION
);
623 fprintf(stderr
, Name
": Creating array inside "
625 mdi
?mdi
->text_version
:"managed", path
);
628 fprintf(stderr
, Name
": Defaulting to version"
629 " %s metadata\n", info
.text_version
);
632 map_update(&map
, fd2devnum(mdfd
), info
.text_version
,
633 info
.uuid
, chosen_name
);
636 if (bitmap_file
&& vers
< 9003) {
637 major_num
= BITMAP_MAJOR_HOSTENDIAN
;
639 fprintf(stderr
, Name
": Warning - bitmaps created on this kernel are not portable\n"
640 " between different architectured. Consider upgrading the Linux kernel.\n");
644 if (bitmap_file
&& strcmp(bitmap_file
, "internal")==0) {
645 if ((vers
%100) < 2) {
646 fprintf(stderr
, Name
": internal bitmaps not supported by this kernel.\n");
649 if (!st
->ss
->add_internal_bitmap
) {
650 fprintf(stderr
, Name
": internal bitmaps not supported with %s metadata\n",
654 if (!st
->ss
->add_internal_bitmap(st
, &bitmap_chunk
,
656 bitmapsize
, 1, major_num
)) {
657 fprintf(stderr
, Name
": Given bitmap chunk size not supported.\n");
664 sysfs_init(&info
, mdfd
, 0);
666 if (st
->ss
->external
&& st
->container_dev
!= NoMdDev
) {
669 /* When creating a member, we need to be careful
670 * to negotiate with mdmon properly.
671 * If it is already running, we cannot write to
672 * the devices and must ask it to do that part.
673 * If it isn't running, we write to the devices,
675 * We hold an exclusive open on the container
676 * device to make sure mdmon doesn't exit after
677 * we checked that it is running.
679 * For now, fail if it is already running.
681 container_fd
= open_dev_excl(st
->container_dev
);
682 if (container_fd
< 0) {
683 fprintf(stderr
, Name
": Cannot get exclusive "
684 "open on container - weird.\n");
687 if (mdmon_running(st
->container_dev
)) {
689 fprintf(stderr
, Name
": reusing mdmon "
691 devnum2devname(st
->container_dev
));
692 st
->update_tail
= &st
->updates
;
696 rv
= set_array_info(mdfd
, st
, &info
);
698 fprintf(stderr
, Name
": failed to set array info for %s: %s\n",
699 mddev
, strerror(errno
));
706 st
->ss
->uuid_from_super(st
, uuid
);
707 if (CreateBitmap(bitmap_file
, force
, (char*)uuid
, bitmap_chunk
,
713 bitmap_fd
= open(bitmap_file
, O_RDWR
);
715 fprintf(stderr
, Name
": weird: %s cannot be openned\n",
719 if (ioctl(mdfd
, SET_BITMAP_FILE
, bitmap_fd
) < 0) {
720 fprintf(stderr
, Name
": Cannot set bitmap file for %s: %s\n",
721 mddev
, strerror(errno
));
726 infos
= malloc(sizeof(*infos
) * total_slots
);
728 for (pass
=1; pass
<=2 ; pass
++) {
729 struct mddev_dev
*moved_disk
= NULL
; /* the disk that was moved out of the insert point */
731 for (dnum
=0, dv
= devlist
; dv
;
732 dv
=(dv
->next
)?(dv
->next
):moved_disk
, dnum
++) {
735 struct mdinfo
*inf
= &infos
[dnum
];
737 if (dnum
>= total_slots
)
739 if (dnum
== insert_point
) {
743 if (strcasecmp(dv
->devname
, "missing")==0)
747 if (have_container
&& dnum
< info
.array
.raid_disks
- 1)
748 /* repeatedly use the container */
755 inf
->disk
.number
= dnum
;
756 inf
->disk
.raid_disk
= dnum
;
757 if (inf
->disk
.raid_disk
< raiddisks
)
758 inf
->disk
.state
= (1<<MD_DISK_ACTIVE
) |
763 if (dv
->writemostly
== 1)
764 inf
->disk
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
769 if (st
->ss
->external
&&
770 st
->container_dev
!= NoMdDev
)
771 fd
= open(dv
->devname
, O_RDWR
);
773 fd
= open(dv
->devname
, O_RDWR
|O_EXCL
);
776 fprintf(stderr
, Name
": failed to open %s "
777 "after earlier success - aborting\n",
782 inf
->disk
.major
= major(stb
.st_rdev
);
783 inf
->disk
.minor
= minor(stb
.st_rdev
);
786 remove_partitions(fd
);
787 if (st
->ss
->add_to_super(st
, &inf
->disk
,
789 ioctl(mdfd
, STOP_ARRAY
, NULL
);
792 st
->ss
->getinfo_super(st
, inf
, NULL
);
793 safe_mode_delay
= inf
->safe_mode_delay
;
795 if (have_container
&& verbose
> 0)
796 fprintf(stderr
, Name
": Using %s for device %d\n",
797 map_dev(inf
->disk
.major
,
801 if (!have_container
) {
802 /* getinfo_super might have lost these ... */
803 inf
->disk
.major
= major(stb
.st_rdev
);
804 inf
->disk
.minor
= minor(stb
.st_rdev
);
811 rv
= add_disk(mdfd
, st
, &info
, inf
);
815 Name
": ADD_NEW_DISK for %s "
817 dv
->devname
, strerror(errno
));
818 st
->ss
->free_super(st
);
823 if (!have_container
&&
824 dv
== moved_disk
&& dnum
!= insert_point
) break;
827 struct mdinfo info_new
;
828 struct map_ent
*me
= NULL
;
830 /* check to see if the uuid has changed due to these
831 * metadata changes, and if so update the member array
832 * and container uuid. Note ->write_init_super clears
833 * the subarray cursor such that ->getinfo_super once
834 * again returns container info.
837 st
->ss
->getinfo_super(st
, &info_new
, NULL
);
838 if (st
->ss
->external
&& level
!= LEVEL_CONTAINER
&&
839 !same_uuid(info_new
.uuid
, info
.uuid
, 0)) {
840 map_update(&map
, fd2devnum(mdfd
),
841 info_new
.text_version
,
842 info_new
.uuid
, chosen_name
);
843 me
= map_by_devnum(&map
, st
->container_dev
);
846 st
->ss
->write_init_super(st
);
848 /* update parent container uuid */
850 char *path
= strdup(me
->path
);
852 st
->ss
->getinfo_super(st
, &info_new
, NULL
);
853 map_update(&map
, st
->container_dev
,
854 info_new
.text_version
,
855 info_new
.uuid
, path
);
860 flush_metadata_updates(st
);
864 st
->ss
->free_super(st
);
866 if (level
== LEVEL_CONTAINER
) {
867 /* No need to start. But we should signal udev to
869 sysfs_uevent(&info
, "change");
871 fprintf(stderr
, Name
": container %s prepared.\n", mddev
);
872 wait_for(chosen_name
, mdfd
);
873 } else if (runstop
== 1 || subdevs
>= raiddisks
) {
874 if (st
->ss
->external
) {
877 case LEVEL_MULTIPATH
:
879 sysfs_set_str(&info
, NULL
, "array_state",
884 sysfs_set_str(&info
, NULL
, "array_state",
888 sysfs_set_safemode(&info
, safe_mode_delay
);
890 /* param is not actually used */
892 if (ioctl(mdfd
, RUN_ARRAY
, ¶m
)) {
893 fprintf(stderr
, Name
": RUN_ARRAY failed: %s\n",
895 ioctl(mdfd
, STOP_ARRAY
, NULL
);
900 fprintf(stderr
, Name
": array %s started.\n", mddev
);
901 if (st
->ss
->external
&& st
->container_dev
!= NoMdDev
) {
903 start_mdmon(st
->container_dev
);
905 ping_monitor(devnum2devname(st
->container_dev
));
908 wait_for(chosen_name
, mdfd
);
910 fprintf(stderr
, Name
": not starting array - not enough devices.\n");
917 map_remove(&map
, fd2devnum(mdfd
));