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