install.c revision 19819
1254721Semaste/*
2254721Semaste * The new sysinstall program.
3254721Semaste *
4254721Semaste * This is probably the last program in the `sysinstall' line - the next
5254721Semaste * generation being essentially a complete rewrite.
6254721Semaste *
7254721Semaste * $Id: install.c,v 1.141 1996/11/14 05:46:22 jkh Exp $
8254721Semaste *
9254721Semaste * Copyright (c) 1995
10254721Semaste *	Jordan Hubbard.  All rights reserved.
11254721Semaste *
12254721Semaste * Redistribution and use in source and binary forms, with or without
13254721Semaste * modification, are permitted provided that the following conditions
14254721Semaste * are met:
15296417Sdim * 1. Redistributions of source code must retain the above copyright
16309124Sdim *    notice, this list of conditions and the following disclaimer,
17296417Sdim *    verbatim and that no modifications are made prior to this
18254721Semaste *    point in the file.
19254721Semaste * 2. Redistributions in binary form must reproduce the above copyright
20254721Semaste *    notice, this list of conditions and the following disclaimer in the
21254721Semaste *    documentation and/or other materials provided with the distribution.
22254721Semaste *
23288943Sdim * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
24254721Semaste * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25254721Semaste * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26254721Semaste * ARE DISCLAIMED.  IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
27254721Semaste * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28288943Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29288943Sdim * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
30254721Semaste * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31254721Semaste * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32254721Semaste * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33254721Semaste * SUCH DAMAGE.
34288943Sdim *
35254721Semaste */
36296417Sdim
37254721Semaste#include "sysinstall.h"
38254721Semaste#include "uc_main.h"
39254721Semaste#include <ctype.h>
40254721Semaste#include <sys/disklabel.h>
41254721Semaste#include <sys/errno.h>
42254721Semaste#include <sys/ioctl.h>
43288943Sdim#include <sys/fcntl.h>
44254721Semaste#include <sys/wait.h>
45262528Semaste#include <sys/param.h>
46254721Semaste#define MSDOSFS
47254721Semaste#include <sys/mount.h>
48254721Semaste#undef MSDOSFS
49254721Semaste#include <sys/stat.h>
50262528Semaste#include <unistd.h>
51254721Semaste#include <sys/mount.h>
52254721Semaste
53254721Semastestatic void	create_termcap(void);
54254721Semaste#ifdef SAVE_USERCONFIG
55254721Semastestatic void	save_userconfig_to_kernel(char *);
56262528Semaste#endif
57262528Semaste
58262528Semaste#define TERMCAP_FILE	"/usr/share/misc/termcap"
59262528Semaste
60262528Semastestatic void	installConfigure(void);
61262528Semaste
62262528SemasteBoolean
63262528SemastecheckLabels(Boolean whinge, Chunk **rdev, Chunk **sdev, Chunk **udev, Chunk **vdev)
64262528Semaste{
65262528Semaste    Device **devs;
66262528Semaste    Boolean status;
67262528Semaste    Disk *disk;
68262528Semaste    Chunk *c1, *c2, *rootdev, *swapdev, *usrdev, *vardev;
69262528Semaste    int i;
70262528Semaste
71262528Semaste    status = TRUE;
72262528Semaste    *rdev = *sdev = *udev = *vdev = rootdev = swapdev = usrdev = vardev = NULL;
73262528Semaste
74262528Semaste    /* We don't need to worry about root/usr/swap if we're already multiuser */
75262528Semaste    if (!RunningAsInit)
76262528Semaste	return status;
77254721Semaste
78254721Semaste    devs = deviceFind(NULL, DEVICE_TYPE_DISK);
79254721Semaste    /* First verify that we have a root device */
80254721Semaste    for (i = 0; devs[i]; i++) {
81254721Semaste	if (!devs[i]->enabled)
82296417Sdim	    continue;
83288943Sdim	disk = (Disk *)devs[i]->private;
84288943Sdim	msgDebug("Scanning disk %s for root filesystem\n", disk->name);
85288943Sdim	if (!disk->chunks)
86288943Sdim	    msgFatal("No chunk list found for %s!", disk->name);
87288943Sdim	for (c1 = disk->chunks->part; c1; c1 = c1->next) {
88254721Semaste	    if (c1->type == freebsd) {
89258884Semaste		for (c2 = c1->part; c2; c2 = c2->next) {
90288943Sdim		    if (c2->type == part && c2->subtype != FS_SWAP && c2->private_data) {
91254721Semaste			if (c2->flags & CHUNK_IS_ROOT) {
92258884Semaste			    if (rootdev) {
93258884Semaste				if (whinge)
94258884Semaste				    msgConfirm("WARNING:  You have more than one root device set?!\n"
95258884Semaste					       "Using the first one found.");
96276479Sdim				continue;
97258884Semaste			    }
98258884Semaste			    else {
99254721Semaste				rootdev = c2;
100254721Semaste				if (isDebug())
101254721Semaste				    msgDebug("Found rootdev at %s!\n", rootdev->name);
102254721Semaste			    }
103254721Semaste			}
104254721Semaste			else if (!strcmp(((PartInfo *)c2->private_data)->mountpoint, "/usr")) {
105254721Semaste			    if (usrdev) {
106254721Semaste				if (whinge)
107254721Semaste				    msgConfirm("WARNING:  You have more than one /usr filesystem.\n"
108288943Sdim					       "Using the first one found.");
109254721Semaste				continue;
110254721Semaste			    }
111254721Semaste			    else {
112254721Semaste				usrdev = c2;
113288943Sdim				if (isDebug())
114254721Semaste				    msgDebug("Found usrdev at %s!\n", usrdev->name);
115254721Semaste			    }
116254721Semaste			}
117254721Semaste			else if (!strcmp(((PartInfo *)c2->private_data)->mountpoint, "/var")) {
118258054Semaste			    if (vardev) {
119296417Sdim				if (whinge)
120296417Sdim				    msgConfirm("WARNING:  You have more than one /var filesystem.\n"
121296417Sdim					       "Using the first one found.");
122296417Sdim				continue;
123296417Sdim			    }
124296417Sdim			    else {
125296417Sdim				vardev = c2;
126296417Sdim				if (isDebug())
127296417Sdim				    msgDebug("Found vardev at %s!\n", vardev->name);
128258054Semaste			    }
129254721Semaste			}
130254721Semaste		    }
131254721Semaste		}
132254721Semaste	    }
133254721Semaste	}
134254721Semaste    }
135254721Semaste
136254721Semaste    /* Now check for swap devices */
137254721Semaste    for (i = 0; devs[i]; i++) {
138254721Semaste	if (!devs[i]->enabled)
139254721Semaste	    continue;
140254721Semaste	disk = (Disk *)devs[i]->private;
141254721Semaste	msgDebug("Scanning disk %s for swap partitions\n", disk->name);
142254721Semaste	if (!disk->chunks)
143254721Semaste	    msgFatal("No chunk list found for %s!", disk->name);
144254721Semaste	for (c1 = disk->chunks->part; c1; c1 = c1->next) {
145288943Sdim	    if (c1->type == freebsd) {
146254721Semaste		for (c2 = c1->part; c2; c2 = c2->next) {
147254721Semaste		    if (c2->type == part && c2->subtype == FS_SWAP && !swapdev) {
148254721Semaste			swapdev = c2;
149254721Semaste			if (isDebug())
150254721Semaste			    msgDebug("Found swapdev at %s!\n", swapdev->name);
151254721Semaste			break;
152254721Semaste		    }
153254721Semaste		}
154254721Semaste	    }
155254721Semaste	}
156254721Semaste    }
157254721Semaste
158254721Semaste    /* Copy our values over */
159254721Semaste    *rdev = rootdev;
160254721Semaste    *sdev = swapdev;
161254721Semaste    *udev = usrdev;
162254721Semaste    *vdev = vardev;
163254721Semaste
164254721Semaste    if (!rootdev && whinge) {
165254721Semaste	msgConfirm("No root device found - you must label a partition as /\n"
166254721Semaste		   "in the label editor.");
167254721Semaste	status = FALSE;
168254721Semaste    }
169276479Sdim    if (!swapdev && whinge) {
170276479Sdim	msgConfirm("No swap devices found - you must create at least one\n"
171296417Sdim		   "swap partition.");
172276479Sdim	status = FALSE;
173254721Semaste    }
174254721Semaste    if (!usrdev && whinge) {
175254721Semaste	msgConfirm("WARNING:  No /usr filesystem found.  This is not technically\n"
176254721Semaste		   "an error if your root filesystem is big enough (or you later\n"
177254721Semaste		   "intend to mount your /usr filesystem over NFS), but it may otherwise\n"
178254721Semaste		   "cause you trouble if you're not exactly sure what you are doing!");
179254721Semaste    }
180254721Semaste    if (!vardev && whinge) {
181254721Semaste	msgConfirm("WARNING:  No /var filesystem found.  This is not technically\n"
182254721Semaste		   "an error if your root filesystem is big enough (or you later\n"
183254721Semaste		   "intend to link /var to someplace else), but it may otherwise\n"
184254721Semaste		   "cause your root filesystem to fill up if you receive lots of mail\n"
185254721Semaste		   "or edit large temporary files.");
186254721Semaste    }
187254721Semaste    return status;
188254721Semaste}
189254721Semaste
190254721Semastestatic int
191254721SemasteinstallInitial(void)
192254721Semaste{
193288943Sdim    static Boolean alreadyDone = FALSE;
194288943Sdim
195288943Sdim    if (alreadyDone)
196288943Sdim	return DITEM_SUCCESS;
197288943Sdim
198288943Sdim    if (!variable_get(DISK_LABELLED)) {
199288943Sdim	msgConfirm("You need to assign disk labels before you can proceed with\n"
200288943Sdim		   "the installation.");
201288943Sdim	return DITEM_FAILURE;
202288943Sdim    }
203288943Sdim    /* If it's labelled, assume it's also partitioned */
204288943Sdim    if (!variable_get(DISK_PARTITIONED))
205288943Sdim	variable_set2(DISK_PARTITIONED, "yes");
206288943Sdim
207288943Sdim    /* If we refuse to proceed, bail. */
208288943Sdim    dialog_clear_norefresh();
209288943Sdim    if (msgYesNo("Last Chance!  Are you SURE you want continue the installation?\n\n"
210254721Semaste		 "If you're running this on a disk with data you wish to save\n"
211254721Semaste		 "then WE STRONGLY ENCOURAGE YOU TO MAKE PROPER BACKUPS before\n"
212254721Semaste		 "proceeding!\n\n"
213254721Semaste		 "We can take no responsibility for lost disk contents!") != 0)
214254721Semaste	return DITEM_FAILURE | DITEM_RESTORE;
215254721Semaste
216254721Semaste    if (DITEM_STATUS(diskLabelCommit(NULL)) != DITEM_SUCCESS) {
217254721Semaste	msgConfirm("Couldn't make filesystems properly.  Aborting.");
218254721Semaste	return DITEM_FAILURE;
219254721Semaste    }
220288943Sdim    else if (isDebug())
221254721Semaste	msgDebug("installInitial: Scribbled successfully on the disk(s)\n");
222288943Sdim
223254721Semaste    if (!copySelf()) {
224288943Sdim	msgConfirm("Couldn't clone the boot floppy onto the root file system.\n"
225254721Semaste		   "Aborting.");
226254721Semaste	return DITEM_FAILURE;
227254721Semaste    }
228254721Semaste
229254721Semaste    if (chroot("/mnt") == -1) {
230254721Semaste	msgConfirm("Unable to chroot to /mnt - this is bad!");
231276479Sdim	return DITEM_FAILURE;
232254721Semaste    }
233254721Semaste
234254721Semaste    chdir("/");
235254721Semaste    variable_set2(RUNNING_ON_ROOT, "yes");
236254721Semaste
237254721Semaste    /* stick a helpful shell over on the 4th VTY */
238276479Sdim    systemCreateHoloshell();
239276479Sdim
240276479Sdim    alreadyDone = TRUE;
241276479Sdim    return DITEM_SUCCESS;
242276479Sdim}
243276479Sdim
244276479Sdimint
245276479SdiminstallFixitCDROM(dialogMenuItem *self)
246276479Sdim{
247276479Sdim    msgConfirm("Sorry, this feature is currently unimplemented but will,\n"
248276479Sdim	       "at some point in the future, support the use of the live\n"
249276479Sdim	       "filesystem CD (CD 2) in fixing your system.");
250254721Semaste    return DITEM_SUCCESS;
251254721Semaste}
252254721Semaste
253258884Semasteint
254258884SemasteinstallFixitFloppy(dialogMenuItem *self)
255258884Semaste{
256258884Semaste    struct ufs_args args;
257254721Semaste    pid_t child;
258288943Sdim    int waitstatus;
259276479Sdim
260254721Semaste    variable_set2(SYSTEM_STATE, "fixit");
261254721Semaste    memset(&args, 0, sizeof(args));
262254721Semaste    args.fspec = "/dev/fd0";
263254721Semaste    Mkdir("/mnt2");
264254721Semaste
265288943Sdim    while (1) {
266254721Semaste	msgConfirm("Please insert a writable fixit floppy and press return");
267258884Semaste	if (mount(MOUNT_UFS, "/mnt2", 0, (caddr_t)&args) != -1)
268258884Semaste	    break;
269258884Semaste	msgConfirm("An attempt to mount the fixit floppy failed, maybe the filesystem\n"
270258884Semaste		   "is unclean.  Trying a forcible mount as a last resort...");
271288943Sdim	if (mount(MOUNT_UFS, "/mnt2", MNT_FORCE, (caddr_t)&args) != -1)
272258884Semaste	    break;
273258884Semaste	if (msgYesNo("Unable to mount the fixit floppy - do you want to try again?") != 0)
274258884Semaste	    return DITEM_FAILURE;
275258884Semaste    }
276258884Semaste    dialog_clear();
277258884Semaste    end_dialog();
278288943Sdim    DialogActive = FALSE;
279258884Semaste    if (!directory_exists("/tmp"))
280254721Semaste	(void)symlink("/mnt2/tmp", "/tmp");
281254721Semaste    if (!directory_exists("/var/tmp/vi.recover")) {
282254721Semaste	if (DITEM_STATUS(Mkdir("/var/tmp/vi.recover")) != DITEM_SUCCESS) {
283254721Semaste	    msgConfirm("Warning:  Was unable to create a /var/tmp/vi.recover directory.\n"
284254721Semaste		       "vi will kvetch and moan about it as a result but should still\n"
285254721Semaste		       "be essentially usable.");
286288943Sdim	}
287254721Semaste    }
288254721Semaste    if (!directory_exists("/bin"))
289288943Sdim	(void)Mkdir("/bin");
290288943Sdim    (void)symlink("/stand/sh", "/bin/sh");
291254721Semaste    /* Link the /etc/ files */
292288943Sdim    if (DITEM_STATUS(Mkdir("/etc")) != DITEM_SUCCESS)
293254721Semaste	msgConfirm("Unable to create an /etc directory!  Things are weird on this floppy..");
294254721Semaste    else if ((symlink("/mnt2/etc/spwd.db", "/etc/spwd.db") == -1 && errno != EEXIST) ||
295288943Sdim	     (symlink("/mnt2/etc/protocols", "/etc/protocols") == -1 && errno != EEXIST) ||
296288943Sdim	     (symlink("/mnt2/etc/services", "/etc/services") == -1 && errno != EEXIST))
297254721Semaste	msgConfirm("Couldn't symlink the /etc/ files!  I'm not sure I like this..");
298288943Sdim    if (!file_readable(TERMCAP_FILE))
299288943Sdim	create_termcap();
300254721Semaste    if (!(child = fork())) {
301288943Sdim	struct termios foo;
302254721Semaste
303258054Semaste	signal(SIGTTOU, SIG_IGN);
304258054Semaste	if (tcgetattr(0, &foo) != -1) {
305258054Semaste	    foo.c_cc[VERASE] = '\010';
306254721Semaste	    if (tcsetattr(0, TCSANOW, &foo) == -1)
307288943Sdim		msgDebug("fixit shell: Unable to set erase character.\n");
308254721Semaste	}
309254721Semaste	else
310262528Semaste	    msgDebug("fixit shell: Unable to get terminal attributes!\n");
311262528Semaste	setenv("PATH", "/bin:/sbin:/usr/bin:/usr/sbin:/stand:/mnt2/stand", 1);
312262528Semaste	/* use the .profile from the fixit floppy */
313254721Semaste	setenv("HOME", "/mnt2", 1);
314254721Semaste	chdir("/mnt2");
315254721Semaste	execlp("sh", "-sh", 0);
316254721Semaste	msgDebug("fixit shell: Failed to execute shell!\n");
317258054Semaste	return -1;
318258054Semaste    }
319258054Semaste    else
320276479Sdim	(void)waitpid(child, &waitstatus, 0);
321276479Sdim
322276479Sdim    DialogActive = TRUE;
323254721Semaste    clear();
324254721Semaste    dialog_clear();
325254721Semaste    unmount("/mnt2", MNT_FORCE);
326254721Semaste    msgConfirm("Please remove the fixit floppy now.");
327254721Semaste    return DITEM_SUCCESS;
328254721Semaste}
329288943Sdim
330254721Semasteint
331254721SemasteinstallExpress(dialogMenuItem *self)
332254721Semaste{
333288943Sdim    int i;
334288943Sdim
335288943Sdim    variable_set2(SYSTEM_STATE, "express");
336254721Semaste    if (DITEM_STATUS((i = diskPartitionEditor(self))) == DITEM_FAILURE)
337254721Semaste	return i;
338254721Semaste
339254721Semaste    if (DITEM_STATUS((i = diskLabelEditor(self))) == DITEM_FAILURE)
340254721Semaste	return i;
341254721Semaste
342254721Semaste    if (!Dists) {
343254721Semaste	dialog_clear_norefresh();
344254721Semaste	if (!dmenuOpenSimple(&MenuDistributions, FALSE) && !Dists)
345254721Semaste	    return DITEM_FAILURE | DITEM_RECREATE;
346254721Semaste    }
347254721Semaste
348288943Sdim    if (!mediaDevice) {
349254721Semaste	dialog_clear_norefresh();
350254721Semaste	if (!dmenuOpenSimple(&MenuMedia, FALSE) || !mediaDevice)
351254721Semaste	    return DITEM_FAILURE | DITEM_RECREATE;
352254721Semaste    }
353254721Semaste
354254721Semaste    if (DITEM_STATUS((i = installCommit(self))) == DITEM_SUCCESS) {
355254721Semaste	i |= DITEM_LEAVE_MENU;
356254721Semaste	/* Give user the option of one last configuration spree */
357254721Semaste	installConfigure();
358254721Semaste
359254721Semaste	/* Now write out any changes .. */
360254721Semaste	configResolv();
361288943Sdim	configSysconfig("/etc/sysconfig");
362254721Semaste    }
363254721Semaste    return i | DITEM_RECREATE;
364288943Sdim}
365288943Sdim
366254721Semaste/* Novice mode installation */
367254721Semasteint
368254721SemasteinstallNovice(dialogMenuItem *self)
369254721Semaste{
370254721Semaste    int i;
371254721Semaste
372254721Semaste    variable_set2(SYSTEM_STATE, "novice");
373254721Semaste    dialog_clear_norefresh();
374254721Semaste    msgConfirm("In the next menu, you will need to set up a DOS-style (\"fdisk\") partitioning\n"
375254721Semaste	       "scheme for your hard disk.  If you simply wish to devote all disk space\n"
376254721Semaste	       "to FreeBSD (overwritting anything else that might be on the disk(s) selected)\n"
377254721Semaste	       "then use the (A)ll command to select the default partitioning scheme followed\n"
378254721Semaste	       "by a (Q)uit.  If you wish to allocate only free space to FreeBSD, move to a\n"
379254721Semaste	       "partition marked \"unused\" and use the (C)reate command.");
380254721Semaste
381254721Semaste    if (DITEM_STATUS(diskPartitionEditor(self)) == DITEM_FAILURE)
382254721Semaste	return DITEM_FAILURE;
383254721Semaste
384254721Semaste    dialog_clear_norefresh();
385254721Semaste    msgConfirm("Next, you need to create BSD partitions inside of the fdisk partition(s)\n"
386254721Semaste	       "just created.  If you have a reasonable amount of disk space (200MB or more)\n"
387254721Semaste	       "and don't have any special requirements, simply use the (A)uto command to\n"
388254721Semaste	       "allocate space automatically.  If you have more specific needs or just don't\n"
389254721Semaste	       "care for the layout chosen by (A)uto, press F1 for more information on\n"
390254721Semaste	       "manual layout.");
391254721Semaste
392254721Semaste    if (DITEM_STATUS(diskLabelEditor(self)) == DITEM_FAILURE)
393254721Semaste	return DITEM_FAILURE;
394254721Semaste
395254721Semaste    while (1) {
396254721Semaste	dialog_clear_norefresh();
397254721Semaste	if (!dmenuOpenSimple(&MenuDistributions, FALSE) && !Dists)
398254721Semaste	    return DITEM_FAILURE | DITEM_RECREATE;
399254721Semaste
400254721Semaste	if (Dists || !msgYesNo("No distributions selected.  Are you sure you wish to continue?"))
401254721Semaste	    break;
402288943Sdim    }
403254721Semaste
404254721Semaste    if (!mediaDevice && !dmenuOpenSimple(&MenuMedia, FALSE))
405296417Sdim	return DITEM_FAILURE | DITEM_RECREATE;
406254721Semaste
407254721Semaste    if (DITEM_STATUS((i = installCommit(self))) == DITEM_FAILURE) {
408254721Semaste	dialog_clear_norefresh();
409254721Semaste	msgConfirm("Installation completed with some errors.  You may wish to\n"
410254721Semaste		   "scroll through the debugging messages on VTY1 with the\n"
411254721Semaste		   "scroll-lock feature.  You can also chose \"No\" at the next\n"
412288943Sdim		   "prompt and go back into the installation menus to try and retry\n"
413288943Sdim		   "whichever operations have failed.");
414288943Sdim	return i | DITEM_RECREATE;
415254721Semaste
416288943Sdim    }
417254721Semaste    else {
418254721Semaste	dialog_clear_norefresh();
419254721Semaste	msgConfirm("Congratulations!  You now have FreeBSD installed on your system.\n\n"
420254721Semaste		   "We will now move on to the final configuration questions.\n"
421254721Semaste		   "For any option you do not wish to configure, simply select\n"
422254721Semaste		   "No.\n\n"
423254721Semaste		   "If you wish to re-enter this utility after the system is up, you\n"
424254721Semaste		   "may do so by typing: /stand/sysinstall.");
425254721Semaste    }
426254721Semaste    if (mediaDevice->type != DEVICE_TYPE_FTP && mediaDevice->type != DEVICE_TYPE_NFS) {
427254721Semaste	if (!msgYesNo("Would you like to configure any SLIP/PPP or network interface devices?")) {
428254721Semaste	    Device *save = mediaDevice;
429254721Semaste
430254721Semaste	    /* This will also set the media device, which we don't want */
431254721Semaste	    tcpDeviceSelect();
432254721Semaste	    /* so we restore our saved value below */
433262528Semaste	    mediaDevice = save;
434276479Sdim	    dialog_clear_norefresh();
435276479Sdim	}
436276479Sdim    }
437262528Semaste
438262528Semaste    dialog_clear_norefresh();
439262528Semaste    if (!msgYesNo("Would you like to configure Samba for connecting NETBUI clients to this\n"
440262528Semaste		  "machine?  Windows 95, Windows NT and Windows for Workgroups\n"
441262528Semaste		  "machines can use NETBUI transport for disk and printer sharing."))
442262528Semaste	configSamba(self);
443262528Semaste
444262528Semaste    dialog_clear_norefresh();
445262528Semaste    if (!msgYesNo("Will this machine be an IP gateway (e.g. will it forward packets\n"
446288943Sdim		  "between interfaces)?"))
447288943Sdim	variable_set2("gateway", "YES");
448288943Sdim
449262528Semaste    dialog_clear_norefresh();
450262528Semaste    if (!msgYesNo("Do you want to allow anonymous FTP connections to this machine?"))
451288943Sdim	configAnonFTP(self);
452288943Sdim
453288943Sdim    dialog_clear_norefresh();
454288943Sdim    if (!msgYesNo("Do you want to configure this machine as an NFS server?"))
455254721Semaste	configNFSServer(self);
456254721Semaste
457254721Semaste    dialog_clear_norefresh();
458254721Semaste    if (!msgYesNo("Do you want to configure this machine as an NFS client?"))
459254721Semaste	variable_set2("nfs_client", "YES");
460254721Semaste
461254721Semaste    dialog_clear_norefresh();
462254721Semaste    if (!msgYesNo("Do you want to configure this machine as a WEB server?"))
463254721Semaste	configApache(self);
464254721Semaste
465254721Semaste    dialog_clear_norefresh();
466254721Semaste    if (!msgYesNo("Would you like to customize your system console settings?")) {
467254721Semaste	WINDOW *w = savescr();
468254721Semaste
469254721Semaste	dmenuOpenSimple(&MenuSyscons, FALSE);
470254721Semaste	restorescr(w);
471254721Semaste    }
472258884Semaste
473288943Sdim    dialog_clear_norefresh();
474258054Semaste    if (!msgYesNo("Would you like to set this machine's time zone now?")) {
475258884Semaste	WINDOW *w = savescr();
476288943Sdim
477258054Semaste	dialog_clear();
478258884Semaste	systemExecute("rm -f /etc/wall_cmos_clock /etc/localtime; tzsetup");
479288943Sdim	restorescr(w);
480258054Semaste    }
481288943Sdim
482288943Sdim    dialog_clear_norefresh();
483258054Semaste    if (!msgYesNo("Does this system have a mouse attached to it?")) {
484288943Sdim	WINDOW *w = savescr();
485288943Sdim
486258884Semaste	dmenuOpenSimple(&MenuMouse, FALSE);
487258884Semaste	restorescr(w);
488258054Semaste    }
489258054Semaste
490258054Semaste    if (directory_exists("/usr/X11R6")) {
491258054Semaste	dialog_clear_norefresh();
492288943Sdim	if (!msgYesNo("Would you like to configure your X server at this time?"))
493258054Semaste	    configXFree86(self);
494258884Semaste    }
495258054Semaste
496258054Semaste    dialog_clear_norefresh();
497258054Semaste    if (!msgYesNo("The FreeBSD package collection is a collection of over 600 ready-to-run\n"
498258054Semaste		  "applications, from text editors to games to WEB servers.  Would you like\n"
499288943Sdim		  "to browse the collection now?"))
500258054Semaste	configPackages(self);
501288943Sdim
502288943Sdim    /* XXX Put whatever other nice configuration questions you'd like to ask the user here XXX */
503288943Sdim
504258054Semaste    /* Give user the option of one last configuration spree */
505288943Sdim    installConfigure();
506288943Sdim
507258884Semaste    /* Now write out any changes .. */
508288943Sdim    configResolv();
509288943Sdim    configSysconfig("/etc/sysconfig");
510288943Sdim
511258884Semaste    return DITEM_LEAVE_MENU | DITEM_RECREATE;
512288943Sdim}
513258054Semaste
514288943Sdim/* The version of commit we call from the Install Custom menu */
515296417Sdimint
516288943SdiminstallCustomCommit(dialogMenuItem *self)
517296417Sdim{
518296417Sdim    int i;
519296417Sdim
520288943Sdim    i = installCommit(self);
521288943Sdim    if (DITEM_STATUS(i) == DITEM_SUCCESS) {
522258884Semaste	/* Give user the option of one last configuration spree */
523288943Sdim	installConfigure();
524258054Semaste
525254721Semaste	/* Now write out any changes .. */
526254721Semaste	configResolv();
527254721Semaste	configSysconfig("/etc/sysconfig");
528258054Semaste	return i;
529258884Semaste    }
530258884Semaste    else
531309124Sdim	msgConfirm("The commit operation completed with errors.  Not\n"
532258884Semaste		   "updating /etc files.");
533258884Semaste    return i;
534258884Semaste}
535258884Semaste
536258884Semaste/*
537258884Semaste * What happens when we finally decide to going ahead with the installation.
538258884Semaste *
539258884Semaste * This is broken into multiple stages so that the user can do a full
540258884Semaste * installation but come back here again to load more distributions,
541258884Semaste * perhaps from a different media type.  This would allow, for
542258884Semaste * example, the user to load the majority of the system from CDROM and
543276479Sdim * then use ftp to load just the DES dist.
544276479Sdim */
545276479Sdimint
546258884SemasteinstallCommit(dialogMenuItem *self)
547276479Sdim{
548276479Sdim    int i;
549276479Sdim    char *str;
550276479Sdim    Boolean need_bin = FALSE;
551276479Sdim
552276479Sdim    if (!mediaVerify())
553288943Sdim	return DITEM_FAILURE;
554288943Sdim
555288943Sdim    str = variable_get(SYSTEM_STATE);
556276479Sdim    if (isDebug())
557276479Sdim	msgDebug("installCommit: System state is `%s'\n", str);
558276479Sdim
559288943Sdim    if (RunningAsInit) {
560288943Sdim	/* Do things we wouldn't do to a multi-user system */
561288943Sdim	if (DITEM_STATUS((i = installInitial())) == DITEM_FAILURE)
562288943Sdim	    return i;
563288943Sdim	if (DITEM_STATUS((i = configFstab())) == DITEM_FAILURE)
564288943Sdim	    return i;
565288943Sdim    }
566288943Sdim
567288943Sdim    if (Dists & DIST_BIN)
568288943Sdim	need_bin = TRUE;
569288943Sdim    i = distExtractAll(self);
570288943Sdim    if (DITEM_STATUS(i) != DITEM_FAILURE || !need_bin || !(Dists & DIST_BIN))
571288943Sdim	i = installFixup(self);
572288943Sdim
573288943Sdim    variable_set2(SYSTEM_STATE, DITEM_STATUS(i) == DITEM_FAILURE ? "error-install" : "full-install");
574288943Sdim    return i | DITEM_RECREATE;
575288943Sdim}
576254721Semaste
577288943Sdimstatic void
578288943SdiminstallConfigure(void)
579288943Sdim{
580288943Sdim    /* Final menu of last resort */
581288943Sdim    dialog_clear_norefresh();
582288943Sdim    if (!msgYesNo("Visit the general configuration menu for a chance to set\n"
583288943Sdim		  "any last options?")) {
584288943Sdim	WINDOW *w = savescr();
585288943Sdim
586288943Sdim	dmenuOpenSimple(&MenuConfigure, FALSE);
587288943Sdim	restorescr(w);
588288943Sdim    }
589288943Sdim}
590288943Sdim
591288943Sdimint
592288943SdiminstallFixup(dialogMenuItem *self)
593288943Sdim{
594288943Sdim    Device **devs;
595288943Sdim    int i;
596288943Sdim
597288943Sdim    if (!file_readable("/kernel")) {
598288943Sdim	if (file_readable("/kernel.GENERIC")) {
599288943Sdim#ifdef SAVE_USERCONFIG
600288943Sdim	    /* Snapshot any boot -c changes back to the GENERIC kernel */
601288943Sdim	    if (!strcmp(variable_get(VAR_RELNAME), RELEASE_NAME))
602288943Sdim		save_userconfig_to_kernel("/kernel.GENERIC");
603288943Sdim#endif
604288943Sdim	    if (vsystem("cp -p /kernel.GENERIC /kernel")) {
605288943Sdim		msgConfirm("Unable to link /kernel into place!");
606288943Sdim		return DITEM_FAILURE;
607288943Sdim	    }
608262528Semaste	}
609254721Semaste	else {
610254721Semaste	    msgConfirm("Can't find a kernel image to link to on the root file system!\n"
611254721Semaste		       "You're going to have a hard time getting this system to\n"
612254721Semaste		       "boot from the hard disk, I'm afraid!");
613254721Semaste	    return DITEM_FAILURE;
614254721Semaste	}
615254721Semaste    }
616254721Semaste
617254721Semaste    /* Resurrect /dev after bin distribution screws it up */
618254721Semaste    if (RunningAsInit) {
619258054Semaste	msgNotify("Remaking all devices.. Please wait!");
620258054Semaste	if (vsystem("cd /dev; sh MAKEDEV all")) {
621288943Sdim	    msgConfirm("MAKEDEV returned non-zero status");
622288943Sdim	    return DITEM_FAILURE;
623296417Sdim	}
624296417Sdim
625288943Sdim	msgNotify("Resurrecting /dev entries for slices..");
626254721Semaste	devs = deviceFind(NULL, DEVICE_TYPE_DISK);
627276479Sdim	if (!devs)
628254721Semaste	    msgFatal("Couldn't get a disk device list!");
629254721Semaste
630254721Semaste	/* Resurrect the slices that the former clobbered */
631254721Semaste	for (i = 0; devs[i]; i++) {
632254721Semaste	    Disk *disk = (Disk *)devs[i]->private;
633254721Semaste	    Chunk *c1;
634254721Semaste
635309124Sdim	    if (!devs[i]->enabled)
636288943Sdim		continue;
637254721Semaste	    if (!disk->chunks)
638262528Semaste		msgFatal("No chunk list found for %s!", disk->name);
639254721Semaste	    for (c1 = disk->chunks->part; c1; c1 = c1->next) {
640254721Semaste		if (c1->type == freebsd) {
641254721Semaste		    msgNotify("Making slice entries for %s", c1->name);
642254721Semaste		    if (vsystem("cd /dev; sh MAKEDEV %sh", c1->name)) {
643254721Semaste			msgConfirm("Unable to make slice entries for %s!", c1->name);
644254721Semaste			return DITEM_FAILURE;
645288943Sdim		    }
646288943Sdim		}
647254721Semaste	    }
648254721Semaste	}
649254721Semaste	/* XXX Do all the last ugly work-arounds here which we'll try and excise someday right?? XXX */
650254721Semaste
651254721Semaste	msgNotify("Fixing permissions..");
652254721Semaste	/* BOGON #1:  XFree86 extracting /usr/X11R6 with root-only perms */
653276479Sdim	if (directory_exists("/usr/X11R6")) {
654276479Sdim	    vsystem("chmod -R a+r /usr/X11R6");
655258054Semaste	    vsystem("find /usr/X11R6 -type d | xargs chmod a+x");
656262528Semaste	}
657288943Sdim	/* BOGON #2: We leave /etc in a bad state */
658296417Sdim	chmod("/etc", 0755);
659296417Sdim
660296417Sdim	/* BOGON #3: No /var/db/mountdtab complains */
661296417Sdim	Mkdir("/var/db");
662296417Sdim	creat("/var/db/mountdtab", 0644);
663254721Semaste
664296417Sdim	/* Now run all the mtree stuff to fix things up */
665296417Sdim        vsystem("mtree -deU -f /etc/mtree/BSD.root.dist -p /");
666296417Sdim        vsystem("mtree -deU -f /etc/mtree/BSD.var.dist -p /var");
667296417Sdim        vsystem("mtree -deU -f /etc/mtree/BSD.usr.dist -p /usr");
668296417Sdim    }
669296417Sdim    return DITEM_SUCCESS;
670296417Sdim}
671296417Sdim
672296417Sdim/* Go newfs and/or mount all the filesystems we've been asked to */
673296417Sdimint
674296417SdiminstallFilesystems(dialogMenuItem *self)
675254721Semaste{
676288943Sdim    int i;
677296417Sdim    Disk *disk;
678254721Semaste    Chunk *c1, *c2, *rootdev, *swapdev, *usrdev, *vardev;
679254721Semaste    Device **devs;
680254721Semaste    PartInfo *root;
681254721Semaste    char dname[80], *str;
682288943Sdim    extern int MakeDevChunk(Chunk *c, char *n);
683288943Sdim    Boolean upgrade = FALSE;
684288943Sdim
685296417Sdim    /* If we've already done this, bail out */
686    if ((str = variable_get(DISK_LABELLED)) && !strcmp(str, "written"))
687	return DITEM_SUCCESS;
688
689    str = variable_get(SYSTEM_STATE);
690
691    if (!checkLabels(TRUE, &rootdev, &swapdev, &usrdev, &vardev))
692	return DITEM_FAILURE;
693
694    if (rootdev)
695	root = (PartInfo *)rootdev->private_data;
696    else
697	root = NULL;
698
699    command_clear();
700    upgrade = str && !strcmp(str, "upgrade");
701
702    if (swapdev) {
703	/* As the very first thing, try to get ourselves some swap space */
704	sprintf(dname, "/dev/%s", swapdev->name);
705	if (!Fake && (!MakeDevChunk(swapdev, "/dev") || !file_readable(dname))) {
706	    msgConfirm("Unable to make device node for %s in /dev!\n"
707		       "The creation of filesystems will be aborted.", dname);
708	    return DITEM_FAILURE;
709	}
710
711	if (!Fake) {
712	    if (!swapon(dname))
713		msgNotify("Added %s as initial swap device", dname);
714	    else
715		msgConfirm("WARNING!  Unable to swap to %s: %s\n"
716			   "This may cause the installation to fail at some point\n"
717			   "if you don't have a lot of memory.", dname, strerror(errno));
718	}
719    }
720
721    if (rootdev) {
722	/* Next, create and/or mount the root device */
723	sprintf(dname, "/dev/r%sa", rootdev->disk->name);
724	if (!Fake && (!MakeDevChunk(rootdev, "/dev") || !file_readable(dname))) {
725	    msgConfirm("Unable to make device node for %s in /dev!\n"
726		       "The creation of filesystems will be aborted.", dname);
727	    return DITEM_FAILURE;
728	}
729	if (strcmp(root->mountpoint, "/"))
730	    msgConfirm("Warning: %s is marked as a root partition but is mounted on %s", rootdev->name, root->mountpoint);
731
732	if (root->newfs) {
733	    int i;
734
735	    msgNotify("Making a new root filesystem on %s", dname);
736	    i = vsystem("%s %s", root->newfs_cmd, dname);
737	    if (i) {
738		msgConfirm("Unable to make new root filesystem on %s!\n"
739			   "Command returned status %d", dname, i);
740		return DITEM_FAILURE;
741	    }
742	}
743	else {
744	    if (!upgrade) {
745		msgConfirm("Warning:  Using existing root partition.  It will be assumed\n"
746			   "that you have the appropriate device entries already in /dev.");
747	    }
748	    msgNotify("Checking integrity of existing %s filesystem.", dname);
749	    i = vsystem("fsck -y %s", dname);
750	    if (i)
751		msgConfirm("Warning: fsck returned status of %d for %s.\n"
752			   "This partition may be unsafe to use.", i, dname);
753	}
754
755	/* Switch to block device */
756	sprintf(dname, "/dev/%sa", rootdev->disk->name);
757	if (Mount("/mnt", dname)) {
758	    msgConfirm("Unable to mount the root file system on %s!  Giving up.", dname);
759	    return DITEM_FAILURE;
760	}
761    }
762
763    /* Now buzz through the rest of the partitions and mount them too */
764    devs = deviceFind(NULL, DEVICE_TYPE_DISK);
765    for (i = 0; devs[i]; i++) {
766	if (!devs[i]->enabled)
767	    continue;
768
769	disk = (Disk *)devs[i]->private;
770	if (!disk->chunks) {
771	    msgConfirm("No chunk list found for %s!", disk->name);
772	    return DITEM_FAILURE;
773	}
774	if (root && (root->newfs || upgrade)) {
775	    Mkdir("/mnt/dev");
776	    if (!Fake)
777		MakeDevDisk(disk, "/mnt/dev");
778	}
779
780	for (c1 = disk->chunks->part; c1; c1 = c1->next) {
781	    if (c1->type == freebsd) {
782		for (c2 = c1->part; c2; c2 = c2->next) {
783		    if (c2->type == part && c2->subtype != FS_SWAP && c2->private_data) {
784			PartInfo *tmp = (PartInfo *)c2->private_data;
785
786			/* Already did root */
787			if (c2 == rootdev)
788			    continue;
789
790			if (tmp->newfs)
791			    command_shell_add(tmp->mountpoint, "%s /mnt/dev/r%s", tmp->newfs_cmd, c2->name);
792			else
793			    command_shell_add(tmp->mountpoint, "fsck -y /mnt/dev/r%s", c2->name);
794			command_func_add(tmp->mountpoint, Mount, c2->name);
795		    }
796		    else if (c2->type == part && c2->subtype == FS_SWAP) {
797			char fname[80];
798			int i;
799
800			if (c2 == swapdev)
801			    continue;
802			sprintf(fname, "/mnt/dev/%s", c2->name);
803			i = (Fake || swapon(fname));
804			if (!i)
805			    msgNotify("Added %s as an additional swap device", fname);
806			else
807			    msgConfirm("Unable to add %s as a swap device: %s", fname, strerror(errno));
808		    }
809		}
810	    }
811	    else if (c1->type == fat && c1->private_data && (root->newfs || upgrade)) {
812		char name[FILENAME_MAX];
813
814		sprintf(name, "/mnt%s", ((PartInfo *)c1->private_data)->mountpoint);
815		Mkdir(name);
816	    }
817	}
818    }
819
820    msgNotify("Copying initial device files..");
821    /* Copy the boot floppy's dev files */
822    if ((root->newfs || upgrade) && vsystem("find -x /dev | cpio %s -pdum /mnt", cpioVerbosity())) {
823	msgConfirm("Couldn't clone the /dev files!");
824	return DITEM_FAILURE;
825    }
826
827    command_sort();
828    command_execute();
829    return DITEM_SUCCESS;
830}
831
832/* Initialize various user-settable values to their defaults */
833int
834installVarDefaults(dialogMenuItem *self)
835{
836    char *cp;
837
838    /* Set default startup options */
839    variable_set2(VAR_ROUTER,			"routed");
840    variable_set2(VAR_RELNAME,			RELEASE_NAME);
841    variable_set2(VAR_CPIO_VERBOSITY,		"high");
842    variable_set2(VAR_TAPE_BLOCKSIZE,		DEFAULT_TAPE_BLOCKSIZE);
843    variable_set2(VAR_INSTALL_ROOT,		"/");
844    cp = getenv("EDITOR");
845    if (!cp)
846	cp = "/usr/bin/ee";
847    variable_set2(VAR_EDITOR,			cp);
848    variable_set2(VAR_FTP_USER,			"ftp");
849    variable_set2(VAR_BROWSER_PACKAGE,		PACKAGE_LYNX);
850    variable_set2(VAR_BROWSER_BINARY,		"/usr/local/bin/lynx");
851    variable_set2(VAR_FTP_STATE,		"passive");
852    variable_set2(VAR_PKG_TMPDIR,		"/usr/tmp");
853    if (getpid() != 1)
854	variable_set2(SYSTEM_STATE,		"update");
855    else
856	variable_set2(SYSTEM_STATE,		"init");
857    return DITEM_SUCCESS;
858}
859
860/* Copy the boot floppy contents into /stand */
861Boolean
862copySelf(void)
863{
864    int i;
865
866    msgWeHaveOutput("Copying the boot floppy to /stand on root filesystem");
867    i = vsystem("find -x /stand | cpio %s -pdum /mnt", cpioVerbosity());
868    if (i) {
869	msgConfirm("Copy returned error status of %d!", i);
870	return FALSE;
871    }
872
873    /* Copy the /etc files into their rightful place */
874    if (vsystem("cd /mnt/stand; find etc | cpio %s -pdum /mnt", cpioVerbosity())) {
875	msgConfirm("Couldn't copy up the /etc files!");
876	return TRUE;
877    }
878    return TRUE;
879}
880
881static void
882create_termcap(void)
883{
884    FILE *fp;
885
886    const char *caps[] = {
887	termcap_vt100, termcap_cons25, termcap_cons25_m, termcap_cons25r,
888	termcap_cons25r_m, termcap_cons25l1, termcap_cons25l1_m, NULL,
889    };
890    const char **cp;
891
892    if (!file_readable(TERMCAP_FILE)) {
893	Mkdir("/usr/share/misc");
894	fp = fopen(TERMCAP_FILE, "w");
895	if (!fp) {
896	    msgConfirm("Unable to initialize termcap file. Some screen-oriented\nutilities may not work.");
897	    return;
898	}
899	cp = caps;
900	while (*cp)
901	    fprintf(fp, "%s\n", *(cp++));
902	fclose(fp);
903    }
904}
905
906#ifdef SAVE_USERCONFIG
907static void
908save_userconfig_to_kernel(char *kern)
909{
910    struct kernel *core, *boot;
911    struct list *c_isa, *b_isa, *c_dev, *b_dev;
912    int i, d;
913
914    if ((core = uc_open("-incore")) == NULL) {
915	msgDebug("save_userconf: Can't read in-core information for kernel.\n");
916	return;
917    }
918
919    if ((boot = uc_open(kern)) == NULL) {
920	msgDebug("save_userconf: Can't read device information for kernel image %s\n", kern);
921	return;
922    }
923
924    msgNotify("Saving any boot -c changes to new kernel...");
925    c_isa = uc_getdev(core, "-isa");
926    b_isa = uc_getdev(boot, "-isa");
927    if (isDebug())
928	msgDebug("save_userconf: got %d ISA device entries from core, %d from boot.\n", c_isa->ac, b_isa->ac);
929    for (d = 0; d < c_isa->ac; d++) {
930	if (isDebug())
931	    msgDebug("save_userconf: ISA device loop, c_isa->av[%d] = %s\n", d, c_isa->av[d]);
932	if (strcmp(c_isa->av[d], "npx0")) { /* special case npx0, which mucks with its id_irq member */
933	    c_dev = uc_getdev(core, c_isa->av[d]);
934	    b_dev = uc_getdev(boot, b_isa->av[d]);
935	    if (!c_dev || !b_dev) {
936		msgDebug("save_userconf: c_dev: %x b_dev: %x\n", c_dev, b_dev);
937		continue;
938	    }
939	    if (isDebug())
940		msgDebug("save_userconf: ISA device %s: %d config parameters (core), %d (boot)\n",
941			 c_isa->av[d], c_dev->ac, b_dev->ac);
942	    for (i = 0; i < c_dev->ac; i++) {
943		if (isDebug())
944		    msgDebug("save_userconf: c_dev->av[%d] = %s, b_dev->av[%d] = %s\n", i, c_dev->av[i], i, b_dev->av[i]);
945		if (strcmp(c_dev->av[i], b_dev->av[i])) {
946		    if (isDebug())
947			msgDebug("save_userconf: %s (boot) -> %s (core)\n",
948				 c_dev->av[i], b_dev->av[i]);
949		    isa_setdev(boot, c_dev);
950		}
951	    }
952	}
953	else {
954	    if (isDebug())
955		msgDebug("skipping npx0\n");
956	}
957    }
958    if (isDebug())
959	msgDebug("Closing kernels\n");
960    uc_close(core, 0);
961    uc_close(boot, 1);
962}
963#endif
964