install.c revision 30345
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 * $Id: install.c,v 1.200 1997/10/06 08:36:12 jkh Exp $
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 "uc_main.h"
39#include <ctype.h>
40#include <sys/disklabel.h>
41#include <sys/errno.h>
42#include <sys/ioctl.h>
43#include <sys/fcntl.h>
44#include <sys/wait.h>
45#include <sys/param.h>
46#define MSDOSFS
47#include <sys/mount.h>
48#include <ufs/ufs/ufsmount.h>
49#include <msdosfs/msdosfsmount.h>
50#undef MSDOSFS
51#include <sys/stat.h>
52#include <unistd.h>
53
54static void	create_termcap(void);
55static void	fixit_common(void);
56#ifdef SAVE_USERCONFIG
57static void	save_userconfig_to_kernel(char *);
58#endif
59
60#define TERMCAP_FILE	"/usr/share/misc/termcap"
61
62static void	installConfigure(void);
63
64Boolean
65checkLabels(Boolean whinge, Chunk **rdev, Chunk **sdev, Chunk **udev, Chunk **vdev)
66{
67    Device **devs;
68    Boolean status;
69    Disk *disk;
70    Chunk *c1, *c2, *rootdev, *swapdev, *usrdev, *vardev;
71    int i;
72
73    /* Don't allow whinging if noWarn is set */
74    if (variable_get(VAR_NO_WARN))
75	whinge = FALSE;
76
77    status = TRUE;
78    *rdev = *sdev = *udev = *vdev = rootdev = swapdev = usrdev = vardev = NULL;
79
80    /* We don't need to worry about root/usr/swap if we're already multiuser */
81    if (!RunningAsInit)
82	return status;
83
84    devs = deviceFind(NULL, DEVICE_TYPE_DISK);
85    /* First verify that we have a root device */
86    for (i = 0; devs[i]; i++) {
87	if (!devs[i]->enabled)
88	    continue;
89	disk = (Disk *)devs[i]->private;
90	msgDebug("Scanning disk %s for root filesystem\n", disk->name);
91	if (!disk->chunks)
92	    msgFatal("No chunk list found for %s!", disk->name);
93	for (c1 = disk->chunks->part; c1; c1 = c1->next) {
94	    if (c1->type == freebsd) {
95		for (c2 = c1->part; c2; c2 = c2->next) {
96		    if (c2->type == part && c2->subtype != FS_SWAP && c2->private_data) {
97			if (c2->flags & CHUNK_IS_ROOT) {
98			    if (rootdev) {
99				if (whinge)
100				    msgConfirm("WARNING:  You have more than one root device set?!\n"
101					       "Using the first one found.");
102				continue;
103			    }
104			    else {
105				rootdev = c2;
106				if (isDebug())
107				    msgDebug("Found rootdev at %s!\n", rootdev->name);
108			    }
109			}
110			else if (!strcmp(((PartInfo *)c2->private_data)->mountpoint, "/usr")) {
111			    if (usrdev) {
112				if (whinge)
113				    msgConfirm("WARNING:  You have more than one /usr filesystem.\n"
114					       "Using the first one found.");
115				continue;
116			    }
117			    else {
118				usrdev = c2;
119				if (isDebug())
120				    msgDebug("Found usrdev at %s!\n", usrdev->name);
121			    }
122			}
123			else if (!strcmp(((PartInfo *)c2->private_data)->mountpoint, "/var")) {
124			    if (vardev) {
125				if (whinge)
126				    msgConfirm("WARNING:  You have more than one /var filesystem.\n"
127					       "Using the first one found.");
128				continue;
129			    }
130			    else {
131				vardev = c2;
132				if (isDebug())
133				    msgDebug("Found vardev at %s!\n", vardev->name);
134			    }
135			}
136		    }
137		}
138	    }
139	}
140    }
141
142    /* Now check for swap devices */
143    for (i = 0; devs[i]; i++) {
144	if (!devs[i]->enabled)
145	    continue;
146	disk = (Disk *)devs[i]->private;
147	msgDebug("Scanning disk %s for swap partitions\n", disk->name);
148	if (!disk->chunks)
149	    msgFatal("No chunk list found for %s!", disk->name);
150	for (c1 = disk->chunks->part; c1; c1 = c1->next) {
151	    if (c1->type == freebsd) {
152		for (c2 = c1->part; c2; c2 = c2->next) {
153		    if (c2->type == part && c2->subtype == FS_SWAP && !swapdev) {
154			swapdev = c2;
155			if (isDebug())
156			    msgDebug("Found swapdev at %s!\n", swapdev->name);
157			break;
158		    }
159		}
160	    }
161	}
162    }
163
164    /* Copy our values over */
165    *rdev = rootdev;
166    *sdev = swapdev;
167    *udev = usrdev;
168    *vdev = vardev;
169
170    if (!rootdev && whinge) {
171	msgConfirm("No root device found - you must label a partition as /\n"
172		   "in the label editor.");
173	status = FALSE;
174    }
175    if (!swapdev && whinge) {
176	msgConfirm("No swap devices found - you must create at least one\n"
177		   "swap partition.");
178	status = FALSE;
179    }
180    if (!usrdev && whinge) {
181	msgConfirm("WARNING:  No /usr filesystem found.  This is not technically\n"
182		   "an error if your root filesystem is big enough (or you later\n"
183		   "intend to mount your /usr filesystem over NFS), but it may otherwise\n"
184		   "cause you trouble if you're not exactly sure what you are doing!");
185    }
186    if (!vardev && whinge && variable_cmp(SYSTEM_STATE, "upgrade")) {
187	msgConfirm("WARNING:  No /var filesystem found.  This is not technically\n"
188		   "an error if your root filesystem is big enough (or you later\n"
189		   "intend to link /var to someplace else), but it may otherwise\n"
190		   "cause your root filesystem to fill up if you receive lots of mail\n"
191		   "or edit large temporary files.");
192    }
193    return status;
194}
195
196static int
197installInitial(void)
198{
199    static Boolean alreadyDone = FALSE;
200
201    if (alreadyDone)
202	return DITEM_SUCCESS;
203
204    if (!variable_get(DISK_LABELLED)) {
205	msgConfirm("You need to assign disk labels before you can proceed with\n"
206		   "the installation.");
207	return DITEM_FAILURE;
208    }
209    /* If it's labelled, assume it's also partitioned */
210    if (!variable_get(DISK_PARTITIONED))
211	variable_set2(DISK_PARTITIONED, "yes");
212
213    /* If we refuse to proceed, bail. */
214    dialog_clear_norefresh();
215    if (msgYesNo("Last Chance!  Are you SURE you want continue the installation?\n\n"
216		 "If you're running this on a disk with data you wish to save\n"
217		 "then WE STRONGLY ENCOURAGE YOU TO MAKE PROPER BACKUPS before\n"
218		 "proceeding!\n\n"
219		 "We can take no responsibility for lost disk contents!") != 0)
220	return DITEM_FAILURE | DITEM_RESTORE;
221
222    if (DITEM_STATUS(diskLabelCommit(NULL)) != DITEM_SUCCESS) {
223	msgConfirm("Couldn't make filesystems properly.  Aborting.");
224	return DITEM_FAILURE;
225    }
226    else if (isDebug())
227	msgDebug("installInitial: Scribbled successfully on the disk(s)\n");
228
229    if (!copySelf()) {
230	msgConfirm("Couldn't clone the boot floppy onto the root file system.\n"
231		   "Aborting.");
232	return DITEM_FAILURE;
233    }
234
235    if (chroot("/mnt") == -1) {
236	msgConfirm("Unable to chroot to %s - this is bad!", "/mnt");
237	return DITEM_FAILURE;
238    }
239
240    chdir("/");
241    variable_set2(RUNNING_ON_ROOT, "yes");
242    configResolv();
243
244    /* stick a helpful shell over on the 4th VTY */
245    systemCreateHoloshell();
246
247    alreadyDone = TRUE;
248    return DITEM_SUCCESS;
249}
250
251int
252installFixitHoloShell(dialogMenuItem *self)
253{
254    systemCreateHoloshell();
255    return DITEM_SUCCESS;
256}
257
258int
259installFixitCDROM(dialogMenuItem *self)
260{
261    struct stat sb;
262
263    if (!RunningAsInit)
264	return DITEM_SUCCESS;
265
266    variable_set2(SYSTEM_STATE, "fixit");
267    (void)unlink("/mnt2");
268    (void)rmdir("/mnt2");
269
270    while (1) {
271	msgConfirm("Please insert the second FreeBSD CDROM and press return");
272	if (DITEM_STATUS(mediaSetCDROM(NULL)) != DITEM_SUCCESS || !mediaDevice || !mediaDevice->init(mediaDevice)) {
273	    /* If we can't initialize it, it's probably not a FreeBSD CDROM so punt on it */
274	    if (mediaDevice) {
275		mediaDevice->shutdown(mediaDevice);
276		mediaDevice = NULL;
277	    }
278	    if (msgYesNo("Unable to mount the CDROM - do you want to try again?") != 0)
279		return DITEM_FAILURE;
280	}
281	else
282	    break;
283    }
284
285    /* Since the fixit code expects everything to be in /mnt2, and the CDROM mounting stuff /dist, do
286     * a little kludge dance here..
287     */
288    if (symlink("/dist", "/mnt2")) {
289	msgConfirm("Unable to symlink /mnt2 to the CDROM mount point.  Please report this\n"
290		   "unexpected failure to freebsd-bugs@FreeBSD.org.");
291	return DITEM_FAILURE;
292    }
293
294    /*
295     * If /tmp points to /mnt2/tmp from a previous fixit floppy session, it's
296     * not very good for us if we point it to the CDROM now.  Rather make it
297     * a directory in the root MFS then.  Experienced admins will still be
298     * able to mount their disk's /tmp over this if they need.
299     */
300    if (lstat("/tmp", &sb) == 0 && (sb.st_mode & S_IFMT) == S_IFLNK)
301	(void)unlink("/tmp");
302    Mkdir("/tmp");
303
304    /*
305     * Since setuid binaries ignore LD_LIBRARY_PATH, we indeed need the
306     * ld.so.hints file.  Fortunately, it's fairly small (~ 3 KB).
307     */
308    if (!file_readable("/var/run/ld.so.hints")) {
309	Mkdir("/var/run");
310	if (vsystem("/mnt2/sbin/ldconfig -s /mnt2/usr/lib")) {
311	    msgConfirm("Warning: ldconfig could not create the ld.so hints file.\n"
312		       "Dynamic executables from the CDROM likely won't work.");
313	}
314    }
315
316    /* Yet another iggly hardcoded pathname. */
317    if (!file_readable("/usr/libexec/ld.so")) {
318	Mkdir("/usr/libexec");
319	if (symlink("/mnt2/usr/libexec/ld.so", "/usr/libexec/ld.so")) {
320	    msgConfirm("Warning: could not create the symlink for ld.so.\n"
321		       "Dynamic executables from the CDROM likely won't work.");
322	}
323    }
324
325    fixit_common();
326
327    mediaDevice->shutdown(mediaDevice);
328    msgConfirm("Please remove the FreeBSD CDROM now.");
329    return DITEM_SUCCESS;
330}
331
332int
333installFixitFloppy(dialogMenuItem *self)
334{
335    struct ufs_args args;
336
337    if (!RunningAsInit)
338	return DITEM_SUCCESS;
339
340    variable_set2(SYSTEM_STATE, "fixit");
341    memset(&args, 0, sizeof(args));
342    args.fspec = "/dev/fd0";
343    Mkdir("/mnt2");
344
345    while (1) {
346	msgConfirm("Please insert a writable fixit floppy and press return");
347	if (mount(MOUNT_UFS, "/mnt2", 0, (caddr_t)&args) != -1)
348	    break;
349	msgConfirm("An attempt to mount the fixit floppy failed, maybe the filesystem\n"
350		   "is unclean.  Trying a forcible mount as a last resort...");
351	if (mount(MOUNT_UFS, "/mnt2", MNT_FORCE, (caddr_t)&args) != -1)
352	    break;
353	if (msgYesNo("Unable to mount the fixit floppy - do you want to try again?") != 0)
354	    return DITEM_FAILURE;
355    }
356
357    if (!directory_exists("/tmp"))
358	(void)symlink("/mnt2/tmp", "/tmp");
359
360    fixit_common();
361
362    unmount("/mnt2", MNT_FORCE);
363    msgConfirm("Please remove the fixit floppy now.");
364    return DITEM_SUCCESS;
365}
366
367/*
368 * The common code for both fixit variants.
369 */
370static void
371fixit_common(void)
372{
373    pid_t child;
374    int waitstatus;
375
376    if (!directory_exists("/var/tmp/vi.recover")) {
377	if (DITEM_STATUS(Mkdir("/var/tmp/vi.recover")) != DITEM_SUCCESS) {
378	    msgConfirm("Warning:  Was unable to create a /var/tmp/vi.recover directory.\n"
379		       "vi will kvetch and moan about it as a result but should still\n"
380		       "be essentially usable.");
381	}
382    }
383    if (!directory_exists("/bin"))
384	(void)Mkdir("/bin");
385    (void)symlink("/stand/sh", "/bin/sh");
386    /* Link the /etc/ files */
387    if (DITEM_STATUS(Mkdir("/etc")) != DITEM_SUCCESS)
388	msgConfirm("Unable to create an /etc directory!  Things are weird on this floppy..");
389    else if ((symlink("/mnt2/etc/spwd.db", "/etc/spwd.db") == -1 && errno != EEXIST) ||
390	     (symlink("/mnt2/etc/protocols", "/etc/protocols") == -1 && errno != EEXIST) ||
391	     (symlink("/mnt2/etc/services", "/etc/services") == -1 && errno != EEXIST))
392	msgConfirm("Couldn't symlink the /etc/ files!  I'm not sure I like this..");
393    if (!file_readable(TERMCAP_FILE))
394	create_termcap();
395    if (!(child = fork())) {
396	int i, fd;
397	struct termios foo;
398	extern int login_tty(int);
399
400	ioctl(0, TIOCNOTTY, NULL);
401	for (i = getdtablesize(); i >= 0; --i)
402	    close(i);
403	fd = open("/dev/ttyv3", O_RDWR);
404	ioctl(0, TIOCSCTTY, &fd);
405	dup2(0, 1);
406	dup2(0, 2);
407	DebugFD = 2;
408	if (login_tty(fd) == -1)
409	    msgDebug("fixit: I can't set the controlling terminal.\n");
410
411	signal(SIGTTOU, SIG_IGN);
412	if (tcgetattr(0, &foo) != -1) {
413	    foo.c_cc[VERASE] = '\010';
414	    if (tcsetattr(0, TCSANOW, &foo) == -1)
415		msgDebug("fixit shell: Unable to set erase character.\n");
416	}
417	else
418	    msgDebug("fixit shell: Unable to get terminal attributes!\n");
419	setenv("PATH", "/bin:/sbin:/usr/bin:/usr/sbin:/stand:"
420	       "/mnt2/stand:/mnt2/bin:/mnt2/sbin:/mnt2/usr/bin:/mnt2/usr/sbin", 1);
421	/* use the .profile from the fixit medium */
422	setenv("HOME", "/mnt2", 1);
423	chdir("/mnt2");
424	execlp("sh", "-sh", 0);
425	msgDebug("fixit shell: Failed to execute shell!\n");
426	_exit(1);;
427    }
428    else {
429	msgNotify("Waiting for fixit shell to exit.  Go to VTY4 now by\n"
430		  "typing ALT-F4.  When you are done, type ``exit'' to exit\n"
431		  "the fixit shell and be returned here.");
432	(void)waitpid(child, &waitstatus, 0);
433    }
434    dialog_clear();
435}
436
437
438int
439installExpress(dialogMenuItem *self)
440{
441    int i;
442
443    variable_set2(SYSTEM_STATE, "express");
444    if (DITEM_STATUS((i = diskPartitionEditor(self))) == DITEM_FAILURE)
445	return i;
446
447    if (DITEM_STATUS((i = diskLabelEditor(self))) == DITEM_FAILURE)
448	return i;
449
450    dialog_clear_norefresh();
451    if (DITEM_STATUS((i = installCommit(self))) == DITEM_SUCCESS) {
452	i |= DITEM_LEAVE_MENU;
453	/* Give user the option of one last configuration spree */
454	installConfigure();
455    }
456    return i | DITEM_RESTORE;
457}
458
459/* Novice mode installation */
460int
461installNovice(dialogMenuItem *self)
462{
463    int i, tries = 0;
464    Device **devs;
465
466    variable_set2(SYSTEM_STATE, "novice");
467    dialog_clear_norefresh();
468    msgConfirm("In the next menu, you will need to set up a DOS-style (\"fdisk\") partitioning\n"
469	       "scheme for your hard disk.  If you simply wish to devote all disk space\n"
470	       "to FreeBSD (overwriting anything else that might be on the disk(s) selected)\n"
471	       "then use the (A)ll command to select the default partitioning scheme followed\n"
472	       "by a (Q)uit.  If you wish to allocate only free space to FreeBSD, move to a\n"
473	       "partition marked \"unused\" and use the (C)reate command.");
474
475nodisks:
476    if (DITEM_STATUS(diskPartitionEditor(self)) == DITEM_FAILURE)
477	return DITEM_FAILURE;
478
479    if (diskGetSelectCount(&devs) <= 0 && tries < 3) {
480	msgConfirm("You need to select some disks to operate on!  Be sure to use SPACE\n"
481		   "instead of RETURN in the disk selection menu when selecting a disk.");
482	++tries;
483	goto nodisks;
484    }
485
486    dialog_clear_norefresh();
487    msgConfirm("Next, you need to create BSD partitions inside of the fdisk partition(s)\n"
488	       "just created.  If you have a reasonable amount of disk space (200MB or more)\n"
489	       "and don't have any special requirements, simply use the (A)uto command to\n"
490	       "allocate space automatically.  If you have more specific needs or just don't\n"
491	       "care for the layout chosen by (A)uto, press F1 for more information on\n"
492	       "manual layout.");
493
494    if (DITEM_STATUS(diskLabelEditor(self)) == DITEM_FAILURE)
495	return DITEM_FAILURE;
496
497    dialog_clear_norefresh();
498    if (DITEM_STATUS((i = installCommit(self))) == DITEM_FAILURE) {
499	dialog_clear_norefresh();
500	msgConfirm("Installation completed with some errors.  You may wish to\n"
501		   "scroll through the debugging messages on VTY1 with the\n"
502		   "scroll-lock feature.  You can also chose \"No\" at the next\n"
503		   "prompt and go back into the installation menus to try and retry\n"
504		   "whichever operations have failed.");
505	return i | DITEM_RESTORE;
506
507    }
508    else {
509	dialog_clear_norefresh();
510	msgConfirm("Congratulations!  You now have FreeBSD installed on your system.\n\n"
511		   "We will now move on to the final configuration questions.\n"
512		   "For any option you do not wish to configure, simply select\n"
513		   "No.\n\n"
514		   "If you wish to re-enter this utility after the system is up, you\n"
515		   "may do so by typing: /stand/sysinstall.");
516    }
517    if (mediaDevice->type != DEVICE_TYPE_FTP && mediaDevice->type != DEVICE_TYPE_NFS) {
518	if (!msgYesNo("Would you like to configure any Ethernet or SLIP/PPP network devices?")) {
519	    Device *tmp;
520
521	    dialog_clear_norefresh();
522	    tmp = tcpDeviceSelect();
523	    dialog_clear_norefresh();
524	    if (tmp && !msgYesNo("Would you like to bring the %s interface up right now?", tmp->name))
525		if (!tmp->init(tmp))
526		    msgConfirm("Initialization of %s device failed.", tmp->name);
527	}
528    }
529
530    dialog_clear_norefresh();
531    if (!msgYesNo("Will this machine be an IP gateway (e.g. will it forward packets\n"
532		  "between interfaces)?"))
533	variable_set2("gateway", "YES");
534
535    dialog_clear_norefresh();
536    if (!msgYesNo("Do you want to allow anonymous FTP connections to this machine?"))
537	configAnonFTP(self);
538
539    dialog_clear_norefresh();
540    if (!msgYesNo("Do you want to configure this machine as an NFS server?"))
541	configNFSServer(self);
542
543    dialog_clear_norefresh();
544    if (!msgYesNo("Do you want to configure this machine as an NFS client?"))
545	variable_set2("nfs_client", "YES");
546
547    dialog_clear_norefresh();
548    if (!msgYesNo("Would you like to customize your system console settings?")) {
549	WINDOW *w = savescr();
550
551	dmenuOpenSimple(&MenuSyscons, FALSE);
552	restorescr(w);
553    }
554
555    dialog_clear_norefresh();
556    if (!msgYesNo("Would you like to set this machine's time zone now?")) {
557	WINDOW *w = savescr();
558
559	dialog_clear();
560	systemExecute("tzsetup");
561	restorescr(w);
562    }
563
564    dialog_clear_norefresh();
565    if (!msgYesNo("Does this system have a mouse attached to it?")) {
566	WINDOW *w = savescr();
567
568	dmenuOpenSimple(&MenuMouse, FALSE);
569	restorescr(w);
570    }
571
572    /* Now would be a good time to checkpoint the configuration data */
573    configRC_conf("/etc/rc.conf");
574    sync();
575
576#ifndef USE_XIG_ENVIRONMENT
577    if (directory_exists("/usr/X11R6")) {
578	dialog_clear_norefresh();
579	if (!msgYesNo("Would you like to configure your X server at this time?"))
580	    configXEnvironment(self);
581    }
582#endif
583
584    dialog_clear_norefresh();
585    if (!msgYesNo("The FreeBSD package collection is a collection of hundreds of ready-to-run\n"
586		  "applications, from text editors to games to WEB servers and more.  Would you\n"
587		  "like to browse the collection now?"))
588	configPackages(self);
589
590    dialog_clear_norefresh();
591    if (!msgYesNo("Would you like to add any initial user accounts to the system?\n"
592		  "Adding at least one account for yourself at this stage is suggested\n"
593		  "since working as the \"root\" user is dangerous (it is easy to do\n"
594		  "things which adversely affect the entire system)."))
595	configUsers(self);
596
597    dialog_clear_norefresh();
598    msgConfirm("Now you must set the system manager's password.\n"
599	       "This is the password you'll use to log in as \"root\".");
600    {
601	WINDOW *w = savescr();
602
603	if (!systemExecute("passwd root"))
604	    variable_set2("root_password", "YES");
605	restorescr(w);
606    }
607
608    dialog_clear_norefresh();
609    if (!msgYesNo("Would you like to register your FreeBSD system at this time?\n\n"
610		  "PLEASE, take just 5 minutes to do this.  If we're ever to get any\n"
611		  "significant base of commercial software for FreeBSD, we need to\n"
612		  "be able to provide more information about the size of our user community.\n"
613		  "This is where your registration can really help us, and you can also\n"
614		  "sign up for the new FreeBSD newsletter (its free!) at the same time.\n"))
615	configRegister(NULL);
616    else {
617	dialog_clear_norefresh();
618	msgConfirm("OK, but if you should change your mind then you always can register\n"
619		   "later by typing ``/stand/sysinstall register'' or by simply visiting our\n"
620		   "web site at http://www.freebsd.org/register.html");
621
622    }
623    /* XXX Put whatever other nice configuration questions you'd like to ask the user here XXX */
624
625    /* Give user the option of one last configuration spree */
626    dialog_clear_norefresh();
627    installConfigure();
628
629    return DITEM_LEAVE_MENU | DITEM_RESTORE;
630}
631
632/* The version of commit we call from the Install Custom menu */
633int
634installCustomCommit(dialogMenuItem *self)
635{
636    int i;
637
638    dialog_clear_norefresh();
639    i = installCommit(self);
640    if (DITEM_STATUS(i) == DITEM_SUCCESS) {
641	/* Give user the option of one last configuration spree */
642	installConfigure();
643	return i;
644    }
645    else
646	msgConfirm("The commit operation completed with errors.  Not\n"
647		   "updating /etc files.");
648    return i;
649}
650
651/*
652 * What happens when we finally decide to going ahead with the installation.
653 *
654 * This is broken into multiple stages so that the user can do a full
655 * installation but come back here again to load more distributions,
656 * perhaps from a different media type.  This would allow, for
657 * example, the user to load the majority of the system from CDROM and
658 * then use ftp to load just the DES dist.
659 */
660int
661installCommit(dialogMenuItem *self)
662{
663    int i;
664    char *str;
665    Boolean need_bin;
666
667    if (!Dists)
668	distConfig(NULL);
669
670    if (!Dists)
671	if (!dmenuOpenSimple(&MenuDistributions, FALSE) && !Dists)
672	    return DITEM_FAILURE | DITEM_RESTORE;
673
674    if (!mediaVerify())
675	return DITEM_FAILURE | DITEM_RESTORE;
676
677    str = variable_get(SYSTEM_STATE);
678    if (isDebug())
679	msgDebug("installCommit: System state is `%s'\n", str);
680
681    if (RunningAsInit) {
682	/* Do things we wouldn't do to a multi-user system */
683	if (DITEM_STATUS((i = installInitial())) == DITEM_FAILURE)
684	    return i;
685	if (DITEM_STATUS((i = configFstab())) == DITEM_FAILURE)
686	    return i;
687    }
688
689try_media:
690    if (!mediaDevice->init(mediaDevice)) {
691	if (!msgYesNo("Unable to initialize selected media. Would you like to\n"
692		      "adjust your media configuration and try again?")) {
693	    mediaDevice = NULL;
694	    if (!mediaVerify())
695		return DITEM_FAILURE | DITEM_RESTORE;
696	    else
697		goto try_media;
698	}
699	else
700	    return DITEM_FAILURE | DITEM_RESTORE;
701    }
702
703    need_bin = Dists & DIST_BIN;
704    i = distExtractAll(self);
705    if (DITEM_STATUS(i) == DITEM_SUCCESS) {
706	if (need_bin && !(Dists & DIST_BIN))
707	    i = installFixup(self);
708    }
709    variable_set2(SYSTEM_STATE, DITEM_STATUS(i) == DITEM_FAILURE ? "error-install" : "full-install");
710
711    /* We always try to install X with the XiG product */
712#ifdef USE_XIG_ENVIRONMENT
713    if (directory_exists("/usr/X11R6"))
714	configXEnvironment(self);
715#endif
716    return i | DITEM_RESTORE;
717}
718
719static void
720installConfigure(void)
721{
722    /* Final menu of last resort */
723    dialog_clear_norefresh();
724    if (!msgYesNo("Visit the general configuration menu for a chance to set\n"
725		  "any last options?")) {
726	WINDOW *w = savescr();
727
728	dmenuOpenSimple(&MenuConfigure, FALSE);
729	restorescr(w);
730    }
731    configRC_conf("/etc/rc.conf");
732    sync();
733}
734
735int
736installFixup(dialogMenuItem *self)
737{
738    Device **devs;
739    int i;
740
741    if (!file_readable("/kernel")) {
742	if (file_readable("/kernel.GENERIC")) {
743#ifdef SAVE_USERCONFIG
744	    /* Snapshot any boot -c changes back to the GENERIC kernel */
745	    if (!variable_cmp(VAR_RELNAME, RELEASE_NAME))
746		save_userconfig_to_kernel("/kernel.GENERIC");
747#endif
748	    if (vsystem("cp -p /kernel.GENERIC /kernel")) {
749		msgConfirm("Unable to link /kernel into place!");
750		return DITEM_FAILURE;
751	    }
752	}
753	else {
754	    msgConfirm("Can't find a kernel image to link to on the root file system!\n"
755		       "You're going to have a hard time getting this system to\n"
756		       "boot from the hard disk, I'm afraid!");
757	    return DITEM_FAILURE;
758	}
759    }
760
761    /* Resurrect /dev after bin distribution screws it up */
762    if (RunningAsInit) {
763	msgNotify("Remaking all devices.. Please wait!");
764	if (vsystem("cd /dev; sh MAKEDEV all")) {
765	    msgConfirm("MAKEDEV returned non-zero status");
766	    return DITEM_FAILURE;
767	}
768
769	msgNotify("Resurrecting /dev entries for slices..");
770	devs = deviceFind(NULL, DEVICE_TYPE_DISK);
771	if (!devs)
772	    msgFatal("Couldn't get a disk device list!");
773
774	/* Resurrect the slices that the former clobbered */
775	for (i = 0; devs[i]; i++) {
776	    Disk *disk = (Disk *)devs[i]->private;
777	    Chunk *c1;
778
779	    if (!devs[i]->enabled)
780		continue;
781	    if (!disk->chunks)
782		msgFatal("No chunk list found for %s!", disk->name);
783	    for (c1 = disk->chunks->part; c1; c1 = c1->next) {
784		if (c1->type == freebsd) {
785		    msgNotify("Making slice entries for %s", c1->name);
786		    if (vsystem("cd /dev; sh MAKEDEV %sh", c1->name)) {
787			msgConfirm("Unable to make slice entries for %s!", c1->name);
788			return DITEM_FAILURE;
789		    }
790		}
791	    }
792	}
793	/* XXX Do all the last ugly work-arounds here which we'll try and excise someday right?? XXX */
794
795	msgNotify("Fixing permissions..");
796	/* BOGON #1:  XFree86 extracting /usr/X11R6 with root-only perms */
797	if (directory_exists("/usr/X11R6")) {
798	    vsystem("chmod -R a+r /usr/X11R6");
799	    vsystem("find /usr/X11R6 -type d | xargs chmod a+x");
800	}
801	/* BOGON #2: We leave /etc in a bad state */
802	chmod("/etc", 0755);
803
804	/* BOGON #3: No /var/db/mountdtab complains */
805	Mkdir("/var/db");
806	creat("/var/db/mountdtab", 0644);
807
808	/* BOGON #4: /compat created by default in root fs */
809	Mkdir("/usr/compat");
810	vsystem("ln -s /usr/compat /compat");
811
812	/* BOGON #5: aliases database not build for bin */
813	vsystem("newaliases");
814
815	/* BOGON #6: deal with new boot files */
816	vsystem("touch /kernel.config");
817	vsystem("touch /boot.config");
818	if (file_readable("/stand/boot.help") && !file_readable("/boot.help"))
819	    vsystem("mv /stand/boot.help /");
820
821	/* Now run all the mtree stuff to fix things up */
822        vsystem("mtree -deU -f /etc/mtree/BSD.root.dist -p /");
823        vsystem("mtree -deU -f /etc/mtree/BSD.var.dist -p /var");
824        vsystem("mtree -deU -f /etc/mtree/BSD.usr.dist -p /usr");
825    }
826    return DITEM_SUCCESS;
827}
828
829/* Go newfs and/or mount all the filesystems we've been asked to */
830int
831installFilesystems(dialogMenuItem *self)
832{
833    int i;
834    Disk *disk;
835    Chunk *c1, *c2, *rootdev, *swapdev, *usrdev, *vardev;
836    Device **devs;
837    PartInfo *root;
838    char dname[80];
839    extern int MakeDevChunk(Chunk *c, char *n);
840    Boolean upgrade = FALSE;
841
842    /* If we've already done this, bail out */
843    if (!variable_cmp(DISK_LABELLED, "written"))
844	return DITEM_SUCCESS;
845
846    upgrade = !variable_cmp(SYSTEM_STATE, "upgrade");
847    if (!checkLabels(TRUE, &rootdev, &swapdev, &usrdev, &vardev))
848	return DITEM_FAILURE;
849
850    if (rootdev)
851	root = (PartInfo *)rootdev->private_data;
852    else
853	root = NULL;
854
855    command_clear();
856    if (swapdev && RunningAsInit) {
857	/* As the very first thing, try to get ourselves some swap space */
858	sprintf(dname, "/dev/%s", swapdev->name);
859	if (!Fake && (!MakeDevChunk(swapdev, "/dev") || !file_readable(dname))) {
860	    msgConfirm("Unable to make device node for %s in /dev!\n"
861		       "The creation of filesystems will be aborted.", dname);
862	    return DITEM_FAILURE;
863	}
864
865	if (!Fake) {
866	    if (!swapon(dname))
867		msgNotify("Added %s as initial swap device", dname);
868	    else
869		msgConfirm("WARNING!  Unable to swap to %s: %s\n"
870			   "This may cause the installation to fail at some point\n"
871			   "if you don't have a lot of memory.", dname, strerror(errno));
872	}
873    }
874
875    if (rootdev && RunningAsInit) {
876	/* Next, create and/or mount the root device */
877	sprintf(dname, "/dev/r%sa", rootdev->disk->name);
878	if (!Fake && (!MakeDevChunk(rootdev, "/dev") || !file_readable(dname))) {
879	    msgConfirm("Unable to make device node for %s in /dev!\n"
880		       "The creation of filesystems will be aborted.", dname);
881	    return DITEM_FAILURE;
882	}
883	if (strcmp(root->mountpoint, "/"))
884	    msgConfirm("Warning: %s is marked as a root partition but is mounted on %s", rootdev->name, root->mountpoint);
885
886	if (root->newfs) {
887	    int i;
888
889	    msgNotify("Making a new root filesystem on %s", dname);
890	    i = vsystem("%s %s", root->newfs_cmd, dname);
891	    if (i) {
892		msgConfirm("Unable to make new root filesystem on %s!\n"
893			   "Command returned status %d", dname, i);
894		return DITEM_FAILURE;
895	    }
896	}
897	else {
898	    if (!upgrade) {
899		msgConfirm("Warning:  Using existing root partition.  It will be assumed\n"
900			   "that you have the appropriate device entries already in /dev.");
901	    }
902	    msgNotify("Checking integrity of existing %s filesystem.", dname);
903	    i = vsystem("fsck -y %s", dname);
904	    if (i)
905		msgConfirm("Warning: fsck returned status of %d for %s.\n"
906			   "This partition may be unsafe to use.", i, dname);
907	}
908
909	/* Switch to block device */
910	sprintf(dname, "/dev/%sa", rootdev->disk->name);
911	if (Mount("/mnt", dname)) {
912	    msgConfirm("Unable to mount the root file system on %s!  Giving up.", dname);
913	    return DITEM_FAILURE;
914	}
915    }
916
917    /* Now buzz through the rest of the partitions and mount them too */
918    devs = deviceFind(NULL, DEVICE_TYPE_DISK);
919    for (i = 0; devs[i]; i++) {
920	if (!devs[i]->enabled)
921	    continue;
922
923	disk = (Disk *)devs[i]->private;
924	if (!disk->chunks) {
925	    msgConfirm("No chunk list found for %s!", disk->name);
926	    return DITEM_FAILURE;
927	}
928	if (RunningAsInit && root && (root->newfs || upgrade)) {
929	    Mkdir("/mnt/dev");
930	    if (!Fake)
931		MakeDevDisk(disk, "/mnt/dev");
932	}
933	else if (!RunningAsInit && !Fake)
934	    MakeDevDisk(disk, "/dev");
935
936	for (c1 = disk->chunks->part; c1; c1 = c1->next) {
937	    if (c1->type == freebsd) {
938		for (c2 = c1->part; c2; c2 = c2->next) {
939		    if (c2->type == part && c2->subtype != FS_SWAP && c2->private_data) {
940			PartInfo *tmp = (PartInfo *)c2->private_data;
941
942			/* Already did root */
943			if (c2 == rootdev)
944			    continue;
945
946			if (tmp->newfs)
947			    command_shell_add(tmp->mountpoint, "%s %s/dev/r%s", tmp->newfs_cmd, RunningAsInit ? "/mnt" : "", c2->name);
948			else
949			    command_shell_add(tmp->mountpoint, "fsck -y %s/dev/r%s", RunningAsInit ? "/mnt" : "", c2->name);
950			command_func_add(tmp->mountpoint, Mount, c2->name);
951		    }
952		    else if (c2->type == part && c2->subtype == FS_SWAP) {
953			char fname[80];
954			int i;
955
956			if (c2 == swapdev)
957			    continue;
958			sprintf(fname, "%s/dev/%s", RunningAsInit ? "/mnt" : "", c2->name);
959			i = (Fake || swapon(fname));
960			if (!i)
961			    msgNotify("Added %s as an additional swap device", fname);
962			else
963			    msgConfirm("Unable to add %s as a swap device: %s", fname, strerror(errno));
964		    }
965		}
966	    }
967	    else if (c1->type == fat && c1->private_data && (root->newfs || upgrade)) {
968		char name[FILENAME_MAX];
969
970		sprintf(name, "%s/%s", RunningAsInit ? "/mnt" : "", ((PartInfo *)c1->private_data)->mountpoint);
971		Mkdir(name);
972	    }
973	}
974    }
975
976    if (RunningAsInit) {
977	msgNotify("Copying initial device files..");
978	/* Copy the boot floppy's dev files */
979	if ((root->newfs || upgrade) && vsystem("find -x /dev | cpio %s -pdum /mnt", cpioVerbosity())) {
980	    msgConfirm("Couldn't clone the /dev files!");
981	    return DITEM_FAILURE;
982	}
983    }
984
985    command_sort();
986    command_execute();
987    return DITEM_SUCCESS;
988}
989
990/* Initialize various user-settable values to their defaults */
991int
992installVarDefaults(dialogMenuItem *self)
993{
994    char *cp;
995
996    /* Set default startup options */
997    variable_set2(VAR_ROUTER_ENABLE,		"NO");
998    variable_set2(VAR_RELNAME,			RELEASE_NAME);
999    variable_set2(VAR_CPIO_VERBOSITY,		"high");
1000    variable_set2(VAR_TAPE_BLOCKSIZE,		DEFAULT_TAPE_BLOCKSIZE);
1001    variable_set2(VAR_INSTALL_ROOT,		"/");
1002    variable_set2(VAR_INSTALL_CFG,		"install.cfg");
1003    cp = getenv("EDITOR");
1004    if (!cp)
1005	cp = "/usr/bin/ee";
1006    variable_set2(VAR_EDITOR,			cp);
1007    variable_set2(VAR_FTP_USER,			"ftp");
1008    variable_set2(VAR_BROWSER_PACKAGE,		PACKAGE_LYNX);
1009    variable_set2(VAR_BROWSER_BINARY,		"/usr/local/bin/lynx");
1010    variable_set2(VAR_FTP_STATE,		"passive");
1011    variable_set2(VAR_NFS_SECURE,		"YES");
1012    variable_set2(VAR_PKG_TMPDIR,		"/usr/tmp");
1013    variable_set2(VAR_GATED_PKG,		PACKAGE_GATED);
1014    variable_set2(VAR_PCNFSD_PKG,		PACKAGE_PCNFSD);
1015    variable_set2(VAR_MEDIA_TIMEOUT,		itoa(MEDIA_TIMEOUT));
1016    if (getpid() != 1)
1017	variable_set2(SYSTEM_STATE,		"update");
1018    else
1019	variable_set2(SYSTEM_STATE,		"init");
1020    return DITEM_SUCCESS;
1021}
1022
1023/* Load the environment up from various system configuration files */
1024void
1025installEnvironment(void)
1026{
1027    if (file_readable("/etc/rc.conf"))
1028	configEnvironmentRC_conf("/etc/rc.conf");
1029    if (file_readable("/etc/resolv.conf"))
1030	configEnvironmentResolv("/etc/resolv.conf");
1031}
1032
1033/* Copy the boot floppy contents into /stand */
1034Boolean
1035copySelf(void)
1036{
1037    int i;
1038
1039    if (file_readable("/boot.help"))
1040	vsystem("cp /boot.help /mnt");
1041    msgWeHaveOutput("Copying the boot floppy to /stand on root filesystem");
1042    i = vsystem("find -x /stand | cpio %s -pdum /mnt", cpioVerbosity());
1043    if (i) {
1044	msgConfirm("Copy returned error status of %d!", i);
1045	return FALSE;
1046    }
1047
1048    /* Copy the /etc files into their rightful place */
1049    if (vsystem("cd /mnt/stand; find etc | cpio %s -pdum /mnt", cpioVerbosity())) {
1050	msgConfirm("Couldn't copy up the /etc files!");
1051	return TRUE;
1052    }
1053    return TRUE;
1054}
1055
1056static void
1057create_termcap(void)
1058{
1059    FILE *fp;
1060
1061    const char *caps[] = {
1062	termcap_vt100, termcap_cons25, termcap_cons25_m, termcap_cons25r,
1063	termcap_cons25r_m, termcap_cons25l1, termcap_cons25l1_m, NULL,
1064    };
1065    const char **cp;
1066
1067    if (!file_readable(TERMCAP_FILE)) {
1068	Mkdir("/usr/share/misc");
1069	fp = fopen(TERMCAP_FILE, "w");
1070	if (!fp) {
1071	    msgConfirm("Unable to initialize termcap file. Some screen-oriented\nutilities may not work.");
1072	    return;
1073	}
1074	cp = caps;
1075	while (*cp)
1076	    fprintf(fp, "%s\n", *(cp++));
1077	fclose(fp);
1078    }
1079}
1080
1081#ifdef SAVE_USERCONFIG
1082static void
1083save_userconfig_to_kernel(char *kern)
1084{
1085    struct kernel *core, *boot;
1086    struct list *c_isa, *b_isa, *c_dev, *b_dev;
1087    int i, d;
1088
1089    if ((core = uc_open("-incore")) == NULL) {
1090	msgDebug("save_userconf: Can't read in-core information for kernel.\n");
1091	return;
1092    }
1093
1094    if ((boot = uc_open(kern)) == NULL) {
1095	msgDebug("save_userconf: Can't read device information for kernel image %s\n", kern);
1096	return;
1097    }
1098
1099    msgNotify("Saving any boot -c changes to new kernel...");
1100    c_isa = uc_getdev(core, "-isa");
1101    b_isa = uc_getdev(boot, "-isa");
1102    if (isDebug())
1103	msgDebug("save_userconf: got %d ISA device entries from core, %d from boot.\n", c_isa->ac, b_isa->ac);
1104    for (d = 0; d < c_isa->ac; d++) {
1105	if (isDebug())
1106	    msgDebug("save_userconf: ISA device loop, c_isa->av[%d] = %s\n", d, c_isa->av[d]);
1107	if (strcmp(c_isa->av[d], "npx0")) { /* special case npx0, which mucks with its id_irq member */
1108	    c_dev = uc_getdev(core, c_isa->av[d]);
1109	    b_dev = uc_getdev(boot, b_isa->av[d]);
1110	    if (!c_dev || !b_dev) {
1111		msgDebug("save_userconf: c_dev: %x b_dev: %x\n", c_dev, b_dev);
1112		continue;
1113	    }
1114	    if (isDebug())
1115		msgDebug("save_userconf: ISA device %s: %d config parameters (core), %d (boot)\n",
1116			 c_isa->av[d], c_dev->ac, b_dev->ac);
1117	    for (i = 0; i < c_dev->ac; i++) {
1118		if (isDebug())
1119		    msgDebug("save_userconf: c_dev->av[%d] = %s, b_dev->av[%d] = %s\n", i, c_dev->av[i], i, b_dev->av[i]);
1120		if (strcmp(c_dev->av[i], b_dev->av[i])) {
1121		    if (isDebug())
1122			msgDebug("save_userconf: %s (boot) -> %s (core)\n",
1123				 c_dev->av[i], b_dev->av[i]);
1124		    isa_setdev(boot, c_dev);
1125		}
1126	    }
1127	}
1128	else {
1129	    if (isDebug())
1130		msgDebug("skipping npx0\n");
1131	}
1132    }
1133    if (isDebug())
1134	msgDebug("Closing kernels\n");
1135    uc_close(core, 0);
1136    uc_close(boot, 1);
1137}
1138#endif
1139