Home | History | Annotate | Line # | Download | only in sysinst
disks.c revision 1.44.2.9
      1 /*	$NetBSD: disks.c,v 1.44.2.9 2019/10/23 06:30:16 msaitoh Exp $ */
      2 
      3 /*
      4  * Copyright 1997 Piermont Information Systems Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Philip A. Nelson for Piermont Information Systems Inc.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. The name of Piermont Information Systems Inc. may not be used to endorse
     18  *    or promote products derived from this software without specific prior
     19  *    written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
     22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
     25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     31  * THE POSSIBILITY OF SUCH DAMAGE.
     32  *
     33  */
     34 
     35 /* disks.c -- routines to deal with finding disks and labeling disks. */
     36 
     37 
     38 #include <assert.h>
     39 #include <errno.h>
     40 #include <inttypes.h>
     41 #include <stdio.h>
     42 #include <stdlib.h>
     43 #include <unistd.h>
     44 #include <fcntl.h>
     45 #include <fnmatch.h>
     46 #include <util.h>
     47 #include <uuid.h>
     48 #include <paths.h>
     49 #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 		newfs = NULL;
   1125 		fsname = NULL;
   1126 
   1127 		if (ptn->size == 0 || parts == NULL|| ptn->type == PT_swap)
   1128 			continue;
   1129 
   1130 		if (parts->pscheme->get_part_device(parts, ptn->cur_part_id,
   1131 		    devdev, sizeof devdev, &partno, parent_device_only, false)
   1132 		    && is_active_rootpart(devdev, partno))
   1133 			continue;
   1134 
   1135 		parts->pscheme->get_part_device(parts, ptn->cur_part_id,
   1136 		    devdev, sizeof devdev, &partno, plain_name, true);
   1137 
   1138 		parts->pscheme->get_part_device(parts, ptn->cur_part_id,
   1139 		    rdev, sizeof rdev, &partno, raw_dev_name, true);
   1140 
   1141 		switch (ptn->fs_type) {
   1142 		case FS_APPLEUFS:
   1143 			asprintf(&newfs, "/sbin/newfs");
   1144 			mnt_opts = "-tffs -o async";
   1145 			fsname = "ffs";
   1146 			break;
   1147 		case FS_BSDFFS:
   1148 			asprintf(&newfs,
   1149 			    "/sbin/newfs -V2 -O %d",
   1150 			    ptn->fs_version == 2 ? 2 : 1);
   1151 			if (ptn->mountflags & PUIMNT_LOG)
   1152 				mnt_opts = "-tffs -o log";
   1153 			else
   1154 				mnt_opts = "-tffs -o async";
   1155 			fsname = "ffs";
   1156 			break;
   1157 		case FS_BSDLFS:
   1158 			asprintf(&newfs, "/sbin/newfs_lfs");
   1159 			mnt_opts = "-tlfs";
   1160 			fsname = "lfs";
   1161 			break;
   1162 		case FS_MSDOS:
   1163 			asprintf(&newfs, "/sbin/newfs_msdos");
   1164 			mnt_opts = "-tmsdos";
   1165 			fsname = "msdos";
   1166 			break;
   1167 		case FS_SYSVBFS:
   1168 			asprintf(&newfs, "/sbin/newfs_sysvbfs");
   1169 			mnt_opts = "-tsysvbfs";
   1170 			fsname = "sysvbfs";
   1171 			break;
   1172 		case FS_V7:
   1173 			asprintf(&newfs, "/sbin/newfs_v7fs");
   1174 			mnt_opts = "-tv7fs";
   1175 			fsname = "v7fs";
   1176 			break;
   1177 		case FS_EX2FS:
   1178 			asprintf(&newfs, "/sbin/newfs_ext2fs");
   1179 			mnt_opts = "-text2fs";
   1180 			fsname = "ext2fs";
   1181 			break;
   1182 		}
   1183 		if ((ptn->instflags & PUIINST_NEWFS) && newfs != NULL) {
   1184 			if (ptn->fs_type == FS_MSDOS) {
   1185 			        /* newfs only if mount fails */
   1186 			        if (run_program(RUN_SILENT | RUN_ERROR_OK,
   1187 				    "mount -rt msdos %s /mnt2", devdev) != 0)
   1188 					error = run_program(
   1189 					    RUN_DISPLAY | RUN_PROGRESS,
   1190 					    "%s %s",
   1191 					    newfs, rdev);
   1192 				else {
   1193 					run_program(RUN_SILENT | RUN_ERROR_OK,
   1194 					    "umount /mnt2");
   1195 					error = 0;
   1196 				}
   1197 			} else {
   1198 				error = run_program(RUN_DISPLAY | RUN_PROGRESS,
   1199 			    "%s %s", newfs, rdev);
   1200 			}
   1201 		} else if ((ptn->instflags & (PUIINST_MOUNT|PUIINST_BOOT))
   1202 		    && fsname != NULL) {
   1203 			/* We'd better check it isn't dirty */
   1204 			error = fsck_preen(devdev, fsname, false);
   1205 		}
   1206 		free(newfs);
   1207 		if (error != 0)
   1208 			return error;
   1209 
   1210 		ptn->instflags &= ~PUIINST_NEWFS;
   1211 		md_pre_mount(install, i);
   1212 
   1213 		if (partman_go == 0 && (ptn->instflags & PUIINST_MOUNT) &&
   1214 				mnt_opts != NULL) {
   1215 			make_target_dir(ptn->mount);
   1216 			error = target_mount_do(mnt_opts, devdev,
   1217 			    ptn->mount);
   1218 			if (error) {
   1219 				msg_display_subst(MSG_mountfail, 2, devdev,
   1220 				    ptn->mount);
   1221 				hit_enter_to_continue(NULL, NULL);
   1222 				return error;
   1223 			}
   1224 		}
   1225 	}
   1226 	return 0;
   1227 }
   1228 
   1229 int
   1230 make_fstab(struct install_partition_desc *install)
   1231 {
   1232 	FILE *f;
   1233 	const char *dump_dev = NULL;
   1234 	const char *dev;
   1235 	char dev_buf[PATH_MAX], swap_dev[PATH_MAX];
   1236 
   1237 	if (pm->cur_system)
   1238 		return 1;
   1239 
   1240 	swap_dev[0] = 0;
   1241 
   1242 	/* Create the fstab. */
   1243 	make_target_dir("/etc");
   1244 	f = target_fopen("/etc/fstab", "w");
   1245 	scripting_fprintf(NULL, "cat <<EOF >%s/etc/fstab\n", target_prefix());
   1246 
   1247 	if (logfp)
   1248 		(void)fprintf(logfp,
   1249 		    "Making %s/etc/fstab (%s).\n", target_prefix(),
   1250 		    pm->diskdev);
   1251 
   1252 	if (f == NULL) {
   1253 		msg_display(MSG_createfstab);
   1254 		if (logfp)
   1255 			(void)fprintf(logfp, "Failed to make /etc/fstab!\n");
   1256 		hit_enter_to_continue(NULL, NULL);
   1257 #ifndef DEBUG
   1258 		return 1;
   1259 #else
   1260 		f = stdout;
   1261 #endif
   1262 	}
   1263 
   1264 	scripting_fprintf(f, "# NetBSD /etc/fstab\n# See /usr/share/examples/"
   1265 			"fstab/ for more examples.\n");
   1266 
   1267 	if (pm->no_part) {
   1268 		/* single dk? target */
   1269 		char buf[200], parent[200], swap[200], *prompt;
   1270 		int res;
   1271 
   1272 		if (!get_name_and_parent(pm->diskdev, buf, parent))
   1273 			goto done_with_disks;
   1274 		scripting_fprintf(f, NAME_PREFIX "%s\t/\tffs\trw\t\t1 1\n",
   1275 		    buf);
   1276 		if (!find_swap_part_on(parent, swap))
   1277 			goto done_with_disks;
   1278 		const char *args[] = { parent, swap };
   1279 		prompt = str_arg_subst(msg_string(MSG_Auto_add_swap_part),
   1280 		    __arraycount(args), args);
   1281 		res = ask_yesno(prompt);
   1282 		free(prompt);
   1283 		if (res)
   1284 			scripting_fprintf(f, NAME_PREFIX "%s\tnone"
   1285 			    "\tswap\tsw,dp\t\t0 0\n", swap);
   1286 		goto done_with_disks;
   1287 	}
   1288 
   1289 	for (size_t i = 0; i < install->num; i++) {
   1290 
   1291 		const struct part_usage_info *ptn = &install->infos[i];
   1292 
   1293 		if (ptn->size == 0)
   1294 			continue;
   1295 
   1296 		if (ptn->type != PT_swap &&
   1297 		    (ptn->instflags & PUIINST_MOUNT) == 0)
   1298 			continue;
   1299 
   1300 		const char *s = "";
   1301 		const char *mp = ptn->mount;
   1302 		const char *fstype = "ffs";
   1303 		int fsck_pass = 0, dump_freq = 0;
   1304 
   1305 		if (ptn->parts->pscheme->get_part_device(ptn->parts,
   1306 			    ptn->cur_part_id, dev_buf, sizeof dev_buf, NULL,
   1307 			    logical_name, true))
   1308 			dev = dev_buf;
   1309 		else
   1310 			dev = NULL;
   1311 
   1312 		if (!*mp) {
   1313 			/*
   1314 			 * No mount point specified, comment out line and
   1315 			 * use /mnt as a placeholder for the mount point.
   1316 			 */
   1317 			s = "# ";
   1318 			mp = "/mnt";
   1319 		}
   1320 
   1321 		switch (ptn->fs_type) {
   1322 		case FS_UNUSED:
   1323 			continue;
   1324 		case FS_BSDLFS:
   1325 			/* If there is no LFS, just comment it out. */
   1326 			if (!check_lfs_progs())
   1327 				s = "# ";
   1328 			fstype = "lfs";
   1329 			/* FALLTHROUGH */
   1330 		case FS_BSDFFS:
   1331 			fsck_pass = (strcmp(mp, "/") == 0) ? 1 : 2;
   1332 			dump_freq = 1;
   1333 			break;
   1334 		case FS_MSDOS:
   1335 			fstype = "msdos";
   1336 			break;
   1337 		case FS_SWAP:
   1338 			if (swap_dev[0] == 0) {
   1339 				strncpy(swap_dev, dev, sizeof swap_dev);
   1340 				dump_dev = ",dp";
   1341 			} else {
   1342 				dump_dev = "";
   1343 			}
   1344 			scripting_fprintf(f, "%s\t\tnone\tswap\tsw%s\t\t 0 0\n",
   1345 				dev, dump_dev);
   1346 			continue;
   1347 		case FS_SYSVBFS:
   1348 			fstype = "sysvbfs";
   1349 			make_target_dir("/stand");
   1350 			break;
   1351 		default:
   1352 			fstype = "???";
   1353 			s = "# ";
   1354 			break;
   1355 		}
   1356 		/* The code that remounts root rw doesn't check the partition */
   1357 		if (strcmp(mp, "/") == 0 &&
   1358 		    (ptn->instflags & PUIINST_MOUNT) == 0)
   1359 			s = "# ";
   1360 
   1361  		scripting_fprintf(f,
   1362 		  "%s%s\t\t%s\t%s\trw%s%s%s%s%s%s%s%s\t\t %d %d\n",
   1363 		   s, dev, mp, fstype,
   1364 		   ptn->mountflags & PUIMNT_LOG ? ",log" : "",
   1365 		   ptn->mountflags & PUIMNT_NOAUTO ? ",noauto" : "",
   1366 		   ptn->mountflags & PUIMNT_ASYNC ? ",async" : "",
   1367 		   ptn->mountflags & PUIMNT_NOATIME ? ",noatime" : "",
   1368 		   ptn->mountflags & PUIMNT_NODEV ? ",nodev" : "",
   1369 		   ptn->mountflags & PUIMNT_NODEVMTIME ? ",nodevmtime" : "",
   1370 		   ptn->mountflags & PUIMNT_NOEXEC ? ",noexec" : "",
   1371 		   ptn->mountflags & PUIMNT_NOSUID ? ",nosuid" : "",
   1372 		   dump_freq, fsck_pass);
   1373 	}
   1374 
   1375 done_with_disks:
   1376 	if (tmp_ramdisk_size > 0) {
   1377 #ifdef HAVE_TMPFS
   1378 		scripting_fprintf(f, "tmpfs\t\t/tmp\ttmpfs\trw,-m=1777,-s=%"
   1379 		    PRIu64 "\n",
   1380 		    tmp_ramdisk_size * 512);
   1381 #else
   1382 		if (swap_dev[0] != 0)
   1383 			scripting_fprintf(f, "%s\t\t/tmp\tmfs\trw,-s=%"
   1384 			    PRIu64 "\n", swap_dev, tmp_ramdisk_size);
   1385 		else
   1386 			scripting_fprintf(f, "swap\t\t/tmp\tmfs\trw,-s=%"
   1387 			    PRIu64 "\n", tmp_ramdisk_size);
   1388 #endif
   1389 	}
   1390 
   1391 	if (cdrom_dev[0] == 0)
   1392 		get_default_cdrom(cdrom_dev, sizeof(cdrom_dev));
   1393 
   1394 	/* Add /kern, /proc and /dev/pts to fstab and make mountpoint. */
   1395 	scripting_fprintf(f, "kernfs\t\t/kern\tkernfs\trw\n");
   1396 	scripting_fprintf(f, "ptyfs\t\t/dev/pts\tptyfs\trw\n");
   1397 	scripting_fprintf(f, "procfs\t\t/proc\tprocfs\trw\n");
   1398 	scripting_fprintf(f, "/dev/%s\t\t/cdrom\tcd9660\tro,noauto\n",
   1399 	    cdrom_dev);
   1400 	scripting_fprintf(f, "%stmpfs\t\t/var/shm\ttmpfs\trw,-m1777,-sram%%25\n",
   1401 	    tmpfs_on_var_shm() ? "" : "#");
   1402 	make_target_dir("/kern");
   1403 	make_target_dir("/proc");
   1404 	make_target_dir("/dev/pts");
   1405 	make_target_dir("/cdrom");
   1406 	make_target_dir("/var/shm");
   1407 
   1408 	scripting_fprintf(NULL, "EOF\n");
   1409 
   1410 	fclose(f);
   1411 	fflush(NULL);
   1412 	return 0;
   1413 }
   1414 
   1415 static bool
   1416 find_part_by_name(const char *name, struct disk_partitions **parts,
   1417     part_id *pno)
   1418 {
   1419 	struct pm_devs *i;
   1420 	struct disk_partitions *ps;
   1421 	part_id id;
   1422 	struct disk_desc disks[MAX_DISKS];
   1423 	int n, cnt;
   1424 
   1425 	if (SLIST_EMPTY(&pm_head)) {
   1426 		/*
   1427 		 * List has not been filled, only "pm" is valid - check
   1428 		 * that first.
   1429 		 */
   1430 		if (pm->parts->pscheme->find_by_name != NULL) {
   1431 			id = pm->parts->pscheme->find_by_name(pm->parts, name);
   1432 			if (id != NO_PART) {
   1433 				*pno = id;
   1434 				*parts = pm->parts;
   1435 				return true;
   1436 			}
   1437 		}
   1438 		/*
   1439 		 * Not that easy - check all other disks
   1440 		 */
   1441 		cnt = get_disks(disks, false);
   1442 		for (n = 0; n < cnt; n++) {
   1443 			if (strcmp(disks[n].dd_name, pm->diskdev) == 0)
   1444 				continue;
   1445 			ps = partitions_read_disk(disks[n].dd_name,
   1446 			    disks[n].dd_totsec);
   1447 			if (ps == NULL)
   1448 				continue;
   1449 			if (ps->pscheme->find_by_name == NULL)
   1450 				continue;
   1451 			id = ps->pscheme->find_by_name(ps, name);
   1452 			if (id != NO_PART) {
   1453 				*pno = id;
   1454 				*parts = ps;
   1455 				return true;	/* XXX this leaks memory */
   1456 			}
   1457 			ps->pscheme->free(ps);
   1458 		}
   1459 	} else {
   1460 		SLIST_FOREACH(i, &pm_head, l) {
   1461 			if (i->parts == NULL)
   1462 				continue;
   1463 			if (i->parts->pscheme->find_by_name == NULL)
   1464 				continue;
   1465 			id = i->parts->pscheme->find_by_name(i->parts, name);
   1466 			if (id == NO_PART)
   1467 				continue;
   1468 			*pno = id;
   1469 			*parts = i->parts;
   1470 			return true;
   1471 		}
   1472 	}
   1473 
   1474 	*pno = NO_PART;
   1475 	*parts = NULL;
   1476 	return false;
   1477 }
   1478 
   1479 static int
   1480 /*ARGSUSED*/
   1481 process_found_fs(struct data *list, size_t num, const struct lookfor *item,
   1482     bool with_fsck)
   1483 {
   1484 	int error;
   1485 	char rdev[PATH_MAX], dev[PATH_MAX],
   1486 	    options[STRSIZE], tmp[STRSIZE], *op, *last;
   1487 	const char *fsname = (const char*)item->var;
   1488 	part_id pno;
   1489 	struct disk_partitions *parts;
   1490 	size_t len;
   1491 	bool first, is_root;
   1492 
   1493 	if (num < 2 || strstr(list[2].u.s_val, "noauto") != NULL)
   1494 		return 0;
   1495 
   1496 	is_root = strcmp(list[1].u.s_val, "/") == 0;
   1497 	if (is_root && target_mounted())
   1498 		return 0;
   1499 
   1500 	if (strcmp(item->head, name_prefix) == 0) {
   1501 		/* this fstab entry uses NAME= syntax */
   1502 		if (!find_part_by_name(list[0].u.s_val,
   1503 		    &parts, &pno) || parts == NULL || pno == NO_PART)
   1504 			return 0;
   1505 		parts->pscheme->get_part_device(parts, pno,
   1506 		    dev, sizeof(dev), NULL, plain_name, true);
   1507 		parts->pscheme->get_part_device(parts, pno,
   1508 		    rdev, sizeof(rdev), NULL, raw_dev_name, true);
   1509 	} else {
   1510 		/* this fstab entry uses the plain device name */
   1511 		if (is_root) {
   1512 			/*
   1513 			 * PR 54480: we can not use the current device name
   1514 			 * as it might be different from the real environment.
   1515 			 * This is an abuse of the functionality, but it used
   1516 			 * to work before (and still does work if only a single
   1517 			 * target disk is involved).
   1518 			 * Use the device name from the current "pm" instead.
   1519 			 */
   1520 			strcpy(rdev, "/dev/r");
   1521 			strlcat(rdev, pm->diskdev, sizeof(rdev));
   1522 			strcpy(dev, "/dev/");
   1523 			strlcat(dev, pm->diskdev, sizeof(dev));
   1524 			/* copy over the partition letter, if any */
   1525 			len = strlen(list[0].u.s_val);
   1526 			if (list[0].u.s_val[len-1] >= 'a' &&
   1527 			    list[0].u.s_val[len-1] <=
   1528 			    ('a' + getmaxpartitions())) {
   1529 				strlcat(rdev, &list[0].u.s_val[len-1],
   1530 				    sizeof(rdev));
   1531 				strlcat(dev, &list[0].u.s_val[len-1],
   1532 				    sizeof(dev));
   1533 			}
   1534 		} else {
   1535 			strcpy(rdev, "/dev/r");
   1536 			strlcat(rdev, list[0].u.s_val, sizeof(rdev));
   1537 			strcpy(dev, "/dev/");
   1538 			strlcat(dev, list[0].u.s_val, sizeof(dev));
   1539 		}
   1540 	}
   1541 
   1542 	if (with_fsck) {
   1543 		/* need the raw device for fsck_preen */
   1544 		error = fsck_preen(rdev, fsname, false);
   1545 		if (error != 0)
   1546 			return error;
   1547 	}
   1548 
   1549 	/* add mount option for fs type */
   1550 	strcpy(options, "-t ");
   1551 	strlcat(options, fsname, sizeof(options));
   1552 
   1553 	/* extract mount options from fstab */
   1554 	strlcpy(tmp, list[2].u.s_val, sizeof(tmp));
   1555 	for (first = true, op = strtok_r(tmp, ",", &last); op != NULL;
   1556 	    op = strtok_r(NULL, ",", &last)) {
   1557 		if (strcmp(op, FSTAB_RW) == 0 ||
   1558 		    strcmp(op, FSTAB_RQ) == 0 ||
   1559 		    strcmp(op, FSTAB_RO) == 0 ||
   1560 		    strcmp(op, FSTAB_SW) == 0 ||
   1561 		    strcmp(op, FSTAB_DP) == 0 ||
   1562 		    strcmp(op, FSTAB_XX) == 0)
   1563 			continue;
   1564 		if (first) {
   1565 			first = false;
   1566 			strlcat(options, " -o ", sizeof(options));
   1567 		} else {
   1568 			strlcat(options, ",", sizeof(options));
   1569 		}
   1570 		strlcat(options, op, sizeof(options));
   1571 	}
   1572 
   1573 	error = target_mount(options, dev, list[1].u.s_val);
   1574 	if (error != 0) {
   1575 		msg_fmt_display(MSG_mount_failed, "%s", list[0].u.s_val);
   1576 		if (!ask_noyes(NULL))
   1577 			return error;
   1578 	}
   1579 	return 0;
   1580 }
   1581 
   1582 static int
   1583 /*ARGSUSED*/
   1584 found_fs(struct data *list, size_t num, const struct lookfor *item)
   1585 {
   1586 	return process_found_fs(list, num, item, true);
   1587 }
   1588 
   1589 static int
   1590 /*ARGSUSED*/
   1591 found_fs_nocheck(struct data *list, size_t num, const struct lookfor *item)
   1592 {
   1593 	return process_found_fs(list, num, item, false);
   1594 }
   1595 
   1596 /*
   1597  * Do an fsck. On failure, inform the user by showing a warning
   1598  * message and doing menu_ok() before proceeding.
   1599  * The device passed should be the full qualified path to raw disk
   1600  * (e.g. /dev/rwd0a).
   1601  * Returns 0 on success, or nonzero return code from fsck() on failure.
   1602  */
   1603 static int
   1604 fsck_preen(const char *disk, const char *fsname, bool silent)
   1605 {
   1606 	char *prog, err[12];
   1607 	int error;
   1608 
   1609 	if (fsname == NULL)
   1610 		return 0;
   1611 	/* first, check if fsck program exists, if not, assume ok */
   1612 	asprintf(&prog, "/sbin/fsck_%s", fsname);
   1613 	if (prog == NULL)
   1614 		return 0;
   1615 	if (access(prog, X_OK) != 0) {
   1616 		free(prog);
   1617 		return 0;
   1618 	}
   1619 	if (!strcmp(fsname,"ffs"))
   1620 		fixsb(prog, disk);
   1621 	error = run_program(silent? RUN_SILENT|RUN_ERROR_OK : 0, "%s -p -q %s", prog, disk);
   1622 	free(prog);
   1623 	if (error != 0 && !silent) {
   1624 		sprintf(err, "%d", error);
   1625 		msg_display_subst(msg_string(MSG_badfs), 3,
   1626 		    disk, fsname, err);
   1627 		if (ask_noyes(NULL))
   1628 			error = 0;
   1629 		/* XXX at this point maybe we should run a full fsck? */
   1630 	}
   1631 	return error;
   1632 }
   1633 
   1634 /* This performs the same function as the etc/rc.d/fixsb script
   1635  * which attempts to correct problems with ffs1 filesystems
   1636  * which may have been introduced by booting a netbsd-current kernel
   1637  * from between April of 2003 and January 2004. For more information
   1638  * This script was developed as a response to NetBSD pr install/25138
   1639  * Additional prs regarding the original issue include:
   1640  *  bin/17910 kern/21283 kern/21404 port-macppc/23925 port-macppc/23926
   1641  */
   1642 static void
   1643 fixsb(const char *prog, const char *disk)
   1644 {
   1645 	int fd;
   1646 	int rval;
   1647 	union {
   1648 		struct fs fs;
   1649 		char buf[SBLOCKSIZE];
   1650 	} sblk;
   1651 	struct fs *fs = &sblk.fs;
   1652 
   1653 	fd = open(disk, O_RDONLY);
   1654 	if (fd == -1)
   1655 		return;
   1656 
   1657 	/* Read ffsv1 main superblock */
   1658 	rval = pread(fd, sblk.buf, sizeof sblk.buf, SBLOCK_UFS1);
   1659 	close(fd);
   1660 	if (rval != sizeof sblk.buf)
   1661 		return;
   1662 
   1663 	if (fs->fs_magic != FS_UFS1_MAGIC &&
   1664 	    fs->fs_magic != FS_UFS1_MAGIC_SWAPPED)
   1665 		/* Not FFSv1 */
   1666 		return;
   1667 	if (fs->fs_old_flags & FS_FLAGS_UPDATED)
   1668 		/* properly updated fslevel 4 */
   1669 		return;
   1670 	if (fs->fs_bsize != fs->fs_maxbsize)
   1671 		/* not messed up */
   1672 		return;
   1673 
   1674 	/*
   1675 	 * OK we have a munged fs, first 'upgrade' to fslevel 4,
   1676 	 * We specify -b16 in order to stop fsck bleating that the
   1677 	 * sb doesn't match the first alternate.
   1678 	 */
   1679 	run_program(RUN_DISPLAY | RUN_PROGRESS,
   1680 	    "%s -p -b 16 -c 4 %s", prog, disk);
   1681 	/* Then downgrade to fslevel 3 */
   1682 	run_program(RUN_DISPLAY | RUN_PROGRESS,
   1683 	    "%s -p -c 3 %s", prog, disk);
   1684 }
   1685 
   1686 /*
   1687  * fsck and mount the root partition.
   1688  * devdev is the fully qualified block device name.
   1689  */
   1690 static int
   1691 mount_root(const char *devdev, bool first, bool writeable,
   1692      struct install_partition_desc *install)
   1693 {
   1694 	int	error;
   1695 
   1696 	error = fsck_preen(devdev, "ffs", false);
   1697 	if (error != 0)
   1698 		return error;
   1699 
   1700 	if (first)
   1701 		md_pre_mount(install, 0);
   1702 
   1703 	/* Mount devdev on target's "".
   1704 	 * If we pass "" as mount-on, Prefixing will DTRT.
   1705 	 * for now, use no options.
   1706 	 * XXX consider -o remount in case target root is
   1707 	 * current root, still readonly from single-user?
   1708 	 */
   1709 	return target_mount(writeable? "" : "-r", devdev, "");
   1710 }
   1711 
   1712 /* Get information on the file systems mounted from the root filesystem.
   1713  * Offer to convert them into 4.4BSD inodes if they are not 4.4BSD
   1714  * inodes.  Fsck them.  Mount them.
   1715  */
   1716 
   1717 int
   1718 mount_disks(struct install_partition_desc *install)
   1719 {
   1720 	char *fstab;
   1721 	int   fstabsize;
   1722 	int   error;
   1723 	char devdev[PATH_MAX];
   1724 	size_t i, num_fs_types, num_entries;
   1725 	struct lookfor *fstabbuf, *l;
   1726 
   1727 	if (install->cur_system)
   1728 		return 0;
   1729 
   1730 	/*
   1731 	 * Check what file system tools are available and create parsers
   1732 	 * for the corresponding fstab(5) entries - all others will be
   1733 	 * ignored.
   1734 	 */
   1735 	num_fs_types = 1;	/* ffs is implicit */
   1736 	for (i = 0; i < __arraycount(extern_fs_with_chk); i++) {
   1737 		sprintf(devdev, "/sbin/newfs_%s", extern_fs_with_chk[i]);
   1738 		if (file_exists_p(devdev))
   1739 			num_fs_types++;
   1740 	}
   1741 	for (i = 0; i < __arraycount(extern_fs_newfs_only); i++) {
   1742 		sprintf(devdev, "/sbin/newfs_%s", extern_fs_newfs_only[i]);
   1743 		if (file_exists_p(devdev))
   1744 			num_fs_types++;
   1745 	}
   1746 	num_entries = 2 *  num_fs_types + 1;	/* +1 for "ufs" special case */
   1747 	fstabbuf = calloc(num_entries, sizeof(*fstabbuf));
   1748 	if (fstabbuf == NULL)
   1749 		return -1;
   1750 	l = fstabbuf;
   1751 	l->head = "/dev/";
   1752 	l->fmt = strdup("/dev/%s %s ffs %s");
   1753 	l->todo = "c";
   1754 	l->var = __UNCONST("ffs");
   1755 	l->func = found_fs;
   1756 	l++;
   1757 	l->head = "/dev/";
   1758 	l->fmt = strdup("/dev/%s %s ufs %s");
   1759 	l->todo = "c";
   1760 	l->var = __UNCONST("ffs");
   1761 	l->func = found_fs;
   1762 	l++;
   1763 	l->head = NAME_PREFIX;
   1764 	l->fmt = strdup(NAME_PREFIX "%s %s ffs %s");
   1765 	l->todo = "c";
   1766 	l->var = __UNCONST("ffs");
   1767 	l->func = found_fs;
   1768 	l++;
   1769 	for (i = 0; i < __arraycount(extern_fs_with_chk); i++) {
   1770 		sprintf(devdev, "/sbin/newfs_%s", extern_fs_with_chk[i]);
   1771 		if (!file_exists_p(devdev))
   1772 			continue;
   1773 		sprintf(devdev, "/dev/%%s %%s %s %%s", extern_fs_with_chk[i]);
   1774 		l->head = "/dev/";
   1775 		l->fmt = strdup(devdev);
   1776 		l->todo = "c";
   1777 		l->var = __UNCONST(extern_fs_with_chk[i]);
   1778 		l->func = found_fs;
   1779 		l++;
   1780 		sprintf(devdev, NAME_PREFIX "%%s %%s %s %%s",
   1781 		    extern_fs_with_chk[i]);
   1782 		l->head = NAME_PREFIX;
   1783 		l->fmt = strdup(devdev);
   1784 		l->todo = "c";
   1785 		l->var = __UNCONST(extern_fs_with_chk[i]);
   1786 		l->func = found_fs;
   1787 		l++;
   1788 	}
   1789 	for (i = 0; i < __arraycount(extern_fs_newfs_only); i++) {
   1790 		sprintf(devdev, "/sbin/newfs_%s", extern_fs_newfs_only[i]);
   1791 		if (!file_exists_p(devdev))
   1792 			continue;
   1793 		sprintf(devdev, "/dev/%%s %%s %s %%s", extern_fs_newfs_only[i]);
   1794 		l->head = "/dev/";
   1795 		l->fmt = strdup(devdev);
   1796 		l->todo = "c";
   1797 		l->var = __UNCONST(extern_fs_newfs_only[i]);
   1798 		l->func = found_fs_nocheck;
   1799 		l++;
   1800 		sprintf(devdev, NAME_PREFIX "%%s %%s %s %%s",
   1801 		    extern_fs_newfs_only[i]);
   1802 		l->head = NAME_PREFIX;
   1803 		l->fmt = strdup(devdev);
   1804 		l->todo = "c";
   1805 		l->var = __UNCONST(extern_fs_newfs_only[i]);
   1806 		l->func = found_fs_nocheck;
   1807 		l++;
   1808 	}
   1809 	assert((size_t)(l - fstabbuf) == num_entries);
   1810 
   1811 	/* First the root device. */
   1812 	if (target_already_root())
   1813 		/* avoid needing to call target_already_root() again */
   1814 		targetroot_mnt[0] = 0;
   1815 	else {
   1816 		for (i = 0; i < install->num; i++) {
   1817 			if (is_root_part_mount(install->infos[i].mount))
   1818 				break;
   1819 		}
   1820 
   1821 		if (i >= install->num) {
   1822 			hit_enter_to_continue(MSG_noroot, NULL);
   1823 			return -1;
   1824 		}
   1825 
   1826 		if (!install->infos[i].parts->pscheme->get_part_device(
   1827 		    install->infos[i].parts, install->infos[i].cur_part_id,
   1828 		    devdev, sizeof devdev, NULL, plain_name, true))
   1829 			return -1;
   1830 		error = mount_root(devdev, true, false, install);
   1831 		if (error != 0 && error != EBUSY)
   1832 			return -1;
   1833 	}
   1834 
   1835 	/* Check the target /etc/fstab exists before trying to parse it. */
   1836 	if (target_dir_exists_p("/etc") == 0 ||
   1837 	    target_file_exists_p("/etc/fstab") == 0) {
   1838 		msg_fmt_display(MSG_noetcfstab, "%s", pm->diskdev);
   1839 		hit_enter_to_continue(NULL, NULL);
   1840 		return -1;
   1841 	}
   1842 
   1843 
   1844 	/* Get fstab entries from the target-root /etc/fstab. */
   1845 	fstabsize = target_collect_file(T_FILE, &fstab, "/etc/fstab");
   1846 	if (fstabsize < 0) {
   1847 		/* error ! */
   1848 		msg_fmt_display(MSG_badetcfstab, "%s", pm->diskdev);
   1849 		hit_enter_to_continue(NULL, NULL);
   1850 		umount_root();
   1851 		return -2;
   1852 	}
   1853 	/*
   1854 	 * We unmount the read-only root again, so we can mount it
   1855 	 * with proper options from /etc/fstab
   1856 	 */
   1857 	umount_root();
   1858 
   1859 	/*
   1860 	 * Now do all entries in /etc/fstab and mount them if required
   1861 	 */
   1862 	error = walk(fstab, (size_t)fstabsize, fstabbuf, num_entries);
   1863 	free(fstab);
   1864 	for (i = 0; i < num_entries; i++)
   1865 		free(__UNCONST(fstabbuf[i].fmt));
   1866 	free(fstabbuf);
   1867 
   1868 	return error;
   1869 }
   1870 
   1871 int
   1872 set_swap_if_low_ram(struct install_partition_desc *install)
   1873 {
   1874 	if (get_ramsize() <= 32)
   1875 		return set_swap(install);
   1876 	return 0;
   1877 }
   1878 
   1879 int
   1880 set_swap(struct install_partition_desc *install)
   1881 {
   1882 	size_t i;
   1883 	char dev_buf[PATH_MAX];
   1884 	int rval;
   1885 
   1886 	for (i = 0; i < install->num; i++) {
   1887 		if (install->infos[i].type == PT_swap)
   1888 			break;
   1889 	}
   1890 	if (i >= install->num)
   1891 		return 0;
   1892 
   1893 	if (!install->infos[i].parts->pscheme->get_part_device(
   1894 	    install->infos[i].parts, install->infos[i].cur_part_id, dev_buf,
   1895 	    sizeof dev_buf, NULL, plain_name, true))
   1896 		return -1;
   1897 
   1898 	rval = swapctl(SWAP_ON, dev_buf, 0);
   1899 	if (rval != 0)
   1900 		return -1;
   1901 
   1902 	return 0;
   1903 }
   1904 
   1905 int
   1906 check_swap(const char *disk, int remove_swap)
   1907 {
   1908 	struct swapent *swap;
   1909 	char *cp;
   1910 	int nswap;
   1911 	int l;
   1912 	int rval = 0;
   1913 
   1914 	nswap = swapctl(SWAP_NSWAP, 0, 0);
   1915 	if (nswap <= 0)
   1916 		return 0;
   1917 
   1918 	swap = malloc(nswap * sizeof *swap);
   1919 	if (swap == NULL)
   1920 		return -1;
   1921 
   1922 	nswap = swapctl(SWAP_STATS, swap, nswap);
   1923 	if (nswap < 0)
   1924 		goto bad_swap;
   1925 
   1926 	l = strlen(disk);
   1927 	while (--nswap >= 0) {
   1928 		/* Should we check the se_dev or se_path? */
   1929 		cp = swap[nswap].se_path;
   1930 		if (memcmp(cp, "/dev/", 5) != 0)
   1931 			continue;
   1932 		if (memcmp(cp + 5, disk, l) != 0)
   1933 			continue;
   1934 		if (!isalpha(*(unsigned char *)(cp + 5 + l)))
   1935 			continue;
   1936 		if (cp[5 + l + 1] != 0)
   1937 			continue;
   1938 		/* ok path looks like it is for this device */
   1939 		if (!remove_swap) {
   1940 			/* count active swap areas */
   1941 			rval++;
   1942 			continue;
   1943 		}
   1944 		if (swapctl(SWAP_OFF, cp, 0) == -1)
   1945 			rval = -1;
   1946 	}
   1947 
   1948     done:
   1949 	free(swap);
   1950 	return rval;
   1951 
   1952     bad_swap:
   1953 	rval = -1;
   1954 	goto done;
   1955 }
   1956 
   1957 #ifdef HAVE_BOOTXX_xFS
   1958 char *
   1959 bootxx_name(struct install_partition_desc *install)
   1960 {
   1961 	size_t i;
   1962 	int fstype = -1;
   1963 	const char *bootxxname;
   1964 	char *bootxx;
   1965 
   1966 	/* find a partition to be mounted as / */
   1967 	for (i = 0; i < install->num; i++) {
   1968 		if ((install->infos[i].instflags & PUIINST_MOUNT)
   1969 		    && strcmp(install->infos[i].mount, "/") == 0) {
   1970 			fstype = install->infos[i].fs_type;
   1971 			break;
   1972 		}
   1973 	}
   1974 	if (fstype < 0) {
   1975 		/* not found? take first root type partition instead */
   1976 		for (i = 0; i < install->num; i++) {
   1977 			if (install->infos[i].type == PT_root) {
   1978 				fstype = install->infos[i].fs_type;
   1979 				break;
   1980 			}
   1981 		}
   1982 	}
   1983 
   1984 	/* check we have boot code for the root partition type */
   1985 	switch (fstype) {
   1986 #if defined(BOOTXX_FFSV1) || defined(BOOTXX_FFSV2)
   1987 	case FS_BSDFFS:
   1988 		if (install->infos[0].fs_version == 2) {
   1989 #ifdef BOOTXX_FFSV2
   1990 			bootxxname = BOOTXX_FFSV2;
   1991 #else
   1992 			bootxxname = NULL;
   1993 #endif
   1994 		} else {
   1995 #ifdef BOOTXX_FFSV1
   1996 			bootxxname = BOOTXX_FFSV1;
   1997 #else
   1998 			bootxxname = NULL;
   1999 #endif
   2000 		}
   2001 		break;
   2002 #endif
   2003 #ifdef BOOTXX_LFSV2
   2004 	case FS_BSDLFS:
   2005 		bootxxname = BOOTXX_LFSV2;
   2006 		break;
   2007 #endif
   2008 	default:
   2009 		bootxxname = NULL;
   2010 		break;
   2011 	}
   2012 
   2013 	if (bootxxname == NULL)
   2014 		return NULL;
   2015 
   2016 	asprintf(&bootxx, "%s/%s", BOOTXXDIR, bootxxname);
   2017 	return bootxx;
   2018 }
   2019 #endif
   2020 
   2021 /* from dkctl.c */
   2022 static int
   2023 get_dkwedges_sort(const void *a, const void *b)
   2024 {
   2025 	const struct dkwedge_info *dkwa = a, *dkwb = b;
   2026 	const daddr_t oa = dkwa->dkw_offset, ob = dkwb->dkw_offset;
   2027 	return (oa < ob) ? -1 : (oa > ob) ? 1 : 0;
   2028 }
   2029 
   2030 int
   2031 get_dkwedges(struct dkwedge_info **dkw, const char *diskdev)
   2032 {
   2033 	struct dkwedge_list dkwl;
   2034 
   2035 	*dkw = NULL;
   2036 	if (!get_wedge_list(diskdev, &dkwl))
   2037 		return -1;
   2038 
   2039 	if (dkwl.dkwl_nwedges > 0 && *dkw != NULL) {
   2040 		qsort(*dkw, dkwl.dkwl_nwedges, sizeof(**dkw),
   2041 		    get_dkwedges_sort);
   2042 	}
   2043 
   2044 	return dkwl.dkwl_nwedges;
   2045 }
   2046