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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
|
import os
import string
from booty import BootyNoKernelWarning
from util import getDiskPart
from bootloaderInfo import *
from flags import flags
import checkbootloader
import iutil
class x86BootloaderInfo(efiBootloaderInfo):
def setPassword(self, val, isCrypted = 1):
if not val:
self.password = val
self.pure = val
return
if isCrypted and self.useGrubVal == 0:
self.pure = None
return
elif isCrypted:
self.password = val
self.pure = None
else:
salt = "$1$"
saltLen = 8
saltchars = string.letters + string.digits + './'
for i in range(saltLen):
salt += random.choice(saltchars)
self.password = crypt.crypt(val, salt)
self.pure = val
def getPassword (self):
return self.pure
def setUseGrub(self, val):
self.useGrubVal = val
def getPhysicalDevices(self, device):
# This finds a list of devices on which the given device name resides.
# Accepted values for "device" are raid1 md devices (i.e. "md0"),
# physical disks ("hda"), and real partitions on physical disks
# ("hda1"). Volume groups/logical volumes are not accepted.
path = self.storage.devicetree.getDeviceByName(device).path[5:]
if device in map (lambda x: x.name, self.storage.lvs + self.storage.vgs):
return []
if path.startswith("mapper/luks-"):
return []
if path.startswith('md'):
if flags.cmdline.has_key("iswmd"):
return [device]
else:
bootable = 0
parts = checkbootloader.getRaidDisks(device, self.storage,
raidLevel=1, stripPart=0)
parts.sort()
return parts
return [device]
def runGrubInstall(self, instRoot, bootDev, cmds, cfPath):
if cfPath == "/":
syncDataToDisk(bootDev, "/boot", instRoot)
else:
syncDataToDisk(bootDev, "/", instRoot)
# copy the stage files over into /boot
rc = iutil.execWithRedirect("/sbin/grub-install",
["--just-copy"],
stdout = "/dev/tty5", stderr = "/dev/tty5",
root = instRoot)
if rc:
return rc
# really install the bootloader
for cmd in cmds:
p = os.pipe()
os.write(p[1], cmd + '\n')
os.close(p[1])
# FIXME: hack to try to make sure everything is written
# to the disk
if cfPath == "/":
syncDataToDisk(bootDev, "/boot", instRoot)
else:
syncDataToDisk(bootDev, "/", instRoot)
rc = iutil.execWithRedirect('/sbin/grub' ,
[ "--batch", "--no-floppy",
"--device-map=/boot/grub/device.map" ],
stdin = p[0],
stdout = "/dev/tty5", stderr = "/dev/tty5",
root = instRoot)
os.close(p[0])
if rc:
return rc
def installGrub(self, instRoot, bootDevs, grubTarget, grubPath,
target, cfPath):
if iutil.isEfi():
return efiBootloaderInfo.installGrub(self, instRoot, bootDevs, grubTarget,
grubPath, target, cfPath)
args = "--stage2=/boot/grub/stage2 "
for bootDev in bootDevs:
cmds = []
gtPart = self.getMatchingPart(bootDev, grubTarget)
gtDisk = self.grubbyPartitionName(getDiskPart(gtPart, self.storage)[0])
bPart = self.grubbyPartitionName(bootDev)
cmd = "root %s\n" % (bPart,)
stage1Target = gtDisk
if target == "partition":
stage1Target = self.grubbyPartitionName(gtPart)
cmd += "install %s%s/stage1 d %s %s/stage2 p %s%s/grub.conf" % \
(args, grubPath, stage1Target, grubPath, bPart, grubPath)
cmds.append(cmd)
rc = self.runGrubInstall(instRoot, bootDev, cmds, cfPath)
if rc:
return rc
return 0
def writeGrub(self, instRoot, bl, kernelList, chainList,
defaultDev, justConfigFile):
rootDev = self.storage.fsset.rootDevice
# XXX old config file should be read here for upgrade
cf = "%s%s" % (instRoot, self.configfile)
self.perms = 0600
if os.access (cf, os.R_OK):
self.perms = os.stat(cf)[0] & 0777
os.rename(cf, cf + '.rpmsave')
grubTarget = bl.getDevice()
path = self.storage.devicetree.getDeviceByName(grubTarget).path[5:]
target = "mbr"
if (path.startswith('rd/') or path.startswith('ida/') or
path.startswith('cciss/') or
path.startswith('sx8/') or
path.startswith('mapper/')):
if grubTarget[-1].isdigit():
if grubTarget[-2] == 'p' or \
(grubTarget[-2].isdigit() and grubTarget[-3] == 'p'):
target = "partition"
elif grubTarget[-1].isdigit() and not path.startswith('md'):
target = "partition"
f = open(cf, "w+")
f.write("# grub.conf generated by anaconda\n")
f.write("#\n")
f.write("# Note that you do not have to rerun grub "
"after making changes to this file\n")
try:
bootDev = self.storage.fsset.mountpoints["/boot"]
grubPath = "/grub"
cfPath = "/"
f.write("# NOTICE: You have a /boot partition. This means "
"that\n")
f.write("# all kernel and initrd paths are relative "
"to /boot/, eg.\n")
except KeyError:
bootDev = self.storage.fsset.rootDevice
grubPath = "/boot/grub"
cfPath = "/boot/"
f.write("# NOTICE: You do not have a /boot partition. "
"This means that\n")
f.write("# all kernel and initrd paths are relative "
"to /, eg.\n")
bootDevs = self.getPhysicalDevices(bootDev.name)
f.write('# root %s\n' % self.grubbyPartitionName(bootDevs[0]))
f.write("# kernel %svmlinuz-version ro root=%s\n" % (cfPath, rootDev.path))
f.write("# initrd %sinitrd-version.img\n" % (cfPath))
f.write("#boot=/dev/%s\n" % (grubTarget))
# get the default image to boot... we have to walk and find it
# since grub indexes by where it is in the config file
if defaultDev.name == rootDev.name:
default = 0
else:
# if the default isn't linux, it's the first thing in the
# chain list
default = len(kernelList)
# keep track of which devices are used for the device.map
usedDevs = {}
f.write('default=%s\n' % (default))
f.write('timeout=%d\n' % (self.timeout or 0))
if self.serial == 1:
# grub the 0-based number of the serial console device
unit = self.serialDevice[-1]
# and we want to set the speed too
speedend = 0
for char in self.serialOptions:
if char not in string.digits:
break
speedend = speedend + 1
if speedend != 0:
speed = self.serialOptions[:speedend]
else:
# reasonable default
speed = "9600"
f.write("serial --unit=%s --speed=%s\n" %(unit, speed))
f.write("terminal --timeout=%s serial console\n" % (self.timeout or 5))
else:
# we only want splashimage if they're not using a serial console
if os.access("%s/boot/grub/splash.xpm.gz" %(instRoot,), os.R_OK):
f.write('splashimage=%s%sgrub/splash.xpm.gz\n'
% (self.grubbyPartitionName(bootDevs[0]), cfPath))
f.write("hiddenmenu\n")
for dev in self.getPhysicalDevices(grubTarget):
usedDevs[dev] = 1
if self.password:
f.write('password --md5 %s\n' %(self.password))
for (label, longlabel, version) in kernelList:
kernelTag = "-" + version
kernelFile = "%svmlinuz%s" % (cfPath, kernelTag)
initrd = self.makeInitrd(kernelTag)
f.write('title %s (%s)\n' % (longlabel, version))
f.write('\troot %s\n' % self.grubbyPartitionName(bootDevs[0]))
realroot = " root=%s" % rootDev.fstabSpec
if version.endswith("xen0") or (version.endswith("xen") and not os.path.exists("/proc/xen")):
# hypervisor case
sermap = { "ttyS0": "com1", "ttyS1": "com2",
"ttyS2": "com3", "ttyS3": "com4" }
if self.serial and sermap.has_key(self.serialDevice) and \
self.serialOptions:
hvs = "%s=%s" %(sermap[self.serialDevice],
self.serialOptions)
else:
hvs = ""
if version.endswith("xen0"):
hvFile = "%sxen.gz-%s %s" %(cfPath,
version.replace("xen0", ""),
hvs)
else:
hvFile = "%sxen.gz-%s %s" %(cfPath,
version.replace("xen", ""),
hvs)
f.write('\tkernel %s\n' %(hvFile,))
f.write('\tmodule %s ro%s' %(kernelFile, realroot))
if self.args.get():
f.write(' %s' % self.args.get())
f.write('\n')
if os.access (instRoot + initrd, os.R_OK):
f.write('\tmodule %sinitrd%s.img\n' % (cfPath, kernelTag))
else: # normal kernel
f.write('\tkernel %s ro%s' % (kernelFile, realroot))
if self.args.get():
f.write(' %s' % self.args.get())
f.write('\n')
if os.access (instRoot + initrd, os.R_OK):
f.write('\tinitrd %sinitrd%s.img\n' % (cfPath, kernelTag))
for (label, longlabel, device) in chainList:
if ((not longlabel) or (longlabel == "")):
continue
f.write('title %s\n' % (longlabel))
f.write('\trootnoverify %s\n' % self.grubbyPartitionName(device))
# f.write('\tmakeactive\n')
f.write('\tchainloader +1')
f.write('\n')
usedDevs[device] = 1
f.close()
if not "/efi/" in cf:
os.chmod(cf, self.perms)
try:
# make symlink for menu.lst (default config file name)
menulst = "%s%s/menu.lst" % (instRoot, self.configdir)
if os.access (menulst, os.R_OK):
os.rename(menulst, menulst + ".rpmsave")
os.symlink("./grub.conf", menulst)
except:
pass
try:
# make symlink for /etc/grub.conf (config files belong in /etc)
etcgrub = "%s%s" % (instRoot, "/etc/grub.conf")
if os.access (etcgrub, os.R_OK):
os.rename(etcgrub, etcgrub + ".rpmsave")
os.symlink(".." + self.configfile, etcgrub)
except:
pass
for dev in self.getPhysicalDevices(rootDev.name) + bootDevs:
usedDevs[dev] = 1
if os.access(instRoot + "/boot/grub/device.map", os.R_OK):
os.rename(instRoot + "/boot/grub/device.map",
instRoot + "/boot/grub/device.map.rpmsave")
f = open(instRoot + "/boot/grub/device.map", "w+")
f.write("# this device map was generated by anaconda\n")
devs = usedDevs.keys()
usedDevs = {}
for dev in devs:
drive = getDiskPart(dev, self.storage)[0]
if usedDevs.has_key(drive):
continue
usedDevs[drive] = 1
devs = usedDevs.keys()
devs.sort()
for drive in devs:
# XXX hack city. If they're not the sort of thing that'll
# be in the device map, they shouldn't still be in the list.
path = self.storage.devicetree.getDeviceByName(drive).path
if ((drive.startswith('md') and flags.cmdline.has_key("iswmd")) or
not drive.startswith('md')):
f.write("(%s) %s\n" % (self.grubbyDiskName(drive), path))
f.close()
sysconf = '/etc/sysconfig/grub'
if os.access (instRoot + sysconf, os.R_OK):
self.perms = os.stat(instRoot + sysconf)[0] & 0777
os.rename(instRoot + sysconf,
instRoot + sysconf + '.rpmsave')
# if it's an absolute symlink, just get it out of our way
elif (os.path.islink(instRoot + sysconf) and
os.readlink(instRoot + sysconf)[0] == '/'):
os.rename(instRoot + sysconf,
instRoot + sysconf + '.rpmsave')
f = open(instRoot + sysconf, 'w+')
f.write("boot=/dev/%s\n" %(grubTarget,))
f.write("forcelba=0\n")
f.close()
if not justConfigFile:
return self.installGrub(instRoot, bootDevs, grubTarget, grubPath,
target, cfPath)
return 0
def getMatchingPart(self, bootDev, target):
bootName, bootPartNum = getDiskPart(bootDev, self.storage)
devices = self.getPhysicalDevices(target)
for device in devices:
name, partNum = getDiskPart(device, self.storage)
if name == bootName:
return device
return devices[0]
def grubbyDiskName(self, name):
return "hd%d" % self.drivelist.index(name)
def grubbyPartitionName(self, dev):
(name, partNum) = getDiskPart(dev, self.storage)
if partNum != None:
return "(%s,%d)" % (self.grubbyDiskName(name), partNum)
else:
return "(%s)" %(self.grubbyDiskName(name))
def getBootloaderConfig(self, instRoot, bl, kernelList,
chainList, defaultDev):
config = bootloaderInfo.getBootloaderConfig(self, instRoot,
bl, kernelList, chainList,
defaultDev)
liloTarget = bl.getDevice()
config.addEntry("boot", '/dev/' + liloTarget, replace = 0)
config.addEntry("map", "/boot/map", replace = 0)
config.addEntry("install", "/boot/boot.b", replace = 0)
message = "/boot/message"
if self.pure is not None and not self.useGrubVal:
config.addEntry("restricted", replace = 0)
config.addEntry("password", self.pure, replace = 0)
if self.serial == 1:
# grab the 0-based number of the serial console device
unit = self.serialDevice[-1]
# FIXME: we should probably put some options, but lilo
# only supports up to 9600 baud so just use the defaults
# it's better than nothing :(
config.addEntry("serial=%s" %(unit,))
else:
# message screws up serial console
if os.access(instRoot + message, os.R_OK):
config.addEntry("message", message, replace = 0)
if not config.testEntry('lba32'):
if bl.above1024 and not iutil.isX86(bits=32):
config.addEntry("lba32", replace = 0)
return config
def updateDeviceMap(self, instRoot):
if os.access(instRoot + "/boot/grub/device.map", os.R_OK):
f = open(instRoot + "/boot/grub/device.map", "r")
updatedlines = []
update = False
for line in f:
line = line.strip()
if line.startswith('(hd'):
(grubdisk, path) = line.split()[:2]
i = int(grubdisk.lstrip('(hd ').rstrip(') '))
actual_path = self.storage.devicetree.getDeviceByName(self.drivelist[i]).path
if path != actual_path:
line = "%s %s" % (grubdisk, actual_path)
update = True
updatedlines.append(line)
f.close()
if update:
os.rename(instRoot + "/boot/grub/device.map",
instRoot + "/boot/grub/device.map.rpmsave")
f = open(instRoot + "/boot/grub/device.map", "w+")
upd_comment = "# file updated by anaconda\n"
f.write(upd_comment + '\n'.join(updatedlines) + '\n')
f.close()
# this is a hackish function that depends on the way anaconda writes
# out the grub.conf with a #boot= comment
# XXX this falls into the category of self.doUpgradeOnly
def upgradeGrub(self, instRoot, bl, kernelList, chainList,
defaultDev, justConfigFile):
if justConfigFile:
return ""
theDev = None
for (fn, stanza) in [ ("/etc/sysconfig/grub", "boot="),
("/boot/grub/grub.conf", "#boot=") ]:
try:
f = open(instRoot + fn, "r")
except:
continue
# the following bits of code are straight from checkbootloader.py
lines = f.readlines()
f.close()
for line in lines:
if line.startswith(stanza):
theDev = checkbootloader.getBootDevString(line)
break
if theDev is not None:
break
if theDev is None:
# we could find the dev before, but can't now... cry about it
return ""
# migrate info to /etc/sysconfig/grub
self.writeSysconfig(instRoot, theDev)
# update device.map
self.updateDeviceMap(instRoot)
# more suckage. grub-install can't work without a valid /etc/mtab
# so we have to do shenanigans to get updated grub installed...
# steal some more code above
try:
bootDev = self.storage.fsset.mountpoints["/boot"].name
grubPath = "/grub"
cfPath = "/"
except KeyError:
bootDev = self.storage.fsset.rootDevice.name
grubPath = "/boot/grub"
cfPath = "/boot/"
masterBootDev = bootDev
if masterBootDev[0:2] == 'md':
rootDevs = checkbootloader.getRaidDisks(masterBootDev,
self.storage, raidLevel=1, stripPart=0)
else:
rootDevs = [masterBootDev]
if theDev[5:7] == 'md':
stage1Devs = checkbootloader.getRaidDisks(theDev[5:], self.storage,
raidLevel=1)
else:
stage1Devs = [theDev[5:]]
cmds = []
for stage1Dev in stage1Devs:
# cross fingers; if we can't find a root device on the same
# hardware as this boot device, we just blindly hope the first
# thing in the list works.
grubbyStage1Dev = self.grubbyPartitionName(stage1Dev)
grubbyRootPart = self.grubbyPartitionName(rootDevs[0])
for rootDev in rootDevs:
testGrubbyRootDev = getDiskPart(rootDev, self.storage)[0]
testGrubbyRootDev = self.grubbyPartitionName(testGrubbyRootDev)
if grubbyStage1Dev == testGrubbyRootDev:
grubbyRootPart = self.grubbyPartitionName(rootDev)
break
args = "--stage2=/boot/grub/stage2 "
cmd ="root %s\n" % (grubbyRootPart,)
cmd += "install %s%s/stage1 d %s %s/stage2 p %s%s/grub.conf" \
% (args, grubPath, grubbyStage1Dev, grubPath, grubbyRootPart,
grubPath)
cmds.append(cmd)
if not justConfigFile:
return self.runGrubInstall(instRoot, bootDev, cmds, cfPath)
return 0
def writeSysconfig(self, instRoot, installDev):
sysconf = '/etc/sysconfig/grub'
if not os.access(instRoot + sysconf, os.R_OK):
f = open(instRoot + sysconf, "w+")
f.write("boot=%s\n" %(installDev,))
f.write("forcelba=0\n")
f.close()
def write(self, instRoot, bl, kernelList, chainList,
defaultDev, justConfig):
if self.timeout is None and chainList:
self.timeout = 5
# XXX HACK ALERT - see declaration above
if self.doUpgradeOnly:
if self.useGrubVal:
return self.upgradeGrub(instRoot, bl, kernelList,
chainList, defaultDev, justConfig)
return 0
if len(kernelList) < 1:
raise BootyNoKernelWarning
rc = self.writeGrub(instRoot, bl, kernelList,
chainList, defaultDev,
justConfig | (not self.useGrubVal))
if rc:
return rc
# XXX move the lilo.conf out of the way if they're using GRUB
# so that /sbin/installkernel does a more correct thing
if self.useGrubVal and os.access(instRoot + '/etc/lilo.conf', os.R_OK):
os.rename(instRoot + "/etc/lilo.conf",
instRoot + "/etc/lilo.conf.anaconda")
return 0
def getArgList(self):
args = bootloaderInfo.getArgList(self)
if self.password:
args.append("--md5pass=%s" %(self.password))
return args
def __init__(self, storage, network):
bootloaderInfo.__init__(self, storage, network)
# these have to be set /before/ efiBootloaderInfo.__init__(), or
# they'll be overwritten.
self._configdir = "/boot/grub"
self._configname = "grub.conf"
efiBootloaderInfo.__init__(self, storage, network, initialize=False)
# XXX use checkbootloader to determine what to default to
self.useGrubVal = 1
self.kernelLocation = "/boot/"
self.password = None
self.pure = None
|