Home | History | Annotate | Line # | Download | only in sysinst
disks.c revision 1.54
      1 /*	$NetBSD: disks.c,v 1.54 2019/10/25 12:49:58 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 						    disk->dd_no_mbr);
    906 					}
    907 				}
    908 				continue;
    909 			}
    910 		}
    911 		pm = pm_new;
    912 		pm->found = 1;
    913 		pm->ptstart = 0;
    914 		pm->ptsize = 0;
    915 		pm->bootable = 0;
    916 		strlcpy(pm->diskdev, disk->dd_name, sizeof pm->diskdev);
    917 		strlcpy(pm->diskdev_descr, disk->dd_descr, sizeof pm->diskdev_descr);
    918 		/* Use as a default disk if the user has the sets on a local disk */
    919 		strlcpy(localfs_dev, disk->dd_name, sizeof localfs_dev);
    920 
    921 		/*
    922 		 * Init disk size and geometry
    923 		 */
    924 		pm->sectorsize = disk->dd_secsize;
    925 		pm->dlcyl = disk->dd_cyl;
    926 		pm->dlhead = disk->dd_head;
    927 		pm->dlsec = disk->dd_sec;
    928 		pm->dlsize = disk->dd_totsec;
    929 		if (pm->dlsize == 0)
    930 			pm->dlsize = disk->dd_cyl * disk->dd_head
    931 			    * disk->dd_sec;
    932 
    933 		pm->parts = partitions_read_disk(pm->diskdev,
    934 		    disk->dd_totsec, disk->dd_no_mbr);
    935 
    936 again:
    937 
    938 #ifdef DEBUG_VERBOSE
    939 		if (pm->parts) {
    940 			fputs("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n", stderr);
    941 			dump_parts(pm->parts);
    942 
    943 			if (pm->parts->pscheme->secondary_partitions) {
    944 				const struct disk_partitions *sparts =
    945 				    pm->parts->pscheme->secondary_partitions(
    946 				    pm->parts, pm->ptstart, false);
    947 				if (sparts != NULL)
    948 					dump_parts(sparts);
    949 			}
    950 		}
    951 #endif
    952 
    953 		pm->no_mbr = disk->dd_no_mbr;
    954 		pm->no_part = disk->dd_no_part;
    955 		if (!pm->no_part) {
    956 			pm->sectorsize = disk->dd_secsize;
    957 			pm->dlcyl = disk->dd_cyl;
    958 			pm->dlhead = disk->dd_head;
    959 			pm->dlsec = disk->dd_sec;
    960 			pm->dlsize = disk->dd_totsec;
    961 			if (pm->dlsize == 0)
    962 				pm->dlsize = disk->dd_cyl * disk->dd_head
    963 				    * disk->dd_sec;
    964 
    965 			if (pm->parts && pm->parts->pscheme->size_limit != 0
    966 			    && pm->dlsize > pm->parts->pscheme->size_limit
    967 			    && ! partman_go) {
    968 
    969 				char size[5], limit[5];
    970 
    971 				humanize_number(size, sizeof(size),
    972 				    (uint64_t)pm->dlsize * 512U,
    973 				    "", HN_AUTOSCALE, HN_B | HN_NOSPACE
    974 				    | HN_DECIMAL);
    975 
    976 				humanize_number(limit, sizeof(limit),
    977 				    (uint64_t)pm->parts->pscheme->size_limit
    978 					* 512U,
    979 				    "", HN_AUTOSCALE, HN_B | HN_NOSPACE
    980 				    | HN_DECIMAL);
    981 
    982 				if (logfp)
    983 					fprintf(logfp,
    984 					    "disk %s: is too big (%" PRIu64
    985 					    " blocks, %s), will be truncated\n",
    986 						pm->diskdev, pm->dlsize,
    987 						size);
    988 
    989 				msg_display_subst(MSG_toobigdisklabel, 5,
    990 				   pm->diskdev,
    991 				   msg_string(pm->parts->pscheme->name),
    992 				   msg_string(pm->parts->pscheme->short_name),
    993 				   size, limit);
    994 
    995 				int sel = -1;
    996 				const char *err = NULL;
    997 				process_menu(MENU_convertscheme, &sel);
    998 				if (sel == 1) {
    999 					if (!delete_scheme(pm)) {
   1000 						return -1;
   1001 					}
   1002 					goto again;
   1003 				} else if (sel == 2) {
   1004 					if (!convert_scheme(pm,
   1005 					     partman_go < 0, &err)) {
   1006 						if (err != NULL)
   1007 							err_msg_win(err);
   1008 						return -1;
   1009 					}
   1010 					goto again;
   1011 				} else if (sel == 3) {
   1012 					return -1;
   1013 				}
   1014 				pm->dlsize = pm->parts->pscheme->size_limit;
   1015 			}
   1016 		} else {
   1017 			pm->sectorsize = 0;
   1018 			pm->dlcyl = 0;
   1019 			pm->dlhead = 0;
   1020 			pm->dlsec = 0;
   1021 			pm->dlsize = 0;
   1022 			pm->no_mbr = 1;
   1023 		}
   1024 		pm->dlcylsize = pm->dlhead * pm->dlsec;
   1025 
   1026 		if (partman_go) {
   1027 			pm_getrefdev(pm_new);
   1028 			if (SLIST_EMPTY(&pm_head) || pm_last == NULL)
   1029 				 SLIST_INSERT_HEAD(&pm_head, pm_new, l);
   1030 			else
   1031 				 SLIST_INSERT_AFTER(pm_last, pm_new, l);
   1032 			pm_new = malloc(sizeof (struct pm_devs));
   1033 			memset(pm_new, 0, sizeof *pm_new);
   1034 		} else
   1035 			/* We are not in partman and do not want to process
   1036 			 * all devices, exit */
   1037 			break;
   1038 	}
   1039 
   1040 	return numdisks-skipped;
   1041 }
   1042 
   1043 static int
   1044 sort_part_usage_by_mount(const void *a, const void *b)
   1045 {
   1046 	const struct part_usage_info *pa = a, *pb = b;
   1047 
   1048 	/* sort all real partitions by mount point */
   1049 	if ((pa->instflags & PUIINST_MOUNT) &&
   1050 	    (pb->instflags & PUIINST_MOUNT))
   1051 		return strcmp(pa->mount, pb->mount);
   1052 
   1053 	/* real partitions go first */
   1054 	if (pa->instflags & PUIINST_MOUNT)
   1055 		return -1;
   1056 	if (pb->instflags & PUIINST_MOUNT)
   1057 		return 1;
   1058 
   1059 	/* arbitrary order for all other partitions */
   1060 	if (pa->type == PT_swap)
   1061 		return -1;
   1062 	if (pb->type == PT_swap)
   1063 		return 1;
   1064 	if (pa->type < pb->type)
   1065 		return -1;
   1066 	if (pa->type > pb->type)
   1067 		return 1;
   1068 	if (pa->cur_part_id < pb->cur_part_id)
   1069 		return -1;
   1070 	if (pa->cur_part_id > pb->cur_part_id)
   1071 		return 1;
   1072 	return (uintptr_t)a < (uintptr_t)b ? -1 : 1;
   1073 }
   1074 
   1075 int
   1076 make_filesystems(struct install_partition_desc *install)
   1077 {
   1078 	int error = 0, partno = -1;
   1079 	char *newfs = NULL, devdev[PATH_MAX], rdev[PATH_MAX];
   1080 	size_t i;
   1081 	struct part_usage_info *ptn;
   1082 	struct disk_partitions *parts;
   1083 	const char *mnt_opts = NULL, *fsname = NULL;
   1084 
   1085 	if (pm->cur_system)
   1086 		return 1;
   1087 
   1088 	if (pm->no_part) {
   1089 		/* check if this target device already has a ffs */
   1090 		snprintf(rdev, sizeof rdev, _PATH_DEV "/r%s", pm->diskdev);
   1091 		error = fsck_preen(rdev, "ffs", true);
   1092 		if (error) {
   1093 			if (!ask_noyes(MSG_No_filesystem_newfs))
   1094 				return EINVAL;
   1095 			error = run_program(RUN_DISPLAY | RUN_PROGRESS,
   1096 			    "/sbin/newfs -V2 -O2 %s", rdev);
   1097 		}
   1098 
   1099 		md_pre_mount(install, 0);
   1100 
   1101 		make_target_dir("/");
   1102 
   1103 		snprintf(devdev, sizeof devdev, _PATH_DEV "%s", pm->diskdev);
   1104 		error = target_mount_do("-o async", devdev, "/");
   1105 		if (error) {
   1106 			msg_display_subst(MSG_mountfail, 2, devdev, "/");
   1107 			hit_enter_to_continue(NULL, NULL);
   1108 		}
   1109 
   1110 		return error;
   1111 	}
   1112 
   1113 	/* Making new file systems and mounting them */
   1114 
   1115 	/* sort to ensure /usr/local is mounted after /usr (etc) */
   1116 	qsort(install->infos, install->num, sizeof(*install->infos),
   1117 	    sort_part_usage_by_mount);
   1118 
   1119 	for (i = 0; i < install->num; i++) {
   1120 		/*
   1121 		 * Newfs all file systems mareked as needing this.
   1122 		 * Mount the ones that have a mountpoint in the target.
   1123 		 */
   1124 		ptn = &install->infos[i];
   1125 		parts = ptn->parts;
   1126 		newfs = NULL;
   1127 		fsname = NULL;
   1128 
   1129 		if (ptn->size == 0 || parts == NULL|| ptn->type == PT_swap)
   1130 			continue;
   1131 
   1132 		if (parts->pscheme->get_part_device(parts, ptn->cur_part_id,
   1133 		    devdev, sizeof devdev, &partno, parent_device_only, false)
   1134 		    && is_active_rootpart(devdev, partno))
   1135 			continue;
   1136 
   1137 		parts->pscheme->get_part_device(parts, ptn->cur_part_id,
   1138 		    devdev, sizeof devdev, &partno, plain_name, true);
   1139 
   1140 		parts->pscheme->get_part_device(parts, ptn->cur_part_id,
   1141 		    rdev, sizeof rdev, &partno, raw_dev_name, true);
   1142 
   1143 		switch (ptn->fs_type) {
   1144 		case FS_APPLEUFS:
   1145 			asprintf(&newfs, "/sbin/newfs");
   1146 			mnt_opts = "-tffs -o async";
   1147 			fsname = "ffs";
   1148 			break;
   1149 		case FS_BSDFFS:
   1150 			asprintf(&newfs,
   1151 			    "/sbin/newfs -V2 -O %d",
   1152 			    ptn->fs_version == 2 ? 2 : 1);
   1153 			if (ptn->mountflags & PUIMNT_LOG)
   1154 				mnt_opts = "-tffs -o log";
   1155 			else
   1156 				mnt_opts = "-tffs -o async";
   1157 			fsname = "ffs";
   1158 			break;
   1159 		case FS_BSDLFS:
   1160 			asprintf(&newfs, "/sbin/newfs_lfs");
   1161 			mnt_opts = "-tlfs";
   1162 			fsname = "lfs";
   1163 			break;
   1164 		case FS_MSDOS:
   1165 			asprintf(&newfs, "/sbin/newfs_msdos");
   1166 			mnt_opts = "-tmsdos";
   1167 			fsname = "msdos";
   1168 			break;
   1169 		case FS_SYSVBFS:
   1170 			asprintf(&newfs, "/sbin/newfs_sysvbfs");
   1171 			mnt_opts = "-tsysvbfs";
   1172 			fsname = "sysvbfs";
   1173 			break;
   1174 		case FS_V7:
   1175 			asprintf(&newfs, "/sbin/newfs_v7fs");
   1176 			mnt_opts = "-tv7fs";
   1177 			fsname = "v7fs";
   1178 			break;
   1179 		case FS_EX2FS:
   1180 			asprintf(&newfs, "/sbin/newfs_ext2fs");
   1181 			mnt_opts = "-text2fs";
   1182 			fsname = "ext2fs";
   1183 			break;
   1184 		}
   1185 		if ((ptn->instflags & PUIINST_NEWFS) && newfs != NULL) {
   1186 			if (ptn->fs_type == FS_MSDOS) {
   1187 			        /* newfs only if mount fails */
   1188 			        if (run_program(RUN_SILENT | RUN_ERROR_OK,
   1189 				    "mount -rt msdos %s /mnt2", devdev) != 0)
   1190 					error = run_program(
   1191 					    RUN_DISPLAY | RUN_PROGRESS,
   1192 					    "%s %s",
   1193 					    newfs, rdev);
   1194 				else {
   1195 					run_program(RUN_SILENT | RUN_ERROR_OK,
   1196 					    "umount /mnt2");
   1197 					error = 0;
   1198 				}
   1199 			} else {
   1200 				error = run_program(RUN_DISPLAY | RUN_PROGRESS,
   1201 			    "%s %s", newfs, rdev);
   1202 			}
   1203 		} else if ((ptn->instflags & (PUIINST_MOUNT|PUIINST_BOOT))
   1204 		    && fsname != NULL) {
   1205 			/* We'd better check it isn't dirty */
   1206 			error = fsck_preen(devdev, fsname, false);
   1207 		}
   1208 		free(newfs);
   1209 		if (error != 0)
   1210 			return error;
   1211 
   1212 		ptn->instflags &= ~PUIINST_NEWFS;
   1213 		md_pre_mount(install, i);
   1214 
   1215 		if (partman_go == 0 && (ptn->instflags & PUIINST_MOUNT) &&
   1216 				mnt_opts != NULL) {
   1217 			make_target_dir(ptn->mount);
   1218 			error = target_mount_do(mnt_opts, devdev,
   1219 			    ptn->mount);
   1220 			if (error) {
   1221 				msg_display_subst(MSG_mountfail, 2, devdev,
   1222 				    ptn->mount);
   1223 				hit_enter_to_continue(NULL, NULL);
   1224 				return error;
   1225 			}
   1226 		}
   1227 	}
   1228 	return 0;
   1229 }
   1230 
   1231 int
   1232 make_fstab(struct install_partition_desc *install)
   1233 {
   1234 	FILE *f;
   1235 	const char *dump_dev = NULL;
   1236 	const char *dev;
   1237 	char dev_buf[PATH_MAX], swap_dev[PATH_MAX];
   1238 
   1239 	if (pm->cur_system)
   1240 		return 1;
   1241 
   1242 	swap_dev[0] = 0;
   1243 
   1244 	/* Create the fstab. */
   1245 	make_target_dir("/etc");
   1246 	f = target_fopen("/etc/fstab", "w");
   1247 	scripting_fprintf(NULL, "cat <<EOF >%s/etc/fstab\n", target_prefix());
   1248 
   1249 	if (logfp)
   1250 		(void)fprintf(logfp,
   1251 		    "Making %s/etc/fstab (%s).\n", target_prefix(),
   1252 		    pm->diskdev);
   1253 
   1254 	if (f == NULL) {
   1255 		msg_display(MSG_createfstab);
   1256 		if (logfp)
   1257 			(void)fprintf(logfp, "Failed to make /etc/fstab!\n");
   1258 		hit_enter_to_continue(NULL, NULL);
   1259 #ifndef DEBUG
   1260 		return 1;
   1261 #else
   1262 		f = stdout;
   1263 #endif
   1264 	}
   1265 
   1266 	scripting_fprintf(f, "# NetBSD /etc/fstab\n# See /usr/share/examples/"
   1267 			"fstab/ for more examples.\n");
   1268 
   1269 	if (pm->no_part) {
   1270 		/* single dk? target */
   1271 		char buf[200], parent[200], swap[200], *prompt;
   1272 		int res;
   1273 
   1274 		if (!get_name_and_parent(pm->diskdev, buf, parent))
   1275 			goto done_with_disks;
   1276 		scripting_fprintf(f, NAME_PREFIX "%s\t/\tffs\trw\t\t1 1\n",
   1277 		    buf);
   1278 		if (!find_swap_part_on(parent, swap))
   1279 			goto done_with_disks;
   1280 		const char *args[] = { parent, swap };
   1281 		prompt = str_arg_subst(msg_string(MSG_Auto_add_swap_part),
   1282 		    __arraycount(args), args);
   1283 		res = ask_yesno(prompt);
   1284 		free(prompt);
   1285 		if (res)
   1286 			scripting_fprintf(f, NAME_PREFIX "%s\tnone"
   1287 			    "\tswap\tsw,dp\t\t0 0\n", swap);
   1288 		goto done_with_disks;
   1289 	}
   1290 
   1291 	for (size_t i = 0; i < install->num; i++) {
   1292 
   1293 		const struct part_usage_info *ptn = &install->infos[i];
   1294 
   1295 		if (ptn->size == 0)
   1296 			continue;
   1297 
   1298 		if (ptn->type != PT_swap &&
   1299 		    (ptn->instflags & PUIINST_MOUNT) == 0)
   1300 			continue;
   1301 
   1302 		const char *s = "";
   1303 		const char *mp = ptn->mount;
   1304 		const char *fstype = "ffs";
   1305 		int fsck_pass = 0, dump_freq = 0;
   1306 
   1307 		if (ptn->parts->pscheme->get_part_device(ptn->parts,
   1308 			    ptn->cur_part_id, dev_buf, sizeof dev_buf, NULL,
   1309 			    logical_name, true))
   1310 			dev = dev_buf;
   1311 		else
   1312 			dev = NULL;
   1313 
   1314 		if (!*mp) {
   1315 			/*
   1316 			 * No mount point specified, comment out line and
   1317 			 * use /mnt as a placeholder for the mount point.
   1318 			 */
   1319 			s = "# ";
   1320 			mp = "/mnt";
   1321 		}
   1322 
   1323 		switch (ptn->fs_type) {
   1324 		case FS_UNUSED:
   1325 			continue;
   1326 		case FS_BSDLFS:
   1327 			/* If there is no LFS, just comment it out. */
   1328 			if (!check_lfs_progs())
   1329 				s = "# ";
   1330 			fstype = "lfs";
   1331 			/* FALLTHROUGH */
   1332 		case FS_BSDFFS:
   1333 			fsck_pass = (strcmp(mp, "/") == 0) ? 1 : 2;
   1334 			dump_freq = 1;
   1335 			break;
   1336 		case FS_MSDOS:
   1337 			fstype = "msdos";
   1338 			break;
   1339 		case FS_SWAP:
   1340 			if (swap_dev[0] == 0) {
   1341 				strncpy(swap_dev, dev, sizeof swap_dev);
   1342 				dump_dev = ",dp";
   1343 			} else {
   1344 				dump_dev = "";
   1345 			}
   1346 			scripting_fprintf(f, "%s\t\tnone\tswap\tsw%s\t\t 0 0\n",
   1347 				dev, dump_dev);
   1348 			continue;
   1349 		case FS_SYSVBFS:
   1350 			fstype = "sysvbfs";
   1351 			make_target_dir("/stand");
   1352 			break;
   1353 		default:
   1354 			fstype = "???";
   1355 			s = "# ";
   1356 			break;
   1357 		}
   1358 		/* The code that remounts root rw doesn't check the partition */
   1359 		if (strcmp(mp, "/") == 0 &&
   1360 		    (ptn->instflags & PUIINST_MOUNT) == 0)
   1361 			s = "# ";
   1362 
   1363  		scripting_fprintf(f,
   1364 		  "%s%s\t\t%s\t%s\trw%s%s%s%s%s%s%s%s\t\t %d %d\n",
   1365 		   s, dev, mp, fstype,
   1366 		   ptn->mountflags & PUIMNT_LOG ? ",log" : "",
   1367 		   ptn->mountflags & PUIMNT_NOAUTO ? ",noauto" : "",
   1368 		   ptn->mountflags & PUIMNT_ASYNC ? ",async" : "",
   1369 		   ptn->mountflags & PUIMNT_NOATIME ? ",noatime" : "",
   1370 		   ptn->mountflags & PUIMNT_NODEV ? ",nodev" : "",
   1371 		   ptn->mountflags & PUIMNT_NODEVMTIME ? ",nodevmtime" : "",
   1372 		   ptn->mountflags & PUIMNT_NOEXEC ? ",noexec" : "",
   1373 		   ptn->mountflags & PUIMNT_NOSUID ? ",nosuid" : "",
   1374 		   dump_freq, fsck_pass);
   1375 	}
   1376 
   1377 done_with_disks:
   1378 	if (tmp_ramdisk_size > 0) {
   1379 #ifdef HAVE_TMPFS
   1380 		scripting_fprintf(f, "tmpfs\t\t/tmp\ttmpfs\trw,-m=1777,-s=%"
   1381 		    PRIu64 "\n",
   1382 		    tmp_ramdisk_size * 512);
   1383 #else
   1384 		if (swap_dev[0] != 0)
   1385 			scripting_fprintf(f, "%s\t\t/tmp\tmfs\trw,-s=%"
   1386 			    PRIu64 "\n", swap_dev, tmp_ramdisk_size);
   1387 		else
   1388 			scripting_fprintf(f, "swap\t\t/tmp\tmfs\trw,-s=%"
   1389 			    PRIu64 "\n", tmp_ramdisk_size);
   1390 #endif
   1391 	}
   1392 
   1393 	if (cdrom_dev[0] == 0)
   1394 		get_default_cdrom(cdrom_dev, sizeof(cdrom_dev));
   1395 
   1396 	/* Add /kern, /proc and /dev/pts to fstab and make mountpoint. */
   1397 	scripting_fprintf(f, "kernfs\t\t/kern\tkernfs\trw\n");
   1398 	scripting_fprintf(f, "ptyfs\t\t/dev/pts\tptyfs\trw\n");
   1399 	scripting_fprintf(f, "procfs\t\t/proc\tprocfs\trw\n");
   1400 	scripting_fprintf(f, "/dev/%s\t\t/cdrom\tcd9660\tro,noauto\n",
   1401 	    cdrom_dev);
   1402 	scripting_fprintf(f, "%stmpfs\t\t/var/shm\ttmpfs\trw,-m1777,-sram%%25\n",
   1403 	    tmpfs_on_var_shm() ? "" : "#");
   1404 	make_target_dir("/kern");
   1405 	make_target_dir("/proc");
   1406 	make_target_dir("/dev/pts");
   1407 	make_target_dir("/cdrom");
   1408 	make_target_dir("/var/shm");
   1409 
   1410 	scripting_fprintf(NULL, "EOF\n");
   1411 
   1412 	fclose(f);
   1413 	fflush(NULL);
   1414 	return 0;
   1415 }
   1416 
   1417 static bool
   1418 find_part_by_name(const char *name, struct disk_partitions **parts,
   1419     part_id *pno)
   1420 {
   1421 	struct pm_devs *i;
   1422 	struct disk_partitions *ps;
   1423 	part_id id;
   1424 	struct disk_desc disks[MAX_DISKS];
   1425 	int n, cnt;
   1426 
   1427 	if (SLIST_EMPTY(&pm_head)) {
   1428 		/*
   1429 		 * List has not been filled, only "pm" is valid - check
   1430 		 * that first.
   1431 		 */
   1432 		if (pm->parts->pscheme->find_by_name != NULL) {
   1433 			id = pm->parts->pscheme->find_by_name(pm->parts, name);
   1434 			if (id != NO_PART) {
   1435 				*pno = id;
   1436 				*parts = pm->parts;
   1437 				return true;
   1438 			}
   1439 		}
   1440 		/*
   1441 		 * Not that easy - check all other disks
   1442 		 */
   1443 		cnt = get_disks(disks, false);
   1444 		for (n = 0; n < cnt; n++) {
   1445 			if (strcmp(disks[n].dd_name, pm->diskdev) == 0)
   1446 				continue;
   1447 			ps = partitions_read_disk(disks[n].dd_name,
   1448 			    disks[n].dd_totsec, disks[n].dd_no_mbr);
   1449 			if (ps == NULL)
   1450 				continue;
   1451 			if (ps->pscheme->find_by_name == NULL)
   1452 				continue;
   1453 			id = ps->pscheme->find_by_name(ps, name);
   1454 			if (id != NO_PART) {
   1455 				*pno = id;
   1456 				*parts = ps;
   1457 				return true;	/* XXX this leaks memory */
   1458 			}
   1459 			ps->pscheme->free(ps);
   1460 		}
   1461 	} else {
   1462 		SLIST_FOREACH(i, &pm_head, l) {
   1463 			if (i->parts == NULL)
   1464 				continue;
   1465 			if (i->parts->pscheme->find_by_name == NULL)
   1466 				continue;
   1467 			id = i->parts->pscheme->find_by_name(i->parts, name);
   1468 			if (id == NO_PART)
   1469 				continue;
   1470 			*pno = id;
   1471 			*parts = i->parts;
   1472 			return true;
   1473 		}
   1474 	}
   1475 
   1476 	*pno = NO_PART;
   1477 	*parts = NULL;
   1478 	return false;
   1479 }
   1480 
   1481 static int
   1482 /*ARGSUSED*/
   1483 process_found_fs(struct data *list, size_t num, const struct lookfor *item,
   1484     bool with_fsck)
   1485 {
   1486 	int error;
   1487 	char rdev[PATH_MAX], dev[PATH_MAX],
   1488 	    options[STRSIZE], tmp[STRSIZE], *op, *last;
   1489 	const char *fsname = (const char*)item->var;
   1490 	part_id pno;
   1491 	struct disk_partitions *parts;
   1492 	size_t len;
   1493 	bool first, is_root;
   1494 
   1495 	if (num < 2 || strstr(list[2].u.s_val, "noauto") != NULL)
   1496 		return 0;
   1497 
   1498 	is_root = strcmp(list[1].u.s_val, "/") == 0;
   1499 	if (is_root && target_mounted())
   1500 		return 0;
   1501 
   1502 	if (strcmp(item->head, name_prefix) == 0) {
   1503 		/* this fstab entry uses NAME= syntax */
   1504 		if (!find_part_by_name(list[0].u.s_val,
   1505 		    &parts, &pno) || parts == NULL || pno == NO_PART)
   1506 			return 0;
   1507 		parts->pscheme->get_part_device(parts, pno,
   1508 		    dev, sizeof(dev), NULL, plain_name, true);
   1509 		parts->pscheme->get_part_device(parts, pno,
   1510 		    rdev, sizeof(rdev), NULL, raw_dev_name, true);
   1511 	} else {
   1512 		/* this fstab entry uses the plain device name */
   1513 		if (is_root) {
   1514 			/*
   1515 			 * PR 54480: we can not use the current device name
   1516 			 * as it might be different from the real environment.
   1517 			 * This is an abuse of the functionality, but it used
   1518 			 * to work before (and still does work if only a single
   1519 			 * target disk is involved).
   1520 			 * Use the device name from the current "pm" instead.
   1521 			 */
   1522 			strcpy(rdev, "/dev/r");
   1523 			strlcat(rdev, pm->diskdev, sizeof(rdev));
   1524 			strcpy(dev, "/dev/");
   1525 			strlcat(dev, pm->diskdev, sizeof(dev));
   1526 			/* copy over the partition letter, if any */
   1527 			len = strlen(list[0].u.s_val);
   1528 			if (list[0].u.s_val[len-1] >= 'a' &&
   1529 			    list[0].u.s_val[len-1] <=
   1530 			    ('a' + getmaxpartitions())) {
   1531 				strlcat(rdev, &list[0].u.s_val[len-1],
   1532 				    sizeof(rdev));
   1533 				strlcat(dev, &list[0].u.s_val[len-1],
   1534 				    sizeof(dev));
   1535 			}
   1536 		} else {
   1537 			strcpy(rdev, "/dev/r");
   1538 			strlcat(rdev, list[0].u.s_val, sizeof(rdev));
   1539 			strcpy(dev, "/dev/");
   1540 			strlcat(dev, list[0].u.s_val, sizeof(dev));
   1541 		}
   1542 	}
   1543 
   1544 	if (with_fsck) {
   1545 		/* need the raw device for fsck_preen */
   1546 		error = fsck_preen(rdev, fsname, false);
   1547 		if (error != 0)
   1548 			return error;
   1549 	}
   1550 
   1551 	/* add mount option for fs type */
   1552 	strcpy(options, "-t ");
   1553 	strlcat(options, fsname, sizeof(options));
   1554 
   1555 	/* extract mount options from fstab */
   1556 	strlcpy(tmp, list[2].u.s_val, sizeof(tmp));
   1557 	for (first = true, op = strtok_r(tmp, ",", &last); op != NULL;
   1558 	    op = strtok_r(NULL, ",", &last)) {
   1559 		if (strcmp(op, FSTAB_RW) == 0 ||
   1560 		    strcmp(op, FSTAB_RQ) == 0 ||
   1561 		    strcmp(op, FSTAB_RO) == 0 ||
   1562 		    strcmp(op, FSTAB_SW) == 0 ||
   1563 		    strcmp(op, FSTAB_DP) == 0 ||
   1564 		    strcmp(op, FSTAB_XX) == 0)
   1565 			continue;
   1566 		if (first) {
   1567 			first = false;
   1568 			strlcat(options, " -o ", sizeof(options));
   1569 		} else {
   1570 			strlcat(options, ",", sizeof(options));
   1571 		}
   1572 		strlcat(options, op, sizeof(options));
   1573 	}
   1574 
   1575 	error = target_mount(options, dev, list[1].u.s_val);
   1576 	if (error != 0) {
   1577 		msg_fmt_display(MSG_mount_failed, "%s", list[0].u.s_val);
   1578 		if (!ask_noyes(NULL))
   1579 			return error;
   1580 	}
   1581 	return 0;
   1582 }
   1583 
   1584 static int
   1585 /*ARGSUSED*/
   1586 found_fs(struct data *list, size_t num, const struct lookfor *item)
   1587 {
   1588 	return process_found_fs(list, num, item, true);
   1589 }
   1590 
   1591 static int
   1592 /*ARGSUSED*/
   1593 found_fs_nocheck(struct data *list, size_t num, const struct lookfor *item)
   1594 {
   1595 	return process_found_fs(list, num, item, false);
   1596 }
   1597 
   1598 /*
   1599  * Do an fsck. On failure, inform the user by showing a warning
   1600  * message and doing menu_ok() before proceeding.
   1601  * The device passed should be the full qualified path to raw disk
   1602  * (e.g. /dev/rwd0a).
   1603  * Returns 0 on success, or nonzero return code from fsck() on failure.
   1604  */
   1605 static int
   1606 fsck_preen(const char *disk, const char *fsname, bool silent)
   1607 {
   1608 	char *prog, err[12];
   1609 	int error;
   1610 
   1611 	if (fsname == NULL)
   1612 		return 0;
   1613 	/* first, check if fsck program exists, if not, assume ok */
   1614 	asprintf(&prog, "/sbin/fsck_%s", fsname);
   1615 	if (prog == NULL)
   1616 		return 0;
   1617 	if (access(prog, X_OK) != 0) {
   1618 		free(prog);
   1619 		return 0;
   1620 	}
   1621 	if (!strcmp(fsname,"ffs"))
   1622 		fixsb(prog, disk);
   1623 	error = run_program(silent? RUN_SILENT|RUN_ERROR_OK : 0, "%s -p -q %s", prog, disk);
   1624 	free(prog);
   1625 	if (error != 0 && !silent) {
   1626 		sprintf(err, "%d", error);
   1627 		msg_display_subst(msg_string(MSG_badfs), 3,
   1628 		    disk, fsname, err);
   1629 		if (ask_noyes(NULL))
   1630 			error = 0;
   1631 		/* XXX at this point maybe we should run a full fsck? */
   1632 	}
   1633 	return error;
   1634 }
   1635 
   1636 /* This performs the same function as the etc/rc.d/fixsb script
   1637  * which attempts to correct problems with ffs1 filesystems
   1638  * which may have been introduced by booting a netbsd-current kernel
   1639  * from between April of 2003 and January 2004. For more information
   1640  * This script was developed as a response to NetBSD pr install/25138
   1641  * Additional prs regarding the original issue include:
   1642  *  bin/17910 kern/21283 kern/21404 port-macppc/23925 port-macppc/23926
   1643  */
   1644 static void
   1645 fixsb(const char *prog, const char *disk)
   1646 {
   1647 	int fd;
   1648 	int rval;
   1649 	union {
   1650 		struct fs fs;
   1651 		char buf[SBLOCKSIZE];
   1652 	} sblk;
   1653 	struct fs *fs = &sblk.fs;
   1654 
   1655 	fd = open(disk, O_RDONLY);
   1656 	if (fd == -1)
   1657 		return;
   1658 
   1659 	/* Read ffsv1 main superblock */
   1660 	rval = pread(fd, sblk.buf, sizeof sblk.buf, SBLOCK_UFS1);
   1661 	close(fd);
   1662 	if (rval != sizeof sblk.buf)
   1663 		return;
   1664 
   1665 	if (fs->fs_magic != FS_UFS1_MAGIC &&
   1666 	    fs->fs_magic != FS_UFS1_MAGIC_SWAPPED)
   1667 		/* Not FFSv1 */
   1668 		return;
   1669 	if (fs->fs_old_flags & FS_FLAGS_UPDATED)
   1670 		/* properly updated fslevel 4 */
   1671 		return;
   1672 	if (fs->fs_bsize != fs->fs_maxbsize)
   1673 		/* not messed up */
   1674 		return;
   1675 
   1676 	/*
   1677 	 * OK we have a munged fs, first 'upgrade' to fslevel 4,
   1678 	 * We specify -b16 in order to stop fsck bleating that the
   1679 	 * sb doesn't match the first alternate.
   1680 	 */
   1681 	run_program(RUN_DISPLAY | RUN_PROGRESS,
   1682 	    "%s -p -b 16 -c 4 %s", prog, disk);
   1683 	/* Then downgrade to fslevel 3 */
   1684 	run_program(RUN_DISPLAY | RUN_PROGRESS,
   1685 	    "%s -p -c 3 %s", prog, disk);
   1686 }
   1687 
   1688 /*
   1689  * fsck and mount the root partition.
   1690  * devdev is the fully qualified block device name.
   1691  */
   1692 static int
   1693 mount_root(const char *devdev, bool first, bool writeable,
   1694      struct install_partition_desc *install)
   1695 {
   1696 	int	error;
   1697 
   1698 	error = fsck_preen(devdev, "ffs", false);
   1699 	if (error != 0)
   1700 		return error;
   1701 
   1702 	if (first)
   1703 		md_pre_mount(install, 0);
   1704 
   1705 	/* Mount devdev on target's "".
   1706 	 * If we pass "" as mount-on, Prefixing will DTRT.
   1707 	 * for now, use no options.
   1708 	 * XXX consider -o remount in case target root is
   1709 	 * current root, still readonly from single-user?
   1710 	 */
   1711 	return target_mount(writeable? "" : "-r", devdev, "");
   1712 }
   1713 
   1714 /* Get information on the file systems mounted from the root filesystem.
   1715  * Offer to convert them into 4.4BSD inodes if they are not 4.4BSD
   1716  * inodes.  Fsck them.  Mount them.
   1717  */
   1718 
   1719 int
   1720 mount_disks(struct install_partition_desc *install)
   1721 {
   1722 	char *fstab;
   1723 	int   fstabsize;
   1724 	int   error;
   1725 	char devdev[PATH_MAX];
   1726 	size_t i, num_fs_types, num_entries;
   1727 	struct lookfor *fstabbuf, *l;
   1728 
   1729 	if (install->cur_system)
   1730 		return 0;
   1731 
   1732 	/*
   1733 	 * Check what file system tools are available and create parsers
   1734 	 * for the corresponding fstab(5) entries - all others will be
   1735 	 * ignored.
   1736 	 */
   1737 	num_fs_types = 1;	/* ffs is implicit */
   1738 	for (i = 0; i < __arraycount(extern_fs_with_chk); i++) {
   1739 		sprintf(devdev, "/sbin/newfs_%s", extern_fs_with_chk[i]);
   1740 		if (file_exists_p(devdev))
   1741 			num_fs_types++;
   1742 	}
   1743 	for (i = 0; i < __arraycount(extern_fs_newfs_only); i++) {
   1744 		sprintf(devdev, "/sbin/newfs_%s", extern_fs_newfs_only[i]);
   1745 		if (file_exists_p(devdev))
   1746 			num_fs_types++;
   1747 	}
   1748 	num_entries = 2 *  num_fs_types + 1;	/* +1 for "ufs" special case */
   1749 	fstabbuf = calloc(num_entries, sizeof(*fstabbuf));
   1750 	if (fstabbuf == NULL)
   1751 		return -1;
   1752 	l = fstabbuf;
   1753 	l->head = "/dev/";
   1754 	l->fmt = strdup("/dev/%s %s ffs %s");
   1755 	l->todo = "c";
   1756 	l->var = __UNCONST("ffs");
   1757 	l->func = found_fs;
   1758 	l++;
   1759 	l->head = "/dev/";
   1760 	l->fmt = strdup("/dev/%s %s ufs %s");
   1761 	l->todo = "c";
   1762 	l->var = __UNCONST("ffs");
   1763 	l->func = found_fs;
   1764 	l++;
   1765 	l->head = NAME_PREFIX;
   1766 	l->fmt = strdup(NAME_PREFIX "%s %s ffs %s");
   1767 	l->todo = "c";
   1768 	l->var = __UNCONST("ffs");
   1769 	l->func = found_fs;
   1770 	l++;
   1771 	for (i = 0; i < __arraycount(extern_fs_with_chk); i++) {
   1772 		sprintf(devdev, "/sbin/newfs_%s", extern_fs_with_chk[i]);
   1773 		if (!file_exists_p(devdev))
   1774 			continue;
   1775 		sprintf(devdev, "/dev/%%s %%s %s %%s", extern_fs_with_chk[i]);
   1776 		l->head = "/dev/";
   1777 		l->fmt = strdup(devdev);
   1778 		l->todo = "c";
   1779 		l->var = __UNCONST(extern_fs_with_chk[i]);
   1780 		l->func = found_fs;
   1781 		l++;
   1782 		sprintf(devdev, NAME_PREFIX "%%s %%s %s %%s",
   1783 		    extern_fs_with_chk[i]);
   1784 		l->head = NAME_PREFIX;
   1785 		l->fmt = strdup(devdev);
   1786 		l->todo = "c";
   1787 		l->var = __UNCONST(extern_fs_with_chk[i]);
   1788 		l->func = found_fs;
   1789 		l++;
   1790 	}
   1791 	for (i = 0; i < __arraycount(extern_fs_newfs_only); i++) {
   1792 		sprintf(devdev, "/sbin/newfs_%s", extern_fs_newfs_only[i]);
   1793 		if (!file_exists_p(devdev))
   1794 			continue;
   1795 		sprintf(devdev, "/dev/%%s %%s %s %%s", extern_fs_newfs_only[i]);
   1796 		l->head = "/dev/";
   1797 		l->fmt = strdup(devdev);
   1798 		l->todo = "c";
   1799 		l->var = __UNCONST(extern_fs_newfs_only[i]);
   1800 		l->func = found_fs_nocheck;
   1801 		l++;
   1802 		sprintf(devdev, NAME_PREFIX "%%s %%s %s %%s",
   1803 		    extern_fs_newfs_only[i]);
   1804 		l->head = NAME_PREFIX;
   1805 		l->fmt = strdup(devdev);
   1806 		l->todo = "c";
   1807 		l->var = __UNCONST(extern_fs_newfs_only[i]);
   1808 		l->func = found_fs_nocheck;
   1809 		l++;
   1810 	}
   1811 	assert((size_t)(l - fstabbuf) == num_entries);
   1812 
   1813 	/* First the root device. */
   1814 	if (target_already_root())
   1815 		/* avoid needing to call target_already_root() again */
   1816 		targetroot_mnt[0] = 0;
   1817 	else {
   1818 		for (i = 0; i < install->num; i++) {
   1819 			if (is_root_part_mount(install->infos[i].mount))
   1820 				break;
   1821 		}
   1822 
   1823 		if (i >= install->num) {
   1824 			hit_enter_to_continue(MSG_noroot, NULL);
   1825 			return -1;
   1826 		}
   1827 
   1828 		if (!install->infos[i].parts->pscheme->get_part_device(
   1829 		    install->infos[i].parts, install->infos[i].cur_part_id,
   1830 		    devdev, sizeof devdev, NULL, plain_name, true))
   1831 			return -1;
   1832 		error = mount_root(devdev, true, false, install);
   1833 		if (error != 0 && error != EBUSY)
   1834 			return -1;
   1835 	}
   1836 
   1837 	/* Check the target /etc/fstab exists before trying to parse it. */
   1838 	if (target_dir_exists_p("/etc") == 0 ||
   1839 	    target_file_exists_p("/etc/fstab") == 0) {
   1840 		msg_fmt_display(MSG_noetcfstab, "%s", pm->diskdev);
   1841 		hit_enter_to_continue(NULL, NULL);
   1842 		return -1;
   1843 	}
   1844 
   1845 
   1846 	/* Get fstab entries from the target-root /etc/fstab. */
   1847 	fstabsize = target_collect_file(T_FILE, &fstab, "/etc/fstab");
   1848 	if (fstabsize < 0) {
   1849 		/* error ! */
   1850 		msg_fmt_display(MSG_badetcfstab, "%s", pm->diskdev);
   1851 		hit_enter_to_continue(NULL, NULL);
   1852 		umount_root();
   1853 		return -2;
   1854 	}
   1855 	/*
   1856 	 * We unmount the read-only root again, so we can mount it
   1857 	 * with proper options from /etc/fstab
   1858 	 */
   1859 	umount_root();
   1860 
   1861 	/*
   1862 	 * Now do all entries in /etc/fstab and mount them if required
   1863 	 */
   1864 	error = walk(fstab, (size_t)fstabsize, fstabbuf, num_entries);
   1865 	free(fstab);
   1866 	for (i = 0; i < num_entries; i++)
   1867 		free(__UNCONST(fstabbuf[i].fmt));
   1868 	free(fstabbuf);
   1869 
   1870 	return error;
   1871 }
   1872 
   1873 int
   1874 set_swap_if_low_ram(struct install_partition_desc *install)
   1875 {
   1876 	if (get_ramsize() <= 32)
   1877 		return set_swap(install);
   1878 	return 0;
   1879 }
   1880 
   1881 int
   1882 set_swap(struct install_partition_desc *install)
   1883 {
   1884 	size_t i;
   1885 	char dev_buf[PATH_MAX];
   1886 	int rval;
   1887 
   1888 	for (i = 0; i < install->num; i++) {
   1889 		if (install->infos[i].type == PT_swap)
   1890 			break;
   1891 	}
   1892 	if (i >= install->num)
   1893 		return 0;
   1894 
   1895 	if (!install->infos[i].parts->pscheme->get_part_device(
   1896 	    install->infos[i].parts, install->infos[i].cur_part_id, dev_buf,
   1897 	    sizeof dev_buf, NULL, plain_name, true))
   1898 		return -1;
   1899 
   1900 	rval = swapctl(SWAP_ON, dev_buf, 0);
   1901 	if (rval != 0)
   1902 		return -1;
   1903 
   1904 	return 0;
   1905 }
   1906 
   1907 int
   1908 check_swap(const char *disk, int remove_swap)
   1909 {
   1910 	struct swapent *swap;
   1911 	char *cp;
   1912 	int nswap;
   1913 	int l;
   1914 	int rval = 0;
   1915 
   1916 	nswap = swapctl(SWAP_NSWAP, 0, 0);
   1917 	if (nswap <= 0)
   1918 		return 0;
   1919 
   1920 	swap = malloc(nswap * sizeof *swap);
   1921 	if (swap == NULL)
   1922 		return -1;
   1923 
   1924 	nswap = swapctl(SWAP_STATS, swap, nswap);
   1925 	if (nswap < 0)
   1926 		goto bad_swap;
   1927 
   1928 	l = strlen(disk);
   1929 	while (--nswap >= 0) {
   1930 		/* Should we check the se_dev or se_path? */
   1931 		cp = swap[nswap].se_path;
   1932 		if (memcmp(cp, "/dev/", 5) != 0)
   1933 			continue;
   1934 		if (memcmp(cp + 5, disk, l) != 0)
   1935 			continue;
   1936 		if (!isalpha(*(unsigned char *)(cp + 5 + l)))
   1937 			continue;
   1938 		if (cp[5 + l + 1] != 0)
   1939 			continue;
   1940 		/* ok path looks like it is for this device */
   1941 		if (!remove_swap) {
   1942 			/* count active swap areas */
   1943 			rval++;
   1944 			continue;
   1945 		}
   1946 		if (swapctl(SWAP_OFF, cp, 0) == -1)
   1947 			rval = -1;
   1948 	}
   1949 
   1950     done:
   1951 	free(swap);
   1952 	return rval;
   1953 
   1954     bad_swap:
   1955 	rval = -1;
   1956 	goto done;
   1957 }
   1958 
   1959 #ifdef HAVE_BOOTXX_xFS
   1960 char *
   1961 bootxx_name(struct install_partition_desc *install)
   1962 {
   1963 	size_t i;
   1964 	int fstype = -1;
   1965 	const char *bootxxname;
   1966 	char *bootxx;
   1967 
   1968 	/* find a partition to be mounted as / */
   1969 	for (i = 0; i < install->num; i++) {
   1970 		if ((install->infos[i].instflags & PUIINST_MOUNT)
   1971 		    && strcmp(install->infos[i].mount, "/") == 0) {
   1972 			fstype = install->infos[i].fs_type;
   1973 			break;
   1974 		}
   1975 	}
   1976 	if (fstype < 0) {
   1977 		/* not found? take first root type partition instead */
   1978 		for (i = 0; i < install->num; i++) {
   1979 			if (install->infos[i].type == PT_root) {
   1980 				fstype = install->infos[i].fs_type;
   1981 				break;
   1982 			}
   1983 		}
   1984 	}
   1985 
   1986 	/* check we have boot code for the root partition type */
   1987 	switch (fstype) {
   1988 #if defined(BOOTXX_FFSV1) || defined(BOOTXX_FFSV2)
   1989 	case FS_BSDFFS:
   1990 		if (install->infos[0].fs_version == 2) {
   1991 #ifdef BOOTXX_FFSV2
   1992 			bootxxname = BOOTXX_FFSV2;
   1993 #else
   1994 			bootxxname = NULL;
   1995 #endif
   1996 		} else {
   1997 #ifdef BOOTXX_FFSV1
   1998 			bootxxname = BOOTXX_FFSV1;
   1999 #else
   2000 			bootxxname = NULL;
   2001 #endif
   2002 		}
   2003 		break;
   2004 #endif
   2005 #ifdef BOOTXX_LFSV2
   2006 	case FS_BSDLFS:
   2007 		bootxxname = BOOTXX_LFSV2;
   2008 		break;
   2009 #endif
   2010 	default:
   2011 		bootxxname = NULL;
   2012 		break;
   2013 	}
   2014 
   2015 	if (bootxxname == NULL)
   2016 		return NULL;
   2017 
   2018 	asprintf(&bootxx, "%s/%s", BOOTXXDIR, bootxxname);
   2019 	return bootxx;
   2020 }
   2021 #endif
   2022 
   2023 /* from dkctl.c */
   2024 static int
   2025 get_dkwedges_sort(const void *a, const void *b)
   2026 {
   2027 	const struct dkwedge_info *dkwa = a, *dkwb = b;
   2028 	const daddr_t oa = dkwa->dkw_offset, ob = dkwb->dkw_offset;
   2029 	return (oa < ob) ? -1 : (oa > ob) ? 1 : 0;
   2030 }
   2031 
   2032 int
   2033 get_dkwedges(struct dkwedge_info **dkw, const char *diskdev)
   2034 {
   2035 	struct dkwedge_list dkwl;
   2036 
   2037 	*dkw = NULL;
   2038 	if (!get_wedge_list(diskdev, &dkwl))
   2039 		return -1;
   2040 
   2041 	if (dkwl.dkwl_nwedges > 0 && *dkw != NULL) {
   2042 		qsort(*dkw, dkwl.dkwl_nwedges, sizeof(**dkw),
   2043 		    get_dkwedges_sort);
   2044 	}
   2045 
   2046 	return dkwl.dkwl_nwedges;
   2047 }
   2048