install.c revision 17126
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.112 1996/07/09 14:28: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 <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	msgConfirm("Finally, you must specify an installation medium.");
383	if (!dmenuOpenSimple(&MenuMedia, FALSE) || !mediaDevice)
384	    return DITEM_FAILURE | DITEM_RECREATE;
385    }
386
387    if (DITEM_STATUS((i = installCommit(self))) == DITEM_FAILURE) {
388	msgConfirm("Installation completed with some errors.  You may wish to\n"
389		   "scroll through the debugging messages on VTY1 with the\n"
390		   "scroll-lock feature.  You can also chose \"No\" at the next\n"
391		   "prompt and go back into the installation menus to try and retry\n"
392		   "whichever operations have failed.");
393	return i | DITEM_RECREATE;
394
395    }
396    else
397	msgConfirm("Congradulations!  You now have FreeBSD installed on your system.\n\n"
398		   "We will now move on to the final configuration questions.\n"
399		   "For any option you do not wish to configure, simply select\n"
400		   "No.\n\n"
401		   "If you wish to re-enter this utility after the system is up, you\n"
402		   "may do so by typing: /stand/sysinstall.");
403
404    if (mediaDevice->type != DEVICE_TYPE_FTP && mediaDevice->type != DEVICE_TYPE_NFS) {
405	if (!msgYesNo("Does this system have a network interface card?")) {
406	    Device *save = mediaDevice;
407
408	    /* This will also set the media device, which we don't want */
409	    tcpDeviceSelect();
410	    /* so we restore our saved value below */
411	    mediaDevice = save;
412	    dialog_clear();
413	}
414    }
415
416    if (!msgYesNo("Would you like to configure Samba for connecting NETBUI clients to this\n"
417		  "machine?  Windows 95, Windows NT and Windows for Workgroups\n"
418		  "machines can use NETBUI transport for disk and printer sharing."))
419	configSamba(self);
420
421    if (!msgYesNo("Will this machine be an IP gateway (e.g. will it forward packets\n"
422		  "between interfaces)?"))
423	variable_set2("gateway", "YES");
424
425    if (!msgYesNo("Do you want to allow anonymous FTP connections to this machine?"))
426	configAnonFTP(self);
427
428    if (!msgYesNo("Do you want to configure this machine as an NFS server?"))
429	configNFSServer(self);
430
431    if (!msgYesNo("Do you want to configure this machine as an NFS client?"))
432	variable_set2("nfs_client", "YES");
433
434    if (!msgYesNo("Do you want to configure this machine as a WEB server?"))
435	configApache(self);
436
437    if (!msgYesNo("Would you like to customize your system console settings?")) {
438	WINDOW *w = savescr();
439
440	dmenuOpenSimple(&MenuSyscons, FALSE);
441	restorescr(w);
442    }
443
444    if (!msgYesNo("Would you like to set this machine's time zone now?")) {
445	WINDOW *w = savescr();
446
447	dialog_clear();
448	systemExecute("rm -f /etc/wall_cmos_clock /etc/localtime; tzsetup");
449	restorescr(w);
450    }
451
452    if (!msgYesNo("Does this system have a mouse attached to it?")) {
453	WINDOW *w = savescr();
454
455	dmenuOpenSimple(&MenuMouse, FALSE);
456	restorescr(w);
457    }
458
459    if (directory_exists("/usr/X11R6")) {
460	if (!msgYesNo("Would you like to configure your X server at this time?"))
461	    configXFree86(self);
462    }
463
464    if (cdromMounted) {
465	if (!msgYesNo("Would you like to link to the ports tree on your CDROM?\n\n"
466		      "This will require that you have your FreeBSD CD in the CDROM\n"
467		      "drive to use the ports collection, but at a substantial savings\n"
468		      "in disk space (NOTE:  This may take as long as 15 or 20 minutes\n"
469		      "depending on the speed of your CDROM drive)."))
470	    configPorts(self);
471    }
472
473    if (!msgYesNo("The FreeBSD package collection is a collection of over 450 ready-to-run\n"
474		  "applications, from text editors to games to WEB servers.  Would you like\n"
475		  "to browse the collection now?"))
476	configPackages(self);
477
478    /* XXX Put whatever other nice configuration questions you'd like to ask the user here XXX */
479
480    /* Give user the option of one last configuration spree */
481    installConfigure();
482
483    /* Now write out any changes .. */
484    configResolv();
485    configSysconfig("/etc/sysconfig");
486
487    return DITEM_LEAVE_MENU | DITEM_RECREATE;
488}
489
490/* The version of commit we call from the Install Custom menu */
491int
492installCustomCommit(dialogMenuItem *self)
493{
494    int i;
495
496    i = installCommit(self);
497    if (DITEM_STATUS(i) == DITEM_SUCCESS) {
498	/* Give user the option of one last configuration spree */
499	installConfigure();
500
501	/* Now write out any changes .. */
502	configResolv();
503	configSysconfig("/etc/sysconfig");
504	return i;
505    }
506    else
507	msgConfirm("The commit operation completed with errors.  Not\n"
508		   "updating /etc files.");
509    return i;
510}
511
512/*
513 * What happens when we finally decide to going ahead with the installation.
514 *
515 * This is broken into multiple stages so that the user can do a full
516 * installation but come back here again to load more distributions,
517 * perhaps from a different media type.  This would allow, for
518 * example, the user to load the majority of the system from CDROM and
519 * then use ftp to load just the DES dist.
520 */
521int
522installCommit(dialogMenuItem *self)
523{
524    int i;
525    char *str;
526
527    if (!mediaVerify())
528	return DITEM_FAILURE;
529
530    str = variable_get(SYSTEM_STATE);
531    if (isDebug())
532	msgDebug("installCommit: System state is `%s'\n", str);
533
534    if (RunningAsInit) {
535	/* Do things we wouldn't do to a multi-user system */
536	if (DITEM_STATUS((i = installInitial())) == DITEM_FAILURE)
537	    return i;
538	if (DITEM_STATUS((i = configFstab())) == DITEM_FAILURE)
539	    return i;
540    }
541
542    i = distExtractAll(self);
543    if (DITEM_STATUS(i) == DITEM_FAILURE)
544    	(void)installFixup(self);
545    else
546    	i = installFixup(self);
547
548    /* Don't print this if we're express or novice installing - they have their own error reporting */
549    if (strcmp(str, "express") && strcmp(str, "novice")) {
550	if (Dists || DITEM_STATUS(i) == DITEM_FAILURE)
551	    msgConfirm("Installation completed with some errors.  You may wish to\n"
552		       "scroll through the debugging messages on VTY1 with the\n"
553		       "scroll-lock feature.");
554	else
555	    msgConfirm("Installation completed successfully.\n\n"
556		       "If you have any network devices you have not yet configured,\n"
557		       "see the Interfaces configuration item on the Configuration menu.");
558    }
559    variable_set2(SYSTEM_STATE, DITEM_STATUS(i) == DITEM_FAILURE ? "error-install" : "full-install");
560    return i | DITEM_RECREATE;
561}
562
563static void
564installConfigure(void)
565{
566    /* Final menu of last resort */
567    if (!msgYesNo("Visit the general configuration menu for a chance to set\n"
568		  "any last options?")) {
569	WINDOW *w = savescr();
570
571	dmenuOpenSimple(&MenuConfigure, FALSE);
572	restorescr(w);
573    }
574}
575
576int
577installFixup(dialogMenuItem *self)
578{
579    Device **devs;
580    int i;
581
582    if (!file_readable("/kernel")) {
583	if (file_readable("/kernel.GENERIC")) {
584	    if (vsystem("cp -p /kernel.GENERIC /kernel")) {
585		msgConfirm("Unable to link /kernel into place!");
586		return DITEM_FAILURE;
587	    }
588	}
589	else {
590	    msgConfirm("Can't find a kernel image to link to on the root file system!\n"
591		       "You're going to have a hard time getting this system to\n"
592		       "boot from the hard disk, I'm afraid!");
593	    return DITEM_FAILURE;
594	}
595    }
596    /* Resurrect /dev after bin distribution screws it up */
597    if (RunningAsInit) {
598	msgNotify("Remaking all devices.. Please wait!");
599	if (vsystem("cd /dev; sh MAKEDEV all")) {
600	    msgConfirm("MAKEDEV returned non-zero status");
601	    return DITEM_FAILURE;
602	}
603
604	msgNotify("Resurrecting /dev entries for slices..");
605	devs = deviceFind(NULL, DEVICE_TYPE_DISK);
606	if (!devs)
607	    msgFatal("Couldn't get a disk device list!");
608
609	/* Resurrect the slices that the former clobbered */
610	for (i = 0; devs[i]; i++) {
611	    Disk *disk = (Disk *)devs[i]->private;
612	    Chunk *c1;
613
614	    if (!devs[i]->enabled)
615		continue;
616	    if (!disk->chunks)
617		msgFatal("No chunk list found for %s!", disk->name);
618	    for (c1 = disk->chunks->part; c1; c1 = c1->next) {
619		if (c1->type == freebsd) {
620		    msgNotify("Making slice entries for %s", c1->name);
621		    if (vsystem("cd /dev; sh MAKEDEV %sh", c1->name)) {
622			msgConfirm("Unable to make slice entries for %s!", c1->name);
623			return DITEM_FAILURE;
624		    }
625		}
626	    }
627	}
628	/* XXX Do all the last ugly work-arounds here which we'll try and excise someday right?? XXX */
629
630	msgNotify("Fixing permissions..");
631	/* BOGON #1:  XFree86 extracting /usr/X11R6 with root-only perms */
632	if (directory_exists("/usr/X11R6")) {
633	    vsystem("chmod -R a+r /usr/X11R6");
634	    vsystem("find /usr/X11R6 -type d | xargs chmod a+x");
635	}
636	/* BOGON #2: We leave /etc in a bad state */
637	chmod("/etc", 0755);
638
639	/* BOGON #3: No /var/db/mountdtab complains */
640	Mkdir("/var/db");
641	creat("/var/db/mountdtab", 0644);
642
643	/* Now run all the mtree stuff to fix things up */
644        vsystem("mtree -deU -f /etc/mtree/BSD.root.dist -p /");
645        vsystem("mtree -deU -f /etc/mtree/BSD.var.dist -p /var");
646        vsystem("mtree -deU -f /etc/mtree/BSD.usr.dist -p /usr");
647    }
648    return DITEM_SUCCESS;
649}
650
651/* Go newfs and/or mount all the filesystems we've been asked to */
652int
653installFilesystems(dialogMenuItem *self)
654{
655    int i;
656    Disk *disk;
657    Chunk *c1, *c2, *rootdev, *swapdev, *usrdev;
658    Device **devs;
659    PartInfo *root;
660    char dname[80], *str;
661    extern int MakeDevChunk(Chunk *c, char *n);
662    Boolean upgrade = FALSE;
663
664    str = variable_get(SYSTEM_STATE);
665
666    if (!checkLabels(&rootdev, &swapdev, &usrdev))
667	return DITEM_FAILURE;
668
669    if (rootdev)
670	root = (PartInfo *)rootdev->private_data;
671    else
672	root = NULL;
673
674    command_clear();
675    upgrade = str && !strcmp(str, "upgrade");
676
677    if (swapdev) {
678	/* As the very first thing, try to get ourselves some swap space */
679	sprintf(dname, "/dev/%s", swapdev->name);
680	if (!Fake && (!MakeDevChunk(swapdev, "/dev") || !file_readable(dname))) {
681	    msgConfirm("Unable to make device node for %s in /dev!\n"
682		       "The creation of filesystems will be aborted.", dname);
683	    return DITEM_FAILURE;
684	}
685
686	if (!Fake) {
687	    if (!swapon(dname))
688		msgNotify("Added %s as initial swap device", dname);
689	    else
690		msgConfirm("WARNING!  Unable to swap to %s: %s\n"
691			   "This may cause the installation to fail at some point\n"
692			   "if you don't have a lot of memory.", dname, strerror(errno));
693	}
694    }
695
696    if (rootdev) {
697	/* Next, create and/or mount the root device */
698	sprintf(dname, "/dev/r%sa", rootdev->disk->name);
699	if (!Fake && (!MakeDevChunk(rootdev, "/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	if (strcmp(root->mountpoint, "/"))
705	    msgConfirm("Warning: %s is marked as a root partition but is mounted on %s", rootdev->name, root->mountpoint);
706
707	if (root->newfs) {
708	    int i;
709
710	    msgNotify("Making a new root filesystem on %s", dname);
711	    i = vsystem("%s %s", root->newfs_cmd, dname);
712	    if (i) {
713		msgConfirm("Unable to make new root filesystem on %s!\n"
714			   "Command returned status %d", dname, i);
715		return DITEM_FAILURE;
716	    }
717	}
718	else {
719	    if (!upgrade) {
720		msgConfirm("Warning:  Using existing root partition.  It will be assumed\n"
721			   "that you have the appropriate device entries already in /dev.");
722	    }
723	    msgNotify("Checking integrity of existing %s filesystem.", dname);
724	    i = vsystem("fsck -y %s", dname);
725	    if (i)
726		msgConfirm("Warning: fsck returned status of %d for %s.\n"
727			   "This partition may be unsafe to use.", i, dname);
728	}
729
730	/* Switch to block device */
731	sprintf(dname, "/dev/%sa", rootdev->disk->name);
732	if (Mount("/mnt", dname)) {
733	    msgConfirm("Unable to mount the root file system on %s!  Giving up.", dname);
734	    return DITEM_FAILURE;
735	}
736    }
737
738    /* Now buzz through the rest of the partitions and mount them too */
739    devs = deviceFind(NULL, DEVICE_TYPE_DISK);
740    for (i = 0; devs[i]; i++) {
741	if (!devs[i]->enabled)
742	    continue;
743
744	disk = (Disk *)devs[i]->private;
745	if (!disk->chunks) {
746	    msgConfirm("No chunk list found for %s!", disk->name);
747	    return DITEM_FAILURE;
748	}
749	if (root && (root->newfs || upgrade)) {
750	    Mkdir("/mnt/dev");
751	    if (!Fake)
752		MakeDevDisk(disk, "/mnt/dev");
753	}
754
755	for (c1 = disk->chunks->part; c1; c1 = c1->next) {
756	    if (c1->type == freebsd) {
757		for (c2 = c1->part; c2; c2 = c2->next) {
758		    if (c2->type == part && c2->subtype != FS_SWAP && c2->private_data) {
759			PartInfo *tmp = (PartInfo *)c2->private_data;
760
761			/* Already did root */
762			if (c2 == rootdev)
763			    continue;
764
765			if (tmp->newfs)
766			    command_shell_add(tmp->mountpoint, "%s /mnt/dev/r%s", tmp->newfs_cmd, c2->name);
767			else
768			    command_shell_add(tmp->mountpoint, "fsck -y /mnt/dev/r%s", c2->name);
769			command_func_add(tmp->mountpoint, Mount, c2->name);
770		    }
771		    else if (c2->type == part && c2->subtype == FS_SWAP) {
772			char fname[80];
773			int i;
774
775			if (c2 == swapdev)
776			    continue;
777			sprintf(fname, "/mnt/dev/%s", c2->name);
778			i = (Fake || swapon(fname));
779			if (!i)
780			    msgNotify("Added %s as an additional swap device", fname);
781			else
782			    msgConfirm("Unable to add %s as a swap device: %s", fname, strerror(errno));
783		    }
784		}
785	    }
786	    else if (c1->type == fat && c1->private_data && (root->newfs || upgrade)) {
787		char name[FILENAME_MAX];
788
789		sprintf(name, "/mnt%s", ((PartInfo *)c1->private_data)->mountpoint);
790		Mkdir(name);
791	    }
792	}
793    }
794
795    msgNotify("Copying initial device files..");
796    /* Copy the boot floppy's dev files */
797    if ((root->newfs || upgrade) && vsystem("find -x /dev | cpio %s -pdum /mnt", cpioVerbosity())) {
798	msgConfirm("Couldn't clone the /dev files!");
799	return DITEM_FAILURE;
800    }
801
802    command_sort();
803    command_execute();
804    return DITEM_SUCCESS;
805}
806
807/* Initialize various user-settable values to their defaults */
808int
809installVarDefaults(dialogMenuItem *self)
810{
811    char *cp;
812
813    /* Set default startup options */
814    variable_set2(VAR_ROUTEDFLAGS,		"-q");
815    variable_set2(VAR_RELNAME,			RELEASE_NAME);
816    variable_set2(VAR_CPIO_VERBOSITY,		"high");
817    variable_set2(VAR_TAPE_BLOCKSIZE,		DEFAULT_TAPE_BLOCKSIZE);
818    variable_set2(VAR_INSTALL_ROOT,		"/");
819    cp = getenv("EDITOR");
820    if (!cp)
821	cp = "/usr/bin/ee";
822    variable_set2(VAR_EDITOR,			cp);
823    variable_set2(VAR_FTP_USER,			"ftp");
824    variable_set2(VAR_BROWSER_PACKAGE,		"lynx-2.5FM");
825    variable_set2(VAR_BROWSER_BINARY,		"/usr/local/bin/lynx");
826    variable_set2(VAR_FTP_STATE,		"passive");
827    variable_set2(VAR_PKG_TMPDIR,		"/usr/tmp");
828    if (getpid() != 1)
829	variable_set2(SYSTEM_STATE,		"update");
830    else
831	variable_set2(SYSTEM_STATE,		"init");
832    return DITEM_SUCCESS;
833}
834
835/* Copy the boot floppy contents into /stand */
836Boolean
837copySelf(void)
838{
839    int i;
840
841    msgWeHaveOutput("Copying the boot floppy to /stand on root filesystem");
842    i = vsystem("find -x /stand | cpio %s -pdum /mnt", cpioVerbosity());
843    if (i) {
844	msgConfirm("Copy returned error status of %d!", i);
845	return FALSE;
846    }
847
848    /* Copy the /etc files into their rightful place */
849    if (vsystem("cd /mnt/stand; find etc | cpio %s -pdum /mnt", cpioVerbosity())) {
850	msgConfirm("Couldn't copy up the /etc files!");
851	return TRUE;
852    }
853    return TRUE;
854}
855
856static void
857create_termcap(void)
858{
859    FILE *fp;
860
861    const char *caps[] = {
862	termcap_vt100, termcap_cons25, termcap_cons25_m, termcap_cons25r,
863	termcap_cons25r_m, termcap_cons25l1, termcap_cons25l1_m, NULL,
864    };
865    const char **cp;
866
867    if (!file_readable(TERMCAP_FILE)) {
868	Mkdir("/usr/share/misc");
869	fp = fopen(TERMCAP_FILE, "w");
870	if (!fp) {
871	    msgConfirm("Unable to initialize termcap file. Some screen-oriented\nutilities may not work.");
872	    return;
873	}
874	cp = caps;
875	while (*cp)
876	    fprintf(fp, "%s\n", *(cp++));
877	fclose(fp);
878    }
879}
880
881