Home | History | Annotate | Line # | Download | only in sysinst
disks.c revision 1.13.12.2
      1  1.13.12.2  pgoyette /*	$NetBSD: disks.c,v 1.13.12.2 2018/06/25 07:26:12 pgoyette Exp $ */
      2        1.1  dholland 
      3        1.1  dholland /*
      4        1.1  dholland  * Copyright 1997 Piermont Information Systems Inc.
      5        1.1  dholland  * All rights reserved.
      6        1.1  dholland  *
      7        1.1  dholland  * Written by Philip A. Nelson for Piermont Information Systems Inc.
      8        1.1  dholland  *
      9        1.1  dholland  * Redistribution and use in source and binary forms, with or without
     10        1.1  dholland  * modification, are permitted provided that the following conditions
     11        1.1  dholland  * are met:
     12        1.1  dholland  * 1. Redistributions of source code must retain the above copyright
     13        1.1  dholland  *    notice, this list of conditions and the following disclaimer.
     14        1.1  dholland  * 2. Redistributions in binary form must reproduce the above copyright
     15        1.1  dholland  *    notice, this list of conditions and the following disclaimer in the
     16        1.1  dholland  *    documentation and/or other materials provided with the distribution.
     17        1.1  dholland  * 3. The name of Piermont Information Systems Inc. may not be used to endorse
     18        1.1  dholland  *    or promote products derived from this software without specific prior
     19        1.1  dholland  *    written permission.
     20        1.1  dholland  *
     21        1.1  dholland  * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
     22        1.1  dholland  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23        1.1  dholland  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24        1.1  dholland  * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
     25        1.1  dholland  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26        1.1  dholland  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27        1.1  dholland  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28        1.1  dholland  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29        1.1  dholland  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30        1.1  dholland  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     31        1.1  dholland  * THE POSSIBILITY OF SUCH DAMAGE.
     32        1.1  dholland  *
     33        1.1  dholland  */
     34        1.1  dholland 
     35        1.1  dholland /* disks.c -- routines to deal with finding disks and labeling disks. */
     36        1.1  dholland 
     37        1.1  dholland 
     38        1.1  dholland #include <errno.h>
     39        1.1  dholland #include <stdio.h>
     40        1.1  dholland #include <stdlib.h>
     41        1.1  dholland #include <unistd.h>
     42        1.1  dholland #include <fcntl.h>
     43        1.1  dholland #include <util.h>
     44        1.2    martin #include <uuid.h>
     45        1.1  dholland 
     46        1.1  dholland #include <sys/param.h>
     47        1.1  dholland #include <sys/sysctl.h>
     48        1.1  dholland #include <sys/swap.h>
     49        1.1  dholland #include <ufs/ufs/dinode.h>
     50        1.1  dholland #include <ufs/ffs/fs.h>
     51        1.1  dholland #define FSTYPENAMES
     52        1.1  dholland #include <sys/disklabel.h>
     53        1.2    martin #include <sys/disklabel_gpt.h>
     54        1.1  dholland 
     55        1.1  dholland #include <dev/scsipi/scsipi_all.h>
     56        1.1  dholland #include <sys/scsiio.h>
     57        1.1  dholland 
     58        1.1  dholland #include <dev/ata/atareg.h>
     59        1.1  dholland #include <sys/ataio.h>
     60        1.1  dholland 
     61        1.1  dholland #include "defs.h"
     62        1.1  dholland #include "md.h"
     63        1.1  dholland #include "msg_defs.h"
     64        1.1  dholland #include "menu_defs.h"
     65        1.1  dholland #include "txtwalk.h"
     66        1.1  dholland 
     67        1.1  dholland /* Disk descriptions */
     68        1.1  dholland struct disk_desc {
     69        1.1  dholland 	char	dd_name[SSTRSIZE];
     70        1.1  dholland 	char	dd_descr[70];
     71  1.13.12.2  pgoyette 	bool	dd_no_mbr, dd_no_part;
     72        1.1  dholland 	uint	dd_cyl;
     73        1.1  dholland 	uint	dd_head;
     74        1.1  dholland 	uint	dd_sec;
     75        1.1  dholland 	uint	dd_secsize;
     76        1.1  dholland 	uint	dd_totsec;
     77        1.1  dholland };
     78        1.1  dholland 
     79        1.2    martin /* gpt(8) use different filesystem names.
     80        1.2    martin    So, we cant use ./common/lib/libutil/getfstypename.c */
     81        1.2    martin struct gptfs_t {
     82        1.2    martin     const char *name;
     83        1.2    martin     int id;
     84        1.2    martin     uuid_t uuid;
     85        1.2    martin };
     86        1.2    martin static const struct gptfs_t gpt_filesystems[] = {
     87        1.2    martin     { "swap", FS_SWAP, GPT_ENT_TYPE_NETBSD_SWAP, },
     88        1.2    martin     { "ffs", FS_BSDFFS, GPT_ENT_TYPE_NETBSD_FFS, },
     89        1.2    martin     { "lfs", FS_BSDLFS, GPT_ENT_TYPE_NETBSD_LFS, },
     90        1.2    martin     { "linux", FS_EX2FS, GPT_ENT_TYPE_LINUX_DATA, },
     91        1.2    martin     { "windows,", FS_MSDOS, GPT_ENT_TYPE_MS_BASIC_DATA, },
     92        1.2    martin     { "hfs", FS_HFS, GPT_ENT_TYPE_APPLE_HFS, },
     93        1.2    martin     { "ufs", FS_OTHER, GPT_ENT_TYPE_APPLE_UFS, },
     94        1.2    martin     { "ccd", FS_CCD, GPT_ENT_TYPE_NETBSD_CCD, },
     95        1.2    martin     { "raid", FS_RAID, GPT_ENT_TYPE_NETBSD_RAIDFRAME, },
     96        1.2    martin     { "cgd", FS_CGD, GPT_ENT_TYPE_NETBSD_CGD, },
     97        1.2    martin     { "efi", FS_OTHER, GPT_ENT_TYPE_EFI, },
     98        1.2    martin     { "bios", FS_OTHER, GPT_ENT_TYPE_BIOS, },
     99        1.2    martin     { NULL, -1, GPT_ENT_TYPE_UNUSED, },
    100        1.2    martin };
    101        1.2    martin 
    102        1.1  dholland /* Local prototypes */
    103        1.1  dholland static int foundffs(struct data *, size_t);
    104        1.1  dholland #ifdef USE_SYSVBFS
    105        1.1  dholland static int foundsysvbfs(struct data *, size_t);
    106        1.1  dholland #endif
    107  1.13.12.2  pgoyette static int fsck_preen(const char *, int, const char *, bool silent);
    108        1.1  dholland static void fixsb(const char *, const char *, char);
    109        1.6    martin static bool is_gpt(const char *);
    110        1.2    martin static int incoregpt(pm_devs_t *, partinfo *);
    111        1.1  dholland 
    112        1.1  dholland #ifndef DISK_NAMES
    113        1.1  dholland #define DISK_NAMES "wd", "sd", "ld", "raid"
    114        1.1  dholland #endif
    115        1.1  dholland 
    116  1.13.12.2  pgoyette static const char *disk_names[] = { DISK_NAMES,
    117  1.13.12.2  pgoyette 				    "vnd", "cgd", "dk:no_part", NULL };
    118        1.1  dholland 
    119        1.1  dholland static bool tmpfs_on_var_shm(void);
    120        1.1  dholland 
    121        1.1  dholland const char *
    122        1.1  dholland getfslabelname(uint8_t f)
    123        1.1  dholland {
    124        1.1  dholland 	if (f >= __arraycount(fstypenames) || fstypenames[f] == NULL)
    125        1.1  dholland 		return "invalid";
    126        1.1  dholland 	return fstypenames[f];
    127        1.1  dholland }
    128        1.1  dholland 
    129        1.1  dholland /*
    130        1.1  dholland  * Decide wether we want to mount a tmpfs on /var/shm: we do this always
    131        1.1  dholland  * when the machine has more than 16 MB of user memory. On smaller machines,
    132        1.1  dholland  * shm_open() and friends will not perform well anyway.
    133        1.1  dholland  */
    134        1.1  dholland static bool
    135        1.1  dholland tmpfs_on_var_shm()
    136        1.1  dholland {
    137        1.1  dholland 	uint64_t ram;
    138        1.1  dholland 	size_t len;
    139        1.1  dholland 
    140        1.1  dholland 	len = sizeof(ram);
    141        1.1  dholland 	if (sysctlbyname("hw.usermem64", &ram, &len, NULL, 0))
    142        1.1  dholland 		return false;
    143        1.1  dholland 
    144        1.1  dholland 	return ram > 16UL*1024UL*1024UL;
    145        1.1  dholland }
    146        1.1  dholland 
    147        1.1  dholland /* from src/sbin/atactl/atactl.c
    148        1.1  dholland  * extract_string: copy a block of bytes out of ataparams and make
    149        1.1  dholland  * a proper string out of it, truncating trailing spaces and preserving
    150        1.1  dholland  * strict typing. And also, not doing unaligned accesses.
    151        1.1  dholland  */
    152        1.1  dholland static void
    153        1.1  dholland ata_extract_string(char *buf, size_t bufmax,
    154        1.1  dholland 		   uint8_t *bytes, unsigned numbytes,
    155        1.1  dholland 		   int needswap)
    156        1.1  dholland {
    157        1.1  dholland 	unsigned i;
    158        1.1  dholland 	size_t j;
    159        1.1  dholland 	unsigned char ch1, ch2;
    160        1.1  dholland 
    161        1.1  dholland 	for (i = 0, j = 0; i < numbytes; i += 2) {
    162        1.1  dholland 		ch1 = bytes[i];
    163        1.1  dholland 		ch2 = bytes[i+1];
    164        1.1  dholland 		if (needswap && j < bufmax-1) {
    165        1.1  dholland 			buf[j++] = ch2;
    166        1.1  dholland 		}
    167        1.1  dholland 		if (j < bufmax-1) {
    168        1.1  dholland 			buf[j++] = ch1;
    169        1.1  dholland 		}
    170        1.1  dholland 		if (!needswap && j < bufmax-1) {
    171        1.1  dholland 			buf[j++] = ch2;
    172        1.1  dholland 		}
    173        1.1  dholland 	}
    174        1.1  dholland 	while (j > 0 && buf[j-1] == ' ') {
    175        1.1  dholland 		j--;
    176        1.1  dholland 	}
    177        1.1  dholland 	buf[j] = '\0';
    178        1.1  dholland }
    179        1.1  dholland 
    180        1.1  dholland /*
    181        1.1  dholland  * from src/sbin/scsictl/scsi_subr.c
    182        1.1  dholland  */
    183        1.1  dholland #define STRVIS_ISWHITE(x) ((x) == ' ' || (x) == '\0' || (x) == (u_char)'\377')
    184        1.1  dholland 
    185        1.1  dholland static void
    186        1.1  dholland scsi_strvis(char *sdst, size_t dlen, const char *ssrc, size_t slen)
    187        1.1  dholland {
    188        1.1  dholland 	u_char *dst = (u_char *)sdst;
    189        1.1  dholland 	const u_char *src = (const u_char *)ssrc;
    190        1.1  dholland 
    191        1.1  dholland 	/* Trim leading and trailing blanks and NULs. */
    192        1.1  dholland 	while (slen > 0 && STRVIS_ISWHITE(src[0]))
    193        1.1  dholland 		++src, --slen;
    194        1.1  dholland 	while (slen > 0 && STRVIS_ISWHITE(src[slen - 1]))
    195        1.1  dholland 		--slen;
    196        1.1  dholland 
    197        1.1  dholland 	while (slen > 0) {
    198        1.1  dholland 		if (*src < 0x20 || *src >= 0x80) {
    199        1.1  dholland 			/* non-printable characters */
    200        1.1  dholland 			dlen -= 4;
    201        1.1  dholland 			if (dlen < 1)
    202        1.1  dholland 				break;
    203        1.1  dholland 			*dst++ = '\\';
    204        1.1  dholland 			*dst++ = ((*src & 0300) >> 6) + '0';
    205        1.1  dholland 			*dst++ = ((*src & 0070) >> 3) + '0';
    206        1.1  dholland 			*dst++ = ((*src & 0007) >> 0) + '0';
    207        1.1  dholland 		} else if (*src == '\\') {
    208        1.1  dholland 			/* quote characters */
    209        1.1  dholland 			dlen -= 2;
    210        1.1  dholland 			if (dlen < 1)
    211        1.1  dholland 				break;
    212        1.1  dholland 			*dst++ = '\\';
    213        1.1  dholland 			*dst++ = '\\';
    214        1.1  dholland 		} else {
    215        1.1  dholland 			/* normal characters */
    216        1.1  dholland 			if (--dlen < 1)
    217        1.1  dholland 				break;
    218        1.1  dholland 			*dst++ = *src;
    219        1.1  dholland 		}
    220        1.1  dholland 		++src, --slen;
    221        1.1  dholland 	}
    222        1.1  dholland 
    223        1.1  dholland 	*dst++ = 0;
    224        1.1  dholland }
    225        1.1  dholland 
    226        1.1  dholland 
    227        1.1  dholland static int
    228        1.1  dholland get_descr_scsi(struct disk_desc *dd, int fd)
    229        1.1  dholland {
    230        1.1  dholland 	struct scsipi_inquiry_data inqbuf;
    231        1.1  dholland 	struct scsipi_inquiry cmd;
    232        1.1  dholland 	scsireq_t req;
    233        1.1  dholland         /* x4 in case every character is escaped, +1 for NUL. */
    234        1.1  dholland 	char vendor[(sizeof(inqbuf.vendor) * 4) + 1],
    235        1.1  dholland 	     product[(sizeof(inqbuf.product) * 4) + 1],
    236        1.1  dholland 	     revision[(sizeof(inqbuf.revision) * 4) + 1];
    237        1.1  dholland 	char size[5];
    238        1.1  dholland 	int error;
    239        1.1  dholland 
    240        1.1  dholland 	memset(&inqbuf, 0, sizeof(inqbuf));
    241        1.1  dholland 	memset(&cmd, 0, sizeof(cmd));
    242        1.1  dholland 	memset(&req, 0, sizeof(req));
    243        1.1  dholland 
    244        1.1  dholland 	cmd.opcode = INQUIRY;
    245        1.1  dholland 	cmd.length = sizeof(inqbuf);
    246        1.1  dholland 	memcpy(req.cmd, &cmd, sizeof(cmd));
    247        1.1  dholland 	req.cmdlen = sizeof(cmd);
    248        1.1  dholland 	req.databuf = &inqbuf;
    249        1.1  dholland 	req.datalen = sizeof(inqbuf);
    250        1.1  dholland 	req.timeout = 10000;
    251        1.1  dholland 	req.flags = SCCMD_READ;
    252        1.1  dholland 	req.senselen = SENSEBUFLEN;
    253        1.1  dholland 
    254        1.1  dholland 	error = ioctl(fd, SCIOCCOMMAND, &req);
    255        1.1  dholland 	if (error == -1 || req.retsts != SCCMD_OK)
    256        1.1  dholland 		return 0;
    257        1.1  dholland 
    258        1.1  dholland 	scsi_strvis(vendor, sizeof(vendor), inqbuf.vendor,
    259        1.1  dholland 	    sizeof(inqbuf.vendor));
    260        1.1  dholland 	scsi_strvis(product, sizeof(product), inqbuf.product,
    261        1.1  dholland 	    sizeof(inqbuf.product));
    262        1.1  dholland 	scsi_strvis(revision, sizeof(revision), inqbuf.revision,
    263        1.1  dholland 	    sizeof(inqbuf.revision));
    264        1.1  dholland 
    265        1.1  dholland 	humanize_number(size, sizeof(size),
    266        1.1  dholland 	    (uint64_t)dd->dd_secsize * (uint64_t)dd->dd_totsec,
    267        1.1  dholland 	    "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
    268        1.1  dholland 
    269        1.1  dholland 	snprintf(dd->dd_descr, sizeof(dd->dd_descr),
    270        1.1  dholland 	    "%s (%s, %s %s)",
    271        1.1  dholland 	    dd->dd_name, size, vendor, product);
    272        1.1  dholland 
    273        1.1  dholland 	return 1;
    274        1.1  dholland }
    275        1.1  dholland 
    276        1.1  dholland static int
    277        1.1  dholland get_descr_ata(struct disk_desc *dd, int fd)
    278        1.1  dholland {
    279        1.1  dholland 	struct atareq req;
    280        1.1  dholland 	static union {
    281        1.1  dholland 		unsigned char inbuf[DEV_BSIZE];
    282        1.1  dholland 		struct ataparams inqbuf;
    283        1.1  dholland 	} inbuf;
    284        1.1  dholland 	struct ataparams *inqbuf = &inbuf.inqbuf;
    285        1.1  dholland 	char model[sizeof(inqbuf->atap_model)+1];
    286        1.1  dholland 	char size[5];
    287        1.1  dholland 	int error, needswap = 0;
    288        1.1  dholland 
    289        1.1  dholland 	memset(&inbuf, 0, sizeof(inbuf));
    290        1.1  dholland 	memset(&req, 0, sizeof(req));
    291        1.1  dholland 
    292        1.1  dholland 	req.flags = ATACMD_READ;
    293        1.1  dholland 	req.command = WDCC_IDENTIFY;
    294        1.1  dholland 	req.databuf = (void *)&inbuf;
    295        1.1  dholland 	req.datalen = sizeof(inbuf);
    296        1.1  dholland 	req.timeout = 1000;
    297        1.1  dholland 
    298        1.1  dholland 	error = ioctl(fd, ATAIOCCOMMAND, &req);
    299        1.1  dholland 	if (error == -1 || req.retsts != ATACMD_OK)
    300        1.1  dholland 		return 0;
    301        1.1  dholland 
    302        1.1  dholland #if BYTE_ORDER == LITTLE_ENDIAN
    303        1.1  dholland 	/*
    304        1.1  dholland 	 * On little endian machines, we need to shuffle the string
    305        1.1  dholland 	 * byte order.  However, we don't have to do this for NEC or
    306        1.1  dholland 	 * Mitsumi ATAPI devices
    307        1.1  dholland 	 */
    308        1.1  dholland 
    309        1.1  dholland 	if (!(inqbuf->atap_config != WDC_CFG_CFA_MAGIC &&
    310        1.1  dholland 	      (inqbuf->atap_config & WDC_CFG_ATAPI) &&
    311        1.1  dholland 	      ((inqbuf->atap_model[0] == 'N' &&
    312       1.10     isaki 	        inqbuf->atap_model[1] == 'E') ||
    313        1.1  dholland 	       (inqbuf->atap_model[0] == 'F' &&
    314       1.10     isaki 	        inqbuf->atap_model[1] == 'X')))) {
    315        1.1  dholland 		needswap = 1;
    316        1.1  dholland 	}
    317        1.1  dholland #endif
    318        1.1  dholland 
    319        1.1  dholland 	ata_extract_string(model, sizeof(model),
    320        1.1  dholland 	    inqbuf->atap_model, sizeof(inqbuf->atap_model), needswap);
    321        1.1  dholland 	humanize_number(size, sizeof(size),
    322        1.1  dholland 	    (uint64_t)dd->dd_secsize * (uint64_t)dd->dd_totsec,
    323        1.1  dholland 	    "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
    324        1.1  dholland 
    325        1.1  dholland 	snprintf(dd->dd_descr, sizeof(dd->dd_descr), "%s (%s, %s)",
    326        1.1  dholland 	    dd->dd_name, size, model);
    327        1.1  dholland 
    328        1.1  dholland 	return 1;
    329        1.1  dholland }
    330        1.1  dholland 
    331        1.1  dholland static void
    332        1.1  dholland get_descr(struct disk_desc *dd)
    333        1.1  dholland {
    334        1.1  dholland 	char diskpath[MAXPATHLEN];
    335        1.1  dholland 	int fd = -1;
    336        1.1  dholland 
    337        1.1  dholland 	fd = opendisk(dd->dd_name, O_RDONLY, diskpath, sizeof(diskpath), 0);
    338        1.1  dholland 	if (fd < 0)
    339        1.1  dholland 		goto done;
    340        1.1  dholland 
    341        1.1  dholland 	dd->dd_descr[0] = '\0';
    342        1.1  dholland 
    343        1.1  dholland 	/* try ATA */
    344        1.1  dholland 	if (get_descr_ata(dd, fd))
    345        1.1  dholland 		goto done;
    346        1.1  dholland 	/* try SCSI */
    347        1.1  dholland 	if (get_descr_scsi(dd, fd))
    348        1.1  dholland 		goto done;
    349        1.2    martin 	/* XXX: get description from raid, cgd, vnd... */
    350        1.1  dholland 
    351        1.1  dholland done:
    352        1.1  dholland 	if (fd >= 0)
    353        1.1  dholland 		close(fd);
    354        1.1  dholland 	if (strlen(dd->dd_descr) == 0)
    355        1.1  dholland 		strcpy(dd->dd_descr, dd->dd_name);
    356        1.1  dholland }
    357        1.1  dholland 
    358        1.1  dholland /* disknames - contains device names without partition letters
    359        1.1  dholland  * cdrom_devices - contains devices including partition letters
    360        1.1  dholland  * returns the first entry in hw.disknames matching a cdrom_device, or
    361        1.1  dholland  * first entry on error or no match
    362        1.1  dholland  */
    363        1.1  dholland const char *
    364        1.1  dholland get_default_cdrom(void)
    365        1.1  dholland {
    366        1.1  dholland 	static const char *cdrom_devices[] = { CD_NAMES, 0};
    367        1.1  dholland 	static const char mib_name[] = "hw.disknames";
    368        1.1  dholland 	size_t len;
    369        1.1  dholland 	char *disknames;
    370        1.1  dholland 	char *last;
    371        1.1  dholland 	char *name;
    372        1.1  dholland 	const char **arg;
    373        1.1  dholland 	const char *cd_dev;
    374        1.1  dholland 
    375        1.1  dholland 	/* On error just use first entry in cdrom_devices */
    376        1.1  dholland 	if (sysctlbyname(mib_name, NULL, &len, NULL, 0) == -1)
    377        1.1  dholland 		return cdrom_devices[0];
    378        1.1  dholland 	if ((disknames = malloc(len + 2)) == 0) /* skip on malloc fail */
    379        1.1  dholland 		return cdrom_devices[0];
    380        1.1  dholland 
    381        1.1  dholland 	(void)sysctlbyname(mib_name, disknames, &len, NULL, 0);
    382       1.10     isaki 	for ((name = strtok_r(disknames, " ", &last)); name;
    383        1.1  dholland 	    (name = strtok_r(NULL, " ", &last))) {
    384        1.1  dholland 		for (arg = cdrom_devices; *arg; ++arg) {
    385        1.1  dholland 			cd_dev = *arg;
    386        1.1  dholland 			/* skip unit and partition */
    387        1.1  dholland 			if (strncmp(cd_dev, name, strlen(cd_dev) - 2) != 0)
    388        1.1  dholland 				continue;
    389        1.1  dholland 			if (name != disknames)
    390        1.1  dholland 				strcpy(disknames, name);
    391        1.1  dholland 			strcat(disknames, "a");
    392        1.1  dholland 			/* XXX: leaks, but so what? */
    393        1.1  dholland 			return disknames;
    394        1.1  dholland 		}
    395        1.1  dholland 	}
    396        1.1  dholland 	free(disknames);
    397        1.1  dholland 	return cdrom_devices[0];
    398        1.1  dholland }
    399        1.1  dholland 
    400  1.13.12.2  pgoyette static void
    401  1.13.12.2  pgoyette get_wedge_descr(struct disk_desc *dd)
    402  1.13.12.2  pgoyette {
    403  1.13.12.2  pgoyette 	struct dkwedge_info dkw;
    404  1.13.12.2  pgoyette 	char buf[MAXPATHLEN];
    405  1.13.12.2  pgoyette 	int fd;
    406  1.13.12.2  pgoyette 
    407  1.13.12.2  pgoyette 	fd = opendisk(dd->dd_name, O_RDONLY, buf, sizeof(buf), 0);
    408  1.13.12.2  pgoyette 	if (fd == -1)
    409  1.13.12.2  pgoyette 		return;
    410  1.13.12.2  pgoyette 
    411  1.13.12.2  pgoyette 	if (ioctl(fd, DIOCGWEDGEINFO, &dkw) == 0) {
    412  1.13.12.2  pgoyette 		fprintf(stderr, "device %s\n", dd->dd_name);
    413  1.13.12.2  pgoyette 		sprintf(dd->dd_descr, "%s (%s@%s)",
    414  1.13.12.2  pgoyette 		    dkw.dkw_wname, dkw.dkw_devname, dkw.dkw_parent);
    415  1.13.12.2  pgoyette 	}
    416  1.13.12.2  pgoyette 	close(fd);
    417  1.13.12.2  pgoyette }
    418  1.13.12.2  pgoyette 
    419  1.13.12.2  pgoyette static bool
    420  1.13.12.2  pgoyette get_name_and_parent(const char *dev, char *name, char *parent)
    421  1.13.12.2  pgoyette {
    422  1.13.12.2  pgoyette 	struct dkwedge_info dkw;
    423  1.13.12.2  pgoyette 	char buf[MAXPATHLEN];
    424  1.13.12.2  pgoyette 	int fd;
    425  1.13.12.2  pgoyette 	bool res = false;
    426  1.13.12.2  pgoyette 
    427  1.13.12.2  pgoyette 	fd = opendisk(dev, O_RDONLY, buf, sizeof(buf), 0);
    428  1.13.12.2  pgoyette 	if (fd == -1)
    429  1.13.12.2  pgoyette 		return false;
    430  1.13.12.2  pgoyette 
    431  1.13.12.2  pgoyette 	if (ioctl(fd, DIOCGWEDGEINFO, &dkw) == 0) {
    432  1.13.12.2  pgoyette 		strcpy(name, (const char *)dkw.dkw_wname);
    433  1.13.12.2  pgoyette 		strcpy(parent, dkw.dkw_parent);
    434  1.13.12.2  pgoyette 		res = true;
    435  1.13.12.2  pgoyette 	}
    436  1.13.12.2  pgoyette 	close(fd);
    437  1.13.12.2  pgoyette 	return res;
    438  1.13.12.2  pgoyette }
    439  1.13.12.2  pgoyette 
    440  1.13.12.2  pgoyette static bool
    441  1.13.12.2  pgoyette find_swap_part_on(const char *dev, char *swap_name)
    442  1.13.12.2  pgoyette {
    443  1.13.12.2  pgoyette 	struct dkwedge_info *dkw;
    444  1.13.12.2  pgoyette 	struct dkwedge_list dkwl;
    445  1.13.12.2  pgoyette 	char buf[MAXPATHLEN];
    446  1.13.12.2  pgoyette 	size_t bufsize;
    447  1.13.12.2  pgoyette 	int fd;
    448  1.13.12.2  pgoyette 	u_int i;
    449  1.13.12.2  pgoyette 	bool res = false;
    450  1.13.12.2  pgoyette 
    451  1.13.12.2  pgoyette 	dkw = NULL;
    452  1.13.12.2  pgoyette 	dkwl.dkwl_buf = dkw;
    453  1.13.12.2  pgoyette 	dkwl.dkwl_bufsize = 0;
    454  1.13.12.2  pgoyette 
    455  1.13.12.2  pgoyette 	fd = opendisk(dev, O_RDONLY, buf, sizeof(buf), 0);
    456  1.13.12.2  pgoyette 	if (fd == -1)
    457  1.13.12.2  pgoyette 		return false;
    458  1.13.12.2  pgoyette 
    459  1.13.12.2  pgoyette 	for (;;) {
    460  1.13.12.2  pgoyette 		if (ioctl(fd, DIOCLWEDGES, &dkwl) == -1) {
    461  1.13.12.2  pgoyette 			dkwl.dkwl_ncopied = 0;
    462  1.13.12.2  pgoyette 			break;
    463  1.13.12.2  pgoyette 		}
    464  1.13.12.2  pgoyette 		if (dkwl.dkwl_nwedges == dkwl.dkwl_ncopied)
    465  1.13.12.2  pgoyette 			break;
    466  1.13.12.2  pgoyette 		bufsize = dkwl.dkwl_nwedges * sizeof(*dkw);
    467  1.13.12.2  pgoyette 		if (dkwl.dkwl_bufsize < bufsize) {
    468  1.13.12.2  pgoyette 			dkw = realloc(dkwl.dkwl_buf, bufsize);
    469  1.13.12.2  pgoyette 			if (dkw == NULL)
    470  1.13.12.2  pgoyette 				break;
    471  1.13.12.2  pgoyette 			dkwl.dkwl_buf = dkw;
    472  1.13.12.2  pgoyette 			dkwl.dkwl_bufsize = bufsize;
    473  1.13.12.2  pgoyette 		}
    474  1.13.12.2  pgoyette 	}
    475  1.13.12.2  pgoyette 
    476  1.13.12.2  pgoyette 	for (i = 0; i < dkwl.dkwl_nwedges; i++) {
    477  1.13.12.2  pgoyette 		res = strcmp(dkw[i].dkw_ptype, DKW_PTYPE_SWAP) == 0;
    478  1.13.12.2  pgoyette 		if (res) {
    479  1.13.12.2  pgoyette 			strcpy(swap_name, (const char*)dkw[i].dkw_wname);
    480  1.13.12.2  pgoyette 			break;
    481  1.13.12.2  pgoyette 		}
    482  1.13.12.2  pgoyette 	}
    483  1.13.12.2  pgoyette 
    484  1.13.12.2  pgoyette 	close(fd);
    485  1.13.12.2  pgoyette 
    486  1.13.12.2  pgoyette 	return res;
    487  1.13.12.2  pgoyette }
    488  1.13.12.2  pgoyette 
    489  1.13.12.2  pgoyette static bool
    490  1.13.12.2  pgoyette is_ffs_wedge(const char *dev)
    491  1.13.12.2  pgoyette {
    492  1.13.12.2  pgoyette 	struct dkwedge_info dkw;
    493  1.13.12.2  pgoyette 	char buf[MAXPATHLEN];
    494  1.13.12.2  pgoyette 	int fd;
    495  1.13.12.2  pgoyette 	bool res;
    496  1.13.12.2  pgoyette 
    497  1.13.12.2  pgoyette 	fd = opendisk(dev, O_RDONLY, buf, sizeof(buf), 0);
    498  1.13.12.2  pgoyette 	if (fd == -1)
    499  1.13.12.2  pgoyette 		return false;
    500  1.13.12.2  pgoyette 
    501  1.13.12.2  pgoyette 	if (ioctl(fd, DIOCGWEDGEINFO, &dkw) == -1)
    502  1.13.12.2  pgoyette 		return false;
    503  1.13.12.2  pgoyette 
    504  1.13.12.2  pgoyette 	res = strcmp(dkw.dkw_ptype, DKW_PTYPE_FFS) == 0;
    505  1.13.12.2  pgoyette 	close(fd);
    506  1.13.12.2  pgoyette 
    507  1.13.12.2  pgoyette 	return res;
    508  1.13.12.2  pgoyette }
    509  1.13.12.2  pgoyette 
    510        1.1  dholland static int
    511  1.13.12.2  pgoyette get_disks(struct disk_desc *dd, bool with_non_partitionable)
    512        1.1  dholland {
    513        1.1  dholland 	const char **xd;
    514        1.1  dholland 	char *cp;
    515        1.1  dholland 	struct disklabel l;
    516        1.1  dholland 	int i;
    517        1.1  dholland 	int numdisks;
    518        1.1  dholland 
    519        1.1  dholland 	/* initialize */
    520        1.1  dholland 	numdisks = 0;
    521        1.1  dholland 
    522        1.1  dholland 	for (xd = disk_names; *xd != NULL; xd++) {
    523        1.1  dholland 		for (i = 0; i < MAX_DISKS; i++) {
    524        1.1  dholland 			strlcpy(dd->dd_name, *xd, sizeof dd->dd_name - 2);
    525        1.1  dholland 			cp = strchr(dd->dd_name, ':');
    526  1.13.12.2  pgoyette 			if (cp != NULL) {
    527        1.1  dholland 				dd->dd_no_mbr = !strcmp(cp, ":no_mbr");
    528  1.13.12.2  pgoyette 				dd->dd_no_part = !strcmp(cp, ":no_part");
    529  1.13.12.2  pgoyette 			} else {
    530  1.13.12.2  pgoyette 				dd->dd_no_mbr = false;
    531  1.13.12.2  pgoyette 				dd->dd_no_part = false;
    532        1.1  dholland 				cp = strchr(dd->dd_name, 0);
    533        1.1  dholland 			}
    534  1.13.12.2  pgoyette 			if (dd->dd_no_part && !with_non_partitionable)
    535  1.13.12.2  pgoyette 				continue;
    536        1.1  dholland 
    537        1.1  dholland 			snprintf(cp, 2 + 1, "%d", i);
    538        1.1  dholland 			if (!get_geom(dd->dd_name, &l)) {
    539        1.1  dholland 				if (errno == ENOENT)
    540        1.1  dholland 					break;
    541  1.13.12.2  pgoyette 				if (errno != ENOTTY || !dd->dd_no_part)
    542  1.13.12.2  pgoyette 					/*
    543  1.13.12.2  pgoyette 					 * Allow plain partitions,
    544  1.13.12.2  pgoyette 					 * like already existing wedges
    545  1.13.12.2  pgoyette 					 * (like dk0) if marked as
    546  1.13.12.2  pgoyette 					 * non-partitioning device.
    547  1.13.12.2  pgoyette 					 * For all other cases, continue
    548  1.13.12.2  pgoyette 					 * with the next disk.
    549  1.13.12.2  pgoyette 					 */
    550  1.13.12.2  pgoyette 					continue;
    551  1.13.12.2  pgoyette 				if (!is_ffs_wedge(dd->dd_name))
    552  1.13.12.2  pgoyette 					continue;
    553        1.1  dholland 			}
    554        1.1  dholland 
    555        1.1  dholland 			/*
    556        1.1  dholland 			 * Exclude a disk mounted as root partition,
    557        1.1  dholland 			 * in case of install-image on a USB memstick.
    558        1.1  dholland 			 */
    559        1.1  dholland 			if (is_active_rootpart(dd->dd_name, 0))
    560        1.1  dholland 				continue;
    561        1.1  dholland 
    562  1.13.12.2  pgoyette 			if (!dd->dd_no_part) {
    563  1.13.12.2  pgoyette 				dd->dd_cyl = l.d_ncylinders;
    564  1.13.12.2  pgoyette 				dd->dd_head = l.d_ntracks;
    565  1.13.12.2  pgoyette 				dd->dd_sec = l.d_nsectors;
    566  1.13.12.2  pgoyette 				dd->dd_secsize = l.d_secsize;
    567  1.13.12.2  pgoyette 				dd->dd_totsec = l.d_secperunit;
    568  1.13.12.2  pgoyette 			}
    569  1.13.12.2  pgoyette 			if (dd->dd_no_part)
    570  1.13.12.2  pgoyette 				get_wedge_descr(dd);
    571  1.13.12.2  pgoyette 			else
    572  1.13.12.2  pgoyette 				get_descr(dd);
    573        1.1  dholland 			dd++;
    574        1.1  dholland 			numdisks++;
    575        1.1  dholland 			if (numdisks >= MAX_DISKS)
    576        1.1  dholland 				return numdisks;
    577        1.1  dholland 		}
    578        1.1  dholland 	}
    579        1.1  dholland 	return numdisks;
    580        1.1  dholland }
    581        1.1  dholland 
    582        1.1  dholland int
    583        1.1  dholland find_disks(const char *doingwhat)
    584        1.1  dholland {
    585        1.1  dholland 	struct disk_desc disks[MAX_DISKS];
    586        1.2    martin 	menu_ent dsk_menu[nelem(disks) + 1]; // + 1 for extended partitioning entry
    587        1.1  dholland 	struct disk_desc *disk;
    588  1.13.12.2  pgoyette 	int i = 0, skipped = 0;
    589  1.13.12.2  pgoyette 	int already_found, numdisks, selected_disk = -1;
    590        1.1  dholland 	int menu_no;
    591        1.2    martin 	pm_devs_t *pm_i, *pm_last = NULL;
    592        1.1  dholland 
    593        1.1  dholland 	/* Find disks. */
    594  1.13.12.2  pgoyette 	numdisks = get_disks(disks, partman_go <= 0);
    595        1.1  dholland 
    596        1.1  dholland 	/* need a redraw here, kernel messages hose everything */
    597        1.1  dholland 	touchwin(stdscr);
    598        1.1  dholland 	refresh();
    599        1.1  dholland 	/* Kill typeahead, it won't be what the user had in mind */
    600        1.1  dholland 	fpurge(stdin);
    601        1.1  dholland 
    602       1.10     isaki 	/*
    603       1.10     isaki 	 * partman_go: <0 - we want to see menu with extended partitioning
    604       1.10     isaki 	 *            ==0 - we want to see simple select disk menu
    605       1.10     isaki 	 *             >0 - we do not want to see any menus, just detect
    606       1.10     isaki 	 *                  all disks
    607       1.10     isaki 	 */
    608        1.2    martin 	if (partman_go <= 0) {
    609        1.2    martin 		if (numdisks == 0) {
    610        1.2    martin 			/* No disks found! */
    611        1.2    martin 			msg_display(MSG_nodisk);
    612        1.2    martin 			process_menu(MENU_ok, NULL);
    613        1.2    martin 			/*endwin();*/
    614        1.2    martin 			return -1;
    615        1.2    martin 		} else {
    616        1.2    martin 			/* One or more disks found! */
    617        1.2    martin 			for (i = 0; i < numdisks; i++) {
    618  1.13.12.2  pgoyette 				dsk_menu[i].opt_name =
    619  1.13.12.2  pgoyette 				    disks[i].dd_descr;
    620        1.2    martin 				dsk_menu[i].opt_menu = OPT_NOMENU;
    621        1.2    martin 				dsk_menu[i].opt_flags = OPT_EXIT;
    622        1.2    martin 				dsk_menu[i].opt_action = set_menu_select;
    623        1.2    martin 			}
    624        1.2    martin 			if (partman_go < 0) {
    625        1.2    martin 				dsk_menu[i].opt_name = MSG_partman;
    626        1.2    martin 				dsk_menu[i].opt_menu = OPT_NOMENU;
    627        1.2    martin 				dsk_menu[i].opt_flags = OPT_EXIT;
    628        1.2    martin 				dsk_menu[i].opt_action = set_menu_select;
    629        1.2    martin 			}
    630        1.2    martin 			menu_no = new_menu(MSG_Available_disks,
    631  1.13.12.2  pgoyette 				dsk_menu, numdisks
    632  1.13.12.2  pgoyette 				 + ((partman_go<0)?1:0), -1,
    633  1.13.12.2  pgoyette 				 4, 0, 0, MC_SCROLL,
    634        1.2    martin 				NULL, NULL, NULL, NULL, NULL);
    635        1.2    martin 			if (menu_no == -1)
    636        1.2    martin 				return -1;
    637        1.2    martin 			msg_display(MSG_ask_disk, doingwhat);
    638        1.2    martin 			process_menu(menu_no, &selected_disk);
    639        1.2    martin 			free_menu(menu_no);
    640        1.2    martin 		}
    641        1.2    martin 		if (partman_go < 0 && selected_disk == numdisks) {
    642        1.2    martin 			partman_go = 1;
    643       1.10     isaki 			return -2;
    644        1.2    martin 		} else
    645        1.2    martin 			partman_go = 0;
    646        1.2    martin 		if (selected_disk < 0 || selected_disk >= numdisks)
    647       1.10     isaki 			return -1;
    648        1.2    martin 	}
    649        1.2    martin 
    650        1.2    martin 	/* Fill pm struct with device(s) info */
    651        1.2    martin 	for (i = 0; i < numdisks; i++) {
    652        1.2    martin 		if (! partman_go)
    653        1.2    martin 			disk = disks + selected_disk;
    654        1.2    martin 		else {
    655        1.2    martin 			disk = disks + i;
    656        1.2    martin 			already_found = 0;
    657        1.2    martin 			SLIST_FOREACH(pm_i, &pm_head, l) {
    658        1.2    martin 				pm_last = pm_i;
    659       1.10     isaki 				if (!already_found &&
    660       1.10     isaki 				    strcmp(pm_i->diskdev, disk->dd_name) == 0) {
    661        1.2    martin 					pm_i->found = 1;
    662        1.2    martin 					break;
    663        1.2    martin 				}
    664        1.2    martin 			}
    665        1.2    martin 			if (pm_i != NULL && pm_i->found)
    666        1.2    martin 				/* We already added this device, skipping */
    667        1.2    martin 				continue;
    668        1.1  dholland 		}
    669        1.2    martin 		pm = pm_new;
    670        1.2    martin 		pm->found = 1;
    671        1.2    martin 		pm->bootable = 0;
    672        1.2    martin 		pm->pi.menu_no = -1;
    673        1.2    martin 		pm->disktype = "unknown";
    674        1.2    martin 		pm->doessf = "";
    675        1.2    martin 		strlcpy(pm->diskdev, disk->dd_name, sizeof pm->diskdev);
    676        1.2    martin 		strlcpy(pm->diskdev_descr, disk->dd_descr, sizeof pm->diskdev_descr);
    677        1.2    martin 		/* Use as a default disk if the user has the sets on a local disk */
    678        1.2    martin 		strlcpy(localfs_dev, disk->dd_name, sizeof localfs_dev);
    679        1.2    martin 
    680        1.2    martin 		pm->gpt = is_gpt(pm->diskdev);
    681        1.2    martin 		pm->no_mbr = disk->dd_no_mbr || pm->gpt;
    682  1.13.12.2  pgoyette 		pm->no_part = disk->dd_no_part;
    683  1.13.12.2  pgoyette 		if (!pm->no_part) {
    684  1.13.12.2  pgoyette 			pm->sectorsize = disk->dd_secsize;
    685  1.13.12.2  pgoyette 			pm->dlcyl = disk->dd_cyl;
    686  1.13.12.2  pgoyette 			pm->dlhead = disk->dd_head;
    687  1.13.12.2  pgoyette 			pm->dlsec = disk->dd_sec;
    688  1.13.12.2  pgoyette 			pm->dlsize = disk->dd_totsec;
    689  1.13.12.2  pgoyette 			if (pm->dlsize == 0)
    690  1.13.12.2  pgoyette 				pm->dlsize = disk->dd_cyl * disk->dd_head * disk->dd_sec;
    691  1.13.12.2  pgoyette 			if (pm->dlsize > UINT32_MAX && ! partman_go) {
    692  1.13.12.2  pgoyette 				if (logfp)
    693  1.13.12.2  pgoyette 					fprintf(logfp, "Cannot process disk %s: too big size (%d)\n",
    694  1.13.12.2  pgoyette 						pm->diskdev, (int)pm->dlsize);
    695  1.13.12.2  pgoyette 				msg_display(MSG_toobigdisklabel);
    696  1.13.12.2  pgoyette 				process_menu(MENU_ok, NULL);
    697  1.13.12.2  pgoyette 				return -1;
    698  1.13.12.2  pgoyette 			}
    699  1.13.12.2  pgoyette 		} else {
    700  1.13.12.2  pgoyette 			pm->sectorsize = 0;
    701  1.13.12.2  pgoyette 			pm->dlcyl = 0;
    702  1.13.12.2  pgoyette 			pm->dlhead = 0;
    703  1.13.12.2  pgoyette 			pm->dlsec = 0;
    704  1.13.12.2  pgoyette 			pm->dlsize = 0;
    705  1.13.12.2  pgoyette 			pm->rootpart = -1;
    706  1.13.12.2  pgoyette 			pm->no_mbr = 1;
    707  1.13.12.2  pgoyette 			memset(&pm->bsdlabel, 0, sizeof(pm->bsdlabel));
    708        1.2    martin 		}
    709        1.2    martin 		pm->dlcylsize = pm->dlhead * pm->dlsec;
    710        1.2    martin 
    711        1.2    martin 		label_read();
    712        1.2    martin 		if (partman_go) {
    713        1.2    martin 			pm_getrefdev(pm_new);
    714        1.2    martin 			if (SLIST_EMPTY(&pm_head) || pm_last == NULL)
    715        1.2    martin 				 SLIST_INSERT_HEAD(&pm_head, pm_new, l);
    716        1.2    martin 			else
    717        1.2    martin 				 SLIST_INSERT_AFTER(pm_last, pm_new, l);
    718        1.2    martin 			pm_new = malloc(sizeof (pm_devs_t));
    719        1.2    martin 			memset(pm_new, 0, sizeof *pm_new);
    720        1.2    martin 		} else
    721        1.2    martin 			/* We is not in partman and do not want to process all devices, exit */
    722        1.2    martin 			break;
    723        1.1  dholland 	}
    724        1.1  dholland 
    725  1.13.12.2  pgoyette 	return numdisks-skipped;
    726        1.2    martin }
    727        1.2    martin 
    728        1.6    martin 
    729        1.2    martin void
    730        1.2    martin label_read(void)
    731        1.2    martin {
    732  1.13.12.2  pgoyette 
    733  1.13.12.2  pgoyette 	if (pm->no_part)
    734  1.13.12.2  pgoyette 		return;
    735  1.13.12.2  pgoyette 
    736        1.8    martin 	check_available_binaries();
    737        1.8    martin 
    738        1.1  dholland 	/* Get existing/default label */
    739        1.2    martin 	memset(&pm->oldlabel, 0, sizeof pm->oldlabel);
    740        1.8    martin 	if (!have_gpt || !pm->gpt)
    741        1.2    martin 		incorelabel(pm->diskdev, pm->oldlabel);
    742        1.2    martin 	else
    743        1.2    martin 		incoregpt(pm, pm->oldlabel);
    744        1.1  dholland 	/* Set 'target' label to current label in case we don't change it */
    745        1.2    martin 	memcpy(&pm->bsdlabel, &pm->oldlabel, sizeof pm->bsdlabel);
    746        1.2    martin #ifndef NO_DISKLABEL
    747        1.2    martin 	if (! pm->gpt)
    748        1.2    martin 		savenewlabel(pm->oldlabel, getmaxpartitions());
    749        1.2    martin #endif
    750        1.1  dholland }
    751        1.1  dholland 
    752        1.1  dholland void
    753        1.1  dholland fmt_fspart(menudesc *m, int ptn, void *arg)
    754        1.1  dholland {
    755        1.1  dholland 	unsigned int poffset, psize, pend;
    756        1.1  dholland 	const char *desc;
    757        1.1  dholland 	static const char *Yes;
    758        1.2    martin 	partinfo *p = pm->bsdlabel + ptn;
    759        1.1  dholland 
    760        1.1  dholland 	if (Yes == NULL)
    761        1.1  dholland 		Yes = msg_string(MSG_Yes);
    762        1.1  dholland 
    763        1.1  dholland 	poffset = p->pi_offset / sizemult;
    764        1.1  dholland 	psize = p->pi_size / sizemult;
    765        1.1  dholland 	if (psize == 0)
    766        1.1  dholland 		pend = 0;
    767        1.1  dholland 	else
    768        1.1  dholland 		pend = (p->pi_offset + p->pi_size) / sizemult - 1;
    769        1.1  dholland 
    770        1.1  dholland 	if (p->pi_fstype == FS_BSDFFS)
    771        1.1  dholland 		if (p->pi_flags & PIF_FFSv2)
    772        1.1  dholland 			desc = "FFSv2";
    773        1.1  dholland 		else
    774        1.1  dholland 			desc = "FFSv1";
    775        1.1  dholland 	else
    776        1.1  dholland 		desc = getfslabelname(p->pi_fstype);
    777        1.1  dholland 
    778        1.1  dholland #ifdef PART_BOOT
    779        1.1  dholland 	if (ptn == PART_BOOT)
    780        1.1  dholland 		desc = msg_string(MSG_Boot_partition_cant_change);
    781        1.1  dholland #endif
    782        1.1  dholland 	if (ptn == getrawpartition())
    783        1.1  dholland 		desc = msg_string(MSG_Whole_disk_cant_change);
    784        1.1  dholland 	else {
    785        1.1  dholland 		if (ptn == PART_C)
    786        1.1  dholland 			desc = msg_string(MSG_NetBSD_partition_cant_change);
    787        1.1  dholland 	}
    788        1.1  dholland 
    789        1.1  dholland 	wprintw(m->mw, msg_string(MSG_fspart_row),
    790        1.1  dholland 			poffset, pend, psize, desc,
    791        1.1  dholland 			p->pi_flags & PIF_NEWFS ? Yes : "",
    792        1.1  dholland 			p->pi_flags & PIF_MOUNT ? Yes : "",
    793        1.1  dholland 			p->pi_mount);
    794        1.1  dholland }
    795        1.1  dholland 
    796        1.1  dholland /*
    797        1.1  dholland  * Label a disk using an MD-specific string DISKLABEL_CMD for
    798        1.1  dholland  * to invoke disklabel.
    799        1.1  dholland  * if MD code does not define DISKLABEL_CMD, this is a no-op.
    800        1.1  dholland  *
    801        1.1  dholland  * i386 port uses "/sbin/disklabel -w -r", just like i386
    802        1.1  dholland  * miniroot scripts, though this may leave a bogus incore label.
    803        1.1  dholland  *
    804        1.1  dholland  * Sun ports should use DISKLABEL_CMD "/sbin/disklabel -w"
    805        1.1  dholland  * to get incore to ondisk inode translation for the Sun proms.
    806        1.1  dholland  */
    807        1.1  dholland int
    808        1.1  dholland write_disklabel (void)
    809        1.1  dholland {
    810        1.8    martin 	int rv = 0;
    811        1.1  dholland 
    812  1.13.12.2  pgoyette 	if (pm && pm->no_part)
    813  1.13.12.2  pgoyette 		return 0;
    814  1.13.12.2  pgoyette 
    815        1.1  dholland #ifdef DISKLABEL_CMD
    816        1.1  dholland 	/* disklabel the disk */
    817        1.8    martin 	rv = run_program(RUN_DISPLAY, "%s -f /tmp/disktab %s '%s'",
    818        1.2    martin 	    DISKLABEL_CMD, pm->diskdev, pm->bsddiskname);
    819        1.8    martin 	if (rv == 0)
    820        1.8    martin 		update_wedges(pm->diskdev);
    821        1.1  dholland #endif
    822        1.8    martin 	return rv;
    823        1.1  dholland }
    824        1.1  dholland 
    825        1.1  dholland 
    826        1.1  dholland static int
    827        1.1  dholland ptn_sort(const void *a, const void *b)
    828        1.1  dholland {
    829        1.2    martin 	return strcmp(pm->bsdlabel[*(const int *)a].pi_mount,
    830        1.2    martin 		      pm->bsdlabel[*(const int *)b].pi_mount);
    831        1.1  dholland }
    832        1.1  dholland 
    833        1.1  dholland int
    834        1.1  dholland make_filesystems(void)
    835        1.1  dholland {
    836        1.1  dholland 	unsigned int i;
    837        1.1  dholland 	int ptn;
    838        1.2    martin 	int ptn_order[nelem(pm->bsdlabel)];
    839        1.1  dholland 	int error = 0;
    840        1.1  dholland 	unsigned int maxpart = getmaxpartitions();
    841        1.2    martin 	char *newfs = NULL, *dev = NULL, *devdev = NULL;
    842        1.1  dholland 	partinfo *lbl;
    843        1.1  dholland 
    844  1.13.12.2  pgoyette 	if (pm->no_part) {
    845  1.13.12.2  pgoyette 		/* check if this target device already has a ffs */
    846  1.13.12.2  pgoyette 		error = fsck_preen(pm->diskdev, -1, "ffs", true);
    847  1.13.12.2  pgoyette 		if (error) {
    848  1.13.12.2  pgoyette 			if (!ask_noyes(MSG_No_filesystem_newfs))
    849  1.13.12.2  pgoyette 				return EINVAL;
    850  1.13.12.2  pgoyette 			error = run_program(RUN_DISPLAY | RUN_PROGRESS,
    851  1.13.12.2  pgoyette 			    "/sbin/newfs -V2 -O2 /dev/r%s", pm->diskdev);
    852  1.13.12.2  pgoyette 		}
    853  1.13.12.2  pgoyette 
    854  1.13.12.2  pgoyette 		md_pre_mount();
    855  1.13.12.2  pgoyette 
    856  1.13.12.2  pgoyette 		make_target_dir("/");
    857  1.13.12.2  pgoyette 		asprintf(&devdev, "/dev/%s", pm->diskdev);
    858  1.13.12.2  pgoyette 		if (devdev == NULL)
    859  1.13.12.2  pgoyette 			return (ENOMEM);
    860  1.13.12.2  pgoyette 		error = target_mount_do("-o async", devdev, "/");
    861  1.13.12.2  pgoyette 		if (error) {
    862  1.13.12.2  pgoyette 			msg_display(MSG_mountfail, devdev, ' ',
    863  1.13.12.2  pgoyette 			    "/");
    864  1.13.12.2  pgoyette 			process_menu(MENU_ok, NULL);
    865  1.13.12.2  pgoyette 		}
    866  1.13.12.2  pgoyette 		free(devdev);
    867  1.13.12.2  pgoyette 		return error;
    868  1.13.12.2  pgoyette 	}
    869  1.13.12.2  pgoyette 
    870        1.2    martin 	if (maxpart > nelem(pm->bsdlabel))
    871        1.2    martin 		maxpart = nelem(pm->bsdlabel);
    872        1.1  dholland 
    873        1.1  dholland 	/* Making new file systems and mounting them */
    874        1.1  dholland 
    875        1.1  dholland 	/* sort to ensure /usr/local is mounted after /usr (etc) */
    876        1.1  dholland 	for (i = 0; i < maxpart; i++)
    877        1.1  dholland 		ptn_order[i] = i;
    878        1.1  dholland 	qsort(ptn_order, maxpart, sizeof ptn_order[0], ptn_sort);
    879        1.1  dholland 
    880        1.1  dholland 	for (i = 0; i < maxpart; i++) {
    881        1.1  dholland 		/*
    882        1.1  dholland 		 * newfs and mount. For now, process only BSD filesystems.
    883        1.1  dholland 		 * but if this is the mounted-on root, has no mount
    884        1.1  dholland 		 * point defined, or is marked preserve, don't touch it!
    885        1.1  dholland 		 */
    886        1.1  dholland 		ptn = ptn_order[i];
    887        1.2    martin 		lbl = pm->bsdlabel + ptn;
    888        1.1  dholland 
    889        1.2    martin 		if (is_active_rootpart(pm->diskdev, ptn))
    890        1.1  dholland 			continue;
    891        1.1  dholland 
    892        1.1  dholland 		if (*lbl->pi_mount == 0)
    893        1.1  dholland 			/* No mount point */
    894        1.1  dholland 			continue;
    895        1.1  dholland 
    896        1.2    martin 		if (pm->isspecial) {
    897        1.2    martin 			asprintf(&dev, "%s", pm->diskdev);
    898        1.2    martin 			ptn = 0 - 'a';
    899        1.2    martin 		} else {
    900        1.2    martin 			asprintf(&dev, "%s%c", pm->diskdev, 'a' + ptn);
    901        1.2    martin 		}
    902        1.2    martin 		if (dev == NULL)
    903        1.2    martin 			return (ENOMEM);
    904        1.2    martin 		asprintf(&devdev, "/dev/%s", dev);
    905        1.2    martin 		if (devdev == NULL)
    906        1.2    martin 			return (ENOMEM);
    907        1.2    martin 
    908        1.1  dholland 		newfs = NULL;
    909        1.2    martin 		lbl->mnt_opts = NULL;
    910        1.2    martin 		lbl->fsname = NULL;
    911        1.1  dholland 		switch (lbl->pi_fstype) {
    912        1.1  dholland 		case FS_APPLEUFS:
    913        1.1  dholland 			asprintf(&newfs, "/sbin/newfs %s%.0d",
    914        1.1  dholland 				lbl->pi_isize != 0 ? "-i" : "", lbl->pi_isize);
    915        1.2    martin 			lbl->mnt_opts = "-tffs -o async";
    916        1.2    martin 			lbl->fsname = "ffs";
    917        1.1  dholland 			break;
    918        1.1  dholland 		case FS_BSDFFS:
    919        1.1  dholland 			asprintf(&newfs,
    920        1.1  dholland 			    "/sbin/newfs -V2 -O %d -b %d -f %d%s%.0d",
    921        1.1  dholland 			    lbl->pi_flags & PIF_FFSv2 ? 2 : 1,
    922        1.1  dholland 			    lbl->pi_fsize * lbl->pi_frag, lbl->pi_fsize,
    923        1.1  dholland 			    lbl->pi_isize != 0 ? " -i " : "", lbl->pi_isize);
    924        1.1  dholland 			if (lbl->pi_flags & PIF_LOG)
    925        1.2    martin 				lbl->mnt_opts = "-tffs -o log";
    926        1.1  dholland 			else
    927        1.2    martin 				lbl->mnt_opts = "-tffs -o async";
    928        1.2    martin 			lbl->fsname = "ffs";
    929        1.1  dholland 			break;
    930        1.1  dholland 		case FS_BSDLFS:
    931        1.1  dholland 			asprintf(&newfs, "/sbin/newfs_lfs -b %d",
    932        1.1  dholland 				lbl->pi_fsize * lbl->pi_frag);
    933        1.2    martin 			lbl->mnt_opts = "-tlfs";
    934        1.2    martin 			lbl->fsname = "lfs";
    935        1.1  dholland 			break;
    936        1.1  dholland 		case FS_MSDOS:
    937        1.1  dholland #ifdef USE_NEWFS_MSDOS
    938        1.1  dholland 			asprintf(&newfs, "/sbin/newfs_msdos");
    939        1.1  dholland #endif
    940        1.2    martin 			lbl->mnt_opts = "-tmsdos";
    941        1.2    martin 			lbl->fsname = "msdos";
    942        1.1  dholland 			break;
    943        1.1  dholland #ifdef USE_SYSVBFS
    944        1.1  dholland 		case FS_SYSVBFS:
    945        1.1  dholland 			asprintf(&newfs, "/sbin/newfs_sysvbfs");
    946        1.2    martin 			lbl->mnt_opts = "-tsysvbfs";
    947        1.2    martin 			lbl->fsname = "sysvbfs";
    948        1.1  dholland 			break;
    949        1.1  dholland #endif
    950        1.1  dholland #ifdef USE_EXT2FS
    951        1.1  dholland 		case FS_EX2FS:
    952        1.1  dholland 			asprintf(&newfs, "/sbin/newfs_ext2fs");
    953        1.2    martin 			lbl->mnt_opts = "-text2fs";
    954        1.2    martin 			lbl->fsname = "ext2fs";
    955        1.1  dholland 			break;
    956        1.1  dholland #endif
    957        1.1  dholland 		}
    958        1.1  dholland 		if (lbl->pi_flags & PIF_NEWFS && newfs != NULL) {
    959        1.1  dholland #ifdef USE_NEWFS_MSDOS
    960        1.1  dholland 			if (lbl->pi_fstype == FS_MSDOS) {
    961        1.1  dholland 			        /* newfs only if mount fails */
    962        1.1  dholland 			        if (run_program(RUN_SILENT | RUN_ERROR_OK,
    963        1.2    martin 				    "mount -rt msdos /dev/%s /mnt2", dev) != 0)
    964        1.1  dholland 					error = run_program(
    965        1.1  dholland 					    RUN_DISPLAY | RUN_PROGRESS,
    966        1.2    martin 					    "%s /dev/r%s",
    967        1.2    martin 					    newfs, dev);
    968        1.1  dholland 				else {
    969       1.10     isaki 					run_program(RUN_SILENT | RUN_ERROR_OK,
    970        1.1  dholland 					    "umount /mnt2");
    971        1.1  dholland 					error = 0;
    972        1.1  dholland 				}
    973        1.1  dholland 			} else
    974        1.1  dholland #endif
    975        1.1  dholland 			error = run_program(RUN_DISPLAY | RUN_PROGRESS,
    976        1.2    martin 			    "%s /dev/r%s", newfs, dev);
    977        1.1  dholland 		} else {
    978        1.1  dholland 			/* We'd better check it isn't dirty */
    979  1.13.12.2  pgoyette 			error = fsck_preen(pm->diskdev, ptn, lbl->fsname, false);
    980        1.1  dholland 		}
    981        1.1  dholland 		free(newfs);
    982       1.12    martin 		if (error != 0) {
    983       1.12    martin 			free(devdev);
    984       1.12    martin 			free(dev);
    985        1.1  dholland 			return error;
    986       1.12    martin 		}
    987        1.1  dholland 
    988        1.2    martin 		lbl->pi_flags ^= PIF_NEWFS;
    989        1.1  dholland 		md_pre_mount();
    990        1.1  dholland 
    991        1.2    martin 		if (partman_go == 0 && lbl->pi_flags & PIF_MOUNT &&
    992        1.2    martin 				lbl->mnt_opts != NULL) {
    993        1.1  dholland 			make_target_dir(lbl->pi_mount);
    994        1.2    martin 			error = target_mount_do(lbl->mnt_opts, devdev, lbl->pi_mount);
    995        1.1  dholland 			if (error) {
    996        1.2    martin 				msg_display(MSG_mountfail, dev, ' ', lbl->pi_mount);
    997        1.1  dholland 				process_menu(MENU_ok, NULL);
    998       1.12    martin 				free(devdev);
    999       1.12    martin 				free(dev);
   1000        1.1  dholland 				return error;
   1001        1.1  dholland 			}
   1002        1.1  dholland 		}
   1003        1.2    martin 		free(devdev);
   1004        1.2    martin 		free(dev);
   1005        1.1  dholland 	}
   1006        1.1  dholland 	return 0;
   1007        1.1  dholland }
   1008        1.1  dholland 
   1009        1.1  dholland int
   1010        1.1  dholland make_fstab(void)
   1011        1.1  dholland {
   1012        1.1  dholland 	FILE *f;
   1013        1.1  dholland 	int i, swap_dev = -1;
   1014        1.1  dholland 	const char *dump_dev;
   1015        1.2    martin 	char *dev = NULL;
   1016        1.2    martin 	pm_devs_t *pm_i;
   1017        1.2    martin #ifndef HAVE_TMPFS
   1018        1.3       mrg 	pm_devs_t *pm_with_swap = NULL;
   1019        1.2    martin #endif
   1020        1.1  dholland 
   1021        1.1  dholland 	/* Create the fstab. */
   1022        1.1  dholland 	make_target_dir("/etc");
   1023        1.1  dholland 	f = target_fopen("/etc/fstab", "w");
   1024        1.2    martin 	scripting_fprintf(NULL, "cat <<EOF >%s/etc/fstab\n", target_prefix());
   1025        1.2    martin 
   1026        1.1  dholland 	if (logfp)
   1027        1.1  dholland 		(void)fprintf(logfp,
   1028        1.2    martin 		    "Making %s/etc/fstab (%s).\n", target_prefix(), pm->diskdev);
   1029       1.10     isaki 
   1030        1.1  dholland 	if (f == NULL) {
   1031        1.1  dholland 		msg_display(MSG_createfstab);
   1032        1.1  dholland 		if (logfp)
   1033        1.1  dholland 			(void)fprintf(logfp, "Failed to make /etc/fstab!\n");
   1034        1.1  dholland 		process_menu(MENU_ok, NULL);
   1035        1.2    martin #ifndef DEBUG
   1036        1.1  dholland 		return 1;
   1037        1.1  dholland #else
   1038        1.1  dholland 		f = stdout;
   1039        1.1  dholland #endif
   1040        1.1  dholland 	}
   1041        1.1  dholland 
   1042  1.13.12.2  pgoyette 	scripting_fprintf(f, "# NetBSD /etc/fstab\n# See /usr/share/examples/"
   1043  1.13.12.2  pgoyette 			"fstab/ for more examples.\n");
   1044  1.13.12.2  pgoyette 
   1045  1.13.12.2  pgoyette 	if (pm->no_part) {
   1046  1.13.12.2  pgoyette 		/* single dk? target */
   1047  1.13.12.2  pgoyette 		char buf[200], parent[200], swap[200], *prompt;
   1048  1.13.12.2  pgoyette 		int res;
   1049  1.13.12.2  pgoyette 
   1050  1.13.12.2  pgoyette 		if (!get_name_and_parent(pm->diskdev, buf, parent))
   1051  1.13.12.2  pgoyette 			goto done_with_disks;
   1052  1.13.12.2  pgoyette 		scripting_fprintf(f, "NAME=%s\t/\tffs\trw\t\t1 1\n",
   1053  1.13.12.2  pgoyette 		    buf);
   1054  1.13.12.2  pgoyette 		if (!find_swap_part_on(parent, swap))
   1055  1.13.12.2  pgoyette 			goto done_with_disks;
   1056  1.13.12.2  pgoyette 		asprintf(&prompt, msg_string(MSG_Auto_add_swap_part),
   1057  1.13.12.2  pgoyette 		    swap, parent);
   1058  1.13.12.2  pgoyette 		res = ask_yesno(prompt);
   1059  1.13.12.2  pgoyette 		free(prompt);
   1060  1.13.12.2  pgoyette 		if (res)
   1061  1.13.12.2  pgoyette 			scripting_fprintf(f, "NAME=%s\tnone"
   1062  1.13.12.2  pgoyette 			    "\tswap\tsw,dp\t\t0 0\n", swap);
   1063  1.13.12.2  pgoyette 		goto done_with_disks;
   1064  1.13.12.2  pgoyette 	}
   1065  1.13.12.2  pgoyette 
   1066        1.2    martin 	if (! partman_go) {
   1067        1.2    martin 		/* We want to process only one disk... */
   1068        1.2    martin 		pm_i = pm;
   1069        1.2    martin 		goto onlyonediskinfstab;
   1070        1.2    martin 	}
   1071        1.2    martin 	SLIST_FOREACH(pm_i, &pm_head, l) {
   1072        1.2    martin 		onlyonediskinfstab:
   1073        1.2    martin 		for (i = 0; i < getmaxpartitions(); i++) {
   1074        1.2    martin 			const char *s = "";
   1075        1.2    martin 			const char *mp = pm_i->bsdlabel[i].pi_mount;
   1076        1.2    martin 			const char *fstype = "ffs";
   1077        1.2    martin 			int fsck_pass = 0, dump_freq = 0;
   1078       1.10     isaki 
   1079        1.2    martin 			if (dev != NULL)
   1080        1.2    martin 				free(dev);
   1081        1.2    martin 			if (pm_i->isspecial)
   1082        1.2    martin 				asprintf(&dev, "%s", pm_i->diskdev);
   1083        1.2    martin 			else
   1084        1.2    martin 				asprintf(&dev, "%s%c", pm_i->diskdev, 'a' + i);
   1085        1.2    martin 			if (dev == NULL)
   1086        1.2    martin 				return (ENOMEM);
   1087        1.2    martin 
   1088        1.2    martin 			if (!*mp) {
   1089        1.2    martin 				/*
   1090        1.2    martin 				 * No mount point specified, comment out line and
   1091        1.2    martin 				 * use /mnt as a placeholder for the mount point.
   1092        1.2    martin 				 */
   1093        1.1  dholland 				s = "# ";
   1094        1.2    martin 				mp = "/mnt";
   1095        1.1  dholland 			}
   1096        1.2    martin 
   1097        1.2    martin 			switch (pm_i->bsdlabel[i].pi_fstype) {
   1098        1.2    martin 			case FS_UNUSED:
   1099        1.2    martin 				continue;
   1100        1.2    martin 			case FS_BSDLFS:
   1101        1.2    martin 				/* If there is no LFS, just comment it out. */
   1102        1.2    martin 				if (!check_lfs_progs())
   1103        1.2    martin 					s = "# ";
   1104        1.2    martin 				fstype = "lfs";
   1105        1.2    martin 				/* FALLTHROUGH */
   1106        1.2    martin 			case FS_BSDFFS:
   1107        1.2    martin 				fsck_pass = (strcmp(mp, "/") == 0) ? 1 : 2;
   1108        1.2    martin 				dump_freq = 1;
   1109        1.2    martin 				break;
   1110        1.2    martin 			case FS_MSDOS:
   1111        1.2    martin 				fstype = "msdos";
   1112        1.2    martin 				break;
   1113        1.2    martin 			case FS_SWAP:
   1114        1.2    martin 				if (pm_i->isspecial)
   1115        1.2    martin 					continue;
   1116        1.2    martin 				if (swap_dev == -1) {
   1117        1.2    martin 					swap_dev = i;
   1118        1.2    martin 					dump_dev = ",dp";
   1119        1.2    martin #ifndef HAVE_TMPFS
   1120        1.2    martin 					pm_with_swap = pm_i;
   1121        1.2    martin #endif
   1122        1.2    martin 				} else {
   1123        1.2    martin 					dump_dev = "";
   1124        1.2    martin 				}
   1125        1.2    martin 				scripting_fprintf(f, "/dev/%s\t\tnone\tswap\tsw%s\t\t 0 0\n",
   1126        1.2    martin 					dev, dump_dev);
   1127        1.2    martin 				continue;
   1128        1.1  dholland #ifdef USE_SYSVBFS
   1129        1.2    martin 			case FS_SYSVBFS:
   1130        1.2    martin 				fstype = "sysvbfs";
   1131        1.2    martin 				make_target_dir("/stand");
   1132        1.2    martin 				break;
   1133        1.1  dholland #endif
   1134        1.2    martin 			default:
   1135        1.2    martin 				fstype = "???";
   1136        1.2    martin 				s = "# ";
   1137        1.2    martin 				break;
   1138        1.2    martin 			}
   1139        1.2    martin 			/* The code that remounts root rw doesn't check the partition */
   1140        1.2    martin 			if (strcmp(mp, "/") == 0 && !(pm_i->bsdlabel[i].pi_flags & PIF_MOUNT))
   1141        1.2    martin 				s = "# ";
   1142        1.2    martin 
   1143        1.2    martin 	 		scripting_fprintf(f,
   1144        1.2    martin 			  "%s/dev/%s\t\t%s\t%s\trw%s%s%s%s%s%s%s%s\t\t %d %d\n",
   1145        1.2    martin 			   s, dev, mp, fstype,
   1146        1.2    martin 			   pm_i->bsdlabel[i].pi_flags & PIF_LOG ? ",log" : "",
   1147        1.2    martin 			   pm_i->bsdlabel[i].pi_flags & PIF_MOUNT ? "" : ",noauto",
   1148        1.2    martin 			   pm_i->bsdlabel[i].pi_flags & PIF_ASYNC ? ",async" : "",
   1149        1.2    martin 			   pm_i->bsdlabel[i].pi_flags & PIF_NOATIME ? ",noatime" : "",
   1150        1.2    martin 			   pm_i->bsdlabel[i].pi_flags & PIF_NODEV ? ",nodev" : "",
   1151        1.2    martin 			   pm_i->bsdlabel[i].pi_flags & PIF_NODEVMTIME ? ",nodevmtime" : "",
   1152        1.2    martin 			   pm_i->bsdlabel[i].pi_flags & PIF_NOEXEC ? ",noexec" : "",
   1153        1.2    martin 			   pm_i->bsdlabel[i].pi_flags & PIF_NOSUID ? ",nosuid" : "",
   1154        1.2    martin 			   dump_freq, fsck_pass);
   1155        1.2    martin 	 		if (pm_i->isspecial)
   1156        1.2    martin 	 			/* Special device (such as dk*) have only one partition */
   1157        1.2    martin 	 			break;
   1158        1.2    martin 		}
   1159        1.2    martin 		/* Simple install, only one disk */
   1160        1.2    martin 		if (!partman_go)
   1161        1.1  dholland 			break;
   1162        1.1  dholland 	}
   1163  1.13.12.2  pgoyette done_with_disks:
   1164        1.1  dholland 	if (tmp_ramdisk_size != 0) {
   1165        1.1  dholland #ifdef HAVE_TMPFS
   1166        1.1  dholland 		scripting_fprintf(f, "tmpfs\t\t/tmp\ttmpfs\trw,-m=1777,-s=%"
   1167        1.1  dholland 		    PRIi64 "\n",
   1168        1.1  dholland 		    tmp_ramdisk_size * 512);
   1169        1.1  dholland #else
   1170        1.2    martin 		if (swap_dev != -1 && pm_with_swap != NULL)
   1171        1.1  dholland 			scripting_fprintf(f, "/dev/%s%c\t\t/tmp\tmfs\trw,-s=%"
   1172        1.1  dholland 			    PRIi64 "\n",
   1173        1.2    martin 			    pm_with_swap->diskdev, 'a' + swap_dev, tmp_ramdisk_size);
   1174        1.1  dholland 		else
   1175        1.1  dholland 			scripting_fprintf(f, "swap\t\t/tmp\tmfs\trw,-s=%"
   1176        1.1  dholland 			    PRIi64 "\n",
   1177        1.1  dholland 			    tmp_ramdisk_size);
   1178        1.1  dholland #endif
   1179        1.1  dholland 	}
   1180        1.1  dholland 
   1181        1.1  dholland 	/* Add /kern, /proc and /dev/pts to fstab and make mountpoint. */
   1182        1.1  dholland 	scripting_fprintf(f, "kernfs\t\t/kern\tkernfs\trw\n");
   1183        1.1  dholland 	scripting_fprintf(f, "ptyfs\t\t/dev/pts\tptyfs\trw\n");
   1184        1.1  dholland 	scripting_fprintf(f, "procfs\t\t/proc\tprocfs\trw\n");
   1185        1.1  dholland 	scripting_fprintf(f, "/dev/%s\t\t/cdrom\tcd9660\tro,noauto\n",
   1186        1.1  dholland 	    get_default_cdrom());
   1187        1.1  dholland 	scripting_fprintf(f, "%stmpfs\t\t/var/shm\ttmpfs\trw,-m1777,-sram%%25\n",
   1188        1.1  dholland 	    tmpfs_on_var_shm() ? "" : "#");
   1189        1.1  dholland 	make_target_dir("/kern");
   1190        1.1  dholland 	make_target_dir("/proc");
   1191        1.1  dholland 	make_target_dir("/dev/pts");
   1192        1.1  dholland 	make_target_dir("/cdrom");
   1193        1.1  dholland 	make_target_dir("/var/shm");
   1194        1.1  dholland 
   1195        1.1  dholland 	scripting_fprintf(NULL, "EOF\n");
   1196        1.1  dholland 
   1197        1.2    martin 	if (dev != NULL)
   1198        1.2    martin 		free(dev);
   1199        1.1  dholland 	fclose(f);
   1200        1.1  dholland 	fflush(NULL);
   1201        1.1  dholland 	return 0;
   1202        1.1  dholland }
   1203        1.1  dholland 
   1204        1.1  dholland 
   1205        1.1  dholland 
   1206        1.1  dholland static int
   1207        1.1  dholland /*ARGSUSED*/
   1208        1.1  dholland foundffs(struct data *list, size_t num)
   1209        1.1  dholland {
   1210        1.1  dholland 	int error;
   1211        1.1  dholland 
   1212        1.1  dholland 	if (num < 2 || strcmp(list[1].u.s_val, "/") == 0 ||
   1213        1.1  dholland 	    strstr(list[2].u.s_val, "noauto") != NULL)
   1214        1.1  dholland 		return 0;
   1215        1.1  dholland 
   1216  1.13.12.2  pgoyette 	error = fsck_preen(list[0].u.s_val, ' '-'a', "ffs", false);
   1217        1.1  dholland 	if (error != 0)
   1218        1.1  dholland 		return error;
   1219        1.1  dholland 
   1220        1.1  dholland 	error = target_mount("", list[0].u.s_val, ' '-'a', list[1].u.s_val);
   1221        1.1  dholland 	if (error != 0) {
   1222        1.1  dholland 		msg_display(MSG_mount_failed, list[0].u.s_val);
   1223        1.9    martin 		if (!ask_noyes(NULL))
   1224        1.1  dholland 			return error;
   1225        1.1  dholland 	}
   1226        1.1  dholland 	return 0;
   1227        1.1  dholland }
   1228        1.1  dholland 
   1229        1.1  dholland #ifdef USE_SYSVBFS
   1230        1.1  dholland static int
   1231        1.1  dholland /*ARGSUSED*/
   1232        1.1  dholland foundsysvbfs(struct data *list, size_t num)
   1233        1.1  dholland {
   1234        1.1  dholland 	int error;
   1235        1.1  dholland 
   1236        1.1  dholland 	if (num < 2 || strcmp(list[1].u.s_val, "/") == 0 ||
   1237        1.1  dholland 	    strstr(list[2].u.s_val, "noauto") != NULL)
   1238        1.1  dholland 		return 0;
   1239        1.1  dholland 
   1240        1.1  dholland 	error = target_mount("", list[0].u.s_val, ' '-'a', list[1].u.s_val);
   1241        1.1  dholland 	if (error != 0)
   1242        1.1  dholland 		return error;
   1243        1.1  dholland 	return 0;
   1244        1.1  dholland }
   1245        1.1  dholland #endif
   1246        1.1  dholland 
   1247        1.1  dholland /*
   1248        1.1  dholland  * Do an fsck. On failure, inform the user by showing a warning
   1249        1.1  dholland  * message and doing menu_ok() before proceeding.
   1250        1.1  dholland  * Returns 0 on success, or nonzero return code from fsck() on failure.
   1251        1.1  dholland  */
   1252        1.1  dholland static int
   1253  1.13.12.2  pgoyette fsck_preen(const char *disk, int ptn, const char *fsname, bool silent)
   1254        1.1  dholland {
   1255        1.1  dholland 	char *prog;
   1256        1.1  dholland 	int error;
   1257        1.1  dholland 
   1258        1.2    martin 	ptn = (ptn < 0)? 0 : 'a' + ptn;
   1259        1.1  dholland 	if (fsname == NULL)
   1260        1.1  dholland 		return 0;
   1261        1.1  dholland 	/* first, check if fsck program exists, if not, assume ok */
   1262        1.1  dholland 	asprintf(&prog, "/sbin/fsck_%s", fsname);
   1263        1.1  dholland 	if (prog == NULL)
   1264        1.1  dholland 		return 0;
   1265       1.12    martin 	if (access(prog, X_OK) != 0) {
   1266       1.12    martin 		free(prog);
   1267        1.1  dholland 		return 0;
   1268       1.12    martin 	}
   1269        1.1  dholland 	if (!strcmp(fsname,"ffs"))
   1270        1.1  dholland 		fixsb(prog, disk, ptn);
   1271  1.13.12.2  pgoyette 	error = run_program(silent? RUN_SILENT|RUN_ERROR_OK : 0, "%s -p -q /dev/r%s%c", prog, disk, ptn);
   1272        1.1  dholland 	free(prog);
   1273  1.13.12.2  pgoyette 	if (error != 0 && !silent) {
   1274        1.1  dholland 		msg_display(MSG_badfs, disk, ptn, error);
   1275        1.9    martin 		if (ask_noyes(NULL))
   1276        1.1  dholland 			error = 0;
   1277        1.1  dholland 		/* XXX at this point maybe we should run a full fsck? */
   1278        1.1  dholland 	}
   1279        1.1  dholland 	return error;
   1280        1.1  dholland }
   1281        1.1  dholland 
   1282        1.1  dholland /* This performs the same function as the etc/rc.d/fixsb script
   1283        1.1  dholland  * which attempts to correct problems with ffs1 filesystems
   1284        1.1  dholland  * which may have been introduced by booting a netbsd-current kernel
   1285        1.1  dholland  * from between April of 2003 and January 2004. For more information
   1286        1.1  dholland  * This script was developed as a response to NetBSD pr install/25138
   1287        1.1  dholland  * Additional prs regarding the original issue include:
   1288        1.1  dholland  *  bin/17910 kern/21283 kern/21404 port-macppc/23925 port-macppc/23926
   1289        1.1  dholland  */
   1290        1.1  dholland static void
   1291        1.1  dholland fixsb(const char *prog, const char *disk, char ptn)
   1292        1.1  dholland {
   1293        1.1  dholland 	int fd;
   1294        1.1  dholland 	int rval;
   1295        1.1  dholland 	union {
   1296        1.1  dholland 		struct fs fs;
   1297        1.1  dholland 		char buf[SBLOCKSIZE];
   1298        1.1  dholland 	} sblk;
   1299        1.1  dholland 	struct fs *fs = &sblk.fs;
   1300        1.1  dholland 
   1301        1.1  dholland 	snprintf(sblk.buf, sizeof(sblk.buf), "/dev/r%s%c",
   1302        1.1  dholland 		disk, ptn == ' ' ? 0 : ptn);
   1303        1.1  dholland 	fd = open(sblk.buf, O_RDONLY);
   1304        1.1  dholland 	if (fd == -1)
   1305        1.1  dholland 		return;
   1306        1.1  dholland 
   1307        1.1  dholland 	/* Read ffsv1 main superblock */
   1308        1.1  dholland 	rval = pread(fd, sblk.buf, sizeof sblk.buf, SBLOCK_UFS1);
   1309        1.1  dholland 	close(fd);
   1310        1.1  dholland 	if (rval != sizeof sblk.buf)
   1311        1.1  dholland 		return;
   1312        1.1  dholland 
   1313        1.1  dholland 	if (fs->fs_magic != FS_UFS1_MAGIC &&
   1314        1.1  dholland 	    fs->fs_magic != FS_UFS1_MAGIC_SWAPPED)
   1315        1.1  dholland 		/* Not FFSv1 */
   1316        1.1  dholland 		return;
   1317        1.1  dholland 	if (fs->fs_old_flags & FS_FLAGS_UPDATED)
   1318        1.1  dholland 		/* properly updated fslevel 4 */
   1319        1.1  dholland 		return;
   1320        1.1  dholland 	if (fs->fs_bsize != fs->fs_maxbsize)
   1321        1.1  dholland 		/* not messed up */
   1322        1.1  dholland 		return;
   1323        1.1  dholland 
   1324        1.1  dholland 	/*
   1325        1.1  dholland 	 * OK we have a munged fs, first 'upgrade' to fslevel 4,
   1326        1.1  dholland 	 * We specify -b16 in order to stop fsck bleating that the
   1327        1.1  dholland 	 * sb doesn't match the first alternate.
   1328        1.1  dholland 	 */
   1329        1.1  dholland 	run_program(RUN_DISPLAY | RUN_PROGRESS,
   1330        1.1  dholland 	    "%s -p -b 16 -c 4 /dev/r%s%c", prog, disk, ptn);
   1331        1.1  dholland 	/* Then downgrade to fslevel 3 */
   1332        1.1  dholland 	run_program(RUN_DISPLAY | RUN_PROGRESS,
   1333        1.1  dholland 	    "%s -p -c 3 /dev/r%s%c", prog, disk, ptn);
   1334        1.1  dholland }
   1335        1.1  dholland 
   1336        1.1  dholland /*
   1337        1.1  dholland  * fsck and mount the root partition.
   1338        1.1  dholland  */
   1339        1.1  dholland static int
   1340        1.1  dholland mount_root(void)
   1341        1.1  dholland {
   1342        1.1  dholland 	int	error;
   1343        1.2    martin 	int ptn = (pm->isspecial)? 0 - 'a' : pm->rootpart;
   1344        1.1  dholland 
   1345  1.13.12.2  pgoyette 	error = fsck_preen(pm->diskdev, ptn, "ffs", false);
   1346        1.1  dholland 	if (error != 0)
   1347        1.1  dholland 		return error;
   1348        1.1  dholland 
   1349        1.1  dholland 	md_pre_mount();
   1350        1.1  dholland 
   1351        1.1  dholland 	/* Mount /dev/<diskdev>a on target's "".
   1352        1.1  dholland 	 * If we pass "" as mount-on, Prefixing will DTRT.
   1353        1.1  dholland 	 * for now, use no options.
   1354        1.1  dholland 	 * XXX consider -o remount in case target root is
   1355        1.1  dholland 	 * current root, still readonly from single-user?
   1356        1.1  dholland 	 */
   1357        1.2    martin 	return target_mount("", pm->diskdev, ptn, "");
   1358        1.1  dholland }
   1359        1.1  dholland 
   1360        1.1  dholland /* Get information on the file systems mounted from the root filesystem.
   1361        1.1  dholland  * Offer to convert them into 4.4BSD inodes if they are not 4.4BSD
   1362        1.1  dholland  * inodes.  Fsck them.  Mount them.
   1363        1.1  dholland  */
   1364        1.1  dholland 
   1365        1.1  dholland int
   1366        1.1  dholland mount_disks(void)
   1367        1.1  dholland {
   1368        1.1  dholland 	char *fstab;
   1369        1.1  dholland 	int   fstabsize;
   1370        1.1  dholland 	int   error;
   1371        1.1  dholland 
   1372        1.1  dholland 	static struct lookfor fstabbuf[] = {
   1373        1.1  dholland 		{"/dev/", "/dev/%s %s ffs %s", "c", NULL, 0, 0, foundffs},
   1374        1.1  dholland 		{"/dev/", "/dev/%s %s ufs %s", "c", NULL, 0, 0, foundffs},
   1375        1.1  dholland #ifdef USE_SYSVBFS
   1376        1.1  dholland 		{"/dev/", "/dev/%s %s sysvbfs %s", "c", NULL, 0, 0,
   1377        1.1  dholland 		    foundsysvbfs},
   1378        1.1  dholland #endif
   1379        1.1  dholland 	};
   1380        1.1  dholland 	static size_t numfstabbuf = sizeof(fstabbuf) / sizeof(struct lookfor);
   1381        1.1  dholland 
   1382        1.1  dholland 	/* First the root device. */
   1383        1.1  dholland 	if (target_already_root())
   1384        1.1  dholland 		/* avoid needing to call target_already_root() again */
   1385        1.1  dholland 		targetroot_mnt[0] = 0;
   1386        1.1  dholland 	else {
   1387        1.1  dholland 		error = mount_root();
   1388        1.1  dholland 		if (error != 0 && error != EBUSY)
   1389        1.1  dholland 			return -1;
   1390        1.1  dholland 	}
   1391        1.1  dholland 
   1392        1.1  dholland 	/* Check the target /etc/fstab exists before trying to parse it. */
   1393        1.1  dholland 	if (target_dir_exists_p("/etc") == 0 ||
   1394        1.1  dholland 	    target_file_exists_p("/etc/fstab") == 0) {
   1395        1.2    martin 		msg_display(MSG_noetcfstab, pm->diskdev);
   1396        1.1  dholland 		process_menu(MENU_ok, NULL);
   1397        1.1  dholland 		return -1;
   1398        1.1  dholland 	}
   1399        1.1  dholland 
   1400        1.1  dholland 
   1401        1.1  dholland 	/* Get fstab entries from the target-root /etc/fstab. */
   1402        1.1  dholland 	fstabsize = target_collect_file(T_FILE, &fstab, "/etc/fstab");
   1403        1.1  dholland 	if (fstabsize < 0) {
   1404        1.1  dholland 		/* error ! */
   1405        1.2    martin 		msg_display(MSG_badetcfstab, pm->diskdev);
   1406        1.1  dholland 		process_menu(MENU_ok, NULL);
   1407        1.2    martin 		return -2;
   1408        1.1  dholland 	}
   1409        1.1  dholland 	error = walk(fstab, (size_t)fstabsize, fstabbuf, numfstabbuf);
   1410        1.1  dholland 	free(fstab);
   1411        1.1  dholland 
   1412        1.1  dholland 	return error;
   1413        1.1  dholland }
   1414        1.1  dholland 
   1415        1.1  dholland int
   1416       1.10     isaki set_swap_if_low_ram(const char *disk, partinfo *pp)
   1417       1.10     isaki {
   1418       1.10     isaki 	if (get_ramsize() <= 32)
   1419       1.10     isaki 		return set_swap(disk, pp);
   1420       1.10     isaki 	return 0;
   1421        1.7       abs }
   1422        1.7       abs 
   1423        1.7       abs int
   1424        1.1  dholland set_swap(const char *disk, partinfo *pp)
   1425        1.1  dholland {
   1426        1.1  dholland 	int i;
   1427        1.1  dholland 	char *cp;
   1428        1.1  dholland 	int rval;
   1429        1.1  dholland 
   1430        1.1  dholland 	if (pp == NULL)
   1431        1.2    martin 		pp = pm->oldlabel;
   1432        1.1  dholland 
   1433        1.1  dholland 	for (i = 0; i < MAXPARTITIONS; i++) {
   1434        1.1  dholland 		if (pp[i].pi_fstype != FS_SWAP)
   1435        1.1  dholland 			continue;
   1436        1.1  dholland 		asprintf(&cp, "/dev/%s%c", disk, 'a' + i);
   1437        1.1  dholland 		rval = swapctl(SWAP_ON, cp, 0);
   1438        1.1  dholland 		free(cp);
   1439        1.1  dholland 		if (rval != 0)
   1440        1.1  dholland 			return -1;
   1441        1.1  dholland 	}
   1442        1.1  dholland 
   1443        1.1  dholland 	return 0;
   1444        1.1  dholland }
   1445        1.1  dholland 
   1446        1.1  dholland int
   1447        1.1  dholland check_swap(const char *disk, int remove_swap)
   1448        1.1  dholland {
   1449        1.1  dholland 	struct swapent *swap;
   1450        1.1  dholland 	char *cp;
   1451        1.1  dholland 	int nswap;
   1452        1.1  dholland 	int l;
   1453        1.1  dholland 	int rval = 0;
   1454        1.1  dholland 
   1455        1.1  dholland 	nswap = swapctl(SWAP_NSWAP, 0, 0);
   1456        1.1  dholland 	if (nswap <= 0)
   1457        1.1  dholland 		return 0;
   1458        1.1  dholland 
   1459        1.1  dholland 	swap = malloc(nswap * sizeof *swap);
   1460        1.1  dholland 	if (swap == NULL)
   1461        1.1  dholland 		return -1;
   1462        1.1  dholland 
   1463        1.1  dholland 	nswap = swapctl(SWAP_STATS, swap, nswap);
   1464        1.1  dholland 	if (nswap < 0)
   1465        1.1  dholland 		goto bad_swap;
   1466        1.1  dholland 
   1467        1.1  dholland 	l = strlen(disk);
   1468        1.1  dholland 	while (--nswap >= 0) {
   1469        1.1  dholland 		/* Should we check the se_dev or se_path? */
   1470        1.1  dholland 		cp = swap[nswap].se_path;
   1471        1.1  dholland 		if (memcmp(cp, "/dev/", 5) != 0)
   1472        1.1  dholland 			continue;
   1473        1.1  dholland 		if (memcmp(cp + 5, disk, l) != 0)
   1474        1.1  dholland 			continue;
   1475        1.1  dholland 		if (!isalpha(*(unsigned char *)(cp + 5 + l)))
   1476        1.1  dholland 			continue;
   1477        1.1  dholland 		if (cp[5 + l + 1] != 0)
   1478        1.1  dholland 			continue;
   1479        1.1  dholland 		/* ok path looks like it is for this device */
   1480        1.1  dholland 		if (!remove_swap) {
   1481        1.1  dholland 			/* count active swap areas */
   1482        1.1  dholland 			rval++;
   1483        1.1  dholland 			continue;
   1484        1.1  dholland 		}
   1485        1.1  dholland 		if (swapctl(SWAP_OFF, cp, 0) == -1)
   1486        1.1  dholland 			rval = -1;
   1487        1.1  dholland 	}
   1488        1.1  dholland 
   1489        1.1  dholland     done:
   1490        1.1  dholland 	free(swap);
   1491        1.1  dholland 	return rval;
   1492        1.1  dholland 
   1493        1.1  dholland     bad_swap:
   1494        1.1  dholland 	rval = -1;
   1495        1.1  dholland 	goto done;
   1496        1.1  dholland }
   1497        1.1  dholland 
   1498        1.1  dholland #ifdef HAVE_BOOTXX_xFS
   1499        1.1  dholland char *
   1500        1.1  dholland bootxx_name(void)
   1501        1.1  dholland {
   1502        1.1  dholland 	int fstype;
   1503        1.1  dholland 	const char *bootxxname;
   1504        1.1  dholland 	char *bootxx;
   1505        1.1  dholland 
   1506        1.1  dholland 	/* check we have boot code for the root partition type */
   1507        1.2    martin 	fstype = pm->bsdlabel[pm->rootpart].pi_fstype;
   1508        1.1  dholland 	switch (fstype) {
   1509        1.1  dholland #if defined(BOOTXX_FFSV1) || defined(BOOTXX_FFSV2)
   1510        1.1  dholland 	case FS_BSDFFS:
   1511        1.2    martin 		if (pm->bsdlabel[pm->rootpart].pi_flags & PIF_FFSv2) {
   1512        1.1  dholland #ifdef BOOTXX_FFSV2
   1513        1.1  dholland 			bootxxname = BOOTXX_FFSV2;
   1514        1.1  dholland #else
   1515        1.1  dholland 			bootxxname = NULL;
   1516        1.1  dholland #endif
   1517        1.1  dholland 		} else {
   1518        1.1  dholland #ifdef BOOTXX_FFSV1
   1519        1.1  dholland 			bootxxname = BOOTXX_FFSV1;
   1520        1.1  dholland #else
   1521        1.1  dholland 			bootxxname = NULL;
   1522        1.1  dholland #endif
   1523        1.1  dholland 		}
   1524        1.1  dholland 		break;
   1525        1.1  dholland #endif
   1526       1.11  pgoyette #ifdef BOOTXX_LFSV2
   1527        1.1  dholland 	case FS_BSDLFS:
   1528       1.11  pgoyette 		bootxxname = BOOTXX_LFSV2;
   1529        1.1  dholland 		break;
   1530        1.1  dholland #endif
   1531        1.1  dholland 	default:
   1532        1.1  dholland 		bootxxname = NULL;
   1533        1.1  dholland 		break;
   1534        1.1  dholland 	}
   1535        1.1  dholland 
   1536        1.1  dholland 	if (bootxxname == NULL)
   1537        1.1  dholland 		return NULL;
   1538        1.1  dholland 
   1539        1.1  dholland 	asprintf(&bootxx, "%s/%s", BOOTXXDIR, bootxxname);
   1540        1.1  dholland 	return bootxx;
   1541        1.1  dholland }
   1542        1.1  dholland #endif
   1543        1.2    martin 
   1544        1.2    martin static int
   1545        1.2    martin get_fsid_by_gptuuid(const char *str)
   1546        1.2    martin {
   1547        1.2    martin 	int i;
   1548        1.2    martin 	uuid_t uuid;
   1549        1.2    martin 	uint32_t status;
   1550        1.2    martin 
   1551        1.2    martin 	uuid_from_string(str, &uuid, &status);
   1552        1.2    martin 	if (status == uuid_s_ok) {
   1553        1.2    martin 		for (i = 0; gpt_filesystems[i].id > 0; i++)
   1554        1.2    martin 			if (uuid_equal(&uuid, &(gpt_filesystems[i].uuid), NULL))
   1555        1.2    martin 				return gpt_filesystems[i].id;
   1556        1.2    martin 	}
   1557        1.2    martin 	return FS_OTHER;
   1558        1.2    martin }
   1559        1.2    martin 
   1560        1.2    martin const char *
   1561        1.2    martin get_gptfs_by_id(int filesystem)
   1562        1.2    martin {
   1563        1.2    martin 	int i;
   1564        1.2    martin 	for (i = 0; gpt_filesystems[i].id > 0; i++)
   1565        1.2    martin 		if (filesystem == gpt_filesystems[i].id)
   1566        1.2    martin 			return gpt_filesystems[i].name;
   1567        1.2    martin 	return NULL;
   1568        1.2    martin }
   1569        1.2    martin 
   1570        1.2    martin /* from dkctl.c */
   1571        1.2    martin static int
   1572        1.2    martin get_dkwedges_sort(const void *a, const void *b)
   1573        1.2    martin {
   1574        1.2    martin 	const struct dkwedge_info *dkwa = a, *dkwb = b;
   1575        1.2    martin 	const daddr_t oa = dkwa->dkw_offset, ob = dkwb->dkw_offset;
   1576        1.2    martin 	return (oa < ob) ? -1 : (oa > ob) ? 1 : 0;
   1577        1.2    martin }
   1578        1.2    martin 
   1579        1.2    martin int
   1580        1.2    martin get_dkwedges(struct dkwedge_info **dkw, const char *diskdev)
   1581        1.2    martin {
   1582        1.2    martin 	int fd;
   1583        1.2    martin 	char buf[STRSIZE];
   1584        1.2    martin 	size_t bufsize;
   1585        1.2    martin 	struct dkwedge_list dkwl;
   1586        1.2    martin 
   1587        1.2    martin 	*dkw = NULL;
   1588        1.2    martin 	dkwl.dkwl_buf = *dkw;
   1589        1.2    martin 	dkwl.dkwl_bufsize = 0;
   1590        1.2    martin 	fd = opendisk(diskdev, O_RDONLY, buf, STRSIZE, 0);
   1591        1.2    martin 	if (fd < 0)
   1592        1.2    martin 		return -1;
   1593        1.2    martin 
   1594        1.2    martin 	for (;;) {
   1595        1.2    martin 		if (ioctl(fd, DIOCLWEDGES, &dkwl) == -1)
   1596        1.2    martin 			return -2;
   1597        1.2    martin 		if (dkwl.dkwl_nwedges == dkwl.dkwl_ncopied)
   1598        1.2    martin 			break;
   1599        1.2    martin 		bufsize = dkwl.dkwl_nwedges * sizeof(**dkw);
   1600        1.2    martin 		if (dkwl.dkwl_bufsize < bufsize) {
   1601        1.2    martin 			*dkw = realloc(dkwl.dkwl_buf, bufsize);
   1602        1.2    martin 			if (*dkw == NULL)
   1603        1.2    martin 				return -3;
   1604        1.2    martin 			dkwl.dkwl_buf = *dkw;
   1605        1.2    martin 			dkwl.dkwl_bufsize = bufsize;
   1606        1.2    martin 		}
   1607        1.2    martin 	}
   1608        1.2    martin 
   1609        1.5    martin 	if (dkwl.dkwl_nwedges > 0 && *dkw != NULL)
   1610        1.2    martin 		qsort(*dkw, dkwl.dkwl_nwedges, sizeof(**dkw), get_dkwedges_sort);
   1611        1.2    martin 
   1612        1.2    martin 	close(fd);
   1613        1.2    martin 	return dkwl.dkwl_nwedges;
   1614        1.2    martin }
   1615        1.2    martin 
   1616        1.2    martin /* XXX: rewrite */
   1617        1.2    martin static int
   1618        1.2    martin incoregpt(pm_devs_t *pm_cur, partinfo *lp)
   1619        1.2    martin {
   1620        1.2    martin 	int i, num;
   1621        1.4       riz 	unsigned int p_num;
   1622        1.4       riz 	uint64_t p_start, p_size;
   1623        1.2    martin 	char *textbuf, *t, *tt, p_type[STRSIZE];
   1624        1.2    martin 	struct dkwedge_info *dkw;
   1625        1.2    martin 
   1626        1.2    martin 	num = get_dkwedges(&dkw, pm_cur->diskdev);
   1627        1.2    martin 	if (dkw != NULL) {
   1628        1.2    martin 		for (i = 0; i < num && i < MAX_WEDGES; i++)
   1629        1.2    martin 			run_program(RUN_SILENT, "dkctl %s delwedge %s",
   1630        1.2    martin 				pm_cur->diskdev, dkw[i].dkw_devname);
   1631        1.2    martin 		free (dkw);
   1632        1.2    martin 	}
   1633        1.2    martin 
   1634        1.2    martin 	if (collect(T_OUTPUT, &textbuf, "gpt show -u %s 2>/dev/null", pm_cur->diskdev) < 1)
   1635        1.2    martin 		return -1;
   1636        1.2    martin 
   1637        1.2    martin 	(void)strtok(textbuf, "\n"); /* ignore first line */
   1638        1.2    martin 	while ((t = strtok(NULL, "\n")) != NULL) {
   1639        1.4       riz 		i = 0; p_start = 0; p_size = 0; p_num = 0; strcpy(p_type, ""); /* init */
   1640        1.2    martin 		while ((tt = strsep(&t, " \t")) != NULL) {
   1641        1.4       riz 			if (strlen(tt) == 0)
   1642        1.2    martin 				continue;
   1643        1.2    martin 			if (i == 0)
   1644        1.4       riz 				p_start = strtouq(tt, NULL, 10);
   1645        1.2    martin 			if (i == 1)
   1646        1.4       riz 				p_size = strtouq(tt, NULL, 10);
   1647        1.2    martin 			if (i == 2)
   1648        1.4       riz 				p_num = strtouq(tt, NULL, 10);
   1649        1.2    martin 			if (i > 2 || (i == 2 && p_num == 0))
   1650        1.2    martin 				if (
   1651        1.2    martin 					strcmp(tt, "GPT") &&
   1652        1.2    martin 					strcmp(tt, "part") &&
   1653        1.2    martin 					strcmp(tt, "-")
   1654        1.2    martin 					)
   1655       1.13  christos 						strlcat(p_type, tt, STRSIZE);
   1656        1.2    martin 			i++;
   1657        1.2    martin 		}
   1658        1.4       riz 		if (p_start == 0 || p_size == 0)
   1659        1.2    martin 			continue;
   1660        1.2    martin 		else if (! strcmp(p_type, "Pritable"))
   1661        1.2    martin 			pm_cur->ptstart = p_start + p_size;
   1662        1.2    martin 		else if (! strcmp(p_type, "Sectable"))
   1663        1.2    martin 			pm_cur->ptsize = p_start - pm_cur->ptstart - 1;
   1664        1.4       riz 		else if (p_num == 0 && strlen(p_type) > 0)
   1665        1.2    martin 			/* Utilitary entry (PMBR, etc) */
   1666        1.2    martin 			continue;
   1667        1.4       riz 		else if (p_num == 0) {
   1668        1.2    martin 			/* Free space */
   1669        1.2    martin 			continue;
   1670        1.2    martin 		} else {
   1671        1.2    martin 			/* Usual partition */
   1672        1.2    martin 			lp[p_num].pi_size = p_size;
   1673        1.2    martin 			lp[p_num].pi_offset = p_start;
   1674        1.2    martin 			lp[p_num].pi_fstype = get_fsid_by_gptuuid(p_type);
   1675        1.2    martin 		}
   1676        1.2    martin 	}
   1677        1.2    martin 	free(textbuf);
   1678        1.2    martin 
   1679        1.2    martin 	return 0;
   1680        1.2    martin }
   1681        1.2    martin 
   1682        1.6    martin static bool
   1683        1.2    martin is_gpt(const char *dev)
   1684        1.2    martin {
   1685  1.13.12.1  pgoyette 
   1686        1.8    martin 	check_available_binaries();
   1687        1.8    martin 
   1688        1.8    martin 	if (!have_gpt)
   1689        1.6    martin 		return false;
   1690        1.6    martin 
   1691  1.13.12.1  pgoyette 	return run_program(RUN_SILENT | RUN_ERROR_OK,
   1692  1.13.12.1  pgoyette 		"gpt -qr header %s", dev) == 0;
   1693        1.2    martin }
   1694