install.c revision 18687
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.127 1996/10/04 13:33:43 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	    dialog_clear();
600
601	    if (vsystem("cp -p /kernel.GENERIC /kernel")) {
602		msgConfirm("Unable to link /kernel into place!");
603		return DITEM_FAILURE;
604	    }
605	}
606	else {
607	    msgConfirm("Can't find a kernel image to link to on the root file system!\n"
608		       "You're going to have a hard time getting this system to\n"
609		       "boot from the hard disk, I'm afraid!");
610	    return DITEM_FAILURE;
611	}
612    }
613
614    /* Resurrect /dev after bin distribution screws it up */
615    if (RunningAsInit) {
616	msgNotify("Remaking all devices.. Please wait!");
617	if (vsystem("cd /dev; sh MAKEDEV all")) {
618	    msgConfirm("MAKEDEV returned non-zero status");
619	    return DITEM_FAILURE;
620	}
621
622	msgNotify("Resurrecting /dev entries for slices..");
623	devs = deviceFind(NULL, DEVICE_TYPE_DISK);
624	if (!devs)
625	    msgFatal("Couldn't get a disk device list!");
626
627	/* Resurrect the slices that the former clobbered */
628	for (i = 0; devs[i]; i++) {
629	    Disk *disk = (Disk *)devs[i]->private;
630	    Chunk *c1;
631
632	    if (!devs[i]->enabled)
633		continue;
634	    if (!disk->chunks)
635		msgFatal("No chunk list found for %s!", disk->name);
636	    for (c1 = disk->chunks->part; c1; c1 = c1->next) {
637		if (c1->type == freebsd) {
638		    msgNotify("Making slice entries for %s", c1->name);
639		    if (vsystem("cd /dev; sh MAKEDEV %sh", c1->name)) {
640			msgConfirm("Unable to make slice entries for %s!", c1->name);
641			return DITEM_FAILURE;
642		    }
643		}
644	    }
645	}
646	/* XXX Do all the last ugly work-arounds here which we'll try and excise someday right?? XXX */
647
648	msgNotify("Fixing permissions..");
649	/* BOGON #1:  XFree86 extracting /usr/X11R6 with root-only perms */
650	if (directory_exists("/usr/X11R6")) {
651	    vsystem("chmod -R a+r /usr/X11R6");
652	    vsystem("find /usr/X11R6 -type d | xargs chmod a+x");
653	}
654	/* BOGON #2: We leave /etc in a bad state */
655	chmod("/etc", 0755);
656
657	/* BOGON #3: No /var/db/mountdtab complains */
658	Mkdir("/var/db");
659	creat("/var/db/mountdtab", 0644);
660
661	/* Now run all the mtree stuff to fix things up */
662        vsystem("mtree -deU -f /etc/mtree/BSD.root.dist -p /");
663        vsystem("mtree -deU -f /etc/mtree/BSD.var.dist -p /var");
664        vsystem("mtree -deU -f /etc/mtree/BSD.usr.dist -p /usr");
665    }
666    return DITEM_SUCCESS;
667}
668
669/* Go newfs and/or mount all the filesystems we've been asked to */
670int
671installFilesystems(dialogMenuItem *self)
672{
673    int i;
674    Disk *disk;
675    Chunk *c1, *c2, *rootdev, *swapdev, *usrdev, *vardev;
676    Device **devs;
677    PartInfo *root;
678    char dname[80], *str;
679    extern int MakeDevChunk(Chunk *c, char *n);
680    Boolean upgrade = FALSE;
681
682    /* If we've already done this, bail out */
683    if (variable_get(DISK_PREPARED))
684	return DITEM_SUCCESS;
685
686    str = variable_get(SYSTEM_STATE);
687
688    if (!checkLabels(TRUE, &rootdev, &swapdev, &usrdev, &vardev))
689	return DITEM_FAILURE;
690
691    if (rootdev)
692	root = (PartInfo *)rootdev->private_data;
693    else
694	root = NULL;
695
696    command_clear();
697    upgrade = str && !strcmp(str, "upgrade");
698
699    if (swapdev) {
700	/* As the very first thing, try to get ourselves some swap space */
701	sprintf(dname, "/dev/%s", swapdev->name);
702	if (!Fake && (!MakeDevChunk(swapdev, "/dev") || !file_readable(dname))) {
703	    msgConfirm("Unable to make device node for %s in /dev!\n"
704		       "The creation of filesystems will be aborted.", dname);
705	    return DITEM_FAILURE;
706	}
707
708	if (!Fake) {
709	    if (!swapon(dname))
710		msgNotify("Added %s as initial swap device", dname);
711	    else
712		msgConfirm("WARNING!  Unable to swap to %s: %s\n"
713			   "This may cause the installation to fail at some point\n"
714			   "if you don't have a lot of memory.", dname, strerror(errno));
715	}
716    }
717
718    if (rootdev) {
719	/* Next, create and/or mount the root device */
720	sprintf(dname, "/dev/r%sa", rootdev->disk->name);
721	if (!Fake && (!MakeDevChunk(rootdev, "/dev") || !file_readable(dname))) {
722	    msgConfirm("Unable to make device node for %s in /dev!\n"
723		       "The creation of filesystems will be aborted.", dname);
724	    return DITEM_FAILURE;
725	}
726	if (strcmp(root->mountpoint, "/"))
727	    msgConfirm("Warning: %s is marked as a root partition but is mounted on %s", rootdev->name, root->mountpoint);
728
729	if (root->newfs) {
730	    int i;
731
732	    msgNotify("Making a new root filesystem on %s", dname);
733	    i = vsystem("%s %s", root->newfs_cmd, dname);
734	    if (i) {
735		msgConfirm("Unable to make new root filesystem on %s!\n"
736			   "Command returned status %d", dname, i);
737		return DITEM_FAILURE;
738	    }
739	}
740	else {
741	    if (!upgrade) {
742		msgConfirm("Warning:  Using existing root partition.  It will be assumed\n"
743			   "that you have the appropriate device entries already in /dev.");
744	    }
745	    msgNotify("Checking integrity of existing %s filesystem.", dname);
746	    i = vsystem("fsck -y %s", dname);
747	    if (i)
748		msgConfirm("Warning: fsck returned status of %d for %s.\n"
749			   "This partition may be unsafe to use.", i, dname);
750	}
751
752	/* Switch to block device */
753	sprintf(dname, "/dev/%sa", rootdev->disk->name);
754	if (Mount("/mnt", dname)) {
755	    msgConfirm("Unable to mount the root file system on %s!  Giving up.", dname);
756	    return DITEM_FAILURE;
757	}
758    }
759
760    /* Now buzz through the rest of the partitions and mount them too */
761    devs = deviceFind(NULL, DEVICE_TYPE_DISK);
762    for (i = 0; devs[i]; i++) {
763	if (!devs[i]->enabled)
764	    continue;
765
766	disk = (Disk *)devs[i]->private;
767	if (!disk->chunks) {
768	    msgConfirm("No chunk list found for %s!", disk->name);
769	    return DITEM_FAILURE;
770	}
771	if (root && (root->newfs || upgrade)) {
772	    Mkdir("/mnt/dev");
773	    if (!Fake)
774		MakeDevDisk(disk, "/mnt/dev");
775	}
776
777	for (c1 = disk->chunks->part; c1; c1 = c1->next) {
778	    if (c1->type == freebsd) {
779		for (c2 = c1->part; c2; c2 = c2->next) {
780		    if (c2->type == part && c2->subtype != FS_SWAP && c2->private_data) {
781			PartInfo *tmp = (PartInfo *)c2->private_data;
782
783			/* Already did root */
784			if (c2 == rootdev)
785			    continue;
786
787			if (tmp->newfs)
788			    command_shell_add(tmp->mountpoint, "%s /mnt/dev/r%s", tmp->newfs_cmd, c2->name);
789			else
790			    command_shell_add(tmp->mountpoint, "fsck -y /mnt/dev/r%s", c2->name);
791			command_func_add(tmp->mountpoint, Mount, c2->name);
792		    }
793		    else if (c2->type == part && c2->subtype == FS_SWAP) {
794			char fname[80];
795			int i;
796
797			if (c2 == swapdev)
798			    continue;
799			sprintf(fname, "/mnt/dev/%s", c2->name);
800			i = (Fake || swapon(fname));
801			if (!i)
802			    msgNotify("Added %s as an additional swap device", fname);
803			else
804			    msgConfirm("Unable to add %s as a swap device: %s", fname, strerror(errno));
805		    }
806		}
807	    }
808	    else if (c1->type == fat && c1->private_data && (root->newfs || upgrade)) {
809		char name[FILENAME_MAX];
810
811		sprintf(name, "/mnt%s", ((PartInfo *)c1->private_data)->mountpoint);
812		Mkdir(name);
813	    }
814	}
815    }
816
817    msgNotify("Copying initial device files..");
818    /* Copy the boot floppy's dev files */
819    if ((root->newfs || upgrade) && vsystem("find -x /dev | cpio %s -pdum /mnt", cpioVerbosity())) {
820	msgConfirm("Couldn't clone the /dev files!");
821	return DITEM_FAILURE;
822    }
823
824    command_sort();
825    command_execute();
826    variable_set2(DISK_PREPARED, "yes");
827    return DITEM_SUCCESS;
828}
829
830/* Initialize various user-settable values to their defaults */
831int
832installVarDefaults(dialogMenuItem *self)
833{
834    char *cp;
835
836    /* Set default startup options */
837    variable_set2(VAR_ROUTEDFLAGS,		"-q");
838    variable_set2(VAR_RELNAME,			RELEASE_NAME);
839    variable_set2(VAR_CPIO_VERBOSITY,		"high");
840    variable_set2(VAR_TAPE_BLOCKSIZE,		DEFAULT_TAPE_BLOCKSIZE);
841    variable_set2(VAR_INSTALL_ROOT,		"/");
842    cp = getenv("EDITOR");
843    if (!cp)
844	cp = "/usr/bin/ee";
845    variable_set2(VAR_EDITOR,			cp);
846    variable_set2(VAR_FTP_USER,			"ftp");
847    variable_set2(VAR_BROWSER_PACKAGE,		"lynx-2.5FM");
848    variable_set2(VAR_BROWSER_BINARY,		"/usr/local/bin/lynx");
849    variable_set2(VAR_FTP_STATE,		"passive");
850    variable_set2(VAR_PKG_TMPDIR,		"/usr/tmp");
851    if (getpid() != 1)
852	variable_set2(SYSTEM_STATE,		"update");
853    else
854	variable_set2(SYSTEM_STATE,		"init");
855    return DITEM_SUCCESS;
856}
857
858/* Copy the boot floppy contents into /stand */
859Boolean
860copySelf(void)
861{
862    int i;
863
864    msgWeHaveOutput("Copying the boot floppy to /stand on root filesystem");
865    i = vsystem("find -x /stand | cpio %s -pdum /mnt", cpioVerbosity());
866    if (i) {
867	msgConfirm("Copy returned error status of %d!", i);
868	return FALSE;
869    }
870
871    /* Copy the /etc files into their rightful place */
872    if (vsystem("cd /mnt/stand; find etc | cpio %s -pdum /mnt", cpioVerbosity())) {
873	msgConfirm("Couldn't copy up the /etc files!");
874	return TRUE;
875    }
876    return TRUE;
877}
878
879static void
880create_termcap(void)
881{
882    FILE *fp;
883
884    const char *caps[] = {
885	termcap_vt100, termcap_cons25, termcap_cons25_m, termcap_cons25r,
886	termcap_cons25r_m, termcap_cons25l1, termcap_cons25l1_m, NULL,
887    };
888    const char **cp;
889
890    if (!file_readable(TERMCAP_FILE)) {
891	Mkdir("/usr/share/misc");
892	fp = fopen(TERMCAP_FILE, "w");
893	if (!fp) {
894	    msgConfirm("Unable to initialize termcap file. Some screen-oriented\nutilities may not work.");
895	    return;
896	}
897	cp = caps;
898	while (*cp)
899	    fprintf(fp, "%s\n", *(cp++));
900	fclose(fp);
901    }
902}
903
904static char *isa_list[] = {
905  "device",
906  "ioport",
907  "irq",
908  "drq",
909  "iomem",
910  "iosize",
911  "flags",
912  "alive",
913  "enabled",
914};
915
916static void
917save_userconfig_to_kernel(char *kern)
918{
919    struct kernel *core, *boot;
920    struct list *c_isa, *c_dev, *b_dev;
921    int i, d;
922
923    core = uc_open("-incore");
924    if (core < 0) {
925	msgDebug("Can't read in-core information for kernel.\n");
926	return;
927    }
928
929    boot = uc_open(kern);
930    if (boot < 0) {
931	msgDebug("Can't read device information for kernel image %s\n", kern);
932	return;
933    }
934    c_isa = uc_getdev(core, "-isa");
935    for (d = 0; d < c_isa->ac; d++) {
936	if (isDebug())
937	    msgDebug("Outer loop, c_isa->av[%d] = %s\n", d, c_isa->av[d]);
938	if (strcmp(c_isa->av[d], "npx0")) { /* special case npx0, which
939					       mucks with its id_irq member */
940	    c_dev = uc_getdev(core, c_isa->av[d]);
941	    b_dev = uc_getdev(boot, c_isa->av[d]);
942	    for (i = 0; i < c_dev->ac; i++) {
943		if (isDebug())
944		    msgDebug("Inner loop, c_dev->av[%d] = %s\n", i, c_dev->av[i]);
945		if (strcmp(c_dev->av[i], b_dev->av[i])) {
946		    if (isDebug())
947			msgDebug("%s %s changed: %s (boot) -> %s (core)\n",
948				 c_dev->av[0], isa_list[i], b_dev->av[i], c_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