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