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