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