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