2 * The new sysinstall program.
4 * This is probably the last program in the `sysinstall' line - the next
5 * generation being essentially a complete rewrite.
10 * Jordan Hubbard. All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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
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.
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
37 #include "sysinstall.h"
38 #include <sys/param.h>
39 #include <sys/mount.h>
47 unsigned int SrcDists;
48 unsigned int XOrgDists;
49 unsigned int KernelDists;
51 enum _disttype { DT_TARBALL, DT_SUBDIST, DT_PACKAGE };
53 typedef struct _dist {
55 unsigned int *my_mask;
57 enum _disttype my_type;
59 char *my_string; /* DT_TARBALL & DT_PACKAGE */
60 struct _dist *my_dist; /* DT_SUBDIST */
64 static Distribution KernelDistTable[];
65 static Distribution SrcDistTable[];
66 static Distribution XOrgDistTable[];
68 #define DTE_TARBALL(name, mask, flag, directory) \
69 { name, mask, DIST_ ## flag, DT_TARBALL, { directory } }
70 #define DTE_PACKAGE(name, mask, flag, package) \
71 { name, mask, DIST_ ## flag, DT_PACKAGE, { package } }
72 #define DTE_SUBDIST(name, mask, flag, subdist) \
73 { name, mask, DIST_ ## flag, DT_SUBDIST, { .my_dist = subdist } }
74 #define DTE_END { NULL, NULL, 0, 0, { NULL } }
76 #define BASE_DIST (&DistTable[0])
78 /* The top-level distribution categories */
79 static Distribution DistTable[] = {
80 DTE_TARBALL("base", &Dists, BASE, "/"),
81 DTE_SUBDIST("kernels", &Dists, KERNEL, KernelDistTable),
82 DTE_TARBALL("doc", &Dists, DOC, "/"),
83 DTE_TARBALL("games", &Dists, GAMES, "/"),
84 DTE_TARBALL("manpages", &Dists, MANPAGES, "/"),
85 DTE_TARBALL("catpages", &Dists, CATPAGES, "/"),
86 DTE_TARBALL("proflibs", &Dists, PROFLIBS, "/"),
87 DTE_TARBALL("dict", &Dists, DICT, "/"),
88 DTE_TARBALL("info", &Dists, INFO, "/"),
90 DTE_TARBALL("lib32", &Dists, LIB32, "/"),
92 DTE_SUBDIST("src", &Dists, SRC, SrcDistTable),
93 DTE_TARBALL("ports", &Dists, PORTS, "/usr"),
94 DTE_TARBALL("local", &Dists, LOCAL, "/"),
95 DTE_PACKAGE("X.Org", &Dists, XORG, "xorg"),
99 /* The kernel distributions */
100 static Distribution KernelDistTable[] = {
101 DTE_TARBALL("GENERIC", &KernelDists, KERNEL_GENERIC, "/boot"),
103 DTE_TARBALL("SMP", &KernelDists, KERNEL_SMP, "/boot"),
108 /* The /usr/src distribution */
109 static Distribution SrcDistTable[] = {
110 DTE_TARBALL("sbase", &SrcDists, SRC_BASE, "/usr/src"),
111 DTE_TARBALL("scddl", &SrcDists, SRC_CDDL, "/usr/src"),
112 DTE_TARBALL("scompat", &SrcDists, SRC_COMPAT, "/usr/src"),
113 DTE_TARBALL("scontrib", &SrcDists, SRC_CONTRIB, "/usr/src"),
114 DTE_TARBALL("scrypto", &SrcDists, SRC_SCRYPTO, "/usr/src"),
115 DTE_TARBALL("sgnu", &SrcDists, SRC_GNU, "/usr/src"),
116 DTE_TARBALL("setc", &SrcDists, SRC_ETC, "/usr/src"),
117 DTE_TARBALL("sgames", &SrcDists, SRC_GAMES, "/usr/src"),
118 DTE_TARBALL("sinclude", &SrcDists, SRC_INCLUDE, "/usr/src"),
119 DTE_TARBALL("skrb5", &SrcDists, SRC_SKERBEROS5, "/usr/src"),
120 DTE_TARBALL("slib", &SrcDists, SRC_LIB, "/usr/src"),
121 DTE_TARBALL("slibexec", &SrcDists, SRC_LIBEXEC, "/usr/src"),
122 DTE_TARBALL("srelease", &SrcDists, SRC_RELEASE, "/usr/src"),
123 DTE_TARBALL("sbin", &SrcDists, SRC_BIN, "/usr/src"),
124 DTE_TARBALL("ssecure", &SrcDists, SRC_SSECURE, "/usr/src"),
125 DTE_TARBALL("ssbin", &SrcDists, SRC_SBIN, "/usr/src"),
126 DTE_TARBALL("sshare", &SrcDists, SRC_SHARE, "/usr/src"),
127 DTE_TARBALL("ssys", &SrcDists, SRC_SYS, "/usr/src"),
128 DTE_TARBALL("subin", &SrcDists, SRC_UBIN, "/usr/src"),
129 DTE_TARBALL("susbin", &SrcDists, SRC_USBIN, "/usr/src"),
130 DTE_TARBALL("stools", &SrcDists, SRC_TOOLS, "/usr/src"),
131 DTE_TARBALL("srescue", &SrcDists, SRC_RESCUE, "/usr/src"),
135 static int distMaybeSetPorts(dialogMenuItem *self);
138 distVerifyFlags(void)
145 Dists |= DIST_KERNEL;
147 msgDebug("Dist Masks: Dists: %0x, Srcs: %0x Kernels: %0x\n", Dists,
148 SrcDists, KernelDists);
149 msgDebug("XServer: %0x\n", XOrgDists);
154 distReset(dialogMenuItem *self)
160 return DITEM_SUCCESS | DITEM_REDRAW;
164 distConfig(dialogMenuItem *self)
170 if ((cp = variable_get(VAR_DIST_MAIN)) != NULL)
173 if ((cp = variable_get(VAR_DIST_SRC)) != NULL)
176 if ((cp = variable_get(VAR_DIST_X11)) != NULL)
177 XOrgDists = atoi(cp);
179 if ((cp = variable_get(VAR_DIST_KERNEL)) != NULL)
180 KernelDists = atoi(cp);
183 return DITEM_SUCCESS | DITEM_REDRAW;
190 XOrgDists = DIST_XORG_ALL;
191 return DITEM_SUCCESS;
198 /* select default kernel based on deduced cpu count */
199 return NCpus > 1 ? DIST_KERNEL_SMP : DIST_KERNEL_GENERIC;
201 return DIST_KERNEL_GENERIC;
206 distSetDeveloper(dialogMenuItem *self)
211 Dists = _DIST_DEVELOPER;
212 SrcDists = DIST_SRC_ALL;
213 KernelDists = selectKernel();
214 i = distMaybeSetPorts(self);
220 distSetXDeveloper(dialogMenuItem *self)
224 i = distSetDeveloper(self);
231 distSetKernDeveloper(dialogMenuItem *self)
236 Dists = _DIST_DEVELOPER;
237 SrcDists = DIST_SRC_SYS | DIST_SRC_BASE;
238 KernelDists = selectKernel();
239 i = distMaybeSetPorts(self);
245 distSetXKernDeveloper(dialogMenuItem *self)
249 i = distSetKernDeveloper(self);
256 distSetUser(dialogMenuItem *self)
262 KernelDists = selectKernel();
263 i = distMaybeSetPorts(self);
269 distSetXUser(dialogMenuItem *self)
273 i = distSetUser(self);
280 distSetMinimum(dialogMenuItem *self)
283 Dists = DIST_BASE | DIST_KERNEL;
284 KernelDists = selectKernel();
286 return DITEM_SUCCESS | DITEM_REDRAW;
290 distSetEverything(dialogMenuItem *self)
295 SrcDists = DIST_SRC_ALL;
296 XOrgDists = DIST_XORG_ALL;
297 KernelDists = DIST_KERNEL_ALL;
298 i = distMaybeSetPorts(self);
300 return i | DITEM_REDRAW;
304 distMaybeSetPorts(dialogMenuItem *self)
306 dialog_clear_norefresh();
307 if (!msgYesNo("Would you like to install the FreeBSD ports collection?\n\n"
308 "This will give you ready access to over 17,000 ported software packages,\n"
309 "at a cost of around 400MB of disk space when \"clean\" and possibly\n"
310 "much more than that when a lot of the distribution tarballs are loaded\n"
311 "(unless you have the extra discs available from a FreeBSD CD/DVD distribution\n"
312 "and can mount them on /cdrom, in which case this is far less of a problem).\n\n"
313 "The ports collection is a very valuable resource and well worth having\n"
314 "on your /usr partition, so it is advisable to say Yes to this option.\n\n"
315 "For more information on the ports collection & the latest ports, visit:\n"
316 " http://www.freebsd.org/ports\n"))
319 Dists &= ~DIST_PORTS;
320 return DITEM_SUCCESS | DITEM_RESTORE;
324 distSetByName(Distribution *dist, char *name)
326 int i, status = FALSE;
328 /* Loop through current set */
329 for (i = 0; dist[i].my_name; i++) {
330 switch (dist[i].my_type) {
333 if (!strcmp(dist[i].my_name, name)) {
334 *(dist[i].my_mask) |= dist[i].my_bit;
339 if (distSetByName(dist[i].my_data.my_dist, name)) {
350 distUnsetByName(Distribution *dist, char *name)
352 int i, status = FALSE;
354 /* Loop through current set */
355 for (i = 0; dist[i].my_name; i++) {
356 switch (dist[i].my_type) {
359 if (!strcmp(dist[i].my_name, name)) {
360 *(dist[i].my_mask) &= ~(dist[i].my_bit);
365 if (distUnsetByName(dist[i].my_data.my_dist, name)) {
374 /* Just for the dispatch stuff */
376 distSetCustom(dialogMenuItem *self)
378 char *cp, *cp2, *tmp;
380 if (!(tmp = variable_get(VAR_DISTS))) {
381 msgDebug("distSetCustom() called without %s variable set.\n", VAR_DISTS);
382 return DITEM_FAILURE;
385 cp = alloca(strlen(tmp) + 1);
387 msgFatal("Couldn't alloca() %d bytes!\n", (int)(strlen(tmp) + 1));
390 if ((cp2 = index(cp, ' ')) != NULL)
392 if (!distSetByName(DistTable, cp))
393 msgDebug("distSetCustom: Warning, no such release \"%s\"\n", cp);
397 return DITEM_SUCCESS;
400 /* Just for the dispatch stuff */
402 distUnsetCustom(dialogMenuItem *self)
404 char *cp, *cp2, *tmp;
406 if (!(tmp = variable_get(VAR_DISTS))) {
407 msgDebug("distUnsetCustom() called without %s variable set.\n", VAR_DISTS);
408 return DITEM_FAILURE;
411 cp = alloca(strlen(tmp) + 1);
413 msgFatal("Couldn't alloca() %d bytes!\n", (int)(strlen(tmp) + 1));
416 if ((cp2 = index(cp, ' ')) != NULL)
418 if (!distUnsetByName(DistTable, cp))
419 msgDebug("distUnsetCustom: Warning, no such release \"%s\"\n", cp);
422 return DITEM_SUCCESS;
426 distSetSrc(dialogMenuItem *self)
430 dialog_clear_norefresh();
431 if (!dmenuOpenSimple(&MenuSrcDistributions, FALSE))
436 return i | DITEM_RESTORE;
440 distSetKernel(dialogMenuItem *self)
444 dialog_clear_norefresh();
445 if (!dmenuOpenSimple(&MenuKernelDistributions, FALSE))
450 return i | DITEM_RESTORE;
453 static Boolean got_intr = FALSE;
455 /* timeout handler */
459 msgDebug("User generated interrupt.\n");
464 check_for_interrupt(void)
474 * translate distribution filename to lower case
475 * as doTARBALL does in release/Makefile
478 translateDist(char trdist[PATH_MAX], const char *dist)
483 * translate distribution filename to lower case
484 * as doTARBALL does in release/Makefile
486 for (j = 0; j < PATH_MAX-1 && dist[j] != '\0'; j++)
487 trdist[j] = tolower(dist[j]);
492 * Try to get distribution as multiple pieces, locating and parsing an
493 * info file which tells us how many we need for this distribution.
496 distExtractTarball(char *path, char *dist, char *my_dir, int is_base)
498 char *buf = NULL, trdist[PATH_MAX], fname[PATH_MAX];
499 struct timeval start, stop;
500 int j, status, total, intr;
501 int cpid, zpid, fd2, chunk, numchunks;
502 properties dist_attr = NULL;
506 translateDist(trdist, dist);
508 msgDebug("%s: path \"%s\" dist \"%s\" trdist \"%s\" "
509 "my_dir \"%s\" %sis_base\n",
510 __func__, path, dist, trdist, my_dir, is_base ? "" : "!");
514 snprintf(fname, sizeof (fname), "%s/%s.inf", path, trdist);
517 fp = DEVICE_GET(mediaDevice, fname, TRUE);
518 intr = check_for_interrupt();
519 if (fp == (FILE *)IO_ERROR || intr || !mediaDevice) {
521 msgDebug("%s: fname %s fp: %p, intr: %d mediaDevice: %p\n",
522 __func__, fname, fp, intr, mediaDevice);
523 /* Hard error, can't continue */
524 if (!msgYesNo("Unable to open %s: %s.\nReinitialize media?",
525 fname, !intr ? "I/O error." : "User interrupt.")) {
526 DEVICE_SHUTDOWN(mediaDevice);
527 if (!DEVICE_INIT(mediaDevice))
532 } else if (fp == NULL) {
533 /* No attributes file, so try as a single file. */
534 snprintf(fname, sizeof(fname), "%s/%s.%s", path, trdist,
535 USE_GZIP ? "tgz" : "tbz");
537 msgDebug("%s: fp is NULL (1) fname: %s\n", __func__, fname);
539 * Passing TRUE as 3rd parm to get routine makes this a "probing"
540 * get, for which errors are not considered too significant.
543 fp = DEVICE_GET(mediaDevice, fname, TRUE);
544 intr = check_for_interrupt();
545 if (fp == (FILE *)IO_ERROR || intr || !mediaDevice) {
547 msgDebug("%s: fname %s fp: %p, intr: %d mediaDevice: %p\n",
548 __func__, fname, fp, intr, mediaDevice);
549 /* Hard error, can't continue */
550 msgConfirm("Unable to open %s: %s", fname,
551 !intr ? "I/O error" : "User interrupt");
552 DEVICE_SHUTDOWN(mediaDevice);
553 if (!DEVICE_INIT(mediaDevice))
556 } else if (fp != NULL) {
557 char *dir = root_bias(my_dir);
559 dialog_clear_norefresh();
560 msgNotify("Extracting %s into %s directory...", dist, dir);
561 status = mediaExtractDist(dir, dist, fp);
566 msgDebug("%s: fp is NULL (2) fname %s\n", __func__, fname);
572 msgDebug("Parsing attributes file for distribution %s\n", dist);
574 dist_attr = properties_read(fileno(fp));
575 intr = check_for_interrupt();
576 if (intr || !dist_attr) {
578 msgDebug("%s: intr %d dist_attr %p\n", __func__, intr, dist_attr);
579 msgConfirm("Cannot parse information file for the %s distribution: %s\n"
580 "Please verify that your media is valid and try again.",
581 dist, !intr ? "I/O error" : "User interrupt");
583 tmp = property_find(dist_attr, "Pieces");
585 numchunks = strtol(tmp, 0, 0);
590 msgDebug("%s: numchunks is zero\n", __func__);
595 msgDebug("Attempting to extract distribution from %u chunks.\n",
599 (void)gettimeofday(&start, (struct timezone *)NULL);
601 /* We have one or more chunks, initialize unpackers... */
602 mediaExtractDistBegin(root_bias(my_dir), &fd2, &zpid, &cpid);
604 /* And go for all the chunks */
605 dialog_clear_norefresh();
606 for (chunk = 0; chunk < numchunks; chunk++) {
607 int n, retval, last_msg, chunksize, realsize;
613 snprintf(fname, sizeof(fname), "cksum.%c%c", (chunk / 26) + 'a',
615 tmp = property_find(dist_attr, fname);
618 tmp = index(tmp, ' ');
619 chunksize = strtol(tmp, 0, 0);
621 snprintf(fname, sizeof(fname), "%s/%s.%c%c", path, trdist, (chunk / 26) + 'a',
624 msgDebug("trying for piece %d of %d: %s\n", chunk + 1, numchunks,
626 fp = DEVICE_GET(mediaDevice, fname, FALSE);
627 intr = check_for_interrupt();
628 /* XXX: this can't work if we get an I/O error */
629 if (fp <= (FILE *)NULL || intr) {
631 msgConfirm("Failed to find %s on this media. Reinitializing media.", fname);
633 msgConfirm("Failed to retreive piece file %s.\n"
634 "%s: Reinitializing media.",
635 fname, !intr ? "I/O error" : "User interrupt");
636 DEVICE_SHUTDOWN(mediaDevice);
637 if (!DEVICE_INIT(mediaDevice))
643 snprintf(prompt, sizeof(prompt), "Extracting %s into %s directory...",
644 dist, root_bias(my_dir));
645 dialog_gauge("Progress", prompt, 8, 15, 6, 50,
646 (chunk + 1) * 100 / numchunks);
648 buf = safe_realloc(buf, chunksize);
653 n = fread(buf + realsize, 1, BUFSIZ, fp);
654 if (check_for_interrupt()) {
655 msgConfirm("Media read error: User interrupt.");
663 /* Print statistics about how we're doing */
664 (void) gettimeofday(&stop, (struct timezone *)0);
665 stop.tv_sec = stop.tv_sec - start.tv_sec;
666 stop.tv_usec = stop.tv_usec - start.tv_usec;
667 if (stop.tv_usec < 0)
668 stop.tv_sec--, stop.tv_usec += 1000000;
669 seconds = stop.tv_sec + (stop.tv_usec / 1000000.0);
673 if (seconds != last_msg) {
675 msgInfo("%10d bytes read from %s dist, chunk %2d of %2d @ %.1f KBytes/sec.",
676 total, dist, chunk + 1, numchunks,
677 (total / seconds) / 1000.0);
682 if (!chunksize || (realsize == chunksize)) {
683 /* No substitution necessary */
684 retval = write(fd2, buf, realsize);
685 if (retval != realsize) {
687 dialog_clear_norefresh();
688 msgConfirm("Write failure on transfer! (wrote %d bytes of %d bytes)", retval, realsize);
692 for (j = 0; j < realsize; j++) {
693 /* On finding CRLF, skip the CR; don't exceed end of buffer. */
694 if ((buf[j] != 0x0d) || (j == total - 1) || (buf[j + 1] != 0x0a)) {
695 retval = write(fd2, buf + j, 1);
698 dialog_clear_norefresh();
699 msgConfirm("Write failure on transfer! (wrote %d bytes of %d bytes)", j, chunksize);
711 properties_free(dist_attr);
714 status = mediaExtractDistEnd(zpid, cpid);
716 (void)mediaExtractDistEnd(zpid, cpid);
723 distExtract(char *parent, Distribution *me)
727 WINDOW *w = savescr();
728 struct sigaction old, new;
732 msgDebug("distExtract: parent: %s, me: %s\n", parent ? parent : "(none)", me->my_name);
734 /* Make ^C fake a sudden timeout */
735 new.sa_handler = handle_intr;
737 (void)sigemptyset(&new.sa_mask);
738 dialog_clear_norefresh();
739 dialog_msgbox("Please Wait", "Extracting all requested distributions...", -1, -1, 0);
740 sigaction(SIGINT, &new, &old);
742 /* Loop through to see if we're in our parent's plans */
743 for (i = 0; me[i].my_name; i++) {
744 dist = me[i].my_name;
745 path = parent ? parent : dist;
747 /* If our bit isn't set, go to the next */
748 if (!(me[i].my_bit & *(me[i].my_mask)))
751 switch (me[i].my_type) {
753 /* Recurse if we actually have a sub-distribution */
754 status = distExtract(dist, me[i].my_data.my_dist);
756 dialog_clear_norefresh();
757 msgConfirm("Unable to transfer all components of the %s distribution.\n"
758 "You may wish to switch media types and try again.\n",
763 dialog_clear_norefresh();
764 msgNotify("Installing %s distribution...", dist);
765 status = (package_add(me[i].my_data.my_string) == DITEM_SUCCESS);
767 dialog_clear_norefresh();
770 status = distExtractTarball(path, dist, me[i].my_data.my_string,
771 &me[i] == BASE_DIST);
773 dialog_clear_norefresh();
774 if (me[i].my_bit != DIST_LOCAL) {
775 status = msgYesNo("Unable to transfer the %s distribution from\n%s.\n\n"
776 "Do you want to try to retrieve it again?",
777 me[i].my_name, mediaDevice->name);
787 * If extract was successful, remove ourselves from further
791 *(me[i].my_mask) &= ~(me[i].my_bit);
793 sigaction(SIGINT, &old, NULL); /* Restore signal handler */
799 printSelected(char *buf, int selected, Distribution *me, int *col)
803 /* Loop through to see if we're in our parent's plans */
804 for (i = 0; me[i].my_name; i++) {
806 /* If our bit isn't set, go to the next */
807 if (!(me[i].my_bit & selected))
810 *col += strlen(me[i].my_name);
815 sprintf(&buf[strlen(buf)], " %s", me[i].my_name);
817 /* Recurse if have a sub-distribution */
818 if (me[i].my_type == DT_SUBDIST)
819 printSelected(buf, *(me[i].my_mask), me[i].my_data.my_dist, col);
824 distExtractAll(dialogMenuItem *self)
826 int old_dists, old_kernel, retries = 0, status = DITEM_SUCCESS;
832 if (!dmenuOpenSimple(&MenuSubDistributions, FALSE) || !Dists)
833 return DITEM_FAILURE;
836 if (!mediaVerify() || !DEVICE_INIT(mediaDevice))
837 return DITEM_FAILURE;
840 old_kernel = KernelDists;
843 dialog_clear_norefresh();
845 msgNotify("Attempting to install all selected distributions..");
847 /* Try for 3 times around the loop, then give up. */
848 while (Dists && ++retries < 3)
849 distExtract(NULL, DistTable);
851 dialog_clear_norefresh();
852 /* Only do base fixup if base dist was successfully extracted */
853 if ((old_dists & DIST_BASE) && !(Dists & DIST_BASE))
854 status |= installFixupBase(self);
855 /* Only do kernel fixup if kernel dist was successfully extracted */
856 if ((old_dists & DIST_KERNEL) && !(Dists & DIST_KERNEL))
857 status |= installFixupKernel(self, old_kernel);
859 /* Clear any local dist flags now */
860 Dists &= ~DIST_LOCAL;
866 dialog_clear_norefresh();
867 printSelected(buf, Dists, DistTable, &col);
868 dialog_clear_norefresh();
870 msgConfirm("Couldn't extract the following distributions. This may\n"
871 "be because they were not available on the installation\n"
872 "media you've chosen:\n\n\t%s", buf);