install.c revision 16830
170856Sjhb/*
270856Sjhb * The new sysinstall program.
370856Sjhb *
470856Sjhb * This is probably the last program in the `sysinstall' line - the next
570856Sjhb * generation being essentially a complete rewrite.
670856Sjhb *
770856Sjhb * $Id: install.c,v 1.103 1996/06/25 18:41:09 jkh Exp $
870856Sjhb *
970856Sjhb * Copyright (c) 1995
1070856Sjhb *	Jordan Hubbard.  All rights reserved.
1170856Sjhb *
1270856Sjhb * Redistribution and use in source and binary forms, with or without
1370856Sjhb * modification, are permitted provided that the following conditions
1470856Sjhb * are met:
1570856Sjhb * 1. Redistributions of source code must retain the above copyright
1670856Sjhb *    notice, this list of conditions and the following disclaimer,
1770856Sjhb *    verbatim and that no modifications are made prior to this
1870856Sjhb *    point in the file.
1970856Sjhb * 2. Redistributions in binary form must reproduce the above copyright
2070856Sjhb *    notice, this list of conditions and the following disclaimer in the
2170856Sjhb *    documentation and/or other materials provided with the distribution.
2270856Sjhb *
2370856Sjhb * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
2470856Sjhb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2570856Sjhb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2670856Sjhb * ARE DISCLAIMED.  IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
2770856Sjhb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2870856Sjhb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2970856Sjhb * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
30100280Sgordon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31100280Sgordon * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32100280Sgordon * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33100280Sgordon * SUCH DAMAGE.
34100280Sgordon *
35100280Sgordon */
36100280Sgordon
37102993Sfenner#include "sysinstall.h"
38102993Sfenner#include <ctype.h>
39100280Sgordon#include <sys/disklabel.h>
40100280Sgordon#include <sys/errno.h>
4170856Sjhb#include <sys/ioctl.h>
4270856Sjhb#include <sys/fcntl.h>
4370856Sjhb#include <sys/wait.h>
4470856Sjhb#include <sys/param.h>
4570856Sjhb#define MSDOSFS
46102993Sfenner#include <sys/mount.h>
47102993Sfenner#undef MSDOSFS
48102993Sfenner#include <sys/stat.h>
49102993Sfenner#include <unistd.h>
50102993Sfenner#include <sys/mount.h>
51102993Sfenner
52102993Sfennerstatic void	create_termcap(void);
53102993Sfenner
54102993Sfenner#define TERMCAP_FILE	"/usr/share/misc/termcap"
55102993Sfenner
5670856Sjhbstatic void	installConfigure(void);
57102993Sfenner
58102993Sfennerstatic Boolean
59102993SfennercheckLabels(Chunk **rdev, Chunk **sdev, Chunk **udev)
6070856Sjhb{
61102993Sfenner    Device **devs;
62102993Sfenner    Boolean status;
63102993Sfenner    Disk *disk;
64102993Sfenner    Chunk *c1, *c2, *rootdev, *swapdev, *usrdev;
65102993Sfenner    int i;
66102993Sfenner
67102993Sfenner    status = TRUE;
68102993Sfenner    *rdev = *sdev = *udev = rootdev = swapdev = usrdev = NULL;
69102993Sfenner    devs = deviceFind(NULL, DEVICE_TYPE_DISK);
7070856Sjhb    /* First verify that we have a root device */
71102993Sfenner    for (i = 0; devs[i]; i++) {
72102993Sfenner	if (!devs[i]->enabled)
73102993Sfenner	    continue;
74102993Sfenner	disk = (Disk *)devs[i]->private;
75102993Sfenner	msgDebug("Scanning disk %s for root filesystem\n", disk->name);
76102993Sfenner	if (!disk->chunks)
77102993Sfenner	    msgFatal("No chunk list found for %s!", disk->name);
78102993Sfenner	for (c1 = disk->chunks->part; c1; c1 = c1->next) {
79102993Sfenner	    if (c1->type == freebsd) {
8070856Sjhb		for (c2 = c1->part; c2; c2 = c2->next) {
81102993Sfenner		    if (c2->type == part && c2->subtype != FS_SWAP && c2->private_data) {
82102993Sfenner			if (c2->flags & CHUNK_IS_ROOT) {
83102993Sfenner			    if (rootdev) {
84102993Sfenner				msgConfirm("WARNING:  You have more than one root device set?!\n"
85102993Sfenner					   "Using the first one found.");
86102993Sfenner				continue;
87102993Sfenner			    }
88102993Sfenner			    rootdev = c2;
89102993Sfenner			    if (isDebug())
9070856Sjhb				msgDebug("Found rootdev at %s!\n", rootdev->name);
91102993Sfenner			}
92102993Sfenner			else if (!strcmp(((PartInfo *)c2->private_data)->mountpoint, "/usr")) {
93102993Sfenner			    if (usrdev) {
94102993Sfenner				msgConfirm("WARNING:  You have more than one /usr filesystem.\n"
95102993Sfenner					   "Using the first one found.");
96102993Sfenner				continue;
97102993Sfenner			    }
98102993Sfenner			    usrdev = c2;
99102993Sfenner			    if (isDebug())
100102993Sfenner				msgDebug("Found usrdev at %s!\n", usrdev->name);
101102993Sfenner			}
102102993Sfenner		    }
103102993Sfenner		}
104102993Sfenner	    }
10570856Sjhb	}
106102993Sfenner    }
107102993Sfenner
108102993Sfenner    swapdev = NULL;
109102993Sfenner    /* Now check for swap devices */
110102993Sfenner    for (i = 0; devs[i]; i++) {
111102993Sfenner	if (!devs[i]->enabled)
112102993Sfenner	    continue;
113102993Sfenner	disk = (Disk *)devs[i]->private;
114102993Sfenner	msgDebug("Scanning disk %s for swap partitions\n", disk->name);
11570856Sjhb	if (!disk->chunks)
116102993Sfenner	    msgFatal("No chunk list found for %s!", disk->name);
117102993Sfenner	for (c1 = disk->chunks->part; c1; c1 = c1->next) {
118102993Sfenner	    if (c1->type == freebsd) {
119102993Sfenner		for (c2 = c1->part; c2; c2 = c2->next) {
120102993Sfenner		    if (c2->type == part && c2->subtype == FS_SWAP && !swapdev) {
121102993Sfenner			swapdev = c2;
122102993Sfenner			if (isDebug())
123102993Sfenner			    msgDebug("Found swapdev at %s!\n", swapdev->name);
124102993Sfenner			break;
12570856Sjhb		    }
126102993Sfenner		}
127102993Sfenner	    }
128102993Sfenner	}
129102993Sfenner    }
130102993Sfenner
131102993Sfenner    *rdev = rootdev;
132102993Sfenner    if (!rootdev) {
133102993Sfenner	msgConfirm("No root device found - you must label a partition as /\n"
134102993Sfenner		   "in the label editor.");
13570856Sjhb	status = FALSE;
136102993Sfenner    }
137102993Sfenner
138102993Sfenner    *sdev = swapdev;
139102993Sfenner    if (!swapdev) {
140102993Sfenner	msgConfirm("No swap devices found - you must create at least one\n"
141102993Sfenner		   "swap partition.");
142102993Sfenner	status = FALSE;
143102993Sfenner    }
144102993Sfenner
14570856Sjhb    *udev = usrdev;
146102993Sfenner    if (!usrdev) {
147102993Sfenner	msgConfirm("WARNING:  No /usr filesystem found.  This is not technically\n"
148102993Sfenner		   "an error if your root filesystem is big enough (or you later\n"
149102993Sfenner		   "intend to mount your /usr filesystem over NFS), but it may otherwise\n"
150102993Sfenner		   "cause you trouble if you're not exactly sure what you are doing!");
151102993Sfenner    }
152102993Sfenner    return status;
153102993Sfenner}
154102993Sfenner
15570856Sjhbstatic int
156102993SfennerinstallInitial(void)
157102993Sfenner{
158102993Sfenner    static Boolean alreadyDone = FALSE;
159102993Sfenner
160102993Sfenner    if (alreadyDone)
161102993Sfenner	return DITEM_SUCCESS;
162102993Sfenner
163102993Sfenner    if (!variable_get(DISK_LABELLED)) {
164102993Sfenner	msgConfirm("You need to assign disk labels before you can proceed with\n"
16570856Sjhb		   "the installation.");
166102993Sfenner	return DITEM_FAILURE;
167102993Sfenner    }
168102993Sfenner    /* If it's labelled, assume it's also partitioned */
169102993Sfenner    if (!variable_get(DISK_PARTITIONED))
170102993Sfenner	variable_set2(DISK_PARTITIONED, "yes");
171102993Sfenner
172102993Sfenner    /* If we refuse to proceed, bail. */
173102993Sfenner    dialog_clear();
174102993Sfenner    if (msgYesNo("Last Chance!  Are you SURE you want continue the installation?\n\n"
175102993Sfenner		 "If you're running this on a disk with data you wish to save\n"
176102993Sfenner		 "then WE STRONGLY ENCOURAGE YOU TO MAKE PROPER BACKUPS before\n"
177102993Sfenner		 "proceeding!\n\n"
178102993Sfenner		 "We can take no responsibility for lost disk contents!"))
17970856Sjhb	return DITEM_FAILURE | DITEM_RESTORE;
180102993Sfenner
181102993Sfenner    if (DITEM_STATUS(diskLabelCommit(NULL)) != DITEM_SUCCESS) {
182102993Sfenner	msgConfirm("Couldn't make filesystems properly.  Aborting.");
183102993Sfenner	return DITEM_FAILURE;
184102993Sfenner    }
185102993Sfenner    else if (isDebug())
186102993Sfenner	msgDebug("installInitial: Scribbled successfully on the disk(s)\n");
187102993Sfenner
188100284Sdougb    if (!copySelf()) {
189102993Sfenner	msgConfirm("Couldn't clone the boot floppy onto the root file system.\n"
190102993Sfenner		   "Aborting.");
191102993Sfenner	return DITEM_FAILURE;
192102993Sfenner    }
193102993Sfenner
194102993Sfenner    if (chroot("/mnt") == -1) {
195102993Sfenner	msgConfirm("Unable to chroot to /mnt - this is bad!");
196102993Sfenner	return DITEM_FAILURE;
197102993Sfenner    }
198102993Sfenner
199102993Sfenner    chdir("/");
200    variable_set2(RUNNING_ON_ROOT, "yes");
201
202    /* stick a helpful shell over on the 4th VTY */
203    systemCreateHoloshell();
204
205    alreadyDone = TRUE;
206    return DITEM_SUCCESS;
207}
208
209int
210installFixitCDROM(dialogMenuItem *self)
211{
212    msgConfirm("Sorry, this feature is currently unimplemented but will,\n"
213	       "at some point in the future, support the use of the live\n"
214	       "filesystem CD (CD 2) in fixing your system.");
215    return DITEM_SUCCESS;
216}
217
218int
219installFixitFloppy(dialogMenuItem *self)
220{
221    struct ufs_args args;
222    pid_t child;
223    int waitstatus;
224
225    variable_set2(SYSTEM_STATE, "fixit");
226    memset(&args, 0, sizeof(args));
227    args.fspec = "/dev/fd0";
228    Mkdir("/mnt2", NULL);
229
230    while (1) {
231	msgConfirm("Please insert a writable fixit floppy and press return");
232	if (mount(MOUNT_UFS, "/mnt2", 0, (caddr_t)&args) != -1)
233	    break;
234	if (msgYesNo("Unable to mount the fixit floppy - do you want to try again?"))
235	    return DITEM_FAILURE;
236    }
237    dialog_clear();
238    dialog_update();
239    end_dialog();
240    DialogActive = FALSE;
241    if (!directory_exists("/tmp"))
242	(void)symlink("/mnt2/tmp", "/tmp");
243    if (!directory_exists("/var/tmp/vi.recover")) {
244	if (DITEM_STATUS(Mkdir("/var/tmp/vi.recover", NULL)) != DITEM_SUCCESS) {
245	    msgConfirm("Warning:  Was unable to create a /var/tmp/vi.recover directory.\n"
246		       "vi will kvetch and moan about it as a result but should still\n"
247		       "be essentially usable.");
248	}
249    }
250    /* Link the spwd.db file */
251    if (DITEM_STATUS(Mkdir("/etc", NULL)) != DITEM_SUCCESS)
252	msgConfirm("Unable to create an /etc directory!  Things are weird on this floppy..");
253    else if (symlink("/mnt2/etc/spwd.db", "/etc/spwd.db") == -1)
254	msgConfirm("Couldn't symlink the /etc/spwd.db file!  I'm not sure I like this..");
255    if (!file_readable(TERMCAP_FILE))
256	create_termcap();
257    if (!(child = fork())) {
258	struct termios foo;
259
260	signal(SIGTTOU, SIG_IGN);
261	if (tcgetattr(0, &foo) != -1) {
262	    foo.c_cc[VERASE] = '\010';
263	    if (tcsetattr(0, TCSANOW, &foo) == -1)
264		msgDebug("fixit shell: Unable to set erase character.\n");
265	}
266	else
267	    msgDebug("fixit shell: Unable to get terminal attributes!\n");
268	printf("When you're finished with this shell, please type exit.\n");
269	printf("The fixit floppy itself is mounted as /mnt2\n");
270	setenv("PATH", "/bin:/sbin:/usr/bin:/usr/sbin:/stand:/mnt2/stand", 1);
271	execlp("sh", "-sh", 0);
272	msgDebug("fixit shell: Failed to execute shell!\n");
273	return -1;
274    }
275    else
276	(void)waitpid(child, &waitstatus, 0);
277
278    DialogActive = TRUE;
279    clear();
280    dialog_clear();
281    dialog_update();
282    unmount("/mnt2", MNT_FORCE);
283    msgConfirm("Please remove the fixit floppy now.");
284    return DITEM_SUCCESS;
285}
286
287int
288installExpress(dialogMenuItem *self)
289{
290    int i;
291
292    variable_set2(SYSTEM_STATE, "express");
293    if (DITEM_STATUS((i = diskPartitionEditor(self))) == DITEM_FAILURE)
294	return i;
295
296    if (DITEM_STATUS((i = diskLabelEditor(self))) == DITEM_FAILURE)
297	return i;
298
299    if (!Dists) {
300	dialog_clear();
301	if (!dmenuOpenSimple(&MenuDistributions) && !Dists)
302	    return DITEM_FAILURE | DITEM_RESTORE | DITEM_RECREATE;
303    }
304
305    if (!mediaDevice) {
306	dialog_clear();
307	if (!dmenuOpenSimple(&MenuMedia) || !mediaDevice)
308	    return DITEM_FAILURE | DITEM_RESTORE | DITEM_RECREATE;
309    }
310
311    if (DITEM_STATUS((i = installCommit(self))) == DITEM_SUCCESS) {
312	i |= DITEM_LEAVE_MENU;
313	/* Give user the option of one last configuration spree */
314	installConfigure();
315
316	/* Now write out any changes .. */
317	configResolv();
318	configSysconfig();
319    }
320    return i | DITEM_RESTORE | DITEM_RECREATE;
321}
322
323/* Novice mode installation */
324int
325installNovice(dialogMenuItem *self)
326{
327    int i;
328    extern int cdromMounted;
329
330    variable_set2(SYSTEM_STATE, "novice");
331    dialog_clear();
332    msgConfirm("In the next menu, you will need to set up a DOS-style (\"fdisk\") partitioning\n"
333	       "scheme for your hard disk.  If you simply wish to devote all disk space\n"
334	       "to FreeBSD (overwritting anything else that might be on the disk(s) selected)\n"
335	       "then use the (A)ll command to select the default partitioning scheme followed\n"
336	       "by a (Q)uit.  If you wish to allocate only free space to FreeBSD, move to a\n"
337	       "partition marked \"unused\" and use the (C)reate command.");
338
339    if (DITEM_STATUS(diskPartitionEditor(self)) == DITEM_FAILURE)
340	return DITEM_FAILURE;
341
342    dialog_clear();
343    msgConfirm("Next, you need to create BSD partitions inside of the fdisk partition(s)\n"
344	       "just created.  If you have a reasonable amount of disk space (200MB or more)\n"
345	       "and don't have any special requirements, simply use the (A)uto command to\n"
346	       "allocate space automatically.  If you have more specific needs or just don't\n"
347	       "care for the layout chosen by (A)uto, press F1 for more information on\n"
348	       "manual layout.");
349
350    if (DITEM_STATUS(diskLabelEditor(self)) == DITEM_FAILURE)
351	return DITEM_FAILURE;
352
353    dialog_clear();
354    msgConfirm("Now it is time to select an installation subset.  There are a number of\n"
355	       "canned distribution sets, ranging from minimal installation sets to full\n"
356	       "X11 developer oriented configurations.  You can also select a custom set\n"
357	       "of distributions if none of the provided ones are suitable.");
358    while (1) {
359	if (!dmenuOpenSimple(&MenuDistributions) && !Dists)
360	    return DITEM_FAILURE | DITEM_RESTORE | DITEM_RECREATE;
361
362	if (Dists || !msgYesNo("No distributions selected.  Are you sure you wish to continue?"))
363	    break;
364    }
365
366    if (!mediaDevice) {
367	dialog_clear();
368	msgConfirm("Finally, you must specify an installation medium.");
369	if (!dmenuOpenSimple(&MenuMedia) || !mediaDevice)
370	    return DITEM_FAILURE | DITEM_RESTORE | DITEM_RECREATE;
371    }
372
373    if (DITEM_STATUS((i = installCommit(self))) == DITEM_FAILURE) {
374	msgConfirm("Installation completed with some errors.  You may wish to\n"
375		   "scroll through the debugging messages on VTY1 with the\n"
376		   "scroll-lock feature.  You can also chose \"No\" at the next\n"
377		   "prompt and go back into the installation menus to try and retry\n"
378		   "whichever operations have failed.");
379	return i | DITEM_RESTORE | DITEM_RECREATE;
380
381    }
382    else
383	msgConfirm("Congradulations!  You now have FreeBSD installed on your system.\n\n"
384		   "We will now move on to the final configuration questions.\n"
385		   "For any option you do not wish to configure, simply select\n"
386		   "No.\n\n"
387		   "If you wish to re-enter this utility after the system is up, you\n"
388		   "may do so by typing: /stand/sysinstall.");
389
390    if (mediaDevice->type != DEVICE_TYPE_FTP && mediaDevice->type != DEVICE_TYPE_NFS) {
391	if (!msgYesNo("Does this system have a network interface card?")) {
392	    Device *save = mediaDevice;
393
394	    /* This will also set the media device, which we don't want */
395	    tcpDeviceSelect();
396	    /* so we restore our saved value below */
397	    mediaDevice = save;
398	    dialog_clear();
399	}
400    }
401
402    if (!msgYesNo("Would you like to configure Samba for connecting NETBUI clients to this\n"
403		  "machine?  Windows 95, Windows NT and Windows for Workgroups\n"
404		  "machines can use NETBUI transport for disk and printer sharing."))
405	configSamba(self);
406
407    if (!msgYesNo("Will this machine be an IP gateway (e.g. will it forward packets\n"
408		  "between interfaces)?"))
409	variable_set2("gateway", "YES");
410
411    if (!msgYesNo("Do you want to allow anonymous FTP connections to this machine?"))
412	configAnonFTP(self);
413
414    if (!msgYesNo("Do you want to configure this machine as an NFS server?"))
415	configNFSServer(self);
416
417    if (!msgYesNo("Do you want to configure this machine as an NFS client?"))
418	variable_set2("nfs_client", "YES");
419
420    if (!msgYesNo("Do you want to configure this machine as a WEB server?"))
421	configApache(self);
422
423    if (!msgYesNo("Would you like to customize your system console settings?")) {
424	WINDOW *w = savescr();
425
426	dmenuOpenSimple(&MenuSyscons);
427	restorescr(w);
428    }
429
430    if (!msgYesNo("Would you like to set this machine's time zone now?")) {
431	WINDOW *w = savescr();
432
433	dialog_clear();
434	systemExecute("rm -f /etc/wall_cmos_clock /etc/localtime; tzsetup");
435	restorescr(w);
436    }
437
438    if (!msgYesNo("Does this system have a mouse attached to it?")) {
439	WINDOW *w = savescr();
440
441	dmenuOpenSimple(&MenuMouse);
442	restorescr(w);
443    }
444
445    if (directory_exists("/usr/X11R6")) {
446	if (!msgYesNo("Would you like to configure your X server at this time?"))
447	    configXFree86(self);
448    }
449
450    if (cdromMounted) {
451	if (!msgYesNo("Would you like to link to the ports tree on your CDROM?\n\n"
452		      "This will require that you have your FreeBSD CD in the CDROM\n"
453		      "drive to use the ports collection, but at a substantial savings\n"
454		      "in disk space (NOTE:  This may take as long as 15 or 20 minutes\n"
455		      "depending on the speed of your CDROM drive)."))
456	    configPorts(self);
457    }
458
459    if (!msgYesNo("The FreeBSD package collection is a collection of over 450 ready-to-run\n"
460		  "applications, from text editors to games to WEB servers.  Would you like\n"
461		  "to browse the collection now?"))
462	configPackages(self);
463
464    /* XXX Put whatever other nice configuration questions you'd like to ask the user here XXX */
465
466    /* Give user the option of one last configuration spree */
467    installConfigure();
468
469    /* Now write out any changes .. */
470    configResolv();
471    configSysconfig();
472
473    return DITEM_LEAVE_MENU | DITEM_RESTORE | DITEM_RECREATE;
474}
475
476/* The version of commit we call from the Install Custom menu */
477int
478installCustomCommit(dialogMenuItem *self)
479{
480    int i;
481
482    i = installCommit(self);
483    if (DITEM_STATUS(i) == DITEM_SUCCESS) {
484	/* Give user the option of one last configuration spree */
485	installConfigure();
486
487	/* Now write out any changes .. */
488	configResolv();
489	configSysconfig();
490	return i;
491    }
492    else
493	msgConfirm("The commit operation completed with errors.  Not\n"
494		   "updating /etc files.");
495    return i;
496}
497
498/*
499 * What happens when we finally decide to going ahead with the installation.
500 *
501 * This is broken into multiple stages so that the user can do a full
502 * installation but come back here again to load more distributions,
503 * perhaps from a different media type.  This would allow, for
504 * example, the user to load the majority of the system from CDROM and
505 * then use ftp to load just the DES dist.
506 */
507int
508installCommit(dialogMenuItem *self)
509{
510    int i;
511    char *str;
512
513    if (!mediaVerify())
514	return DITEM_FAILURE;
515
516    str = variable_get(SYSTEM_STATE);
517    if (isDebug())
518	msgDebug("installCommit: System state is `%s'\n", str);
519
520    if (RunningAsInit) {
521	/* Do things we wouldn't do to a multi-user system */
522	if (DITEM_STATUS((i = installInitial())) == DITEM_FAILURE)
523	    return i;
524	if (DITEM_STATUS((i = configFstab())) == DITEM_FAILURE)
525	    return i;
526    }
527
528    i = distExtractAll(self);
529    if (DITEM_STATUS(i) == DITEM_FAILURE)
530    	(void)installFixup(self);
531    else
532    	i = installFixup(self);
533
534    /* Don't print this if we're express or novice installing - they have their own error reporting */
535    if (strcmp(str, "express") && strcmp(str, "novice")) {
536	if (Dists || DITEM_STATUS(i) == DITEM_FAILURE)
537	    msgConfirm("Installation completed with some errors.  You may wish to\n"
538		       "scroll through the debugging messages on VTY1 with the\n"
539		       "scroll-lock feature.");
540	else
541	    msgConfirm("Installation completed successfully.\n\n"
542		       "If you have any network devices you have not yet configured,\n"
543		       "see the Interfaces configuration item on the Configuration menu.");
544    }
545    variable_set2(SYSTEM_STATE, DITEM_STATUS(i) == DITEM_FAILURE ? "error-install" : "full-install");
546    return i | DITEM_RESTORE | DITEM_RECREATE;
547}
548
549static void
550installConfigure(void)
551{
552    /* Final menu of last resort */
553    if (!msgYesNo("Visit the general configuration menu for a chance to set\n"
554		  "any last options?")) {
555	WINDOW *w = savescr();
556
557	dmenuOpenSimple(&MenuConfigure);
558	restorescr(w);
559    }
560}
561
562int
563installFixup(dialogMenuItem *self)
564{
565    Device **devs;
566    int i;
567
568    if (!file_readable("/kernel")) {
569	if (file_readable("/kernel.GENERIC")) {
570	    if (vsystem("cp -p /kernel.GENERIC /kernel")) {
571		msgConfirm("Unable to link /kernel into place!");
572		return DITEM_FAILURE;
573	    }
574	}
575	else {
576	    msgConfirm("Can't find a kernel image to link to on the root file system!\n"
577		       "You're going to have a hard time getting this system to\n"
578		       "boot from the hard disk, I'm afraid!");
579	    return DITEM_FAILURE;
580	}
581    }
582    /* Resurrect /dev after bin distribution screws it up */
583    if (RunningAsInit) {
584	msgNotify("Remaking all devices.. Please wait!");
585	if (vsystem("cd /dev; sh MAKEDEV all")) {
586	    msgConfirm("MAKEDEV returned non-zero status");
587	    return DITEM_FAILURE;
588	}
589
590	msgNotify("Resurrecting /dev entries for slices..");
591	devs = deviceFind(NULL, DEVICE_TYPE_DISK);
592	if (!devs)
593	    msgFatal("Couldn't get a disk device list!");
594
595	/* Resurrect the slices that the former clobbered */
596	for (i = 0; devs[i]; i++) {
597	    Disk *disk = (Disk *)devs[i]->private;
598	    Chunk *c1;
599
600	    if (!devs[i]->enabled)
601		continue;
602	    if (!disk->chunks)
603		msgFatal("No chunk list found for %s!", disk->name);
604	    for (c1 = disk->chunks->part; c1; c1 = c1->next) {
605		if (c1->type == freebsd) {
606		    msgNotify("Making slice entries for %s", c1->name);
607		    if (vsystem("cd /dev; sh MAKEDEV %sh", c1->name)) {
608			msgConfirm("Unable to make slice entries for %s!", c1->name);
609			return DITEM_FAILURE;
610		    }
611		}
612	    }
613	}
614	/* XXX Do all the last ugly work-arounds here which we'll try and excise someday right?? XXX */
615
616	msgNotify("Fixing permissions..");
617	/* BOGON #1:  XFree86 extracting /usr/X11R6 with root-only perms */
618	if (directory_exists("/usr/X11R6")) {
619	    vsystem("chmod -R a+r /usr/X11R6");
620	    vsystem("find /usr/X11R6 -type d | xargs chmod a+x");
621	}
622	/* BOGON #2: We leave /etc in a bad state */
623	chmod("/etc", 0755);
624
625	/* BOGON #3: No /var/db/mountdtab complains */
626	Mkdir("/var/db", NULL);
627	creat("/var/db/mountdtab", 0644);
628
629	/* Now run all the mtree stuff to fix things up */
630        vsystem("mtree -deU -f /etc/mtree/BSD.root.dist -p /");
631        vsystem("mtree -deU -f /etc/mtree/BSD.var.dist -p /var");
632        vsystem("mtree -deU -f /etc/mtree/BSD.usr.dist -p /usr");
633    }
634    return DITEM_SUCCESS;
635}
636
637/* Go newfs and/or mount all the filesystems we've been asked to */
638int
639installFilesystems(dialogMenuItem *self)
640{
641    int i;
642    Disk *disk;
643    Chunk *c1, *c2, *rootdev, *swapdev, *usrdev;
644    Device **devs;
645    PartInfo *root;
646    char dname[80], *str;
647    extern int MakeDevChunk(Chunk *c, char *n);
648    Boolean upgrade = FALSE;
649
650    str = variable_get(SYSTEM_STATE);
651
652    if (!checkLabels(&rootdev, &swapdev, &usrdev))
653	return DITEM_FAILURE;
654
655    root = (PartInfo *)rootdev->private_data;
656    command_clear();
657    upgrade = str && !strcmp(str, "upgrade");
658
659    /* As the very first thing, try to get ourselves some swap space */
660    sprintf(dname, "/dev/%s", swapdev->name);
661    if (!Fake && (!MakeDevChunk(swapdev, "/dev") || !file_readable(dname))) {
662	msgConfirm("Unable to make device node for %s in /dev!\n"
663		   "The creation of filesystems will be aborted.", dname);
664	return DITEM_FAILURE;
665    }
666    if (!Fake && !swapon(dname))
667	msgNotify("Added %s as initial swap device", dname);
668    else if (!Fake)
669	msgConfirm("WARNING!  Unable to swap to %s: %s\n"
670		   "This may cause the installation to fail at some point\n"
671		   "if you don't have a lot of memory.", dname, strerror(errno));
672
673    /* Next, create and/or mount the root device */
674    sprintf(dname, "/dev/r%sa", rootdev->disk->name);
675    if (!Fake && (!MakeDevChunk(rootdev, "/dev") || !file_readable(dname))) {
676	msgConfirm("Unable to make device node for %s in /dev!\n"
677		   "The creation of filesystems will be aborted.", dname);
678	return DITEM_FAILURE;
679    }
680
681    if (strcmp(root->mountpoint, "/"))
682	msgConfirm("Warning: %s is marked as a root partition but is mounted on %s", rootdev->name, root->mountpoint);
683
684    if (root->newfs) {
685	int i;
686
687	msgNotify("Making a new root filesystem on %s", dname);
688	i = vsystem("%s %s", root->newfs_cmd, dname);
689	if (i) {
690	    msgConfirm("Unable to make new root filesystem on %s!\n"
691		       "Command returned status %d", dname, i);
692	    return DITEM_FAILURE;
693	}
694    }
695    else {
696	if (!upgrade) {
697	    msgConfirm("Warning:  Root device is selected read-only.  It will be assumed\n"
698		       "that you have the appropriate device entries already in /dev.");
699	}
700	msgNotify("Checking integrity of existing %s filesystem.", dname);
701	i = vsystem("fsck -y %s", dname);
702	if (i)
703	    msgConfirm("Warning: fsck returned status of %d for %s.\n"
704		       "This partition may be unsafe to use.", i, dname);
705    }
706    /* Switch to block device */
707    sprintf(dname, "/dev/%sa", rootdev->disk->name);
708    if (Mount("/mnt", dname)) {
709	msgConfirm("Unable to mount the root file system on %s!  Giving up.", dname);
710	return DITEM_FAILURE;
711    }
712
713    /* Now buzz through the rest of the partitions and mount them too */
714    devs = deviceFind(NULL, DEVICE_TYPE_DISK);
715    for (i = 0; devs[i]; i++) {
716	if (!devs[i]->enabled)
717	    continue;
718
719	disk = (Disk *)devs[i]->private;
720	if (!disk->chunks) {
721	    msgConfirm("No chunk list found for %s!", disk->name);
722	    return DITEM_FAILURE;
723	}
724	if (root->newfs || upgrade) {
725	    Mkdir("/mnt/dev", NULL);
726	    if (!Fake)
727		MakeDevDisk(disk, "/mnt/dev");
728	}
729
730	for (c1 = disk->chunks->part; c1; c1 = c1->next) {
731	    if (c1->type == freebsd) {
732		for (c2 = c1->part; c2; c2 = c2->next) {
733		    if (c2->type == part && c2->subtype != FS_SWAP && c2->private_data) {
734			PartInfo *tmp = (PartInfo *)c2->private_data;
735
736			/* Already did root */
737			if (c2 == rootdev)
738			    continue;
739
740			if (tmp->newfs)
741			    command_shell_add(tmp->mountpoint, "%s /mnt/dev/r%s", tmp->newfs_cmd, c2->name);
742			else
743			    command_shell_add(tmp->mountpoint, "fsck -y /mnt/dev/r%s", c2->name);
744			command_func_add(tmp->mountpoint, Mount, c2->name);
745		    }
746		    else if (c2->type == part && c2->subtype == FS_SWAP) {
747			char fname[80];
748			int i;
749
750			if (c2 == swapdev)
751			    continue;
752			sprintf(fname, "/mnt/dev/%s", c2->name);
753			i = (Fake || swapon(fname));
754			if (!i)
755			    msgNotify("Added %s as an additional swap device", fname);
756			else
757			    msgConfirm("Unable to add %s as a swap device: %s", fname, strerror(errno));
758		    }
759		}
760	    }
761	    else if (c1->type == fat && c1->private_data && (root->newfs || upgrade)) {
762		char name[FILENAME_MAX];
763
764		sprintf(name, "/mnt%s", ((PartInfo *)c1->private_data)->mountpoint);
765		Mkdir(name, NULL);
766	    }
767	}
768    }
769
770    msgNotify("Copying initial device files..");
771    /* Copy the boot floppy's dev files */
772    if ((root->newfs || upgrade) && vsystem("find -x /dev | cpio %s -pdum /mnt", cpioVerbosity())) {
773	msgConfirm("Couldn't clone the /dev files!");
774	return DITEM_FAILURE;
775    }
776
777    command_sort();
778    command_execute();
779    return DITEM_SUCCESS;
780}
781
782/* Initialize various user-settable values to their defaults */
783int
784installVarDefaults(dialogMenuItem *self)
785{
786    /* Set default startup options */
787    variable_set2(VAR_ROUTEDFLAGS,		"-q");
788    variable_set2(VAR_RELNAME,			RELEASE_NAME);
789    variable_set2(VAR_CPIO_VERBOSITY,		"high");
790    variable_set2(VAR_TAPE_BLOCKSIZE,		DEFAULT_TAPE_BLOCKSIZE);
791    if (RunningAsInit)
792    	variable_set2(VAR_EDITOR,		"/usr/bin/ee");
793    else {
794	char *cp = getenv("EDITOR");
795
796	if (!cp)
797	    cp = "/usr/bin/ee";
798    	variable_set2(VAR_EDITOR,		cp);
799    }
800    variable_set2(VAR_FTP_USER,			"ftp");
801    variable_set2(VAR_BROWSER_PACKAGE,		"lynx-2.5FM");
802    variable_set2(VAR_BROWSER_BINARY,		"/usr/local/bin/lynx");
803    variable_set2(VAR_CONFIG_FILE,		"freebsd.cfg");
804    variable_set2(VAR_FTP_STATE,		"passive");
805    variable_set2(VAR_FTP_ONERROR,		"abort");
806    variable_set2(VAR_FTP_RETRIES,		MAX_FTP_RETRIES);
807    variable_set2(VAR_PKG_TMPDIR,		"/usr/tmp");
808    if (getpid() != 1)
809	variable_set2(SYSTEM_STATE,		"update");
810    else
811	variable_set2(SYSTEM_STATE,		"init");
812    return DITEM_SUCCESS;
813}
814
815/* Copy the boot floppy contents into /stand */
816Boolean
817copySelf(void)
818{
819    int i;
820
821    msgWeHaveOutput("Copying the boot floppy to /stand on root filesystem");
822    i = vsystem("find -x /stand | cpio %s -pdum /mnt", cpioVerbosity());
823    if (i) {
824	msgConfirm("Copy returned error status of %d!", i);
825	return FALSE;
826    }
827
828    /* Copy the /etc files into their rightful place */
829    if (vsystem("cd /mnt/stand; find etc | cpio %s -pdum /mnt", cpioVerbosity())) {
830	msgConfirm("Couldn't copy up the /etc files!");
831	return TRUE;
832    }
833    return TRUE;
834}
835
836static void
837create_termcap(void)
838{
839    FILE *fp;
840
841    const char *caps[] = {
842	termcap_vt100, termcap_cons25, termcap_cons25_m, termcap_cons25r,
843	termcap_cons25r_m, termcap_cons25l1, termcap_cons25l1_m, NULL,
844    };
845    const char **cp;
846
847    if (!file_readable(TERMCAP_FILE)) {
848	Mkdir("/usr/share/misc", NULL);
849	fp = fopen(TERMCAP_FILE, "w");
850	if (!fp) {
851	    msgConfirm("Unable to initialize termcap file. Some screen-oriented\nutilities may not work.");
852	    return;
853	}
854	cp = caps;
855	while (*cp)
856	    fprintf(fp, "%s\n", *(cp++));
857	fclose(fp);
858    }
859}
860
861