summaryrefslogtreecommitdiffstats
path: root/upgrade.py
blob: 4aee58c7ac82164edefcaca61b7b975899bc1cb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#
# upgrade.py - Existing install probe and upgrade procedure
#
# Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007  Red Hat, Inc.
# All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Author(s): Matt Wilson <msw@redhat.com>
#

import isys
import os
import iutil
import time
import sys
import os.path
import partedUtils
import string
import selinux
import lvm
from flags import flags
from fsset import *
from constants import *
from product import productName

import rhpl
import rhpl.arch

import gettext
_ = lambda x: gettext.ldgettext("anaconda", x)

import rpm

import logging
log = logging.getLogger("anaconda")

def guessGuestArch(rootdir):
    """root path -> None|"architecture"
    Guess the architecture of installed system
    """

    ts = rpm.ts(rootdir)

    packages = ["filesystem", "initscripts"]

    #get information from packages
    for pkg in packages:
        try:
            mi=ts.dbMatch("name",pkg)
            for hdr in mi:
                return hdr["arch"]
        except:
            pass

    return None


def isUpgradingArch(anaconda):
    """anaconda -> (bool, oldarch)
    Check if the upgrade should change architecture of instalation"""

    try:
        rpmplatform = open(anaconda.rootPath+"/etc/rpm/platform").readline().strip()
        rpmarch = rpmplatform[:rpmplatform.index("-")]
        return rhpl.arch.canonArch!=rpmarch, rpmarch
    except IOError:
        #try some fallback methods
        rpmarch = guessGuestArch(anaconda.rootPath)
        if rpmarch:
            return rhpl.arch.canonArch!=rpmarch, rpmarch
        else:
            return False, "unknown"

def queryUpgradeArch(anaconda):
    archupgrade, oldrpmarch = isUpgradingArch(anaconda) #Check if we are to upgrade the architecture of previous product

    if anaconda.dir == DISPATCH_FORWARD or not archupgrade:
        return DISPATCH_FORWARD

    rc = anaconda.intf.messageWindow(_("Proceed with upgrade?"),
                       _("You have choosen the upgrade for %s "
                         "architecture, but the installed system "
                         "is for %s architecture. "
                         "\n\n" % (rhpl.arch.canonArch, oldrpmarch,)) + 
                       _("Would you like to upgrade "
                         " the installed system to the %s architecture?" % (rhpl.arch.canonArch,)),
                         type="custom", custom_icon=["error","error"],
                         custom_buttons=[_("_Exit installer"), _("_Continue")])

    if rc == 0:
        sys.exit(0)

    flags.updateRpmPlatform = True

    return DISPATCH_FORWARD

def queryUpgradeContinue(anaconda):
    if anaconda.dir == DISPATCH_FORWARD:
        return

    rc = anaconda.intf.messageWindow(_("Proceed with upgrade?"),
                       _("The file systems of the Linux installation "
                         "you have chosen to upgrade have already been "
                         "mounted. You cannot go back past this point. "
                         "\n\n") + 
                       _("Would you like to continue with the upgrade?"),
                         type="custom", custom_icon=["error","error"],
                         custom_buttons=[_("_Exit installer"), _("_Continue")])
    if rc == 0:
        sys.exit(0)
    return DISPATCH_FORWARD

def findRootParts(anaconda):
    if anaconda.dir == DISPATCH_BACK:
        return
    if anaconda.id.rootParts is None:
        anaconda.id.rootParts = findExistingRoots(anaconda)

    anaconda.id.upgradeRoot = []
    for (dev, fs, meta, label) in anaconda.id.rootParts:
        anaconda.id.upgradeRoot.append( (dev, fs) )

    if anaconda.id.rootParts is not None and len(anaconda.id.rootParts) > 0:
        anaconda.dispatch.skipStep("findinstall", skip = 0)
        if productName.find("Red Hat Enterprise Linux") == -1:
            anaconda.dispatch.skipStep("installtype", skip = 1)
    else:
        anaconda.dispatch.skipStep("findinstall", skip = 1)
        anaconda.dispatch.skipStep("installtype", skip = 0)

