disks.c revision 1.45 1 1.45 martin /* $NetBSD: disks.c,v 1.45 2019/08/01 16:32:06 martin Exp $ */
2 1.1 dholland
3 1.1 dholland /*
4 1.1 dholland * Copyright 1997 Piermont Information Systems Inc.
5 1.1 dholland * All rights reserved.
6 1.1 dholland *
7 1.1 dholland * Written by Philip A. Nelson for Piermont Information Systems Inc.
8 1.1 dholland *
9 1.1 dholland * Redistribution and use in source and binary forms, with or without
10 1.1 dholland * modification, are permitted provided that the following conditions
11 1.1 dholland * are met:
12 1.1 dholland * 1. Redistributions of source code must retain the above copyright
13 1.1 dholland * notice, this list of conditions and the following disclaimer.
14 1.1 dholland * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 dholland * notice, this list of conditions and the following disclaimer in the
16 1.1 dholland * documentation and/or other materials provided with the distribution.
17 1.1 dholland * 3. The name of Piermont Information Systems Inc. may not be used to endorse
18 1.1 dholland * or promote products derived from this software without specific prior
19 1.1 dholland * written permission.
20 1.1 dholland *
21 1.1 dholland * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
22 1.1 dholland * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 dholland * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 dholland * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
25 1.1 dholland * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 1.1 dholland * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 1.1 dholland * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 1.1 dholland * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 1.1 dholland * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 1.1 dholland * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 1.1 dholland * THE POSSIBILITY OF SUCH DAMAGE.
32 1.1 dholland *
33 1.1 dholland */
34 1.1 dholland
35 1.1 dholland /* disks.c -- routines to deal with finding disks and labeling disks. */
36 1.1 dholland
37 1.1 dholland
38 1.30 martin #include <assert.h>
39 1.1 dholland #include <errno.h>
40 1.17 martin #include <inttypes.h>
41 1.1 dholland #include <stdio.h>
42 1.1 dholland #include <stdlib.h>
43 1.1 dholland #include <unistd.h>
44 1.1 dholland #include <fcntl.h>
45 1.20 martin #include <fnmatch.h>
46 1.1 dholland #include <util.h>
47 1.2 martin #include <uuid.h>
48 1.30 martin #include <paths.h>
49 1.1 dholland
50 1.1 dholland #include <sys/param.h>
51 1.1 dholland #include <sys/sysctl.h>
52 1.1 dholland #include <sys/swap.h>
53 1.30 martin #include <sys/disklabel_gpt.h>
54 1.1 dholland #include <ufs/ufs/dinode.h>
55 1.1 dholland #include <ufs/ffs/fs.h>
56 1.1 dholland
57 1.1 dholland #include <dev/scsipi/scsipi_all.h>
58 1.1 dholland #include <sys/scsiio.h>
59 1.1 dholland
60 1.1 dholland #include <dev/ata/atareg.h>
61 1.1 dholland #include <sys/ataio.h>
62 1.1 dholland
63 1.1 dholland #include "defs.h"
64 1.1 dholland #include "md.h"
65 1.1 dholland #include "msg_defs.h"
66 1.1 dholland #include "menu_defs.h"
67 1.1 dholland #include "txtwalk.h"
68 1.1 dholland
69 1.31 martin /* #define DEBUG_VERBOSE 1 */
70 1.30 martin
71 1.1 dholland /* Disk descriptions */
72 1.1 dholland struct disk_desc {
73 1.1 dholland char dd_name[SSTRSIZE];
74 1.29 mrg char dd_descr[256];
75 1.15 martin bool dd_no_mbr, dd_no_part;
76 1.1 dholland uint dd_cyl;
77 1.1 dholland uint dd_head;
78 1.1 dholland uint dd_sec;
79 1.1 dholland uint dd_secsize;
80 1.30 martin daddr_t dd_totsec;
81 1.2 martin };
82 1.2 martin
83 1.45 martin static const char name_prefix[] = "NAME=";
84 1.45 martin
85 1.1 dholland /* Local prototypes */
86 1.1 dholland static int foundffs(struct data *, size_t);
87 1.1 dholland #ifdef USE_SYSVBFS
88 1.1 dholland static int foundsysvbfs(struct data *, size_t);
89 1.1 dholland #endif
90 1.30 martin static int fsck_preen(const char *, const char *, bool silent);
91 1.30 martin static void fixsb(const char *, const char *);
92 1.1 dholland
93 1.1 dholland
94 1.1 dholland static bool tmpfs_on_var_shm(void);
95 1.1 dholland
96 1.1 dholland const char *
97 1.30 martin getfslabelname(uint f, uint f_version)
98 1.1 dholland {
99 1.30 martin if (f == FS_TMPFS)
100 1.30 martin return "tmpfs";
101 1.30 martin else if (f == FS_MFS)
102 1.30 martin return "mfs";
103 1.30 martin else if (f == FS_BSDFFS && f_version > 0)
104 1.30 martin return f_version == 2 ?
105 1.30 martin msg_string(MSG_fs_type_ffsv2) : msg_string(MSG_fs_type_ffs);
106 1.30 martin else if (f >= __arraycount(fstypenames) || fstypenames[f] == NULL)
107 1.1 dholland return "invalid";
108 1.1 dholland return fstypenames[f];
109 1.1 dholland }
110 1.1 dholland
111 1.1 dholland /*
112 1.1 dholland * Decide wether we want to mount a tmpfs on /var/shm: we do this always
113 1.1 dholland * when the machine has more than 16 MB of user memory. On smaller machines,
114 1.1 dholland * shm_open() and friends will not perform well anyway.
115 1.1 dholland */
116 1.1 dholland static bool
117 1.1 dholland tmpfs_on_var_shm()
118 1.1 dholland {
119 1.1 dholland uint64_t ram;
120 1.1 dholland size_t len;
121 1.1 dholland
122 1.1 dholland len = sizeof(ram);
123 1.1 dholland if (sysctlbyname("hw.usermem64", &ram, &len, NULL, 0))
124 1.1 dholland return false;
125 1.1 dholland
126 1.28 martin return ram > 16 * MEG;
127 1.1 dholland }
128 1.1 dholland
129 1.1 dholland /* from src/sbin/atactl/atactl.c
130 1.1 dholland * extract_string: copy a block of bytes out of ataparams and make
131 1.1 dholland * a proper string out of it, truncating trailing spaces and preserving
132 1.1 dholland * strict typing. And also, not doing unaligned accesses.
133 1.1 dholland */
134 1.1 dholland static void
135 1.1 dholland ata_extract_string(char *buf, size_t bufmax,
136 1.1 dholland uint8_t *bytes, unsigned numbytes,
137 1.1 dholland int needswap)
138 1.1 dholland {
139 1.1 dholland unsigned i;
140 1.1 dholland size_t j;
141 1.1 dholland unsigned char ch1, ch2;
142 1.1 dholland
143 1.1 dholland for (i = 0, j = 0; i < numbytes; i += 2) {
144 1.1 dholland ch1 = bytes[i];
145 1.1 dholland ch2 = bytes[i+1];
146 1.1 dholland if (needswap && j < bufmax-1) {
147 1.1 dholland buf[j++] = ch2;
148 1.1 dholland }
149 1.1 dholland if (j < bufmax-1) {
150 1.1 dholland buf[j++] = ch1;
151 1.1 dholland }
152 1.1 dholland if (!needswap && j < bufmax-1) {
153 1.1 dholland buf[j++] = ch2;
154 1.1 dholland }
155 1.1 dholland }
156 1.1 dholland while (j > 0 && buf[j-1] == ' ') {
157 1.1 dholland j--;
158 1.1 dholland }
159 1.1 dholland buf[j] = '\0';
160 1.1 dholland }
161 1.1 dholland
162 1.1 dholland /*
163 1.1 dholland * from src/sbin/scsictl/scsi_subr.c
164 1.1 dholland */
165 1.1 dholland #define STRVIS_ISWHITE(x) ((x) == ' ' || (x) == '\0' || (x) == (u_char)'\377')
166 1.1 dholland
167 1.1 dholland static void
168 1.1 dholland scsi_strvis(char *sdst, size_t dlen, const char *ssrc, size_t slen)
169 1.1 dholland {
170 1.1 dholland u_char *dst = (u_char *)sdst;
171 1.1 dholland const u_char *src = (const u_char *)ssrc;
172 1.1 dholland
173 1.1 dholland /* Trim leading and trailing blanks and NULs. */
174 1.1 dholland while (slen > 0 && STRVIS_ISWHITE(src[0]))
175 1.1 dholland ++src, --slen;
176 1.1 dholland while (slen > 0 && STRVIS_ISWHITE(src[slen - 1]))
177 1.1 dholland --slen;
178 1.1 dholland
179 1.1 dholland while (slen > 0) {
180 1.1 dholland if (*src < 0x20 || *src >= 0x80) {
181 1.1 dholland /* non-printable characters */
182 1.1 dholland dlen -= 4;
183 1.1 dholland if (dlen < 1)
184 1.1 dholland break;
185 1.1 dholland *dst++ = '\\';
186 1.1 dholland *dst++ = ((*src & 0300) >> 6) + '0';
187 1.1 dholland *dst++ = ((*src & 0070) >> 3) + '0';
188 1.1 dholland *dst++ = ((*src & 0007) >> 0) + '0';
189 1.1 dholland } else if (*src == '\\') {
190 1.1 dholland /* quote characters */
191 1.1 dholland dlen -= 2;
192 1.1 dholland if (dlen < 1)
193 1.1 dholland break;
194 1.1 dholland *dst++ = '\\';
195 1.1 dholland *dst++ = '\\';
196 1.1 dholland } else {
197 1.1 dholland /* normal characters */
198 1.1 dholland if (--dlen < 1)
199 1.1 dholland break;
200 1.1 dholland *dst++ = *src;
201 1.1 dholland }
202 1.1 dholland ++src, --slen;
203 1.1 dholland }
204 1.1 dholland
205 1.1 dholland *dst++ = 0;
206 1.1 dholland }
207 1.1 dholland
208 1.1 dholland
209 1.1 dholland static int
210 1.35 christos get_descr_scsi(struct disk_desc *dd)
211 1.1 dholland {
212 1.1 dholland struct scsipi_inquiry_data inqbuf;
213 1.1 dholland struct scsipi_inquiry cmd;
214 1.1 dholland scsireq_t req;
215 1.1 dholland /* x4 in case every character is escaped, +1 for NUL. */
216 1.1 dholland char vendor[(sizeof(inqbuf.vendor) * 4) + 1],
217 1.1 dholland product[(sizeof(inqbuf.product) * 4) + 1],
218 1.1 dholland revision[(sizeof(inqbuf.revision) * 4) + 1];
219 1.1 dholland char size[5];
220 1.1 dholland
221 1.1 dholland memset(&inqbuf, 0, sizeof(inqbuf));
222 1.1 dholland memset(&cmd, 0, sizeof(cmd));
223 1.1 dholland memset(&req, 0, sizeof(req));
224 1.1 dholland
225 1.1 dholland cmd.opcode = INQUIRY;
226 1.1 dholland cmd.length = sizeof(inqbuf);
227 1.1 dholland memcpy(req.cmd, &cmd, sizeof(cmd));
228 1.1 dholland req.cmdlen = sizeof(cmd);
229 1.1 dholland req.databuf = &inqbuf;
230 1.1 dholland req.datalen = sizeof(inqbuf);
231 1.1 dholland req.timeout = 10000;
232 1.1 dholland req.flags = SCCMD_READ;
233 1.1 dholland req.senselen = SENSEBUFLEN;
234 1.1 dholland
235 1.35 christos if (!disk_ioctl(dd->dd_name, SCIOCCOMMAND, &req)
236 1.35 christos || req.retsts != SCCMD_OK)
237 1.1 dholland return 0;
238 1.1 dholland
239 1.1 dholland scsi_strvis(vendor, sizeof(vendor), inqbuf.vendor,
240 1.1 dholland sizeof(inqbuf.vendor));
241 1.1 dholland scsi_strvis(product, sizeof(product), inqbuf.product,
242 1.1 dholland sizeof(inqbuf.product));
243 1.1 dholland scsi_strvis(revision, sizeof(revision), inqbuf.revision,
244 1.1 dholland sizeof(inqbuf.revision));
245 1.1 dholland
246 1.1 dholland humanize_number(size, sizeof(size),
247 1.1 dholland (uint64_t)dd->dd_secsize * (uint64_t)dd->dd_totsec,
248 1.1 dholland "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
249 1.1 dholland
250 1.1 dholland snprintf(dd->dd_descr, sizeof(dd->dd_descr),
251 1.1 dholland "%s (%s, %s %s)",
252 1.1 dholland dd->dd_name, size, vendor, product);
253 1.1 dholland
254 1.1 dholland return 1;
255 1.1 dholland }
256 1.1 dholland
257 1.1 dholland static int
258 1.35 christos get_descr_ata(struct disk_desc *dd)
259 1.1 dholland {
260 1.1 dholland struct atareq req;
261 1.1 dholland static union {
262 1.1 dholland unsigned char inbuf[DEV_BSIZE];
263 1.1 dholland struct ataparams inqbuf;
264 1.1 dholland } inbuf;
265 1.1 dholland struct ataparams *inqbuf = &inbuf.inqbuf;
266 1.1 dholland char model[sizeof(inqbuf->atap_model)+1];
267 1.1 dholland char size[5];
268 1.35 christos int needswap = 0;
269 1.1 dholland
270 1.1 dholland memset(&inbuf, 0, sizeof(inbuf));
271 1.1 dholland memset(&req, 0, sizeof(req));
272 1.1 dholland
273 1.1 dholland req.flags = ATACMD_READ;
274 1.1 dholland req.command = WDCC_IDENTIFY;
275 1.1 dholland req.databuf = (void *)&inbuf;
276 1.1 dholland req.datalen = sizeof(inbuf);
277 1.1 dholland req.timeout = 1000;
278 1.1 dholland
279 1.35 christos if (!disk_ioctl(dd->dd_name, ATAIOCCOMMAND, &req)
280 1.35 christos || req.retsts != ATACMD_OK)
281 1.1 dholland return 0;
282 1.1 dholland
283 1.1 dholland #if BYTE_ORDER == LITTLE_ENDIAN
284 1.1 dholland /*
285 1.1 dholland * On little endian machines, we need to shuffle the string
286 1.1 dholland * byte order. However, we don't have to do this for NEC or
287 1.1 dholland * Mitsumi ATAPI devices
288 1.1 dholland */
289 1.1 dholland
290 1.1 dholland if (!(inqbuf->atap_config != WDC_CFG_CFA_MAGIC &&
291 1.1 dholland (inqbuf->atap_config & WDC_CFG_ATAPI) &&
292 1.1 dholland ((inqbuf->atap_model[0] == 'N' &&
293 1.10 isaki inqbuf->atap_model[1] == 'E') ||
294 1.1 dholland (inqbuf->atap_model[0] == 'F' &&
295 1.10 isaki inqbuf->atap_model[1] == 'X')))) {
296 1.1 dholland needswap = 1;
297 1.1 dholland }
298 1.1 dholland #endif
299 1.1 dholland
300 1.1 dholland ata_extract_string(model, sizeof(model),
301 1.1 dholland inqbuf->atap_model, sizeof(inqbuf->atap_model), needswap);
302 1.1 dholland humanize_number(size, sizeof(size),
303 1.1 dholland (uint64_t)dd->dd_secsize * (uint64_t)dd->dd_totsec,
304 1.1 dholland "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
305 1.1 dholland
306 1.1 dholland snprintf(dd->dd_descr, sizeof(dd->dd_descr), "%s (%s, %s)",
307 1.1 dholland dd->dd_name, size, model);
308 1.1 dholland
309 1.1 dholland return 1;
310 1.1 dholland }
311 1.1 dholland
312 1.1 dholland static void
313 1.1 dholland get_descr(struct disk_desc *dd)
314 1.1 dholland {
315 1.35 christos char size[5];
316 1.1 dholland dd->dd_descr[0] = '\0';
317 1.1 dholland
318 1.1 dholland /* try ATA */
319 1.35 christos if (get_descr_ata(dd))
320 1.1 dholland goto done;
321 1.1 dholland /* try SCSI */
322 1.35 christos if (get_descr_scsi(dd))
323 1.1 dholland goto done;
324 1.30 martin
325 1.30 martin /* XXX: identify for ld @ NVME or microSD */
326 1.30 martin
327 1.2 martin /* XXX: get description from raid, cgd, vnd... */
328 1.35 christos done:
329 1.30 martin /* punt, just give some generic info */
330 1.30 martin humanize_number(size, sizeof(size),
331 1.30 martin (uint64_t)dd->dd_secsize * (uint64_t)dd->dd_totsec,
332 1.30 martin "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
333 1.30 martin
334 1.30 martin snprintf(dd->dd_descr, sizeof(dd->dd_descr),
335 1.30 martin "%s (%s)", dd->dd_name, size);
336 1.1 dholland }
337 1.1 dholland
338 1.21 martin /*
339 1.21 martin * State for helper callback for get_default_cdrom
340 1.21 martin */
341 1.21 martin struct default_cdrom_data {
342 1.21 martin char *device;
343 1.21 martin size_t max_len;
344 1.21 martin bool found;
345 1.21 martin };
346 1.21 martin
347 1.21 martin /*
348 1.21 martin * Helper function for get_default_cdrom, gets passed a device
349 1.21 martin * name and a void pointer to default_cdrom_data.
350 1.21 martin */
351 1.21 martin static bool
352 1.21 martin get_default_cdrom_helper(void *state, const char *dev)
353 1.21 martin {
354 1.21 martin struct default_cdrom_data *data = state;
355 1.21 martin
356 1.25 martin if (!is_cdrom_device(dev, false))
357 1.22 martin return true;
358 1.22 martin
359 1.21 martin strlcpy(data->device, dev, data->max_len);
360 1.22 martin strlcat(data->device, "a", data->max_len); /* default to partition a */
361 1.21 martin data->found = true;
362 1.21 martin
363 1.21 martin return false; /* one is enough, stop iteration */
364 1.21 martin }
365 1.21 martin
366 1.21 martin /*
367 1.21 martin * Set the argument to the name of the first CD devices actually
368 1.21 martin * available, leave it unmodified otherwise.
369 1.21 martin * Return true if a device has been found.
370 1.1 dholland */
371 1.18 martin bool
372 1.18 martin get_default_cdrom(char *cd, size_t max_len)
373 1.1 dholland {
374 1.21 martin struct default_cdrom_data state;
375 1.21 martin
376 1.21 martin state.device = cd;
377 1.21 martin state.max_len = max_len;
378 1.21 martin state.found = false;
379 1.21 martin
380 1.21 martin if (enumerate_disks(&state, get_default_cdrom_helper))
381 1.21 martin return state.found;
382 1.21 martin
383 1.21 martin return false;
384 1.1 dholland }
385 1.1 dholland
386 1.30 martin static bool
387 1.15 martin get_wedge_descr(struct disk_desc *dd)
388 1.15 martin {
389 1.15 martin struct dkwedge_info dkw;
390 1.15 martin
391 1.35 christos if (!get_wedge_info(dd->dd_name, &dkw))
392 1.30 martin return false;
393 1.15 martin
394 1.35 christos snprintf(dd->dd_descr, sizeof(dd->dd_descr), "%s (%s@%s)",
395 1.35 christos dkw.dkw_wname, dkw.dkw_devname, dkw.dkw_parent);
396 1.35 christos return true;
397 1.15 martin }
398 1.15 martin
399 1.15 martin static bool
400 1.15 martin get_name_and_parent(const char *dev, char *name, char *parent)
401 1.15 martin {
402 1.15 martin struct dkwedge_info dkw;
403 1.15 martin
404 1.35 christos if (!get_wedge_info(dev, &dkw))
405 1.15 martin return false;
406 1.35 christos strcpy(name, (const char *)dkw.dkw_wname);
407 1.35 christos strcpy(parent, dkw.dkw_parent);
408 1.35 christos return true;
409 1.15 martin }
410 1.15 martin
411 1.15 martin static bool
412 1.15 martin find_swap_part_on(const char *dev, char *swap_name)
413 1.15 martin {
414 1.35 christos struct dkwedge_list dkwl;
415 1.15 martin struct dkwedge_info *dkw;
416 1.15 martin u_int i;
417 1.15 martin bool res = false;
418 1.15 martin
419 1.35 christos if (!get_wedge_list(dev, &dkwl))
420 1.15 martin return false;
421 1.15 martin
422 1.35 christos dkw = dkwl.dkwl_buf;
423 1.15 martin for (i = 0; i < dkwl.dkwl_nwedges; i++) {
424 1.15 martin res = strcmp(dkw[i].dkw_ptype, DKW_PTYPE_SWAP) == 0;
425 1.15 martin if (res) {
426 1.15 martin strcpy(swap_name, (const char*)dkw[i].dkw_wname);
427 1.15 martin break;
428 1.15 martin }
429 1.15 martin }
430 1.35 christos free(dkwl.dkwl_buf);
431 1.15 martin
432 1.15 martin return res;
433 1.15 martin }
434 1.15 martin
435 1.15 martin static bool
436 1.15 martin is_ffs_wedge(const char *dev)
437 1.15 martin {
438 1.15 martin struct dkwedge_info dkw;
439 1.15 martin
440 1.35 christos if (!get_wedge_info(dev, &dkw))
441 1.15 martin return false;
442 1.15 martin
443 1.35 christos return strcmp(dkw.dkw_ptype, DKW_PTYPE_FFS) == 0;
444 1.15 martin }
445 1.15 martin
446 1.18 martin /*
447 1.18 martin * Does this device match an entry in our default CDROM device list?
448 1.25 martin * If looking for install targets, we also flag floopy devices.
449 1.18 martin */
450 1.22 martin bool
451 1.25 martin is_cdrom_device(const char *dev, bool as_target)
452 1.18 martin {
453 1.25 martin static const char *target_devices[] = {
454 1.24 martin #ifdef CD_NAMES
455 1.24 martin CD_NAMES
456 1.24 martin #endif
457 1.24 martin #if defined(CD_NAMES) && defined(FLOPPY_NAMES)
458 1.24 martin ,
459 1.24 martin #endif
460 1.24 martin #ifdef FLOPPY_NAMES
461 1.24 martin FLOPPY_NAMES
462 1.24 martin #endif
463 1.24 martin #if defined(CD_NAMES) || defined(FLOPPY_NAMES)
464 1.24 martin ,
465 1.24 martin #endif
466 1.24 martin 0
467 1.24 martin };
468 1.25 martin static const char *src_devices[] = {
469 1.25 martin #ifdef CD_NAMES
470 1.25 martin CD_NAMES ,
471 1.25 martin #endif
472 1.25 martin 0
473 1.25 martin };
474 1.18 martin
475 1.25 martin for (const char **dev_pat = as_target ? target_devices : src_devices;
476 1.25 martin *dev_pat; dev_pat++)
477 1.20 martin if (fnmatch(*dev_pat, dev, 0) == 0)
478 1.20 martin return true;
479 1.18 martin
480 1.18 martin return false;
481 1.18 martin }
482 1.18 martin
483 1.27 martin /* does this device match any entry in the driver list? */
484 1.27 martin static bool
485 1.27 martin dev_in_list(const char *dev, const char **list)
486 1.27 martin {
487 1.27 martin
488 1.27 martin for ( ; *list; list++) {
489 1.27 martin
490 1.27 martin size_t len = strlen(*list);
491 1.27 martin
492 1.27 martin /* start of name matches? */
493 1.27 martin if (strncmp(dev, *list, len) == 0) {
494 1.27 martin char *endp;
495 1.27 martin int e;
496 1.27 martin
497 1.27 martin /* remainder of name is a decimal number? */
498 1.27 martin strtou(dev+len, &endp, 10, 0, INT_MAX, &e);
499 1.27 martin if (endp && *endp == 0 && e == 0)
500 1.27 martin return true;
501 1.27 martin }
502 1.27 martin }
503 1.27 martin
504 1.27 martin return false;
505 1.27 martin }
506 1.27 martin
507 1.27 martin bool
508 1.27 martin is_bootable_device(const char *dev)
509 1.27 martin {
510 1.27 martin static const char *non_bootable_devs[] = {
511 1.27 martin "raid", /* bootcode lives outside of raid */
512 1.27 martin "xbd", /* xen virtual device, can not boot from that */
513 1.27 martin NULL
514 1.27 martin };
515 1.27 martin
516 1.27 martin return !dev_in_list(dev, non_bootable_devs);
517 1.27 martin }
518 1.27 martin
519 1.27 martin bool
520 1.27 martin is_partitionable_device(const char *dev)
521 1.27 martin {
522 1.27 martin static const char *non_partitionable_devs[] = {
523 1.42 msaitoh "dk", /* this is already a partitioned slice */
524 1.27 martin NULL
525 1.27 martin };
526 1.27 martin
527 1.27 martin return !dev_in_list(dev, non_partitionable_devs);
528 1.27 martin }
529 1.27 martin
530 1.18 martin /*
531 1.18 martin * Multi-purpose helper function:
532 1.21 martin * iterate all known disks, invoke a callback for each.
533 1.21 martin * Stop iteration when the callback returns false.
534 1.21 martin * Return true when iteration actually happend, false on error.
535 1.18 martin */
536 1.22 martin bool
537 1.21 martin enumerate_disks(void *state, bool (*func)(void *state, const char *dev))
538 1.1 dholland {
539 1.17 martin static const int mib[] = { CTL_HW, HW_DISKNAMES };
540 1.17 martin static const unsigned int miblen = __arraycount(mib);
541 1.17 martin const char *xd;
542 1.21 martin char *disk_names;
543 1.17 martin size_t len;
544 1.1 dholland
545 1.21 martin if (sysctl(mib, miblen, NULL, &len, NULL, 0) == -1)
546 1.21 martin return false;
547 1.1 dholland
548 1.17 martin disk_names = malloc(len);
549 1.17 martin if (disk_names == NULL)
550 1.21 martin return false;
551 1.17 martin
552 1.17 martin if (sysctl(mib, miblen, disk_names, &len, NULL, 0) == -1) {
553 1.17 martin free(disk_names);
554 1.21 martin return false;
555 1.17 martin }
556 1.1 dholland
557 1.17 martin for (xd = strtok(disk_names, " "); xd != NULL; xd = strtok(NULL, " ")) {
558 1.21 martin if (!(*func)(state, xd))
559 1.21 martin break;
560 1.21 martin }
561 1.21 martin free(disk_names);
562 1.21 martin
563 1.21 martin return true;
564 1.21 martin }
565 1.21 martin
566 1.21 martin /*
567 1.21 martin * Helper state for get_disks
568 1.21 martin */
569 1.21 martin struct get_disks_state {
570 1.21 martin int numdisks;
571 1.21 martin struct disk_desc *dd;
572 1.21 martin bool with_non_partitionable;
573 1.21 martin };
574 1.21 martin
575 1.21 martin /*
576 1.21 martin * Helper function for get_disks enumartion
577 1.21 martin */
578 1.21 martin static bool
579 1.21 martin get_disks_helper(void *arg, const char *dev)
580 1.21 martin {
581 1.21 martin struct get_disks_state *state = arg;
582 1.30 martin struct disk_geom geo;
583 1.18 martin
584 1.21 martin /* is this a CD device? */
585 1.25 martin if (is_cdrom_device(dev, true))
586 1.21 martin return true;
587 1.21 martin
588 1.27 martin memset(state->dd, 0, sizeof(*state->dd));
589 1.21 martin strlcpy(state->dd->dd_name, dev, sizeof state->dd->dd_name - 2);
590 1.27 martin state->dd->dd_no_mbr = !is_bootable_device(dev);
591 1.27 martin state->dd->dd_no_part = !is_partitionable_device(dev);
592 1.21 martin
593 1.21 martin if (state->dd->dd_no_part && !state->with_non_partitionable)
594 1.21 martin return true;
595 1.1 dholland
596 1.30 martin if (!get_disk_geom(state->dd->dd_name, &geo)) {
597 1.21 martin if (errno == ENOENT)
598 1.21 martin return true;
599 1.21 martin if (errno != ENOTTY || !state->dd->dd_no_part)
600 1.21 martin /*
601 1.21 martin * Allow plain partitions,
602 1.21 martin * like already existing wedges
603 1.21 martin * (like dk0) if marked as
604 1.21 martin * non-partitioning device.
605 1.21 martin * For all other cases, continue
606 1.21 martin * with the next disk.
607 1.21 martin */
608 1.21 martin return true;
609 1.21 martin if (!is_ffs_wedge(state->dd->dd_name))
610 1.21 martin return true;
611 1.21 martin }
612 1.1 dholland
613 1.21 martin /*
614 1.21 martin * Exclude a disk mounted as root partition,
615 1.21 martin * in case of install-image on a USB memstick.
616 1.21 martin */
617 1.23 martin if (is_active_rootpart(state->dd->dd_name,
618 1.23 martin state->dd->dd_no_part ? -1 : 0))
619 1.21 martin return true;
620 1.17 martin
621 1.30 martin state->dd->dd_cyl = geo.dg_ncylinders;
622 1.30 martin state->dd->dd_head = geo.dg_ntracks;
623 1.30 martin state->dd->dd_sec = geo.dg_nsectors;
624 1.30 martin state->dd->dd_secsize = geo.dg_secsize;
625 1.30 martin state->dd->dd_totsec = geo.dg_secperunit;
626 1.30 martin
627 1.30 martin if (!state->dd->dd_no_part || !get_wedge_descr(state->dd))
628 1.21 martin get_descr(state->dd);
629 1.21 martin state->dd++;
630 1.21 martin state->numdisks++;
631 1.21 martin if (state->numdisks == MAX_DISKS)
632 1.21 martin return false;
633 1.21 martin
634 1.21 martin return true;
635 1.21 martin }
636 1.21 martin
637 1.21 martin /*
638 1.21 martin * Get all disk devices that are not CDs.
639 1.21 martin * Optionally leave out those that can not be partitioned further.
640 1.21 martin */
641 1.21 martin static int
642 1.21 martin get_disks(struct disk_desc *dd, bool with_non_partitionable)
643 1.21 martin {
644 1.21 martin struct get_disks_state state;
645 1.21 martin
646 1.21 martin /* initialize */
647 1.21 martin state.numdisks = 0;
648 1.21 martin state.dd = dd;
649 1.21 martin state.with_non_partitionable = with_non_partitionable;
650 1.21 martin
651 1.21 martin if (enumerate_disks(&state, get_disks_helper))
652 1.21 martin return state.numdisks;
653 1.21 martin
654 1.21 martin return 0;
655 1.1 dholland }
656 1.1 dholland
657 1.30 martin #ifdef DEBUG_VERBOSE
658 1.30 martin static void
659 1.30 martin dump_parts(const struct disk_partitions *parts)
660 1.30 martin {
661 1.30 martin fprintf(stderr, "%s partitions on %s:\n",
662 1.30 martin MSG_XLAT(parts->pscheme->short_name), parts->disk);
663 1.30 martin
664 1.30 martin for (size_t p = 0; p < parts->num_part; p++) {
665 1.30 martin struct disk_part_info info;
666 1.30 martin
667 1.30 martin if (parts->pscheme->get_part_info(
668 1.30 martin parts, p, &info)) {
669 1.30 martin fprintf(stderr, " #%zu: start: %" PRIu64 " "
670 1.30 martin "size: %" PRIu64 ", flags: %x\n",
671 1.30 martin p, info.start, info.size,
672 1.30 martin info.flags);
673 1.30 martin if (info.nat_type)
674 1.30 martin fprintf(stderr, "\ttype: %s\n",
675 1.30 martin info.nat_type->description);
676 1.30 martin } else {
677 1.30 martin fprintf(stderr, "failed to get info "
678 1.30 martin "for partition #%zu\n", p);
679 1.30 martin }
680 1.30 martin }
681 1.30 martin fprintf(stderr, "%" PRIu64 " sectors free, disk size %" PRIu64
682 1.30 martin " sectors, %zu partitions used\n", parts->free_space,
683 1.30 martin parts->disk_size, parts->num_part);
684 1.30 martin }
685 1.30 martin #endif
686 1.30 martin
687 1.30 martin static bool
688 1.30 martin delete_scheme(struct pm_devs *p)
689 1.30 martin {
690 1.30 martin
691 1.30 martin if (!ask_noyes(MSG_removepartswarn))
692 1.30 martin return false;
693 1.30 martin
694 1.30 martin p->parts->pscheme->free(p->parts);
695 1.30 martin p->parts = NULL;
696 1.30 martin return true;
697 1.30 martin }
698 1.30 martin
699 1.30 martin
700 1.30 martin static void
701 1.30 martin convert_copy(struct disk_partitions *old_parts,
702 1.30 martin struct disk_partitions *new_parts)
703 1.30 martin {
704 1.30 martin struct disk_part_info oinfo, ninfo;
705 1.30 martin part_id i;
706 1.30 martin
707 1.30 martin for (i = 0; i < old_parts->num_part; i++) {
708 1.30 martin if (!old_parts->pscheme->get_part_info(old_parts, i, &oinfo))
709 1.30 martin continue;
710 1.30 martin
711 1.30 martin if (oinfo.flags & PTI_PSCHEME_INTERNAL)
712 1.30 martin continue;
713 1.30 martin
714 1.30 martin if (oinfo.flags & PTI_SEC_CONTAINER) {
715 1.30 martin if (old_parts->pscheme->secondary_partitions) {
716 1.30 martin struct disk_partitions *sec_part =
717 1.30 martin old_parts->pscheme->
718 1.30 martin secondary_partitions(
719 1.32 martin old_parts, oinfo.start, false);
720 1.30 martin if (sec_part)
721 1.30 martin convert_copy(sec_part, new_parts);
722 1.30 martin }
723 1.30 martin continue;
724 1.30 martin }
725 1.30 martin
726 1.30 martin if (!new_parts->pscheme->adapt_foreign_part_info(new_parts,
727 1.30 martin &oinfo, &ninfo))
728 1.30 martin continue;
729 1.30 martin new_parts->pscheme->add_partition(new_parts, &ninfo, NULL);
730 1.30 martin }
731 1.30 martin }
732 1.30 martin
733 1.30 martin bool
734 1.30 martin convert_scheme(struct pm_devs *p, bool is_boot_drive, const char **err_msg)
735 1.30 martin {
736 1.30 martin struct disk_partitions *old_parts, *new_parts;
737 1.30 martin const struct disk_partitioning_scheme *new_scheme;
738 1.30 martin
739 1.30 martin *err_msg = NULL;
740 1.30 martin
741 1.30 martin old_parts = p->parts;
742 1.30 martin new_scheme = select_part_scheme(p, old_parts->pscheme,
743 1.30 martin false, MSG_select_other_partscheme);
744 1.30 martin
745 1.30 martin if (new_scheme == NULL)
746 1.30 martin return false;
747 1.30 martin
748 1.30 martin new_parts = new_scheme->create_new_for_disk(p->diskdev,
749 1.30 martin 0, p->dlsize, p->dlsize, is_boot_drive);
750 1.30 martin if (new_parts == NULL)
751 1.30 martin return false;
752 1.30 martin
753 1.30 martin convert_copy(old_parts, new_parts);
754 1.30 martin
755 1.30 martin if (new_parts->num_part == 0) {
756 1.30 martin /* need to cleanup */
757 1.30 martin new_parts->pscheme->free(new_parts);
758 1.30 martin return false;
759 1.30 martin }
760 1.30 martin
761 1.30 martin old_parts->pscheme->free(old_parts);
762 1.30 martin p->parts = new_parts;
763 1.30 martin return true;
764 1.30 martin }
765 1.30 martin
766 1.40 martin static struct pm_devs *
767 1.40 martin dummy_whole_system_pm(void)
768 1.40 martin {
769 1.40 martin static struct pm_devs whole_system = {
770 1.40 martin .diskdev = "/",
771 1.40 martin .no_mbr = true,
772 1.40 martin .no_part = true,
773 1.40 martin .cur_system = true,
774 1.40 martin };
775 1.40 martin static bool init = false;
776 1.40 martin
777 1.40 martin if (!init) {
778 1.40 martin strlcpy(whole_system.diskdev_descr,
779 1.40 martin msg_string(MSG_running_system),
780 1.40 martin sizeof whole_system.diskdev_descr);
781 1.40 martin }
782 1.40 martin
783 1.40 martin return &whole_system;
784 1.40 martin }
785 1.40 martin
786 1.1 dholland int
787 1.40 martin find_disks(const char *doingwhat, bool allow_cur_system)
788 1.1 dholland {
789 1.1 dholland struct disk_desc disks[MAX_DISKS];
790 1.40 martin /* need two more menu entries: current system + extended partitioning */
791 1.40 martin menu_ent dsk_menu[__arraycount(disks) + 2];
792 1.1 dholland struct disk_desc *disk;
793 1.15 martin int i = 0, skipped = 0;
794 1.15 martin int already_found, numdisks, selected_disk = -1;
795 1.1 dholland int menu_no;
796 1.30 martin struct pm_devs *pm_i, *pm_last = NULL;
797 1.30 martin
798 1.30 martin memset(dsk_menu, 0, sizeof(dsk_menu));
799 1.1 dholland
800 1.1 dholland /* Find disks. */
801 1.21 martin numdisks = get_disks(disks, partman_go <= 0);
802 1.1 dholland
803 1.1 dholland /* need a redraw here, kernel messages hose everything */
804 1.1 dholland touchwin(stdscr);
805 1.1 dholland refresh();
806 1.1 dholland /* Kill typeahead, it won't be what the user had in mind */
807 1.1 dholland fpurge(stdin);
808 1.1 dholland
809 1.10 isaki /*
810 1.10 isaki * partman_go: <0 - we want to see menu with extended partitioning
811 1.10 isaki * ==0 - we want to see simple select disk menu
812 1.10 isaki * >0 - we do not want to see any menus, just detect
813 1.10 isaki * all disks
814 1.10 isaki */
815 1.2 martin if (partman_go <= 0) {
816 1.40 martin if (numdisks == 0 && !allow_cur_system) {
817 1.2 martin /* No disks found! */
818 1.30 martin hit_enter_to_continue(MSG_nodisk, NULL);
819 1.2 martin /*endwin();*/
820 1.2 martin return -1;
821 1.2 martin } else {
822 1.40 martin /* One or more disks found or current system allowed */
823 1.40 martin i = 0;
824 1.40 martin if (allow_cur_system) {
825 1.40 martin dsk_menu[i].opt_name = MSG_running_system;
826 1.40 martin dsk_menu[i].opt_flags = OPT_EXIT;
827 1.40 martin dsk_menu[i].opt_action = set_menu_select;
828 1.40 martin i++;
829 1.40 martin }
830 1.43 martin for (; i < numdisks+allow_cur_system; i++) {
831 1.15 martin dsk_menu[i].opt_name =
832 1.40 martin disks[i-allow_cur_system].dd_descr;
833 1.2 martin dsk_menu[i].opt_flags = OPT_EXIT;
834 1.2 martin dsk_menu[i].opt_action = set_menu_select;
835 1.2 martin }
836 1.2 martin if (partman_go < 0) {
837 1.2 martin dsk_menu[i].opt_name = MSG_partman;
838 1.2 martin dsk_menu[i].opt_flags = OPT_EXIT;
839 1.2 martin dsk_menu[i].opt_action = set_menu_select;
840 1.40 martin i++;
841 1.2 martin }
842 1.2 martin menu_no = new_menu(MSG_Available_disks,
843 1.40 martin dsk_menu, i, -1,
844 1.15 martin 4, 0, 0, MC_SCROLL,
845 1.2 martin NULL, NULL, NULL, NULL, NULL);
846 1.2 martin if (menu_no == -1)
847 1.2 martin return -1;
848 1.33 christos msg_fmt_display(MSG_ask_disk, "%s", doingwhat);
849 1.2 martin process_menu(menu_no, &selected_disk);
850 1.2 martin free_menu(menu_no);
851 1.40 martin if (allow_cur_system) {
852 1.40 martin if (selected_disk == 0) {
853 1.40 martin pm = dummy_whole_system_pm();
854 1.40 martin return 1;
855 1.40 martin } else {
856 1.40 martin selected_disk--;
857 1.40 martin }
858 1.40 martin }
859 1.2 martin }
860 1.2 martin if (partman_go < 0 && selected_disk == numdisks) {
861 1.2 martin partman_go = 1;
862 1.10 isaki return -2;
863 1.2 martin } else
864 1.2 martin partman_go = 0;
865 1.2 martin if (selected_disk < 0 || selected_disk >= numdisks)
866 1.10 isaki return -1;
867 1.2 martin }
868 1.2 martin
869 1.2 martin /* Fill pm struct with device(s) info */
870 1.2 martin for (i = 0; i < numdisks; i++) {
871 1.2 martin if (! partman_go)
872 1.2 martin disk = disks + selected_disk;
873 1.2 martin else {
874 1.2 martin disk = disks + i;
875 1.2 martin already_found = 0;
876 1.2 martin SLIST_FOREACH(pm_i, &pm_head, l) {
877 1.2 martin pm_last = pm_i;
878 1.44 martin if (strcmp(pm_i->diskdev, disk->dd_name) == 0) {
879 1.44 martin already_found = 1;
880 1.2 martin break;
881 1.2 martin }
882 1.2 martin }
883 1.44 martin if (pm_i != NULL && already_found) {
884 1.44 martin /*
885 1.44 martin * We already added this device, but
886 1.44 martin * partitions might have changed
887 1.44 martin */
888 1.44 martin if (!pm_i->found) {
889 1.44 martin pm_i->found = true;
890 1.44 martin if (pm_i->parts == NULL) {
891 1.44 martin pm_i->parts =
892 1.44 martin partitions_read_disk(
893 1.44 martin pm_i->diskdev,
894 1.44 martin disk->dd_totsec);
895 1.44 martin }
896 1.44 martin }
897 1.2 martin continue;
898 1.44 martin }
899 1.1 dholland }
900 1.2 martin pm = pm_new;
901 1.2 martin pm->found = 1;
902 1.30 martin pm->ptstart = 0;
903 1.30 martin pm->ptsize = 0;
904 1.2 martin pm->bootable = 0;
905 1.2 martin strlcpy(pm->diskdev, disk->dd_name, sizeof pm->diskdev);
906 1.2 martin strlcpy(pm->diskdev_descr, disk->dd_descr, sizeof pm->diskdev_descr);
907 1.2 martin /* Use as a default disk if the user has the sets on a local disk */
908 1.2 martin strlcpy(localfs_dev, disk->dd_name, sizeof localfs_dev);
909 1.2 martin
910 1.30 martin /*
911 1.30 martin * Init disk size and geometry
912 1.30 martin */
913 1.30 martin pm->sectorsize = disk->dd_secsize;
914 1.30 martin pm->dlcyl = disk->dd_cyl;
915 1.30 martin pm->dlhead = disk->dd_head;
916 1.30 martin pm->dlsec = disk->dd_sec;
917 1.30 martin pm->dlsize = disk->dd_totsec;
918 1.30 martin if (pm->dlsize == 0)
919 1.30 martin pm->dlsize = disk->dd_cyl * disk->dd_head
920 1.30 martin * disk->dd_sec;
921 1.30 martin
922 1.30 martin pm->parts = partitions_read_disk(pm->diskdev, disk->dd_totsec);
923 1.30 martin
924 1.30 martin again:
925 1.30 martin
926 1.30 martin #ifdef DEBUG_VERBOSE
927 1.30 martin if (pm->parts) {
928 1.30 martin fputs("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n", stderr);
929 1.30 martin dump_parts(pm->parts);
930 1.30 martin
931 1.30 martin if (pm->parts->pscheme->secondary_partitions) {
932 1.30 martin const struct disk_partitions *sparts =
933 1.30 martin pm->parts->pscheme->secondary_partitions(
934 1.32 martin pm->parts, pm->ptstart, false);
935 1.30 martin if (sparts != NULL)
936 1.30 martin dump_parts(sparts);
937 1.30 martin }
938 1.30 martin }
939 1.30 martin #endif
940 1.30 martin
941 1.30 martin pm->no_mbr = disk->dd_no_mbr;
942 1.15 martin pm->no_part = disk->dd_no_part;
943 1.15 martin if (!pm->no_part) {
944 1.15 martin pm->sectorsize = disk->dd_secsize;
945 1.15 martin pm->dlcyl = disk->dd_cyl;
946 1.15 martin pm->dlhead = disk->dd_head;
947 1.15 martin pm->dlsec = disk->dd_sec;
948 1.15 martin pm->dlsize = disk->dd_totsec;
949 1.15 martin if (pm->dlsize == 0)
950 1.30 martin pm->dlsize = disk->dd_cyl * disk->dd_head
951 1.30 martin * disk->dd_sec;
952 1.30 martin
953 1.30 martin if (pm->parts && pm->parts->pscheme->size_limit != 0
954 1.30 martin && pm->dlsize > pm->parts->pscheme->size_limit
955 1.30 martin && ! partman_go) {
956 1.30 martin
957 1.30 martin char size[5], limit[5];
958 1.30 martin
959 1.30 martin humanize_number(size, sizeof(size),
960 1.30 martin (uint64_t)pm->dlsize * 512U,
961 1.30 martin "", HN_AUTOSCALE, HN_B | HN_NOSPACE
962 1.30 martin | HN_DECIMAL);
963 1.30 martin
964 1.30 martin humanize_number(limit, sizeof(limit),
965 1.30 martin (uint64_t)pm->parts->pscheme->size_limit
966 1.30 martin * 512U,
967 1.30 martin "", HN_AUTOSCALE, HN_B | HN_NOSPACE
968 1.30 martin | HN_DECIMAL);
969 1.30 martin
970 1.15 martin if (logfp)
971 1.30 martin fprintf(logfp,
972 1.30 martin "disk %s: is too big (%" PRIu64
973 1.30 martin " blocks, %s), will be truncated\n",
974 1.30 martin pm->diskdev, pm->dlsize,
975 1.30 martin size);
976 1.30 martin
977 1.30 martin msg_display_subst(MSG_toobigdisklabel, 5,
978 1.30 martin pm->diskdev,
979 1.30 martin msg_string(pm->parts->pscheme->name),
980 1.30 martin msg_string(pm->parts->pscheme->short_name),
981 1.30 martin size, limit);
982 1.30 martin
983 1.30 martin int sel = -1;
984 1.30 martin const char *err = NULL;
985 1.30 martin process_menu(MENU_convertscheme, &sel);
986 1.30 martin if (sel == 1) {
987 1.30 martin if (!delete_scheme(pm)) {
988 1.30 martin return -1;
989 1.30 martin }
990 1.30 martin goto again;
991 1.30 martin } else if (sel == 2) {
992 1.30 martin if (!convert_scheme(pm,
993 1.30 martin partman_go < 0, &err)) {
994 1.30 martin if (err != NULL)
995 1.30 martin err_msg_win(err);
996 1.30 martin return -1;
997 1.30 martin }
998 1.30 martin goto again;
999 1.30 martin } else if (sel == 3) {
1000 1.30 martin return -1;
1001 1.30 martin }
1002 1.30 martin pm->dlsize = pm->parts->pscheme->size_limit;
1003 1.15 martin }
1004 1.15 martin } else {
1005 1.15 martin pm->sectorsize = 0;
1006 1.15 martin pm->dlcyl = 0;
1007 1.15 martin pm->dlhead = 0;
1008 1.15 martin pm->dlsec = 0;
1009 1.15 martin pm->dlsize = 0;
1010 1.15 martin pm->no_mbr = 1;
1011 1.2 martin }
1012 1.2 martin pm->dlcylsize = pm->dlhead * pm->dlsec;
1013 1.2 martin
1014 1.2 martin if (partman_go) {
1015 1.2 martin pm_getrefdev(pm_new);
1016 1.2 martin if (SLIST_EMPTY(&pm_head) || pm_last == NULL)
1017 1.2 martin SLIST_INSERT_HEAD(&pm_head, pm_new, l);
1018 1.2 martin else
1019 1.2 martin SLIST_INSERT_AFTER(pm_last, pm_new, l);
1020 1.30 martin pm_new = malloc(sizeof (struct pm_devs));
1021 1.2 martin memset(pm_new, 0, sizeof *pm_new);
1022 1.2 martin } else
1023 1.30 martin /* We are not in partman and do not want to process
1024 1.30 martin * all devices, exit */
1025 1.2 martin break;
1026 1.1 dholland }
1027 1.1 dholland
1028 1.15 martin return numdisks-skipped;
1029 1.2 martin }
1030 1.2 martin
1031 1.30 martin static int
1032 1.30 martin sort_part_usage_by_mount(const void *a, const void *b)
1033 1.2 martin {
1034 1.30 martin const struct part_usage_info *pa = a, *pb = b;
1035 1.15 martin
1036 1.30 martin /* sort all real partitions by mount point */
1037 1.30 martin if ((pa->instflags & PUIINST_MOUNT) &&
1038 1.30 martin (pb->instflags & PUIINST_MOUNT))
1039 1.30 martin return strcmp(pa->mount, pb->mount);
1040 1.15 martin
1041 1.30 martin /* real partitions go first */
1042 1.30 martin if (pa->instflags & PUIINST_MOUNT)
1043 1.30 martin return -1;
1044 1.30 martin if (pb->instflags & PUIINST_MOUNT)
1045 1.30 martin return 1;
1046 1.8 martin
1047 1.30 martin /* arbitrary order for all other partitions */
1048 1.30 martin if (pa->type == PT_swap)
1049 1.30 martin return -1;
1050 1.30 martin if (pb->type == PT_swap)
1051 1.30 martin return 1;
1052 1.30 martin if (pa->type < pb->type)
1053 1.30 martin return -1;
1054 1.30 martin if (pa->type > pb->type)
1055 1.30 martin return 1;
1056 1.30 martin if (pa->cur_part_id < pb->cur_part_id)
1057 1.30 martin return -1;
1058 1.30 martin if (pa->cur_part_id > pb->cur_part_id)
1059 1.30 martin return 1;
1060 1.30 martin return (uintptr_t)a < (uintptr_t)b ? -1 : 1;
1061 1.1 dholland }
1062 1.1 dholland
1063 1.1 dholland int
1064 1.30 martin make_filesystems(struct install_partition_desc *install)
1065 1.1 dholland {
1066 1.30 martin int error = 0, partno = -1;
1067 1.30 martin char *newfs = NULL, devdev[PATH_MAX], rdev[PATH_MAX];
1068 1.30 martin size_t i;
1069 1.30 martin struct part_usage_info *ptn;
1070 1.30 martin struct disk_partitions *parts;
1071 1.30 martin const char *mnt_opts = NULL, *fsname = NULL;
1072 1.1 dholland
1073 1.40 martin if (pm->cur_system)
1074 1.40 martin return 1;
1075 1.40 martin
1076 1.15 martin if (pm->no_part) {
1077 1.15 martin /* check if this target device already has a ffs */
1078 1.30 martin snprintf(rdev, sizeof rdev, _PATH_DEV "/r%s", pm->diskdev);
1079 1.30 martin error = fsck_preen(rdev, "ffs", true);
1080 1.15 martin if (error) {
1081 1.15 martin if (!ask_noyes(MSG_No_filesystem_newfs))
1082 1.15 martin return EINVAL;
1083 1.15 martin error = run_program(RUN_DISPLAY | RUN_PROGRESS,
1084 1.30 martin "/sbin/newfs -V2 -O2 %s", rdev);
1085 1.15 martin }
1086 1.15 martin
1087 1.37 martin md_pre_mount(install, 0);
1088 1.15 martin
1089 1.15 martin make_target_dir("/");
1090 1.30 martin
1091 1.30 martin snprintf(devdev, sizeof devdev, _PATH_DEV "%s", pm->diskdev);
1092 1.15 martin error = target_mount_do("-o async", devdev, "/");
1093 1.15 martin if (error) {
1094 1.30 martin msg_display_subst(MSG_mountfail, 2, devdev, "/");
1095 1.30 martin hit_enter_to_continue(NULL, NULL);
1096 1.15 martin }
1097 1.30 martin
1098 1.15 martin return error;
1099 1.15 martin }
1100 1.15 martin
1101 1.1 dholland /* Making new file systems and mounting them */
1102 1.1 dholland
1103 1.1 dholland /* sort to ensure /usr/local is mounted after /usr (etc) */
1104 1.30 martin qsort(install->infos, install->num, sizeof(*install->infos),
1105 1.30 martin sort_part_usage_by_mount);
1106 1.1 dholland
1107 1.30 martin for (i = 0; i < install->num; i++) {
1108 1.1 dholland /*
1109 1.38 martin * Newfs all file systems mareked as needing this.
1110 1.38 martin * Mount the ones that have a mountpoint in the target.
1111 1.1 dholland */
1112 1.30 martin ptn = &install->infos[i];
1113 1.30 martin parts = ptn->parts;
1114 1.1 dholland
1115 1.30 martin if (ptn->size == 0 || parts == NULL)
1116 1.1 dholland continue;
1117 1.1 dholland
1118 1.30 martin if (parts->pscheme->get_part_device(parts, ptn->cur_part_id,
1119 1.30 martin devdev, sizeof devdev, &partno, parent_device_only, false)
1120 1.30 martin && is_active_rootpart(devdev, partno))
1121 1.30 martin continue;
1122 1.30 martin
1123 1.38 martin if (!(ptn->instflags & PUIINST_NEWFS))
1124 1.1 dholland continue;
1125 1.1 dholland
1126 1.30 martin parts->pscheme->get_part_device(parts, ptn->cur_part_id,
1127 1.30 martin devdev, sizeof devdev, &partno, plain_name, true);
1128 1.30 martin
1129 1.30 martin parts->pscheme->get_part_device(parts, ptn->cur_part_id,
1130 1.30 martin rdev, sizeof rdev, &partno, raw_dev_name, true);
1131 1.2 martin
1132 1.1 dholland newfs = NULL;
1133 1.30 martin switch (ptn->fs_type) {
1134 1.1 dholland case FS_APPLEUFS:
1135 1.30 martin asprintf(&newfs, "/sbin/newfs");
1136 1.30 martin mnt_opts = "-tffs -o async";
1137 1.30 martin fsname = "ffs";
1138 1.1 dholland break;
1139 1.1 dholland case FS_BSDFFS:
1140 1.1 dholland asprintf(&newfs,
1141 1.30 martin "/sbin/newfs -V2 -O %d",
1142 1.30 martin ptn->fs_version == 2 ? 2 : 1);
1143 1.30 martin if (ptn->mountflags & PUIMNT_LOG)
1144 1.30 martin mnt_opts = "-tffs -o log";
1145 1.1 dholland else
1146 1.30 martin mnt_opts = "-tffs -o async";
1147 1.30 martin fsname = "ffs";
1148 1.1 dholland break;
1149 1.1 dholland case FS_BSDLFS:
1150 1.30 martin asprintf(&newfs, "/sbin/newfs_lfs");
1151 1.30 martin mnt_opts = "-tlfs";
1152 1.30 martin fsname = "lfs";
1153 1.1 dholland break;
1154 1.1 dholland case FS_MSDOS:
1155 1.1 dholland asprintf(&newfs, "/sbin/newfs_msdos");
1156 1.30 martin mnt_opts = "-tmsdos";
1157 1.30 martin fsname = "msdos";
1158 1.1 dholland break;
1159 1.1 dholland #ifdef USE_SYSVBFS
1160 1.1 dholland case FS_SYSVBFS:
1161 1.1 dholland asprintf(&newfs, "/sbin/newfs_sysvbfs");
1162 1.30 martin mnt_opts = "-tsysvbfs";
1163 1.30 martin fsname = "sysvbfs";
1164 1.1 dholland break;
1165 1.1 dholland #endif
1166 1.1 dholland #ifdef USE_EXT2FS
1167 1.1 dholland case FS_EX2FS:
1168 1.1 dholland asprintf(&newfs, "/sbin/newfs_ext2fs");
1169 1.30 martin mnt_opts = "-text2fs";
1170 1.30 martin fsname = "ext2fs";
1171 1.1 dholland break;
1172 1.1 dholland #endif
1173 1.1 dholland }
1174 1.30 martin if ((ptn->instflags & PUIINST_NEWFS) && newfs != NULL) {
1175 1.30 martin if (ptn->fs_type == FS_MSDOS) {
1176 1.1 dholland /* newfs only if mount fails */
1177 1.1 dholland if (run_program(RUN_SILENT | RUN_ERROR_OK,
1178 1.30 martin "mount -rt msdos %s /mnt2", devdev) != 0)
1179 1.1 dholland error = run_program(
1180 1.1 dholland RUN_DISPLAY | RUN_PROGRESS,
1181 1.30 martin "%s %s",
1182 1.30 martin newfs, rdev);
1183 1.1 dholland else {
1184 1.10 isaki run_program(RUN_SILENT | RUN_ERROR_OK,
1185 1.1 dholland "umount /mnt2");
1186 1.1 dholland error = 0;
1187 1.1 dholland }
1188 1.30 martin } else {
1189 1.30 martin error = run_program(RUN_DISPLAY | RUN_PROGRESS,
1190 1.30 martin "%s %s", newfs, rdev);
1191 1.30 martin }
1192 1.1 dholland } else {
1193 1.1 dholland /* We'd better check it isn't dirty */
1194 1.30 martin error = fsck_preen(devdev, fsname, false);
1195 1.1 dholland }
1196 1.1 dholland free(newfs);
1197 1.30 martin if (error != 0)
1198 1.1 dholland return error;
1199 1.1 dholland
1200 1.30 martin ptn->instflags &= ~PUIINST_NEWFS;
1201 1.37 martin md_pre_mount(install, i);
1202 1.1 dholland
1203 1.30 martin if (partman_go == 0 && (ptn->instflags & PUIINST_MOUNT) &&
1204 1.30 martin mnt_opts != NULL) {
1205 1.30 martin make_target_dir(ptn->mount);
1206 1.30 martin error = target_mount_do(mnt_opts, devdev,
1207 1.30 martin ptn->mount);
1208 1.1 dholland if (error) {
1209 1.30 martin msg_display_subst(MSG_mountfail, 2, devdev,
1210 1.30 martin ptn->mount);
1211 1.30 martin hit_enter_to_continue(NULL, NULL);
1212 1.1 dholland return error;
1213 1.1 dholland }
1214 1.1 dholland }
1215 1.1 dholland }
1216 1.1 dholland return 0;
1217 1.1 dholland }
1218 1.1 dholland
1219 1.1 dholland int
1220 1.30 martin make_fstab(struct install_partition_desc *install)
1221 1.1 dholland {
1222 1.1 dholland FILE *f;
1223 1.30 martin const char *dump_dev = NULL;
1224 1.30 martin const char *dev;
1225 1.30 martin char dev_buf[PATH_MAX], swap_dev[PATH_MAX];
1226 1.30 martin
1227 1.40 martin if (pm->cur_system)
1228 1.40 martin return 1;
1229 1.40 martin
1230 1.30 martin swap_dev[0] = 0;
1231 1.1 dholland
1232 1.1 dholland /* Create the fstab. */
1233 1.1 dholland make_target_dir("/etc");
1234 1.1 dholland f = target_fopen("/etc/fstab", "w");
1235 1.2 martin scripting_fprintf(NULL, "cat <<EOF >%s/etc/fstab\n", target_prefix());
1236 1.2 martin
1237 1.1 dholland if (logfp)
1238 1.1 dholland (void)fprintf(logfp,
1239 1.30 martin "Making %s/etc/fstab (%s).\n", target_prefix(),
1240 1.30 martin pm->diskdev);
1241 1.10 isaki
1242 1.1 dholland if (f == NULL) {
1243 1.1 dholland msg_display(MSG_createfstab);
1244 1.1 dholland if (logfp)
1245 1.1 dholland (void)fprintf(logfp, "Failed to make /etc/fstab!\n");
1246 1.30 martin hit_enter_to_continue(NULL, NULL);
1247 1.2 martin #ifndef DEBUG
1248 1.1 dholland return 1;
1249 1.1 dholland #else
1250 1.1 dholland f = stdout;
1251 1.1 dholland #endif
1252 1.1 dholland }
1253 1.1 dholland
1254 1.16 martin scripting_fprintf(f, "# NetBSD /etc/fstab\n# See /usr/share/examples/"
1255 1.16 martin "fstab/ for more examples.\n");
1256 1.15 martin
1257 1.15 martin if (pm->no_part) {
1258 1.15 martin /* single dk? target */
1259 1.15 martin char buf[200], parent[200], swap[200], *prompt;
1260 1.15 martin int res;
1261 1.15 martin
1262 1.15 martin if (!get_name_and_parent(pm->diskdev, buf, parent))
1263 1.15 martin goto done_with_disks;
1264 1.15 martin scripting_fprintf(f, "NAME=%s\t/\tffs\trw\t\t1 1\n",
1265 1.15 martin buf);
1266 1.15 martin if (!find_swap_part_on(parent, swap))
1267 1.15 martin goto done_with_disks;
1268 1.30 martin const char *args[] = { parent, swap };
1269 1.30 martin prompt = str_arg_subst(msg_string(MSG_Auto_add_swap_part),
1270 1.30 martin __arraycount(args), args);
1271 1.15 martin res = ask_yesno(prompt);
1272 1.15 martin free(prompt);
1273 1.15 martin if (res)
1274 1.15 martin scripting_fprintf(f, "NAME=%s\tnone"
1275 1.15 martin "\tswap\tsw,dp\t\t0 0\n", swap);
1276 1.15 martin goto done_with_disks;
1277 1.15 martin }
1278 1.15 martin
1279 1.30 martin for (size_t i = 0; i < install->num; i++) {
1280 1.30 martin
1281 1.30 martin const struct part_usage_info *ptn = &install->infos[i];
1282 1.30 martin
1283 1.30 martin if (ptn->type != PT_swap &&
1284 1.30 martin (ptn->instflags & PUIINST_MOUNT) == 0)
1285 1.30 martin continue;
1286 1.30 martin
1287 1.30 martin const char *s = "";
1288 1.30 martin const char *mp = ptn->mount;
1289 1.30 martin const char *fstype = "ffs";
1290 1.30 martin int fsck_pass = 0, dump_freq = 0;
1291 1.30 martin
1292 1.30 martin if (ptn->parts->pscheme->get_part_device(ptn->parts,
1293 1.30 martin ptn->cur_part_id, dev_buf, sizeof dev_buf, NULL,
1294 1.30 martin logical_name, true))
1295 1.30 martin dev = dev_buf;
1296 1.30 martin else
1297 1.30 martin dev = NULL;
1298 1.30 martin
1299 1.30 martin if (!*mp) {
1300 1.30 martin /*
1301 1.30 martin * No mount point specified, comment out line and
1302 1.30 martin * use /mnt as a placeholder for the mount point.
1303 1.30 martin */
1304 1.30 martin s = "# ";
1305 1.30 martin mp = "/mnt";
1306 1.30 martin }
1307 1.30 martin
1308 1.30 martin switch (ptn->fs_type) {
1309 1.30 martin case FS_UNUSED:
1310 1.30 martin continue;
1311 1.30 martin case FS_BSDLFS:
1312 1.30 martin /* If there is no LFS, just comment it out. */
1313 1.30 martin if (!check_lfs_progs())
1314 1.1 dholland s = "# ";
1315 1.30 martin fstype = "lfs";
1316 1.30 martin /* FALLTHROUGH */
1317 1.30 martin case FS_BSDFFS:
1318 1.30 martin fsck_pass = (strcmp(mp, "/") == 0) ? 1 : 2;
1319 1.30 martin dump_freq = 1;
1320 1.30 martin break;
1321 1.30 martin case FS_MSDOS:
1322 1.30 martin fstype = "msdos";
1323 1.30 martin break;
1324 1.30 martin case FS_SWAP:
1325 1.30 martin if (swap_dev[0] == 0) {
1326 1.30 martin strncpy(swap_dev, dev, sizeof swap_dev);
1327 1.30 martin dump_dev = ",dp";
1328 1.30 martin } else {
1329 1.30 martin dump_dev = "";
1330 1.1 dholland }
1331 1.30 martin scripting_fprintf(f, "%s\t\tnone\tswap\tsw%s\t\t 0 0\n",
1332 1.30 martin dev, dump_dev);
1333 1.30 martin continue;
1334 1.1 dholland #ifdef USE_SYSVBFS
1335 1.30 martin case FS_SYSVBFS:
1336 1.30 martin fstype = "sysvbfs";
1337 1.30 martin make_target_dir("/stand");
1338 1.30 martin break;
1339 1.1 dholland #endif
1340 1.30 martin default:
1341 1.30 martin fstype = "???";
1342 1.30 martin s = "# ";
1343 1.30 martin break;
1344 1.2 martin }
1345 1.30 martin /* The code that remounts root rw doesn't check the partition */
1346 1.30 martin if (strcmp(mp, "/") == 0 &&
1347 1.30 martin (ptn->instflags & PUIINST_MOUNT) == 0)
1348 1.30 martin s = "# ";
1349 1.30 martin
1350 1.30 martin scripting_fprintf(f,
1351 1.30 martin "%s%s\t\t%s\t%s\trw%s%s%s%s%s%s%s%s\t\t %d %d\n",
1352 1.30 martin s, dev, mp, fstype,
1353 1.30 martin ptn->mountflags & PUIMNT_LOG ? ",log" : "",
1354 1.30 martin ptn->mountflags & PUIMNT_NOAUTO ? "" : ",noauto",
1355 1.30 martin ptn->mountflags & PUIMNT_ASYNC ? ",async" : "",
1356 1.30 martin ptn->mountflags & PUIMNT_NOATIME ? ",noatime" : "",
1357 1.30 martin ptn->mountflags & PUIMNT_NODEV ? ",nodev" : "",
1358 1.30 martin ptn->mountflags & PUIMNT_NODEVMTIME ? ",nodevmtime" : "",
1359 1.30 martin ptn->mountflags & PUIMNT_NOEXEC ? ",noexec" : "",
1360 1.30 martin ptn->mountflags & PUIMNT_NOSUID ? ",nosuid" : "",
1361 1.30 martin dump_freq, fsck_pass);
1362 1.1 dholland }
1363 1.30 martin
1364 1.15 martin done_with_disks:
1365 1.30 martin if (tmp_ramdisk_size > 0) {
1366 1.1 dholland #ifdef HAVE_TMPFS
1367 1.1 dholland scripting_fprintf(f, "tmpfs\t\t/tmp\ttmpfs\trw,-m=1777,-s=%"
1368 1.30 martin PRIu64 "\n",
1369 1.1 dholland tmp_ramdisk_size * 512);
1370 1.1 dholland #else
1371 1.30 martin if (swap_dev[0] != 0)
1372 1.30 martin scripting_fprintf(f, "%s\t\t/tmp\tmfs\trw,-s=%"
1373 1.30 martin PRIu64 "\n", swap_dev, tmp_ramdisk_size);
1374 1.1 dholland else
1375 1.1 dholland scripting_fprintf(f, "swap\t\t/tmp\tmfs\trw,-s=%"
1376 1.30 martin PRIu64 "\n", tmp_ramdisk_size);
1377 1.1 dholland #endif
1378 1.1 dholland }
1379 1.1 dholland
1380 1.18 martin if (cdrom_dev[0] == 0)
1381 1.18 martin get_default_cdrom(cdrom_dev, sizeof(cdrom_dev));
1382 1.18 martin
1383 1.1 dholland /* Add /kern, /proc and /dev/pts to fstab and make mountpoint. */
1384 1.1 dholland scripting_fprintf(f, "kernfs\t\t/kern\tkernfs\trw\n");
1385 1.1 dholland scripting_fprintf(f, "ptyfs\t\t/dev/pts\tptyfs\trw\n");
1386 1.1 dholland scripting_fprintf(f, "procfs\t\t/proc\tprocfs\trw\n");
1387 1.1 dholland scripting_fprintf(f, "/dev/%s\t\t/cdrom\tcd9660\tro,noauto\n",
1388 1.18 martin cdrom_dev);
1389 1.1 dholland scripting_fprintf(f, "%stmpfs\t\t/var/shm\ttmpfs\trw,-m1777,-sram%%25\n",
1390 1.1 dholland tmpfs_on_var_shm() ? "" : "#");
1391 1.1 dholland make_target_dir("/kern");
1392 1.1 dholland make_target_dir("/proc");
1393 1.1 dholland make_target_dir("/dev/pts");
1394 1.1 dholland make_target_dir("/cdrom");
1395 1.1 dholland make_target_dir("/var/shm");
1396 1.1 dholland
1397 1.1 dholland scripting_fprintf(NULL, "EOF\n");
1398 1.1 dholland
1399 1.1 dholland fclose(f);
1400 1.1 dholland fflush(NULL);
1401 1.1 dholland return 0;
1402 1.1 dholland }
1403 1.1 dholland
1404 1.1 dholland static int
1405 1.1 dholland /*ARGSUSED*/
1406 1.1 dholland foundffs(struct data *list, size_t num)
1407 1.1 dholland {
1408 1.1 dholland int error;
1409 1.45 martin char rbuf[PATH_MAX], buf[PATH_MAX];
1410 1.45 martin const char *rdev, *dev;
1411 1.1 dholland
1412 1.1 dholland if (num < 2 || strcmp(list[1].u.s_val, "/") == 0 ||
1413 1.1 dholland strstr(list[2].u.s_val, "noauto") != NULL)
1414 1.1 dholland return 0;
1415 1.1 dholland
1416 1.45 martin /* need the raw device for fsck_preen */
1417 1.45 martin if (strncmp(list[0].u.s_val, name_prefix, sizeof(name_prefix)-1)
1418 1.45 martin != 0) {
1419 1.45 martin strcpy(rbuf, "/dev/r");
1420 1.45 martin strlcat(rbuf, list[0].u.s_val, sizeof(rbuf));
1421 1.45 martin rdev = rbuf;
1422 1.45 martin strcpy(buf, "/dev/");
1423 1.45 martin strlcat(buf, list[0].u.s_val, sizeof(buf));
1424 1.45 martin dev = buf;
1425 1.45 martin } else {
1426 1.45 martin rdev = list[0].u.s_val;
1427 1.45 martin dev = list[0].u.s_val;
1428 1.45 martin }
1429 1.45 martin
1430 1.45 martin error = fsck_preen(rdev, "ffs", false);
1431 1.1 dholland if (error != 0)
1432 1.1 dholland return error;
1433 1.1 dholland
1434 1.45 martin error = target_mount("", dev, list[1].u.s_val);
1435 1.1 dholland if (error != 0) {
1436 1.33 christos msg_fmt_display(MSG_mount_failed, "%s", list[0].u.s_val);
1437 1.9 martin if (!ask_noyes(NULL))
1438 1.1 dholland return error;
1439 1.1 dholland }
1440 1.1 dholland return 0;
1441 1.1 dholland }
1442 1.1 dholland
1443 1.1 dholland #ifdef USE_SYSVBFS
1444 1.1 dholland static int
1445 1.1 dholland /*ARGSUSED*/
1446 1.1 dholland foundsysvbfs(struct data *list, size_t num)
1447 1.1 dholland {
1448 1.1 dholland int error;
1449 1.1 dholland
1450 1.1 dholland if (num < 2 || strcmp(list[1].u.s_val, "/") == 0 ||
1451 1.1 dholland strstr(list[2].u.s_val, "noauto") != NULL)
1452 1.1 dholland return 0;
1453 1.1 dholland
1454 1.30 martin error = target_mount("", list[0].u.s_val, list[1].u.s_val);
1455 1.1 dholland if (error != 0)
1456 1.1 dholland return error;
1457 1.1 dholland return 0;
1458 1.1 dholland }
1459 1.1 dholland #endif
1460 1.1 dholland
1461 1.1 dholland /*
1462 1.1 dholland * Do an fsck. On failure, inform the user by showing a warning
1463 1.1 dholland * message and doing menu_ok() before proceeding.
1464 1.30 martin * The device passed should be the full qualified path to raw disk
1465 1.30 martin * (e.g. /dev/rwd0a).
1466 1.1 dholland * Returns 0 on success, or nonzero return code from fsck() on failure.
1467 1.1 dholland */
1468 1.1 dholland static int
1469 1.30 martin fsck_preen(const char *disk, const char *fsname, bool silent)
1470 1.1 dholland {
1471 1.30 martin char *prog, err[12];
1472 1.1 dholland int error;
1473 1.1 dholland
1474 1.1 dholland if (fsname == NULL)
1475 1.1 dholland return 0;
1476 1.1 dholland /* first, check if fsck program exists, if not, assume ok */
1477 1.1 dholland asprintf(&prog, "/sbin/fsck_%s", fsname);
1478 1.1 dholland if (prog == NULL)
1479 1.1 dholland return 0;
1480 1.12 martin if (access(prog, X_OK) != 0) {
1481 1.12 martin free(prog);
1482 1.1 dholland return 0;
1483 1.12 martin }
1484 1.1 dholland if (!strcmp(fsname,"ffs"))
1485 1.30 martin fixsb(prog, disk);
1486 1.30 martin error = run_program(silent? RUN_SILENT|RUN_ERROR_OK : 0, "%s -p -q %s", prog, disk);
1487 1.1 dholland free(prog);
1488 1.15 martin if (error != 0 && !silent) {
1489 1.30 martin sprintf(err, "%d", error);
1490 1.30 martin msg_display_subst(msg_string(MSG_badfs), 3,
1491 1.30 martin disk, fsname, err);
1492 1.9 martin if (ask_noyes(NULL))
1493 1.1 dholland error = 0;
1494 1.1 dholland /* XXX at this point maybe we should run a full fsck? */
1495 1.1 dholland }
1496 1.1 dholland return error;
1497 1.1 dholland }
1498 1.1 dholland
1499 1.1 dholland /* This performs the same function as the etc/rc.d/fixsb script
1500 1.1 dholland * which attempts to correct problems with ffs1 filesystems
1501 1.1 dholland * which may have been introduced by booting a netbsd-current kernel
1502 1.1 dholland * from between April of 2003 and January 2004. For more information
1503 1.1 dholland * This script was developed as a response to NetBSD pr install/25138
1504 1.1 dholland * Additional prs regarding the original issue include:
1505 1.1 dholland * bin/17910 kern/21283 kern/21404 port-macppc/23925 port-macppc/23926
1506 1.1 dholland */
1507 1.1 dholland static void
1508 1.30 martin fixsb(const char *prog, const char *disk)
1509 1.1 dholland {
1510 1.1 dholland int fd;
1511 1.1 dholland int rval;
1512 1.1 dholland union {
1513 1.1 dholland struct fs fs;
1514 1.1 dholland char buf[SBLOCKSIZE];
1515 1.1 dholland } sblk;
1516 1.1 dholland struct fs *fs = &sblk.fs;
1517 1.1 dholland
1518 1.30 martin fd = open(disk, O_RDONLY);
1519 1.1 dholland if (fd == -1)
1520 1.1 dholland return;
1521 1.1 dholland
1522 1.1 dholland /* Read ffsv1 main superblock */
1523 1.1 dholland rval = pread(fd, sblk.buf, sizeof sblk.buf, SBLOCK_UFS1);
1524 1.1 dholland close(fd);
1525 1.1 dholland if (rval != sizeof sblk.buf)
1526 1.1 dholland return;
1527 1.1 dholland
1528 1.1 dholland if (fs->fs_magic != FS_UFS1_MAGIC &&
1529 1.1 dholland fs->fs_magic != FS_UFS1_MAGIC_SWAPPED)
1530 1.1 dholland /* Not FFSv1 */
1531 1.1 dholland return;
1532 1.1 dholland if (fs->fs_old_flags & FS_FLAGS_UPDATED)
1533 1.1 dholland /* properly updated fslevel 4 */
1534 1.1 dholland return;
1535 1.1 dholland if (fs->fs_bsize != fs->fs_maxbsize)
1536 1.1 dholland /* not messed up */
1537 1.1 dholland return;
1538 1.1 dholland
1539 1.1 dholland /*
1540 1.1 dholland * OK we have a munged fs, first 'upgrade' to fslevel 4,
1541 1.1 dholland * We specify -b16 in order to stop fsck bleating that the
1542 1.1 dholland * sb doesn't match the first alternate.
1543 1.1 dholland */
1544 1.1 dholland run_program(RUN_DISPLAY | RUN_PROGRESS,
1545 1.30 martin "%s -p -b 16 -c 4 %s", prog, disk);
1546 1.1 dholland /* Then downgrade to fslevel 3 */
1547 1.1 dholland run_program(RUN_DISPLAY | RUN_PROGRESS,
1548 1.30 martin "%s -p -c 3 %s", prog, disk);
1549 1.1 dholland }
1550 1.1 dholland
1551 1.1 dholland /*
1552 1.1 dholland * fsck and mount the root partition.
1553 1.30 martin * devdev is the fully qualified block device name.
1554 1.1 dholland */
1555 1.1 dholland static int
1556 1.30 martin mount_root(const char *devdev, struct install_partition_desc *install)
1557 1.1 dholland {
1558 1.1 dholland int error;
1559 1.1 dholland
1560 1.30 martin error = fsck_preen(devdev, "ffs", false);
1561 1.1 dholland if (error != 0)
1562 1.1 dholland return error;
1563 1.1 dholland
1564 1.37 martin md_pre_mount(install, 0);
1565 1.1 dholland
1566 1.30 martin /* Mount devdev on target's "".
1567 1.1 dholland * If we pass "" as mount-on, Prefixing will DTRT.
1568 1.1 dholland * for now, use no options.
1569 1.1 dholland * XXX consider -o remount in case target root is
1570 1.1 dholland * current root, still readonly from single-user?
1571 1.1 dholland */
1572 1.30 martin return target_mount("", devdev, "");
1573 1.1 dholland }
1574 1.1 dholland
1575 1.1 dholland /* Get information on the file systems mounted from the root filesystem.
1576 1.1 dholland * Offer to convert them into 4.4BSD inodes if they are not 4.4BSD
1577 1.1 dholland * inodes. Fsck them. Mount them.
1578 1.1 dholland */
1579 1.1 dholland
1580 1.1 dholland int
1581 1.30 martin mount_disks(struct install_partition_desc *install)
1582 1.1 dholland {
1583 1.1 dholland char *fstab;
1584 1.1 dholland int fstabsize;
1585 1.1 dholland int error;
1586 1.30 martin char devdev[PATH_MAX];
1587 1.39 martin size_t i;
1588 1.1 dholland
1589 1.40 martin if (install->cur_system)
1590 1.40 martin return 0;
1591 1.40 martin
1592 1.1 dholland static struct lookfor fstabbuf[] = {
1593 1.1 dholland {"/dev/", "/dev/%s %s ffs %s", "c", NULL, 0, 0, foundffs},
1594 1.1 dholland {"/dev/", "/dev/%s %s ufs %s", "c", NULL, 0, 0, foundffs},
1595 1.1 dholland #ifdef USE_SYSVBFS
1596 1.1 dholland {"/dev/", "/dev/%s %s sysvbfs %s", "c", NULL, 0, 0,
1597 1.1 dholland foundsysvbfs},
1598 1.1 dholland #endif
1599 1.1 dholland };
1600 1.1 dholland static size_t numfstabbuf = sizeof(fstabbuf) / sizeof(struct lookfor);
1601 1.1 dholland
1602 1.1 dholland /* First the root device. */
1603 1.1 dholland if (target_already_root())
1604 1.1 dholland /* avoid needing to call target_already_root() again */
1605 1.1 dholland targetroot_mnt[0] = 0;
1606 1.1 dholland else {
1607 1.39 martin for (i = 0; i < install->num; i++) {
1608 1.39 martin if (is_root_part_mount(install->infos[i].mount))
1609 1.39 martin break;
1610 1.39 martin }
1611 1.39 martin
1612 1.39 martin if (i >= install->num) {
1613 1.39 martin hit_enter_to_continue(MSG_noroot, NULL);
1614 1.39 martin return -1;
1615 1.39 martin }
1616 1.39 martin
1617 1.39 martin if (!install->infos[i].parts->pscheme->get_part_device(
1618 1.39 martin install->infos[i].parts, install->infos[i].cur_part_id,
1619 1.30 martin devdev, sizeof devdev, NULL, plain_name, true))
1620 1.30 martin return -1;
1621 1.30 martin error = mount_root(devdev, install);
1622 1.1 dholland if (error != 0 && error != EBUSY)
1623 1.1 dholland return -1;
1624 1.1 dholland }
1625 1.1 dholland
1626 1.1 dholland /* Check the target /etc/fstab exists before trying to parse it. */
1627 1.1 dholland if (target_dir_exists_p("/etc") == 0 ||
1628 1.1 dholland target_file_exists_p("/etc/fstab") == 0) {
1629 1.33 christos msg_fmt_display(MSG_noetcfstab, "%s", pm->diskdev);
1630 1.30 martin hit_enter_to_continue(NULL, NULL);
1631 1.1 dholland return -1;
1632 1.1 dholland }
1633 1.1 dholland
1634 1.1 dholland
1635 1.1 dholland /* Get fstab entries from the target-root /etc/fstab. */
1636 1.1 dholland fstabsize = target_collect_file(T_FILE, &fstab, "/etc/fstab");
1637 1.1 dholland if (fstabsize < 0) {
1638 1.1 dholland /* error ! */
1639 1.33 christos msg_fmt_display(MSG_badetcfstab, "%s", pm->diskdev);
1640 1.30 martin hit_enter_to_continue(NULL, NULL);
1641 1.2 martin return -2;
1642 1.1 dholland }
1643 1.1 dholland error = walk(fstab, (size_t)fstabsize, fstabbuf, numfstabbuf);
1644 1.1 dholland free(fstab);
1645 1.1 dholland
1646 1.1 dholland return error;
1647 1.1 dholland }
1648 1.1 dholland
1649 1.1 dholland int
1650 1.30 martin set_swap_if_low_ram(struct install_partition_desc *install)
1651 1.10 isaki {
1652 1.10 isaki if (get_ramsize() <= 32)
1653 1.30 martin return set_swap(install);
1654 1.10 isaki return 0;
1655 1.7 abs }
1656 1.7 abs
1657 1.7 abs int
1658 1.30 martin set_swap(struct install_partition_desc *install)
1659 1.1 dholland {
1660 1.30 martin size_t i;
1661 1.30 martin char dev_buf[PATH_MAX];
1662 1.1 dholland int rval;
1663 1.1 dholland
1664 1.30 martin for (i = 0; i < install->num; i++) {
1665 1.30 martin if (install->infos[i].type == PT_swap)
1666 1.30 martin break;
1667 1.30 martin }
1668 1.30 martin if (i >= install->num)
1669 1.30 martin return 0;
1670 1.1 dholland
1671 1.30 martin if (!install->infos[i].parts->pscheme->get_part_device(
1672 1.30 martin install->infos[i].parts, install->infos[i].cur_part_id, dev_buf,
1673 1.30 martin sizeof dev_buf, NULL, plain_name, true))
1674 1.30 martin return -1;
1675 1.30 martin
1676 1.30 martin rval = swapctl(SWAP_ON, dev_buf, 0);
1677 1.30 martin if (rval != 0)
1678 1.30 martin return -1;
1679 1.1 dholland
1680 1.1 dholland return 0;
1681 1.1 dholland }
1682 1.1 dholland
1683 1.1 dholland int
1684 1.1 dholland check_swap(const char *disk, int remove_swap)
1685 1.1 dholland {
1686 1.1 dholland struct swapent *swap;
1687 1.1 dholland char *cp;
1688 1.1 dholland int nswap;
1689 1.1 dholland int l;
1690 1.1 dholland int rval = 0;
1691 1.1 dholland
1692 1.1 dholland nswap = swapctl(SWAP_NSWAP, 0, 0);
1693 1.1 dholland if (nswap <= 0)
1694 1.1 dholland return 0;
1695 1.1 dholland
1696 1.1 dholland swap = malloc(nswap * sizeof *swap);
1697 1.1 dholland if (swap == NULL)
1698 1.1 dholland return -1;
1699 1.1 dholland
1700 1.1 dholland nswap = swapctl(SWAP_STATS, swap, nswap);
1701 1.1 dholland if (nswap < 0)
1702 1.1 dholland goto bad_swap;
1703 1.1 dholland
1704 1.1 dholland l = strlen(disk);
1705 1.1 dholland while (--nswap >= 0) {
1706 1.1 dholland /* Should we check the se_dev or se_path? */
1707 1.1 dholland cp = swap[nswap].se_path;
1708 1.1 dholland if (memcmp(cp, "/dev/", 5) != 0)
1709 1.1 dholland continue;
1710 1.1 dholland if (memcmp(cp + 5, disk, l) != 0)
1711 1.1 dholland continue;
1712 1.1 dholland if (!isalpha(*(unsigned char *)(cp + 5 + l)))
1713 1.1 dholland continue;
1714 1.1 dholland if (cp[5 + l + 1] != 0)
1715 1.1 dholland continue;
1716 1.1 dholland /* ok path looks like it is for this device */
1717 1.1 dholland if (!remove_swap) {
1718 1.1 dholland /* count active swap areas */
1719 1.1 dholland rval++;
1720 1.1 dholland continue;
1721 1.1 dholland }
1722 1.1 dholland if (swapctl(SWAP_OFF, cp, 0) == -1)
1723 1.1 dholland rval = -1;
1724 1.1 dholland }
1725 1.1 dholland
1726 1.1 dholland done:
1727 1.1 dholland free(swap);
1728 1.1 dholland return rval;
1729 1.1 dholland
1730 1.1 dholland bad_swap:
1731 1.1 dholland rval = -1;
1732 1.1 dholland goto done;
1733 1.1 dholland }
1734 1.1 dholland
1735 1.1 dholland #ifdef HAVE_BOOTXX_xFS
1736 1.1 dholland char *
1737 1.30 martin bootxx_name(struct install_partition_desc *install)
1738 1.1 dholland {
1739 1.1 dholland int fstype;
1740 1.1 dholland const char *bootxxname;
1741 1.1 dholland char *bootxx;
1742 1.1 dholland
1743 1.1 dholland /* check we have boot code for the root partition type */
1744 1.30 martin fstype = install->infos[0].fs_type;
1745 1.1 dholland switch (fstype) {
1746 1.1 dholland #if defined(BOOTXX_FFSV1) || defined(BOOTXX_FFSV2)
1747 1.1 dholland case FS_BSDFFS:
1748 1.30 martin if (install->infos[0].fs_version == 2) {
1749 1.1 dholland #ifdef BOOTXX_FFSV2
1750 1.1 dholland bootxxname = BOOTXX_FFSV2;
1751 1.1 dholland #else
1752 1.1 dholland bootxxname = NULL;
1753 1.1 dholland #endif
1754 1.1 dholland } else {
1755 1.1 dholland #ifdef BOOTXX_FFSV1
1756 1.1 dholland bootxxname = BOOTXX_FFSV1;
1757 1.1 dholland #else
1758 1.1 dholland bootxxname = NULL;
1759 1.1 dholland #endif
1760 1.1 dholland }
1761 1.1 dholland break;
1762 1.1 dholland #endif
1763 1.11 pgoyette #ifdef BOOTXX_LFSV2
1764 1.1 dholland case FS_BSDLFS:
1765 1.11 pgoyette bootxxname = BOOTXX_LFSV2;
1766 1.1 dholland break;
1767 1.1 dholland #endif
1768 1.1 dholland default:
1769 1.1 dholland bootxxname = NULL;
1770 1.1 dholland break;
1771 1.1 dholland }
1772 1.1 dholland
1773 1.1 dholland if (bootxxname == NULL)
1774 1.1 dholland return NULL;
1775 1.1 dholland
1776 1.1 dholland asprintf(&bootxx, "%s/%s", BOOTXXDIR, bootxxname);
1777 1.1 dholland return bootxx;
1778 1.1 dholland }
1779 1.1 dholland #endif
1780 1.2 martin
1781 1.2 martin /* from dkctl.c */
1782 1.2 martin static int
1783 1.2 martin get_dkwedges_sort(const void *a, const void *b)
1784 1.2 martin {
1785 1.2 martin const struct dkwedge_info *dkwa = a, *dkwb = b;
1786 1.2 martin const daddr_t oa = dkwa->dkw_offset, ob = dkwb->dkw_offset;
1787 1.2 martin return (oa < ob) ? -1 : (oa > ob) ? 1 : 0;
1788 1.2 martin }
1789 1.2 martin
1790 1.2 martin int
1791 1.2 martin get_dkwedges(struct dkwedge_info **dkw, const char *diskdev)
1792 1.2 martin {
1793 1.2 martin struct dkwedge_list dkwl;
1794 1.2 martin
1795 1.2 martin *dkw = NULL;
1796 1.35 christos if (!get_wedge_list(diskdev, &dkwl))
1797 1.2 martin return -1;
1798 1.2 martin
1799 1.35 christos if (dkwl.dkwl_nwedges > 0 && *dkw != NULL) {
1800 1.35 christos qsort(*dkw, dkwl.dkwl_nwedges, sizeof(**dkw),
1801 1.35 christos get_dkwedges_sort);
1802 1.2 martin }
1803 1.2 martin
1804 1.2 martin return dkwl.dkwl_nwedges;
1805 1.2 martin }
1806