blob: 86b60fbbfe6478a23388da4036bc374ea72a7408 [file] [log] [blame]
mblighc86b0b42006-07-28 17:35:28 +00001__author__ = """Copyright Martin J. Bligh, 2006"""
mbligha2508052006-05-28 21:29:53 +00002
apwba5bbfd2006-10-16 09:25:45 +00003import os,os.path,shutil,urllib,copy,pickle,re,glob
mblighf4c35322006-03-13 01:01:10 +00004from autotest_utils import *
mbligh10038012006-10-19 03:32:45 +00005import kernel_config, test, os_dep
mblighf4c35322006-03-13 01:01:10 +00006
7class kernel:
mblighc86b0b42006-07-28 17:35:28 +00008 """ Class for compiling kernels.
9
10 Data for the object includes the src files
11 used to create the kernel, patches applied, config (base + changes),
12 the build directory itself, and logged output
13
14 Properties:
15 job
16 Backpointer to the job object we're part of
17 autodir
18 Path to the top level autotest dir (/usr/local/autotest)
19 top_dir
20 Path to the top level dir of this kernel object
21 src_dir
22 <top_dir>/src/
23 build_dir
24 <top_dir>/patches/
25 config_dir
26 <top_dir>/config
27 log_dir
28 <top_dir>/log
29 """
30
mblighf4c35322006-03-13 01:01:10 +000031 autodir = ''
32
mblighfdbcaec2006-10-01 23:28:57 +000033 def __init__(self, job, top_directory, base_tree, leave = 0):
mblighc86b0b42006-07-28 17:35:28 +000034 """Initialize the kernel build environment
35
36 job
37 which job this build is part of
38 top_directory
39 top of the build environment
40 base_tree
mbligh534015f2006-09-15 03:28:56 +000041 base kernel tree. Can be one of the following:
42 1. A local tarball
43 2. A URL to a tarball
44 3. A local directory (will symlink it)
45 4. A shorthand expandable (eg '2.6.11-git3')
mblighc86b0b42006-07-28 17:35:28 +000046 """
mblighf4c35322006-03-13 01:01:10 +000047 self.job = job
48 autodir = job.autodir
49 self.top_dir = top_directory
50 if not self.top_dir.startswith(autodir):
51 raise
mblighfdbcaec2006-10-01 23:28:57 +000052 if os.path.isdir(self.top_dir) and not leave:
mbligh31186612006-04-22 21:55:56 +000053 system('rm -rf ' + self.top_dir)
mblighf4c35322006-03-13 01:01:10 +000054 os.mkdir(self.top_dir)
55
mbligh93057012006-08-06 15:51:56 +000056 self.build_dir = os.path.join(self.top_dir, 'build')
mbligha2508052006-05-28 21:29:53 +000057 # created by get_kernel_tree
mbligh93057012006-08-06 15:51:56 +000058 self.src_dir = os.path.join(self.top_dir, 'src')
59 self.patch_dir = os.path.join(self.top_dir, 'patches')
60 self.config_dir = os.path.join(self.top_dir, 'config')
61 self.log_dir = os.path.join(self.top_dir, 'log')
mblighf4c35322006-03-13 01:01:10 +000062 os.mkdir(self.src_dir)
63 os.mkdir(self.patch_dir)
64 os.mkdir(self.config_dir)
65 os.mkdir(self.log_dir)
66
mbligh4426de02006-10-10 07:18:28 +000067 logpath = os.path.join(self.log_dir, 'build_log')
68 self.logfile = open(logpath, 'w+')
69
mbligh5970cf02006-08-06 15:39:22 +000070 self.target_arch = None
mblighfdbcaec2006-10-01 23:28:57 +000071 self.build_target = 'bzImage'
72
73 if leave:
74 return
mbligh534015f2006-09-15 03:28:56 +000075
mbligh4426de02006-10-10 07:18:28 +000076 self.logfile.write('BASE: %s\n' % base_tree)
mbligh534015f2006-09-15 03:28:56 +000077 if os.path.exists(base_tree):
78 self.get_kernel_tree(base_tree)
79 else:
apw8ad56be2006-11-06 17:49:54 +000080 args = self.job.config_get('mirror.ftp_kernel_org')
mbligh548f29a2006-10-17 04:55:12 +000081 if args:
82 args = '-l ' + args
83 base_components = kernelexpand(base_tree, args)
mbligh534015f2006-09-15 03:28:56 +000084 print 'kernelexpand: '
85 print base_components
86 self.get_kernel_tree(base_components.pop(0))
87 if base_components: # apply remaining patches
88 self.patch(*base_components)
mblighf4c35322006-03-13 01:01:10 +000089
90
apwfa526952006-04-21 17:27:09 +000091 def patch(self, *patches):
mblighc86b0b42006-07-28 17:35:28 +000092 """Apply a list of patches (in order)"""
mbligh534015f2006-09-15 03:28:56 +000093 print 'Applying patches: ', patches
94 # self.job.stdout.redirect(os.path.join(self.log_dir, 'stdout'))
mblighf4c35322006-03-13 01:01:10 +000095 local_patches = self.get_patches(patches)
mbligh4426de02006-10-10 07:18:28 +000096 for patch in patches:
97 self.logfile.write('PATCH: %s\n' % patch)
mblighf4c35322006-03-13 01:01:10 +000098 self.apply_patches(local_patches)
mbligh534015f2006-09-15 03:28:56 +000099 # self.job.stdout.restore()
mblighf4c35322006-03-13 01:01:10 +0000100
101
apwfa526952006-04-21 17:27:09 +0000102 def config(self, config_file, config_list = None):
mbligh93057012006-08-06 15:51:56 +0000103 self.job.stdout.redirect(os.path.join(self.log_dir, 'stdout'))
mblighf4c35322006-03-13 01:01:10 +0000104 config = kernel_config.kernel_config(self.build_dir, self.config_dir, config_file, config_list)
105 self.job.stdout.restore()
106
107
108 def get_patches(self, patches):
mblighc86b0b42006-07-28 17:35:28 +0000109 """fetch the patches to the local patch_dir"""
mblighf4c35322006-03-13 01:01:10 +0000110 local_patches = []
111 for patch in patches:
mbligh93057012006-08-06 15:51:56 +0000112 dest = os.path.join(self.patch_dir, basename(patch))
mblighf4c35322006-03-13 01:01:10 +0000113 get_file(patch, dest)
114 local_patches.append(dest)
mbligh534015f2006-09-15 03:28:56 +0000115 return local_patches
mblighf4c35322006-03-13 01:01:10 +0000116
117
mbligh534015f2006-09-15 03:28:56 +0000118 def apply_patches(self, local_patches):
mblighc86b0b42006-07-28 17:35:28 +0000119 """apply the list of patches, in order"""
mblighf4c35322006-03-13 01:01:10 +0000120 builddir = self.build_dir
121 os.chdir(builddir)
122
mbligh534015f2006-09-15 03:28:56 +0000123 print "apply_patches: ", local_patches
124 if not local_patches:
mblighf4c35322006-03-13 01:01:10 +0000125 return None
mbligh534015f2006-09-15 03:28:56 +0000126 for patch in local_patches:
mblighf4c35322006-03-13 01:01:10 +0000127 print 'Patching from', basename(patch), '...'
mbligh4426de02006-10-10 07:18:28 +0000128 cat_file_to_cmd(patch, 'patch -p1 > /dev/null')
mblighf4c35322006-03-13 01:01:10 +0000129
130
mbligh5970cf02006-08-06 15:39:22 +0000131 def get_kernel_tree(self, base_tree):
132 """Extract/link base_tree to self.top_dir/build"""
133
134 # if base_tree is a dir, assume uncompressed kernel
135 if os.path.isdir(base_tree):
136 print 'Symlinking existing kernel source'
137 os.symlink(base_tree,
138 os.path.join(self.top_dir, 'build'))
mblighf4c35322006-03-13 01:01:10 +0000139
mbligh5970cf02006-08-06 15:39:22 +0000140 # otherwise, extract tarball
141 else:
142 os.chdir(self.top_dir)
143 tarball = os.path.join('src', basename(base_tree))
144 get_file(base_tree, tarball)
145
146 print 'Extracting kernel tarball:', tarball, '...'
147 extract_tarball_to_dir(tarball, 'build')
148
mblighf4c35322006-03-13 01:01:10 +0000149
mblighfdbcaec2006-10-01 23:28:57 +0000150 def extraversion(self, tag, append=1):
151 os.chdir(self.build_dir)
mbligh4426de02006-10-10 07:18:28 +0000152 extraversion_sub = r's/^EXTRAVERSION =\s*\(.*\)/EXTRAVERSION = '
mblighfdbcaec2006-10-01 23:28:57 +0000153 if append:
mbligh4426de02006-10-10 07:18:28 +0000154 p = extraversion_sub + '\\1-%s/' % tag
mblighfdbcaec2006-10-01 23:28:57 +0000155 else:
mbligh4426de02006-10-10 07:18:28 +0000156 p = extraversion_sub + '-%s/' % tag
157 system('sed -i.old "%s" Makefile' % p)
mblighfdbcaec2006-10-01 23:28:57 +0000158
159
160 def build(self, make_opts = '', logfile = '', extraversion='autotest'):
mblighc86b0b42006-07-28 17:35:28 +0000161 """build the kernel
162
163 make_opts
164 additional options to make, if any
165 """
mbligh10038012006-10-19 03:32:45 +0000166 os_dep.commands('gcc', 'make')
mbligh6d4c9412006-09-13 23:08:44 +0000167 if logfile == '':
168 logfile = os.path.join(self.log_dir, 'kernel_build')
mblighf4c35322006-03-13 01:01:10 +0000169 os.chdir(self.build_dir)
mblighfdbcaec2006-10-01 23:28:57 +0000170 if extraversion:
171 self.extraversion(extraversion)
mbligh93057012006-08-06 15:51:56 +0000172 print os.path.join(self.log_dir, 'stdout')
mbligh6d4c9412006-09-13 23:08:44 +0000173 self.job.stdout.redirect(logfile + '.stdout')
174 self.job.stderr.redirect(logfile + '.stderr')
mbligha2508052006-05-28 21:29:53 +0000175 self.set_cross_cc()
mblighf4c35322006-03-13 01:01:10 +0000176 # setup_config_file(config_file, config_overrides)
apwc7846102006-04-06 18:22:13 +0000177
178 # Not needed on 2.6, but hard to tell -- handle failure
mblighfdbcaec2006-10-01 23:28:57 +0000179 system('make dep', ignorestatus=1)
mblighf4c35322006-03-13 01:01:10 +0000180 threads = 2 * count_cpus()
mbligh6d4c9412006-09-13 23:08:44 +0000181 build_string = 'make -j %d %s %s' % (threads, make_opts,
182 self.build_target)
183 # eg make bzImage, or make zImage
184 print build_string
185 system(build_string)
mblighf4c35322006-03-13 01:01:10 +0000186 if kernel_config.modules_needed('.config'):
apwd4701972006-10-16 09:24:56 +0000187 system('make -j %d modules' % (threads))
apwc7846102006-04-06 18:22:13 +0000188
mblighf4c35322006-03-13 01:01:10 +0000189 self.job.stdout.restore()
190 self.job.stderr.restore()
mbligh4426de02006-10-10 07:18:28 +0000191
192 kernel_version = self.get_kernel_build_ver()
193 kernel_version = re.sub('-autotest', '', kernel_version)
194 self.logfile.write('BUILD VERSION: %s\n' % kernel_version)
195
mblighf4c35322006-03-13 01:01:10 +0000196
197
198 def build_timed(self, threads, timefile = '/dev/null', make_opts = ''):
mblighc86b0b42006-07-28 17:35:28 +0000199 """time the bulding of the kernel"""
mblighb8a14e32006-05-06 00:17:35 +0000200 os.chdir(self.build_dir)
201 print "make clean"
202 system('make clean')
mbligha2bb9d62006-10-09 16:26:03 +0000203 build_string = "/usr/bin/time -o %s make %s -j %s vmlinux" % (timefile, make_opts, threads)
mblighf4c35322006-03-13 01:01:10 +0000204 print build_string
apwc7846102006-04-06 18:22:13 +0000205 system(build_string)
206 if (not os.path.isfile('vmlinux')):
207 raise TestError("no vmlinux found, kernel build failed")
mblighb8a14e32006-05-06 00:17:35 +0000208
209
210 def clean(self):
mblighc86b0b42006-07-28 17:35:28 +0000211 """make clean in the kernel tree"""
mblighb8a14e32006-05-06 00:17:35 +0000212 os.chdir(self.build_dir)
mblighf4c35322006-03-13 01:01:10 +0000213 print "make clean"
apwc7846102006-04-06 18:22:13 +0000214 system('make clean')
mblighf4c35322006-03-13 01:01:10 +0000215
mbligh50f42ea2006-09-30 22:22:21 +0000216
217 def mkinitrd(self, version, image, system_map, initrd):
218 """Build kernel initrd image.
219 Try to use distro specific way to build initrd image.
220 Parameters:
221 version
222 new kernel version
223 image
224 new kernel image file
225 system_map
226 System.map file
227 initrd
228 initrd image file to build
229 """
230 vendor = get_os_vendor()
231
232 if os.path.isfile(initrd):
mblighfdbcaec2006-10-01 23:28:57 +0000233 print "Existing %s file, will remove it." % initrd
mbligh50f42ea2006-09-30 22:22:21 +0000234 os.remove(initrd)
235
236 if vendor in ['Red Hat', 'Fedora Core']:
mblighfdbcaec2006-10-01 23:28:57 +0000237 system('mkinitrd %s %s' % (initrd, version))
mbligh50f42ea2006-09-30 22:22:21 +0000238 elif vendor in ['SUSE']:
mblighfdbcaec2006-10-01 23:28:57 +0000239 system('mkinitrd -k %s -i %s -M %s' % (image, initrd, system_map))
mbligh50f42ea2006-09-30 22:22:21 +0000240 else:
mblighfdbcaec2006-10-01 23:28:57 +0000241 raise TestError('Unsupported vendor %s' % vendor)
mbligh50f42ea2006-09-30 22:22:21 +0000242
243
mbligh6a1d4db2006-10-06 04:30:16 +0000244 def install(self, tag='autotest', prefix = '/'):
mblighc86b0b42006-07-28 17:35:28 +0000245 """make install in the kernel tree"""
mblighf4c35322006-03-13 01:01:10 +0000246 os.chdir(self.build_dir)
mbligh0ad65582006-10-06 04:16:36 +0000247
mbligh6a1d4db2006-10-06 04:30:16 +0000248 if not os.path.isdir(prefix):
249 os.mkdir(prefix)
mbligha87116f2006-10-10 02:47:08 +0000250 self.boot_dir = os.path.join(prefix, 'boot')
251 if not os.path.isdir(self.boot_dir):
252 os.mkdir(self.boot_dir)
mbligh0ad65582006-10-06 04:16:36 +0000253
apwba5bbfd2006-10-16 09:25:45 +0000254 image = glob.glob('arch/*/boot/' + self.build_target)[0]
mbligha87116f2006-10-10 02:47:08 +0000255
256 # remember installed files
257 self.image = self.boot_dir + '/vmlinuz-' + tag
258 self.vmlinux = self.boot_dir + '/vmlinux-' + tag
259 self.system_map = self.boot_dir + '/System.map-' + tag
260 self.config = self.boot_dir + '/config-' + tag
261 self.initrd = ''
262
263 # copy to boot dir
264 force_copy(image, self.image)
265 force_copy('vmlinux', self.vmlinux)
266 force_copy('System.map', self.system_map)
267 force_copy('.config', self.config)
268
mbligh6a1d4db2006-10-06 04:30:16 +0000269 if not kernel_config.modules_needed('.config'):
270 return
271
272 system('make modules_install INSTALL_MOD_PATH=%s' % prefix)
273 if prefix == '/':
mbligha87116f2006-10-10 02:47:08 +0000274 self.initrd = self.boot_dir + '/initrd-' + tag
275 self.mkinitrd(self.get_kernel_build_ver(), self.image, \
276 self.system_map, self.initrd)
277
278
279 def add_to_bootloader(self, tag='autotest', args=''):
280 """ add this kernel to bootloader, taking an
281 optional parameter of space separated parameters
282 e.g.: kernel.add_to_bootloader('mykernel', 'ro acpi=off')
283 """
284
285 # remove existing entry if present
286 self.job.bootloader.remove_kernel(tag)
287
288 # add the kernel entry
289 # add_kernel(image, title='autotest', inird='')
290 self.job.bootloader.add_kernel(self.image, tag, self.initrd)
291
292 # if no args passed, populate from /proc/cmdline
293 if not args:
294 args = open('/proc/cmdline', 'r').readline().strip()
295
296 # add args to entry one at a time
297 for a in args.split(' '):
298 self.job.bootloader.add_args(tag, a)
mbligh6a1d4db2006-10-06 04:30:16 +0000299
mbligh201aa892006-10-29 04:02:05 +0000300
mbligh548f29a2006-10-17 04:55:12 +0000301 def get_kernel_build_arch(self, arch=None):
mbligh201aa892006-10-29 04:02:05 +0000302 """
303 Work out the current kernel architecture (as a kernel arch)
304 """
mbligh548f29a2006-10-17 04:55:12 +0000305 if not arch:
306 arch = get_current_kernel_arch()
307 if re.match('i.86', arch):
308 return 'i386'
309 elif re.match('sun4u', arch):
310 return 'sparc64'
311 elif re.match('arm.*', arch):
312 return 'arm'
313 elif re.match('sa110', arch):
314 return 'arm'
315 elif re.match('s390x', arch):
316 return 's390'
317 elif re.match('parisc64', arch):
318 return 'parisc'
319 elif re.match('ppc.*', arch):
320 return 'powerpc'
321 elif re.match('mips.*', arch):
322 return 'mips'
323 else:
324 return arch
325
mbligh6a1d4db2006-10-06 04:30:16 +0000326
apw1b5dc362006-10-31 11:24:26 +0000327 def boot(self, args=''):
328 """ install and boot this kernel, do not care how
329 just make it happen.
330 """
331
332 # Install this kernel.
333 self.install()
334 self.add_to_bootloader(args=args)
335
336 # Boot it.
337 self.job.reboot()
338
339
mblighfdbcaec2006-10-01 23:28:57 +0000340 def get_kernel_build_ver(self):
mblighe11f5fc2006-10-04 04:42:22 +0000341 """Check Makefile and .config to return kernel version"""
342 version = patchlevel = sublevel = extraversion = localversion = ''
343
344 for line in open(self.build_dir + '/Makefile', 'r').readlines():
345 if line.startswith('VERSION'):
346 version = line[line.index('=') + 1:].strip()
347 if line.startswith('PATCHLEVEL'):
348 patchlevel = line[line.index('=') + 1:].strip()
349 if line.startswith('SUBLEVEL'):
350 sublevel = line[line.index('=') + 1:].strip()
351 if line.startswith('EXTRAVERSION'):
352 extraversion = line[line.index('=') + 1:].strip()
353
354 for line in open(self.build_dir + '/.config', 'r').readlines():
355 if line.startswith('CONFIG_LOCALVERSION='):
356 localversion = line.rstrip().split('"')[1]
357
358 return "%s.%s.%s%s%s" %(version, patchlevel, sublevel, extraversion, localversion)
mblighfdbcaec2006-10-01 23:28:57 +0000359
360
mbligh5970cf02006-08-06 15:39:22 +0000361 def set_cross_cc(self, target_arch=None, cross_compile=None,
362 build_target='bzImage'):
mblighc86b0b42006-07-28 17:35:28 +0000363 """Set up to cross-compile.
mblighcc2e6662006-09-14 01:24:07 +0000364 This is broken. We need to work out what the default
365 compile produces, and if not, THEN set the cross
366 compiler.
mblighc86b0b42006-07-28 17:35:28 +0000367 """
mblighcc2e6662006-09-14 01:24:07 +0000368
mbligh5970cf02006-08-06 15:39:22 +0000369 if self.target_arch:
370 return
371
372 self.build_target = build_target
373
374 # If no 'target_arch' given assume native compilation
375 if target_arch == None:
mbligh548f29a2006-10-17 04:55:12 +0000376 target_arch = get_current_kernel_arch()
mbligh5970cf02006-08-06 15:39:22 +0000377 if target_arch == 'ppc64':
mbligh5970cf02006-08-06 15:39:22 +0000378 if self.build_target == 'bzImage':
379 self.build_target = 'zImage'
mblighcc2e6662006-09-14 01:24:07 +0000380
381 return # HACK. Crap out for now.
mblighf4c35322006-03-13 01:01:10 +0000382
mblighcc2e6662006-09-14 01:24:07 +0000383 # At this point I know what arch I *want* to build for
384 # but have no way of working out what arch the default
385 # compiler DOES build for.
386
387 # Oh, and BTW, install_package() doesn't exist yet.
388
389 if target_arch == 'ppc64':
390 install_package('ppc64-cross')
391 cross_compile = os.path.join(autodir, 'sources/ppc64-cross/bin')
392
393 elif target_arch == 'x86_64':
394 install_package('x86_64-cross')
395 cross_compile = os.path.join(autodir, 'sources/x86_64-cross/bin')
mblighb8a14e32006-05-06 00:17:35 +0000396
mbligh5970cf02006-08-06 15:39:22 +0000397 os.environ['ARCH'] = self.target_arch = target_arch
398
399 self.cross_compile = cross_compile
400 if self.cross_compile:
401 os.environ['CROSS_COMPILE'] = self.cross_compile
mblighcc2e6662006-09-14 01:24:07 +0000402
403
mblighb8a14e32006-05-06 00:17:35 +0000404 def pickle_dump(self, filename):
mblighc86b0b42006-07-28 17:35:28 +0000405 """dump a pickle of ourself out to the specified filename
406
407 we can't pickle the backreference to job (it contains fd's),
mbligh709bb9b2006-10-12 04:32:16 +0000408 nor would we want to. Same for logfile (fd's).
mblighc86b0b42006-07-28 17:35:28 +0000409 """
mblighb8a14e32006-05-06 00:17:35 +0000410 temp = copy.copy(self)
411 temp.job = None
mbligh709bb9b2006-10-12 04:32:16 +0000412 temp.logfile = None
mblighb8a14e32006-05-06 00:17:35 +0000413 pickle.dump(temp, open(filename, 'w'))
414