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