]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Build.c
util: make env checking more generic
[thirdparty/mdadm.git] / Build.c
1 /*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001-2006 Neil Brown <neilb@suse.de>
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 "mdadm.h"
31
32 #define REGISTER_DEV _IO (MD_MAJOR, 1)
33 #define START_MD _IO (MD_MAJOR, 2)
34 #define STOP_MD _IO (MD_MAJOR, 3)
35
36 int Build(char *mddev, int chunk, int level, int layout,
37 int raiddisks, mddev_dev_t devlist, int assume_clean,
38 char *bitmap_file, int bitmap_chunk, int write_behind,
39 int delay, int verbose, int autof)
40 {
41 /* Build a linear or raid0 arrays without superblocks
42 * We cannot really do any checks, we just do it.
43 * For md_version < 0.90.0, we call REGISTER_DEV
44 * with the device numbers, and then
45 * START_MD giving the "geometry"
46 * geometry is 0xpp00cc
47 * where pp is personality: 1==linear, 2=raid0
48 * cc = chunk size factor: 0==4k, 1==8k etc.
49 *
50 * For md_version >= 0.90.0 we call
51 * SET_ARRAY_INFO, ADD_NEW_DISK, RUN_ARRAY
52 *
53 */
54 int i;
55 int vers;
56 struct stat stb;
57 int subdevs = 0, missing_disks = 0;
58 mddev_dev_t dv;
59 int bitmap_fd;
60 unsigned long long size = ~0ULL;
61 unsigned long long bitmapsize;
62 int mdfd;
63
64 /* scan all devices, make sure they really are block devices */
65 for (dv = devlist; dv; dv=dv->next) {
66 subdevs++;
67 if (strcmp("missing", dv->devname) == 0) {
68 missing_disks++;
69 continue;
70 }
71 if (stat(dv->devname, &stb)) {
72 fprintf(stderr, Name ": Cannot find %s: %s\n",
73 dv->devname, strerror(errno));
74 return 1;
75 }
76 if ((stb.st_mode & S_IFMT) != S_IFBLK) {
77 fprintf(stderr, Name ": %s is not a block device.\n",
78 dv->devname);
79 return 1;
80 }
81 }
82
83 if (raiddisks != subdevs) {
84 fprintf(stderr, Name ": requested %d devices in array but listed %d\n",
85 raiddisks, subdevs);
86 return 1;
87 }
88
89 if (layout == UnSet)
90 switch(level) {
91 default: /* no layout */
92 layout = 0;
93 break;
94 case 10:
95 layout = 0x102; /* near=2, far=1 */
96 if (verbose > 0)
97 fprintf(stderr,
98 Name ": layout defaults to n1\n");
99 break;
100 case 5:
101 case 6:
102 layout = map_name(r5layout, "default");
103 if (verbose > 0)
104 fprintf(stderr,
105 Name ": layout defaults to %s\n", map_num(r5layout, layout));
106 break;
107 case LEVEL_FAULTY:
108 layout = map_name(faultylayout, "default");
109
110 if (verbose > 0)
111 fprintf(stderr,
112 Name ": layout defaults to %s\n", map_num(faultylayout, layout));
113 break;
114 }
115
116 /* We need to create the device */
117 mdfd = create_mddev(mddev, autof);
118 if (mdfd < 0)
119 return 1;
120
121 vers = md_get_version(mdfd);
122
123 /* looks Ok, go for it */
124 if (vers >= 9000) {
125 mdu_array_info_t array;
126 array.level = level;
127 array.size = 0;
128 array.nr_disks = raiddisks;
129 array.raid_disks = raiddisks;
130 array.md_minor = 0;
131 if (fstat(mdfd, &stb)==0)
132 array.md_minor = minor(stb.st_rdev);
133 array.not_persistent = 1;
134 array.state = 0; /* not clean, but no errors */
135 if (assume_clean)
136 array.state |= 1;
137 array.active_disks = raiddisks - missing_disks;
138 array.working_disks = raiddisks - missing_disks;
139 array.spare_disks = 0;
140 array.failed_disks = missing_disks;
141 if (chunk == 0 && (level==0 || level==LEVEL_LINEAR))
142 chunk = 64;
143 array.chunk_size = chunk*1024;
144 array.layout = layout;
145 if (ioctl(mdfd, SET_ARRAY_INFO, &array)) {
146 fprintf(stderr, Name ": SET_ARRAY_INFO failed for %s: %s\n",
147 mddev, strerror(errno));
148 goto abort;
149 }
150 } else if (bitmap_file) {
151 fprintf(stderr, Name ": bitmaps not supported with this kernel\n");
152 goto abort;
153 }
154
155 if (bitmap_file && level <= 0) {
156 fprintf(stderr, Name ": bitmaps not meaningful with level %s\n",
157 map_num(pers, level)?:"given");
158 goto abort;
159 }
160 /* now add the devices */
161 for ((i=0), (dv = devlist) ; dv ; i++, dv=dv->next) {
162 unsigned long long dsize;
163 int fd;
164 if (strcmp("missing", dv->devname) == 0)
165 continue;
166 if (stat(dv->devname, &stb)) {
167 fprintf(stderr, Name ": Weird: %s has disappeared.\n",
168 dv->devname);
169 goto abort;
170 }
171 if ((stb.st_mode & S_IFMT)!= S_IFBLK) {
172 fprintf(stderr, Name ": Wierd: %s is no longer a block device.\n",
173 dv->devname);
174 goto abort;
175 }
176 fd = open(dv->devname, O_RDONLY|O_EXCL);
177 if (fd < 0) {
178 fprintf(stderr, Name ": Cannot open %s: %s\n",
179 dv->devname, strerror(errno));
180 goto abort;
181 }
182 if (get_dev_size(fd, NULL, &dsize) &&
183 (size == 0 || dsize < size))
184 size = dsize;
185 close(fd);
186 if (vers>= 9000) {
187 mdu_disk_info_t disk;
188 disk.number = i;
189 disk.raid_disk = i;
190 disk.state = (1<<MD_DISK_SYNC) | (1<<MD_DISK_ACTIVE);
191 if (dv->writemostly == 1)
192 disk.state |= 1<<MD_DISK_WRITEMOSTLY;
193 disk.major = major(stb.st_rdev);
194 disk.minor = minor(stb.st_rdev);
195 if (ioctl(mdfd, ADD_NEW_DISK, &disk)) {
196 fprintf(stderr, Name ": ADD_NEW_DISK failed for %s: %s\n",
197 dv->devname, strerror(errno));
198 goto abort;
199 }
200 } else {
201 if (ioctl(mdfd, REGISTER_DEV, &stb.st_rdev)) {
202 fprintf(stderr, Name ": REGISTER_DEV failed for %s: %s.\n",
203 dv->devname, strerror(errno));
204 goto abort;
205 }
206 }
207 }
208 /* now to start it */
209 if (vers >= 9000) {
210 mdu_param_t param; /* not used by syscall */
211 if (bitmap_file) {
212 bitmap_fd = open(bitmap_file, O_RDWR);
213 if (bitmap_fd < 0) {
214 int major = BITMAP_MAJOR_HI;
215 #if 0
216 if (bitmap_chunk == UnSet) {
217 fprintf(stderr, Name ": %s cannot be openned.",
218 bitmap_file);
219 goto abort;
220 }
221 #endif
222 if (vers < 9003) {
223 major = BITMAP_MAJOR_HOSTENDIAN;
224 #ifdef __BIG_ENDIAN
225 fprintf(stderr, Name ": Warning - bitmaps created on this kernel are not portable\n"
226 " between different architectures. Consider upgrading the Linux kernel.\n");
227 #endif
228 }
229 bitmapsize = size>>9; /* FIXME wrong for RAID10 */
230 if (CreateBitmap(bitmap_file, 1, NULL, bitmap_chunk,
231 delay, write_behind, bitmapsize, major)) {
232 goto abort;
233 }
234 bitmap_fd = open(bitmap_file, O_RDWR);
235 if (bitmap_fd < 0) {
236 fprintf(stderr, Name ": %s cannot be openned.",
237 bitmap_file);
238 goto abort;
239 }
240 }
241 if (bitmap_fd >= 0) {
242 if (ioctl(mdfd, SET_BITMAP_FILE, bitmap_fd) < 0) {
243 fprintf(stderr, Name ": Cannot set bitmap file for %s: %s\n",
244 mddev, strerror(errno));
245 goto abort;
246 }
247 }
248 }
249 if (ioctl(mdfd, RUN_ARRAY, &param)) {
250 fprintf(stderr, Name ": RUN_ARRAY failed: %s\n",
251 strerror(errno));
252 goto abort;
253 }
254 } else {
255 unsigned long arg;
256 arg=0;
257 while (chunk > 4096) {
258 arg++;
259 chunk >>= 1;
260 }
261 if (level == 0)
262 chunk |= 0x20000;
263 else chunk |= 0x10000;
264 if (ioctl(mdfd, START_MD, arg)) {
265 fprintf(stderr, Name ": START_MD failed: %s\n",
266 strerror(errno));
267 goto abort;
268 }
269 }
270 if (verbose >= 0)
271 fprintf(stderr, Name ": array %s built and started.\n",
272 mddev);
273 close(mdfd);
274 return 0;
275
276 abort:
277 if (vers >= 9000)
278 ioctl(mdfd, STOP_ARRAY, 0);
279 else
280 ioctl(mdfd, STOP_MD, 0);
281 close(mdfd);
282 return 1;
283 }