summaryrefslogtreecommitdiffstats
path: root/bootloader.py
blob: efed9062f4ade09c41789ac3482e75f62375c579 (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
#
# bootloader.py: bootloader configuration data
#
# Erik Troan <ewt@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 partitioning
from translate import _
from lilo import LiloConfigFile
import os
import language
from flags import flags
import iutil
import string
from log import log

initrdsMade = {}

class KernelArguments:

    def get(self):
	return self.args

    def set(self, args):
	self.args = args

    def __init__(self):
	cdrw = isys.ideCdRwList()
	str = ""
	for device in cdrw:
	    if str: str = str + " "
	    str = str + ("%s=ide-scsi" % device)

	self.args = str

class BootImages:

    # returns dictionary of (label, devtype) pairs indexed by device
    def getImages(self):
	# return a copy so users can modify it w/o affecting us

	dict = {}
	for key in self.images.keys():
	    dict[key] = self.images[key]

	return dict

    def setImageLabel(self, dev, label):
	self.images[dev] = (label, self.images[dev][1])

    # default is a device
    def setDefault(self, default):
	self.default = default

    def getDefault(self):
	return self.default

    def setup(self, diskSet, fsset):
	devices = {}
	devs = availableBootDevices(diskSet, fsset)
	for (dev, type) in devs:
	    devices[dev] = 1

	# These partitions have disappeared
	for dev in self.images.keys():
	    if devices.has_key(dev): del self.images[dev]

	# These have appeared
	for (dev, type) in devs:
	    if not self.images.has_key(dev):
                if type == "FAT":
                    self.images[dev] = ("DOS", type)
                else:
                    self.images[dev] = (None, type)

	if not self.images.has_key(self.default):
	    entry = fsset.getEntryByMountPoint('/')
	    self.default = entry.device.getDevice()
	    (label, type) = self.images[self.default]
	    if not label:
		self.images[self.default] = ("Red Hat Linux 7.2", type)

    def __init__(self):
	self.default = None
	self.images = {}
	
class x86BootloaderInfo:

    def getDevice(self):
	return self.device

    def setDevice(self, device):
	self.device = device

    def setUseGrub(self, val):
	self.useGrubVal = val

    def useGrub(self):
	return self.useGrubVal

    def writeGrub(self, instRoot, fsset, bl, langs, kernelList, chainList,
		  defaultDev, justConfigFile):
	images = bl.images.getImages()
        rootDev = fsset.getEntryByMountPoint("/").device.getDevice()
	grubRootDev = grubbyPartitionName(rootDev)

	cf = '/boot/grub/grub.conf'
	perms = 0644
        if os.access (instRoot + cf, os.R_OK):
	    perms = os.stat(instRoot + cf)[0] & 0777
	    os.rename(instRoot + cf,
		      instRoot + cf + '.rpmsave')

	f = open(instRoot + cf, "w+")

	bootDev = fsset.getEntryByMountPoint("/boot")
	grubPath = "/grub"
	cfPath = "/"
	if not bootDev:
	    bootDev = fsset.getEntryByMountPoint("/")
	    grubPath = "/boot/grub"
	    cfPath = "/boot/"
	else:
            f.write ("# NOTICE:  You have a /boot partition.  This means that\n")
            f.write ("#          all kernel paths are relative to /boot/\n")

	bootDev = bootDev.device.getDevice()

	f.write('default=0\n')
	f.write('timeout=30\n')

	for (label, version) in kernelList:
	    kernelTag = "-" + version
	    kernelFile = cfPath + "vmlinuz" + kernelTag

	    initrd = makeInitrd (kernelTag, instRoot)

	    f.write('title %s (%s)\n' % (label, version))
	    f.write('\troot %s\n' % grubbyPartitionName(bootDev))
	    f.write('\tkernel %s ro root=/dev/%s' % (kernelFile, rootDev))
	    if self.args.get():
		f.write(' %s' % self.args.get())
	    f.write('\n')

	    if os.access (instRoot + initrd, os.R_OK):
		f.write('\tinitrd %s\n' % (cfPath + initrd[len(cfPath):]))

	for (label, device) in chainList:
	    f.write('title %s\n' % (label))
	    f.write('\trootnoverify %s\n' % grubbyPartitionName(device))
            f.write('\tmakeactive\n')
            f.write('\tchainloader +1')
	    f.write('\n')

	f.close()

	part = grubbyPartitionName(bootDev)
	prefix = grubbyPartitionName(bootDev) + "/" + grubPath
	cmd = "root %s\ninstall %s/i386-pc/stage1 d (%s) %s/i386-pc/stage2 p %s%s/grub.conf" % \
	    (part, grubPath, grubbyDiskName(bl.getDevice()), grubPath,
	     part, grubPath)

	log("GRUB command %s", cmd)

	if not justConfigFile:
	    p = os.pipe()
	    os.write(p[1], cmd + '\n')
	    os.close(p[1])

	    iutil.execWithRedirect('/sbin/grub' ,
				    [ "grub", "--batch" ], stdin = p[0],
				    stdout = "/dev/tty5", stderr = "/dev/tty5",
				    root = instRoot)
	    os.close(p[0])

	return None

    def writeLilo(self, instRoot, fsset, bl, langs, kernelList, chainList,
		  defaultDev, justConfigFile):
	images = bl.images.getImages()

        # on upgrade read in the lilo config file
	lilo = LiloConfigFile ()
	perms = 0644
        if os.access (instRoot + '/etc/lilo.conf', os.R_OK):
	    perms = os.stat(instRoot + '/etc/lilo.conf')[0] & 0777
	    lilo.read (instRoot + '/etc/lilo.conf')
	    os.rename(instRoot + '/etc/lilo.conf',
		      instRoot + '/etc/lilo.conf.rpmsave')

	# Remove any invalid entries that are in the file; we probably
	# just removed those kernels. 
	for label in lilo.listImages():
	    (fsType, sl) = lilo.getImage(label)
	    if fsType == "other": continue

	    if not os.access(instRoot + sl.getPath(), os.R_OK):
		lilo.delImage(label)

	liloTarget = bl.getDevice()

	lilo.addEntry("boot", '/dev/' + liloTarget, replace = 0)
	lilo.addEntry("map", "/boot/map", replace = 0)
	lilo.addEntry("install", "/boot/boot.b", replace = 0)
	lilo.addEntry("prompt", replace = 0)
	lilo.addEntry("timeout", "50", replace = 0)
        message = "/boot/message"
        for lang in language.expandLangs(langs.getDefault()):
            fn = "/boot/message." + lang
            if os.access(instRoot + fn, os.R_OK):
                message = fn
                break

	lilo.addEntry("message", message, replace = 0)

        if not lilo.testEntry('lba32') and not lilo.testEntry('linear'):
	    lilo.addEntry("linear", replace = 0)

        rootDev = fsset.getEntryByMountPoint("/").device.getDevice()
	if not rootDev:
            raise RuntimeError, "Installing lilo, but there is no root device"

	if rootDev == defaultDev:
	    lilo.addEntry("default", kernelList[0][0])
	else:
	    lilo.addEntry("default", otherList[0][0])

	for (label, version) in kernelList:
	    kernelTag = "-" + version
	    kernelFile = "/boot/vmlinuz" + kernelTag

	    try:
		lilo.delImage(label)
	    except IndexError, msg:
		pass

	    sl = LiloConfigFile(imageType = "image", path = kernelFile)

	    initrd = makeInitrd (kernelTag, instRoot)

	    sl.addEntry("label", label)
	    if os.access (instRoot + initrd, os.R_OK):
		sl.addEntry("initrd", initrd)

	    sl.addEntry("read-only")
	    sl.addEntry("root", '/dev/' + rootDev)

	    if self.args.get():
		sl.addEntry('append', '"%s"' % self.args.get())
		
	    lilo.addImage (sl)

	for (label, device) in chainList:
	    try:
		(fsType, sl) = lilo.getImage(label)
		lilo.delImage(label)
	    except IndexError:
		sl = LiloConfigFile(imageType = "other", path = device)
		sl.addEntry("optional")

	    sl.addEntry("label", label)
	    lilo.addImage (sl)

	# Sanity check #1. There could be aliases in sections which conflict
	# with the new images we just created. If so, erase those aliases
	imageNames = {}
	for label in lilo.listImages():
	    imageNames[label] = 1

	for label in lilo.listImages():
	    (fsType, sl) = lilo.getImage(label)
	    if sl.testEntry('alias'):
		alias = sl.getEntry('alias')
		if imageNames.has_key(alias):
		    sl.delEntry('alias')
		imageNames[alias] = 1

	# Sanity check #2. If single-key is turned on, go through all of
	# the image names (including aliases) (we just built the list) and
	# see if single-key will still work.
	if lilo.testEntry('single-key'):
	    singleKeys = {}
	    turnOff = 0
	    for label in imageNames.keys():
		l = label[0]
		if singleKeys.has_key(l):
		    turnOff = 1
		singleKeys[l] = 1
	    if turnOff:
		lilo.delEntry('single-key')

	lilo.write(instRoot + "/etc/lilo.conf", perms = perms)

	if not justConfigFile:
	    # throw away stdout, catch stderr
	    str = iutil.execWithCapture(instRoot + '/sbin/lilo' ,
					[ "lilo", "-r", instRoot ],
					catchfd = 2, closefd = 1)
	else:
	    str = ""

	return str

    def write(self, instRoot, fsset, bl, langs, kernelList, chainList,
		  defaultDev, justConfig):
	if self.useGrubVal:
	    str = self.writeGrub(instRoot, fsset, bl, langs, kernelList, 
				 chainList, defaultDev, justConfig)
	else:
	    str = self.writeLilo(instRoot, fsset, bl, langs, kernelList, 
				 chainList, defaultDev, justConfig)


    def __init__(self):
	self.args = KernelArguments()
	self.images = BootImages()
	self.useGrubVal = 1		    # use lilo otherwise
	self.device = None

def availableBootDevices(diskSet, fsset):
    devs = []
    foundDos = 0
    for (dev, type) in diskSet.partitionTypes():
	if type == 'FAT' and not foundDos:
	    foundDos = 1
	    isys.makeDevInode(dev, '/tmp/' + dev)

	    try:
		bootable = isys.checkBoot('/tmp/' + dev)
		devs.append((dev, type))
	    except:
		pass
	elif type == 'ntfs' or type =='hpfs':
	    devs.append((dev, type))

    devs.append((fsset.getEntryByMountPoint('/').device.getDevice(), 'ext2'))

    devs.sort()

    return devs

def partitioningComplete(dispatch, bl, fsset, diskSet):
    choices = fsset.bootloaderChoices(diskSet)
    if not choices:
	dispatch.skipStep("instbootloader")
    else:
	dispatch.skipStep("instbootloader", skip = 0)

    bl.images.setup(diskSet, fsset)

def writeBootloader(intf, instRoot, fsset, bl, langs, comps):
    justConfigFile = not flags.setupFilesystems

    w = intf.waitWindow(_("Bootloader"), _("Installing bootloader..."))

    kernelList = []
    otherList = []
    rootDev = fsset.getEntryByMountPoint('/').device.getDevice()
    defaultDev = bl.images.getDefault()

    for (dev, (label, type)) in bl.images.getImages().items():
	if dev == rootDev:
	    kernelLabel = label
	elif dev == defaultDev:
	    otherList = [(label, dev)] + otherList
	else:
	    otherList.append(label, dev)

    plainLabelUsed = 0
    for (version, nick) in comps.kernelVersionList():
	if plainLabelUsed:
	    kernelList.append(kernelLabel + "-" + nick, version)
	else:
	    kernelList.append(kernelLabel, version)
	    plainLabelUsed = 1

    bl.write(instRoot, fsset, bl, langs, kernelList, otherList, defaultDev,
	     justConfigFile)

    w.pop()

def makeInitrd (kernelTag, instRoot):
    global initrdsMade

    initrd = "/boot/initrd%s.img" % (kernelTag, )
    
    if not initrdsMade.has_key(initrd) and flags.setupFilesystems:
	iutil.execWithRedirect("/sbin/mkinitrd",
			      [ "/sbin/mkinitrd",
				"--ifneeded",
				"-f",
				initrd,
				kernelTag[1:] ],
			      stdout = None, stderr = None, searchPath = 1,
			      root = instRoot)
	initrdsMade[kernelTag] = 1

    return initrd

def grubbyDiskName(name):
    drives = isys.hardDriveDict().keys()
    drives.sort (isys.compareDrives)

    return "hd%d" % drives.index(name)

def grubbyPartitionName(dev):
    cut = -1
    if dev[-2] in string.digits:
	cut = -2

    partNum = int(dev[cut:]) - 1
    name = dev[:cut]

    # hack off the trailing 'p' from /dev/cciss/*, for example
    if name[-1] == 'p':
	for letter in name:
	    if letter not in string.letters and letter != "/":
		name = name[:-1]
		break

    return "(%s,%d)" % (grubbyDiskName(name), partNum)