def findExistingRoots(anaconda, upgradeany = 0):
    if not flags.setupFilesystems:
        relstr = partedUtils.getReleaseString (anaconda.rootPath)
        if ((flags.cmdline.has_key("upgradeany")) or
            (upgradeany == 1) or
            (partedUtils.productMatches(relstr, productName))):
            return [(anaconda.rootPath, 'ext2', "")]
        return []

    anaconda.id.diskset.openDevices()
    anaconda.id.partitions.getEncryptedDevices(anaconda.id.diskset)
    rootparts = anaconda.id.diskset.findExistingRootPartitions(upgradeany = upgradeany)

    # close the devices to make sure we don't leave things sitting open 
    anaconda.id.diskset.closeDevices()

    # this is a hack... need to clear the skipped disk list after this
    partedUtils.DiskSet.skippedDisks = []
    partedUtils.DiskSet.exclusiveDisks = []

    return rootparts

def getDirtyDevString(dirtyDevs):
    ret = ""
    for dev in dirtyDevs:
        if dev != "loop":
            ret = "/dev/%s\n" % (dev,)
        else:
            ret = "%s\n" % (dev,)
    return ret

def mountRootPartition(anaconda, rootInfo, oldfsset, allowDirty = 0,
		       warnDirty = 0, readOnly = 0):
    (root, rootFs) = rootInfo
    bindMount = 0

    encryptedDevices = anaconda.id.partitions.encryptedDevices
    diskset = partedUtils.DiskSet(anaconda)
    diskset.openDevices()
    for cryptoDev in encryptedDevices.values():
        cryptoDev.openDevice()
    diskset.startMPath()
    diskset.startDmRaid()
    diskset.startMdRaid()
    for cryptoDev in encryptedDevices.values():
        cryptoDev.openDevice()
    lvm.vgscan()
    lvm.vgactivate()
    for cryptoDev in encryptedDevices.values():
        cryptoDev.openDevice()

    if root in anaconda.id.partitions.protectedPartitions() and os.path.ismount("/mnt/isodir"):
        root = "/mnt/isodir"
        bindMount = 1

    log.info("going to mount %s on %s as %s" %(root, anaconda.rootPath, rootFs))
    isys.mount(root, anaconda.rootPath, rootFs, bindMount=bindMount)

    oldfsset.reset()
    newfsset = readFstab(anaconda)

    for entry in newfsset.entries:
        oldfsset.add(entry)

    if not bindMount:
        isys.umount(anaconda.rootPath)

    dirtyDevs = oldfsset.hasDirtyFilesystems(anaconda.rootPath)
    if not allowDirty and dirtyDevs != []:
        lvm.vgdeactivate()
        diskset.stopMdRaid()
        diskset.stopDmRaid()
        diskset.stopMPath()
        anaconda.intf.messageWindow(_("Dirty File Systems"),
                           _("The following file systems for your Linux system "
                             "were not unmounted cleanly.  Please boot your "
                             "Linux installation, let the file systems be "
                             "checked and shut down cleanly to upgrade.\n"
                             "%s" %(getDirtyDevString(dirtyDevs),)))
        sys.exit(0)
    elif warnDirty and dirtyDevs != []:
        rc = anaconda.intf.messageWindow(_("Dirty File Systems"),
                                _("The following file systems for your Linux "
                                  "system were not unmounted cleanly.  Would "
                                  "you like to mount them anyway?\n"
                                  "%s" % (getDirtyDevString(dirtyDevs,))),
                                type = "yesno")
        if rc == 0:
            return -1

    if flags.setupFilesystems:
        for dev, crypto in encryptedDevices.items():
            if crypto.openDevice():
                log.error("failed to open encrypted device %s" % (dev,))

        oldfsset.mountFilesystems(anaconda, readOnly = readOnly,
                                  skiprootfs = bindMount)

    rootEntry = oldfsset.getEntryByMountPoint("/")
    if (not rootEntry or not rootEntry.fsystem or not rootEntry.fsystem.isMountable()):
        raise RuntimeError, "/etc/fstab did not list a fstype for the root partition which we support"

