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