disks.c revision 1.8 1 /* $NetBSD: disks.c,v 1.8 2015/05/09 12:06:31 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 process_menu(MENU_noyes, NULL);
1014 if (!yesno)
1015 return error;
1016 }
1017 return 0;
1018 }
1019
1020 #ifdef USE_SYSVBFS
1021 static int
1022 /*ARGSUSED*/
1023 foundsysvbfs(struct data *list, size_t num)
1024 {
1025 int error;
1026
1027 if (num < 2 || strcmp(list[1].u.s_val, "/") == 0 ||
1028 strstr(list[2].u.s_val, "noauto") != NULL)
1029 return 0;
1030
1031 error = target_mount("", list[0].u.s_val, ' '-'a', list[1].u.s_val);
1032 if (error != 0)
1033 return error;
1034 return 0;
1035 }
1036 #endif
1037
1038 /*
1039 * Do an fsck. On failure, inform the user by showing a warning
1040 * message and doing menu_ok() before proceeding.
1041 * Returns 0 on success, or nonzero return code from fsck() on failure.
1042 */
1043 static int
1044 fsck_preen(const char *disk, int ptn, const char *fsname)
1045 {
1046 char *prog;
1047 int error;
1048
1049 ptn = (ptn < 0)? 0 : 'a' + ptn;
1050 if (fsname == NULL)
1051 return 0;
1052 /* first, check if fsck program exists, if not, assume ok */
1053 asprintf(&prog, "/sbin/fsck_%s", fsname);
1054 if (prog == NULL)
1055 return 0;
1056 if (access(prog, X_OK) != 0)
1057 return 0;
1058 if (!strcmp(fsname,"ffs"))
1059 fixsb(prog, disk, ptn);
1060 error = run_program(0, "%s -p -q /dev/r%s%c", prog, disk, ptn);
1061 free(prog);
1062 if (error != 0) {
1063 msg_display(MSG_badfs, disk, ptn, error);
1064 process_menu(MENU_noyes, NULL);
1065 if (yesno)
1066 error = 0;
1067 /* XXX at this point maybe we should run a full fsck? */
1068 }
1069 return error;
1070 }
1071
1072 /* This performs the same function as the etc/rc.d/fixsb script
1073 * which attempts to correct problems with ffs1 filesystems
1074 * which may have been introduced by booting a netbsd-current kernel
1075 * from between April of 2003 and January 2004. For more information
1076 * This script was developed as a response to NetBSD pr install/25138
1077 * Additional prs regarding the original issue include:
1078 * bin/17910 kern/21283 kern/21404 port-macppc/23925 port-macppc/23926
1079 */
1080 static void
1081 fixsb(const char *prog, const char *disk, char ptn)
1082 {
1083 int fd;
1084 int rval;
1085 union {
1086 struct fs fs;
1087 char buf[SBLOCKSIZE];
1088 } sblk;
1089 struct fs *fs = &sblk.fs;
1090
1091 snprintf(sblk.buf, sizeof(sblk.buf), "/dev/r%s%c",
1092 disk, ptn == ' ' ? 0 : ptn);
1093 fd = open(sblk.buf, O_RDONLY);
1094 if (fd == -1)
1095 return;
1096
1097 /* Read ffsv1 main superblock */
1098 rval = pread(fd, sblk.buf, sizeof sblk.buf, SBLOCK_UFS1);
1099 close(fd);
1100 if (rval != sizeof sblk.buf)
1101 return;
1102
1103 if (fs->fs_magic != FS_UFS1_MAGIC &&
1104 fs->fs_magic != FS_UFS1_MAGIC_SWAPPED)
1105 /* Not FFSv1 */
1106 return;
1107 if (fs->fs_old_flags & FS_FLAGS_UPDATED)
1108 /* properly updated fslevel 4 */
1109 return;
1110 if (fs->fs_bsize != fs->fs_maxbsize)
1111 /* not messed up */
1112 return;
1113
1114 /*
1115 * OK we have a munged fs, first 'upgrade' to fslevel 4,
1116 * We specify -b16 in order to stop fsck bleating that the
1117 * sb doesn't match the first alternate.
1118 */
1119 run_program(RUN_DISPLAY | RUN_PROGRESS,
1120 "%s -p -b 16 -c 4 /dev/r%s%c", prog, disk, ptn);
1121 /* Then downgrade to fslevel 3 */
1122 run_program(RUN_DISPLAY | RUN_PROGRESS,
1123 "%s -p -c 3 /dev/r%s%c", prog, disk, ptn);
1124 }
1125
1126 /*
1127 * fsck and mount the root partition.
1128 */
1129 static int
1130 mount_root(void)
1131 {
1132 int error;
1133 int ptn = (pm->isspecial)? 0 - 'a' : pm->rootpart;
1134
1135 error = fsck_preen(pm->diskdev, ptn, "ffs");
1136 if (error != 0)
1137 return error;
1138
1139 md_pre_mount();
1140
1141 /* Mount /dev/<diskdev>a on target's "".
1142 * If we pass "" as mount-on, Prefixing will DTRT.
1143 * for now, use no options.
1144 * XXX consider -o remount in case target root is
1145 * current root, still readonly from single-user?
1146 */
1147 return target_mount("", pm->diskdev, ptn, "");
1148 }
1149
1150 /* Get information on the file systems mounted from the root filesystem.
1151 * Offer to convert them into 4.4BSD inodes if they are not 4.4BSD
1152 * inodes. Fsck them. Mount them.
1153 */
1154
1155 int
1156 mount_disks(void)
1157 {
1158 char *fstab;
1159 int fstabsize;
1160 int error;
1161
1162 static struct lookfor fstabbuf[] = {
1163 {"/dev/", "/dev/%s %s ffs %s", "c", NULL, 0, 0, foundffs},
1164 {"/dev/", "/dev/%s %s ufs %s", "c", NULL, 0, 0, foundffs},
1165 #ifdef USE_SYSVBFS
1166 {"/dev/", "/dev/%s %s sysvbfs %s", "c", NULL, 0, 0,
1167 foundsysvbfs},
1168 #endif
1169 };
1170 static size_t numfstabbuf = sizeof(fstabbuf) / sizeof(struct lookfor);
1171
1172 /* First the root device. */
1173 if (target_already_root())
1174 /* avoid needing to call target_already_root() again */
1175 targetroot_mnt[0] = 0;
1176 else {
1177 error = mount_root();
1178 if (error != 0 && error != EBUSY)
1179 return -1;
1180 }
1181
1182 /* Check the target /etc/fstab exists before trying to parse it. */
1183 if (target_dir_exists_p("/etc") == 0 ||
1184 target_file_exists_p("/etc/fstab") == 0) {
1185 msg_display(MSG_noetcfstab, pm->diskdev);
1186 process_menu(MENU_ok, NULL);
1187 return -1;
1188 }
1189
1190
1191 /* Get fstab entries from the target-root /etc/fstab. */
1192 fstabsize = target_collect_file(T_FILE, &fstab, "/etc/fstab");
1193 if (fstabsize < 0) {
1194 /* error ! */
1195 msg_display(MSG_badetcfstab, pm->diskdev);
1196 process_menu(MENU_ok, NULL);
1197 return -2;
1198 }
1199 error = walk(fstab, (size_t)fstabsize, fstabbuf, numfstabbuf);
1200 free(fstab);
1201
1202 return error;
1203 }
1204
1205 int
1206 set_swap_if_low_ram(const char *disk, partinfo *pp) {
1207 if (get_ramsize() <= 32)
1208 return set_swap(disk, pp);
1209 return 0;
1210 }
1211
1212 int
1213 set_swap(const char *disk, partinfo *pp)
1214 {
1215 int i;
1216 char *cp;
1217 int rval;
1218
1219 if (pp == NULL)
1220 pp = pm->oldlabel;
1221
1222 for (i = 0; i < MAXPARTITIONS; i++) {
1223 if (pp[i].pi_fstype != FS_SWAP)
1224 continue;
1225 asprintf(&cp, "/dev/%s%c", disk, 'a' + i);
1226 rval = swapctl(SWAP_ON, cp, 0);
1227 free(cp);
1228 if (rval != 0)
1229 return -1;
1230 }
1231
1232 return 0;
1233 }
1234
1235 int
1236 check_swap(const char *disk, int remove_swap)
1237 {
1238 struct swapent *swap;
1239 char *cp;
1240 int nswap;
1241 int l;
1242 int rval = 0;
1243
1244 nswap = swapctl(SWAP_NSWAP, 0, 0);
1245 if (nswap <= 0)
1246 return 0;
1247
1248 swap = malloc(nswap * sizeof *swap);
1249 if (swap == NULL)
1250 return -1;
1251
1252 nswap = swapctl(SWAP_STATS, swap, nswap);
1253 if (nswap < 0)
1254 goto bad_swap;
1255
1256 l = strlen(disk);
1257 while (--nswap >= 0) {
1258 /* Should we check the se_dev or se_path? */
1259 cp = swap[nswap].se_path;
1260 if (memcmp(cp, "/dev/", 5) != 0)
1261 continue;
1262 if (memcmp(cp + 5, disk, l) != 0)
1263 continue;
1264 if (!isalpha(*(unsigned char *)(cp + 5 + l)))
1265 continue;
1266 if (cp[5 + l + 1] != 0)
1267 continue;
1268 /* ok path looks like it is for this device */
1269 if (!remove_swap) {
1270 /* count active swap areas */
1271 rval++;
1272 continue;
1273 }
1274 if (swapctl(SWAP_OFF, cp, 0) == -1)
1275 rval = -1;
1276 }
1277
1278 done:
1279 free(swap);
1280 return rval;
1281
1282 bad_swap:
1283 rval = -1;
1284 goto done;
1285 }
1286
1287 #ifdef HAVE_BOOTXX_xFS
1288 char *
1289 bootxx_name(void)
1290 {
1291 int fstype;
1292 const char *bootxxname;
1293 char *bootxx;
1294
1295 /* check we have boot code for the root partition type */
1296 fstype = pm->bsdlabel[pm->rootpart].pi_fstype;
1297 switch (fstype) {
1298 #if defined(BOOTXX_FFSV1) || defined(BOOTXX_FFSV2)
1299 case FS_BSDFFS:
1300 if (pm->bsdlabel[pm->rootpart].pi_flags & PIF_FFSv2) {
1301 #ifdef BOOTXX_FFSV2
1302 bootxxname = BOOTXX_FFSV2;
1303 #else
1304 bootxxname = NULL;
1305 #endif
1306 } else {
1307 #ifdef BOOTXX_FFSV1
1308 bootxxname = BOOTXX_FFSV1;
1309 #else
1310 bootxxname = NULL;
1311 #endif
1312 }
1313 break;
1314 #endif
1315 #ifdef BOOTXX_LFS
1316 case FS_BSDLFS:
1317 bootxxname = BOOTXX_LFS;
1318 break;
1319 #endif
1320 default:
1321 bootxxname = NULL;
1322 break;
1323 }
1324
1325 if (bootxxname == NULL)
1326 return NULL;
1327
1328 asprintf(&bootxx, "%s/%s", BOOTXXDIR, bootxxname);
1329 return bootxx;
1330 }
1331 #endif
1332
1333 static int
1334 get_fsid_by_gptuuid(const char *str)
1335 {
1336 int i;
1337 uuid_t uuid;
1338 uint32_t status;
1339
1340 uuid_from_string(str, &uuid, &status);
1341 if (status == uuid_s_ok) {
1342 for (i = 0; gpt_filesystems[i].id > 0; i++)
1343 if (uuid_equal(&uuid, &(gpt_filesystems[i].uuid), NULL))
1344 return gpt_filesystems[i].id;
1345 }
1346 return FS_OTHER;
1347 }
1348
1349 const char *
1350 get_gptfs_by_id(int filesystem)
1351 {
1352 int i;
1353 for (i = 0; gpt_filesystems[i].id > 0; i++)
1354 if (filesystem == gpt_filesystems[i].id)
1355 return gpt_filesystems[i].name;
1356 return NULL;
1357 }
1358
1359 /* from dkctl.c */
1360 static int
1361 get_dkwedges_sort(const void *a, const void *b)
1362 {
1363 const struct dkwedge_info *dkwa = a, *dkwb = b;
1364 const daddr_t oa = dkwa->dkw_offset, ob = dkwb->dkw_offset;
1365 return (oa < ob) ? -1 : (oa > ob) ? 1 : 0;
1366 }
1367
1368 int
1369 get_dkwedges(struct dkwedge_info **dkw, const char *diskdev)
1370 {
1371 int fd;
1372 char buf[STRSIZE];
1373 size_t bufsize;
1374 struct dkwedge_list dkwl;
1375
1376 *dkw = NULL;
1377 dkwl.dkwl_buf = *dkw;
1378 dkwl.dkwl_bufsize = 0;
1379 fd = opendisk(diskdev, O_RDONLY, buf, STRSIZE, 0);
1380 if (fd < 0)
1381 return -1;
1382
1383 for (;;) {
1384 if (ioctl(fd, DIOCLWEDGES, &dkwl) == -1)
1385 return -2;
1386 if (dkwl.dkwl_nwedges == dkwl.dkwl_ncopied)
1387 break;
1388 bufsize = dkwl.dkwl_nwedges * sizeof(**dkw);
1389 if (dkwl.dkwl_bufsize < bufsize) {
1390 *dkw = realloc(dkwl.dkwl_buf, bufsize);
1391 if (*dkw == NULL)
1392 return -3;
1393 dkwl.dkwl_buf = *dkw;
1394 dkwl.dkwl_bufsize = bufsize;
1395 }
1396 }
1397
1398 if (dkwl.dkwl_nwedges > 0 && *dkw != NULL)
1399 qsort(*dkw, dkwl.dkwl_nwedges, sizeof(**dkw), get_dkwedges_sort);
1400
1401 close(fd);
1402 return dkwl.dkwl_nwedges;
1403 }
1404
1405 /* XXX: rewrite */
1406 static int
1407 incoregpt(pm_devs_t *pm_cur, partinfo *lp)
1408 {
1409 int i, num;
1410 unsigned int p_num;
1411 uint64_t p_start, p_size;
1412 char *textbuf, *t, *tt, p_type[STRSIZE];
1413 struct dkwedge_info *dkw;
1414
1415 num = get_dkwedges(&dkw, pm_cur->diskdev);
1416 if (dkw != NULL) {
1417 for (i = 0; i < num && i < MAX_WEDGES; i++)
1418 run_program(RUN_SILENT, "dkctl %s delwedge %s",
1419 pm_cur->diskdev, dkw[i].dkw_devname);
1420 free (dkw);
1421 }
1422
1423 if (collect(T_OUTPUT, &textbuf, "gpt show -u %s 2>/dev/null", pm_cur->diskdev) < 1)
1424 return -1;
1425
1426 (void)strtok(textbuf, "\n"); /* ignore first line */
1427 while ((t = strtok(NULL, "\n")) != NULL) {
1428 i = 0; p_start = 0; p_size = 0; p_num = 0; strcpy(p_type, ""); /* init */
1429 while ((tt = strsep(&t, " \t")) != NULL) {
1430 if (strlen(tt) == 0)
1431 continue;
1432 if (i == 0)
1433 p_start = strtouq(tt, NULL, 10);
1434 if (i == 1)
1435 p_size = strtouq(tt, NULL, 10);
1436 if (i == 2)
1437 p_num = strtouq(tt, NULL, 10);
1438 if (i > 2 || (i == 2 && p_num == 0))
1439 if (
1440 strcmp(tt, "GPT") &&
1441 strcmp(tt, "part") &&
1442 strcmp(tt, "-")
1443 )
1444 strncat(p_type, tt, STRSIZE);
1445 i++;
1446 }
1447 if (p_start == 0 || p_size == 0)
1448 continue;
1449 else if (! strcmp(p_type, "Pritable"))
1450 pm_cur->ptstart = p_start + p_size;
1451 else if (! strcmp(p_type, "Sectable"))
1452 pm_cur->ptsize = p_start - pm_cur->ptstart - 1;
1453 else if (p_num == 0 && strlen(p_type) > 0)
1454 /* Utilitary entry (PMBR, etc) */
1455 continue;
1456 else if (p_num == 0) {
1457 /* Free space */
1458 continue;
1459 } else {
1460 /* Usual partition */
1461 lp[p_num].pi_size = p_size;
1462 lp[p_num].pi_offset = p_start;
1463 lp[p_num].pi_fstype = get_fsid_by_gptuuid(p_type);
1464 }
1465 }
1466 free(textbuf);
1467
1468 return 0;
1469 }
1470
1471 static bool
1472 is_gpt(const char *dev)
1473 {
1474 check_available_binaries();
1475
1476 if (!have_gpt)
1477 return false;
1478
1479 return !run_program(RUN_SILENT | RUN_ERROR_OK,
1480 "sh -c 'gpt show %s |grep -e Pri\\ GPT\\ table'", dev);
1481 }
1482