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