fb64ded4989bbc1b2b5cde2e6bf679a01bd3efb1
[dragonfly.git] / release / sysinstall / installUpgrade.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/installUpgrade.c,v 1.71.2.12 2002/06/10 04:28:53 obrien Exp $
8  * $DragonFly: src/release/sysinstall/Attic/installUpgrade.c,v 1.2 2003/06/17 04:27:21 dillon Exp $
9  *
10  * Copyright (c) 1995
11  *      Jordan Hubbard.  All rights reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer,
18  *    verbatim and that no modifications are made prior to this
19  *    point in the file.
20  * 2. Redistributions in binary form must reproduce the above copyright
21  *    notice, this list of conditions and the following disclaimer in the
22  *    documentation and/or other materials provided with the distribution.
23  *
24  * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  */
37
38 #include "sysinstall.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 #include <sys/stat.h>
46 #include <unistd.h>
47 #include <sys/mount.h>
48
49 static int installUpgradeNonInteractive(dialogMenuItem *self);
50
51 typedef struct _hitList {
52     enum { JUST_COPY, CALL_HANDLER } action ;
53     char *name;
54     Boolean optional;
55     void (*handler)(struct _hitList *self);
56 } HitList;
57
58 /* These are the only meaningful files I know about */
59 static HitList etc_files [] = {
60    { JUST_COPY,         "Xaccel.ini",           TRUE, NULL },
61    { JUST_COPY,         "X11",                  TRUE, NULL },
62    { JUST_COPY,         "adduser.conf",         TRUE, NULL },
63    { JUST_COPY,         "aliases",              TRUE, NULL },
64    { JUST_COPY,         "aliases.db",           TRUE, NULL },
65    { JUST_COPY,         "amd.map",              TRUE, NULL },
66    { JUST_COPY,         "auth.conf",            TRUE, NULL },
67    { JUST_COPY,         "crontab",              TRUE, NULL },
68    { JUST_COPY,         "csh.cshrc",            TRUE, NULL },
69    { JUST_COPY,         "csh.login",            TRUE, NULL },
70    { JUST_COPY,         "csh.logout",           TRUE, NULL },
71    { JUST_COPY,         "cvsupfile",            TRUE, NULL },
72    { JUST_COPY,         "dhclient.conf",        TRUE, NULL },
73    { JUST_COPY,         "disktab",              TRUE, NULL },
74    { JUST_COPY,         "dm.conf",              TRUE, NULL },
75    { JUST_COPY,         "dumpdates",            TRUE, NULL },
76    { JUST_COPY,         "exports",              TRUE, NULL },
77    { JUST_COPY,         "fbtab",                TRUE, NULL },
78    { JUST_COPY,         "fstab",                FALSE, NULL },
79    { JUST_COPY,         "ftpusers",             TRUE, NULL },
80    { JUST_COPY,         "gettytab",             TRUE, NULL },
81    { JUST_COPY,         "gnats",                TRUE, NULL },
82    { JUST_COPY,         "group",                FALSE, NULL },
83    { JUST_COPY,         "hosts",                TRUE, NULL },
84    { JUST_COPY,         "host.conf",            TRUE, NULL },
85    { JUST_COPY,         "hosts.allow",          TRUE, NULL },
86    { JUST_COPY,         "hosts.equiv",          TRUE, NULL },
87    { JUST_COPY,         "hosts.lpd",            TRUE, NULL },
88    { JUST_COPY,         "inetd.conf",           TRUE, NULL },
89    { JUST_COPY,         "kerberosIV",           TRUE, NULL },
90    { JUST_COPY,         "localtime",            TRUE, NULL },
91    { JUST_COPY,         "login.access",         TRUE, NULL },
92    { JUST_COPY,         "login.conf",           TRUE, NULL },
93    { JUST_COPY,         "mail",                 TRUE, NULL },
94    { JUST_COPY,         "mail.rc",              TRUE, NULL },
95    { JUST_COPY,         "make.conf",            TRUE, NULL },
96    { JUST_COPY,         "manpath.config",       TRUE, NULL },
97    { JUST_COPY,         "master.passwd",        FALSE, NULL },
98    { JUST_COPY,         "modems",               TRUE, NULL },
99    { JUST_COPY,         "motd",                 TRUE, NULL },
100    { JUST_COPY,         "namedb",               TRUE, NULL },
101    { JUST_COPY,         "networks",             TRUE, NULL },
102    { JUST_COPY,         "newsyslog.conf",       TRUE, NULL },
103    { JUST_COPY,         "nsmb.conf",            TRUE, NULL },
104    { JUST_COPY,         "pam.conf",             TRUE, NULL },
105    { JUST_COPY,         "passwd",               TRUE, NULL },
106    { JUST_COPY,         "periodic",             TRUE, NULL },
107    { JUST_COPY,         "ppp",                  TRUE, NULL },
108    { JUST_COPY,         "printcap",             TRUE, NULL },
109    { JUST_COPY,         "profile",              TRUE, NULL },
110    { JUST_COPY,         "pwd.db",               TRUE, NULL },
111    { JUST_COPY,         "rc.local",             TRUE, NULL },
112    { JUST_COPY,         "rc.firewall",          TRUE, NULL },
113    { JUST_COPY,         "rc.conf.local",        TRUE, NULL },
114    { JUST_COPY,         "remote",               TRUE, NULL },
115    { JUST_COPY,         "resolv.conf",          TRUE, NULL },
116    { JUST_COPY,         "rmt",                  TRUE, NULL },
117    { JUST_COPY,         "sendmail.cf",          TRUE, NULL },
118    { JUST_COPY,         "sendmail.cw",          TRUE, NULL },
119    { JUST_COPY,         "services",             TRUE, NULL },
120    { JUST_COPY,         "shells",               TRUE, NULL },
121    { JUST_COPY,         "skeykeys",             TRUE, NULL },
122    { JUST_COPY,         "spwd.db",              TRUE, NULL },
123    { JUST_COPY,         "ssh",                  TRUE, NULL },
124    { JUST_COPY,         "syslog.conf",          TRUE, NULL },
125    { JUST_COPY,         "ttys",                 TRUE, NULL },
126    { JUST_COPY,         "uucp",                 TRUE, NULL },
127    { 0 },
128 };
129
130 void
131 traverseHitlist(HitList *h)
132 {
133     system("rm -rf /etc/upgrade");
134     Mkdir("/etc/upgrade");
135     while (h->name) {
136         if (!file_readable(h->name)) {
137             if (!h->optional)
138                 msgConfirm("Unable to find an old /etc/%s file!  That is decidedly non-standard and\n"
139                            "your upgraded system may function a little strangely as a result.", h->name);
140         }
141         else {
142             if (h->action == JUST_COPY) {
143                 /* Move the just-loaded copy aside */
144                 vsystem("mv /etc/%s /etc/upgrade/%s", h->name, h->name);
145
146                 /* Copy the old one into its place */
147                 msgNotify("Resurrecting %s..", h->name);
148                 /* Do this with tar so that symlinks and such are preserved */
149                 if (vsystem("tar cf - %s | tar xpf - -C /etc", h->name))
150                     msgConfirm("Unable to resurrect your old /etc/%s!  Hmmmm.", h->name);
151             }
152             else /* call handler */
153                 h->handler(h);
154         }
155         ++h;
156     }
157 }
158
159 int
160 installUpgrade(dialogMenuItem *self)
161 {
162     char saved_etc[FILENAME_MAX];
163     Boolean extractingBin = TRUE;
164
165     if (variable_get(VAR_NONINTERACTIVE))
166         return installUpgradeNonInteractive(self);
167
168     variable_set2(SYSTEM_STATE, "upgrade", 0);
169     dialog_clear();
170
171     if (msgYesNo("Before beginning a binary upgrade, please review the upgrade instructions,\n"
172                  "which are located in the \"Install\" document under the main documentation\n"
173                  "menu.  Given that you have read these instructions and understand the risks\n"
174                  "and precautions involved, are you sure that you want to proceed with\n"
175                  "this upgrade?") != 0)
176         return DITEM_FAILURE;
177
178     if (!Dists) {
179         msgConfirm("First, you must select some distribution components.  The upgrade procedure\n"
180                    "will only upgrade the distributions you select in the next set of menus.");
181         if (!dmenuOpenSimple(&MenuDistributions, FALSE) || !Dists)
182             return DITEM_FAILURE;
183     }
184     else if (!(Dists & DIST_BIN)) {         /* No bin selected?  Not much of an upgrade.. */
185         if (msgYesNo("You didn't select the bin distribution as one of the distributons to load.\n"
186                      "This one is pretty vital to a successful upgrade.  Are you SURE you don't\n"
187                      "want to select the bin distribution?  Chose No to bring up the Distributions\n"
188                      "menu again.") != 0) {
189             if (!dmenuOpenSimple(&MenuDistributions, FALSE))
190                 return DITEM_FAILURE;
191         }
192     }
193
194     /* Still?!  OK!  They must know what they're doing.. */
195     if (!(Dists & DIST_BIN))
196         extractingBin = FALSE;
197
198     if (RunningAsInit) {
199         Device **devs;
200         int i, cnt;
201         char *cp;
202
203         cp = variable_get(VAR_DISK);
204         devs = deviceFind(cp, DEVICE_TYPE_DISK);
205         cnt = deviceCount(devs);
206         if (!cnt) {
207             msgConfirm("No disks found!  Please verify that your disk controller is being\n"
208                        "properly probed at boot time.  See the Hardware Guide on the\n"
209                        "Documentation menu for clues on diagnosing this type of problem.");
210             return DITEM_FAILURE | DITEM_RESTORE;
211         }
212         else {
213             /* Enable all the drives before we start */
214             for (i = 0; i < cnt; i++)
215                 devs[i]->enabled = TRUE;
216         }
217
218         msgConfirm("OK.  First, we're going to go to the disk label editor.  In this editor\n"
219                    "you will be expected to Mount any partitions you're interested in\n"
220                    "upgrading.  DO NOT set the Newfs flag to Y on anything in the label editor\n"
221                    "unless you're absolutely sure you know what you're doing!  In this\n"
222                    "instance, you'll be using the label editor as little more than a fancy\n"
223                    "screen-oriented partition mounting tool.\n\n"
224                    "Once you're done in the label editor, press Q to return here for the next\n"
225                    "step.");
226
227         if (DITEM_STATUS(diskLabelEditor(self)) == DITEM_FAILURE) {
228             msgConfirm("The disk label editor returned an error status.  Upgrade operation\n"
229                        "aborted.");
230             return DITEM_FAILURE | DITEM_RESTORE;
231         }
232
233         /* Don't write out MBR info */
234         variable_set2(DISK_PARTITIONED, "written", 0);
235         if (DITEM_STATUS(diskLabelCommit(self)) == DITEM_FAILURE) {
236             msgConfirm("Not all file systems were properly mounted.  Upgrade operation\n"
237                        "aborted.");
238             variable_unset(DISK_PARTITIONED);
239             return DITEM_FAILURE | DITEM_RESTORE;
240         }
241
242         msgNotify("Updating /stand on root filesystem");
243         (void)vsystem("find -x /stand | cpio %s -pdum /mnt", cpioVerbosity());
244
245         if (DITEM_STATUS(chroot("/mnt")) == DITEM_FAILURE) {
246             msgConfirm("Unable to chroot to /mnt - something is wrong with the\n"
247                        "root partition or the way it's mounted if this doesn't work.");
248             variable_unset(DISK_PARTITIONED);
249             return DITEM_FAILURE | DITEM_RESTORE;
250         }
251         chdir("/");
252         installEnvironment();
253         systemCreateHoloshell();
254     }
255
256     saved_etc[0] = '\0';
257
258     /* Don't allow sources to be upgraded unless if we have src already */
259     if (directory_exists("/usr/src/") && (Dists & DIST_SRC)) {
260         Dists &= ~DIST_SRC;
261         SrcDists = 0;
262         msgConfirm("Warning: /usr/src exists and sources were selected as upgrade\n"
263                    "targets.  Unfortunately, this is not the way to upgrade your\n"
264                    "sources - please use CTM or CVSup or some other method which\n"
265                    "handles ``deletion events'', unlike this particular feature.\n\n"
266                    "Your existing /usr/src will not be affected by this upgrade.\n");
267     }
268
269     if (extractingBin) {
270         while (!*saved_etc) {
271             char *cp = msgGetInput("/var/tmp/etc", "Under which directory do you wish to save your current /etc?");
272
273             if (!cp || !*cp || Mkdir(cp)) {
274                 if (msgYesNo("Directory was not specified, was invalid or user selected Cancel.\n\n"
275                              "Doing an upgrade without first backing up your /etc directory is a very\n"
276                              "bad idea!  Do you want to go back and specify the save directory again?") != 0)
277                     break;
278             }
279             else {
280                 SAFE_STRCPY(saved_etc, cp);
281             }
282         }
283
284         if (saved_etc[0]) {
285             msgNotify("Preserving /etc directory..");
286             if (vsystem("tar -cBpf - -C /etc . | tar --unlink -xBpf - -C %s", saved_etc))
287                 if (msgYesNo("Unable to backup your /etc into %s.\n"
288                              "Do you want to continue anyway?", saved_etc) != 0)
289                     return DITEM_FAILURE;
290             msgNotify("Preserving /root directory..");
291             vsystem("tar -cBpf - -C / root | tar --unlink -xBpf - -C %s", saved_etc);
292         }
293
294         msgNotify("chflags'ing old binaries - please wait.");
295         (void)vsystem("chflags -R noschg /bin /sbin /usr/sbin /usr/bin /usr/lib /usr/libexec /kernel*");
296
297         if (file_readable("/kernel")) {
298             msgNotify("Moving old kernel to /kernel.prev");
299             if (system("mv /kernel /kernel.prev")) {
300                 if (!msgYesNo("Hmmm!  I couldn't move the old kernel over!  Do you want to\n"
301                               "treat this as a big problem and abort the upgrade?  Due to the\n"
302                               "way that this upgrade process works, you will have to reboot\n"
303                               "and start over from the beginning.  Select Yes to reboot now"))
304                     systemShutdown(1);
305             }
306             else 
307                 msgConfirm("NOTICE: Your old kernel is in /kernel.prev should this upgrade\n"
308                            "fail for any reason and you need to boot your old kernel");
309         }
310     }
311
312 media:
313     /* We do this very late, but we unfortunately need to back up /etc first */
314     if (!mediaVerify())
315         return DITEM_FAILURE;
316
317     if (!DEVICE_INIT(mediaDevice)) {
318         if (!msgYesNo("Couldn't initialize the media.  Would you like\n"
319                    "to adjust your media selection and try again?")) {
320             mediaDevice = NULL;
321             goto media;
322         }
323         else
324             return DITEM_FAILURE | DITEM_REDRAW | DITEM_RESTORE;
325     }
326     
327     msgNotify("Beginning extraction of distributions..");
328     if (DITEM_STATUS(distExtractAll(self)) == DITEM_FAILURE) {
329         msgConfirm("Hmmmm.  We couldn't even extract the bin distribution.  This upgrade\n"
330                    "should be considered a failure and started from the beginning, sorry!\n"
331                    "The system will reboot now.");
332         dialog_clear();
333         systemShutdown(1);
334     }
335     else if (Dists) {
336         if (!extractingBin || !(Dists & DIST_BIN)) {
337             msgNotify("The extraction process seems to have had some problems, but we got most\n"
338                        "of the essentials.  We'll treat this as a warning since it may have been\n"
339                        "only non-essential distributions which failed to load.");
340         }
341         else {
342             msgConfirm("Hmmmm.  We couldn't even extract the bin distribution.  This upgrade\n"
343                        "should be considered a failure and started from the beginning, sorry!\n"
344                        "The system will reboot now.");
345             dialog_clear();
346             systemShutdown(1);
347         }
348     }
349
350     if (extractingBin)
351         vsystem("disklabel -B `awk '$2~/\\/$/ {print substr($1, 6, 3)}' /etc/fstab`");
352     msgNotify("First stage of upgrade completed successfully!\n\n"
353                "Next comes stage 2, where we attempt to resurrect your /etc\n"
354                "directory!");
355
356     if (saved_etc && chdir(saved_etc)) {
357         msgConfirm("Unable to go to your saved /etc directory in %s?!  Argh!\n"
358                    "Something went seriously wrong!  It's quite possible that\n"
359                    "your former /etc is toast.  I hope you didn't have any\n"
360                    "important customizations you wanted to keep in there.. :(", saved_etc);
361     }
362     else {
363         /* Now try to resurrect the /etc files */
364         traverseHitlist(etc_files);
365         /* Resurrect the root dotfiles */
366         vsystem("tar -cBpf - root | tar -xBpf - -C / && rm -rf root");
367     }
368
369     msgConfirm("Upgrade completed!  All of your old /etc files have been restored.\n"
370                "For your reference, the new /etc files are in /etc/upgrade/ in case\n"
371                "you wish to upgrade these files by hand (though that should not be\n"
372                "strictly necessary).  If your root partition is specified in /etc/fstab\n"
373                "using the old \"compatibility\" slice, you may also wish to update it to\n"
374                "use a fully qualified slice name in order to avoid warnings on startup.\n\n"
375                "When you're ready to reboot into the new system, simply exit the installation.");
376     return DITEM_SUCCESS | DITEM_REDRAW | DITEM_RESTORE;
377 }
378
379 static int
380 installUpgradeNonInteractive(dialogMenuItem *self)
381 {
382     char *saved_etc;
383     Boolean extractingBin = TRUE;
384
385     variable_set2(SYSTEM_STATE, "upgrade", 0);
386
387     /* Make sure at least BIN is selected */
388     Dists |= DIST_BIN;
389
390     if (RunningAsInit) {
391         Device **devs;
392         int i, cnt;
393         char *cp;
394
395         cp = variable_get(VAR_DISK);
396         devs = deviceFind(cp, DEVICE_TYPE_DISK);
397         cnt = deviceCount(devs);
398         if (!cnt) {
399             msgConfirm("No disks found!  Please verify that your disk controller is being\n"
400                        "properly probed at boot time.  See the Hardware Guide on the\n"
401                        "Documentation menu for clues on diagnosing this type of problem.");
402             return DITEM_FAILURE;
403         }
404         else {
405             /* Enable all the drives befor we start */
406             for (i = 0; i < cnt; i++)
407                 devs[i]->enabled = TRUE;
408         }
409
410         msgConfirm("OK.  First, we're going to go to the disk label editor.  In this editor\n"
411                    "you will be expected to Mount any partitions you're interested in\n"
412                    "upgrading.  DO NOT set the Newfs flag to Y on anything in the label editor\n"
413                    "unless you're absolutely sure you know what you're doing!  In this\n"
414                    "instance, you'll be using the label editor as little more than a fancy\n"
415                    "screen-oriented partition mounting tool.\n\n"
416                    "Once you're done in the label editor, press Q to return here for the next\n"
417                    "step.");
418
419         if (DITEM_STATUS(diskLabelEditor(self)) == DITEM_FAILURE) {
420             msgConfirm("The disk label editor returned an error status.  Upgrade operation\n"
421                        "aborted.");
422             return DITEM_FAILURE;
423         }
424
425         /* Don't write out MBR info */
426         variable_set2(DISK_PARTITIONED, "written", 0);
427         if (DITEM_STATUS(diskLabelCommit(self)) == DITEM_FAILURE) {
428             msgConfirm("Not all file systems were properly mounted.  Upgrade operation\n"
429                        "aborted.");
430             variable_unset(DISK_PARTITIONED);
431             return DITEM_FAILURE;
432         }
433
434         if (extractingBin) {
435             msgNotify("chflags'ing old binaries - please wait.");
436             (void)vsystem("chflags -R noschg /mnt/");
437         }
438         msgNotify("Updating /stand on root filesystem");
439         (void)vsystem("find -x /stand | cpio %s -pdum /mnt", cpioVerbosity());
440
441         if (DITEM_STATUS(chroot("/mnt")) == DITEM_FAILURE) {
442             msgConfirm("Unable to chroot to /mnt - something is wrong with the\n"
443                        "root partition or the way it's mounted if this doesn't work.");
444             variable_unset(DISK_PARTITIONED);
445             return DITEM_FAILURE;
446         }
447         chdir("/");
448         systemCreateHoloshell();
449     }
450
451     if (!mediaVerify() || !DEVICE_INIT(mediaDevice)) {
452         msgNotify("Upgrade: Couldn't initialize media.");
453         return DITEM_FAILURE;
454     }
455
456     saved_etc = "/var/tmp/etc";
457     Mkdir(saved_etc);
458     msgNotify("Preserving /etc directory..");
459     if (vsystem("tar -cpBf - -C /etc . | tar -xpBf - -C %s", saved_etc)) {
460         msgNotify("Unable to backup your /etc into %s.", saved_etc);
461         return DITEM_FAILURE;
462     }
463
464     if (file_readable("/kernel")) {
465         msgNotify("Moving old kernel to /kernel.prev");
466         if (!system("chflags noschg /kernel && mv /kernel /kernel.prev")) {
467             /* Give us a working kernel in case we crash and reboot */
468             system("cp /kernel.prev /kernel");
469         }
470     }
471
472     msgNotify("Beginning extraction of distributions..");
473     if (DITEM_STATUS(distExtractAll(self)) == DITEM_FAILURE) {
474         msgConfirm("Hmmmm.  We couldn't even extract the bin distribution.  This upgrade\n"
475                    "should be considered a failure and started from the beginning, sorry!\n"
476                    "The system will reboot now.");
477         dialog_clear();
478         systemShutdown(1);
479     }
480     else if (Dists) {
481         if (!(Dists & DIST_BIN)) {
482             msgNotify("The extraction process seems to have had some problems, but we got most\n"
483                        "of the essentials.  We'll treat this as a warning since it may have been\n"
484                        "only non-essential distributions which failed to upgrade.");
485         }
486         else {
487             msgConfirm("Hmmmm.  We couldn't even extract the bin distribution.  This upgrade\n"
488                        "should be considered a failure and started from the beginning, sorry!\n"
489                        "The system will reboot now.");
490             dialog_clear();
491             systemShutdown(1);
492         }
493     }
494
495     msgNotify("First stage of upgrade completed successfully.");
496     if (vsystem("tar -cpBf - -C %s . | tar --unlink -xpBf - -C /etc", saved_etc)) {
497         msgNotify("Unable to resurrect your old /etc!");
498         return DITEM_FAILURE;
499     }
500     return DITEM_SUCCESS | DITEM_REDRAW;
501 }