summaryrefslogtreecommitdiffstats
path: root/upgrade.py
blob: 3b08d37e6d1a575a55f0d3c9ff7dcfb24bb779b5 (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
#
# upgrade.py - Existing install probe and upgrade procedure
#
# Matt Wilsonm <msw@redhat.com>
#
# Copyright 2001 Red Hat, Inc.
#
# This software may be freely redistributed under the terms of the GNU
# library public license.
#
# You should have received a copy of the GNU Library Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

import isys
import _balkan
import os
from translate import _
import raid
import iutil
from log import log
import os.path
from flags import flags
from partitioning import *
import fsset
import time
import rpm

def findExistingRoots (intf, id, chroot):
    if not flags.setupFilesystems: return [ (chroot, 'ext2') ]

    diskset = DiskSet()
    diskset.openDevices()
    
    win = intf.waitWindow (_("Searching"),
		    _("Searching for Red Hat Linux installations..."))

    rootparts = diskset.findExistingRootPartitions()
    win.pop ()

    return rootparts

def mountRootPartition(intf, rootInfo, oldfsset, instPath, allowDirty = 0,
		       raiseErrors = 0):
    (root, rootFs) = rootInfo

    diskset = DiskSet()
    mdList = raid.startAllRaid(diskset.driveList())

    if rootFs == "vfat":
	fsset.mountLoopbackRoot(root)
    else:
	isys.mount(root, '/mnt/sysimage')

    oldfsset.reset()
    newfsset = fsset.readFstab(instPath + '/etc/fstab')
    for entry in newfsset.entries:
        oldfsset.add(entry)

    if rootFs == "vfat":
	fsset.unmountLoopbackRoot()
    else:
	isys.umount('/mnt/sysimage')        

    raid.stopAllRaid(mdList)

    if not allowDirty and oldfsset.hasDirtyFilesystems():
        import sys
	intf.messageWindow(_("Dirty Filesystems"),
	    _("One or more of the filesystems for your Linux system "
	      "was not unmounted cleanly. Please boot your Linux "
	      "installation, let the filesystems be checked, and "
	      "shut down cleanly to upgrade."))
	sys.exit(0)

    if flags.setupFilesystems:
        oldfsset.mountFilesystems (instPath)

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

    # don't do this if we have more then 512 MB
    if mem > 510: return None

    swap = iutil.swapAmount() / 1024

    # if we have twice as much swap as ram, we're safe
    if swap >= (mem * 2):
	return None

    fsList = []

    if fsset.rootOnLoop():
	space = isys.pathSpaceAvailable("/mnt/loophost")

        for entry in fsset.entries:
            if entry.mountpoint != '/':
                continue
            
	    info = (entry.mountpoint, entry.device.getDevice(), space)
	    fsList.append(info)
    else:
        for entry in fsset.entries:
            # XXX multifsify
            if (entry.fsystem.getName() == "ext2"
                or entry.fsystem.getName() == "ext3"):
                if flags.setupFilesystems and not entry.isMounted():
                    continue
                space = isys.pathSpaceAvailable(instPath + entry.mountpoint)
                info = (entry.mountpoint, entry.device.getDevice(), space)
                fsList.append(info)

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

    return (fsList, suggestion, suggMnt)

def swapfileExists(swapname):

    try:
        rc = os.lstat(swapname)
	return 1
    except:
	return 0

# XXX fix me.
def createSwapFile(instPath, thefsset, mntPoint, size):
    fstabPath = instPath + "/etc/fstab"
    prefix = ""
    if theFstab.rootOnLoop():
	instPath = "/mnt/loophost"
	prefix = "/initrd/loopfs"

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

    existingSwaps = theFstab.swapList(files = 1)
    swapFileDict = {}
    for n in existingSwaps:
	dev = n[0]
	swapFileDict[dev] = 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

    theFstab.addMount(file, size, "swap")
    theFstab.turnOnSwap(instPath)

    f = open(fstabPath, "a")
    f.write(fstab.fstabFormatString % (prefix + file, "swap", "swap", "defaults",
	    0, 0))
    f.close()

def upgradeMountFilesystems(intf, rootInfo, oldfsset, instPath):
    # mount everything and turn on swap

    if flags.setupFilesystems:
	try:
	    mountRootPartition(intf, rootInfo, oldfsset, instPath,
                               allowDirty = 0)
	except SystemError, msg:
	    intf.messageWindow(_("Dirty Filesystems"),
		_("One or more of the filesystems listed in the "
		  "/etc/fstab on your Linux system 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' ]
	badLinks = []
	for n in checkLinks:
	    if not os.path.islink(instPath + n): continue
	    l = os.readlink(self.instPath + 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'
	    intf.messageWindow(("Absolute Symlinks"), message)
	    sys.exit(0)
    else:
	newfsset = fsset.readFstab(instPath + '/etc/fstab')
        for entry in newfsset.entries:
            oldfsset.add(entry)
        
    if flags.setupFilesystems:
        oldfsset.turnOnSwap(instPath)

rebuildTime = None

def upgradeFindPackages (intf, method, id, instPath):
    global rebuildTime
    if not rebuildTime:
	rebuildTime = str(int(time.time()))
    method.mergeFullHeaders(id.hdList)

    win = intf.waitWindow (_("Finding"),
                           _("Finding packages to upgrade..."))

    id.dbpath = "/var/lib/anaconda-rebuilddb" + rebuildTime
    rpm.addMacro("_dbpath_rebuild", id.dbpath)
    rpm.addMacro("_dbapi", "-1")

    # now, set the system clock so the timestamps will be right:
    if flags.setupFilesystems:
        iutil.setClock (instPath)
    
    # and rebuild the database so we can run the dependency problem
    # sets against the on disk db

    rebuildpath = instPath + id.dbpath
    rc = rpm.rebuilddb (instPath)
    if rc:
        try:
            iutil.rmrf (rebuildpath)
        except:
            pass
        
	win.pop()
	intf.messageWindow(_("Error"),
                           _("Rebuild of RPM database failed. "
                             "You may be out of disk space?"))
	if files.setupFilesystems:
	    fsset.umountFilesystems (instPath)
	sys.exit(0)

    rpm.addMacro("_dbpath", id.dbpath)
    rpm.addMacro("_dbapi", "3")
    try:
	packages = rpm.findUpgradeSet (id.hdList.hdlist, instPath)
    except rpm.error:
	iutil.rmrf (rebuildpath)
	win.pop()
	intf.messageWindow(_("Error"),
                           _("An error occured when finding the packages to "
                             "upgrade."))
	if flags.setupFilesystems:
	    fsset.umountFilesystems (instPath)
	sys.exit(0)
	    
    # Turn off all comps
    for comp in id.comps:
	comp.unselect()

    # unselect all packages
    for package in id.hdList.packages.values ():
	package.selected = 0

    hasX = 0
    hasFileManager = 0
    # turn on the packages in the upgrade set
    for package in packages:
	id.hdList[package[rpm.RPMTAG_NAME]].select()
	if package[rpm.RPMTAG_NAME] == "XFree86":
	    hasX = 1
	if package[rpm.RPMTAG_NAME] == "gmc":
	    hasFileManager = 1
	if package[rpm.RPMTAG_NAME] == "kdebase":
	    hasFileManager = 1

    # open up the database to check dependencies
    db = rpm.opendb (0, instPath)

    # if we have X but not gmc, we need to turn on GNOME.  We only
    # want to turn on packages we don't have installed already, though.
    if hasX and not hasFileManager:
	log ("Has X but no desktop -- Installing GNOME")
	for package in id.comps['GNOME'].pkgs:
	    try:
		rec = db.findbyname (package.name)
	    except rpm.error:
		rec = None
	    if not rec:
		log ("GNOME: Adding %s", package)
		package.select()
	
    del db

    # new package dependency fixup
    deps = id.comps.verifyDeps(instPath, 1)
    for (name, suggest) in deps:
        if name != _("no suggestion"):
            log ("Upgrade Dependency: %s needs %s, "
                 "automatically added.", name, suggest)
    id.comps.selectDeps (deps)

    win.pop ()