Initial import from FreeBSD RELENG_4:
[games.git] / release / sysinstall / install.c
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  * $FreeBSD: src/release/sysinstall/install.c,v 1.268.2.42 2003/02/22 21:16:47 ceri 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 #include <ufs/ufs/ufsmount.h>
48 #include <msdosfs/msdosfsmount.h>
49 #undef MSDOSFS
50 #include <sys/stat.h>
51 #include <sys/sysctl.h>
52 #include <unistd.h>
53 #include <termios.h>
54
55 /* Hack for rsaref package add, which displays interactive license.
56  * Used by package.c
57  */
58 int _interactiveHack;
59 int FixItMode = 0;
60
61 static void     create_termcap(void);
62 static void     fixit_common(void);
63
64 #define TERMCAP_FILE    "/usr/share/misc/termcap"
65
66 static void     installConfigure(void);
67
68 Boolean
69 checkLabels(Boolean whinge, Chunk **rdev, Chunk **sdev, Chunk **udev, Chunk **vdev, Chunk **tdev, Chunk **hdev)
70 {
71     Device **devs;
72     Boolean status;
73     Disk *disk;
74     Chunk *c1, *c2, *rootdev, *swapdev, *usrdev, *vardev, *tmpdev, *homedev;
75     int i;
76
77     /* Don't allow whinging if noWarn is set */
78     if (variable_get(VAR_NO_WARN))
79         whinge = FALSE;
80
81     status = TRUE;
82     if (rdev)
83         *rdev = NULL;
84     if (sdev)
85         *sdev = NULL;
86     if (udev)
87         *udev = NULL;
88     if (vdev)
89         *vdev = NULL;
90     if (tdev)
91         *tdev = NULL;
92     if (hdev)
93         *hdev = NULL;
94     rootdev = swapdev = usrdev = vardev = tmpdev = homedev = NULL;
95
96     /* We don't need to worry about root/usr/swap if we're already multiuser */
97     if (!RunningAsInit)
98         return status;
99
100     devs = deviceFind(NULL, DEVICE_TYPE_DISK);
101     /* First verify that we have a root device */
102     for (i = 0; devs[i]; i++) {
103         if (!devs[i]->enabled)
104             continue;
105         disk = (Disk *)devs[i]->private;
106         msgDebug("Scanning disk %s for root filesystem\n", disk->name);
107         if (!disk->chunks)
108             msgFatal("No chunk list found for %s!", disk->name);
109         for (c1 = disk->chunks->part; c1; c1 = c1->next) {
110             if (c1->type == freebsd) {
111                 for (c2 = c1->part; c2; c2 = c2->next) {
112                     if (c2->type == part && c2->subtype != FS_SWAP && c2->private_data) {
113                         if (!strcmp(((PartInfo *)c2->private_data)->mountpoint, "/")) {
114                             if (rootdev) {
115                                 if (whinge)
116                                     msgConfirm("WARNING:  You have more than one root device set?!\n"
117                                                "Using the first one found.");
118                                 continue;
119                             }
120                             else {
121                                 rootdev = c2;
122                                 if (isDebug())
123                                     msgDebug("Found rootdev at %s!\n", rootdev->name);
124                             }
125                         }
126                         else if (!strcmp(((PartInfo *)c2->private_data)->mountpoint, "/usr")) {
127                             if (usrdev) {
128                                 if (whinge)
129                                     msgConfirm("WARNING:  You have more than one /usr filesystem.\n"
130                                                "Using the first one found.");
131                                 continue;
132                             }
133                             else {
134                                 usrdev = c2;
135                                 if (isDebug())
136                                     msgDebug("Found usrdev at %s!\n", usrdev->name);
137                             }
138                         }
139                         else if (!strcmp(((PartInfo *)c2->private_data)->mountpoint, "/var")) {
140                             if (vardev) {
141                                 if (whinge)
142                                     msgConfirm("WARNING:  You have more than one /var filesystem.\n"
143                                                "Using the first one found.");
144                                 continue;
145                             }
146                             else {
147                                 vardev = c2;
148                                 if (isDebug())
149                                     msgDebug("Found vardev at %s!\n", vardev->name);
150                             }
151                         } else if (!strcmp(((PartInfo *)c2->private_data)->mountpoint, "/tmp")) {
152                             if (tmpdev) {
153                                 if (whinge)
154                                     msgConfirm("WARNING:  You have more than one /tmp filesystem.\n"
155                                                "Using the first one found.");
156                                 continue;
157                             }
158                             else {
159                                 tmpdev = c2;
160                                 if (isDebug())
161                                     msgDebug("Found tmpdev at %s!\n", tmpdev->name);
162                             }
163                         } else if (!strcmp(((PartInfo *)c2->private_data)->mountpoint, "/home")) {
164                             if (homedev) {
165                                 if (whinge)
166                                     msgConfirm("WARNING:  You have more than one /home filesystem.\n"
167                                                "Using the first one found.");
168                                 continue;
169                             }
170                             else {
171                                 homedev = c2;
172                                 if (isDebug())
173                                     msgDebug("Found homedev at %s!\n", homedev->name);
174                             }
175                         }
176                     }
177                 }
178             }
179         }
180     }
181
182     /* Now check for swap devices */
183     for (i = 0; devs[i]; i++) {
184         if (!devs[i]->enabled)
185             continue;
186         disk = (Disk *)devs[i]->private;
187         msgDebug("Scanning disk %s for swap partitions\n", disk->name);
188         if (!disk->chunks)
189             msgFatal("No chunk list found for %s!", disk->name);
190         for (c1 = disk->chunks->part; c1; c1 = c1->next) {
191             if (c1->type == freebsd) {
192                 for (c2 = c1->part; c2; c2 = c2->next) {
193                     if (c2->type == part && c2->subtype == FS_SWAP && !swapdev) {
194                         swapdev = c2;
195                         if (isDebug())
196                             msgDebug("Found swapdev at %s!\n", swapdev->name);
197                         break;
198                     }
199                 }
200             }
201         }
202     }
203
204     /* Copy our values over */
205     if (rdev)
206         *rdev = rootdev;
207     if (sdev)
208         *sdev = swapdev;
209     if (udev)
210         *udev = usrdev;
211     if (vdev)
212         *vdev = vardev;
213     if (tdev)
214         *tdev = tmpdev;
215     if (hdev)
216         *hdev = homedev;
217
218     if (!rootdev && whinge) {
219         msgConfirm("No root device found - you must label a partition as /\n"
220                    "in the label editor.");
221         status = FALSE;
222     }
223     if (!swapdev && whinge) {
224         msgConfirm("No swap devices found - you must create at least one\n"
225                    "swap partition.");
226         status = FALSE;
227     }
228     return status;
229 }
230
231 static int
232 installInitial(void)
233 {
234     static Boolean alreadyDone = FALSE;
235     int status = DITEM_SUCCESS;
236
237     if (alreadyDone)
238         return DITEM_SUCCESS;
239
240     if (!variable_get(DISK_LABELLED)) {
241         msgConfirm("You need to assign disk labels before you can proceed with\n"
242                    "the installation.");
243         return DITEM_FAILURE;
244     }
245     /* If it's labelled, assume it's also partitioned */
246     if (!variable_get(DISK_PARTITIONED))
247         variable_set2(DISK_PARTITIONED, "yes", 0);
248
249     /* If we refuse to proceed, bail. */
250     dialog_clear_norefresh();
251     if (!variable_get(VAR_NO_WARN)) {
252         if (msgYesNo(
253             "Last Chance!  Are you SURE you want continue the installation?\n\n"
254             "If you're running this on a disk with data you wish to save\n"
255             "then WE STRONGLY ENCOURAGE YOU TO MAKE PROPER BACKUPS before\n"
256             "proceeding!\n\n"
257             "We can take no responsibility for lost disk contents!") != 0)
258         return DITEM_FAILURE;
259     }
260
261     if (DITEM_STATUS(diskLabelCommit(NULL)) != DITEM_SUCCESS) {
262         msgConfirm("Couldn't make filesystems properly.  Aborting.");
263         return DITEM_FAILURE;
264     }
265
266     if (!copySelf()) {
267         msgConfirm("installInitial: Couldn't clone the boot floppy onto the\n"
268                    "root file system.  Aborting!");
269         return DITEM_FAILURE;
270     }
271
272     if (!Restarting && chroot("/mnt") == -1) {
273         msgConfirm("installInitial: Unable to chroot to %s - this is bad!",
274                    "/mnt");
275         return DITEM_FAILURE;
276     }
277
278     chdir("/");
279     variable_set2(RUNNING_ON_ROOT, "yes", 0);
280
281     /* Configure various files in /etc */
282     if (DITEM_STATUS(configResolv(NULL)) == DITEM_FAILURE)
283         status = DITEM_FAILURE;
284     if (DITEM_STATUS(configFstab(NULL)) == DITEM_FAILURE)
285         status = DITEM_FAILURE;
286
287     /* stick a helpful shell over on the 4th VTY */
288     if (!variable_get(VAR_NO_HOLOSHELL))
289         systemCreateHoloshell();
290
291     alreadyDone = TRUE;
292     return status;
293 }
294
295 int
296 installFixitHoloShell(dialogMenuItem *self)
297 {
298     FixItMode = 1;
299     systemCreateHoloshell();
300     return DITEM_SUCCESS;
301     FixItMode = 0;
302 }
303
304 int
305 installFixitCDROM(dialogMenuItem *self)
306 {
307     struct stat sb;
308
309     if (!RunningAsInit)
310         return DITEM_SUCCESS;
311
312     variable_set2(SYSTEM_STATE, "fixit", 0);
313     (void)unlink("/mnt2");
314     (void)rmdir("/mnt2");
315
316     while (1) {
317         msgConfirm("Please insert a FreeBSD live filesystem CD/DVD and press return");
318         if (DITEM_STATUS(mediaSetCDROM(NULL)) != DITEM_SUCCESS
319             || !DEVICE_INIT(mediaDevice)) {
320             /* If we can't initialize it, it's probably not a FreeBSD CDROM so punt on it */
321             mediaClose();
322             if (msgYesNo("Unable to mount the disc - do you want to try again?") != 0)
323                 return DITEM_FAILURE;
324         }
325         else
326             break;
327     }
328
329     /* Since the fixit code expects everything to be in /mnt2, and the CDROM mounting stuff /dist, do
330      * a little kludge dance here..
331      */
332     if (symlink("/dist", "/mnt2")) {
333         msgConfirm("Unable to symlink /mnt2 to the disc mount point.  Please report this\n"
334                    "unexpected failure to freebsd-bugs@FreeBSD.org.");
335         return DITEM_FAILURE;
336     }
337
338     /*
339      * If /tmp points to /mnt2/tmp from a previous fixit floppy session, it's
340      * not very good for us if we point it to the CDROM now.  Rather make it
341      * a directory in the root MFS then.  Experienced admins will still be
342      * able to mount their disk's /tmp over this if they need.
343      */
344     if (lstat("/tmp", &sb) == 0 && (sb.st_mode & S_IFMT) == S_IFLNK)
345         (void)unlink("/tmp");
346     Mkdir("/tmp");
347
348     /*
349      * Since setuid binaries ignore LD_LIBRARY_PATH, we indeed need the
350      * ld.so.hints file.  Fortunately, it's fairly small (~ 3 KB).
351      */
352     if (!file_readable("/var/run/ld.so.hints")) {
353         Mkdir("/var/run");
354         if (vsystem("/mnt2/sbin/ldconfig -s /mnt2/usr/lib")) {
355             msgConfirm("Warning: ldconfig could not create the ld.so hints file.\n"
356                        "Dynamic executables from the disc likely won't work.");
357         }
358     }
359
360     /* Yet more iggly hardcoded pathnames. */
361     Mkdir("/usr/libexec");
362     if (!file_readable("/usr/libexec/ld.so") && file_readable("/mnt2/usr/libexec/ld.so")) {
363         if (symlink("/mnt2/usr/libexec/ld.so", "/usr/libexec/ld.so"))
364             msgDebug("Couldn't link to ld.so - not necessarily a problem for ELF\n");
365     }
366     if (!file_readable("/usr/libexec/ld-elf.so.1")) {
367         if (symlink("/mnt2/usr/libexec/ld-elf.so.1", "/usr/libexec/ld-elf.so.1")) {
368             msgConfirm("Warning: could not create the symlink for ld-elf.so.1\n"
369                        "Dynamic executables from the disc likely won't work.");
370         }
371     }
372     /* optional nicety */
373     if (!file_readable("/usr/bin/vi"))
374         symlink("/mnt2/usr/bin/vi", "/usr/bin/vi");
375     fixit_common();
376     mediaClose();
377     msgConfirm("Please remove the FreeBSD fixit CDROM/DVD now.");
378     return DITEM_SUCCESS;
379 }
380
381 int
382 installFixitFloppy(dialogMenuItem *self)
383 {
384     struct ufs_args args;
385     extern char *distWanted;
386
387     if (!RunningAsInit)
388         return DITEM_SUCCESS;
389
390     /* Try to open the floppy drive */
391     if (DITEM_STATUS(mediaSetFloppy(NULL)) == DITEM_FAILURE || !mediaDevice) {
392         msgConfirm("Unable to set media device to floppy.");
393         mediaClose();
394         return DITEM_FAILURE;
395     }
396
397     memset(&args, 0, sizeof(args));
398     args.fspec = mediaDevice->devname;
399     mediaDevice->private = "/mnt2";
400     distWanted = NULL;
401     Mkdir("/mnt2");
402
403     variable_set2(SYSTEM_STATE, "fixit", 0);
404
405     while (1) {
406         if (!DEVICE_INIT(mediaDevice)) {
407             if (msgYesNo("The attempt to mount the fixit floppy failed, bad floppy\n"
408                          "or unclean filesystem.  Do you want to try again?"))
409                 return DITEM_FAILURE;
410         }
411         else
412             break;
413     }
414     if (!directory_exists("/tmp"))
415         (void)symlink("/mnt2/tmp", "/tmp");
416     fixit_common();
417     mediaClose();
418     msgConfirm("Please remove the fixit floppy now.");
419     return DITEM_SUCCESS;
420 }
421
422 /*
423  * The common code for both fixit variants.
424  */
425 static void
426 fixit_common(void)
427 {
428     pid_t child;
429     int waitstatus;
430
431     if (!directory_exists("/var/tmp/vi.recover")) {
432         if (DITEM_STATUS(Mkdir("/var/tmp/vi.recover")) != DITEM_SUCCESS) {
433             msgConfirm("Warning:  Was unable to create a /var/tmp/vi.recover directory.\n"
434                        "vi will kvetch and moan about it as a result but should still\n"
435                        "be essentially usable.");
436         }
437     }
438     if (!directory_exists("/bin"))
439         (void)Mkdir("/bin");
440     (void)symlink("/stand/sh", "/bin/sh");
441     /* Link the /etc/ files */
442     if (DITEM_STATUS(Mkdir("/etc")) != DITEM_SUCCESS)
443         msgConfirm("Unable to create an /etc directory!  Things are weird on this floppy..");
444     else if ((symlink("/mnt2/etc/spwd.db", "/etc/spwd.db") == -1 && errno != EEXIST) ||
445              (symlink("/mnt2/etc/protocols", "/etc/protocols") == -1 && errno != EEXIST) ||
446              (symlink("/mnt2/etc/group", "/etc/group") == -1 && errno != EEXIST) ||
447              (symlink("/mnt2/etc/services", "/etc/services") == -1 && errno != EEXIST))
448         msgConfirm("Couldn't symlink the /etc/ files!  I'm not sure I like this..");
449     if (!file_readable(TERMCAP_FILE))
450         create_termcap();
451     if (strcmp(variable_get(VAR_FIXIT_TTY), "serial") == 0) 
452         systemSuspendDialog();  /* must be before the fork() */
453     if (!(child = fork())) {
454         int i, fd;
455         struct termios foo;
456         extern int login_tty(int);
457
458         ioctl(0, TIOCNOTTY, NULL);
459         for (i = getdtablesize(); i >= 0; --i)
460             close(i);
461
462         if (strcmp(variable_get(VAR_FIXIT_TTY), "serial") == 0) 
463             fd = open("/dev/console", O_RDWR);
464         else
465             fd = open("/dev/ttyv3", O_RDWR);
466         ioctl(0, TIOCSCTTY, &fd);
467         dup2(0, 1);
468         dup2(0, 2);
469         DebugFD = 2;
470         if (login_tty(fd) == -1)
471             msgDebug("fixit: I can't set the controlling terminal.\n");
472
473         signal(SIGTTOU, SIG_IGN);
474         if (tcgetattr(0, &foo) != -1) {
475             foo.c_cc[VERASE] = '\010';
476             if (tcsetattr(0, TCSANOW, &foo) == -1)
477                 msgDebug("fixit shell: Unable to set erase character.\n");
478         }
479         else
480             msgDebug("fixit shell: Unable to get terminal attributes!\n");
481         setenv("PATH", "/bin:/sbin:/usr/bin:/usr/sbin:/stand:"
482                "/mnt2/stand:/mnt2/bin:/mnt2/sbin:/mnt2/usr/bin:/mnt2/usr/sbin", 1);
483         setenv("MAKEDEVPATH", "/sbin:/bin:/stand:"
484                "/mnt2/sbin:/mnt2/bin:/mnt2/stand", 1);
485         if (strcmp(variable_get(VAR_FIXIT_TTY), "serial") == 0) {
486             printf("Waiting for fixit shell to exit.\n"
487                 "When you are done, type ``exit'' to exit\n"
488                 "the fixit shell and be returned here.\n\n");
489             fflush(stdout);
490         }
491
492         /* use the .profile from the fixit medium */
493         setenv("HOME", "/mnt2", 1);
494         chdir("/mnt2");
495         execlp("sh", "-sh", (char *)0);
496         msgDebug("fixit shell: Failed to execute shell!\n");
497         _exit(1);;
498     }
499     else {
500         if (strcmp(variable_get(VAR_FIXIT_TTY), "standard") == 0) {
501             dialog_clear_norefresh();
502             msgNotify("Waiting for fixit shell to exit.  Go to VTY4 now by\n"
503                 "typing ALT-F4.  When you are done, type ``exit'' to exit\n"
504                 "the fixit shell and be returned here\n.");
505         }
506         (void)waitpid(child, &waitstatus, 0);
507         if (strcmp(variable_get(VAR_FIXIT_TTY), "serial") == 0)
508             systemResumeDialog();
509     }
510     dialog_clear();
511 }
512
513
514 int
515 installExpress(dialogMenuItem *self)
516 {
517     int i;
518
519     dialog_clear_norefresh();
520     variable_set2(SYSTEM_STATE, "express", 0);
521 #ifndef __alpha__
522     if (DITEM_STATUS((i = diskPartitionEditor(self))) == DITEM_FAILURE)
523         return i;
524 #endif
525     
526     if (DITEM_STATUS((i = diskLabelEditor(self))) == DITEM_FAILURE)
527         return i;
528
529     if (DITEM_STATUS((i = installCommit(self))) == DITEM_SUCCESS) {
530         i |= DITEM_LEAVE_MENU;
531         /* Set default security level */
532         configSecurityModerate(NULL);
533
534         /* Give user the option of one last configuration spree */
535         installConfigure();
536     }
537     return i;
538 }
539
540 /* Standard mode installation */
541 int
542 installStandard(dialogMenuItem *self)
543 {
544     int i, tries = 0;
545     Device **devs;
546
547     variable_set2(SYSTEM_STATE, "standard", 0);
548     dialog_clear_norefresh();
549 #ifndef __alpha__
550     msgConfirm("In the next menu, you will need to set up a DOS-style (\"fdisk\") partitioning\n"
551                "scheme for your hard disk.  If you simply wish to devote all disk space\n"
552                "to FreeBSD (overwriting anything else that might be on the disk(s) selected)\n"
553                "then use the (A)ll command to select the default partitioning scheme followed\n"
554                "by a (Q)uit.  If you wish to allocate only free space to FreeBSD, move to a\n"
555                "partition marked \"unused\" and use the (C)reate command.");
556
557 nodisks:
558     if (DITEM_STATUS(diskPartitionEditor(self)) == DITEM_FAILURE)
559         return DITEM_FAILURE;
560
561     if (diskGetSelectCount(&devs) <= 0 && tries < 3) {
562         msgConfirm("You need to select some disks to operate on!  Be sure to use SPACE\n"
563                    "instead of RETURN in the disk selection menu when selecting a disk.");
564         ++tries;
565         goto nodisks;
566     }
567 #endif
568
569 #ifdef __alpha__
570     msgConfirm("Now you need to create BSD partitions on the disk which you are\n"
571                "installing to.  If you have a reasonable amount of disk space (200MB or more)\n"
572                "and don't have any special requirements, simply use the (A)uto command to\n"
573                "allocate space automatically.  If you have more specific needs or just don't\n"
574                "care for the layout chosen by (A)uto, press F1 for more information on\n"
575                "manual layout.");
576 #else
577     msgConfirm("Now you need to create BSD partitions inside of the fdisk partition(s)\n"
578                "just created.  If you have a reasonable amount of disk space (200MB or more)\n"
579                "and don't have any special requirements, simply use the (A)uto command to\n"
580                "allocate space automatically.  If you have more specific needs or just don't\n"
581                "care for the layout chosen by (A)uto, press F1 for more information on\n"
582                "manual layout.");
583 #endif
584
585     if (DITEM_STATUS(diskLabelEditor(self)) == DITEM_FAILURE)
586         return DITEM_FAILURE;
587
588     if (DITEM_STATUS((i = installCommit(self))) == DITEM_FAILURE) {
589         dialog_clear();
590         msgConfirm("Installation completed with some errors.  You may wish to\n"
591                    "scroll through the debugging messages on VTY1 with the\n"
592                    "scroll-lock feature.  You can also choose \"No\" at the next\n"
593                    "prompt and go back into the installation menus to retry\n"
594                    "whichever operations have failed.");
595         return i;
596
597     }
598     else {
599         dialog_clear();
600         msgConfirm("Congratulations!  You now have FreeBSD installed on your system.\n\n"
601                    "We will now move on to the final configuration questions.\n"
602                    "For any option you do not wish to configure, simply select\n"
603                    "No.\n\n"
604                    "If you wish to re-enter this utility after the system is up, you\n"
605                    "may do so by typing: /stand/sysinstall.");
606     }
607     if (mediaDevice->type != DEVICE_TYPE_FTP && mediaDevice->type != DEVICE_TYPE_NFS) {
608         if (!msgYesNo("Would you like to configure any Ethernet or SLIP/PPP network devices?")) {
609             Device *tmp = tcpDeviceSelect();
610
611             if (tmp && !((DevInfo *)tmp->private)->use_dhcp && !msgYesNo("Would you like to bring the %s interface up right now?", tmp->name))
612                 if (!DEVICE_INIT(tmp))
613                     msgConfirm("Initialization of %s device failed.", tmp->name);
614         }
615         dialog_clear_norefresh();
616     }
617
618     if (!msgNoYes("Do you want this machine to function as a network gateway?"))
619         variable_set2("gateway_enable", "YES", 1);
620
621     dialog_clear_norefresh();
622     if (!msgNoYes("Do you want to configure inetd and the network services that it provides?"))
623         configInetd(self);
624
625     dialog_clear_norefresh();
626     if (!msgNoYes("Do you want to have anonymous FTP access to this machine?"))
627         configAnonFTP(self);
628
629     dialog_clear_norefresh();
630     if (!msgNoYes("Do you want to configure this machine as an NFS server?"))
631         configNFSServer(self);
632
633     dialog_clear_norefresh();
634     if (!msgNoYes("Do you want to configure this machine as an NFS client?"))
635         variable_set2("nfs_client_enable", "YES", 1);
636
637     if (!msgNoYes("Do you want to select a default security profile for\n"
638                  "this host (select No for \"moderate\" security)?"))
639         configSecurityProfile(self);
640     else
641         configSecurityModerate(self);
642
643     dialog_clear_norefresh();
644     if (!msgNoYes("Would you like to customize your system console settings?"))
645         dmenuOpenSimple(&MenuSyscons, FALSE);
646
647     dialog_clear_norefresh();
648     if (!msgYesNo("Would you like to set this machine's time zone now?"))
649         systemExecute("tzsetup");
650
651 #ifdef __i386__
652     dialog_clear_norefresh();
653     if (!msgYesNo("Would you like to enable Linux binary compatibility?"))
654         (void)configLinux(self);
655 #endif
656
657     dialog_clear_norefresh();
658     if (!msgNoYes("Does this system have a non-USB mouse attached to it?"))
659         dmenuOpenSimple(&MenuMouse, FALSE);
660
661     /* Now would be a good time to checkpoint the configuration data */
662     configRC_conf();
663     sync();
664
665     if (directory_exists("/usr/X11R6")) {
666         dialog_clear_norefresh();
667         if (!msgYesNo("Would you like to configure your X server at this time?"))
668             (void)configXSetup(self);
669     }
670
671     dialog_clear_norefresh();
672     if (!msgYesNo("The FreeBSD package collection is a collection of thousands of ready-to-run\n"
673                   "applications, from text editors to games to WEB servers and more.  Would you\n"
674                   "like to browse the collection now?")) {
675         (void)configPackages(self);
676     }
677
678     if (!msgYesNo("Would you like to add any initial user accounts to the system?\n"
679                   "Adding at least one account for yourself at this stage is suggested\n"
680                   "since working as the \"root\" user is dangerous (it is easy to do\n"
681                   "things which adversely affect the entire system)."))
682         (void)configUsers(self);
683
684     msgConfirm("Now you must set the system manager's password.\n"
685                "This is the password you'll use to log in as \"root\".");
686     if (!systemExecute("passwd root"))
687         variable_set2("root_password", "YES", 0);
688
689     /* XXX Put whatever other nice configuration questions you'd like to ask the user here XXX */
690
691     /* Give user the option of one last configuration spree */
692     dialog_clear_norefresh();
693     installConfigure();
694     return DITEM_LEAVE_MENU;
695 }
696
697 /* The version of commit we call from the Install Custom menu */
698 int
699 installCustomCommit(dialogMenuItem *self)
700 {
701     int i;
702
703     i = installCommit(self);
704     if (DITEM_STATUS(i) == DITEM_SUCCESS) {
705         /* Set default security level */
706         configSecurityModerate(NULL);
707
708         /* Give user the option of one last configuration spree */
709         installConfigure();
710         return i;
711     }
712     else
713         msgConfirm("The commit operation completed with errors.  Not\n"
714                    "updating /etc files.");
715     return i;
716 }
717
718 /*
719  * What happens when we finally decide to going ahead with the installation.
720  *
721  * This is broken into multiple stages so that the user can do a full
722  * installation but come back here again to load more distributions,
723  * perhaps from a different media type.  This would allow, for
724  * example, the user to load the majority of the system from CDROM and
725  * then use ftp to load just the CRYPTO dist.
726  */
727 int
728 installCommit(dialogMenuItem *self)
729 {
730     int i;
731     char *str;
732
733     dialog_clear_norefresh();
734     if (!Dists)
735         distConfig(NULL);
736
737     if (!Dists) {
738         (void)dmenuOpenSimple(&MenuDistributions, FALSE);
739         /* select reasonable defaults if necessary */
740         if (!Dists)
741             Dists = _DIST_USER;
742     }
743
744     if (!mediaVerify())
745         return DITEM_FAILURE;
746
747     str = variable_get(SYSTEM_STATE);
748     if (isDebug())
749         msgDebug("installCommit: System state is `%s'\n", str);
750
751     /* Installation stuff we wouldn't do to a running system */
752     if (RunningAsInit && DITEM_STATUS((i = installInitial())) == DITEM_FAILURE)
753         return i;
754
755 try_media:
756     if (!DEVICE_INIT(mediaDevice)) {
757         if (!msgYesNo("Unable to initialize selected media. Would you like to\n"
758                       "adjust your media configuration and try again?")) {
759             mediaDevice = NULL;
760             if (!mediaVerify())
761                 return DITEM_FAILURE;
762             else
763                 goto try_media;
764         }
765         else
766             return DITEM_FAILURE;
767     }
768
769     /* Now go get it all */
770     i = distExtractAll(self);
771
772     /* When running as init, *now* it's safe to grab the rc.foo vars */
773     installEnvironment();
774
775     variable_set2(SYSTEM_STATE, DITEM_STATUS(i) == DITEM_FAILURE ? "error-install" : "full-install", 0);
776
777     return i;
778 }
779
780 static void
781 installConfigure(void)
782 {
783     /* Final menu of last resort */
784     if (!msgNoYes("Visit the general configuration menu for a chance to set\n"
785                   "any last options?"))
786         dmenuOpenSimple(&MenuConfigure, FALSE);
787     configRC_conf();
788     sync();
789 }
790
791 int
792 installFixupBin(dialogMenuItem *self)
793 {
794     Device **devs;
795     char *cp;
796     int i;
797     FILE *fp;
798     int kstat = 1;
799
800     /* All of this is done only as init, just to be safe */
801     if (RunningAsInit) {
802         /* Fix up kernel first */
803         if (!file_readable("/kernel")) {
804             char *generic_kernel = "/kernel.GENERIC";
805             if (file_readable(generic_kernel)) {
806                 if (vsystem("cp -p %s /kernel", generic_kernel)) {
807                     msgConfirm("Unable to copy /kernel into place!");
808                     return DITEM_FAILURE;
809                 }
810             }
811         }
812 #ifdef __i386__
813         /* Snapshot any boot -c changes back to the new kernel */
814         cp = variable_get(VAR_KGET);
815         if (cp && (*cp == 'Y' || *cp == 'y')) {
816             if ((kstat = kget("/boot/kernel.conf")) != NULL) {
817                 msgConfirm("Unable to save boot -c changes to new kernel,\n"
818                            "please see the debug screen (ALT-F2) for details.");
819             }
820         }
821         if ((fp = fopen("/boot/loader.conf", "a")) != NULL) {
822             if (!kstat || !OnVTY)
823                 fprintf(fp, "# -- sysinstall generated deltas -- #\n");
824             if (!kstat)
825                 fprintf(fp, "userconfig_script_load=\"YES\"\n");
826             if (!OnVTY)
827                 fprintf(fp, "console=\"comconsole\"\n");
828             fclose(fp);
829         }
830 #endif
831         /* BOGON #1: Resurrect /dev after bin distribution screws it up */
832         dialog_clear_norefresh();
833         msgNotify("Remaking all devices.. Please wait!");
834         if (vsystem("cd /dev; sh MAKEDEV all")) {
835             msgConfirm("MAKEDEV returned non-zero status");
836             return DITEM_FAILURE | DITEM_RESTORE;
837         }
838
839         dialog_clear_norefresh();
840         msgNotify("Resurrecting /dev entries for slices..");
841         devs = deviceFind(NULL, DEVICE_TYPE_DISK);
842         if (!devs)
843             msgFatal("Couldn't get a disk device list!");
844
845         /* Resurrect the slices that the former clobbered */
846         for (i = 0; devs[i]; i++) {
847             Disk *disk = (Disk *)devs[i]->private;
848             Chunk *c1;
849
850             if (!devs[i]->enabled)
851                 continue;
852             if (!disk->chunks)
853                 msgFatal("No chunk list found for %s!", disk->name);
854             for (c1 = disk->chunks->part; c1; c1 = c1->next) {
855                 if (c1->type == freebsd) {
856                     dialog_clear_norefresh();
857                     msgNotify("Making slice entries for %s", c1->name);
858                     if (vsystem("cd /dev; sh MAKEDEV %sh", c1->name)) {
859                         msgConfirm("Unable to make slice entries for %s!", c1->name);
860                         return DITEM_FAILURE | DITEM_RESTORE;
861                     }
862                 }
863             }
864         }
865         
866         /* BOGON #2: We leave /etc in a bad state */
867         chmod("/etc", 0755);
868         
869         /* BOGON #3: No /var/db/mountdtab complains */
870         Mkdir("/var/db");
871         creat("/var/db/mountdtab", 0644);
872         
873         /* BOGON #4: /compat created by default in root fs */
874         Mkdir("/usr/compat");
875         vsystem("ln -s usr/compat /compat");
876
877         /* BOGON #5: aliases database not build for bin */
878         vsystem("newaliases");
879
880         /* Now run all the mtree stuff to fix things up */
881         vsystem("mtree -deU -f /etc/mtree/BSD.root.dist -p /");
882         vsystem("mtree -deU -f /etc/mtree/BSD.var.dist -p /var");
883         vsystem("mtree -deU -f /etc/mtree/BSD.usr.dist -p /usr");
884
885         /* Do all the last ugly work-arounds here */
886     }
887     return DITEM_SUCCESS | DITEM_RESTORE;
888 }
889
890 #ifdef X_AS_PKG
891 int
892 installX11package(dialogMenuItem *self)
893 {
894     WINDOW *w = savescr();
895     int i;
896
897     dialog_clear_norefresh();
898     msgNotify("Installing XFree86 package...");
899     i = package_add("XFree86-4");
900     restorescr(w);
901     return i;
902 }
903 #endif
904
905 /* Fix side-effects from the the XFree86 installation */
906 int
907 installFixupXFree(dialogMenuItem *self)
908 {
909     /* BOGON #1:  XFree86 requires various specialized fixups */
910     if (directory_exists("/usr/X11R6")) {
911         dialog_clear_norefresh();
912         msgNotify("Fixing permissions in XFree86 tree..");
913         vsystem("chmod -R a+r /usr/X11R6");
914         vsystem("find /usr/X11R6 -type d | xargs chmod a+x");
915
916 #ifndef X_AS_PKG
917         /* Also do bogus minimal package registration so ports don't whine */
918         if (file_readable("/usr/X11R6/lib/X11/pkgreg.tar.gz")) {
919             dialog_clear_norefresh();
920             msgNotify("Installing package metainfo..");
921             vsystem("tar xpzf /usr/X11R6/lib/X11/pkgreg.tar.gz -C / && rm /usr/X11R6/lib/X11/pkgreg.tar.gz");
922         }
923 #endif
924     }
925     return DITEM_SUCCESS | DITEM_RESTORE;
926 }
927
928 /* Go newfs and/or mount all the filesystems we've been asked to */
929 int
930 installFilesystems(dialogMenuItem *self)
931 {
932     int i;
933     Disk *disk;
934     Chunk *c1, *c2, *rootdev, *swapdev;
935     Device **devs;
936     PartInfo *root;
937     char dname[80];
938     extern int MakeDevChunk(Chunk *c, char *n);
939     Boolean upgrade = FALSE;
940
941     /* If we've already done this, bail out */
942     if (!variable_cmp(DISK_LABELLED, "written"))
943         return DITEM_SUCCESS;
944
945     upgrade = !variable_cmp(SYSTEM_STATE, "upgrade");
946     if (!checkLabels(TRUE, &rootdev, &swapdev, NULL, NULL, NULL, NULL))
947         return DITEM_FAILURE;
948
949     if (rootdev)
950         root = (PartInfo *)rootdev->private_data;
951     else
952         root = NULL;
953
954     command_clear();
955     if (swapdev && RunningAsInit) {
956         /* As the very first thing, try to get ourselves some swap space */
957         sprintf(dname, "/dev/%s", swapdev->name);
958         if (!Fake && (!MakeDevChunk(swapdev, "/dev") || !file_readable(dname))) {
959             msgConfirm("Unable to make device node for %s in /dev!\n"
960                        "The creation of filesystems will be aborted.", dname);
961             return DITEM_FAILURE;
962         }
963
964         if (!Fake) {
965             if (!swapon(dname)) {
966                 dialog_clear_norefresh();
967                 msgNotify("Added %s as initial swap device", dname);
968             }
969             else {
970                 msgConfirm("WARNING!  Unable to swap to %s: %s\n"
971                            "This may cause the installation to fail at some point\n"
972                            "if you don't have a lot of memory.", dname, strerror(errno));
973             }
974         }
975     }
976
977     if (rootdev && RunningAsInit) {
978         /* Next, create and/or mount the root device */
979         sprintf(dname, "/dev/%s", rootdev->name);
980         if (!Fake && (!MakeDevChunk(rootdev, "/dev") || !file_readable(dname))) {
981             msgConfirm("Unable to make device node for %s in /dev!\n"
982                        "The creation of filesystems will be aborted.", dname);
983             return DITEM_FAILURE | DITEM_RESTORE;
984         }
985         if (strcmp(root->mountpoint, "/"))
986             msgConfirm("Warning: %s is marked as a root partition but is mounted on %s", rootdev->name, root->mountpoint);
987
988         if (root->newfs && (!upgrade || !msgNoYes("You are upgrading - are you SURE you want to newfs the root partition?"))) {
989             int i;
990
991             dialog_clear_norefresh();
992             msgNotify("Making a new root filesystem on %s", dname);
993             i = vsystem("%s %s", root->newfs_cmd, dname);
994             if (i) {
995                 msgConfirm("Unable to make new root filesystem on %s!\n"
996                            "Command returned status %d", dname, i);
997                 return DITEM_FAILURE | DITEM_RESTORE;
998             }
999         }
1000         else {
1001             if (!upgrade) {
1002                 msgConfirm("Warning:  Using existing root partition.  It will be assumed\n"
1003                            "that you have the appropriate device entries already in /dev.");
1004             }
1005             dialog_clear_norefresh();
1006             msgNotify("Checking integrity of existing %s filesystem.", dname);
1007             i = vsystem("fsck -y %s", dname);
1008             if (i)
1009                 msgConfirm("Warning: fsck returned status of %d for %s.\n"
1010                            "This partition may be unsafe to use.", i, dname);
1011         }
1012         if (root->soft) {
1013             i = vsystem("tunefs -n enable %s", dname);
1014             if (i)
1015                 msgConfirm("Warning:  Unable to enable softupdates for root filesystem on %s", dname);
1016         }
1017
1018         /* Switch to block device */
1019         sprintf(dname, "/dev/%s", rootdev->name);
1020         if (Mount("/mnt", dname)) {
1021             msgConfirm("Unable to mount the root file system on %s!  Giving up.", dname);
1022             return DITEM_FAILURE | DITEM_RESTORE;
1023         }
1024     }
1025
1026     /* Now buzz through the rest of the partitions and mount them too */
1027     devs = deviceFind(NULL, DEVICE_TYPE_DISK);
1028     for (i = 0; devs[i]; i++) {
1029         if (!devs[i]->enabled)
1030             continue;
1031
1032         disk = (Disk *)devs[i]->private;
1033         if (!disk->chunks) {
1034             msgConfirm("No chunk list found for %s!", disk->name);
1035             return DITEM_FAILURE | DITEM_RESTORE;
1036         }
1037         if (RunningAsInit && root && (root->newfs || upgrade)) {
1038             Mkdir("/mnt/dev");
1039             if (!Fake)
1040                 MakeDevDisk(disk, "/mnt/dev");
1041         }
1042         else if (!RunningAsInit && !Fake)
1043             MakeDevDisk(disk, "/dev");
1044
1045         for (c1 = disk->chunks->part; c1; c1 = c1->next) {
1046             if (c1->type == freebsd) {
1047                 for (c2 = c1->part; c2; c2 = c2->next) {
1048                     if (c2->type == part && c2->subtype != FS_SWAP && c2->private_data) {
1049                         PartInfo *tmp = (PartInfo *)c2->private_data;
1050
1051                         /* Already did root */
1052                         if (c2 == rootdev)
1053                             continue;
1054
1055                         if (tmp->newfs && (!upgrade || !msgNoYes("You are upgrading - are you SURE you want to newfs /dev/%s?", c2->name)))
1056                             command_shell_add(tmp->mountpoint, "%s %s/dev/%s", tmp->newfs_cmd, RunningAsInit ? "/mnt" : "", c2->name);
1057                         else
1058                             command_shell_add(tmp->mountpoint, "fsck -y %s/dev/%s", RunningAsInit ? "/mnt" : "", c2->name);
1059                         if (tmp->soft)
1060                             command_shell_add(tmp->mountpoint, "tunefs -n enable %s/dev/%s", RunningAsInit ? "/mnt" : "", c2->name);
1061                         command_func_add(tmp->mountpoint, Mount, c2->name);
1062                     }
1063                     else if (c2->type == part && c2->subtype == FS_SWAP) {
1064                         char fname[80];
1065                         int i;
1066
1067                         if (c2 == swapdev)
1068                             continue;
1069                         sprintf(fname, "%s/dev/%s", RunningAsInit ? "/mnt" : "", c2->name);
1070                         i = (Fake || swapon(fname));
1071                         if (!i) {
1072                             dialog_clear_norefresh();
1073                             msgNotify("Added %s as an additional swap device", fname);
1074                         }
1075                         else {
1076                             msgConfirm("Unable to add %s as a swap device: %s", fname, strerror(errno));
1077                         }
1078                     }
1079                 }
1080             }
1081             else if (c1->type == fat && c1->private_data && (root->newfs || upgrade)) {
1082                 char name[FILENAME_MAX];
1083
1084                 sprintf(name, "%s/%s", RunningAsInit ? "/mnt" : "", ((PartInfo *)c1->private_data)->mountpoint);
1085                 Mkdir(name);
1086             }
1087         }
1088     }
1089
1090     if (RunningAsInit) {
1091         dialog_clear_norefresh();
1092         msgNotify("Copying initial device files..");
1093         /* Copy the boot floppy's dev files */
1094         if ((root->newfs || upgrade) && vsystem("find -x /dev | cpio %s -pdum /mnt", cpioVerbosity())) {
1095             msgConfirm("Couldn't clone the /dev files!");
1096             return DITEM_FAILURE | DITEM_RESTORE;
1097         }
1098     }
1099
1100     command_sort();
1101     command_execute();
1102     dialog_clear_norefresh();
1103     return DITEM_SUCCESS | DITEM_RESTORE;
1104 }
1105
1106 static char *
1107 getRelname(void)
1108 {
1109     static char buf[64];
1110     size_t sz = (sizeof buf) - 1;
1111
1112     if (sysctlbyname("kern.osrelease", buf, &sz, NULL, 0) != -1) {
1113         buf[sz] = '\0';
1114         return buf;
1115     }
1116     else
1117         return "<unknown>";
1118 }
1119
1120 /* Initialize various user-settable values to their defaults */
1121 int
1122 installVarDefaults(dialogMenuItem *self)
1123 {
1124     char *cp;
1125
1126     /* Set default startup options */
1127     variable_set2(VAR_RELNAME,                  getRelname(), 0);
1128     variable_set2(VAR_CPIO_VERBOSITY,           "high", 0);
1129     variable_set2(VAR_KGET,                     "YES", 0);
1130     variable_set2(VAR_TAPE_BLOCKSIZE,           DEFAULT_TAPE_BLOCKSIZE, 0);
1131     variable_set2(VAR_INSTALL_ROOT,             "/", 0);
1132     variable_set2(VAR_INSTALL_CFG,              "install.cfg", 0);
1133     cp = getenv("EDITOR");
1134     if (!cp)
1135         cp = "/usr/bin/ee";
1136     variable_set2(VAR_EDITOR,                   cp, 0);
1137     variable_set2(VAR_FTP_USER,                 "ftp", 0);
1138     variable_set2(VAR_BROWSER_PACKAGE,          "links", 0);
1139     variable_set2(VAR_BROWSER_BINARY,           "/usr/local/bin/links", 0);
1140     variable_set2(VAR_FTP_STATE,                "passive", 0);
1141     variable_set2(VAR_NFS_SECURE,               "NO", -1);
1142     if (OnVTY)
1143             variable_set2(VAR_FIXIT_TTY,                "standard", 0);
1144     else
1145             variable_set2(VAR_FIXIT_TTY,                "serial", 0);
1146     variable_set2(VAR_PKG_TMPDIR,               "/var/tmp", 0);
1147     variable_set2(VAR_MEDIA_TIMEOUT,            itoa(MEDIA_TIMEOUT), 0);
1148     if (getpid() != 1)
1149         variable_set2(SYSTEM_STATE,             "update", 0);
1150     else
1151         variable_set2(SYSTEM_STATE,             "init", 0);
1152     variable_set2(VAR_NEWFS_ARGS,               "-b 16384 -f 2048", 0);
1153     return DITEM_SUCCESS;
1154 }
1155
1156 /* Load the environment up from various system configuration files */
1157 void
1158 installEnvironment(void)
1159 {
1160     configEnvironmentRC_conf();
1161     if (file_readable("/etc/resolv.conf"))
1162         configEnvironmentResolv("/etc/resolv.conf");
1163 }
1164
1165 /* Copy the boot floppy contents into /stand */
1166 Boolean
1167 copySelf(void)
1168 {
1169     int i;
1170
1171     if (file_readable("/boot.help"))
1172         vsystem("cp /boot.help /mnt");
1173     msgWeHaveOutput("Copying the boot floppy to /stand on root filesystem");
1174     i = vsystem("find -x /stand | cpio %s -pdum /mnt", cpioVerbosity());
1175     if (i) {
1176         msgConfirm("Copy returned error status of %d!", i);
1177         return FALSE;
1178     }
1179
1180     /* Copy the /etc files into their rightful place */
1181     if (vsystem("cd /mnt/stand; find etc | cpio %s -pdum /mnt", cpioVerbosity())) {
1182         msgConfirm("Couldn't copy up the /etc files!");
1183         return TRUE;
1184     }
1185     return TRUE;
1186 }
1187
1188 static void
1189 create_termcap(void)
1190 {
1191     FILE *fp;
1192
1193     const char *caps[] = {
1194         termcap_vt100, termcap_cons25, termcap_cons25_m, termcap_cons25r,
1195         termcap_cons25r_m, termcap_cons25l1, termcap_cons25l1_m,
1196         termcap_xterm, NULL,
1197     };
1198     const char **cp;
1199
1200     if (!file_readable(TERMCAP_FILE)) {
1201         Mkdir("/usr/share/misc");
1202         fp = fopen(TERMCAP_FILE, "w");
1203         if (!fp) {
1204             msgConfirm("Unable to initialize termcap file. Some screen-oriented\nutilities may not work.");
1205             return;
1206         }
1207         cp = caps;
1208         while (*cp)
1209             fprintf(fp, "%s\n", *(cp++));
1210         fclose(fp);
1211     }
1212 }