install.c revision 29073
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.196 1997/08/18 21:47:33 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    dialog_clear_norefresh();
451    if (DITEM_STATUS((i = installCommit(self))) == DITEM_SUCCESS) {
452	i |= DITEM_LEAVE_MENU;
453	/* Give user the option of one last configuration spree */
454	installConfigure();
455    }
456    return i | DITEM_RESTORE;
457}
458
459/* Novice mode installation */
460int
461installNovice(dialogMenuItem *self)
462{
463    int i;
464
465    variable_set2(SYSTEM_STATE, "novice");
466    dialog_clear_norefresh();
467    msgConfirm("In the next menu, you will need to set up a DOS-style (\"fdisk\") partitioning\n"
468	       "scheme for your hard disk.  If you simply wish to devote all disk space\n"
469	       "to FreeBSD (overwriting anything else that might be on the disk(s) selected)\n"
470	       "then use the (A)ll command to select the default partitioning scheme followed\n"
471	       "by a (Q)uit.  If you wish to allocate only free space to FreeBSD, move to a\n"
472	       "partition marked \"unused\" and use the (C)reate command.");
473
474    if (DITEM_STATUS(diskPartitionEditor(self)) == DITEM_FAILURE)
475	return DITEM_FAILURE;
476
477    dialog_clear_norefresh();
478    msgConfirm("Next, you need to create BSD partitions inside of the fdisk partition(s)\n"
479	       "just created.  If you have a reasonable amount of disk space (200MB or more)\n"
480	       "and don't have any special requirements, simply use the (A)uto command to\n"
481	       "allocate space automatically.  If you have more specific needs or just don't\n"
482	       "care for the layout chosen by (A)uto, press F1 for more information on\n"
483	       "manual layout.");
484
485    if (DITEM_STATUS(diskLabelEditor(self)) == DITEM_FAILURE)
486	return DITEM_FAILURE;
487
488    dialog_clear_norefresh();
489    if (DITEM_STATUS((i = installCommit(self))) == DITEM_FAILURE) {
490	dialog_clear_norefresh();
491	msgConfirm("Installation completed with some errors.  You may wish to\n"
492		   "scroll through the debugging messages on VTY1 with the\n"
493		   "scroll-lock feature.  You can also chose \"No\" at the next\n"
494		   "prompt and go back into the installation menus to try and retry\n"
495		   "whichever operations have failed.");
496	return i | DITEM_RESTORE;
497
498    }
499    else {
500	dialog_clear_norefresh();
501	msgConfirm("Congratulations!  You now have FreeBSD installed on your system.\n\n"
502		   "We will now move on to the final configuration questions.\n"
503		   "For any option you do not wish to configure, simply select\n"
504		   "No.\n\n"
505		   "If you wish to re-enter this utility after the system is up, you\n"
506		   "may do so by typing: /stand/sysinstall.");
507    }
508    if (mediaDevice->type != DEVICE_TYPE_FTP && mediaDevice->type != DEVICE_TYPE_NFS) {
509	if (!msgYesNo("Would you like to configure any Ethernet or SLIP/PPP network devices?")) {
510	    Device *tmp;
511
512	    dialog_clear_norefresh();
513	    tmp = tcpDeviceSelect();
514	    dialog_clear_norefresh();
515	    if (tmp && !msgYesNo("Would you like to bring the %s interface up right now?", tmp->name))
516		if (!tmp->init(tmp))
517		    msgConfirm("Initialization of %s device failed.", tmp->name);
518	}
519    }
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("Would you like to customize your system console settings?")) {
540	WINDOW *w = savescr();
541
542	dmenuOpenSimple(&MenuSyscons, FALSE);
543	restorescr(w);
544    }
545
546    dialog_clear_norefresh();
547    if (!msgYesNo("Would you like to set this machine's time zone now?")) {
548	WINDOW *w = savescr();
549
550	dialog_clear();
551	systemExecute("tzsetup");
552	restorescr(w);
553    }
554
555    dialog_clear_norefresh();
556    if (!msgYesNo("Does this system have a mouse attached to it?")) {
557	WINDOW *w = savescr();
558
559	dmenuOpenSimple(&MenuMouse, FALSE);
560	restorescr(w);
561    }
562
563    /* Now would be a good time to checkpoint the configuration data */
564    configRC_conf("/etc/rc.conf");
565    sync();
566
567#ifndef USE_XIG_ENVIRONMENT
568    if (directory_exists("/usr/X11R6")) {
569	dialog_clear_norefresh();
570	if (!msgYesNo("Would you like to configure your X server at this time?"))
571	    configXEnvironment(self);
572    }
573#endif
574
575    dialog_clear_norefresh();
576    if (!msgYesNo("The FreeBSD package collection is a collection of hundreds of ready-to-run\n"
577		  "applications, from text editors to games to WEB servers and more.  Would you\n"
578		  "like to browse the collection now?"))
579	configPackages(self);
580
581    dialog_clear_norefresh();
582    if (!msgYesNo("Would you like to add any initial user accounts to the system?\n"
583		  "Adding at least one account for yourself at this stage is suggested\n"
584		  "since working as the \"root\" user is dangerous (it is easy to do\n"
585		  "things which adversely affect the entire system)."))
586	configUsers(self);
587
588    dialog_clear_norefresh();
589    msgConfirm("Now you must set the system manager's password.\n"
590	       "This is the password you'll use to log in as \"root\".");
591    {
592	WINDOW *w = savescr();
593
594	if (!systemExecute("passwd root"))
595	    variable_set2("root_password", "YES");
596	restorescr(w);
597    }
598
599    dialog_clear_norefresh();
600    if (!msgYesNo("Would you like to register your FreeBSD system at this time?\n\n"
601		  "PLEASE, take just 5 minutes to do this.  If we're ever to get any\n"
602		  "significant base of commercial software for FreeBSD, we need to\n"
603		  "be able to provide more information about the size of our user community.\n"
604		  "This is where your registration can really help us, and you can also\n"
605		  "sign up for the new FreeBSD newsletter (its free!) at the same time.\n"))
606	configRegister(NULL);
607    else {
608	dialog_clear_norefresh();
609	msgConfirm("OK, but if you should change your mind then you always can register\n"
610		   "later by typing ``/stand/sysinstall register'' or by simply visiting our\n"
611		   "web site at http://www.freebsd.org/register.html");
612
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    dialog_clear_norefresh();
618    installConfigure();
619
620    return DITEM_LEAVE_MENU | DITEM_RESTORE;
621}
622
623/* The version of commit we call from the Install Custom menu */
624int
625installCustomCommit(dialogMenuItem *self)
626{
627    int i;
628
629    dialog_clear_norefresh();
630    i = installCommit(self);
631    if (DITEM_STATUS(i) == DITEM_SUCCESS) {
632	/* Give user the option of one last configuration spree */
633	installConfigure();
634	return i;
635    }
636    else
637	msgConfirm("The commit operation completed with errors.  Not\n"
638		   "updating /etc files.");
639    return i;
640}
641
642/*
643 * What happens when we finally decide to going ahead with the installation.
644 *
645 * This is broken into multiple stages so that the user can do a full
646 * installation but come back here again to load more distributions,
647 * perhaps from a different media type.  This would allow, for
648 * example, the user to load the majority of the system from CDROM and
649 * then use ftp to load just the DES dist.
650 */
651int
652installCommit(dialogMenuItem *self)
653{
654    int i;
655    char *str;
656    Boolean need_bin;
657
658    if (!Dists)
659	distConfig(NULL);
660
661    if (!Dists)
662	if (!dmenuOpenSimple(&MenuDistributions, FALSE) && !Dists)
663	    return DITEM_FAILURE | DITEM_RESTORE;
664
665    if (!mediaVerify())
666	return DITEM_FAILURE | DITEM_RESTORE;
667
668    str = variable_get(SYSTEM_STATE);
669    if (isDebug())
670	msgDebug("installCommit: System state is `%s'\n", str);
671
672    if (RunningAsInit) {
673	/* Do things we wouldn't do to a multi-user system */
674	if (DITEM_STATUS((i = installInitial())) == DITEM_FAILURE)
675	    return i;
676	if (DITEM_STATUS((i = configFstab())) == DITEM_FAILURE)
677	    return i;
678    }
679
680try_media:
681    if (!mediaDevice->init(mediaDevice)) {
682	if (!msgYesNo("Unable to initialize selected media. Would you like to\n"
683		      "adjust your media configuration and try again?")) {
684	    mediaDevice = NULL;
685	    if (!mediaVerify())
686		return DITEM_FAILURE | DITEM_RESTORE;
687	    else
688		goto try_media;
689	}
690	else
691	    return DITEM_FAILURE | DITEM_RESTORE;
692    }
693
694    need_bin = Dists & DIST_BIN;
695    i = distExtractAll(self);
696    if (DITEM_STATUS(i) == DITEM_SUCCESS) {
697	if (need_bin && !(Dists & DIST_BIN))
698	    i = installFixup(self);
699    }
700    variable_set2(SYSTEM_STATE, DITEM_STATUS(i) == DITEM_FAILURE ? "error-install" : "full-install");
701
702    /* We always try to install X with the XiG product */
703#ifdef USE_XIG_ENVIRONMENT
704    if (directory_exists("/usr/X11R6"))
705	configXEnvironment(self);
706#endif
707    return i | DITEM_RESTORE;
708}
709
710static void
711installConfigure(void)
712{
713    /* Final menu of last resort */
714    dialog_clear_norefresh();
715    if (!msgYesNo("Visit the general configuration menu for a chance to set\n"
716		  "any last options?")) {
717	WINDOW *w = savescr();
718
719	dmenuOpenSimple(&MenuConfigure, FALSE);
720	restorescr(w);
721    }
722    configRC_conf("/etc/rc.conf");
723    sync();
724}
725
726int
727installFixup(dialogMenuItem *self)
728{
729    Device **devs;
730    int i;
731
732    if (!file_readable("/kernel")) {
733	if (file_readable("/kernel.GENERIC")) {
734#ifdef SAVE_USERCONFIG
735	    /* Snapshot any boot -c changes back to the GENERIC kernel */
736	    if (!strcmp(variable_get(VAR_RELNAME), RELEASE_NAME))
737		save_userconfig_to_kernel("/kernel.GENERIC");
738#endif
739	    if (vsystem("cp -p /kernel.GENERIC /kernel")) {
740		msgConfirm("Unable to link /kernel into place!");
741		return DITEM_FAILURE;
742	    }
743	}
744	else {
745	    msgConfirm("Can't find a kernel image to link to on the root file system!\n"
746		       "You're going to have a hard time getting this system to\n"
747		       "boot from the hard disk, I'm afraid!");
748	    return DITEM_FAILURE;
749	}
750    }
751
752    /* Resurrect /dev after bin distribution screws it up */
753    if (RunningAsInit) {
754	msgNotify("Remaking all devices.. Please wait!");
755	if (vsystem("cd /dev; sh MAKEDEV all")) {
756	    msgConfirm("MAKEDEV returned non-zero status");
757	    return DITEM_FAILURE;
758	}
759
760	msgNotify("Resurrecting /dev entries for slices..");
761	devs = deviceFind(NULL, DEVICE_TYPE_DISK);
762	if (!devs)
763	    msgFatal("Couldn't get a disk device list!");
764
765	/* Resurrect the slices that the former clobbered */
766	for (i = 0; devs[i]; i++) {
767	    Disk *disk = (Disk *)devs[i]->private;
768	    Chunk *c1;
769
770	    if (!devs[i]->enabled)
771		continue;
772	    if (!disk->chunks)
773		msgFatal("No chunk list found for %s!", disk->name);
774	    for (c1 = disk->chunks->part; c1; c1 = c1->next) {
775		if (c1->type == freebsd) {
776		    msgNotify("Making slice entries for %s", c1->name);
777		    if (vsystem("cd /dev; sh MAKEDEV %sh", c1->name)) {
778			msgConfirm("Unable to make slice entries for %s!", c1->name);
779			return DITEM_FAILURE;
780		    }
781		}
782	    }
783	}
784	/* XXX Do all the last ugly work-arounds here which we'll try and excise someday right?? XXX */
785
786	msgNotify("Fixing permissions..");
787	/* BOGON #1:  XFree86 extracting /usr/X11R6 with root-only perms */
788	if (directory_exists("/usr/X11R6")) {
789	    vsystem("chmod -R a+r /usr/X11R6");
790	    vsystem("find /usr/X11R6 -type d | xargs chmod a+x");
791	}
792	/* BOGON #2: We leave /etc in a bad state */
793	chmod("/etc", 0755);
794
795	/* BOGON #3: No /var/db/mountdtab complains */
796	Mkdir("/var/db");
797	creat("/var/db/mountdtab", 0644);
798
799	/* BOGON #4: /compat created by default in root fs */
800	Mkdir("/usr/compat");
801	vsystem("ln -s /usr/compat /compat");
802
803	/* BOGON #5: aliases database not build for bin */
804	vsystem("newaliases");
805
806	/* BOGON #6: deal with new boot files */
807	vsystem("touch /kernel.config");
808	vsystem("touch /boot.config");
809
810	/* Now run all the mtree stuff to fix things up */
811        vsystem("mtree -deU -f /etc/mtree/BSD.root.dist -p /");
812        vsystem("mtree -deU -f /etc/mtree/BSD.var.dist -p /var");
813        vsystem("mtree -deU -f /etc/mtree/BSD.usr.dist -p /usr");
814    }
815    return DITEM_SUCCESS;
816}
817
818/* Go newfs and/or mount all the filesystems we've been asked to */
819int
820installFilesystems(dialogMenuItem *self)
821{
822    int i;
823    Disk *disk;
824    Chunk *c1, *c2, *rootdev, *swapdev, *usrdev, *vardev;
825    Device **devs;
826    PartInfo *root;
827    char dname[80], *str;
828    extern int MakeDevChunk(Chunk *c, char *n);
829    Boolean upgrade = FALSE;
830
831    /* If we've already done this, bail out */
832    if ((str = variable_get(DISK_LABELLED)) && !strcmp(str, "written"))
833	return DITEM_SUCCESS;
834
835    str = variable_get(SYSTEM_STATE);
836
837    if (!checkLabels(TRUE, &rootdev, &swapdev, &usrdev, &vardev))
838	return DITEM_FAILURE;
839
840    if (rootdev)
841	root = (PartInfo *)rootdev->private_data;
842    else
843	root = NULL;
844
845    command_clear();
846    upgrade = str && !strcmp(str, "upgrade");
847
848    if (swapdev && RunningAsInit) {
849	/* As the very first thing, try to get ourselves some swap space */
850	sprintf(dname, "/dev/%s", swapdev->name);
851	if (!Fake && (!MakeDevChunk(swapdev, "/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
857	if (!Fake) {
858	    if (!swapon(dname))
859		msgNotify("Added %s as initial swap device", dname);
860	    else
861		msgConfirm("WARNING!  Unable to swap to %s: %s\n"
862			   "This may cause the installation to fail at some point\n"
863			   "if you don't have a lot of memory.", dname, strerror(errno));
864	}
865    }
866
867    if (rootdev && RunningAsInit) {
868	/* Next, create and/or mount the root device */
869	sprintf(dname, "/dev/r%sa", rootdev->disk->name);
870	if (!Fake && (!MakeDevChunk(rootdev, "/dev") || !file_readable(dname))) {
871	    msgConfirm("Unable to make device node for %s in /dev!\n"
872		       "The creation of filesystems will be aborted.", dname);
873	    return DITEM_FAILURE;
874	}
875	if (strcmp(root->mountpoint, "/"))
876	    msgConfirm("Warning: %s is marked as a root partition but is mounted on %s", rootdev->name, root->mountpoint);
877
878	if (root->newfs) {
879	    int i;
880
881	    msgNotify("Making a new root filesystem on %s", dname);
882	    i = vsystem("%s %s", root->newfs_cmd, dname);
883	    if (i) {
884		msgConfirm("Unable to make new root filesystem on %s!\n"
885			   "Command returned status %d", dname, i);
886		return DITEM_FAILURE;
887	    }
888	}
889	else {
890	    if (!upgrade) {
891		msgConfirm("Warning:  Using existing root partition.  It will be assumed\n"
892			   "that you have the appropriate device entries already in /dev.");
893	    }
894	    msgNotify("Checking integrity of existing %s filesystem.", dname);
895	    i = vsystem("fsck -y %s", dname);
896	    if (i)
897		msgConfirm("Warning: fsck returned status of %d for %s.\n"
898			   "This partition may be unsafe to use.", i, dname);
899	}
900
901	/* Switch to block device */
902	sprintf(dname, "/dev/%sa", rootdev->disk->name);
903	if (Mount("/mnt", dname)) {
904	    msgConfirm("Unable to mount the root file system on %s!  Giving up.", dname);
905	    return DITEM_FAILURE;
906	}
907    }
908
909    /* Now buzz through the rest of the partitions and mount them too */
910    devs = deviceFind(NULL, DEVICE_TYPE_DISK);
911    for (i = 0; devs[i]; i++) {
912	if (!devs[i]->enabled)
913	    continue;
914
915	disk = (Disk *)devs[i]->private;
916	if (!disk->chunks) {
917	    msgConfirm("No chunk list found for %s!", disk->name);
918	    return DITEM_FAILURE;
919	}
920	if (RunningAsInit && root && (root->newfs || upgrade)) {
921	    Mkdir("/mnt/dev");
922	    if (!Fake)
923		MakeDevDisk(disk, "/mnt/dev");
924	}
925	else if (!RunningAsInit && !Fake)
926	    MakeDevDisk(disk, "/dev");
927
928	for (c1 = disk->chunks->part; c1; c1 = c1->next) {
929	    if (c1->type == freebsd) {
930		for (c2 = c1->part; c2; c2 = c2->next) {
931		    if (c2->type == part && c2->subtype != FS_SWAP && c2->private_data) {
932			PartInfo *tmp = (PartInfo *)c2->private_data;
933
934			/* Already did root */
935			if (c2 == rootdev)
936			    continue;
937
938			if (tmp->newfs)
939			    command_shell_add(tmp->mountpoint, "%s %s/dev/r%s", tmp->newfs_cmd, RunningAsInit ? "/mnt" : "", c2->name);
940			else
941			    command_shell_add(tmp->mountpoint, "fsck -y %s/dev/r%s", RunningAsInit ? "/mnt" : "", c2->name);
942			command_func_add(tmp->mountpoint, Mount, c2->name);
943		    }
944		    else if (c2->type == part && c2->subtype == FS_SWAP) {
945			char fname[80];
946			int i;
947
948			if (c2 == swapdev)
949			    continue;
950			sprintf(fname, "%s/dev/%s", RunningAsInit ? "/mnt" : "", c2->name);
951			i = (Fake || swapon(fname));
952			if (!i)
953			    msgNotify("Added %s as an additional swap device", fname);
954			else
955			    msgConfirm("Unable to add %s as a swap device: %s", fname, strerror(errno));
956		    }
957		}
958	    }
959	    else if (c1->type == fat && c1->private_data && (root->newfs || upgrade)) {
960		char name[FILENAME_MAX];
961
962		sprintf(name, "%s/%s", RunningAsInit ? "/mnt" : "", ((PartInfo *)c1->private_data)->mountpoint);
963		Mkdir(name);
964	    }
965	}
966    }
967
968    if (RunningAsInit) {
969	msgNotify("Copying initial device files..");
970	/* Copy the boot floppy's dev files */
971	if ((root->newfs || upgrade) && vsystem("find -x /dev | cpio %s -pdum /mnt", cpioVerbosity())) {
972	    msgConfirm("Couldn't clone the /dev files!");
973	    return DITEM_FAILURE;
974	}
975    }
976
977    command_sort();
978    command_execute();
979    return DITEM_SUCCESS;
980}
981
982/* Initialize various user-settable values to their defaults */
983int
984installVarDefaults(dialogMenuItem *self)
985{
986    char *cp;
987
988    /* Set default startup options */
989    variable_set2(VAR_ROUTER_ENABLE,		"NO");
990    variable_set2(VAR_RELNAME,			RELEASE_NAME);
991    variable_set2(VAR_CPIO_VERBOSITY,		"high");
992    variable_set2(VAR_TAPE_BLOCKSIZE,		DEFAULT_TAPE_BLOCKSIZE);
993    variable_set2(VAR_INSTALL_ROOT,		"/");
994    variable_set2(VAR_INSTALL_CFG,		"install.cfg");
995    cp = getenv("EDITOR");
996    if (!cp)
997	cp = "/usr/bin/ee";
998    variable_set2(VAR_EDITOR,			cp);
999    variable_set2(VAR_FTP_USER,			"ftp");
1000    variable_set2(VAR_BROWSER_PACKAGE,		PACKAGE_LYNX);
1001    variable_set2(VAR_BROWSER_BINARY,		"/usr/local/bin/lynx");
1002    variable_set2(VAR_FTP_STATE,		"passive");
1003    variable_set2(VAR_NFS_SECURE,		"YES");
1004    variable_set2(VAR_PKG_TMPDIR,		"/usr/tmp");
1005    variable_set2(VAR_GATED_PKG,		PACKAGE_GATED);
1006    variable_set2(VAR_PCNFSD_PKG,		PACKAGE_PCNFSD);
1007    variable_set2(VAR_MEDIA_TIMEOUT,		itoa(MEDIA_TIMEOUT));
1008    if (getpid() != 1)
1009	variable_set2(SYSTEM_STATE,		"update");
1010    else
1011	variable_set2(SYSTEM_STATE,		"init");
1012    return DITEM_SUCCESS;
1013}
1014
1015/* Load the environment up from various system configuration files */
1016void
1017installEnvironment(void)
1018{
1019    if (file_readable("/etc/rc.conf"))
1020	configEnvironmentRC_conf("/etc/rc.conf");
1021    if (file_readable("/etc/resolv.conf"))
1022	configEnvironmentResolv("/etc/resolv.conf");
1023}
1024
1025/* Copy the boot floppy contents into /stand */
1026Boolean
1027copySelf(void)
1028{
1029    int i;
1030
1031    if (file_readable("/boot.help"))
1032	vsystem("cp /boot.help /mnt");
1033    msgWeHaveOutput("Copying the boot floppy to /stand on root filesystem");
1034    i = vsystem("find -x /stand | cpio %s -pdum /mnt", cpioVerbosity());
1035    if (i) {
1036	msgConfirm("Copy returned error status of %d!", i);
1037	return FALSE;
1038    }
1039
1040    /* Copy the /etc files into their rightful place */
1041    if (vsystem("cd /mnt/stand; find etc | cpio %s -pdum /mnt", cpioVerbosity())) {
1042	msgConfirm("Couldn't copy up the /etc files!");
1043	return TRUE;
1044    }
1045    return TRUE;
1046}
1047
1048static void
1049create_termcap(void)
1050{
1051    FILE *fp;
1052
1053    const char *caps[] = {
1054	termcap_vt100, termcap_cons25, termcap_cons25_m, termcap_cons25r,
1055	termcap_cons25r_m, termcap_cons25l1, termcap_cons25l1_m, NULL,
1056    };
1057    const char **cp;
1058
1059    if (!file_readable(TERMCAP_FILE)) {
1060	Mkdir("/usr/share/misc");
1061	fp = fopen(TERMCAP_FILE, "w");
1062	if (!fp) {
1063	    msgConfirm("Unable to initialize termcap file. Some screen-oriented\nutilities may not work.");
1064	    return;
1065	}
1066	cp = caps;
1067	while (*cp)
1068	    fprintf(fp, "%s\n", *(cp++));
1069	fclose(fp);
1070    }
1071}
1072
1073#ifdef SAVE_USERCONFIG
1074static void
1075save_userconfig_to_kernel(char *kern)
1076{
1077    struct kernel *core, *boot;
1078    struct list *c_isa, *b_isa, *c_dev, *b_dev;
1079    int i, d;
1080
1081    if ((core = uc_open("-incore")) == NULL) {
1082	msgDebug("save_userconf: Can't read in-core information for kernel.\n");
1083	return;
1084    }
1085
1086    if ((boot = uc_open(kern)) == NULL) {
1087	msgDebug("save_userconf: Can't read device information for kernel image %s\n", kern);
1088	return;
1089    }
1090
1091    msgNotify("Saving any boot -c changes to new kernel...");
1092    c_isa = uc_getdev(core, "-isa");
1093    b_isa = uc_getdev(boot, "-isa");
1094    if (isDebug())
1095	msgDebug("save_userconf: got %d ISA device entries from core, %d from boot.\n", c_isa->ac, b_isa->ac);
1096    for (d = 0; d < c_isa->ac; d++) {
1097	if (isDebug())
1098	    msgDebug("save_userconf: ISA device loop, c_isa->av[%d] = %s\n", d, c_isa->av[d]);
1099	if (strcmp(c_isa->av[d], "npx0")) { /* special case npx0, which mucks with its id_irq member */
1100	    c_dev = uc_getdev(core, c_isa->av[d]);
1101	    b_dev = uc_getdev(boot, b_isa->av[d]);
1102	    if (!c_dev || !b_dev) {
1103		msgDebug("save_userconf: c_dev: %x b_dev: %x\n", c_dev, b_dev);
1104		continue;
1105	    }
1106	    if (isDebug())
1107		msgDebug("save_userconf: ISA device %s: %d config parameters (core), %d (boot)\n",
1108			 c_isa->av[d], c_dev->ac, b_dev->ac);
1109	    for (i = 0; i < c_dev->ac; i++) {
1110		if (isDebug())
1111		    msgDebug("save_userconf: c_dev->av[%d] = %s, b_dev->av[%d] = %s\n", i, c_dev->av[i], i, b_dev->av[i]);
1112		if (strcmp(c_dev->av[i], b_dev->av[i])) {
1113		    if (isDebug())
1114			msgDebug("save_userconf: %s (boot) -> %s (core)\n",
1115				 c_dev->av[i], b_dev->av[i]);
1116		    isa_setdev(boot, c_dev);
1117		}
1118	    }
1119	}
1120	else {
1121	    if (isDebug())
1122		msgDebug("skipping npx0\n");
1123	}
1124    }
1125    if (isDebug())
1126	msgDebug("Closing kernels\n");
1127    uc_close(core, 0);
1128    uc_close(boot, 1);
1129}
1130#endif
1131