1 /* $NetBSD: disks.c,v 1.5 2014/08/19 13:26:27 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 int 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 void 580 label_read(void) 581 { 582 /* Get existing/default label */ 583 memset(&pm->oldlabel, 0, sizeof pm->oldlabel); 584 if (! pm->gpt) 585 incorelabel(pm->diskdev, pm->oldlabel); 586 else 587 incoregpt(pm, pm->oldlabel); 588 /* Set 'target' label to current label in case we don't change it */ 589 memcpy(&pm->bsdlabel, &pm->oldlabel, sizeof pm->bsdlabel); 590 #ifndef NO_DISKLABEL 591 if (! pm->gpt) 592 savenewlabel(pm->oldlabel, getmaxpartitions()); 593 #endif 594 } 595 596 void 597 fmt_fspart(menudesc *m, int ptn, void *arg) 598 { 599 unsigned int poffset, psize, pend; 600 const char *desc; 601 static const char *Yes; 602 partinfo *p = pm->bsdlabel + ptn; 603 604 if (Yes == NULL) 605 Yes = msg_string(MSG_Yes); 606 607 poffset = p->pi_offset / sizemult; 608 psize = p->pi_size / sizemult; 609 if (psize == 0) 610 pend = 0; 611 else 612 pend = (p->pi_offset + p->pi_size) / sizemult - 1; 613 614 if (p->pi_fstype == FS_BSDFFS) 615 if (p->pi_flags & PIF_FFSv2) 616 desc = "FFSv2"; 617 else 618 desc = "FFSv1"; 619 else 620 desc = getfslabelname(p->pi_fstype); 621 622 #ifdef PART_BOOT 623 if (ptn == PART_BOOT) 624 desc = msg_string(MSG_Boot_partition_cant_change); 625 #endif 626 if (ptn == getrawpartition()) 627 desc = msg_string(MSG_Whole_disk_cant_change); 628 else { 629 if (ptn == PART_C) 630 desc = msg_string(MSG_NetBSD_partition_cant_change); 631 } 632 633 wprintw(m->mw, msg_string(MSG_fspart_row), 634 poffset, pend, psize, desc, 635 p->pi_flags & PIF_NEWFS ? Yes : "", 636 p->pi_flags & PIF_MOUNT ? Yes : "", 637 p->pi_mount); 638 } 639 640 /* 641 * Label a disk using an MD-specific string DISKLABEL_CMD for 642 * to invoke disklabel. 643 * if MD code does not define DISKLABEL_CMD, this is a no-op. 644 * 645 * i386 port uses "/sbin/disklabel -w -r", just like i386 646 * miniroot scripts, though this may leave a bogus incore label. 647 * 648 * Sun ports should use DISKLABEL_CMD "/sbin/disklabel -w" 649 * to get incore to ondisk inode translation for the Sun proms. 650 */ 651 int 652 write_disklabel (void) 653 { 654 655 #ifdef DISKLABEL_CMD 656 /* disklabel the disk */ 657 return run_program(RUN_DISPLAY, "%s -f /tmp/disktab %s '%s'", 658 DISKLABEL_CMD, pm->diskdev, pm->bsddiskname); 659 #else 660 return 0; 661 #endif 662 } 663 664 665 static int 666 ptn_sort(const void *a, const void *b) 667 { 668 return strcmp(pm->bsdlabel[*(const int *)a].pi_mount, 669 pm->bsdlabel[*(const int *)b].pi_mount); 670 } 671 672 int 673 make_filesystems(void) 674 { 675 unsigned int i; 676 int ptn; 677 int ptn_order[nelem(pm->bsdlabel)]; 678 int error = 0; 679 unsigned int maxpart = getmaxpartitions(); 680 char *newfs = NULL, *dev = NULL, *devdev = NULL; 681 partinfo *lbl; 682 683 if (maxpart > nelem(pm->bsdlabel)) 684 maxpart = nelem(pm->bsdlabel); 685 686 /* Making new file systems and mounting them */ 687 688 /* sort to ensure /usr/local is mounted after /usr (etc) */ 689 for (i = 0; i < maxpart; i++) 690 ptn_order[i] = i; 691 qsort(ptn_order, maxpart, sizeof ptn_order[0], ptn_sort); 692 693 for (i = 0; i < maxpart; i++) { 694 /* 695 * newfs and mount. For now, process only BSD filesystems. 696 * but if this is the mounted-on root, has no mount 697 * point defined, or is marked preserve, don't touch it! 698 */ 699 ptn = ptn_order[i]; 700 lbl = pm->bsdlabel + ptn; 701 702 if (is_active_rootpart(pm->diskdev, ptn)) 703 continue; 704 705 if (*lbl->pi_mount == 0) 706 /* No mount point */ 707 continue; 708 709 if (pm->isspecial) { 710 asprintf(&dev, "%s", pm->diskdev); 711 ptn = 0 - 'a'; 712 } else { 713 asprintf(&dev, "%s%c", pm->diskdev, 'a' + ptn); 714 } 715 if (dev == NULL) 716 return (ENOMEM); 717 asprintf(&devdev, "/dev/%s", dev); 718 if (devdev == NULL) 719 return (ENOMEM); 720 721 newfs = NULL; 722 lbl->mnt_opts = NULL; 723 lbl->fsname = NULL; 724 switch (lbl->pi_fstype) { 725 case FS_APPLEUFS: 726 asprintf(&newfs, "/sbin/newfs %s%.0d", 727 lbl->pi_isize != 0 ? "-i" : "", lbl->pi_isize); 728 lbl->mnt_opts = "-tffs -o async"; 729 lbl->fsname = "ffs"; 730 break; 731 case FS_BSDFFS: 732 asprintf(&newfs, 733 "/sbin/newfs -V2 -O %d -b %d -f %d%s%.0d", 734 lbl->pi_flags & PIF_FFSv2 ? 2 : 1, 735 lbl->pi_fsize * lbl->pi_frag, lbl->pi_fsize, 736 lbl->pi_isize != 0 ? " -i " : "", lbl->pi_isize); 737 if (lbl->pi_flags & PIF_LOG) 738 lbl->mnt_opts = "-tffs -o log"; 739 else 740 lbl->mnt_opts = "-tffs -o async"; 741 lbl->fsname = "ffs"; 742 break; 743 case FS_BSDLFS: 744 asprintf(&newfs, "/sbin/newfs_lfs -b %d", 745 lbl->pi_fsize * lbl->pi_frag); 746 lbl->mnt_opts = "-tlfs"; 747 lbl->fsname = "lfs"; 748 break; 749 case FS_MSDOS: 750 #ifdef USE_NEWFS_MSDOS 751 asprintf(&newfs, "/sbin/newfs_msdos"); 752 #endif 753 lbl->mnt_opts = "-tmsdos"; 754 lbl->fsname = "msdos"; 755 break; 756 #ifdef USE_SYSVBFS 757 case FS_SYSVBFS: 758 asprintf(&newfs, "/sbin/newfs_sysvbfs"); 759 lbl->mnt_opts = "-tsysvbfs"; 760 lbl->fsname = "sysvbfs"; 761 break; 762 #endif 763 #ifdef USE_EXT2FS 764 case FS_EX2FS: 765 asprintf(&newfs, "/sbin/newfs_ext2fs"); 766 lbl->mnt_opts = "-text2fs"; 767 lbl->fsname = "ext2fs"; 768 break; 769 #endif 770 } 771 if (lbl->pi_flags & PIF_NEWFS && newfs != NULL) { 772 #ifdef USE_NEWFS_MSDOS 773 if (lbl->pi_fstype == FS_MSDOS) { 774 /* newfs only if mount fails */ 775 if (run_program(RUN_SILENT | RUN_ERROR_OK, 776 "mount -rt msdos /dev/%s /mnt2", dev) != 0) 777 error = run_program( 778 RUN_DISPLAY | RUN_PROGRESS, 779 "%s /dev/r%s", 780 newfs, dev); 781 else { 782 run_program(RUN_SILENT | RUN_ERROR_OK, 783 "umount /mnt2"); 784 error = 0; 785 } 786 } else 787 #endif 788 error = run_program(RUN_DISPLAY | RUN_PROGRESS, 789 "%s /dev/r%s", newfs, dev); 790 } else { 791 /* We'd better check it isn't dirty */ 792 error = fsck_preen(pm->diskdev, ptn, lbl->fsname); 793 } 794 free(newfs); 795 if (error != 0) 796 return error; 797 798 lbl->pi_flags ^= PIF_NEWFS; 799 md_pre_mount(); 800 801 if (partman_go == 0 && lbl->pi_flags & PIF_MOUNT && 802 lbl->mnt_opts != NULL) { 803 make_target_dir(lbl->pi_mount); 804 error = target_mount_do(lbl->mnt_opts, devdev, lbl->pi_mount); 805 if (error) { 806 msg_display(MSG_mountfail, dev, ' ', lbl->pi_mount); 807 process_menu(MENU_ok, NULL); 808 return error; 809 } 810 } 811 free(devdev); 812 free(dev); 813 } 814 return 0; 815 } 816 817 int 818 make_fstab(void) 819 { 820 FILE *f; 821 int i, swap_dev = -1; 822 const char *dump_dev; 823 char *dev = NULL; 824 pm_devs_t *pm_i; 825 #ifndef HAVE_TMPFS 826 pm_devs_t *pm_with_swap = NULL; 827 #endif 828 829 /* Create the fstab. */ 830 make_target_dir("/etc"); 831 f = target_fopen("/etc/fstab", "w"); 832 scripting_fprintf(NULL, "cat <<EOF >%s/etc/fstab\n", target_prefix()); 833 834 if (logfp) 835 (void)fprintf(logfp, 836 "Making %s/etc/fstab (%s).\n", target_prefix(), pm->diskdev); 837 838 if (f == NULL) { 839 msg_display(MSG_createfstab); 840 if (logfp) 841 (void)fprintf(logfp, "Failed to make /etc/fstab!\n"); 842 process_menu(MENU_ok, NULL); 843 #ifndef DEBUG 844 return 1; 845 #else 846 f = stdout; 847 #endif 848 } 849 850 scripting_fprintf(f, "# NetBSD %s/etc/fstab\n# See /usr/share/examples/" 851 "fstab/ for more examples.\n", target_prefix()); 852 if (! partman_go) { 853 /* We want to process only one disk... */ 854 pm_i = pm; 855 goto onlyonediskinfstab; 856 } 857 SLIST_FOREACH(pm_i, &pm_head, l) { 858 onlyonediskinfstab: 859 for (i = 0; i < getmaxpartitions(); i++) { 860 const char *s = ""; 861 const char *mp = pm_i->bsdlabel[i].pi_mount; 862 const char *fstype = "ffs"; 863 int fsck_pass = 0, dump_freq = 0; 864 865 if (dev != NULL) 866 free(dev); 867 if (pm_i->isspecial) 868 asprintf(&dev, "%s", pm_i->diskdev); 869 else 870 asprintf(&dev, "%s%c", pm_i->diskdev, 'a' + i); 871 if (dev == NULL) 872 return (ENOMEM); 873 874 if (!*mp) { 875 /* 876 * No mount point specified, comment out line and 877 * use /mnt as a placeholder for the mount point. 878 */ 879 s = "# "; 880 mp = "/mnt"; 881 } 882 883 switch (pm_i->bsdlabel[i].pi_fstype) { 884 case FS_UNUSED: 885 continue; 886 case FS_BSDLFS: 887 /* If there is no LFS, just comment it out. */ 888 if (!check_lfs_progs()) 889 s = "# "; 890 fstype = "lfs"; 891 /* FALLTHROUGH */ 892 case FS_BSDFFS: 893 fsck_pass = (strcmp(mp, "/") == 0) ? 1 : 2; 894 dump_freq = 1; 895 break; 896 case FS_MSDOS: 897 fstype = "msdos"; 898 break; 899 case FS_SWAP: 900 if (pm_i->isspecial) 901 continue; 902 if (swap_dev == -1) { 903 swap_dev = i; 904 dump_dev = ",dp"; 905 #ifndef HAVE_TMPFS 906 pm_with_swap = pm_i; 907 #endif 908 } else { 909 dump_dev = ""; 910 } 911 scripting_fprintf(f, "/dev/%s\t\tnone\tswap\tsw%s\t\t 0 0\n", 912 dev, dump_dev); 913 continue; 914 #ifdef USE_SYSVBFS 915 case FS_SYSVBFS: 916 fstype = "sysvbfs"; 917 make_target_dir("/stand"); 918 break; 919 #endif 920 default: 921 fstype = "???"; 922 s = "# "; 923 break; 924 } 925 /* The code that remounts root rw doesn't check the partition */ 926 if (strcmp(mp, "/") == 0 && !(pm_i->bsdlabel[i].pi_flags & PIF_MOUNT)) 927 s = "# "; 928 929 scripting_fprintf(f, 930 "%s/dev/%s\t\t%s\t%s\trw%s%s%s%s%s%s%s%s\t\t %d %d\n", 931 s, dev, mp, fstype, 932 pm_i->bsdlabel[i].pi_flags & PIF_LOG ? ",log" : "", 933 pm_i->bsdlabel[i].pi_flags & PIF_MOUNT ? "" : ",noauto", 934 pm_i->bsdlabel[i].pi_flags & PIF_ASYNC ? ",async" : "", 935 pm_i->bsdlabel[i].pi_flags & PIF_NOATIME ? ",noatime" : "", 936 pm_i->bsdlabel[i].pi_flags & PIF_NODEV ? ",nodev" : "", 937 pm_i->bsdlabel[i].pi_flags & PIF_NODEVMTIME ? ",nodevmtime" : "", 938 pm_i->bsdlabel[i].pi_flags & PIF_NOEXEC ? ",noexec" : "", 939 pm_i->bsdlabel[i].pi_flags & PIF_NOSUID ? ",nosuid" : "", 940 dump_freq, fsck_pass); 941 if (pm_i->isspecial) 942 /* Special device (such as dk*) have only one partition */ 943 break; 944 } 945 /* Simple install, only one disk */ 946 if (!partman_go) 947 break; 948 } 949 if (tmp_ramdisk_size != 0) { 950 #ifdef HAVE_TMPFS 951 scripting_fprintf(f, "tmpfs\t\t/tmp\ttmpfs\trw,-m=1777,-s=%" 952 PRIi64 "\n", 953 tmp_ramdisk_size * 512); 954 #else 955 if (swap_dev != -1 && pm_with_swap != NULL) 956 scripting_fprintf(f, "/dev/%s%c\t\t/tmp\tmfs\trw,-s=%" 957 PRIi64 "\n", 958 pm_with_swap->diskdev, 'a' + swap_dev, tmp_ramdisk_size); 959 else 960 scripting_fprintf(f, "swap\t\t/tmp\tmfs\trw,-s=%" 961 PRIi64 "\n", 962 tmp_ramdisk_size); 963 #endif 964 } 965 966 /* Add /kern, /proc and /dev/pts to fstab and make mountpoint. */ 967 scripting_fprintf(f, "kernfs\t\t/kern\tkernfs\trw\n"); 968 scripting_fprintf(f, "ptyfs\t\t/dev/pts\tptyfs\trw\n"); 969 scripting_fprintf(f, "procfs\t\t/proc\tprocfs\trw\n"); 970 scripting_fprintf(f, "/dev/%s\t\t/cdrom\tcd9660\tro,noauto\n", 971 get_default_cdrom()); 972 scripting_fprintf(f, "%stmpfs\t\t/var/shm\ttmpfs\trw,-m1777,-sram%%25\n", 973 tmpfs_on_var_shm() ? "" : "#"); 974 make_target_dir("/kern"); 975 make_target_dir("/proc"); 976 make_target_dir("/dev/pts"); 977 make_target_dir("/cdrom"); 978 make_target_dir("/var/shm"); 979 980 scripting_fprintf(NULL, "EOF\n"); 981 982 if (dev != NULL) 983 free(dev); 984 fclose(f); 985 fflush(NULL); 986 return 0; 987 } 988 989 990 991 static int 992 /*ARGSUSED*/ 993 foundffs(struct data *list, size_t num) 994 { 995 int error; 996 997 if (num < 2 || strcmp(list[1].u.s_val, "/") == 0 || 998 strstr(list[2].u.s_val, "noauto") != NULL) 999 return 0; 1000 1001 error = fsck_preen(list[0].u.s_val, ' '-'a', "ffs"); 1002 if (error != 0) 1003 return error; 1004 1005 error = target_mount("", list[0].u.s_val, ' '-'a', list[1].u.s_val); 1006 if (error != 0) { 1007 msg_display(MSG_mount_failed, list[0].u.s_val); 1008 process_menu(MENU_noyes, NULL); 1009 if (!yesno) 1010 return error; 1011 } 1012 return 0; 1013 } 1014 1015 #ifdef USE_SYSVBFS 1016 static int 1017 /*ARGSUSED*/ 1018 foundsysvbfs(struct data *list, size_t num) 1019 { 1020 int error; 1021 1022 if (num < 2 || strcmp(list[1].u.s_val, "/") == 0 || 1023 strstr(list[2].u.s_val, "noauto") != NULL) 1024 return 0; 1025 1026 error = target_mount("", list[0].u.s_val, ' '-'a', list[1].u.s_val); 1027 if (error != 0) 1028 return error; 1029 return 0; 1030 } 1031 #endif 1032 1033 /* 1034 * Do an fsck. On failure, inform the user by showing a warning 1035 * message and doing menu_ok() before proceeding. 1036 * Returns 0 on success, or nonzero return code from fsck() on failure. 1037 */ 1038 static int 1039 fsck_preen(const char *disk, int ptn, const char *fsname) 1040 { 1041 char *prog; 1042 int error; 1043 1044 ptn = (ptn < 0)? 0 : 'a' + ptn; 1045 if (fsname == NULL) 1046 return 0; 1047 /* first, check if fsck program exists, if not, assume ok */ 1048 asprintf(&prog, "/sbin/fsck_%s", fsname); 1049 if (prog == NULL) 1050 return 0; 1051 if (access(prog, X_OK) != 0) 1052 return 0; 1053 if (!strcmp(fsname,"ffs")) 1054 fixsb(prog, disk, ptn); 1055 error = run_program(0, "%s -p -q /dev/r%s%c", prog, disk, ptn); 1056 free(prog); 1057 if (error != 0) { 1058 msg_display(MSG_badfs, disk, ptn, error); 1059 process_menu(MENU_noyes, NULL); 1060 if (yesno) 1061 error = 0; 1062 /* XXX at this point maybe we should run a full fsck? */ 1063 } 1064 return error; 1065 } 1066 1067 /* This performs the same function as the etc/rc.d/fixsb script 1068 * which attempts to correct problems with ffs1 filesystems 1069 * which may have been introduced by booting a netbsd-current kernel 1070 * from between April of 2003 and January 2004. For more information 1071 * This script was developed as a response to NetBSD pr install/25138 1072 * Additional prs regarding the original issue include: 1073 * bin/17910 kern/21283 kern/21404 port-macppc/23925 port-macppc/23926 1074 */ 1075 static void 1076 fixsb(const char *prog, const char *disk, char ptn) 1077 { 1078 int fd; 1079 int rval; 1080 union { 1081 struct fs fs; 1082 char buf[SBLOCKSIZE]; 1083 } sblk; 1084 struct fs *fs = &sblk.fs; 1085 1086 snprintf(sblk.buf, sizeof(sblk.buf), "/dev/r%s%c", 1087 disk, ptn == ' ' ? 0 : ptn); 1088 fd = open(sblk.buf, O_RDONLY); 1089 if (fd == -1) 1090 return; 1091 1092 /* Read ffsv1 main superblock */ 1093 rval = pread(fd, sblk.buf, sizeof sblk.buf, SBLOCK_UFS1); 1094 close(fd); 1095 if (rval != sizeof sblk.buf) 1096 return; 1097 1098 if (fs->fs_magic != FS_UFS1_MAGIC && 1099 fs->fs_magic != FS_UFS1_MAGIC_SWAPPED) 1100 /* Not FFSv1 */ 1101 return; 1102 if (fs->fs_old_flags & FS_FLAGS_UPDATED) 1103 /* properly updated fslevel 4 */ 1104 return; 1105 if (fs->fs_bsize != fs->fs_maxbsize) 1106 /* not messed up */ 1107 return; 1108 1109 /* 1110 * OK we have a munged fs, first 'upgrade' to fslevel 4, 1111 * We specify -b16 in order to stop fsck bleating that the 1112 * sb doesn't match the first alternate. 1113 */ 1114 run_program(RUN_DISPLAY | RUN_PROGRESS, 1115 "%s -p -b 16 -c 4 /dev/r%s%c", prog, disk, ptn); 1116 /* Then downgrade to fslevel 3 */ 1117 run_program(RUN_DISPLAY | RUN_PROGRESS, 1118 "%s -p -c 3 /dev/r%s%c", prog, disk, ptn); 1119 } 1120 1121 /* 1122 * fsck and mount the root partition. 1123 */ 1124 static int 1125 mount_root(void) 1126 { 1127 int error; 1128 int ptn = (pm->isspecial)? 0 - 'a' : pm->rootpart; 1129 1130 error = fsck_preen(pm->diskdev, ptn, "ffs"); 1131 if (error != 0) 1132 return error; 1133 1134 md_pre_mount(); 1135 1136 /* Mount /dev/<diskdev>a on target's "". 1137 * If we pass "" as mount-on, Prefixing will DTRT. 1138 * for now, use no options. 1139 * XXX consider -o remount in case target root is 1140 * current root, still readonly from single-user? 1141 */ 1142 return target_mount("", pm->diskdev, ptn, ""); 1143 } 1144 1145 /* Get information on the file systems mounted from the root filesystem. 1146 * Offer to convert them into 4.4BSD inodes if they are not 4.4BSD 1147 * inodes. Fsck them. Mount them. 1148 */ 1149 1150 int 1151 mount_disks(void) 1152 { 1153 char *fstab; 1154 int fstabsize; 1155 int error; 1156 1157 static struct lookfor fstabbuf[] = { 1158 {"/dev/", "/dev/%s %s ffs %s", "c", NULL, 0, 0, foundffs}, 1159 {"/dev/", "/dev/%s %s ufs %s", "c", NULL, 0, 0, foundffs}, 1160 #ifdef USE_SYSVBFS 1161 {"/dev/", "/dev/%s %s sysvbfs %s", "c", NULL, 0, 0, 1162 foundsysvbfs}, 1163 #endif 1164 }; 1165 static size_t numfstabbuf = sizeof(fstabbuf) / sizeof(struct lookfor); 1166 1167 /* First the root device. */ 1168 if (target_already_root()) 1169 /* avoid needing to call target_already_root() again */ 1170 targetroot_mnt[0] = 0; 1171 else { 1172 error = mount_root(); 1173 if (error != 0 && error != EBUSY) 1174 return -1; 1175 } 1176 1177 /* Check the target /etc/fstab exists before trying to parse it. */ 1178 if (target_dir_exists_p("/etc") == 0 || 1179 target_file_exists_p("/etc/fstab") == 0) { 1180 msg_display(MSG_noetcfstab, pm->diskdev); 1181 process_menu(MENU_ok, NULL); 1182 return -1; 1183 } 1184 1185 1186 /* Get fstab entries from the target-root /etc/fstab. */ 1187 fstabsize = target_collect_file(T_FILE, &fstab, "/etc/fstab"); 1188 if (fstabsize < 0) { 1189 /* error ! */ 1190 msg_display(MSG_badetcfstab, pm->diskdev); 1191 process_menu(MENU_ok, NULL); 1192 return -2; 1193 } 1194 error = walk(fstab, (size_t)fstabsize, fstabbuf, numfstabbuf); 1195 free(fstab); 1196 1197 return error; 1198 } 1199 1200 int 1201 set_swap(const char *disk, partinfo *pp) 1202 { 1203 int i; 1204 char *cp; 1205 int rval; 1206 1207 if (pp == NULL) 1208 pp = pm->oldlabel; 1209 1210 for (i = 0; i < MAXPARTITIONS; i++) { 1211 if (pp[i].pi_fstype != FS_SWAP) 1212 continue; 1213 asprintf(&cp, "/dev/%s%c", disk, 'a' + i); 1214 rval = swapctl(SWAP_ON, cp, 0); 1215 free(cp); 1216 if (rval != 0) 1217 return -1; 1218 } 1219 1220 return 0; 1221 } 1222 1223 int 1224 check_swap(const char *disk, int remove_swap) 1225 { 1226 struct swapent *swap; 1227 char *cp; 1228 int nswap; 1229 int l; 1230 int rval = 0; 1231 1232 nswap = swapctl(SWAP_NSWAP, 0, 0); 1233 if (nswap <= 0) 1234 return 0; 1235 1236 swap = malloc(nswap * sizeof *swap); 1237 if (swap == NULL) 1238 return -1; 1239 1240 nswap = swapctl(SWAP_STATS, swap, nswap); 1241 if (nswap < 0) 1242 goto bad_swap; 1243 1244 l = strlen(disk); 1245 while (--nswap >= 0) { 1246 /* Should we check the se_dev or se_path? */ 1247 cp = swap[nswap].se_path; 1248 if (memcmp(cp, "/dev/", 5) != 0) 1249 continue; 1250 if (memcmp(cp + 5, disk, l) != 0) 1251 continue; 1252 if (!isalpha(*(unsigned char *)(cp + 5 + l))) 1253 continue; 1254 if (cp[5 + l + 1] != 0) 1255 continue; 1256 /* ok path looks like it is for this device */ 1257 if (!remove_swap) { 1258 /* count active swap areas */ 1259 rval++; 1260 continue; 1261 } 1262 if (swapctl(SWAP_OFF, cp, 0) == -1) 1263 rval = -1; 1264 } 1265 1266 done: 1267 free(swap); 1268 return rval; 1269 1270 bad_swap: 1271 rval = -1; 1272 goto done; 1273 } 1274 1275 #ifdef HAVE_BOOTXX_xFS 1276 char * 1277 bootxx_name(void) 1278 { 1279 int fstype; 1280 const char *bootxxname; 1281 char *bootxx; 1282 1283 /* check we have boot code for the root partition type */ 1284 fstype = pm->bsdlabel[pm->rootpart].pi_fstype; 1285 switch (fstype) { 1286 #if defined(BOOTXX_FFSV1) || defined(BOOTXX_FFSV2) 1287 case FS_BSDFFS: 1288 if (pm->bsdlabel[pm->rootpart].pi_flags & PIF_FFSv2) { 1289 #ifdef BOOTXX_FFSV2 1290 bootxxname = BOOTXX_FFSV2; 1291 #else 1292 bootxxname = NULL; 1293 #endif 1294 } else { 1295 #ifdef BOOTXX_FFSV1 1296 bootxxname = BOOTXX_FFSV1; 1297 #else 1298 bootxxname = NULL; 1299 #endif 1300 } 1301 break; 1302 #endif 1303 #ifdef BOOTXX_LFS 1304 case FS_BSDLFS: 1305 bootxxname = BOOTXX_LFS; 1306 break; 1307 #endif 1308 default: 1309 bootxxname = NULL; 1310 break; 1311 } 1312 1313 if (bootxxname == NULL) 1314 return NULL; 1315 1316 asprintf(&bootxx, "%s/%s", BOOTXXDIR, bootxxname); 1317 return bootxx; 1318 } 1319 #endif 1320 1321 static int 1322 get_fsid_by_gptuuid(const char *str) 1323 { 1324 int i; 1325 uuid_t uuid; 1326 uint32_t status; 1327 1328 uuid_from_string(str, &uuid, &status); 1329 if (status == uuid_s_ok) { 1330 for (i = 0; gpt_filesystems[i].id > 0; i++) 1331 if (uuid_equal(&uuid, &(gpt_filesystems[i].uuid), NULL)) 1332 return gpt_filesystems[i].id; 1333 } 1334 return FS_OTHER; 1335 } 1336 1337 const char * 1338 get_gptfs_by_id(int filesystem) 1339 { 1340 int i; 1341 for (i = 0; gpt_filesystems[i].id > 0; i++) 1342 if (filesystem == gpt_filesystems[i].id) 1343 return gpt_filesystems[i].name; 1344 return NULL; 1345 } 1346 1347 /* from dkctl.c */ 1348 static int 1349 get_dkwedges_sort(const void *a, const void *b) 1350 { 1351 const struct dkwedge_info *dkwa = a, *dkwb = b; 1352 const daddr_t oa = dkwa->dkw_offset, ob = dkwb->dkw_offset; 1353 return (oa < ob) ? -1 : (oa > ob) ? 1 : 0; 1354 } 1355 1356 int 1357 get_dkwedges(struct dkwedge_info **dkw, const char *diskdev) 1358 { 1359 int fd; 1360 char buf[STRSIZE]; 1361 size_t bufsize; 1362 struct dkwedge_list dkwl; 1363 1364 *dkw = NULL; 1365 dkwl.dkwl_buf = *dkw; 1366 dkwl.dkwl_bufsize = 0; 1367 fd = opendisk(diskdev, O_RDONLY, buf, STRSIZE, 0); 1368 if (fd < 0) 1369 return -1; 1370 1371 for (;;) { 1372 if (ioctl(fd, DIOCLWEDGES, &dkwl) == -1) 1373 return -2; 1374 if (dkwl.dkwl_nwedges == dkwl.dkwl_ncopied) 1375 break; 1376 bufsize = dkwl.dkwl_nwedges * sizeof(**dkw); 1377 if (dkwl.dkwl_bufsize < bufsize) { 1378 *dkw = realloc(dkwl.dkwl_buf, bufsize); 1379 if (*dkw == NULL) 1380 return -3; 1381 dkwl.dkwl_buf = *dkw; 1382 dkwl.dkwl_bufsize = bufsize; 1383 } 1384 } 1385 1386 if (dkwl.dkwl_nwedges > 0 && *dkw != NULL) 1387 qsort(*dkw, dkwl.dkwl_nwedges, sizeof(**dkw), get_dkwedges_sort); 1388 1389 close(fd); 1390 return dkwl.dkwl_nwedges; 1391 } 1392 1393 /* XXX: rewrite */ 1394 static int 1395 incoregpt(pm_devs_t *pm_cur, partinfo *lp) 1396 { 1397 int i, num; 1398 unsigned int p_num; 1399 uint64_t p_start, p_size; 1400 char *textbuf, *t, *tt, p_type[STRSIZE]; 1401 struct dkwedge_info *dkw; 1402 1403 num = get_dkwedges(&dkw, pm_cur->diskdev); 1404 if (dkw != NULL) { 1405 for (i = 0; i < num && i < MAX_WEDGES; i++) 1406 run_program(RUN_SILENT, "dkctl %s delwedge %s", 1407 pm_cur->diskdev, dkw[i].dkw_devname); 1408 free (dkw); 1409 } 1410 1411 if (collect(T_OUTPUT, &textbuf, "gpt show -u %s 2>/dev/null", pm_cur->diskdev) < 1) 1412 return -1; 1413 1414 (void)strtok(textbuf, "\n"); /* ignore first line */ 1415 while ((t = strtok(NULL, "\n")) != NULL) { 1416 i = 0; p_start = 0; p_size = 0; p_num = 0; strcpy(p_type, ""); /* init */ 1417 while ((tt = strsep(&t, " \t")) != NULL) { 1418 if (strlen(tt) == 0) 1419 continue; 1420 if (i == 0) 1421 p_start = strtouq(tt, NULL, 10); 1422 if (i == 1) 1423 p_size = strtouq(tt, NULL, 10); 1424 if (i == 2) 1425 p_num = strtouq(tt, NULL, 10); 1426 if (i > 2 || (i == 2 && p_num == 0)) 1427 if ( 1428 strcmp(tt, "GPT") && 1429 strcmp(tt, "part") && 1430 strcmp(tt, "-") 1431 ) 1432 strncat(p_type, tt, STRSIZE); 1433 i++; 1434 } 1435 if (p_start == 0 || p_size == 0) 1436 continue; 1437 else if (! strcmp(p_type, "Pritable")) 1438 pm_cur->ptstart = p_start + p_size; 1439 else if (! strcmp(p_type, "Sectable")) 1440 pm_cur->ptsize = p_start - pm_cur->ptstart - 1; 1441 else if (p_num == 0 && strlen(p_type) > 0) 1442 /* Utilitary entry (PMBR, etc) */ 1443 continue; 1444 else if (p_num == 0) { 1445 /* Free space */ 1446 continue; 1447 } else { 1448 /* Usual partition */ 1449 lp[p_num].pi_size = p_size; 1450 lp[p_num].pi_offset = p_start; 1451 lp[p_num].pi_fstype = get_fsid_by_gptuuid(p_type); 1452 } 1453 } 1454 free(textbuf); 1455 1456 return 0; 1457 } 1458 1459 static int 1460 is_gpt(const char *dev) 1461 { 1462 return ! run_program(RUN_SILENT | RUN_ERROR_OK, 1463 "sh -c 'gpt show %s |grep -e Pri\\ GPT\\ table'", dev); 1464 } 1465