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