]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - Create.c
59a41a66aff5a2e6b3b6dcde4d64e0e9b23deda4
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 int Create(struct supertype
*st
, char *mddev
, int mdfd
,
36 int chunk
, int level
, int layout
, unsigned long long size
, int raiddisks
, int sparedisks
,
37 char *name
, char *homehost
, int *uuid
,
38 int subdevs
, mddev_dev_t devlist
,
39 int runstop
, int verbose
, int force
, int assume_clean
,
40 char *bitmap_file
, int bitmap_chunk
, int write_behind
, int delay
)
43 * Create a new raid array.
45 * First check that necessary details are available
46 * (i.e. level, raid-disks)
48 * Then check each disk to see what might be on it
49 * and report anything interesting.
51 * If anything looks odd, and runstop not set,
54 * SET_ARRAY_INFO and ADD_NEW_DISK, and
55 * if runstop==run, or raiddisks disks were used,
58 unsigned long long minsize
=0, maxsize
=0;
65 int first_missing
= subdevs
* 2;
66 int second_missing
= subdevs
* 2;
67 int missing_disks
= 0;
68 int insert_point
= subdevs
* 2; /* where to insert a missing drive */
73 int have_container
= 0;
76 unsigned long long bitmapsize
;
80 int major_num
= BITMAP_MAJOR_HI
;
82 memset(&info
, 0, sizeof(info
));
84 vers
= md_get_version(mdfd
);
86 fprintf(stderr
, Name
": Create requires md driver version 0.90.0 or later\n");
90 memset(&inf
, 0, sizeof(inf
));
91 ioctl(mdfd
, GET_ARRAY_INFO
, &inf
);
92 if (inf
.working_disks
!= 0) {
93 fprintf(stderr
, Name
": another array by this name"
94 " is already running.\n");
99 /* "ddf" and "imsm" metadata only supports one level - should possibly
100 * push this into metadata handler??
102 if (st
&& (st
->ss
== &super_ddf
|| st
->ss
== &super_imsm
))
103 level
= LEVEL_CONTAINER
;
106 if (level
== UnSet
) {
108 Name
": a RAID level is needed to create an array.\n");
111 if (raiddisks
< 4 && level
== 6) {
113 Name
": at least 4 raid-devices needed for level 6\n");
116 if (raiddisks
> 256 && level
== 6) {
118 Name
": no more than 256 raid-devices supported for level 6\n");
121 if (raiddisks
< 2 && level
>= 4) {
123 Name
": at least 2 raid-devices needed for level 4 or 5\n");
126 if (level
<= 0 && sparedisks
) {
128 Name
": This level does not support spare devices\n");
132 if (subdevs
== 1 && strcmp(devlist
->devname
, "missing") != 0) {
133 /* If given a single device, it might be a container, and we can
134 * extract a device list from there
136 mdu_array_info_t inf
;
139 memset(&inf
, 0, sizeof(inf
));
140 fd
= open(devlist
->devname
, O_RDONLY
, 0);
142 ioctl(fd
, GET_ARRAY_INFO
, &inf
) == 0 &&
143 inf
.raid_disks
== 0) {
144 /* yep, looks like a container */
146 rv
= st
->ss
->load_super(st
, fd
,
151 st
= guess_super(fd
);
152 if (st
&& !(rv
= st
->ss
->
162 if (have_container
) {
167 if (subdevs
> raiddisks
+sparedisks
) {
168 fprintf(stderr
, Name
": You have listed more devices (%d) than are in the array(%d)!\n", subdevs
, raiddisks
+sparedisks
);
171 if (!have_container
&& subdevs
< raiddisks
+sparedisks
) {
172 fprintf(stderr
, Name
": You haven't given enough devices (real or missing) to create this array\n");
175 if (bitmap_file
&& level
<= 0) {
176 fprintf(stderr
, Name
": bitmaps not meaningful with level %s\n",
177 map_num(pers
, level
)?:"given");
181 /* now set some defaults */
184 default: /* no layout */
188 layout
= 0x102; /* near=2, far=1 */
191 Name
": layout defaults to n1\n");
195 layout
= map_name(r5layout
, "default");
198 Name
": layout defaults to %s\n", map_num(r5layout
, layout
));
201 layout
= map_name(faultylayout
, "default");
205 Name
": layout defaults to %s\n", map_num(faultylayout
, layout
));
210 /* check layout fits in array*/
211 if ((layout
&255) * ((layout
>>8)&255) > raiddisks
) {
212 fprintf(stderr
, Name
": that layout requires at least %d devices\n",
213 (layout
&255) * ((layout
>>8)&255));
223 case LEVEL_LINEAR
: /* linear */
227 fprintf(stderr
, Name
": chunk size defaults to 64K\n");
232 case LEVEL_MULTIPATH
:
233 case LEVEL_CONTAINER
:
237 fprintf(stderr
, Name
": chunk size ignored for this level\n");
241 fprintf(stderr
, Name
": unknown level %d\n", level
);
245 if (st
&& ! st
->ss
->validate_geometry(st
, level
, layout
, raiddisks
,
246 chunk
, size
, NULL
, NULL
))
249 /* now look at the subdevs */
250 info
.array
.active_disks
= 0;
251 info
.array
.working_disks
= 0;
253 for (dv
=devlist
; dv
; dv
=dv
->next
, dnum
++) {
254 char *dname
= dv
->devname
;
255 unsigned long long freesize
;
256 if (strcasecmp(dname
, "missing")==0) {
257 if (first_missing
> dnum
)
258 first_missing
= dnum
;
259 if (second_missing
> dnum
&& dnum
> first_missing
)
260 second_missing
= dnum
;
264 info
.array
.working_disks
++;
265 if (dnum
< raiddisks
)
266 info
.array
.active_disks
++;
268 struct createinfo
*ci
= conf_get_create_info();
273 /* Need to choose a default metadata, which is different
274 * depending on geometry of array.
277 char *name
= "default";
278 for(i
=0; !st
&& superlist
[i
]; i
++) {
279 st
= superlist
[i
]->match_metadata_desc(name
);
280 if (st
&& !st
->ss
->validate_geometry
281 (st
, level
, layout
, raiddisks
,
282 chunk
, size
, dname
, &freesize
))
287 fprintf(stderr
, Name
": device %s not suitable "
288 "for any style of array\n",
292 if (st
->ss
->major
!= 0 ||
293 st
->minor_version
!= 90)
294 fprintf(stderr
, Name
": Defaulting to version"
299 if (!st
->ss
->validate_geometry(st
, level
, layout
,
305 Name
": %s is not suitable for "
313 freesize
/= 2; /* convert to K */
315 /* round to chunk size */
316 freesize
= freesize
& ~(chunk
-1);
319 if (size
&& freesize
< size
) {
320 fprintf(stderr
, Name
": %s is smaller that given size."
321 " %lluK < %lluK + metadata\n",
322 dname
, freesize
, size
);
326 if (maxdisc
== NULL
|| (maxdisc
&& freesize
> maxsize
)) {
330 if (mindisc
==NULL
|| (mindisc
&& freesize
< minsize
)) {
334 if (runstop
!= 1 || verbose
>= 0) {
335 int fd
= open(dname
, O_RDONLY
, 0);
337 fprintf(stderr
, Name
": Cannot open %s: %s\n",
338 dname
, strerror(errno
));
342 warn
|= check_ext2(fd
, dname
);
343 warn
|= check_reiser(fd
, dname
);
344 warn
|= check_raid(fd
, dname
);
349 fprintf(stderr
, Name
": create aborted\n");
353 if (mindisc
== NULL
&& !have_container
) {
354 fprintf(stderr
, Name
": no size and no drives given - aborting create.\n");
357 if (level
> 0 || level
== LEVEL_MULTIPATH
358 || level
== LEVEL_FAULTY
359 || (st
&& st
->ss
->external
) ) {
360 /* size is meaningful */
361 if (minsize
> 0x100000000ULL
&& st
->ss
->major
== 0) {
362 fprintf(stderr
, Name
": devices too large for RAID level %d\n", level
);
367 fprintf(stderr
, Name
": size set to %lluK\n", size
);
370 if (level
> 0 && ((maxsize
-size
)*100 > maxsize
)) {
371 if (runstop
!= 1 || verbose
>= 0)
372 fprintf(stderr
, Name
": largest drive (%s) exceed size (%lluK) by more than 1%%\n",
379 if (!ask("Continue creating array? ")) {
380 fprintf(stderr
, Name
": create aborted.\n");
385 fprintf(stderr
, Name
": creation continuing despite oddities due to --run\n");
389 /* If this is raid4/5, we want to configure the last active slot
390 * as missing, so that a reconstruct happens (faster than re-parity)
391 * FIX: Can we do this for raid6 as well?
393 if (assume_clean
==0 && force
== 0 && first_missing
>= raiddisks
) {
397 insert_point
= raiddisks
-1;
399 info
.array
.active_disks
--;
406 /* For raid6, if creating with 1 missing drive, make a good drive
407 * into a spare, else the create will fail
409 if (assume_clean
== 0 && force
== 0 && first_missing
< raiddisks
&&
410 second_missing
>= raiddisks
&& level
== 6) {
411 insert_point
= raiddisks
- 1;
412 if (insert_point
== first_missing
)
415 info
.array
.active_disks
--;
419 if (level
<= 0 && first_missing
< subdevs
* 2) {
421 Name
": This level does not support missing devices\n");
425 /* Ok, lets try some ioctls */
427 info
.array
.level
= level
;
428 info
.array
.size
= size
;
429 info
.array
.raid_disks
= raiddisks
;
430 /* The kernel should *know* what md_minor we are dealing
431 * with, but it chooses to trust me instead. Sigh
433 info
.array
.md_minor
= 0;
434 if (fstat(mdfd
, &stb
)==0)
435 info
.array
.md_minor
= minor(stb
.st_rdev
);
436 info
.array
.not_persistent
= 0;
438 if ( ( (level
== 4 || level
== 5) &&
439 (insert_point
< raiddisks
|| first_missing
< raiddisks
) )
441 ( level
== 6 && (insert_point
< raiddisks
442 || second_missing
< raiddisks
))
446 info
.array
.state
= 1; /* clean, but one+ drive will be missing*/
448 info
.array
.state
= 0; /* not clean, but no errors */
451 /* for raid10, the bitmap size is the capacity of the array,
452 * which is array.size * raid_disks / ncopies;
453 * .. but convert to sectors.
455 int ncopies
= ((layout
>>8) & 255) * (layout
& 255);
456 bitmapsize
= (unsigned long long)size
* raiddisks
/ ncopies
* 2;
457 /* printf("bms=%llu as=%d rd=%d nc=%d\n", bitmapsize, size, raiddisks, ncopies);*/
459 bitmapsize
= (unsigned long long)size
* 2;
461 /* There is lots of redundancy in these disk counts,
462 * raid_disks is the most meaningful value
463 * it describes the geometry of the array
465 * nr_disks is total number of used slots.
466 * it should be raid_disks+spare_disks
467 * spare_disks is the number of extra disks present
469 * active_disks is the number of working disks in
470 * active slots. (With raid_disks)
471 * working_disks is the total number of working disks,
473 * failed_disks is the number of disks marked failed
475 * Ideally, the kernel would keep these (except raid_disks)
476 * up-to-date as we ADD_NEW_DISK, but it doesn't (yet).
477 * So for now, we assume that all raid and spare
478 * devices will be given.
480 info
.array
.spare_disks
=sparedisks
;
481 info
.array
.failed_disks
=missing_disks
;
482 info
.array
.nr_disks
= info
.array
.working_disks
483 + info
.array
.failed_disks
;
484 info
.array
.layout
= layout
;
485 info
.array
.chunk_size
= chunk
*1024;
486 info
.array
.major_version
= st
->ss
->major
;
488 if (name
== NULL
|| *name
== 0) {
489 /* base name on mddev */
494 * /dev/md/home -> home
495 * /dev/mdhome -> home
497 name
= strrchr(mddev
, '/');
500 if (strncmp(name
, "md_d", 4)==0 &&
503 (name
-mddev
) == 5 /* /dev/ */)
505 else if (strncmp(name
, "md", 2)==0 &&
508 (name
-mddev
) == 5 /* /dev/ */)
512 if (!st
->ss
->init_super(st
, &info
.array
, size
, name
, homehost
, uuid
))
515 if (bitmap_file
&& vers
< 9003) {
516 major_num
= BITMAP_MAJOR_HOSTENDIAN
;
518 fprintf(stderr
, Name
": Warning - bitmaps created on this kernel are not portable\n"
519 " between different architectured. Consider upgrading the Linux kernel.\n");
523 if (bitmap_file
&& strcmp(bitmap_file
, "internal")==0) {
524 if ((vers
%100) < 2) {
525 fprintf(stderr
, Name
": internal bitmaps not supported by this kernel.\n");
528 if (!st
->ss
->add_internal_bitmap(st
, &bitmap_chunk
,
530 bitmapsize
, 1, major_num
)) {
531 fprintf(stderr
, Name
": Given bitmap chunk size not supported.\n");
538 sra
= sysfs_read(mdfd
, 0, 0);
540 if (st
->ss
->external
) {
542 if (st
->ss
->external
== 1)
544 strcat(strcpy(ver
, "external:"), st
->ss
->text_version
);
547 sprintf(ver
, "external:/%s/%d",
548 devnum2devname(st
->container_dev
),
549 st
->container_member
);
550 /* When creating a member, we need to be careful
551 * to negotiate with mdmon properly.
552 * If it is already running, we cannot write to
553 * the devices and must ask it to do that part.
554 * If it isn't running, we write to the devices,
556 * We hold an exclusive open on the container
557 * device to make sure mdmon doesn't exit after
558 * we checked that it is running.
560 * For now, fail if it is already running.
562 container_fd
= open_dev_excl(st
->container_dev
);
563 if (container_fd
< 0) {
564 fprintf(stderr
, Name
": Cannot get exclusive "
565 "open on container - weird.\n");
568 if (mdmon_running(st
->container_dev
)) {
569 fprintf(stderr
, Name
": mdmon already running "
571 devnum2devname(st
->container_dev
));
576 if ((vers
% 100) < 2 ||
578 sysfs_set_str(sra
, NULL
, "metadata_version",
580 fprintf(stderr
, Name
": This kernel does not "
581 "support external metadata.\n");
584 rv
= sysfs_set_array(sra
, &info
);
585 } else if ((vers
% 100) >= 1) { /* can use different versions */
586 mdu_array_info_t inf
;
587 memset(&inf
, 0, sizeof(inf
));
588 inf
.major_version
= st
->ss
->major
;
589 inf
.minor_version
= st
->minor_version
;
590 rv
= ioctl(mdfd
, SET_ARRAY_INFO
, &inf
);
592 rv
= ioctl(mdfd
, SET_ARRAY_INFO
, NULL
);
594 fprintf(stderr
, Name
": SET_ARRAY_INFO failed for %s: %s\n",
595 mddev
, strerror(errno
));
602 st
->ss
->uuid_from_super(st
, uuid
);
603 if (CreateBitmap(bitmap_file
, force
, (char*)uuid
, bitmap_chunk
,
609 bitmap_fd
= open(bitmap_file
, O_RDWR
);
611 fprintf(stderr
, Name
": weird: %s cannot be openned\n",
615 if (ioctl(mdfd
, SET_BITMAP_FILE
, bitmap_fd
) < 0) {
616 fprintf(stderr
, Name
": Cannot set bitmap file for %s: %s\n",
617 mddev
, strerror(errno
));
623 for (pass
=1; pass
<=2 ; pass
++) {
624 mddev_dev_t moved_disk
= NULL
; /* the disk that was moved out of the insert point */
626 for (dnum
=0, dv
= devlist
; dv
;
627 dv
=(dv
->next
)?(dv
->next
):moved_disk
, dnum
++) {
631 info
.disk
.number
= dnum
;
632 if (dnum
== insert_point
) {
635 info
.disk
.raid_disk
= info
.disk
.number
;
636 if (info
.disk
.raid_disk
< raiddisks
)
637 info
.disk
.state
= (1<<MD_DISK_ACTIVE
) |
642 info
.disk
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
644 if (dnum
== insert_point
||
645 strcasecmp(dv
->devname
, "missing")==0)
648 if (st
->ss
->external
== 2)
649 fd
= open(dv
->devname
, O_RDWR
, 0);
651 fd
= open(dv
->devname
, O_RDWR
|O_EXCL
,0);
654 fprintf(stderr
, Name
": failed to open %s "
655 "after earlier success - aborting\n",
660 info
.disk
.major
= major(stb
.st_rdev
);
661 info
.disk
.minor
= minor(stb
.st_rdev
);
665 remove_partitions(fd
);
666 st
->ss
->add_to_super(st
, &info
.disk
,
671 info
.component_size
= info
.array
.size
* 2;
675 if (st
->ss
->external
) {
676 st
->ss
->getinfo_super_n(st
, &info
);
677 rv
= sysfs_add_disk(sra
, &info
);
679 rv
= ioctl(mdfd
, ADD_NEW_DISK
,
684 Name
": ADD_NEW_DISK for %s "
686 dv
->devname
, strerror(errno
));
687 st
->ss
->free_super(st
);
692 if (dv
== moved_disk
&& dnum
!= insert_point
) break;
695 st
->ss
->write_init_super(st
);
697 st
->ss
->free_super(st
);
699 /* param is not actually used */
700 if (level
== LEVEL_CONTAINER
)
701 /* No need to start */
703 else if (runstop
== 1 || subdevs
>= raiddisks
) {
704 if (st
->ss
->external
) {
707 case LEVEL_MULTIPATH
:
709 sysfs_set_str(sra
, NULL
, "array_state",
714 sysfs_set_str(sra
, NULL
, "array_state",
720 if (ioctl(mdfd
, RUN_ARRAY
, ¶m
)) {
721 fprintf(stderr
, Name
": RUN_ARRAY failed: %s\n",
723 Manage_runstop(mddev
, mdfd
, -1, 0);
728 fprintf(stderr
, Name
": array %s started.\n", mddev
);
729 if (st
->ss
->external
== 2) {
731 int dn
= st
->container_dev
;
735 /* FIXME yuk. CLOSE_EXEC?? */
736 for (i
=3; i
< 100; i
++)
738 execl("./mdmon", "mdmon",
739 map_dev(dev2major(dn
),
743 case -1: fprintf(stderr
, Name
": cannot fork. "
744 "Array remains readonly\n");
746 default: ; /* parent - good */
749 signal_mdmon(st
->container_dev
);
750 /* FIXME wait for mdmon to set array to read-auto */
755 fprintf(stderr
, Name
": not starting array - not enough devices.\n");