install.c revision 123707
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 123707 2003-12-21 17:16:44Z scottl $
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/rescue/ldconfig -s /mnt2/lib /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("/libexec");
365    if (!file_readable("/libexec/ld.so") && file_readable("/mnt2/libexec/ld.so")) {
366	if (symlink("/mnt2/libexec/ld.so", "/libexec/ld.so"))
367	    msgDebug("Couldn't link to ld.so - not necessarily a problem for ELF\n");
368    }
369    if (!file_readable("/libexec/ld-elf.so.1")) {
370	if (symlink("/mnt2/libexec/ld-elf.so.1", "/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
533	/* Give user the option of one last configuration spree */
534	installConfigure();
535    }
536    return i;
537}
538
539/* Standard mode installation */
540int
541installStandard(dialogMenuItem *self)
542{
543    int i, tries = 0;
544    Device **devs;
545
546    variable_set2(SYSTEM_STATE, "standard", 0);
547    dialog_clear_norefresh();
548#ifdef WITH_SLICES
549    msgConfirm("In the next menu, you will need to set up a DOS-style (\"fdisk\") partitioning\n"
550	       "scheme for your hard disk.  If you simply wish to devote all disk space\n"
551	       "to FreeBSD (overwriting anything else that might be on the disk(s) selected)\n"
552	       "then use the (A)ll command to select the default partitioning scheme followed\n"
553	       "by a (Q)uit.  If you wish to allocate only free space to FreeBSD, move to a\n"
554	       "partition marked \"unused\" and use the (C)reate command.");
555
556nodisks:
557    if (DITEM_STATUS(diskPartitionEditor(self)) == DITEM_FAILURE)
558	return DITEM_FAILURE;
559
560    if (diskGetSelectCount(&devs) <= 0 && tries < 3) {
561	msgConfirm("You need to select some disks to operate on!  Be sure to use SPACE\n"
562		   "instead of RETURN in the disk selection menu when selecting a disk.");
563	++tries;
564	goto nodisks;
565    }
566
567    msgConfirm("Now you need to create BSD partitions inside of the fdisk partition(s)\n"
568	       "just created.  If you have a reasonable amount of disk space (200MB or more)\n"
569	       "and don't have any special requirements, simply use the (A)uto command to\n"
570	       "allocate space automatically.  If you have more specific needs or just don't\n"
571	       "care for the layout chosen by (A)uto, press F1 for more information on\n"
572	       "manual layout.");
573#else
574    msgConfirm("First you need to create BSD partitions on the disk which you are\n"
575	       "installing to.  If you have a reasonable amount of disk space (200MB or more)\n"
576	       "and don't have any special requirements, simply use the (A)uto command to\n"
577	       "allocate space automatically.  If you have more specific needs or just don't\n"
578	       "care for the layout chosen by (A)uto, press F1 for more information on\n"
579	       "manual layout.");
580#endif
581
582    if (DITEM_STATUS(diskLabelEditor(self)) == DITEM_FAILURE)
583	return DITEM_FAILURE;
584
585    if (DITEM_STATUS((i = installCommit(self))) == DITEM_FAILURE) {
586	dialog_clear();
587	msgConfirm("Installation completed with some errors.  You may wish to\n"
588		   "scroll through the debugging messages on VTY1 with the\n"
589		   "scroll-lock feature.  You can also choose \"No\" at the next\n"
590		   "prompt and go back into the installation menus to retry\n"
591		   "whichever operations have failed.");
592	return i;
593
594    }
595    else {
596	dialog_clear();
597	msgConfirm("Congratulations!  You now have FreeBSD installed on your system.\n\n"
598		   "We will now move on to the final configuration questions.\n"
599		   "For any option you do not wish to configure, simply select\n"
600		   "No.\n\n"
601		   "If you wish to re-enter this utility after the system is up, you\n"
602		   "may do so by typing: /usr/sbin/sysinstall.");
603    }
604    if (mediaDevice->type != DEVICE_TYPE_FTP && mediaDevice->type != DEVICE_TYPE_NFS) {
605	if (!msgYesNo("Would you like to configure any Ethernet or SLIP/PPP network devices?")) {
606	    Device *tmp = tcpDeviceSelect();
607
608	    if (tmp && !((DevInfo *)tmp->private)->use_dhcp && !msgYesNo("Would you like to bring the %s interface up right now?", tmp->name))
609		if (!DEVICE_INIT(tmp))
610		    msgConfirm("Initialization of %s device failed.", tmp->name);
611	}
612	dialog_clear_norefresh();
613    }
614
615    if (!msgNoYes("Do you want this machine to function as a network gateway?"))
616	variable_set2("gateway_enable", "YES", 1);
617
618    dialog_clear_norefresh();
619    if (!msgNoYes("Do you want to configure inetd and the network services that it provides?"))
620        configInetd(self);
621
622    dialog_clear_norefresh();
623    if (!msgNoYes("Would you like to enable SSH login?"))
624	variable_set2("sshd_enable", "YES", 1);
625
626    dialog_clear_norefresh();
627    if (!msgNoYes("Do you want to have anonymous FTP access to this machine?"))
628	configAnonFTP(self);
629
630    dialog_clear_norefresh();
631    if (!msgNoYes("Do you want to configure this machine as an NFS server?"))
632	configNFSServer(self);
633
634    dialog_clear_norefresh();
635    if (!msgNoYes("Do you want to configure this machine as an NFS client?"))
636	variable_set2("nfs_client_enable", "YES", 1);
637
638#ifdef WITH_SYSCONS
639    dialog_clear_norefresh();
640    if (!msgNoYes("Would you like to customize your system console settings?"))
641	dmenuOpenSimple(&MenuSyscons, FALSE);
642#endif
643
644    dialog_clear_norefresh();
645    if (!msgYesNo("Would you like to set this machine's time zone now?"))
646	systemExecute("tzsetup");
647
648#ifdef WITH_LINUX
649    dialog_clear_norefresh();
650    if (!msgYesNo("Would you like to enable Linux binary compatibility?"))
651	(void)configLinux(self);
652#endif
653
654#ifdef __alpha__
655    dialog_clear_norefresh();
656    if (!msgYesNo("Would you like to enable OSF/1 binary compatibility?"))
657	(void)configOSF1(self);
658#endif
659
660#ifdef WITH_MICE
661    dialog_clear_norefresh();
662    if (!msgNoYes("Does this system have a PS/2, serial, or bus mouse?"))
663	dmenuOpenSimple(&MenuMouse, FALSE);
664#endif
665
666#ifdef __i386__
667    if (checkLoaderACPI() != 0) {
668    	dialog_clear_norefresh();
669    	if (!msgNoYes("ACPI was disabled during boot.\n"
670		      "Would you like to disable it permanently?"))
671		(void)configLoaderACPI(1 /*disable*/);
672    }
673#endif
674
675    /* Now would be a good time to checkpoint the configuration data */
676    configRC_conf();
677    sync();
678
679    if (directory_exists("/usr/X11R6")) {
680	dialog_clear_norefresh();
681	if (!msgYesNo("Would you like to configure your X server at this time?"))
682	    (void)configXSetup(self);
683    }
684
685    dialog_clear_norefresh();
686    if (!msgYesNo("The FreeBSD package collection is a collection of thousands of ready-to-run\n"
687		  "applications, from text editors to games to WEB servers and more.  Would you\n"
688		  "like to browse the collection now?")) {
689	(void)configPackages(self);
690    }
691
692    if (!msgYesNo("Would you like to add any initial user accounts to the system?\n"
693		  "Adding at least one account for yourself at this stage is suggested\n"
694		  "since working as the \"root\" user is dangerous (it is easy to do\n"
695		  "things which adversely affect the entire system)."))
696	(void)configUsers(self);
697
698    msgConfirm("Now you must set the system manager's password.\n"
699	       "This is the password you'll use to log in as \"root\".");
700    if (!systemExecute("passwd root"))
701	variable_set2("root_password", "YES", 0);
702
703    /* XXX Put whatever other nice configuration questions you'd like to ask the user here XXX */
704
705    /* Give user the option of one last configuration spree */
706    dialog_clear_norefresh();
707    installConfigure();
708    return DITEM_LEAVE_MENU;
709}
710
711/* The version of commit we call from the Install Custom menu */
712int
713installCustomCommit(dialogMenuItem *self)
714{
715    int i;
716
717    i = installCommit(self);
718    if (DITEM_STATUS(i) == DITEM_SUCCESS) {
719	/* Give user the option of one last configuration spree */
720	installConfigure();
721	return i;
722    }
723    else
724	msgConfirm("The commit operation completed with errors.  Not\n"
725		   "updating /etc files.");
726    return i;
727}
728
729/*
730 * What happens when we finally decide to going ahead with the installation.
731 *
732 * This is broken into multiple stages so that the user can do a full
733 * installation but come back here again to load more distributions,
734 * perhaps from a different media type.  This would allow, for
735 * example, the user to load the majority of the system from CDROM and
736 * then use ftp to load just the CRYPTO dist.
737 */
738int
739installCommit(dialogMenuItem *self)
740{
741    int i;
742    char *str;
743
744    dialog_clear_norefresh();
745    if (!Dists)
746	distConfig(NULL);
747
748    if (!Dists) {
749	(void)dmenuOpenSimple(&MenuDistributions, FALSE);
750	/* select reasonable defaults if necessary */
751	if (!Dists)
752	    Dists = _DIST_USER;
753    }
754
755    if (!mediaVerify())
756	return DITEM_FAILURE;
757
758    str = variable_get(SYSTEM_STATE);
759    if (isDebug())
760	msgDebug("installCommit: System state is `%s'\n", str);
761
762    /* Installation stuff we wouldn't do to a running system */
763    if (RunningAsInit && DITEM_STATUS((i = installInitial())) == DITEM_FAILURE)
764	return i;
765
766try_media:
767    if (!DEVICE_INIT(mediaDevice)) {
768	if (!msgYesNo("Unable to initialize selected media. Would you like to\n"
769		      "adjust your media configuration and try again?")) {
770	    mediaDevice = NULL;
771	    if (!mediaVerify())
772		return DITEM_FAILURE;
773	    else
774		goto try_media;
775	}
776	else
777	    return DITEM_FAILURE;
778    }
779
780    /* Now go get it all */
781    i = distExtractAll(self);
782
783    /* When running as init, *now* it's safe to grab the rc.foo vars */
784    installEnvironment();
785
786    variable_set2(SYSTEM_STATE, DITEM_STATUS(i) == DITEM_FAILURE ? "error-install" : "full-install", 0);
787
788    return i;
789}
790
791static void
792installConfigure(void)
793{
794    /* Final menu of last resort */
795    if (!msgNoYes("Visit the general configuration menu for a chance to set\n"
796		  "any last options?"))
797	dmenuOpenSimple(&MenuConfigure, FALSE);
798    configRC_conf();
799    sync();
800}
801
802int
803installFixupBase(dialogMenuItem *self)
804{
805    Device **devs;
806    char *cp;
807    int i;
808    FILE *fp;
809    int kstat = 1;
810
811    /* All of this is done only as init, just to be safe */
812    if (RunningAsInit) {
813#if defined(__i386__) || defined(__amd64__)
814	if ((fp = fopen("/boot/loader.conf", "a")) != NULL) {
815	    if (!kstat || !OnVTY)
816		fprintf(fp, "# -- sysinstall generated deltas -- #\n");
817	    if (!kstat)
818		fprintf(fp, "userconfig_script_load=\"YES\"\n");
819	    if (!OnVTY)
820		fprintf(fp, "console=\"comconsole\"\n");
821	    fclose(fp);
822	}
823#endif
824
825	/* BOGON #2: We leave /etc in a bad state */
826	chmod("/etc", 0755);
827
828	/* BOGON #3: No /var/db/mountdtab complains */
829	Mkdir("/var/db");
830	creat("/var/db/mountdtab", 0644);
831
832	/* BOGON #4: /compat created by default in root fs */
833	Mkdir("/usr/compat");
834	vsystem("ln -s usr/compat /compat");
835
836	/* BOGON #5: aliases database not build for bin */
837	vsystem("newaliases");
838
839	/* Now run all the mtree stuff to fix things up */
840        vsystem("mtree -deU -f /etc/mtree/BSD.root.dist -p /");
841        vsystem("mtree -deU -f /etc/mtree/BSD.var.dist -p /var");
842        vsystem("mtree -deU -f /etc/mtree/BSD.usr.dist -p /usr");
843
844	/* Do all the last ugly work-arounds here */
845    }
846    return DITEM_SUCCESS | DITEM_RESTORE;
847}
848
849#define	QUEUE_YES	1
850#define	QUEUE_NO	0
851static int
852performNewfs(PartInfo *pi, char *dname, int queue)
853{
854	char buffer[LINE_MAX];
855
856	if (pi->do_newfs) {
857		switch(pi->newfs_type) {
858		case NEWFS_UFS:
859			snprintf(buffer, LINE_MAX, "%s %s %s %s %s",
860			    NEWFS_UFS_CMD,
861			    pi->newfs_data.newfs_ufs.softupdates ?  "-U" : "",
862			    pi->newfs_data.newfs_ufs.ufs1 ? "-O1" : "-O2",
863			    pi->newfs_data.newfs_ufs.user_options,
864			    dname);
865			break;
866
867		case NEWFS_MSDOS:
868			snprintf(buffer, LINE_MAX, "%s %s", NEWFS_MSDOS_CMD,
869			    dname);
870			break;
871
872		case NEWFS_CUSTOM:
873			snprintf(buffer, LINE_MAX, "%s %s",
874			    pi->newfs_data.newfs_custom.command, dname);
875			break;
876		}
877
878		if (queue == QUEUE_YES) {
879			command_shell_add(pi->mountpoint, buffer);
880			return (0);
881		} else
882			return (vsystem(buffer));
883	}
884	return (0);
885}
886
887/* Go newfs and/or mount all the filesystems we've been asked to */
888int
889installFilesystems(dialogMenuItem *self)
890{
891    int i;
892    Disk *disk;
893    Chunk *c1, *c2, *rootdev, *swapdev;
894    Device **devs;
895    PartInfo *root;
896    char dname[80];
897    Boolean upgrade = FALSE;
898#if defined(__ia64__)
899    char efi_bootdir[FILENAME_MAX];
900#endif
901
902    /* If we've already done this, bail out */
903    if (!variable_cmp(DISK_LABELLED, "written"))
904	return DITEM_SUCCESS;
905
906    upgrade = !variable_cmp(SYSTEM_STATE, "upgrade");
907    if (!checkLabels(TRUE, &rootdev, &swapdev, NULL, NULL, NULL, NULL))
908	return DITEM_FAILURE;
909
910    if (rootdev)
911	root = (PartInfo *)rootdev->private_data;
912    else
913	root = NULL;
914
915    command_clear();
916    if (swapdev && RunningAsInit) {
917	/* As the very first thing, try to get ourselves some swap space */
918	sprintf(dname, "/dev/%s", swapdev->name);
919	if (!Fake && !file_readable(dname)) {
920	    msgConfirm("Unable to find device node for %s in /dev!\n"
921		       "The creation of filesystems will be aborted.", dname);
922	    return DITEM_FAILURE;
923	}
924
925	if (!Fake) {
926	    if (!swapon(dname)) {
927		dialog_clear_norefresh();
928		msgNotify("Added %s as initial swap device", dname);
929	    }
930	    else {
931		msgConfirm("WARNING!  Unable to swap to %s: %s\n"
932			   "This may cause the installation to fail at some point\n"
933			   "if you don't have a lot of memory.", dname, strerror(errno));
934	    }
935	}
936    }
937
938    if (rootdev && RunningAsInit) {
939	/* Next, create and/or mount the root device */
940	sprintf(dname, "/dev/%s", rootdev->name);
941	if (!Fake && !file_readable(dname)) {
942	    msgConfirm("Unable to make device node for %s in /dev!\n"
943		       "The creation of filesystems will be aborted.", dname);
944	    return DITEM_FAILURE | DITEM_RESTORE;
945	}
946	if (strcmp(root->mountpoint, "/"))
947	    msgConfirm("Warning: %s is marked as a root partition but is mounted on %s", rootdev->name, root->mountpoint);
948
949	if (root->do_newfs && (!upgrade ||
950	    !msgNoYes("You are upgrading - are you SURE you want to newfs "
951	    "the root partition?"))) {
952	    int i;
953
954	    dialog_clear_norefresh();
955	    msgNotify("Making a new root filesystem on %s", dname);
956	    i = performNewfs(root, dname, QUEUE_NO);
957	    if (i) {
958		msgConfirm("Unable to make new root filesystem on %s!\n"
959			   "Command returned status %d", dname, i);
960		return DITEM_FAILURE | DITEM_RESTORE;
961	    }
962	}
963	else {
964	    if (!upgrade) {
965		msgConfirm("Warning:  Using existing root partition.  It will be assumed\n"
966			   "that you have the appropriate device entries already in /dev.");
967	    }
968	    dialog_clear_norefresh();
969	    msgNotify("Checking integrity of existing %s filesystem.", dname);
970	    i = vsystem("fsck_ffs -y %s", dname);
971	    if (i)
972		msgConfirm("Warning: fsck returned status of %d for %s.\n"
973			   "This partition may be unsafe to use.", i, dname);
974	}
975
976	/*
977	 * If soft updates was enabled in the editor but we didn't newfs,
978	 * use tunefs to update the soft updates flag on the file system.
979	 */
980	if (!root->do_newfs && root->newfs_type == NEWFS_UFS &&
981	    root->newfs_data.newfs_ufs.softupdates) {
982		i = vsystem("tunefs -n enable %s", dname);
983		if (i)
984			msgConfirm("Warning: Unable to enable soft updates"
985			    " for root file system on %s", dname);
986	}
987
988	/* Switch to block device */
989	sprintf(dname, "/dev/%s", rootdev->name);
990	if (Mount("/mnt", dname)) {
991	    msgConfirm("Unable to mount the root file system on %s!  Giving up.", dname);
992	    return DITEM_FAILURE | DITEM_RESTORE;
993	}
994
995	/* Mount devfs for other partitions to mount */
996	Mkdir("/mnt/dev");
997	if (!Fake) {
998	    struct iovec iov[4];
999
1000	    iov[0].iov_base = "fstype";
1001	    iov[0].iov_len = strlen(iov[0].iov_base) + 1;
1002	    iov[1].iov_base = "devfs";
1003	    iov[1].iov_len = strlen(iov[1].iov_base) + 1;
1004	    iov[2].iov_base = "fspath";
1005	    iov[2].iov_len = strlen(iov[2].iov_base) + 1;
1006	    iov[3].iov_base = "/mnt/dev";
1007	    iov[3].iov_len = strlen(iov[3].iov_base) + 1;
1008	    i = nmount(iov, 4, 0);
1009
1010	    if (i) {
1011		dialog_clear_norefresh();
1012		msgConfirm("Unable to mount DEVFS (error %d)", errno);
1013		return DITEM_FAILURE | DITEM_RESTORE;
1014	    }
1015	}
1016    }
1017
1018    /* Now buzz through the rest of the partitions and mount them too */
1019    devs = deviceFind(NULL, DEVICE_TYPE_DISK);
1020    for (i = 0; devs[i]; i++) {
1021	if (!devs[i]->enabled)
1022	    continue;
1023
1024	disk = (Disk *)devs[i]->private;
1025	if (!disk->chunks) {
1026	    msgConfirm("No chunk list found for %s!", disk->name);
1027	    return DITEM_FAILURE | DITEM_RESTORE;
1028	}
1029	for (c1 = disk->chunks->part; c1; c1 = c1->next) {
1030	    if (c1->type == freebsd) {
1031		for (c2 = c1->part; c2; c2 = c2->next) {
1032		    if (c2->type == part && c2->subtype != FS_SWAP && c2->private_data) {
1033			PartInfo *tmp = (PartInfo *)c2->private_data;
1034
1035			/* Already did root */
1036			if (c2 == rootdev)
1037			    continue;
1038
1039			sprintf(dname, "%s/dev/%s",
1040			    RunningAsInit ? "/mnt" : "", c2->name);
1041
1042			if (tmp->do_newfs && (!upgrade ||
1043			    !msgNoYes("You are upgrading - are you SURE you"
1044			    " want to newfs /dev/%s?", c2->name)))
1045				performNewfs(tmp, dname, QUEUE_YES);
1046			else
1047			    command_shell_add(tmp->mountpoint,
1048				"fsck_ffs -y %s/dev/%s", RunningAsInit ?
1049				"/mnt" : "", c2->name);
1050#if 0
1051			if (tmp->soft)
1052			    command_shell_add(tmp->mountpoint,
1053			    "tunefs -n enable %s/dev/%s", RunningAsInit ?
1054			    "/mnt" : "", c2->name);
1055#endif
1056			command_func_add(tmp->mountpoint, Mount, c2->name);
1057		    }
1058		    else if (c2->type == part && c2->subtype == FS_SWAP) {
1059			char fname[80];
1060			int i;
1061
1062			if (c2 == swapdev)
1063			    continue;
1064			sprintf(fname, "%s/dev/%s", RunningAsInit ? "/mnt" : "", c2->name);
1065			i = (Fake || swapon(fname));
1066			if (!i) {
1067			    dialog_clear_norefresh();
1068			    msgNotify("Added %s as an additional swap device", fname);
1069			}
1070			else {
1071			    msgConfirm("Unable to add %s as a swap device: %s", fname, strerror(errno));
1072			}
1073		    }
1074		}
1075	    }
1076	    else if (c1->type == fat && c1->private_data &&
1077		(root->do_newfs || upgrade)) {
1078		char name[FILENAME_MAX];
1079
1080		sprintf(name, "%s/%s", RunningAsInit ? "/mnt" : "", ((PartInfo *)c1->private_data)->mountpoint);
1081		Mkdir(name);
1082	    }
1083#if defined(__ia64__)
1084	    else if (c1->type == efi && c1->private_data) {
1085		char bootdir[FILENAME_MAX];
1086		PartInfo *pi = (PartInfo *)c1->private_data;
1087		char *p;
1088
1089		sprintf(dname, "%s/dev/%s", RunningAsInit ? "/mnt" : "",
1090		    c1->name);
1091
1092		if (pi->do_newfs && (!upgrade ||
1093		    !msgNoYes("You are upgrading - are you SURE you want to "
1094		    "newfs /dev/%s?", c1->name)))
1095			performNewfs(pi, dname, QUEUE_YES);
1096
1097		command_func_add(pi->mountpoint, Mount_msdosfs, c1->name);
1098
1099		/*
1100		 * Create a directory boot on the EFI filesystem and create a
1101		 * link boot on the root filesystem pointing to the one on the
1102		 * EFI filesystem. That way, we install the loader, kernel
1103		 * and modules on the EFI filesystem.
1104		 */
1105		sprintf(bootdir, "%s", RunningAsInit ? "/mnt" : "");
1106		sprintf(efi_bootdir, "%s/%s", bootdir, pi->mountpoint);
1107		strcat(bootdir, "/boot");
1108		strcat(efi_bootdir, "/boot");
1109		command_func_add(pi->mountpoint, Mkdir_command, efi_bootdir);
1110
1111		/* Make a relative link. */
1112		p = &efi_bootdir[(RunningAsInit) ? 4 : 0];
1113		while (*p == '/')
1114			p++;
1115		symlink(p, bootdir);
1116	    }
1117#endif
1118	}
1119    }
1120
1121    command_sort();
1122    command_execute();
1123    dialog_clear_norefresh();
1124    return DITEM_SUCCESS | DITEM_RESTORE;
1125}
1126
1127static char *
1128getRelname(void)
1129{
1130    static char buf[64];
1131    size_t sz = (sizeof buf) - 1;
1132
1133    if (sysctlbyname("kern.osrelease", buf, &sz, NULL, 0) != -1) {
1134	buf[sz] = '\0';
1135	return buf;
1136    }
1137    else
1138	return "<unknown>";
1139}
1140
1141/* Initialize various user-settable values to their defaults */
1142int
1143installVarDefaults(dialogMenuItem *self)
1144{
1145    char *cp;
1146
1147    /* Set default startup options */
1148    variable_set2(VAR_RELNAME,			getRelname(), 0);
1149    variable_set2(VAR_CPIO_VERBOSITY,		"high", 0);
1150    variable_set2(VAR_TAPE_BLOCKSIZE,		DEFAULT_TAPE_BLOCKSIZE, 0);
1151    variable_set2(VAR_INSTALL_ROOT,		"/", 0);
1152    variable_set2(VAR_INSTALL_CFG,		"install.cfg", 0);
1153    variable_set2(VAR_SKIP_PCCARD,		"NO", 0);
1154    cp = getenv("EDITOR");
1155    if (!cp)
1156	cp = "/usr/bin/ee";
1157    variable_set2(VAR_EDITOR,			cp, 0);
1158    variable_set2(VAR_FTP_USER,			"ftp", 0);
1159    variable_set2(VAR_BROWSER_PACKAGE,		"links", 0);
1160    variable_set2(VAR_BROWSER_BINARY,		"/usr/local/bin/links", 0);
1161    variable_set2(VAR_FTP_STATE,		"passive", 0);
1162    variable_set2(VAR_NFS_SECURE,		"NO", -1);
1163    variable_set2(VAR_NFS_TCP,   		"NO", -1);
1164    variable_set2(VAR_NFS_V3,   		"YES", -1);
1165    if (OnVTY)
1166	    variable_set2(VAR_FIXIT_TTY,		"standard", 0);
1167    else
1168	    variable_set2(VAR_FIXIT_TTY,		"serial", 0);
1169    variable_set2(VAR_PKG_TMPDIR,		"/var/tmp", 0);
1170    variable_set2(VAR_MEDIA_TIMEOUT,		itoa(MEDIA_TIMEOUT), 0);
1171    if (getpid() != 1)
1172	variable_set2(SYSTEM_STATE,		"update", 0);
1173    else
1174	variable_set2(SYSTEM_STATE,		"init", 0);
1175    variable_set2(VAR_NEWFS_ARGS,		"-b 16384 -f 2048", 0);
1176    variable_set2(VAR_CONSTERM,                 "NO", 0);
1177    return DITEM_SUCCESS;
1178}
1179
1180/* Load the environment up from various system configuration files */
1181void
1182installEnvironment(void)
1183{
1184    configEnvironmentRC_conf();
1185    if (file_readable("/etc/resolv.conf"))
1186	configEnvironmentResolv("/etc/resolv.conf");
1187}
1188
1189/* Copy the boot floppy contents into /stand */
1190Boolean
1191copySelf(void)
1192{
1193    int i;
1194
1195    if (file_readable("/boot.help"))
1196	vsystem("cp /boot.help /mnt");
1197    msgWeHaveOutput("Copying the boot floppy to /stand on root filesystem");
1198    i = vsystem("find -x /stand | cpio %s -pdum /mnt", cpioVerbosity());
1199    if (i) {
1200	msgConfirm("Copy returned error status of %d!", i);
1201	return FALSE;
1202    }
1203
1204    /* Copy the /etc files into their rightful place */
1205    if (vsystem("cd /mnt/stand; find etc | cpio %s -pdum /mnt", cpioVerbosity())) {
1206	msgConfirm("Couldn't copy up the /etc files!");
1207	return TRUE;
1208    }
1209    return TRUE;
1210}
1211
1212static void
1213create_termcap(void)
1214{
1215    FILE *fp;
1216
1217    const char *caps[] = {
1218	termcap_vt100, termcap_cons25, termcap_cons25_m, termcap_cons25r,
1219	termcap_cons25r_m, termcap_cons25l1, termcap_cons25l1_m,
1220	termcap_xterm, NULL,
1221    };
1222    const char **cp;
1223
1224    if (!file_readable(TERMCAP_FILE)) {
1225	Mkdir("/usr/share/misc");
1226	fp = fopen(TERMCAP_FILE, "w");
1227	if (!fp) {
1228	    msgConfirm("Unable to initialize termcap file. Some screen-oriented\nutilities may not work.");
1229	    return;
1230	}
1231	cp = caps;
1232	while (*cp)
1233	    fprintf(fp, "%s\n", *(cp++));
1234	fclose(fp);
1235    }
1236}
1237