disks.c revision 1.1 1 /* $NetBSD: disks.c,v 1.1 2014/07/26 19:30:44 dholland 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 <stdio.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42 #include <fcntl.h>
43 #include <util.h>
44
45 #include <sys/param.h>
46 #include <sys/sysctl.h>
47 #include <sys/swap.h>
48 #include <ufs/ufs/dinode.h>
49 #include <ufs/ffs/fs.h>
50 #define FSTYPENAMES
51 #include <sys/disklabel.h>
52
53 #include <dev/scsipi/scsipi_all.h>
54 #include <sys/scsiio.h>
55
56 #include <dev/ata/atareg.h>
57 #include <sys/ataio.h>
58
59 #include "defs.h"
60 #include "md.h"
61 #include "msg_defs.h"
62 #include "menu_defs.h"
63 #include "txtwalk.h"
64
65 /* Disk descriptions */
66 #define MAX_DISKS 15
67 struct disk_desc {
68 char dd_name[SSTRSIZE];
69 char dd_descr[70];
70 uint dd_no_mbr;
71 uint dd_cyl;
72 uint dd_head;
73 uint dd_sec;
74 uint dd_secsize;
75 uint dd_totsec;
76 };
77
78 /* Local prototypes */
79 static int foundffs(struct data *, size_t);
80 #ifdef USE_SYSVBFS
81 static int foundsysvbfs(struct data *, size_t);
82 #endif
83 static int fsck_preen(const char *, int, const char *);
84 static void fixsb(const char *, const char *, char);
85
86 #ifndef DISK_NAMES
87 #define DISK_NAMES "wd", "sd", "ld", "raid"
88 #endif
89
90 static const char *disk_names[] = { DISK_NAMES, "vnd", NULL };
91
92 static bool tmpfs_on_var_shm(void);
93
94 const char *
95 getfslabelname(uint8_t f)
96 {
97 if (f >= __arraycount(fstypenames) || fstypenames[f] == NULL)
98 return "invalid";
99 return fstypenames[f];
100 }
101
102 /*
103 * Decide wether we want to mount a tmpfs on /var/shm: we do this always
104 * when the machine has more than 16 MB of user memory. On smaller machines,
105 * shm_open() and friends will not perform well anyway.
106 */
107 static bool
108 tmpfs_on_var_shm()
109 {
110 uint64_t ram;
111 size_t len;
112
113 len = sizeof(ram);
114 if (sysctlbyname("hw.usermem64", &ram, &len, NULL, 0))
115 return false;
116
117 return ram > 16UL*1024UL*1024UL;
118 }
119
120 /* from src/sbin/atactl/atactl.c
121 * extract_string: copy a block of bytes out of ataparams and make
122 * a proper string out of it, truncating trailing spaces and preserving
123 * strict typing. And also, not doing unaligned accesses.
124 */
125 static void
126 ata_extract_string(char *buf, size_t bufmax,
127 uint8_t *bytes, unsigned numbytes,
128 int needswap)
129 {
130 unsigned i;
131 size_t j;
132 unsigned char ch1, ch2;
133
134 for (i = 0, j = 0; i < numbytes; i += 2) {
135 ch1 = bytes[i];
136 ch2 = bytes[i+1];
137 if (needswap && j < bufmax-1) {
138 buf[j++] = ch2;
139 }
140 if (j < bufmax-1) {
141 buf[j++] = ch1;
142 }
143 if (!needswap && j < bufmax-1) {
144 buf[j++] = ch2;
145 }
146 }
147 while (j > 0 && buf[j-1] == ' ') {
148 j--;
149 }
150 buf[j] = '\0';
151 }
152
153 /*
154 * from src/sbin/scsictl/scsi_subr.c
155 */
156 #define STRVIS_ISWHITE(x) ((x) == ' ' || (x) == '\0' || (x) == (u_char)'\377')
157
158 static void
159 scsi_strvis(char *sdst, size_t dlen, const char *ssrc, size_t slen)
160 {
161 u_char *dst = (u_char *)sdst;
162 const u_char *src = (const u_char *)ssrc;
163
164 /* Trim leading and trailing blanks and NULs. */
165 while (slen > 0 && STRVIS_ISWHITE(src[0]))
166 ++src, --slen;
167 while (slen > 0 && STRVIS_ISWHITE(src[slen - 1]))
168 --slen;
169
170 while (slen > 0) {
171 if (*src < 0x20 || *src >= 0x80) {
172 /* non-printable characters */
173 dlen -= 4;
174 if (dlen < 1)
175 break;
176 *dst++ = '\\';
177 *dst++ = ((*src & 0300) >> 6) + '0';
178 *dst++ = ((*src & 0070) >> 3) + '0';
179 *dst++ = ((*src & 0007) >> 0) + '0';
180 } else if (*src == '\\') {
181 /* quote characters */
182 dlen -= 2;
183 if (dlen < 1)
184 break;
185 *dst++ = '\\';
186 *dst++ = '\\';
187 } else {
188 /* normal characters */
189 if (--dlen < 1)
190 break;
191 *dst++ = *src;
192 }
193 ++src, --slen;
194 }
195
196 *dst++ = 0;
197 }
198
199
200 static int
201 get_descr_scsi(struct disk_desc *dd, int fd)
202 {
203 struct scsipi_inquiry_data inqbuf;
204 struct scsipi_inquiry cmd;
205 scsireq_t req;
206 /* x4 in case every character is escaped, +1 for NUL. */
207 char vendor[(sizeof(inqbuf.vendor) * 4) + 1],
208 product[(sizeof(inqbuf.product) * 4) + 1],
209 revision[(sizeof(inqbuf.revision) * 4) + 1];
210 char size[5];
211 int error;
212
213 memset(&inqbuf, 0, sizeof(inqbuf));
214 memset(&cmd, 0, sizeof(cmd));
215 memset(&req, 0, sizeof(req));
216
217 cmd.opcode = INQUIRY;
218 cmd.length = sizeof(inqbuf);
219 memcpy(req.cmd, &cmd, sizeof(cmd));
220 req.cmdlen = sizeof(cmd);
221 req.databuf = &inqbuf;
222 req.datalen = sizeof(inqbuf);
223 req.timeout = 10000;
224 req.flags = SCCMD_READ;
225 req.senselen = SENSEBUFLEN;
226
227 error = ioctl(fd, SCIOCCOMMAND, &req);
228 if (error == -1 || req.retsts != SCCMD_OK)
229 return 0;
230
231 scsi_strvis(vendor, sizeof(vendor), inqbuf.vendor,
232 sizeof(inqbuf.vendor));
233 scsi_strvis(product, sizeof(product), inqbuf.product,
234 sizeof(inqbuf.product));
235 scsi_strvis(revision, sizeof(revision), inqbuf.revision,
236 sizeof(inqbuf.revision));
237
238 humanize_number(size, sizeof(size),
239 (uint64_t)dd->dd_secsize * (uint64_t)dd->dd_totsec,
240 "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
241
242 snprintf(dd->dd_descr, sizeof(dd->dd_descr),
243 "%s (%s, %s %s)",
244 dd->dd_name, size, vendor, product);
245
246 return 1;
247 }
248
249 static int
250 get_descr_ata(struct disk_desc *dd, int fd)
251 {
252 struct atareq req;
253 static union {
254 unsigned char inbuf[DEV_BSIZE];
255 struct ataparams inqbuf;
256 } inbuf;
257 struct ataparams *inqbuf = &inbuf.inqbuf;
258 char model[sizeof(inqbuf->atap_model)+1];
259 char size[5];
260 int error, needswap = 0;
261
262 memset(&inbuf, 0, sizeof(inbuf));
263 memset(&req, 0, sizeof(req));
264
265 req.flags = ATACMD_READ;
266 req.command = WDCC_IDENTIFY;
267 req.databuf = (void *)&inbuf;
268 req.datalen = sizeof(inbuf);
269 req.timeout = 1000;
270
271 error = ioctl(fd, ATAIOCCOMMAND, &req);
272 if (error == -1 || req.retsts != ATACMD_OK)
273 return 0;
274
275 #if BYTE_ORDER == LITTLE_ENDIAN
276 /*
277 * On little endian machines, we need to shuffle the string
278 * byte order. However, we don't have to do this for NEC or
279 * Mitsumi ATAPI devices
280 */
281
282 if (!(inqbuf->atap_config != WDC_CFG_CFA_MAGIC &&
283 (inqbuf->atap_config & WDC_CFG_ATAPI) &&
284 ((inqbuf->atap_model[0] == 'N' &&
285 inqbuf->atap_model[1] == 'E') ||
286 (inqbuf->atap_model[0] == 'F' &&
287 inqbuf->atap_model[1] == 'X')))) {
288 needswap = 1;
289 }
290 #endif
291
292 ata_extract_string(model, sizeof(model),
293 inqbuf->atap_model, sizeof(inqbuf->atap_model), needswap);
294 humanize_number(size, sizeof(size),
295 (uint64_t)dd->dd_secsize * (uint64_t)dd->dd_totsec,
296 "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
297
298 snprintf(dd->dd_descr, sizeof(dd->dd_descr), "%s (%s, %s)",
299 dd->dd_name, size, model);
300
301 return 1;
302 }
303
304 static void
305 get_descr(struct disk_desc *dd)
306 {
307 char diskpath[MAXPATHLEN];
308 int fd = -1;
309
310 fd = opendisk(dd->dd_name, O_RDONLY, diskpath, sizeof(diskpath), 0);
311 if (fd < 0)
312 goto done;
313
314 dd->dd_descr[0] = '\0';
315
316 /* try ATA */
317 if (get_descr_ata(dd, fd))
318 goto done;
319 /* try SCSI */
320 if (get_descr_scsi(dd, fd))
321 goto done;
322
323 done:
324 if (fd >= 0)
325 close(fd);
326 if (strlen(dd->dd_descr) == 0)
327 strcpy(dd->dd_descr, dd->dd_name);
328 }
329
330 /* disknames - contains device names without partition letters
331 * cdrom_devices - contains devices including partition letters
332 * returns the first entry in hw.disknames matching a cdrom_device, or
333 * first entry on error or no match
334 */
335 const char *
336 get_default_cdrom(void)
337 {
338 static const char *cdrom_devices[] = { CD_NAMES, 0};
339 static const char mib_name[] = "hw.disknames";
340 size_t len;
341 char *disknames;
342 char *last;
343 char *name;
344 const char **arg;
345 const char *cd_dev;
346
347 /* On error just use first entry in cdrom_devices */
348 if (sysctlbyname(mib_name, NULL, &len, NULL, 0) == -1)
349 return cdrom_devices[0];
350 if ((disknames = malloc(len + 2)) == 0) /* skip on malloc fail */
351 return cdrom_devices[0];
352
353 (void)sysctlbyname(mib_name, disknames, &len, NULL, 0);
354 for ((name = strtok_r(disknames, " ", &last)); name;
355 (name = strtok_r(NULL, " ", &last))) {
356 for (arg = cdrom_devices; *arg; ++arg) {
357 cd_dev = *arg;
358 /* skip unit and partition */
359 if (strncmp(cd_dev, name, strlen(cd_dev) - 2) != 0)
360 continue;
361 if (name != disknames)
362 strcpy(disknames, name);
363 strcat(disknames, "a");
364 /* XXX: leaks, but so what? */
365 return disknames;
366 }
367 }
368 free(disknames);
369 return cdrom_devices[0];
370 }
371
372 static int
373 get_disks(struct disk_desc *dd)
374 {
375 const char **xd;
376 char *cp;
377 struct disklabel l;
378 int i;
379 int numdisks;
380
381 /* initialize */
382 numdisks = 0;
383
384 for (xd = disk_names; *xd != NULL; xd++) {
385 for (i = 0; i < MAX_DISKS; i++) {
386 strlcpy(dd->dd_name, *xd, sizeof dd->dd_name - 2);
387 cp = strchr(dd->dd_name, ':');
388 if (cp != NULL)
389 dd->dd_no_mbr = !strcmp(cp, ":no_mbr");
390 else {
391 dd->dd_no_mbr = 0;
392 cp = strchr(dd->dd_name, 0);
393 }
394
395 snprintf(cp, 2 + 1, "%d", i);
396 if (!get_geom(dd->dd_name, &l)) {
397 if (errno == ENOENT)
398 break;
399 continue;
400 }
401
402 /*
403 * Exclude a disk mounted as root partition,
404 * in case of install-image on a USB memstick.
405 */
406 if (is_active_rootpart(dd->dd_name, 0))
407 continue;
408
409 dd->dd_cyl = l.d_ncylinders;
410 dd->dd_head = l.d_ntracks;
411 dd->dd_sec = l.d_nsectors;
412 dd->dd_secsize = l.d_secsize;
413 dd->dd_totsec = l.d_secperunit;
414 get_descr(dd);
415 dd++;
416 numdisks++;
417 if (numdisks >= MAX_DISKS)
418 return numdisks;
419 }
420 }
421 return numdisks;
422 }
423
424 static int
425 set_dsk_select(menudesc *m, void *arg)
426 {
427 *(int *)arg = m->cursel;
428 return 1;
429 }
430
431 int
432 find_disks(const char *doingwhat)
433 {
434 struct disk_desc disks[MAX_DISKS];
435 menu_ent dsk_menu[nelem(disks)];
436 struct disk_desc *disk;
437 int i;
438 int numdisks;
439 int selected_disk = 0;
440 int menu_no;
441
442 /* Find disks. */
443 numdisks = get_disks(disks);
444
445 /* need a redraw here, kernel messages hose everything */
446 touchwin(stdscr);
447 refresh();
448 /* Kill typeahead, it won't be what the user had in mind */
449 fpurge(stdin);
450
451 if (numdisks == 0) {
452 /* No disks found! */
453 msg_display(MSG_nodisk);
454 process_menu(MENU_ok, NULL);
455 /*endwin();*/
456 return -1;
457 }
458
459 if (numdisks == 1) {
460 /* One disk found! */
461 msg_display(MSG_onedisk, disks[0].dd_descr, doingwhat);
462 process_menu(MENU_ok, NULL);
463 } else {
464 /* Multiple disks found! */
465 for (i = 0; i < numdisks; i++) {
466 dsk_menu[i].opt_name = disks[i].dd_descr;
467 dsk_menu[i].opt_menu = OPT_NOMENU;
468 dsk_menu[i].opt_flags = OPT_EXIT;
469 dsk_menu[i].opt_action = set_dsk_select;
470 }
471 menu_no = new_menu(MSG_Available_disks,
472 dsk_menu, numdisks, -1, 4, 0, 0,
473 MC_SCROLL | MC_NOEXITOPT,
474 NULL, NULL, NULL, NULL, NULL);
475 if (menu_no == -1)
476 return -1;
477 msg_display(MSG_ask_disk, doingwhat);
478 process_menu(menu_no, &selected_disk);
479 free_menu(menu_no);
480 }
481
482 disk = disks + selected_disk;
483 strlcpy(diskdev, disk->dd_name, sizeof diskdev);
484
485 /* Use as a default disk if the user has the sets on a local disk */
486 strlcpy(localfs_dev, disk->dd_name, sizeof localfs_dev);
487
488 sectorsize = disk->dd_secsize;
489 dlcyl = disk->dd_cyl;
490 dlhead = disk->dd_head;
491 dlsec = disk->dd_sec;
492 dlsize = disk->dd_totsec;
493 no_mbr = disk->dd_no_mbr;
494 if (dlsize == 0)
495 dlsize = disk->dd_cyl * disk->dd_head * disk->dd_sec;
496 if (dlsize > UINT32_MAX) {
497 msg_display(MSG_toobigdisklabel);
498 process_menu(MENU_ok, NULL);
499 return -1;
500 }
501 dlcylsize = dlhead * dlsec;
502
503 /* Get existing/default label */
504 memset(&oldlabel, 0, sizeof oldlabel);
505 incorelabel(diskdev, oldlabel);
506
507 /* Set 'target' label to current label in case we don't change it */
508 memcpy(&bsdlabel, &oldlabel, sizeof bsdlabel);
509
510 return numdisks;
511 }
512
513 void
514 fmt_fspart(menudesc *m, int ptn, void *arg)
515 {
516 unsigned int poffset, psize, pend;
517 const char *desc;
518 static const char *Yes;
519 partinfo *p = bsdlabel + ptn;
520
521 if (Yes == NULL)
522 Yes = msg_string(MSG_Yes);
523
524 poffset = p->pi_offset / sizemult;
525 psize = p->pi_size / sizemult;
526 if (psize == 0)
527 pend = 0;
528 else
529 pend = (p->pi_offset + p->pi_size) / sizemult - 1;
530
531 if (p->pi_fstype == FS_BSDFFS)
532 if (p->pi_flags & PIF_FFSv2)
533 desc = "FFSv2";
534 else
535 desc = "FFSv1";
536 else
537 desc = getfslabelname(p->pi_fstype);
538
539 #ifdef PART_BOOT
540 if (ptn == PART_BOOT)
541 desc = msg_string(MSG_Boot_partition_cant_change);
542 #endif
543 if (ptn == getrawpartition())
544 desc = msg_string(MSG_Whole_disk_cant_change);
545 else {
546 if (ptn == PART_C)
547 desc = msg_string(MSG_NetBSD_partition_cant_change);
548 }
549
550 wprintw(m->mw, msg_string(MSG_fspart_row),
551 poffset, pend, psize, desc,
552 p->pi_flags & PIF_NEWFS ? Yes : "",
553 p->pi_flags & PIF_MOUNT ? Yes : "",
554 p->pi_mount);
555 }
556
557 /*
558 * Label a disk using an MD-specific string DISKLABEL_CMD for
559 * to invoke disklabel.
560 * if MD code does not define DISKLABEL_CMD, this is a no-op.
561 *
562 * i386 port uses "/sbin/disklabel -w -r", just like i386
563 * miniroot scripts, though this may leave a bogus incore label.
564 *
565 * Sun ports should use DISKLABEL_CMD "/sbin/disklabel -w"
566 * to get incore to ondisk inode translation for the Sun proms.
567 */
568 int
569 write_disklabel (void)
570 {
571
572 #ifdef DISKLABEL_CMD
573 /* disklabel the disk */
574 return run_program(RUN_DISPLAY, "%s -f /tmp/disktab %s '%s'",
575 DISKLABEL_CMD, diskdev, bsddiskname);
576 #else
577 return 0;
578 #endif
579 }
580
581
582 static int
583 ptn_sort(const void *a, const void *b)
584 {
585 return strcmp(bsdlabel[*(const int *)a].pi_mount,
586 bsdlabel[*(const int *)b].pi_mount);
587 }
588
589 int
590 make_filesystems(void)
591 {
592 unsigned int i;
593 int ptn;
594 int ptn_order[nelem(bsdlabel)];
595 int error = 0;
596 unsigned int maxpart = getmaxpartitions();
597 char *newfs;
598 const char *mnt_opts;
599 const char *fsname;
600 partinfo *lbl;
601
602 if (maxpart > nelem(bsdlabel))
603 maxpart = nelem(bsdlabel);
604
605 /* Making new file systems and mounting them */
606
607 /* sort to ensure /usr/local is mounted after /usr (etc) */
608 for (i = 0; i < maxpart; i++)
609 ptn_order[i] = i;
610 qsort(ptn_order, maxpart, sizeof ptn_order[0], ptn_sort);
611
612 for (i = 0; i < maxpart; i++) {
613 /*
614 * newfs and mount. For now, process only BSD filesystems.
615 * but if this is the mounted-on root, has no mount
616 * point defined, or is marked preserve, don't touch it!
617 */
618 ptn = ptn_order[i];
619 lbl = bsdlabel + ptn;
620
621 if (is_active_rootpart(diskdev, ptn))
622 continue;
623
624 if (*lbl->pi_mount == 0)
625 /* No mount point */
626 continue;
627
628 newfs = NULL;
629 mnt_opts = NULL;
630 fsname = NULL;
631 switch (lbl->pi_fstype) {
632 case FS_APPLEUFS:
633 asprintf(&newfs, "/sbin/newfs %s%.0d",
634 lbl->pi_isize != 0 ? "-i" : "", lbl->pi_isize);
635 mnt_opts = "-tffs -o async";
636 fsname = "ffs";
637 break;
638 case FS_BSDFFS:
639 asprintf(&newfs,
640 "/sbin/newfs -V2 -O %d -b %d -f %d%s%.0d",
641 lbl->pi_flags & PIF_FFSv2 ? 2 : 1,
642 lbl->pi_fsize * lbl->pi_frag, lbl->pi_fsize,
643 lbl->pi_isize != 0 ? " -i " : "", lbl->pi_isize);
644 if (lbl->pi_flags & PIF_LOG)
645 mnt_opts = "-tffs -o log";
646 else
647 mnt_opts = "-tffs -o async";
648 fsname = "ffs";
649 break;
650 case FS_BSDLFS:
651 asprintf(&newfs, "/sbin/newfs_lfs -b %d",
652 lbl->pi_fsize * lbl->pi_frag);
653 mnt_opts = "-tlfs";
654 fsname = "lfs";
655 break;
656 case FS_MSDOS:
657 #ifdef USE_NEWFS_MSDOS
658 asprintf(&newfs, "/sbin/newfs_msdos");
659 #endif
660 mnt_opts = "-tmsdos";
661 fsname = "msdos";
662 break;
663 #ifdef USE_SYSVBFS
664 case FS_SYSVBFS:
665 asprintf(&newfs, "/sbin/newfs_sysvbfs");
666 mnt_opts = "-tsysvbfs";
667 fsname = "sysvbfs";
668 break;
669 #endif
670 #ifdef USE_EXT2FS
671 case FS_EX2FS:
672 asprintf(&newfs, "/sbin/newfs_ext2fs");
673 mnt_opts = "-text2fs";
674 fsname = "ext2fs";
675 break;
676 #endif
677 }
678 if (lbl->pi_flags & PIF_NEWFS && newfs != NULL) {
679 #ifdef USE_NEWFS_MSDOS
680 if (lbl->pi_fstype == FS_MSDOS) {
681 /* newfs only if mount fails */
682 if (run_program(RUN_SILENT | RUN_ERROR_OK,
683 "mount -rt msdos /dev/%s%c /mnt2",
684 diskdev, 'a' + ptn) != 0)
685 error = run_program(
686 RUN_DISPLAY | RUN_PROGRESS,
687 "%s /dev/r%s%c",
688 newfs, diskdev, 'a' + ptn);
689 else {
690 run_program(RUN_SILENT | RUN_ERROR_OK,
691 "umount /mnt2");
692 error = 0;
693 }
694 } else
695 #endif
696 error = run_program(RUN_DISPLAY | RUN_PROGRESS,
697 "%s /dev/r%s%c", newfs, diskdev, 'a' + ptn);
698 } else {
699 /* We'd better check it isn't dirty */
700 error = fsck_preen(diskdev, ptn, fsname);
701 }
702 free(newfs);
703 if (error != 0)
704 return error;
705
706 md_pre_mount();
707
708 if (lbl->pi_flags & PIF_MOUNT && mnt_opts != NULL) {
709 make_target_dir(lbl->pi_mount);
710 error = target_mount(mnt_opts, diskdev, ptn,
711 lbl->pi_mount);
712 if (error) {
713 msg_display(MSG_mountfail,
714 diskdev, 'a' + ptn, lbl->pi_mount);
715 process_menu(MENU_ok, NULL);
716 return error;
717 }
718 }
719 }
720 return 0;
721 }
722
723 int
724 make_fstab(void)
725 {
726 FILE *f;
727 int i, swap_dev = -1;
728 const char *dump_dev;
729
730 /* Create the fstab. */
731 make_target_dir("/etc");
732 f = target_fopen("/etc/fstab", "w");
733 if (logfp)
734 (void)fprintf(logfp,
735 "Creating %s/etc/fstab.\n", target_prefix());
736 scripting_fprintf(NULL, "cat <<EOF >%s/etc/fstab\n", target_prefix());
737
738 if (f == NULL) {
739 #ifndef DEBUG
740 msg_display(MSG_createfstab);
741 if (logfp)
742 (void)fprintf(logfp, "Failed to make /etc/fstab!\n");
743 process_menu(MENU_ok, NULL);
744 return 1;
745 #else
746 f = stdout;
747 #endif
748 }
749
750 scripting_fprintf(f, "# NetBSD %s/etc/fstab\n# See /usr/share/examples/"
751 "fstab/ for more examples.\n", target_prefix());
752 for (i = 0; i < getmaxpartitions(); i++) {
753 const char *s = "";
754 const char *mp = bsdlabel[i].pi_mount;
755 const char *fstype = "ffs";
756 int fsck_pass = 0, dump_freq = 0;
757
758 if (!*mp) {
759 /*
760 * No mount point specified, comment out line and
761 * use /mnt as a placeholder for the mount point.
762 */
763 s = "# ";
764 mp = "/mnt";
765 }
766
767 switch (bsdlabel[i].pi_fstype) {
768 case FS_UNUSED:
769 continue;
770 case FS_BSDLFS:
771 /* If there is no LFS, just comment it out. */
772 if (!check_lfs_progs())
773 s = "# ";
774 fstype = "lfs";
775 /* FALLTHROUGH */
776 case FS_BSDFFS:
777 fsck_pass = (strcmp(mp, "/") == 0) ? 1 : 2;
778 dump_freq = 1;
779 break;
780 case FS_MSDOS:
781 fstype = "msdos";
782 break;
783 case FS_SWAP:
784 if (swap_dev == -1) {
785 swap_dev = i;
786 dump_dev = ",dp";
787 } else {
788 dump_dev ="";
789 }
790 scripting_fprintf(f, "/dev/%s%c\t\tnone\tswap\tsw%s\t\t 0 0\n",
791 diskdev, 'a' + i, dump_dev);
792 continue;
793 #ifdef USE_SYSVBFS
794 case FS_SYSVBFS:
795 fstype = "sysvbfs";
796 make_target_dir("/stand");
797 break;
798 #endif
799 default:
800 fstype = "???";
801 s = "# ";
802 break;
803 }
804 /* The code that remounts root rw doesn't check the partition */
805 if (strcmp(mp, "/") == 0 && !(bsdlabel[i].pi_flags & PIF_MOUNT))
806 s = "# ";
807
808 scripting_fprintf(f,
809 "%s/dev/%s%c\t\t%s\t%s\trw%s%s%s%s%s%s%s%s\t\t %d %d\n",
810 s, diskdev, 'a' + i, mp, fstype,
811 bsdlabel[i].pi_flags & PIF_LOG ? ",log" : "",
812 bsdlabel[i].pi_flags & PIF_MOUNT ? "" : ",noauto",
813 bsdlabel[i].pi_flags & PIF_ASYNC ? ",async" : "",
814 bsdlabel[i].pi_flags & PIF_NOATIME ? ",noatime" : "",
815 bsdlabel[i].pi_flags & PIF_NODEV ? ",nodev" : "",
816 bsdlabel[i].pi_flags & PIF_NODEVMTIME ? ",nodevmtime" : "",
817 bsdlabel[i].pi_flags & PIF_NOEXEC ? ",noexec" : "",
818 bsdlabel[i].pi_flags & PIF_NOSUID ? ",nosuid" : "",
819 dump_freq, fsck_pass);
820 }
821
822 if (tmp_ramdisk_size != 0) {
823 #ifdef HAVE_TMPFS
824 scripting_fprintf(f, "tmpfs\t\t/tmp\ttmpfs\trw,-m=1777,-s=%"
825 PRIi64 "\n",
826 tmp_ramdisk_size * 512);
827 #else
828 if (swap_dev != -1)
829 scripting_fprintf(f, "/dev/%s%c\t\t/tmp\tmfs\trw,-s=%"
830 PRIi64 "\n",
831 diskdev, 'a' + swap_dev, tmp_ramdisk_size);
832 else
833 scripting_fprintf(f, "swap\t\t/tmp\tmfs\trw,-s=%"
834 PRIi64 "\n",
835 tmp_ramdisk_size);
836 #endif
837 }
838
839 /* Add /kern, /proc and /dev/pts to fstab and make mountpoint. */
840 scripting_fprintf(f, "kernfs\t\t/kern\tkernfs\trw\n");
841 scripting_fprintf(f, "ptyfs\t\t/dev/pts\tptyfs\trw\n");
842 scripting_fprintf(f, "procfs\t\t/proc\tprocfs\trw\n");
843 scripting_fprintf(f, "/dev/%s\t\t/cdrom\tcd9660\tro,noauto\n",
844 get_default_cdrom());
845 scripting_fprintf(f, "%stmpfs\t\t/var/shm\ttmpfs\trw,-m1777,-sram%%25\n",
846 tmpfs_on_var_shm() ? "" : "#");
847 make_target_dir("/kern");
848 make_target_dir("/proc");
849 make_target_dir("/dev/pts");
850 make_target_dir("/cdrom");
851 make_target_dir("/var/shm");
852
853 scripting_fprintf(NULL, "EOF\n");
854
855 #ifndef DEBUG
856 fclose(f);
857 fflush(NULL);
858 #endif
859 return 0;
860 }
861
862
863
864 static int
865 /*ARGSUSED*/
866 foundffs(struct data *list, size_t num)
867 {
868 int error;
869
870 if (num < 2 || strcmp(list[1].u.s_val, "/") == 0 ||
871 strstr(list[2].u.s_val, "noauto") != NULL)
872 return 0;
873
874 error = fsck_preen(list[0].u.s_val, ' '-'a', "ffs");
875 if (error != 0)
876 return error;
877
878 error = target_mount("", list[0].u.s_val, ' '-'a', list[1].u.s_val);
879 if (error != 0) {
880 msg_display(MSG_mount_failed, list[0].u.s_val);
881 process_menu(MENU_noyes, NULL);
882 if (!yesno)
883 return error;
884 }
885 return 0;
886 }
887
888 #ifdef USE_SYSVBFS
889 static int
890 /*ARGSUSED*/
891 foundsysvbfs(struct data *list, size_t num)
892 {
893 int error;
894
895 if (num < 2 || strcmp(list[1].u.s_val, "/") == 0 ||
896 strstr(list[2].u.s_val, "noauto") != NULL)
897 return 0;
898
899 error = target_mount("", list[0].u.s_val, ' '-'a', list[1].u.s_val);
900 if (error != 0)
901 return error;
902 return 0;
903 }
904 #endif
905
906 /*
907 * Do an fsck. On failure, inform the user by showing a warning
908 * message and doing menu_ok() before proceeding.
909 * Returns 0 on success, or nonzero return code from fsck() on failure.
910 */
911 static int
912 fsck_preen(const char *disk, int ptn, const char *fsname)
913 {
914 char *prog;
915 int error;
916
917 ptn += 'a';
918 if (fsname == NULL)
919 return 0;
920 /* first, check if fsck program exists, if not, assume ok */
921 asprintf(&prog, "/sbin/fsck_%s", fsname);
922 if (prog == NULL)
923 return 0;
924 if (access(prog, X_OK) != 0)
925 return 0;
926 if (!strcmp(fsname,"ffs"))
927 fixsb(prog, disk, ptn);
928 error = run_program(0, "%s -p -q /dev/r%s%c", prog, disk, ptn);
929 free(prog);
930 if (error != 0) {
931 msg_display(MSG_badfs, disk, ptn, error);
932 process_menu(MENU_noyes, NULL);
933 if (yesno)
934 error = 0;
935 /* XXX at this point maybe we should run a full fsck? */
936 }
937 return error;
938 }
939
940 /* This performs the same function as the etc/rc.d/fixsb script
941 * which attempts to correct problems with ffs1 filesystems
942 * which may have been introduced by booting a netbsd-current kernel
943 * from between April of 2003 and January 2004. For more information
944 * This script was developed as a response to NetBSD pr install/25138
945 * Additional prs regarding the original issue include:
946 * bin/17910 kern/21283 kern/21404 port-macppc/23925 port-macppc/23926
947 */
948 static void
949 fixsb(const char *prog, const char *disk, char ptn)
950 {
951 int fd;
952 int rval;
953 union {
954 struct fs fs;
955 char buf[SBLOCKSIZE];
956 } sblk;
957 struct fs *fs = &sblk.fs;
958
959 snprintf(sblk.buf, sizeof(sblk.buf), "/dev/r%s%c",
960 disk, ptn == ' ' ? 0 : ptn);
961 fd = open(sblk.buf, O_RDONLY);
962 if (fd == -1)
963 return;
964
965 /* Read ffsv1 main superblock */
966 rval = pread(fd, sblk.buf, sizeof sblk.buf, SBLOCK_UFS1);
967 close(fd);
968 if (rval != sizeof sblk.buf)
969 return;
970
971 if (fs->fs_magic != FS_UFS1_MAGIC &&
972 fs->fs_magic != FS_UFS1_MAGIC_SWAPPED)
973 /* Not FFSv1 */
974 return;
975 if (fs->fs_old_flags & FS_FLAGS_UPDATED)
976 /* properly updated fslevel 4 */
977 return;
978 if (fs->fs_bsize != fs->fs_maxbsize)
979 /* not messed up */
980 return;
981
982 /*
983 * OK we have a munged fs, first 'upgrade' to fslevel 4,
984 * We specify -b16 in order to stop fsck bleating that the
985 * sb doesn't match the first alternate.
986 */
987 run_program(RUN_DISPLAY | RUN_PROGRESS,
988 "%s -p -b 16 -c 4 /dev/r%s%c", prog, disk, ptn);
989 /* Then downgrade to fslevel 3 */
990 run_program(RUN_DISPLAY | RUN_PROGRESS,
991 "%s -p -c 3 /dev/r%s%c", prog, disk, ptn);
992 }
993
994 /*
995 * fsck and mount the root partition.
996 */
997 static int
998 mount_root(void)
999 {
1000 int error;
1001
1002 error = fsck_preen(diskdev, rootpart, "ffs");
1003 if (error != 0)
1004 return error;
1005
1006 md_pre_mount();
1007
1008 /* Mount /dev/<diskdev>a on target's "".
1009 * If we pass "" as mount-on, Prefixing will DTRT.
1010 * for now, use no options.
1011 * XXX consider -o remount in case target root is
1012 * current root, still readonly from single-user?
1013 */
1014 return target_mount("", diskdev, rootpart, "");
1015 }
1016
1017 /* Get information on the file systems mounted from the root filesystem.
1018 * Offer to convert them into 4.4BSD inodes if they are not 4.4BSD
1019 * inodes. Fsck them. Mount them.
1020 */
1021
1022 int
1023 mount_disks(void)
1024 {
1025 char *fstab;
1026 int fstabsize;
1027 int error;
1028
1029 static struct lookfor fstabbuf[] = {
1030 {"/dev/", "/dev/%s %s ffs %s", "c", NULL, 0, 0, foundffs},
1031 {"/dev/", "/dev/%s %s ufs %s", "c", NULL, 0, 0, foundffs},
1032 #ifdef USE_SYSVBFS
1033 {"/dev/", "/dev/%s %s sysvbfs %s", "c", NULL, 0, 0,
1034 foundsysvbfs},
1035 #endif
1036 };
1037 static size_t numfstabbuf = sizeof(fstabbuf) / sizeof(struct lookfor);
1038
1039 /* First the root device. */
1040 if (target_already_root())
1041 /* avoid needing to call target_already_root() again */
1042 targetroot_mnt[0] = 0;
1043 else {
1044 error = mount_root();
1045 if (error != 0 && error != EBUSY)
1046 return -1;
1047 }
1048
1049 /* Check the target /etc/fstab exists before trying to parse it. */
1050 if (target_dir_exists_p("/etc") == 0 ||
1051 target_file_exists_p("/etc/fstab") == 0) {
1052 msg_display(MSG_noetcfstab, diskdev);
1053 process_menu(MENU_ok, NULL);
1054 return -1;
1055 }
1056
1057
1058 /* Get fstab entries from the target-root /etc/fstab. */
1059 fstabsize = target_collect_file(T_FILE, &fstab, "/etc/fstab");
1060 if (fstabsize < 0) {
1061 /* error ! */
1062 msg_display(MSG_badetcfstab, diskdev);
1063 process_menu(MENU_ok, NULL);
1064 return -1;
1065 }
1066 error = walk(fstab, (size_t)fstabsize, fstabbuf, numfstabbuf);
1067 free(fstab);
1068
1069 return error;
1070 }
1071
1072 int
1073 set_swap(const char *disk, partinfo *pp)
1074 {
1075 int i;
1076 char *cp;
1077 int rval;
1078
1079 if (pp == NULL)
1080 pp = oldlabel;
1081
1082 for (i = 0; i < MAXPARTITIONS; i++) {
1083 if (pp[i].pi_fstype != FS_SWAP)
1084 continue;
1085 asprintf(&cp, "/dev/%s%c", disk, 'a' + i);
1086 rval = swapctl(SWAP_ON, cp, 0);
1087 free(cp);
1088 if (rval != 0)
1089 return -1;
1090 }
1091
1092 return 0;
1093 }
1094
1095 int
1096 check_swap(const char *disk, int remove_swap)
1097 {
1098 struct swapent *swap;
1099 char *cp;
1100 int nswap;
1101 int l;
1102 int rval = 0;
1103
1104 nswap = swapctl(SWAP_NSWAP, 0, 0);
1105 if (nswap <= 0)
1106 return 0;
1107
1108 swap = malloc(nswap * sizeof *swap);
1109 if (swap == NULL)
1110 return -1;
1111
1112 nswap = swapctl(SWAP_STATS, swap, nswap);
1113 if (nswap < 0)
1114 goto bad_swap;
1115
1116 l = strlen(disk);
1117 while (--nswap >= 0) {
1118 /* Should we check the se_dev or se_path? */
1119 cp = swap[nswap].se_path;
1120 if (memcmp(cp, "/dev/", 5) != 0)
1121 continue;
1122 if (memcmp(cp + 5, disk, l) != 0)
1123 continue;
1124 if (!isalpha(*(unsigned char *)(cp + 5 + l)))
1125 continue;
1126 if (cp[5 + l + 1] != 0)
1127 continue;
1128 /* ok path looks like it is for this device */
1129 if (!remove_swap) {
1130 /* count active swap areas */
1131 rval++;
1132 continue;
1133 }
1134 if (swapctl(SWAP_OFF, cp, 0) == -1)
1135 rval = -1;
1136 }
1137
1138 done:
1139 free(swap);
1140 return rval;
1141
1142 bad_swap:
1143 rval = -1;
1144 goto done;
1145 }
1146
1147 #ifdef HAVE_BOOTXX_xFS
1148 char *
1149 bootxx_name(void)
1150 {
1151 int fstype;
1152 const char *bootxxname;
1153 char *bootxx;
1154
1155 /* check we have boot code for the root partition type */
1156 fstype = bsdlabel[rootpart].pi_fstype;
1157 switch (fstype) {
1158 #if defined(BOOTXX_FFSV1) || defined(BOOTXX_FFSV2)
1159 case FS_BSDFFS:
1160 if (bsdlabel[rootpart].pi_flags & PIF_FFSv2) {
1161 #ifdef BOOTXX_FFSV2
1162 bootxxname = BOOTXX_FFSV2;
1163 #else
1164 bootxxname = NULL;
1165 #endif
1166 } else {
1167 #ifdef BOOTXX_FFSV1
1168 bootxxname = BOOTXX_FFSV1;
1169 #else
1170 bootxxname = NULL;
1171 #endif
1172 }
1173 break;
1174 #endif
1175 #ifdef BOOTXX_LFS
1176 case FS_BSDLFS:
1177 bootxxname = BOOTXX_LFS;
1178 break;
1179 #endif
1180 default:
1181 bootxxname = NULL;
1182 break;
1183 }
1184
1185 if (bootxxname == NULL)
1186 return NULL;
1187
1188 asprintf(&bootxx, "%s/%s", BOOTXXDIR, bootxxname);
1189 return bootxx;
1190 }
1191 #endif
1192