xref: /netbsd-src/usr.sbin/sysinst/disks.c (revision 2d8e86c2f207da6fbbd50f11b6f33765ebdfa0e9)
1 /*	$NetBSD: disks.c,v 1.50 2019/08/08 13:45:19 martin Exp $ */
2 
3 /*
4  * Copyright 1997 Piermont Information Systems Inc.
5  * All rights reserved.
6  *
7  * Written by Philip A. Nelson for Piermont Information Systems Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. The name of Piermont Information Systems Inc. may not be used to endorse
18  *    or promote products derived from this software without specific prior
19  *    written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31  * THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34 
35 /* disks.c -- routines to deal with finding disks and labeling disks. */
36 
37 
38 #include <assert.h>
39 #include <errno.h>
40 #include <inttypes.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <unistd.h>
44 #include <fcntl.h>
45 #include <fnmatch.h>
46 #include <util.h>
47 #include <uuid.h>
48 #include <paths.h>
49 #include <fstab.h>
50 
51 #include <sys/param.h>
52 #include <sys/sysctl.h>
53 #include <sys/swap.h>
54 #include <sys/disklabel_gpt.h>
55 #include <ufs/ufs/dinode.h>
56 #include <ufs/ffs/fs.h>
57 
58 #include <dev/scsipi/scsipi_all.h>
59 #include <sys/scsiio.h>
60 
61 #include <dev/ata/atareg.h>
62 #include <sys/ataio.h>
63 
64 #include "defs.h"
65 #include "md.h"
66 #include "msg_defs.h"
67 #include "menu_defs.h"
68 #include "txtwalk.h"
69 
70 /* #define DEBUG_VERBOSE	1 */
71 
72 /* Disk descriptions */
73 struct disk_desc {
74 	char	dd_name[SSTRSIZE];
75 	char	dd_descr[256];
76 	bool	dd_no_mbr, dd_no_part;
77 	uint	dd_cyl;
78 	uint	dd_head;
79 	uint	dd_sec;
80 	uint	dd_secsize;
81 	daddr_t	dd_totsec;
82 };
83 
84 #define	NAME_PREFIX	"NAME="
85 static const char name_prefix[] = NAME_PREFIX;
86 
87 /* things we could have as /sbin/newfs_* and /sbin/fsck_* */
88 static const char *extern_fs_with_chk[] = {
89 	"ext2fs", "lfs", "msdos", "v7fs"
90 };
91 
92 /* things we could have as /sbin/newfs_* but not /sbin/fsck_* */
93 static const char *extern_fs_newfs_only[] = {
94 	"sysvbfs", "udf"
95 };
96 
97 /* Local prototypes */
98 static int found_fs(struct data *, size_t, const struct lookfor*);
99 static int found_fs_nocheck(struct data *, size_t, const struct lookfor*);
100 static int fsck_preen(const char *, const char *, bool silent);
101 static void fixsb(const char *, const char *);
102 
103 
104 static bool tmpfs_on_var_shm(void);
105 
106 const char *
107 getfslabelname(uint f, uint f_version)
108 {
109 	if (f == FS_TMPFS)
110 		return "tmpfs";
111 	else if (f == FS_MFS)
112 		return "mfs";
113 	else if (f == FS_BSDFFS && f_version > 0)
114 		return f_version == 2 ?
115 		    msg_string(MSG_fs_type_ffsv2) : msg_string(MSG_fs_type_ffs);
116 	else if (f >= __arraycount(fstypenames) || fstypenames[f] == NULL)
117 		return "invalid";
118 	return fstypenames[f];
119 }
120 
121 /*
122  * Decide wether we want to mount a tmpfs on /var/shm: we do this always
123  * when the machine has more than 16 MB of user memory. On smaller machines,
124  * shm_open() and friends will not perform well anyway.
125  */
126 static bool
127 tmpfs_on_var_shm()
128 {
129 	uint64_t ram;
130 	size_t len;
131 
132 	len = sizeof(ram);
133 	if (sysctlbyname("hw.usermem64", &ram, &len, NULL, 0))
134 		return false;
135 
136 	return ram > 16 * MEG;
137 }
138 
139 /* from src/sbin/atactl/atactl.c
140  * extract_string: copy a block of bytes out of ataparams and make
141  * a proper string out of it, truncating trailing spaces and preserving
142  * strict typing. And also, not doing unaligned accesses.
143  */
144 static void
145 ata_extract_string(char *buf, size_t bufmax,
146 		   uint8_t *bytes, unsigned numbytes,
147 		   int needswap)
148 {
149 	unsigned i;
150 	size_t j;
151 	unsigned char ch1, ch2;
152 
153 	for (i = 0, j = 0; i < numbytes; i += 2) {
154 		ch1 = bytes[i];
155 		ch2 = bytes[i+1];
156 		if (needswap && j < bufmax-1) {
157 			buf[j++] = ch2;
158 		}
159 		if (j < bufmax-1) {
160 			buf[j++] = ch1;
161 		}
162 		if (!needswap && j < bufmax-1) {
163 			buf[j++] = ch2;
164 		}
165 	}
166 	while (j > 0 && buf[j-1] == ' ') {
167 		j--;
168 	}
169 	buf[j] = '\0';
170 }
171 
172 /*
173  * from src/sbin/scsictl/scsi_subr.c
174  */
175 #define STRVIS_ISWHITE(x) ((x) == ' ' || (x) == '\0' || (x) == (u_char)'\377')
176 
177 static void
178 scsi_strvis(char *sdst, size_t dlen, const char *ssrc, size_t slen)
179 {
180 	u_char *dst = (u_char *)sdst;
181 	const u_char *src = (const u_char *)ssrc;
182 
183 	/* Trim leading and trailing blanks and NULs. */
184 	while (slen > 0 && STRVIS_ISWHITE(src[0]))
185 		++src, --slen;
186 	while (slen > 0 && STRVIS_ISWHITE(src[slen - 1]))
187 		--slen;
188 
189 	while (slen > 0) {
190 		if (*src < 0x20 || *src >= 0x80) {
191 			/* non-printable characters */
192 			dlen -= 4;
193 			if (dlen < 1)
194 				break;
195 			*dst++ = '\\';
196 			*dst++ = ((*src & 0300) >> 6) + '0';
197 			*dst++ = ((*src & 0070) >> 3) + '0';
198 			*dst++ = ((*src & 0007) >> 0) + '0';
199 		} else if (*src == '\\') {
200 			/* quote characters */
201 			dlen -= 2;
202 			if (dlen < 1)
203 				break;
204 			*dst++ = '\\';
205 			*dst++ = '\\';
206 		} else {
207 			/* normal characters */
208 			if (--dlen < 1)
209 				break;
210 			*dst++ = *src;
211 		}
212 		++src, --slen;
213 	}
214 
215 	*dst++ = 0;
216 }
217 
218 
219 static int
220 get_descr_scsi(struct disk_desc *dd)
221 {
222 	struct scsipi_inquiry_data inqbuf;
223 	struct scsipi_inquiry cmd;
224 	scsireq_t req;
225         /* x4 in case every character is escaped, +1 for NUL. */
226 	char vendor[(sizeof(inqbuf.vendor) * 4) + 1],
227 	     product[(sizeof(inqbuf.product) * 4) + 1],
228 	     revision[(sizeof(inqbuf.revision) * 4) + 1];
229 	char size[5];
230 
231 	memset(&inqbuf, 0, sizeof(inqbuf));
232 	memset(&cmd, 0, sizeof(cmd));
233 	memset(&req, 0, sizeof(req));
234 
235 	cmd.opcode = INQUIRY;
236 	cmd.length = sizeof(inqbuf);
237 	memcpy(req.cmd, &cmd, sizeof(cmd));
238 	req.cmdlen = sizeof(cmd);
239 	req.databuf = &inqbuf;
240 	req.datalen = sizeof(inqbuf);
241 	req.timeout = 10000;
242 	req.flags = SCCMD_READ;
243 	req.senselen = SENSEBUFLEN;
244 
245 	if (!disk_ioctl(dd->dd_name, SCIOCCOMMAND, &req)
246 	    || req.retsts != SCCMD_OK)
247 		return 0;
248 
249 	scsi_strvis(vendor, sizeof(vendor), inqbuf.vendor,
250 	    sizeof(inqbuf.vendor));
251 	scsi_strvis(product, sizeof(product), inqbuf.product,
252 	    sizeof(inqbuf.product));
253 	scsi_strvis(revision, sizeof(revision), inqbuf.revision,
254 	    sizeof(inqbuf.revision));
255 
256 	humanize_number(size, sizeof(size),
257 	    (uint64_t)dd->dd_secsize * (uint64_t)dd->dd_totsec,
258 	    "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
259 
260 	snprintf(dd->dd_descr, sizeof(dd->dd_descr),
261 	    "%s (%s, %s %s)",
262 	    dd->dd_name, size, vendor, product);
263 
264 	return 1;
265 }
266 
267 static int
268 get_descr_ata(struct disk_desc *dd)
269 {
270 	struct atareq req;
271 	static union {
272 		unsigned char inbuf[DEV_BSIZE];
273 		struct ataparams inqbuf;
274 	} inbuf;
275 	struct ataparams *inqbuf = &inbuf.inqbuf;
276 	char model[sizeof(inqbuf->atap_model)+1];
277 	char size[5];
278 	int needswap = 0;
279 
280 	memset(&inbuf, 0, sizeof(inbuf));
281 	memset(&req, 0, sizeof(req));
282 
283 	req.flags = ATACMD_READ;
284 	req.command = WDCC_IDENTIFY;
285 	req.databuf = (void *)&inbuf;
286 	req.datalen = sizeof(inbuf);
287 	req.timeout = 1000;
288 
289 	if (!disk_ioctl(dd->dd_name, ATAIOCCOMMAND, &req)
290 	    || req.retsts != ATACMD_OK)
291 		return 0;
292 
293 #if BYTE_ORDER == LITTLE_ENDIAN
294 	/*
295 	 * On little endian machines, we need to shuffle the string
296 	 * byte order.  However, we don't have to do this for NEC or
297 	 * Mitsumi ATAPI devices
298 	 */
299 
300 	if (!(inqbuf->atap_config != WDC_CFG_CFA_MAGIC &&
301 	      (inqbuf->atap_config & WDC_CFG_ATAPI) &&
302 	      ((inqbuf->atap_model[0] == 'N' &&
303 	        inqbuf->atap_model[1] == 'E') ||
304 	       (inqbuf->atap_model[0] == 'F' &&
305 	        inqbuf->atap_model[1] == 'X')))) {
306 		needswap = 1;
307 	}
308 #endif
309 
310 	ata_extract_string(model, sizeof(model),
311 	    inqbuf->atap_model, sizeof(inqbuf->atap_model), needswap);
312 	humanize_number(size, sizeof(size),
313 	    (uint64_t)dd->dd_secsize * (uint64_t)dd->dd_totsec,
314 	    "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
315 
316 	snprintf(dd->dd_descr, sizeof(dd->dd_descr), "%s (%s, %s)",
317 	    dd->dd_name, size, model);
318 
319 	return 1;
320 }
321 
322 static void
323 get_descr(struct disk_desc *dd)
324 {
325 	char size[5];
326 	dd->dd_descr[0] = '\0';
327 
328 	/* try ATA */
329 	if (get_descr_ata(dd))
330 		goto done;
331 	/* try SCSI */
332 	if (get_descr_scsi(dd))
333 		goto done;
334 
335 	/* XXX: identify for ld @ NVME or microSD */
336 
337 	/* XXX: get description from raid, cgd, vnd... */
338 done:
339 	/* punt, just give some generic info */
340 	humanize_number(size, sizeof(size),
341 	    (uint64_t)dd->dd_secsize * (uint64_t)dd->dd_totsec,
342 	    "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
343 
344 	snprintf(dd->dd_descr, sizeof(dd->dd_descr),
345 	    "%s (%s)", dd->dd_name, size);
346 }
347 
348 /*
349  * State for helper callback for get_default_cdrom
350  */
351 struct default_cdrom_data {
352 	char *device;
353 	size_t max_len;
354 	bool found;
355 };
356 
357 /*
358  * Helper function for get_default_cdrom, gets passed a device
359  * name and a void pointer to default_cdrom_data.
360  */
361 static bool
362 get_default_cdrom_helper(void *state, const char *dev)
363 {
364 	struct default_cdrom_data *data = state;
365 
366 	if (!is_cdrom_device(dev, false))
367 		return true;
368 
369 	strlcpy(data->device, dev, data->max_len);
370 	strlcat(data->device, "a", data->max_len); /* default to partition a */
371 	data->found = true;
372 
373 	return false;	/* one is enough, stop iteration */
374 }
375 
376 /*
377  * Set the argument to the name of the first CD devices actually
378  * available, leave it unmodified otherwise.
379  * Return true if a device has been found.
380  */
381 bool
382 get_default_cdrom(char *cd, size_t max_len)
383 {
384 	struct default_cdrom_data state;
385 
386 	state.device = cd;
387 	state.max_len = max_len;
388 	state.found = false;
389 
390 	if (enumerate_disks(&state, get_default_cdrom_helper))
391 		return state.found;
392 
393 	return false;
394 }
395 
396 static bool
397 get_wedge_descr(struct disk_desc *dd)
398 {
399 	struct dkwedge_info dkw;
400 
401 	if (!get_wedge_info(dd->dd_name, &dkw))
402 		return false;
403 
404 	snprintf(dd->dd_descr, sizeof(dd->dd_descr), "%s (%s@%s)",
405 	    dkw.dkw_wname, dkw.dkw_devname, dkw.dkw_parent);
406 	return true;
407 }
408 
409 static bool
410 get_name_and_parent(const char *dev, char *name, char *parent)
411 {
412 	struct dkwedge_info dkw;
413 
414 	if (!get_wedge_info(dev, &dkw))
415 		return false;
416 	strcpy(name, (const char *)dkw.dkw_wname);
417 	strcpy(parent, dkw.dkw_parent);
418 	return true;
419 }
420 
421 static bool
422 find_swap_part_on(const char *dev, char *swap_name)
423 {
424 	struct dkwedge_list dkwl;
425 	struct dkwedge_info *dkw;
426 	u_int i;
427 	bool res = false;
428 
429 	if (!get_wedge_list(dev, &dkwl))
430 		return false;
431 
432 	dkw = dkwl.dkwl_buf;
433 	for (i = 0; i < dkwl.dkwl_nwedges; i++) {
434 		res = strcmp(dkw[i].dkw_ptype, DKW_PTYPE_SWAP) == 0;
435 		if (res) {
436 			strcpy(swap_name, (const char*)dkw[i].dkw_wname);
437 			break;
438 		}
439 	}
440 	free(dkwl.dkwl_buf);
441 
442 	return res;
443 }
444 
445 static bool
446 is_ffs_wedge(const char *dev)
447 {
448 	struct dkwedge_info dkw;
449 
450 	if (!get_wedge_info(dev, &dkw))
451 		return false;
452 
453 	return strcmp(dkw.dkw_ptype, DKW_PTYPE_FFS) == 0;
454 }
455 
456 /*
457  * Does this device match an entry in our default CDROM device list?
458  * If looking for install targets, we also flag floopy devices.
459  */
460 bool
461 is_cdrom_device(const char *dev, bool as_target)
462 {
463 	static const char *target_devices[] = {
464 #ifdef CD_NAMES
465 		CD_NAMES
466 #endif
467 #if defined(CD_NAMES) && defined(FLOPPY_NAMES)
468 		,
469 #endif
470 #ifdef FLOPPY_NAMES
471 		FLOPPY_NAMES
472 #endif
473 #if defined(CD_NAMES) || defined(FLOPPY_NAMES)
474 		,
475 #endif
476 		0
477 	};
478 	static const char *src_devices[] = {
479 #ifdef CD_NAMES
480 		CD_NAMES ,
481 #endif
482 		0
483 	};
484 
485 	for (const char **dev_pat = as_target ? target_devices : src_devices;
486 	     *dev_pat; dev_pat++)
487 		if (fnmatch(*dev_pat, dev, 0) == 0)
488 			return true;
489 
490 	return false;
491 }
492 
493 /* does this device match any entry in the driver list? */
494 static bool
495 dev_in_list(const char *dev, const char **list)
496 {
497 
498 	for ( ; *list; list++) {
499 
500 		size_t len = strlen(*list);
501 
502 		/* start of name matches? */
503 		if (strncmp(dev, *list, len) == 0) {
504 			char *endp;
505 			int e;
506 
507 			/* remainder of name is a decimal number? */
508 			strtou(dev+len, &endp, 10, 0, INT_MAX, &e);
509 			if (endp && *endp == 0 && e == 0)
510 				return true;
511 		}
512 	}
513 
514 	return false;
515 }
516 
517 bool
518 is_bootable_device(const char *dev)
519 {
520 	static const char *non_bootable_devs[] = {
521 		"raid",	/* bootcode lives outside of raid */
522 		"xbd",	/* xen virtual device, can not boot from that */
523 		NULL
524 	};
525 
526 	return !dev_in_list(dev, non_bootable_devs);
527 }
528 
529 bool
530 is_partitionable_device(const char *dev)
531 {
532 	static const char *non_partitionable_devs[] = {
533 		"dk",	/* this is already a partitioned slice */
534 		NULL
535 	};
536 
537 	return !dev_in_list(dev, non_partitionable_devs);
538 }
539 
540 /*
541  * Multi-purpose helper function:
542  * iterate all known disks, invoke a callback for each.
543  * Stop iteration when the callback returns false.
544  * Return true when iteration actually happend, false on error.
545  */
546 bool
547 enumerate_disks(void *state, bool (*func)(void *state, const char *dev))
548 {
549 	static const int mib[] = { CTL_HW, HW_DISKNAMES };
550 	static const unsigned int miblen = __arraycount(mib);
551 	const char *xd;
552 	char *disk_names;
553 	size_t len;
554 
555 	if (sysctl(mib, miblen, NULL, &len, NULL, 0) == -1)
556 		return false;
557 
558 	disk_names = malloc(len);
559 	if (disk_names == NULL)
560 		return false;
561 
562 	if (sysctl(mib, miblen, disk_names, &len, NULL, 0) == -1) {
563 		free(disk_names);
564 		return false;
565 	}
566 
567 	for (xd = strtok(disk_names, " "); xd != NULL; xd = strtok(NULL, " ")) {
568 		if (!(*func)(state, xd))
569 			break;
570 	}
571 	free(disk_names);
572 
573 	return true;
574 }
575 
576 /*
577  * Helper state for get_disks
578  */
579 struct get_disks_state {
580 	int numdisks;
581 	struct disk_desc *dd;
582 	bool with_non_partitionable;
583 };
584 
585 /*
586  * Helper function for get_disks enumartion
587  */
588 static bool
589 get_disks_helper(void *arg, const char *dev)
590 {
591 	struct get_disks_state *state = arg;
592 	struct disk_geom geo;
593 
594 	/* is this a CD device? */
595 	if (is_cdrom_device(dev, true))
596 		return true;
597 
598 	memset(state->dd, 0, sizeof(*state->dd));
599 	strlcpy(state->dd->dd_name, dev, sizeof state->dd->dd_name - 2);
600 	state->dd->dd_no_mbr = !is_bootable_device(dev);
601 	state->dd->dd_no_part = !is_partitionable_device(dev);
602 
603 	if (state->dd->dd_no_part && !state->with_non_partitionable)
604 		return true;
605 
606 	if (!get_disk_geom(state->dd->dd_name, &geo)) {
607 		if (errno == ENOENT)
608 			return true;
609 		if (errno != ENOTTY || !state->dd->dd_no_part)
610 			/*
611 			 * Allow plain partitions,
612 			 * like already existing wedges
613 			 * (like dk0) if marked as
614 			 * non-partitioning device.
615 			 * For all other cases, continue
616 			 * with the next disk.
617 			 */
618 			return true;
619 		if (!is_ffs_wedge(state->dd->dd_name))
620 			return true;
621 	}
622 
623 	/*
624 	 * Exclude a disk mounted as root partition,
625 	 * in case of install-image on a USB memstick.
626 	 */
627 	if (is_active_rootpart(state->dd->dd_name,
628 	    state->dd->dd_no_part ? -1 : 0))
629 		return true;
630 
631 	state->dd->dd_cyl = geo.dg_ncylinders;
632 	state->dd->dd_head = geo.dg_ntracks;
633 	state->dd->dd_sec = geo.dg_nsectors;
634 	state->dd->dd_secsize = geo.dg_secsize;
635 	state->dd->dd_totsec = geo.dg_secperunit;
636 
637 	if (!state->dd->dd_no_part || !get_wedge_descr(state->dd))
638 		get_descr(state->dd);
639 	state->dd++;
640 	state->numdisks++;
641 	if (state->numdisks == MAX_DISKS)
642 		return false;
643 
644 	return true;
645 }
646 
647 /*
648  * Get all disk devices that are not CDs.
649  * Optionally leave out those that can not be partitioned further.
650  */
651 static int
652 get_disks(struct disk_desc *dd, bool with_non_partitionable)
653 {
654 	struct get_disks_state state;
655 
656 	/* initialize */
657 	state.numdisks = 0;
658 	state.dd = dd;
659 	state.with_non_partitionable = with_non_partitionable;
660 
661 	if (enumerate_disks(&state, get_disks_helper))
662 		return state.numdisks;
663 
664 	return 0;
665 }
666 
667 #ifdef DEBUG_VERBOSE
668 static void
669 dump_parts(const struct disk_partitions *parts)
670 {
671 	fprintf(stderr, "%s partitions on %s:\n",
672 	    MSG_XLAT(parts->pscheme->short_name), parts->disk);
673 
674 	for (size_t p = 0; p < parts->num_part; p++) {
675 		struct disk_part_info info;
676 
677 		if (parts->pscheme->get_part_info(
678 		    parts, p, &info)) {
679 			fprintf(stderr, " #%zu: start: %" PRIu64 " "
680 			    "size: %" PRIu64 ", flags: %x\n",
681 			    p, info.start, info.size,
682 			    info.flags);
683 			if (info.nat_type)
684 				fprintf(stderr, "\ttype: %s\n",
685 				    info.nat_type->description);
686 		} else {
687 			fprintf(stderr, "failed to get info "
688 			    "for partition #%zu\n", p);
689 		}
690 	}
691 	fprintf(stderr, "%" PRIu64 " sectors free, disk size %" PRIu64
692 	    " sectors, %zu partitions used\n", parts->free_space,
693 	    parts->disk_size, parts->num_part);
694 }
695 #endif
696 
697 static bool
698 delete_scheme(struct pm_devs *p)
699 {
700 
701 	if (!ask_noyes(MSG_removepartswarn))
702 		return false;
703 
704 	p->parts->pscheme->free(p->parts);
705 	p->parts = NULL;
706 	return true;
707 }
708 
709 
710 static void
711 convert_copy(struct disk_partitions *old_parts,
712     struct disk_partitions *new_parts)
713 {
714 	struct disk_part_info oinfo, ninfo;
715 	part_id i;
716 
717 	for (i = 0; i < old_parts->num_part; i++) {
718 		if (!old_parts->pscheme->get_part_info(old_parts, i, &oinfo))
719 			continue;
720 
721 		if (oinfo.flags & PTI_PSCHEME_INTERNAL)
722 			continue;
723 
724 		if (oinfo.flags & PTI_SEC_CONTAINER) {
725 		    	if (old_parts->pscheme->secondary_partitions) {
726 				struct disk_partitions *sec_part =
727 					old_parts->pscheme->
728 					    secondary_partitions(
729 					    old_parts, oinfo.start, false);
730 				if (sec_part)
731 					convert_copy(sec_part, new_parts);
732 			}
733 			continue;
734 		}
735 
736 		if (!new_parts->pscheme->adapt_foreign_part_info(new_parts,
737 			    &oinfo, &ninfo))
738 			continue;
739 		new_parts->pscheme->add_partition(new_parts, &ninfo, NULL);
740 	}
741 }
742 
743 bool
744 convert_scheme(struct pm_devs *p, bool is_boot_drive, const char **err_msg)
745 {
746 	struct disk_partitions *old_parts, *new_parts;
747 	const struct disk_partitioning_scheme *new_scheme;
748 
749 	*err_msg = NULL;
750 
751 	old_parts = p->parts;
752 	new_scheme = select_part_scheme(p, old_parts->pscheme,
753 	    false, MSG_select_other_partscheme);
754 
755 	if (new_scheme == NULL)
756 		return false;
757 
758 	new_parts = new_scheme->create_new_for_disk(p->diskdev,
759 	    0, p->dlsize, p->dlsize, is_boot_drive);
760 	if (new_parts == NULL)
761 		return false;
762 
763 	convert_copy(old_parts, new_parts);
764 
765 	if (new_parts->num_part == 0) {
766 		/* need to cleanup */
767 		new_parts->pscheme->free(new_parts);
768 		return false;
769 	}
770 
771 	old_parts->pscheme->free(old_parts);
772 	p->parts = new_parts;
773 	return true;
774 }
775 
776 static struct pm_devs *
777 dummy_whole_system_pm(void)
778 {
779 	static struct pm_devs whole_system = {
780 		.diskdev = "/",
781 		.no_mbr = true,
782 		.no_part = true,
783 		.cur_system = true,
784 	};
785 	static bool init = false;
786 
787 	if (!init) {
788 		strlcpy(whole_system.diskdev_descr,
789 		    msg_string(MSG_running_system),
790 		    sizeof whole_system.diskdev_descr);
791 	}
792 
793 	return &whole_system;
794 }
795 
796 int
797 find_disks(const char *doingwhat, bool allow_cur_system)
798 {
799 	struct disk_desc disks[MAX_DISKS];
800 	/* need two more menu entries: current system + extended partitioning */
801 	menu_ent dsk_menu[__arraycount(disks) + 2];
802 	struct disk_desc *disk;
803 	int i = 0, skipped = 0;
804 	int already_found, numdisks, selected_disk = -1;
805 	int menu_no;
806 	struct pm_devs *pm_i, *pm_last = NULL;
807 
808 	memset(dsk_menu, 0, sizeof(dsk_menu));
809 
810 	/* Find disks. */
811 	numdisks = get_disks(disks, partman_go <= 0);
812 
813 	/* need a redraw here, kernel messages hose everything */
814 	touchwin(stdscr);
815 	refresh();
816 	/* Kill typeahead, it won't be what the user had in mind */
817 	fpurge(stdin);
818 
819 	/*
820 	 * partman_go: <0 - we want to see menu with extended partitioning
821 	 *            ==0 - we want to see simple select disk menu
822 	 *             >0 - we do not want to see any menus, just detect
823 	 *                  all disks
824 	 */
825 	if (partman_go <= 0) {
826 		if (numdisks == 0 && !allow_cur_system) {
827 			/* No disks found! */
828 			hit_enter_to_continue(MSG_nodisk, NULL);
829 			/*endwin();*/
830 			return -1;
831 		} else {
832 			/* One or more disks found or current system allowed */
833 			i = 0;
834 			if (allow_cur_system) {
835 				dsk_menu[i].opt_name = MSG_running_system;
836 				dsk_menu[i].opt_flags = OPT_EXIT;
837 				dsk_menu[i].opt_action = set_menu_select;
838 				i++;
839 			}
840 			for (; i < numdisks+allow_cur_system; i++) {
841 				dsk_menu[i].opt_name =
842 				    disks[i-allow_cur_system].dd_descr;
843 				dsk_menu[i].opt_flags = OPT_EXIT;
844 				dsk_menu[i].opt_action = set_menu_select;
845 			}
846 			if (partman_go < 0) {
847 				dsk_menu[i].opt_name = MSG_partman;
848 				dsk_menu[i].opt_flags = OPT_EXIT;
849 				dsk_menu[i].opt_action = set_menu_select;
850 				i++;
851 			}
852 			menu_no = new_menu(MSG_Available_disks,
853 				dsk_menu, i, -1,
854 				 4, 0, 0, MC_SCROLL,
855 				NULL, NULL, NULL, NULL, NULL);
856 			if (menu_no == -1)
857 				return -1;
858 			msg_fmt_display(MSG_ask_disk, "%s", doingwhat);
859 			process_menu(menu_no, &selected_disk);
860 			free_menu(menu_no);
861 			if (allow_cur_system) {
862 				if (selected_disk == 0) {
863 					pm = dummy_whole_system_pm();
864 					return 1;
865 				} else {
866 					selected_disk--;
867 				}
868 			}
869 		}
870 		if (partman_go < 0 && selected_disk == numdisks) {
871 			partman_go = 1;
872 			return -2;
873 		} else
874 			partman_go = 0;
875 		if (selected_disk < 0 || selected_disk >= numdisks)
876 			return -1;
877 	}
878 
879 	/* Fill pm struct with device(s) info */
880 	for (i = 0; i < numdisks; i++) {
881 		if (! partman_go)
882 			disk = disks + selected_disk;
883 		else {
884 			disk = disks + i;
885 			already_found = 0;
886 			SLIST_FOREACH(pm_i, &pm_head, l) {
887 				pm_last = pm_i;
888 				if (strcmp(pm_i->diskdev, disk->dd_name) == 0) {
889 					already_found = 1;
890 					break;
891 				}
892 			}
893 			if (pm_i != NULL && already_found) {
894 				/*
895 				 * We already added this device, but
896 				 * partitions might have changed
897 				 */
898 				if (!pm_i->found) {
899 					pm_i->found = true;
900 					if (pm_i->parts == NULL) {
901 						pm_i->parts =
902 						    partitions_read_disk(
903 						    pm_i->diskdev,
904 						    disk->dd_totsec);
905 					}
906 				}
907 				continue;
908 			}
909 		}
910 		pm = pm_new;
911 		pm->found = 1;
912 		pm->ptstart = 0;
913 		pm->ptsize = 0;
914 		pm->bootable = 0;
915 		strlcpy(pm->diskdev, disk->dd_name, sizeof pm->diskdev);
916 		strlcpy(pm->diskdev_descr, disk->dd_descr, sizeof pm->diskdev_descr);
917 		/* Use as a default disk if the user has the sets on a local disk */
918 		strlcpy(localfs_dev, disk->dd_name, sizeof localfs_dev);
919 
920 		/*
921 		 * Init disk size and geometry
922 		 */
923 		pm->sectorsize = disk->dd_secsize;
924 		pm->dlcyl = disk->dd_cyl;
925 		pm->dlhead = disk->dd_head;
926 		pm->dlsec = disk->dd_sec;
927 		pm->dlsize = disk->dd_totsec;
928 		if (pm->dlsize == 0)
929 			pm->dlsize = disk->dd_cyl * disk->dd_head
930 			    * disk->dd_sec;
931 
932 		pm->parts = partitions_read_disk(pm->diskdev, disk->dd_totsec);
933 
934 again:
935 
936 #ifdef DEBUG_VERBOSE
937 		if (pm->parts) {
938 			fputs("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n", stderr);
939 			dump_parts(pm->parts);
940 
941 			if (pm->parts->pscheme->secondary_partitions) {
942 				const struct disk_partitions *sparts =
943 				    pm->parts->pscheme->secondary_partitions(
944 				    pm->parts, pm->ptstart, false);
945 				if (sparts != NULL)
946 					dump_parts(sparts);
947 			}
948 		}
949 #endif
950 
951 		pm->no_mbr = disk->dd_no_mbr;
952 		pm->no_part = disk->dd_no_part;
953 		if (!pm->no_part) {
954 			pm->sectorsize = disk->dd_secsize;
955 			pm->dlcyl = disk->dd_cyl;
956 			pm->dlhead = disk->dd_head;
957 			pm->dlsec = disk->dd_sec;
958 			pm->dlsize = disk->dd_totsec;
959 			if (pm->dlsize == 0)
960 				pm->dlsize = disk->dd_cyl * disk->dd_head
961 				    * disk->dd_sec;
962 
963 			if (pm->parts && pm->parts->pscheme->size_limit != 0
964 			    && pm->dlsize > pm->parts->pscheme->size_limit
965 			    && ! partman_go) {
966 
967 				char size[5], limit[5];
968 
969 				humanize_number(size, sizeof(size),
970 				    (uint64_t)pm->dlsize * 512U,
971 				    "", HN_AUTOSCALE, HN_B | HN_NOSPACE
972 				    | HN_DECIMAL);
973 
974 				humanize_number(limit, sizeof(limit),
975 				    (uint64_t)pm->parts->pscheme->size_limit
976 					* 512U,
977 				    "", HN_AUTOSCALE, HN_B | HN_NOSPACE
978 				    | HN_DECIMAL);
979 
980 				if (logfp)
981 					fprintf(logfp,
982 					    "disk %s: is too big (%" PRIu64
983 					    " blocks, %s), will be truncated\n",
984 						pm->diskdev, pm->dlsize,
985 						size);
986 
987 				msg_display_subst(MSG_toobigdisklabel, 5,
988 				   pm->diskdev,
989 				   msg_string(pm->parts->pscheme->name),
990 				   msg_string(pm->parts->pscheme->short_name),
991 				   size, limit);
992 
993 				int sel = -1;
994 				const char *err = NULL;
995 				process_menu(MENU_convertscheme, &sel);
996 				if (sel == 1) {
997 					if (!delete_scheme(pm)) {
998 						return -1;
999 					}
1000 					goto again;
1001 				} else if (sel == 2) {
1002 					if (!convert_scheme(pm,
1003 					     partman_go < 0, &err)) {
1004 						if (err != NULL)
1005 							err_msg_win(err);
1006 						return -1;
1007 					}
1008 					goto again;
1009 				} else if (sel == 3) {
1010 					return -1;
1011 				}
1012 				pm->dlsize = pm->parts->pscheme->size_limit;
1013 			}
1014 		} else {
1015 			pm->sectorsize = 0;
1016 			pm->dlcyl = 0;
1017 			pm->dlhead = 0;
1018 			pm->dlsec = 0;
1019 			pm->dlsize = 0;
1020 			pm->no_mbr = 1;
1021 		}
1022 		pm->dlcylsize = pm->dlhead * pm->dlsec;
1023 
1024 		if (partman_go) {
1025 			pm_getrefdev(pm_new);
1026 			if (SLIST_EMPTY(&pm_head) || pm_last == NULL)
1027 				 SLIST_INSERT_HEAD(&pm_head, pm_new, l);
1028 			else
1029 				 SLIST_INSERT_AFTER(pm_last, pm_new, l);
1030 			pm_new = malloc(sizeof (struct pm_devs));
1031 			memset(pm_new, 0, sizeof *pm_new);
1032 		} else
1033 			/* We are not in partman and do not want to process
1034 			 * all devices, exit */
1035 			break;
1036 	}
1037 
1038 	return numdisks-skipped;
1039 }
1040 
1041 static int
1042 sort_part_usage_by_mount(const void *a, const void *b)
1043 {
1044 	const struct part_usage_info *pa = a, *pb = b;
1045 
1046 	/* sort all real partitions by mount point */
1047 	if ((pa->instflags & PUIINST_MOUNT) &&
1048 	    (pb->instflags & PUIINST_MOUNT))
1049 		return strcmp(pa->mount, pb->mount);
1050 
1051 	/* real partitions go first */
1052 	if (pa->instflags & PUIINST_MOUNT)
1053 		return -1;
1054 	if (pb->instflags & PUIINST_MOUNT)
1055 		return 1;
1056 
1057 	/* arbitrary order for all other partitions */
1058 	if (pa->type == PT_swap)
1059 		return -1;
1060 	if (pb->type == PT_swap)
1061 		return 1;
1062 	if (pa->type < pb->type)
1063 		return -1;
1064 	if (pa->type > pb->type)
1065 		return 1;
1066 	if (pa->cur_part_id < pb->cur_part_id)
1067 		return -1;
1068 	if (pa->cur_part_id > pb->cur_part_id)
1069 		return 1;
1070 	return (uintptr_t)a < (uintptr_t)b ? -1 : 1;
1071 }
1072 
1073 int
1074 make_filesystems(struct install_partition_desc *install)
1075 {
1076 	int error = 0, partno = -1;
1077 	char *newfs = NULL, devdev[PATH_MAX], rdev[PATH_MAX];
1078 	size_t i;
1079 	struct part_usage_info *ptn;
1080 	struct disk_partitions *parts;
1081 	const char *mnt_opts = NULL, *fsname = NULL;
1082 
1083 	if (pm->cur_system)
1084 		return 1;
1085 
1086 	if (pm->no_part) {
1087 		/* check if this target device already has a ffs */
1088 		snprintf(rdev, sizeof rdev, _PATH_DEV "/r%s", pm->diskdev);
1089 		error = fsck_preen(rdev, "ffs", true);
1090 		if (error) {
1091 			if (!ask_noyes(MSG_No_filesystem_newfs))
1092 				return EINVAL;
1093 			error = run_program(RUN_DISPLAY | RUN_PROGRESS,
1094 			    "/sbin/newfs -V2 -O2 %s", rdev);
1095 		}
1096 
1097 		md_pre_mount(install, 0);
1098 
1099 		make_target_dir("/");
1100 
1101 		snprintf(devdev, sizeof devdev, _PATH_DEV "%s", pm->diskdev);
1102 		error = target_mount_do("-o async", devdev, "/");
1103 		if (error) {
1104 			msg_display_subst(MSG_mountfail, 2, devdev, "/");
1105 			hit_enter_to_continue(NULL, NULL);
1106 		}
1107 
1108 		return error;
1109 	}
1110 
1111 	/* Making new file systems and mounting them */
1112 
1113 	/* sort to ensure /usr/local is mounted after /usr (etc) */
1114 	qsort(install->infos, install->num, sizeof(*install->infos),
1115 	    sort_part_usage_by_mount);
1116 
1117 	for (i = 0; i < install->num; i++) {
1118 		/*
1119 		 * Newfs all file systems mareked as needing this.
1120 		 * Mount the ones that have a mountpoint in the target.
1121 		 */
1122 		ptn = &install->infos[i];
1123 		parts = ptn->parts;
1124 		newfs = NULL;
1125 		fsname = NULL;
1126 
1127 		if (ptn->size == 0 || parts == NULL|| ptn->type == PT_swap)
1128 			continue;
1129 
1130 		if (parts->pscheme->get_part_device(parts, ptn->cur_part_id,
1131 		    devdev, sizeof devdev, &partno, parent_device_only, false)
1132 		    && is_active_rootpart(devdev, partno))
1133 			continue;
1134 
1135 		parts->pscheme->get_part_device(parts, ptn->cur_part_id,
1136 		    devdev, sizeof devdev, &partno, plain_name, true);
1137 
1138 		parts->pscheme->get_part_device(parts, ptn->cur_part_id,
1139 		    rdev, sizeof rdev, &partno, raw_dev_name, true);
1140 
1141 		switch (ptn->fs_type) {
1142 		case FS_APPLEUFS:
1143 			asprintf(&newfs, "/sbin/newfs");
1144 			mnt_opts = "-tffs -o async";
1145 			fsname = "ffs";
1146 			break;
1147 		case FS_BSDFFS:
1148 			asprintf(&newfs,
1149 			    "/sbin/newfs -V2 -O %d",
1150 			    ptn->fs_version == 2 ? 2 : 1);
1151 			if (ptn->mountflags & PUIMNT_LOG)
1152 				mnt_opts = "-tffs -o log";
1153 			else
1154 				mnt_opts = "-tffs -o async";
1155 			fsname = "ffs";
1156 			break;
1157 		case FS_BSDLFS:
1158 			asprintf(&newfs, "/sbin/newfs_lfs");
1159 			mnt_opts = "-tlfs";
1160 			fsname = "lfs";
1161 			break;
1162 		case FS_MSDOS:
1163 			asprintf(&newfs, "/sbin/newfs_msdos");
1164 			mnt_opts = "-tmsdos";
1165 			fsname = "msdos";
1166 			break;
1167 		case FS_SYSVBFS:
1168 			asprintf(&newfs, "/sbin/newfs_sysvbfs");
1169 			mnt_opts = "-tsysvbfs";
1170 			fsname = "sysvbfs";
1171 			break;
1172 		case FS_V7:
1173 			asprintf(&newfs, "/sbin/newfs_v7fs");
1174 			mnt_opts = "-tv7fs";
1175 			fsname = "v7fs";
1176 			break;
1177 		case FS_EX2FS:
1178 			asprintf(&newfs, "/sbin/newfs_ext2fs");
1179 			mnt_opts = "-text2fs";
1180 			fsname = "ext2fs";
1181 			break;
1182 		}
1183 		if ((ptn->instflags & PUIINST_NEWFS) && newfs != NULL) {
1184 			if (ptn->fs_type == FS_MSDOS) {
1185 			        /* newfs only if mount fails */
1186 			        if (run_program(RUN_SILENT | RUN_ERROR_OK,
1187 				    "mount -rt msdos %s /mnt2", devdev) != 0)
1188 					error = run_program(
1189 					    RUN_DISPLAY | RUN_PROGRESS,
1190 					    "%s %s",
1191 					    newfs, rdev);
1192 				else {
1193 					run_program(RUN_SILENT | RUN_ERROR_OK,
1194 					    "umount /mnt2");
1195 					error = 0;
1196 				}
1197 			} else {
1198 				error = run_program(RUN_DISPLAY | RUN_PROGRESS,
1199 			    "%s %s", newfs, rdev);
1200 			}
1201 		} else if ((ptn->instflags & (PUIINST_MOUNT|PUIINST_BOOT))
1202 		    && fsname != NULL) {
1203 			/* We'd better check it isn't dirty */
1204 			error = fsck_preen(devdev, fsname, false);
1205 		}
1206 		free(newfs);
1207 		if (error != 0)
1208 			return error;
1209 
1210 		ptn->instflags &= ~PUIINST_NEWFS;
1211 		md_pre_mount(install, i);
1212 
1213 		if (partman_go == 0 && (ptn->instflags & PUIINST_MOUNT) &&
1214 				mnt_opts != NULL) {
1215 			make_target_dir(ptn->mount);
1216 			error = target_mount_do(mnt_opts, devdev,
1217 			    ptn->mount);
1218 			if (error) {
1219 				msg_display_subst(MSG_mountfail, 2, devdev,
1220 				    ptn->mount);
1221 				hit_enter_to_continue(NULL, NULL);
1222 				return error;
1223 			}
1224 		}
1225 	}
1226 	return 0;
1227 }
1228 
1229 int
1230 make_fstab(struct install_partition_desc *install)
1231 {
1232 	FILE *f;
1233 	const char *dump_dev = NULL;
1234 	const char *dev;
1235 	char dev_buf[PATH_MAX], swap_dev[PATH_MAX];
1236 
1237 	if (pm->cur_system)
1238 		return 1;
1239 
1240 	swap_dev[0] = 0;
1241 
1242 	/* Create the fstab. */
1243 	make_target_dir("/etc");
1244 	f = target_fopen("/etc/fstab", "w");
1245 	scripting_fprintf(NULL, "cat <<EOF >%s/etc/fstab\n", target_prefix());
1246 
1247 	if (logfp)
1248 		(void)fprintf(logfp,
1249 		    "Making %s/etc/fstab (%s).\n", target_prefix(),
1250 		    pm->diskdev);
1251 
1252 	if (f == NULL) {
1253 		msg_display(MSG_createfstab);
1254 		if (logfp)
1255 			(void)fprintf(logfp, "Failed to make /etc/fstab!\n");
1256 		hit_enter_to_continue(NULL, NULL);
1257 #ifndef DEBUG
1258 		return 1;
1259 #else
1260 		f = stdout;
1261 #endif
1262 	}
1263 
1264 	scripting_fprintf(f, "# NetBSD /etc/fstab\n# See /usr/share/examples/"
1265 			"fstab/ for more examples.\n");
1266 
1267 	if (pm->no_part) {
1268 		/* single dk? target */
1269 		char buf[200], parent[200], swap[200], *prompt;
1270 		int res;
1271 
1272 		if (!get_name_and_parent(pm->diskdev, buf, parent))
1273 			goto done_with_disks;
1274 		scripting_fprintf(f, NAME_PREFIX "%s\t/\tffs\trw\t\t1 1\n",
1275 		    buf);
1276 		if (!find_swap_part_on(parent, swap))
1277 			goto done_with_disks;
1278 		const char *args[] = { parent, swap };
1279 		prompt = str_arg_subst(msg_string(MSG_Auto_add_swap_part),
1280 		    __arraycount(args), args);
1281 		res = ask_yesno(prompt);
1282 		free(prompt);
1283 		if (res)
1284 			scripting_fprintf(f, NAME_PREFIX "%s\tnone"
1285 			    "\tswap\tsw,dp\t\t0 0\n", swap);
1286 		goto done_with_disks;
1287 	}
1288 
1289 	for (size_t i = 0; i < install->num; i++) {
1290 
1291 		const struct part_usage_info *ptn = &install->infos[i];
1292 
1293 		if (ptn->type != PT_swap &&
1294 		    (ptn->instflags & PUIINST_MOUNT) == 0)
1295 			continue;
1296 
1297 		const char *s = "";
1298 		const char *mp = ptn->mount;
1299 		const char *fstype = "ffs";
1300 		int fsck_pass = 0, dump_freq = 0;
1301 
1302 		if (ptn->parts->pscheme->get_part_device(ptn->parts,
1303 			    ptn->cur_part_id, dev_buf, sizeof dev_buf, NULL,
1304 			    logical_name, true))
1305 			dev = dev_buf;
1306 		else
1307 			dev = NULL;
1308 
1309 		if (!*mp) {
1310 			/*
1311 			 * No mount point specified, comment out line and
1312 			 * use /mnt as a placeholder for the mount point.
1313 			 */
1314 			s = "# ";
1315 			mp = "/mnt";
1316 		}
1317 
1318 		switch (ptn->fs_type) {
1319 		case FS_UNUSED:
1320 			continue;
1321 		case FS_BSDLFS:
1322 			/* If there is no LFS, just comment it out. */
1323 			if (!check_lfs_progs())
1324 				s = "# ";
1325 			fstype = "lfs";
1326 			/* FALLTHROUGH */
1327 		case FS_BSDFFS:
1328 			fsck_pass = (strcmp(mp, "/") == 0) ? 1 : 2;
1329 			dump_freq = 1;
1330 			break;
1331 		case FS_MSDOS:
1332 			fstype = "msdos";
1333 			break;
1334 		case FS_SWAP:
1335 			if (swap_dev[0] == 0) {
1336 				strncpy(swap_dev, dev, sizeof swap_dev);
1337 				dump_dev = ",dp";
1338 			} else {
1339 				dump_dev = "";
1340 			}
1341 			scripting_fprintf(f, "%s\t\tnone\tswap\tsw%s\t\t 0 0\n",
1342 				dev, dump_dev);
1343 			continue;
1344 		case FS_SYSVBFS:
1345 			fstype = "sysvbfs";
1346 			make_target_dir("/stand");
1347 			break;
1348 		default:
1349 			fstype = "???";
1350 			s = "# ";
1351 			break;
1352 		}
1353 		/* The code that remounts root rw doesn't check the partition */
1354 		if (strcmp(mp, "/") == 0 &&
1355 		    (ptn->instflags & PUIINST_MOUNT) == 0)
1356 			s = "# ";
1357 
1358  		scripting_fprintf(f,
1359 		  "%s%s\t\t%s\t%s\trw%s%s%s%s%s%s%s%s\t\t %d %d\n",
1360 		   s, dev, mp, fstype,
1361 		   ptn->mountflags & PUIMNT_LOG ? ",log" : "",
1362 		   ptn->mountflags & PUIMNT_NOAUTO ? ",noauto" : "",
1363 		   ptn->mountflags & PUIMNT_ASYNC ? ",async" : "",
1364 		   ptn->mountflags & PUIMNT_NOATIME ? ",noatime" : "",
1365 		   ptn->mountflags & PUIMNT_NODEV ? ",nodev" : "",
1366 		   ptn->mountflags & PUIMNT_NODEVMTIME ? ",nodevmtime" : "",
1367 		   ptn->mountflags & PUIMNT_NOEXEC ? ",noexec" : "",
1368 		   ptn->mountflags & PUIMNT_NOSUID ? ",nosuid" : "",
1369 		   dump_freq, fsck_pass);
1370 	}
1371 
1372 done_with_disks:
1373 	if (tmp_ramdisk_size > 0) {
1374 #ifdef HAVE_TMPFS
1375 		scripting_fprintf(f, "tmpfs\t\t/tmp\ttmpfs\trw,-m=1777,-s=%"
1376 		    PRIu64 "\n",
1377 		    tmp_ramdisk_size * 512);
1378 #else
1379 		if (swap_dev[0] != 0)
1380 			scripting_fprintf(f, "%s\t\t/tmp\tmfs\trw,-s=%"
1381 			    PRIu64 "\n", swap_dev, tmp_ramdisk_size);
1382 		else
1383 			scripting_fprintf(f, "swap\t\t/tmp\tmfs\trw,-s=%"
1384 			    PRIu64 "\n", tmp_ramdisk_size);
1385 #endif
1386 	}
1387 
1388 	if (cdrom_dev[0] == 0)
1389 		get_default_cdrom(cdrom_dev, sizeof(cdrom_dev));
1390 
1391 	/* Add /kern, /proc and /dev/pts to fstab and make mountpoint. */
1392 	scripting_fprintf(f, "kernfs\t\t/kern\tkernfs\trw\n");
1393 	scripting_fprintf(f, "ptyfs\t\t/dev/pts\tptyfs\trw\n");
1394 	scripting_fprintf(f, "procfs\t\t/proc\tprocfs\trw\n");
1395 	scripting_fprintf(f, "/dev/%s\t\t/cdrom\tcd9660\tro,noauto\n",
1396 	    cdrom_dev);
1397 	scripting_fprintf(f, "%stmpfs\t\t/var/shm\ttmpfs\trw,-m1777,-sram%%25\n",
1398 	    tmpfs_on_var_shm() ? "" : "#");
1399 	make_target_dir("/kern");
1400 	make_target_dir("/proc");
1401 	make_target_dir("/dev/pts");
1402 	make_target_dir("/cdrom");
1403 	make_target_dir("/var/shm");
1404 
1405 	scripting_fprintf(NULL, "EOF\n");
1406 
1407 	fclose(f);
1408 	fflush(NULL);
1409 	return 0;
1410 }
1411 
1412 static bool
1413 find_part_by_name(const char *name, struct disk_partitions **parts,
1414     part_id *pno)
1415 {
1416 	struct pm_devs *i;
1417 	struct disk_partitions *ps;
1418 	part_id id;
1419 	struct disk_desc disks[MAX_DISKS];
1420 	int n, cnt;
1421 
1422 	if (SLIST_EMPTY(&pm_head)) {
1423 		/*
1424 		 * List has not been filled, only "pm" is valid - check
1425 		 * that first.
1426 		 */
1427 		if (pm->parts->pscheme->find_by_name != NULL) {
1428 			id = pm->parts->pscheme->find_by_name(pm->parts, name);
1429 			if (id != NO_PART) {
1430 				*pno = id;
1431 				*parts = pm->parts;
1432 				return true;
1433 			}
1434 		}
1435 		/*
1436 		 * Not that easy - check all other disks
1437 		 */
1438 		cnt = get_disks(disks, false);
1439 		for (n = 0; n < cnt; n++) {
1440 			if (strcmp(disks[n].dd_name, pm->diskdev) == 0)
1441 				continue;
1442 			ps = partitions_read_disk(disks[n].dd_name,
1443 			    disks[n].dd_totsec);
1444 			if (ps == NULL)
1445 				continue;
1446 			if (ps->pscheme->find_by_name == NULL)
1447 				continue;
1448 			id = ps->pscheme->find_by_name(ps, name);
1449 			if (id != NO_PART) {
1450 				*pno = id;
1451 				*parts = ps;
1452 				return true;	/* XXX this leaks memory */
1453 			}
1454 			ps->pscheme->free(ps);
1455 		}
1456 	} else {
1457 		SLIST_FOREACH(i, &pm_head, l) {
1458 			if (i->parts == NULL)
1459 				continue;
1460 			if (i->parts->pscheme->find_by_name == NULL)
1461 				continue;
1462 			id = i->parts->pscheme->find_by_name(i->parts, name);
1463 			if (id == NO_PART)
1464 				continue;
1465 			*pno = id;
1466 			*parts = i->parts;
1467 			return true;
1468 		}
1469 	}
1470 
1471 	*pno = NO_PART;
1472 	*parts = NULL;
1473 	return false;
1474 }
1475 
1476 static int
1477 /*ARGSUSED*/
1478 process_found_fs(struct data *list, size_t num, const struct lookfor *item,
1479     bool with_fsck)
1480 {
1481 	int error;
1482 	char rdev[PATH_MAX], dev[PATH_MAX],
1483 	    options[STRSIZE], tmp[STRSIZE], *op, *last;
1484 	const char *fsname = (const char*)item->var;
1485 	part_id pno;
1486 	struct disk_partitions *parts;
1487 	bool first;
1488 
1489 	if (num < 2 || strstr(list[2].u.s_val, "noauto") != NULL)
1490 		return 0;
1491 
1492 	if ((strcmp(list[1].u.s_val, "/") == 0) && target_mounted())
1493 		return 0;
1494 
1495 	if (strcmp(item->head, name_prefix) == 0) {
1496 		/* this fstab entry uses NAME= syntax */
1497 		if (!find_part_by_name(list[0].u.s_val,
1498 		    &parts, &pno) || parts == NULL || pno == NO_PART)
1499 			return 0;
1500 		parts->pscheme->get_part_device(parts, pno,
1501 		    dev, sizeof(dev), NULL, plain_name, true);
1502 		parts->pscheme->get_part_device(parts, pno,
1503 		    rdev, sizeof(rdev), NULL, raw_dev_name, true);
1504 	} else {
1505 		/* plain device name */
1506 		strcpy(rdev, "/dev/r");
1507 		strlcat(rdev, list[0].u.s_val, sizeof(rdev));
1508 		strcpy(dev, "/dev/");
1509 		strlcat(dev, list[0].u.s_val, sizeof(dev));
1510 	}
1511 
1512 	if (with_fsck) {
1513 		/* need the raw device for fsck_preen */
1514 		error = fsck_preen(rdev, fsname, false);
1515 		if (error != 0)
1516 			return error;
1517 	}
1518 
1519 	/* add mount option for fs type */
1520 	strcpy(options, "-t ");
1521 	strlcat(options, fsname, sizeof(options));
1522 
1523 	/* extract mount options from fstab */
1524 	strlcpy(tmp, list[2].u.s_val, sizeof(tmp));
1525 	for (first = true, op = strtok_r(tmp, ",", &last); op != NULL;
1526 	    op = strtok_r(NULL, ",", &last)) {
1527 		if (strcmp(op, FSTAB_RW) == 0 ||
1528 		    strcmp(op, FSTAB_RQ) == 0 ||
1529 		    strcmp(op, FSTAB_RO) == 0 ||
1530 		    strcmp(op, FSTAB_SW) == 0 ||
1531 		    strcmp(op, FSTAB_DP) == 0 ||
1532 		    strcmp(op, FSTAB_XX) == 0)
1533 			continue;
1534 		if (first) {
1535 			first = false;
1536 			strlcat(options, " -o ", sizeof(options));
1537 		} else {
1538 			strlcat(options, ",", sizeof(options));
1539 		}
1540 		strlcat(options, op, sizeof(options));
1541 	}
1542 
1543 	error = target_mount(options, dev, list[1].u.s_val);
1544 	if (error != 0) {
1545 		msg_fmt_display(MSG_mount_failed, "%s", list[0].u.s_val);
1546 		if (!ask_noyes(NULL))
1547 			return error;
1548 	}
1549 	return 0;
1550 }
1551 
1552 static int
1553 /*ARGSUSED*/
1554 found_fs(struct data *list, size_t num, const struct lookfor *item)
1555 {
1556 	return process_found_fs(list, num, item, true);
1557 }
1558 
1559 static int
1560 /*ARGSUSED*/
1561 found_fs_nocheck(struct data *list, size_t num, const struct lookfor *item)
1562 {
1563 	return process_found_fs(list, num, item, false);
1564 }
1565 
1566 /*
1567  * Do an fsck. On failure, inform the user by showing a warning
1568  * message and doing menu_ok() before proceeding.
1569  * The device passed should be the full qualified path to raw disk
1570  * (e.g. /dev/rwd0a).
1571  * Returns 0 on success, or nonzero return code from fsck() on failure.
1572  */
1573 static int
1574 fsck_preen(const char *disk, const char *fsname, bool silent)
1575 {
1576 	char *prog, err[12];
1577 	int error;
1578 
1579 	if (fsname == NULL)
1580 		return 0;
1581 	/* first, check if fsck program exists, if not, assume ok */
1582 	asprintf(&prog, "/sbin/fsck_%s", fsname);
1583 	if (prog == NULL)
1584 		return 0;
1585 	if (access(prog, X_OK) != 0) {
1586 		free(prog);
1587 		return 0;
1588 	}
1589 	if (!strcmp(fsname,"ffs"))
1590 		fixsb(prog, disk);
1591 	error = run_program(silent? RUN_SILENT|RUN_ERROR_OK : 0, "%s -p -q %s", prog, disk);
1592 	free(prog);
1593 	if (error != 0 && !silent) {
1594 		sprintf(err, "%d", error);
1595 		msg_display_subst(msg_string(MSG_badfs), 3,
1596 		    disk, fsname, err);
1597 		if (ask_noyes(NULL))
1598 			error = 0;
1599 		/* XXX at this point maybe we should run a full fsck? */
1600 	}
1601 	return error;
1602 }
1603 
1604 /* This performs the same function as the etc/rc.d/fixsb script
1605  * which attempts to correct problems with ffs1 filesystems
1606  * which may have been introduced by booting a netbsd-current kernel
1607  * from between April of 2003 and January 2004. For more information
1608  * This script was developed as a response to NetBSD pr install/25138
1609  * Additional prs regarding the original issue include:
1610  *  bin/17910 kern/21283 kern/21404 port-macppc/23925 port-macppc/23926
1611  */
1612 static void
1613 fixsb(const char *prog, const char *disk)
1614 {
1615 	int fd;
1616 	int rval;
1617 	union {
1618 		struct fs fs;
1619 		char buf[SBLOCKSIZE];
1620 	} sblk;
1621 	struct fs *fs = &sblk.fs;
1622 
1623 	fd = open(disk, O_RDONLY);
1624 	if (fd == -1)
1625 		return;
1626 
1627 	/* Read ffsv1 main superblock */
1628 	rval = pread(fd, sblk.buf, sizeof sblk.buf, SBLOCK_UFS1);
1629 	close(fd);
1630 	if (rval != sizeof sblk.buf)
1631 		return;
1632 
1633 	if (fs->fs_magic != FS_UFS1_MAGIC &&
1634 	    fs->fs_magic != FS_UFS1_MAGIC_SWAPPED)
1635 		/* Not FFSv1 */
1636 		return;
1637 	if (fs->fs_old_flags & FS_FLAGS_UPDATED)
1638 		/* properly updated fslevel 4 */
1639 		return;
1640 	if (fs->fs_bsize != fs->fs_maxbsize)
1641 		/* not messed up */
1642 		return;
1643 
1644 	/*
1645 	 * OK we have a munged fs, first 'upgrade' to fslevel 4,
1646 	 * We specify -b16 in order to stop fsck bleating that the
1647 	 * sb doesn't match the first alternate.
1648 	 */
1649 	run_program(RUN_DISPLAY | RUN_PROGRESS,
1650 	    "%s -p -b 16 -c 4 %s", prog, disk);
1651 	/* Then downgrade to fslevel 3 */
1652 	run_program(RUN_DISPLAY | RUN_PROGRESS,
1653 	    "%s -p -c 3 %s", prog, disk);
1654 }
1655 
1656 /*
1657  * fsck and mount the root partition.
1658  * devdev is the fully qualified block device name.
1659  */
1660 static int
1661 mount_root(const char *devdev, bool first, bool writeable,
1662      struct install_partition_desc *install)
1663 {
1664 	int	error;
1665 
1666 	error = fsck_preen(devdev, "ffs", false);
1667 	if (error != 0)
1668 		return error;
1669 
1670 	if (first)
1671 		md_pre_mount(install, 0);
1672 
1673 	/* Mount devdev on target's "".
1674 	 * If we pass "" as mount-on, Prefixing will DTRT.
1675 	 * for now, use no options.
1676 	 * XXX consider -o remount in case target root is
1677 	 * current root, still readonly from single-user?
1678 	 */
1679 	return target_mount(writeable? "" : "-r", devdev, "");
1680 }
1681 
1682 /* Get information on the file systems mounted from the root filesystem.
1683  * Offer to convert them into 4.4BSD inodes if they are not 4.4BSD
1684  * inodes.  Fsck them.  Mount them.
1685  */
1686 
1687 int
1688 mount_disks(struct install_partition_desc *install)
1689 {
1690 	char *fstab;
1691 	int   fstabsize;
1692 	int   error;
1693 	char devdev[PATH_MAX];
1694 	size_t i, num_fs_types, num_entries;
1695 	struct lookfor *fstabbuf, *l;
1696 
1697 	if (install->cur_system)
1698 		return 0;
1699 
1700 	/*
1701 	 * Check what file system tools are available and create parsers
1702 	 * for the corresponding fstab(5) entries - all others will be
1703 	 * ignored.
1704 	 */
1705 	num_fs_types = 1;	/* ffs is implicit */
1706 	for (i = 0; i < __arraycount(extern_fs_with_chk); i++) {
1707 		sprintf(devdev, "/sbin/newfs_%s", extern_fs_with_chk[i]);
1708 		if (file_exists_p(devdev))
1709 			num_fs_types++;
1710 	}
1711 	for (i = 0; i < __arraycount(extern_fs_newfs_only); i++) {
1712 		sprintf(devdev, "/sbin/newfs_%s", extern_fs_newfs_only[i]);
1713 		if (file_exists_p(devdev))
1714 			num_fs_types++;
1715 	}
1716 	num_entries = 2 *  num_fs_types + 1;	/* +1 for "ufs" special case */
1717 	fstabbuf = calloc(num_entries, sizeof(*fstabbuf));
1718 	if (fstabbuf == NULL)
1719 		return -1;
1720 	l = fstabbuf;
1721 	l->head = "/dev/";
1722 	l->fmt = strdup("/dev/%s %s ffs %s");
1723 	l->todo = "c";
1724 	l->var = __UNCONST("ffs");
1725 	l->func = found_fs;
1726 	l++;
1727 	l->head = "/dev/";
1728 	l->fmt = strdup("/dev/%s %s ufs %s");
1729 	l->todo = "c";
1730 	l->var = __UNCONST("ffs");
1731 	l->func = found_fs;
1732 	l++;
1733 	l->head = NAME_PREFIX;
1734 	l->fmt = strdup(NAME_PREFIX "%s %s ffs %s");
1735 	l->todo = "c";
1736 	l->var = __UNCONST("ffs");
1737 	l->func = found_fs;
1738 	l++;
1739 	for (i = 0; i < __arraycount(extern_fs_with_chk); i++) {
1740 		sprintf(devdev, "/sbin/newfs_%s", extern_fs_with_chk[i]);
1741 		if (!file_exists_p(devdev))
1742 			continue;
1743 		sprintf(devdev, "/dev/%%s %%s %s %%s", extern_fs_with_chk[i]);
1744 		l->head = "/dev/";
1745 		l->fmt = strdup(devdev);
1746 		l->todo = "c";
1747 		l->var = __UNCONST(extern_fs_with_chk[i]);
1748 		l->func = found_fs;
1749 		l++;
1750 		sprintf(devdev, NAME_PREFIX "%%s %%s %s %%s",
1751 		    extern_fs_with_chk[i]);
1752 		l->head = NAME_PREFIX;
1753 		l->fmt = strdup(devdev);
1754 		l->todo = "c";
1755 		l->var = __UNCONST(extern_fs_with_chk[i]);
1756 		l->func = found_fs;
1757 		l++;
1758 	}
1759 	for (i = 0; i < __arraycount(extern_fs_newfs_only); i++) {
1760 		sprintf(devdev, "/sbin/newfs_%s", extern_fs_newfs_only[i]);
1761 		if (!file_exists_p(devdev))
1762 			continue;
1763 		sprintf(devdev, "/dev/%%s %%s %s %%s", extern_fs_newfs_only[i]);
1764 		l->head = "/dev/";
1765 		l->fmt = strdup(devdev);
1766 		l->todo = "c";
1767 		l->var = __UNCONST(extern_fs_newfs_only[i]);
1768 		l->func = found_fs_nocheck;
1769 		l++;
1770 		sprintf(devdev, NAME_PREFIX "%%s %%s %s %%s",
1771 		    extern_fs_newfs_only[i]);
1772 		l->head = NAME_PREFIX;
1773 		l->fmt = strdup(devdev);
1774 		l->todo = "c";
1775 		l->var = __UNCONST(extern_fs_newfs_only[i]);
1776 		l->func = found_fs_nocheck;
1777 		l++;
1778 	}
1779 	assert((size_t)(l - fstabbuf) == num_entries);
1780 
1781 	/* First the root device. */
1782 	if (target_already_root())
1783 		/* avoid needing to call target_already_root() again */
1784 		targetroot_mnt[0] = 0;
1785 	else {
1786 		for (i = 0; i < install->num; i++) {
1787 			if (is_root_part_mount(install->infos[i].mount))
1788 				break;
1789 		}
1790 
1791 		if (i >= install->num) {
1792 			hit_enter_to_continue(MSG_noroot, NULL);
1793 			return -1;
1794 		}
1795 
1796 		if (!install->infos[i].parts->pscheme->get_part_device(
1797 		    install->infos[i].parts, install->infos[i].cur_part_id,
1798 		    devdev, sizeof devdev, NULL, plain_name, true))
1799 			return -1;
1800 		error = mount_root(devdev, true, false, install);
1801 		if (error != 0 && error != EBUSY)
1802 			return -1;
1803 	}
1804 
1805 	/* Check the target /etc/fstab exists before trying to parse it. */
1806 	if (target_dir_exists_p("/etc") == 0 ||
1807 	    target_file_exists_p("/etc/fstab") == 0) {
1808 		msg_fmt_display(MSG_noetcfstab, "%s", pm->diskdev);
1809 		hit_enter_to_continue(NULL, NULL);
1810 		return -1;
1811 	}
1812 
1813 
1814 	/* Get fstab entries from the target-root /etc/fstab. */
1815 	fstabsize = target_collect_file(T_FILE, &fstab, "/etc/fstab");
1816 	if (fstabsize < 0) {
1817 		/* error ! */
1818 		msg_fmt_display(MSG_badetcfstab, "%s", pm->diskdev);
1819 		hit_enter_to_continue(NULL, NULL);
1820 		umount_root();
1821 		return -2;
1822 	}
1823 	/*
1824 	 * We unmount the read-only root again, so we can mount it
1825 	 * with proper options from /etc/fstab
1826 	 */
1827 	umount_root();
1828 
1829 	/*
1830 	 * Now do all entries in /etc/fstab and mount them if required
1831 	 */
1832 	error = walk(fstab, (size_t)fstabsize, fstabbuf, num_entries);
1833 	free(fstab);
1834 	for (i = 0; i < num_entries; i++)
1835 		free(__UNCONST(fstabbuf[i].fmt));
1836 	free(fstabbuf);
1837 
1838 	return error;
1839 }
1840 
1841 int
1842 set_swap_if_low_ram(struct install_partition_desc *install)
1843 {
1844 	if (get_ramsize() <= 32)
1845 		return set_swap(install);
1846 	return 0;
1847 }
1848 
1849 int
1850 set_swap(struct install_partition_desc *install)
1851 {
1852 	size_t i;
1853 	char dev_buf[PATH_MAX];
1854 	int rval;
1855 
1856 	for (i = 0; i < install->num; i++) {
1857 		if (install->infos[i].type == PT_swap)
1858 			break;
1859 	}
1860 	if (i >= install->num)
1861 		return 0;
1862 
1863 	if (!install->infos[i].parts->pscheme->get_part_device(
1864 	    install->infos[i].parts, install->infos[i].cur_part_id, dev_buf,
1865 	    sizeof dev_buf, NULL, plain_name, true))
1866 		return -1;
1867 
1868 	rval = swapctl(SWAP_ON, dev_buf, 0);
1869 	if (rval != 0)
1870 		return -1;
1871 
1872 	return 0;
1873 }
1874 
1875 int
1876 check_swap(const char *disk, int remove_swap)
1877 {
1878 	struct swapent *swap;
1879 	char *cp;
1880 	int nswap;
1881 	int l;
1882 	int rval = 0;
1883 
1884 	nswap = swapctl(SWAP_NSWAP, 0, 0);
1885 	if (nswap <= 0)
1886 		return 0;
1887 
1888 	swap = malloc(nswap * sizeof *swap);
1889 	if (swap == NULL)
1890 		return -1;
1891 
1892 	nswap = swapctl(SWAP_STATS, swap, nswap);
1893 	if (nswap < 0)
1894 		goto bad_swap;
1895 
1896 	l = strlen(disk);
1897 	while (--nswap >= 0) {
1898 		/* Should we check the se_dev or se_path? */
1899 		cp = swap[nswap].se_path;
1900 		if (memcmp(cp, "/dev/", 5) != 0)
1901 			continue;
1902 		if (memcmp(cp + 5, disk, l) != 0)
1903 			continue;
1904 		if (!isalpha(*(unsigned char *)(cp + 5 + l)))
1905 			continue;
1906 		if (cp[5 + l + 1] != 0)
1907 			continue;
1908 		/* ok path looks like it is for this device */
1909 		if (!remove_swap) {
1910 			/* count active swap areas */
1911 			rval++;
1912 			continue;
1913 		}
1914 		if (swapctl(SWAP_OFF, cp, 0) == -1)
1915 			rval = -1;
1916 	}
1917 
1918     done:
1919 	free(swap);
1920 	return rval;
1921 
1922     bad_swap:
1923 	rval = -1;
1924 	goto done;
1925 }
1926 
1927 #ifdef HAVE_BOOTXX_xFS
1928 char *
1929 bootxx_name(struct install_partition_desc *install)
1930 {
1931 	int fstype;
1932 	const char *bootxxname;
1933 	char *bootxx;
1934 
1935 	/* check we have boot code for the root partition type */
1936 	fstype = install->infos[0].fs_type;
1937 	switch (fstype) {
1938 #if defined(BOOTXX_FFSV1) || defined(BOOTXX_FFSV2)
1939 	case FS_BSDFFS:
1940 		if (install->infos[0].fs_version == 2) {
1941 #ifdef BOOTXX_FFSV2
1942 			bootxxname = BOOTXX_FFSV2;
1943 #else
1944 			bootxxname = NULL;
1945 #endif
1946 		} else {
1947 #ifdef BOOTXX_FFSV1
1948 			bootxxname = BOOTXX_FFSV1;
1949 #else
1950 			bootxxname = NULL;
1951 #endif
1952 		}
1953 		break;
1954 #endif
1955 #ifdef BOOTXX_LFSV2
1956 	case FS_BSDLFS:
1957 		bootxxname = BOOTXX_LFSV2;
1958 		break;
1959 #endif
1960 	default:
1961 		bootxxname = NULL;
1962 		break;
1963 	}
1964 
1965 	if (bootxxname == NULL)
1966 		return NULL;
1967 
1968 	asprintf(&bootxx, "%s/%s", BOOTXXDIR, bootxxname);
1969 	return bootxx;
1970 }
1971 #endif
1972 
1973 /* from dkctl.c */
1974 static int
1975 get_dkwedges_sort(const void *a, const void *b)
1976 {
1977 	const struct dkwedge_info *dkwa = a, *dkwb = b;
1978 	const daddr_t oa = dkwa->dkw_offset, ob = dkwb->dkw_offset;
1979 	return (oa < ob) ? -1 : (oa > ob) ? 1 : 0;
1980 }
1981 
1982 int
1983 get_dkwedges(struct dkwedge_info **dkw, const char *diskdev)
1984 {
1985 	struct dkwedge_list dkwl;
1986 
1987 	*dkw = NULL;
1988 	if (!get_wedge_list(diskdev, &dkwl))
1989 		return -1;
1990 
1991 	if (dkwl.dkwl_nwedges > 0 && *dkw != NULL) {
1992 		qsort(*dkw, dkwl.dkwl_nwedges, sizeof(**dkw),
1993 		    get_dkwedges_sort);
1994 	}
1995 
1996 	return dkwl.dkwl_nwedges;
1997 }
1998