]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - Create.c
2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2002 Neil Brown <neilb@cse.unsw.edu.au>
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
34 int Create(struct supertype
*st
, char *mddev
, int mdfd
,
35 int chunk
, int level
, int layout
, unsigned long size
, int raiddisks
, int sparedisks
,
37 int subdevs
, mddev_dev_t devlist
,
38 int runstop
, int verbose
, int force
,
39 char *bitmap_file
, int bitmap_chunk
, int write_behind
, int delay
)
42 * Create a new raid array.
44 * First check that necessary details are available
45 * (i.e. level, raid-disks)
47 * Then check each disk to see what might be on it
48 * and report anything interesting.
50 * If anything looks odd, and runstop not set,
53 * SET_ARRAY_INFO and ADD_NEW_DISK, and
54 * if runstop==run, or raiddisks disks were used,
57 unsigned long long minsize
=0, maxsize
=0;
64 int first_missing
= subdevs
* 2;
65 int missing_disks
= 0;
66 int insert_point
= subdevs
* 2; /* where to insert a missing drive */
73 mdu_array_info_t array
;
75 vers
= md_get_version(mdfd
);
77 fprintf(stderr
, Name
": Create requires md driver version 0.90.0 or later\n");
82 Name
": a RAID level is needed to create an array.\n");
87 Name
": a number of --raid-devices must be given to create an array\n");
90 if (raiddisks
< 4 && level
== 6) {
92 Name
": at least 4 raid-devices needed for level 6\n");
95 if (raiddisks
> 256 && level
== 6) {
97 Name
": no more than 256 raid-devices supported for level 6\n");
100 if (raiddisks
< 2 && level
>= 4) {
102 Name
": at least 2 raid-devices needed for level 4 or 5\n");
105 if (subdevs
> raiddisks
+sparedisks
) {
106 fprintf(stderr
, Name
": You have listed more devices (%d) than are in the array(%d)!\n", subdevs
, raiddisks
+sparedisks
);
109 if (subdevs
< raiddisks
+sparedisks
) {
110 fprintf(stderr
, Name
": You haven't given enough devices (real or missing) to create this array\n");
114 /* now set some defaults */
117 default: /* no layout */
121 layout
= 0x102; /* near=2, far=1 */
124 Name
": layout defaults to n1\n");
128 layout
= map_name(r5layout
, "default");
131 Name
": layout defaults to %s\n", map_num(r5layout
, layout
));
134 layout
= map_name(faultylayout
, "default");
138 Name
": layout defaults to %s\n", map_num(faultylayout
, layout
));
143 /* check layout fits in array*/
144 if ((layout
&255) * ((layout
>>8)&255) > raiddisks
) {
145 fprintf(stderr
, Name
": that layout requires at least %d devices\n",
146 (layout
&255) * ((layout
>>8)&255));
156 case -1: /* linear */
160 fprintf(stderr
, Name
": chunk size defaults to 64K\n");
163 default: /* raid1, multipath */
167 fprintf(stderr
, Name
": chunk size ignored for this level\n");
172 /* now look at the subdevs */
173 array
.active_disks
= 0;
174 array
.working_disks
= 0;
176 for (dv
=devlist
; dv
; dv
=dv
->next
, dnum
++) {
177 char *dname
= dv
->devname
;
179 unsigned long long ldsize
, freesize
;
181 if (strcasecmp(dname
, "missing")==0) {
182 if (first_missing
> dnum
)
183 first_missing
= dnum
;
187 array
.working_disks
++;
188 if (dnum
< raiddisks
)
189 array
.active_disks
++;
190 fd
= open(dname
, O_RDONLY
|O_EXCL
, 0);
192 fprintf(stderr
, Name
": Cannot open %s: %s\n",
193 dname
, strerror(errno
));
198 if (ioctl(fd
, BLKGETSIZE64
, &ldsize
)==0)
202 if (ioctl(fd
, BLKGETSIZE
, &dsize
)) {
203 fprintf(stderr
, Name
": Cannot get size of %s: %s\n",
204 dname
, strerror(errno
));
213 freesize
= st
->ss
->avail_size(st
, ldsize
>> 9);
215 fprintf(stderr
, Name
": %s is too small: %luK\n",
216 dname
, (unsigned long)(ldsize
>>10));
222 freesize
/= 2; /* convert to K */
224 if (size
&& freesize
< size
) {
225 fprintf(stderr
, Name
": %s is smaller that given size."
226 " %lluK < %luK + superblock\n", dname
, freesize
, size
);
231 if (maxdisc
== NULL
|| (maxdisc
&& freesize
> maxsize
)) {
235 if (mindisc
==NULL
|| (mindisc
&& freesize
< minsize
)) {
239 if (runstop
!= 1 || verbose
>= 0) {
240 warn
|= check_ext2(fd
, dname
);
241 warn
|= check_reiser(fd
, dname
);
242 warn
|= check_raid(fd
, dname
);
247 fprintf(stderr
, Name
": create aborted\n");
251 if (mindisc
== NULL
) {
252 fprintf(stderr
, Name
": no size and no drives given - aborting create.\n");
255 if (level
> 0 || level
== LEVEL_MULTIPATH
|| level
== LEVEL_FAULTY
) {
256 /* size is meaningful */
257 if (minsize
> 0x100000000ULL
) {
258 fprintf(stderr
, Name
": devices too large for RAID level %d\n", level
);
263 fprintf(stderr
, Name
": size set to %luK\n", size
);
266 if (level
> 0 && ((maxsize
-size
)*100 > maxsize
)) {
267 if (runstop
!= 1 || verbose
>= 0)
268 fprintf(stderr
, Name
": largest drive (%s) exceed size (%luK) by more than 1%%\n",
275 if (!ask("Continue creating array? ")) {
276 fprintf(stderr
, Name
": create aborted.\n");
281 fprintf(stderr
, Name
": creation continuing despite oddities due to --run\n");
285 /* If this is raid5, we want to configure the last active slot
286 * as missing, so that a reconstruct happens (faster than re-parity)
287 * FIX: Can we do this for raid6 as well?
289 if (force
== 0 && first_missing
>= raiddisks
) {
292 insert_point
= raiddisks
-1;
294 array
.active_disks
--;
302 /* Ok, lets try some ioctls */
306 array
.raid_disks
= raiddisks
;
307 /* The kernel should *know* what md_minor we are dealing
308 * with, but it chooses to trust me instead. Sigh
311 if (fstat(mdfd
, &stb
)==0)
312 array
.md_minor
= minor(stb
.st_rdev
);
313 array
.not_persistent
= 0;
314 /*** FIX: Need to do something about RAID-6 here ***/
315 if ( ( (level
== 5) &&
316 (insert_point
< raiddisks
|| first_missing
< raiddisks
) )
318 ( level
== 6 && missing_disks
== 2)
320 array
.state
= 1; /* clean, but one+ drive will be missing */
322 array
.state
= 0; /* not clean, but no errors */
324 /* There is lots of redundancy in these disk counts,
325 * raid_disks is the most meaningful value
326 * it describes the geometry of the array
328 * nr_disks is total number of used slots.
329 * it should be raid_disks+spare_disks
330 * spare_disks is the number of extra disks present
332 * active_disks is the number of working disks in
333 * active slots. (With raid_disks)
334 * working_disks is the total number of working disks,
336 * failed_disks is the number of disks marked failed
338 * Ideally, the kernel would keep these (except raid_disks)
339 * up-to-date as we ADD_NEW_DISK, but it doesn't (yet).
340 * So for now, we assume that all raid and spare
341 * devices will be given.
343 array
.spare_disks
=sparedisks
;
344 array
.failed_disks
=missing_disks
;
345 array
.nr_disks
= array
.working_disks
+ array
.failed_disks
;
346 array
.layout
= layout
;
347 array
.chunk_size
= chunk
*1024;
350 if (!st
->ss
->init_super(st
, &super
, &array
, name
))
353 if (bitmap_file
&& strcmp(bitmap_file
, "internal")==0) {
354 if ((vers
%100) < 2) {
355 fprintf(stderr
, Name
": internal bitmaps not supported by this kernel.\n");
358 if (!st
->ss
->add_internal_bitmap(st
, super
, bitmap_chunk
, delay
, write_behind
,
360 fprintf(stderr
, Name
": Given bitmap chunk size not supported.\n");
368 if ((vers
% 100) >= 1) { /* can use different versions */
369 mdu_array_info_t inf
;
370 memset(&inf
, 0, sizeof(inf
));
371 inf
.major_version
= st
->ss
->major
;
372 inf
.minor_version
= st
->minor_version
;
373 rv
= ioctl(mdfd
, SET_ARRAY_INFO
, &inf
);
375 rv
= ioctl(mdfd
, SET_ARRAY_INFO
, NULL
);
377 fprintf(stderr
, Name
": SET_ARRAY_INFO failed for %s: %s\n",
378 mddev
, strerror(errno
));
385 if (bitmap_chunk
== UnSet
)
386 bitmap_chunk
= DEFAULT_BITMAP_CHUNK
;
388 st
->ss
->uuid_from_super(uuid
, super
);
389 if (CreateBitmap(bitmap_file
, force
, (char*)uuid
, bitmap_chunk
,
391 array
.size
*2ULL /* FIXME wrong for raid10 */)) {
394 bitmap_fd
= open(bitmap_file
, O_RDWR
);
396 fprintf(stderr
, Name
": weird: %s cannot be openned\n",
400 if (ioctl(mdfd
, SET_BITMAP_FILE
, bitmap_fd
) < 0) {
401 fprintf(stderr
, Name
": Cannot set bitmap file for %s: %s\n",
402 mddev
, strerror(errno
));
409 for (pass
=1; pass
<=2 ; pass
++) {
410 mddev_dev_t moved_disk
= NULL
; /* the disk that was moved out of the insert point */
412 for (dnum
=0, dv
= devlist
; dv
;
413 dv
=(dv
->next
)?(dv
->next
):moved_disk
, dnum
++) {
416 mdu_disk_info_t disk
;
419 if (dnum
== insert_point
) {
422 disk
.raid_disk
= disk
.number
;
423 if (disk
.raid_disk
< raiddisks
)
424 disk
.state
= (1<<MD_DISK_ACTIVE
) |
429 disk
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
431 if (dnum
== insert_point
||
432 strcasecmp(dv
->devname
, "missing")==0) {
435 disk
.state
= (1<<MD_DISK_FAULTY
);
437 fd
= open(dv
->devname
, O_RDONLY
|O_EXCL
, 0);
439 fprintf(stderr
, Name
": failed to open %s after earlier success - aborting\n",
444 disk
.major
= major(stb
.st_rdev
);
445 disk
.minor
= minor(stb
.st_rdev
);
450 st
->ss
->add_to_super(super
, &disk
);
453 if (disk
.state
== 1) break;
454 st
->ss
->write_init_super(st
, super
, &disk
, dv
->devname
);
456 if (ioctl(mdfd
, ADD_NEW_DISK
, &disk
)) {
457 fprintf(stderr
, Name
": ADD_NEW_DISK for %s failed: %s\n",
458 dv
->devname
, strerror(errno
));
465 if (dv
== moved_disk
&& dnum
!= insert_point
) break;
470 /* param is not actually used */
471 if (runstop
== 1 || subdevs
>= raiddisks
) {
473 if (ioctl(mdfd
, RUN_ARRAY
, ¶m
)) {
474 fprintf(stderr
, Name
": RUN_ARRAY failed: %s\n",
476 Manage_runstop(mddev
, mdfd
, -1, 0);
480 fprintf(stderr
, Name
": array %s started.\n", mddev
);
482 fprintf(stderr
, Name
": not starting array - not enough devices.\n");