1 /* $NetBSD: gpt.c,v 1.30 2022/12/15 14:54:27 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
gpt_md_init(bool is_boot_disk,size_t * max_parts,size_t * head,size_t * tail)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
gpt_add_info(struct gpt_part_entry * part,const char * tag,char * val,bool details)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
update_part_from_wedge_info(struct gpt_disk_partitions * parts,const struct dkwedge_info * dkw)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 *
gpt_read_from_disk(const char * dev,daddr_t start,daddr_t len,size_t bps,const struct disk_partitioning_scheme * scheme)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
gpt_cyl_size(const struct disk_partitions * arg)458 gpt_cyl_size(const struct disk_partitions *arg)
459 {
460 return MEG / 512;
461 }
462
463 static struct disk_partitions *
gpt_create_new(const char * disk,daddr_t start,daddr_t len,bool is_boot_drive,struct disk_partitions * parent)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
gpt_get_part_info(const struct disk_partitions * arg,part_id id,struct disk_part_info * info)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
gpt_get_part_attr_str(const struct disk_partitions * arg,part_id id,char * str,size_t avail_space)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
gpt_insert_part_into_list(struct gpt_disk_partitions * parts,struct gpt_part_entry ** list,struct gpt_part_entry * entry,const char ** err_msg)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
gpt_set_part_info(struct disk_partitions * arg,part_id id,const struct disk_part_info * info,const char ** err_msg)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
626 for (no = 0; p != NULL && no < id; no++)
627 p = p->gp_next;
628
629 if (no != id || p == NULL)
630 return false;
631
632 /* update target mark - we can only have one */
633 if (info->flags & PTI_INSTALL_TARGET) {
634 p->gp_flags |= GPEF_TARGET;
635 for (n = parts->partitions; n != NULL; n = n->gp_next)
636 if (n != p)
637 n->gp_flags &= ~GPEF_TARGET;
638 } else {
639 p->gp_flags &= ~GPEF_TARGET;
640 }
641
642 if ((p->gp_flags & GPEF_ON_DISK)) {
643 if (info->start != p->gp_start) {
644 /* partition moved, we need to delete and re-add */
645 n = calloc(1, sizeof(*n));
646 if (n == NULL) {
647 if (err_msg)
648 *err_msg = err_outofmem;
649 return false;
650 }
651 *n = *p;
652 p->gp_flags &= ~GPEF_ON_DISK;
653 if (!gpt_insert_part_into_list(parts, &parts->obsolete,
654 n, err_msg))
655 return false;
656 } else if (info->size != p->gp_size) {
657 p->gp_flags |= GPEF_RESIZED;
658 }
659 }
660
661 p->gp_flags |= GPEF_MODIFIED;
662
663 lendiff = info->size - p->gp_size;
664 parts->dp.free_space -= lendiff;
665 return gpt_info_to_part(p, info, err_msg);
666 }
667
668 static size_t
gpt_get_free_spaces_internal(const struct gpt_disk_partitions * parts,struct disk_part_free_space * result,size_t max_num_result,daddr_t min_space_size,daddr_t align,daddr_t start,daddr_t ignore)669 gpt_get_free_spaces_internal(const struct gpt_disk_partitions *parts,
670 struct disk_part_free_space *result, size_t max_num_result,
671 daddr_t min_space_size, daddr_t align, daddr_t start, daddr_t ignore)
672 {
673 size_t cnt = 0;
674 daddr_t s, e, from, size, end_of_disk;
675 struct gpt_part_entry *p;
676
677 if (align > 1)
678 start = max(roundup(start, align), align);
679 if (start < 0 || start < (daddr_t)parts->prologue)
680 start = parts->prologue;
681 if (parts->dp.disk_start != 0 && parts->dp.disk_start > start)
682 start = parts->dp.disk_start;
683 if (min_space_size < 1)
684 min_space_size = 1;
685 end_of_disk = parts->dp.disk_start + parts->dp.disk_size
686 - parts->epilogue;
687 from = start;
688 while (from < end_of_disk && cnt < max_num_result) {
689 again:
690 size = parts->dp.disk_start + parts->dp.disk_size - from;
691 start = from;
692 if (start + size > end_of_disk)
693 size = end_of_disk - start;
694 for (p = parts->partitions; p != NULL; p = p->gp_next) {
695 s = p->gp_start;
696 e = p->gp_size + s;
697 if (s == ignore)
698 continue;
699 if (e < from)
700 continue;
701 if (s <= from && e > from) {
702 if (e - 1 >= end_of_disk)
703 return cnt;
704 from = e + 1;
705 if (align > 1) {
706 from = max(roundup(from, align), align);
707 if (from >= end_of_disk) {
708 size = 0;
709 break;
710 }
711 }
712 goto again;
713 }
714 if (s > from && s - from < size) {
715 size = s - from;
716 }
717 }
718 if (size >= min_space_size) {
719 result->start = start;
720 result->size = size;
721 result++;
722 cnt++;
723 }
724 from += size + 1;
725 if (align > 1)
726 from = max(roundup(from, align), align);
727 }
728
729 return cnt;
730 }
731
732 static daddr_t
gpt_max_free_space_at(const struct disk_partitions * arg,daddr_t start)733 gpt_max_free_space_at(const struct disk_partitions *arg, daddr_t start)
734 {
735 const struct gpt_disk_partitions *parts =
736 (const struct gpt_disk_partitions*)arg;
737 struct disk_part_free_space space;
738
739 if (gpt_get_free_spaces_internal(parts, &space, 1, 1, 0,
740 start, start) == 1)
741 return space.size;
742
743 return 0;
744 }
745
746 static size_t
gpt_get_free_spaces(const struct disk_partitions * arg,struct disk_part_free_space * result,size_t max_num_result,daddr_t min_space_size,daddr_t align,daddr_t start,daddr_t ignore)747 gpt_get_free_spaces(const struct disk_partitions *arg,
748 struct disk_part_free_space *result, size_t max_num_result,
749 daddr_t min_space_size, daddr_t align, daddr_t start,
750 daddr_t ignore)
751 {
752 const struct gpt_disk_partitions *parts =
753 (const struct gpt_disk_partitions*)arg;
754
755 return gpt_get_free_spaces_internal(parts, result,
756 max_num_result, min_space_size, align, start, ignore);
757 }
758
759 static void
gpt_match_ptype(const char * name,struct gpt_ptype_desc * t)760 gpt_match_ptype(const char *name, struct gpt_ptype_desc *t)
761 {
762 size_t i;
763
764 for (i = 0; i < __arraycount(gpt_fs_types); i++) {
765 if (strcmp(name, gpt_fs_types[i].name) == 0) {
766 t->gent.generic_ptype = gpt_fs_types[i].ptype;
767 t->fsflags = gpt_fs_types[i].fsflags;
768 t->default_fs_type = gpt_fs_types[i].fstype;
769
770 /* recongnize special entries */
771 if (gpt_native_root == NULL && i == 0)
772 gpt_native_root = &t->gent;
773
774 return;
775 }
776 }
777
778 t->gent.generic_ptype = PT_unknown;
779 t->fsflags = 0;
780 t->default_fs_type = FS_BSDFFS;
781 }
782
783 static void
gpt_internal_add_ptype(const char * uid,const char * name,const char * desc)784 gpt_internal_add_ptype(const char *uid, const char *name, const char *desc)
785 {
786 if (gpt_ptype_cnt >= gpt_ptype_alloc) {
787 gpt_ptype_alloc = gpt_ptype_alloc ? 2*gpt_ptype_alloc
788 : GPT_PTYPE_ALLOC;
789 struct gpt_ptype_desc *nptypes = realloc(gpt_ptype_descs,
790 gpt_ptype_alloc*sizeof(*gpt_ptype_descs));
791 if (nptypes == 0)
792 errx(EXIT_FAILURE, "out of memory");
793 gpt_ptype_descs = nptypes;
794 }
795
796 strlcpy(gpt_ptype_descs[gpt_ptype_cnt].tid, uid,
797 sizeof(gpt_ptype_descs[gpt_ptype_cnt].tid));
798 gpt_ptype_descs[gpt_ptype_cnt].gent.short_desc = strdup(name);
799 gpt_ptype_descs[gpt_ptype_cnt].gent.description = strdup(desc);
800 gpt_match_ptype(name, &gpt_ptype_descs[gpt_ptype_cnt]);
801 gpt_ptype_cnt++;
802 }
803
804 static void
gpt_init_ptypes(void)805 gpt_init_ptypes(void)
806 {
807 if (gpt_ptype_cnt == 0)
808 gpt_uuid_query(gpt_internal_add_ptype);
809 }
810
811 static void
gpt_cleanup(void)812 gpt_cleanup(void)
813 {
814 /* free all of gpt_ptype_descs */
815 for (size_t i = 0; i < gpt_ptype_cnt; i++) {
816 free(__UNCONST(gpt_ptype_descs[i].gent.short_desc));
817 free(__UNCONST(gpt_ptype_descs[i].gent.description));
818 }
819 free(gpt_ptype_descs);
820 gpt_ptype_descs = NULL;
821 gpt_ptype_cnt = gpt_ptype_alloc = 0;
822 }
823
824 static size_t
gpt_type_count(void)825 gpt_type_count(void)
826 {
827 if (gpt_ptype_cnt == 0)
828 gpt_init_ptypes();
829
830 return gpt_ptype_cnt;
831 }
832
833 static const struct part_type_desc *
gpt_get_ptype(size_t ndx)834 gpt_get_ptype(size_t ndx)
835 {
836 if (gpt_ptype_cnt == 0)
837 gpt_init_ptypes();
838
839 if (ndx >= gpt_ptype_cnt)
840 return NULL;
841
842 return &gpt_ptype_descs[ndx].gent;
843 }
844
845 static const struct part_type_desc *
gpt_get_generic_type(enum part_type gent)846 gpt_get_generic_type(enum part_type gent)
847 {
848 if (gpt_ptype_cnt == 0)
849 gpt_init_ptypes();
850
851 if (gent == PT_root)
852 return gpt_native_root;
853 if (gent == PT_unknown)
854 return NULL;
855
856 for (size_t i = 0; i < gpt_ptype_cnt; i++)
857 if (gpt_ptype_descs[i].gent.generic_ptype == gent)
858 return &gpt_ptype_descs[i].gent;
859
860 return NULL;
861 }
862
863 static const struct gpt_ptype_desc *
gpt_find_native_type(const struct part_type_desc * gent)864 gpt_find_native_type(const struct part_type_desc *gent)
865 {
866 if (gpt_ptype_cnt == 0)
867 gpt_init_ptypes();
868
869 if (gent == NULL)
870 return NULL;
871
872 for (size_t i = 0; i < gpt_ptype_cnt; i++)
873 if (gent == &gpt_ptype_descs[i].gent)
874 return &gpt_ptype_descs[i];
875
876 gent = gpt_get_generic_type(gent->generic_ptype);
877 if (gent == NULL)
878 return NULL;
879
880 /* this can not recurse deeper than once, we would not have found a
881 * generic type a few lines above if it would. */
882 return gpt_find_native_type(gent);
883 }
884
885 static const struct gpt_ptype_desc *
gpt_find_guid_type(const char * uid)886 gpt_find_guid_type(const char *uid)
887 {
888 if (gpt_ptype_cnt == 0)
889 gpt_init_ptypes();
890
891 if (uid == NULL || uid[0] == 0)
892 return NULL;
893
894 for (size_t i = 0; i < gpt_ptype_cnt; i++)
895 if (strcmp(gpt_ptype_descs[i].tid, uid) == 0)
896 return &gpt_ptype_descs[i];
897
898 return NULL;
899 }
900
901 static const struct part_type_desc *
gpt_find_type(const char * desc)902 gpt_find_type(const char *desc)
903 {
904 if (gpt_ptype_cnt == 0)
905 gpt_init_ptypes();
906
907 if (desc == NULL || desc[0] == 0)
908 return NULL;
909
910 for (size_t i = 0; i < gpt_ptype_cnt; i++)
911 if (strcmp(gpt_ptype_descs[i].gent.short_desc, desc) == 0)
912 return &gpt_ptype_descs[i].gent;
913
914 return NULL;
915 }
916
917 static const struct part_type_desc *
gpt_get_fs_part_type(enum part_type pt,unsigned fstype,unsigned fs_sub_type)918 gpt_get_fs_part_type(enum part_type pt, unsigned fstype, unsigned fs_sub_type)
919 {
920 size_t i;
921
922 /* Try with complete match (including part_type) first */
923 for (i = 0; i < __arraycount(gpt_fs_types); i++)
924 if (fstype == gpt_fs_types[i].fstype &&
925 pt == gpt_fs_types[i].ptype)
926 return gpt_find_type(gpt_fs_types[i].name);
927
928 /* If that did not work, ignore part_type */
929 for (i = 0; i < __arraycount(gpt_fs_types); i++)
930 if (fstype == gpt_fs_types[i].fstype)
931 return gpt_find_type(gpt_fs_types[i].name);
932
933 return NULL;
934 }
935
936 static bool
gpt_get_default_fstype(const struct part_type_desc * nat_type,unsigned * fstype,unsigned * fs_sub_type)937 gpt_get_default_fstype(const struct part_type_desc *nat_type,
938 unsigned *fstype, unsigned *fs_sub_type)
939 {
940 const struct gpt_ptype_desc *gtype;
941
942 gtype = gpt_find_native_type(nat_type);
943 if (gtype == NULL)
944 return false;
945
946 *fstype = gtype->default_fs_type;
947 #ifdef DEFAULT_UFS2
948 if (gtype->default_fs_type == FS_BSDFFS)
949 *fs_sub_type = 2;
950 else
951 #endif
952 *fs_sub_type = 0;
953 return true;
954 }
955
956 static const struct part_type_desc *
gpt_get_uuid_part_type(const uuid_t * id)957 gpt_get_uuid_part_type(const uuid_t *id)
958 {
959 char str[GUID_STR_LEN], desc[GUID_STR_LEN + MENUSTRSIZE];
960 const struct gpt_ptype_desc *t;
961 char *guid = NULL;
962 uint32_t err;
963
964 uuid_to_string(id, &guid, &err);
965 strlcpy(str, err == uuid_s_ok ? guid : "-", sizeof str);
966 free(guid);
967
968 t = gpt_find_guid_type(str);
969 if (t == NULL) {
970 snprintf(desc, sizeof desc, "%s (%s)",
971 msg_string(MSG_custom_type), str);
972 gpt_internal_add_ptype(str, str, desc);
973 t = gpt_find_guid_type(str);
974 assert(t != NULL);
975 }
976 return &t->gent;
977 }
978
979 static const struct part_type_desc *
gpt_create_custom_part_type(const char * custom,const char ** err_msg)980 gpt_create_custom_part_type(const char *custom, const char **err_msg)
981 {
982 uuid_t id;
983 uint32_t err;
984
985 uuid_from_string(custom, &id, &err);
986 if (err_msg != NULL &&
987 (err == uuid_s_invalid_string_uuid || err == uuid_s_bad_version)) {
988 *err_msg = MSG_invalid_guid;
989 return NULL;
990 }
991 if (err != uuid_s_ok)
992 return NULL;
993
994 return gpt_get_uuid_part_type(&id);
995 }
996
997 static const struct part_type_desc *
gpt_create_unknown_part_type(void)998 gpt_create_unknown_part_type(void)
999 {
1000 uuid_t id;
1001 uint32_t err;
1002
1003 uuid_create(&id, &err);
1004 if (err != uuid_s_ok)
1005 return NULL;
1006
1007 return gpt_get_uuid_part_type(&id);
1008 }
1009
1010 static daddr_t
gpt_get_part_alignment(const struct disk_partitions * parts)1011 gpt_get_part_alignment(const struct disk_partitions *parts)
1012 {
1013
1014 assert(parts->disk_size > 0);
1015 if (parts->disk_size < 0)
1016 return 1;
1017
1018 /* Use 1MB offset/alignemnt for large (>128GB) disks */
1019 if (parts->disk_size > HUGE_DISK_SIZE)
1020 return 2048;
1021 else if (parts->disk_size > TINY_DISK_SIZE)
1022 return 64;
1023 else
1024 return 4;
1025 }
1026
1027 static bool
gpt_can_add_partition(const struct disk_partitions * arg)1028 gpt_can_add_partition(const struct disk_partitions *arg)
1029 {
1030 const struct gpt_disk_partitions *parts =
1031 (const struct gpt_disk_partitions*)arg;
1032 struct disk_part_free_space space;
1033 daddr_t align;
1034
1035 if (parts->dp.num_part >= parts->max_num_parts)
1036 return false;
1037
1038 align = gpt_get_part_alignment(arg);
1039 if (parts->dp.free_space <= align)
1040 return false;
1041
1042 if (gpt_get_free_spaces_internal(parts, &space, 1, align, align,
1043 0, -1) < 1)
1044 return false;
1045
1046 return true;
1047 }
1048
1049 static bool
gpt_info_to_part(struct gpt_part_entry * p,const struct disk_part_info * info,const char ** err_msg)1050 gpt_info_to_part(struct gpt_part_entry *p, const struct disk_part_info *info,
1051 const char **err_msg)
1052 {
1053 p->gp_type = gpt_find_native_type(info->nat_type);
1054 p->gp_start = info->start;
1055 p->gp_size = info->size;
1056 if (info->last_mounted != NULL && info->last_mounted !=
1057 p->last_mounted) {
1058 free(__UNCONST(p->last_mounted));
1059 p->last_mounted = strdup(info->last_mounted);
1060 }
1061 p->fs_type = info->fs_type;
1062 p->fs_sub_type = info->fs_sub_type;
1063 p->fs_opt1 = info->fs_opt1;
1064 p->fs_opt2 = info->fs_opt2;
1065 p->fs_opt3 = info->fs_opt3;
1066
1067 return true;
1068 }
1069
1070 static part_id
gpt_add_part(struct disk_partitions * arg,const struct disk_part_info * info,const char ** err_msg)1071 gpt_add_part(struct disk_partitions *arg,
1072 const struct disk_part_info *info, const char **err_msg)
1073 {
1074 struct gpt_disk_partitions *parts =
1075 (struct gpt_disk_partitions*)arg;
1076 struct disk_part_free_space space;
1077 struct disk_part_info data = *info;
1078 struct gpt_part_entry *p, *n;
1079 bool ok;
1080
1081 if (err_msg != NULL)
1082 *err_msg = NULL;
1083
1084 if (gpt_get_free_spaces_internal(parts, &space, 1, 1, 1,
1085 info->start, -1) < 1) {
1086 if (err_msg)
1087 *err_msg = msg_string(MSG_No_free_space);
1088 return NO_PART;
1089 }
1090 if (parts->dp.num_part >= parts->max_num_parts) {
1091 if (err_msg)
1092 *err_msg = msg_string(MSG_err_too_many_partitions);
1093 return NO_PART;
1094 }
1095
1096 if (data.size > space.size)
1097 data.size = space.size;
1098
1099 p = calloc(1, sizeof(*p));
1100 if (p == NULL) {
1101 if (err_msg != NULL)
1102 *err_msg = INTERNAL_ERROR;
1103 return NO_PART;
1104 }
1105 if (!gpt_info_to_part(p, &data, err_msg)) {
1106 free(p);
1107 return NO_PART;
1108 }
1109 p->gp_flags |= GPEF_MODIFIED;
1110 ok = gpt_insert_part_into_list(parts, &parts->partitions, p, err_msg);
1111 if (ok) {
1112 if (info->flags & PTI_INSTALL_TARGET) {
1113 /* update target mark - we can only have one */
1114 p->gp_flags |= GPEF_TARGET;
1115 for (n = parts->partitions; n != NULL; n = n->gp_next)
1116 if (n != p)
1117 n->gp_flags &= ~GPEF_TARGET;
1118 }
1119
1120 parts->dp.num_part++;
1121 parts->dp.free_space -= p->gp_size;
1122 return parts->dp.num_part-1;
1123 } else {
1124 free(p);
1125 return NO_PART;
1126 }
1127 }
1128
1129 static bool
gpt_delete_partition(struct disk_partitions * arg,part_id id,const char ** err_msg)1130 gpt_delete_partition(struct disk_partitions *arg, part_id id,
1131 const char **err_msg)
1132 {
1133 struct gpt_disk_partitions *parts = (struct gpt_disk_partitions*)arg;
1134 struct gpt_part_entry *p, *last = NULL;
1135 part_id i;
1136 bool res;
1137
1138 if (parts->dp.num_part == 0)
1139 return false;
1140
1141 for (i = 0, p = parts->partitions;
1142 i != id && i < parts->dp.num_part && p != NULL;
1143 i++, p = p->gp_next)
1144 last = p;
1145
1146 if (p == NULL) {
1147 if (err_msg)
1148 *err_msg = INTERNAL_ERROR;
1149 return false;
1150 }
1151
1152 if (last == NULL)
1153 parts->partitions = p->gp_next;
1154 else
1155 last->gp_next = p->gp_next;
1156
1157 res = true;
1158 if (p->gp_flags & GPEF_ON_DISK) {
1159 if (!gpt_insert_part_into_list(parts, &parts->obsolete,
1160 p, err_msg))
1161 res = false;
1162 } else {
1163 free(p);
1164 }
1165
1166 if (res) {
1167 parts->dp.num_part--;
1168 parts->dp.free_space += p->gp_size;
1169 }
1170
1171 return res;
1172 }
1173
1174 static bool
gpt_delete_all_partitions(struct disk_partitions * arg)1175 gpt_delete_all_partitions(struct disk_partitions *arg)
1176 {
1177 struct gpt_disk_partitions *parts = (struct gpt_disk_partitions*)arg;
1178
1179 while (parts->dp.num_part > 0) {
1180 if (!gpt_delete_partition(&parts->dp, 0, NULL))
1181 return false;
1182 }
1183
1184 return true;
1185 }
1186
1187 static bool
gpt_read_part(const char * disk,daddr_t start,struct gpt_part_entry * p)1188 gpt_read_part(const char *disk, daddr_t start, struct gpt_part_entry *p)
1189 {
1190 char *textbuf, *t, *tt;
1191 static const char expected_hdr[] = "Details for index ";
1192
1193 /* run gpt show for this partition */
1194 if (collect(T_OUTPUT, &textbuf,
1195 "gpt -r show -b %" PRIu64 " %s 2>/dev/null", start, disk) < 1)
1196 return false;
1197
1198 /*
1199 * gpt show should respond with single partition details, but will
1200 * fall back to "show -a" output if something is wrong
1201 */
1202 t = strtok(textbuf, "\n"); /* first line is special */
1203 if (strncmp(t, expected_hdr, sizeof(expected_hdr)-1) != 0) {
1204 free(textbuf);
1205 return false;
1206 }
1207
1208 /* parse output into "old" */
1209 while ((t = strtok(NULL, "\n")) != NULL) {
1210 tt = strsep(&t, " \t");
1211 if (strlen(tt) == 0)
1212 continue;
1213 gpt_add_info(p, tt, t, true);
1214 }
1215 free(textbuf);
1216
1217 return true;
1218 }
1219
1220 static bool
gpt_apply_attr(const char * disk,const char * cmd,off_t start,uint todo)1221 gpt_apply_attr(const char *disk, const char *cmd, off_t start, uint todo)
1222 {
1223 size_t i;
1224 char attr_str[STRSIZE];
1225
1226 if (todo == 0)
1227 return true;
1228
1229 strcpy(attr_str, "-a ");
1230 for (i = 0; todo != 0; i++) {
1231 if (!(gpt_avail_attrs[i].flag & todo))
1232 continue;
1233 todo &= ~gpt_avail_attrs[i].flag;
1234 if (attr_str[0])
1235 strlcat(attr_str, ",",
1236 sizeof(attr_str));
1237 strlcat(attr_str,
1238 gpt_avail_attrs[i].name,
1239 sizeof(attr_str));
1240 }
1241 if (run_program(RUN_SILENT,
1242 "gpt %s %s -b %" PRIu64 " %s", cmd, attr_str, start, disk) != 0)
1243 return false;
1244 return true;
1245 }
1246
1247 /*
1248 * Modify an existing on-disk partition.
1249 * Start and size can not be changed here, caller needs to deal
1250 * with that kind of changes upfront.
1251 */
1252 static bool
gpt_modify_part(const char * disk,struct gpt_part_entry * p)1253 gpt_modify_part(const char *disk, struct gpt_part_entry *p)
1254 {
1255 struct gpt_part_entry old;
1256 uint todo_set, todo_unset;
1257
1258 /*
1259 * Query current on-disk state
1260 */
1261 memset(&old, 0, sizeof old);
1262 if (!gpt_read_part(disk, p->gp_start, &old))
1263 return false;
1264
1265 /* Reject unsupported changes */
1266 if (old.gp_start != p->gp_start || old.gp_size != p->gp_size)
1267 return false;
1268
1269 /*
1270 * GUID should never change, but the internal copy
1271 * may not yet know it.
1272 */
1273 strcpy(p->gp_id, old.gp_id);
1274
1275 /* Check type */
1276 if (p->gp_type != old.gp_type) {
1277 if (run_program(RUN_SILENT,
1278 "gpt type -b %" PRIu64 " -T %s %s",
1279 p->gp_start, p->gp_type->tid, disk) != 0)
1280 return false;
1281 }
1282
1283 /* Check label */
1284 if (strcmp(p->gp_label, old.gp_label) != 0) {
1285 if (run_program(RUN_SILENT,
1286 "gpt label -b %" PRIu64 " -l \'%s\' %s",
1287 p->gp_start, p->gp_label, disk) != 0)
1288 return false;
1289 }
1290
1291 /* Check attributes */
1292 if (p->gp_attr != old.gp_attr) {
1293 if (p->gp_attr == 0) {
1294 if (run_program(RUN_SILENT,
1295 "gpt set -N -b %" PRIu64 " %s",
1296 p->gp_start, disk) != 0)
1297 return false;
1298 } else {
1299 todo_set = (p->gp_attr ^ old.gp_attr) & p->gp_attr;
1300 todo_unset = (p->gp_attr ^ old.gp_attr) & old.gp_attr;
1301 if (!gpt_apply_attr(disk, "unset", p->gp_start,
1302 todo_unset))
1303 return false;
1304 if (!gpt_apply_attr(disk, "set", p->gp_start,
1305 todo_set))
1306 return false;
1307 }
1308 }
1309
1310 return true;
1311 }
1312
1313 /*
1314 * verbatim copy from sys/dev/dkwedge/dkwedge_bsdlabel.c:
1315 * map FS_* to wedge strings
1316 */
1317 static const char *
bsdlabel_fstype_to_str(uint8_t fstype)1318 bsdlabel_fstype_to_str(uint8_t fstype)
1319 {
1320 const char *str;
1321
1322 /*
1323 * For each type known to FSTYPE_DEFN (from <sys/disklabel.h>),
1324 * a suitable case branch will convert the type number to a string.
1325 */
1326 switch (fstype) {
1327 #define FSTYPE_TO_STR_CASE(tag, number, name, fsck, mount) \
1328 case __CONCAT(FS_,tag): str = __CONCAT(DKW_PTYPE_,tag); break;
1329 FSTYPE_DEFN(FSTYPE_TO_STR_CASE)
1330 #undef FSTYPE_TO_STR_CASE
1331 default: str = NULL; break;
1332 }
1333
1334 return (str);
1335 }
1336
1337 /*
1338 * diskfd is an open file descriptor for a disk we had trouble with
1339 * creating some new wedges.
1340 * Go through all wedges actually on that disk, check if we have a
1341 * record for them and remove all others.
1342 * This should sync our internal model of partitions with the real state.
1343 */
1344 static void
gpt_sanitize(int diskfd,const struct gpt_disk_partitions * parts,struct gpt_part_entry * ignore)1345 gpt_sanitize(int diskfd, const struct gpt_disk_partitions *parts,
1346 struct gpt_part_entry *ignore)
1347 {
1348 struct dkwedge_info *dkw, delw;
1349 struct dkwedge_list dkwl;
1350 size_t bufsize;
1351 u_int i;
1352
1353 dkw = NULL;
1354 dkwl.dkwl_buf = dkw;
1355 dkwl.dkwl_bufsize = 0;
1356
1357 /* get a list of all wedges */
1358 for (;;) {
1359 if (ioctl(diskfd, DIOCLWEDGES, &dkwl) == -1)
1360 return;
1361 if (dkwl.dkwl_nwedges == dkwl.dkwl_ncopied)
1362 break;
1363 bufsize = dkwl.dkwl_nwedges * sizeof(*dkw);
1364 if (dkwl.dkwl_bufsize < bufsize) {
1365 dkw = realloc(dkwl.dkwl_buf, bufsize);
1366 if (dkw == NULL)
1367 return;
1368 dkwl.dkwl_buf = dkw;
1369 dkwl.dkwl_bufsize = bufsize;
1370 }
1371 }
1372
1373 /* try to remove all the ones we do not know about */
1374 for (i = 0; i < dkwl.dkwl_nwedges; i++) {
1375 bool found = false;
1376 const char *devname = dkw[i].dkw_devname;
1377
1378 for (struct gpt_part_entry *pe = parts->partitions;
1379 pe != NULL; pe = pe->gp_next) {
1380 if (pe == ignore)
1381 continue;
1382 if ((pe->gp_flags & GPEF_WEDGE) &&
1383 strcmp(pe->gp_dev_name, devname) == 0) {
1384 found = true;
1385 break;
1386 }
1387 }
1388 if (found)
1389 continue;
1390 memset(&delw, 0, sizeof(delw));
1391 strlcpy(delw.dkw_devname, devname, sizeof(delw.dkw_devname));
1392 (void)ioctl(diskfd, DIOCDWEDGE, &delw);
1393 }
1394
1395 /* cleanup */
1396 free(dkw);
1397 }
1398
1399 static bool
gpt_add_wedge(const char * disk,struct gpt_part_entry * p,const struct gpt_disk_partitions * parts)1400 gpt_add_wedge(const char *disk, struct gpt_part_entry *p,
1401 const struct gpt_disk_partitions *parts)
1402 {
1403 struct dkwedge_info dkw;
1404 const char *tname;
1405 char diskpath[MAXPATHLEN];
1406 int fd;
1407
1408 memset(&dkw, 0, sizeof(dkw));
1409 tname = bsdlabel_fstype_to_str(p->fs_type);
1410 if (tname)
1411 strlcpy(dkw.dkw_ptype, tname, sizeof(dkw.dkw_ptype));
1412
1413 strlcpy((char*)&dkw.dkw_wname, p->gp_id, sizeof(dkw.dkw_wname));
1414 dkw.dkw_offset = p->gp_start;
1415 dkw.dkw_size = p->gp_size;
1416 if (dkw.dkw_wname[0] == 0) {
1417 if (p->gp_label[0] != 0)
1418 strlcpy((char*)&dkw.dkw_wname,
1419 p->gp_label, sizeof(dkw.dkw_wname));
1420 }
1421 if (dkw.dkw_wname[0] == 0) {
1422 snprintf((char*)dkw.dkw_wname, sizeof dkw.dkw_wname,
1423 "%s_%" PRIi64 "@%" PRIi64, disk, p->gp_size, p->gp_start);
1424 }
1425
1426 fd = opendisk(disk, O_RDWR, diskpath, sizeof(diskpath), 0);
1427 if (fd < 0)
1428 return false;
1429 if (ioctl(fd, DIOCAWEDGE, &dkw) == -1) {
1430 if (errno == EINVAL) {
1431 /* sanitize existing wedges and try again */
1432 gpt_sanitize(fd, parts, p);
1433 if (ioctl(fd, DIOCAWEDGE, &dkw) == 0)
1434 goto ok;
1435 }
1436 close(fd);
1437 return false;
1438 }
1439 ok:
1440 close(fd);
1441
1442 strlcpy(p->gp_dev_name, dkw.dkw_devname, sizeof(p->gp_dev_name));
1443 p->gp_flags |= GPEF_WEDGE;
1444 return true;
1445 }
1446
1447 static void
escape_spaces(char * dest,const char * src)1448 escape_spaces(char *dest, const char *src)
1449 {
1450 unsigned char c;
1451
1452 while (*src) {
1453 c = *src++;
1454 if (isspace(c) || c == '\\')
1455 *dest++ = '\\';
1456 *dest++ = c;
1457 }
1458 *dest = 0;
1459 }
1460
1461 static bool
gpt_get_part_device(const struct disk_partitions * arg,part_id id,char * devname,size_t max_devname_len,int * part,enum dev_name_usage usage,bool with_path,bool life)1462 gpt_get_part_device(const struct disk_partitions *arg,
1463 part_id id, char *devname, size_t max_devname_len, int *part,
1464 enum dev_name_usage usage, bool with_path, bool life)
1465 {
1466 const struct gpt_disk_partitions *parts =
1467 (const struct gpt_disk_partitions*)arg;
1468 struct gpt_part_entry *p = parts->partitions;
1469 char tmpname[GPT_LABEL_LEN*2];
1470 part_id no;
1471
1472
1473 for (no = 0; p != NULL && no < id; no++)
1474 p = p->gp_next;
1475
1476 if (no != id || p == NULL)
1477 return false;
1478
1479 if (part)
1480 *part = -1;
1481
1482 if (usage == logical_name && p->gp_label[0] == 0 && p->gp_id[0] == 0)
1483 usage = plain_name;
1484 if (usage == plain_name || usage == raw_dev_name)
1485 life = true;
1486 if (!(p->gp_flags & GPEF_WEDGE) && life &&
1487 !gpt_add_wedge(arg->disk, p, parts))
1488 return false;
1489
1490 switch (usage) {
1491 case logical_name:
1492 if (p->gp_label[0] != 0) {
1493 escape_spaces(tmpname, p->gp_label);
1494 snprintf(devname, max_devname_len,
1495 "NAME=%s", tmpname);
1496 } else {
1497 snprintf(devname, max_devname_len,
1498 "NAME=%s", p->gp_id);
1499 }
1500 break;
1501 case plain_name:
1502 assert(p->gp_flags & GPEF_WEDGE);
1503 if (with_path)
1504 snprintf(devname, max_devname_len, _PATH_DEV "%s",
1505 p->gp_dev_name);
1506 else
1507 strlcpy(devname, p->gp_dev_name, max_devname_len);
1508 break;
1509 case raw_dev_name:
1510 assert(p->gp_flags & GPEF_WEDGE);
1511 if (with_path)
1512 snprintf(devname, max_devname_len, _PATH_DEV "r%s",
1513 p->gp_dev_name);
1514 else
1515 snprintf(devname, max_devname_len, "r%s",
1516 p->gp_dev_name);
1517 break;
1518 default:
1519 return false;
1520 }
1521
1522 return true;
1523 }
1524
1525 static bool
gpt_write_to_disk(struct disk_partitions * arg)1526 gpt_write_to_disk(struct disk_partitions *arg)
1527 {
1528 struct gpt_disk_partitions *parts = (struct gpt_disk_partitions*)arg;
1529 struct gpt_part_entry *p, *n;
1530 char label_arg[sizeof(p->gp_label) + 10];
1531 char diskpath[MAXPATHLEN];
1532 int fd, bits = 0;
1533 bool root_is_new = false, efi_is_new = false;
1534 part_id root_id = NO_PART, efi_id = NO_PART, pno;
1535
1536 /*
1537 * Remove all wedges on this disk - they may become invalid and we
1538 * have no easy way to associate them with the partitioning data.
1539 * Instead we will explicitly request creation of wedges on demand
1540 * later.
1541 */
1542 fd = opendisk(arg->disk, O_RDWR, diskpath, sizeof(diskpath), 0);
1543 if (fd < 0)
1544 return false;
1545 if (ioctl(fd, DIOCRMWEDGES, &bits) == -1)
1546 return false;
1547 close(fd);
1548
1549 /*
1550 * Collect first root and efi partition (if available), clear
1551 * "have wedge" flags.
1552 */
1553 for (pno = 0, p = parts->partitions; p != NULL; p = p->gp_next, pno++) {
1554 p->gp_flags &= ~GPEF_WEDGE;
1555 if (root_id == NO_PART && p->gp_type != NULL) {
1556 if (p->gp_type->gent.generic_ptype == PT_root &&
1557 (p->gp_flags & GPEF_TARGET)) {
1558 root_id = pno;
1559 root_is_new = !(p->gp_flags & GPEF_ON_DISK);
1560 } else if (efi_id == NO_PART &&
1561 p->gp_type->gent.generic_ptype == PT_EFI_SYSTEM) {
1562 efi_id = pno;
1563 efi_is_new = !(p->gp_flags & GPEF_ON_DISK);
1564 }
1565 }
1566 }
1567
1568 /*
1569 * If no GPT on disk yet, create it.
1570 */
1571 if (!parts->has_gpt) {
1572 char limit[30];
1573
1574 if (parts->max_num_parts > 0)
1575 sprintf(limit, "-p %zu", parts->max_num_parts);
1576 else
1577 limit[0] = 0;
1578 if (run_program(RUN_SILENT, "gpt create %s %s",
1579 limit, parts->dp.disk))
1580 return false;
1581 parts->has_gpt = true;
1582 }
1583
1584 /*
1585 * Delete all old partitions
1586 */
1587 for (p = parts->obsolete; p != NULL; p = n) {
1588 run_program(RUN_SILENT, "gpt -n remove -b %" PRIu64 " %s",
1589 p->gp_start, arg->disk);
1590 n = p->gp_next;
1591 free(p);
1592 }
1593 parts->obsolete = NULL;
1594
1595 /*
1596 * Modify existing but changed partitions
1597 */
1598 for (p = parts->partitions; p != NULL; p = p->gp_next) {
1599 if (!(p->gp_flags & GPEF_ON_DISK))
1600 continue;
1601
1602 if (p->gp_flags & GPEF_RESIZED) {
1603 run_program(RUN_SILENT,
1604 "gpt -n resize -b %" PRIu64 " -s %" PRIu64 "s %s",
1605 p->gp_start, p->gp_size, arg->disk);
1606 p->gp_flags &= ~GPEF_RESIZED;
1607 }
1608
1609 if (!(p->gp_flags & GPEF_MODIFIED))
1610 continue;
1611
1612 if (!gpt_modify_part(parts->dp.disk, p))
1613 return false;
1614 }
1615
1616 /*
1617 * Add new partitions
1618 */
1619 for (p = parts->partitions; p != NULL; p = p->gp_next) {
1620 if (p->gp_flags & GPEF_ON_DISK)
1621 continue;
1622 if (!(p->gp_flags & GPEF_MODIFIED))
1623 continue;
1624
1625 if (p->gp_label[0] == 0)
1626 label_arg[0] = 0;
1627 else
1628 sprintf(label_arg, "-l \'%s\'", p->gp_label);
1629
1630 if (p->gp_type != NULL)
1631 run_program(RUN_SILENT,
1632 "gpt -n add -b %" PRIu64 " -s %" PRIu64
1633 "s -t %s %s %s",
1634 p->gp_start, p->gp_size, p->gp_type->tid,
1635 label_arg, arg->disk);
1636 else
1637 run_program(RUN_SILENT,
1638 "gpt -n add -b %" PRIu64 " -s %" PRIu64
1639 "s %s %s",
1640 p->gp_start, p->gp_size, label_arg, arg->disk);
1641 gpt_apply_attr(arg->disk, "set", p->gp_start, p->gp_attr);
1642 gpt_read_part(arg->disk, p->gp_start, p);
1643 p->gp_flags |= GPEF_ON_DISK;
1644 }
1645
1646 /*
1647 * Additional MD bootloader magic...
1648 */
1649 if (!md_gpt_post_write(&parts->dp, root_id, root_is_new, efi_id,
1650 efi_is_new))
1651 return false;
1652
1653 return true;
1654 }
1655
1656 static part_id
gpt_find_by_name(struct disk_partitions * arg,const char * name)1657 gpt_find_by_name(struct disk_partitions *arg, const char *name)
1658 {
1659 struct gpt_disk_partitions *parts = (struct gpt_disk_partitions*)arg;
1660 struct gpt_part_entry *p;
1661 part_id pno;
1662
1663 for (pno = 0, p = parts->partitions; p != NULL;
1664 p = p->gp_next, pno++) {
1665 if (strcmp(p->gp_label, name) == 0)
1666 return pno;
1667 if (strcmp(p->gp_id, name) == 0)
1668 return pno;
1669 }
1670
1671 return NO_PART;
1672 }
1673
1674 bool
gpt_parts_check(void)1675 gpt_parts_check(void)
1676 {
1677
1678 check_available_binaries();
1679
1680 return have_gpt && have_dk;
1681 }
1682
1683 static void
gpt_free(struct disk_partitions * arg)1684 gpt_free(struct disk_partitions *arg)
1685 {
1686 struct gpt_disk_partitions *parts = (struct gpt_disk_partitions*)arg;
1687 struct gpt_part_entry *p, *n;
1688
1689 assert(parts != NULL);
1690 for (p = parts->partitions; p != NULL; p = n) {
1691 if (p->gp_flags & GPEF_WEDGE)
1692 register_post_umount_delwedge(parts->dp.disk,
1693 p->gp_dev_name);
1694 free(__UNCONST(p->last_mounted));
1695 n = p->gp_next;
1696 free(p);
1697 }
1698 free(__UNCONST(parts->dp.disk));
1699 free(parts);
1700 }
1701
1702 static void
gpt_destroy_part_scheme(struct disk_partitions * arg)1703 gpt_destroy_part_scheme(struct disk_partitions *arg)
1704 {
1705
1706 run_program(RUN_SILENT, "gpt destroy %s", arg->disk);
1707 gpt_free(arg);
1708 }
1709
1710 static bool
gpt_custom_attribute_writable(const struct disk_partitions * arg,part_id ptn,size_t attr_no)1711 gpt_custom_attribute_writable(const struct disk_partitions *arg,
1712 part_id ptn, size_t attr_no)
1713 {
1714 const struct gpt_disk_partitions *parts =
1715 (const struct gpt_disk_partitions*)arg;
1716 size_t i;
1717 struct gpt_part_entry *p;
1718
1719 if (attr_no >= arg->pscheme->custom_attribute_count)
1720 return false;
1721
1722 const msg label = arg->pscheme->custom_attributes[attr_no].label;
1723
1724 /* we can not edit the uuid attribute */
1725 if (label == MSG_ptn_uuid)
1726 return false;
1727
1728 /* the label is always editable */
1729 if (label == MSG_ptn_label)
1730 return true;
1731
1732 /* the GPT type is read only */
1733 if (label == MSG_ptn_gpt_type)
1734 return false;
1735
1736 /* BOOTME makes no sense on swap partitions */
1737 for (i = 0, p = parts->partitions; p != NULL; i++, p = p->gp_next)
1738 if (i == ptn)
1739 break;
1740
1741 if (p == NULL)
1742 return false;
1743
1744 if (p->fs_type == FS_SWAP ||
1745 (p->gp_type != NULL && p->gp_type->gent.generic_ptype == PT_swap))
1746 return false;
1747
1748 return true;
1749 }
1750
1751 static const char *
gpt_get_label_str(const struct disk_partitions * arg,part_id ptn)1752 gpt_get_label_str(const struct disk_partitions *arg, part_id ptn)
1753 {
1754 const struct gpt_disk_partitions *parts =
1755 (const struct gpt_disk_partitions*)arg;
1756 size_t i;
1757 struct gpt_part_entry *p;
1758
1759 for (i = 0, p = parts->partitions; p != NULL; i++, p = p->gp_next)
1760 if (i == ptn)
1761 break;
1762
1763 if (p == NULL)
1764 return NULL;
1765
1766 if (p->gp_label[0] != 0)
1767 return p->gp_label;
1768 return p->gp_id;
1769 }
1770
1771 static bool
gpt_format_custom_attribute(const struct disk_partitions * arg,part_id ptn,size_t attr_no,const struct disk_part_info * info,char * out,size_t out_space)1772 gpt_format_custom_attribute(const struct disk_partitions *arg,
1773 part_id ptn, size_t attr_no, const struct disk_part_info *info,
1774 char *out, size_t out_space)
1775 {
1776 const struct gpt_disk_partitions *parts =
1777 (const struct gpt_disk_partitions*)arg;
1778 size_t i;
1779 struct gpt_part_entry *p, data;
1780
1781 for (i = 0, p = parts->partitions; p != NULL; i++, p = p->gp_next)
1782 if (i == ptn)
1783 break;
1784
1785 if (p == NULL)
1786 return false;
1787
1788 if (attr_no >= parts->dp.pscheme->custom_attribute_count)
1789 return false;
1790
1791 const msg label = parts->dp.pscheme->custom_attributes[attr_no].label;
1792
1793 if (info != NULL) {
1794 data = *p;
1795 gpt_info_to_part(&data, info, NULL);
1796 p = &data;
1797 }
1798
1799 if (label == MSG_ptn_label)
1800 strlcpy(out, p->gp_label, out_space);
1801 else if (label == MSG_ptn_uuid)
1802 strlcpy(out, p->gp_id, out_space);
1803 else if (label == MSG_ptn_gpt_type) {
1804 if (p->gp_type != NULL)
1805 strlcpy(out, p->gp_type->gent.description, out_space);
1806 else if (out_space > 1)
1807 out[0] = 0;
1808 } else if (label == MSG_ptn_boot)
1809 strlcpy(out, msg_string(p->gp_attr & GPT_ATTR_BOOT ?
1810 MSG_Yes : MSG_No), out_space);
1811 else
1812 return false;
1813
1814 return true;
1815 }
1816
1817 static bool
gpt_custom_attribute_toggle(struct disk_partitions * arg,part_id ptn,size_t attr_no)1818 gpt_custom_attribute_toggle(struct disk_partitions *arg,
1819 part_id ptn, size_t attr_no)
1820 {
1821 const struct gpt_disk_partitions *parts =
1822 (const struct gpt_disk_partitions*)arg;
1823 size_t i;
1824 struct gpt_part_entry *p;
1825
1826 for (i = 0, p = parts->partitions; p != NULL; i++, p = p->gp_next)
1827 if (i == ptn)
1828 break;
1829
1830 if (p == NULL)
1831 return false;
1832
1833 if (attr_no >= parts->dp.pscheme->custom_attribute_count)
1834 return false;
1835
1836 const msg label = parts->dp.pscheme->custom_attributes[attr_no].label;
1837 if (label != MSG_ptn_boot)
1838 return false;
1839
1840 if (p->gp_attr & GPT_ATTR_BOOT) {
1841 p->gp_attr &= ~GPT_ATTR_BOOT;
1842 } else {
1843 for (i = 0, p = parts->partitions; p != NULL;
1844 i++, p = p->gp_next)
1845 if (i == ptn)
1846 p->gp_attr |= GPT_ATTR_BOOT;
1847 else
1848 p->gp_attr &= ~GPT_ATTR_BOOT;
1849 }
1850 return true;
1851 }
1852
1853 static bool
gpt_custom_attribute_set_str(struct disk_partitions * arg,part_id ptn,size_t attr_no,const char * new_val)1854 gpt_custom_attribute_set_str(struct disk_partitions *arg,
1855 part_id ptn, size_t attr_no, const char *new_val)
1856 {
1857 const struct gpt_disk_partitions *parts =
1858 (const struct gpt_disk_partitions*)arg;
1859 size_t i;
1860 struct gpt_part_entry *p;
1861
1862 for (i = 0, p = parts->partitions; p != NULL; i++, p = p->gp_next)
1863 if (i == ptn)
1864 break;
1865
1866 if (p == NULL)
1867 return false;
1868
1869 if (attr_no >= parts->dp.pscheme->custom_attribute_count)
1870 return false;
1871
1872 const msg label = parts->dp.pscheme->custom_attributes[attr_no].label;
1873
1874 if (label != MSG_ptn_label)
1875 return false;
1876
1877 strlcpy(p->gp_label, new_val, sizeof(p->gp_label));
1878 return true;
1879 }
1880
1881 static bool
gpt_have_boot_support(const char * disk)1882 gpt_have_boot_support(const char *disk)
1883 {
1884 #ifdef HAVE_GPT_BOOT
1885 return true;
1886 #else
1887 return false;
1888 #endif
1889 }
1890
1891 const struct disk_part_custom_attribute gpt_custom_attrs[] = {
1892 { .label = MSG_ptn_label, .type = pet_str },
1893 { .label = MSG_ptn_uuid, .type = pet_str },
1894 { .label = MSG_ptn_gpt_type, .type = pet_str },
1895 { .label = MSG_ptn_boot, .type = pet_bool },
1896 };
1897
1898 const struct disk_partitioning_scheme
1899 gpt_parts = {
1900 .name = MSG_parttype_gpt,
1901 .short_name = MSG_parttype_gpt_short,
1902 .part_flag_desc = MSG_gpt_flag_desc,
1903 .custom_attribute_count = __arraycount(gpt_custom_attrs),
1904 .custom_attributes = gpt_custom_attrs,
1905 .get_part_types_count = gpt_type_count,
1906 .get_part_type = gpt_get_ptype,
1907 .get_generic_part_type = gpt_get_generic_type,
1908 .get_fs_part_type = gpt_get_fs_part_type,
1909 .get_default_fstype = gpt_get_default_fstype,
1910 .create_custom_part_type = gpt_create_custom_part_type,
1911 .create_unknown_part_type = gpt_create_unknown_part_type,
1912 .get_part_alignment = gpt_get_part_alignment,
1913 .read_from_disk = gpt_read_from_disk,
1914 .get_cylinder_size = gpt_cyl_size,
1915 .create_new_for_disk = gpt_create_new,
1916 .have_boot_support = gpt_have_boot_support,
1917 .find_by_name = gpt_find_by_name,
1918 .can_add_partition = gpt_can_add_partition,
1919 .custom_attribute_writable = gpt_custom_attribute_writable,
1920 .format_custom_attribute = gpt_format_custom_attribute,
1921 .custom_attribute_toggle = gpt_custom_attribute_toggle,
1922 .custom_attribute_set_str = gpt_custom_attribute_set_str,
1923 .other_partition_identifier = gpt_get_label_str,
1924 .get_part_device = gpt_get_part_device,
1925 .max_free_space_at = gpt_max_free_space_at,
1926 .get_free_spaces = gpt_get_free_spaces,
1927 .adapt_foreign_part_info = generic_adapt_foreign_part_info,
1928 .get_part_info = gpt_get_part_info,
1929 .get_part_attr_str = gpt_get_part_attr_str,
1930 .set_part_info = gpt_set_part_info,
1931 .add_partition = gpt_add_part,
1932 .delete_all_partitions = gpt_delete_all_partitions,
1933 .delete_partition = gpt_delete_partition,
1934 .write_to_disk = gpt_write_to_disk,
1935 .free = gpt_free,
1936 .destroy_part_scheme = gpt_destroy_part_scheme,
1937 .cleanup = gpt_cleanup,
1938 };
1939