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, OSNAME " 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, OSNAME " 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, OSNAME " 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 snprintf(warning, sizeof(warning), 281 "The existing partition scheme on this " 282 "disk (%s) is not bootable on this platform. To install " 283 OSNAME ", it must be repartitioned. This will destroy all " 284 "data on the disk. Are you sure you want to proceed?", 285 scheme); 286 conf.title = "Non-bootable Disk"; 287 subchoice = bsddialog_yesno(&conf, warning, 0, 0); 288 if (subchoice != BSDDIALOG_YES) 289 goto query; 290 291 gpart_destroy(gpart); 292 scheme = choose_part_type(default_scheme()); 293 if (scheme == NULL) 294 return NULL; 295 gpart_partition(disk, scheme); 296 } 297 298 if (scheme == NULL || choice == 0) { 299 if (gpart != NULL && scheme != NULL) { 300 /* Erase partitioned disk */ 301 conf.title = "Confirmation"; 302 choice = bsddialog_yesno(&conf, "This will erase " 303 "the disk. Are you sure you want to proceed?", 0, 0); 304 if (choice != BSDDIALOG_YES) 305 goto query; 306 307 gpart_destroy(gpart); 308 } 309 310 scheme = choose_part_type(default_scheme()); 311 if (scheme == NULL) 312 return NULL; 313 gpart_partition(disk, scheme); 314 } 315 316 if (strcmp(scheme, "MBR") == 0) { 317 struct gmesh submesh; 318 319 if (geom_gettree(&submesh) == 0) { 320 gpart_create(provider_for_name(&submesh, disk), 321 "freebsd", NULL, NULL, &retval, 322 choice /* Non-interactive for "Entire Disk" */); 323 geom_deletetree(&submesh); 324 } 325 } else { 326 retval = strdup(disk); 327 } 328 329 return (retval); 330 } 331 332 int 333 wizard_makeparts(struct gmesh *mesh, const char *disk, const char *fstype, 334 int interactive) 335 { 336 struct gclass *classp; 337 struct ggeom *gp; 338 struct gprovider *pp; 339 char *fsnames[] = {"freebsd-ufs", "freebsd-zfs"}; 340 char *fsname; 341 struct gmesh submesh; 342 char swapsizestr[10], rootsizestr[10]; 343 intmax_t swapsize, available; 344 int error, retval; 345 struct bsddialog_conf conf; 346 347 if (strcmp(fstype, "zfs") == 0) { 348 fsname = fsnames[1]; 349 } else { 350 /* default to UFS */ 351 fsname = fsnames[0]; 352 } 353 354 LIST_FOREACH(classp, &mesh->lg_class, lg_class) 355 if (strcmp(classp->lg_name, "PART") == 0) 356 break; 357 358 LIST_FOREACH(gp, &classp->lg_geom, lg_geom) 359 if (strcmp(gp->lg_name, disk) == 0) 360 break; 361 362 pp = provider_for_name(mesh, disk); 363 364 available = gpart_max_free(gp, NULL)*pp->lg_sectorsize; 365 if (interactive && available < MIN_FREE_SPACE) { 366 char availablestr[10], neededstr[10], message[512]; 367 humanize_number(availablestr, 7, available, "B", HN_AUTOSCALE, 368 HN_DECIMAL); 369 humanize_number(neededstr, 7, MIN_FREE_SPACE, "B", HN_AUTOSCALE, 370 HN_DECIMAL); 371 snprintf(message, sizeof(message), 372 "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