disks.c revision 54587
1/*
2 * The new sysinstall program.
3 *
4 * This is probably the last program in the `sysinstall' line - the next
5 * generation being essentially a complete rewrite.
6 *
7 * $FreeBSD: head/usr.sbin/sade/disks.c 54587 1999-12-14 04:25:29Z jkh $
8 *
9 * Copyright (c) 1995
10 *	Jordan Hubbard.  All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 *    notice, this list of conditions and the following disclaimer,
17 *    verbatim and that no modifications are made prior to this
18 *    point in the file.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 *    notice, this list of conditions and the following disclaimer in the
21 *    documentation and/or other materials provided with the distribution.
22 *
23 * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 */
36
37#include "sysinstall.h"
38#include <ctype.h>
39#include <fcntl.h>
40#include <sys/stat.h>
41#include <sys/disklabel.h>
42
43/* Where we start displaying chunk information on the screen */
44#define CHUNK_START_ROW		5
45
46/* Where we keep track of MBR chunks */
47static struct chunk *chunk_info[16];
48static int current_chunk;
49
50static void	diskPartitionNonInteractive(Device *dev);
51
52static void
53record_chunks(Disk *d)
54{
55    struct chunk *c1 = NULL;
56    int i = 0;
57    int last_free = 0;
58
59    if (!d->chunks)
60	msgFatal("No chunk list found for %s!", d->name);
61
62    for (c1 = d->chunks->part; c1; c1 = c1->next) {
63	if (c1->type == unused && c1->size > last_free) {
64	    last_free = c1->size;
65	    current_chunk = i;
66	}
67	chunk_info[i++] = c1;
68    }
69    chunk_info[i] = NULL;
70    if (current_chunk >= i)
71	current_chunk = i - 1;
72}
73
74static int Total;
75
76static void
77print_chunks(Disk *d)
78{
79    int row;
80    int i;
81
82    for (i = Total = 0; chunk_info[i]; i++)
83	Total += chunk_info[i]->size;
84    if (d->bios_cyl > 65536 || d->bios_hd > 256 || d->bios_sect >= 64) {
85	dialog_clear_norefresh();
86	msgConfirm("WARNING:  A geometry of %d/%d/%d for %s is incorrect.  Using\n"
87		   "a more likely geometry.  If this geometry is incorrect or you\n"
88		   "are unsure as to whether or not it's correct, please consult\n"
89		   "the Hardware Guide in the Documentation submenu or use the\n"
90		   "(G)eometry command to change it now.\n\n"
91		   "Remember: you need to enter whatever your BIOS thinks the\n"
92		   "geometry is!  For IDE, it's what you were told in the BIOS\n"
93		   "setup. For SCSI, it's the translation mode your controller is\n"
94		   "using.  Do NOT use a ``physical geometry''.",
95	  d->bios_cyl, d->bios_hd, d->bios_sect, d->name);
96	Sanitize_Bios_Geom(d);
97    }
98    attrset(A_NORMAL);
99    mvaddstr(0, 0, "Disk name:\t");
100    clrtobot();
101    attrset(A_REVERSE); addstr(d->name); attrset(A_NORMAL);
102    attrset(A_REVERSE); mvaddstr(0, 55, "FDISK Partition Editor"); attrset(A_NORMAL);
103    mvprintw(1, 0,
104	     "DISK Geometry:\t%lu cyls/%lu heads/%lu sectors = %lu sectors",
105	     d->bios_cyl, d->bios_hd, d->bios_sect,
106	     d->bios_cyl * d->bios_hd * d->bios_sect);
107    mvprintw(3, 0, "%10s %10s %10s %8s %6s %10s %8s %8s",
108	     "Offset", "Size", "End", "Name", "PType", "Desc",
109	     "Subtype", "Flags");
110    for (i = 0, row = CHUNK_START_ROW; chunk_info[i]; i++, row++) {
111	if (i == current_chunk)
112	    attrset(ATTR_SELECTED);
113	mvprintw(row, 0, "%10ld %10lu %10lu %8s %6d %10s %8d\t%-6s",
114		 chunk_info[i]->offset, chunk_info[i]->size,
115		 chunk_info[i]->end, chunk_info[i]->name,
116		 chunk_info[i]->type,
117		 slice_type_name(chunk_info[i]->type, chunk_info[i]->subtype),
118		 chunk_info[i]->subtype, ShowChunkFlags(chunk_info[i]));
119	if (i == current_chunk)
120	    attrset(A_NORMAL);
121    }
122}
123
124static void
125print_command_summary()
126{
127    mvprintw(14, 0, "The following commands are supported (in upper or lower case):");
128    mvprintw(16, 0, "A = Use Entire Disk                             C = Create Slice");
129    mvprintw(17, 0, "D = Delete Slice       G = Set Drive Geometry   S = Set Bootable");
130    mvprintw(18, 0, "T = Change Type        U = Undo All Changes     Q = Finish");
131    if (!RunningAsInit)
132	mvprintw(18, 48, "W = Write Changes");
133    mvprintw(21, 0, "Use F1 or ? to get more help, arrow keys to select.");
134    move(0, 0);
135}
136
137static u_char *
138getBootMgr(char *dname)
139{
140#ifndef __alpha__	/* only meaningful on x86 */
141    extern u_char mbr[], boot0[];
142    char str[80];
143    char *cp;
144    int i = 0;
145
146    cp = variable_get(VAR_BOOTMGR);
147    if (!cp) {
148	/* Figure out what kind of MBR the user wants */
149	sprintf(str, "Install Boot Manager for drive %s?", dname);
150	MenuMBRType.title = str;
151	i = dmenuOpenSimple(&MenuMBRType, FALSE);
152    }
153    else {
154	if (!strncmp(cp, "boot", 4))
155	    BootMgr = 0;
156	else if (!strcmp(cp, "standard"))
157	    BootMgr = 1;
158	else
159	    BootMgr = 2;
160    }
161    if (cp || i) {
162	switch (BootMgr) {
163	case 0:
164	    return boot0;
165
166	case 1:
167	    return mbr;
168
169	case 2:
170	default:
171	    break;
172	}
173    }
174#endif
175    return NULL;
176}
177
178int
179diskGetSelectCount(Device ***devs)
180{
181    int i, cnt, enabled;
182    char *cp;
183    Device **dp;
184
185    cp = variable_get(VAR_DISK);
186    dp = *devs = deviceFind(cp, DEVICE_TYPE_DISK);
187    cnt = deviceCount(dp);
188    if (!cnt)
189	return -1;
190    for (i = 0, enabled = 0; i < cnt; i++) {
191	if (dp[i]->enabled)
192	    ++enabled;
193    }
194    return enabled;
195}
196
197void
198diskPartition(Device *dev)
199{
200    char *cp, *p;
201    int rv, key = 0;
202    Boolean chunking;
203    char *msg = NULL;
204    u_char *mbrContents;
205    WINDOW *w = savescr();
206    Disk *d = (Disk *)dev->private;
207
208    chunking = TRUE;
209    keypad(stdscr, TRUE);
210
211    /* Flush both the dialog and curses library views of the screen
212       since we don't always know who called us */
213    dialog_clear_norefresh(), clear();
214    current_chunk = 0;
215
216    /* Set up the chunk array */
217    record_chunks(d);
218
219    while (chunking) {
220	char *val, geometry[80];
221
222	/* Now print our overall state */
223	if (d)
224	    print_chunks(d);
225	print_command_summary();
226	if (msg) {
227	    attrset(title_attr); mvprintw(23, 0, msg); attrset(A_NORMAL);
228	    beep();
229	    msg = NULL;
230	}
231	else {
232	    move(23, 0);
233	    clrtoeol();
234	}
235
236	/* Get command character */
237	key = getch();
238	switch (toupper(key)) {
239	case '\014':	/* ^L (redraw) */
240	    clear();
241	    msg = NULL;
242	    break;
243
244	case '\020':	/* ^P */
245	case KEY_UP:
246	case '-':
247	    if (current_chunk != 0)
248		--current_chunk;
249	    break;
250
251	case '\016':	/* ^N */
252	case KEY_DOWN:
253	case '+':
254	case '\r':
255	case '\n':
256	    if (chunk_info[current_chunk + 1])
257		++current_chunk;
258	    break;
259
260	case KEY_HOME:
261	    current_chunk = 0;
262	    break;
263
264	case KEY_END:
265	    while (chunk_info[current_chunk + 1])
266		++current_chunk;
267	    break;
268
269	case KEY_F(1):
270	case '?':
271	    systemDisplayHelp("slice");
272	    clear();
273	    break;
274
275	case 'A':
276#ifdef __alpha__
277	    rv = 1;
278#else	    /* The rest is only relevant on x86 */
279	    cp = variable_get(VAR_DEDICATE_DISK);
280	    if (cp && !strcasecmp(cp, "always"))
281		rv = 1;
282	    else {
283		rv = msgYesNo("Do you want to do this with a true partition entry\n"
284			      "so as to remain cooperative with any future possible\n"
285			      "operating systems on the drive(s)?\n"
286			      "(See also the section about ``dangerously dedicated''\n"
287			      "disks in the FreeBSD FAQ.)");
288		if (rv == -1)
289		    rv = 0;
290	    }
291#endif
292	    All_FreeBSD(d, rv);
293	    variable_set2(DISK_PARTITIONED, "yes", 0);
294	    record_chunks(d);
295	    clear();
296	    break;
297
298	case 'C':
299	    if (chunk_info[current_chunk]->type != unused)
300		msg = "Slice in use, delete it first or move to an unused one.";
301	    else {
302		char *val, tmp[20], *cp;
303		int size, subtype;
304		chunk_e partitiontype;
305
306		snprintf(tmp, 20, "%lu", chunk_info[current_chunk]->size);
307		val = msgGetInput(tmp, "Please specify the size for new FreeBSD slice in blocks\n"
308				  "or append a trailing `M' for megabytes (e.g. 20M).");
309		if (val && (size = strtol(val, &cp, 0)) > 0) {
310		    if (*cp && toupper(*cp) == 'M')
311			size *= ONE_MEG;
312		    strcpy(tmp, "165");
313		    val = msgGetInput(tmp, "Enter type of partition to create:\n\n"
314				      "Pressing Enter will choose the default, a native FreeBSD\n"
315				      "slice (type 165).  You can choose other types, 6 for a\n"
316				      "DOS partition or 131 for a Linux partition, for example.\n\n"
317				      "Note:  If you choose a non-FreeBSD partition type, it will not\n"
318				      "be formatted or otherwise prepared, it will simply reserve space\n"
319				      "for you to use another tool, such as DOS FORMAT, to later format\n"
320				      "and use the partition.");
321		    if (val && (subtype = strtol(val, NULL, 0)) > 0) {
322			if (subtype == 165)
323			    partitiontype = freebsd;
324			else if (subtype == 6)
325			    partitiontype = fat;
326			else
327			    partitiontype = unknown;
328#ifdef __alpha__
329			if (partitiontype == freebsd && size == chunk_info[current_chunk]->size)
330			    All_FreeBSD(d, 1);
331			else
332#endif
333			Create_Chunk(d, chunk_info[current_chunk]->offset, size, partitiontype, subtype,
334				     (chunk_info[current_chunk]->flags & CHUNK_ALIGN));
335			variable_set2(DISK_PARTITIONED, "yes", 0);
336			record_chunks(d);
337		    }
338		}
339		clear();
340	    }
341	    break;
342
343	case KEY_DC:
344	case 'D':
345	    if (chunk_info[current_chunk]->type == unused)
346		msg = "Slice is already unused!";
347	    else {
348		Delete_Chunk(d, chunk_info[current_chunk]);
349		variable_set2(DISK_PARTITIONED, "yes", 0);
350		record_chunks(d);
351	    }
352	    break;
353
354	case 'T':
355	    if (chunk_info[current_chunk]->type == unused)
356		msg = "Slice is currently unused (use create instead)";
357	    else {
358		char *val, tmp[20];
359		int subtype;
360		chunk_e partitiontype;
361
362		strcpy(tmp, "165");
363		val = msgGetInput(tmp, "New partition type:\n\n"
364				  "Pressing Enter will choose the default, a native FreeBSD\n"
365				  "slice (type 165).  Other popular values are 6 for\n"
366				  "DOS FAT partition, 131 for a Linux ext2fs partition or\n"
367				  "130 for a Linux swap partition.\n\n"
368				  "Note:  If you choose a non-FreeBSD partition type, it will not\n"
369				  "be formatted or otherwise prepared, it will simply reserve space\n"
370				  "for you to use another tool, such as DOS format, to later format\n"
371				  "and actually use the partition.");
372		if (val && (subtype = strtol(val, NULL, 0)) > 0) {
373		    if (subtype == 165)
374			partitiontype = freebsd;
375		    else if (subtype == 6)
376			partitiontype = fat;
377		    else
378			partitiontype = unknown;
379		    chunk_info[current_chunk]->type = partitiontype;
380		    chunk_info[current_chunk]->subtype = subtype;
381		}
382	    }
383	    break;
384
385	case 'G':
386	    snprintf(geometry, 80, "%lu/%lu/%lu", d->bios_cyl, d->bios_hd, d->bios_sect);
387	    val = msgGetInput(geometry, "Please specify the new geometry in cyl/hd/sect format.\n"
388			      "Don't forget to use the two slash (/) separator characters!\n"
389			      "It's not possible to parse the field without them.");
390	    if (val) {
391		long nc, nh, ns;
392		nc = strtol(val, &val, 0);
393		nh = strtol(val + 1, &val, 0);
394		ns = strtol(val + 1, 0, 0);
395		Set_Bios_Geom(d, nc, nh, ns);
396	    }
397	    clear();
398	    break;
399
400	case 'S':
401	    /* Set Bootable */
402	    chunk_info[current_chunk]->flags |= CHUNK_ACTIVE;
403	    break;
404
405	case 'U':
406	    if ((cp = variable_get(DISK_LABELLED)) && !strcmp(cp, "written")) {
407		msgConfirm("You've already written this information out - you\n"
408			   "can't undo it.");
409	    }
410	    else if (!msgYesNo("Are you SURE you want to Undo everything?")) {
411		char cp[BUFSIZ];
412
413		sstrncpy(cp, d->name, sizeof cp);
414		Free_Disk(dev->private);
415		d = Open_Disk(cp);
416		if (!d)
417		    msgConfirm("Can't reopen disk %s! Internal state is probably corrupted", cp);
418		dev->private = d;
419		variable_unset(DISK_PARTITIONED);
420		variable_unset(DISK_LABELLED);
421		if (d)
422		    record_chunks(d);
423	    }
424	    clear();
425	    break;
426
427	case 'W':
428	    if (!msgYesNo("WARNING:  This should only be used when modifying an EXISTING\n"
429			       "installation.  If you are installing FreeBSD for the first time\n"
430			       "then you should simply type Q when you're finished here and your\n"
431			       "changes will be committed in one batch automatically at the end of\n"
432			       "these questions.  If you're adding a disk, you should NOT write\n"
433			       "from this screen, you should do it from the label editor.\n\n"
434			       "Are you absolutely sure you want to do this now?")) {
435		variable_set2(DISK_PARTITIONED, "yes", 0);
436
437		/* Don't trash the MBR if the first (and therefore only) chunk is marked for a truly dedicated
438		 * disk (i.e., the disklabel starts at sector 0), even in cases where the user has requested
439		 * booteasy or a "standard" MBR -- both would be fatal in this case.
440		 */
441		/*
442		 * Don't offer to update the MBR on this disk if the first "real" chunk looks like
443		 * a FreeBSD "all disk" partition, or the disk is entirely FreeBSD.
444		 */
445		if (((d->chunks->part->type != freebsd) || (d->chunks->part->offset > 1)))
446		    mbrContents = getBootMgr(d->name);
447		else
448		    mbrContents = NULL;
449		Set_Boot_Mgr(d, mbrContents);
450
451		if (DITEM_STATUS(diskPartitionWrite(NULL)) != DITEM_SUCCESS)
452		    msgConfirm("Disk partition write returned an error status!");
453		else
454		    msgConfirm("Wrote FDISK partition information out successfully.");
455	    }
456	    clear();
457	    break;
458
459	case '|':
460	    if (!msgYesNo("Are you SURE you want to go into Wizard mode?\n"
461			  "No seat belts whatsoever are provided!")) {
462		clear();
463		refresh();
464		slice_wizard(d);
465		variable_set2(DISK_PARTITIONED, "yes", 0);
466		record_chunks(d);
467	    }
468	    else
469		msg = "Wise choice!";
470	    clear();
471	    break;
472
473	case '\033':	/* ESC */
474	case 'Q':
475	    chunking = FALSE;
476	    /* Don't trash the MBR if the first (and therefore only) chunk is marked for a truly dedicated
477	     * disk (i.e., the disklabel starts at sector 0), even in cases where the user has requested
478	     * booteasy or a "standard" MBR -- both would be fatal in this case.
479	     */
480#if 0
481	    if ((d->chunks->part->flags & CHUNK_FORCE_ALL) != CHUNK_FORCE_ALL
482		&& (mbrContents = getBootMgr(d->name)) != NULL)
483		Set_Boot_Mgr(d, mbrContents);
484#else
485	    /*
486	     * Don't offer to update the MBR on this disk if the first "real" chunk looks like
487	     * a FreeBSD "all disk" partition, or the disk is entirely FreeBSD.
488	     */
489	    if (((d->chunks->part->type != freebsd) || (d->chunks->part->offset > 1)) &&
490		(mbrContents = getBootMgr(d->name)) != NULL)
491		Set_Boot_Mgr(d, mbrContents);
492#endif
493	    break;
494
495	default:
496	    beep();
497	    msg = "Type F1 or ? for help";
498	    break;
499	}
500    }
501    p = CheckRules(d);
502    if (p) {
503	char buf[FILENAME_MAX];
504
505        use_helpline("Press F1 to read more about disk slices.");
506	use_helpfile(systemHelpFile("partition", buf));
507	if (!variable_get(VAR_NO_WARN))
508	    dialog_mesgbox("Disk slicing warning:", p, -1, -1);
509	free(p);
510    }
511    restorescr(w);
512}
513
514static u_char *
515bootalloc(char *name)
516{
517    char buf[FILENAME_MAX];
518    struct stat sb;
519
520    snprintf(buf, sizeof buf, "/boot/%s", name);
521    if (stat(buf, &sb) != -1) {
522	int fd;
523
524	fd = open(buf, O_RDONLY);
525	if (fd != -1) {
526	    u_char *cp;
527
528	    cp = malloc(sb.st_size);
529	    if (read(fd, cp, sb.st_size) != sb.st_size) {
530		free(cp);
531		close(fd);
532		msgDebug("bootalloc: couldn't read %d bytes from %s\n", sb.st_size, buf);
533		return NULL;
534	    }
535	    close(fd);
536	    return cp;
537	}
538	msgDebug("bootalloc: couldn't open %s\n", buf);
539    }
540    else
541	msgDebug("bootalloc: can't stat %s\n", buf);
542    return NULL;
543}
544
545static int
546partitionHook(dialogMenuItem *selected)
547{
548    Device **devs = NULL;
549
550    devs = deviceFind(selected->prompt, DEVICE_TYPE_DISK);
551    if (!devs) {
552	msgConfirm("Unable to find disk %s!", selected->prompt);
553	return DITEM_FAILURE;
554    }
555    /* Toggle enabled status? */
556    if (!devs[0]->enabled) {
557	devs[0]->enabled = TRUE;
558	diskPartition(devs[0]);
559    }
560    else
561	devs[0]->enabled = FALSE;
562    return DITEM_SUCCESS;
563}
564
565static int
566partitionCheck(dialogMenuItem *selected)
567{
568    Device **devs = NULL;
569
570    devs = deviceFind(selected->prompt, DEVICE_TYPE_DISK);
571    if (!devs || devs[0]->enabled == FALSE)
572	return FALSE;
573    return TRUE;
574}
575
576int
577diskPartitionEditor(dialogMenuItem *self)
578{
579    DMenu *menu;
580    Device **devs;
581    int i, cnt, devcnt;
582
583    cnt = diskGetSelectCount(&devs);
584    devcnt = deviceCount(devs);
585    if (cnt == -1) {
586	msgConfirm("No disks found!  Please verify that your disk controller is being\n"
587		   "properly probed at boot time.  See the Hardware Guide on the\n"
588		   "Documentation menu for clues on diagnosing this type of problem.");
589	return DITEM_FAILURE;
590    }
591    else if (cnt) {
592	/* Some are already selected */
593	for (i = 0; i < devcnt; i++) {
594	    if (devs[i]->enabled) {
595		if (variable_get(VAR_NONINTERACTIVE))
596		    diskPartitionNonInteractive(devs[i]);
597		else
598		    diskPartition(devs[i]);
599	    }
600	}
601    }
602    else {
603	/* No disks are selected, fall-back case now */
604	if (devcnt == 1) {
605	    devs[0]->enabled = TRUE;
606	    if (variable_get(VAR_NONINTERACTIVE))
607		diskPartitionNonInteractive(devs[0]);
608	    else
609		diskPartition(devs[0]);
610	    return DITEM_SUCCESS;
611	}
612	else {
613	    menu = deviceCreateMenu(&MenuDiskDevices, DEVICE_TYPE_DISK, partitionHook, partitionCheck);
614	    if (!menu) {
615		msgConfirm("No devices suitable for installation found!\n\n"
616			   "Please verify that your disk controller (and attached drives)\n"
617			   "were detected properly.  This can be done by pressing the\n"
618			   "[Scroll Lock] key and using the Arrow keys to move back to\n"
619			   "the boot messages.  Press [Scroll Lock] again to return.");
620		return DITEM_FAILURE;
621	    }
622	    else {
623		i = dmenuOpenSimple(menu, FALSE) ? DITEM_SUCCESS : DITEM_FAILURE;
624		free(menu);
625	    }
626	    return i;
627	}
628    }
629    return DITEM_SUCCESS;
630}
631
632int
633diskPartitionWrite(dialogMenuItem *self)
634{
635    Device **devs;
636    int i;
637    char *cp;
638
639    devs = deviceFind(NULL, DEVICE_TYPE_DISK);
640    if (!devs) {
641	msgConfirm("Unable to find any disks to write to??");
642	return DITEM_FAILURE;
643    }
644    if (isDebug())
645	msgDebug("diskPartitionWrite: Examining %d devices\n", deviceCount(devs));
646    cp = variable_get(DISK_PARTITIONED);
647    if (cp && !strcmp(cp, "written"))
648	return DITEM_SUCCESS;
649
650    for (i = 0; devs[i]; i++) {
651	Disk *d = (Disk *)devs[i]->private;
652	static u_char *boot1;
653#ifndef __alpha__
654	static u_char *boot2;
655#endif
656
657	if (!devs[i]->enabled)
658	    continue;
659
660#ifdef __alpha__
661	if (!boot1) boot1 = bootalloc("boot1");
662	Set_Boot_Blocks(d, boot1, NULL);
663#else
664	if (!boot1) boot1 = bootalloc("boot1");
665	if (!boot2) boot2 = bootalloc("boot2");
666	Set_Boot_Blocks(d, boot1, boot2);
667#endif
668
669	msgNotify("Writing partition information to drive %s", d->name);
670	if (!Fake && Write_Disk(d)) {
671	    msgConfirm("ERROR: Unable to write data to disk %s!", d->name);
672	    return DITEM_FAILURE;
673	}
674
675	/* If we've been through here before, we don't need to do the rest */
676	if (cp && !strcmp(cp, "written"))
677	    return DITEM_SUCCESS;
678    }
679    /* Now it's not "yes", but "written" */
680    variable_set2(DISK_PARTITIONED, "written", 0);
681    return DITEM_SUCCESS;
682}
683
684/* Partition a disk based wholly on which variables are set */
685static void
686diskPartitionNonInteractive(Device *dev)
687{
688    char *cp;
689    int i, sz, all_disk = 0;
690    u_char *mbrContents;
691    Disk *d = (Disk *)dev->private;
692
693    record_chunks(d);
694    cp = variable_get(VAR_GEOMETRY);
695    if (cp) {
696	msgDebug("Setting geometry from script to: %s\n", cp);
697	d->bios_cyl = strtol(cp, &cp, 0);
698	d->bios_hd = strtol(cp + 1, &cp, 0);
699	d->bios_sect = strtol(cp + 1, 0, 0);
700    }
701
702    cp = variable_get(VAR_PARTITION);
703    if (cp) {
704	if (!strcmp(cp, "free")) {
705	    /* Do free disk space case */
706	    for (i = 0; chunk_info[i]; i++) {
707		/* If a chunk is at least 10MB in size, use it. */
708		if (chunk_info[i]->type == unused && chunk_info[i]->size > (10 * ONE_MEG)) {
709		    Create_Chunk(d, chunk_info[i]->offset, chunk_info[i]->size, freebsd, 3,
710				 (chunk_info[i]->flags & CHUNK_ALIGN));
711		    variable_set2(DISK_PARTITIONED, "yes", 0);
712		    break;
713		}
714	    }
715	    if (!chunk_info[i]) {
716		msgConfirm("Unable to find any free space on this disk!");
717		return;
718	    }
719	}
720	else if (!strcmp(cp, "all")) {
721	    /* Do all disk space case */
722	    msgDebug("Warning:  Devoting all of disk %s to FreeBSD.\n", d->name);
723
724	    All_FreeBSD(d, FALSE);
725	}
726	else if (!strcmp(cp, "exclusive")) {
727	    /* Do really-all-the-disk-space case */
728	    msgDebug("Warning:  Devoting all of disk %s to FreeBSD.\n", d->name);
729
730	    All_FreeBSD(d, all_disk = TRUE);
731	}
732	else if ((sz = strtol(cp, &cp, 0))) {
733	    /* Look for sz bytes free */
734	    if (*cp && toupper(*cp) == 'M')
735		sz *= ONE_MEG;
736	    for (i = 0; chunk_info[i]; i++) {
737		/* If a chunk is at least sz MB, use it. */
738		if (chunk_info[i]->type == unused && chunk_info[i]->size >= sz) {
739		    Create_Chunk(d, chunk_info[i]->offset, sz, freebsd, 3, (chunk_info[i]->flags & CHUNK_ALIGN));
740		    variable_set2(DISK_PARTITIONED, "yes", 0);
741		    break;
742		}
743	    }
744	    if (!chunk_info[i]) {
745		msgConfirm("Unable to find %d free blocks on this disk!", sz);
746		return;
747	    }
748	}
749	else if (!strcmp(cp, "existing")) {
750	    /* Do existing FreeBSD case */
751	    for (i = 0; chunk_info[i]; i++) {
752		if (chunk_info[i]->type == freebsd)
753		    break;
754	    }
755	    if (!chunk_info[i]) {
756		msgConfirm("Unable to find any existing FreeBSD partitions on this disk!");
757		return;
758	    }
759	}
760	else {
761	    msgConfirm("`%s' is an invalid value for %s - is config file valid?", cp, VAR_PARTITION);
762	    return;
763	}
764	if (!all_disk) {
765	    mbrContents = getBootMgr(d->name);
766	    Set_Boot_Mgr(d, mbrContents);
767	}
768	variable_set2(DISK_PARTITIONED, "yes", 0);
769    }
770}
771