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