]>
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 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;
74 unsigned long long bitmapsize
;
78 int major_num
= BITMAP_MAJOR_HI
;
80 memset(&info
, 0, sizeof(info
));
82 vers
= md_get_version(mdfd
);
84 fprintf(stderr
, Name
": Create requires md driver version 0.90.0 or later\n");
88 memset(&inf
, 0, sizeof(inf
));
89 ioctl(mdfd
, GET_ARRAY_INFO
, &inf
);
90 if (inf
.working_disks
!= 0) {
91 fprintf(stderr
, Name
": another array by this name"
92 " is already running.\n");
97 /* "ddf" and "imsm" metadata only supports one level - should possibly
98 * push this into metadata handler??
100 if (st
&& (st
->ss
== &super_ddf
|| st
->ss
== &super_imsm
))
101 level
= LEVEL_CONTAINER
;
104 if (level
== UnSet
) {
106 Name
": a RAID level is needed to create an array.\n");
109 if (raiddisks
< 4 && level
== 6) {
111 Name
": at least 4 raid-devices needed for level 6\n");
114 if (raiddisks
> 256 && level
== 6) {
116 Name
": no more than 256 raid-devices supported for level 6\n");
119 if (raiddisks
< 2 && level
>= 4) {
121 Name
": at least 2 raid-devices needed for level 4 or 5\n");
124 if (level
<= 0 && sparedisks
) {
126 Name
": This level does not support spare devices\n");
130 if (subdevs
== 1 && strcmp(devlist
->devname
, "missing") != 0) {
131 /* If given a single device, it might be a container, and we can
132 * extract a device list from there
134 mdu_array_info_t inf
;
137 memset(&inf
, 0, sizeof(inf
));
138 fd
= open(devlist
->devname
, O_RDONLY
, 0);
140 ioctl(fd
, GET_ARRAY_INFO
, &inf
) == 0 &&
141 inf
.raid_disks
== 0) {
142 /* yep, looks like a container */
144 rv
= st
->ss
->load_super(st
, fd
,
149 st
= guess_super(fd
);
150 if (st
&& !(rv
= st
->ss
->
160 if (have_container
) {
165 if (subdevs
> raiddisks
+sparedisks
) {
166 fprintf(stderr
, Name
": You have listed more devices (%d) than are in the array(%d)!\n", subdevs
, raiddisks
+sparedisks
);
169 if (!have_container
&& subdevs
< raiddisks
+sparedisks
) {
170 fprintf(stderr
, Name
": You haven't given enough devices (real or missing) to create this array\n");
173 if (bitmap_file
&& level
<= 0) {
174 fprintf(stderr
, Name
": bitmaps not meaningful with level %s\n",
175 map_num(pers
, level
)?:"given");
179 /* now set some defaults */
182 default: /* no layout */
186 layout
= 0x102; /* near=2, far=1 */
189 Name
": layout defaults to n1\n");
193 layout
= map_name(r5layout
, "default");
196 Name
": layout defaults to %s\n", map_num(r5layout
, layout
));
199 layout
= map_name(faultylayout
, "default");
203 Name
": layout defaults to %s\n", map_num(faultylayout
, layout
));
208 /* check layout fits in array*/
209 if ((layout
&255) * ((layout
>>8)&255) > raiddisks
) {
210 fprintf(stderr
, Name
": that layout requires at least %d devices\n",
211 (layout
&255) * ((layout
>>8)&255));
221 case LEVEL_LINEAR
: /* linear */
225 fprintf(stderr
, Name
": chunk size defaults to 64K\n");
230 case LEVEL_MULTIPATH
:
231 case LEVEL_CONTAINER
:
235 fprintf(stderr
, Name
": chunk size ignored for this level\n");
239 fprintf(stderr
, Name
": unknown level %d\n", level
);
243 if (st
&& ! st
->ss
->validate_geometry(st
, level
, layout
, raiddisks
,
244 chunk
, size
, NULL
, NULL
))
247 /* now look at the subdevs */
248 info
.array
.active_disks
= 0;
249 info
.array
.working_disks
= 0;
251 for (dv
=devlist
; dv
; dv
=dv
->next
, dnum
++) {
252 char *dname
= dv
->devname
;
253 unsigned long long freesize
;
254 if (strcasecmp(dname
, "missing")==0) {
255 if (first_missing
> dnum
)
256 first_missing
= dnum
;
257 if (second_missing
> dnum
&& dnum
> first_missing
)
258 second_missing
= dnum
;
262 info
.array
.working_disks
++;
263 if (dnum
< raiddisks
)
264 info
.array
.active_disks
++;
266 struct createinfo
*ci
= conf_get_create_info();
271 /* Need to choose a default metadata, which is different
272 * depending on geometry of array.
275 char *name
= "default";
276 for(i
=0; !st
&& superlist
[i
]; i
++) {
277 st
= superlist
[i
]->match_metadata_desc(name
);
278 if (st
&& !st
->ss
->validate_geometry
279 (st
, level
, layout
, raiddisks
,
280 chunk
, size
, dname
, &freesize
))
285 fprintf(stderr
, Name
": device %s not suitable "
286 "for any style of array\n",
290 if (st
->ss
->major
!= 0 ||
291 st
->minor_version
!= 90)
292 fprintf(stderr
, Name
": Defaulting to version"
297 if (!st
->ss
->validate_geometry(st
, level
, layout
,
303 Name
": %s is not suitable for "
311 freesize
/= 2; /* convert to K */
313 /* round to chunk size */
314 freesize
= freesize
& ~(chunk
-1);
317 if (size
&& freesize
< size
) {
318 fprintf(stderr
, Name
": %s is smaller that given size."
319 " %lluK < %lluK + metadata\n",
320 dname
, freesize
, size
);
324 if (maxdisc
== NULL
|| (maxdisc
&& freesize
> maxsize
)) {
328 if (mindisc
==NULL
|| (mindisc
&& freesize
< minsize
)) {
332 if (runstop
!= 1 || verbose
>= 0) {
333 int fd
= open(dname
, O_RDONLY
, 0);
335 fprintf(stderr
, Name
": Cannot open %s: %s\n",
336 dname
, strerror(errno
));
340 warn
|= check_ext2(fd
, dname
);
341 warn
|= check_reiser(fd
, dname
);
342 warn
|= check_raid(fd
, dname
);
347 fprintf(stderr
, Name
": create aborted\n");
351 if (mindisc
== NULL
&& !have_container
) {
352 fprintf(stderr
, Name
": no size and no drives given - aborting create.\n");
355 if (level
> 0 || level
== LEVEL_MULTIPATH
356 || level
== LEVEL_FAULTY
357 || (st
&& st
->ss
->external
) ) {
358 /* size is meaningful */
359 if (minsize
> 0x100000000ULL
&& st
->ss
->major
== 0) {
360 fprintf(stderr
, Name
": devices too large for RAID level %d\n", level
);
365 fprintf(stderr
, Name
": size set to %lluK\n", size
);
368 if (level
> 0 && ((maxsize
-size
)*100 > maxsize
)) {
369 if (runstop
!= 1 || verbose
>= 0)
370 fprintf(stderr
, Name
": largest drive (%s) exceed size (%lluK) by more than 1%%\n",
377 if (!ask("Continue creating array? ")) {
378 fprintf(stderr
, Name
": create aborted.\n");
383 fprintf(stderr
, Name
": creation continuing despite oddities due to --run\n");
387 /* If this is raid4/5, we want to configure the last active slot
388 * as missing, so that a reconstruct happens (faster than re-parity)
389 * FIX: Can we do this for raid6 as well?
391 if (assume_clean
==0 && force
== 0 && first_missing
>= raiddisks
) {
395 insert_point
= raiddisks
-1;
397 info
.array
.active_disks
--;
404 /* For raid6, if creating with 1 missing drive, make a good drive
405 * into a spare, else the create will fail
407 if (assume_clean
== 0 && force
== 0 && first_missing
< raiddisks
&&
408 second_missing
>= raiddisks
&& level
== 6) {
409 insert_point
= raiddisks
- 1;
410 if (insert_point
== first_missing
)
413 info
.array
.active_disks
--;
417 if (level
<= 0 && first_missing
< subdevs
* 2) {
419 Name
": This level does not support missing devices\n");
423 /* Ok, lets try some ioctls */
425 info
.array
.level
= level
;
426 info
.array
.size
= size
;
427 info
.array
.raid_disks
= raiddisks
;
428 /* The kernel should *know* what md_minor we are dealing
429 * with, but it chooses to trust me instead. Sigh
431 info
.array
.md_minor
= 0;
432 if (fstat(mdfd
, &stb
)==0)
433 info
.array
.md_minor
= minor(stb
.st_rdev
);
434 info
.array
.not_persistent
= 0;
436 if ( ( (level
== 4 || level
== 5) &&
437 (insert_point
< raiddisks
|| first_missing
< raiddisks
) )
439 ( level
== 6 && (insert_point
< raiddisks
440 || second_missing
< raiddisks
))
444 info
.array
.state
= 1; /* clean, but one+ drive will be missing*/
446 info
.array
.state
= 0; /* not clean, but no errors */
449 /* for raid10, the bitmap size is the capacity of the array,
450 * which is array.size * raid_disks / ncopies;
451 * .. but convert to sectors.
453 int ncopies
= ((layout
>>8) & 255) * (layout
& 255);
454 bitmapsize
= (unsigned long long)size
* raiddisks
/ ncopies
* 2;
455 /* printf("bms=%llu as=%d rd=%d nc=%d\n", bitmapsize, size, raiddisks, ncopies);*/
457 bitmapsize
= (unsigned long long)size
* 2;
459 /* There is lots of redundancy in these disk counts,
460 * raid_disks is the most meaningful value
461 * it describes the geometry of the array
463 * nr_disks is total number of used slots.
464 * it should be raid_disks+spare_disks
465 * spare_disks is the number of extra disks present
467 * active_disks is the number of working disks in
468 * active slots. (With raid_disks)
469 * working_disks is the total number of working disks,
471 * failed_disks is the number of disks marked failed
473 * Ideally, the kernel would keep these (except raid_disks)
474 * up-to-date as we ADD_NEW_DISK, but it doesn't (yet).
475 * So for now, we assume that all raid and spare
476 * devices will be given.
478 info
.array
.spare_disks
=sparedisks
;
479 info
.array
.failed_disks
=missing_disks
;
480 info
.array
.nr_disks
= info
.array
.working_disks
481 + info
.array
.failed_disks
;
482 info
.array
.layout
= layout
;
483 info
.array
.chunk_size
= chunk
*1024;
484 info
.array
.major_version
= st
->ss
->major
;
486 if (name
== NULL
|| *name
== 0) {
487 /* base name on mddev */
492 * /dev/md/home -> home
493 * /dev/mdhome -> home
495 name
= strrchr(mddev
, '/');
498 if (strncmp(name
, "md_d", 4)==0 &&
501 (name
-mddev
) == 5 /* /dev/ */)
503 else if (strncmp(name
, "md", 2)==0 &&
506 (name
-mddev
) == 5 /* /dev/ */)
510 if (!st
->ss
->init_super(st
, &info
.array
, size
, name
, homehost
, uuid
))
513 if (bitmap_file
&& vers
< 9003) {
514 major_num
= BITMAP_MAJOR_HOSTENDIAN
;
516 fprintf(stderr
, Name
": Warning - bitmaps created on this kernel are not portable\n"
517 " between different architectured. Consider upgrading the Linux kernel.\n");
521 if (bitmap_file
&& strcmp(bitmap_file
, "internal")==0) {
522 if ((vers
%100) < 2) {
523 fprintf(stderr
, Name
": internal bitmaps not supported by this kernel.\n");
526 if (!st
->ss
->add_internal_bitmap(st
, &bitmap_chunk
,
528 bitmapsize
, 1, major_num
)) {
529 fprintf(stderr
, Name
": Given bitmap chunk size not supported.\n");
536 sra
= sysfs_read(mdfd
, 0, 0);
538 if (st
->ss
->external
) {
540 if (st
->ss
->external
== 1)
542 strcat(strcpy(ver
, "external:"), st
->ss
->text_version
);
545 sprintf(ver
, "external:/%s/%d",
546 devnum2devname(st
->container_dev
),
547 st
->container_member
);
549 if ((vers
% 100) < 2 ||
551 sysfs_set_str(sra
, NULL
, "metadata_version",
553 fprintf(stderr
, Name
": This kernel does not "
554 "support external metadata.\n");
557 rv
= sysfs_set_array(sra
, &info
);
558 } else if ((vers
% 100) >= 1) { /* can use different versions */
559 mdu_array_info_t inf
;
560 memset(&inf
, 0, sizeof(inf
));
561 inf
.major_version
= st
->ss
->major
;
562 inf
.minor_version
= st
->minor_version
;
563 rv
= ioctl(mdfd
, SET_ARRAY_INFO
, &inf
);
565 rv
= ioctl(mdfd
, SET_ARRAY_INFO
, NULL
);
567 fprintf(stderr
, Name
": SET_ARRAY_INFO failed for %s: %s\n",
568 mddev
, strerror(errno
));
575 st
->ss
->uuid_from_super(st
, uuid
);
576 if (CreateBitmap(bitmap_file
, force
, (char*)uuid
, bitmap_chunk
,
582 bitmap_fd
= open(bitmap_file
, O_RDWR
);
584 fprintf(stderr
, Name
": weird: %s cannot be openned\n",
588 if (ioctl(mdfd
, SET_BITMAP_FILE
, bitmap_fd
) < 0) {
589 fprintf(stderr
, Name
": Cannot set bitmap file for %s: %s\n",
590 mddev
, strerror(errno
));
596 for (pass
=1; pass
<=2 ; pass
++) {
597 mddev_dev_t moved_disk
= NULL
; /* the disk that was moved out of the insert point */
599 for (dnum
=0, dv
= devlist
; dv
;
600 dv
=(dv
->next
)?(dv
->next
):moved_disk
, dnum
++) {
604 info
.disk
.number
= dnum
;
605 if (dnum
== insert_point
) {
608 info
.disk
.raid_disk
= info
.disk
.number
;
609 if (info
.disk
.raid_disk
< raiddisks
)
610 info
.disk
.state
= (1<<MD_DISK_ACTIVE
) |
615 info
.disk
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
617 if (dnum
== insert_point
||
618 strcasecmp(dv
->devname
, "missing")==0)
621 if (st
->ss
->external
== 2)
622 fd
= open(dv
->devname
, O_RDWR
, 0);
624 fd
= open(dv
->devname
, O_RDWR
|O_EXCL
,0);
627 fprintf(stderr
, Name
": failed to open %s "
628 "after earlier success - aborting\n",
633 info
.disk
.major
= major(stb
.st_rdev
);
634 info
.disk
.minor
= minor(stb
.st_rdev
);
638 remove_partitions(fd
);
639 st
->ss
->add_to_super(st
, &info
.disk
,
644 info
.component_size
= info
.array
.size
* 2;
648 if (st
->ss
->external
) {
649 st
->ss
->getinfo_super_n(st
, &info
);
650 rv
= sysfs_add_disk(sra
, &info
);
652 rv
= ioctl(mdfd
, ADD_NEW_DISK
,
657 Name
": ADD_NEW_DISK for %s "
659 dv
->devname
, strerror(errno
));
660 st
->ss
->free_super(st
);
665 if (dv
== moved_disk
&& dnum
!= insert_point
) break;
668 st
->ss
->write_init_super(st
);
670 st
->ss
->free_super(st
);
672 /* param is not actually used */
673 if (level
== LEVEL_CONTAINER
)
674 /* No need to start */
676 else if (runstop
== 1 || subdevs
>= raiddisks
) {
677 if (st
->ss
->external
) {
680 case LEVEL_MULTIPATH
:
682 sysfs_set_str(sra
, NULL
, "array_state",
686 sysfs_set_str(sra
, NULL
, "array_state",
692 if (ioctl(mdfd
, RUN_ARRAY
, ¶m
)) {
693 fprintf(stderr
, Name
": RUN_ARRAY failed: %s\n",
695 Manage_runstop(mddev
, mdfd
, -1, 0);
700 fprintf(stderr
, Name
": array %s started.\n", mddev
);
702 fprintf(stderr
, Name
": not starting array - not enough devices.\n");