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