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