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