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