]>
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_layout
)
35 layout
= st
->ss
->default_layout(level
);
39 default: /* no layout */
43 layout
= 0x102; /* near=2, far=1 */
46 Name
": layout defaults to n1\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
, mddev_dev_t 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
));
124 if (level
== UnSet
) {
125 /* "ddf" and "imsm" metadata only supports one level - should possibly
126 * push this into metadata handler??
128 if (st
&& (st
->ss
== &super_ddf
|| st
->ss
== &super_imsm
))
129 level
= LEVEL_CONTAINER
;
132 if (level
== UnSet
) {
134 Name
": a RAID level is needed to create an array.\n");
137 if (raiddisks
< 4 && level
== 6) {
139 Name
": at least 4 raid-devices needed for level 6\n");
142 if (raiddisks
> 256 && level
== 6) {
144 Name
": no more than 256 raid-devices supported for level 6\n");
147 if (raiddisks
< 2 && level
>= 4) {
149 Name
": at least 2 raid-devices needed for level 4 or 5\n");
152 if (level
<= 0 && sparedisks
) {
154 Name
": This level does not support spare devices\n");
158 if (subdevs
== 1 && strcmp(devlist
->devname
, "missing") != 0) {
159 /* If given a single device, it might be a container, and we can
160 * extract a device list from there
162 mdu_array_info_t inf
;
165 memset(&inf
, 0, sizeof(inf
));
166 fd
= open(devlist
->devname
, O_RDONLY
);
168 ioctl(fd
, GET_ARRAY_INFO
, &inf
) == 0 &&
169 inf
.raid_disks
== 0) {
170 /* yep, looks like a container */
172 rv
= st
->ss
->load_super(st
, fd
,
177 st
= guess_super(fd
);
178 if (st
&& !(rv
= st
->ss
->
185 if (have_container
) {
187 first_missing
= subdevs
* 2;
188 second_missing
= subdevs
* 2;
189 insert_point
= subdevs
* 2;
195 if (st
&& st
->ss
->external
&& sparedisks
) {
197 Name
": This metadata type does not support "
198 "spare disks at create time\n");
201 if (subdevs
> raiddisks
+sparedisks
) {
202 fprintf(stderr
, Name
": You have listed more devices (%d) than are in the array(%d)!\n", subdevs
, raiddisks
+sparedisks
);
205 if (!have_container
&& subdevs
< raiddisks
+sparedisks
) {
206 fprintf(stderr
, Name
": You haven't given enough devices (real or missing) to create this array\n");
209 if (bitmap_file
&& level
<= 0) {
210 fprintf(stderr
, Name
": bitmaps not meaningful with level %s\n",
211 map_num(pers
, level
)?:"given");
215 /* now set some defaults */
218 if (layout
== UnSet
) {
219 do_default_layout
= 1;
220 layout
= default_layout(st
, level
, verbose
);
224 /* check layout fits in array*/
225 if ((layout
&255) * ((layout
>>8)&255) > raiddisks
) {
226 fprintf(stderr
, Name
": that layout requires at least %d devices\n",
227 (layout
&255) * ((layout
>>8)&255));
240 fprintf(stderr
, Name
": chunk size defaults to 512K\n");
244 /* a chunksize of zero 0s perfectly valid (and preferred) since 2.6.16 */
245 if (get_linux_version() < 2006016 && chunk
== 0) {
248 fprintf(stderr
, Name
": chunk size defaults to 64K\n");
253 case LEVEL_MULTIPATH
:
254 case LEVEL_CONTAINER
:
258 fprintf(stderr
, Name
": chunk size ignored for this level\n");
262 fprintf(stderr
, Name
": unknown level %d\n", level
);
267 size
&= ~(unsigned long long)(chunk
- 1);
269 if (st
&& ! st
->ss
->validate_geometry(st
, level
, layout
, raiddisks
,
270 chunk
, size
*2, NULL
, &newsize
, verbose
>=0))
274 if (size
&& verbose
> 0)
275 fprintf(stderr
, Name
": setting size to %lluK\n",
276 (unsigned long long)size
);
279 /* now look at the subdevs */
280 info
.array
.active_disks
= 0;
281 info
.array
.working_disks
= 0;
283 for (dv
=devlist
; dv
&& !have_container
; dv
=dv
->next
, dnum
++) {
284 char *dname
= dv
->devname
;
285 unsigned long long freesize
;
286 if (strcasecmp(dname
, "missing")==0) {
287 if (first_missing
> dnum
)
288 first_missing
= dnum
;
289 if (second_missing
> dnum
&& dnum
> first_missing
)
290 second_missing
= dnum
;
294 info
.array
.working_disks
++;
295 if (dnum
< raiddisks
)
296 info
.array
.active_disks
++;
298 struct createinfo
*ci
= conf_get_create_info();
303 /* Need to choose a default metadata, which is different
304 * depending on geometry of array.
307 char *name
= "default";
308 for(i
=0; !st
&& superlist
[i
]; i
++) {
309 st
= superlist
[i
]->match_metadata_desc(name
);
310 if (do_default_layout
)
311 layout
= default_layout(st
, level
, verbose
);
312 if (st
&& !st
->ss
->validate_geometry
313 (st
, level
, layout
, raiddisks
,
314 chunk
, size
*2, dname
, &freesize
,
320 fprintf(stderr
, Name
": device %s not suitable "
321 "for any style of array\n",
325 if (st
->ss
!= &super0
||
326 st
->minor_version
!= 90)
329 if (do_default_layout
)
330 layout
= default_layout(st
, level
, verbose
);
331 if (!st
->ss
->validate_geometry(st
, level
, layout
,
333 chunk
, size
*2, dname
,
338 Name
": %s is not suitable for "
346 freesize
/= 2; /* convert to K */
348 /* round to chunk size */
349 freesize
= freesize
& ~(chunk
-1);
352 if (size
&& freesize
< size
) {
353 fprintf(stderr
, Name
": %s is smaller than given size."
354 " %lluK < %lluK + metadata\n",
355 dname
, freesize
, size
);
359 if (maxdisc
== NULL
|| (maxdisc
&& freesize
> maxsize
)) {
363 if (mindisc
==NULL
|| (mindisc
&& freesize
< minsize
)) {
367 if (runstop
!= 1 || verbose
>= 0) {
368 int fd
= open(dname
, O_RDONLY
);
370 fprintf(stderr
, Name
": Cannot open %s: %s\n",
371 dname
, strerror(errno
));
375 warn
|= check_ext2(fd
, dname
);
376 warn
|= check_reiser(fd
, dname
);
377 warn
|= check_raid(fd
, dname
);
378 if (st
&& strcmp(st
->ss
->name
, "1.x") == 0 &&
379 st
->minor_version
>= 1 &&
383 fprintf(stderr
, Name
": Note: this array has metadata at the start and\n"
384 " may not be suitable as a boot device. If you plan to\n"
385 " store '/' or '/boot' on this device please ensure that\n"
386 " your boot-loader understands md/v1.x metadata, or use\n"
387 " --metadata=1.0\n");
393 info
.array
.working_disks
= raiddisks
;
395 fprintf(stderr
, Name
": create aborted\n");
399 if (mindisc
== NULL
&& !have_container
) {
400 fprintf(stderr
, Name
": no size and no drives given - aborting create.\n");
403 if (level
> 0 || level
== LEVEL_MULTIPATH
404 || level
== LEVEL_FAULTY
405 || st
->ss
->external
) {
406 /* size is meaningful */
407 if (!st
->ss
->validate_geometry(st
, level
, layout
,
411 fprintf(stderr
, Name
": devices too large for RAID level %d\n", level
);
416 fprintf(stderr
, Name
": size set to %lluK\n", size
);
419 if (!have_container
&& level
> 0 && ((maxsize
-size
)*100 > maxsize
)) {
420 if (runstop
!= 1 || verbose
>= 0)
421 fprintf(stderr
, Name
": largest drive (%s) exceeds size (%lluK) by more than 1%%\n",
426 if (st
->ss
->detail_platform
&& st
->ss
->detail_platform(0, 1) != 0) {
427 if (runstop
!= 1 || verbose
>= 0)
428 fprintf(stderr
, Name
": %s unable to enumerate platform support\n"
429 " array may not be compatible with hardware/firmware\n",
436 if (!ask("Continue creating array? ")) {
437 fprintf(stderr
, Name
": create aborted.\n");
442 fprintf(stderr
, Name
": creation continuing despite oddities due to --run\n");
446 /* If this is raid4/5, we want to configure the last active slot
447 * as missing, so that a reconstruct happens (faster than re-parity)
448 * FIX: Can we do this for raid6 as well?
450 if (st
->ss
->external
== 0 &&
451 assume_clean
==0 && force
== 0 && first_missing
>= raiddisks
) {
455 insert_point
= raiddisks
-1;
457 info
.array
.active_disks
--;
464 /* For raid6, if creating with 1 missing drive, make a good drive
465 * into a spare, else the create will fail
467 if (assume_clean
== 0 && force
== 0 && first_missing
< raiddisks
&&
468 st
->ss
->external
== 0 &&
469 second_missing
>= raiddisks
&& level
== 6) {
470 insert_point
= raiddisks
- 1;
471 if (insert_point
== first_missing
)
474 info
.array
.active_disks
--;
478 if (level
<= 0 && first_missing
< subdevs
* 2) {
480 Name
": This level does not support missing devices\n");
484 /* We need to create the device */
486 mdfd
= create_mddev(mddev
, name
, autof
, LOCAL
, chosen_name
);
491 vers
= md_get_version(mdfd
);
493 fprintf(stderr
, Name
": Create requires md driver version 0.90.0 or later\n");
496 mdu_array_info_t inf
;
497 memset(&inf
, 0, sizeof(inf
));
498 ioctl(mdfd
, GET_ARRAY_INFO
, &inf
);
499 if (inf
.working_disks
!= 0) {
500 fprintf(stderr
, Name
": another array by this name"
501 " is already running.\n");
506 /* Ok, lets try some ioctls */
508 info
.array
.level
= level
;
509 info
.array
.size
= size
;
510 info
.array
.raid_disks
= raiddisks
;
511 /* The kernel should *know* what md_minor we are dealing
512 * with, but it chooses to trust me instead. Sigh
514 info
.array
.md_minor
= 0;
515 if (fstat(mdfd
, &stb
)==0)
516 info
.array
.md_minor
= minor(stb
.st_rdev
);
517 info
.array
.not_persistent
= 0;
519 if ( ( (level
== 4 || level
== 5) &&
520 (insert_point
< raiddisks
|| first_missing
< raiddisks
) )
522 ( level
== 6 && (insert_point
< raiddisks
523 || second_missing
< raiddisks
))
529 info
.array
.state
= 1; /* clean, but one+ drive will be missing*/
530 info
.resync_start
= ~0ULL;
532 info
.array
.state
= 0; /* not clean, but no errors */
533 info
.resync_start
= 0;
536 /* for raid10, the bitmap size is the capacity of the array,
537 * which is array.size * raid_disks / ncopies;
538 * .. but convert to sectors.
540 int ncopies
= ((layout
>>8) & 255) * (layout
& 255);
541 bitmapsize
= (unsigned long long)size
* raiddisks
/ ncopies
* 2;
542 /* printf("bms=%llu as=%d rd=%d nc=%d\n", bitmapsize, size, raiddisks, ncopies);*/
544 bitmapsize
= (unsigned long long)size
* 2;
546 /* There is lots of redundancy in these disk counts,
547 * raid_disks is the most meaningful value
548 * it describes the geometry of the array
550 * nr_disks is total number of used slots.
551 * it should be raid_disks+spare_disks
552 * spare_disks is the number of extra disks present
554 * active_disks is the number of working disks in
555 * active slots. (With raid_disks)
556 * working_disks is the total number of working disks,
558 * failed_disks is the number of disks marked failed
560 * Ideally, the kernel would keep these (except raid_disks)
561 * up-to-date as we ADD_NEW_DISK, but it doesn't (yet).
562 * So for now, we assume that all raid and spare
563 * devices will be given.
565 info
.array
.spare_disks
=sparedisks
;
566 info
.array
.failed_disks
=missing_disks
;
567 info
.array
.nr_disks
= info
.array
.working_disks
568 + info
.array
.failed_disks
;
569 info
.array
.layout
= layout
;
570 info
.array
.chunk_size
= chunk
*1024;
572 if (name
== NULL
|| *name
== 0) {
573 /* base name on mddev */
578 * /dev/md/home -> home
579 * /dev/mdhome -> home
581 /* FIXME compare this with rules in create_mddev */
582 name
= strrchr(mddev
, '/');
585 if (strncmp(name
, "md_d", 4)==0 &&
588 (name
-mddev
) == 5 /* /dev/ */)
590 else if (strncmp(name
, "md", 2)==0 &&
593 (name
-mddev
) == 5 /* /dev/ */)
597 if (!st
->ss
->init_super(st
, &info
.array
, size
, name
, homehost
, uuid
))
600 total_slots
= info
.array
.nr_disks
;
601 sysfs_init(&info
, mdfd
, 0);
602 st
->ss
->getinfo_super(st
, &info
);
604 if (did_default
&& verbose
>= 0) {
605 if (is_subarray(info
.text_version
)) {
606 int dnum
= devname2devnum(info
.text_version
+1);
608 int mdp
= get_mdp_major();
611 path
= map_dev(MD_MAJOR
, dnum
, 1);
613 path
= map_dev(mdp
, (-1-dnum
)<< 6, 1);
615 mdi
= sysfs_read(-1, dnum
, GET_VERSION
);
617 fprintf(stderr
, Name
": Creating array inside "
619 mdi
?mdi
->text_version
:"managed", path
);
622 fprintf(stderr
, Name
": Defaulting to version"
623 " %s metadata\n", info
.text_version
);
626 map_update(&map
, fd2devnum(mdfd
), info
.text_version
,
627 info
.uuid
, chosen_name
);
630 if (bitmap_file
&& vers
< 9003) {
631 major_num
= BITMAP_MAJOR_HOSTENDIAN
;
633 fprintf(stderr
, Name
": Warning - bitmaps created on this kernel are not portable\n"
634 " between different architectured. Consider upgrading the Linux kernel.\n");
638 if (bitmap_file
&& strcmp(bitmap_file
, "internal")==0) {
639 if ((vers
%100) < 2) {
640 fprintf(stderr
, Name
": internal bitmaps not supported by this kernel.\n");
643 if (!st
->ss
->add_internal_bitmap(st
, &bitmap_chunk
,
645 bitmapsize
, 1, major_num
)) {
646 fprintf(stderr
, Name
": Given bitmap chunk size not supported.\n");
653 sysfs_init(&info
, mdfd
, 0);
655 if (st
->ss
->external
&& st
->subarray
[0]) {
658 /* When creating a member, we need to be careful
659 * to negotiate with mdmon properly.
660 * If it is already running, we cannot write to
661 * the devices and must ask it to do that part.
662 * If it isn't running, we write to the devices,
664 * We hold an exclusive open on the container
665 * device to make sure mdmon doesn't exit after
666 * we checked that it is running.
668 * For now, fail if it is already running.
670 container_fd
= open_dev_excl(st
->container_dev
);
671 if (container_fd
< 0) {
672 fprintf(stderr
, Name
": Cannot get exclusive "
673 "open on container - weird.\n");
676 if (mdmon_running(st
->container_dev
)) {
678 fprintf(stderr
, Name
": reusing mdmon "
680 devnum2devname(st
->container_dev
));
681 st
->update_tail
= &st
->updates
;
685 rv
= set_array_info(mdfd
, st
, &info
);
687 fprintf(stderr
, Name
": failed to set array info for %s: %s\n",
688 mddev
, strerror(errno
));
695 st
->ss
->uuid_from_super(st
, uuid
);
696 if (CreateBitmap(bitmap_file
, force
, (char*)uuid
, bitmap_chunk
,
702 bitmap_fd
= open(bitmap_file
, O_RDWR
);
704 fprintf(stderr
, Name
": weird: %s cannot be openned\n",
708 if (ioctl(mdfd
, SET_BITMAP_FILE
, bitmap_fd
) < 0) {
709 fprintf(stderr
, Name
": Cannot set bitmap file for %s: %s\n",
710 mddev
, strerror(errno
));
715 infos
= malloc(sizeof(*infos
) * total_slots
);
717 for (pass
=1; pass
<=2 ; pass
++) {
718 mddev_dev_t moved_disk
= NULL
; /* the disk that was moved out of the insert point */
720 for (dnum
=0, dv
= devlist
; dv
;
721 dv
=(dv
->next
)?(dv
->next
):moved_disk
, dnum
++) {
724 struct mdinfo
*inf
= &infos
[dnum
];
726 if (dnum
>= total_slots
)
728 if (dnum
== insert_point
) {
732 if (strcasecmp(dv
->devname
, "missing")==0)
736 if (have_container
&& dnum
< info
.array
.raid_disks
- 1)
737 /* repeatedly use the container */
744 inf
->disk
.number
= dnum
;
745 inf
->disk
.raid_disk
= dnum
;
746 if (inf
->disk
.raid_disk
< raiddisks
)
747 inf
->disk
.state
= (1<<MD_DISK_ACTIVE
) |
752 if (dv
->writemostly
== 1)
753 inf
->disk
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
758 if (st
->ss
->external
&& st
->subarray
[0])
759 fd
= open(dv
->devname
, O_RDWR
);
761 fd
= open(dv
->devname
, O_RDWR
|O_EXCL
);
764 fprintf(stderr
, Name
": failed to open %s "
765 "after earlier success - aborting\n",
770 inf
->disk
.major
= major(stb
.st_rdev
);
771 inf
->disk
.minor
= minor(stb
.st_rdev
);
774 remove_partitions(fd
);
775 if (st
->ss
->add_to_super(st
, &inf
->disk
,
778 st
->ss
->getinfo_super(st
, inf
);
779 safe_mode_delay
= inf
->safe_mode_delay
;
781 if (have_container
&& verbose
> 0)
782 fprintf(stderr
, Name
": Using %s for device %d\n",
783 map_dev(inf
->disk
.major
,
787 if (!have_container
) {
788 /* getinfo_super might have lost these ... */
789 inf
->disk
.major
= major(stb
.st_rdev
);
790 inf
->disk
.minor
= minor(stb
.st_rdev
);
797 rv
= add_disk(mdfd
, st
, &info
, inf
);
801 Name
": ADD_NEW_DISK for %s "
803 dv
->devname
, strerror(errno
));
804 st
->ss
->free_super(st
);
809 if (!have_container
&&
810 dv
== moved_disk
&& dnum
!= insert_point
) break;
813 struct mdinfo info_new
;
814 struct map_ent
*me
= NULL
;
816 /* check to see if the uuid has changed due to these
817 * metadata changes, and if so update the member array
818 * and container uuid. Note ->write_init_super clears
819 * the subarray cursor such that ->getinfo_super once
820 * again returns container info.
823 st
->ss
->getinfo_super(st
, &info_new
);
824 if (st
->ss
->external
&& level
!= LEVEL_CONTAINER
&&
825 !same_uuid(info_new
.uuid
, info
.uuid
, 0)) {
826 map_update(&map
, fd2devnum(mdfd
),
827 info_new
.text_version
,
828 info_new
.uuid
, chosen_name
);
829 me
= map_by_devnum(&map
, st
->container_dev
);
832 st
->ss
->write_init_super(st
);
834 /* update parent container uuid */
836 char *path
= strdup(me
->path
);
838 st
->ss
->getinfo_super(st
, &info_new
);
839 map_update(&map
, st
->container_dev
,
840 info_new
.text_version
,
841 info_new
.uuid
, path
);
846 flush_metadata_updates(st
);
850 st
->ss
->free_super(st
);
852 if (level
== LEVEL_CONTAINER
) {
853 /* No need to start. But we should signal udev to
855 sysfs_uevent(&info
, "change");
857 fprintf(stderr
, Name
": container %s prepared.\n", mddev
);
858 wait_for(chosen_name
, mdfd
);
859 } else if (runstop
== 1 || subdevs
>= raiddisks
) {
860 if (st
->ss
->external
) {
863 case LEVEL_MULTIPATH
:
865 sysfs_set_str(&info
, NULL
, "array_state",
870 sysfs_set_str(&info
, NULL
, "array_state",
874 sysfs_set_safemode(&info
, safe_mode_delay
);
876 /* param is not actually used */
878 if (ioctl(mdfd
, RUN_ARRAY
, ¶m
)) {
879 fprintf(stderr
, Name
": RUN_ARRAY failed: %s\n",
881 Manage_runstop(mddev
, mdfd
, -1, 0);
886 fprintf(stderr
, Name
": array %s started.\n", mddev
);
887 if (st
->ss
->external
&& st
->subarray
[0]) {
889 start_mdmon(st
->container_dev
);
891 ping_monitor(devnum2devname(st
->container_dev
));
894 wait_for(chosen_name
, mdfd
);
896 fprintf(stderr
, Name
": not starting array - not enough devices.\n");