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