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