install.c revision 79452
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 79452 2001-07-09 09:24:06Z brian $
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
274	    || !DEVICE_INIT(mediaDevice)) {
275	    /* If we can't initialize it, it's probably not a FreeBSD CDROM so punt on it */
276	    mediaClose();
277	    if (msgYesNo("Unable to mount the disc - do you want to try again?") != 0)
278		return DITEM_FAILURE;
279	}
280	else
281	    break;
282    }
283
284    /* Since the fixit code expects everything to be in /mnt2, and the CDROM mounting stuff /dist, do
285     * a little kludge dance here..
286     */
287    if (symlink("/dist", "/mnt2")) {
288	msgConfirm("Unable to symlink /mnt2 to the disc mount point.  Please report this\n"
289		   "unexpected failure to freebsd-bugs@FreeBSD.org.");
290	return DITEM_FAILURE;
291    }
292
293    /*
294     * If /tmp points to /mnt2/tmp from a previous fixit floppy session, it's
295     * not very good for us if we point it to the CDROM now.  Rather make it
296     * a directory in the root MFS then.  Experienced admins will still be
297     * able to mount their disk's /tmp over this if they need.
298     */
299    if (lstat("/tmp", &sb) == 0 && (sb.st_mode & S_IFMT) == S_IFLNK)
300	(void)unlink("/tmp");
301    Mkdir("/tmp");
302
303    /*
304     * Since setuid binaries ignore LD_LIBRARY_PATH, we indeed need the
305     * ld.so.hints file.  Fortunately, it's fairly small (~ 3 KB).
306     */
307    if (!file_readable("/var/run/ld.so.hints")) {
308	Mkdir("/var/run");
309	if (vsystem("/mnt2/sbin/ldconfig -s /mnt2/usr/lib")) {
310	    msgConfirm("Warning: ldconfig could not create the ld.so hints file.\n"
311		       "Dynamic executables from the disc likely won't work.");
312	}
313    }
314
315    /* Yet more iggly hardcoded pathnames. */
316    Mkdir("/usr/libexec");
317    if (!file_readable("/usr/libexec/ld.so") && file_readable("/mnt2/usr/libexec/ld.so")) {
318	if (symlink("/mnt2/usr/libexec/ld.so", "/usr/libexec/ld.so"))
319	    msgDebug("Couldn't link to ld.so - not necessarily a problem for ELF\n");
320    }
321    if (!file_readable("/usr/libexec/ld-elf.so.1")) {
322	if (symlink("/mnt2/usr/libexec/ld-elf.so.1", "/usr/libexec/ld-elf.so.1")) {
323	    msgConfirm("Warning: could not create the symlink for ld-elf.so.1\n"
324		       "Dynamic executables from the disc likely won't work.");
325	}
326    }
327    /* optional nicety */
328    if (!file_readable("/usr/bin/vi"))
329	symlink("/mnt2/usr/bin/vi", "/usr/bin/vi");
330    fixit_common();
331    mediaClose();
332    msgConfirm("Please remove the FreeBSD fixit CDROM now.");
333    return DITEM_SUCCESS;
334}
335
336int
337installFixitFloppy(dialogMenuItem *self)
338{
339    struct ufs_args args;
340    extern char *distWanted;
341
342    if (!RunningAsInit)
343	return DITEM_SUCCESS;
344
345    /* Try to open the floppy drive */
346    if (DITEM_STATUS(mediaSetFloppy(NULL)) == DITEM_FAILURE || !mediaDevice) {
347	msgConfirm("Unable to set media device to floppy.");
348	mediaClose();
349	return DITEM_FAILURE;
350    }
351
352    memset(&args, 0, sizeof(args));
353    args.fspec = mediaDevice->devname;
354    mediaDevice->private = "/mnt2";
355    distWanted = NULL;
356    Mkdir("/mnt2");
357
358    variable_set2(SYSTEM_STATE, "fixit", 0);
359
360    while (1) {
361	if (!DEVICE_INIT(mediaDevice)) {
362	    if (msgYesNo("The attempt to mount the fixit floppy failed, bad floppy\n"
363			 "or unclean filesystem.  Do you want to try again?"))
364		return DITEM_FAILURE;
365	}
366	else
367	    break;
368    }
369    if (!directory_exists("/tmp"))
370	(void)symlink("/mnt2/tmp", "/tmp");
371    fixit_common();
372    mediaClose();
373    msgConfirm("Please remove the fixit floppy now.");
374    return DITEM_SUCCESS;
375}
376
377/*
378 * The common code for both fixit variants.
379 */
380static void
381fixit_common(void)
382{
383    pid_t child;
384    int waitstatus;
385
386    if (!directory_exists("/var/tmp/vi.recover")) {
387	if (DITEM_STATUS(Mkdir("/var/tmp/vi.recover")) != DITEM_SUCCESS) {
388	    msgConfirm("Warning:  Was unable to create a /var/tmp/vi.recover directory.\n"
389		       "vi will kvetch and moan about it as a result but should still\n"
390		       "be essentially usable.");
391	}
392    }
393    if (!directory_exists("/bin"))
394	(void)Mkdir("/bin");
395    (void)symlink("/stand/sh", "/bin/sh");
396    /* Link the /etc/ files */
397    if (DITEM_STATUS(Mkdir("/etc")) != DITEM_SUCCESS)
398	msgConfirm("Unable to create an /etc directory!  Things are weird on this floppy..");
399    else if ((symlink("/mnt2/etc/spwd.db", "/etc/spwd.db") == -1 && errno != EEXIST) ||
400	     (symlink("/mnt2/etc/protocols", "/etc/protocols") == -1 && errno != EEXIST) ||
401	     (symlink("/mnt2/etc/services", "/etc/services") == -1 && errno != EEXIST))
402	msgConfirm("Couldn't symlink the /etc/ files!  I'm not sure I like this..");
403    if (!file_readable(TERMCAP_FILE))
404	create_termcap();
405    if (strcmp(variable_get(VAR_FIXIT_TTY), "serial") == 0)
406	systemSuspendDialog();	/* must be before the fork() */
407    if (!(child = fork())) {
408	int i, fd;
409	struct termios foo;
410	extern int login_tty(int);
411
412	ioctl(0, TIOCNOTTY, NULL);
413	for (i = getdtablesize(); i >= 0; --i)
414	    close(i);
415
416	if (strcmp(variable_get(VAR_FIXIT_TTY), "serial") == 0)
417	    fd = open("/dev/console", O_RDWR);
418	else
419	    fd = open("/dev/ttyv3", O_RDWR);
420	ioctl(0, TIOCSCTTY, &fd);
421	dup2(0, 1);
422	dup2(0, 2);
423	DebugFD = 2;
424	if (login_tty(fd) == -1)
425	    msgDebug("fixit: I can't set the controlling terminal.\n");
426
427	signal(SIGTTOU, SIG_IGN);
428	if (tcgetattr(0, &foo) != -1) {
429	    foo.c_cc[VERASE] = '\010';
430	    if (tcsetattr(0, TCSANOW, &foo) == -1)
431		msgDebug("fixit shell: Unable to set erase character.\n");
432	}
433	else
434	    msgDebug("fixit shell: Unable to get terminal attributes!\n");
435	setenv("PATH", "/bin:/sbin:/usr/bin:/usr/sbin:/stand:"
436	       "/mnt2/stand:/mnt2/bin:/mnt2/sbin:/mnt2/usr/bin:/mnt2/usr/sbin", 1);
437	setenv("MAKEDEVPATH", "/sbin:/bin:/stand:"
438	       "/mnt2/sbin:/mnt2/bin:/mnt2/stand", 1);
439	if (strcmp(variable_get(VAR_FIXIT_TTY), "serial") == 0) {
440	    printf("Waiting for fixit shell to exit.\n"
441		"When you are done, type ``exit'' to exit\n"
442		"the fixit shell and be returned here.\n\n");
443	    fflush(stdout);
444	}
445
446	/* use the .profile from the fixit medium */
447	setenv("HOME", "/mnt2", 1);
448	chdir("/mnt2");
449	execlp("sh", "-sh", (char *)0);
450	msgDebug("fixit shell: Failed to execute shell!\n");
451	_exit(1);;
452    }
453    else {
454	if (strcmp(variable_get(VAR_FIXIT_TTY), "standard") == 0) {
455	    dialog_clear_norefresh();
456	    msgNotify("Waiting for fixit shell to exit.  Go to VTY4 now by\n"
457		"typing ALT-F4.  When you are done, type ``exit'' to exit\n"
458		"the fixit shell and be returned here\n.");
459	}
460	(void)waitpid(child, &waitstatus, 0);
461	if (strcmp(variable_get(VAR_FIXIT_TTY), "serial") == 0)
462	    systemResumeDialog();
463    }
464    dialog_clear();
465}
466
467
468int
469installExpress(dialogMenuItem *self)
470{
471    int i;
472
473    dialog_clear_norefresh();
474    variable_set2(SYSTEM_STATE, "express", 0);
475#ifndef __alpha__
476    if (DITEM_STATUS((i = diskPartitionEditor(self))) == DITEM_FAILURE)
477	return i;
478#endif
479
480    if (DITEM_STATUS((i = diskLabelEditor(self))) == DITEM_FAILURE)
481	return i;
482
483    if (DITEM_STATUS((i = installCommit(self))) == DITEM_SUCCESS) {
484	i |= DITEM_LEAVE_MENU;
485	/* Set default security level */
486	configSecurityModerate(NULL);
487
488	/* Give user the option of one last configuration spree */
489	installConfigure();
490    }
491    return i;
492}
493
494/* Standard mode installation */
495int
496installStandard(dialogMenuItem *self)
497{
498    int i, tries = 0;
499    Device **devs;
500
501    variable_set2(SYSTEM_STATE, "standard", 0);
502    dialog_clear_norefresh();
503#ifndef __alpha__
504    msgConfirm("In the next menu, you will need to set up a DOS-style (\"fdisk\") partitioning\n"
505	       "scheme for your hard disk.  If you simply wish to devote all disk space\n"
506	       "to FreeBSD (overwriting anything else that might be on the disk(s) selected)\n"
507	       "then use the (A)ll command to select the default partitioning scheme followed\n"
508	       "by a (Q)uit.  If you wish to allocate only free space to FreeBSD, move to a\n"
509	       "partition marked \"unused\" and use the (C)reate command.");
510
511nodisks:
512    if (DITEM_STATUS(diskPartitionEditor(self)) == DITEM_FAILURE)
513	return DITEM_FAILURE;
514
515    if (diskGetSelectCount(&devs) <= 0 && tries < 3) {
516	msgConfirm("You need to select some disks to operate on!  Be sure to use SPACE\n"
517		   "instead of RETURN in the disk selection menu when selecting a disk.");
518	++tries;
519	goto nodisks;
520    }
521#endif
522
523#ifdef __alpha__
524    msgConfirm("Now you need to create BSD partitions on the disk which you are\n"
525	       "installing to.  If you have a reasonable amount of disk space (200MB or more)\n"
526	       "and don't have any special requirements, simply use the (A)uto command to\n"
527	       "allocate space automatically.  If you have more specific needs or just don't\n"
528	       "care for the layout chosen by (A)uto, press F1 for more information on\n"
529	       "manual layout.");
530#else
531    msgConfirm("Now you need to create BSD partitions inside of the fdisk partition(s)\n"
532	       "just created.  If you have a reasonable amount of disk space (200MB or more)\n"
533	       "and don't have any special requirements, simply use the (A)uto command to\n"
534	       "allocate space automatically.  If you have more specific needs or just don't\n"
535	       "care for the layout chosen by (A)uto, press F1 for more information on\n"
536	       "manual layout.");
537#endif
538
539    if (DITEM_STATUS(diskLabelEditor(self)) == DITEM_FAILURE)
540	return DITEM_FAILURE;
541
542    if (DITEM_STATUS((i = installCommit(self))) == DITEM_FAILURE) {
543	dialog_clear();
544	msgConfirm("Installation completed with some errors.  You may wish to\n"
545		   "scroll through the debugging messages on VTY1 with the\n"
546		   "scroll-lock feature.  You can also choose \"No\" at the next\n"
547		   "prompt and go back into the installation menus to try and retry\n"
548		   "whichever operations have failed.");
549	return i;
550
551    }
552    else {
553	dialog_clear();
554	msgConfirm("Congratulations!  You now have FreeBSD installed on your system.\n\n"
555		   "We will now move on to the final configuration questions.\n"
556		   "For any option you do not wish to configure, simply select\n"
557		   "No.\n\n"
558		   "If you wish to re-enter this utility after the system is up, you\n"
559		   "may do so by typing: /stand/sysinstall.");
560    }
561    if (mediaDevice->type != DEVICE_TYPE_FTP && mediaDevice->type != DEVICE_TYPE_NFS) {
562	if (!msgYesNo("Would you like to configure any Ethernet or SLIP/PPP network devices?")) {
563	    Device *tmp = tcpDeviceSelect();
564
565	    if (tmp && !((DevInfo *)tmp->private)->use_dhcp && !msgYesNo("Would you like to bring the %s interface up right now?", tmp->name))
566		if (!DEVICE_INIT(tmp))
567		    msgConfirm("Initialization of %s device failed.", tmp->name);
568	}
569	dialog_clear_norefresh();
570    }
571
572    if (!msgNoYes("Do you want this machine to function as a network gateway?"))
573	variable_set2("gateway_enable", "YES", 1);
574
575    dialog_clear_norefresh();
576    if (!msgNoYes("Do you want to have anonymous FTP access to this machine?"))
577	configAnonFTP(self);
578
579    dialog_clear_norefresh();
580    if (!msgNoYes("Do you want to configure this machine as an NFS server?"))
581	configNFSServer(self);
582
583    dialog_clear_norefresh();
584    if (!msgNoYes("Do you want to configure this machine as an NFS client?"))
585	variable_set2("nfs_client_enable", "YES", 1);
586
587    if (!msgNoYes("Do you want to select a default security profile for\n"
588	         "this host (select No for \"medium\" security)?"))
589	configSecurityProfile(self);
590    else
591	configSecurityModerate(self);
592
593    dialog_clear_norefresh();
594    if (!msgNoYes("Would you like to customize your system console settings?"))
595	dmenuOpenSimple(&MenuSyscons, FALSE);
596
597    dialog_clear_norefresh();
598    if (!msgYesNo("Would you like to set this machine's time zone now?"))
599	systemExecute("tzsetup");
600
601#ifdef __i386__
602    dialog_clear_norefresh();
603    if (!msgYesNo("Would you like to enable Linux binary compatibility?"))
604	(void)configLinux(self);
605#endif
606
607    dialog_clear_norefresh();
608    if (msgNoYes("Does this system have a USB mouse attached to it?"))
609	dmenuOpenSimple(&MenuMouse, FALSE);
610
611    /* Now would be a good time to checkpoint the configuration data */
612    configRC_conf();
613    sync();
614
615    if (directory_exists("/usr/X11R6")) {
616	dialog_clear_norefresh();
617	if (!msgYesNo("Would you like to configure your X server at this time?"))
618	    (void)configXSetup(self);
619    }
620
621    dialog_clear_norefresh();
622    if (!msgYesNo("The FreeBSD package collection is a collection of thousands of ready-to-run\n"
623		  "applications, from text editors to games to WEB servers and more.  Would you\n"
624		  "like to browse the collection now?")) {
625	(void)configPackages(self);
626    }
627
628    if (!msgYesNo("Would you like to add any initial user accounts to the system?\n"
629		  "Adding at least one account for yourself at this stage is suggested\n"
630		  "since working as the \"root\" user is dangerous (it is easy to do\n"
631		  "things which adversely affect the entire system)."))
632	(void)configUsers(self);
633
634    msgConfirm("Now you must set the system manager's password.\n"
635	       "This is the password you'll use to log in as \"root\".");
636    if (!systemExecute("passwd root"))
637	variable_set2("root_password", "YES", 0);
638
639    /* XXX Put whatever other nice configuration questions you'd like to ask the user here XXX */
640
641    /* Give user the option of one last configuration spree */
642    dialog_clear_norefresh();
643    installConfigure();
644    return DITEM_LEAVE_MENU;
645}
646
647/* The version of commit we call from the Install Custom menu */
648int
649installCustomCommit(dialogMenuItem *self)
650{
651    int i;
652
653    i = installCommit(self);
654    if (DITEM_STATUS(i) == DITEM_SUCCESS) {
655	/* Set default security level */
656	configSecurityModerate(NULL);
657
658	/* Give user the option of one last configuration spree */
659	installConfigure();
660	return i;
661    }
662    else
663	msgConfirm("The commit operation completed with errors.  Not\n"
664		   "updating /etc files.");
665    return i;
666}
667
668/*
669 * What happens when we finally decide to going ahead with the installation.
670 *
671 * This is broken into multiple stages so that the user can do a full
672 * installation but come back here again to load more distributions,
673 * perhaps from a different media type.  This would allow, for
674 * example, the user to load the majority of the system from CDROM and
675 * then use ftp to load just the CRYPTO dist.
676 */
677int
678installCommit(dialogMenuItem *self)
679{
680    int i;
681    char *str;
682
683    dialog_clear_norefresh();
684    if (!Dists)
685	distConfig(NULL);
686
687    if (!Dists) {
688	(void)dmenuOpenSimple(&MenuDistributions, FALSE);
689	/* select reasonable defaults if necessary */
690	if (!Dists)
691	    Dists = _DIST_USER;
692    }
693
694    if (!mediaVerify())
695	return DITEM_FAILURE;
696
697    str = variable_get(SYSTEM_STATE);
698    if (isDebug())
699	msgDebug("installCommit: System state is `%s'\n", str);
700
701    /* Installation stuff we wouldn't do to a running system */
702    if (RunningAsInit && DITEM_STATUS((i = installInitial())) == DITEM_FAILURE)
703	return i;
704
705try_media:
706    if (!DEVICE_INIT(mediaDevice)) {
707	if (!msgYesNo("Unable to initialize selected media. Would you like to\n"
708		      "adjust your media configuration and try again?")) {
709	    mediaDevice = NULL;
710	    if (!mediaVerify())
711		return DITEM_FAILURE;
712	    else
713		goto try_media;
714	}
715	else
716	    return DITEM_FAILURE;
717    }
718
719    /* Now go get it all */
720    i = distExtractAll(self);
721
722    /* When running as init, *now* it's safe to grab the rc.foo vars */
723    installEnvironment();
724
725    variable_set2(SYSTEM_STATE, DITEM_STATUS(i) == DITEM_FAILURE ? "error-install" : "full-install", 0);
726
727    return i;
728}
729
730static void
731installConfigure(void)
732{
733    /* Final menu of last resort */
734    if (!msgNoYes("Visit the general configuration menu for a chance to set\n"
735		  "any last options?"))
736	dmenuOpenSimple(&MenuConfigure, FALSE);
737    configRC_conf();
738    sync();
739}
740
741int
742installFixupBin(dialogMenuItem *self)
743{
744    Device **devs;
745    char *cp;
746    int i;
747    FILE *fp;
748    int kstat = 1;
749
750    /* All of this is done only as init, just to be safe */
751    if (RunningAsInit) {
752#ifdef __i386__
753        /* Snapshot any boot -c changes back to the new kernel */
754	cp = variable_get(VAR_KGET);
755	if (cp && (*cp == 'Y' || *cp == 'y')) {
756	    if ((kstat = kget("/boot/kernel.conf")) != NULL) {
757		msgConfirm("Unable to save boot -c changes to new kernel,\n"
758			   "please see the debug screen (ALT-F2) for details.");
759	    }
760	}
761	if ((fp = fopen("/boot/loader.conf", "a")) != NULL) {
762	    if (!kstat || !OnVTY)
763		fprintf(fp, "# -- sysinstall generated deltas -- #\n");
764	    if (!kstat)
765		fprintf(fp, "userconfig_script_load=\"YES\"\n");
766	    if (!OnVTY)
767		fprintf(fp, "console=\"comconsole\"\n");
768	    fclose(fp);
769	}
770#endif
771	/* BOGON #1: Resurrect /dev after bin distribution screws it up */
772	dialog_clear_norefresh();
773	msgNotify("Remaking all devices.. Please wait!");
774	if (vsystem("cd /dev; sh MAKEDEV all")) {
775	    msgConfirm("MAKEDEV returned non-zero status");
776	    return DITEM_FAILURE | DITEM_RESTORE;
777	}
778
779	dialog_clear_norefresh();
780	msgNotify("Resurrecting /dev entries for slices..");
781	devs = deviceFind(NULL, DEVICE_TYPE_DISK);
782	if (!devs)
783	    msgFatal("Couldn't get a disk device list!");
784
785	/* Resurrect the slices that the former clobbered */
786	for (i = 0; devs[i]; i++) {
787	    Disk *disk = (Disk *)devs[i]->private;
788	    Chunk *c1;
789
790	    if (!devs[i]->enabled)
791		continue;
792	    if (!disk->chunks)
793		msgFatal("No chunk list found for %s!", disk->name);
794	    for (c1 = disk->chunks->part; c1; c1 = c1->next) {
795		if (c1->type == freebsd) {
796		    dialog_clear_norefresh();
797		    msgNotify("Making slice entries for %s", c1->name);
798		    if (vsystem("cd /dev; sh MAKEDEV %sh", c1->name)) {
799			msgConfirm("Unable to make slice entries for %s!", c1->name);
800			return DITEM_FAILURE | DITEM_RESTORE;
801		    }
802		}
803	    }
804	}
805
806	/* BOGON #2: We leave /etc in a bad state */
807	chmod("/etc", 0755);
808
809	/* BOGON #3: No /var/db/mountdtab complains */
810	Mkdir("/var/db");
811	creat("/var/db/mountdtab", 0644);
812
813	/* BOGON #4: /compat created by default in root fs */
814	Mkdir("/usr/compat");
815	vsystem("ln -s usr/compat /compat");
816
817	/* BOGON #5: aliases database not build for bin */
818	vsystem("newaliases");
819
820	/* Now run all the mtree stuff to fix things up */
821        vsystem("mtree -deU -f /etc/mtree/BSD.root.dist -p /");
822        vsystem("mtree -deU -f /etc/mtree/BSD.var.dist -p /var");
823        vsystem("mtree -deU -f /etc/mtree/BSD.usr.dist -p /usr");
824
825	/* Do all the last ugly work-arounds here */
826    }
827    return DITEM_SUCCESS | DITEM_RESTORE;
828}
829
830/* Fix side-effects from the the XFree86 installation */
831int
832installFixupXFree(dialogMenuItem *self)
833{
834    /* BOGON #1:  XFree86 requires various specialized fixups */
835    if (directory_exists("/usr/X11R6")) {
836	dialog_clear_norefresh();
837	msgNotify("Fixing permissions in XFree86 tree..");
838	vsystem("chmod -R a+r /usr/X11R6");
839	vsystem("find /usr/X11R6 -type d | xargs chmod a+x");
840
841	/* Also do bogus minimal package registration so ports don't whine */
842	if (file_readable("/usr/X11R6/lib/X11/pkgreg.tar.gz")) {
843	    dialog_clear_norefresh();
844	    msgNotify("Installing package metainfo..");
845	    vsystem("tar xpzf /usr/X11R6/lib/X11/pkgreg.tar.gz -C / && rm /usr/X11R6/lib/X11/pkgreg.tar.gz");
846	}
847    }
848    return DITEM_SUCCESS | DITEM_RESTORE;
849}
850
851/* Go newfs and/or mount all the filesystems we've been asked to */
852int
853installFilesystems(dialogMenuItem *self)
854{
855    int i;
856    Disk *disk;
857    Chunk *c1, *c2, *rootdev, *swapdev, *usrdev, *vardev;
858    Device **devs;
859    PartInfo *root;
860    char dname[80];
861    extern int MakeDevChunk(Chunk *c, char *n);
862    Boolean upgrade = FALSE;
863
864    /* If we've already done this, bail out */
865    if (!variable_cmp(DISK_LABELLED, "written"))
866	return DITEM_SUCCESS;
867
868    upgrade = !variable_cmp(SYSTEM_STATE, "upgrade");
869    if (!checkLabels(TRUE, &rootdev, &swapdev, &usrdev, &vardev))
870	return DITEM_FAILURE;
871
872    if (rootdev)
873	root = (PartInfo *)rootdev->private_data;
874    else
875	root = NULL;
876
877    command_clear();
878    if (swapdev && RunningAsInit) {
879	/* As the very first thing, try to get ourselves some swap space */
880	sprintf(dname, "/dev/%s", swapdev->name);
881	if (!Fake && (!MakeDevChunk(swapdev, "/dev") || !file_readable(dname))) {
882	    msgConfirm("Unable to make device node for %s in /dev!\n"
883		       "The creation of filesystems will be aborted.", dname);
884	    return DITEM_FAILURE;
885	}
886
887	if (!Fake) {
888	    if (!swapon(dname)) {
889		dialog_clear_norefresh();
890		msgNotify("Added %s as initial swap device", dname);
891	    }
892	    else {
893		msgConfirm("WARNING!  Unable to swap to %s: %s\n"
894			   "This may cause the installation to fail at some point\n"
895			   "if you don't have a lot of memory.", dname, strerror(errno));
896	    }
897	}
898    }
899
900    if (rootdev && RunningAsInit) {
901	/* Next, create and/or mount the root device */
902	sprintf(dname, "/dev/%s", rootdev->name);
903	if (!Fake && (!MakeDevChunk(rootdev, "/dev") || !file_readable(dname))) {
904	    msgConfirm("Unable to make device node for %s in /dev!\n"
905		       "The creation of filesystems will be aborted.", dname);
906	    return DITEM_FAILURE | DITEM_RESTORE;
907	}
908	if (strcmp(root->mountpoint, "/"))
909	    msgConfirm("Warning: %s is marked as a root partition but is mounted on %s", rootdev->name, root->mountpoint);
910
911	if (root->newfs && (!upgrade || !msgNoYes("You are upgrading - are you SURE you want to newfs the root partition?"))) {
912	    int i;
913
914	    dialog_clear_norefresh();
915	    msgNotify("Making a new root filesystem on %s", dname);
916	    i = vsystem("%s %s", root->newfs_cmd, dname);
917	    if (i) {
918		msgConfirm("Unable to make new root filesystem on %s!\n"
919			   "Command returned status %d", dname, i);
920		return DITEM_FAILURE | DITEM_RESTORE;
921	    }
922	}
923	else {
924	    if (!upgrade) {
925		msgConfirm("Warning:  Using existing root partition.  It will be assumed\n"
926			   "that you have the appropriate device entries already in /dev.");
927	    }
928	    dialog_clear_norefresh();
929	    msgNotify("Checking integrity of existing %s filesystem.", dname);
930	    i = vsystem("fsck -y %s", dname);
931	    if (i)
932		msgConfirm("Warning: fsck returned status of %d for %s.\n"
933			   "This partition may be unsafe to use.", i, dname);
934	}
935	if (root->soft) {
936	    i = vsystem("tunefs -n enable %s", dname);
937	    if (i)
938		msgConfirm("Warning:  Unable to enable softupdates for root filesystem on %s", dname);
939	}
940
941	/* Switch to block device */
942	sprintf(dname, "/dev/%s", rootdev->name);
943	if (Mount("/mnt", dname)) {
944	    msgConfirm("Unable to mount the root file system on %s!  Giving up.", dname);
945	    return DITEM_FAILURE | DITEM_RESTORE;
946	}
947    }
948
949    /* Now buzz through the rest of the partitions and mount them too */
950    devs = deviceFind(NULL, DEVICE_TYPE_DISK);
951    for (i = 0; devs[i]; i++) {
952	if (!devs[i]->enabled)
953	    continue;
954
955	disk = (Disk *)devs[i]->private;
956	if (!disk->chunks) {
957	    msgConfirm("No chunk list found for %s!", disk->name);
958	    return DITEM_FAILURE | DITEM_RESTORE;
959	}
960	if (RunningAsInit && root && (root->newfs || upgrade)) {
961	    Mkdir("/mnt/dev");
962	    if (!Fake)
963		MakeDevDisk(disk, "/mnt/dev");
964	}
965	else if (!RunningAsInit && !Fake)
966	    MakeDevDisk(disk, "/dev");
967
968	for (c1 = disk->chunks->part; c1; c1 = c1->next) {
969	    if (c1->type == freebsd) {
970		for (c2 = c1->part; c2; c2 = c2->next) {
971		    if (c2->type == part && c2->subtype != FS_SWAP && c2->private_data) {
972			PartInfo *tmp = (PartInfo *)c2->private_data;
973
974			/* Already did root */
975			if (c2 == rootdev)
976			    continue;
977
978			if (tmp->newfs && (!upgrade || !msgNoYes("You are upgrading - are you SURE you want to newfs /dev/%s?", c2->name)))
979			    command_shell_add(tmp->mountpoint, "%s %s/dev/%s", tmp->newfs_cmd, RunningAsInit ? "/mnt" : "", c2->name);
980			else
981			    command_shell_add(tmp->mountpoint, "fsck -y %s/dev/%s", RunningAsInit ? "/mnt" : "", c2->name);
982			if (tmp->soft)
983			    command_shell_add(tmp->mountpoint, "tunefs -n enable %s/dev/%s", RunningAsInit ? "/mnt" : "", c2->name);
984			command_func_add(tmp->mountpoint, Mount, c2->name);
985		    }
986		    else if (c2->type == part && c2->subtype == FS_SWAP) {
987			char fname[80];
988			int i;
989
990			if (c2 == swapdev)
991			    continue;
992			sprintf(fname, "%s/dev/%s", RunningAsInit ? "/mnt" : "", c2->name);
993			i = (Fake || swapon(fname));
994			if (!i) {
995			    dialog_clear_norefresh();
996			    msgNotify("Added %s as an additional swap device", fname);
997			}
998			else {
999			    msgConfirm("Unable to add %s as a swap device: %s", fname, strerror(errno));
1000			}
1001		    }
1002		}
1003	    }
1004	    else if (c1->type == fat && c1->private_data && (root->newfs || upgrade)) {
1005		char name[FILENAME_MAX];
1006
1007		sprintf(name, "%s/%s", RunningAsInit ? "/mnt" : "", ((PartInfo *)c1->private_data)->mountpoint);
1008		Mkdir(name);
1009	    }
1010	}
1011    }
1012
1013    if (RunningAsInit) {
1014	dialog_clear_norefresh();
1015	msgNotify("Copying initial device files..");
1016	/* Copy the boot floppy's dev files */
1017	if ((root->newfs || upgrade) && vsystem("find -x /dev | cpio %s -pdum /mnt", cpioVerbosity())) {
1018	    msgConfirm("Couldn't clone the /dev files!");
1019	    return DITEM_FAILURE | DITEM_RESTORE;
1020	}
1021    }
1022
1023    command_sort();
1024    command_execute();
1025    dialog_clear_norefresh();
1026    return DITEM_SUCCESS | DITEM_RESTORE;
1027}
1028
1029static char *
1030getRelname(void)
1031{
1032    static char buf[64];
1033    int sz = (sizeof buf) - 1;
1034
1035    if (sysctlbyname("kern.osrelease", buf, &sz, NULL, 0) != -1) {
1036	buf[sz] = '\0';
1037	return buf;
1038    }
1039    else
1040	return "<unknown>";
1041}
1042
1043/* Initialize various user-settable values to their defaults */
1044int
1045installVarDefaults(dialogMenuItem *self)
1046{
1047    char *cp;
1048
1049    /* Set default startup options */
1050    variable_set2(VAR_RELNAME,			getRelname(), 0);
1051    variable_set2(VAR_CPIO_VERBOSITY,		"high", 0);
1052    variable_set2(VAR_KGET,			"YES", 0);
1053    variable_set2(VAR_TAPE_BLOCKSIZE,		DEFAULT_TAPE_BLOCKSIZE, 0);
1054    variable_set2(VAR_INSTALL_ROOT,		"/", 0);
1055    variable_set2(VAR_INSTALL_CFG,		"install.cfg", 0);
1056    variable_set2(VAR_TRY_DHCP,			"NO", 0);	/* For now */
1057    variable_set2(VAR_TRY_RTSOL,		"NO", 0);	/* For now */
1058    cp = getenv("EDITOR");
1059    if (!cp)
1060	cp = "/usr/bin/ee";
1061    variable_set2(VAR_EDITOR,			cp, 0);
1062    variable_set2(VAR_FTP_USER,			"ftp", 0);
1063    variable_set2(VAR_BROWSER_PACKAGE,		"links", 0);
1064    variable_set2(VAR_BROWSER_BINARY,		"/usr/local/bin/links", 0);
1065    variable_set2(VAR_FTP_STATE,		"passive", 0);
1066    variable_set2(VAR_NFS_SECURE,		"NO", -1);
1067    if (OnVTY)
1068	    variable_set2(VAR_FIXIT_TTY,		"standard", 0);
1069    else
1070	    variable_set2(VAR_FIXIT_TTY,		"serial", 0);
1071    variable_set2(VAR_PKG_TMPDIR,		"/usr/tmp", 0);
1072    variable_set2(VAR_MEDIA_TIMEOUT,		itoa(MEDIA_TIMEOUT), 0);
1073    if (getpid() != 1)
1074	variable_set2(SYSTEM_STATE,		"update", 0);
1075    else
1076	variable_set2(SYSTEM_STATE,		"init", 0);
1077    variable_set2(VAR_NEWFS_ARGS,		"-b 8192 -f 1024 -c 22", 0);
1078    return DITEM_SUCCESS;
1079}
1080
1081/* Load the environment up from various system configuration files */
1082void
1083installEnvironment(void)
1084{
1085    configEnvironmentRC_conf();
1086    if (file_readable("/etc/resolv.conf"))
1087	configEnvironmentResolv("/etc/resolv.conf");
1088}
1089
1090/* Copy the boot floppy contents into /stand */
1091Boolean
1092copySelf(void)
1093{
1094    int i;
1095
1096    if (file_readable("/boot.help"))
1097	vsystem("cp /boot.help /mnt");
1098    msgWeHaveOutput("Copying the boot floppy to /stand on root filesystem");
1099    i = vsystem("find -x /stand | cpio %s -pdum /mnt", cpioVerbosity());
1100    if (i) {
1101	msgConfirm("Copy returned error status of %d!", i);
1102	return FALSE;
1103    }
1104
1105    /* Copy the /etc files into their rightful place */
1106    if (vsystem("cd /mnt/stand; find etc | cpio %s -pdum /mnt", cpioVerbosity())) {
1107	msgConfirm("Couldn't copy up the /etc files!");
1108	return TRUE;
1109    }
1110    return TRUE;
1111}
1112
1113static void
1114create_termcap(void)
1115{
1116    FILE *fp;
1117
1118    const char *caps[] = {
1119	termcap_vt100, termcap_cons25, termcap_cons25_m, termcap_cons25r,
1120	termcap_cons25r_m, termcap_cons25l1, termcap_cons25l1_m,
1121	termcap_xterm, NULL,
1122    };
1123    const char **cp;
1124
1125    if (!file_readable(TERMCAP_FILE)) {
1126	Mkdir("/usr/share/misc");
1127	fp = fopen(TERMCAP_FILE, "w");
1128	if (!fp) {
1129	    msgConfirm("Unable to initialize termcap file. Some screen-oriented\nutilities may not work.");
1130	    return;
1131	}
1132	cp = caps;
1133	while (*cp)
1134	    fprintf(fp, "%s\n", *(cp++));
1135	fclose(fp);
1136    }
1137}
1138