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/sysctl.h> 33 #include <errno.h> 34 #include <inttypes.h> 35 #include <libutil.h> 36 #include <string.h> 37 38 #include <libgeom.h> 39 #include <dialog.h> 40 #include <dlg_keys.h> 41 42 #include "partedit.h" 43 44 #define MIN_FREE_SPACE (1024*1024*1024) /* 1 GB */ 45 #define SWAP_SIZE(available) MIN(available/20, 4*1024*1024*1024LL) 46 47 static char *wizard_partition(struct gmesh *mesh, const char *disk); 48 49 int 50 part_wizard(const char *fsreq) 51 { 52 char *disk, *schemeroot; 53 const char *fstype; 54 struct gmesh mesh; 55 int error; 56 57 if (fsreq != NULL) 58 fstype = fsreq; 59 else 60 fstype = "ufs"; 61 62 startwizard: 63 error = geom_gettree(&mesh); 64 65 dlg_put_backtitle(); 66 error = geom_gettree(&mesh); 67 disk = boot_disk_select(&mesh); 68 if (disk == NULL) 69 return (1); 70 71 dlg_clear(); 72 dlg_put_backtitle(); 73 schemeroot = wizard_partition(&mesh, disk); 74 free(disk); 75 if (schemeroot == NULL) 76 return (1); 77 78 geom_deletetree(&mesh); 79 dlg_clear(); 80 dlg_put_backtitle(); 81 error = geom_gettree(&mesh); 82 83 error = wizard_makeparts(&mesh, schemeroot, fstype, 1); 84 if (error) 85 goto startwizard; 86 free(schemeroot); 87 88 geom_deletetree(&mesh); 89 90 return (0); 91 } 92 93 char * 94 boot_disk_select(struct gmesh *mesh) 95 { 96 struct gclass *classp; 97 struct gconfig *gc; 98 struct ggeom *gp; 99 struct gprovider *pp; 100 DIALOG_LISTITEM *disks = NULL; 101 const char *type, *desc; 102 char diskdesc[512]; 103 char *chosen; 104 int i, err, selected, n = 0; 105 106 LIST_FOREACH(classp, &mesh->lg_class, lg_class) { 107 if (strcmp(classp->lg_name, "DISK") != 0 && 108 strcmp(classp->lg_name, "RAID") != 0 && 109 strcmp(classp->lg_name, "MD") != 0) 110 continue; 111 112 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) { 113 if (LIST_EMPTY(&gp->lg_provider)) 114 continue; 115 116 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) { 117 desc = type = NULL; 118 LIST_FOREACH(gc, &pp->lg_config, lg_config) { 119 if (strcmp(gc->lg_name, "type") == 0) 120 type = gc->lg_val; 121 if (strcmp(gc->lg_name, "descr") == 0) 122 desc = gc->lg_val; 123 } 124 125 /* Skip swap-backed md and WORM devices */ 126 if (strcmp(classp->lg_name, "MD") == 0 && 127 type != NULL && strcmp(type, "swap") == 0) 128 continue; 129 if (strncmp(pp->lg_name, "cd", 2) == 0) 130 continue; 131 132 disks = realloc(disks, (++n)*sizeof(disks[0])); 133 disks[n-1].name = pp->lg_name; 134 humanize_number(diskdesc, 7, pp->lg_mediasize, 135 "B", HN_AUTOSCALE, HN_DECIMAL); 136 if (strncmp(pp->lg_name, "ad", 2) == 0) 137 strcat(diskdesc, " ATA Hard Disk"); 138 else if (strncmp(pp->lg_name, "md", 2) == 0) 139 strcat(diskdesc, " Memory Disk"); 140 else 141 strcat(diskdesc, " Disk"); 142 143 if (desc != NULL) 144 snprintf(diskdesc, sizeof(diskdesc), 145 "%s <%s>", diskdesc, desc); 146 147 disks[n-1].text = strdup(diskdesc); 148 disks[n-1].help = NULL; 149 disks[n-1].state = 0; 150 } 151 } 152 } 153 154 if (n > 1) { 155 err = dlg_menu("Partitioning", 156 "Select the disk on which to install FreeBSD.", 0, 0, 0, 157 n, disks, &selected, NULL); 158 159 chosen = (err == 0) ? strdup(disks[selected].name) : NULL; 160 } else if (n == 1) { 161 chosen = strdup(disks[0].name); 162 } else { 163 chosen = NULL; 164 } 165 166 for (i = 0; i < n; i++) 167 free(disks[i].text); 168 169 return (chosen); 170 } 171 172 static struct gprovider * 173 provider_for_name(struct gmesh *mesh, const char *name) 174 { 175 struct gclass *classp; 176 struct gprovider *pp = NULL; 177 struct ggeom *gp; 178 179 LIST_FOREACH(classp, &mesh->lg_class, lg_class) { 180 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) { 181 if (LIST_EMPTY(&gp->lg_provider)) 182 continue; 183 184 LIST_FOREACH(pp, &gp->lg_provider, lg_provider) 185 if (strcmp(pp->lg_name, name) == 0) 186 break; 187 188 if (pp != NULL) break; 189 } 190 191 if (pp != NULL) break; 192 } 193 194 return (pp); 195 } 196 197 static char * 198 wizard_partition(struct gmesh *mesh, const char *disk) 199 { 200 struct gclass *classp; 201 struct ggeom *gpart = NULL; 202 struct gconfig *gc; 203 char *retval = NULL; 204 const char *scheme = NULL; 205 char message[512]; 206 int choice; 207 208 LIST_FOREACH(classp, &mesh->lg_class, lg_class) 209 if (strcmp(classp->lg_name, "PART") == 0) 210 break; 211 212 if (classp != NULL) { 213 LIST_FOREACH(gpart, &classp->lg_geom, lg_geom) 214 if (strcmp(gpart->lg_name, disk) == 0) 215 break; 216 } 217 218 if (gpart != NULL) { 219 LIST_FOREACH(gc, &gpart->lg_config, lg_config) { 220 if (strcmp(gc->lg_name, "scheme") == 0) { 221 scheme = gc->lg_val; 222 break; 223 } 224 } 225 } 226 227 /* Treat uncommitted scheme deletions as no scheme */ 228 if (scheme != NULL && strcmp(scheme, "(none)") == 0) 229 scheme = NULL; 230 231 query: 232 dialog_vars.yes_label = "Entire Disk"; 233 dialog_vars.no_label = "Partition"; 234 if (gpart != NULL) 235 dialog_vars.defaultno = TRUE; 236 237 snprintf(message, sizeof(message), "Would you like to use this entire " 238 "disk (%s) for FreeBSD or partition it to share it with other " 239 "operating systems? Using the entire disk will erase any data " 240 "currently stored there.", disk); 241 choice = dialog_yesno("Partition", message, 0, 0); 242 243 dialog_vars.yes_label = NULL; 244 dialog_vars.no_label = NULL; 245 dialog_vars.defaultno = FALSE; 246 247 if (choice == 1 && scheme != NULL && !is_scheme_bootable(scheme)) { 248 char warning[512]; 249 int subchoice; 250 251 sprintf(warning, "The existing partition scheme on this " 252 "disk (%s) is not bootable on this platform. To install " 253 "FreeBSD, it must be repartitioned. This will destroy all " 254 "data on the disk. Are you sure you want to proceed?", 255 scheme); 256 subchoice = dialog_yesno("Non-bootable Disk", warning, 0, 0); 257 if (subchoice != 0) 258 goto query; 259 260 gpart_destroy(gpart); 261 scheme = choose_part_type(default_scheme()); 262 if (scheme == NULL) 263 return NULL; 264 gpart_partition(disk, scheme); 265 } 266 267 if (scheme == NULL || choice == 0) { 268 if (gpart != NULL && scheme != NULL) { 269 /* Erase partitioned disk */ 270 choice = dialog_yesno("Confirmation", "This will erase " 271 "the disk. Are you sure you want to proceed?", 0, 0); 272 if (choice != 0) 273 goto query; 274 275 gpart_destroy(gpart); 276 } 277 278 scheme = choose_part_type(default_scheme()); 279 if (scheme == NULL) 280 return NULL; 281 gpart_partition(disk, scheme); 282 } 283 284 if (strcmp(scheme, "MBR") == 0) { 285 struct gmesh submesh; 286 geom_gettree(&submesh); 287 gpart_create(provider_for_name(&submesh, disk), 288 "freebsd", NULL, NULL, &retval, 289 choice /* Non-interactive for "Entire Disk" */); 290 geom_deletetree(&submesh); 291 } else { 292 retval = strdup(disk); 293 } 294 295 return (retval); 296 } 297 298 int 299 wizard_makeparts(struct gmesh *mesh, const char *disk, const char *fstype, 300 int interactive) 301 { 302 struct gclass *classp; 303 struct ggeom *gp; 304 struct gprovider *pp; 305 char *fsnames[] = {"freebsd-ufs", "freebsd-zfs"}; 306 char *fsname; 307 struct gmesh submesh; 308 char swapsizestr[10], rootsizestr[10]; 309 intmax_t swapsize, available; 310 int retval; 311 312 if (strcmp(fstype, "zfs") == 0) { 313 fsname = fsnames[1]; 314 } else { 315 /* default to UFS */ 316 fsname = fsnames[0]; 317 } 318 319 LIST_FOREACH(classp, &mesh->lg_class, lg_class) 320 if (strcmp(classp->lg_name, "PART") == 0) 321 break; 322 323 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) 324 if (strcmp(gp->lg_name, disk) == 0) 325 break; 326 327 pp = provider_for_name(mesh, disk); 328 329 available = gpart_max_free(gp, NULL)*pp->lg_sectorsize; 330 if (interactive && available < MIN_FREE_SPACE) { 331 char availablestr[10], neededstr[10], message[512]; 332 humanize_number(availablestr, 7, available, "B", HN_AUTOSCALE, 333 HN_DECIMAL); 334 humanize_number(neededstr, 7, MIN_FREE_SPACE, "B", HN_AUTOSCALE, 335 HN_DECIMAL); 336 sprintf(message, "There is not enough free space on %s to " 337 "install FreeBSD (%s free, %s required). Would you like " 338 "to choose another disk or to open the partition editor?", 339 disk, availablestr, neededstr); 340 341 dialog_vars.yes_label = "Another Disk"; 342 dialog_vars.no_label = "Editor"; 343 retval = dialog_yesno("Warning", message, 0, 0); 344 dialog_vars.yes_label = NULL; 345 dialog_vars.no_label = NULL; 346 347 return (!retval); /* Editor -> return 0 */ 348 } 349 350 swapsize = SWAP_SIZE(available); 351 humanize_number(swapsizestr, 7, swapsize, "B", HN_AUTOSCALE, 352 HN_NOSPACE | HN_DECIMAL); 353 humanize_number(rootsizestr, 7, available - swapsize - 1024*1024, 354 "B", HN_AUTOSCALE, HN_NOSPACE | HN_DECIMAL); 355 356 geom_gettree(&submesh); 357 pp = provider_for_name(&submesh, disk); 358 gpart_create(pp, fsname, rootsizestr, "/", NULL, 0); 359 geom_deletetree(&submesh); 360 361 geom_gettree(&submesh); 362 pp = provider_for_name(&submesh, disk); 363 gpart_create(pp, "freebsd-swap", swapsizestr, NULL, NULL, 0); 364 geom_deletetree(&submesh); 365 366 return (0); 367 } 368