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