install.c revision 27451
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.192 1997/07/16 05:22:40 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	msgConfirm("OK, but if you should change your mind then you always can register\n"
609		   "later by typing ``/stand/sysinstall register'' or by simply visiting our\n"
610		   "web site at http://www.freebsd.org/register.html");
611
612    /* XXX Put whatever other nice configuration questions you'd like to ask the user here XXX */
613
614    /* Give user the option of one last configuration spree */
615    installConfigure();
616
617    return DITEM_LEAVE_MENU | DITEM_RESTORE;
618}
619
620/* The version of commit we call from the Install Custom menu */
621int
622installCustomCommit(dialogMenuItem *self)
623{
624    int i;
625
626    dialog_clear_norefresh();
627    i = installCommit(self);
628    if (DITEM_STATUS(i) == DITEM_SUCCESS) {
629	/* Give user the option of one last configuration spree */
630	installConfigure();
631	return i;
632    }
633    else
634	msgConfirm("The commit operation completed with errors.  Not\n"
635		   "updating /etc files.");
636    return i;
637}
638
639/*
640 * What happens when we finally decide to going ahead with the installation.
641 *
642 * This is broken into multiple stages so that the user can do a full
643 * installation but come back here again to load more distributions,
644 * perhaps from a different media type.  This would allow, for
645 * example, the user to load the majority of the system from CDROM and
646 * then use ftp to load just the DES dist.
647 */
648int
649installCommit(dialogMenuItem *self)
650{
651    int i;
652    char *str;
653    Boolean need_bin;
654
655    if (!Dists)
656	distConfig(NULL);
657
658    if (!Dists)
659	if (!dmenuOpenSimple(&MenuDistributions, FALSE) && !Dists)
660	    return DITEM_FAILURE | DITEM_RESTORE;
661
662    if (!mediaVerify())
663	return DITEM_FAILURE | DITEM_RESTORE;
664
665    str = variable_get(SYSTEM_STATE);
666    if (isDebug())
667	msgDebug("installCommit: System state is `%s'\n", str);
668
669    if (RunningAsInit) {
670	/* Do things we wouldn't do to a multi-user system */
671	if (DITEM_STATUS((i = installInitial())) == DITEM_FAILURE)
672	    return i;
673	if (DITEM_STATUS((i = configFstab())) == DITEM_FAILURE)
674	    return i;
675    }
676
677try_media:
678    if (!mediaDevice->init(mediaDevice)) {
679	if (!msgYesNo("Unable to initialize selected media. Would you like to\n"
680		      "adjust your media configuration and try again?")) {
681	    mediaDevice = NULL;
682	    if (!mediaVerify())
683		return DITEM_FAILURE | DITEM_RESTORE;
684	    else
685		goto try_media;
686	}
687	else
688	    return DITEM_FAILURE | DITEM_RESTORE;
689    }
690
691    need_bin = Dists & DIST_BIN;
692    i = distExtractAll(self);
693    if (DITEM_STATUS(i) == DITEM_SUCCESS) {
694	if (need_bin && !(Dists & DIST_BIN))
695	    i = installFixup(self);
696    }
697    variable_set2(SYSTEM_STATE, DITEM_STATUS(i) == DITEM_FAILURE ? "error-install" : "full-install");
698
699    /* We always try to install X with the XiG product */
700#ifdef USE_XIG_ENVIRONMENT
701    if (directory_exists("/usr/X11R6"))
702	configXEnvironment(self);
703#endif
704    return i | DITEM_RESTORE;
705}
706
707static void
708installConfigure(void)
709{
710    /* Final menu of last resort */
711    dialog_clear_norefresh();
712    if (!msgYesNo("Visit the general configuration menu for a chance to set\n"
713		  "any last options?")) {
714	WINDOW *w = savescr();
715
716	dmenuOpenSimple(&MenuConfigure, FALSE);
717	restorescr(w);
718    }
719    configRC_conf("/etc/rc.conf");
720    sync();
721}
722
723int
724installFixup(dialogMenuItem *self)
725{
726    Device **devs;
727    int i;
728
729    if (!file_readable("/kernel")) {
730	if (file_readable("/kernel.GENERIC")) {
731#ifdef SAVE_USERCONFIG
732	    /* Snapshot any boot -c changes back to the GENERIC kernel */
733	    if (!strcmp(variable_get(VAR_RELNAME), RELEASE_NAME))
734		save_userconfig_to_kernel("/kernel.GENERIC");
735#endif
736	    if (vsystem("cp -p /kernel.GENERIC /kernel")) {
737		msgConfirm("Unable to link /kernel into place!");
738		return DITEM_FAILURE;
739	    }
740	}
741	else {
742	    msgConfirm("Can't find a kernel image to link to on the root file system!\n"
743		       "You're going to have a hard time getting this system to\n"
744		       "boot from the hard disk, I'm afraid!");
745	    return DITEM_FAILURE;
746	}
747    }
748
749    /* Resurrect /dev after bin distribution screws it up */
750    if (RunningAsInit) {
751	msgNotify("Remaking all devices.. Please wait!");
752	if (vsystem("cd /dev; sh MAKEDEV all")) {
753	    msgConfirm("MAKEDEV returned non-zero status");
754	    return DITEM_FAILURE;
755	}
756
757	msgNotify("Resurrecting /dev entries for slices..");
758	devs = deviceFind(NULL, DEVICE_TYPE_DISK);
759	if (!devs)
760	    msgFatal("Couldn't get a disk device list!");
761
762	/* Resurrect the slices that the former clobbered */
763	for (i = 0; devs[i]; i++) {
764	    Disk *disk = (Disk *)devs[i]->private;
765	    Chunk *c1;
766
767	    if (!devs[i]->enabled)
768		continue;
769	    if (!disk->chunks)
770		msgFatal("No chunk list found for %s!", disk->name);
771	    for (c1 = disk->chunks->part; c1; c1 = c1->next) {
772		if (c1->type == freebsd) {
773		    msgNotify("Making slice entries for %s", c1->name);
774		    if (vsystem("cd /dev; sh MAKEDEV %sh", c1->name)) {
775			msgConfirm("Unable to make slice entries for %s!", c1->name);
776			return DITEM_FAILURE;
777		    }
778		}
779	    }
780	}
781	/* XXX Do all the last ugly work-arounds here which we'll try and excise someday right?? XXX */
782
783	msgNotify("Fixing permissions..");
784	/* BOGON #1:  XFree86 extracting /usr/X11R6 with root-only perms */
785	if (directory_exists("/usr/X11R6")) {
786	    vsystem("chmod -R a+r /usr/X11R6");
787	    vsystem("find /usr/X11R6 -type d | xargs chmod a+x");
788	}
789	/* BOGON #2: We leave /etc in a bad state */
790	chmod("/etc", 0755);
791
792	/* BOGON #3: No /var/db/mountdtab complains */
793	Mkdir("/var/db");
794	creat("/var/db/mountdtab", 0644);
795
796	/* BOGON #4: /compat created by default in root fs */
797	Mkdir(/usr/compat");
798	vsystem("ln -s /usr/compat /compat");
799
800	/* Now run all the mtree stuff to fix things up */
801        vsystem("mtree -deU -f /etc/mtree/BSD.root.dist -p /");
802        vsystem("mtree -deU -f /etc/mtree/BSD.var.dist -p /var");
803        vsystem("mtree -deU -f /etc/mtree/BSD.usr.dist -p /usr");
804    }
805    return DITEM_SUCCESS;
806}
807
808/* Go newfs and/or mount all the filesystems we've been asked to */
809int
810installFilesystems(dialogMenuItem *self)
811{
812    int i;
813    Disk *disk;
814    Chunk *c1, *c2, *rootdev, *swapdev, *usrdev, *vardev;
815    Device **devs;
816    PartInfo *root;
817    char dname[80], *str;
818    extern int MakeDevChunk(Chunk *c, char *n);
819    Boolean upgrade = FALSE;
820
821    /* If we've already done this, bail out */
822    if ((str = variable_get(DISK_LABELLED)) && !strcmp(str, "written"))
823	return DITEM_SUCCESS;
824
825    str = variable_get(SYSTEM_STATE);
826
827    if (!checkLabels(TRUE, &rootdev, &swapdev, &usrdev, &vardev))
828	return DITEM_FAILURE;
829
830    if (rootdev)
831	root = (PartInfo *)rootdev->private_data;
832    else
833	root = NULL;
834
835    command_clear();
836    upgrade = str && !strcmp(str, "upgrade");
837
838    if (swapdev && RunningAsInit) {
839	/* As the very first thing, try to get ourselves some swap space */
840	sprintf(dname, "/dev/%s", swapdev->name);
841	if (!Fake && (!MakeDevChunk(swapdev, "/dev") || !file_readable(dname))) {
842	    msgConfirm("Unable to make device node for %s in /dev!\n"
843		       "The creation of filesystems will be aborted.", dname);
844	    return DITEM_FAILURE;
845	}
846
847	if (!Fake) {
848	    if (!swapon(dname))
849		msgNotify("Added %s as initial swap device", dname);
850	    else
851		msgConfirm("WARNING!  Unable to swap to %s: %s\n"
852			   "This may cause the installation to fail at some point\n"
853			   "if you don't have a lot of memory.", dname, strerror(errno));
854	}
855    }
856
857    if (rootdev && RunningAsInit) {
858	/* Next, create and/or mount the root device */
859	sprintf(dname, "/dev/r%sa", rootdev->disk->name);
860	if (!Fake && (!MakeDevChunk(rootdev, "/dev") || !file_readable(dname))) {
861	    msgConfirm("Unable to make device node for %s in /dev!\n"
862		       "The creation of filesystems will be aborted.", dname);
863	    return DITEM_FAILURE;
864	}
865	if (strcmp(root->mountpoint, "/"))
866	    msgConfirm("Warning: %s is marked as a root partition but is mounted on %s", rootdev->name, root->mountpoint);
867
868	if (root->newfs) {
869	    int i;
870
871	    msgNotify("Making a new root filesystem on %s", dname);
872	    i = vsystem("%s %s", root->newfs_cmd, dname);
873	    if (i) {
874		msgConfirm("Unable to make new root filesystem on %s!\n"
875			   "Command returned status %d", dname, i);
876		return DITEM_FAILURE;
877	    }
878	}
879	else {
880	    if (!upgrade) {
881		msgConfirm("Warning:  Using existing root partition.  It will be assumed\n"
882			   "that you have the appropriate device entries already in /dev.");
883	    }
884	    msgNotify("Checking integrity of existing %s filesystem.", dname);
885	    i = vsystem("fsck -y %s", dname);
886	    if (i)
887		msgConfirm("Warning: fsck returned status of %d for %s.\n"
888			   "This partition may be unsafe to use.", i, dname);
889	}
890
891	/* Switch to block device */
892	sprintf(dname, "/dev/%sa", rootdev->disk->name);
893	if (Mount("/mnt", dname)) {
894	    msgConfirm("Unable to mount the root file system on %s!  Giving up.", dname);
895	    return DITEM_FAILURE;
896	}
897    }
898
899    /* Now buzz through the rest of the partitions and mount them too */
900    devs = deviceFind(NULL, DEVICE_TYPE_DISK);
901    for (i = 0; devs[i]; i++) {
902	if (!devs[i]->enabled)
903	    continue;
904
905	disk = (Disk *)devs[i]->private;
906	if (!disk->chunks) {
907	    msgConfirm("No chunk list found for %s!", disk->name);
908	    return DITEM_FAILURE;
909	}
910	if (RunningAsInit && root && (root->newfs || upgrade)) {
911	    Mkdir("/mnt/dev");
912	    if (!Fake)
913		MakeDevDisk(disk, "/mnt/dev");
914	}
915	else if (!RunningAsInit && !Fake)
916	    MakeDevDisk(disk, "/dev");
917
918	for (c1 = disk->chunks->part; c1; c1 = c1->next) {
919	    if (c1->type == freebsd) {
920		for (c2 = c1->part; c2; c2 = c2->next) {
921		    if (c2->type == part && c2->subtype != FS_SWAP && c2->private_data) {
922			PartInfo *tmp = (PartInfo *)c2->private_data;
923
924			/* Already did root */
925			if (c2 == rootdev)
926			    continue;
927
928			if (tmp->newfs)
929			    command_shell_add(tmp->mountpoint, "%s %s/dev/r%s", tmp->newfs_cmd, RunningAsInit ? "/mnt" : "", c2->name);
930			else
931			    command_shell_add(tmp->mountpoint, "fsck -y %s/dev/r%s", RunningAsInit ? "/mnt" : "", c2->name);
932			command_func_add(tmp->mountpoint, Mount, c2->name);
933		    }
934		    else if (c2->type == part && c2->subtype == FS_SWAP) {
935			char fname[80];
936			int i;
937
938			if (c2 == swapdev)
939			    continue;
940			sprintf(fname, "%s/dev/%s", RunningAsInit ? "/mnt" : "", c2->name);
941			i = (Fake || swapon(fname));
942			if (!i)
943			    msgNotify("Added %s as an additional swap device", fname);
944			else
945			    msgConfirm("Unable to add %s as a swap device: %s", fname, strerror(errno));
946		    }
947		}
948	    }
949	    else if (c1->type == fat && c1->private_data && (root->newfs || upgrade)) {
950		char name[FILENAME_MAX];
951
952		sprintf(name, "%s/%s", RunningAsInit ? "/mnt" : "", ((PartInfo *)c1->private_data)->mountpoint);
953		Mkdir(name);
954	    }
955	}
956    }
957
958    if (RunningAsInit) {
959	msgNotify("Copying initial device files..");
960	/* Copy the boot floppy's dev files */
961	if ((root->newfs || upgrade) && vsystem("find -x /dev | cpio %s -pdum /mnt", cpioVerbosity())) {
962	    msgConfirm("Couldn't clone the /dev files!");
963	    return DITEM_FAILURE;
964	}
965    }
966
967    command_sort();
968    command_execute();
969    return DITEM_SUCCESS;
970}
971
972/* Initialize various user-settable values to their defaults */
973int
974installVarDefaults(dialogMenuItem *self)
975{
976    char *cp;
977
978    /* Set default startup options */
979    variable_set2(VAR_ROUTER_ENABLE,		"NO");
980    variable_set2(VAR_RELNAME,			RELEASE_NAME);
981    variable_set2(VAR_CPIO_VERBOSITY,		"high");
982    variable_set2(VAR_TAPE_BLOCKSIZE,		DEFAULT_TAPE_BLOCKSIZE);
983    variable_set2(VAR_INSTALL_ROOT,		"/");
984    variable_set2(VAR_INSTALL_CFG,		"install.cfg");
985    cp = getenv("EDITOR");
986    if (!cp)
987	cp = "/usr/bin/ee";
988    variable_set2(VAR_EDITOR,			cp);
989    variable_set2(VAR_FTP_USER,			"ftp");
990    variable_set2(VAR_BROWSER_PACKAGE,		PACKAGE_LYNX);
991    variable_set2(VAR_BROWSER_BINARY,		"/usr/local/bin/lynx");
992    variable_set2(VAR_FTP_STATE,		"passive");
993    variable_set2(VAR_NFS_SECURE,		"YES");
994    variable_set2(VAR_PKG_TMPDIR,		"/usr/tmp");
995    variable_set2(VAR_GATED_PKG,		PACKAGE_GATED);
996    variable_set2(VAR_PCNFSD_PKG,		PACKAGE_PCNFSD);
997    variable_set2(VAR_MEDIA_TIMEOUT,		itoa(MEDIA_TIMEOUT));
998    if (getpid() != 1)
999	variable_set2(SYSTEM_STATE,		"update");
1000    else
1001	variable_set2(SYSTEM_STATE,		"init");
1002    return DITEM_SUCCESS;
1003}
1004
1005/* Load the environment up from various system configuration files */
1006void
1007installEnvironment(void)
1008{
1009    if (file_readable("/etc/rc.conf"))
1010	configEnvironmentRC_conf("/etc/rc.conf");
1011    if (file_readable("/etc/resolv.conf"))
1012	configEnvironmentResolv("/etc/resolv.conf");
1013}
1014
1015/* Copy the boot floppy contents into /stand */
1016Boolean
1017copySelf(void)
1018{
1019    int i;
1020
1021    msgWeHaveOutput("Copying the boot floppy to /stand on root filesystem");
1022    i = vsystem("find -x /stand | cpio %s -pdum /mnt", cpioVerbosity());
1023    if (i) {
1024	msgConfirm("Copy returned error status of %d!", i);
1025	return FALSE;
1026    }
1027
1028    /* Copy the /etc files into their rightful place */
1029    if (vsystem("cd /mnt/stand; find etc | cpio %s -pdum /mnt", cpioVerbosity())) {
1030	msgConfirm("Couldn't copy up the /etc files!");
1031	return TRUE;
1032    }
1033    return TRUE;
1034}
1035
1036static void
1037create_termcap(void)
1038{
1039    FILE *fp;
1040
1041    const char *caps[] = {
1042	termcap_vt100, termcap_cons25, termcap_cons25_m, termcap_cons25r,
1043	termcap_cons25r_m, termcap_cons25l1, termcap_cons25l1_m, NULL,
1044    };
1045    const char **cp;
1046
1047    if (!file_readable(TERMCAP_FILE)) {
1048	Mkdir("/usr/share/misc");
1049	fp = fopen(TERMCAP_FILE, "w");
1050	if (!fp) {
1051	    msgConfirm("Unable to initialize termcap file. Some screen-oriented\nutilities may not work.");
1052	    return;
1053	}
1054	cp = caps;
1055	while (*cp)
1056	    fprintf(fp, "%s\n", *(cp++));
1057	fclose(fp);
1058    }
1059}
1060
1061#ifdef SAVE_USERCONFIG
1062static void
1063save_userconfig_to_kernel(char *kern)
1064{
1065    struct kernel *core, *boot;
1066    struct list *c_isa, *b_isa, *c_dev, *b_dev;
1067    int i, d;
1068
1069    if ((core = uc_open("-incore")) == NULL) {
1070	msgDebug("save_userconf: Can't read in-core information for kernel.\n");
1071	return;
1072    }
1073
1074    if ((boot = uc_open(kern)) == NULL) {
1075	msgDebug("save_userconf: Can't read device information for kernel image %s\n", kern);
1076	return;
1077    }
1078
1079    msgNotify("Saving any boot -c changes to new kernel...");
1080    c_isa = uc_getdev(core, "-isa");
1081    b_isa = uc_getdev(boot, "-isa");
1082    if (isDebug())
1083	msgDebug("save_userconf: got %d ISA device entries from core, %d from boot.\n", c_isa->ac, b_isa->ac);
1084    for (d = 0; d < c_isa->ac; d++) {
1085	if (isDebug())
1086	    msgDebug("save_userconf: ISA device loop, c_isa->av[%d] = %s\n", d, c_isa->av[d]);
1087	if (strcmp(c_isa->av[d], "npx0")) { /* special case npx0, which mucks with its id_irq member */
1088	    c_dev = uc_getdev(core, c_isa->av[d]);
1089	    b_dev = uc_getdev(boot, b_isa->av[d]);
1090	    if (!c_dev || !b_dev) {
1091		msgDebug("save_userconf: c_dev: %x b_dev: %x\n", c_dev, b_dev);
1092		continue;
1093	    }
1094	    if (isDebug())
1095		msgDebug("save_userconf: ISA device %s: %d config parameters (core), %d (boot)\n",
1096			 c_isa->av[d], c_dev->ac, b_dev->ac);
1097	    for (i = 0; i < c_dev->ac; i++) {
1098		if (isDebug())
1099		    msgDebug("save_userconf: c_dev->av[%d] = %s, b_dev->av[%d] = %s\n", i, c_dev->av[i], i, b_dev->av[i]);
1100		if (strcmp(c_dev->av[i], b_dev->av[i])) {
1101		    if (isDebug())
1102			msgDebug("save_userconf: %s (boot) -> %s (core)\n",
1103				 c_dev->av[i], b_dev->av[i]);
1104		    isa_setdev(boot, c_dev);
1105		}
1106	    }
1107	}
1108	else {
1109	    if (isDebug())
1110		msgDebug("skipping npx0\n");
1111	}
1112    }
1113    if (isDebug())
1114	msgDebug("Closing kernels\n");
1115    uc_close(core, 0);
1116    uc_close(boot, 1);
1117}
1118#endif
1119