install.c revision 26764
1254853Sdumbbell/*
2254853Sdumbbell * The new sysinstall program.
3254853Sdumbbell *
4254853Sdumbbell * This is probably the last program in the `sysinstall' line - the next
5254853Sdumbbell * generation being essentially a complete rewrite.
6254853Sdumbbell *
7254853Sdumbbell * $Id: install.c,v 1.190 1997/06/20 07:04:16 jkh Exp $
8254853Sdumbbell *
9254853Sdumbbell * Copyright (c) 1995
10254853Sdumbbell *	Jordan Hubbard.  All rights reserved.
11254853Sdumbbell *
12254853Sdumbbell * Redistribution and use in source and binary forms, with or without
13254853Sdumbbell * modification, are permitted provided that the following conditions
14254853Sdumbbell * are met:
15254853Sdumbbell * 1. Redistributions of source code must retain the above copyright
16254853Sdumbbell *    notice, this list of conditions and the following disclaimer,
17254853Sdumbbell *    verbatim and that no modifications are made prior to this
18254853Sdumbbell *    point in the file.
19254853Sdumbbell * 2. Redistributions in binary form must reproduce the above copyright
20254853Sdumbbell *    notice, this list of conditions and the following disclaimer in the
21254853Sdumbbell *    documentation and/or other materials provided with the distribution.
22254853Sdumbbell *
23254853Sdumbbell * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
24254853Sdumbbell * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25254853Sdumbbell * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26254853Sdumbbell * ARE DISCLAIMED.  IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
27254853Sdumbbell * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28254853Sdumbbell * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29254853Sdumbbell * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
30254853Sdumbbell * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31254853Sdumbbell * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32254853Sdumbbell * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33254853Sdumbbell * SUCH DAMAGE.
34254853Sdumbbell *
35254853Sdumbbell */
36254853Sdumbbell
37254853Sdumbbell#include "sysinstall.h"
38254853Sdumbbell#include "uc_main.h"
39254853Sdumbbell#include <ctype.h>
40254853Sdumbbell#include <sys/disklabel.h>
41254853Sdumbbell#include <sys/errno.h>
42254853Sdumbbell#include <sys/ioctl.h>
43254853Sdumbbell#include <sys/fcntl.h>
44254853Sdumbbell#include <sys/wait.h>
45254853Sdumbbell#include <sys/param.h>
46254853Sdumbbell#define MSDOSFS
47254853Sdumbbell#include <sys/mount.h>
48254853Sdumbbell#include <ufs/ufs/ufsmount.h>
49254853Sdumbbell#include <msdosfs/msdosfsmount.h>
50254853Sdumbbell#undef MSDOSFS
51254853Sdumbbell#include <sys/stat.h>
52254853Sdumbbell#include <unistd.h>
53254853Sdumbbell
54254853Sdumbbellstatic void	create_termcap(void);
55254853Sdumbbellstatic void	fixit_common(void);
56254853Sdumbbell#ifdef SAVE_USERCONFIG
57254853Sdumbbellstatic void	save_userconfig_to_kernel(char *);
58254853Sdumbbell#endif
59254853Sdumbbell
60254853Sdumbbell#define TERMCAP_FILE	"/usr/share/misc/termcap"
61254853Sdumbbell
62254853Sdumbbellstatic void	installConfigure(void);
63254853Sdumbbell
64254853SdumbbellBoolean
65254853SdumbbellcheckLabels(Boolean whinge, Chunk **rdev, Chunk **sdev, Chunk **udev, Chunk **vdev)
66254853Sdumbbell{
67254853Sdumbbell    Device **devs;
68254853Sdumbbell    Boolean status;
69254853Sdumbbell    Disk *disk;
70254853Sdumbbell    Chunk *c1, *c2, *rootdev, *swapdev, *usrdev, *vardev;
71254853Sdumbbell    int i;
72254853Sdumbbell
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("Would you like to configure Samba for connecting NETBUI clients to this\n"
523		  "machine?  Windows 95, Windows NT and Windows for Workgroups\n"
524		  "machines can use NETBUI transport for disk and printer sharing."))
525	configSamba(self);
526
527    dialog_clear_norefresh();
528    if (!msgYesNo("Will this machine be an IP gateway (e.g. will it forward packets\n"
529		  "between interfaces)?"))
530	variable_set2("gateway", "YES");
531
532    dialog_clear_norefresh();
533    if (!msgYesNo("Do you want to allow anonymous FTP connections to this machine?"))
534	configAnonFTP(self);
535
536    dialog_clear_norefresh();
537    if (!msgYesNo("Do you want to configure this machine as an NFS server?"))
538	configNFSServer(self);
539
540    dialog_clear_norefresh();
541    if (!msgYesNo("Do you want to configure this machine as an NFS client?"))
542	variable_set2("nfs_client", "YES");
543
544    dialog_clear_norefresh();
545    if (!msgYesNo("Would you like to customize your system console settings?")) {
546	WINDOW *w = savescr();
547
548	dmenuOpenSimple(&MenuSyscons, FALSE);
549	restorescr(w);
550    }
551
552    dialog_clear_norefresh();
553    if (!msgYesNo("Would you like to set this machine's time zone now?")) {
554	WINDOW *w = savescr();
555
556	dialog_clear();
557	systemExecute("tzsetup");
558	restorescr(w);
559    }
560
561    dialog_clear_norefresh();
562    if (!msgYesNo("Does this system have a mouse attached to it?")) {
563	WINDOW *w = savescr();
564
565	dmenuOpenSimple(&MenuMouse, FALSE);
566	restorescr(w);
567    }
568
569    /* Now would be a good time to checkpoint the configuration data */
570    configRC_conf("/etc/rc.conf");
571    sync();
572
573#ifndef USE_XIG_ENVIRONMENT
574    if (directory_exists("/usr/X11R6")) {
575	dialog_clear_norefresh();
576	if (!msgYesNo("Would you like to configure your X server at this time?"))
577	    configXEnvironment(self);
578    }
579#endif
580
581    dialog_clear_norefresh();
582    if (!msgYesNo("The FreeBSD package collection is a collection of hundreds of ready-to-run\n"
583		  "applications, from text editors to games to WEB servers and more.  Would you\n"
584		  "like to browse the collection now?"))
585	configPackages(self);
586
587    dialog_clear_norefresh();
588    if (!msgYesNo("Would you like to add any initial user accounts to the system?\n"
589		  "Adding at least one account for yourself at this stage is suggested\n"
590		  "since working as the \"root\" user is dangerous (it is easy to do\n"
591		  "things which adversely affect the entire system)."))
592	configUsers(self);
593
594    dialog_clear_norefresh();
595    msgConfirm("Now you must set the system manager's password.\n"
596	       "This is the password you'll use to log in as \"root\".");
597    {
598	WINDOW *w = savescr();
599
600	if (!systemExecute("passwd root"))
601	    variable_set2("root_password", "YES");
602	restorescr(w);
603    }
604
605    dialog_clear_norefresh();
606    if (!msgYesNo("Would you like to register your FreeBSD system at this time?\n\n"
607		  "PLEASE, take just 5 minutes to do this.  If we're ever to get any\n"
608		  "significant base of commercial software for FreeBSD, we need to\n"
609		  "be able to provide more information about the size of our user community.\n"
610		  "This is where your registration can really help us, and you can also\n"
611		  "sign up for the new FreeBSD newsletter (its free!) at the same time.\n"))
612	configRegister(NULL);
613    else
614	msgConfirm("OK, but if you should change your mind then you always can register\n"
615		   "later by typing ``/stand/sysinstall register'' or by simply visiting our\n"
616		   "web site at http://www.freebsd.org/register.html");
617
618    /* XXX Put whatever other nice configuration questions you'd like to ask the user here XXX */
619
620    /* Give user the option of one last configuration spree */
621    installConfigure();
622
623    return DITEM_LEAVE_MENU | DITEM_RESTORE;
624}
625
626/* The version of commit we call from the Install Custom menu */
627int
628installCustomCommit(dialogMenuItem *self)
629{
630    int i;
631
632    dialog_clear_norefresh();
633    i = installCommit(self);
634    if (DITEM_STATUS(i) == DITEM_SUCCESS) {
635	/* Give user the option of one last configuration spree */
636	installConfigure();
637	return i;
638    }
639    else
640	msgConfirm("The commit operation completed with errors.  Not\n"
641		   "updating /etc files.");
642    return i;
643}
644
645/*
646 * What happens when we finally decide to going ahead with the installation.
647 *
648 * This is broken into multiple stages so that the user can do a full
649 * installation but come back here again to load more distributions,
650 * perhaps from a different media type.  This would allow, for
651 * example, the user to load the majority of the system from CDROM and
652 * then use ftp to load just the DES dist.
653 */
654int
655installCommit(dialogMenuItem *self)
656{
657    int i;
658    char *str;
659    Boolean need_bin;
660
661    if (!Dists)
662	distConfig(NULL);
663
664    if (!Dists)
665	if (!dmenuOpenSimple(&MenuDistributions, FALSE) && !Dists)
666	    return DITEM_FAILURE | DITEM_RESTORE;
667
668    if (!mediaVerify())
669	return DITEM_FAILURE | DITEM_RESTORE;
670
671    str = variable_get(SYSTEM_STATE);
672    if (isDebug())
673	msgDebug("installCommit: System state is `%s'\n", str);
674
675    if (RunningAsInit) {
676	/* Do things we wouldn't do to a multi-user system */
677	if (DITEM_STATUS((i = installInitial())) == DITEM_FAILURE)
678	    return i;
679	if (DITEM_STATUS((i = configFstab())) == DITEM_FAILURE)
680	    return i;
681    }
682
683try_media:
684    if (!mediaDevice->init(mediaDevice)) {
685	if (!msgYesNo("Unable to initialize selected media. Would you like to\n"
686		      "adjust your media configuration and try again?")) {
687	    mediaDevice = NULL;
688	    if (!mediaVerify())
689		return DITEM_FAILURE | DITEM_RESTORE;
690	    else
691		goto try_media;
692	}
693	else
694	    return DITEM_FAILURE | DITEM_RESTORE;
695    }
696
697    need_bin = Dists & DIST_BIN;
698    i = distExtractAll(self);
699    if (DITEM_STATUS(i) == DITEM_SUCCESS) {
700	if (need_bin && !(Dists & DIST_BIN))
701	    i = installFixup(self);
702    }
703    variable_set2(SYSTEM_STATE, DITEM_STATUS(i) == DITEM_FAILURE ? "error-install" : "full-install");
704
705    /* We always try to install X with the XiG product */
706#ifdef USE_XIG_ENVIRONMENT
707    if (directory_exists("/usr/X11R6"))
708	configXEnvironment(self);
709#endif
710    return i | DITEM_RESTORE;
711}
712
713static void
714installConfigure(void)
715{
716    /* Final menu of last resort */
717    dialog_clear_norefresh();
718    if (!msgYesNo("Visit the general configuration menu for a chance to set\n"
719		  "any last options?")) {
720	WINDOW *w = savescr();
721
722	dmenuOpenSimple(&MenuConfigure, FALSE);
723	restorescr(w);
724    }
725    configRC_conf("/etc/rc.conf");
726    sync();
727}
728
729int
730installFixup(dialogMenuItem *self)
731{
732    Device **devs;
733    int i;
734
735    if (!file_readable("/kernel")) {
736	if (file_readable("/kernel.GENERIC")) {
737#ifdef SAVE_USERCONFIG
738	    /* Snapshot any boot -c changes back to the GENERIC kernel */
739	    if (!strcmp(variable_get(VAR_RELNAME), RELEASE_NAME))
740		save_userconfig_to_kernel("/kernel.GENERIC");
741#endif
742	    if (vsystem("cp -p /kernel.GENERIC /kernel")) {
743		msgConfirm("Unable to link /kernel into place!");
744		return DITEM_FAILURE;
745	    }
746	}
747	else {
748	    msgConfirm("Can't find a kernel image to link to on the root file system!\n"
749		       "You're going to have a hard time getting this system to\n"
750		       "boot from the hard disk, I'm afraid!");
751	    return DITEM_FAILURE;
752	}
753    }
754
755    /* Resurrect /dev after bin distribution screws it up */
756    if (RunningAsInit) {
757	msgNotify("Remaking all devices.. Please wait!");
758	if (vsystem("cd /dev; sh MAKEDEV all")) {
759	    msgConfirm("MAKEDEV returned non-zero status");
760	    return DITEM_FAILURE;
761	}
762
763	msgNotify("Resurrecting /dev entries for slices..");
764	devs = deviceFind(NULL, DEVICE_TYPE_DISK);
765	if (!devs)
766	    msgFatal("Couldn't get a disk device list!");
767
768	/* Resurrect the slices that the former clobbered */
769	for (i = 0; devs[i]; i++) {
770	    Disk *disk = (Disk *)devs[i]->private;
771	    Chunk *c1;
772
773	    if (!devs[i]->enabled)
774		continue;
775	    if (!disk->chunks)
776		msgFatal("No chunk list found for %s!", disk->name);
777	    for (c1 = disk->chunks->part; c1; c1 = c1->next) {
778		if (c1->type == freebsd) {
779		    msgNotify("Making slice entries for %s", c1->name);
780		    if (vsystem("cd /dev; sh MAKEDEV %sh", c1->name)) {
781			msgConfirm("Unable to make slice entries for %s!", c1->name);
782			return DITEM_FAILURE;
783		    }
784		}
785	    }
786	}
787	/* XXX Do all the last ugly work-arounds here which we'll try and excise someday right?? XXX */
788
789	msgNotify("Fixing permissions..");
790	/* BOGON #1:  XFree86 extracting /usr/X11R6 with root-only perms */
791	if (directory_exists("/usr/X11R6")) {
792	    vsystem("chmod -R a+r /usr/X11R6");
793	    vsystem("find /usr/X11R6 -type d | xargs chmod a+x");
794	}
795	/* BOGON #2: We leave /etc in a bad state */
796	chmod("/etc", 0755);
797
798	/* BOGON #3: No /var/db/mountdtab complains */
799	Mkdir("/var/db");
800	creat("/var/db/mountdtab", 0644);
801
802	/* Now run all the mtree stuff to fix things up */
803        vsystem("mtree -deU -f /etc/mtree/BSD.root.dist -p /");
804        vsystem("mtree -deU -f /etc/mtree/BSD.var.dist -p /var");
805        vsystem("mtree -deU -f /etc/mtree/BSD.usr.dist -p /usr");
806    }
807    return DITEM_SUCCESS;
808}
809
810/* Go newfs and/or mount all the filesystems we've been asked to */
811int
812installFilesystems(dialogMenuItem *self)
813{
814    int i;
815    Disk *disk;
816    Chunk *c1, *c2, *rootdev, *swapdev, *usrdev, *vardev;
817    Device **devs;
818    PartInfo *root;
819    char dname[80], *str;
820    extern int MakeDevChunk(Chunk *c, char *n);
821    Boolean upgrade = FALSE;
822
823    /* If we've already done this, bail out */
824    if ((str = variable_get(DISK_LABELLED)) && !strcmp(str, "written"))
825	return DITEM_SUCCESS;
826
827    str = variable_get(SYSTEM_STATE);
828
829    if (!checkLabels(TRUE, &rootdev, &swapdev, &usrdev, &vardev))
830	return DITEM_FAILURE;
831
832    if (rootdev)
833	root = (PartInfo *)rootdev->private_data;
834    else
835	root = NULL;
836
837    command_clear();
838    upgrade = str && !strcmp(str, "upgrade");
839
840    if (swapdev && RunningAsInit) {
841	/* As the very first thing, try to get ourselves some swap space */
842	sprintf(dname, "/dev/%s", swapdev->name);
843	if (!Fake && (!MakeDevChunk(swapdev, "/dev") || !file_readable(dname))) {
844	    msgConfirm("Unable to make device node for %s in /dev!\n"
845		       "The creation of filesystems will be aborted.", dname);
846	    return DITEM_FAILURE;
847	}
848
849	if (!Fake) {
850	    if (!swapon(dname))
851		msgNotify("Added %s as initial swap device", dname);
852	    else
853		msgConfirm("WARNING!  Unable to swap to %s: %s\n"
854			   "This may cause the installation to fail at some point\n"
855			   "if you don't have a lot of memory.", dname, strerror(errno));
856	}
857    }
858
859    if (rootdev && RunningAsInit) {
860	/* Next, create and/or mount the root device */
861	sprintf(dname, "/dev/r%sa", rootdev->disk->name);
862	if (!Fake && (!MakeDevChunk(rootdev, "/dev") || !file_readable(dname))) {
863	    msgConfirm("Unable to make device node for %s in /dev!\n"
864		       "The creation of filesystems will be aborted.", dname);
865	    return DITEM_FAILURE;
866	}
867	if (strcmp(root->mountpoint, "/"))
868	    msgConfirm("Warning: %s is marked as a root partition but is mounted on %s", rootdev->name, root->mountpoint);
869
870	if (root->newfs) {
871	    int i;
872
873	    msgNotify("Making a new root filesystem on %s", dname);
874	    i = vsystem("%s %s", root->newfs_cmd, dname);
875	    if (i) {
876		msgConfirm("Unable to make new root filesystem on %s!\n"
877			   "Command returned status %d", dname, i);
878		return DITEM_FAILURE;
879	    }
880	}
881	else {
882	    if (!upgrade) {
883		msgConfirm("Warning:  Using existing root partition.  It will be assumed\n"
884			   "that you have the appropriate device entries already in /dev.");
885	    }
886	    msgNotify("Checking integrity of existing %s filesystem.", dname);
887	    i = vsystem("fsck -y %s", dname);
888	    if (i)
889		msgConfirm("Warning: fsck returned status of %d for %s.\n"
890			   "This partition may be unsafe to use.", i, dname);
891	}
892
893	/* Switch to block device */
894	sprintf(dname, "/dev/%sa", rootdev->disk->name);
895	if (Mount("/mnt", dname)) {
896	    msgConfirm("Unable to mount the root file system on %s!  Giving up.", dname);
897	    return DITEM_FAILURE;
898	}
899    }
900
901    /* Now buzz through the rest of the partitions and mount them too */
902    devs = deviceFind(NULL, DEVICE_TYPE_DISK);
903    for (i = 0; devs[i]; i++) {
904	if (!devs[i]->enabled)
905	    continue;
906
907	disk = (Disk *)devs[i]->private;
908	if (!disk->chunks) {
909	    msgConfirm("No chunk list found for %s!", disk->name);
910	    return DITEM_FAILURE;
911	}
912	if (RunningAsInit && root && (root->newfs || upgrade)) {
913	    Mkdir("/mnt/dev");
914	    if (!Fake)
915		MakeDevDisk(disk, "/mnt/dev");
916	}
917	else if (!RunningAsInit && !Fake)
918	    MakeDevDisk(disk, "/dev");
919
920	for (c1 = disk->chunks->part; c1; c1 = c1->next) {
921	    if (c1->type == freebsd) {
922		for (c2 = c1->part; c2; c2 = c2->next) {
923		    if (c2->type == part && c2->subtype != FS_SWAP && c2->private_data) {
924			PartInfo *tmp = (PartInfo *)c2->private_data;
925
926			/* Already did root */
927			if (c2 == rootdev)
928			    continue;
929
930			if (tmp->newfs)
931			    command_shell_add(tmp->mountpoint, "%s %s/dev/r%s", tmp->newfs_cmd, RunningAsInit ? "/mnt" : "", c2->name);
932			else
933			    command_shell_add(tmp->mountpoint, "fsck -y %s/dev/r%s", RunningAsInit ? "/mnt" : "", c2->name);
934			command_func_add(tmp->mountpoint, Mount, c2->name);
935		    }
936		    else if (c2->type == part && c2->subtype == FS_SWAP) {
937			char fname[80];
938			int i;
939
940			if (c2 == swapdev)
941			    continue;
942			sprintf(fname, "%s/dev/%s", RunningAsInit ? "/mnt" : "", c2->name);
943			i = (Fake || swapon(fname));
944			if (!i)
945			    msgNotify("Added %s as an additional swap device", fname);
946			else
947			    msgConfirm("Unable to add %s as a swap device: %s", fname, strerror(errno));
948		    }
949		}
950	    }
951	    else if (c1->type == fat && c1->private_data && (root->newfs || upgrade)) {
952		char name[FILENAME_MAX];
953
954		sprintf(name, "%s/%s", RunningAsInit ? "/mnt" : "", ((PartInfo *)c1->private_data)->mountpoint);
955		Mkdir(name);
956	    }
957	}
958    }
959
960    if (RunningAsInit) {
961	msgNotify("Copying initial device files..");
962	/* Copy the boot floppy's dev files */
963	if ((root->newfs || upgrade) && vsystem("find -x /dev | cpio %s -pdum /mnt", cpioVerbosity())) {
964	    msgConfirm("Couldn't clone the /dev files!");
965	    return DITEM_FAILURE;
966	}
967    }
968
969    command_sort();
970    command_execute();
971    return DITEM_SUCCESS;
972}
973
974/* Initialize various user-settable values to their defaults */
975int
976installVarDefaults(dialogMenuItem *self)
977{
978    char *cp;
979
980    /* Set default startup options */
981    variable_set2(VAR_ROUTER_ENABLE,		"NO");
982    variable_set2(VAR_RELNAME,			RELEASE_NAME);
983    variable_set2(VAR_CPIO_VERBOSITY,		"high");
984    variable_set2(VAR_TAPE_BLOCKSIZE,		DEFAULT_TAPE_BLOCKSIZE);
985    variable_set2(VAR_INSTALL_ROOT,		"/");
986    variable_set2(VAR_INSTALL_CFG,		"install.cfg");
987    cp = getenv("EDITOR");
988    if (!cp)
989	cp = "/usr/bin/ee";
990    variable_set2(VAR_EDITOR,			cp);
991    variable_set2(VAR_FTP_USER,			"ftp");
992    variable_set2(VAR_BROWSER_PACKAGE,		PACKAGE_LYNX);
993    variable_set2(VAR_BROWSER_BINARY,		"/usr/local/bin/lynx");
994    variable_set2(VAR_FTP_STATE,		"passive");
995    variable_set2(VAR_NFS_SECURE,		"YES");
996    variable_set2(VAR_PKG_TMPDIR,		"/usr/tmp");
997    variable_set2(VAR_SAMBA_PKG,		PACKAGE_SAMBA);
998    variable_set2(VAR_GATED_PKG,		PACKAGE_GATED);
999    variable_set2(VAR_PCNFSD_PKG,		PACKAGE_PCNFSD);
1000    variable_set2(VAR_MEDIA_TIMEOUT,		itoa(MEDIA_TIMEOUT));
1001    if (getpid() != 1)
1002	variable_set2(SYSTEM_STATE,		"update");
1003    else
1004	variable_set2(SYSTEM_STATE,		"init");
1005    return DITEM_SUCCESS;
1006}
1007
1008/* Load the environment up from various system configuration files */
1009void
1010installEnvironment(void)
1011{
1012    if (file_readable("/etc/rc.conf"))
1013	configEnvironmentRC_conf("/etc/rc.conf");
1014    if (file_readable("/etc/resolv.conf"))
1015	configEnvironmentResolv("/etc/resolv.conf");
1016}
1017
1018/* Copy the boot floppy contents into /stand */
1019Boolean
1020copySelf(void)
1021{
1022    int i;
1023
1024    msgWeHaveOutput("Copying the boot floppy to /stand on root filesystem");
1025    i = vsystem("find -x /stand | cpio %s -pdum /mnt", cpioVerbosity());
1026    if (i) {
1027	msgConfirm("Copy returned error status of %d!", i);
1028	return FALSE;
1029    }
1030
1031    /* Copy the /etc files into their rightful place */
1032    if (vsystem("cd /mnt/stand; find etc | cpio %s -pdum /mnt", cpioVerbosity())) {
1033	msgConfirm("Couldn't copy up the /etc files!");
1034	return TRUE;
1035    }
1036    return TRUE;
1037}
1038
1039static void
1040create_termcap(void)
1041{
1042    FILE *fp;
1043
1044    const char *caps[] = {
1045	termcap_vt100, termcap_cons25, termcap_cons25_m, termcap_cons25r,
1046	termcap_cons25r_m, termcap_cons25l1, termcap_cons25l1_m, NULL,
1047    };
1048    const char **cp;
1049
1050    if (!file_readable(TERMCAP_FILE)) {
1051	Mkdir("/usr/share/misc");
1052	fp = fopen(TERMCAP_FILE, "w");
1053	if (!fp) {
1054	    msgConfirm("Unable to initialize termcap file. Some screen-oriented\nutilities may not work.");
1055	    return;
1056	}
1057	cp = caps;
1058	while (*cp)
1059	    fprintf(fp, "%s\n", *(cp++));
1060	fclose(fp);
1061    }
1062}
1063
1064#ifdef SAVE_USERCONFIG
1065static void
1066save_userconfig_to_kernel(char *kern)
1067{
1068    struct kernel *core, *boot;
1069    struct list *c_isa, *b_isa, *c_dev, *b_dev;
1070    int i, d;
1071
1072    if ((core = uc_open("-incore")) == NULL) {
1073	msgDebug("save_userconf: Can't read in-core information for kernel.\n");
1074	return;
1075    }
1076
1077    if ((boot = uc_open(kern)) == NULL) {
1078	msgDebug("save_userconf: Can't read device information for kernel image %s\n", kern);
1079	return;
1080    }
1081
1082    msgNotify("Saving any boot -c changes to new kernel...");
1083    c_isa = uc_getdev(core, "-isa");
1084    b_isa = uc_getdev(boot, "-isa");
1085    if (isDebug())
1086	msgDebug("save_userconf: got %d ISA device entries from core, %d from boot.\n", c_isa->ac, b_isa->ac);
1087    for (d = 0; d < c_isa->ac; d++) {
1088	if (isDebug())
1089	    msgDebug("save_userconf: ISA device loop, c_isa->av[%d] = %s\n", d, c_isa->av[d]);
1090	if (strcmp(c_isa->av[d], "npx0")) { /* special case npx0, which mucks with its id_irq member */
1091	    c_dev = uc_getdev(core, c_isa->av[d]);
1092	    b_dev = uc_getdev(boot, b_isa->av[d]);
1093	    if (!c_dev || !b_dev) {
1094		msgDebug("save_userconf: c_dev: %x b_dev: %x\n", c_dev, b_dev);
1095		continue;
1096	    }
1097	    if (isDebug())
1098		msgDebug("save_userconf: ISA device %s: %d config parameters (core), %d (boot)\n",
1099			 c_isa->av[d], c_dev->ac, b_dev->ac);
1100	    for (i = 0; i < c_dev->ac; i++) {
1101		if (isDebug())
1102		    msgDebug("save_userconf: c_dev->av[%d] = %s, b_dev->av[%d] = %s\n", i, c_dev->av[i], i, b_dev->av[i]);
1103		if (strcmp(c_dev->av[i], b_dev->av[i])) {
1104		    if (isDebug())
1105			msgDebug("save_userconf: %s (boot) -> %s (core)\n",
1106				 c_dev->av[i], b_dev->av[i]);
1107		    isa_setdev(boot, c_dev);
1108		}
1109	    }
1110	}
1111	else {
1112	    if (isDebug())
1113		msgDebug("skipping npx0\n");
1114	}
1115    }
1116    if (isDebug())
1117	msgDebug("Closing kernels\n");
1118    uc_close(core, 0);
1119    uc_close(boot, 1);
1120}
1121#endif
1122