Home | History | Annotate | Line # | Download | only in sysinst
disks.c revision 1.95.2.1
      1  1.95.2.1  perseant /*	$NetBSD: disks.c,v 1.95.2.1 2025/08/02 05:58:55 perseant 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.30    martin #include <assert.h>
     39       1.1  dholland #include <errno.h>
     40      1.17    martin #include <inttypes.h>
     41       1.1  dholland #include <stdio.h>
     42       1.1  dholland #include <stdlib.h>
     43       1.1  dholland #include <unistd.h>
     44       1.1  dholland #include <fcntl.h>
     45      1.20    martin #include <fnmatch.h>
     46       1.1  dholland #include <util.h>
     47       1.2    martin #include <uuid.h>
     48      1.30    martin #include <paths.h>
     49      1.47    martin #include <fstab.h>
     50       1.1  dholland 
     51       1.1  dholland #include <sys/param.h>
     52       1.1  dholland #include <sys/sysctl.h>
     53       1.1  dholland #include <sys/swap.h>
     54      1.30    martin #include <sys/disklabel_gpt.h>
     55       1.1  dholland #include <ufs/ufs/dinode.h>
     56       1.1  dholland #include <ufs/ffs/fs.h>
     57       1.1  dholland 
     58       1.1  dholland #include <dev/scsipi/scsipi_all.h>
     59       1.1  dholland #include <sys/scsiio.h>
     60       1.1  dholland 
     61       1.1  dholland #include <dev/ata/atareg.h>
     62       1.1  dholland #include <sys/ataio.h>
     63       1.1  dholland 
     64      1.78  jmcneill #include <sys/drvctlio.h>
     65      1.78  jmcneill 
     66       1.1  dholland #include "defs.h"
     67       1.1  dholland #include "md.h"
     68       1.1  dholland #include "msg_defs.h"
     69       1.1  dholland #include "menu_defs.h"
     70       1.1  dholland #include "txtwalk.h"
     71       1.1  dholland 
     72      1.31    martin /* #define DEBUG_VERBOSE	1 */
     73      1.30    martin 
     74       1.1  dholland /* Disk descriptions */
     75       1.1  dholland struct disk_desc {
     76       1.1  dholland 	char	dd_name[SSTRSIZE];
     77      1.29       mrg 	char	dd_descr[256];
     78      1.15    martin 	bool	dd_no_mbr, dd_no_part;
     79       1.1  dholland 	uint	dd_cyl;
     80       1.1  dholland 	uint	dd_head;
     81       1.1  dholland 	uint	dd_sec;
     82       1.1  dholland 	uint	dd_secsize;
     83      1.30    martin 	daddr_t	dd_totsec;
     84       1.2    martin };
     85       1.2    martin 
     86      1.47    martin #define	NAME_PREFIX	"NAME="
     87      1.47    martin static const char name_prefix[] = NAME_PREFIX;
     88      1.47    martin 
     89      1.47    martin /* things we could have as /sbin/newfs_* and /sbin/fsck_* */
     90      1.47    martin static const char *extern_fs_with_chk[] = {
     91      1.94   reinoud 	"ext2fs", "lfs", "msdos", "udf", "v7fs"
     92      1.47    martin };
     93      1.47    martin 
     94      1.47    martin /* things we could have as /sbin/newfs_* but not /sbin/fsck_* */
     95      1.47    martin static const char *extern_fs_newfs_only[] = {
     96      1.94   reinoud 	"sysvbfs"
     97      1.47    martin };
     98      1.45    martin 
     99       1.1  dholland /* Local prototypes */
    100      1.47    martin static int found_fs(struct data *, size_t, const struct lookfor*);
    101      1.47    martin static int found_fs_nocheck(struct data *, size_t, const struct lookfor*);
    102      1.30    martin static int fsck_preen(const char *, const char *, bool silent);
    103      1.30    martin static void fixsb(const char *, const char *);
    104       1.1  dholland 
    105       1.1  dholland 
    106       1.1  dholland static bool tmpfs_on_var_shm(void);
    107       1.1  dholland 
    108       1.1  dholland const char *
    109      1.30    martin getfslabelname(uint f, uint f_version)
    110       1.1  dholland {
    111      1.30    martin 	if (f == FS_TMPFS)
    112      1.30    martin 		return "tmpfs";
    113      1.30    martin 	else if (f == FS_MFS)
    114      1.30    martin 		return "mfs";
    115      1.82    martin 	else if (f == FS_EFI_SP)
    116      1.82    martin 		return msg_string(MSG_fs_type_efi_sp);
    117      1.93    martin 	else if (f == FS_BSDFFS) {
    118      1.91    martin 		switch (f_version) {
    119      1.91    martin 		default:
    120      1.91    martin 		case 1:	return msg_string(MSG_fs_type_ffs);
    121      1.91    martin 		case 2:	return msg_string(MSG_fs_type_ffsv2);
    122      1.91    martin 		case 3:	return msg_string(MSG_fs_type_ffsv2ea);
    123      1.91    martin 		}
    124      1.91    martin 	} else if (f == FS_EX2FS && f_version == 1)
    125      1.58    martin 		return msg_string(MSG_fs_type_ext2old);
    126      1.30    martin 	else if (f >= __arraycount(fstypenames) || fstypenames[f] == NULL)
    127       1.1  dholland 		return "invalid";
    128       1.1  dholland 	return fstypenames[f];
    129       1.1  dholland }
    130       1.1  dholland 
    131       1.1  dholland /*
    132      1.95   msaitoh  * Decide whether we want to mount a tmpfs on /var/shm: we do this always
    133       1.1  dholland  * when the machine has more than 16 MB of user memory. On smaller machines,
    134       1.1  dholland  * shm_open() and friends will not perform well anyway.
    135       1.1  dholland  */
    136       1.1  dholland static bool
    137      1.86   tsutsui tmpfs_on_var_shm(void)
    138       1.1  dholland {
    139       1.1  dholland 	uint64_t ram;
    140       1.1  dholland 	size_t len;
    141       1.1  dholland 
    142       1.1  dholland 	len = sizeof(ram);
    143       1.1  dholland 	if (sysctlbyname("hw.usermem64", &ram, &len, NULL, 0))
    144       1.1  dholland 		return false;
    145       1.1  dholland 
    146      1.28    martin 	return ram > 16 * MEG;
    147       1.1  dholland }
    148       1.1  dholland 
    149      1.87    martin /*
    150      1.90    martin  * Find length of string but ignore trailing whitespace
    151      1.87    martin  */
    152      1.90    martin static int
    153      1.90    martin trimmed_len(const char *s)
    154      1.87    martin {
    155      1.90    martin 	size_t len = strlen(s);
    156      1.87    martin 
    157      1.90    martin 	while (len > 0 && isspace((unsigned char)s[len - 1]))
    158      1.90    martin 		len--;
    159      1.90    martin 	return len;
    160      1.87    martin }
    161      1.87    martin 
    162       1.1  dholland /* from src/sbin/atactl/atactl.c
    163       1.1  dholland  * extract_string: copy a block of bytes out of ataparams and make
    164       1.1  dholland  * a proper string out of it, truncating trailing spaces and preserving
    165       1.1  dholland  * strict typing. And also, not doing unaligned accesses.
    166       1.1  dholland  */
    167       1.1  dholland static void
    168       1.1  dholland ata_extract_string(char *buf, size_t bufmax,
    169       1.1  dholland 		   uint8_t *bytes, unsigned numbytes,
    170       1.1  dholland 		   int needswap)
    171       1.1  dholland {
    172       1.1  dholland 	unsigned i;
    173       1.1  dholland 	size_t j;
    174       1.1  dholland 	unsigned char ch1, ch2;
    175       1.1  dholland 
    176       1.1  dholland 	for (i = 0, j = 0; i < numbytes; i += 2) {
    177       1.1  dholland 		ch1 = bytes[i];
    178       1.1  dholland 		ch2 = bytes[i+1];
    179       1.1  dholland 		if (needswap && j < bufmax-1) {
    180       1.1  dholland 			buf[j++] = ch2;
    181       1.1  dholland 		}
    182       1.1  dholland 		if (j < bufmax-1) {
    183       1.1  dholland 			buf[j++] = ch1;
    184       1.1  dholland 		}
    185       1.1  dholland 		if (!needswap && j < bufmax-1) {
    186       1.1  dholland 			buf[j++] = ch2;
    187       1.1  dholland 		}
    188       1.1  dholland 	}
    189       1.1  dholland 	while (j > 0 && buf[j-1] == ' ') {
    190       1.1  dholland 		j--;
    191       1.1  dholland 	}
    192       1.1  dholland 	buf[j] = '\0';
    193       1.1  dholland }
    194       1.1  dholland 
    195       1.1  dholland /*
    196       1.1  dholland  * from src/sbin/scsictl/scsi_subr.c
    197       1.1  dholland  */
    198       1.1  dholland #define STRVIS_ISWHITE(x) ((x) == ' ' || (x) == '\0' || (x) == (u_char)'\377')
    199       1.1  dholland 
    200       1.1  dholland static void
    201       1.1  dholland scsi_strvis(char *sdst, size_t dlen, const char *ssrc, size_t slen)
    202       1.1  dholland {
    203       1.1  dholland 	u_char *dst = (u_char *)sdst;
    204       1.1  dholland 	const u_char *src = (const u_char *)ssrc;
    205       1.1  dholland 
    206       1.1  dholland 	/* Trim leading and trailing blanks and NULs. */
    207       1.1  dholland 	while (slen > 0 && STRVIS_ISWHITE(src[0]))
    208       1.1  dholland 		++src, --slen;
    209       1.1  dholland 	while (slen > 0 && STRVIS_ISWHITE(src[slen - 1]))
    210       1.1  dholland 		--slen;
    211       1.1  dholland 
    212       1.1  dholland 	while (slen > 0) {
    213       1.1  dholland 		if (*src < 0x20 || *src >= 0x80) {
    214       1.1  dholland 			/* non-printable characters */
    215       1.1  dholland 			dlen -= 4;
    216       1.1  dholland 			if (dlen < 1)
    217       1.1  dholland 				break;
    218       1.1  dholland 			*dst++ = '\\';
    219       1.1  dholland 			*dst++ = ((*src & 0300) >> 6) + '0';
    220       1.1  dholland 			*dst++ = ((*src & 0070) >> 3) + '0';
    221       1.1  dholland 			*dst++ = ((*src & 0007) >> 0) + '0';
    222       1.1  dholland 		} else if (*src == '\\') {
    223       1.1  dholland 			/* quote characters */
    224       1.1  dholland 			dlen -= 2;
    225       1.1  dholland 			if (dlen < 1)
    226       1.1  dholland 				break;
    227       1.1  dholland 			*dst++ = '\\';
    228       1.1  dholland 			*dst++ = '\\';
    229       1.1  dholland 		} else {
    230       1.1  dholland 			/* normal characters */
    231       1.1  dholland 			if (--dlen < 1)
    232       1.1  dholland 				break;
    233       1.1  dholland 			*dst++ = *src;
    234       1.1  dholland 		}
    235       1.1  dholland 		++src, --slen;
    236       1.1  dholland 	}
    237       1.1  dholland 
    238       1.1  dholland 	*dst++ = 0;
    239       1.1  dholland }
    240       1.1  dholland 
    241       1.1  dholland 
    242       1.1  dholland static int
    243      1.35  christos get_descr_scsi(struct disk_desc *dd)
    244       1.1  dholland {
    245       1.1  dholland 	struct scsipi_inquiry_data inqbuf;
    246       1.1  dholland 	struct scsipi_inquiry cmd;
    247       1.1  dholland 	scsireq_t req;
    248       1.1  dholland         /* x4 in case every character is escaped, +1 for NUL. */
    249       1.1  dholland 	char vendor[(sizeof(inqbuf.vendor) * 4) + 1],
    250       1.1  dholland 	     product[(sizeof(inqbuf.product) * 4) + 1],
    251       1.1  dholland 	     revision[(sizeof(inqbuf.revision) * 4) + 1];
    252       1.1  dholland 	char size[5];
    253       1.1  dholland 
    254       1.1  dholland 	memset(&inqbuf, 0, sizeof(inqbuf));
    255       1.1  dholland 	memset(&cmd, 0, sizeof(cmd));
    256       1.1  dholland 	memset(&req, 0, sizeof(req));
    257       1.1  dholland 
    258       1.1  dholland 	cmd.opcode = INQUIRY;
    259       1.1  dholland 	cmd.length = sizeof(inqbuf);
    260       1.1  dholland 	memcpy(req.cmd, &cmd, sizeof(cmd));
    261       1.1  dholland 	req.cmdlen = sizeof(cmd);
    262       1.1  dholland 	req.databuf = &inqbuf;
    263       1.1  dholland 	req.datalen = sizeof(inqbuf);
    264       1.1  dholland 	req.timeout = 10000;
    265       1.1  dholland 	req.flags = SCCMD_READ;
    266       1.1  dholland 	req.senselen = SENSEBUFLEN;
    267       1.1  dholland 
    268      1.35  christos 	if (!disk_ioctl(dd->dd_name, SCIOCCOMMAND, &req)
    269      1.35  christos 	    || req.retsts != SCCMD_OK)
    270       1.1  dholland 		return 0;
    271       1.1  dholland 
    272       1.1  dholland 	scsi_strvis(vendor, sizeof(vendor), inqbuf.vendor,
    273       1.1  dholland 	    sizeof(inqbuf.vendor));
    274       1.1  dholland 	scsi_strvis(product, sizeof(product), inqbuf.product,
    275       1.1  dholland 	    sizeof(inqbuf.product));
    276       1.1  dholland 	scsi_strvis(revision, sizeof(revision), inqbuf.revision,
    277       1.1  dholland 	    sizeof(inqbuf.revision));
    278       1.1  dholland 
    279       1.1  dholland 	humanize_number(size, sizeof(size),
    280       1.1  dholland 	    (uint64_t)dd->dd_secsize * (uint64_t)dd->dd_totsec,
    281       1.1  dholland 	    "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
    282       1.1  dholland 
    283       1.1  dholland 	snprintf(dd->dd_descr, sizeof(dd->dd_descr),
    284       1.1  dholland 	    "%s (%s, %s %s)",
    285       1.1  dholland 	    dd->dd_name, size, vendor, product);
    286       1.1  dholland 
    287       1.1  dholland 	return 1;
    288       1.1  dholland }
    289       1.1  dholland 
    290       1.1  dholland static int
    291      1.35  christos get_descr_ata(struct disk_desc *dd)
    292       1.1  dholland {
    293       1.1  dholland 	struct atareq req;
    294       1.1  dholland 	static union {
    295       1.1  dholland 		unsigned char inbuf[DEV_BSIZE];
    296       1.1  dholland 		struct ataparams inqbuf;
    297       1.1  dholland 	} inbuf;
    298       1.1  dholland 	struct ataparams *inqbuf = &inbuf.inqbuf;
    299       1.1  dholland 	char model[sizeof(inqbuf->atap_model)+1];
    300       1.1  dholland 	char size[5];
    301      1.35  christos 	int needswap = 0;
    302       1.1  dholland 
    303       1.1  dholland 	memset(&inbuf, 0, sizeof(inbuf));
    304       1.1  dholland 	memset(&req, 0, sizeof(req));
    305       1.1  dholland 
    306       1.1  dholland 	req.flags = ATACMD_READ;
    307       1.1  dholland 	req.command = WDCC_IDENTIFY;
    308       1.1  dholland 	req.databuf = (void *)&inbuf;
    309       1.1  dholland 	req.datalen = sizeof(inbuf);
    310       1.1  dholland 	req.timeout = 1000;
    311       1.1  dholland 
    312      1.35  christos 	if (!disk_ioctl(dd->dd_name, ATAIOCCOMMAND, &req)
    313      1.35  christos 	    || req.retsts != ATACMD_OK)
    314       1.1  dholland 		return 0;
    315       1.1  dholland 
    316       1.1  dholland #if BYTE_ORDER == LITTLE_ENDIAN
    317       1.1  dholland 	/*
    318       1.1  dholland 	 * On little endian machines, we need to shuffle the string
    319       1.1  dholland 	 * byte order.  However, we don't have to do this for NEC or
    320       1.1  dholland 	 * Mitsumi ATAPI devices
    321       1.1  dholland 	 */
    322       1.1  dholland 
    323       1.1  dholland 	if (!(inqbuf->atap_config != WDC_CFG_CFA_MAGIC &&
    324       1.1  dholland 	      (inqbuf->atap_config & WDC_CFG_ATAPI) &&
    325       1.1  dholland 	      ((inqbuf->atap_model[0] == 'N' &&
    326      1.10     isaki 	        inqbuf->atap_model[1] == 'E') ||
    327       1.1  dholland 	       (inqbuf->atap_model[0] == 'F' &&
    328      1.10     isaki 	        inqbuf->atap_model[1] == 'X')))) {
    329       1.1  dholland 		needswap = 1;
    330       1.1  dholland 	}
    331       1.1  dholland #endif
    332       1.1  dholland 
    333       1.1  dholland 	ata_extract_string(model, sizeof(model),
    334       1.1  dholland 	    inqbuf->atap_model, sizeof(inqbuf->atap_model), needswap);
    335       1.1  dholland 	humanize_number(size, sizeof(size),
    336       1.1  dholland 	    (uint64_t)dd->dd_secsize * (uint64_t)dd->dd_totsec,
    337       1.1  dholland 	    "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
    338       1.1  dholland 
    339       1.1  dholland 	snprintf(dd->dd_descr, sizeof(dd->dd_descr), "%s (%s, %s)",
    340       1.1  dholland 	    dd->dd_name, size, model);
    341       1.1  dholland 
    342       1.1  dholland 	return 1;
    343       1.1  dholland }
    344       1.1  dholland 
    345      1.78  jmcneill static int
    346      1.78  jmcneill get_descr_drvctl(struct disk_desc *dd)
    347      1.78  jmcneill {
    348      1.78  jmcneill 	prop_dictionary_t command_dict;
    349      1.78  jmcneill 	prop_dictionary_t args_dict;
    350      1.78  jmcneill 	prop_dictionary_t results_dict;
    351      1.78  jmcneill 	prop_dictionary_t props;
    352      1.78  jmcneill 	int8_t perr;
    353      1.78  jmcneill 	int error, fd;
    354      1.78  jmcneill 	bool rv;
    355      1.90    martin 	char size[5];
    356      1.78  jmcneill 	const char *model;
    357      1.78  jmcneill 
    358      1.78  jmcneill 	fd = open("/dev/drvctl", O_RDONLY);
    359      1.78  jmcneill 	if (fd == -1)
    360      1.78  jmcneill 		return 0;
    361      1.78  jmcneill 
    362      1.78  jmcneill 	command_dict = prop_dictionary_create();
    363      1.78  jmcneill 	args_dict = prop_dictionary_create();
    364      1.78  jmcneill 
    365      1.79  jmcneill 	prop_dictionary_set_string_nocopy(command_dict, "drvctl-command",
    366      1.78  jmcneill 	    "get-properties");
    367      1.79  jmcneill 	prop_dictionary_set_string_nocopy(args_dict, "device-name",
    368      1.78  jmcneill 	    dd->dd_name);
    369      1.78  jmcneill 	prop_dictionary_set(command_dict, "drvctl-arguments", args_dict);
    370      1.78  jmcneill 	prop_object_release(args_dict);
    371      1.78  jmcneill 
    372      1.78  jmcneill 	error = prop_dictionary_sendrecv_ioctl(command_dict, fd,
    373      1.78  jmcneill 	    DRVCTLCOMMAND, &results_dict);
    374      1.78  jmcneill 	prop_object_release(command_dict);
    375      1.78  jmcneill 	close(fd);
    376      1.78  jmcneill 	if (error)
    377      1.78  jmcneill 		return 0;
    378      1.78  jmcneill 
    379      1.78  jmcneill 	rv = prop_dictionary_get_int8(results_dict, "drvctl-error", &perr);
    380      1.78  jmcneill 	if (rv == false || perr != 0) {
    381      1.78  jmcneill 		prop_object_release(results_dict);
    382      1.78  jmcneill 		return 0;
    383      1.78  jmcneill 	}
    384      1.78  jmcneill 
    385      1.78  jmcneill 	props = prop_dictionary_get(results_dict,
    386      1.78  jmcneill 	    "drvctl-result-data");
    387      1.78  jmcneill 	if (props == NULL) {
    388      1.78  jmcneill 		prop_object_release(results_dict);
    389      1.78  jmcneill 		return 0;
    390      1.78  jmcneill 	}
    391      1.78  jmcneill 	props = prop_dictionary_get(props, "disk-info");
    392      1.78  jmcneill 	if (props == NULL ||
    393      1.78  jmcneill 	    !prop_dictionary_get_string(props, "type", &model)) {
    394      1.78  jmcneill 		prop_object_release(results_dict);
    395      1.78  jmcneill 		return 0;
    396      1.78  jmcneill 	}
    397      1.78  jmcneill 
    398      1.78  jmcneill 	humanize_number(size, sizeof(size),
    399      1.78  jmcneill 	    (uint64_t)dd->dd_secsize * (uint64_t)dd->dd_totsec,
    400      1.78  jmcneill 	    "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
    401      1.78  jmcneill 
    402      1.90    martin 	snprintf(dd->dd_descr, sizeof(dd->dd_descr), "%s (%s, %.*s)",
    403      1.90    martin 	    dd->dd_name, size, trimmed_len(model), model);
    404      1.78  jmcneill 
    405      1.78  jmcneill 	prop_object_release(results_dict);
    406      1.78  jmcneill 
    407      1.78  jmcneill 	return 1;
    408      1.78  jmcneill }
    409      1.78  jmcneill 
    410       1.1  dholland static void
    411       1.1  dholland get_descr(struct disk_desc *dd)
    412       1.1  dholland {
    413      1.35  christos 	char size[5];
    414       1.1  dholland 	dd->dd_descr[0] = '\0';
    415       1.1  dholland 
    416      1.78  jmcneill 	/* try drvctl first, fallback to direct probing */
    417      1.78  jmcneill 	if (get_descr_drvctl(dd))
    418      1.78  jmcneill 		return;
    419       1.1  dholland 	/* try ATA */
    420      1.35  christos 	if (get_descr_ata(dd))
    421      1.77  jmcneill 		return;
    422       1.1  dholland 	/* try SCSI */
    423      1.35  christos 	if (get_descr_scsi(dd))
    424      1.77  jmcneill 		return;
    425      1.30    martin 
    426       1.2    martin 	/* XXX: get description from raid, cgd, vnd... */
    427      1.77  jmcneill 
    428      1.30    martin 	/* punt, just give some generic info */
    429      1.30    martin 	humanize_number(size, sizeof(size),
    430      1.30    martin 	    (uint64_t)dd->dd_secsize * (uint64_t)dd->dd_totsec,
    431      1.30    martin 	    "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
    432      1.30    martin 
    433      1.30    martin 	snprintf(dd->dd_descr, sizeof(dd->dd_descr),
    434      1.30    martin 	    "%s (%s)", dd->dd_name, size);
    435       1.1  dholland }
    436       1.1  dholland 
    437      1.21    martin /*
    438      1.21    martin  * State for helper callback for get_default_cdrom
    439      1.21    martin  */
    440      1.21    martin struct default_cdrom_data {
    441      1.21    martin 	char *device;
    442      1.21    martin 	size_t max_len;
    443      1.21    martin 	bool found;
    444      1.21    martin };
    445      1.21    martin 
    446      1.21    martin /*
    447      1.21    martin  * Helper function for get_default_cdrom, gets passed a device
    448      1.21    martin  * name and a void pointer to default_cdrom_data.
    449      1.21    martin  */
    450      1.21    martin static bool
    451      1.21    martin get_default_cdrom_helper(void *state, const char *dev)
    452      1.21    martin {
    453      1.21    martin 	struct default_cdrom_data *data = state;
    454      1.21    martin 
    455      1.25    martin 	if (!is_cdrom_device(dev, false))
    456      1.22    martin 		return true;
    457      1.22    martin 
    458      1.21    martin 	strlcpy(data->device, dev, data->max_len);
    459      1.22    martin 	strlcat(data->device, "a", data->max_len); /* default to partition a */
    460      1.21    martin 	data->found = true;
    461      1.21    martin 
    462      1.21    martin 	return false;	/* one is enough, stop iteration */
    463      1.21    martin }
    464      1.21    martin 
    465      1.21    martin /*
    466      1.21    martin  * Set the argument to the name of the first CD devices actually
    467      1.21    martin  * available, leave it unmodified otherwise.
    468      1.21    martin  * Return true if a device has been found.
    469       1.1  dholland  */
    470      1.18    martin bool
    471      1.18    martin get_default_cdrom(char *cd, size_t max_len)
    472       1.1  dholland {
    473      1.21    martin 	struct default_cdrom_data state;
    474      1.21    martin 
    475      1.21    martin 	state.device = cd;
    476      1.21    martin 	state.max_len = max_len;
    477      1.21    martin 	state.found = false;
    478      1.21    martin 
    479      1.21    martin 	if (enumerate_disks(&state, get_default_cdrom_helper))
    480      1.21    martin 		return state.found;
    481      1.21    martin 
    482      1.21    martin 	return false;
    483       1.1  dholland }
    484       1.1  dholland 
    485      1.30    martin static bool
    486      1.15    martin get_wedge_descr(struct disk_desc *dd)
    487      1.15    martin {
    488      1.15    martin 	struct dkwedge_info dkw;
    489      1.15    martin 
    490      1.35  christos 	if (!get_wedge_info(dd->dd_name, &dkw))
    491      1.30    martin 		return false;
    492      1.15    martin 
    493      1.35  christos 	snprintf(dd->dd_descr, sizeof(dd->dd_descr), "%s (%s@%s)",
    494      1.35  christos 	    dkw.dkw_wname, dkw.dkw_devname, dkw.dkw_parent);
    495      1.35  christos 	return true;
    496      1.15    martin }
    497      1.15    martin 
    498      1.15    martin static bool
    499      1.15    martin get_name_and_parent(const char *dev, char *name, char *parent)
    500      1.15    martin {
    501      1.15    martin 	struct dkwedge_info dkw;
    502      1.15    martin 
    503      1.35  christos 	if (!get_wedge_info(dev, &dkw))
    504      1.15    martin 		return false;
    505      1.35  christos 	strcpy(name, (const char *)dkw.dkw_wname);
    506      1.35  christos 	strcpy(parent, dkw.dkw_parent);
    507      1.35  christos 	return true;
    508      1.15    martin }
    509      1.15    martin 
    510      1.15    martin static bool
    511      1.15    martin find_swap_part_on(const char *dev, char *swap_name)
    512      1.15    martin {
    513      1.35  christos 	struct dkwedge_list dkwl;
    514      1.15    martin 	struct dkwedge_info *dkw;
    515      1.15    martin 	u_int i;
    516      1.15    martin 	bool res = false;
    517      1.15    martin 
    518      1.35  christos 	if (!get_wedge_list(dev, &dkwl))
    519      1.15    martin 		return false;
    520      1.15    martin 
    521      1.35  christos 	dkw = dkwl.dkwl_buf;
    522      1.15    martin 	for (i = 0; i < dkwl.dkwl_nwedges; i++) {
    523      1.15    martin 		res = strcmp(dkw[i].dkw_ptype, DKW_PTYPE_SWAP) == 0;
    524      1.15    martin 		if (res) {
    525      1.15    martin 			strcpy(swap_name, (const char*)dkw[i].dkw_wname);
    526      1.15    martin 			break;
    527      1.15    martin 		}
    528      1.15    martin 	}
    529      1.35  christos 	free(dkwl.dkwl_buf);
    530      1.15    martin 
    531      1.15    martin 	return res;
    532      1.15    martin }
    533      1.15    martin 
    534      1.15    martin static bool
    535      1.15    martin is_ffs_wedge(const char *dev)
    536      1.15    martin {
    537      1.15    martin 	struct dkwedge_info dkw;
    538      1.15    martin 
    539      1.35  christos 	if (!get_wedge_info(dev, &dkw))
    540      1.15    martin 		return false;
    541      1.15    martin 
    542      1.35  christos 	return strcmp(dkw.dkw_ptype, DKW_PTYPE_FFS) == 0;
    543      1.15    martin }
    544      1.15    martin 
    545      1.18    martin /*
    546      1.18    martin  * Does this device match an entry in our default CDROM device list?
    547      1.25    martin  * If looking for install targets, we also flag floopy devices.
    548      1.18    martin  */
    549      1.22    martin bool
    550      1.25    martin is_cdrom_device(const char *dev, bool as_target)
    551      1.18    martin {
    552      1.25    martin 	static const char *target_devices[] = {
    553      1.24    martin #ifdef CD_NAMES
    554      1.24    martin 		CD_NAMES
    555      1.24    martin #endif
    556      1.24    martin #if defined(CD_NAMES) && defined(FLOPPY_NAMES)
    557      1.24    martin 		,
    558      1.24    martin #endif
    559      1.24    martin #ifdef FLOPPY_NAMES
    560      1.24    martin 		FLOPPY_NAMES
    561      1.24    martin #endif
    562      1.24    martin #if defined(CD_NAMES) || defined(FLOPPY_NAMES)
    563      1.24    martin 		,
    564      1.24    martin #endif
    565      1.24    martin 		0
    566      1.24    martin 	};
    567      1.25    martin 	static const char *src_devices[] = {
    568      1.25    martin #ifdef CD_NAMES
    569      1.25    martin 		CD_NAMES ,
    570      1.25    martin #endif
    571      1.25    martin 		0
    572      1.25    martin 	};
    573      1.18    martin 
    574      1.25    martin 	for (const char **dev_pat = as_target ? target_devices : src_devices;
    575      1.25    martin 	     *dev_pat; dev_pat++)
    576      1.20    martin 		if (fnmatch(*dev_pat, dev, 0) == 0)
    577      1.20    martin 			return true;
    578      1.18    martin 
    579      1.18    martin 	return false;
    580      1.18    martin }
    581      1.18    martin 
    582      1.27    martin /* does this device match any entry in the driver list? */
    583      1.27    martin static bool
    584      1.27    martin dev_in_list(const char *dev, const char **list)
    585      1.27    martin {
    586      1.27    martin 
    587      1.27    martin 	for ( ; *list; list++) {
    588      1.27    martin 
    589      1.27    martin 		size_t len = strlen(*list);
    590      1.27    martin 
    591      1.27    martin 		/* start of name matches? */
    592      1.27    martin 		if (strncmp(dev, *list, len) == 0) {
    593      1.27    martin 			char *endp;
    594      1.27    martin 			int e;
    595      1.27    martin 
    596      1.27    martin 			/* remainder of name is a decimal number? */
    597      1.27    martin 			strtou(dev+len, &endp, 10, 0, INT_MAX, &e);
    598      1.27    martin 			if (endp && *endp == 0 && e == 0)
    599      1.27    martin 				return true;
    600      1.27    martin 		}
    601      1.27    martin 	}
    602      1.27    martin 
    603      1.27    martin 	return false;
    604      1.27    martin }
    605      1.27    martin 
    606      1.27    martin bool
    607      1.27    martin is_bootable_device(const char *dev)
    608      1.27    martin {
    609      1.27    martin 	static const char *non_bootable_devs[] = {
    610      1.27    martin 		"raid",	/* bootcode lives outside of raid */
    611      1.27    martin 		"xbd",	/* xen virtual device, can not boot from that */
    612      1.27    martin 		NULL
    613      1.27    martin 	};
    614      1.27    martin 
    615      1.27    martin 	return !dev_in_list(dev, non_bootable_devs);
    616      1.27    martin }
    617      1.27    martin 
    618      1.27    martin bool
    619      1.27    martin is_partitionable_device(const char *dev)
    620      1.27    martin {
    621      1.27    martin 	static const char *non_partitionable_devs[] = {
    622      1.42   msaitoh 		"dk",	/* this is already a partitioned slice */
    623      1.27    martin 		NULL
    624      1.27    martin 	};
    625      1.27    martin 
    626      1.27    martin 	return !dev_in_list(dev, non_partitionable_devs);
    627      1.27    martin }
    628      1.27    martin 
    629      1.18    martin /*
    630      1.18    martin  * Multi-purpose helper function:
    631      1.21    martin  * iterate all known disks, invoke a callback for each.
    632      1.21    martin  * Stop iteration when the callback returns false.
    633      1.76    andvar  * Return true when iteration actually happened, false on error.
    634      1.18    martin  */
    635      1.22    martin bool
    636      1.21    martin enumerate_disks(void *state, bool (*func)(void *state, const char *dev))
    637       1.1  dholland {
    638      1.17    martin 	static const int mib[] = { CTL_HW, HW_DISKNAMES };
    639      1.17    martin 	static const unsigned int miblen = __arraycount(mib);
    640      1.17    martin 	const char *xd;
    641      1.21    martin 	char *disk_names;
    642      1.17    martin 	size_t len;
    643       1.1  dholland 
    644      1.21    martin 	if (sysctl(mib, miblen, NULL, &len, NULL, 0) == -1)
    645      1.21    martin 		return false;
    646       1.1  dholland 
    647      1.17    martin 	disk_names = malloc(len);
    648      1.17    martin 	if (disk_names == NULL)
    649      1.21    martin 		return false;
    650      1.17    martin 
    651      1.17    martin 	if (sysctl(mib, miblen, disk_names, &len, NULL, 0) == -1) {
    652      1.17    martin 		free(disk_names);
    653      1.21    martin 		return false;
    654      1.17    martin 	}
    655       1.1  dholland 
    656      1.17    martin 	for (xd = strtok(disk_names, " "); xd != NULL; xd = strtok(NULL, " ")) {
    657      1.21    martin 		if (!(*func)(state, xd))
    658      1.21    martin 			break;
    659      1.21    martin 	}
    660      1.21    martin 	free(disk_names);
    661      1.21    martin 
    662      1.21    martin 	return true;
    663      1.21    martin }
    664      1.21    martin 
    665      1.21    martin /*
    666      1.21    martin  * Helper state for get_disks
    667      1.21    martin  */
    668      1.21    martin struct get_disks_state {
    669      1.21    martin 	int numdisks;
    670      1.21    martin 	struct disk_desc *dd;
    671      1.21    martin 	bool with_non_partitionable;
    672      1.21    martin };
    673      1.21    martin 
    674      1.21    martin /*
    675      1.21    martin  * Helper function for get_disks enumartion
    676      1.21    martin  */
    677      1.21    martin static bool
    678      1.21    martin get_disks_helper(void *arg, const char *dev)
    679      1.21    martin {
    680      1.21    martin 	struct get_disks_state *state = arg;
    681      1.30    martin 	struct disk_geom geo;
    682      1.18    martin 
    683      1.21    martin 	/* is this a CD device? */
    684      1.25    martin 	if (is_cdrom_device(dev, true))
    685      1.21    martin 		return true;
    686      1.21    martin 
    687      1.27    martin 	memset(state->dd, 0, sizeof(*state->dd));
    688      1.21    martin 	strlcpy(state->dd->dd_name, dev, sizeof state->dd->dd_name - 2);
    689      1.27    martin 	state->dd->dd_no_mbr = !is_bootable_device(dev);
    690      1.27    martin 	state->dd->dd_no_part = !is_partitionable_device(dev);
    691      1.21    martin 
    692      1.21    martin 	if (state->dd->dd_no_part && !state->with_non_partitionable)
    693      1.21    martin 		return true;
    694       1.1  dholland 
    695      1.30    martin 	if (!get_disk_geom(state->dd->dd_name, &geo)) {
    696      1.21    martin 		if (errno == ENOENT)
    697      1.21    martin 			return true;
    698      1.21    martin 		if (errno != ENOTTY || !state->dd->dd_no_part)
    699      1.21    martin 			/*
    700      1.21    martin 			 * Allow plain partitions,
    701      1.21    martin 			 * like already existing wedges
    702      1.21    martin 			 * (like dk0) if marked as
    703      1.21    martin 			 * non-partitioning device.
    704      1.21    martin 			 * For all other cases, continue
    705      1.21    martin 			 * with the next disk.
    706      1.21    martin 			 */
    707      1.21    martin 			return true;
    708      1.21    martin 		if (!is_ffs_wedge(state->dd->dd_name))
    709      1.21    martin 			return true;
    710      1.21    martin 	}
    711       1.1  dholland 
    712      1.21    martin 	/*
    713      1.21    martin 	 * Exclude a disk mounted as root partition,
    714      1.21    martin 	 * in case of install-image on a USB memstick.
    715      1.21    martin 	 */
    716      1.23    martin 	if (is_active_rootpart(state->dd->dd_name,
    717      1.23    martin 	    state->dd->dd_no_part ? -1 : 0))
    718      1.21    martin 		return true;
    719      1.17    martin 
    720      1.30    martin 	state->dd->dd_cyl = geo.dg_ncylinders;
    721      1.30    martin 	state->dd->dd_head = geo.dg_ntracks;
    722      1.30    martin 	state->dd->dd_sec = geo.dg_nsectors;
    723      1.30    martin 	state->dd->dd_secsize = geo.dg_secsize;
    724      1.30    martin 	state->dd->dd_totsec = geo.dg_secperunit;
    725      1.30    martin 
    726      1.30    martin 	if (!state->dd->dd_no_part || !get_wedge_descr(state->dd))
    727      1.21    martin 		get_descr(state->dd);
    728      1.21    martin 	state->dd++;
    729      1.21    martin 	state->numdisks++;
    730      1.21    martin 	if (state->numdisks == MAX_DISKS)
    731      1.21    martin 		return false;
    732      1.21    martin 
    733      1.21    martin 	return true;
    734      1.21    martin }
    735      1.21    martin 
    736      1.21    martin /*
    737      1.21    martin  * Get all disk devices that are not CDs.
    738      1.21    martin  * Optionally leave out those that can not be partitioned further.
    739      1.21    martin  */
    740      1.21    martin static int
    741      1.21    martin get_disks(struct disk_desc *dd, bool with_non_partitionable)
    742      1.21    martin {
    743      1.21    martin 	struct get_disks_state state;
    744      1.21    martin 
    745      1.21    martin 	/* initialize */
    746      1.21    martin 	state.numdisks = 0;
    747      1.21    martin 	state.dd = dd;
    748      1.21    martin 	state.with_non_partitionable = with_non_partitionable;
    749      1.21    martin 
    750      1.21    martin 	if (enumerate_disks(&state, get_disks_helper))
    751      1.21    martin 		return state.numdisks;
    752      1.21    martin 
    753      1.21    martin 	return 0;
    754       1.1  dholland }
    755       1.1  dholland 
    756      1.30    martin #ifdef DEBUG_VERBOSE
    757      1.30    martin static void
    758      1.30    martin dump_parts(const struct disk_partitions *parts)
    759      1.30    martin {
    760      1.30    martin 	fprintf(stderr, "%s partitions on %s:\n",
    761      1.30    martin 	    MSG_XLAT(parts->pscheme->short_name), parts->disk);
    762      1.30    martin 
    763      1.30    martin 	for (size_t p = 0; p < parts->num_part; p++) {
    764      1.30    martin 		struct disk_part_info info;
    765      1.30    martin 
    766      1.30    martin 		if (parts->pscheme->get_part_info(
    767      1.30    martin 		    parts, p, &info)) {
    768      1.30    martin 			fprintf(stderr, " #%zu: start: %" PRIu64 " "
    769      1.30    martin 			    "size: %" PRIu64 ", flags: %x\n",
    770      1.30    martin 			    p, info.start, info.size,
    771      1.30    martin 			    info.flags);
    772      1.30    martin 			if (info.nat_type)
    773      1.30    martin 				fprintf(stderr, "\ttype: %s\n",
    774      1.30    martin 				    info.nat_type->description);
    775      1.30    martin 		} else {
    776      1.30    martin 			fprintf(stderr, "failed to get info "
    777      1.30    martin 			    "for partition #%zu\n", p);
    778      1.30    martin 		}
    779      1.30    martin 	}
    780      1.72    rillig 	fprintf(stderr, "%" PRIu64 " sectors free, disk size %" PRIu64
    781      1.30    martin 	    " sectors, %zu partitions used\n", parts->free_space,
    782      1.30    martin 	    parts->disk_size, parts->num_part);
    783      1.30    martin }
    784      1.30    martin #endif
    785      1.30    martin 
    786      1.30    martin static bool
    787      1.30    martin delete_scheme(struct pm_devs *p)
    788      1.30    martin {
    789      1.30    martin 
    790      1.30    martin 	if (!ask_noyes(MSG_removepartswarn))
    791      1.30    martin 		return false;
    792      1.30    martin 
    793      1.30    martin 	p->parts->pscheme->free(p->parts);
    794      1.30    martin 	p->parts = NULL;
    795      1.30    martin 	return true;
    796      1.30    martin }
    797      1.30    martin 
    798      1.30    martin 
    799      1.83    martin static bool
    800      1.72    rillig convert_copy(struct disk_partitions *old_parts,
    801      1.30    martin     struct disk_partitions *new_parts)
    802      1.30    martin {
    803      1.30    martin 	struct disk_part_info oinfo, ninfo;
    804      1.30    martin 	part_id i;
    805      1.83    martin 	bool err = false;
    806      1.30    martin 
    807      1.30    martin 	for (i = 0; i < old_parts->num_part; i++) {
    808      1.30    martin 		if (!old_parts->pscheme->get_part_info(old_parts, i, &oinfo))
    809      1.30    martin 			continue;
    810      1.30    martin 
    811      1.30    martin 		if (oinfo.flags & PTI_PSCHEME_INTERNAL)
    812      1.30    martin 			continue;
    813      1.30    martin 
    814      1.30    martin 		if (oinfo.flags & PTI_SEC_CONTAINER) {
    815      1.30    martin 		    	if (old_parts->pscheme->secondary_partitions) {
    816      1.30    martin 				struct disk_partitions *sec_part =
    817      1.30    martin 					old_parts->pscheme->
    818      1.30    martin 					    secondary_partitions(
    819      1.32    martin 					    old_parts, oinfo.start, false);
    820      1.83    martin 				if (sec_part && !convert_copy(sec_part,
    821      1.83    martin 				    new_parts))
    822      1.83    martin 					err = true;
    823      1.30    martin 			}
    824      1.30    martin 			continue;
    825      1.30    martin 		}
    826      1.30    martin 
    827      1.30    martin 		if (!new_parts->pscheme->adapt_foreign_part_info(new_parts,
    828      1.83    martin 			    &ninfo, old_parts->pscheme, &oinfo)) {
    829      1.83    martin 			err = true;
    830      1.30    martin 			continue;
    831      1.83    martin 		}
    832      1.83    martin 		if (!new_parts->pscheme->add_partition(new_parts, &ninfo,
    833      1.83    martin 		    NULL))
    834      1.83    martin 			err = true;
    835      1.30    martin 	}
    836      1.83    martin 	return !err;
    837      1.30    martin }
    838      1.30    martin 
    839      1.30    martin bool
    840      1.30    martin convert_scheme(struct pm_devs *p, bool is_boot_drive, const char **err_msg)
    841      1.30    martin {
    842      1.30    martin 	struct disk_partitions *old_parts, *new_parts;
    843      1.30    martin 	const struct disk_partitioning_scheme *new_scheme;
    844      1.30    martin 
    845      1.30    martin 	*err_msg = NULL;
    846      1.30    martin 
    847      1.30    martin 	old_parts = p->parts;
    848      1.30    martin 	new_scheme = select_part_scheme(p, old_parts->pscheme,
    849      1.30    martin 	    false, MSG_select_other_partscheme);
    850      1.30    martin 
    851      1.73    martin 	if (new_scheme == NULL) {
    852      1.73    martin 		if (err_msg)
    853      1.73    martin 			*err_msg = INTERNAL_ERROR;
    854      1.30    martin 		return false;
    855      1.73    martin 	}
    856      1.30    martin 
    857      1.30    martin 	new_parts = new_scheme->create_new_for_disk(p->diskdev,
    858      1.62    martin 	    0, p->dlsize, is_boot_drive, NULL);
    859      1.73    martin 	if (new_parts == NULL) {
    860      1.73    martin 		if (err_msg)
    861      1.73    martin 			*err_msg = MSG_out_of_memory;
    862      1.30    martin 		return false;
    863      1.73    martin 	}
    864      1.30    martin 
    865      1.83    martin 	if (!convert_copy(old_parts, new_parts)) {
    866      1.30    martin 		/* need to cleanup */
    867      1.83    martin 		if (err_msg)
    868      1.83    martin 			*err_msg = MSG_cvtscheme_error;
    869      1.30    martin 		new_parts->pscheme->free(new_parts);
    870      1.30    martin 		return false;
    871      1.30    martin 	}
    872      1.30    martin 
    873      1.30    martin 	old_parts->pscheme->free(old_parts);
    874      1.30    martin 	p->parts = new_parts;
    875      1.30    martin 	return true;
    876      1.30    martin }
    877      1.30    martin 
    878      1.40    martin static struct pm_devs *
    879      1.40    martin dummy_whole_system_pm(void)
    880      1.40    martin {
    881      1.40    martin 	static struct pm_devs whole_system = {
    882      1.40    martin 		.diskdev = "/",
    883      1.40    martin 		.no_mbr = true,
    884      1.40    martin 		.no_part = true,
    885      1.40    martin 		.cur_system = true,
    886      1.40    martin 	};
    887      1.40    martin 	static bool init = false;
    888      1.40    martin 
    889      1.40    martin 	if (!init) {
    890      1.40    martin 		strlcpy(whole_system.diskdev_descr,
    891      1.40    martin 		    msg_string(MSG_running_system),
    892      1.40    martin 		    sizeof whole_system.diskdev_descr);
    893      1.40    martin 	}
    894      1.40    martin 
    895      1.40    martin 	return &whole_system;
    896      1.40    martin }
    897      1.40    martin 
    898       1.1  dholland int
    899      1.40    martin find_disks(const char *doingwhat, bool allow_cur_system)
    900       1.1  dholland {
    901       1.1  dholland 	struct disk_desc disks[MAX_DISKS];
    902      1.40    martin 	/* need two more menu entries: current system + extended partitioning */
    903      1.80    martin 	menu_ent dsk_menu[__arraycount(disks) + 2],
    904      1.80    martin 	    wedge_menu[__arraycount(dsk_menu)];
    905      1.80    martin 	int disk_no[__arraycount(dsk_menu)], wedge_no[__arraycount(dsk_menu)];
    906       1.1  dholland 	struct disk_desc *disk;
    907      1.80    martin 	int i = 0, dno, wno, skipped = 0;
    908      1.15    martin 	int already_found, numdisks, selected_disk = -1;
    909      1.80    martin 	int menu_no, w_menu_no;
    910      1.81    martin 	size_t max_desc_len;
    911      1.30    martin 	struct pm_devs *pm_i, *pm_last = NULL;
    912      1.80    martin 	bool any_wedges = false;
    913      1.30    martin 
    914      1.30    martin 	memset(dsk_menu, 0, sizeof(dsk_menu));
    915      1.80    martin 	memset(wedge_menu, 0, sizeof(wedge_menu));
    916       1.1  dholland 
    917       1.1  dholland 	/* Find disks. */
    918      1.21    martin 	numdisks = get_disks(disks, partman_go <= 0);
    919       1.1  dholland 
    920       1.1  dholland 	/* need a redraw here, kernel messages hose everything */
    921       1.1  dholland 	touchwin(stdscr);
    922       1.1  dholland 	refresh();
    923       1.1  dholland 	/* Kill typeahead, it won't be what the user had in mind */
    924       1.1  dholland 	fpurge(stdin);
    925      1.81    martin 	/*
    926      1.81    martin 	 * we need space for the menu box and the row label,
    927      1.81    martin 	 * this sums up to 7 characters.
    928      1.81    martin 	 */
    929      1.81    martin 	max_desc_len = getmaxx(stdscr) - 8;
    930      1.81    martin 	if (max_desc_len >= __arraycount(disks[0].dd_descr))
    931      1.81    martin 		max_desc_len = __arraycount(disks[0].dd_descr) - 1;
    932       1.1  dholland 
    933      1.10     isaki 	/*
    934      1.10     isaki 	 * partman_go: <0 - we want to see menu with extended partitioning
    935      1.10     isaki 	 *            ==0 - we want to see simple select disk menu
    936      1.10     isaki 	 *             >0 - we do not want to see any menus, just detect
    937      1.10     isaki 	 *                  all disks
    938      1.10     isaki 	 */
    939       1.2    martin 	if (partman_go <= 0) {
    940      1.40    martin 		if (numdisks == 0 && !allow_cur_system) {
    941       1.2    martin 			/* No disks found! */
    942      1.30    martin 			hit_enter_to_continue(MSG_nodisk, NULL);
    943       1.2    martin 			/*endwin();*/
    944       1.2    martin 			return -1;
    945       1.2    martin 		} else {
    946      1.40    martin 			/* One or more disks found or current system allowed */
    947      1.80    martin 			dno = wno = 0;
    948      1.40    martin 			if (allow_cur_system) {
    949      1.80    martin 				dsk_menu[dno].opt_name = MSG_running_system;
    950      1.80    martin 				dsk_menu[dno].opt_flags = OPT_EXIT;
    951      1.80    martin 				dsk_menu[dno].opt_action = set_menu_select;
    952      1.80    martin 				disk_no[dno] = -1;
    953      1.80    martin 				i++; dno++;
    954      1.40    martin 			}
    955      1.80    martin 			for (i = 0; i < numdisks; i++) {
    956      1.80    martin 				if (disks[i].dd_no_part) {
    957      1.80    martin 					any_wedges = true;
    958      1.80    martin 					wedge_menu[wno].opt_name =
    959      1.80    martin 					    disks[i].dd_descr;
    960      1.81    martin 					disks[i].dd_descr[max_desc_len] = 0;
    961      1.80    martin 					wedge_menu[wno].opt_flags = OPT_EXIT;
    962      1.80    martin 					wedge_menu[wno].opt_action =
    963      1.80    martin 					    set_menu_select;
    964      1.80    martin 					wedge_no[wno] = i;
    965      1.80    martin 					wno++;
    966      1.80    martin 				} else {
    967      1.80    martin 					dsk_menu[dno].opt_name =
    968      1.80    martin 					    disks[i].dd_descr;
    969      1.81    martin 					disks[i].dd_descr[max_desc_len] = 0;
    970      1.80    martin 					dsk_menu[dno].opt_flags = OPT_EXIT;
    971      1.80    martin 					dsk_menu[dno].opt_action =
    972      1.80    martin 					    set_menu_select;
    973      1.80    martin 					disk_no[dno] = i;
    974      1.80    martin 					dno++;
    975      1.80    martin 				}
    976      1.80    martin 			}
    977      1.80    martin 			if (any_wedges) {
    978      1.80    martin 				dsk_menu[dno].opt_name = MSG_selectwedge;
    979      1.80    martin 				dsk_menu[dno].opt_flags = OPT_EXIT;
    980      1.80    martin 				dsk_menu[dno].opt_action = set_menu_select;
    981      1.80    martin 				disk_no[dno] = -2;
    982      1.80    martin 				dno++;
    983       1.2    martin 			}
    984       1.2    martin 			if (partman_go < 0) {
    985      1.80    martin 				dsk_menu[dno].opt_name = MSG_partman;
    986      1.80    martin 				dsk_menu[dno].opt_flags = OPT_EXIT;
    987      1.80    martin 				dsk_menu[dno].opt_action = set_menu_select;
    988      1.80    martin 				disk_no[dno] = -3;
    989      1.80    martin 				dno++;
    990       1.2    martin 			}
    991      1.80    martin 			w_menu_no = -1;
    992       1.2    martin 			menu_no = new_menu(MSG_Available_disks,
    993      1.80    martin 				dsk_menu, dno, -1,
    994  1.95.2.1  perseant 				 7, 0, 0, MC_SCROLL,
    995      1.57    martin 				NULL, NULL, NULL, NULL, MSG_exit_menu_generic);
    996       1.2    martin 			if (menu_no == -1)
    997       1.2    martin 				return -1;
    998      1.80    martin 			for (;;) {
    999      1.80    martin 				msg_fmt_display(MSG_ask_disk, "%s", doingwhat);
   1000      1.80    martin 				i = -1;
   1001      1.80    martin 				process_menu(menu_no, &i);
   1002      1.85    martin 				if (i == -1)
   1003      1.85    martin 					return -1;
   1004      1.80    martin 				if (disk_no[i] == -2) {
   1005      1.80    martin 					/* do wedges menu */
   1006      1.80    martin 					if (w_menu_no == -1) {
   1007      1.80    martin 						w_menu_no = new_menu(
   1008      1.80    martin 						    MSG_Available_wedges,
   1009      1.80    martin 						    wedge_menu, wno, -1,
   1010      1.80    martin 						    4, 0, 0, MC_SCROLL,
   1011      1.80    martin 						    NULL, NULL, NULL, NULL,
   1012      1.80    martin 						    MSG_exit_menu_generic);
   1013      1.80    martin 						if (w_menu_no == -1) {
   1014      1.80    martin 							selected_disk = -1;
   1015      1.80    martin 							break;
   1016      1.80    martin 						}
   1017      1.80    martin 					}
   1018      1.80    martin 					i = -1;
   1019      1.80    martin 					process_menu(w_menu_no, &i);
   1020      1.80    martin 					if (i == -1)
   1021      1.80    martin 						continue;
   1022      1.80    martin 					selected_disk = wedge_no[i];
   1023      1.80    martin 					break;
   1024      1.80    martin 				}
   1025      1.80    martin 				selected_disk = disk_no[i];
   1026      1.80    martin 				break;
   1027      1.80    martin 			}
   1028      1.80    martin 			if (w_menu_no >= 0)
   1029      1.80    martin 				free_menu(w_menu_no);
   1030       1.2    martin 			free_menu(menu_no);
   1031      1.80    martin 			if (allow_cur_system && selected_disk == -1) {
   1032      1.80    martin 				pm = dummy_whole_system_pm();
   1033      1.80    martin 				return 1;
   1034      1.40    martin 			}
   1035       1.2    martin 		}
   1036      1.80    martin 		if (partman_go < 0 &&  selected_disk == -3) {
   1037       1.2    martin 			partman_go = 1;
   1038      1.10     isaki 			return -2;
   1039       1.2    martin 		} else
   1040       1.2    martin 			partman_go = 0;
   1041      1.80    martin 		if (selected_disk < 0 ||  selected_disk < 0
   1042      1.80    martin 		    || selected_disk >= numdisks)
   1043      1.10     isaki 			return -1;
   1044       1.2    martin 	}
   1045       1.2    martin 
   1046       1.2    martin 	/* Fill pm struct with device(s) info */
   1047       1.2    martin 	for (i = 0; i < numdisks; i++) {
   1048       1.2    martin 		if (! partman_go)
   1049       1.2    martin 			disk = disks + selected_disk;
   1050       1.2    martin 		else {
   1051       1.2    martin 			disk = disks + i;
   1052       1.2    martin 			already_found = 0;
   1053       1.2    martin 			SLIST_FOREACH(pm_i, &pm_head, l) {
   1054       1.2    martin 				pm_last = pm_i;
   1055      1.44    martin 				if (strcmp(pm_i->diskdev, disk->dd_name) == 0) {
   1056      1.44    martin 					already_found = 1;
   1057       1.2    martin 					break;
   1058       1.2    martin 				}
   1059       1.2    martin 			}
   1060      1.44    martin 			if (pm_i != NULL && already_found) {
   1061      1.44    martin 				/*
   1062      1.44    martin 				 * We already added this device, but
   1063      1.44    martin 				 * partitions might have changed
   1064      1.44    martin 				 */
   1065      1.44    martin 				if (!pm_i->found) {
   1066      1.44    martin 					pm_i->found = true;
   1067      1.44    martin 					if (pm_i->parts == NULL) {
   1068      1.44    martin 						pm_i->parts =
   1069      1.44    martin 						    partitions_read_disk(
   1070      1.44    martin 						    pm_i->diskdev,
   1071      1.54    martin 						    disk->dd_totsec,
   1072      1.62    martin 						    disk->dd_secsize,
   1073      1.54    martin 						    disk->dd_no_mbr);
   1074      1.44    martin 					}
   1075      1.44    martin 				}
   1076       1.2    martin 				continue;
   1077      1.44    martin 			}
   1078       1.1  dholland 		}
   1079       1.2    martin 		pm = pm_new;
   1080       1.2    martin 		pm->found = 1;
   1081      1.30    martin 		pm->ptstart = 0;
   1082      1.30    martin 		pm->ptsize = 0;
   1083       1.2    martin 		strlcpy(pm->diskdev, disk->dd_name, sizeof pm->diskdev);
   1084       1.2    martin 		strlcpy(pm->diskdev_descr, disk->dd_descr, sizeof pm->diskdev_descr);
   1085       1.2    martin 		/* Use as a default disk if the user has the sets on a local disk */
   1086       1.2    martin 		strlcpy(localfs_dev, disk->dd_name, sizeof localfs_dev);
   1087       1.2    martin 
   1088      1.30    martin 		/*
   1089      1.30    martin 		 * Init disk size and geometry
   1090      1.30    martin 		 */
   1091      1.30    martin 		pm->sectorsize = disk->dd_secsize;
   1092      1.30    martin 		pm->dlcyl = disk->dd_cyl;
   1093      1.30    martin 		pm->dlhead = disk->dd_head;
   1094      1.30    martin 		pm->dlsec = disk->dd_sec;
   1095      1.30    martin 		pm->dlsize = disk->dd_totsec;
   1096      1.30    martin 		if (pm->dlsize == 0)
   1097      1.62    martin 			pm->dlsize =
   1098      1.62    martin 			    disk->dd_cyl * disk->dd_head * disk->dd_sec;
   1099      1.30    martin 
   1100      1.54    martin 		pm->parts = partitions_read_disk(pm->diskdev,
   1101      1.62    martin 		    pm->dlsize, disk->dd_secsize, disk->dd_no_mbr);
   1102      1.30    martin 
   1103      1.30    martin again:
   1104      1.30    martin 
   1105      1.30    martin #ifdef DEBUG_VERBOSE
   1106      1.30    martin 		if (pm->parts) {
   1107      1.30    martin 			fputs("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n", stderr);
   1108      1.30    martin 			dump_parts(pm->parts);
   1109      1.30    martin 
   1110      1.30    martin 			if (pm->parts->pscheme->secondary_partitions) {
   1111      1.30    martin 				const struct disk_partitions *sparts =
   1112      1.30    martin 				    pm->parts->pscheme->secondary_partitions(
   1113      1.32    martin 				    pm->parts, pm->ptstart, false);
   1114      1.30    martin 				if (sparts != NULL)
   1115      1.30    martin 					dump_parts(sparts);
   1116      1.30    martin 			}
   1117      1.30    martin 		}
   1118      1.30    martin #endif
   1119      1.30    martin 
   1120      1.30    martin 		pm->no_mbr = disk->dd_no_mbr;
   1121      1.15    martin 		pm->no_part = disk->dd_no_part;
   1122      1.15    martin 		if (!pm->no_part) {
   1123      1.15    martin 			pm->sectorsize = disk->dd_secsize;
   1124      1.15    martin 			pm->dlcyl = disk->dd_cyl;
   1125      1.15    martin 			pm->dlhead = disk->dd_head;
   1126      1.15    martin 			pm->dlsec = disk->dd_sec;
   1127      1.15    martin 			pm->dlsize = disk->dd_totsec;
   1128      1.15    martin 			if (pm->dlsize == 0)
   1129      1.62    martin 				pm->dlsize =
   1130      1.62    martin 				    disk->dd_cyl * disk->dd_head * disk->dd_sec;
   1131      1.30    martin 
   1132      1.30    martin 			if (pm->parts && pm->parts->pscheme->size_limit != 0
   1133      1.30    martin 			    && pm->dlsize > pm->parts->pscheme->size_limit
   1134      1.30    martin 			    && ! partman_go) {
   1135      1.30    martin 
   1136      1.30    martin 				char size[5], limit[5];
   1137      1.30    martin 
   1138      1.30    martin 				humanize_number(size, sizeof(size),
   1139      1.62    martin 				    (uint64_t)pm->dlsize * pm->sectorsize,
   1140      1.30    martin 				    "", HN_AUTOSCALE, HN_B | HN_NOSPACE
   1141      1.30    martin 				    | HN_DECIMAL);
   1142      1.30    martin 
   1143      1.30    martin 				humanize_number(limit, sizeof(limit),
   1144      1.30    martin 				    (uint64_t)pm->parts->pscheme->size_limit
   1145      1.30    martin 					* 512U,
   1146      1.30    martin 				    "", HN_AUTOSCALE, HN_B | HN_NOSPACE
   1147      1.30    martin 				    | HN_DECIMAL);
   1148      1.30    martin 
   1149      1.15    martin 				if (logfp)
   1150      1.30    martin 					fprintf(logfp,
   1151      1.30    martin 					    "disk %s: is too big (%" PRIu64
   1152      1.30    martin 					    " blocks, %s), will be truncated\n",
   1153      1.30    martin 						pm->diskdev, pm->dlsize,
   1154      1.30    martin 						size);
   1155      1.30    martin 
   1156      1.30    martin 				msg_display_subst(MSG_toobigdisklabel, 5,
   1157      1.30    martin 				   pm->diskdev,
   1158      1.30    martin 				   msg_string(pm->parts->pscheme->name),
   1159      1.30    martin 				   msg_string(pm->parts->pscheme->short_name),
   1160      1.30    martin 				   size, limit);
   1161      1.30    martin 
   1162      1.30    martin 				int sel = -1;
   1163      1.30    martin 				const char *err = NULL;
   1164      1.30    martin 				process_menu(MENU_convertscheme, &sel);
   1165      1.30    martin 				if (sel == 1) {
   1166      1.30    martin 					if (!delete_scheme(pm)) {
   1167      1.30    martin 						return -1;
   1168      1.30    martin 					}
   1169      1.30    martin 					goto again;
   1170      1.30    martin 				} else if (sel == 2) {
   1171      1.30    martin 					if (!convert_scheme(pm,
   1172      1.30    martin 					     partman_go < 0, &err)) {
   1173      1.30    martin 						if (err != NULL)
   1174      1.30    martin 							err_msg_win(err);
   1175      1.30    martin 						return -1;
   1176      1.30    martin 					}
   1177      1.30    martin 					goto again;
   1178      1.30    martin 				} else if (sel == 3) {
   1179      1.30    martin 					return -1;
   1180      1.30    martin 				}
   1181      1.30    martin 				pm->dlsize = pm->parts->pscheme->size_limit;
   1182      1.15    martin 			}
   1183      1.15    martin 		} else {
   1184      1.15    martin 			pm->sectorsize = 0;
   1185      1.15    martin 			pm->dlcyl = 0;
   1186      1.15    martin 			pm->dlhead = 0;
   1187      1.15    martin 			pm->dlsec = 0;
   1188      1.15    martin 			pm->dlsize = 0;
   1189      1.15    martin 			pm->no_mbr = 1;
   1190       1.2    martin 		}
   1191       1.2    martin 		pm->dlcylsize = pm->dlhead * pm->dlsec;
   1192       1.2    martin 
   1193       1.2    martin 		if (partman_go) {
   1194       1.2    martin 			pm_getrefdev(pm_new);
   1195       1.2    martin 			if (SLIST_EMPTY(&pm_head) || pm_last == NULL)
   1196       1.2    martin 				 SLIST_INSERT_HEAD(&pm_head, pm_new, l);
   1197       1.2    martin 			else
   1198       1.2    martin 				 SLIST_INSERT_AFTER(pm_last, pm_new, l);
   1199      1.30    martin 			pm_new = malloc(sizeof (struct pm_devs));
   1200       1.2    martin 			memset(pm_new, 0, sizeof *pm_new);
   1201       1.2    martin 		} else
   1202      1.30    martin 			/* We are not in partman and do not want to process
   1203      1.30    martin 			 * all devices, exit */
   1204       1.2    martin 			break;
   1205       1.1  dholland 	}
   1206       1.1  dholland 
   1207      1.15    martin 	return numdisks-skipped;
   1208       1.2    martin }
   1209       1.2    martin 
   1210      1.30    martin static int
   1211      1.30    martin sort_part_usage_by_mount(const void *a, const void *b)
   1212       1.2    martin {
   1213      1.30    martin 	const struct part_usage_info *pa = a, *pb = b;
   1214      1.15    martin 
   1215      1.30    martin 	/* sort all real partitions by mount point */
   1216      1.30    martin 	if ((pa->instflags & PUIINST_MOUNT) &&
   1217      1.30    martin 	    (pb->instflags & PUIINST_MOUNT))
   1218      1.30    martin 		return strcmp(pa->mount, pb->mount);
   1219      1.15    martin 
   1220      1.30    martin 	/* real partitions go first */
   1221      1.30    martin 	if (pa->instflags & PUIINST_MOUNT)
   1222      1.30    martin 		return -1;
   1223      1.30    martin 	if (pb->instflags & PUIINST_MOUNT)
   1224      1.30    martin 		return 1;
   1225       1.8    martin 
   1226      1.30    martin 	/* arbitrary order for all other partitions */
   1227      1.30    martin 	if (pa->type == PT_swap)
   1228      1.30    martin 		return -1;
   1229      1.30    martin 	if (pb->type == PT_swap)
   1230      1.30    martin 		return 1;
   1231      1.30    martin 	if (pa->type < pb->type)
   1232      1.30    martin 		return -1;
   1233      1.30    martin 	if (pa->type > pb->type)
   1234      1.30    martin 		return 1;
   1235      1.30    martin 	if (pa->cur_part_id < pb->cur_part_id)
   1236      1.30    martin 		return -1;
   1237      1.30    martin 	if (pa->cur_part_id > pb->cur_part_id)
   1238      1.30    martin 		return 1;
   1239      1.30    martin 	return (uintptr_t)a < (uintptr_t)b ? -1 : 1;
   1240       1.1  dholland }
   1241       1.1  dholland 
   1242      1.84    martin /*
   1243      1.84    martin  * Are we able to newfs this type of file system?
   1244      1.84    martin  * Keep in sync with switch labels below!
   1245      1.84    martin  */
   1246      1.84    martin bool
   1247      1.84    martin can_newfs_fstype(unsigned int t)
   1248      1.84    martin {
   1249      1.84    martin 	switch (t) {
   1250      1.84    martin 	case FS_APPLEUFS:
   1251      1.84    martin 	case FS_BSDFFS:
   1252      1.84    martin 	case FS_BSDLFS:
   1253      1.84    martin 	case FS_MSDOS:
   1254      1.84    martin 	case FS_EFI_SP:
   1255      1.84    martin 	case FS_SYSVBFS:
   1256      1.84    martin 	case FS_V7:
   1257      1.84    martin 	case FS_EX2FS:
   1258      1.84    martin 		return true;
   1259      1.84    martin 	}
   1260      1.84    martin 	return false;
   1261      1.84    martin }
   1262      1.84    martin 
   1263       1.1  dholland int
   1264      1.30    martin make_filesystems(struct install_partition_desc *install)
   1265       1.1  dholland {
   1266      1.30    martin 	int error = 0, partno = -1;
   1267      1.71    martin 	char *newfs = NULL, devdev[PATH_MAX], rdev[PATH_MAX],
   1268      1.71    martin 	    opts[200], opt[30];
   1269      1.30    martin 	size_t i;
   1270      1.30    martin 	struct part_usage_info *ptn;
   1271      1.30    martin 	struct disk_partitions *parts;
   1272      1.30    martin 	const char *mnt_opts = NULL, *fsname = NULL;
   1273       1.1  dholland 
   1274      1.40    martin 	if (pm->cur_system)
   1275      1.40    martin 		return 1;
   1276      1.40    martin 
   1277      1.15    martin 	if (pm->no_part) {
   1278      1.15    martin 		/* check if this target device already has a ffs */
   1279      1.30    martin 		snprintf(rdev, sizeof rdev, _PATH_DEV "/r%s", pm->diskdev);
   1280      1.30    martin 		error = fsck_preen(rdev, "ffs", true);
   1281      1.15    martin 		if (error) {
   1282      1.15    martin 			if (!ask_noyes(MSG_No_filesystem_newfs))
   1283      1.15    martin 				return EINVAL;
   1284      1.15    martin 			error = run_program(RUN_DISPLAY | RUN_PROGRESS,
   1285      1.91    martin 			    "/sbin/newfs -V2 -O2ea %s", rdev);
   1286      1.15    martin 		}
   1287      1.15    martin 
   1288      1.37    martin 		md_pre_mount(install, 0);
   1289      1.15    martin 
   1290      1.15    martin 		make_target_dir("/");
   1291      1.30    martin 
   1292      1.30    martin 		snprintf(devdev, sizeof devdev, _PATH_DEV "%s", pm->diskdev);
   1293      1.15    martin 		error = target_mount_do("-o async", devdev, "/");
   1294      1.15    martin 		if (error) {
   1295      1.30    martin 			msg_display_subst(MSG_mountfail, 2, devdev, "/");
   1296      1.30    martin 			hit_enter_to_continue(NULL, NULL);
   1297      1.15    martin 		}
   1298      1.30    martin 
   1299      1.15    martin 		return error;
   1300      1.15    martin 	}
   1301      1.15    martin 
   1302       1.1  dholland 	/* Making new file systems and mounting them */
   1303       1.1  dholland 
   1304       1.1  dholland 	/* sort to ensure /usr/local is mounted after /usr (etc) */
   1305      1.30    martin 	qsort(install->infos, install->num, sizeof(*install->infos),
   1306      1.30    martin 	    sort_part_usage_by_mount);
   1307       1.1  dholland 
   1308      1.30    martin 	for (i = 0; i < install->num; i++) {
   1309       1.1  dholland 		/*
   1310      1.68    martin 		 * Newfs all file systems marked as needing this.
   1311      1.38    martin 		 * Mount the ones that have a mountpoint in the target.
   1312       1.1  dholland 		 */
   1313      1.30    martin 		ptn = &install->infos[i];
   1314      1.30    martin 		parts = ptn->parts;
   1315      1.50    martin 		newfs = NULL;
   1316      1.50    martin 		fsname = NULL;
   1317       1.1  dholland 
   1318      1.46    martin 		if (ptn->size == 0 || parts == NULL|| ptn->type == PT_swap)
   1319      1.72    rillig 			continue;
   1320       1.1  dholland 
   1321      1.30    martin 		if (parts->pscheme->get_part_device(parts, ptn->cur_part_id,
   1322      1.59    martin 		    devdev, sizeof devdev, &partno, parent_device_only, false,
   1323      1.59    martin 		    false) && is_active_rootpart(devdev, partno))
   1324      1.30    martin 			continue;
   1325      1.30    martin 
   1326      1.30    martin 		parts->pscheme->get_part_device(parts, ptn->cur_part_id,
   1327      1.59    martin 		    devdev, sizeof devdev, &partno, plain_name, true, true);
   1328      1.30    martin 
   1329      1.30    martin 		parts->pscheme->get_part_device(parts, ptn->cur_part_id,
   1330      1.59    martin 		    rdev, sizeof rdev, &partno, raw_dev_name, true, true);
   1331       1.2    martin 
   1332      1.71    martin 		opts[0] = 0;
   1333      1.30    martin 		switch (ptn->fs_type) {
   1334       1.1  dholland 		case FS_APPLEUFS:
   1335      1.71    martin 			if (ptn->fs_opt3 != 0)
   1336      1.71    martin 				snprintf(opts, sizeof opts, "-i %u",
   1337      1.71    martin 				    ptn->fs_opt3);
   1338      1.71    martin 			asprintf(&newfs, "/sbin/newfs %s", opts);
   1339      1.30    martin 			mnt_opts = "-tffs -o async";
   1340      1.30    martin 			fsname = "ffs";
   1341       1.1  dholland 			break;
   1342       1.1  dholland 		case FS_BSDFFS:
   1343      1.71    martin 			if (ptn->fs_opt3 != 0)
   1344      1.71    martin 				snprintf(opts, sizeof opts, "-i %u ",
   1345      1.71    martin 				    ptn->fs_opt3);
   1346      1.71    martin 			if (ptn->fs_opt1 != 0) {
   1347      1.71    martin 				snprintf(opt, sizeof opt, "-b %u ",
   1348      1.71    martin 				    ptn->fs_opt1);
   1349      1.71    martin 				strcat(opts, opt);
   1350      1.71    martin 			}
   1351      1.71    martin 			if (ptn->fs_opt2 != 0) {
   1352      1.71    martin 				snprintf(opt, sizeof opt, "-f %u ",
   1353      1.71    martin 				    ptn->fs_opt2);
   1354      1.71    martin 				strcat(opts, opt);
   1355      1.71    martin 			}
   1356      1.91    martin 			const char *ffs_fmt;
   1357      1.91    martin 			switch (ptn->fs_version) {
   1358      1.91    martin 			case 3: ffs_fmt = "2ea"; break;
   1359      1.91    martin 			case 2: ffs_fmt = "2"; break;
   1360      1.91    martin 			case 1:
   1361      1.91    martin 			default: ffs_fmt = "1"; break;
   1362      1.91    martin 			}
   1363       1.1  dholland 			asprintf(&newfs,
   1364      1.91    martin 			    "/sbin/newfs -V2 -O %s %s",
   1365      1.91    martin 			    ffs_fmt, opts);
   1366      1.30    martin 			if (ptn->mountflags & PUIMNT_LOG)
   1367      1.30    martin 				mnt_opts = "-tffs -o log";
   1368       1.1  dholland 			else
   1369      1.30    martin 				mnt_opts = "-tffs -o async";
   1370      1.30    martin 			fsname = "ffs";
   1371       1.1  dholland 			break;
   1372       1.1  dholland 		case FS_BSDLFS:
   1373      1.71    martin 			if (ptn->fs_opt1 != 0 && ptn->fs_opt2 != 0)
   1374      1.71    martin 				snprintf(opts, sizeof opts, "-b %u",
   1375      1.71    martin 				     ptn->fs_opt1 * ptn->fs_opt2);
   1376      1.71    martin 			asprintf(&newfs, "/sbin/newfs_lfs %s", opts);
   1377      1.30    martin 			mnt_opts = "-tlfs";
   1378      1.30    martin 			fsname = "lfs";
   1379       1.1  dholland 			break;
   1380       1.1  dholland 		case FS_MSDOS:
   1381      1.84    martin 		case FS_EFI_SP:
   1382       1.1  dholland 			asprintf(&newfs, "/sbin/newfs_msdos");
   1383      1.30    martin 			mnt_opts = "-tmsdos";
   1384      1.30    martin 			fsname = "msdos";
   1385       1.1  dholland 			break;
   1386       1.1  dholland 		case FS_SYSVBFS:
   1387       1.1  dholland 			asprintf(&newfs, "/sbin/newfs_sysvbfs");
   1388      1.30    martin 			mnt_opts = "-tsysvbfs";
   1389      1.30    martin 			fsname = "sysvbfs";
   1390       1.1  dholland 			break;
   1391      1.50    martin 		case FS_V7:
   1392      1.50    martin 			asprintf(&newfs, "/sbin/newfs_v7fs");
   1393      1.50    martin 			mnt_opts = "-tv7fs";
   1394      1.50    martin 			fsname = "v7fs";
   1395      1.50    martin 			break;
   1396       1.1  dholland 		case FS_EX2FS:
   1397      1.58    martin 			asprintf(&newfs,
   1398      1.58    martin 			    ptn->fs_version == 1 ?
   1399      1.58    martin 				"/sbin/newfs_ext2fs -O 0" :
   1400      1.58    martin 				"/sbin/newfs_ext2fs");
   1401      1.30    martin 			mnt_opts = "-text2fs";
   1402      1.30    martin 			fsname = "ext2fs";
   1403       1.1  dholland 			break;
   1404       1.1  dholland 		}
   1405      1.30    martin 		if ((ptn->instflags & PUIINST_NEWFS) && newfs != NULL) {
   1406      1.70    martin 			error = run_program(RUN_DISPLAY | RUN_PROGRESS,
   1407      1.30    martin 			    "%s %s", newfs, rdev);
   1408      1.50    martin 		} else if ((ptn->instflags & (PUIINST_MOUNT|PUIINST_BOOT))
   1409      1.50    martin 		    && fsname != NULL) {
   1410       1.1  dholland 			/* We'd better check it isn't dirty */
   1411      1.30    martin 			error = fsck_preen(devdev, fsname, false);
   1412       1.1  dholland 		}
   1413       1.1  dholland 		free(newfs);
   1414      1.30    martin 		if (error != 0)
   1415       1.1  dholland 			return error;
   1416       1.1  dholland 
   1417      1.30    martin 		ptn->instflags &= ~PUIINST_NEWFS;
   1418      1.37    martin 		md_pre_mount(install, i);
   1419       1.1  dholland 
   1420      1.30    martin 		if (partman_go == 0 && (ptn->instflags & PUIINST_MOUNT) &&
   1421      1.30    martin 				mnt_opts != NULL) {
   1422      1.30    martin 			make_target_dir(ptn->mount);
   1423      1.30    martin 			error = target_mount_do(mnt_opts, devdev,
   1424      1.30    martin 			    ptn->mount);
   1425       1.1  dholland 			if (error) {
   1426      1.72    rillig 				msg_display_subst(MSG_mountfail, 2, devdev,
   1427      1.30    martin 				    ptn->mount);
   1428      1.30    martin 				hit_enter_to_continue(NULL, NULL);
   1429       1.1  dholland 				return error;
   1430       1.1  dholland 			}
   1431       1.1  dholland 		}
   1432       1.1  dholland 	}
   1433       1.1  dholland 	return 0;
   1434       1.1  dholland }
   1435       1.1  dholland 
   1436       1.1  dholland int
   1437      1.30    martin make_fstab(struct install_partition_desc *install)
   1438       1.1  dholland {
   1439       1.1  dholland 	FILE *f;
   1440      1.30    martin 	const char *dump_dev = NULL;
   1441      1.30    martin 	const char *dev;
   1442      1.30    martin 	char dev_buf[PATH_MAX], swap_dev[PATH_MAX];
   1443      1.30    martin 
   1444      1.40    martin 	if (pm->cur_system)
   1445      1.40    martin 		return 1;
   1446      1.40    martin 
   1447      1.30    martin 	swap_dev[0] = 0;
   1448       1.1  dholland 
   1449       1.1  dholland 	/* Create the fstab. */
   1450       1.1  dholland 	make_target_dir("/etc");
   1451       1.1  dholland 	f = target_fopen("/etc/fstab", "w");
   1452       1.2    martin 	scripting_fprintf(NULL, "cat <<EOF >%s/etc/fstab\n", target_prefix());
   1453       1.2    martin 
   1454       1.1  dholland 	if (logfp)
   1455       1.1  dholland 		(void)fprintf(logfp,
   1456      1.30    martin 		    "Making %s/etc/fstab (%s).\n", target_prefix(),
   1457      1.30    martin 		    pm->diskdev);
   1458      1.10     isaki 
   1459       1.1  dholland 	if (f == NULL) {
   1460       1.1  dholland 		msg_display(MSG_createfstab);
   1461       1.1  dholland 		if (logfp)
   1462       1.1  dholland 			(void)fprintf(logfp, "Failed to make /etc/fstab!\n");
   1463      1.30    martin 		hit_enter_to_continue(NULL, NULL);
   1464       1.2    martin #ifndef DEBUG
   1465       1.1  dholland 		return 1;
   1466       1.1  dholland #else
   1467       1.1  dholland 		f = stdout;
   1468       1.1  dholland #endif
   1469       1.1  dholland 	}
   1470       1.1  dholland 
   1471      1.16    martin 	scripting_fprintf(f, "# NetBSD /etc/fstab\n# See /usr/share/examples/"
   1472      1.16    martin 			"fstab/ for more examples.\n");
   1473      1.15    martin 
   1474      1.15    martin 	if (pm->no_part) {
   1475      1.15    martin 		/* single dk? target */
   1476      1.15    martin 		char buf[200], parent[200], swap[200], *prompt;
   1477      1.15    martin 		int res;
   1478      1.15    martin 
   1479      1.15    martin 		if (!get_name_and_parent(pm->diskdev, buf, parent))
   1480      1.15    martin 			goto done_with_disks;
   1481      1.47    martin 		scripting_fprintf(f, NAME_PREFIX "%s\t/\tffs\trw\t\t1 1\n",
   1482      1.15    martin 		    buf);
   1483      1.15    martin 		if (!find_swap_part_on(parent, swap))
   1484      1.15    martin 			goto done_with_disks;
   1485      1.30    martin 		const char *args[] = { parent, swap };
   1486      1.30    martin 		prompt = str_arg_subst(msg_string(MSG_Auto_add_swap_part),
   1487      1.30    martin 		    __arraycount(args), args);
   1488      1.15    martin 		res = ask_yesno(prompt);
   1489      1.15    martin 		free(prompt);
   1490      1.15    martin 		if (res)
   1491      1.47    martin 			scripting_fprintf(f, NAME_PREFIX "%s\tnone"
   1492      1.15    martin 			    "\tswap\tsw,dp\t\t0 0\n", swap);
   1493      1.15    martin 		goto done_with_disks;
   1494      1.15    martin 	}
   1495      1.15    martin 
   1496      1.30    martin 	for (size_t i = 0; i < install->num; i++) {
   1497      1.30    martin 
   1498      1.30    martin 		const struct part_usage_info *ptn = &install->infos[i];
   1499      1.30    martin 
   1500      1.53    martin 		if (ptn->size == 0)
   1501      1.53    martin 			continue;
   1502      1.53    martin 
   1503      1.60    martin 		bool is_tmpfs = ptn->type == PT_root &&
   1504      1.60    martin 		    ptn->fs_type == FS_TMPFS &&
   1505      1.60    martin 		    (ptn->flags & PUIFLG_JUST_MOUNTPOINT);
   1506      1.60    martin 
   1507      1.60    martin 		if (!is_tmpfs && ptn->type != PT_swap &&
   1508      1.30    martin 		    (ptn->instflags & PUIINST_MOUNT) == 0)
   1509      1.30    martin 			continue;
   1510      1.30    martin 
   1511      1.30    martin 		const char *s = "";
   1512      1.30    martin 		const char *mp = ptn->mount;
   1513      1.30    martin 		const char *fstype = "ffs";
   1514      1.30    martin 		int fsck_pass = 0, dump_freq = 0;
   1515      1.30    martin 
   1516      1.30    martin 		if (ptn->parts->pscheme->get_part_device(ptn->parts,
   1517      1.30    martin 			    ptn->cur_part_id, dev_buf, sizeof dev_buf, NULL,
   1518      1.59    martin 			    logical_name, true, false))
   1519      1.30    martin 			dev = dev_buf;
   1520      1.30    martin 		else
   1521      1.30    martin 			dev = NULL;
   1522      1.30    martin 
   1523      1.30    martin 		if (!*mp) {
   1524      1.30    martin 			/*
   1525      1.30    martin 			 * No mount point specified, comment out line and
   1526      1.30    martin 			 * use /mnt as a placeholder for the mount point.
   1527      1.30    martin 			 */
   1528      1.30    martin 			s = "# ";
   1529      1.30    martin 			mp = "/mnt";
   1530      1.30    martin 		}
   1531      1.30    martin 
   1532      1.30    martin 		switch (ptn->fs_type) {
   1533      1.30    martin 		case FS_UNUSED:
   1534      1.30    martin 			continue;
   1535      1.30    martin 		case FS_BSDLFS:
   1536      1.30    martin 			/* If there is no LFS, just comment it out. */
   1537      1.30    martin 			if (!check_lfs_progs())
   1538       1.1  dholland 				s = "# ";
   1539      1.30    martin 			fstype = "lfs";
   1540      1.30    martin 			/* FALLTHROUGH */
   1541      1.30    martin 		case FS_BSDFFS:
   1542      1.30    martin 			fsck_pass = (strcmp(mp, "/") == 0) ? 1 : 2;
   1543      1.30    martin 			dump_freq = 1;
   1544      1.30    martin 			break;
   1545      1.30    martin 		case FS_MSDOS:
   1546      1.30    martin 			fstype = "msdos";
   1547      1.30    martin 			break;
   1548      1.30    martin 		case FS_SWAP:
   1549      1.30    martin 			if (swap_dev[0] == 0) {
   1550      1.66    martin 				strlcpy(swap_dev, dev, sizeof swap_dev);
   1551      1.30    martin 				dump_dev = ",dp";
   1552      1.30    martin 			} else {
   1553      1.30    martin 				dump_dev = "";
   1554       1.1  dholland 			}
   1555      1.30    martin 			scripting_fprintf(f, "%s\t\tnone\tswap\tsw%s\t\t 0 0\n",
   1556      1.30    martin 				dev, dump_dev);
   1557      1.30    martin 			continue;
   1558      1.60    martin #ifdef HAVE_TMPFS
   1559      1.60    martin 		case FS_TMPFS:
   1560      1.60    martin 			if (ptn->size < 0)
   1561      1.60    martin 				scripting_fprintf(f,
   1562  1.95.2.1  perseant 				    "tmpfs\t\t/tmp\ttmpfs\trw,-m1777,"
   1563  1.95.2.1  perseant 				    "-sram%%%" PRIu64 "\n", -ptn->size);
   1564      1.60    martin 			else
   1565      1.60    martin 				scripting_fprintf(f,
   1566  1.95.2.1  perseant 				    "tmpfs\t\t/tmp\ttmpfs\trw,-m1777,"
   1567  1.95.2.1  perseant 				    "-s%" PRIu64 "M\n", ptn->size);
   1568      1.60    martin 			continue;
   1569      1.60    martin #else
   1570      1.60    martin 		case FS_MFS:
   1571      1.60    martin 			if (swap_dev[0] != 0)
   1572      1.60    martin 				scripting_fprintf(f,
   1573      1.60    martin 				    "%s\t\t/tmp\tmfs\trw,-s=%"
   1574      1.60    martin 				    PRIu64 "\n", swap_dev, ptn->size);
   1575      1.60    martin 			else
   1576      1.60    martin 				scripting_fprintf(f,
   1577      1.60    martin 				    "swap\t\t/tmp\tmfs\trw,-s=%"
   1578      1.60    martin 				    PRIu64 "\n", ptn->size);
   1579      1.60    martin 			continue;
   1580      1.60    martin #endif
   1581      1.30    martin 		case FS_SYSVBFS:
   1582      1.30    martin 			fstype = "sysvbfs";
   1583      1.30    martin 			make_target_dir("/stand");
   1584      1.30    martin 			break;
   1585      1.30    martin 		default:
   1586      1.30    martin 			fstype = "???";
   1587      1.30    martin 			s = "# ";
   1588      1.30    martin 			break;
   1589       1.2    martin 		}
   1590      1.30    martin 		/* The code that remounts root rw doesn't check the partition */
   1591      1.30    martin 		if (strcmp(mp, "/") == 0 &&
   1592      1.30    martin 		    (ptn->instflags & PUIINST_MOUNT) == 0)
   1593      1.30    martin 			s = "# ";
   1594      1.30    martin 
   1595      1.30    martin  		scripting_fprintf(f,
   1596      1.30    martin 		  "%s%s\t\t%s\t%s\trw%s%s%s%s%s%s%s%s\t\t %d %d\n",
   1597      1.30    martin 		   s, dev, mp, fstype,
   1598      1.30    martin 		   ptn->mountflags & PUIMNT_LOG ? ",log" : "",
   1599      1.48    martin 		   ptn->mountflags & PUIMNT_NOAUTO ? ",noauto" : "",
   1600      1.30    martin 		   ptn->mountflags & PUIMNT_ASYNC ? ",async" : "",
   1601      1.30    martin 		   ptn->mountflags & PUIMNT_NOATIME ? ",noatime" : "",
   1602      1.30    martin 		   ptn->mountflags & PUIMNT_NODEV ? ",nodev" : "",
   1603      1.30    martin 		   ptn->mountflags & PUIMNT_NODEVMTIME ? ",nodevmtime" : "",
   1604      1.30    martin 		   ptn->mountflags & PUIMNT_NOEXEC ? ",noexec" : "",
   1605      1.30    martin 		   ptn->mountflags & PUIMNT_NOSUID ? ",nosuid" : "",
   1606      1.30    martin 		   dump_freq, fsck_pass);
   1607       1.1  dholland 	}
   1608      1.30    martin 
   1609      1.15    martin done_with_disks:
   1610      1.18    martin 	if (cdrom_dev[0] == 0)
   1611      1.18    martin 		get_default_cdrom(cdrom_dev, sizeof(cdrom_dev));
   1612      1.18    martin 
   1613       1.1  dholland 	/* Add /kern, /proc and /dev/pts to fstab and make mountpoint. */
   1614       1.1  dholland 	scripting_fprintf(f, "kernfs\t\t/kern\tkernfs\trw\n");
   1615       1.1  dholland 	scripting_fprintf(f, "ptyfs\t\t/dev/pts\tptyfs\trw\n");
   1616       1.1  dholland 	scripting_fprintf(f, "procfs\t\t/proc\tprocfs\trw\n");
   1617      1.69    martin 	if (cdrom_dev[0] != 0)
   1618      1.69    martin 		scripting_fprintf(f, "/dev/%s\t\t/cdrom\tcd9660\tro,noauto\n",
   1619      1.69    martin 		    cdrom_dev);
   1620       1.1  dholland 	scripting_fprintf(f, "%stmpfs\t\t/var/shm\ttmpfs\trw,-m1777,-sram%%25\n",
   1621       1.1  dholland 	    tmpfs_on_var_shm() ? "" : "#");
   1622       1.1  dholland 	make_target_dir("/kern");
   1623       1.1  dholland 	make_target_dir("/proc");
   1624       1.1  dholland 	make_target_dir("/dev/pts");
   1625      1.69    martin 	if (cdrom_dev[0] != 0)
   1626      1.69    martin 		make_target_dir("/cdrom");
   1627       1.1  dholland 	make_target_dir("/var/shm");
   1628       1.1  dholland 
   1629       1.1  dholland 	scripting_fprintf(NULL, "EOF\n");
   1630       1.1  dholland 
   1631       1.1  dholland 	fclose(f);
   1632       1.1  dholland 	fflush(NULL);
   1633       1.1  dholland 	return 0;
   1634       1.1  dholland }
   1635       1.1  dholland 
   1636      1.47    martin static bool
   1637      1.47    martin find_part_by_name(const char *name, struct disk_partitions **parts,
   1638      1.47    martin     part_id *pno)
   1639      1.47    martin {
   1640      1.47    martin 	struct pm_devs *i;
   1641      1.47    martin 	struct disk_partitions *ps;
   1642      1.47    martin 	part_id id;
   1643      1.47    martin 	struct disk_desc disks[MAX_DISKS];
   1644      1.47    martin 	int n, cnt;
   1645      1.47    martin 
   1646      1.47    martin 	if (SLIST_EMPTY(&pm_head)) {
   1647      1.47    martin 		/*
   1648      1.47    martin 		 * List has not been filled, only "pm" is valid - check
   1649      1.47    martin 		 * that first.
   1650      1.47    martin 		 */
   1651      1.65    martin 		if (pm->parts != NULL &&
   1652      1.65    martin 		    pm->parts->pscheme->find_by_name != NULL) {
   1653      1.47    martin 			id = pm->parts->pscheme->find_by_name(pm->parts, name);
   1654      1.47    martin 			if (id != NO_PART) {
   1655      1.47    martin 				*pno = id;
   1656      1.47    martin 				*parts = pm->parts;
   1657      1.47    martin 				return true;
   1658      1.47    martin 			}
   1659      1.47    martin 		}
   1660      1.47    martin 		/*
   1661      1.47    martin 		 * Not that easy - check all other disks
   1662      1.47    martin 		 */
   1663      1.47    martin 		cnt = get_disks(disks, false);
   1664      1.47    martin 		for (n = 0; n < cnt; n++) {
   1665      1.47    martin 			if (strcmp(disks[n].dd_name, pm->diskdev) == 0)
   1666      1.47    martin 				continue;
   1667      1.47    martin 			ps = partitions_read_disk(disks[n].dd_name,
   1668      1.62    martin 			    disks[n].dd_totsec,
   1669      1.62    martin 			    disks[n].dd_secsize,
   1670      1.62    martin 			    disks[n].dd_no_mbr);
   1671      1.47    martin 			if (ps == NULL)
   1672      1.47    martin 				continue;
   1673      1.47    martin 			if (ps->pscheme->find_by_name == NULL)
   1674      1.47    martin 				continue;
   1675      1.47    martin 			id = ps->pscheme->find_by_name(ps, name);
   1676      1.47    martin 			if (id != NO_PART) {
   1677      1.47    martin 				*pno = id;
   1678      1.47    martin 				*parts = ps;
   1679      1.47    martin 				return true;	/* XXX this leaks memory */
   1680      1.47    martin 			}
   1681      1.47    martin 			ps->pscheme->free(ps);
   1682      1.47    martin 		}
   1683      1.47    martin 	} else {
   1684      1.47    martin 		SLIST_FOREACH(i, &pm_head, l) {
   1685      1.47    martin 			if (i->parts == NULL)
   1686      1.47    martin 				continue;
   1687      1.47    martin 			if (i->parts->pscheme->find_by_name == NULL)
   1688      1.47    martin 				continue;
   1689      1.47    martin 			id = i->parts->pscheme->find_by_name(i->parts, name);
   1690      1.47    martin 			if (id == NO_PART)
   1691      1.47    martin 				continue;
   1692      1.47    martin 			*pno = id;
   1693      1.47    martin 			*parts = i->parts;
   1694      1.47    martin 			return true;
   1695      1.47    martin 		}
   1696      1.47    martin 	}
   1697      1.47    martin 
   1698      1.47    martin 	*pno = NO_PART;
   1699      1.47    martin 	*parts = NULL;
   1700      1.47    martin 	return false;
   1701      1.47    martin }
   1702      1.47    martin 
   1703       1.1  dholland static int
   1704       1.1  dholland /*ARGSUSED*/
   1705      1.47    martin process_found_fs(struct data *list, size_t num, const struct lookfor *item,
   1706      1.47    martin     bool with_fsck)
   1707       1.1  dholland {
   1708       1.1  dholland 	int error;
   1709      1.47    martin 	char rdev[PATH_MAX], dev[PATH_MAX],
   1710      1.47    martin 	    options[STRSIZE], tmp[STRSIZE], *op, *last;
   1711      1.47    martin 	const char *fsname = (const char*)item->var;
   1712      1.47    martin 	part_id pno;
   1713      1.47    martin 	struct disk_partitions *parts;
   1714      1.51    martin 	size_t len;
   1715      1.51    martin 	bool first, is_root;
   1716      1.47    martin 
   1717      1.47    martin 	if (num < 2 || strstr(list[2].u.s_val, "noauto") != NULL)
   1718      1.47    martin 		return 0;
   1719       1.1  dholland 
   1720      1.51    martin 	is_root = strcmp(list[1].u.s_val, "/") == 0;
   1721      1.51    martin 	if (is_root && target_mounted())
   1722       1.1  dholland 		return 0;
   1723       1.1  dholland 
   1724      1.47    martin 	if (strcmp(item->head, name_prefix) == 0) {
   1725      1.47    martin 		/* this fstab entry uses NAME= syntax */
   1726      1.64    martin 
   1727      1.64    martin 		/* unescape */
   1728      1.64    martin 		char *src, *dst;
   1729      1.64    martin 		for (src = list[0].u.s_val, dst =src; src[0] != 0; ) {
   1730      1.64    martin 			if (src[0] == '\\' && src[1] != 0)
   1731      1.64    martin 				src++;
   1732      1.64    martin 			*dst++ = *src++;
   1733      1.64    martin 		}
   1734      1.64    martin 		*dst = 0;
   1735      1.64    martin 
   1736      1.47    martin 		if (!find_part_by_name(list[0].u.s_val,
   1737      1.47    martin 		    &parts, &pno) || parts == NULL || pno == NO_PART)
   1738      1.47    martin 			return 0;
   1739      1.47    martin 		parts->pscheme->get_part_device(parts, pno,
   1740      1.59    martin 		    dev, sizeof(dev), NULL, plain_name, true, true);
   1741      1.47    martin 		parts->pscheme->get_part_device(parts, pno,
   1742      1.59    martin 		    rdev, sizeof(rdev), NULL, raw_dev_name, true, true);
   1743      1.45    martin 	} else {
   1744      1.51    martin 		/* this fstab entry uses the plain device name */
   1745      1.51    martin 		if (is_root) {
   1746      1.51    martin 			/*
   1747      1.51    martin 			 * PR 54480: we can not use the current device name
   1748      1.51    martin 			 * as it might be different from the real environment.
   1749      1.51    martin 			 * This is an abuse of the functionality, but it used
   1750      1.51    martin 			 * to work before (and still does work if only a single
   1751      1.51    martin 			 * target disk is involved).
   1752      1.51    martin 			 * Use the device name from the current "pm" instead.
   1753      1.51    martin 			 */
   1754      1.51    martin 			strcpy(rdev, "/dev/r");
   1755      1.51    martin 			strlcat(rdev, pm->diskdev, sizeof(rdev));
   1756      1.51    martin 			strcpy(dev, "/dev/");
   1757      1.51    martin 			strlcat(dev, pm->diskdev, sizeof(dev));
   1758      1.51    martin 			/* copy over the partition letter, if any */
   1759      1.51    martin 			len = strlen(list[0].u.s_val);
   1760      1.51    martin 			if (list[0].u.s_val[len-1] >= 'a' &&
   1761      1.51    martin 			    list[0].u.s_val[len-1] <=
   1762      1.51    martin 			    ('a' + getmaxpartitions())) {
   1763      1.51    martin 				strlcat(rdev, &list[0].u.s_val[len-1],
   1764      1.51    martin 				    sizeof(rdev));
   1765      1.51    martin 				strlcat(dev, &list[0].u.s_val[len-1],
   1766      1.51    martin 				    sizeof(dev));
   1767      1.51    martin 			}
   1768      1.51    martin 		} else {
   1769      1.51    martin 			strcpy(rdev, "/dev/r");
   1770      1.51    martin 			strlcat(rdev, list[0].u.s_val, sizeof(rdev));
   1771      1.51    martin 			strcpy(dev, "/dev/");
   1772      1.51    martin 			strlcat(dev, list[0].u.s_val, sizeof(dev));
   1773      1.51    martin 		}
   1774      1.47    martin 	}
   1775      1.47    martin 
   1776      1.47    martin 	if (with_fsck) {
   1777      1.47    martin 		/* need the raw device for fsck_preen */
   1778      1.47    martin 		error = fsck_preen(rdev, fsname, false);
   1779      1.47    martin 		if (error != 0)
   1780      1.47    martin 			return error;
   1781      1.45    martin 	}
   1782      1.45    martin 
   1783      1.47    martin 	/* add mount option for fs type */
   1784      1.47    martin 	strcpy(options, "-t ");
   1785      1.47    martin 	strlcat(options, fsname, sizeof(options));
   1786      1.47    martin 
   1787      1.47    martin 	/* extract mount options from fstab */
   1788      1.47    martin 	strlcpy(tmp, list[2].u.s_val, sizeof(tmp));
   1789      1.47    martin 	for (first = true, op = strtok_r(tmp, ",", &last); op != NULL;
   1790      1.47    martin 	    op = strtok_r(NULL, ",", &last)) {
   1791      1.47    martin 		if (strcmp(op, FSTAB_RW) == 0 ||
   1792      1.47    martin 		    strcmp(op, FSTAB_RQ) == 0 ||
   1793      1.47    martin 		    strcmp(op, FSTAB_RO) == 0 ||
   1794      1.47    martin 		    strcmp(op, FSTAB_SW) == 0 ||
   1795      1.47    martin 		    strcmp(op, FSTAB_DP) == 0 ||
   1796      1.47    martin 		    strcmp(op, FSTAB_XX) == 0)
   1797      1.47    martin 			continue;
   1798      1.47    martin 		if (first) {
   1799      1.47    martin 			first = false;
   1800      1.47    martin 			strlcat(options, " -o ", sizeof(options));
   1801      1.47    martin 		} else {
   1802      1.47    martin 			strlcat(options, ",", sizeof(options));
   1803      1.47    martin 		}
   1804      1.47    martin 		strlcat(options, op, sizeof(options));
   1805      1.47    martin 	}
   1806       1.1  dholland 
   1807      1.47    martin 	error = target_mount(options, dev, list[1].u.s_val);
   1808       1.1  dholland 	if (error != 0) {
   1809      1.33  christos 		msg_fmt_display(MSG_mount_failed, "%s", list[0].u.s_val);
   1810       1.9    martin 		if (!ask_noyes(NULL))
   1811       1.1  dholland 			return error;
   1812       1.1  dholland 	}
   1813       1.1  dholland 	return 0;
   1814       1.1  dholland }
   1815       1.1  dholland 
   1816       1.1  dholland static int
   1817       1.1  dholland /*ARGSUSED*/
   1818      1.47    martin found_fs(struct data *list, size_t num, const struct lookfor *item)
   1819       1.1  dholland {
   1820      1.47    martin 	return process_found_fs(list, num, item, true);
   1821      1.47    martin }
   1822       1.1  dholland 
   1823      1.47    martin static int
   1824      1.47    martin /*ARGSUSED*/
   1825      1.47    martin found_fs_nocheck(struct data *list, size_t num, const struct lookfor *item)
   1826      1.47    martin {
   1827      1.47    martin 	return process_found_fs(list, num, item, false);
   1828       1.1  dholland }
   1829       1.1  dholland 
   1830       1.1  dholland /*
   1831       1.1  dholland  * Do an fsck. On failure, inform the user by showing a warning
   1832       1.1  dholland  * message and doing menu_ok() before proceeding.
   1833      1.30    martin  * The device passed should be the full qualified path to raw disk
   1834      1.30    martin  * (e.g. /dev/rwd0a).
   1835       1.1  dholland  * Returns 0 on success, or nonzero return code from fsck() on failure.
   1836       1.1  dholland  */
   1837       1.1  dholland static int
   1838      1.30    martin fsck_preen(const char *disk, const char *fsname, bool silent)
   1839       1.1  dholland {
   1840      1.30    martin 	char *prog, err[12];
   1841       1.1  dholland 	int error;
   1842       1.1  dholland 
   1843       1.1  dholland 	if (fsname == NULL)
   1844       1.1  dholland 		return 0;
   1845       1.1  dholland 	/* first, check if fsck program exists, if not, assume ok */
   1846       1.1  dholland 	asprintf(&prog, "/sbin/fsck_%s", fsname);
   1847       1.1  dholland 	if (prog == NULL)
   1848       1.1  dholland 		return 0;
   1849      1.12    martin 	if (access(prog, X_OK) != 0) {
   1850      1.12    martin 		free(prog);
   1851       1.1  dholland 		return 0;
   1852      1.12    martin 	}
   1853       1.1  dholland 	if (!strcmp(fsname,"ffs"))
   1854      1.30    martin 		fixsb(prog, disk);
   1855      1.30    martin 	error = run_program(silent? RUN_SILENT|RUN_ERROR_OK : 0, "%s -p -q %s", prog, disk);
   1856       1.1  dholland 	free(prog);
   1857      1.15    martin 	if (error != 0 && !silent) {
   1858      1.30    martin 		sprintf(err, "%d", error);
   1859      1.30    martin 		msg_display_subst(msg_string(MSG_badfs), 3,
   1860      1.30    martin 		    disk, fsname, err);
   1861       1.9    martin 		if (ask_noyes(NULL))
   1862       1.1  dholland 			error = 0;
   1863       1.1  dholland 		/* XXX at this point maybe we should run a full fsck? */
   1864       1.1  dholland 	}
   1865       1.1  dholland 	return error;
   1866       1.1  dholland }
   1867       1.1  dholland 
   1868       1.1  dholland /* This performs the same function as the etc/rc.d/fixsb script
   1869       1.1  dholland  * which attempts to correct problems with ffs1 filesystems
   1870       1.1  dholland  * which may have been introduced by booting a netbsd-current kernel
   1871       1.1  dholland  * from between April of 2003 and January 2004. For more information
   1872       1.1  dholland  * This script was developed as a response to NetBSD pr install/25138
   1873       1.1  dholland  * Additional prs regarding the original issue include:
   1874       1.1  dholland  *  bin/17910 kern/21283 kern/21404 port-macppc/23925 port-macppc/23926
   1875       1.1  dholland  */
   1876       1.1  dholland static void
   1877      1.30    martin fixsb(const char *prog, const char *disk)
   1878       1.1  dholland {
   1879       1.1  dholland 	int fd;
   1880       1.1  dholland 	int rval;
   1881       1.1  dholland 	union {
   1882       1.1  dholland 		struct fs fs;
   1883       1.1  dholland 		char buf[SBLOCKSIZE];
   1884       1.1  dholland 	} sblk;
   1885       1.1  dholland 	struct fs *fs = &sblk.fs;
   1886       1.1  dholland 
   1887      1.30    martin 	fd = open(disk, O_RDONLY);
   1888       1.1  dholland 	if (fd == -1)
   1889       1.1  dholland 		return;
   1890       1.1  dholland 
   1891       1.1  dholland 	/* Read ffsv1 main superblock */
   1892       1.1  dholland 	rval = pread(fd, sblk.buf, sizeof sblk.buf, SBLOCK_UFS1);
   1893       1.1  dholland 	close(fd);
   1894       1.1  dholland 	if (rval != sizeof sblk.buf)
   1895       1.1  dholland 		return;
   1896       1.1  dholland 
   1897       1.1  dholland 	if (fs->fs_magic != FS_UFS1_MAGIC &&
   1898       1.1  dholland 	    fs->fs_magic != FS_UFS1_MAGIC_SWAPPED)
   1899       1.1  dholland 		/* Not FFSv1 */
   1900       1.1  dholland 		return;
   1901       1.1  dholland 	if (fs->fs_old_flags & FS_FLAGS_UPDATED)
   1902       1.1  dholland 		/* properly updated fslevel 4 */
   1903       1.1  dholland 		return;
   1904       1.1  dholland 	if (fs->fs_bsize != fs->fs_maxbsize)
   1905       1.1  dholland 		/* not messed up */
   1906       1.1  dholland 		return;
   1907       1.1  dholland 
   1908       1.1  dholland 	/*
   1909       1.1  dholland 	 * OK we have a munged fs, first 'upgrade' to fslevel 4,
   1910       1.1  dholland 	 * We specify -b16 in order to stop fsck bleating that the
   1911       1.1  dholland 	 * sb doesn't match the first alternate.
   1912       1.1  dholland 	 */
   1913       1.1  dholland 	run_program(RUN_DISPLAY | RUN_PROGRESS,
   1914      1.30    martin 	    "%s -p -b 16 -c 4 %s", prog, disk);
   1915       1.1  dholland 	/* Then downgrade to fslevel 3 */
   1916       1.1  dholland 	run_program(RUN_DISPLAY | RUN_PROGRESS,
   1917      1.30    martin 	    "%s -p -c 3 %s", prog, disk);
   1918       1.1  dholland }
   1919       1.1  dholland 
   1920       1.1  dholland /*
   1921       1.1  dholland  * fsck and mount the root partition.
   1922      1.30    martin  * devdev is the fully qualified block device name.
   1923       1.1  dholland  */
   1924       1.1  dholland static int
   1925      1.47    martin mount_root(const char *devdev, bool first, bool writeable,
   1926      1.47    martin      struct install_partition_desc *install)
   1927       1.1  dholland {
   1928       1.1  dholland 	int	error;
   1929       1.1  dholland 
   1930      1.30    martin 	error = fsck_preen(devdev, "ffs", false);
   1931       1.1  dholland 	if (error != 0)
   1932       1.1  dholland 		return error;
   1933       1.1  dholland 
   1934      1.47    martin 	if (first)
   1935      1.47    martin 		md_pre_mount(install, 0);
   1936       1.1  dholland 
   1937      1.30    martin 	/* Mount devdev on target's "".
   1938       1.1  dholland 	 * If we pass "" as mount-on, Prefixing will DTRT.
   1939       1.1  dholland 	 * for now, use no options.
   1940       1.1  dholland 	 * XXX consider -o remount in case target root is
   1941       1.1  dholland 	 * current root, still readonly from single-user?
   1942       1.1  dholland 	 */
   1943      1.47    martin 	return target_mount(writeable? "" : "-r", devdev, "");
   1944       1.1  dholland }
   1945       1.1  dholland 
   1946       1.1  dholland /* Get information on the file systems mounted from the root filesystem.
   1947       1.1  dholland  * Offer to convert them into 4.4BSD inodes if they are not 4.4BSD
   1948       1.1  dholland  * inodes.  Fsck them.  Mount them.
   1949       1.1  dholland  */
   1950       1.1  dholland 
   1951       1.1  dholland int
   1952      1.30    martin mount_disks(struct install_partition_desc *install)
   1953       1.1  dholland {
   1954       1.1  dholland 	char *fstab;
   1955       1.1  dholland 	int   fstabsize;
   1956       1.1  dholland 	int   error;
   1957      1.30    martin 	char devdev[PATH_MAX];
   1958      1.47    martin 	size_t i, num_fs_types, num_entries;
   1959      1.47    martin 	struct lookfor *fstabbuf, *l;
   1960       1.1  dholland 
   1961      1.40    martin 	if (install->cur_system)
   1962      1.40    martin 		return 0;
   1963      1.40    martin 
   1964      1.47    martin 	/*
   1965      1.47    martin 	 * Check what file system tools are available and create parsers
   1966      1.47    martin 	 * for the corresponding fstab(5) entries - all others will be
   1967      1.47    martin 	 * ignored.
   1968      1.47    martin 	 */
   1969      1.47    martin 	num_fs_types = 1;	/* ffs is implicit */
   1970      1.47    martin 	for (i = 0; i < __arraycount(extern_fs_with_chk); i++) {
   1971      1.47    martin 		sprintf(devdev, "/sbin/newfs_%s", extern_fs_with_chk[i]);
   1972      1.47    martin 		if (file_exists_p(devdev))
   1973      1.47    martin 			num_fs_types++;
   1974      1.47    martin 	}
   1975      1.47    martin 	for (i = 0; i < __arraycount(extern_fs_newfs_only); i++) {
   1976      1.47    martin 		sprintf(devdev, "/sbin/newfs_%s", extern_fs_newfs_only[i]);
   1977      1.47    martin 		if (file_exists_p(devdev))
   1978      1.47    martin 			num_fs_types++;
   1979      1.47    martin 	}
   1980      1.47    martin 	num_entries = 2 *  num_fs_types + 1;	/* +1 for "ufs" special case */
   1981      1.47    martin 	fstabbuf = calloc(num_entries, sizeof(*fstabbuf));
   1982      1.47    martin 	if (fstabbuf == NULL)
   1983      1.47    martin 		return -1;
   1984      1.47    martin 	l = fstabbuf;
   1985      1.47    martin 	l->head = "/dev/";
   1986      1.47    martin 	l->fmt = strdup("/dev/%s %s ffs %s");
   1987      1.47    martin 	l->todo = "c";
   1988      1.47    martin 	l->var = __UNCONST("ffs");
   1989      1.47    martin 	l->func = found_fs;
   1990      1.47    martin 	l++;
   1991      1.47    martin 	l->head = "/dev/";
   1992      1.47    martin 	l->fmt = strdup("/dev/%s %s ufs %s");
   1993      1.47    martin 	l->todo = "c";
   1994      1.47    martin 	l->var = __UNCONST("ffs");
   1995      1.47    martin 	l->func = found_fs;
   1996      1.47    martin 	l++;
   1997      1.47    martin 	l->head = NAME_PREFIX;
   1998      1.47    martin 	l->fmt = strdup(NAME_PREFIX "%s %s ffs %s");
   1999      1.47    martin 	l->todo = "c";
   2000      1.47    martin 	l->var = __UNCONST("ffs");
   2001      1.47    martin 	l->func = found_fs;
   2002      1.47    martin 	l++;
   2003      1.47    martin 	for (i = 0; i < __arraycount(extern_fs_with_chk); i++) {
   2004      1.47    martin 		sprintf(devdev, "/sbin/newfs_%s", extern_fs_with_chk[i]);
   2005      1.47    martin 		if (!file_exists_p(devdev))
   2006      1.47    martin 			continue;
   2007      1.47    martin 		sprintf(devdev, "/dev/%%s %%s %s %%s", extern_fs_with_chk[i]);
   2008      1.47    martin 		l->head = "/dev/";
   2009      1.47    martin 		l->fmt = strdup(devdev);
   2010      1.47    martin 		l->todo = "c";
   2011      1.47    martin 		l->var = __UNCONST(extern_fs_with_chk[i]);
   2012      1.47    martin 		l->func = found_fs;
   2013      1.47    martin 		l++;
   2014      1.47    martin 		sprintf(devdev, NAME_PREFIX "%%s %%s %s %%s",
   2015      1.47    martin 		    extern_fs_with_chk[i]);
   2016      1.47    martin 		l->head = NAME_PREFIX;
   2017      1.47    martin 		l->fmt = strdup(devdev);
   2018      1.47    martin 		l->todo = "c";
   2019      1.47    martin 		l->var = __UNCONST(extern_fs_with_chk[i]);
   2020      1.47    martin 		l->func = found_fs;
   2021      1.47    martin 		l++;
   2022      1.47    martin 	}
   2023      1.47    martin 	for (i = 0; i < __arraycount(extern_fs_newfs_only); i++) {
   2024      1.47    martin 		sprintf(devdev, "/sbin/newfs_%s", extern_fs_newfs_only[i]);
   2025      1.47    martin 		if (!file_exists_p(devdev))
   2026      1.47    martin 			continue;
   2027      1.47    martin 		sprintf(devdev, "/dev/%%s %%s %s %%s", extern_fs_newfs_only[i]);
   2028      1.47    martin 		l->head = "/dev/";
   2029      1.47    martin 		l->fmt = strdup(devdev);
   2030      1.47    martin 		l->todo = "c";
   2031      1.47    martin 		l->var = __UNCONST(extern_fs_newfs_only[i]);
   2032      1.47    martin 		l->func = found_fs_nocheck;
   2033      1.47    martin 		l++;
   2034      1.47    martin 		sprintf(devdev, NAME_PREFIX "%%s %%s %s %%s",
   2035      1.47    martin 		    extern_fs_newfs_only[i]);
   2036      1.47    martin 		l->head = NAME_PREFIX;
   2037      1.47    martin 		l->fmt = strdup(devdev);
   2038      1.47    martin 		l->todo = "c";
   2039      1.47    martin 		l->var = __UNCONST(extern_fs_newfs_only[i]);
   2040      1.47    martin 		l->func = found_fs_nocheck;
   2041      1.47    martin 		l++;
   2042      1.47    martin 	}
   2043      1.47    martin 	assert((size_t)(l - fstabbuf) == num_entries);
   2044       1.1  dholland 
   2045       1.1  dholland 	/* First the root device. */
   2046      1.65    martin 	if (target_already_root()) {
   2047       1.1  dholland 		/* avoid needing to call target_already_root() again */
   2048       1.1  dholland 		targetroot_mnt[0] = 0;
   2049      1.65    martin 	} else if (pm->no_part) {
   2050      1.65    martin 		snprintf(devdev, sizeof devdev, _PATH_DEV "%s", pm->diskdev);
   2051      1.65    martin 		error = mount_root(devdev, true, false, install);
   2052      1.65    martin 		if (error != 0 && error != EBUSY)
   2053      1.65    martin 			return -1;
   2054      1.65    martin 	} else {
   2055      1.39    martin 		for (i = 0; i < install->num; i++) {
   2056      1.39    martin 			if (is_root_part_mount(install->infos[i].mount))
   2057      1.39    martin 				break;
   2058      1.39    martin 		}
   2059      1.39    martin 
   2060      1.39    martin 		if (i >= install->num) {
   2061      1.39    martin 			hit_enter_to_continue(MSG_noroot, NULL);
   2062      1.39    martin 			return -1;
   2063      1.39    martin 		}
   2064      1.39    martin 
   2065      1.39    martin 		if (!install->infos[i].parts->pscheme->get_part_device(
   2066      1.39    martin 		    install->infos[i].parts, install->infos[i].cur_part_id,
   2067      1.59    martin 		    devdev, sizeof devdev, NULL, plain_name, true, true))
   2068      1.30    martin 			return -1;
   2069      1.47    martin 		error = mount_root(devdev, true, false, install);
   2070       1.1  dholland 		if (error != 0 && error != EBUSY)
   2071       1.1  dholland 			return -1;
   2072       1.1  dholland 	}
   2073       1.1  dholland 
   2074       1.1  dholland 	/* Check the target /etc/fstab exists before trying to parse it. */
   2075       1.1  dholland 	if (target_dir_exists_p("/etc") == 0 ||
   2076       1.1  dholland 	    target_file_exists_p("/etc/fstab") == 0) {
   2077      1.33  christos 		msg_fmt_display(MSG_noetcfstab, "%s", pm->diskdev);
   2078      1.30    martin 		hit_enter_to_continue(NULL, NULL);
   2079       1.1  dholland 		return -1;
   2080       1.1  dholland 	}
   2081       1.1  dholland 
   2082       1.1  dholland 
   2083       1.1  dholland 	/* Get fstab entries from the target-root /etc/fstab. */
   2084       1.1  dholland 	fstabsize = target_collect_file(T_FILE, &fstab, "/etc/fstab");
   2085       1.1  dholland 	if (fstabsize < 0) {
   2086       1.1  dholland 		/* error ! */
   2087      1.33  christos 		msg_fmt_display(MSG_badetcfstab, "%s", pm->diskdev);
   2088      1.30    martin 		hit_enter_to_continue(NULL, NULL);
   2089      1.47    martin 		umount_root();
   2090       1.2    martin 		return -2;
   2091       1.1  dholland 	}
   2092      1.47    martin 	/*
   2093      1.47    martin 	 * We unmount the read-only root again, so we can mount it
   2094      1.47    martin 	 * with proper options from /etc/fstab
   2095      1.47    martin 	 */
   2096      1.47    martin 	umount_root();
   2097      1.47    martin 
   2098      1.47    martin 	/*
   2099      1.47    martin 	 * Now do all entries in /etc/fstab and mount them if required
   2100      1.47    martin 	 */
   2101      1.47    martin 	error = walk(fstab, (size_t)fstabsize, fstabbuf, num_entries);
   2102       1.1  dholland 	free(fstab);
   2103      1.47    martin 	for (i = 0; i < num_entries; i++)
   2104      1.47    martin 		free(__UNCONST(fstabbuf[i].fmt));
   2105      1.47    martin 	free(fstabbuf);
   2106       1.1  dholland 
   2107       1.1  dholland 	return error;
   2108       1.1  dholland }
   2109       1.1  dholland 
   2110      1.67    martin static char swap_dev[PATH_MAX];
   2111      1.67    martin 
   2112      1.75    martin void
   2113      1.30    martin set_swap_if_low_ram(struct install_partition_desc *install)
   2114      1.10     isaki {
   2115      1.67    martin 	swap_dev[0] = 0;
   2116      1.61    martin 	if (get_ramsize() <= TINY_RAM_SIZE)
   2117      1.75    martin 		set_swap(install);
   2118       1.7       abs }
   2119       1.7       abs 
   2120      1.75    martin void
   2121      1.30    martin set_swap(struct install_partition_desc *install)
   2122       1.1  dholland {
   2123      1.30    martin 	size_t i;
   2124       1.1  dholland 	int rval;
   2125       1.1  dholland 
   2126      1.67    martin 	swap_dev[0] = 0;
   2127      1.30    martin 	for (i = 0; i < install->num; i++) {
   2128      1.30    martin 		if (install->infos[i].type == PT_swap)
   2129      1.30    martin 			break;
   2130      1.30    martin 	}
   2131      1.30    martin 	if (i >= install->num)
   2132      1.75    martin 		return;
   2133       1.1  dholland 
   2134      1.30    martin 	if (!install->infos[i].parts->pscheme->get_part_device(
   2135      1.67    martin 	    install->infos[i].parts, install->infos[i].cur_part_id, swap_dev,
   2136      1.67    martin 	    sizeof swap_dev, NULL, plain_name, true, true))
   2137      1.75    martin 		return;
   2138      1.30    martin 
   2139      1.67    martin 	rval = swapctl(SWAP_ON, swap_dev, 0);
   2140      1.75    martin 	if (rval != 0)
   2141      1.67    martin 		swap_dev[0] = 0;
   2142      1.67    martin }
   2143       1.1  dholland 
   2144      1.67    martin void
   2145      1.67    martin clear_swap(void)
   2146      1.67    martin {
   2147      1.67    martin 
   2148      1.67    martin 	if (swap_dev[0] == 0)
   2149      1.67    martin 		return;
   2150      1.67    martin 	swapctl(SWAP_OFF, swap_dev, 0);
   2151      1.67    martin 	swap_dev[0] = 0;
   2152       1.1  dholland }
   2153       1.1  dholland 
   2154       1.1  dholland int
   2155       1.1  dholland check_swap(const char *disk, int remove_swap)
   2156       1.1  dholland {
   2157       1.1  dholland 	struct swapent *swap;
   2158       1.1  dholland 	char *cp;
   2159       1.1  dholland 	int nswap;
   2160       1.1  dholland 	int l;
   2161       1.1  dholland 	int rval = 0;
   2162       1.1  dholland 
   2163       1.1  dholland 	nswap = swapctl(SWAP_NSWAP, 0, 0);
   2164       1.1  dholland 	if (nswap <= 0)
   2165       1.1  dholland 		return 0;
   2166       1.1  dholland 
   2167       1.1  dholland 	swap = malloc(nswap * sizeof *swap);
   2168       1.1  dholland 	if (swap == NULL)
   2169       1.1  dholland 		return -1;
   2170       1.1  dholland 
   2171       1.1  dholland 	nswap = swapctl(SWAP_STATS, swap, nswap);
   2172       1.1  dholland 	if (nswap < 0)
   2173       1.1  dholland 		goto bad_swap;
   2174       1.1  dholland 
   2175       1.1  dholland 	l = strlen(disk);
   2176       1.1  dholland 	while (--nswap >= 0) {
   2177       1.1  dholland 		/* Should we check the se_dev or se_path? */
   2178       1.1  dholland 		cp = swap[nswap].se_path;
   2179       1.1  dholland 		if (memcmp(cp, "/dev/", 5) != 0)
   2180       1.1  dholland 			continue;
   2181       1.1  dholland 		if (memcmp(cp + 5, disk, l) != 0)
   2182       1.1  dholland 			continue;
   2183       1.1  dholland 		if (!isalpha(*(unsigned char *)(cp + 5 + l)))
   2184       1.1  dholland 			continue;
   2185       1.1  dholland 		if (cp[5 + l + 1] != 0)
   2186       1.1  dholland 			continue;
   2187       1.1  dholland 		/* ok path looks like it is for this device */
   2188       1.1  dholland 		if (!remove_swap) {
   2189       1.1  dholland 			/* count active swap areas */
   2190       1.1  dholland 			rval++;
   2191       1.1  dholland 			continue;
   2192       1.1  dholland 		}
   2193       1.1  dholland 		if (swapctl(SWAP_OFF, cp, 0) == -1)
   2194       1.1  dholland 			rval = -1;
   2195       1.1  dholland 	}
   2196       1.1  dholland 
   2197       1.1  dholland     done:
   2198       1.1  dholland 	free(swap);
   2199       1.1  dholland 	return rval;
   2200       1.1  dholland 
   2201       1.1  dholland     bad_swap:
   2202       1.1  dholland 	rval = -1;
   2203       1.1  dholland 	goto done;
   2204       1.1  dholland }
   2205       1.1  dholland 
   2206       1.1  dholland #ifdef HAVE_BOOTXX_xFS
   2207       1.1  dholland char *
   2208      1.30    martin bootxx_name(struct install_partition_desc *install)
   2209       1.1  dholland {
   2210      1.52    martin 	size_t i;
   2211      1.52    martin 	int fstype = -1;
   2212       1.1  dholland 	const char *bootxxname;
   2213       1.1  dholland 	char *bootxx;
   2214       1.1  dholland 
   2215      1.52    martin 	/* find a partition to be mounted as / */
   2216      1.52    martin 	for (i = 0; i < install->num; i++) {
   2217      1.52    martin 		if ((install->infos[i].instflags & PUIINST_MOUNT)
   2218      1.52    martin 		    && strcmp(install->infos[i].mount, "/") == 0) {
   2219      1.52    martin 			fstype = install->infos[i].fs_type;
   2220      1.52    martin 			break;
   2221      1.52    martin 		}
   2222      1.52    martin 	}
   2223      1.52    martin 	if (fstype < 0) {
   2224      1.52    martin 		/* not found? take first root type partition instead */
   2225      1.52    martin 		for (i = 0; i < install->num; i++) {
   2226      1.52    martin 			if (install->infos[i].type == PT_root) {
   2227      1.52    martin 				fstype = install->infos[i].fs_type;
   2228      1.52    martin 				break;
   2229      1.52    martin 			}
   2230      1.52    martin 		}
   2231      1.52    martin 	}
   2232      1.52    martin 
   2233       1.1  dholland 	/* check we have boot code for the root partition type */
   2234       1.1  dholland 	switch (fstype) {
   2235       1.1  dholland #if defined(BOOTXX_FFSV1) || defined(BOOTXX_FFSV2)
   2236       1.1  dholland 	case FS_BSDFFS:
   2237      1.92    martin 		if (install->infos[i].fs_version >= 2) {
   2238       1.1  dholland #ifdef BOOTXX_FFSV2
   2239       1.1  dholland 			bootxxname = BOOTXX_FFSV2;
   2240       1.1  dholland #else
   2241       1.1  dholland 			bootxxname = NULL;
   2242       1.1  dholland #endif
   2243       1.1  dholland 		} else {
   2244       1.1  dholland #ifdef BOOTXX_FFSV1
   2245       1.1  dholland 			bootxxname = BOOTXX_FFSV1;
   2246       1.1  dholland #else
   2247       1.1  dholland 			bootxxname = NULL;
   2248       1.1  dholland #endif
   2249       1.1  dholland 		}
   2250       1.1  dholland 		break;
   2251       1.1  dholland #endif
   2252      1.11  pgoyette #ifdef BOOTXX_LFSV2
   2253       1.1  dholland 	case FS_BSDLFS:
   2254      1.11  pgoyette 		bootxxname = BOOTXX_LFSV2;
   2255       1.1  dholland 		break;
   2256       1.1  dholland #endif
   2257       1.1  dholland 	default:
   2258       1.1  dholland 		bootxxname = NULL;
   2259       1.1  dholland 		break;
   2260       1.1  dholland 	}
   2261       1.1  dholland 
   2262       1.1  dholland 	if (bootxxname == NULL)
   2263       1.1  dholland 		return NULL;
   2264       1.1  dholland 
   2265       1.1  dholland 	asprintf(&bootxx, "%s/%s", BOOTXXDIR, bootxxname);
   2266       1.1  dholland 	return bootxx;
   2267       1.1  dholland }
   2268       1.1  dholland #endif
   2269       1.2    martin 
   2270       1.2    martin /* from dkctl.c */
   2271       1.2    martin static int
   2272       1.2    martin get_dkwedges_sort(const void *a, const void *b)
   2273       1.2    martin {
   2274       1.2    martin 	const struct dkwedge_info *dkwa = a, *dkwb = b;
   2275       1.2    martin 	const daddr_t oa = dkwa->dkw_offset, ob = dkwb->dkw_offset;
   2276       1.2    martin 	return (oa < ob) ? -1 : (oa > ob) ? 1 : 0;
   2277       1.2    martin }
   2278       1.2    martin 
   2279       1.2    martin int
   2280       1.2    martin get_dkwedges(struct dkwedge_info **dkw, const char *diskdev)
   2281       1.2    martin {
   2282       1.2    martin 	struct dkwedge_list dkwl;
   2283       1.2    martin 
   2284       1.2    martin 	*dkw = NULL;
   2285      1.35  christos 	if (!get_wedge_list(diskdev, &dkwl))
   2286       1.2    martin 		return -1;
   2287       1.2    martin 
   2288      1.35  christos 	if (dkwl.dkwl_nwedges > 0 && *dkw != NULL) {
   2289      1.35  christos 		qsort(*dkw, dkwl.dkwl_nwedges, sizeof(**dkw),
   2290      1.35  christos 		    get_dkwedges_sort);
   2291       1.2    martin 	}
   2292       1.2    martin 
   2293       1.2    martin 	return dkwl.dkwl_nwedges;
   2294       1.2    martin }
   2295      1.55    martin 
   2296      1.56    martin #ifndef NO_CLONES
   2297      1.55    martin /*
   2298      1.55    martin  * Helper structures used in the partition select menu
   2299      1.55    martin  */
   2300      1.55    martin struct single_partition {
   2301      1.55    martin 	struct disk_partitions *parts;
   2302      1.55    martin 	part_id id;
   2303      1.55    martin };
   2304      1.55    martin 
   2305      1.55    martin struct sel_menu_data {
   2306      1.55    martin 	struct single_partition *partitions;
   2307      1.55    martin 	struct selected_partition result;
   2308      1.55    martin };
   2309      1.55    martin 
   2310      1.55    martin static int
   2311      1.55    martin select_single_part(menudesc *m, void *arg)
   2312      1.55    martin {
   2313      1.55    martin 	struct sel_menu_data *data = arg;
   2314      1.55    martin 
   2315      1.55    martin 	data->result.parts = data->partitions[m->cursel].parts;
   2316      1.55    martin 	data->result.id = data->partitions[m->cursel].id;
   2317      1.55    martin 
   2318      1.55    martin 	return 1;
   2319      1.55    martin }
   2320      1.55    martin 
   2321      1.55    martin static void
   2322      1.55    martin display_single_part(menudesc *m, int opt, void *arg)
   2323      1.55    martin {
   2324      1.55    martin 	const struct sel_menu_data *data = arg;
   2325      1.55    martin 	struct disk_part_info info;
   2326      1.55    martin 	struct disk_partitions *parts = data->partitions[opt].parts;
   2327      1.55    martin 	part_id id = data->partitions[opt].id;
   2328      1.55    martin 	int l;
   2329      1.55    martin 	const char *desc = NULL;
   2330      1.55    martin 	char line[MENUSTRSIZE*2];
   2331      1.55    martin 
   2332      1.55    martin 	if (!parts->pscheme->get_part_info(parts, id, &info))
   2333      1.55    martin 		return;
   2334      1.55    martin 
   2335      1.55    martin 	if (parts->pscheme->other_partition_identifier != NULL)
   2336      1.55    martin 		desc = parts->pscheme->other_partition_identifier(
   2337      1.55    martin 		    parts, id);
   2338      1.55    martin 
   2339      1.55    martin 	daddr_t start = info.start / sizemult;
   2340      1.55    martin 	daddr_t size = info.size / sizemult;
   2341      1.55    martin 	snprintf(line, sizeof line, "%s [%" PRIu64 " @ %" PRIu64 "]",
   2342      1.55    martin 	    parts->disk, size, start);
   2343      1.55    martin 
   2344      1.55    martin 	if (info.nat_type != NULL) {
   2345      1.55    martin 		strlcat(line, " ", sizeof line);
   2346      1.55    martin 		strlcat(line, info.nat_type->description, sizeof line);
   2347      1.55    martin 	}
   2348      1.55    martin 
   2349      1.55    martin 	if (desc != NULL) {
   2350      1.55    martin 		strlcat(line, ": ", sizeof line);
   2351      1.55    martin 		strlcat(line, desc, sizeof line);
   2352      1.55    martin 	}
   2353      1.55    martin 
   2354      1.55    martin 	l = strlen(line);
   2355      1.55    martin 	if (l >= (m->w))
   2356      1.55    martin 		strcpy(line + (m->w-3), "...");
   2357      1.55    martin 	wprintw(m->mw, "%s", line);
   2358      1.55    martin }
   2359      1.55    martin 
   2360      1.55    martin /*
   2361      1.55    martin  * is the given "test" partitions set used in the selected set?
   2362      1.55    martin  */
   2363      1.55    martin static bool
   2364      1.55    martin selection_has_parts(struct selected_partitions *sel,
   2365      1.55    martin     const struct disk_partitions *test)
   2366      1.55    martin {
   2367      1.55    martin 	size_t i;
   2368      1.55    martin 
   2369      1.55    martin 	for (i = 0; i < sel->num_sel; i++) {
   2370      1.55    martin 		if (sel->selection[i].parts == test)
   2371      1.55    martin 			return true;
   2372      1.55    martin 	}
   2373      1.55    martin 	return false;
   2374      1.55    martin }
   2375      1.55    martin 
   2376      1.55    martin /*
   2377      1.55    martin  * is the given "test" partition in the selected set?
   2378      1.55    martin  */
   2379      1.55    martin static bool
   2380      1.55    martin selection_has_partition(struct selected_partitions *sel,
   2381      1.55    martin     const struct disk_partitions *test, part_id test_id)
   2382      1.55    martin {
   2383      1.55    martin 	size_t i;
   2384      1.55    martin 
   2385      1.55    martin 	for (i = 0; i < sel->num_sel; i++) {
   2386      1.55    martin 		if (sel->selection[i].parts == test &&
   2387      1.55    martin 		    sel->selection[i].id == test_id)
   2388      1.55    martin 			return true;
   2389      1.55    martin 	}
   2390      1.55    martin 	return false;
   2391      1.55    martin }
   2392      1.55    martin 
   2393      1.55    martin /*
   2394      1.55    martin  * let the user select a partition, optionally skipping all partitions
   2395      1.55    martin  * on the "ignore" device
   2396      1.55    martin  */
   2397      1.55    martin static bool
   2398      1.55    martin add_select_partition(struct selected_partitions *res,
   2399      1.55    martin     struct disk_partitions **all_parts, size_t all_cnt)
   2400      1.55    martin {
   2401      1.55    martin 	struct disk_partitions *ps;
   2402      1.55    martin 	struct disk_part_info info;
   2403      1.55    martin 	part_id id;
   2404      1.55    martin 	struct single_partition *partitions, *pp;
   2405      1.55    martin 	struct menu_ent *part_menu_opts, *menup;
   2406      1.55    martin 	size_t n, part_cnt;
   2407      1.55    martin 	int sel_menu;
   2408      1.55    martin 
   2409      1.55    martin 	/*
   2410      1.55    martin 	 * count how many items our menu will have
   2411      1.55    martin 	 */
   2412      1.55    martin 	part_cnt = 0;
   2413      1.55    martin 	for (n = 0; n < all_cnt; n++) {
   2414      1.55    martin 		ps = all_parts[n];
   2415      1.55    martin 		for (id = 0; id < ps->num_part; id++) {
   2416      1.55    martin 			if (selection_has_partition(res, ps, id))
   2417      1.55    martin 				continue;
   2418      1.55    martin 			if (!ps->pscheme->get_part_info(ps, id, &info))
   2419      1.55    martin 				continue;
   2420      1.55    martin 			if (info.flags & (PTI_SEC_CONTAINER|PTI_WHOLE_DISK|
   2421      1.55    martin 			    PTI_PSCHEME_INTERNAL|PTI_RAW_PART))
   2422      1.55    martin 				continue;
   2423      1.55    martin 			part_cnt++;
   2424      1.55    martin 		}
   2425      1.55    martin 	}
   2426      1.55    martin 
   2427      1.55    martin 	/*
   2428      1.55    martin 	 * create a menu from this and let the user
   2429      1.55    martin 	 * select one partition
   2430      1.55    martin 	 */
   2431      1.55    martin 	part_menu_opts = NULL;
   2432      1.55    martin 	partitions = calloc(part_cnt, sizeof *partitions);
   2433      1.55    martin 	if (partitions == NULL)
   2434      1.55    martin 		goto done;
   2435      1.55    martin 	part_menu_opts = calloc(part_cnt, sizeof *part_menu_opts);
   2436      1.55    martin 	if (part_menu_opts == NULL)
   2437      1.55    martin 		goto done;
   2438      1.55    martin 	pp = partitions;
   2439      1.55    martin 	menup = part_menu_opts;
   2440      1.55    martin 	for (n = 0; n < all_cnt; n++) {
   2441      1.55    martin 		ps = all_parts[n];
   2442      1.55    martin 		for (id = 0; id < ps->num_part; id++) {
   2443      1.55    martin 			if (selection_has_partition(res, ps, id))
   2444      1.55    martin 				continue;
   2445      1.55    martin 			if (!ps->pscheme->get_part_info(ps, id, &info))
   2446      1.55    martin 				continue;
   2447      1.55    martin 			if (info.flags & (PTI_SEC_CONTAINER|PTI_WHOLE_DISK|
   2448      1.55    martin 			    PTI_PSCHEME_INTERNAL|PTI_RAW_PART))
   2449      1.55    martin 				continue;
   2450      1.55    martin 			pp->parts = ps;
   2451      1.55    martin 			pp->id = id;
   2452      1.55    martin 			pp++;
   2453      1.55    martin 			menup->opt_action = select_single_part;
   2454      1.55    martin 			menup++;
   2455      1.55    martin 		}
   2456      1.55    martin 	}
   2457      1.55    martin 	sel_menu = new_menu(MSG_select_foreign_part, part_menu_opts, part_cnt,
   2458      1.55    martin 	    3, 3, 0, 60,
   2459      1.55    martin 	    MC_SUBMENU | MC_SCROLL | MC_NOCLEAR,
   2460      1.55    martin 	    NULL, display_single_part, NULL,
   2461      1.57    martin 	    NULL, MSG_exit_menu_generic);
   2462      1.55    martin 	if (sel_menu != -1) {
   2463      1.55    martin 		struct selected_partition *newsels;
   2464      1.55    martin 		struct sel_menu_data data;
   2465      1.55    martin 
   2466      1.55    martin 		memset(&data, 0, sizeof data);
   2467      1.55    martin 		data.partitions = partitions;
   2468      1.55    martin 		process_menu(sel_menu, &data);
   2469      1.55    martin 		free_menu(sel_menu);
   2470      1.55    martin 
   2471      1.55    martin 		if (data.result.parts != NULL) {
   2472      1.55    martin 			newsels = realloc(res->selection,
   2473      1.55    martin 			    sizeof(*res->selection)*(res->num_sel+1));
   2474      1.55    martin 			if (newsels != NULL) {
   2475      1.55    martin 				res->selection = newsels;
   2476      1.55    martin 				newsels += res->num_sel++;
   2477      1.55    martin 				newsels->parts = data.result.parts;
   2478      1.55    martin 				newsels->id = data.result.id;
   2479      1.55    martin 			}
   2480      1.55    martin 		}
   2481      1.55    martin 	}
   2482      1.55    martin 
   2483      1.55    martin 	/*
   2484      1.55    martin 	 * Final cleanup
   2485      1.55    martin 	 */
   2486      1.55    martin done:
   2487      1.55    martin 	free(part_menu_opts);
   2488      1.55    martin 	free(partitions);
   2489      1.55    martin 
   2490      1.55    martin 	return res->num_sel > 0;
   2491      1.55    martin }
   2492      1.55    martin 
   2493      1.55    martin struct part_selection_and_all_parts {
   2494      1.55    martin 	struct selected_partitions *selection;
   2495      1.55    martin 	struct disk_partitions **all_parts;
   2496      1.55    martin 	size_t all_cnt;
   2497      1.55    martin 	char *title;
   2498      1.55    martin 	bool cancelled;
   2499      1.55    martin };
   2500      1.55    martin 
   2501      1.55    martin static int
   2502      1.55    martin toggle_clone_data(struct menudesc *m, void *arg)
   2503      1.55    martin {
   2504      1.55    martin 	struct part_selection_and_all_parts *sel = arg;
   2505      1.55    martin 
   2506      1.55    martin 	sel->selection->with_data = !sel->selection->with_data;
   2507      1.55    martin 	return 0;
   2508      1.55    martin }
   2509      1.55    martin 
   2510      1.55    martin static int
   2511      1.55    martin add_another(struct menudesc *m, void *arg)
   2512      1.55    martin {
   2513      1.55    martin 	struct part_selection_and_all_parts *sel = arg;
   2514      1.55    martin 
   2515      1.55    martin 	add_select_partition(sel->selection, sel->all_parts, sel->all_cnt);
   2516      1.55    martin 	return 0;
   2517      1.55    martin }
   2518      1.55    martin 
   2519      1.55    martin static int
   2520      1.55    martin cancel_clone(struct menudesc *m, void *arg)
   2521      1.72    rillig {
   2522      1.55    martin 	struct part_selection_and_all_parts *sel = arg;
   2523      1.55    martin 
   2524      1.55    martin 	sel->cancelled = true;
   2525      1.55    martin 	return 1;
   2526      1.55    martin }
   2527      1.55    martin 
   2528      1.55    martin static void
   2529      1.55    martin update_sel_part_title(struct part_selection_and_all_parts *sel)
   2530      1.55    martin {
   2531      1.55    martin 	struct disk_part_info info;
   2532      1.55    martin 	char *buf, line[MENUSTRSIZE];
   2533      1.55    martin 	size_t buf_len, i;
   2534      1.55    martin 
   2535      1.55    martin 	buf_len = MENUSTRSIZE * (1+sel->selection->num_sel);
   2536      1.55    martin 	buf = malloc(buf_len);
   2537      1.55    martin 	if (buf == NULL)
   2538      1.55    martin 		return;
   2539      1.55    martin 
   2540      1.55    martin 	strcpy(buf, msg_string(MSG_select_source_hdr));
   2541      1.55    martin 	for (i = 0; i < sel->selection->num_sel; i++) {
   2542      1.55    martin 		struct selected_partition *s =
   2543      1.55    martin 		    &sel->selection->selection[i];
   2544      1.55    martin 		if (!s->parts->pscheme->get_part_info(s->parts, s->id, &info))
   2545      1.55    martin 			continue;
   2546      1.55    martin 		daddr_t start = info.start / sizemult;
   2547      1.55    martin 		daddr_t size = info.size / sizemult;
   2548      1.55    martin 		sprintf(line, "\n  %s [%" PRIu64 " @ %" PRIu64 "] ",
   2549      1.55    martin 		    s->parts->disk, size, start);
   2550      1.55    martin 		if (info.nat_type != NULL)
   2551      1.55    martin 			strlcat(line, info.nat_type->description, sizeof(line));
   2552      1.55    martin 		strlcat(buf, line, buf_len);
   2553      1.55    martin 	}
   2554      1.55    martin 	free(sel->title);
   2555      1.55    martin 	sel->title = buf;
   2556      1.55    martin }
   2557      1.55    martin 
   2558      1.55    martin static void
   2559      1.55    martin post_sel_part(struct menudesc *m, void *arg)
   2560      1.55    martin {
   2561      1.55    martin 	struct part_selection_and_all_parts *sel = arg;
   2562      1.55    martin 
   2563      1.55    martin 	if (m->mw == NULL)
   2564      1.55    martin 		return;
   2565      1.55    martin 	update_sel_part_title(sel);
   2566      1.55    martin 	m->title = sel->title;
   2567      1.55    martin 	m->h = 0;
   2568      1.55    martin 	resize_menu_height(m);
   2569      1.55    martin }
   2570      1.55    martin 
   2571      1.55    martin static void
   2572      1.55    martin fmt_sel_part_line(struct menudesc *m, int i, void *arg)
   2573      1.55    martin {
   2574      1.55    martin 	struct part_selection_and_all_parts *sel = arg;
   2575      1.55    martin 
   2576      1.55    martin 	wprintw(m->mw, "%s: %s", msg_string(MSG_clone_with_data),
   2577      1.55    martin 	    sel->selection->with_data ?
   2578      1.55    martin 		msg_string(MSG_Yes) :
   2579      1.55    martin 		 msg_string(MSG_No));
   2580      1.55    martin }
   2581      1.55    martin 
   2582      1.55    martin bool
   2583      1.55    martin select_partitions(struct selected_partitions *res,
   2584      1.55    martin     const struct disk_partitions *ignore)
   2585      1.55    martin {
   2586      1.55    martin 	struct disk_desc disks[MAX_DISKS];
   2587      1.55    martin 	struct disk_partitions *ps;
   2588      1.55    martin 	struct part_selection_and_all_parts data;
   2589      1.55    martin 	struct pm_devs *i;
   2590      1.55    martin 	size_t j;
   2591      1.55    martin 	int cnt, n, m;
   2592      1.55    martin 	static menu_ent men[] = {
   2593      1.55    martin 		{ .opt_name = MSG_select_source_add,
   2594      1.55    martin 		  .opt_action = add_another },
   2595      1.55    martin 		{ .opt_action = toggle_clone_data },
   2596      1.55    martin 		{ .opt_name = MSG_cancel, .opt_action = cancel_clone },
   2597      1.55    martin 	};
   2598      1.55    martin 
   2599      1.55    martin 	memset(res, 0, sizeof *res);
   2600      1.55    martin 	memset(&data, 0, sizeof data);
   2601      1.55    martin 	data.selection = res;
   2602      1.55    martin 
   2603      1.55    martin 	/*
   2604      1.55    martin 	 * collect all available partition sets
   2605      1.55    martin 	 */
   2606      1.55    martin 	data.all_cnt = 0;
   2607      1.55    martin 	if (SLIST_EMPTY(&pm_head)) {
   2608      1.55    martin 		cnt = get_disks(disks, false);
   2609      1.55    martin 		if (cnt <= 0)
   2610      1.55    martin 			return false;
   2611      1.55    martin 
   2612      1.55    martin 		/*
   2613      1.55    martin 		 * allocate two slots for each disk (primary/secondary)
   2614      1.55    martin 		 */
   2615      1.55    martin 		data.all_parts = calloc(2*cnt, sizeof *data.all_parts);
   2616      1.55    martin 		if (data.all_parts == NULL)
   2617      1.55    martin 			return false;
   2618      1.55    martin 
   2619      1.55    martin 		for (n = 0; n < cnt; n++) {
   2620      1.55    martin 			if (ignore != NULL &&
   2621      1.55    martin 			    strcmp(disks[n].dd_name, ignore->disk) == 0)
   2622      1.55    martin 				continue;
   2623      1.55    martin 
   2624      1.55    martin 			ps = partitions_read_disk(disks[n].dd_name,
   2625      1.62    martin 			    disks[n].dd_totsec,
   2626      1.62    martin 			    disks[n].dd_secsize,
   2627      1.62    martin 			    disks[n].dd_no_mbr);
   2628      1.55    martin 			if (ps == NULL)
   2629      1.55    martin 				continue;
   2630      1.55    martin 			data.all_parts[data.all_cnt++] = ps;
   2631      1.55    martin 			ps = get_inner_parts(ps);
   2632      1.55    martin 			if (ps == NULL)
   2633      1.55    martin 				continue;
   2634      1.55    martin 			data.all_parts[data.all_cnt++] = ps;
   2635      1.55    martin 		}
   2636      1.55    martin 		if (data.all_cnt > 0)
   2637      1.55    martin 			res->free_parts = true;
   2638      1.55    martin 	} else {
   2639      1.55    martin 		cnt = 0;
   2640      1.55    martin 		SLIST_FOREACH(i, &pm_head, l)
   2641      1.55    martin 			cnt++;
   2642      1.55    martin 
   2643      1.55    martin 		data.all_parts = calloc(cnt, sizeof *data.all_parts);
   2644      1.55    martin 		if (data.all_parts == NULL)
   2645      1.55    martin 			return false;
   2646      1.55    martin 
   2647      1.55    martin 		SLIST_FOREACH(i, &pm_head, l) {
   2648      1.55    martin 			if (i->parts == NULL)
   2649      1.55    martin 				continue;
   2650      1.55    martin 			if (i->parts == ignore)
   2651      1.55    martin 				continue;
   2652      1.55    martin 			data.all_parts[data.all_cnt++] = i->parts;
   2653      1.55    martin 		}
   2654      1.55    martin 	}
   2655      1.55    martin 
   2656      1.55    martin 	if (!add_select_partition(res, data.all_parts, data.all_cnt))
   2657      1.55    martin 		goto fail;
   2658      1.55    martin 
   2659      1.55    martin 	/* loop with menu */
   2660      1.55    martin 	update_sel_part_title(&data);
   2661      1.55    martin 	m = new_menu(data.title, men, __arraycount(men), 3, 2, 0, 65, MC_SCROLL,
   2662      1.57    martin 	    post_sel_part, fmt_sel_part_line, NULL, NULL, MSG_clone_src_done);
   2663      1.55    martin 	process_menu(m, &data);
   2664      1.55    martin 	free(data.title);
   2665      1.55    martin 	if (res->num_sel == 0)
   2666      1.55    martin 		goto fail;
   2667      1.55    martin 
   2668      1.55    martin 	/* cleanup */
   2669      1.55    martin 	if (res->free_parts) {
   2670      1.55    martin 		for (j = 0; j < data.all_cnt; j++) {
   2671      1.55    martin 			if (selection_has_parts(res, data.all_parts[j]))
   2672      1.55    martin 				continue;
   2673      1.55    martin 			if (data.all_parts[j]->parent != NULL)
   2674      1.55    martin 				continue;
   2675      1.55    martin 			data.all_parts[j]->pscheme->free(data.all_parts[j]);
   2676      1.55    martin 		}
   2677      1.55    martin 	}
   2678      1.55    martin 	free(data.all_parts);
   2679      1.55    martin 	return true;
   2680      1.55    martin 
   2681      1.55    martin fail:
   2682      1.55    martin 	if (res->free_parts) {
   2683      1.55    martin 		for (j = 0; j < data.all_cnt; j++) {
   2684      1.55    martin 			if (data.all_parts[j]->parent != NULL)
   2685      1.55    martin 				continue;
   2686      1.55    martin 			data.all_parts[j]->pscheme->free(data.all_parts[j]);
   2687      1.55    martin 		}
   2688      1.55    martin 	}
   2689      1.55    martin 	free(data.all_parts);
   2690      1.55    martin 	return false;
   2691      1.55    martin }
   2692      1.55    martin 
   2693      1.55    martin void
   2694      1.55    martin free_selected_partitions(struct selected_partitions *selected)
   2695      1.55    martin {
   2696      1.55    martin 	size_t i;
   2697      1.55    martin 	struct disk_partitions *parts;
   2698      1.55    martin 
   2699      1.55    martin 	if (!selected->free_parts)
   2700      1.55    martin 		return;
   2701      1.55    martin 
   2702      1.55    martin 	for (i = 0; i < selected->num_sel; i++) {
   2703      1.55    martin 		parts = selected->selection[i].parts;
   2704      1.55    martin 
   2705      1.55    martin 		/* remove from list before testing for other instances */
   2706      1.55    martin 		selected->selection[i].parts = NULL;
   2707      1.55    martin 
   2708      1.74    andvar 		/* if this is the secondary partition set, the parent owns it */
   2709      1.55    martin 		if (parts->parent != NULL)
   2710      1.55    martin 			continue;
   2711      1.55    martin 
   2712      1.55    martin 		/* only free once (we use the last one) */
   2713      1.55    martin 		if (selection_has_parts(selected, parts))
   2714      1.55    martin 			continue;
   2715      1.55    martin 		parts->pscheme->free(parts);
   2716      1.55    martin 	}
   2717      1.55    martin 	free(selected->selection);
   2718      1.55    martin }
   2719      1.55    martin 
   2720      1.55    martin daddr_t
   2721      1.55    martin selected_parts_size(struct selected_partitions *selected)
   2722      1.55    martin {
   2723      1.55    martin 	struct disk_part_info info;
   2724      1.55    martin 	size_t i;
   2725      1.55    martin 	daddr_t s = 0;
   2726      1.55    martin 
   2727      1.55    martin 	for (i = 0; i < selected->num_sel; i++) {
   2728      1.55    martin 		if (!selected->selection[i].parts->pscheme->get_part_info(
   2729      1.55    martin 		    selected->selection[i].parts,
   2730      1.55    martin 		    selected->selection[i].id, &info))
   2731      1.55    martin 			continue;
   2732      1.55    martin 		s += info.size;
   2733      1.55    martin 	}
   2734      1.55    martin 
   2735      1.55    martin 	return s;
   2736      1.55    martin }
   2737      1.55    martin 
   2738      1.55    martin int
   2739      1.55    martin clone_target_select(menudesc *m, void *arg)
   2740      1.55    martin {
   2741      1.55    martin 	struct clone_target_menu_data *data = arg;
   2742      1.55    martin 
   2743      1.55    martin 	data->res = m->cursel;
   2744      1.55    martin 	return 1;
   2745      1.55    martin }
   2746      1.55    martin 
   2747      1.55    martin bool
   2748      1.55    martin clone_partition_data(struct disk_partitions *dest_parts, part_id did,
   2749      1.55    martin     struct disk_partitions *src_parts, part_id sid)
   2750      1.55    martin {
   2751      1.55    martin 	char src_dev[MAXPATHLEN], target_dev[MAXPATHLEN];
   2752      1.55    martin 
   2753      1.55    martin 	if (!src_parts->pscheme->get_part_device(
   2754      1.55    martin 	    src_parts, sid, src_dev, sizeof src_dev, NULL,
   2755      1.59    martin 	    raw_dev_name, true, true))
   2756      1.55    martin 		return false;
   2757      1.55    martin 	if (!dest_parts->pscheme->get_part_device(
   2758      1.55    martin 	    dest_parts, did, target_dev, sizeof target_dev, NULL,
   2759      1.59    martin 	    raw_dev_name, true, true))
   2760      1.55    martin 		return false;
   2761      1.55    martin 
   2762      1.72    rillig 	return run_program(RUN_DISPLAY | RUN_PROGRESS,
   2763      1.55    martin 	    "progress -f %s -b 1m dd bs=1m of=%s",
   2764      1.55    martin 	    src_dev, target_dev) == 0;
   2765      1.55    martin }
   2766      1.56    martin #endif
   2767      1.56    martin 
   2768