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