xref: /netbsd-src/usr.sbin/sysinst/gpt.c (revision aef5eb5f59cdfe8314f1b5f78ac04eb144e44010)
1 /*	$NetBSD: gpt.c,v 1.29 2022/06/11 15:41:19 martin Exp $	*/
2 
3 /*
4  * Copyright 2018 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26  * THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  */
29 
30 #include "defs.h"
31 #include "mbr.h"
32 #include "md.h"
33 #include "gpt_uuid.h"
34 #include <assert.h>
35 #include <errno.h>
36 #include <err.h>
37 #include <paths.h>
38 #include <sys/param.h>
39 #include <sys/ioctl.h>
40 #include <util.h>
41 #include <uuid.h>
42 
43 bool	gpt_parts_check(void);	/* check for needed binaries */
44 
45 
46 /*************** GPT ************************************************/
47 /* a GPT based disk_partitions interface */
48 
49 #define GUID_STR_LEN	40
50 #define	GPT_PTYPE_ALLOC	32	/* initial type array allocation, should be >
51 				 * gpt type -l | wc -l */
52 #define	GPT_DEV_LEN	DISKNAMESIZE	/* dkNN */
53 
54 #define	GPT_PARTS_PER_SEC	4	/* a 512 byte sector holds 4 entries */
55 #define	GPT_DEFAULT_MAX_PARTS	128
56 
57 /* a usable label will be short, so we can get away with an arbitrary limit */
58 #define	GPT_LABEL_LEN		96
59 
60 #define	GPT_ATTR_BIOSBOOT	1
61 #define	GPT_ATTR_BOOTME		2
62 #define	GPT_ATTR_BOOTONCE	4
63 #define	GPT_ATTR_BOOTFAILED	8
64 #define	GPT_ATTR_NOBLOCKIO	16
65 #define	GPT_ATTR_REQUIRED	32
66 
67 /* when we don't care for BIOS or UEFI boot, use the combined boot flags */
68 #define	GPT_ATTR_BOOT	(GPT_ATTR_BIOSBOOT|GPT_ATTR_BOOTME)
69 
70 struct gpt_attr_desc {
71 	const char *name;
72 	uint flag;
73 };
74 static const struct gpt_attr_desc gpt_avail_attrs[] = {
75 	{ "biosboot", GPT_ATTR_BIOSBOOT },
76 	{ "bootme", GPT_ATTR_BOOTME },
77 	{ "bootonce", GPT_ATTR_BOOTONCE },
78 	{ "bootfailed", GPT_ATTR_BOOTFAILED },
79 	{ "noblockio", GPT_ATTR_NOBLOCKIO },
80 	{ "required", GPT_ATTR_REQUIRED },
81 	{ NULL, 0 }
82 };
83 
84 struct gpt_ptype_desc {
85 	struct part_type_desc gent;
86 	char tid[GUID_STR_LEN];
87 	uint fsflags, default_fs_type;
88 };
89 
90 static const
91 struct {
92 	const char *name;
93 	uint fstype;
94 	enum part_type ptype;
95 	uint fsflags;
96 } gpt_fs_types[] = {
97 	{ .name = "ffs",	.fstype = FS_BSDFFS,	.ptype = PT_root,
98 	  .fsflags = GLM_LIKELY_FFS },
99 	{ .name = "swap",	.fstype = FS_SWAP,	.ptype = PT_swap },
100 	{ .name = "windows",	.fstype = FS_MSDOS,	.ptype = PT_FAT,
101 	  .fsflags = GLM_MAYBE_FAT32|GLM_MAYBE_NTFS },
102 	{ .name = "windows",	.fstype = FS_NTFS,	.ptype = PT_FAT,
103 	  .fsflags = GLM_MAYBE_FAT32|GLM_MAYBE_NTFS },
104 	{ .name = "efi",	.fstype = FS_MSDOS,	.ptype = PT_EFI_SYSTEM,
105 	  .fsflags = GLM_MAYBE_FAT32 },
106 	{ .name = "efi",	.fstype = FS_EFI_SP,	.ptype = PT_EFI_SYSTEM,
107 	  .fsflags = GLM_MAYBE_FAT32 },
108 	{ .name = "bios",	.fstype = FS_MSDOS,	.ptype = PT_FAT,
109 	  .fsflags = GLM_MAYBE_FAT32 },
110 	{ .name = "lfs",	.fstype = FS_BSDLFS,	.ptype = PT_root },
111 	{ .name = "linux-data",	.fstype = FS_EX2FS,	.ptype = PT_root },
112 	{ .name = "apple",	.fstype = FS_HFS,	.ptype = PT_unknown },
113 	{ .name = "ccd",	.fstype = FS_CCD,	.ptype = PT_root },
114 	{ .name = "cgd",	.fstype = FS_CGD,	.ptype = PT_root },
115 	{ .name = "raid",	.fstype = FS_RAID,	.ptype = PT_root },
116 	{ .name = "vmcore",	.fstype = FS_VMKCORE,	.ptype = PT_unknown },
117 	{ .name = "vmfs",	.fstype = FS_VMFS,	.ptype = PT_unknown },
118 	{ .name = "vmresered",	.fstype = FS_VMWRESV,	.ptype = PT_unknown },
119 	{ .name = "zfs",	.fstype = FS_ZFS,	.ptype = PT_root },
120 };
121 
122 static size_t gpt_ptype_cnt = 0, gpt_ptype_alloc = 0;
123 static struct gpt_ptype_desc *gpt_ptype_descs = NULL;
124 
125 /* "well" known types with special handling */
126 static const struct part_type_desc *gpt_native_root;
127 
128 /* similar to struct gpt_ent, but matching our needs */
129 struct gpt_part_entry {
130 	const struct gpt_ptype_desc *gp_type;
131 	char gp_id[GUID_STR_LEN];	/* partition guid as string */
132 	daddr_t gp_start, gp_size;
133 	uint gp_attr;			/* various attribute bits */
134 	char gp_label[GPT_LABEL_LEN];	/* user defined label */
135 	char gp_dev_name[GPT_DEV_LEN];	/* name of wedge */
136 	const char *last_mounted;	/* last mounted if known */
137 	uint fs_type, fs_sub_type,	/* FS_* and maybe sub type */
138 	    fs_opt1, fs_opt2, fs_opt3;	/* transient file system options */
139 	uint gp_flags;
140 #define	GPEF_ON_DISK	1		/* This entry exists on-disk */
141 #define	GPEF_MODIFIED	2		/* this entry has been changed */
142 #define	GPEF_WEDGE	4		/* wedge for this exists */
143 #define	GPEF_RESIZED	8		/* size has changed */
144 #define	GPEF_TARGET	16		/* marked install target */
145 	struct gpt_part_entry *gp_next;
146 };
147 
148 static const struct gpt_ptype_desc *gpt_find_native_type(
149     const struct part_type_desc *gent);
150 static const struct gpt_ptype_desc *gpt_find_guid_type(const char*);
151 static bool
152 gpt_info_to_part(struct gpt_part_entry *p, const struct disk_part_info *info,
153     const char **err_msg);
154 
155 const struct disk_partitioning_scheme gpt_parts;
156 struct gpt_disk_partitions {
157 	struct disk_partitions dp;
158 	/*
159 	 * We keep a list of our current valid partitions, pointed
160 	 * to by "partitions".
161 	 * dp.num_part is the number of entries in "partitions".
162 	 * When partitions that have a representation on disk already
163 	 * are deleted, we move them to the "obsolete" list so we
164 	 * can issue the proper commands to remove it when writing back.
165 	 */
166 	struct gpt_part_entry *partitions,	/* current partitions */
167 	    *obsolete;				/* deleted partitions */
168 	size_t max_num_parts;			/* how many entries max? */
169 	size_t prologue, epilogue;		/* number of sectors res. */
170 	bool has_gpt;	/* disk already has a GPT */
171 };
172 
173 /*
174  * Init global variables from MD details
175  */
176 static void
177 gpt_md_init(bool is_boot_disk, size_t *max_parts, size_t *head, size_t *tail)
178 {
179 	size_t num;
180 
181 	if (is_boot_disk) {
182 #ifdef MD_GPT_INITIAL_SIZE
183 #if MD_GPT_INITIAL_SIZE < 2*512
184 #error	impossible small GPT prologue
185 #endif
186 		num = ((MD_GPT_INITIAL_SIZE-(2*512))/512)*GPT_PARTS_PER_SEC;
187 #else
188 		num = GPT_DEFAULT_MAX_PARTS;
189 #endif
190 	} else {
191 		num = GPT_DEFAULT_MAX_PARTS;
192 	}
193 	*max_parts = num;
194 	*head = 2 + num/GPT_PARTS_PER_SEC;
195 	*tail = 1 + num/GPT_PARTS_PER_SEC;
196 }
197 
198 /*
199  * Parse a part of "gpt show" output into a struct gpt_part_entry.
200  * Output is from "show -a" format if details = false, otherwise
201  * from details for a specific partition (show -i or show -b)
202  */
203 static void
204 gpt_add_info(struct gpt_part_entry *part, const char *tag, char *val,
205     bool details)
206 {
207 	char *s, *e;
208 
209 	if (details && strcmp(tag, "Start:") == 0) {
210 		part->gp_start = strtouq(val, NULL, 10);
211 	} else if (details && strcmp(tag, "Size:") == 0) {
212 		part->gp_size = strtouq(val, NULL, 10);
213 	} else if (details && strcmp(tag, "Type:") == 0) {
214 		s = strchr(val, '(');
215 		if (!s)
216 			return;
217 		e = strchr(s, ')');
218 		if (!e)
219 			return;
220 		*e = 0;
221 		part->gp_type = gpt_find_guid_type(s+1);
222 	} else if (strcmp(tag, "TypeID:") == 0) {
223 		part->gp_type = gpt_find_guid_type(val);
224 	} else if (strcmp(tag, "GUID:") == 0) {
225 		strlcpy(part->gp_id, val, sizeof(part->gp_id));
226 	} else if (strcmp(tag, "Label:") == 0) {
227 		strlcpy(part->gp_label, val, sizeof(part->gp_label));
228 	} else if (strcmp(tag, "Attributes:") == 0) {
229 		char *n;
230 
231 		while ((n = strsep(&val, ", ")) != NULL) {
232 			if (*n == 0)
233 				continue;
234 			for (const struct gpt_attr_desc *p = gpt_avail_attrs;
235 			    p->name != NULL; p++) {
236 				if (strcmp(p->name, n) == 0)
237 					part->gp_attr |= p->flag;
238 			}
239 		}
240 	}
241 }
242 
243 /*
244  * Find the partition matching this wedge info and record that we
245  * have a wedge already.
246  */
247 static void
248 update_part_from_wedge_info(struct gpt_disk_partitions *parts,
249     const struct dkwedge_info *dkw)
250 {
251 	for (struct gpt_part_entry *p = parts->partitions; p != NULL;
252 	    p = p->gp_next) {
253 		if (p->gp_start != dkw->dkw_offset ||
254 		    (uint64_t)p->gp_size != dkw->dkw_size)
255 			continue;
256 		p->gp_flags |= GPEF_WEDGE;
257 		strlcpy(p->gp_dev_name, dkw->dkw_devname,
258 		    sizeof p->gp_dev_name);
259 		return;
260 	}
261 }
262 
263 static struct disk_partitions *
264 gpt_read_from_disk(const char *dev, daddr_t start, daddr_t len, size_t bps,
265     const struct disk_partitioning_scheme *scheme)
266 {
267 	char diskpath[MAXPATHLEN];
268 	int fd;
269 	struct dkwedge_info *dkw;
270 	struct dkwedge_list dkwl;
271 	size_t bufsize, dk;
272 
273 	assert(start == 0);
274 	assert(have_gpt);
275 
276 	if (run_program(RUN_SILENT | RUN_ERROR_OK,
277 	    "gpt -rq header %s", dev) != 0)
278 		return NULL;
279 
280 	/* read the partitions */
281 	int i;
282 	unsigned int p_index;
283 	daddr_t p_start = 0, p_size = 0, avail_start = 0, avail_size = 0,
284 	    disk_size = 0;
285 	char *textbuf, *t, *tt, p_type[STRSIZE];
286 	static const char regpart_prefix[] = "GPT part - ";
287 	struct gpt_disk_partitions *parts;
288 	struct gpt_part_entry *last = NULL, *add_to = NULL;
289 	const struct gpt_ptype_desc *native_root
290 	     = gpt_find_native_type(gpt_native_root);
291 	bool have_target = false;
292 
293 	if (collect(T_OUTPUT, &textbuf, "gpt -r show -a %s 2>/dev/null", dev)
294 	    < 1)
295 		return NULL;
296 
297 	/* parse output and create our list */
298 	parts = calloc(1, sizeof(*parts));
299 	if (parts == NULL)
300 		return NULL;
301 
302 	(void)strtok(textbuf, "\n"); /* ignore first line */
303 	while ((t = strtok(NULL, "\n")) != NULL) {
304 		i = 0; p_start = 0; p_size = 0; p_index = 0;
305 		p_type[0] = 0;
306 		while ((tt = strsep(&t, " \t")) != NULL) {
307 			if (strlen(tt) == 0)
308 				continue;
309 			if (i == 0) {
310 				if (add_to != NULL)
311 					gpt_add_info(add_to, tt, t, false);
312 				p_start = strtouq(tt, NULL, 10);
313 				if (p_start == 0 && add_to != NULL)
314 					break;
315 				else
316 					add_to = NULL;
317 			}
318 			if (i == 1)
319 				p_size = strtouq(tt, NULL, 10);
320 			if (i == 2)
321 				p_index = strtouq(tt, NULL, 10);
322 			if (i > 2 || (i == 2 && p_index == 0)) {
323 				if (p_type[0])
324 					strlcat(p_type, " ", STRSIZE);
325 				strlcat(p_type, tt, STRSIZE);
326 			}
327 			i++;
328 		}
329 
330 		if (p_start == 0 || p_size == 0)
331 			continue;
332 		else if (strcmp(p_type, "Pri GPT table") == 0) {
333 			avail_start = p_start + p_size;
334 			parts->prologue = avail_start;
335 			parts->epilogue = p_size + 1;
336 			parts->max_num_parts = p_size * GPT_PARTS_PER_SEC;
337 		} else if (strcmp(p_type, "Sec GPT table") == 0)
338 			avail_size = p_start - avail_start;
339 		else if(strcmp(p_type, "Sec GPT header") == 0)
340 			disk_size = p_start + p_size;
341 		else if (p_index == 0 && strlen(p_type) > 0)
342 			/* Utilitary entry (PMBR, etc) */
343 			continue;
344 		else if (p_index == 0) {
345 			/* Free space */
346 			continue;
347 		} else {
348 			/* Usual partition */
349 			tt = p_type;
350 			if (strncmp(tt, regpart_prefix,
351 			    strlen(regpart_prefix)) == 0)
352 				tt += strlen(regpart_prefix);
353 
354 			/* Add to our linked list */
355 			struct gpt_part_entry *np = calloc(1, sizeof(*np));
356 			if (np == NULL)
357 				break;
358 
359 			strlcpy(np->gp_label, tt, sizeof(np->gp_label));
360 			np->gp_start = p_start;
361 			np->gp_size = p_size;
362 			np->gp_flags |= GPEF_ON_DISK;
363 			if (!have_target && native_root != NULL &&
364 			    strcmp(np->gp_id, native_root->tid) == 0) {
365 				have_target = true;
366 				np->gp_flags |= GPEF_TARGET;
367 			}
368 
369 			if (last == NULL)
370 				parts->partitions = np;
371 			else
372 				last->gp_next = np;
373 			last = np;
374 			add_to = np;
375 			parts->dp.num_part++;
376 		}
377 	}
378 	free(textbuf);
379 
380 	/* If the GPT was not complete (e.g. truncated image), barf */
381 	if (disk_size <= 0) {
382 		free(parts);
383 		return NULL;
384 	}
385 
386 	parts->dp.pscheme = scheme;
387 	parts->dp.disk = strdup(dev);
388 	parts->dp.disk_start = start;
389 	parts->dp.disk_size = disk_size;
390 	parts->dp.free_space = avail_size;
391 	parts->dp.bytes_per_sector = bps;
392 	parts->has_gpt = true;
393 
394 	fd = opendisk(parts->dp.disk, O_RDONLY, diskpath, sizeof(diskpath), 0);
395 	for (struct gpt_part_entry *p = parts->partitions; p != NULL;
396 	    p = p->gp_next) {
397 #ifdef DEFAULT_UFS2
398 		bool fs_is_default = false;
399 #endif
400 
401 		if (p->gp_type != NULL) {
402 
403 			if (p->gp_type->fsflags != 0) {
404 				const char *lm = get_last_mounted(fd,
405 				    p->gp_start, &p->fs_type,
406 				    &p->fs_sub_type, p->gp_type->fsflags);
407 				if (lm != NULL && *lm != 0) {
408 					char *path = strdup(lm);
409 					canonicalize_last_mounted(path);
410 					p->last_mounted = path;
411 				} else {
412 					p->fs_type = p->gp_type->
413 					    default_fs_type;
414 #ifdef DEFAULT_UFS2
415 					fs_is_default = true;
416 #endif
417 				}
418 			} else {
419 				p->fs_type = p->gp_type->default_fs_type;
420 #ifdef DEFAULT_UFS2
421 				fs_is_default = true;
422 #endif
423 			}
424 #ifdef DEFAULT_UFS2
425 			if (fs_is_default && p->fs_type == FS_BSDFFS)
426 				p->fs_sub_type = 2;
427 #endif
428 		}
429 
430 		parts->dp.free_space -= p->gp_size;
431 	}
432 
433 	/*
434 	 * Check if we have any (matching/auto-configured) wedges already
435 	 */
436 	dkw = NULL;
437 	dkwl.dkwl_buf = dkw;
438 	dkwl.dkwl_bufsize = 0;
439 	if (ioctl(fd, DIOCLWEDGES, &dkwl) == 0) {
440 		/* do not even try to deal with any races at this point */
441 		bufsize = dkwl.dkwl_nwedges * sizeof(*dkw);
442 		dkw = malloc(bufsize);
443 		dkwl.dkwl_buf = dkw;
444 		dkwl.dkwl_bufsize = bufsize;
445 		if (dkw != NULL && ioctl(fd, DIOCLWEDGES, &dkwl) == 0) {
446 			for (dk = 0; dk < dkwl.dkwl_ncopied; dk++)
447 				update_part_from_wedge_info(parts, &dkw[dk]);
448 		}
449 		free(dkw);
450 	}
451 
452 	close(fd);
453 
454 	return &parts->dp;
455 }
456 
457 static size_t
458 gpt_cyl_size(const struct disk_partitions *arg)
459 {
460 	return MEG / 512;
461 }
462 
463 static struct disk_partitions *
464 gpt_create_new(const char *disk, daddr_t start, daddr_t len,
465     bool is_boot_drive, struct disk_partitions *parent)
466 {
467 	struct gpt_disk_partitions *parts;
468 	struct disk_geom geo;
469 
470 	if (start != 0) {
471 		assert(0);
472 		return NULL;
473 	}
474 
475 	if (!get_disk_geom(disk, &geo))
476 		return NULL;
477 
478 	parts = calloc(1, sizeof(*parts));
479 	if (!parts)
480 		return NULL;
481 
482 	parts->dp.pscheme = &gpt_parts;
483 	parts->dp.disk = strdup(disk);
484 
485 	gpt_md_init(is_boot_drive, &parts->max_num_parts, &parts->prologue,
486 	    &parts->epilogue);
487 
488 	parts->dp.disk_start = start;
489 	parts->dp.disk_size = len;
490 	parts->dp.bytes_per_sector = geo.dg_secsize;
491 	parts->dp.free_space = len - start - parts->prologue - parts->epilogue;
492 	parts->has_gpt = false;
493 
494 	return &parts->dp;
495 }
496 
497 static bool
498 gpt_get_part_info(const struct disk_partitions *arg, part_id id,
499     struct disk_part_info *info)
500 {
501 	static const struct part_type_desc gpt_unknown_type =
502 		{ .generic_ptype = PT_undef,
503 		  .short_desc = "<unknown>" };
504 	const struct gpt_disk_partitions *parts =
505 	    (const struct gpt_disk_partitions*)arg;
506 	const struct gpt_part_entry *p = parts->partitions;
507 	part_id no;
508 
509 	for (no = 0; p != NULL && no < id; no++)
510 		p = p->gp_next;
511 
512 	if (no != id || p == NULL)
513 		return false;
514 
515 	memset(info, 0, sizeof(*info));
516 	info->start = p->gp_start;
517 	info->size = p->gp_size;
518 	if (p->gp_type)
519 		info->nat_type = &p->gp_type->gent;
520 	else
521 		info->nat_type = &gpt_unknown_type;
522 	info->last_mounted = p->last_mounted;
523 	info->fs_type = p->fs_type;
524 	info->fs_sub_type = p->fs_sub_type;
525 	info->fs_opt1 = p->fs_opt1;
526 	info->fs_opt2 = p->fs_opt2;
527 	info->fs_opt3 = p->fs_opt3;
528 	if (p->gp_flags & GPEF_TARGET)
529 		info->flags |= PTI_INSTALL_TARGET;
530 
531 	return true;
532 }
533 
534 static bool
535 gpt_get_part_attr_str(const struct disk_partitions *arg, part_id id,
536     char *str, size_t avail_space)
537 {
538 	const struct gpt_disk_partitions *parts =
539 	    (const struct gpt_disk_partitions*)arg;
540 	const struct gpt_part_entry *p = parts->partitions;
541 	part_id no;
542 	static const char *flags = NULL;
543 
544 	for (no = 0; p != NULL && no < id; no++)
545 		p = p->gp_next;
546 
547 	if (no != id || p == NULL)
548 		return false;
549 
550 	if (flags == NULL)
551 		flags = msg_string(MSG_gpt_flags);
552 
553 	if (avail_space < 2)
554 		return false;
555 
556 	if (p->gp_attr & GPT_ATTR_BOOT)
557 		*str++ = flags[0];
558 	*str = 0;
559 
560 	return true;
561 }
562 
563 /*
564  * Find insert position and check for duplicates.
565  * If all goes well, insert the new "entry" in the "list".
566  * If there are collisions, report "no free space".
567  * We keep all lists sorted by start sector number,
568  */
569 static bool
570 gpt_insert_part_into_list(struct gpt_disk_partitions *parts,
571     struct gpt_part_entry **list,
572     struct gpt_part_entry *entry, const char **err_msg)
573 {
574 	struct gpt_part_entry *p, *last;
575 
576 	/* find the first entry past the new one (if any) */
577 	for (last = NULL, p = *list; p != NULL; last = p, p = p->gp_next) {
578 		if (p->gp_start > entry->gp_start)
579 			break;
580 	}
581 
582 	/* check if last partition overlaps with new one */
583 	if (last) {
584 		if (last->gp_start + last->gp_size > entry->gp_start) {
585 			if (err_msg)
586 				*err_msg = msg_string(MSG_No_free_space);
587 			return false;
588 		}
589 	}
590 
591 	if (p == NULL) {
592 		entry->gp_next = NULL;
593 		if (last != NULL) {
594 			last->gp_next = entry;
595 		}
596 	} else {
597 		/* check if new entry overlaps with next */
598 		if (entry->gp_start + entry->gp_size > p->gp_start) {
599 			if (err_msg)
600 				*err_msg = msg_string(MSG_No_free_space);
601 			return false;
602 		}
603 
604 		entry->gp_next = p;
605 		if (last != NULL)
606 			last->gp_next = entry;
607 		else
608 			*list = entry;
609 	}
610 	if (*list == NULL)
611 		*list = entry;
612 
613 	return true;
614 }
615 
616 static bool
617 gpt_set_part_info(struct disk_partitions *arg, part_id id,
618     const struct disk_part_info *info, const char **err_msg)
619 {
620 	struct gpt_disk_partitions *parts =
621 	    (struct gpt_disk_partitions*)arg;
622 	struct gpt_part_entry *p = parts->partitions, *n;
623 	part_id no;
624 	daddr_t lendiff;
625 	bool was_target;
626 
627 	for (no = 0; p != NULL && no < id; no++)
628 		p = p->gp_next;
629 
630 	if (no != id || p == NULL)
631 		return false;
632 
633 	/* update target mark - we can only have one */
634 	was_target = (p->gp_flags & GPEF_TARGET) != 0;
635 	if (info->flags & PTI_INSTALL_TARGET)
636 		p->gp_flags |= GPEF_TARGET;
637 	else
638 		p->gp_flags &= ~GPEF_TARGET;
639 	if (was_target)
640 		for (n = parts->partitions; n != NULL; n = n->gp_next)
641 			if (n != p)
642 				n->gp_flags &= ~GPEF_TARGET;
643 
644 	if ((p->gp_flags & GPEF_ON_DISK)) {
645 		if (info->start != p->gp_start) {
646 			/* partition moved, we need to delete and re-add */
647 			n = calloc(1, sizeof(*n));
648 			if (n == NULL) {
649 				if (err_msg)
650 					*err_msg = err_outofmem;
651 				return false;
652 			}
653 			*n = *p;
654 			p->gp_flags &= ~GPEF_ON_DISK;
655 			if (!gpt_insert_part_into_list(parts, &parts->obsolete,
656 			    n, err_msg))
657 				return false;
658 		} else if (info->size != p->gp_size) {
659 			p->gp_flags |= GPEF_RESIZED;
660 		}
661 	}
662 
663 	p->gp_flags |= GPEF_MODIFIED;
664 
665 	lendiff = info->size - p->gp_size;
666 	parts->dp.free_space -= lendiff;
667 	return gpt_info_to_part(p, info, err_msg);
668 }
669 
670 static size_t
671 gpt_get_free_spaces_internal(const struct gpt_disk_partitions *parts,
672     struct disk_part_free_space *result, size_t max_num_result,
673     daddr_t min_space_size, daddr_t align, daddr_t start, daddr_t ignore)
674 {
675 	size_t cnt = 0;
676 	daddr_t s, e, from, size, end_of_disk;
677 	struct gpt_part_entry *p;
678 
679 	if (align > 1)
680 		start = max(roundup(start, align), align);
681 	if (start < 0 || start < (daddr_t)parts->prologue)
682 		start = parts->prologue;
683 	if (parts->dp.disk_start != 0 && parts->dp.disk_start > start)
684 		start = parts->dp.disk_start;
685 	if (min_space_size < 1)
686 		min_space_size = 1;
687 	end_of_disk = parts->dp.disk_start + parts->dp.disk_size
688 	    - parts->epilogue;
689 	from = start;
690 	while (from < end_of_disk && cnt < max_num_result) {
691 again:
692 		size = parts->dp.disk_start + parts->dp.disk_size - from;
693 		start = from;
694 		if (start + size > end_of_disk)
695 			size = end_of_disk - start;
696 		for (p = parts->partitions; p != NULL; p = p->gp_next) {
697 			s = p->gp_start;
698 			e = p->gp_size + s;
699 			if (s == ignore)
700 				continue;
701 			if (e < from)
702 				continue;
703 			if (s <= from && e > from) {
704 				if (e - 1 >= end_of_disk)
705 					return cnt;
706 				from = e + 1;
707 				if (align > 1) {
708 					from = max(roundup(from, align), align);
709 					if (from >= end_of_disk) {
710 						size = 0;
711 						break;
712 					}
713 				}
714 				goto again;
715 			}
716 			if (s > from && s - from < size) {
717 				size = s - from;
718 			}
719 		}
720 		if (size >= min_space_size) {
721 			result->start = start;
722 			result->size = size;
723 			result++;
724 			cnt++;
725 		}
726 		from += size + 1;
727 		if (align > 1)
728 			from = max(roundup(from, align), align);
729 	}
730 
731 	return cnt;
732 }
733 
734 static daddr_t
735 gpt_max_free_space_at(const struct disk_partitions *arg, daddr_t start)
736 {
737 	const struct gpt_disk_partitions *parts =
738 	    (const struct gpt_disk_partitions*)arg;
739 	struct disk_part_free_space space;
740 
741 	if (gpt_get_free_spaces_internal(parts, &space, 1, 1, 0,
742 	    start, start) == 1)
743 		return space.size;
744 
745 	return 0;
746 }
747 
748 static size_t
749 gpt_get_free_spaces(const struct disk_partitions *arg,
750     struct disk_part_free_space *result, size_t max_num_result,
751     daddr_t min_space_size, daddr_t align, daddr_t start,
752     daddr_t ignore)
753 {
754 	const struct gpt_disk_partitions *parts =
755 	    (const struct gpt_disk_partitions*)arg;
756 
757 	return gpt_get_free_spaces_internal(parts, result,
758 	    max_num_result, min_space_size, align, start, ignore);
759 }
760 
761 static void
762 gpt_match_ptype(const char *name, struct gpt_ptype_desc *t)
763 {
764 	size_t i;
765 
766 	for (i = 0; i < __arraycount(gpt_fs_types); i++) {
767 		if (strcmp(name, gpt_fs_types[i].name) == 0) {
768 			t->gent.generic_ptype = gpt_fs_types[i].ptype;
769 			t->fsflags = gpt_fs_types[i].fsflags;
770 			t->default_fs_type = gpt_fs_types[i].fstype;
771 
772 			/* recongnize special entries */
773 			if (gpt_native_root == NULL && i == 0)
774 				gpt_native_root = &t->gent;
775 
776 			return;
777 		}
778 	}
779 
780 	t->gent.generic_ptype = PT_unknown;
781 	t->fsflags = 0;
782 	t->default_fs_type = FS_BSDFFS;
783 }
784 
785 static void
786 gpt_internal_add_ptype(const char *uid, const char *name, const char *desc)
787 {
788 	if (gpt_ptype_cnt >= gpt_ptype_alloc) {
789 		gpt_ptype_alloc = gpt_ptype_alloc ? 2*gpt_ptype_alloc
790 		    : GPT_PTYPE_ALLOC;
791 		struct gpt_ptype_desc *nptypes = realloc(gpt_ptype_descs,
792 		    gpt_ptype_alloc*sizeof(*gpt_ptype_descs));
793 		if (nptypes == 0)
794 			errx(EXIT_FAILURE, "out of memory");
795 		gpt_ptype_descs = nptypes;
796 	}
797 
798 	strlcpy(gpt_ptype_descs[gpt_ptype_cnt].tid, uid,
799 	    sizeof(gpt_ptype_descs[gpt_ptype_cnt].tid));
800 	gpt_ptype_descs[gpt_ptype_cnt].gent.short_desc = strdup(name);
801 	gpt_ptype_descs[gpt_ptype_cnt].gent.description = strdup(desc);
802 	gpt_match_ptype(name, &gpt_ptype_descs[gpt_ptype_cnt]);
803 	gpt_ptype_cnt++;
804 }
805 
806 static void
807 gpt_init_ptypes(void)
808 {
809 	if (gpt_ptype_cnt == 0)
810 		gpt_uuid_query(gpt_internal_add_ptype);
811 }
812 
813 static void
814 gpt_cleanup(void)
815 {
816 	/* free all of gpt_ptype_descs */
817 	for (size_t i = 0; i < gpt_ptype_cnt; i++) {
818 		free(__UNCONST(gpt_ptype_descs[i].gent.short_desc));
819 		free(__UNCONST(gpt_ptype_descs[i].gent.description));
820 	}
821 	free(gpt_ptype_descs);
822 	gpt_ptype_descs = NULL;
823 	gpt_ptype_cnt = gpt_ptype_alloc = 0;
824 }
825 
826 static size_t
827 gpt_type_count(void)
828 {
829 	if (gpt_ptype_cnt == 0)
830 		gpt_init_ptypes();
831 
832 	return gpt_ptype_cnt;
833 }
834 
835 static const struct part_type_desc *
836 gpt_get_ptype(size_t ndx)
837 {
838 	if (gpt_ptype_cnt == 0)
839 		gpt_init_ptypes();
840 
841 	if (ndx >= gpt_ptype_cnt)
842 		return NULL;
843 
844 	return &gpt_ptype_descs[ndx].gent;
845 }
846 
847 static const struct part_type_desc *
848 gpt_get_generic_type(enum part_type gent)
849 {
850 	if (gpt_ptype_cnt == 0)
851 		gpt_init_ptypes();
852 
853 	if (gent == PT_root)
854 		return gpt_native_root;
855 	if (gent == PT_unknown)
856 		return NULL;
857 
858 	for (size_t i = 0; i < gpt_ptype_cnt; i++)
859 		if (gpt_ptype_descs[i].gent.generic_ptype == gent)
860 			return &gpt_ptype_descs[i].gent;
861 
862 	return NULL;
863 }
864 
865 static const struct gpt_ptype_desc *
866 gpt_find_native_type(const struct part_type_desc *gent)
867 {
868 	if (gpt_ptype_cnt == 0)
869 		gpt_init_ptypes();
870 
871 	if (gent == NULL)
872 		return NULL;
873 
874 	for (size_t i = 0; i < gpt_ptype_cnt; i++)
875 		if (gent == &gpt_ptype_descs[i].gent)
876 			return &gpt_ptype_descs[i];
877 
878 	gent = gpt_get_generic_type(gent->generic_ptype);
879 	if (gent == NULL)
880 		return NULL;
881 
882 	/* this can not recurse deeper than once, we would not have found a
883 	 * generic type a few lines above if it would. */
884 	return gpt_find_native_type(gent);
885 }
886 
887 static const struct gpt_ptype_desc *
888 gpt_find_guid_type(const char *uid)
889 {
890 	if (gpt_ptype_cnt == 0)
891 		gpt_init_ptypes();
892 
893 	if (uid == NULL || uid[0] == 0)
894 		return NULL;
895 
896 	for (size_t i = 0; i < gpt_ptype_cnt; i++)
897 		if (strcmp(gpt_ptype_descs[i].tid, uid) == 0)
898 			return &gpt_ptype_descs[i];
899 
900 	return NULL;
901 }
902 
903 static const struct part_type_desc *
904 gpt_find_type(const char *desc)
905 {
906 	if (gpt_ptype_cnt == 0)
907 		gpt_init_ptypes();
908 
909 	if (desc == NULL || desc[0] == 0)
910 		return NULL;
911 
912 	for (size_t i = 0; i < gpt_ptype_cnt; i++)
913 		if (strcmp(gpt_ptype_descs[i].gent.short_desc, desc) == 0)
914 			return &gpt_ptype_descs[i].gent;
915 
916 	return NULL;
917 }
918 
919 static const struct part_type_desc *
920 gpt_get_fs_part_type(enum part_type pt, unsigned fstype, unsigned fs_sub_type)
921 {
922 	size_t i;
923 
924 	/* Try with complete match (including part_type) first */
925 	for (i = 0; i < __arraycount(gpt_fs_types); i++)
926 		if (fstype == gpt_fs_types[i].fstype &&
927 		    pt == gpt_fs_types[i].ptype)
928 			return gpt_find_type(gpt_fs_types[i].name);
929 
930 	/* If that did not work, ignore part_type */
931 	for (i = 0; i < __arraycount(gpt_fs_types); i++)
932 		if (fstype == gpt_fs_types[i].fstype)
933 			return gpt_find_type(gpt_fs_types[i].name);
934 
935 	return NULL;
936 }
937 
938 static bool
939 gpt_get_default_fstype(const struct part_type_desc *nat_type,
940     unsigned *fstype, unsigned *fs_sub_type)
941 {
942 	const struct gpt_ptype_desc *gtype;
943 
944 	gtype = gpt_find_native_type(nat_type);
945 	if (gtype == NULL)
946 		return false;
947 
948 	*fstype = gtype->default_fs_type;
949 #ifdef DEFAULT_UFS2
950 	if (gtype->default_fs_type == FS_BSDFFS)
951 		*fs_sub_type = 2;
952 	else
953 #endif
954 		*fs_sub_type = 0;
955 	return true;
956 }
957 
958 static const struct part_type_desc *
959 gpt_get_uuid_part_type(const uuid_t *id)
960 {
961 	char str[GUID_STR_LEN], desc[GUID_STR_LEN + MENUSTRSIZE];
962 	const struct gpt_ptype_desc *t;
963 	char *guid = NULL;
964 	uint32_t err;
965 
966 	uuid_to_string(id, &guid, &err);
967 	strlcpy(str, err == uuid_s_ok ? guid : "-", sizeof str);
968 	free(guid);
969 
970 	t = gpt_find_guid_type(str);
971 	if (t == NULL) {
972 		snprintf(desc, sizeof desc, "%s (%s)",
973 		    msg_string(MSG_custom_type), str);
974 		gpt_internal_add_ptype(str, str, desc);
975 		t = gpt_find_guid_type(str);
976 		assert(t != NULL);
977 	}
978 	return &t->gent;
979 }
980 
981 static const struct part_type_desc *
982 gpt_create_custom_part_type(const char *custom, const char **err_msg)
983 {
984 	uuid_t id;
985 	uint32_t err;
986 
987 	uuid_from_string(custom, &id, &err);
988 	if (err_msg != NULL &&
989 	   (err == uuid_s_invalid_string_uuid || err == uuid_s_bad_version)) {
990 		*err_msg = MSG_invalid_guid;
991 		return NULL;
992 	}
993 	if (err != uuid_s_ok)
994 		return NULL;
995 
996 	return gpt_get_uuid_part_type(&id);
997 }
998 
999 static const struct part_type_desc *
1000 gpt_create_unknown_part_type(void)
1001 {
1002 	uuid_t id;
1003 	uint32_t err;
1004 
1005 	uuid_create(&id, &err);
1006 	if (err != uuid_s_ok)
1007 		return NULL;
1008 
1009 	return gpt_get_uuid_part_type(&id);
1010 }
1011 
1012 static daddr_t
1013 gpt_get_part_alignment(const struct disk_partitions *parts)
1014 {
1015 
1016 	assert(parts->disk_size > 0);
1017 	if (parts->disk_size < 0)
1018 		return 1;
1019 
1020 	/* Use 1MB offset/alignemnt for large (>128GB) disks */
1021 	if (parts->disk_size > HUGE_DISK_SIZE)
1022 		return 2048;
1023 	else if (parts->disk_size > TINY_DISK_SIZE)
1024 		return 64;
1025 	else
1026 		return 4;
1027 }
1028 
1029 static bool
1030 gpt_can_add_partition(const struct disk_partitions *arg)
1031 {
1032 	const struct gpt_disk_partitions *parts =
1033 	    (const struct gpt_disk_partitions*)arg;
1034 	struct disk_part_free_space space;
1035 	daddr_t align;
1036 
1037 	if (parts->dp.num_part >= parts->max_num_parts)
1038 		return false;
1039 
1040 	align = gpt_get_part_alignment(arg);
1041 	if (parts->dp.free_space <= align)
1042 		return false;
1043 
1044 	if (gpt_get_free_spaces_internal(parts, &space, 1, align, align,
1045 	    0, -1) < 1)
1046 		return false;
1047 
1048 	return true;
1049 }
1050 
1051 static bool
1052 gpt_info_to_part(struct gpt_part_entry *p, const struct disk_part_info *info,
1053     const char **err_msg)
1054 {
1055 	p->gp_type = gpt_find_native_type(info->nat_type);
1056 	p->gp_start = info->start;
1057 	p->gp_size = info->size;
1058 	if (info->last_mounted != NULL && info->last_mounted !=
1059 	    p->last_mounted) {
1060 		free(__UNCONST(p->last_mounted));
1061 		p->last_mounted = strdup(info->last_mounted);
1062 	}
1063 	p->fs_type = info->fs_type;
1064 	p->fs_sub_type = info->fs_sub_type;
1065 	p->fs_opt1 = info->fs_opt1;
1066 	p->fs_opt2 = info->fs_opt2;
1067 	p->fs_opt3 = info->fs_opt3;
1068 
1069 	return true;
1070 }
1071 
1072 static part_id
1073 gpt_add_part(struct disk_partitions *arg,
1074     const struct disk_part_info *info, const char **err_msg)
1075 {
1076 	struct gpt_disk_partitions *parts =
1077 	    (struct gpt_disk_partitions*)arg;
1078 	struct disk_part_free_space space;
1079 	struct disk_part_info data = *info;
1080 	struct gpt_part_entry *p;
1081 	bool ok;
1082 
1083 	if (err_msg != NULL)
1084 		*err_msg = NULL;
1085 
1086 	if (gpt_get_free_spaces_internal(parts, &space, 1, 1, 1,
1087 	    info->start, -1) < 1) {
1088 		if (err_msg)
1089 			*err_msg = msg_string(MSG_No_free_space);
1090 		return NO_PART;
1091 	}
1092 	if (parts->dp.num_part >= parts->max_num_parts) {
1093 		if (err_msg)
1094 			*err_msg = msg_string(MSG_err_too_many_partitions);
1095 		return NO_PART;
1096 	}
1097 
1098 	if (data.size > space.size)
1099 		data.size = space.size;
1100 
1101 	p = calloc(1, sizeof(*p));
1102 	if (p == NULL) {
1103 		if (err_msg != NULL)
1104 			*err_msg = INTERNAL_ERROR;
1105 		return NO_PART;
1106 	}
1107 	if (!gpt_info_to_part(p, &data, err_msg)) {
1108 		free(p);
1109 		return NO_PART;
1110 	}
1111 	p->gp_flags |= GPEF_MODIFIED;
1112 	ok = gpt_insert_part_into_list(parts, &parts->partitions, p, err_msg);
1113 	if (ok) {
1114 		parts->dp.num_part++;
1115 		parts->dp.free_space -= p->gp_size;
1116 		return parts->dp.num_part-1;
1117 	} else {
1118 		free(p);
1119 		return NO_PART;
1120 	}
1121 }
1122 
1123 static bool
1124 gpt_delete_partition(struct disk_partitions *arg, part_id id,
1125     const char **err_msg)
1126 {
1127 	struct gpt_disk_partitions *parts = (struct gpt_disk_partitions*)arg;
1128 	struct gpt_part_entry *p, *last = NULL;
1129 	part_id i;
1130 	bool res;
1131 
1132 	if (parts->dp.num_part == 0)
1133 		return false;
1134 
1135 	for (i = 0, p = parts->partitions;
1136 	    i != id && i < parts->dp.num_part && p != NULL;
1137 	    i++, p = p->gp_next)
1138 		last = p;
1139 
1140 	if (p == NULL) {
1141 		if (err_msg)
1142 			*err_msg = INTERNAL_ERROR;
1143 		return false;
1144 	}
1145 
1146 	if (last == NULL)
1147 		parts->partitions = p->gp_next;
1148 	else
1149 		last->gp_next = p->gp_next;
1150 
1151 	res = true;
1152 	if (p->gp_flags & GPEF_ON_DISK) {
1153 		if (!gpt_insert_part_into_list(parts, &parts->obsolete,
1154 		    p, err_msg))
1155 			res = false;
1156 	} else {
1157 		free(p);
1158 	}
1159 
1160 	if (res) {
1161 		parts->dp.num_part--;
1162 		parts->dp.free_space += p->gp_size;
1163 	}
1164 
1165 	return res;
1166 }
1167 
1168 static bool
1169 gpt_delete_all_partitions(struct disk_partitions *arg)
1170 {
1171 	struct gpt_disk_partitions *parts = (struct gpt_disk_partitions*)arg;
1172 
1173 	while (parts->dp.num_part > 0) {
1174 		if (!gpt_delete_partition(&parts->dp, 0, NULL))
1175 			return false;
1176 	}
1177 
1178 	return true;
1179 }
1180 
1181 static bool
1182 gpt_read_part(const char *disk, daddr_t start, struct gpt_part_entry *p)
1183 {
1184 	char *textbuf, *t, *tt;
1185 	static const char expected_hdr[] = "Details for index ";
1186 
1187 	/* run gpt show for this partition */
1188 	if (collect(T_OUTPUT, &textbuf,
1189 	    "gpt -r show -b %" PRIu64 " %s 2>/dev/null", start, disk) < 1)
1190 		return false;
1191 
1192 	/*
1193 	 * gpt show should respond with single partition details, but will
1194 	 * fall back to "show -a" output if something is wrong
1195 	 */
1196 	t = strtok(textbuf, "\n"); /* first line is special */
1197 	if (strncmp(t, expected_hdr, sizeof(expected_hdr)-1) != 0) {
1198 		free(textbuf);
1199 		return false;
1200 	}
1201 
1202 	/* parse output into "old" */
1203 	while ((t = strtok(NULL, "\n")) != NULL) {
1204 		tt = strsep(&t, " \t");
1205 		if (strlen(tt) == 0)
1206 			continue;
1207 		gpt_add_info(p, tt, t, true);
1208 	}
1209 	free(textbuf);
1210 
1211 	return true;
1212 }
1213 
1214 static bool
1215 gpt_apply_attr(const char *disk, const char *cmd, off_t start, uint todo)
1216 {
1217 	size_t i;
1218 	char attr_str[STRSIZE];
1219 
1220 	if (todo == 0)
1221 		return true;
1222 
1223 	strcpy(attr_str, "-a ");
1224 	for (i = 0; todo != 0; i++) {
1225 		if (!(gpt_avail_attrs[i].flag & todo))
1226 			continue;
1227 		todo &= ~gpt_avail_attrs[i].flag;
1228 		if (attr_str[0])
1229 			strlcat(attr_str, ",",
1230 			    sizeof(attr_str));
1231 		strlcat(attr_str,
1232 		    gpt_avail_attrs[i].name,
1233 		    sizeof(attr_str));
1234 	}
1235 	if (run_program(RUN_SILENT,
1236 	    "gpt %s %s -b %" PRIu64 " %s", cmd, attr_str, start, disk) != 0)
1237 		return false;
1238 	return true;
1239 }
1240 
1241 /*
1242  * Modify an existing on-disk partition.
1243  * Start and size can not be changed here, caller needs to deal
1244  * with that kind of changes upfront.
1245  */
1246 static bool
1247 gpt_modify_part(const char *disk, struct gpt_part_entry *p)
1248 {
1249 	struct gpt_part_entry old;
1250 	uint todo_set, todo_unset;
1251 
1252 	/*
1253 	 * Query current on-disk state
1254 	 */
1255 	memset(&old, 0, sizeof old);
1256 	if (!gpt_read_part(disk, p->gp_start, &old))
1257 		return false;
1258 
1259 	/* Reject unsupported changes */
1260 	if (old.gp_start != p->gp_start || old.gp_size != p->gp_size)
1261 		return false;
1262 
1263 	/*
1264 	 * GUID should never change, but the internal copy
1265 	 * may not yet know it.
1266 	 */
1267 	strcpy(p->gp_id, old.gp_id);
1268 
1269 	/* Check type */
1270 	if (p->gp_type != old.gp_type) {
1271 		if (run_program(RUN_SILENT,
1272 		    "gpt type -b %" PRIu64 " -T %s %s",
1273 		    p->gp_start, p->gp_type->tid, disk) != 0)
1274 			return false;
1275 	}
1276 
1277 	/* Check label */
1278 	if (strcmp(p->gp_label, old.gp_label) != 0) {
1279 		if (run_program(RUN_SILENT,
1280 		    "gpt label -b %" PRIu64 " -l \'%s\' %s",
1281 		    p->gp_start, p->gp_label, disk) != 0)
1282 			return false;
1283 	}
1284 
1285 	/* Check attributes */
1286 	if (p->gp_attr != old.gp_attr) {
1287 		if (p->gp_attr == 0) {
1288 			if (run_program(RUN_SILENT,
1289 			    "gpt set -N -b %" PRIu64 " %s",
1290 			    p->gp_start, disk) != 0)
1291 				return false;
1292 		} else {
1293 			todo_set = (p->gp_attr ^ old.gp_attr) & p->gp_attr;
1294 			todo_unset = (p->gp_attr ^ old.gp_attr) & old.gp_attr;
1295 			if (!gpt_apply_attr(disk, "unset", p->gp_start,
1296 			    todo_unset))
1297 				return false;
1298 			if (!gpt_apply_attr(disk, "set", p->gp_start,
1299 			    todo_set))
1300 				return false;
1301 		}
1302 	}
1303 
1304 	return true;
1305 }
1306 
1307 /*
1308  * verbatim copy from sys/dev/dkwedge/dkwedge_bsdlabel.c:
1309  *  map FS_* to wedge strings
1310  */
1311 static const char *
1312 bsdlabel_fstype_to_str(uint8_t fstype)
1313 {
1314 	const char *str;
1315 
1316 	/*
1317 	 * For each type known to FSTYPE_DEFN (from <sys/disklabel.h>),
1318 	 * a suitable case branch will convert the type number to a string.
1319 	 */
1320 	switch (fstype) {
1321 #define FSTYPE_TO_STR_CASE(tag, number, name, fsck, mount) \
1322 	case __CONCAT(FS_,tag):	str = __CONCAT(DKW_PTYPE_,tag);			break;
1323 	FSTYPE_DEFN(FSTYPE_TO_STR_CASE)
1324 #undef FSTYPE_TO_STR_CASE
1325 	default:		str = NULL;			break;
1326 	}
1327 
1328 	return (str);
1329 }
1330 
1331 /*
1332  * diskfd is an open file descriptor for a disk we had trouble with
1333  * creating some new wedges.
1334  * Go through all wedges actually on that disk, check if we have a
1335  * record for them and remove all others.
1336  * This should sync our internal model of partitions with the real state.
1337  */
1338 static void
1339 gpt_sanitize(int diskfd, const struct gpt_disk_partitions *parts,
1340     struct gpt_part_entry *ignore)
1341 {
1342 	struct dkwedge_info *dkw, delw;
1343 	struct dkwedge_list dkwl;
1344 	size_t bufsize;
1345 	u_int i;
1346 
1347 	dkw = NULL;
1348 	dkwl.dkwl_buf = dkw;
1349 	dkwl.dkwl_bufsize = 0;
1350 
1351 	/* get a list of all wedges */
1352 	for (;;) {
1353 		if (ioctl(diskfd, DIOCLWEDGES, &dkwl) == -1)
1354 			return;
1355 		if (dkwl.dkwl_nwedges == dkwl.dkwl_ncopied)
1356 			break;
1357 		bufsize = dkwl.dkwl_nwedges * sizeof(*dkw);
1358 		if (dkwl.dkwl_bufsize < bufsize) {
1359 			dkw = realloc(dkwl.dkwl_buf, bufsize);
1360 			if (dkw == NULL)
1361 				return;
1362 			dkwl.dkwl_buf = dkw;
1363 			dkwl.dkwl_bufsize = bufsize;
1364 		}
1365 	}
1366 
1367 	/* try to remove all the ones we do not know about */
1368 	for (i = 0; i < dkwl.dkwl_nwedges; i++) {
1369 		bool found = false;
1370 		const char *devname = dkw[i].dkw_devname;
1371 
1372 		for (struct gpt_part_entry *pe = parts->partitions;
1373 		    pe != NULL; pe = pe->gp_next) {
1374 			if (pe == ignore)
1375 				continue;
1376 			if ((pe->gp_flags & GPEF_WEDGE) &&
1377 			    strcmp(pe->gp_dev_name, devname) == 0) {
1378 				found = true;
1379 				break;
1380 			}
1381 		}
1382 		if (found)
1383 			continue;
1384 		memset(&delw, 0, sizeof(delw));
1385 		strlcpy(delw.dkw_devname, devname, sizeof(delw.dkw_devname));
1386 		(void)ioctl(diskfd, DIOCDWEDGE, &delw);
1387 	}
1388 
1389 	/* cleanup */
1390 	free(dkw);
1391 }
1392 
1393 static bool
1394 gpt_add_wedge(const char *disk, struct gpt_part_entry *p,
1395     const struct gpt_disk_partitions *parts)
1396 {
1397 	struct dkwedge_info dkw;
1398 	const char *tname;
1399 	char diskpath[MAXPATHLEN];
1400 	int fd;
1401 
1402 	memset(&dkw, 0, sizeof(dkw));
1403 	tname = bsdlabel_fstype_to_str(p->fs_type);
1404 	if (tname)
1405 		strlcpy(dkw.dkw_ptype, tname, sizeof(dkw.dkw_ptype));
1406 
1407 	strlcpy((char*)&dkw.dkw_wname, p->gp_id, sizeof(dkw.dkw_wname));
1408 	dkw.dkw_offset = p->gp_start;
1409 	dkw.dkw_size = p->gp_size;
1410 	if (dkw.dkw_wname[0] == 0) {
1411 		if (p->gp_label[0] != 0)
1412 				strlcpy((char*)&dkw.dkw_wname,
1413 				    p->gp_label, sizeof(dkw.dkw_wname));
1414 	}
1415 	if (dkw.dkw_wname[0] == 0) {
1416 		snprintf((char*)dkw.dkw_wname, sizeof dkw.dkw_wname,
1417 		    "%s_%" PRIi64 "@%" PRIi64, disk, p->gp_size, p->gp_start);
1418 	}
1419 
1420 	fd = opendisk(disk, O_RDWR, diskpath, sizeof(diskpath), 0);
1421 	if (fd < 0)
1422 		return false;
1423 	if (ioctl(fd, DIOCAWEDGE, &dkw) == -1) {
1424 		if (errno == EINVAL) {
1425 			/* sanitize existing wedges and try again */
1426 			gpt_sanitize(fd, parts, p);
1427 			if (ioctl(fd, DIOCAWEDGE, &dkw) == 0)
1428 				goto ok;
1429 		}
1430 		close(fd);
1431 		return false;
1432 	}
1433 ok:
1434 	close(fd);
1435 
1436 	strlcpy(p->gp_dev_name, dkw.dkw_devname, sizeof(p->gp_dev_name));
1437 	p->gp_flags |= GPEF_WEDGE;
1438 	return true;
1439 }
1440 
1441 static void
1442 escape_spaces(char *dest, const char *src)
1443 {
1444 	unsigned char c;
1445 
1446 	while (*src) {
1447 		c = *src++;
1448 		if (isspace(c) || c == '\\')
1449 			*dest++ = '\\';
1450 		*dest++ = c;
1451 	}
1452 	*dest = 0;
1453 }
1454 
1455 static bool
1456 gpt_get_part_device(const struct disk_partitions *arg,
1457     part_id id, char *devname, size_t max_devname_len, int *part,
1458     enum dev_name_usage usage, bool with_path, bool life)
1459 {
1460 	const struct gpt_disk_partitions *parts =
1461 	    (const struct gpt_disk_partitions*)arg;
1462 	struct  gpt_part_entry *p = parts->partitions;
1463 	char tmpname[GPT_LABEL_LEN*2];
1464 	part_id no;
1465 
1466 
1467 	for (no = 0; p != NULL && no < id; no++)
1468 		p = p->gp_next;
1469 
1470 	if (no != id || p == NULL)
1471 		return false;
1472 
1473 	if (part)
1474 		*part = -1;
1475 
1476 	if (usage == logical_name && p->gp_label[0] == 0 && p->gp_id[0] == 0)
1477 		usage = plain_name;
1478 	if (usage == plain_name || usage == raw_dev_name)
1479 		life = true;
1480 	if (!(p->gp_flags & GPEF_WEDGE) && life &&
1481 	    !gpt_add_wedge(arg->disk, p, parts))
1482 		return false;
1483 
1484 	switch (usage) {
1485 	case logical_name:
1486 		if (p->gp_label[0] != 0) {
1487 			escape_spaces(tmpname, p->gp_label);
1488 			snprintf(devname, max_devname_len,
1489 			    "NAME=%s", tmpname);
1490 		} else {
1491 			snprintf(devname, max_devname_len,
1492 			    "NAME=%s", p->gp_id);
1493 		}
1494 		break;
1495 	case plain_name:
1496 		assert(p->gp_flags & GPEF_WEDGE);
1497 		if (with_path)
1498 			snprintf(devname, max_devname_len, _PATH_DEV "%s",
1499 			    p->gp_dev_name);
1500 		else
1501 			strlcpy(devname, p->gp_dev_name, max_devname_len);
1502 		break;
1503 	case raw_dev_name:
1504 		assert(p->gp_flags & GPEF_WEDGE);
1505 		if (with_path)
1506 			snprintf(devname, max_devname_len, _PATH_DEV "r%s",
1507 			    p->gp_dev_name);
1508 		else
1509 			snprintf(devname, max_devname_len, "r%s",
1510 			    p->gp_dev_name);
1511 		break;
1512 	default:
1513 		return false;
1514 	}
1515 
1516 	return true;
1517 }
1518 
1519 static bool
1520 gpt_write_to_disk(struct disk_partitions *arg)
1521 {
1522 	struct gpt_disk_partitions *parts = (struct gpt_disk_partitions*)arg;
1523 	struct gpt_part_entry *p, *n;
1524 	char label_arg[sizeof(p->gp_label) + 10];
1525 	char diskpath[MAXPATHLEN];
1526 	int fd, bits = 0;
1527 	bool root_is_new = false, efi_is_new = false;
1528 	part_id root_id = NO_PART, efi_id = NO_PART, pno;
1529 
1530 	/*
1531 	 * Remove all wedges on this disk - they may become invalid and we
1532 	 * have no easy way to associate them with the partitioning data.
1533 	 * Instead we will explicitly request creation of wedges on demand
1534 	 * later.
1535 	 */
1536 	fd = opendisk(arg->disk, O_RDWR, diskpath, sizeof(diskpath), 0);
1537 	if (fd < 0)
1538 		return false;
1539 	if (ioctl(fd, DIOCRMWEDGES, &bits) == -1)
1540 		return false;
1541 	close(fd);
1542 
1543 	/*
1544 	 * Collect first root and efi partition (if available), clear
1545 	 * "have wedge" flags.
1546 	 */
1547 	for (pno = 0, p = parts->partitions; p != NULL; p = p->gp_next, pno++) {
1548 		p->gp_flags &= ~GPEF_WEDGE;
1549 		if (root_id == NO_PART && p->gp_type != NULL) {
1550 			if (p->gp_type->gent.generic_ptype == PT_root &&
1551 			    (p->gp_flags & GPEF_TARGET)) {
1552 				root_id = pno;
1553 				root_is_new = !(p->gp_flags & GPEF_ON_DISK);
1554 			} else if (efi_id == NO_PART &&
1555 			    p->gp_type->gent.generic_ptype == PT_EFI_SYSTEM) {
1556 				efi_id = pno;
1557 				efi_is_new = !(p->gp_flags & GPEF_ON_DISK);
1558 			}
1559 		}
1560 	}
1561 
1562 	/*
1563 	 * If no GPT on disk yet, create it.
1564 	 */
1565 	if (!parts->has_gpt) {
1566 		char limit[30];
1567 
1568 		if (parts->max_num_parts > 0)
1569 			sprintf(limit, "-p %zu", parts->max_num_parts);
1570 		else
1571 			limit[0] = 0;
1572 		if (run_program(RUN_SILENT, "gpt create %s %s",
1573 		    limit, parts->dp.disk))
1574 			return false;
1575 		parts->has_gpt = true;
1576 	}
1577 
1578 	/*
1579 	 * Delete all old partitions
1580 	 */
1581 	for (p = parts->obsolete; p != NULL; p = n) {
1582 		run_program(RUN_SILENT, "gpt -n remove -b %" PRIu64 " %s",
1583 		    p->gp_start, arg->disk);
1584 		n = p->gp_next;
1585 		free(p);
1586 	}
1587 	parts->obsolete = NULL;
1588 
1589 	/*
1590 	 * Modify existing but changed partitions
1591 	 */
1592 	for (p = parts->partitions; p != NULL; p = p->gp_next) {
1593 		if (!(p->gp_flags & GPEF_ON_DISK))
1594 			continue;
1595 
1596 		if (p->gp_flags & GPEF_RESIZED) {
1597 			run_program(RUN_SILENT,
1598 			    "gpt -n resize -b %" PRIu64 " -s %" PRIu64 "s %s",
1599 			    p->gp_start, p->gp_size, arg->disk);
1600 			p->gp_flags &= ~GPEF_RESIZED;
1601 		}
1602 
1603 		if (!(p->gp_flags & GPEF_MODIFIED))
1604 			continue;
1605 
1606 		if (!gpt_modify_part(parts->dp.disk, p))
1607 			return false;
1608 	}
1609 
1610 	/*
1611 	 * Add new partitions
1612 	 */
1613 	for (p = parts->partitions; p != NULL; p = p->gp_next) {
1614 		if (p->gp_flags & GPEF_ON_DISK)
1615 			continue;
1616 		if (!(p->gp_flags & GPEF_MODIFIED))
1617 			continue;
1618 
1619 		if (p->gp_label[0] == 0)
1620 			label_arg[0] = 0;
1621 		else
1622 			sprintf(label_arg, "-l \'%s\'", p->gp_label);
1623 
1624 		if (p->gp_type != NULL)
1625 			run_program(RUN_SILENT,
1626 			    "gpt -n add -b %" PRIu64 " -s %" PRIu64
1627 			    "s -t %s %s %s",
1628 			    p->gp_start, p->gp_size, p->gp_type->tid,
1629 			    label_arg, arg->disk);
1630 		else
1631 			run_program(RUN_SILENT,
1632 			    "gpt -n add -b %" PRIu64 " -s %" PRIu64
1633 			    "s %s %s",
1634 			    p->gp_start, p->gp_size, label_arg, arg->disk);
1635 		gpt_apply_attr(arg->disk, "set", p->gp_start, p->gp_attr);
1636 		gpt_read_part(arg->disk, p->gp_start, p);
1637 		p->gp_flags |= GPEF_ON_DISK;
1638 	}
1639 
1640 	/*
1641 	 * Additional MD bootloader magic...
1642 	 */
1643 	if (!md_gpt_post_write(&parts->dp, root_id, root_is_new, efi_id,
1644 	    efi_is_new))
1645 		return false;
1646 
1647 	return true;
1648 }
1649 
1650 static part_id
1651 gpt_find_by_name(struct disk_partitions *arg, const char *name)
1652 {
1653 	struct gpt_disk_partitions *parts = (struct gpt_disk_partitions*)arg;
1654 	struct gpt_part_entry *p;
1655 	part_id pno;
1656 
1657 	for (pno = 0, p = parts->partitions; p != NULL;
1658 	    p = p->gp_next, pno++) {
1659 		if (strcmp(p->gp_label, name) == 0)
1660 			return pno;
1661 		if (strcmp(p->gp_id, name) == 0)
1662 			return pno;
1663 	}
1664 
1665 	return NO_PART;
1666 }
1667 
1668 bool
1669 gpt_parts_check(void)
1670 {
1671 
1672 	check_available_binaries();
1673 
1674 	return have_gpt && have_dk;
1675 }
1676 
1677 static void
1678 gpt_free(struct disk_partitions *arg)
1679 {
1680 	struct gpt_disk_partitions *parts = (struct gpt_disk_partitions*)arg;
1681 	struct gpt_part_entry *p, *n;
1682 
1683 	assert(parts != NULL);
1684 	for (p = parts->partitions; p != NULL; p = n) {
1685 		if (p->gp_flags & GPEF_WEDGE)
1686 			register_post_umount_delwedge(parts->dp.disk,
1687 			    p->gp_dev_name);
1688 		free(__UNCONST(p->last_mounted));
1689 		n = p->gp_next;
1690 		free(p);
1691 	}
1692 	free(__UNCONST(parts->dp.disk));
1693 	free(parts);
1694 }
1695 
1696 static void
1697 gpt_destroy_part_scheme(struct disk_partitions *arg)
1698 {
1699 
1700 	run_program(RUN_SILENT, "gpt destroy %s", arg->disk);
1701 	gpt_free(arg);
1702 }
1703 
1704 static bool
1705 gpt_custom_attribute_writable(const struct disk_partitions *arg,
1706     part_id ptn, size_t attr_no)
1707 {
1708 	const struct gpt_disk_partitions *parts =
1709 	    (const struct gpt_disk_partitions*)arg;
1710 	size_t i;
1711 	struct gpt_part_entry *p;
1712 
1713 	if (attr_no >= arg->pscheme->custom_attribute_count)
1714 		return false;
1715 
1716 	const msg label = arg->pscheme->custom_attributes[attr_no].label;
1717 
1718 	/* we can not edit the uuid attribute */
1719 	if (label == MSG_ptn_uuid)
1720 		return false;
1721 
1722 	/* the label is always editable */
1723 	if (label == MSG_ptn_label)
1724 		return true;
1725 
1726 	/* the GPT type is read only */
1727 	if (label == MSG_ptn_gpt_type)
1728 		return false;
1729 
1730 	/* BOOTME makes no sense on swap partitions */
1731 	for (i = 0, p = parts->partitions; p != NULL; i++, p = p->gp_next)
1732 		if (i == ptn)
1733 			break;
1734 
1735 	if (p == NULL)
1736 		return false;
1737 
1738 	if (p->fs_type == FS_SWAP ||
1739 	    (p->gp_type != NULL && p->gp_type->gent.generic_ptype == PT_swap))
1740 		return false;
1741 
1742 	return true;
1743 }
1744 
1745 static const char *
1746 gpt_get_label_str(const struct disk_partitions *arg, part_id ptn)
1747 {
1748 	const struct gpt_disk_partitions *parts =
1749 	    (const struct gpt_disk_partitions*)arg;
1750 	size_t i;
1751 	struct gpt_part_entry *p;
1752 
1753 	for (i = 0, p = parts->partitions; p != NULL; i++, p = p->gp_next)
1754 		if (i == ptn)
1755 			break;
1756 
1757 	if (p == NULL)
1758 		return NULL;
1759 
1760 	if (p->gp_label[0] != 0)
1761 		return p->gp_label;
1762 	return p->gp_id;
1763 }
1764 
1765 static bool
1766 gpt_format_custom_attribute(const struct disk_partitions *arg,
1767     part_id ptn, size_t attr_no, const struct disk_part_info *info,
1768     char *out, size_t out_space)
1769 {
1770 	const struct gpt_disk_partitions *parts =
1771 	    (const struct gpt_disk_partitions*)arg;
1772 	size_t i;
1773 	struct gpt_part_entry *p, data;
1774 
1775 	for (i = 0, p = parts->partitions; p != NULL; i++, p = p->gp_next)
1776 		if (i == ptn)
1777 			break;
1778 
1779 	if (p == NULL)
1780 		return false;
1781 
1782 	if (attr_no >= parts->dp.pscheme->custom_attribute_count)
1783 		return false;
1784 
1785 	const msg label = parts->dp.pscheme->custom_attributes[attr_no].label;
1786 
1787 	if (info != NULL) {
1788 		data = *p;
1789 		gpt_info_to_part(&data, info, NULL);
1790 		p = &data;
1791 	}
1792 
1793 	if (label == MSG_ptn_label)
1794 		strlcpy(out, p->gp_label, out_space);
1795 	else if (label == MSG_ptn_uuid)
1796 		strlcpy(out, p->gp_id, out_space);
1797 	else if (label == MSG_ptn_gpt_type) {
1798 		if (p->gp_type != NULL)
1799 			strlcpy(out, p->gp_type->gent.description, out_space);
1800 		else if (out_space > 1)
1801 			out[0] = 0;
1802 	} else if (label == MSG_ptn_boot)
1803 		strlcpy(out, msg_string(p->gp_attr & GPT_ATTR_BOOT ?
1804 		    MSG_Yes : MSG_No), out_space);
1805 	else
1806 		return false;
1807 
1808 	return true;
1809 }
1810 
1811 static bool
1812 gpt_custom_attribute_toggle(struct disk_partitions *arg,
1813     part_id ptn, size_t attr_no)
1814 {
1815 	const struct gpt_disk_partitions *parts =
1816 	    (const struct gpt_disk_partitions*)arg;
1817 	size_t i;
1818 	struct gpt_part_entry *p;
1819 
1820 	for (i = 0, p = parts->partitions; p != NULL; i++, p = p->gp_next)
1821 		if (i == ptn)
1822 			break;
1823 
1824 	if (p == NULL)
1825 		return false;
1826 
1827 	if (attr_no >= parts->dp.pscheme->custom_attribute_count)
1828 		return false;
1829 
1830 	const msg label = parts->dp.pscheme->custom_attributes[attr_no].label;
1831 	if (label != MSG_ptn_boot)
1832 		return false;
1833 
1834 	if (p->gp_attr & GPT_ATTR_BOOT) {
1835 		p->gp_attr &= ~GPT_ATTR_BOOT;
1836 	} else {
1837 		for (i = 0, p = parts->partitions; p != NULL;
1838 		    i++, p = p->gp_next)
1839 			if (i == ptn)
1840 				p->gp_attr |= GPT_ATTR_BOOT;
1841 			else
1842 				p->gp_attr &= ~GPT_ATTR_BOOT;
1843 	}
1844 	return true;
1845 }
1846 
1847 static bool
1848 gpt_custom_attribute_set_str(struct disk_partitions *arg,
1849     part_id ptn, size_t attr_no, const char *new_val)
1850 {
1851 	const struct gpt_disk_partitions *parts =
1852 	    (const struct gpt_disk_partitions*)arg;
1853 	size_t i;
1854 	struct gpt_part_entry *p;
1855 
1856 	for (i = 0, p = parts->partitions; p != NULL; i++, p = p->gp_next)
1857 		if (i == ptn)
1858 			break;
1859 
1860 	if (p == NULL)
1861 		return false;
1862 
1863 	if (attr_no >= parts->dp.pscheme->custom_attribute_count)
1864 		return false;
1865 
1866 	const msg label = parts->dp.pscheme->custom_attributes[attr_no].label;
1867 
1868 	if (label != MSG_ptn_label)
1869 		return false;
1870 
1871 	strlcpy(p->gp_label, new_val, sizeof(p->gp_label));
1872 	return true;
1873 }
1874 
1875 static bool
1876 gpt_have_boot_support(const char *disk)
1877 {
1878 #ifdef	HAVE_GPT_BOOT
1879 	return true;
1880 #else
1881 	return false;
1882 #endif
1883 }
1884 
1885 const struct disk_part_custom_attribute gpt_custom_attrs[] = {
1886 	{ .label = MSG_ptn_label,	.type = pet_str },
1887 	{ .label = MSG_ptn_uuid,	.type = pet_str },
1888 	{ .label = MSG_ptn_gpt_type,	.type = pet_str },
1889 	{ .label = MSG_ptn_boot,	.type = pet_bool },
1890 };
1891 
1892 const struct disk_partitioning_scheme
1893 gpt_parts = {
1894 	.name = MSG_parttype_gpt,
1895 	.short_name = MSG_parttype_gpt_short,
1896 	.part_flag_desc = MSG_gpt_flag_desc,
1897 	.custom_attribute_count = __arraycount(gpt_custom_attrs),
1898 	.custom_attributes = gpt_custom_attrs,
1899 	.get_part_types_count = gpt_type_count,
1900 	.get_part_type = gpt_get_ptype,
1901 	.get_generic_part_type = gpt_get_generic_type,
1902 	.get_fs_part_type = gpt_get_fs_part_type,
1903 	.get_default_fstype = gpt_get_default_fstype,
1904 	.create_custom_part_type = gpt_create_custom_part_type,
1905 	.create_unknown_part_type = gpt_create_unknown_part_type,
1906 	.get_part_alignment = gpt_get_part_alignment,
1907 	.read_from_disk = gpt_read_from_disk,
1908 	.get_cylinder_size = gpt_cyl_size,
1909 	.create_new_for_disk = gpt_create_new,
1910 	.have_boot_support = gpt_have_boot_support,
1911 	.find_by_name = gpt_find_by_name,
1912 	.can_add_partition = gpt_can_add_partition,
1913 	.custom_attribute_writable = gpt_custom_attribute_writable,
1914 	.format_custom_attribute = gpt_format_custom_attribute,
1915 	.custom_attribute_toggle = gpt_custom_attribute_toggle,
1916 	.custom_attribute_set_str = gpt_custom_attribute_set_str,
1917 	.other_partition_identifier = gpt_get_label_str,
1918 	.get_part_device = gpt_get_part_device,
1919 	.max_free_space_at = gpt_max_free_space_at,
1920 	.get_free_spaces = gpt_get_free_spaces,
1921 	.adapt_foreign_part_info = generic_adapt_foreign_part_info,
1922 	.get_part_info = gpt_get_part_info,
1923 	.get_part_attr_str = gpt_get_part_attr_str,
1924 	.set_part_info = gpt_set_part_info,
1925 	.add_partition = gpt_add_part,
1926 	.delete_all_partitions = gpt_delete_all_partitions,
1927 	.delete_partition = gpt_delete_partition,
1928 	.write_to_disk = gpt_write_to_disk,
1929 	.free = gpt_free,
1930 	.destroy_part_scheme = gpt_destroy_part_scheme,
1931 	.cleanup = gpt_cleanup,
1932 };
1933