]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - Create.c
331835dbcb635b1c606261d2a1ee3828ee3af067
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
,
36 int subdevs
, mddev_dev_t devlist
,
37 int runstop
, int verbose
, int force
)
40 * Create a new raid array.
42 * First check that necessary details are available
43 * (i.e. level, raid-disks)
45 * Then check each disk to see what might be on it
46 * and report anything interesting.
48 * If anything looks odd, and runstop not set,
51 * SET_ARRAY_INFO and ADD_NEW_DISK, and
52 * if runstop==run, or raiddisks disks were used,
55 unsigned long long minsize
=0, maxsize
=0;
62 int first_missing
= subdevs
* 2;
63 int missing_disks
= 0;
64 int insert_point
= subdevs
* 2; /* where to insert a missing drive */
70 mdu_array_info_t array
;
72 vers
= md_get_version(mdfd
);
74 fprintf(stderr
, Name
": Create requires md driver version 0.90.0 or later\n");
79 Name
": a RAID level is needed to create an array.\n");
84 Name
": a number of --raid-devices must be given to create an array\n");
87 if (raiddisks
< 4 && level
== 6) {
89 Name
": at least 4 raid-devices needed for level 6\n");
92 if (raiddisks
> 256 && level
== 6) {
94 Name
": no more than 256 raid-devices supported for level 6\n");
97 if (raiddisks
< 2 && level
>= 4) {
99 Name
": at least 2 raid-devices needed for level 4 or 5\n");
102 if (subdevs
> raiddisks
+sparedisks
) {
103 fprintf(stderr
, Name
": You have listed more devices (%d) than are in the array(%d)!\n", subdevs
, raiddisks
+sparedisks
);
106 if (subdevs
< raiddisks
+sparedisks
) {
107 fprintf(stderr
, Name
": You haven't given enough devices (real or missing) to create this array\n");
111 /* now set some defaults */
114 default: /* no layout */
118 layout
= 0x102; /* near=2, far=1 */
121 Name
": layout defaults to n1\n");
125 layout
= map_name(r5layout
, "default");
128 Name
": layout defaults to %s\n", map_num(r5layout
, layout
));
131 layout
= map_name(faultylayout
, "default");
135 Name
": layout defaults to %s\n", map_num(faultylayout
, layout
));
140 /* check layout fits in array*/
141 if ((layout
&255) * ((layout
>>8)&255) > raiddisks
) {
142 fprintf(stderr
, Name
": that layout requires at least %d devices\n",
143 (layout
&255) * ((layout
>>8)&255));
153 case -1: /* linear */
157 fprintf(stderr
, Name
": chunk size defaults to 64K\n");
160 default: /* raid1, multipath */
164 fprintf(stderr
, Name
": chunk size ignored for this level\n");
169 /* now look at the subdevs */
170 array
.active_disks
= 0;
171 array
.working_disks
= 0;
173 for (dv
=devlist
; dv
; dv
=dv
->next
, dnum
++) {
174 char *dname
= dv
->devname
;
176 unsigned long long ldsize
, freesize
;
178 if (strcasecmp(dname
, "missing")==0) {
179 if (first_missing
> dnum
)
180 first_missing
= dnum
;
184 array
.working_disks
++;
185 if (dnum
< raiddisks
)
186 array
.active_disks
++;
187 fd
= open(dname
, O_RDONLY
|O_EXCL
, 0);
189 fprintf(stderr
, Name
": Cannot open %s: %s\n",
190 dname
, strerror(errno
));
195 if (ioctl(fd
, BLKGETSIZE64
, &ldsize
)==0)
199 if (ioctl(fd
, BLKGETSIZE
, &dsize
)) {
200 fprintf(stderr
, Name
": Cannot get size of %s: %s\n",
201 dname
, strerror(errno
));
210 freesize
= st
->ss
->avail_size(ldsize
);
212 fprintf(stderr
, Name
": %s is too small: %luK\n",
213 dname
, (unsigned long)(ldsize
>>10));
219 freesize
/= 2; /* convert to K */
221 if (size
&& freesize
< size
) {
222 fprintf(stderr
, Name
": %s is smaller that given size."
223 " %lluK < %luK + superblock\n", dname
, freesize
, size
);
228 if (maxdisc
== NULL
|| (maxdisc
&& freesize
> maxsize
)) {
232 if (mindisc
==NULL
|| (mindisc
&& freesize
< minsize
)) {
236 warn
|= check_ext2(fd
, dname
);
237 warn
|= check_reiser(fd
, dname
);
238 warn
|= check_raid(fd
, dname
);
242 fprintf(stderr
, Name
": create aborted\n");
246 if (mindisc
== NULL
) {
247 fprintf(stderr
, Name
": no size and no drives given - aborting create.\n");
251 /* size is meaningful */
252 if (minsize
> 0x100000000ULL
) {
253 fprintf(stderr
, Name
": devices too large for RAID level %d\n", level
);
258 fprintf(stderr
, Name
": size set to %luK\n", size
);
261 if (level
> 0 && ((maxsize
-size
)*100 > maxsize
)) {
262 fprintf(stderr
, Name
": largest drive (%s) exceed size (%luK) by more than 1%%\n",
269 if (!ask("Continue creating array? ")) {
270 fprintf(stderr
, Name
": create aborted.\n");
275 fprintf(stderr
, Name
": creation continuing despite oddities due to --run\n");
279 /* If this is raid5, we want to configure the last active slot
280 * as missing, so that a reconstruct happens (faster than re-parity)
281 * FIX: Can we do this for raid6 as well?
283 if (force
== 0 && first_missing
>= raiddisks
) {
286 insert_point
= raiddisks
-1;
288 array
.active_disks
--;
296 /* Ok, lets try some ioctls */
300 array
.raid_disks
= raiddisks
;
301 /* The kernel should *know* what md_minor we are dealing
302 * with, but it chooses to trust me instead. Sigh
305 if (fstat(mdfd
, &stb
)==0)
306 array
.md_minor
= minor(stb
.st_rdev
);
307 array
.not_persistent
= 0;
308 /*** FIX: Need to do something about RAID-6 here ***/
309 if ( ( (level
== 5) &&
310 (insert_point
< raiddisks
|| first_missing
< raiddisks
) )
312 ( level
== 6 && missing_disks
== 2)
314 array
.state
= 1; /* clean, but one+ drive will be missing */
316 array
.state
= 0; /* not clean, but no errors */
318 /* There is lots of redundancy in these disk counts,
319 * raid_disks is the most meaningful value
320 * it describes the geometry of the array
322 * nr_disks is total number of used slots.
323 * it should be raid_disks+spare_disks
324 * spare_disks is the number of extra disks present
326 * active_disks is the number of working disks in
327 * active slots. (With raid_disks)
328 * working_disks is the total number of working disks,
330 * failed_disks is the number of disks marked failed
332 * Ideally, the kernel would keep these (except raid_disks)
333 * up-to-date as we ADD_NEW_DISK, but it doesn't (yet).
334 * So for now, we assume that all raid and spare
335 * devices will be given.
337 array
.spare_disks
=sparedisks
;
338 array
.failed_disks
=missing_disks
;
339 array
.nr_disks
= array
.working_disks
+ array
.failed_disks
;
340 array
.layout
= layout
;
341 array
.chunk_size
= chunk
*1024;
342 printf("VERS = %d\n", vers
);
344 if (!st
->ss
->init_super(&super
, &array
))
347 if ((vers
% 100) >= 1) { /* can use different versions */
348 mdu_array_info_t inf
;
349 memset(&inf
, 0, sizeof(inf
));
350 inf
.major_version
= st
->ss
->major
;
351 inf
.minor_version
= st
->minor_version
;
352 rv
= ioctl(mdfd
, SET_ARRAY_INFO
, &inf
);
354 rv
= ioctl(mdfd
, SET_ARRAY_INFO
, NULL
);
356 fprintf(stderr
, Name
": SET_ARRAY_INFO failed for %s: %s\n",
357 mddev
, strerror(errno
));
363 for (pass
=1; pass
<=2 ; pass
++) {
364 mddev_dev_t moved_disk
= NULL
; /* the disk that was moved out of the insert point */
366 for (dnum
=0, dv
= devlist
; dv
;
367 dv
=(dv
->next
)?(dv
->next
):moved_disk
, dnum
++) {
370 mdu_disk_info_t disk
;
373 if (dnum
== insert_point
) {
376 disk
.raid_disk
= disk
.number
;
377 if (disk
.raid_disk
< raiddisks
)
378 disk
.state
= 6; /* active and in sync */
381 if (dnum
== insert_point
||
382 strcasecmp(dv
->devname
, "missing")==0) {
385 disk
.state
= 1; /* faulty */
387 fd
= open(dv
->devname
, O_RDONLY
|O_EXCL
, 0);
389 fprintf(stderr
, Name
": failed to open %s after earlier success - aborting\n",
394 disk
.major
= major(stb
.st_rdev
);
395 disk
.minor
= minor(stb
.st_rdev
);
401 st
->ss
->add_to_super(super
, &disk
);
404 st
->ss
->write_init_super(st
, super
, &disk
, dv
->devname
);
406 if (ioctl(mdfd
, ADD_NEW_DISK
, &disk
)) {
407 fprintf(stderr
, Name
": ADD_NEW_DISK for %s failed: %s\n",
408 dv
->devname
, strerror(errno
));
415 if (dv
== moved_disk
&& dnum
!= insert_point
) break;
420 /* param is not actually used */
421 if (runstop
== 1 || subdevs
>= raiddisks
) {
423 if (ioctl(mdfd
, RUN_ARRAY
, ¶m
)) {
424 fprintf(stderr
, Name
": RUN_ARRAY failed: %s\n",
426 Manage_runstop(mddev
, mdfd
, -1);
429 fprintf(stderr
, Name
": array %s started.\n", mddev
);
431 fprintf(stderr
, Name
": not starting array - not enough devices.\n");