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