summaryrefslogtreecommitdiffstats
path: root/todo.py
blob: 14339929602b4d0ff4c229bb8f4cbe4e7538c7de (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
# For an install to proceed, the following todo fields must be filled in
#
#	mount list (unless todo.runLive)		addMount()
#	lilo boot.b installation (may be None)		liloLocation()

import rpm, os
import util, isys
from lilo import LiloConfiguration
from syslog import Syslog

def instCallback(what, amount, total, key, data):
    if (what == rpm.RPMCALLBACK_INST_OPEN_FILE):
	(h, method) = key
	data.setPackage(h[rpm.RPMTAG_NAME])
	data.setPackageScale(0, 1)
	fn = method.getFilename(h)
	d = os.open(fn, os.O_RDONLY)
	return d
    elif (what == rpm.RPMCALLBACK_INST_PROGRESS):
	data.setPackageScale(amount, total)

class ToDo:

    def umountFilesystems(self):
	if (not self.setupFilesystems): return 

	self.mounts.sort(mountListCmp)
	self.mounts.reverse()
	for n in self.mounts:
	    isys.makeDevInode(n, '/tmp/' + n)
	    isys.umount(n)
            os.remove('/tmp/' + n)

    def makeFilesystems(self):
	if (not self.setupFilesystems): return 

	self.mounts.sort(mountListCmp)
	for n in self.mounts:
	    (device, mntpoint, format) = n
	    if not format: continue
	    w = self.intf.waitWindow("Formatting", 
			"Formatting %s filesystem..." % (mntpoint,))
	    isys.makeDevInode(n, '/tmp/' + device)
	    util.execWithRedirect("mke2fs", [ "mke2fs", '/tmp/' + device ],
				  stdout = None, stderr = None, searchPath = 1)
            os.remove('/tmp/' + device)
	    w.pop()

    def mountFilesystems(self):
	if (not self.setupFilesystems): return 

	for n in self.mounts:
	    (device, mntpoint, format) = n
            isys.makeDevInode(device, '/tmp/' + device)
	    isys.mount( '/tmp/' + device, self.instPath + mntpoint)
	    os.remove( '/tmp' + device);

    def doInstall(self):
	# make sure we have the header list and comps file
	self.headerList()
	self.compsList()

	self.makeFilesystems()
	self.mountFilesystems()

	if not self.installSystem: 
	    return

	for i in [ '/var', '/var/lib', '/var/lib/rpm', '/tmp', '/dev' ]:
	    try:
	        os.mkdir(self.instPath + i)
	    except os.error, (errno, msg):
                print 'Error making directory %s: %s' % (i, msg)

	db = rpm.opendb(1, self.instPath)
	ts = rpm.TransactionSet(self.instPath, db)

	for p in self.hdList.selected():
	    ts.add(p.h, (p.h, self.method))

	ts.order()

	instLog = open(self.instPath + '/tmp/install.log', "w+")
	syslog = Syslog(root = self.instPath, output = instLog)

	instLogFd = os.open(self.instPath + '/tmp/install.log',
			    os.O_RDWR)
	ts.scriptFd = instLogFd
	# the transaction set dup()s the file descriptor and will close the
	# dup'd when we go out of scope
	os.close(instLogFd)	

	p = self.intf.packageProgessWindow()
	ts.run(0, 0, instCallback, p)

	syslog.kill()

	self.writeFstab()
	self.installLilo()

    def installLilo(self):
	if not self.liloDevice: return

	# FIXME: make an initrd here

	l = LiloConfiguration()
	l.addEntry("boot", '/dev/' + self.liloDevice)
	l.addEntry("map", "/boot/map")
	l.addEntry("install", "/boot/boot.b")
	l.addEntry("prompt")
	l.addEntry("timeout", "50")

	sl = LiloConfiguration()
	sl.addEntry("label", "linux")

	for n in self.mounts:
	    (dev, fs, reformat) = n
	    if fs == '/':
		sl.addEntry("root", '/dev/' + dev)
	sl.addEntry("read-only")

	kernelFile = '/boot/vmlinuz-' +  \
		str(self.kernelPackage[rpm.RPMTAG_VERSION]) + "-" + \
		str(self.kernelPackage[rpm.RPMTAG_RELEASE])
	    
	l.addImage(kernelFile, sl)
	l.write(self.instPath + "/etc/lilo.conf")

	util.execWithRedirect(self.instPath + '/sbin/lilo' , [ "lilo", 
				"-r", self.instPath ], stdout = None)

    def writeFstab(self):
	format = "%-23s %-23s %-7s %-15s %d %d\n";

	f = open(self.instPath + "/etc/fstab", "w")
	self.mounts.sort(mountListCmp)
	for n in self.mounts: 
	    (dev, fs, reformat) = n
	    if (fs == '/'):
		f.write(format % ( '/dev/' + dev, fs, 'ext2', 'defaults', 1, 1))
	    else:
		f.write(format % ( '/dev/' + dev, fs, 'ext2', 'defaults', 1, 2))
	f.write(format % ("/mnt/floppy", "/dev/fd0", 'ext', 'noauto', 0, 0))
	f.write(format % ("none", "/proc", 'proc', 'defaults', 0, 0))
	f.write(format % ("none", "/dev/pts", 'devpts', 'gid=5,mode=620', 0, 0))
	f.close()

    def addMount(self, device, location, reformat = 1):
	self.mounts.append((device, location, reformat))

    def freeHeaders(self):
	if (self.hdList):
	    self.hdList = None

    def headerList(self):
	if (not self.hdList):
	    w = self.intf.waitWindow("Reading", 
			"Reading package information...")
	    self.hdList = self.method.readHeaders()
	    w.pop()
	return self.hdList

    def liloLocation(self, device):
	self.liloDevice = device

    def compsList(self):
	if (not self.comps):
	    self.headerList()
	    self.comps = self.method.readComps(self.hdList)
	self.comps['Base'].select(1)
	self.kernelPackage = self.hdList['kernel']

	if (self.hdList.has_key('kernel-smp') and isys.smpAvailable()):
	    self.hdList['kernel'].selected = 0
	    self.hdList['kernel-smp'].selected = 1
	    self.kernelPackage = self.hdList['kernel-smp']

	return self.comps

    def __init__(self, intf, method, rootPath, setupFilesystems = 1,
		 installSystem = 1):
	self.intf = intf
	self.method = method
	self.mounts = []
	self.hdList = None
	self.comps = None
	self.instPath = rootPath
	self.setupFilesystems = setupFilesystems
	self.installSystem = installSystem

def mountListCmp(first, second):
    mnt1 = first[1]
    mnt2 = first[2]
    if (first < second):
	return -1
    elif (first == second):
	return 0
    return 1