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