]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/bsdinstall/partedit/gpart_ops.c
Merge OpenSSL 1.0.2n.
[FreeBSD/FreeBSD.git] / usr.sbin / bsdinstall / partedit / gpart_ops.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2011 Nathan Whitehorn
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 THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * 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 THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30
31 #include <sys/param.h>
32 #include <sys/stat.h>
33 #include <errno.h>
34 #include <libutil.h>
35 #include <inttypes.h>
36
37 #include <libgeom.h>
38 #include <dialog.h>
39 #include <dlg_keys.h>
40
41 #include "partedit.h"
42
43 #define GPART_FLAGS "x" /* Do not commit changes by default */
44
45 static void
46 gpart_show_error(const char *title, const char *explanation, const char *errstr)
47 {
48         char *errmsg;
49         char message[512];
50         int error;
51
52         if (explanation == NULL)
53                 explanation = "";
54
55         error = strtol(errstr, &errmsg, 0);
56         if (errmsg != errstr) {
57                 while (errmsg[0] == ' ')
58                         errmsg++;
59                 if (errmsg[0] != '\0')
60                         sprintf(message, "%s%s. %s", explanation,
61                             strerror(error), errmsg);
62                 else
63                         sprintf(message, "%s%s", explanation, strerror(error));
64         } else {
65                 sprintf(message, "%s%s", explanation, errmsg);
66         }
67
68         dialog_msgbox(title, message, 0, 0, TRUE);
69 }
70
71 static int
72 scheme_supports_labels(const char *scheme)
73 {
74         if (strcmp(scheme, "APM") == 0)
75                 return (1);
76         if (strcmp(scheme, "GPT") == 0)
77                 return (1);
78
79         return (0);
80 }
81
82 static void
83 newfs_command(const char *fstype, char *command, int use_default)
84 {
85         if (strcmp(fstype, "freebsd-ufs") == 0) {
86                 int i;
87                 DIALOG_LISTITEM items[] = {
88                         {"UFS1", "UFS Version 1",
89                             "Use version 1 of the UFS file system instead "
90                             "of version 2 (not recommended)", 0 },
91                         {"SU", "Softupdates",
92                             "Enable softupdates (default)", 1 },
93                         {"SUJ", "Softupdates journaling",
94                             "Enable file system journaling (default - "
95                             "turn off for SSDs)", 1 },
96                         {"TRIM", "Enable SSD TRIM support",
97                             "Enable TRIM support, useful on solid-state drives",
98                             0 },
99                 };
100
101                 if (!use_default) {
102                         int choice;
103                         choice = dlg_checklist("UFS Options", "", 0, 0, 0,
104                             nitems(items), items, NULL,
105                             FLAG_CHECK, &i);
106                         if (choice == 1) /* Cancel */
107                                 return;
108                 }
109
110                 strcpy(command, "newfs ");
111                 for (i = 0; i < (int)nitems(items); i++) {
112                         if (items[i].state == 0)
113                                 continue;
114                         if (strcmp(items[i].name, "UFS1") == 0)
115                                 strcat(command, "-O1 ");
116                         else if (strcmp(items[i].name, "SU") == 0)
117                                 strcat(command, "-U ");
118                         else if (strcmp(items[i].name, "SUJ") == 0)
119                                 strcat(command, "-j ");
120                         else if (strcmp(items[i].name, "TRIM") == 0)
121                                 strcat(command, "-t ");
122                 }
123         } else if (strcmp(fstype, "freebsd-zfs") == 0) {
124                 int i;
125                 DIALOG_LISTITEM items[] = {
126                         {"fletcher4", "checksum algorithm: fletcher4",
127                             "Use fletcher4 for data integrity checking. "
128                             "(default)", 1 },
129                         {"fletcher2", "checksum algorithm: fletcher2",
130                             "Use fletcher2 for data integrity checking. "
131                             "(not recommended)", 0 },
132                         {"sha256", "checksum algorithm: sha256",
133                             "Use sha256 for data integrity checking. "
134                             "(not recommended)", 0 },
135                         {"atime", "Update atimes for files",
136                             "Disable atime update", 0 },
137                 };
138
139                 if (!use_default) {
140                         int choice;
141                         choice = dlg_checklist("ZFS Options", "", 0, 0, 0,
142                             nitems(items), items, NULL,
143                             FLAG_CHECK, &i);
144                         if (choice == 1) /* Cancel */
145                                 return;
146                 }
147
148                 strcpy(command, "zpool create -f -m none ");
149                 if (getenv("BSDINSTALL_TMPBOOT") != NULL) {
150                         char zfsboot_path[MAXPATHLEN];
151                         snprintf(zfsboot_path, sizeof(zfsboot_path), "%s/zfs",
152                             getenv("BSDINSTALL_TMPBOOT"));
153                         mkdir(zfsboot_path, S_IRWXU | S_IRGRP | S_IXGRP |
154                             S_IROTH | S_IXOTH);
155                         sprintf(command, "%s -o cachefile=%s/zpool.cache ",
156                             command, zfsboot_path);
157                 }
158                 for (i = 0; i < (int)nitems(items); i++) {
159                         if (items[i].state == 0)
160                                 continue;
161                         if (strcmp(items[i].name, "fletcher4") == 0)
162                                 strcat(command, "-O checksum=fletcher4 ");
163                         else if (strcmp(items[i].name, "fletcher2") == 0)
164                                 strcat(command, "-O checksum=fletcher2 ");
165                         else if (strcmp(items[i].name, "sha256") == 0)
166                                 strcat(command, "-O checksum=sha256 ");
167                         else if (strcmp(items[i].name, "atime") == 0)
168                                 strcat(command, "-O atime=off ");
169                 }
170         } else if (strcmp(fstype, "fat32") == 0 || strcmp(fstype, "efi") == 0) {
171                 int i;
172                 DIALOG_LISTITEM items[] = {
173                         {"FAT32", "FAT Type 32",
174                             "Create a FAT32 filesystem (default)", 1 },
175                         {"FAT16", "FAT Type 16",
176                             "Create a FAT16 filesystem", 0 },
177                         {"FAT12", "FAT Type 12",
178                             "Create a FAT12 filesystem", 0 },
179                 };
180
181                 if (!use_default) {
182                         int choice;
183                         choice = dlg_checklist("FAT Options", "", 0, 0, 0,
184                             nitems(items), items, NULL,
185                             FLAG_RADIO, &i);
186                         if (choice == 1) /* Cancel */
187                                 return;
188                 }
189
190                 strcpy(command, "newfs_msdos ");
191                 for (i = 0; i < (int)nitems(items); i++) {
192                         if (items[i].state == 0)
193                                 continue;
194                         if (strcmp(items[i].name, "FAT32") == 0)
195                                 strcat(command, "-F 32 ");
196                         else if (strcmp(items[i].name, "FAT16") == 0)
197                                 strcat(command, "-F 16 ");
198                         else if (strcmp(items[i].name, "FAT12") == 0)
199                                 strcat(command, "-F 12 ");
200                 }
201         } else {
202                 if (!use_default)
203                         dialog_msgbox("Error", "No configurable options exist "
204                             "for this filesystem.", 0, 0, TRUE);
205                 command[0] = '\0';
206         }
207 }
208
209 const char *
210 choose_part_type(const char *def_scheme)
211 {
212         int cancel, choice;
213         const char *scheme = NULL;
214
215         DIALOG_LISTITEM items[] = {
216                 {"APM", "Apple Partition Map",
217                     "Bootable on PowerPC Apple Hardware", 0 },
218                 {"BSD", "BSD Labels",
219                     "Bootable on most x86 systems", 0 },
220                 {"GPT", "GUID Partition Table",
221                     "Bootable on most x86 systems and EFI aware ARM64", 0 },
222                 {"MBR", "DOS Partitions",
223                     "Bootable on most x86 systems", 0 },
224                 {"VTOC8", "Sun VTOC8 Partition Table",
225                     "Bootable on Sun SPARC systems", 0 },
226         };
227
228 parttypemenu:
229         dialog_vars.default_item = __DECONST(char *, def_scheme);
230         cancel = dlg_menu("Partition Scheme",
231             "Select a partition scheme for this volume:", 0, 0, 0,
232             nitems(items), items, &choice, NULL);
233         dialog_vars.default_item = NULL;
234
235         if (cancel)
236                 return NULL;
237
238         if (!is_scheme_bootable(items[choice].name)) {
239                 char message[512];
240                 sprintf(message, "This partition scheme (%s) is not "
241                     "bootable on this platform. Are you sure you want "
242                     "to proceed?", items[choice].name);
243                 dialog_vars.defaultno = TRUE;
244                 cancel = dialog_yesno("Warning", message, 0, 0);
245                 dialog_vars.defaultno = FALSE;
246                 if (cancel) /* cancel */
247                         goto parttypemenu;
248         }
249
250         scheme = items[choice].name;
251
252         return scheme;
253 }
254
255 int
256 gpart_partition(const char *lg_name, const char *scheme)
257 {
258         int cancel;
259         struct gctl_req *r;
260         const char *errstr;
261
262 schememenu:
263         if (scheme == NULL) {
264                 scheme = choose_part_type(default_scheme());
265
266                 if (scheme == NULL)
267                         return (-1);
268
269                 if (!is_scheme_bootable(scheme)) {
270                         char message[512];
271                         sprintf(message, "This partition scheme (%s) is not "
272                             "bootable on this platform. Are you sure you want "
273                             "to proceed?", scheme);
274                         dialog_vars.defaultno = TRUE;
275                         cancel = dialog_yesno("Warning", message, 0, 0);
276                         dialog_vars.defaultno = FALSE;
277                         if (cancel) { /* cancel */
278                                 /* Reset scheme so user can choose another */
279                                 scheme = NULL;
280                                 goto schememenu;
281                         }
282                 }
283         }
284
285         r = gctl_get_handle();
286         gctl_ro_param(r, "class", -1, "PART");
287         gctl_ro_param(r, "arg0", -1, lg_name);
288         gctl_ro_param(r, "flags", -1, GPART_FLAGS);
289         gctl_ro_param(r, "scheme", -1, scheme);
290         gctl_ro_param(r, "verb", -1, "create");
291
292         errstr = gctl_issue(r);
293         if (errstr != NULL && errstr[0] != '\0') {
294                 gpart_show_error("Error", NULL, errstr);
295                 gctl_free(r);
296                 scheme = NULL;
297                 goto schememenu;
298         }
299         gctl_free(r);
300
301         if (bootcode_path(scheme) != NULL)
302                 get_part_metadata(lg_name, 1)->bootcode = 1;
303         return (0);
304 }
305
306 static void
307 gpart_activate(struct gprovider *pp)
308 {
309         struct gconfig *gc;
310         struct gctl_req *r;
311         const char *errstr, *scheme;
312         const char *attribute = NULL;
313         intmax_t idx;
314
315         /*
316          * Some partition schemes need this partition to be marked 'active'
317          * for it to be bootable.
318          */
319         LIST_FOREACH(gc, &pp->lg_geom->lg_config, lg_config) {
320                 if (strcmp(gc->lg_name, "scheme") == 0) {
321                         scheme = gc->lg_val;
322                         break;
323                 }
324         }
325
326         if (strcmp(scheme, "MBR") == 0 || strcmp(scheme, "EBR") == 0)
327                 attribute = "active";
328         else
329                 return;
330
331         LIST_FOREACH(gc, &pp->lg_config, lg_config) {
332                 if (strcmp(gc->lg_name, "index") == 0) {
333                         idx = atoi(gc->lg_val);
334                         break;
335                 }
336         }
337
338         r = gctl_get_handle();
339         gctl_ro_param(r, "class", -1, "PART");
340         gctl_ro_param(r, "arg0", -1, pp->lg_geom->lg_name);
341         gctl_ro_param(r, "verb", -1, "set");
342         gctl_ro_param(r, "attrib", -1, attribute);
343         gctl_ro_param(r, "index", sizeof(idx), &idx);
344
345         errstr = gctl_issue(r);
346         if (errstr != NULL && errstr[0] != '\0') 
347                 gpart_show_error("Error", "Error marking partition active:",
348                     errstr);
349         gctl_free(r);
350 }
351
352 void
353 gpart_set_root(const char *lg_name, const char *attribute)
354 {
355         struct gctl_req *r;
356         const char *errstr;
357
358         r = gctl_get_handle();
359         gctl_ro_param(r, "class", -1, "PART");
360         gctl_ro_param(r, "arg0", -1, lg_name);
361         gctl_ro_param(r, "flags", -1, "C");
362         gctl_ro_param(r, "verb", -1, "set");
363         gctl_ro_param(r, "attrib", -1, attribute);
364
365         errstr = gctl_issue(r);
366         if (errstr != NULL && errstr[0] != '\0') 
367                 gpart_show_error("Error", "Error setting parameter on disk:",
368                     errstr);
369         gctl_free(r);
370 }
371
372 static void
373 gpart_bootcode(struct ggeom *gp)
374 {
375         const char *bootcode;
376         struct gconfig *gc;
377         struct gctl_req *r;
378         const char *errstr, *scheme;
379         uint8_t *boot;
380         size_t bootsize, bytes;
381         int bootfd;
382
383         /*
384          * Write default bootcode to the newly partitioned disk, if that
385          * applies on this platform.
386          */
387         LIST_FOREACH(gc, &gp->lg_config, lg_config) {
388                 if (strcmp(gc->lg_name, "scheme") == 0) {
389                         scheme = gc->lg_val;
390                         break;
391                 }
392         }
393
394         bootcode = bootcode_path(scheme);
395         if (bootcode == NULL) 
396                 return;
397
398         bootfd = open(bootcode, O_RDONLY);
399         if (bootfd < 0) {
400                 dialog_msgbox("Bootcode Error", strerror(errno), 0, 0,
401                     TRUE);
402                 return;
403         }
404                 
405         bootsize = lseek(bootfd, 0, SEEK_END);
406         boot = malloc(bootsize);
407         lseek(bootfd, 0, SEEK_SET);
408         bytes = 0;
409         while (bytes < bootsize)
410                 bytes += read(bootfd, boot + bytes, bootsize - bytes);
411         close(bootfd);
412
413         r = gctl_get_handle();
414         gctl_ro_param(r, "class", -1, "PART");
415         gctl_ro_param(r, "arg0", -1, gp->lg_name);
416         gctl_ro_param(r, "verb", -1, "bootcode");
417         gctl_ro_param(r, "bootcode", bootsize, boot);
418
419         errstr = gctl_issue(r);
420         if (errstr != NULL && errstr[0] != '\0') 
421                 gpart_show_error("Bootcode Error", NULL, errstr);
422         gctl_free(r);
423         free(boot);
424 }
425
426 static void
427 gpart_partcode(struct gprovider *pp, const char *fstype)
428 {
429         struct gconfig *gc;
430         const char *scheme;
431         const char *indexstr;
432         char message[255], command[255];
433
434         LIST_FOREACH(gc, &pp->lg_geom->lg_config, lg_config) {
435                 if (strcmp(gc->lg_name, "scheme") == 0) {
436                         scheme = gc->lg_val;
437                         break;
438                 }
439         }
440
441         /* Make sure this partition scheme needs partcode on this platform */
442         if (partcode_path(scheme, fstype) == NULL)
443                 return;
444
445         LIST_FOREACH(gc, &pp->lg_config, lg_config) {
446                 if (strcmp(gc->lg_name, "index") == 0) {
447                         indexstr = gc->lg_val;
448                         break;
449                 }
450         }
451
452         /* Shell out to gpart for partcode for now */
453         sprintf(command, "gpart bootcode -p %s -i %s %s",
454             partcode_path(scheme, fstype), indexstr, pp->lg_geom->lg_name);
455         if (system(command) != 0) {
456                 sprintf(message, "Error installing partcode on partition %s",
457                     pp->lg_name);
458                 dialog_msgbox("Error", message, 0, 0, TRUE);
459         }
460 }
461
462 void
463 gpart_destroy(struct ggeom *lg_geom)
464 {
465         struct gctl_req *r;
466         struct gprovider *pp;
467         const char *errstr;
468         int force = 1;
469
470         /* Delete all child metadata */
471         LIST_FOREACH(pp, &lg_geom->lg_provider, lg_provider)
472                 gpart_delete(pp);
473
474         /* Revert any local changes to get this geom into a pristine state */
475         r = gctl_get_handle();
476         gctl_ro_param(r, "class", -1, "PART");
477         gctl_ro_param(r, "arg0", -1, lg_geom->lg_name);
478         gctl_ro_param(r, "verb", -1, "undo");
479         gctl_issue(r); /* Ignore errors -- these are non-fatal */
480         gctl_free(r);
481
482         /* Now destroy the geom itself */
483         r = gctl_get_handle();
484         gctl_ro_param(r, "class", -1, "PART");
485         gctl_ro_param(r, "arg0", -1, lg_geom->lg_name);
486         gctl_ro_param(r, "flags", -1, GPART_FLAGS);
487         gctl_ro_param(r, "force", sizeof(force), &force);
488         gctl_ro_param(r, "verb", -1, "destroy");
489         errstr = gctl_issue(r);
490         if (errstr != NULL && errstr[0] != '\0') {
491                 /*
492                  * Check if we reverted away the existence of the geom
493                  * altogether. Show all other errors to the user.
494                  */
495                 if (strtol(errstr, NULL, 0) != EINVAL)
496                         gpart_show_error("Error", NULL, errstr);
497         }
498         gctl_free(r);
499
500         /* And any metadata associated with the partition scheme itself */
501         delete_part_metadata(lg_geom->lg_name);
502 }
503
504 void
505 gpart_edit(struct gprovider *pp)
506 {
507         struct gctl_req *r;
508         struct gconfig *gc;
509         struct gconsumer *cp;
510         struct ggeom *geom;
511         const char *errstr, *oldtype, *scheme;
512         struct partition_metadata *md;
513         char sizestr[32];
514         char newfs[255];
515         intmax_t idx;
516         int hadlabel, choice, junk, nitems;
517         unsigned i;
518
519         DIALOG_FORMITEM items[] = {
520                 {0, "Type:", 5, 0, 0, FALSE, "", 11, 0, 12, 15, 0,
521                     FALSE, "Filesystem type (e.g. freebsd-ufs, freebsd-zfs, "
522                     "freebsd-swap)", FALSE},
523                 {0, "Size:", 5, 1, 0, FALSE, "", 11, 1, 12, 0, 0,
524                     FALSE, "Partition size. Append K, M, G for kilobytes, "
525                     "megabytes or gigabytes.", FALSE},
526                 {0, "Mountpoint:", 11, 2, 0, FALSE, "", 11, 2, 12, 15, 0,
527                     FALSE, "Path at which to mount this partition (leave blank "
528                     "for swap, set to / for root filesystem)", FALSE},
529                 {0, "Label:", 7, 3, 0, FALSE, "", 11, 3, 12, 15, 0, FALSE,
530                     "Partition name. Not all partition schemes support this.",
531                     FALSE},
532         };
533
534         /*
535          * Find the PART geom we are manipulating. This may be a consumer of
536          * this provider, or its parent. Check the consumer case first.
537          */
538         geom = NULL;
539         LIST_FOREACH(cp, &pp->lg_consumers, lg_consumers)
540                 if (strcmp(cp->lg_geom->lg_class->lg_name, "PART") == 0) {
541                         /* Check for zombie geoms, treating them as blank */
542                         scheme = NULL;
543                         LIST_FOREACH(gc, &cp->lg_geom->lg_config, lg_config) {
544                                 if (strcmp(gc->lg_name, "scheme") == 0) {
545                                         scheme = gc->lg_val;
546                                         break;
547                                 }
548                         }
549                         if (scheme == NULL || strcmp(scheme, "(none)") == 0) {
550                                 gpart_partition(cp->lg_geom->lg_name, NULL);
551                                 return;
552                         }
553
554                         /* If this is a nested partition, edit as usual */
555                         if (strcmp(pp->lg_geom->lg_class->lg_name, "PART") == 0)
556                                 break;
557
558                         /* Destroy the geom and all sub-partitions */
559                         gpart_destroy(cp->lg_geom);
560
561                         /* Now re-partition and return */
562                         gpart_partition(cp->lg_geom->lg_name, NULL);
563                         return;
564                 }
565
566         if (geom == NULL && strcmp(pp->lg_geom->lg_class->lg_name, "PART") == 0)
567                 geom = pp->lg_geom;
568
569         if (geom == NULL) {
570                 /* Disk not partitioned, so partition it */
571                 gpart_partition(pp->lg_name, NULL);
572                 return;
573         }
574
575         LIST_FOREACH(gc, &geom->lg_config, lg_config) {
576                 if (strcmp(gc->lg_name, "scheme") == 0) {
577                         scheme = gc->lg_val;
578                         break;
579                 }
580         }
581
582         nitems = scheme_supports_labels(scheme) ? 4 : 3;
583
584         /* Edit editable parameters of a partition */
585         hadlabel = 0;
586         LIST_FOREACH(gc, &pp->lg_config, lg_config) {
587                 if (strcmp(gc->lg_name, "type") == 0) {
588                         oldtype = gc->lg_val;
589                         items[0].text = gc->lg_val;
590                 }
591                 if (strcmp(gc->lg_name, "label") == 0 && gc->lg_val != NULL) {
592                         hadlabel = 1;
593                         items[3].text = gc->lg_val;
594                 }
595                 if (strcmp(gc->lg_name, "index") == 0)
596                         idx = atoi(gc->lg_val);
597         }
598
599         TAILQ_FOREACH(md, &part_metadata, metadata) {
600                 if (md->name != NULL && strcmp(md->name, pp->lg_name) == 0) {
601                         if (md->fstab != NULL)
602                                 items[2].text = md->fstab->fs_file;
603                         break;
604                 }
605         }
606
607         humanize_number(sizestr, 7, pp->lg_mediasize, "B", HN_AUTOSCALE,
608             HN_NOSPACE | HN_DECIMAL);
609         items[1].text = sizestr;
610
611 editpart:
612         choice = dlg_form("Edit Partition", "", 0, 0, 0, nitems, items, &junk);
613
614         if (choice) /* Cancel pressed */
615                 goto endedit;
616
617         /* If this is the root partition, check that this fs is bootable */
618         if (strcmp(items[2].text, "/") == 0 && !is_fs_bootable(scheme,
619             items[0].text)) {
620                 char message[512];
621                 sprintf(message, "This file system (%s) is not bootable "
622                     "on this system. Are you sure you want to proceed?",
623                     items[0].text);
624                 dialog_vars.defaultno = TRUE;
625                 choice = dialog_yesno("Warning", message, 0, 0);
626                 dialog_vars.defaultno = FALSE;
627                 if (choice == 1) /* cancel */
628                         goto editpart;
629         }
630
631         /* Check if the label has a / in it */
632         if (strchr(items[3].text, '/') != NULL) {
633                 dialog_msgbox("Error", "Label contains a /, which is not an "
634                     "allowed character.", 0, 0, TRUE);
635                 goto editpart;
636         }
637
638         r = gctl_get_handle();
639         gctl_ro_param(r, "class", -1, "PART");
640         gctl_ro_param(r, "arg0", -1, geom->lg_name);
641         gctl_ro_param(r, "flags", -1, GPART_FLAGS);
642         gctl_ro_param(r, "verb", -1, "modify");
643         gctl_ro_param(r, "index", sizeof(idx), &idx);
644         if (hadlabel || items[3].text[0] != '\0')
645                 gctl_ro_param(r, "label", -1, items[3].text);
646         gctl_ro_param(r, "type", -1, items[0].text);
647         errstr = gctl_issue(r);
648         if (errstr != NULL && errstr[0] != '\0') {
649                 gpart_show_error("Error", NULL, errstr);
650                 gctl_free(r);
651                 goto editpart;
652         }
653         gctl_free(r);
654
655         newfs_command(items[0].text, newfs, 1);
656         set_default_part_metadata(pp->lg_name, scheme, items[0].text,
657             items[2].text, (strcmp(oldtype, items[0].text) != 0) ?
658             newfs : NULL);
659
660 endedit:
661         if (strcmp(oldtype, items[0].text) != 0 && cp != NULL)
662                 gpart_destroy(cp->lg_geom);
663         if (strcmp(oldtype, items[0].text) != 0 && strcmp(items[0].text,
664             "freebsd") == 0)
665                 gpart_partition(pp->lg_name, "BSD");
666
667         for (i = 0; i < nitems(items); i++)
668                 if (items[i].text_free)
669                         free(items[i].text);
670 }
671
672 void
673 set_default_part_metadata(const char *name, const char *scheme,
674     const char *type, const char *mountpoint, const char *newfs)
675 {
676         struct partition_metadata *md;
677         char *zpool_name = NULL;
678         int i;
679
680         /* Set part metadata */
681         md = get_part_metadata(name, 1);
682
683         if (newfs) {
684                 if (md->newfs != NULL) {
685                         free(md->newfs);
686                         md->newfs = NULL;
687                 }
688
689                 if (newfs != NULL && newfs[0] != '\0') {
690                         md->newfs = malloc(strlen(newfs) + strlen(" /dev/") +
691                             strlen(mountpoint) + 5 + strlen(name) + 1);
692                         if (strcmp("freebsd-zfs", type) == 0) {
693                                 zpool_name = strdup((strlen(mountpoint) == 1) ?
694                                     "root" : &mountpoint[1]);
695                                 for (i = 0; zpool_name[i] != 0; i++)
696                                         if (!isalnum(zpool_name[i]))
697                                                 zpool_name[i] = '_';
698                                 sprintf(md->newfs, "%s %s /dev/%s", newfs,
699                                     zpool_name, name);
700                         } else {
701                                 sprintf(md->newfs, "%s /dev/%s", newfs, name);
702                         }
703                 }
704         }
705
706         if (strcmp(type, "freebsd-swap") == 0)
707                 mountpoint = "none";
708         if (strcmp(type, bootpart_type(scheme)) == 0)
709                 md->bootcode = 1;
710
711         /* VTOC8 needs partcode at the start of partitions */
712         if (strcmp(scheme, "VTOC8") == 0 && (strcmp(type, "freebsd-ufs") == 0
713             || strcmp(type, "freebsd-zfs") == 0))
714                 md->bootcode = 1;
715
716         if (mountpoint == NULL || mountpoint[0] == '\0') {
717                 if (md->fstab != NULL) {
718                         free(md->fstab->fs_spec);
719                         free(md->fstab->fs_file);
720                         free(md->fstab->fs_vfstype);
721                         free(md->fstab->fs_mntops);
722                         free(md->fstab->fs_type);
723                         free(md->fstab);
724                         md->fstab = NULL;
725                 }
726         } else {
727                 if (md->fstab == NULL) {
728                         md->fstab = malloc(sizeof(struct fstab));
729                 } else {
730                         free(md->fstab->fs_spec);
731                         free(md->fstab->fs_file);
732                         free(md->fstab->fs_vfstype);
733                         free(md->fstab->fs_mntops);
734                         free(md->fstab->fs_type);
735                 }
736                 if (strcmp("freebsd-zfs", type) == 0) {
737                         md->fstab->fs_spec = strdup(zpool_name);
738                 } else {
739                         md->fstab->fs_spec = malloc(strlen(name) +
740                             strlen("/dev/") + 1);
741                         sprintf(md->fstab->fs_spec, "/dev/%s", name);
742                 }
743                 md->fstab->fs_file = strdup(mountpoint);
744                 /* Get VFS from text after freebsd-, if possible */
745                 if (strncmp("freebsd-", type, 8) == 0)
746                         md->fstab->fs_vfstype = strdup(&type[8]);
747                 else if (strcmp("fat32", type) == 0 || strcmp("efi", type) == 0)
748                         md->fstab->fs_vfstype = strdup("msdosfs");
749                 else
750                         md->fstab->fs_vfstype = strdup(type); /* Guess */
751                 if (strcmp(type, "freebsd-swap") == 0) {
752                         md->fstab->fs_type = strdup(FSTAB_SW);
753                         md->fstab->fs_freq = 0;
754                         md->fstab->fs_passno = 0;
755                 } else if (strcmp(type, "freebsd-zfs") == 0) {
756                         md->fstab->fs_type = strdup(FSTAB_RW);
757                         md->fstab->fs_freq = 0;
758                         md->fstab->fs_passno = 0;
759                 } else {
760                         md->fstab->fs_type = strdup(FSTAB_RW);
761                         if (strcmp(mountpoint, "/") == 0) {
762                                 md->fstab->fs_freq = 1;
763                                 md->fstab->fs_passno = 1;
764                         } else {
765                                 md->fstab->fs_freq = 2;
766                                 md->fstab->fs_passno = 2;
767                         }
768                 }
769                 md->fstab->fs_mntops = strdup(md->fstab->fs_type);
770         }
771
772         if (zpool_name != NULL)
773                 free(zpool_name);
774 }
775
776 static
777 int part_compare(const void *xa, const void *xb)
778 {
779         struct gprovider **a = (struct gprovider **)xa;
780         struct gprovider **b = (struct gprovider **)xb;
781         intmax_t astart, bstart;
782         struct gconfig *gc;
783         
784         astart = bstart = 0;
785         LIST_FOREACH(gc, &(*a)->lg_config, lg_config)
786                 if (strcmp(gc->lg_name, "start") == 0) {
787                         astart = strtoimax(gc->lg_val, NULL, 0);
788                         break;
789                 }
790         LIST_FOREACH(gc, &(*b)->lg_config, lg_config)
791                 if (strcmp(gc->lg_name, "start") == 0) {
792                         bstart = strtoimax(gc->lg_val, NULL, 0);
793                         break;
794                 }
795
796         if (astart < bstart)
797                 return -1;
798         else if (astart > bstart)
799                 return 1;
800         else
801                 return 0;
802 }
803
804 intmax_t
805 gpart_max_free(struct ggeom *geom, intmax_t *npartstart)
806 {
807         struct gconfig *gc;
808         struct gprovider *pp, **providers;
809         intmax_t sectorsize, stripesize, offset;
810         intmax_t lastend;
811         intmax_t start, end;
812         intmax_t maxsize, maxstart;
813         intmax_t partstart, partend;
814         int i, nparts;
815
816         /* Now get the maximum free size and free start */
817         start = end = 0;
818         LIST_FOREACH(gc, &geom->lg_config, lg_config) {
819                 if (strcmp(gc->lg_name, "first") == 0)
820                         start = strtoimax(gc->lg_val, NULL, 0);
821                 if (strcmp(gc->lg_name, "last") == 0)
822                         end = strtoimax(gc->lg_val, NULL, 0);
823         }
824
825         i = nparts = 0;
826         LIST_FOREACH(pp, &geom->lg_provider, lg_provider)
827                 nparts++;
828         providers = calloc(nparts, sizeof(providers[0]));
829         LIST_FOREACH(pp, &geom->lg_provider, lg_provider)
830                 providers[i++] = pp;
831         qsort(providers, nparts, sizeof(providers[0]), part_compare);
832
833         lastend = start - 1;
834         maxsize = 0;
835         for (i = 0; i < nparts; i++) {
836                 pp = providers[i];
837
838                 LIST_FOREACH(gc, &pp->lg_config, lg_config) {
839                         if (strcmp(gc->lg_name, "start") == 0)
840                                 partstart = strtoimax(gc->lg_val, NULL, 0);
841                         if (strcmp(gc->lg_name, "end") == 0)
842                                 partend = strtoimax(gc->lg_val, NULL, 0);
843                 }
844
845                 if (partstart - lastend > maxsize) {
846                         maxsize = partstart - lastend - 1;
847                         maxstart = lastend + 1;
848                 }
849
850                 lastend = partend;
851         }
852
853         if (end - lastend > maxsize) {
854                 maxsize = end - lastend - 1;
855                 maxstart = lastend + 1;
856         }
857
858         pp = LIST_FIRST(&geom->lg_consumer)->lg_provider;
859
860         /*
861          * Round the start and size of the largest available space up to
862          * the nearest multiple of the adjusted stripe size.
863          *
864          * The adjusted stripe size is the least common multiple of the
865          * actual stripe size, or the sector size if no stripe size was
866          * reported, and 4096.  The reason for this is that contemporary
867          * disks often have 4096-byte physical sectors but report 512
868          * bytes instead for compatibility with older / broken operating
869          * systems and BIOSes.  For the same reasons, virtualized storage
870          * may also report a 512-byte stripe size, or none at all.
871          */
872         sectorsize = pp->lg_sectorsize;
873         if ((stripesize = pp->lg_stripesize) == 0)
874                 stripesize = sectorsize;
875         while (stripesize % 4096 != 0)
876                 stripesize *= 2;
877         if ((offset = maxstart * sectorsize % stripesize) != 0) {
878                 offset = (stripesize - offset) / sectorsize;
879                 maxstart += offset;
880                 maxsize -= offset;
881         }
882
883         if (npartstart != NULL)
884                 *npartstart = maxstart;
885
886         return (maxsize);
887 }
888
889 void
890 gpart_create(struct gprovider *pp, char *default_type, char *default_size,
891      char *default_mountpoint, char **partname, int interactive)
892 {
893         struct gctl_req *r;
894         struct gconfig *gc;
895         struct gconsumer *cp;
896         struct ggeom *geom;
897         const char *errstr, *scheme;
898         char sizestr[32], startstr[32], output[64], *newpartname;
899         char newfs[255], options_fstype[64];
900         intmax_t maxsize, size, sector, firstfree, stripe;
901         uint64_t bytes;
902         int nitems, choice, junk;
903         unsigned i;
904
905         DIALOG_FORMITEM items[] = {
906                 {0, "Type:", 5, 0, 0, FALSE, "freebsd-ufs", 11, 0, 12, 15, 0,
907                     FALSE, "Filesystem type (e.g. freebsd-ufs, freebsd-zfs, "
908                     "freebsd-swap)", FALSE},
909                 {0, "Size:", 5, 1, 0, FALSE, "", 11, 1, 12, 15, 0,
910                     FALSE, "Partition size. Append K, M, G for kilobytes, "
911                     "megabytes or gigabytes.", FALSE},
912                 {0, "Mountpoint:", 11, 2, 0, FALSE, "", 11, 2, 12, 15, 0,
913                     FALSE, "Path at which to mount partition (blank for "
914                     "swap, / for root filesystem)", FALSE},
915                 {0, "Label:", 7, 3, 0, FALSE, "", 11, 3, 12, 15, 0, FALSE,
916                     "Partition name. Not all partition schemes support this.",
917                     FALSE},
918         };
919
920         if (partname != NULL)
921                 *partname = NULL;
922
923         /* Record sector and stripe sizes */
924         sector = pp->lg_sectorsize;
925         stripe = pp->lg_stripesize;
926
927         /*
928          * Find the PART geom we are manipulating. This may be a consumer of
929          * this provider, or its parent. Check the consumer case first.
930          */
931         geom = NULL;
932         LIST_FOREACH(cp, &pp->lg_consumers, lg_consumers)
933                 if (strcmp(cp->lg_geom->lg_class->lg_name, "PART") == 0) {
934                         geom = cp->lg_geom;
935                         break;
936                 }
937
938         if (geom == NULL && strcmp(pp->lg_geom->lg_class->lg_name, "PART") == 0)
939                 geom = pp->lg_geom;
940
941         /* Now get the partition scheme */
942         scheme = NULL;
943         if (geom != NULL) {
944                 LIST_FOREACH(gc, &geom->lg_config, lg_config) 
945                         if (strcmp(gc->lg_name, "scheme") == 0)
946                                 scheme = gc->lg_val;
947         }
948
949         if (geom == NULL || scheme == NULL || strcmp(scheme, "(none)") == 0) {
950                 if (gpart_partition(pp->lg_name, NULL) == 0)
951                         dialog_msgbox("",
952                             "The partition table has been successfully created."
953                             " Please press Create again to create partitions.",
954                             0, 0, TRUE);
955
956                 return;
957         }
958
959         /*
960          * If we still don't have a geom, either the user has
961          * canceled partitioning or there has been an error which has already
962          * been displayed, so bail.
963          */
964         if (geom == NULL)
965                 return;
966
967         maxsize = size = gpart_max_free(geom, &firstfree);
968         if (size <= 0) {
969                 dialog_msgbox("Error", "No free space left on device.", 0, 0,
970                     TRUE);
971                 return;
972         }
973
974         humanize_number(sizestr, 7, size*sector, "B", HN_AUTOSCALE,
975             HN_NOSPACE | HN_DECIMAL);
976         items[1].text = sizestr;
977
978         /* Special-case the MBR default type for nested partitions */
979         if (strcmp(scheme, "MBR") == 0) {
980                 items[0].text = "freebsd";
981                 items[0].help = "Filesystem type (e.g. freebsd, fat32)";
982         }
983
984         nitems = scheme_supports_labels(scheme) ? 4 : 3;
985
986         if (default_type != NULL)
987                 items[0].text = default_type;
988         if (default_size != NULL)
989                 items[1].text = default_size;
990         if (default_mountpoint != NULL)
991                 items[2].text = default_mountpoint;
992
993         /* Default options */
994         strncpy(options_fstype, items[0].text,
995             sizeof(options_fstype));
996         newfs_command(options_fstype, newfs, 1);
997 addpartform:
998         if (interactive) {
999                 dialog_vars.extra_label = "Options";
1000                 dialog_vars.extra_button = TRUE;
1001                 choice = dlg_form("Add Partition", "", 0, 0, 0, nitems,
1002                     items, &junk);
1003                 dialog_vars.extra_button = FALSE;
1004                 switch (choice) {
1005                 case 0: /* OK */
1006                         break;
1007                 case 1: /* Cancel */
1008                         return;
1009                 case 3: /* Options */
1010                         strncpy(options_fstype, items[0].text,
1011                             sizeof(options_fstype));
1012                         newfs_command(options_fstype, newfs, 0);
1013                         goto addpartform;
1014                 }
1015         }
1016
1017         /*
1018          * If the user changed the fs type after specifying options, undo
1019          * their choices in favor of the new filesystem's defaults.
1020          */
1021         if (strcmp(options_fstype, items[0].text) != 0) {
1022                 strncpy(options_fstype, items[0].text, sizeof(options_fstype));
1023                 newfs_command(options_fstype, newfs, 1);
1024         }
1025
1026         size = maxsize;
1027         if (strlen(items[1].text) > 0) {
1028                 if (expand_number(items[1].text, &bytes) != 0) {
1029                         char error[512];
1030
1031                         sprintf(error, "Invalid size: %s\n", strerror(errno));
1032                         dialog_msgbox("Error", error, 0, 0, TRUE);
1033                         goto addpartform;
1034                 }
1035                 size = MIN((intmax_t)(bytes/sector), maxsize);
1036         }
1037
1038         /* Check if the label has a / in it */
1039         if (strchr(items[3].text, '/') != NULL) {
1040                 dialog_msgbox("Error", "Label contains a /, which is not an "
1041                     "allowed character.", 0, 0, TRUE);
1042                 goto addpartform;
1043         }
1044
1045         /* Warn if no mountpoint set */
1046         if (strcmp(items[0].text, "freebsd-ufs") == 0 &&
1047             items[2].text[0] != '/') {
1048                 choice = 0;
1049                 if (interactive) {
1050                         dialog_vars.defaultno = TRUE;
1051                         choice = dialog_yesno("Warning",
1052                             "This partition does not have a valid mountpoint "
1053                             "(for the partition from which you intend to boot the "
1054                             "operating system, the mountpoint should be /). Are you "
1055                             "sure you want to continue?"
1056                         , 0, 0);
1057                         dialog_vars.defaultno = FALSE;
1058                 }
1059                 if (choice == 1) /* cancel */
1060                         goto addpartform;
1061         }
1062
1063         /*
1064          * Error if this scheme needs nested partitions, this is one, and
1065          * a mountpoint was set.
1066          */
1067         if (strcmp(items[0].text, "freebsd") == 0 &&
1068             strlen(items[2].text) > 0) {
1069                 dialog_msgbox("Error", "Partitions of type \"freebsd\" are "
1070                     "nested BSD-type partition schemes and cannot have "
1071                     "mountpoints. After creating one, select it and press "
1072                     "Create again to add the actual file systems.", 0, 0, TRUE);
1073                 goto addpartform;
1074         }
1075
1076         /* If this is the root partition, check that this scheme is bootable */
1077         if (strcmp(items[2].text, "/") == 0 && !is_scheme_bootable(scheme)) {
1078                 char message[512];
1079                 sprintf(message, "This partition scheme (%s) is not bootable "
1080                     "on this platform. Are you sure you want to proceed?",
1081                     scheme);
1082                 dialog_vars.defaultno = TRUE;
1083                 choice = dialog_yesno("Warning", message, 0, 0);
1084                 dialog_vars.defaultno = FALSE;
1085                 if (choice == 1) /* cancel */
1086                         goto addpartform;
1087         }
1088
1089         /* If this is the root partition, check that this fs is bootable */
1090         if (strcmp(items[2].text, "/") == 0 && !is_fs_bootable(scheme,
1091             items[0].text)) {
1092                 char message[512];
1093                 sprintf(message, "This file system (%s) is not bootable "
1094                     "on this system. Are you sure you want to proceed?",
1095                     items[0].text);
1096                 dialog_vars.defaultno = TRUE;
1097                 choice = dialog_yesno("Warning", message, 0, 0);
1098                 dialog_vars.defaultno = FALSE;
1099                 if (choice == 1) /* cancel */
1100                         goto addpartform;
1101         }
1102
1103         /*
1104          * If this is the root partition, and we need a boot partition, ask
1105          * the user to add one.
1106          */
1107
1108         /* Check for existing freebsd-boot partition */
1109         LIST_FOREACH(pp, &geom->lg_provider, lg_provider) {
1110                 struct partition_metadata *md;
1111                 md = get_part_metadata(pp->lg_name, 0);
1112                 if (md == NULL || !md->bootcode)
1113                         continue;
1114                 LIST_FOREACH(gc, &pp->lg_config, lg_config)
1115                         if (strcmp(gc->lg_name, "type") == 0)
1116                                 break;
1117                 if (gc != NULL && strcmp(gc->lg_val,
1118                     bootpart_type(scheme)) == 0)
1119                         break;
1120         }
1121
1122         /* If there isn't one, and we need one, ask */
1123         if ((strcmp(items[0].text, "freebsd") == 0 ||
1124             strcmp(items[2].text, "/") == 0) && bootpart_size(scheme) > 0 &&
1125             pp == NULL) {
1126                 if (interactive)
1127                         choice = dialog_yesno("Boot Partition",
1128                             "This partition scheme requires a boot partition "
1129                             "for the disk to be bootable. Would you like to "
1130                             "make one now?", 0, 0);
1131                 else
1132                         choice = 0;
1133
1134                 if (choice == 0) { /* yes */
1135                         r = gctl_get_handle();
1136                         gctl_ro_param(r, "class", -1, "PART");
1137                         gctl_ro_param(r, "arg0", -1, geom->lg_name);
1138                         gctl_ro_param(r, "flags", -1, GPART_FLAGS);
1139                         gctl_ro_param(r, "verb", -1, "add");
1140                         gctl_ro_param(r, "type", -1, bootpart_type(scheme));
1141                         snprintf(sizestr, sizeof(sizestr), "%jd",
1142                             bootpart_size(scheme) / sector);
1143                         gctl_ro_param(r, "size", -1, sizestr);
1144                         snprintf(startstr, sizeof(startstr), "%jd", firstfree);
1145                         gctl_ro_param(r, "start", -1, startstr);
1146                         gctl_rw_param(r, "output", sizeof(output), output);
1147                         errstr = gctl_issue(r);
1148                         if (errstr != NULL && errstr[0] != '\0') 
1149                                 gpart_show_error("Error", NULL, errstr);
1150                         gctl_free(r);
1151
1152                         get_part_metadata(strtok(output, " "), 1)->bootcode = 1;
1153
1154                         /* Now adjust the part we are really adding forward */
1155                         firstfree += bootpart_size(scheme) / sector;
1156                         size -= (bootpart_size(scheme) + stripe)/sector;
1157                         if (stripe > 0 && (firstfree*sector % stripe) != 0) 
1158                                 firstfree += (stripe - ((firstfree*sector) %
1159                                     stripe)) / sector;
1160                 }
1161         }
1162         
1163         r = gctl_get_handle();
1164         gctl_ro_param(r, "class", -1, "PART");
1165         gctl_ro_param(r, "arg0", -1, geom->lg_name);
1166         gctl_ro_param(r, "flags", -1, GPART_FLAGS);
1167         gctl_ro_param(r, "verb", -1, "add");
1168
1169         gctl_ro_param(r, "type", -1, items[0].text);
1170         snprintf(sizestr, sizeof(sizestr), "%jd", size);
1171         gctl_ro_param(r, "size", -1, sizestr);
1172         snprintf(startstr, sizeof(startstr), "%jd", firstfree);
1173         gctl_ro_param(r, "start", -1, startstr);
1174         if (items[3].text[0] != '\0')
1175                 gctl_ro_param(r, "label", -1, items[3].text);
1176         gctl_rw_param(r, "output", sizeof(output), output);
1177         errstr = gctl_issue(r);
1178         if (errstr != NULL && errstr[0] != '\0') {
1179                 gpart_show_error("Error", NULL, errstr);
1180                 gctl_free(r);
1181                 goto addpartform;
1182         }
1183         newpartname = strtok(output, " ");
1184         gctl_free(r);
1185
1186         /*
1187          * Try to destroy any geom that gpart picked up already here from
1188          * dirty blocks.
1189          */
1190         r = gctl_get_handle();
1191         gctl_ro_param(r, "class", -1, "PART");
1192         gctl_ro_param(r, "arg0", -1, newpartname);
1193         gctl_ro_param(r, "flags", -1, GPART_FLAGS);
1194         junk = 1;
1195         gctl_ro_param(r, "force", sizeof(junk), &junk);
1196         gctl_ro_param(r, "verb", -1, "destroy");
1197         gctl_issue(r); /* Error usually expected and non-fatal */
1198         gctl_free(r);
1199
1200         if (strcmp(items[0].text, bootpart_type(scheme)) == 0)
1201                 get_part_metadata(newpartname, 1)->bootcode = 1;
1202         else if (strcmp(items[0].text, "freebsd") == 0)
1203                 gpart_partition(newpartname, "BSD");
1204         else
1205                 set_default_part_metadata(newpartname, scheme,
1206                     items[0].text, items[2].text, newfs);
1207
1208         for (i = 0; i < nitems(items); i++)
1209                 if (items[i].text_free)
1210                         free(items[i].text);
1211
1212         if (partname != NULL)
1213                 *partname = strdup(newpartname);
1214 }
1215         
1216 void
1217 gpart_delete(struct gprovider *pp)
1218 {
1219         struct gconfig *gc;
1220         struct ggeom *geom;
1221         struct gconsumer *cp;
1222         struct gctl_req *r;
1223         const char *errstr;
1224         intmax_t idx;
1225         int is_partition;
1226
1227         /* Is it a partition? */
1228         is_partition = (strcmp(pp->lg_geom->lg_class->lg_name, "PART") == 0);
1229
1230         /* Find out if this is the root of a gpart geom */
1231         geom = NULL;
1232         LIST_FOREACH(cp, &pp->lg_consumers, lg_consumers)
1233                 if (strcmp(cp->lg_geom->lg_class->lg_name, "PART") == 0) {
1234                         geom = cp->lg_geom;
1235                         break;
1236                 }
1237
1238         /* If so, destroy all children */
1239         if (geom != NULL) {
1240                 gpart_destroy(geom);
1241
1242                 /* If this is a partition, revert it, so it can be deleted */
1243                 if (is_partition) {
1244                         r = gctl_get_handle();
1245                         gctl_ro_param(r, "class", -1, "PART");
1246                         gctl_ro_param(r, "arg0", -1, geom->lg_name);
1247                         gctl_ro_param(r, "verb", -1, "undo");
1248                         gctl_issue(r); /* Ignore non-fatal errors */
1249                         gctl_free(r);
1250                 }
1251         }
1252
1253         /*
1254          * If this is not a partition, see if that is a problem, complain if
1255          * necessary, and return always, since we need not do anything further,
1256          * error or no.
1257          */
1258         if (!is_partition) {
1259                 if (geom == NULL)
1260                         dialog_msgbox("Error",
1261                             "Only partitions can be deleted.", 0, 0, TRUE);
1262                 return;
1263         }
1264
1265         r = gctl_get_handle();
1266         gctl_ro_param(r, "class", -1, pp->lg_geom->lg_class->lg_name);
1267         gctl_ro_param(r, "arg0", -1, pp->lg_geom->lg_name);
1268         gctl_ro_param(r, "flags", -1, GPART_FLAGS);
1269         gctl_ro_param(r, "verb", -1, "delete");
1270
1271         LIST_FOREACH(gc, &pp->lg_config, lg_config) {
1272                 if (strcmp(gc->lg_name, "index") == 0) {
1273                         idx = atoi(gc->lg_val);
1274                         gctl_ro_param(r, "index", sizeof(idx), &idx);
1275                         break;
1276                 }
1277         }
1278
1279         errstr = gctl_issue(r);
1280         if (errstr != NULL && errstr[0] != '\0') {
1281                 gpart_show_error("Error", NULL, errstr);
1282                 gctl_free(r);
1283                 return;
1284         }
1285
1286         gctl_free(r);
1287
1288         delete_part_metadata(pp->lg_name);
1289 }
1290
1291 void
1292 gpart_revert_all(struct gmesh *mesh)
1293 {
1294         struct gclass *classp;
1295         struct gconfig *gc;
1296         struct ggeom *gp;
1297         struct gctl_req *r;
1298         const char *modified;
1299
1300         LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
1301                 if (strcmp(classp->lg_name, "PART") == 0)
1302                         break;
1303         }
1304
1305         if (strcmp(classp->lg_name, "PART") != 0) {
1306                 dialog_msgbox("Error", "gpart not found!", 0, 0, TRUE);
1307                 return;
1308         }
1309
1310         LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
1311                 modified = "true"; /* XXX: If we don't know (kernel too old),
1312                                     * assume there are modifications. */
1313                 LIST_FOREACH(gc, &gp->lg_config, lg_config) {
1314                         if (strcmp(gc->lg_name, "modified") == 0) {
1315                                 modified = gc->lg_val;
1316                                 break;
1317                         }
1318                 }
1319
1320                 if (strcmp(modified, "false") == 0)
1321                         continue;
1322
1323                 r = gctl_get_handle();
1324                 gctl_ro_param(r, "class", -1, "PART");
1325                 gctl_ro_param(r, "arg0", -1, gp->lg_name);
1326                 gctl_ro_param(r, "verb", -1, "undo");
1327
1328                 gctl_issue(r);
1329                 gctl_free(r);
1330         }
1331 }
1332
1333 void
1334 gpart_commit(struct gmesh *mesh)
1335 {
1336         struct partition_metadata *md;
1337         struct gclass *classp;
1338         struct ggeom *gp;
1339         struct gconfig *gc;
1340         struct gconsumer *cp;
1341         struct gprovider *pp;
1342         struct gctl_req *r;
1343         const char *errstr;
1344         const char *modified;
1345         const char *rootfs;
1346
1347         LIST_FOREACH(classp, &mesh->lg_class, lg_class) {
1348                 if (strcmp(classp->lg_name, "PART") == 0)
1349                         break;
1350         }
1351
1352         /* Figure out what filesystem / uses */
1353         rootfs = "ufs"; /* Assume ufs if nothing else present */
1354         TAILQ_FOREACH(md, &part_metadata, metadata) {
1355                 if (md->fstab != NULL && strcmp(md->fstab->fs_file, "/") == 0) {
1356                         rootfs = md->fstab->fs_vfstype;
1357                         break;
1358                 }
1359         }
1360
1361         if (strcmp(classp->lg_name, "PART") != 0) {
1362                 dialog_msgbox("Error", "gpart not found!", 0, 0, TRUE);
1363                 return;
1364         }
1365
1366         LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
1367                 modified = "true"; /* XXX: If we don't know (kernel too old),
1368                                     * assume there are modifications. */
1369                 LIST_FOREACH(gc, &gp->lg_config, lg_config) {
1370                         if (strcmp(gc->lg_name, "modified") == 0) {
1371                                 modified = gc->lg_val;
1372                                 break;
1373                         }
1374                 }
1375
1376                 if (strcmp(modified, "false") == 0)
1377                         continue;
1378
1379                 /* Add bootcode if necessary, before the commit */
1380                 md = get_part_metadata(gp->lg_name, 0);
1381                 if (md != NULL && md->bootcode)
1382                         gpart_bootcode(gp);
1383
1384                 /* Now install partcode on its partitions, if necessary */
1385                 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
1386                         md = get_part_metadata(pp->lg_name, 0);
1387                         if (md == NULL || !md->bootcode)
1388                                 continue;
1389                 
1390                         /* Mark this partition active if that's required */
1391                         gpart_activate(pp);
1392
1393                         /* Check if the partition has sub-partitions */
1394                         LIST_FOREACH(cp, &pp->lg_consumers, lg_consumers)
1395                                 if (strcmp(cp->lg_geom->lg_class->lg_name,
1396                                     "PART") == 0)
1397                                         break;
1398
1399                         if (cp == NULL) /* No sub-partitions */
1400                                 gpart_partcode(pp, rootfs);
1401                 }
1402
1403                 r = gctl_get_handle();
1404                 gctl_ro_param(r, "class", -1, "PART");
1405                 gctl_ro_param(r, "arg0", -1, gp->lg_name);
1406                 gctl_ro_param(r, "verb", -1, "commit");
1407
1408                 errstr = gctl_issue(r);
1409                 if (errstr != NULL && errstr[0] != '\0') 
1410                         gpart_show_error("Error", NULL, errstr);
1411                 gctl_free(r);
1412         }
1413 }
1414