]>
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;
76 unsigned long long bitmapsize
;
81 int major_num
= BITMAP_MAJOR_HI
;
83 memset(&info
, 0, sizeof(info
));
85 vers
= md_get_version(mdfd
);
87 fprintf(stderr
, Name
": Create requires md driver version 0.90.0 or later\n");
91 memset(&inf
, 0, sizeof(inf
));
92 ioctl(mdfd
, GET_ARRAY_INFO
, &inf
);
93 if (inf
.working_disks
!= 0) {
94 fprintf(stderr
, Name
": another array by this name"
95 " is already running.\n");
100 /* "ddf" and "imsm" metadata only supports one level - should possibly
101 * push this into metadata handler??
103 if (st
&& (st
->ss
== &super_ddf
|| st
->ss
== &super_imsm
))
104 level
= LEVEL_CONTAINER
;
107 if (level
== UnSet
) {
109 Name
": a RAID level is needed to create an array.\n");
112 if (raiddisks
< 4 && level
== 6) {
114 Name
": at least 4 raid-devices needed for level 6\n");
117 if (raiddisks
> 256 && level
== 6) {
119 Name
": no more than 256 raid-devices supported for level 6\n");
122 if (raiddisks
< 2 && level
>= 4) {
124 Name
": at least 2 raid-devices needed for level 4 or 5\n");
127 if (level
<= 0 && sparedisks
) {
129 Name
": This level does not support spare devices\n");
133 if (subdevs
== 1 && strcmp(devlist
->devname
, "missing") != 0) {
134 /* If given a single device, it might be a container, and we can
135 * extract a device list from there
137 mdu_array_info_t inf
;
140 memset(&inf
, 0, sizeof(inf
));
141 fd
= open(devlist
->devname
, O_RDONLY
, 0);
143 ioctl(fd
, GET_ARRAY_INFO
, &inf
) == 0 &&
144 inf
.raid_disks
== 0) {
145 /* yep, looks like a container */
147 rv
= st
->ss
->load_super(st
, fd
,
152 st
= guess_super(fd
);
153 if (st
&& !(rv
= st
->ss
->
163 if (have_container
) {
168 if (subdevs
> raiddisks
+sparedisks
) {
169 fprintf(stderr
, Name
": You have listed more devices (%d) than are in the array(%d)!\n", subdevs
, raiddisks
+sparedisks
);
172 if (!have_container
&& subdevs
< raiddisks
+sparedisks
) {
173 fprintf(stderr
, Name
": You haven't given enough devices (real or missing) to create this array\n");
176 if (bitmap_file
&& level
<= 0) {
177 fprintf(stderr
, Name
": bitmaps not meaningful with level %s\n",
178 map_num(pers
, level
)?:"given");
182 /* now set some defaults */
185 default: /* no layout */
189 layout
= 0x102; /* near=2, far=1 */
192 Name
": layout defaults to n1\n");
196 layout
= map_name(r5layout
, "default");
199 Name
": layout defaults to %s\n", map_num(r5layout
, layout
));
202 layout
= map_name(faultylayout
, "default");
206 Name
": layout defaults to %s\n", map_num(faultylayout
, layout
));
211 /* check layout fits in array*/
212 if ((layout
&255) * ((layout
>>8)&255) > raiddisks
) {
213 fprintf(stderr
, Name
": that layout requires at least %d devices\n",
214 (layout
&255) * ((layout
>>8)&255));
224 case LEVEL_LINEAR
: /* linear */
228 fprintf(stderr
, Name
": chunk size defaults to 64K\n");
233 case LEVEL_MULTIPATH
:
234 case LEVEL_CONTAINER
:
238 fprintf(stderr
, Name
": chunk size ignored for this level\n");
242 fprintf(stderr
, Name
": unknown level %d\n", level
);
246 if (st
&& ! st
->ss
->validate_geometry(st
, level
, layout
, raiddisks
,
247 chunk
, size
, NULL
, NULL
))
250 /* now look at the subdevs */
251 info
.array
.active_disks
= 0;
252 info
.array
.working_disks
= 0;
254 for (dv
=devlist
; dv
; dv
=dv
->next
, dnum
++) {
255 char *dname
= dv
->devname
;
256 unsigned long long freesize
;
257 if (strcasecmp(dname
, "missing")==0) {
258 if (first_missing
> dnum
)
259 first_missing
= dnum
;
260 if (second_missing
> dnum
&& dnum
> first_missing
)
261 second_missing
= dnum
;
265 info
.array
.working_disks
++;
266 if (dnum
< raiddisks
)
267 info
.array
.active_disks
++;
269 struct createinfo
*ci
= conf_get_create_info();
274 /* Need to choose a default metadata, which is different
275 * depending on geometry of array.
278 char *name
= "default";
279 for(i
=0; !st
&& superlist
[i
]; i
++) {
280 st
= superlist
[i
]->match_metadata_desc(name
);
281 if (st
&& !st
->ss
->validate_geometry
282 (st
, level
, layout
, raiddisks
,
283 chunk
, size
, dname
, &freesize
))
288 fprintf(stderr
, Name
": device %s not suitable "
289 "for any style of array\n",
293 if (st
->ss
!= &super0
||
294 st
->minor_version
!= 90)
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
->ss
->external
) {
358 /* size is meaningful */
359 if (!st
->ss
->validate_geometry(st
, level
, layout
,
363 fprintf(stderr
, Name
": devices too large for RAID level %d\n", level
);
368 fprintf(stderr
, Name
": size set to %lluK\n", size
);
371 if (level
> 0 && ((maxsize
-size
)*100 > maxsize
)) {
372 if (runstop
!= 1 || verbose
>= 0)
373 fprintf(stderr
, Name
": largest drive (%s) exceed size (%lluK) by more than 1%%\n",
380 if (!ask("Continue creating array? ")) {
381 fprintf(stderr
, Name
": create aborted.\n");
386 fprintf(stderr
, Name
": creation continuing despite oddities due to --run\n");
390 /* If this is raid4/5, we want to configure the last active slot
391 * as missing, so that a reconstruct happens (faster than re-parity)
392 * FIX: Can we do this for raid6 as well?
394 if (assume_clean
==0 && force
== 0 && first_missing
>= raiddisks
) {
398 insert_point
= raiddisks
-1;
400 info
.array
.active_disks
--;
407 /* For raid6, if creating with 1 missing drive, make a good drive
408 * into a spare, else the create will fail
410 if (assume_clean
== 0 && force
== 0 && first_missing
< raiddisks
&&
411 second_missing
>= raiddisks
&& level
== 6) {
412 insert_point
= raiddisks
- 1;
413 if (insert_point
== first_missing
)
416 info
.array
.active_disks
--;
420 if (level
<= 0 && first_missing
< subdevs
* 2) {
422 Name
": This level does not support missing devices\n");
426 /* Ok, lets try some ioctls */
428 info
.array
.level
= level
;
429 info
.array
.size
= size
;
430 info
.array
.raid_disks
= raiddisks
;
431 /* The kernel should *know* what md_minor we are dealing
432 * with, but it chooses to trust me instead. Sigh
434 info
.array
.md_minor
= 0;
435 if (fstat(mdfd
, &stb
)==0)
436 info
.array
.md_minor
= minor(stb
.st_rdev
);
437 info
.array
.not_persistent
= 0;
439 if ( ( (level
== 4 || level
== 5) &&
440 (insert_point
< raiddisks
|| first_missing
< raiddisks
) )
442 ( level
== 6 && (insert_point
< raiddisks
443 || second_missing
< raiddisks
))
449 info
.array
.state
= 1; /* clean, but one+ drive will be missing*/
451 info
.array
.state
= 0; /* not clean, but no errors */
454 /* for raid10, the bitmap size is the capacity of the array,
455 * which is array.size * raid_disks / ncopies;
456 * .. but convert to sectors.
458 int ncopies
= ((layout
>>8) & 255) * (layout
& 255);
459 bitmapsize
= (unsigned long long)size
* raiddisks
/ ncopies
* 2;
460 /* printf("bms=%llu as=%d rd=%d nc=%d\n", bitmapsize, size, raiddisks, ncopies);*/
462 bitmapsize
= (unsigned long long)size
* 2;
464 /* There is lots of redundancy in these disk counts,
465 * raid_disks is the most meaningful value
466 * it describes the geometry of the array
468 * nr_disks is total number of used slots.
469 * it should be raid_disks+spare_disks
470 * spare_disks is the number of extra disks present
472 * active_disks is the number of working disks in
473 * active slots. (With raid_disks)
474 * working_disks is the total number of working disks,
476 * failed_disks is the number of disks marked failed
478 * Ideally, the kernel would keep these (except raid_disks)
479 * up-to-date as we ADD_NEW_DISK, but it doesn't (yet).
480 * So for now, we assume that all raid and spare
481 * devices will be given.
483 info
.array
.spare_disks
=sparedisks
;
484 info
.array
.failed_disks
=missing_disks
;
485 info
.array
.nr_disks
= info
.array
.working_disks
486 + info
.array
.failed_disks
;
487 info
.array
.layout
= layout
;
488 info
.array
.chunk_size
= chunk
*1024;
489 info
.array
.major_version
= 0; /* Flag to say "not zeroing superblock" */
491 if (name
== NULL
|| *name
== 0) {
492 /* base name on mddev */
497 * /dev/md/home -> home
498 * /dev/mdhome -> home
500 name
= strrchr(mddev
, '/');
503 if (strncmp(name
, "md_d", 4)==0 &&
506 (name
-mddev
) == 5 /* /dev/ */)
508 else if (strncmp(name
, "md", 2)==0 &&
511 (name
-mddev
) == 5 /* /dev/ */)
515 if (!st
->ss
->init_super(st
, &info
.array
, size
, name
, homehost
, uuid
))
518 st
->ss
->getinfo_super(st
, &info
);
521 fprintf(stderr
, Name
": Defaulting to version"
522 " %s metadata\n", info
.text_version
);
524 if (bitmap_file
&& vers
< 9003) {
525 major_num
= BITMAP_MAJOR_HOSTENDIAN
;
527 fprintf(stderr
, Name
": Warning - bitmaps created on this kernel are not portable\n"
528 " between different architectured. Consider upgrading the Linux kernel.\n");
532 if (bitmap_file
&& strcmp(bitmap_file
, "internal")==0) {
533 if ((vers
%100) < 2) {
534 fprintf(stderr
, Name
": internal bitmaps not supported by this kernel.\n");
537 if (!st
->ss
->add_internal_bitmap(st
, &bitmap_chunk
,
539 bitmapsize
, 1, major_num
)) {
540 fprintf(stderr
, Name
": Given bitmap chunk size not supported.\n");
547 sra
= sysfs_read(mdfd
, 0, 0);
549 if (st
->ss
->external
) {
551 strcat(strcpy(ver
, "external:"),
553 if (st
->ss
->external
== 2) {
556 /* When creating a member, we need to be careful
557 * to negotiate with mdmon properly.
558 * If it is already running, we cannot write to
559 * the devices and must ask it to do that part.
560 * If it isn't running, we write to the devices,
562 * We hold an exclusive open on the container
563 * device to make sure mdmon doesn't exit after
564 * we checked that it is running.
566 * For now, fail if it is already running.
568 container_fd
= open_dev_excl(st
->container_dev
);
569 if (container_fd
< 0) {
570 fprintf(stderr
, Name
": Cannot get exclusive "
571 "open on container - weird.\n");
574 if (mdmon_running(st
->container_dev
)) {
575 fprintf(stderr
, Name
": mdmon already running "
577 devnum2devname(st
->container_dev
));
582 if ((vers
% 100) < 2 ||
584 sysfs_set_str(sra
, NULL
, "metadata_version",
586 fprintf(stderr
, Name
": This kernel does not "
587 "support external metadata.\n");
590 rv
= sysfs_set_array(sra
, &info
);
591 } else if ((vers
% 100) >= 1) { /* can use different versions */
592 mdu_array_info_t inf
;
593 memset(&inf
, 0, sizeof(inf
));
594 inf
.major_version
= info
.array
.major_version
;
595 inf
.minor_version
= info
.array
.minor_version
;
596 rv
= ioctl(mdfd
, SET_ARRAY_INFO
, &inf
);
598 rv
= ioctl(mdfd
, SET_ARRAY_INFO
, NULL
);
600 fprintf(stderr
, Name
": SET_ARRAY_INFO failed for %s: %s\n",
601 mddev
, strerror(errno
));
608 st
->ss
->uuid_from_super(st
, uuid
);
609 if (CreateBitmap(bitmap_file
, force
, (char*)uuid
, bitmap_chunk
,
615 bitmap_fd
= open(bitmap_file
, O_RDWR
);
617 fprintf(stderr
, Name
": weird: %s cannot be openned\n",
621 if (ioctl(mdfd
, SET_BITMAP_FILE
, bitmap_fd
) < 0) {
622 fprintf(stderr
, Name
": Cannot set bitmap file for %s: %s\n",
623 mddev
, strerror(errno
));
629 for (pass
=1; pass
<=2 ; pass
++) {
630 mddev_dev_t moved_disk
= NULL
; /* the disk that was moved out of the insert point */
632 for (dnum
=0, dv
= devlist
; dv
;
633 dv
=(dv
->next
)?(dv
->next
):moved_disk
, dnum
++) {
637 info
.disk
.number
= dnum
;
638 if (dnum
== insert_point
) {
641 info
.disk
.raid_disk
= info
.disk
.number
;
642 if (info
.disk
.raid_disk
< raiddisks
)
643 info
.disk
.state
= (1<<MD_DISK_ACTIVE
) |
648 info
.disk
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
650 if (dnum
== insert_point
||
651 strcasecmp(dv
->devname
, "missing")==0)
654 if (st
->ss
->external
== 2)
655 fd
= open(dv
->devname
, O_RDWR
, 0);
657 fd
= open(dv
->devname
, O_RDWR
|O_EXCL
,0);
660 fprintf(stderr
, Name
": failed to open %s "
661 "after earlier success - aborting\n",
666 info
.disk
.major
= major(stb
.st_rdev
);
667 info
.disk
.minor
= minor(stb
.st_rdev
);
671 remove_partitions(fd
);
672 st
->ss
->add_to_super(st
, &info
.disk
,
677 info
.component_size
= info
.array
.size
* 2;
681 if (st
->ss
->external
) {
682 st
->ss
->getinfo_super_n(st
, &info
);
683 rv
= sysfs_add_disk(sra
, &info
);
685 rv
= ioctl(mdfd
, ADD_NEW_DISK
,
690 Name
": ADD_NEW_DISK for %s "
692 dv
->devname
, strerror(errno
));
693 st
->ss
->free_super(st
);
698 if (dv
== moved_disk
&& dnum
!= insert_point
) break;
701 st
->ss
->write_init_super(st
);
703 st
->ss
->free_super(st
);
705 /* param is not actually used */
706 if (level
== LEVEL_CONTAINER
)
707 /* No need to start */
709 else if (runstop
== 1 || subdevs
>= raiddisks
) {
710 if (st
->ss
->external
) {
713 case LEVEL_MULTIPATH
:
715 sysfs_set_str(sra
, NULL
, "array_state",
720 sysfs_set_str(sra
, NULL
, "array_state",
726 if (ioctl(mdfd
, RUN_ARRAY
, ¶m
)) {
727 fprintf(stderr
, Name
": RUN_ARRAY failed: %s\n",
729 Manage_runstop(mddev
, mdfd
, -1, 0);
734 fprintf(stderr
, Name
": array %s started.\n", mddev
);
735 if (st
->ss
->external
== 2) {
737 int dn
= st
->container_dev
;
741 /* FIXME yuk. CLOSE_EXEC?? */
742 for (i
=3; i
< 100; i
++)
744 execl("./mdmon", "mdmon",
745 map_dev(dev2major(dn
),
749 case -1: fprintf(stderr
, Name
": cannot fork. "
750 "Array remains readonly\n");
752 default: ; /* parent - good */
755 signal_mdmon(st
->container_dev
);
756 /* FIXME wait for mdmon to set array to read-auto */
761 fprintf(stderr
, Name
": not starting array - not enough devices.\n");