]>
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
,
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
, int autof
)
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,
59 unsigned long long minsize
=0, maxsize
=0;
66 int first_missing
= subdevs
* 2;
67 int second_missing
= subdevs
* 2;
68 int missing_disks
= 0;
69 int insert_point
= subdevs
* 2; /* where to insert a missing drive */
75 int have_container
= 0;
76 int container_fd
= -1;
78 unsigned long long bitmapsize
;
79 struct mdinfo info
, *infos
;
81 unsigned long safe_mode_delay
= 0;
83 int major_num
= BITMAP_MAJOR_HI
;
85 memset(&info
, 0, sizeof(info
));
88 /* "ddf" and "imsm" metadata only supports one level - should possibly
89 * push this into metadata handler??
91 if (st
&& (st
->ss
== &super_ddf
|| st
->ss
== &super_imsm
))
92 level
= LEVEL_CONTAINER
;
97 Name
": a RAID level is needed to create an array.\n");
100 if (raiddisks
< 4 && level
== 6) {
102 Name
": at least 4 raid-devices needed for level 6\n");
105 if (raiddisks
> 256 && level
== 6) {
107 Name
": no more than 256 raid-devices supported for level 6\n");
110 if (raiddisks
< 2 && level
>= 4) {
112 Name
": at least 2 raid-devices needed for level 4 or 5\n");
115 if (level
<= 0 && sparedisks
) {
117 Name
": This level does not support spare devices\n");
121 if (subdevs
== 1 && strcmp(devlist
->devname
, "missing") != 0) {
122 /* If given a single device, it might be a container, and we can
123 * extract a device list from there
125 mdu_array_info_t inf
;
128 memset(&inf
, 0, sizeof(inf
));
129 fd
= open(devlist
->devname
, O_RDONLY
);
131 ioctl(fd
, GET_ARRAY_INFO
, &inf
) == 0 &&
132 inf
.raid_disks
== 0) {
133 /* yep, looks like a container */
135 rv
= st
->ss
->load_super(st
, fd
,
140 st
= guess_super(fd
);
141 if (st
&& !(rv
= st
->ss
->
151 if (have_container
) {
156 if (st
&& st
->ss
->external
&& sparedisks
) {
158 Name
": This metadata type does not support "
159 "spare disks are create time\n");
162 if (subdevs
> raiddisks
+sparedisks
) {
163 fprintf(stderr
, Name
": You have listed more devices (%d) than are in the array(%d)!\n", subdevs
, raiddisks
+sparedisks
);
166 if (!have_container
&& subdevs
< raiddisks
+sparedisks
) {
167 fprintf(stderr
, Name
": You haven't given enough devices (real or missing) to create this array\n");
170 if (bitmap_file
&& level
<= 0) {
171 fprintf(stderr
, Name
": bitmaps not meaningful with level %s\n",
172 map_num(pers
, level
)?:"given");
176 /* now set some defaults */
179 default: /* no layout */
183 layout
= 0x102; /* near=2, far=1 */
186 Name
": layout defaults to n1\n");
190 layout
= map_name(r5layout
, "default");
193 Name
": layout defaults to %s\n", map_num(r5layout
, layout
));
196 layout
= map_name(faultylayout
, "default");
200 Name
": layout defaults to %s\n", map_num(faultylayout
, layout
));
205 /* check layout fits in array*/
206 if ((layout
&255) * ((layout
>>8)&255) > raiddisks
) {
207 fprintf(stderr
, Name
": that layout requires at least %d devices\n",
208 (layout
&255) * ((layout
>>8)&255));
218 case LEVEL_LINEAR
: /* linear */
222 fprintf(stderr
, Name
": chunk size defaults to 64K\n");
227 case LEVEL_MULTIPATH
:
228 case LEVEL_CONTAINER
:
232 fprintf(stderr
, Name
": chunk size ignored for this level\n");
236 fprintf(stderr
, Name
": unknown level %d\n", level
);
240 if (st
&& ! st
->ss
->validate_geometry(st
, level
, layout
, raiddisks
,
241 chunk
, size
, NULL
, NULL
, verbose
>=0))
244 /* now look at the subdevs */
245 info
.array
.active_disks
= 0;
246 info
.array
.working_disks
= 0;
248 for (dv
=devlist
; dv
; dv
=dv
->next
, dnum
++) {
249 char *dname
= dv
->devname
;
250 unsigned long long freesize
;
251 if (strcasecmp(dname
, "missing")==0) {
252 if (first_missing
> dnum
)
253 first_missing
= dnum
;
254 if (second_missing
> dnum
&& dnum
> first_missing
)
255 second_missing
= dnum
;
259 info
.array
.working_disks
++;
260 if (dnum
< raiddisks
)
261 info
.array
.active_disks
++;
263 struct createinfo
*ci
= conf_get_create_info();
268 /* Need to choose a default metadata, which is different
269 * depending on geometry of array.
272 char *name
= "default";
273 for(i
=0; !st
&& superlist
[i
]; i
++) {
274 st
= superlist
[i
]->match_metadata_desc(name
);
275 if (st
&& !st
->ss
->validate_geometry
276 (st
, level
, layout
, raiddisks
,
277 chunk
, size
, dname
, &freesize
,
283 fprintf(stderr
, Name
": device %s not suitable "
284 "for any style of array\n",
288 if (st
->ss
!= &super0
||
289 st
->minor_version
!= 90)
292 if (!st
->ss
->validate_geometry(st
, level
, layout
,
299 Name
": %s is not suitable for "
307 freesize
/= 2; /* convert to K */
309 /* round to chunk size */
310 freesize
= freesize
& ~(chunk
-1);
313 if (size
&& freesize
< size
) {
314 fprintf(stderr
, Name
": %s is smaller that given size."
315 " %lluK < %lluK + metadata\n",
316 dname
, freesize
, size
);
320 if (maxdisc
== NULL
|| (maxdisc
&& freesize
> maxsize
)) {
324 if (mindisc
==NULL
|| (mindisc
&& freesize
< minsize
)) {
328 if (runstop
!= 1 || verbose
>= 0) {
329 int fd
= open(dname
, O_RDONLY
);
331 fprintf(stderr
, Name
": Cannot open %s: %s\n",
332 dname
, strerror(errno
));
336 warn
|= check_ext2(fd
, dname
);
337 warn
|= check_reiser(fd
, dname
);
338 warn
|= check_raid(fd
, dname
);
343 fprintf(stderr
, Name
": create aborted\n");
347 if (mindisc
== NULL
&& !have_container
) {
348 fprintf(stderr
, Name
": no size and no drives given - aborting create.\n");
351 if (level
> 0 || level
== LEVEL_MULTIPATH
352 || level
== LEVEL_FAULTY
353 || st
->ss
->external
) {
354 /* size is meaningful */
355 if (!st
->ss
->validate_geometry(st
, level
, layout
,
359 fprintf(stderr
, Name
": devices too large for RAID level %d\n", level
);
364 fprintf(stderr
, Name
": size set to %lluK\n", size
);
367 if (level
> 0 && ((maxsize
-size
)*100 > maxsize
)) {
368 if (runstop
!= 1 || verbose
>= 0)
369 fprintf(stderr
, Name
": largest drive (%s) exceed size (%lluK) by more than 1%%\n",
376 if (!ask("Continue creating array? ")) {
377 fprintf(stderr
, Name
": create aborted.\n");
382 fprintf(stderr
, Name
": creation continuing despite oddities due to --run\n");
386 /* If this is raid4/5, we want to configure the last active slot
387 * as missing, so that a reconstruct happens (faster than re-parity)
388 * FIX: Can we do this for raid6 as well?
390 if (assume_clean
==0 && force
== 0 && first_missing
>= raiddisks
) {
394 insert_point
= raiddisks
-1;
396 info
.array
.active_disks
--;
403 /* For raid6, if creating with 1 missing drive, make a good drive
404 * into a spare, else the create will fail
406 if (assume_clean
== 0 && force
== 0 && first_missing
< raiddisks
&&
407 st
->ss
->external
== 0 &&
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 /* We need to create the device */
424 mdfd
= create_mddev(mddev
, autof
);
428 vers
= md_get_version(mdfd
);
430 fprintf(stderr
, Name
": Create requires md driver version 0.90.0 or later\n");
433 mdu_array_info_t inf
;
434 memset(&inf
, 0, sizeof(inf
));
435 ioctl(mdfd
, GET_ARRAY_INFO
, &inf
);
436 if (inf
.working_disks
!= 0) {
437 fprintf(stderr
, Name
": another array by this name"
438 " is already running.\n");
442 /* Ok, lets try some ioctls */
444 info
.array
.level
= level
;
445 info
.array
.size
= size
;
446 info
.array
.raid_disks
= raiddisks
;
447 /* The kernel should *know* what md_minor we are dealing
448 * with, but it chooses to trust me instead. Sigh
450 info
.array
.md_minor
= 0;
451 if (fstat(mdfd
, &stb
)==0)
452 info
.array
.md_minor
= minor(stb
.st_rdev
);
453 info
.array
.not_persistent
= 0;
455 if ( ( (level
== 4 || level
== 5) &&
456 (insert_point
< raiddisks
|| first_missing
< raiddisks
) )
458 ( level
== 6 && (insert_point
< raiddisks
459 || second_missing
< raiddisks
))
465 info
.array
.state
= 1; /* clean, but one+ drive will be missing*/
466 info
.resync_start
= ~0ULL;
468 info
.array
.state
= 0; /* not clean, but no errors */
469 info
.resync_start
= 0;
472 /* for raid10, the bitmap size is the capacity of the array,
473 * which is array.size * raid_disks / ncopies;
474 * .. but convert to sectors.
476 int ncopies
= ((layout
>>8) & 255) * (layout
& 255);
477 bitmapsize
= (unsigned long long)size
* raiddisks
/ ncopies
* 2;
478 /* printf("bms=%llu as=%d rd=%d nc=%d\n", bitmapsize, size, raiddisks, ncopies);*/
480 bitmapsize
= (unsigned long long)size
* 2;
482 /* There is lots of redundancy in these disk counts,
483 * raid_disks is the most meaningful value
484 * it describes the geometry of the array
486 * nr_disks is total number of used slots.
487 * it should be raid_disks+spare_disks
488 * spare_disks is the number of extra disks present
490 * active_disks is the number of working disks in
491 * active slots. (With raid_disks)
492 * working_disks is the total number of working disks,
494 * failed_disks is the number of disks marked failed
496 * Ideally, the kernel would keep these (except raid_disks)
497 * up-to-date as we ADD_NEW_DISK, but it doesn't (yet).
498 * So for now, we assume that all raid and spare
499 * devices will be given.
501 info
.array
.spare_disks
=sparedisks
;
502 info
.array
.failed_disks
=missing_disks
;
503 info
.array
.nr_disks
= info
.array
.working_disks
504 + info
.array
.failed_disks
;
505 info
.array
.layout
= layout
;
506 info
.array
.chunk_size
= chunk
*1024;
508 if (name
== NULL
|| *name
== 0) {
509 /* base name on mddev */
514 * /dev/md/home -> home
515 * /dev/mdhome -> home
517 name
= strrchr(mddev
, '/');
520 if (strncmp(name
, "md_d", 4)==0 &&
523 (name
-mddev
) == 5 /* /dev/ */)
525 else if (strncmp(name
, "md", 2)==0 &&
528 (name
-mddev
) == 5 /* /dev/ */)
532 if (!st
->ss
->init_super(st
, &info
.array
, size
, name
, homehost
, uuid
))
535 total_slots
= info
.array
.nr_disks
;
536 sysfs_init(&info
, mdfd
, 0);
537 st
->ss
->getinfo_super(st
, &info
);
539 if (did_default
&& verbose
>= 0) {
540 if (is_subarray(info
.text_version
)) {
541 int dnum
= devname2devnum(info
.text_version
+1);
543 int mdp
= get_mdp_major();
546 path
= map_dev(MD_MAJOR
, dnum
, 1);
548 path
= map_dev(mdp
, (-1-dnum
)<< 6, 1);
550 mdi
= sysfs_read(-1, dnum
, GET_VERSION
);
552 fprintf(stderr
, Name
": Creating array inside "
554 mdi
?mdi
->text_version
:"managed", path
);
557 fprintf(stderr
, Name
": Defaulting to version"
558 " %s metadata\n", info
.text_version
);
561 if (bitmap_file
&& vers
< 9003) {
562 major_num
= BITMAP_MAJOR_HOSTENDIAN
;
564 fprintf(stderr
, Name
": Warning - bitmaps created on this kernel are not portable\n"
565 " between different architectured. Consider upgrading the Linux kernel.\n");
569 if (bitmap_file
&& strcmp(bitmap_file
, "internal")==0) {
570 if ((vers
%100) < 2) {
571 fprintf(stderr
, Name
": internal bitmaps not supported by this kernel.\n");
574 if (!st
->ss
->add_internal_bitmap(st
, &bitmap_chunk
,
576 bitmapsize
, 1, major_num
)) {
577 fprintf(stderr
, Name
": Given bitmap chunk size not supported.\n");
584 sysfs_init(&info
, mdfd
, 0);
586 if (st
->ss
->external
&& st
->subarray
[0]) {
589 /* When creating a member, we need to be careful
590 * to negotiate with mdmon properly.
591 * If it is already running, we cannot write to
592 * the devices and must ask it to do that part.
593 * If it isn't running, we write to the devices,
595 * We hold an exclusive open on the container
596 * device to make sure mdmon doesn't exit after
597 * we checked that it is running.
599 * For now, fail if it is already running.
601 container_fd
= open_dev_excl(st
->container_dev
);
602 if (container_fd
< 0) {
603 fprintf(stderr
, Name
": Cannot get exclusive "
604 "open on container - weird.\n");
607 if (mdmon_running(st
->container_dev
)) {
609 fprintf(stderr
, Name
": reusing mdmon "
611 devnum2devname(st
->container_dev
));
612 st
->update_tail
= &st
->updates
;
616 rv
= set_array_info(mdfd
, st
, &info
);
618 fprintf(stderr
, Name
": failed to set array info for %s: %s\n",
619 mddev
, strerror(errno
));
626 st
->ss
->uuid_from_super(st
, uuid
);
627 if (CreateBitmap(bitmap_file
, force
, (char*)uuid
, bitmap_chunk
,
633 bitmap_fd
= open(bitmap_file
, O_RDWR
);
635 fprintf(stderr
, Name
": weird: %s cannot be openned\n",
639 if (ioctl(mdfd
, SET_BITMAP_FILE
, bitmap_fd
) < 0) {
640 fprintf(stderr
, Name
": Cannot set bitmap file for %s: %s\n",
641 mddev
, strerror(errno
));
646 infos
= malloc(sizeof(*infos
) * total_slots
);
648 for (pass
=1; pass
<=2 ; pass
++) {
649 mddev_dev_t moved_disk
= NULL
; /* the disk that was moved out of the insert point */
651 for (dnum
=0, dv
= devlist
; dv
;
652 dv
=(dv
->next
)?(dv
->next
):moved_disk
, dnum
++) {
655 struct mdinfo
*inf
= &infos
[dnum
];
657 if (dnum
>= total_slots
)
659 if (dnum
== insert_point
) {
662 if (dnum
== insert_point
||
663 strcasecmp(dv
->devname
, "missing")==0)
670 inf
->disk
.number
= dnum
;
671 inf
->disk
.raid_disk
= dnum
;
672 if (inf
->disk
.raid_disk
< raiddisks
)
673 inf
->disk
.state
= (1<<MD_DISK_ACTIVE
) |
678 if (dv
->writemostly
== 1)
679 inf
->disk
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
681 if (st
->ss
->external
&& st
->subarray
[0])
682 fd
= open(dv
->devname
, O_RDWR
);
684 fd
= open(dv
->devname
, O_RDWR
|O_EXCL
);
687 fprintf(stderr
, Name
": failed to open %s "
688 "after earlier success - aborting\n",
693 inf
->disk
.major
= major(stb
.st_rdev
);
694 inf
->disk
.minor
= minor(stb
.st_rdev
);
696 remove_partitions(fd
);
697 st
->ss
->add_to_super(st
, &inf
->disk
,
699 st
->ss
->getinfo_super(st
, inf
);
700 safe_mode_delay
= inf
->safe_mode_delay
;
702 /* getinfo_super might have lost these ... */
703 inf
->disk
.major
= major(stb
.st_rdev
);
704 inf
->disk
.minor
= minor(stb
.st_rdev
);
710 rv
= add_disk(mdfd
, st
, &info
, inf
);
714 Name
": ADD_NEW_DISK for %s "
716 dv
->devname
, strerror(errno
));
717 st
->ss
->free_super(st
);
722 if (dv
== moved_disk
&& dnum
!= insert_point
) break;
725 st
->ss
->write_init_super(st
);
726 flush_metadata_updates(st
);
730 st
->ss
->free_super(st
);
732 /* param is not actually used */
733 if (level
== LEVEL_CONTAINER
)
734 /* No need to start */
736 else if (runstop
== 1 || subdevs
>= raiddisks
) {
737 if (st
->ss
->external
) {
740 case LEVEL_MULTIPATH
:
742 sysfs_set_str(&info
, NULL
, "array_state",
747 sysfs_set_str(&info
, NULL
, "array_state",
751 sysfs_set_safemode(&info
, safe_mode_delay
);
754 if (ioctl(mdfd
, RUN_ARRAY
, ¶m
)) {
755 fprintf(stderr
, Name
": RUN_ARRAY failed: %s\n",
757 Manage_runstop(mddev
, mdfd
, -1, 0);
762 fprintf(stderr
, Name
": array %s started.\n", mddev
);
763 if (st
->ss
->external
&& st
->subarray
[0]) {
765 start_mdmon(st
->container_dev
);
767 ping_monitor(devnum2devname(st
->container_dev
));
771 fprintf(stderr
, Name
": not starting array - not enough devices.\n");