Home | History | Annotate | Line # | Download | only in sysinst
disks.c revision 1.1
      1  1.1  dholland /*	$NetBSD: disks.c,v 1.1 2014/07/26 19:30:44 dholland Exp $ */
      2  1.1  dholland 
      3  1.1  dholland /*
      4  1.1  dholland  * Copyright 1997 Piermont Information Systems Inc.
      5  1.1  dholland  * All rights reserved.
      6  1.1  dholland  *
      7  1.1  dholland  * Written by Philip A. Nelson for Piermont Information Systems Inc.
      8  1.1  dholland  *
      9  1.1  dholland  * Redistribution and use in source and binary forms, with or without
     10  1.1  dholland  * modification, are permitted provided that the following conditions
     11  1.1  dholland  * are met:
     12  1.1  dholland  * 1. Redistributions of source code must retain the above copyright
     13  1.1  dholland  *    notice, this list of conditions and the following disclaimer.
     14  1.1  dholland  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1  dholland  *    notice, this list of conditions and the following disclaimer in the
     16  1.1  dholland  *    documentation and/or other materials provided with the distribution.
     17  1.1  dholland  * 3. The name of Piermont Information Systems Inc. may not be used to endorse
     18  1.1  dholland  *    or promote products derived from this software without specific prior
     19  1.1  dholland  *    written permission.
     20  1.1  dholland  *
     21  1.1  dholland  * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
     22  1.1  dholland  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.1  dholland  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1  dholland  * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
     25  1.1  dholland  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26  1.1  dholland  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27  1.1  dholland  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  1.1  dholland  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29  1.1  dholland  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  1.1  dholland  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     31  1.1  dholland  * THE POSSIBILITY OF SUCH DAMAGE.
     32  1.1  dholland  *
     33  1.1  dholland  */
     34  1.1  dholland 
     35  1.1  dholland /* disks.c -- routines to deal with finding disks and labeling disks. */
     36  1.1  dholland 
     37  1.1  dholland 
     38  1.1  dholland #include <errno.h>
     39  1.1  dholland #include <stdio.h>
     40  1.1  dholland #include <stdlib.h>
     41  1.1  dholland #include <unistd.h>
     42  1.1  dholland #include <fcntl.h>
     43  1.1  dholland #include <util.h>
     44  1.1  dholland 
     45  1.1  dholland #include <sys/param.h>
     46  1.1  dholland #include <sys/sysctl.h>
     47  1.1  dholland #include <sys/swap.h>
     48  1.1  dholland #include <ufs/ufs/dinode.h>
     49  1.1  dholland #include <ufs/ffs/fs.h>
     50  1.1  dholland #define FSTYPENAMES
     51  1.1  dholland #include <sys/disklabel.h>
     52  1.1  dholland 
     53  1.1  dholland #include <dev/scsipi/scsipi_all.h>
     54  1.1  dholland #include <sys/scsiio.h>
     55  1.1  dholland 
     56  1.1  dholland #include <dev/ata/atareg.h>
     57  1.1  dholland #include <sys/ataio.h>
     58  1.1  dholland 
     59  1.1  dholland #include "defs.h"
     60  1.1  dholland #include "md.h"
     61  1.1  dholland #include "msg_defs.h"
     62  1.1  dholland #include "menu_defs.h"
     63  1.1  dholland #include "txtwalk.h"
     64  1.1  dholland 
     65  1.1  dholland /* Disk descriptions */
     66  1.1  dholland #define MAX_DISKS 15
     67  1.1  dholland struct disk_desc {
     68  1.1  dholland 	char	dd_name[SSTRSIZE];
     69  1.1  dholland 	char	dd_descr[70];
     70  1.1  dholland 	uint	dd_no_mbr;
     71  1.1  dholland 	uint	dd_cyl;
     72  1.1  dholland 	uint	dd_head;
     73  1.1  dholland 	uint	dd_sec;
     74  1.1  dholland 	uint	dd_secsize;
     75  1.1  dholland 	uint	dd_totsec;
     76  1.1  dholland };
     77  1.1  dholland 
     78  1.1  dholland /* Local prototypes */
     79  1.1  dholland static int foundffs(struct data *, size_t);
     80  1.1  dholland #ifdef USE_SYSVBFS
     81  1.1  dholland static int foundsysvbfs(struct data *, size_t);
     82  1.1  dholland #endif
     83  1.1  dholland static int fsck_preen(const char *, int, const char *);
     84  1.1  dholland static void fixsb(const char *, const char *, char);
     85  1.1  dholland 
     86  1.1  dholland #ifndef DISK_NAMES
     87  1.1  dholland #define DISK_NAMES "wd", "sd", "ld", "raid"
     88  1.1  dholland #endif
     89  1.1  dholland 
     90  1.1  dholland static const char *disk_names[] = { DISK_NAMES, "vnd", NULL };
     91  1.1  dholland 
     92  1.1  dholland static bool tmpfs_on_var_shm(void);
     93  1.1  dholland 
     94  1.1  dholland const char *
     95  1.1  dholland getfslabelname(uint8_t f)
     96  1.1  dholland {
     97  1.1  dholland 	if (f >= __arraycount(fstypenames) || fstypenames[f] == NULL)
     98  1.1  dholland 		return "invalid";
     99  1.1  dholland 	return fstypenames[f];
    100  1.1  dholland }
    101  1.1  dholland 
    102  1.1  dholland /*
    103  1.1  dholland  * Decide wether we want to mount a tmpfs on /var/shm: we do this always
    104  1.1  dholland  * when the machine has more than 16 MB of user memory. On smaller machines,
    105  1.1  dholland  * shm_open() and friends will not perform well anyway.
    106  1.1  dholland  */
    107  1.1  dholland static bool
    108  1.1  dholland tmpfs_on_var_shm()
    109  1.1  dholland {
    110  1.1  dholland 	uint64_t ram;
    111  1.1  dholland 	size_t len;
    112  1.1  dholland 
    113  1.1  dholland 	len = sizeof(ram);
    114  1.1  dholland 	if (sysctlbyname("hw.usermem64", &ram, &len, NULL, 0))
    115  1.1  dholland 		return false;
    116  1.1  dholland 
    117  1.1  dholland 	return ram > 16UL*1024UL*1024UL;
    118  1.1  dholland }
    119  1.1  dholland 
    120  1.1  dholland /* from src/sbin/atactl/atactl.c
    121  1.1  dholland  * extract_string: copy a block of bytes out of ataparams and make
    122  1.1  dholland  * a proper string out of it, truncating trailing spaces and preserving
    123  1.1  dholland  * strict typing. And also, not doing unaligned accesses.
    124  1.1  dholland  */
    125  1.1  dholland static void
    126  1.1  dholland ata_extract_string(char *buf, size_t bufmax,
    127  1.1  dholland 		   uint8_t *bytes, unsigned numbytes,
    128  1.1  dholland 		   int needswap)
    129  1.1  dholland {
    130  1.1  dholland 	unsigned i;
    131  1.1  dholland 	size_t j;
    132  1.1  dholland 	unsigned char ch1, ch2;
    133  1.1  dholland 
    134  1.1  dholland 	for (i = 0, j = 0; i < numbytes; i += 2) {
    135  1.1  dholland 		ch1 = bytes[i];
    136  1.1  dholland 		ch2 = bytes[i+1];
    137  1.1  dholland 		if (needswap && j < bufmax-1) {
    138  1.1  dholland 			buf[j++] = ch2;
    139  1.1  dholland 		}
    140  1.1  dholland 		if (j < bufmax-1) {
    141  1.1  dholland 			buf[j++] = ch1;
    142  1.1  dholland 		}
    143  1.1  dholland 		if (!needswap && j < bufmax-1) {
    144  1.1  dholland 			buf[j++] = ch2;
    145  1.1  dholland 		}
    146  1.1  dholland 	}
    147  1.1  dholland 	while (j > 0 && buf[j-1] == ' ') {
    148  1.1  dholland 		j--;
    149  1.1  dholland 	}
    150  1.1  dholland 	buf[j] = '\0';
    151  1.1  dholland }
    152  1.1  dholland 
    153  1.1  dholland /*
    154  1.1  dholland  * from src/sbin/scsictl/scsi_subr.c
    155  1.1  dholland  */
    156  1.1  dholland #define STRVIS_ISWHITE(x) ((x) == ' ' || (x) == '\0' || (x) == (u_char)'\377')
    157  1.1  dholland 
    158  1.1  dholland static void
    159  1.1  dholland scsi_strvis(char *sdst, size_t dlen, const char *ssrc, size_t slen)
    160  1.1  dholland {
    161  1.1  dholland 	u_char *dst = (u_char *)sdst;
    162  1.1  dholland 	const u_char *src = (const u_char *)ssrc;
    163  1.1  dholland 
    164  1.1  dholland 	/* Trim leading and trailing blanks and NULs. */
    165  1.1  dholland 	while (slen > 0 && STRVIS_ISWHITE(src[0]))
    166  1.1  dholland 		++src, --slen;
    167  1.1  dholland 	while (slen > 0 && STRVIS_ISWHITE(src[slen - 1]))
    168  1.1  dholland 		--slen;
    169  1.1  dholland 
    170  1.1  dholland 	while (slen > 0) {
    171  1.1  dholland 		if (*src < 0x20 || *src >= 0x80) {
    172  1.1  dholland 			/* non-printable characters */
    173  1.1  dholland 			dlen -= 4;
    174  1.1  dholland 			if (dlen < 1)
    175  1.1  dholland 				break;
    176  1.1  dholland 			*dst++ = '\\';
    177  1.1  dholland 			*dst++ = ((*src & 0300) >> 6) + '0';
    178  1.1  dholland 			*dst++ = ((*src & 0070) >> 3) + '0';
    179  1.1  dholland 			*dst++ = ((*src & 0007) >> 0) + '0';
    180  1.1  dholland 		} else if (*src == '\\') {
    181  1.1  dholland 			/* quote characters */
    182  1.1  dholland 			dlen -= 2;
    183  1.1  dholland 			if (dlen < 1)
    184  1.1  dholland 				break;
    185  1.1  dholland 			*dst++ = '\\';
    186  1.1  dholland 			*dst++ = '\\';
    187  1.1  dholland 		} else {
    188  1.1  dholland 			/* normal characters */
    189  1.1  dholland 			if (--dlen < 1)
    190  1.1  dholland 				break;
    191  1.1  dholland 			*dst++ = *src;
    192  1.1  dholland 		}
    193  1.1  dholland 		++src, --slen;
    194  1.1  dholland 	}
    195  1.1  dholland 
    196  1.1  dholland 	*dst++ = 0;
    197  1.1  dholland }
    198  1.1  dholland 
    199  1.1  dholland 
    200  1.1  dholland static int
    201  1.1  dholland get_descr_scsi(struct disk_desc *dd, int fd)
    202  1.1  dholland {
    203  1.1  dholland 	struct scsipi_inquiry_data inqbuf;
    204  1.1  dholland 	struct scsipi_inquiry cmd;
    205  1.1  dholland 	scsireq_t req;
    206  1.1  dholland         /* x4 in case every character is escaped, +1 for NUL. */
    207  1.1  dholland 	char vendor[(sizeof(inqbuf.vendor) * 4) + 1],
    208  1.1  dholland 	     product[(sizeof(inqbuf.product) * 4) + 1],
    209  1.1  dholland 	     revision[(sizeof(inqbuf.revision) * 4) + 1];
    210  1.1  dholland 	char size[5];
    211  1.1  dholland 	int error;
    212  1.1  dholland 
    213  1.1  dholland 	memset(&inqbuf, 0, sizeof(inqbuf));
    214  1.1  dholland 	memset(&cmd, 0, sizeof(cmd));
    215  1.1  dholland 	memset(&req, 0, sizeof(req));
    216  1.1  dholland 
    217  1.1  dholland 	cmd.opcode = INQUIRY;
    218  1.1  dholland 	cmd.length = sizeof(inqbuf);
    219  1.1  dholland 	memcpy(req.cmd, &cmd, sizeof(cmd));
    220  1.1  dholland 	req.cmdlen = sizeof(cmd);
    221  1.1  dholland 	req.databuf = &inqbuf;
    222  1.1  dholland 	req.datalen = sizeof(inqbuf);
    223  1.1  dholland 	req.timeout = 10000;
    224  1.1  dholland 	req.flags = SCCMD_READ;
    225  1.1  dholland 	req.senselen = SENSEBUFLEN;
    226  1.1  dholland 
    227  1.1  dholland 	error = ioctl(fd, SCIOCCOMMAND, &req);
    228  1.1  dholland 	if (error == -1 || req.retsts != SCCMD_OK)
    229  1.1  dholland 		return 0;
    230  1.1  dholland 
    231  1.1  dholland 	scsi_strvis(vendor, sizeof(vendor), inqbuf.vendor,
    232  1.1  dholland 	    sizeof(inqbuf.vendor));
    233  1.1  dholland 	scsi_strvis(product, sizeof(product), inqbuf.product,
    234  1.1  dholland 	    sizeof(inqbuf.product));
    235  1.1  dholland 	scsi_strvis(revision, sizeof(revision), inqbuf.revision,
    236  1.1  dholland 	    sizeof(inqbuf.revision));
    237  1.1  dholland 
    238  1.1  dholland 	humanize_number(size, sizeof(size),
    239  1.1  dholland 	    (uint64_t)dd->dd_secsize * (uint64_t)dd->dd_totsec,
    240  1.1  dholland 	    "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
    241  1.1  dholland 
    242  1.1  dholland 	snprintf(dd->dd_descr, sizeof(dd->dd_descr),
    243  1.1  dholland 	    "%s (%s, %s %s)",
    244  1.1  dholland 	    dd->dd_name, size, vendor, product);
    245  1.1  dholland 
    246  1.1  dholland 	return 1;
    247  1.1  dholland }
    248  1.1  dholland 
    249  1.1  dholland static int
    250  1.1  dholland get_descr_ata(struct disk_desc *dd, int fd)
    251  1.1  dholland {
    252  1.1  dholland 	struct atareq req;
    253  1.1  dholland 	static union {
    254  1.1  dholland 		unsigned char inbuf[DEV_BSIZE];
    255  1.1  dholland 		struct ataparams inqbuf;
    256  1.1  dholland 	} inbuf;
    257  1.1  dholland 	struct ataparams *inqbuf = &inbuf.inqbuf;
    258  1.1  dholland 	char model[sizeof(inqbuf->atap_model)+1];
    259  1.1  dholland 	char size[5];
    260  1.1  dholland 	int error, needswap = 0;
    261  1.1  dholland 
    262  1.1  dholland 	memset(&inbuf, 0, sizeof(inbuf));
    263  1.1  dholland 	memset(&req, 0, sizeof(req));
    264  1.1  dholland 
    265  1.1  dholland 	req.flags = ATACMD_READ;
    266  1.1  dholland 	req.command = WDCC_IDENTIFY;
    267  1.1  dholland 	req.databuf = (void *)&inbuf;
    268  1.1  dholland 	req.datalen = sizeof(inbuf);
    269  1.1  dholland 	req.timeout = 1000;
    270  1.1  dholland 
    271  1.1  dholland 	error = ioctl(fd, ATAIOCCOMMAND, &req);
    272  1.1  dholland 	if (error == -1 || req.retsts != ATACMD_OK)
    273  1.1  dholland 		return 0;
    274  1.1  dholland 
    275  1.1  dholland #if BYTE_ORDER == LITTLE_ENDIAN
    276  1.1  dholland 	/*
    277  1.1  dholland 	 * On little endian machines, we need to shuffle the string
    278  1.1  dholland 	 * byte order.  However, we don't have to do this for NEC or
    279  1.1  dholland 	 * Mitsumi ATAPI devices
    280  1.1  dholland 	 */
    281  1.1  dholland 
    282  1.1  dholland 	if (!(inqbuf->atap_config != WDC_CFG_CFA_MAGIC &&
    283  1.1  dholland 	      (inqbuf->atap_config & WDC_CFG_ATAPI) &&
    284  1.1  dholland 	      ((inqbuf->atap_model[0] == 'N' &&
    285  1.1  dholland 		  inqbuf->atap_model[1] == 'E') ||
    286  1.1  dholland 	       (inqbuf->atap_model[0] == 'F' &&
    287  1.1  dholland 		  inqbuf->atap_model[1] == 'X')))) {
    288  1.1  dholland 		needswap = 1;
    289  1.1  dholland 	}
    290  1.1  dholland #endif
    291  1.1  dholland 
    292  1.1  dholland 	ata_extract_string(model, sizeof(model),
    293  1.1  dholland 	    inqbuf->atap_model, sizeof(inqbuf->atap_model), needswap);
    294  1.1  dholland 	humanize_number(size, sizeof(size),
    295  1.1  dholland 	    (uint64_t)dd->dd_secsize * (uint64_t)dd->dd_totsec,
    296  1.1  dholland 	    "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
    297  1.1  dholland 
    298  1.1  dholland 	snprintf(dd->dd_descr, sizeof(dd->dd_descr), "%s (%s, %s)",
    299  1.1  dholland 	    dd->dd_name, size, model);
    300  1.1  dholland 
    301  1.1  dholland 	return 1;
    302  1.1  dholland }
    303  1.1  dholland 
    304  1.1  dholland static void
    305  1.1  dholland get_descr(struct disk_desc *dd)
    306  1.1  dholland {
    307  1.1  dholland 	char diskpath[MAXPATHLEN];
    308  1.1  dholland 	int fd = -1;
    309  1.1  dholland 
    310  1.1  dholland 	fd = opendisk(dd->dd_name, O_RDONLY, diskpath, sizeof(diskpath), 0);
    311  1.1  dholland 	if (fd < 0)
    312  1.1  dholland 		goto done;
    313  1.1  dholland 
    314  1.1  dholland 	dd->dd_descr[0] = '\0';
    315  1.1  dholland 
    316  1.1  dholland 	/* try ATA */
    317  1.1  dholland 	if (get_descr_ata(dd, fd))
    318  1.1  dholland 		goto done;
    319  1.1  dholland 	/* try SCSI */
    320  1.1  dholland 	if (get_descr_scsi(dd, fd))
    321  1.1  dholland 		goto done;
    322  1.1  dholland 
    323  1.1  dholland done:
    324  1.1  dholland 	if (fd >= 0)
    325  1.1  dholland 		close(fd);
    326  1.1  dholland 	if (strlen(dd->dd_descr) == 0)
    327  1.1  dholland 		strcpy(dd->dd_descr, dd->dd_name);
    328  1.1  dholland }
    329  1.1  dholland 
    330  1.1  dholland /* disknames - contains device names without partition letters
    331  1.1  dholland  * cdrom_devices - contains devices including partition letters
    332  1.1  dholland  * returns the first entry in hw.disknames matching a cdrom_device, or
    333  1.1  dholland  * first entry on error or no match
    334  1.1  dholland  */
    335  1.1  dholland const char *
    336  1.1  dholland get_default_cdrom(void)
    337  1.1  dholland {
    338  1.1  dholland 	static const char *cdrom_devices[] = { CD_NAMES, 0};
    339  1.1  dholland 	static const char mib_name[] = "hw.disknames";
    340  1.1  dholland 	size_t len;
    341  1.1  dholland 	char *disknames;
    342  1.1  dholland 	char *last;
    343  1.1  dholland 	char *name;
    344  1.1  dholland 	const char **arg;
    345  1.1  dholland 	const char *cd_dev;
    346  1.1  dholland 
    347  1.1  dholland 	/* On error just use first entry in cdrom_devices */
    348  1.1  dholland 	if (sysctlbyname(mib_name, NULL, &len, NULL, 0) == -1)
    349  1.1  dholland 		return cdrom_devices[0];
    350  1.1  dholland 	if ((disknames = malloc(len + 2)) == 0) /* skip on malloc fail */
    351  1.1  dholland 		return cdrom_devices[0];
    352  1.1  dholland 
    353  1.1  dholland 	(void)sysctlbyname(mib_name, disknames, &len, NULL, 0);
    354  1.1  dholland         for ((name = strtok_r(disknames, " ", &last)); name;
    355  1.1  dholland 	    (name = strtok_r(NULL, " ", &last))) {
    356  1.1  dholland 		for (arg = cdrom_devices; *arg; ++arg) {
    357  1.1  dholland 			cd_dev = *arg;
    358  1.1  dholland 			/* skip unit and partition */
    359  1.1  dholland 			if (strncmp(cd_dev, name, strlen(cd_dev) - 2) != 0)
    360  1.1  dholland 				continue;
    361  1.1  dholland 			if (name != disknames)
    362  1.1  dholland 				strcpy(disknames, name);
    363  1.1  dholland 			strcat(disknames, "a");
    364  1.1  dholland 			/* XXX: leaks, but so what? */
    365  1.1  dholland 			return disknames;
    366  1.1  dholland 		}
    367  1.1  dholland 	}
    368  1.1  dholland 	free(disknames);
    369  1.1  dholland 	return cdrom_devices[0];
    370  1.1  dholland }
    371  1.1  dholland 
    372  1.1  dholland static int
    373  1.1  dholland get_disks(struct disk_desc *dd)
    374  1.1  dholland {
    375  1.1  dholland 	const char **xd;
    376  1.1  dholland 	char *cp;
    377  1.1  dholland 	struct disklabel l;
    378  1.1  dholland 	int i;
    379  1.1  dholland 	int numdisks;
    380  1.1  dholland 
    381  1.1  dholland 	/* initialize */
    382  1.1  dholland 	numdisks = 0;
    383  1.1  dholland 
    384  1.1  dholland 	for (xd = disk_names; *xd != NULL; xd++) {
    385  1.1  dholland 		for (i = 0; i < MAX_DISKS; i++) {
    386  1.1  dholland 			strlcpy(dd->dd_name, *xd, sizeof dd->dd_name - 2);
    387  1.1  dholland 			cp = strchr(dd->dd_name, ':');
    388  1.1  dholland 			if (cp != NULL)
    389  1.1  dholland 				dd->dd_no_mbr = !strcmp(cp, ":no_mbr");
    390  1.1  dholland 			else {
    391  1.1  dholland 				dd->dd_no_mbr = 0;
    392  1.1  dholland 				cp = strchr(dd->dd_name, 0);
    393  1.1  dholland 			}
    394  1.1  dholland 
    395  1.1  dholland 			snprintf(cp, 2 + 1, "%d", i);
    396  1.1  dholland 			if (!get_geom(dd->dd_name, &l)) {
    397  1.1  dholland 				if (errno == ENOENT)
    398  1.1  dholland 					break;
    399  1.1  dholland 				continue;
    400  1.1  dholland 			}
    401  1.1  dholland 
    402  1.1  dholland 			/*
    403  1.1  dholland 			 * Exclude a disk mounted as root partition,
    404  1.1  dholland 			 * in case of install-image on a USB memstick.
    405  1.1  dholland 			 */
    406  1.1  dholland 			if (is_active_rootpart(dd->dd_name, 0))
    407  1.1  dholland 				continue;
    408  1.1  dholland 
    409  1.1  dholland 			dd->dd_cyl = l.d_ncylinders;
    410  1.1  dholland 			dd->dd_head = l.d_ntracks;
    411  1.1  dholland 			dd->dd_sec = l.d_nsectors;
    412  1.1  dholland 			dd->dd_secsize = l.d_secsize;
    413  1.1  dholland 			dd->dd_totsec = l.d_secperunit;
    414  1.1  dholland 			get_descr(dd);
    415  1.1  dholland 			dd++;
    416  1.1  dholland 			numdisks++;
    417  1.1  dholland 			if (numdisks >= MAX_DISKS)
    418  1.1  dholland 				return numdisks;
    419  1.1  dholland 		}
    420  1.1  dholland 	}
    421  1.1  dholland 	return numdisks;
    422  1.1  dholland }
    423  1.1  dholland 
    424  1.1  dholland static int
    425  1.1  dholland set_dsk_select(menudesc *m, void *arg)
    426  1.1  dholland {
    427  1.1  dholland 	*(int *)arg = m->cursel;
    428  1.1  dholland 	return 1;
    429  1.1  dholland }
    430  1.1  dholland 
    431  1.1  dholland int
    432  1.1  dholland find_disks(const char *doingwhat)
    433  1.1  dholland {
    434  1.1  dholland 	struct disk_desc disks[MAX_DISKS];
    435  1.1  dholland 	menu_ent dsk_menu[nelem(disks)];
    436  1.1  dholland 	struct disk_desc *disk;
    437  1.1  dholland 	int i;
    438  1.1  dholland 	int numdisks;
    439  1.1  dholland 	int selected_disk = 0;
    440  1.1  dholland 	int menu_no;
    441  1.1  dholland 
    442  1.1  dholland 	/* Find disks. */
    443  1.1  dholland 	numdisks = get_disks(disks);
    444  1.1  dholland 
    445  1.1  dholland 	/* need a redraw here, kernel messages hose everything */
    446  1.1  dholland 	touchwin(stdscr);
    447  1.1  dholland 	refresh();
    448  1.1  dholland 	/* Kill typeahead, it won't be what the user had in mind */
    449  1.1  dholland 	fpurge(stdin);
    450  1.1  dholland 
    451  1.1  dholland 	if (numdisks == 0) {
    452  1.1  dholland 		/* No disks found! */
    453  1.1  dholland 		msg_display(MSG_nodisk);
    454  1.1  dholland 		process_menu(MENU_ok, NULL);
    455  1.1  dholland 		/*endwin();*/
    456  1.1  dholland 		return -1;
    457  1.1  dholland 	}
    458  1.1  dholland 
    459  1.1  dholland 	if (numdisks == 1) {
    460  1.1  dholland 		/* One disk found! */
    461  1.1  dholland 		msg_display(MSG_onedisk, disks[0].dd_descr, doingwhat);
    462  1.1  dholland 		process_menu(MENU_ok, NULL);
    463  1.1  dholland 	} else {
    464  1.1  dholland 		/* Multiple disks found! */
    465  1.1  dholland 		for (i = 0; i < numdisks; i++) {
    466  1.1  dholland 			dsk_menu[i].opt_name = disks[i].dd_descr;
    467  1.1  dholland 			dsk_menu[i].opt_menu = OPT_NOMENU;
    468  1.1  dholland 			dsk_menu[i].opt_flags = OPT_EXIT;
    469  1.1  dholland 			dsk_menu[i].opt_action = set_dsk_select;
    470  1.1  dholland 		}
    471  1.1  dholland 		menu_no = new_menu(MSG_Available_disks,
    472  1.1  dholland 			dsk_menu, numdisks, -1, 4, 0, 0,
    473  1.1  dholland 			MC_SCROLL | MC_NOEXITOPT,
    474  1.1  dholland 			NULL, NULL, NULL, NULL, NULL);
    475  1.1  dholland 		if (menu_no == -1)
    476  1.1  dholland 			return -1;
    477  1.1  dholland 		msg_display(MSG_ask_disk, doingwhat);
    478  1.1  dholland 		process_menu(menu_no, &selected_disk);
    479  1.1  dholland 		free_menu(menu_no);
    480  1.1  dholland 	}
    481  1.1  dholland 
    482  1.1  dholland 	disk = disks + selected_disk;
    483  1.1  dholland 	strlcpy(diskdev, disk->dd_name, sizeof diskdev);
    484  1.1  dholland 
    485  1.1  dholland 	/* Use as a default disk if the user has the sets on a local disk */
    486  1.1  dholland 	strlcpy(localfs_dev, disk->dd_name, sizeof localfs_dev);
    487  1.1  dholland 
    488  1.1  dholland 	sectorsize = disk->dd_secsize;
    489  1.1  dholland 	dlcyl = disk->dd_cyl;
    490  1.1  dholland 	dlhead = disk->dd_head;
    491  1.1  dholland 	dlsec = disk->dd_sec;
    492  1.1  dholland 	dlsize = disk->dd_totsec;
    493  1.1  dholland 	no_mbr = disk->dd_no_mbr;
    494  1.1  dholland 	if (dlsize == 0)
    495  1.1  dholland 		dlsize = disk->dd_cyl * disk->dd_head * disk->dd_sec;
    496  1.1  dholland 	if (dlsize > UINT32_MAX) {
    497  1.1  dholland 		msg_display(MSG_toobigdisklabel);
    498  1.1  dholland 		process_menu(MENU_ok, NULL);
    499  1.1  dholland 		return -1;
    500  1.1  dholland 	}
    501  1.1  dholland 	dlcylsize = dlhead * dlsec;
    502  1.1  dholland 
    503  1.1  dholland 	/* Get existing/default label */
    504  1.1  dholland 	memset(&oldlabel, 0, sizeof oldlabel);
    505  1.1  dholland 	incorelabel(diskdev, oldlabel);
    506  1.1  dholland 
    507  1.1  dholland 	/* Set 'target' label to current label in case we don't change it */
    508  1.1  dholland 	memcpy(&bsdlabel, &oldlabel, sizeof bsdlabel);
    509  1.1  dholland 
    510  1.1  dholland 	return numdisks;
    511  1.1  dholland }
    512  1.1  dholland 
    513  1.1  dholland void
    514  1.1  dholland fmt_fspart(menudesc *m, int ptn, void *arg)
    515  1.1  dholland {
    516  1.1  dholland 	unsigned int poffset, psize, pend;
    517  1.1  dholland 	const char *desc;
    518  1.1  dholland 	static const char *Yes;
    519  1.1  dholland 	partinfo *p = bsdlabel + ptn;
    520  1.1  dholland 
    521  1.1  dholland 	if (Yes == NULL)
    522  1.1  dholland 		Yes = msg_string(MSG_Yes);
    523  1.1  dholland 
    524  1.1  dholland 	poffset = p->pi_offset / sizemult;
    525  1.1  dholland 	psize = p->pi_size / sizemult;
    526  1.1  dholland 	if (psize == 0)
    527  1.1  dholland 		pend = 0;
    528  1.1  dholland 	else
    529  1.1  dholland 		pend = (p->pi_offset + p->pi_size) / sizemult - 1;
    530  1.1  dholland 
    531  1.1  dholland 	if (p->pi_fstype == FS_BSDFFS)
    532  1.1  dholland 		if (p->pi_flags & PIF_FFSv2)
    533  1.1  dholland 			desc = "FFSv2";
    534  1.1  dholland 		else
    535  1.1  dholland 			desc = "FFSv1";
    536  1.1  dholland 	else
    537  1.1  dholland 		desc = getfslabelname(p->pi_fstype);
    538  1.1  dholland 
    539  1.1  dholland #ifdef PART_BOOT
    540  1.1  dholland 	if (ptn == PART_BOOT)
    541  1.1  dholland 		desc = msg_string(MSG_Boot_partition_cant_change);
    542  1.1  dholland #endif
    543  1.1  dholland 	if (ptn == getrawpartition())
    544  1.1  dholland 		desc = msg_string(MSG_Whole_disk_cant_change);
    545  1.1  dholland 	else {
    546  1.1  dholland 		if (ptn == PART_C)
    547  1.1  dholland 			desc = msg_string(MSG_NetBSD_partition_cant_change);
    548  1.1  dholland 	}
    549  1.1  dholland 
    550  1.1  dholland 	wprintw(m->mw, msg_string(MSG_fspart_row),
    551  1.1  dholland 			poffset, pend, psize, desc,
    552  1.1  dholland 			p->pi_flags & PIF_NEWFS ? Yes : "",
    553  1.1  dholland 			p->pi_flags & PIF_MOUNT ? Yes : "",
    554  1.1  dholland 			p->pi_mount);
    555  1.1  dholland }
    556  1.1  dholland 
    557  1.1  dholland /*
    558  1.1  dholland  * Label a disk using an MD-specific string DISKLABEL_CMD for
    559  1.1  dholland  * to invoke disklabel.
    560  1.1  dholland  * if MD code does not define DISKLABEL_CMD, this is a no-op.
    561  1.1  dholland  *
    562  1.1  dholland  * i386 port uses "/sbin/disklabel -w -r", just like i386
    563  1.1  dholland  * miniroot scripts, though this may leave a bogus incore label.
    564  1.1  dholland  *
    565  1.1  dholland  * Sun ports should use DISKLABEL_CMD "/sbin/disklabel -w"
    566  1.1  dholland  * to get incore to ondisk inode translation for the Sun proms.
    567  1.1  dholland  */
    568  1.1  dholland int
    569  1.1  dholland write_disklabel (void)
    570  1.1  dholland {
    571  1.1  dholland 
    572  1.1  dholland #ifdef DISKLABEL_CMD
    573  1.1  dholland 	/* disklabel the disk */
    574  1.1  dholland 	return run_program(RUN_DISPLAY, "%s -f /tmp/disktab %s '%s'",
    575  1.1  dholland 	    DISKLABEL_CMD, diskdev, bsddiskname);
    576  1.1  dholland #else
    577  1.1  dholland 	return 0;
    578  1.1  dholland #endif
    579  1.1  dholland }
    580  1.1  dholland 
    581  1.1  dholland 
    582  1.1  dholland static int
    583  1.1  dholland ptn_sort(const void *a, const void *b)
    584  1.1  dholland {
    585  1.1  dholland 	return strcmp(bsdlabel[*(const int *)a].pi_mount,
    586  1.1  dholland 		      bsdlabel[*(const int *)b].pi_mount);
    587  1.1  dholland }
    588  1.1  dholland 
    589  1.1  dholland int
    590  1.1  dholland make_filesystems(void)
    591  1.1  dholland {
    592  1.1  dholland 	unsigned int i;
    593  1.1  dholland 	int ptn;
    594  1.1  dholland 	int ptn_order[nelem(bsdlabel)];
    595  1.1  dholland 	int error = 0;
    596  1.1  dholland 	unsigned int maxpart = getmaxpartitions();
    597  1.1  dholland 	char *newfs;
    598  1.1  dholland 	const char *mnt_opts;
    599  1.1  dholland 	const char *fsname;
    600  1.1  dholland 	partinfo *lbl;
    601  1.1  dholland 
    602  1.1  dholland 	if (maxpart > nelem(bsdlabel))
    603  1.1  dholland 		maxpart = nelem(bsdlabel);
    604  1.1  dholland 
    605  1.1  dholland 	/* Making new file systems and mounting them */
    606  1.1  dholland 
    607  1.1  dholland 	/* sort to ensure /usr/local is mounted after /usr (etc) */
    608  1.1  dholland 	for (i = 0; i < maxpart; i++)
    609  1.1  dholland 		ptn_order[i] = i;
    610  1.1  dholland 	qsort(ptn_order, maxpart, sizeof ptn_order[0], ptn_sort);
    611  1.1  dholland 
    612  1.1  dholland 	for (i = 0; i < maxpart; i++) {
    613  1.1  dholland 		/*
    614  1.1  dholland 		 * newfs and mount. For now, process only BSD filesystems.
    615  1.1  dholland 		 * but if this is the mounted-on root, has no mount
    616  1.1  dholland 		 * point defined, or is marked preserve, don't touch it!
    617  1.1  dholland 		 */
    618  1.1  dholland 		ptn = ptn_order[i];
    619  1.1  dholland 		lbl = bsdlabel + ptn;
    620  1.1  dholland 
    621  1.1  dholland 		if (is_active_rootpart(diskdev, ptn))
    622  1.1  dholland 			continue;
    623  1.1  dholland 
    624  1.1  dholland 		if (*lbl->pi_mount == 0)
    625  1.1  dholland 			/* No mount point */
    626  1.1  dholland 			continue;
    627  1.1  dholland 
    628  1.1  dholland 		newfs = NULL;
    629  1.1  dholland 		mnt_opts = NULL;
    630  1.1  dholland 		fsname = NULL;
    631  1.1  dholland 		switch (lbl->pi_fstype) {
    632  1.1  dholland 		case FS_APPLEUFS:
    633  1.1  dholland 			asprintf(&newfs, "/sbin/newfs %s%.0d",
    634  1.1  dholland 				lbl->pi_isize != 0 ? "-i" : "", lbl->pi_isize);
    635  1.1  dholland 			mnt_opts = "-tffs -o async";
    636  1.1  dholland 			fsname = "ffs";
    637  1.1  dholland 			break;
    638  1.1  dholland 		case FS_BSDFFS:
    639  1.1  dholland 			asprintf(&newfs,
    640  1.1  dholland 			    "/sbin/newfs -V2 -O %d -b %d -f %d%s%.0d",
    641  1.1  dholland 			    lbl->pi_flags & PIF_FFSv2 ? 2 : 1,
    642  1.1  dholland 			    lbl->pi_fsize * lbl->pi_frag, lbl->pi_fsize,
    643  1.1  dholland 			    lbl->pi_isize != 0 ? " -i " : "", lbl->pi_isize);
    644  1.1  dholland 			if (lbl->pi_flags & PIF_LOG)
    645  1.1  dholland 				mnt_opts = "-tffs -o log";
    646  1.1  dholland 			else
    647  1.1  dholland 				mnt_opts = "-tffs -o async";
    648  1.1  dholland 			fsname = "ffs";
    649  1.1  dholland 			break;
    650  1.1  dholland 		case FS_BSDLFS:
    651  1.1  dholland 			asprintf(&newfs, "/sbin/newfs_lfs -b %d",
    652  1.1  dholland 				lbl->pi_fsize * lbl->pi_frag);
    653  1.1  dholland 			mnt_opts = "-tlfs";
    654  1.1  dholland 			fsname = "lfs";
    655  1.1  dholland 			break;
    656  1.1  dholland 		case FS_MSDOS:
    657  1.1  dholland #ifdef USE_NEWFS_MSDOS
    658  1.1  dholland 			asprintf(&newfs, "/sbin/newfs_msdos");
    659  1.1  dholland #endif
    660  1.1  dholland 			mnt_opts = "-tmsdos";
    661  1.1  dholland 			fsname = "msdos";
    662  1.1  dholland 			break;
    663  1.1  dholland #ifdef USE_SYSVBFS
    664  1.1  dholland 		case FS_SYSVBFS:
    665  1.1  dholland 			asprintf(&newfs, "/sbin/newfs_sysvbfs");
    666  1.1  dholland 			mnt_opts = "-tsysvbfs";
    667  1.1  dholland 			fsname = "sysvbfs";
    668  1.1  dholland 			break;
    669  1.1  dholland #endif
    670  1.1  dholland #ifdef USE_EXT2FS
    671  1.1  dholland 		case FS_EX2FS:
    672  1.1  dholland 			asprintf(&newfs, "/sbin/newfs_ext2fs");
    673  1.1  dholland 			mnt_opts = "-text2fs";
    674  1.1  dholland 			fsname = "ext2fs";
    675  1.1  dholland 			break;
    676  1.1  dholland #endif
    677  1.1  dholland 		}
    678  1.1  dholland 		if (lbl->pi_flags & PIF_NEWFS && newfs != NULL) {
    679  1.1  dholland #ifdef USE_NEWFS_MSDOS
    680  1.1  dholland 			if (lbl->pi_fstype == FS_MSDOS) {
    681  1.1  dholland 			        /* newfs only if mount fails */
    682  1.1  dholland 			        if (run_program(RUN_SILENT | RUN_ERROR_OK,
    683  1.1  dholland 				    "mount -rt msdos /dev/%s%c /mnt2",
    684  1.1  dholland 				    diskdev, 'a' + ptn) != 0)
    685  1.1  dholland 					error = run_program(
    686  1.1  dholland 					    RUN_DISPLAY | RUN_PROGRESS,
    687  1.1  dholland 					    "%s /dev/r%s%c",
    688  1.1  dholland 					    newfs, diskdev, 'a' + ptn);
    689  1.1  dholland 				else {
    690  1.1  dholland 			        	run_program(RUN_SILENT | RUN_ERROR_OK,
    691  1.1  dholland 					    "umount /mnt2");
    692  1.1  dholland 					error = 0;
    693  1.1  dholland 				}
    694  1.1  dholland 			} else
    695  1.1  dholland #endif
    696  1.1  dholland 			error = run_program(RUN_DISPLAY | RUN_PROGRESS,
    697  1.1  dholland 			    "%s /dev/r%s%c", newfs, diskdev, 'a' + ptn);
    698  1.1  dholland 		} else {
    699  1.1  dholland 			/* We'd better check it isn't dirty */
    700  1.1  dholland 			error = fsck_preen(diskdev, ptn, fsname);
    701  1.1  dholland 		}
    702  1.1  dholland 		free(newfs);
    703  1.1  dholland 		if (error != 0)
    704  1.1  dholland 			return error;
    705  1.1  dholland 
    706  1.1  dholland 		md_pre_mount();
    707  1.1  dholland 
    708  1.1  dholland 		if (lbl->pi_flags & PIF_MOUNT && mnt_opts != NULL) {
    709  1.1  dholland 			make_target_dir(lbl->pi_mount);
    710  1.1  dholland 			error = target_mount(mnt_opts, diskdev, ptn,
    711  1.1  dholland 					    lbl->pi_mount);
    712  1.1  dholland 			if (error) {
    713  1.1  dholland 				msg_display(MSG_mountfail,
    714  1.1  dholland 					    diskdev, 'a' + ptn, lbl->pi_mount);
    715  1.1  dholland 				process_menu(MENU_ok, NULL);
    716  1.1  dholland 				return error;
    717  1.1  dholland 			}
    718  1.1  dholland 		}
    719  1.1  dholland 	}
    720  1.1  dholland 	return 0;
    721  1.1  dholland }
    722  1.1  dholland 
    723  1.1  dholland int
    724  1.1  dholland make_fstab(void)
    725  1.1  dholland {
    726  1.1  dholland 	FILE *f;
    727  1.1  dholland 	int i, swap_dev = -1;
    728  1.1  dholland 	const char *dump_dev;
    729  1.1  dholland 
    730  1.1  dholland 	/* Create the fstab. */
    731  1.1  dholland 	make_target_dir("/etc");
    732  1.1  dholland 	f = target_fopen("/etc/fstab", "w");
    733  1.1  dholland 	if (logfp)
    734  1.1  dholland 		(void)fprintf(logfp,
    735  1.1  dholland 		    "Creating %s/etc/fstab.\n", target_prefix());
    736  1.1  dholland 	scripting_fprintf(NULL, "cat <<EOF >%s/etc/fstab\n", target_prefix());
    737  1.1  dholland 
    738  1.1  dholland 	if (f == NULL) {
    739  1.1  dholland #ifndef DEBUG
    740  1.1  dholland 		msg_display(MSG_createfstab);
    741  1.1  dholland 		if (logfp)
    742  1.1  dholland 			(void)fprintf(logfp, "Failed to make /etc/fstab!\n");
    743  1.1  dholland 		process_menu(MENU_ok, NULL);
    744  1.1  dholland 		return 1;
    745  1.1  dholland #else
    746  1.1  dholland 		f = stdout;
    747  1.1  dholland #endif
    748  1.1  dholland 	}
    749  1.1  dholland 
    750  1.1  dholland 	scripting_fprintf(f, "# NetBSD %s/etc/fstab\n# See /usr/share/examples/"
    751  1.1  dholland 		"fstab/ for more examples.\n", target_prefix());
    752  1.1  dholland 	for (i = 0; i < getmaxpartitions(); i++) {
    753  1.1  dholland 		const char *s = "";
    754  1.1  dholland 		const char *mp = bsdlabel[i].pi_mount;
    755  1.1  dholland 		const char *fstype = "ffs";
    756  1.1  dholland 		int fsck_pass = 0, dump_freq = 0;
    757  1.1  dholland 
    758  1.1  dholland 		if (!*mp) {
    759  1.1  dholland 			/*
    760  1.1  dholland 			 * No mount point specified, comment out line and
    761  1.1  dholland 			 * use /mnt as a placeholder for the mount point.
    762  1.1  dholland 			 */
    763  1.1  dholland 			s = "# ";
    764  1.1  dholland 			mp = "/mnt";
    765  1.1  dholland 		}
    766  1.1  dholland 
    767  1.1  dholland 		switch (bsdlabel[i].pi_fstype) {
    768  1.1  dholland 		case FS_UNUSED:
    769  1.1  dholland 			continue;
    770  1.1  dholland 		case FS_BSDLFS:
    771  1.1  dholland 			/* If there is no LFS, just comment it out. */
    772  1.1  dholland 			if (!check_lfs_progs())
    773  1.1  dholland 				s = "# ";
    774  1.1  dholland 			fstype = "lfs";
    775  1.1  dholland 			/* FALLTHROUGH */
    776  1.1  dholland 		case FS_BSDFFS:
    777  1.1  dholland 			fsck_pass = (strcmp(mp, "/") == 0) ? 1 : 2;
    778  1.1  dholland 			dump_freq = 1;
    779  1.1  dholland 			break;
    780  1.1  dholland 		case FS_MSDOS:
    781  1.1  dholland 			fstype = "msdos";
    782  1.1  dholland 			break;
    783  1.1  dholland 		case FS_SWAP:
    784  1.1  dholland 			if (swap_dev == -1) {
    785  1.1  dholland 				swap_dev = i;
    786  1.1  dholland 				dump_dev = ",dp";
    787  1.1  dholland 			} else {
    788  1.1  dholland 				dump_dev ="";
    789  1.1  dholland 			}
    790  1.1  dholland 			scripting_fprintf(f, "/dev/%s%c\t\tnone\tswap\tsw%s\t\t 0 0\n",
    791  1.1  dholland 				diskdev, 'a' + i, dump_dev);
    792  1.1  dholland 			continue;
    793  1.1  dholland #ifdef USE_SYSVBFS
    794  1.1  dholland 		case FS_SYSVBFS:
    795  1.1  dholland 			fstype = "sysvbfs";
    796  1.1  dholland 			make_target_dir("/stand");
    797  1.1  dholland 			break;
    798  1.1  dholland #endif
    799  1.1  dholland 		default:
    800  1.1  dholland 			fstype = "???";
    801  1.1  dholland 			s = "# ";
    802  1.1  dholland 			break;
    803  1.1  dholland 		}
    804  1.1  dholland 		/* The code that remounts root rw doesn't check the partition */
    805  1.1  dholland 		if (strcmp(mp, "/") == 0 && !(bsdlabel[i].pi_flags & PIF_MOUNT))
    806  1.1  dholland 			s = "# ";
    807  1.1  dholland 
    808  1.1  dholland  		scripting_fprintf(f,
    809  1.1  dholland 		  "%s/dev/%s%c\t\t%s\t%s\trw%s%s%s%s%s%s%s%s\t\t %d %d\n",
    810  1.1  dholland 		   s, diskdev, 'a' + i, mp, fstype,
    811  1.1  dholland 		   bsdlabel[i].pi_flags & PIF_LOG ? ",log" : "",
    812  1.1  dholland 		   bsdlabel[i].pi_flags & PIF_MOUNT ? "" : ",noauto",
    813  1.1  dholland 		   bsdlabel[i].pi_flags & PIF_ASYNC ? ",async" : "",
    814  1.1  dholland 		   bsdlabel[i].pi_flags & PIF_NOATIME ? ",noatime" : "",
    815  1.1  dholland 		   bsdlabel[i].pi_flags & PIF_NODEV ? ",nodev" : "",
    816  1.1  dholland 		   bsdlabel[i].pi_flags & PIF_NODEVMTIME ? ",nodevmtime" : "",
    817  1.1  dholland 		   bsdlabel[i].pi_flags & PIF_NOEXEC ? ",noexec" : "",
    818  1.1  dholland 		   bsdlabel[i].pi_flags & PIF_NOSUID ? ",nosuid" : "",
    819  1.1  dholland 		   dump_freq, fsck_pass);
    820  1.1  dholland 	}
    821  1.1  dholland 
    822  1.1  dholland 	if (tmp_ramdisk_size != 0) {
    823  1.1  dholland #ifdef HAVE_TMPFS
    824  1.1  dholland 		scripting_fprintf(f, "tmpfs\t\t/tmp\ttmpfs\trw,-m=1777,-s=%"
    825  1.1  dholland 		    PRIi64 "\n",
    826  1.1  dholland 		    tmp_ramdisk_size * 512);
    827  1.1  dholland #else
    828  1.1  dholland 		if (swap_dev != -1)
    829  1.1  dholland 			scripting_fprintf(f, "/dev/%s%c\t\t/tmp\tmfs\trw,-s=%"
    830  1.1  dholland 			    PRIi64 "\n",
    831  1.1  dholland 			    diskdev, 'a' + swap_dev, tmp_ramdisk_size);
    832  1.1  dholland 		else
    833  1.1  dholland 			scripting_fprintf(f, "swap\t\t/tmp\tmfs\trw,-s=%"
    834  1.1  dholland 			    PRIi64 "\n",
    835  1.1  dholland 			    tmp_ramdisk_size);
    836  1.1  dholland #endif
    837  1.1  dholland 	}
    838  1.1  dholland 
    839  1.1  dholland 	/* Add /kern, /proc and /dev/pts to fstab and make mountpoint. */
    840  1.1  dholland 	scripting_fprintf(f, "kernfs\t\t/kern\tkernfs\trw\n");
    841  1.1  dholland 	scripting_fprintf(f, "ptyfs\t\t/dev/pts\tptyfs\trw\n");
    842  1.1  dholland 	scripting_fprintf(f, "procfs\t\t/proc\tprocfs\trw\n");
    843  1.1  dholland 	scripting_fprintf(f, "/dev/%s\t\t/cdrom\tcd9660\tro,noauto\n",
    844  1.1  dholland 	    get_default_cdrom());
    845  1.1  dholland 	scripting_fprintf(f, "%stmpfs\t\t/var/shm\ttmpfs\trw,-m1777,-sram%%25\n",
    846  1.1  dholland 	    tmpfs_on_var_shm() ? "" : "#");
    847  1.1  dholland 	make_target_dir("/kern");
    848  1.1  dholland 	make_target_dir("/proc");
    849  1.1  dholland 	make_target_dir("/dev/pts");
    850  1.1  dholland 	make_target_dir("/cdrom");
    851  1.1  dholland 	make_target_dir("/var/shm");
    852  1.1  dholland 
    853  1.1  dholland 	scripting_fprintf(NULL, "EOF\n");
    854  1.1  dholland 
    855  1.1  dholland #ifndef DEBUG
    856  1.1  dholland 	fclose(f);
    857  1.1  dholland 	fflush(NULL);
    858  1.1  dholland #endif
    859  1.1  dholland 	return 0;
    860  1.1  dholland }
    861  1.1  dholland 
    862  1.1  dholland 
    863  1.1  dholland 
    864  1.1  dholland static int
    865  1.1  dholland /*ARGSUSED*/
    866  1.1  dholland foundffs(struct data *list, size_t num)
    867  1.1  dholland {
    868  1.1  dholland 	int error;
    869  1.1  dholland 
    870  1.1  dholland 	if (num < 2 || strcmp(list[1].u.s_val, "/") == 0 ||
    871  1.1  dholland 	    strstr(list[2].u.s_val, "noauto") != NULL)
    872  1.1  dholland 		return 0;
    873  1.1  dholland 
    874  1.1  dholland 	error = fsck_preen(list[0].u.s_val, ' '-'a', "ffs");
    875  1.1  dholland 	if (error != 0)
    876  1.1  dholland 		return error;
    877  1.1  dholland 
    878  1.1  dholland 	error = target_mount("", list[0].u.s_val, ' '-'a', list[1].u.s_val);
    879  1.1  dholland 	if (error != 0) {
    880  1.1  dholland 		msg_display(MSG_mount_failed, list[0].u.s_val);
    881  1.1  dholland 		process_menu(MENU_noyes, NULL);
    882  1.1  dholland 		if (!yesno)
    883  1.1  dholland 			return error;
    884  1.1  dholland 	}
    885  1.1  dholland 	return 0;
    886  1.1  dholland }
    887  1.1  dholland 
    888  1.1  dholland #ifdef USE_SYSVBFS
    889  1.1  dholland static int
    890  1.1  dholland /*ARGSUSED*/
    891  1.1  dholland foundsysvbfs(struct data *list, size_t num)
    892  1.1  dholland {
    893  1.1  dholland 	int error;
    894  1.1  dholland 
    895  1.1  dholland 	if (num < 2 || strcmp(list[1].u.s_val, "/") == 0 ||
    896  1.1  dholland 	    strstr(list[2].u.s_val, "noauto") != NULL)
    897  1.1  dholland 		return 0;
    898  1.1  dholland 
    899  1.1  dholland 	error = target_mount("", list[0].u.s_val, ' '-'a', list[1].u.s_val);
    900  1.1  dholland 	if (error != 0)
    901  1.1  dholland 		return error;
    902  1.1  dholland 	return 0;
    903  1.1  dholland }
    904  1.1  dholland #endif
    905  1.1  dholland 
    906  1.1  dholland /*
    907  1.1  dholland  * Do an fsck. On failure, inform the user by showing a warning
    908  1.1  dholland  * message and doing menu_ok() before proceeding.
    909  1.1  dholland  * Returns 0 on success, or nonzero return code from fsck() on failure.
    910  1.1  dholland  */
    911  1.1  dholland static int
    912  1.1  dholland fsck_preen(const char *disk, int ptn, const char *fsname)
    913  1.1  dholland {
    914  1.1  dholland 	char *prog;
    915  1.1  dholland 	int error;
    916  1.1  dholland 
    917  1.1  dholland 	ptn += 'a';
    918  1.1  dholland 	if (fsname == NULL)
    919  1.1  dholland 		return 0;
    920  1.1  dholland 	/* first, check if fsck program exists, if not, assume ok */
    921  1.1  dholland 	asprintf(&prog, "/sbin/fsck_%s", fsname);
    922  1.1  dholland 	if (prog == NULL)
    923  1.1  dholland 		return 0;
    924  1.1  dholland 	if (access(prog, X_OK) != 0)
    925  1.1  dholland 		return 0;
    926  1.1  dholland 	if (!strcmp(fsname,"ffs"))
    927  1.1  dholland 		fixsb(prog, disk, ptn);
    928  1.1  dholland 	error = run_program(0, "%s -p -q /dev/r%s%c", prog, disk, ptn);
    929  1.1  dholland 	free(prog);
    930  1.1  dholland 	if (error != 0) {
    931  1.1  dholland 		msg_display(MSG_badfs, disk, ptn, error);
    932  1.1  dholland 		process_menu(MENU_noyes, NULL);
    933  1.1  dholland 		if (yesno)
    934  1.1  dholland 			error = 0;
    935  1.1  dholland 		/* XXX at this point maybe we should run a full fsck? */
    936  1.1  dholland 	}
    937  1.1  dholland 	return error;
    938  1.1  dholland }
    939  1.1  dholland 
    940  1.1  dholland /* This performs the same function as the etc/rc.d/fixsb script
    941  1.1  dholland  * which attempts to correct problems with ffs1 filesystems
    942  1.1  dholland  * which may have been introduced by booting a netbsd-current kernel
    943  1.1  dholland  * from between April of 2003 and January 2004. For more information
    944  1.1  dholland  * This script was developed as a response to NetBSD pr install/25138
    945  1.1  dholland  * Additional prs regarding the original issue include:
    946  1.1  dholland  *  bin/17910 kern/21283 kern/21404 port-macppc/23925 port-macppc/23926
    947  1.1  dholland  */
    948  1.1  dholland static void
    949  1.1  dholland fixsb(const char *prog, const char *disk, char ptn)
    950  1.1  dholland {
    951  1.1  dholland 	int fd;
    952  1.1  dholland 	int rval;
    953  1.1  dholland 	union {
    954  1.1  dholland 		struct fs fs;
    955  1.1  dholland 		char buf[SBLOCKSIZE];
    956  1.1  dholland 	} sblk;
    957  1.1  dholland 	struct fs *fs = &sblk.fs;
    958  1.1  dholland 
    959  1.1  dholland 	snprintf(sblk.buf, sizeof(sblk.buf), "/dev/r%s%c",
    960  1.1  dholland 		disk, ptn == ' ' ? 0 : ptn);
    961  1.1  dholland 	fd = open(sblk.buf, O_RDONLY);
    962  1.1  dholland 	if (fd == -1)
    963  1.1  dholland 		return;
    964  1.1  dholland 
    965  1.1  dholland 	/* Read ffsv1 main superblock */
    966  1.1  dholland 	rval = pread(fd, sblk.buf, sizeof sblk.buf, SBLOCK_UFS1);
    967  1.1  dholland 	close(fd);
    968  1.1  dholland 	if (rval != sizeof sblk.buf)
    969  1.1  dholland 		return;
    970  1.1  dholland 
    971  1.1  dholland 	if (fs->fs_magic != FS_UFS1_MAGIC &&
    972  1.1  dholland 	    fs->fs_magic != FS_UFS1_MAGIC_SWAPPED)
    973  1.1  dholland 		/* Not FFSv1 */
    974  1.1  dholland 		return;
    975  1.1  dholland 	if (fs->fs_old_flags & FS_FLAGS_UPDATED)
    976  1.1  dholland 		/* properly updated fslevel 4 */
    977  1.1  dholland 		return;
    978  1.1  dholland 	if (fs->fs_bsize != fs->fs_maxbsize)
    979  1.1  dholland 		/* not messed up */
    980  1.1  dholland 		return;
    981  1.1  dholland 
    982  1.1  dholland 	/*
    983  1.1  dholland 	 * OK we have a munged fs, first 'upgrade' to fslevel 4,
    984  1.1  dholland 	 * We specify -b16 in order to stop fsck bleating that the
    985  1.1  dholland 	 * sb doesn't match the first alternate.
    986  1.1  dholland 	 */
    987  1.1  dholland 	run_program(RUN_DISPLAY | RUN_PROGRESS,
    988  1.1  dholland 	    "%s -p -b 16 -c 4 /dev/r%s%c", prog, disk, ptn);
    989  1.1  dholland 	/* Then downgrade to fslevel 3 */
    990  1.1  dholland 	run_program(RUN_DISPLAY | RUN_PROGRESS,
    991  1.1  dholland 	    "%s -p -c 3 /dev/r%s%c", prog, disk, ptn);
    992  1.1  dholland }
    993  1.1  dholland 
    994  1.1  dholland /*
    995  1.1  dholland  * fsck and mount the root partition.
    996  1.1  dholland  */
    997  1.1  dholland static int
    998  1.1  dholland mount_root(void)
    999  1.1  dholland {
   1000  1.1  dholland 	int	error;
   1001  1.1  dholland 
   1002  1.1  dholland 	error = fsck_preen(diskdev, rootpart, "ffs");
   1003  1.1  dholland 	if (error != 0)
   1004  1.1  dholland 		return error;
   1005  1.1  dholland 
   1006  1.1  dholland 	md_pre_mount();
   1007  1.1  dholland 
   1008  1.1  dholland 	/* Mount /dev/<diskdev>a on target's "".
   1009  1.1  dholland 	 * If we pass "" as mount-on, Prefixing will DTRT.
   1010  1.1  dholland 	 * for now, use no options.
   1011  1.1  dholland 	 * XXX consider -o remount in case target root is
   1012  1.1  dholland 	 * current root, still readonly from single-user?
   1013  1.1  dholland 	 */
   1014  1.1  dholland 	return target_mount("", diskdev, rootpart, "");
   1015  1.1  dholland }
   1016  1.1  dholland 
   1017  1.1  dholland /* Get information on the file systems mounted from the root filesystem.
   1018  1.1  dholland  * Offer to convert them into 4.4BSD inodes if they are not 4.4BSD
   1019  1.1  dholland  * inodes.  Fsck them.  Mount them.
   1020  1.1  dholland  */
   1021  1.1  dholland 
   1022  1.1  dholland int
   1023  1.1  dholland mount_disks(void)
   1024  1.1  dholland {
   1025  1.1  dholland 	char *fstab;
   1026  1.1  dholland 	int   fstabsize;
   1027  1.1  dholland 	int   error;
   1028  1.1  dholland 
   1029  1.1  dholland 	static struct lookfor fstabbuf[] = {
   1030  1.1  dholland 		{"/dev/", "/dev/%s %s ffs %s", "c", NULL, 0, 0, foundffs},
   1031  1.1  dholland 		{"/dev/", "/dev/%s %s ufs %s", "c", NULL, 0, 0, foundffs},
   1032  1.1  dholland #ifdef USE_SYSVBFS
   1033  1.1  dholland 		{"/dev/", "/dev/%s %s sysvbfs %s", "c", NULL, 0, 0,
   1034  1.1  dholland 		    foundsysvbfs},
   1035  1.1  dholland #endif
   1036  1.1  dholland 	};
   1037  1.1  dholland 	static size_t numfstabbuf = sizeof(fstabbuf) / sizeof(struct lookfor);
   1038  1.1  dholland 
   1039  1.1  dholland 	/* First the root device. */
   1040  1.1  dholland 	if (target_already_root())
   1041  1.1  dholland 		/* avoid needing to call target_already_root() again */
   1042  1.1  dholland 		targetroot_mnt[0] = 0;
   1043  1.1  dholland 	else {
   1044  1.1  dholland 		error = mount_root();
   1045  1.1  dholland 		if (error != 0 && error != EBUSY)
   1046  1.1  dholland 			return -1;
   1047  1.1  dholland 	}
   1048  1.1  dholland 
   1049  1.1  dholland 	/* Check the target /etc/fstab exists before trying to parse it. */
   1050  1.1  dholland 	if (target_dir_exists_p("/etc") == 0 ||
   1051  1.1  dholland 	    target_file_exists_p("/etc/fstab") == 0) {
   1052  1.1  dholland 		msg_display(MSG_noetcfstab, diskdev);
   1053  1.1  dholland 		process_menu(MENU_ok, NULL);
   1054  1.1  dholland 		return -1;
   1055  1.1  dholland 	}
   1056  1.1  dholland 
   1057  1.1  dholland 
   1058  1.1  dholland 	/* Get fstab entries from the target-root /etc/fstab. */
   1059  1.1  dholland 	fstabsize = target_collect_file(T_FILE, &fstab, "/etc/fstab");
   1060  1.1  dholland 	if (fstabsize < 0) {
   1061  1.1  dholland 		/* error ! */
   1062  1.1  dholland 		msg_display(MSG_badetcfstab, diskdev);
   1063  1.1  dholland 		process_menu(MENU_ok, NULL);
   1064  1.1  dholland 		return -1;
   1065  1.1  dholland 	}
   1066  1.1  dholland 	error = walk(fstab, (size_t)fstabsize, fstabbuf, numfstabbuf);
   1067  1.1  dholland 	free(fstab);
   1068  1.1  dholland 
   1069  1.1  dholland 	return error;
   1070  1.1  dholland }
   1071  1.1  dholland 
   1072  1.1  dholland int
   1073  1.1  dholland set_swap(const char *disk, partinfo *pp)
   1074  1.1  dholland {
   1075  1.1  dholland 	int i;
   1076  1.1  dholland 	char *cp;
   1077  1.1  dholland 	int rval;
   1078  1.1  dholland 
   1079  1.1  dholland 	if (pp == NULL)
   1080  1.1  dholland 		pp = oldlabel;
   1081  1.1  dholland 
   1082  1.1  dholland 	for (i = 0; i < MAXPARTITIONS; i++) {
   1083  1.1  dholland 		if (pp[i].pi_fstype != FS_SWAP)
   1084  1.1  dholland 			continue;
   1085  1.1  dholland 		asprintf(&cp, "/dev/%s%c", disk, 'a' + i);
   1086  1.1  dholland 		rval = swapctl(SWAP_ON, cp, 0);
   1087  1.1  dholland 		free(cp);
   1088  1.1  dholland 		if (rval != 0)
   1089  1.1  dholland 			return -1;
   1090  1.1  dholland 	}
   1091  1.1  dholland 
   1092  1.1  dholland 	return 0;
   1093  1.1  dholland }
   1094  1.1  dholland 
   1095  1.1  dholland int
   1096  1.1  dholland check_swap(const char *disk, int remove_swap)
   1097  1.1  dholland {
   1098  1.1  dholland 	struct swapent *swap;
   1099  1.1  dholland 	char *cp;
   1100  1.1  dholland 	int nswap;
   1101  1.1  dholland 	int l;
   1102  1.1  dholland 	int rval = 0;
   1103  1.1  dholland 
   1104  1.1  dholland 	nswap = swapctl(SWAP_NSWAP, 0, 0);
   1105  1.1  dholland 	if (nswap <= 0)
   1106  1.1  dholland 		return 0;
   1107  1.1  dholland 
   1108  1.1  dholland 	swap = malloc(nswap * sizeof *swap);
   1109  1.1  dholland 	if (swap == NULL)
   1110  1.1  dholland 		return -1;
   1111  1.1  dholland 
   1112  1.1  dholland 	nswap = swapctl(SWAP_STATS, swap, nswap);
   1113  1.1  dholland 	if (nswap < 0)
   1114  1.1  dholland 		goto bad_swap;
   1115  1.1  dholland 
   1116  1.1  dholland 	l = strlen(disk);
   1117  1.1  dholland 	while (--nswap >= 0) {
   1118  1.1  dholland 		/* Should we check the se_dev or se_path? */
   1119  1.1  dholland 		cp = swap[nswap].se_path;
   1120  1.1  dholland 		if (memcmp(cp, "/dev/", 5) != 0)
   1121  1.1  dholland 			continue;
   1122  1.1  dholland 		if (memcmp(cp + 5, disk, l) != 0)
   1123  1.1  dholland 			continue;
   1124  1.1  dholland 		if (!isalpha(*(unsigned char *)(cp + 5 + l)))
   1125  1.1  dholland 			continue;
   1126  1.1  dholland 		if (cp[5 + l + 1] != 0)
   1127  1.1  dholland 			continue;
   1128  1.1  dholland 		/* ok path looks like it is for this device */
   1129  1.1  dholland 		if (!remove_swap) {
   1130  1.1  dholland 			/* count active swap areas */
   1131  1.1  dholland 			rval++;
   1132  1.1  dholland 			continue;
   1133  1.1  dholland 		}
   1134  1.1  dholland 		if (swapctl(SWAP_OFF, cp, 0) == -1)
   1135  1.1  dholland 			rval = -1;
   1136  1.1  dholland 	}
   1137  1.1  dholland 
   1138  1.1  dholland     done:
   1139  1.1  dholland 	free(swap);
   1140  1.1  dholland 	return rval;
   1141  1.1  dholland 
   1142  1.1  dholland     bad_swap:
   1143  1.1  dholland 	rval = -1;
   1144  1.1  dholland 	goto done;
   1145  1.1  dholland }
   1146  1.1  dholland 
   1147  1.1  dholland #ifdef HAVE_BOOTXX_xFS
   1148  1.1  dholland char *
   1149  1.1  dholland bootxx_name(void)
   1150  1.1  dholland {
   1151  1.1  dholland 	int fstype;
   1152  1.1  dholland 	const char *bootxxname;
   1153  1.1  dholland 	char *bootxx;
   1154  1.1  dholland 
   1155  1.1  dholland 	/* check we have boot code for the root partition type */
   1156  1.1  dholland 	fstype = bsdlabel[rootpart].pi_fstype;
   1157  1.1  dholland 	switch (fstype) {
   1158  1.1  dholland #if defined(BOOTXX_FFSV1) || defined(BOOTXX_FFSV2)
   1159  1.1  dholland 	case FS_BSDFFS:
   1160  1.1  dholland 		if (bsdlabel[rootpart].pi_flags & PIF_FFSv2) {
   1161  1.1  dholland #ifdef BOOTXX_FFSV2
   1162  1.1  dholland 			bootxxname = BOOTXX_FFSV2;
   1163  1.1  dholland #else
   1164  1.1  dholland 			bootxxname = NULL;
   1165  1.1  dholland #endif
   1166  1.1  dholland 		} else {
   1167  1.1  dholland #ifdef BOOTXX_FFSV1
   1168  1.1  dholland 			bootxxname = BOOTXX_FFSV1;
   1169  1.1  dholland #else
   1170  1.1  dholland 			bootxxname = NULL;
   1171  1.1  dholland #endif
   1172  1.1  dholland 		}
   1173  1.1  dholland 		break;
   1174  1.1  dholland #endif
   1175  1.1  dholland #ifdef BOOTXX_LFS
   1176  1.1  dholland 	case FS_BSDLFS:
   1177  1.1  dholland 		bootxxname = BOOTXX_LFS;
   1178  1.1  dholland 		break;
   1179  1.1  dholland #endif
   1180  1.1  dholland 	default:
   1181  1.1  dholland 		bootxxname = NULL;
   1182  1.1  dholland 		break;
   1183  1.1  dholland 	}
   1184  1.1  dholland 
   1185  1.1  dholland 	if (bootxxname == NULL)
   1186  1.1  dholland 		return NULL;
   1187  1.1  dholland 
   1188  1.1  dholland 	asprintf(&bootxx, "%s/%s", BOOTXXDIR, bootxxname);
   1189  1.1  dholland 	return bootxx;
   1190  1.1  dholland }
   1191  1.1  dholland #endif
   1192