def bindMountDevDirectory(instPath):
    fs = fileSystemTypeGet("bind")
    fs.mount("/dev", "/dev", bindMount=1, instroot=instPath)

# returns None if no filesystem exist to migrate
def upgradeMigrateFind(anaconda):
    migents = anaconda.id.fsset.getMigratableEntries()
    if not migents or len(migents) < 1:
        anaconda.dispatch.skipStep("upgrademigratefs")
    else:
        anaconda.dispatch.skipStep("upgrademigratefs", skip = 0)
    

# returns None if no more swap is needed
def upgradeSwapSuggestion(anaconda):
    # mem is in kb -- round it up to the nearest 4Mb
    mem = iutil.memInstalled()
    rem = mem % 16384
    if rem:
	mem = mem + (16384 - rem)
    mem = mem / 1024

    anaconda.dispatch.skipStep("addswap", 0)
    
    # don't do this if we have more then 250 MB
    if mem > 250:
        anaconda.dispatch.skipStep("addswap", 1)
        return
    
    swap = iutil.swapAmount() / 1024

    # if we have twice as much swap as ram and at least 192 megs
    # total, we're safe 
    if (swap >= (mem * 1.5)) and (swap + mem >= 192):
        anaconda.dispatch.skipStep("addswap", 1)
	return

    # if our total is 512 megs or more, we should be safe
    if (swap + mem >= 512):
        anaconda.dispatch.skipStep("addswap", 1)
	return

    fsList = []

    for entry in anaconda.id.fsset.entries:
        if entry.fsystem.getName() in getUsableLinuxFs():
            if flags.setupFilesystems and not entry.isMounted():
                continue
            space = isys.pathSpaceAvailable(anaconda.rootPath + entry.mountpoint)
            if space > 16:
                info = (entry.mountpoint, entry.device.getDevice(), space)
                fsList.append(info)

    suggestion = mem * 2 - swap
    if (swap + mem + suggestion) < 192:
        suggestion = 192 - (swap + mem)
    if suggestion < 32:
        suggestion = 32
    suggSize = 0
    suggMnt = None
    for (mnt, part, size) in fsList:
	if (size > suggSize) and (size > (suggestion + 100)):
	    suggMnt = mnt

    anaconda.id.upgradeSwapInfo = (fsList, suggestion, suggMnt)

def swapfileExists(swapname):
    try:
        os.lstat(swapname)
	return 1
    except:
	return 0

def createSwapFile(instPath, theFsset, mntPoint, size):
    fstabPath = instPath + "/etc/fstab"
    prefix = ""

    if mntPoint != "/":
        file = mntPoint + "/SWAP"
    else:
        file = "/SWAP"

    swapFileDict = {}
    for entry in theFsset.entries:
        if entry.fsystem.getName() == "swap":
            swapFileDict[entry.device.getName()] = 1
        
    count = 0
    while (swapfileExists(instPath + file) or 
	   swapFileDict.has_key(file)):
	count = count + 1
	tmpFile = "/SWAP-%d" % (count)
        if mntPoint != "/":
            file = mntPoint + tmpFile
        else:
            file = tmpFile

    device = SwapFileDevice(file)
    device.setSize(size)
    fsystem = fileSystemTypeGet("swap")
    entry = FileSystemSetEntry(device, "swap", fsystem)
    entry.setFormat(1)
    theFsset.add(entry)
    theFsset.formatEntry(entry, instPath)
    theFsset.turnOnSwap(instPath, upgrading=True)

    # XXX generalize fstab modification
    f = open(fstabPath, "a")
    format = "%-23s %-23s %-7s %-15s %d %d\n";
    f.write(format % (prefix + file, "swap", "swap", "defaults", 0, 0))
    f.close()

