]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Create.c
mdctl-v0.3
[thirdparty/mdadm.git] / Create.c
1 /*
2 * mdctl - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001 Neil Brown <neilb@cse.unsw.edu.au>
5 *
6 *
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.
11 *
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.
16 *
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
20 *
21 * Author: Neil Brown
22 * Email: <neilb@cse.unsw.edu.au>
23 * Paper: Neil Brown
24 * School of Computer Science and Engineering
25 * The University of New South Wales
26 * Sydney, 2052
27 * Australia
28 */
29
30 #include "mdctl.h"
31 #include "md_u.h"
32 #include "md_p.h"
33
34 int Create(char *mddev, int mdfd,
35 int chunk, int level, int layout, int size, int raiddisks, int sparedisks,
36 int subdevs, char *subdev[],
37 int runstop, int verbose)
38 {
39 /*
40 * Create a new raid array.
41 *
42 * First check that necessary details are available
43 * (i.e. level, raid-disks)
44 *
45 * Then check each disk to see what might be on it
46 * and report anything interesting.
47 *
48 * If anything looks odd, and runstop not set,
49 * abort.
50 *
51 * SET_ARRAY_INFO and ADD_NEW_DISK, and
52 * if runstop==run, or raiddisks diskswere used,
53 * RUN_ARRAY
54 */
55 int minsize, maxsize;
56 int maxdisc= -1, mindisc = -1;
57 int i;
58 int fail=0, warn=0;
59
60 mdu_array_info_t array;
61 mdu_param_t param;
62
63
64 if (md_get_version(mdfd) < 9000) {
65 fprintf(stderr, Name ": Create requires md driver verison 0.90.0 or later\n");
66 return 1;
67 }
68 if (level == -10) {
69 fprintf(stderr,
70 Name ": a RAID level is needed to create an array.\n");
71 return 1;
72 }
73 if (raiddisks < 1) {
74 fprintf(stderr,
75 Name ": a number of --raid-disks must be given to create an array\n");
76 return 1;
77 }
78 if (raiddisks+sparedisks > MD_SB_DISKS) {
79 fprintf(stderr,
80 Name ": too many discs requested: %d+%d > %d\n",
81 raiddisks, sparedisks, MD_SB_DISKS);
82 return 1;
83 }
84 if (subdevs > raiddisks+sparedisks) {
85 fprintf(stderr, Name ": You have listed more disks (%d) than are in the array(%d)!\n", subdevs, raiddisks+sparedisks);
86 return 1;
87 }
88 /* now set some defaults */
89 if (layout == -1)
90 switch(level) {
91 default: /* no layout */
92 layout = 0;
93 break;
94 case 5:
95 layout = map_name(r5layout, "default");
96 if (verbose)
97 fprintf(stderr,
98 Name ": layout defaults to %s\n", map_num(r5layout, layout));
99 break;
100 }
101
102 if (chunk == 0) {
103 chunk = 64;
104 if (verbose)
105 fprintf(stderr, Name ": chunk size defaults to 64K\n");
106 }
107
108 /* now look at the subdevs */
109 for (i=0; i<subdevs; i++) {
110 char *dname = subdev[i];
111 int dsize, freesize;
112 int fd = open(dname, O_RDONLY, 0);
113 if (fd <0 ) {
114 fprintf(stderr, Name ": Cannot open %s: %s\n",
115 dname, strerror(errno));
116 fail=1;
117 continue;
118 }
119 if (ioctl(fd, BLKGETSIZE, &dsize)) {
120 fprintf(stderr, Name ": Cannot get size of %s: %s\n",
121 dname, strerror(errno));
122 fail = 1;
123 close(fd);
124 continue;
125 }
126 if (dsize < MD_RESERVED_SECTORS*2) {
127 fprintf(stderr, Name ": %s is too small: %dK\n",
128 dname, dsize/2);
129 fail = 1;
130 close(fd);
131 continue;
132 }
133 freesize = MD_NEW_SIZE_SECTORS(dsize);
134 freesize /= 2;
135
136 if (size && freesize < size) {
137 fprintf(stderr, Name ": %s is smaller that given size."
138 " %dK < %dK + superblock\n", dname, freesize, size);
139 fail = 1;
140 close(fd);
141 continue;
142 }
143 if (maxdisc< 0 || (maxdisc>=0 && freesize > maxsize)) {
144 maxdisc = i;
145 maxsize = freesize;
146 }
147 if (mindisc < 0 || (mindisc >=0 && freesize < minsize)) {
148 mindisc = i;
149 minsize = freesize;
150 }
151 warn |= check_ext2(fd, dname);
152 warn |= check_reiser(fd, dname);
153 warn |= check_raid(fd, dname);
154 close(fd);
155 }
156 if (fail) {
157 fprintf(stderr, Name ": create aborted\n");
158 return 1;
159 }
160 if (size == 0) {
161 if (mindisc == -1) {
162 fprintf(stderr, Name ": no size and no drives given - aborting create.\n");
163 return 1;
164 }
165 size = minsize;
166 if (verbose)
167 fprintf(stderr, Name ": size set to %dK\n", size);
168 }
169 if ((maxsize-size)*100 > maxsize) {
170 fprintf(stderr, Name ": largest drive (%s) exceed size (%dK) by more than 1%\n",
171 subdev[maxdisc], size);
172 warn = 1;
173 }
174
175 if (warn) {
176 if (runstop!= 1) {
177 if (!ask("Continue creating array? ")) {
178 fprintf(stderr, Name ": create aborted.\n");
179 return 1;
180 }
181 } else {
182 if (verbose)
183 fprintf(stderr, Name ": creation continuing despite oddities due to --run\n");
184 }
185 }
186
187 /* Ok, lets try some ioctls */
188
189 array.level = level;
190 array.size = size;
191 array.nr_disks = raiddisks+sparedisks;
192 array.raid_disks = raiddisks;
193 array.md_minor = 0;
194 array.not_persistent = 0;
195 array.state = 0; /* not clean, but no errors */
196 array.active_disks=0;
197 array.working_disks=0;
198 array.spare_disks=0;
199 array.failed_disks=0;
200 array.layout = layout;
201 array.chunk_size = chunk*1024;
202
203 if (ioctl(mdfd, SET_ARRAY_INFO, &array)) {
204 fprintf(stderr, Name ": SET_ARRAY_INFO failed for %s: %s\n",
205 mddev, strerror(errno));
206 return 1;
207 }
208
209 for (i=0; i<subdevs; i++) {
210 int fd = open(subdev[i], O_RDONLY, 0);
211 struct stat stb;
212 mdu_disk_info_t disk;
213 if (fd < 0) {
214 fprintf(stderr, Name ": failed to open %s after earlier success - aborting\n",
215 subdev[i]);
216 return 1;
217 }
218 fstat(fd, &stb);
219 disk.number = i;
220 disk.raid_disk = i;
221 disk.state = 6; /* active and in sync */
222 disk.major = MAJOR(stb.st_rdev);
223 disk.minor = MINOR(stb.st_rdev);
224 close(fd);
225 if (ioctl(mdfd, ADD_NEW_DISK, &disk)) {
226 fprintf(stderr, Name ": ADD_NEW_DISK for %s failed: %s\b",
227 subdev[i], strerror(errno));
228 return 1;
229 }
230 }
231
232 /* param is not actually used */
233 if (runstop == 1 || subdevs >= raiddisks) {
234 if (ioctl(mdfd, RUN_ARRAY, &param)) {
235 fprintf(stderr, Name ": RUN_ARRAY failed: %s\n",
236 strerror(errno));
237 return 1;
238 }
239 fprintf(stderr, Name ": array %s started.\n", mddev);
240 } else {
241 fprintf(stderr, Name ": not starting array - not enough discs.\n");
242 }
243 return 0;
244 }