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