# XXX handle going backwards
def upgradeMountFilesystems(anaconda):
    # mount everything and turn on swap

    if flags.setupFilesystems:
	try:
	    mountRootPartition(anaconda, anaconda.id.upgradeRoot[0], anaconda.id.fsset,
                               allowDirty = 0)
        except SystemError:
	    anaconda.intf.messageWindow(_("Mount failed"),
		_("One or more of the file systems listed in the "
		  "/etc/fstab on your Linux system cannot be mounted. "
		  "Please fix this problem and try to upgrade again."))
	    sys.exit(0)
        except RuntimeError:
            anaconda.intf.messageWindow(_("Mount failed"),
		_("One or more of the file systems listed in the "
                  "/etc/fstab of your Linux system are inconsistent and "
                  "cannot be mounted.  Please fix this problem and try to "
                  "upgrade again."))
            sys.exit(0)

	checkLinks = ( '/etc', '/var', '/var/lib', '/var/lib/rpm',
		       '/boot', '/tmp', '/var/tmp', '/root',
                       '/bin/sh', '/usr/tmp')
	badLinks = []
	for n in checkLinks:
	    if not os.path.islink(anaconda.rootPath + n): continue
	    l = os.readlink(anaconda.rootPath + n)
	    if l[0] == '/':
		badLinks.append(n)

	if badLinks:
	    message = _("The following files are absolute symbolic " 
			"links, which we do not support during an " 
			"upgrade. Please change them to relative "
			"symbolic links and restart the upgrade.\n\n")
	    for n in badLinks:
		message = message + '\t' + n + '\n'
	    anaconda.intf.messageWindow(_("Absolute Symlinks"), message)
	    sys.exit(0)

        # fix for 80446
        badLinks = []
        mustBeLinks = ( '/usr/tmp', )
        for n in mustBeLinks:
            if not os.path.islink(anaconda.rootPath + n):
                badLinks.append(n)

        if badLinks: 
	    message = _("The following are directories which should instead "
                        "be symbolic links, which will cause problems with the "
                        "upgrade.  Please return them to their original state "
                        "as symbolic links and restart the upgrade.\n\n")
            for n in badLinks:
                message = message + '\t' + n + '\n'
	    anaconda.intf.messageWindow(_("Invalid Directories"), message)
	    sys.exit(0)
           
        bindMountDevDirectory(anaconda.rootPath)
    else:
        if not os.access (anaconda.rootPath + "/etc/fstab", os.R_OK):
            anaconda.intf.messageWindow(_("Warning"),
                                        _("%s not found")
                                        % (anaconda.rootPath + "/etc/fstab",),
                                        type="ok")
            return DISPATCH_BACK
            
	newfsset = readFstab(anaconda)
        for entry in newfsset.entries:
            anaconda.id.fsset.add(entry)
    if flags.setupFilesystems:
        if iutil.isPPC():
            anaconda.id.fsset.formatSwap(anaconda.rootPath, forceFormat=True)
        anaconda.id.fsset.turnOnSwap(anaconda.rootPath, upgrading=True)
        anaconda.id.fsset.mkDevRoot(anaconda.rootPath)

    # if they've been booting with selinux disabled, then we should
    # disable it during the install as well (#242510)
    try:
        if os.path.exists(anaconda.rootPath + "/.autorelabel"):
            ctx = selinux.getfilecon(anaconda.rootPath + "/.autorelabel")[1]
            if not ctx or ctx == "unlabeled":
                flags.selinux = False
                log.info("Disabled SELinux for upgrade based on /.autorelabel")
    except Exception, e:
        log.warning("error checking selinux state: %s" %(e,))

def setSteps(anaconda):
    dispatch = anaconda.dispatch
    dispatch.setStepList(
                "language",
                "keyboard",
                "welcome",
                "installtype",
                "findrootparts",
                "findinstall",
                "partitionobjinit",
                "upgrademount",
                "upgrademigfind",
                "upgrademigratefs",
                "upgradearchitecture",
                "upgradecontinue",
                "reposetup",
                "upgbootloader",
                "checkdeps",
                "dependencies",
                "confirmupgrade",
                "postselection",
                "install",
                "migratefilesystems",
                "preinstallconfig",
                "installpackages",
                "postinstallconfig",
                "instbootloader",
                "dopostaction",
                "methodcomplete",
                "postscripts",
                "copylogs",
                "complete"
            )

    if not iutil.isX86():
        dispatch.skipStep("bootloader")

    if not iutil.isX86():
        dispatch.skipStep("upgbootloader")