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