2 * Copyright (c)2004 The DragonFly Project. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
16 * Neither the name of the DragonFly Project nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31 * OF THE POSSIBILITY OF SUCH DAMAGE.
36 * Disk functions for installer.
37 * $Id: fn_disk.c,v 1.40 2005/03/13 01:53:58 cpressey Exp $
45 #define _(String) gettext (String)
47 #define _(String) (String)
50 #include "libaura/mem.h"
51 #include "libaura/fspred.h"
53 #include "libdfui/dfui.h"
54 #include "libdfui/system.h"
56 #include "libinstaller/commands.h"
57 #include "libinstaller/diskutil.h"
58 #include "libinstaller/functions.h"
59 #include "libinstaller/uiutil.h"
62 #include "pathnames.h"
64 /*** DISK-RELATED FUNCTIONS ***/
67 * Ask the user which physical disk they want.
68 * Changes ss->selected_disk if successful.
71 fn_select_disk(struct i_fn_args *a)
74 struct dfui_action *k;
75 struct dfui_response *r;
85 "p", "special", "dfinstaller_select_disk",
90 for (d = storage_disk_first(a->s); d != NULL; d = disk_next(d)) {
91 dfui_form_action_add(f, disk_get_device_name(d),
92 dfui_info_new(disk_get_desc(d), "", ""));
95 k = dfui_form_action_add(f, "cancel",
96 dfui_info_new(a->cancel_desc, "", ""));
97 dfui_action_property_set(k, "accelerator", "ESC");
99 if (!dfui_be_present(a->c, f, &r))
102 if (strcmp(dfui_response_get_action_id(r), "cancel") == 0) {
105 d = disk_find(a->s, dfui_response_get_action_id(r));
107 inform(a->c, _("Internal error - response from frontend "
108 "should be a valid device name."));
111 storage_set_selected_disk(a->s, d);
117 dfui_response_free(r);
121 * Ask the user which slice on a the selected disk they want.
122 * Changes ss->selected_slice.
125 fn_select_slice(struct i_fn_args *a)
128 struct dfui_action *k;
129 struct dfui_response *r;
133 f = dfui_form_create(
135 _("Select Primary Partition"),
140 "p", "special", "dfinstaller_select_slice",
145 for (s = disk_slice_first(storage_get_selected_disk(a->s));
146 s != NULL; s = slice_next(s)) {
147 snprintf(string, 16, "%d", slice_get_number(s));
148 dfui_form_action_add(f, string,
149 dfui_info_new(slice_get_desc(s), "", ""));
152 k = dfui_form_action_add(f, "cancel",
153 dfui_info_new(a->cancel_desc, "", ""));
154 dfui_action_property_set(k, "accelerator", "ESC");
156 if (!dfui_be_present(a->c, f, &r))
159 if (strcmp(dfui_response_get_action_id(r), "cancel") == 0) {
162 s = slice_find(storage_get_selected_disk(a->s),
163 atoi(dfui_response_get_action_id(r)));
165 inform(a->c, _("Internal error - response from frontend "
166 "should be a valid slice number."));
169 storage_set_selected_slice(a->s, s);
175 dfui_response_free(r);
179 * If ss->selected_disk == NULL, user will be asked for which disk.
180 * Returns 1 if disk was formatted, 0 if it wasn't.
181 * If it was, ss->selected_disk and ss->selected_slice are set to it.
184 fn_format_disk(struct i_fn_args *a)
186 struct commands *cmds;
187 char *selected_disk_string;
189 if (storage_get_selected_disk(a->s) == NULL) {
190 a->short_desc = _("Select a disk to format.");
191 a->cancel_desc = _("Return to Utilities Menu");
193 if (!a->result || storage_get_selected_disk(a->s) == NULL) {
199 if (confirm_dangerous_action(a->c,
200 _("WARNING! ALL data in ALL partitions on the disk\n\n"
201 "%s\n\nwill be IRREVOCABLY ERASED!\n\nAre you ABSOLUTELY "
202 "SURE you wish to take this action? This is your "
203 "LAST CHANCE to cancel!"), disk_get_desc(storage_get_selected_disk(a->s)))) {
204 cmds = commands_new();
206 command_add(cmds, "%s%s -BI %s",
207 a->os_root, cmd_name(a, "FDISK"),
208 disk_get_device_name(storage_get_selected_disk(a->s)));
210 if (!commands_execute(a, cmds)) {
211 inform(a->c, _("The disk\n\n%s\n\nwas "
212 "not correctly formatted, and may "
213 "now be in an inconsistent state. "
214 "We recommend re-formatting it "
215 "before attempting to install "
217 disk_get_desc(storage_get_selected_disk(a->s)),
218 OPERATING_SYSTEM_NAME);
226 * Since one of the disks has now changed, we must
227 * refresh our view of them and re-select the disk
228 * since the selected_disk pointer will be invalidated.
230 selected_disk_string = aura_strdup(
231 disk_get_device_name(storage_get_selected_disk(a->s)));
232 if (!survey_storage(a)) {
233 inform(a->c, _("Errors occurred while probing "
234 "the system for its storage capabilities."));
236 storage_set_selected_disk(a->s, disk_find(a->s, selected_disk_string));
237 free(selected_disk_string);
240 * Note that we formatted this disk and that we want
241 * to use the first (and only) slice of it.
243 disk_set_formatted(storage_get_selected_disk(a->s), 1);
244 storage_set_selected_slice(a->s, disk_slice_first(storage_get_selected_disk(a->s)));
246 if (!format_slice(a)) {
247 inform(a->c, _("The sole primary partition of "
248 "the disk\n\n%s\n\nwas "
249 "not correctly formatted, and may "
250 "now be in an inconsistent state. "
251 "We recommend re-formatting the "
252 "disk before attempting to install "
254 disk_get_desc(storage_get_selected_disk(a->s)),
255 OPERATING_SYSTEM_NAME);
260 inform(a->c, _("The disk\n\n%s\n\nwas formatted."),
261 disk_get_desc(storage_get_selected_disk(a->s)));
264 inform(a->c, _("Action cancelled - no disks were formatted."));
270 * Wipes the start of the selected disk.
273 fn_wipe_start_of_disk(struct i_fn_args *a)
275 struct commands *cmds;
277 a->short_desc = _("If you are having problems formatting a disk, "
278 "it may be because of junk that has accumulated "
279 "in the boot block and the partition table. "
280 "A cure for this is to wipe out everything on "
281 "the first few sectors of the disk. However, this "
282 "is a rather drastic action to take, so it is not "
283 "recommended unless you are otherwise "
284 "encountering problems.");
285 a->cancel_desc = _("Return to Utilities Menu");
290 /* XXX check to make sure no slices on this disk are mounted first? */
291 if (storage_get_selected_disk(a->s) != NULL && confirm_dangerous_action(a->c,
292 _("WARNING! ALL data in ALL partitions on the disk\n\n"
293 "%s\n\nwill be IRREVOCABLY ERASED!\n\nAre you ABSOLUTELY "
294 "SURE you wish to take this action? This is your "
295 "LAST CHANCE to cancel!"), disk_get_desc(storage_get_selected_disk(a->s)))) {
296 cmds = commands_new();
298 "%s%s if=%sdev/zero of=%sdev/%s bs=32k count=16",
299 a->os_root, cmd_name(a, "DD"),
300 a->os_root, a->os_root,
301 disk_get_device_name(storage_get_selected_disk(a->s)));
302 if (commands_execute(a, cmds)) {
303 inform(a->c, _("Start of disk was successfully wiped."));
305 inform(a->c, _("Some errors occurred. "
306 "Start of disk was not successfully wiped."));
313 * Wipes the start of the selected slice.
316 fn_wipe_start_of_slice(struct i_fn_args *a)
318 struct commands *cmds;
321 _("If you are having problems formatting a primary partition, "
322 "it may be because of junk that has accumulated in the "
323 "partition's `disklabel'. A cure for this is to wipe out "
324 "everything on the first few sectors of the primary partition. "
325 "However, this is a rather drastic action to take, so it is not "
326 "recommended unless you are otherwise encountering problems.");
327 a->cancel_desc = _("Return to Utilities Menu");
332 if (confirm_dangerous_action(a->c,
333 _("WARNING! ALL data in primary partition #%d,\n\n%s\n\non the "
334 "disk\n\n%s\n\n will be IRREVOCABLY ERASED!\n\nAre you "
335 "ABSOLUTELY SURE you wish to take this action? This is "
336 "your LAST CHANCE to cancel!"),
337 slice_get_number(storage_get_selected_slice(a->s)),
338 slice_get_desc(storage_get_selected_slice(a->s)),
339 disk_get_desc(storage_get_selected_disk(a->s)))) {
340 /* XXX check to make sure this slice is not mounted first */
341 cmds = commands_new();
342 command_add(cmds, "%s%s if=%sdev/zero of=%sdev/%s bs=32k count=16",
343 a->os_root, cmd_name(a, "DD"),
344 a->os_root, a->os_root,
345 slice_get_device_name(storage_get_selected_slice(a->s)));
346 if (commands_execute(a, cmds)) {
347 inform(a->c, _("Start of primary partition was successfully wiped."));
349 inform(a->c, _("Some errors occurred. "
350 "Start of primary partition was not successfully wiped."));
357 ask_to_wipe_boot_sector(struct i_fn_args *a, struct commands *fcmds)
359 struct commands *cmds;
363 for (cmd = command_get_first(fcmds); cmd != NULL;
364 cmd = command_get_next(cmd)) {
365 disk = command_get_tag(cmd);
367 command_get_result(cmd) > 0 &&
368 command_get_result(cmd) < 256) {
369 switch (dfui_be_present_dialog(a->c,
370 _("Bootblock Install Failed"),
371 _("Re-Initialize Bootblock|Cancel"),
372 _("Warning: bootblocks were not successfully "
373 "installed on the disk `%s'. This may be "
374 "because the disk is new and not yet "
375 "formatted. If this is the case, it might "
376 "help to re-initialize the boot sector, "
377 "then try installing the bootblock again. "
378 "Note that this should not affect the "
379 "partition table of the disk."),
382 cmds = commands_new();
384 "%s%s | %s%s -B %sdev/%s",
385 a->os_root, cmd_name(a, "YES"),
386 a->os_root, cmd_name(a, "FDISK"),
388 if (commands_execute(a, cmds)) {
389 inform(a->c, _("Boot sector successfully initialized."));
391 inform(a->c, _("Some errors occurred. "
392 "Boot sector was not successfully initialized."));
404 fn_install_bootblocks(struct i_fn_args *a)
407 struct dfui_response *r;
408 struct dfui_dataset *ds;
410 struct commands *cmds;
412 char disk[64], boot0cfg[32], packet[32];
413 char msg_buf[1][1024];
415 snprintf(msg_buf[0], sizeof(msg_buf[0]),
416 "'Packet Mode' refers to using newer BIOS calls to boot "
417 "from a partition of the disk. It is generally not "
418 "required unless:\n\n"
419 "- your BIOS does not support legacy mode; or\n"
420 "- your %s primary partition resides on a "
421 "cylinder of the disk beyond cylinder 1024; or\n"
422 "- you just can't get it to boot without it.",
423 OPERATING_SYSTEM_NAME);
425 f = dfui_form_create(
427 _("Install Bootblock(s)"),
432 "p", "special", "dfinstaller_install_bootstrap",
434 "f", "disk", _("Disk Drive"),
435 _("The disk on which you wish to install a bootblock"), "",
436 "p", "editable", "false",
437 "f", "boot0cfg", _("Install Bootblock?"),
438 _("Install a bootblock on this disk"), "",
439 "p", "control", "checkbox",
440 "f", "packet", _("Packet Mode?"),
441 _("Select this to use 'packet mode' to boot the disk"), "",
442 "p", "control", "checkbox",
444 "a", "ok", _("Accept and Install Bootblocks"), "", "",
445 "a", "cancel", a->cancel_desc, "", "",
446 "p", "accelerator", "ESC",
451 dfui_form_set_multiple(f, 1);
453 for (d = storage_disk_first(a->s); d != NULL; d = disk_next(d)) {
454 ds = dfui_dataset_new();
455 /* XXX need to see how this is handled in OpenBSD/NetBSD */
456 dfui_dataset_celldata_add(ds, "disk", disk_get_device_name(d));
457 dfui_dataset_celldata_add(ds, "boot0cfg", "Y");
458 dfui_dataset_celldata_add(ds, "packet", "Y");
459 dfui_form_dataset_add(f, ds);
462 if (!dfui_be_present(a->c, f, &r))
466 if (strcmp(dfui_response_get_action_id(r), "ok") == 0) {
467 cmds = commands_new();
469 for (ds = dfui_response_dataset_get_first(r); ds != NULL;
470 ds = dfui_dataset_get_next(ds)) {
471 strlcpy(disk, dfui_dataset_get_value(ds, "disk"), 64);
472 strlcpy(boot0cfg, dfui_dataset_get_value(ds, "boot0cfg"), 32);
473 strlcpy(packet, dfui_dataset_get_value(ds, "packet"), 32);
475 if (strcasecmp(boot0cfg, "Y") == 0) {
476 cmd = command_add(cmds, "%s%s -B -o %spacket %s",
477 a->os_root, cmd_name(a, "BOOT0CFG"),
478 strcasecmp(packet, "Y") == 0 ? "" : "no",
480 command_set_failure_mode(cmd, COMMAND_FAILURE_WARN);
481 command_set_tag(cmd, disk);
482 cmd = command_add(cmds, "%s%s -v %s",
483 a->os_root, cmd_name(a, "BOOT0CFG"),
485 command_set_failure_mode(cmd, COMMAND_FAILURE_WARN);
486 command_set_tag(cmd, disk);
490 if (!commands_execute(a, cmds)) {
491 ask_to_wipe_boot_sector(a, cmds);
493 inform(a->c, _("Bootblocks were successfully installed!"));
500 dfui_response_free(r);
504 fn_format_msdos_floppy(struct i_fn_args *a)
506 struct commands *cmds;
508 switch (dfui_be_present_dialog(a->c, _("Format MSDOS Floppy"),
509 _("Format Floppy|Return to Utilities Menu"),
510 _("Please insert the floppy to be formatted "
511 "in unit 0 (``drive A:'')."))) {
513 cmds = commands_new();
514 command_add(cmds, "%s%s -y -f 1440 /dev/fd0",
515 a->os_root, cmd_name(a, "FDFORMAT"));
516 command_add(cmds, "%s%s -f 1440 fd0",
517 a->os_root, cmd_name(a, "NEWFS_MSDOS"));
518 if (commands_execute(a, cmds))
519 inform(a->c, _("Floppy successfully formatted!"));
521 inform(a->c, _("Floppy was not successfully formatted."));
531 fn_create_cdboot_floppy(struct i_fn_args *a)
533 struct commands *cmds;
534 char msg_buf[1][1024];
536 snprintf(msg_buf[0], sizeof(msg_buf[0]),
537 "%s cannot be installed from a floppy; "
538 "it must be installed from a booted CD-ROM. "
539 "However, many older systems do not support booting "
540 "from a CD-ROM. For these systems, a boot disk can be "
541 "created. This boot disk contains the Smart Boot "
542 "Manager program, which can boot a CD-ROM even "
543 "on systems with BIOSes which do not support booting "
544 "from the CD-ROM.\n\n"
545 "Smart Boot Manager is not a part of %s; "
546 "the Smart Boot Manager project can be found here:\n\n"
547 "http://btmgr.sourceforge.net/\n\n"
548 "To create a CDBoot floppy, insert a blank floppy "
549 "in unit 0 (``drive A:'') before proceeding."
551 OPERATING_SYSTEM_NAME, OPERATING_SYSTEM_NAME);
553 switch (dfui_be_present_dialog(a->c, _("Create CDBoot Floppy"),
554 _("Create CDBoot Floppy|Return to Utilities Menu"),
557 cmds = commands_new();
558 command_add(cmds, "%s%s -c %sboot/cdboot.flp.bz2 | "
559 "%s%s of=%sdev/fd0 bs=32k",
560 a->os_root, cmd_name(a, "BUNZIP2"),
562 a->os_root, cmd_name(a, "DD"),
564 if (commands_execute(a, cmds))
565 inform(a->c, _("CDBoot floppy successfully created!"));
567 inform(a->c, _("CDBoot floppy was not successfully created."));
576 /**** NON-fn_ FUNCTIONS ***/
579 format_slice(struct i_fn_args *a)
581 struct commands *cmds;
585 cmds = commands_new();
588 * The information in a->s NEEDS to be accurate here!
589 * Presumably we just did a survey_storage() recently.
590 * XXX should we do another one here anyway just to be paranoid?
594 * Set the slice's sysid to 165.
596 disk_get_geometry(storage_get_selected_disk(a->s), &cyl, &hd, &sec);
597 command_add(cmds, "%s%s 'g c%d h%d s%d' >%snew.fdisk",
598 a->os_root, cmd_name(a, "ECHO"),
601 command_add(cmds, "%s%s 'p %d %d %lu %lu' >>%snew.fdisk",
602 a->os_root, cmd_name(a, "ECHO"),
603 slice_get_number(storage_get_selected_slice(a->s)),
605 slice_get_start(storage_get_selected_slice(a->s)),
606 slice_get_size(storage_get_selected_slice(a->s)),
608 if (slice_get_flags(storage_get_selected_slice(a->s)) & 0x80) {
609 command_add(cmds, "%s%s 'a %d' >>%snew.fdisk",
610 a->os_root, cmd_name(a, "ECHO"),
611 slice_get_number(storage_get_selected_slice(a->s)),
615 command_add(cmds, "%s%s %snew.fdisk",
616 a->os_root, cmd_name(a, "CAT"), a->tmp);
617 temp_file_add(a, "new.fdisk");
620 * Execute the fdisk script.
622 command_add(cmds, "%s%s -v -f %snew.fdisk %s",
623 a->os_root, cmd_name(a, "FDISK"), a->tmp,
624 disk_get_device_name(storage_get_selected_disk(a->s)));
627 * If there is an old 'virgin' disklabel hanging around
628 * in the temp dir, get rid of it. This won't happen
629 * from a real CD, but might happen with '-o' installs.
631 command_add(cmds, "%s%s -f %sinstall.disklabel.%s",
632 a->os_root, cmd_name(a, "RM"),
634 slice_get_device_name(storage_get_selected_slice(a->s)));
636 result = commands_execute(a, cmds);