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