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