install.c revision 118709
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 118709 2003-08-10 01:04:05Z das $
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#ifndef X_AS_PKG
857/* Fix side-effects from the the XFree86 installation */
858int
859installFixupXFree(dialogMenuItem *self)
860{
861    /* BOGON #1:  XFree86 requires various specialized fixups */
862    if (directory_exists("/usr/X11R6")) {
863	dialog_clear_norefresh();
864	msgNotify("Fixing permissions in XFree86 tree..");
865	vsystem("chmod -R a+r /usr/X11R6");
866	vsystem("find /usr/X11R6 -type d | xargs chmod a+x");
867
868	/* Also do bogus minimal package registration so ports don't whine */
869	if (file_readable("/usr/X11R6/lib/X11/pkgreg.tar.gz")) {
870	    dialog_clear_norefresh();
871	    msgNotify("Installing package metainfo..");
872	    vsystem("tar xpzf /usr/X11R6/lib/X11/pkgreg.tar.gz -C / && rm /usr/X11R6/lib/X11/pkgreg.tar.gz");
873	}
874    }
875    return DITEM_SUCCESS | DITEM_RESTORE;
876}
877#endif
878
879#define	QUEUE_YES	1
880#define	QUEUE_NO	0
881static int
882performNewfs(PartInfo *pi, char *dname, int queue)
883{
884	char buffer[LINE_MAX];
885
886	if (pi->do_newfs) {
887		switch(pi->newfs_type) {
888		case NEWFS_UFS:
889			snprintf(buffer, LINE_MAX, "%s %s %s %s %s",
890			    NEWFS_UFS_CMD,
891			    pi->newfs_data.newfs_ufs.softupdates ?  "-U" : "",
892			    pi->newfs_data.newfs_ufs.ufs1 ? "-O1" : "-O2",
893			    pi->newfs_data.newfs_ufs.user_options,
894			    dname);
895			break;
896
897		case NEWFS_MSDOS:
898			snprintf(buffer, LINE_MAX, "%s %s", NEWFS_MSDOS_CMD,
899			    dname);
900			break;
901
902		case NEWFS_CUSTOM:
903			snprintf(buffer, LINE_MAX, "%s %s",
904			    pi->newfs_data.newfs_custom.command, dname);
905			break;
906		}
907
908		if (queue == QUEUE_YES) {
909			command_shell_add(pi->mountpoint, buffer);
910			return (0);
911		} else
912			return (vsystem(buffer));
913	}
914	return (0);
915}
916
917/* Go newfs and/or mount all the filesystems we've been asked to */
918int
919installFilesystems(dialogMenuItem *self)
920{
921    int i;
922    Disk *disk;
923    Chunk *c1, *c2, *rootdev, *swapdev;
924    Device **devs;
925    PartInfo *root;
926    char dname[80];
927    Boolean upgrade = FALSE;
928#if defined(__ia64__)
929    char efi_bootdir[FILENAME_MAX];
930#endif
931
932    /* If we've already done this, bail out */
933    if (!variable_cmp(DISK_LABELLED, "written"))
934	return DITEM_SUCCESS;
935
936    upgrade = !variable_cmp(SYSTEM_STATE, "upgrade");
937    if (!checkLabels(TRUE, &rootdev, &swapdev, NULL, NULL, NULL, NULL))
938	return DITEM_FAILURE;
939
940    if (rootdev)
941	root = (PartInfo *)rootdev->private_data;
942    else
943	root = NULL;
944
945    command_clear();
946    if (swapdev && RunningAsInit) {
947	/* As the very first thing, try to get ourselves some swap space */
948	sprintf(dname, "/dev/%s", swapdev->name);
949	if (!Fake && !file_readable(dname)) {
950	    msgConfirm("Unable to find device node for %s in /dev!\n"
951		       "The creation of filesystems will be aborted.", dname);
952	    return DITEM_FAILURE;
953	}
954
955	if (!Fake) {
956	    if (!swapon(dname)) {
957		dialog_clear_norefresh();
958		msgNotify("Added %s as initial swap device", dname);
959	    }
960	    else {
961		msgConfirm("WARNING!  Unable to swap to %s: %s\n"
962			   "This may cause the installation to fail at some point\n"
963			   "if you don't have a lot of memory.", dname, strerror(errno));
964	    }
965	}
966    }
967
968    if (rootdev && RunningAsInit) {
969	/* Next, create and/or mount the root device */
970	sprintf(dname, "/dev/%s", rootdev->name);
971	if (!Fake && !file_readable(dname)) {
972	    msgConfirm("Unable to make device node for %s in /dev!\n"
973		       "The creation of filesystems will be aborted.", dname);
974	    return DITEM_FAILURE | DITEM_RESTORE;
975	}
976	if (strcmp(root->mountpoint, "/"))
977	    msgConfirm("Warning: %s is marked as a root partition but is mounted on %s", rootdev->name, root->mountpoint);
978
979	if (root->do_newfs && (!upgrade ||
980	    !msgNoYes("You are upgrading - are you SURE you want to newfs "
981	    "the root partition?"))) {
982	    int i;
983
984	    dialog_clear_norefresh();
985	    msgNotify("Making a new root filesystem on %s", dname);
986	    i = performNewfs(root, dname, QUEUE_NO);
987	    if (i) {
988		msgConfirm("Unable to make new root filesystem on %s!\n"
989			   "Command returned status %d", dname, i);
990		return DITEM_FAILURE | DITEM_RESTORE;
991	    }
992	}
993	else {
994	    if (!upgrade) {
995		msgConfirm("Warning:  Using existing root partition.  It will be assumed\n"
996			   "that you have the appropriate device entries already in /dev.");
997	    }
998	    dialog_clear_norefresh();
999	    msgNotify("Checking integrity of existing %s filesystem.", dname);
1000	    i = vsystem("fsck_ffs -y %s", dname);
1001	    if (i)
1002		msgConfirm("Warning: fsck returned status of %d for %s.\n"
1003			   "This partition may be unsafe to use.", i, dname);
1004	}
1005
1006	/*
1007	 * If soft updates was enabled in the editor but we didn't newfs,
1008	 * use tunefs to update the soft updates flag on the file system.
1009	 */
1010	if (!root->do_newfs && root->newfs_type == NEWFS_UFS &&
1011	    root->newfs_data.newfs_ufs.softupdates) {
1012		i = vsystem("tunefs -n enable %s", dname);
1013		if (i)
1014			msgConfirm("Warning: Unable to enable soft updates"
1015			    " for root file system on %s", dname);
1016	}
1017
1018	/* Switch to block device */
1019	sprintf(dname, "/dev/%s", rootdev->name);
1020	if (Mount("/mnt", dname)) {
1021	    msgConfirm("Unable to mount the root file system on %s!  Giving up.", dname);
1022	    return DITEM_FAILURE | DITEM_RESTORE;
1023	}
1024
1025	/* Mount devfs for other partitions to mount */
1026	Mkdir("/mnt/dev");
1027	if (!Fake) {
1028	    struct iovec iov[4];
1029
1030	    iov[0].iov_base = "fstype";
1031	    iov[0].iov_len = strlen(iov[0].iov_base) + 1;
1032	    iov[1].iov_base = "devfs";
1033	    iov[1].iov_len = strlen(iov[1].iov_base) + 1;
1034	    iov[2].iov_base = "fspath";
1035	    iov[2].iov_len = strlen(iov[2].iov_base) + 1;
1036	    iov[3].iov_base = "/mnt/dev";
1037	    iov[3].iov_len = strlen(iov[3].iov_base) + 1;
1038	    i = nmount(iov, 4, 0);
1039
1040	    if (i) {
1041		dialog_clear_norefresh();
1042		msgConfirm("Unable to mount DEVFS (error %d)", errno);
1043		return DITEM_FAILURE | DITEM_RESTORE;
1044	    }
1045	}
1046    }
1047
1048    /* Now buzz through the rest of the partitions and mount them too */
1049    devs = deviceFind(NULL, DEVICE_TYPE_DISK);
1050    for (i = 0; devs[i]; i++) {
1051	if (!devs[i]->enabled)
1052	    continue;
1053
1054	disk = (Disk *)devs[i]->private;
1055	if (!disk->chunks) {
1056	    msgConfirm("No chunk list found for %s!", disk->name);
1057	    return DITEM_FAILURE | DITEM_RESTORE;
1058	}
1059	for (c1 = disk->chunks->part; c1; c1 = c1->next) {
1060	    if (c1->type == freebsd) {
1061		for (c2 = c1->part; c2; c2 = c2->next) {
1062		    if (c2->type == part && c2->subtype != FS_SWAP && c2->private_data) {
1063			PartInfo *tmp = (PartInfo *)c2->private_data;
1064
1065			/* Already did root */
1066			if (c2 == rootdev)
1067			    continue;
1068
1069			sprintf(dname, "%s/dev/%s",
1070			    RunningAsInit ? "/mnt" : "", c2->name);
1071
1072			if (tmp->do_newfs && (!upgrade ||
1073			    !msgNoYes("You are upgrading - are you SURE you"
1074			    " want to newfs /dev/%s?", c2->name)))
1075				performNewfs(tmp, dname, QUEUE_YES);
1076			else
1077			    command_shell_add(tmp->mountpoint,
1078				"fsck_ffs -y %s/dev/%s", RunningAsInit ?
1079				"/mnt" : "", c2->name);
1080#if 0
1081			if (tmp->soft)
1082			    command_shell_add(tmp->mountpoint,
1083			    "tunefs -n enable %s/dev/%s", RunningAsInit ?
1084			    "/mnt" : "", c2->name);
1085#endif
1086			command_func_add(tmp->mountpoint, Mount, c2->name);
1087		    }
1088		    else if (c2->type == part && c2->subtype == FS_SWAP) {
1089			char fname[80];
1090			int i;
1091
1092			if (c2 == swapdev)
1093			    continue;
1094			sprintf(fname, "%s/dev/%s", RunningAsInit ? "/mnt" : "", c2->name);
1095			i = (Fake || swapon(fname));
1096			if (!i) {
1097			    dialog_clear_norefresh();
1098			    msgNotify("Added %s as an additional swap device", fname);
1099			}
1100			else {
1101			    msgConfirm("Unable to add %s as a swap device: %s", fname, strerror(errno));
1102			}
1103		    }
1104		}
1105	    }
1106	    else if (c1->type == fat && c1->private_data &&
1107		(root->do_newfs || upgrade)) {
1108		char name[FILENAME_MAX];
1109
1110		sprintf(name, "%s/%s", RunningAsInit ? "/mnt" : "", ((PartInfo *)c1->private_data)->mountpoint);
1111		Mkdir(name);
1112	    }
1113#if defined(__ia64__)
1114	    else if (c1->type == efi && c1->private_data) {
1115		char bootdir[FILENAME_MAX];
1116		PartInfo *pi = (PartInfo *)c1->private_data;
1117		char *p;
1118
1119		sprintf(dname, "%s/dev/%s", RunningAsInit ? "/mnt" : "",
1120		    c1->name);
1121
1122		if (pi->do_newfs && (!upgrade ||
1123		    !msgNoYes("You are upgrading - are you SURE you want to "
1124		    "newfs /dev/%s?", c1->name)))
1125			performNewfs(pi, dname, QUEUE_YES);
1126
1127		command_func_add(pi->mountpoint, Mount_msdosfs, c1->name);
1128
1129		/*
1130		 * Create a directory boot on the EFI filesystem and create a
1131		 * link boot on the root filesystem pointing to the one on the
1132		 * EFI filesystem. That way, we install the loader, kernel
1133		 * and modules on the EFI filesystem.
1134		 */
1135		sprintf(bootdir, "%s", RunningAsInit ? "/mnt" : "");
1136		sprintf(efi_bootdir, "%s/%s", bootdir, pi->mountpoint);
1137		strcat(bootdir, "/boot");
1138		strcat(efi_bootdir, "/boot");
1139		command_func_add(pi->mountpoint, Mkdir_command, efi_bootdir);
1140
1141		/* Make a relative link. */
1142		p = &efi_bootdir[(RunningAsInit) ? 4 : 0];
1143		while (*p == '/')
1144			p++;
1145		symlink(p, bootdir);
1146	    }
1147#endif
1148	}
1149    }
1150
1151    command_sort();
1152    command_execute();
1153    dialog_clear_norefresh();
1154    return DITEM_SUCCESS | DITEM_RESTORE;
1155}
1156
1157static char *
1158getRelname(void)
1159{
1160    static char buf[64];
1161    size_t sz = (sizeof buf) - 1;
1162
1163    if (sysctlbyname("kern.osrelease", buf, &sz, NULL, 0) != -1) {
1164	buf[sz] = '\0';
1165	return buf;
1166    }
1167    else
1168	return "<unknown>";
1169}
1170
1171/* Initialize various user-settable values to their defaults */
1172int
1173installVarDefaults(dialogMenuItem *self)
1174{
1175    char *cp;
1176
1177    /* Set default startup options */
1178    variable_set2(VAR_RELNAME,			getRelname(), 0);
1179    variable_set2(VAR_CPIO_VERBOSITY,		"high", 0);
1180    variable_set2(VAR_TAPE_BLOCKSIZE,		DEFAULT_TAPE_BLOCKSIZE, 0);
1181    variable_set2(VAR_INSTALL_ROOT,		"/", 0);
1182    variable_set2(VAR_INSTALL_CFG,		"install.cfg", 0);
1183    cp = getenv("EDITOR");
1184    if (!cp)
1185	cp = "/usr/bin/ee";
1186    variable_set2(VAR_EDITOR,			cp, 0);
1187    variable_set2(VAR_FTP_USER,			"ftp", 0);
1188    variable_set2(VAR_BROWSER_PACKAGE,		"links", 0);
1189    variable_set2(VAR_BROWSER_BINARY,		"/usr/local/bin/links", 0);
1190    variable_set2(VAR_FTP_STATE,		"passive", 0);
1191    variable_set2(VAR_NFS_SECURE,		"NO", -1);
1192    if (OnVTY)
1193	    variable_set2(VAR_FIXIT_TTY,		"standard", 0);
1194    else
1195	    variable_set2(VAR_FIXIT_TTY,		"serial", 0);
1196    variable_set2(VAR_PKG_TMPDIR,		"/var/tmp", 0);
1197    variable_set2(VAR_MEDIA_TIMEOUT,		itoa(MEDIA_TIMEOUT), 0);
1198    if (getpid() != 1)
1199	variable_set2(SYSTEM_STATE,		"update", 0);
1200    else
1201	variable_set2(SYSTEM_STATE,		"init", 0);
1202    variable_set2(VAR_NEWFS_ARGS,		"-b 16384 -f 2048", 0);
1203    variable_set2(VAR_CONSTERM,                 "NO", 0);
1204    return DITEM_SUCCESS;
1205}
1206
1207/* Load the environment up from various system configuration files */
1208void
1209installEnvironment(void)
1210{
1211    configEnvironmentRC_conf();
1212    if (file_readable("/etc/resolv.conf"))
1213	configEnvironmentResolv("/etc/resolv.conf");
1214}
1215
1216/* Copy the boot floppy contents into /stand */
1217Boolean
1218copySelf(void)
1219{
1220    int i;
1221
1222    if (file_readable("/boot.help"))
1223	vsystem("cp /boot.help /mnt");
1224    msgWeHaveOutput("Copying the boot floppy to /stand on root filesystem");
1225    i = vsystem("find -x /stand | cpio %s -pdum /mnt", cpioVerbosity());
1226    if (i) {
1227	msgConfirm("Copy returned error status of %d!", i);
1228	return FALSE;
1229    }
1230
1231    /* Copy the /etc files into their rightful place */
1232    if (vsystem("cd /mnt/stand; find etc | cpio %s -pdum /mnt", cpioVerbosity())) {
1233	msgConfirm("Couldn't copy up the /etc files!");
1234	return TRUE;
1235    }
1236    return TRUE;
1237}
1238
1239static void
1240create_termcap(void)
1241{
1242    FILE *fp;
1243
1244    const char *caps[] = {
1245	termcap_vt100, termcap_cons25, termcap_cons25_m, termcap_cons25r,
1246	termcap_cons25r_m, termcap_cons25l1, termcap_cons25l1_m,
1247	termcap_xterm, NULL,
1248    };
1249    const char **cp;
1250
1251    if (!file_readable(TERMCAP_FILE)) {
1252	Mkdir("/usr/share/misc");
1253	fp = fopen(TERMCAP_FILE, "w");
1254	if (!fp) {
1255	    msgConfirm("Unable to initialize termcap file. Some screen-oriented\nutilities may not work.");
1256	    return;
1257	}
1258	cp = caps;
1259	while (*cp)
1260	    fprintf(fp, "%s\n", *(cp++));
1261	fclose(fp);
1262    }
1263}
1264