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