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