blob: fa3d995a6d953da7fc0d027c5e9694f0876e0c51 [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/
mblighc49af7b2006-11-24 04:02:21 +000023 patch_dir
mblighc86b0b42006-07-28 17:35:28 +000024 <top_dir>/patches/
mblighc49af7b2006-11-24 04:02:21 +000025 build_dir
26 <top_dir>/linux/
mblighc86b0b42006-07-28 17:35:28 +000027 config_dir
mblighc49af7b2006-11-24 04:02:21 +000028 <top_dir>/config/
mblighc86b0b42006-07-28 17:35:28 +000029 log_dir
mblighc49af7b2006-11-24 04:02:21 +000030 <top_dir>/log/
mblighc86b0b42006-07-28 17:35:28 +000031 """
32
mblighf4c35322006-03-13 01:01:10 +000033 autodir = ''
34
mblighfdbcaec2006-10-01 23:28:57 +000035 def __init__(self, job, top_directory, base_tree, leave = 0):
mblighc86b0b42006-07-28 17:35:28 +000036 """Initialize the kernel build environment
37
38 job
39 which job this build is part of
40 top_directory
41 top of the build environment
42 base_tree
mbligh534015f2006-09-15 03:28:56 +000043 base kernel tree. Can be one of the following:
44 1. A local tarball
45 2. A URL to a tarball
46 3. A local directory (will symlink it)
47 4. A shorthand expandable (eg '2.6.11-git3')
mblighc86b0b42006-07-28 17:35:28 +000048 """
mblighf4c35322006-03-13 01:01:10 +000049 self.job = job
50 autodir = job.autodir
51 self.top_dir = top_directory
52 if not self.top_dir.startswith(autodir):
53 raise
mblighfdbcaec2006-10-01 23:28:57 +000054 if os.path.isdir(self.top_dir) and not leave:
mbligh31186612006-04-22 21:55:56 +000055 system('rm -rf ' + self.top_dir)
mblighf4c35322006-03-13 01:01:10 +000056 os.mkdir(self.top_dir)
57
mblighc49af7b2006-11-24 04:02:21 +000058 self.build_dir = os.path.join(self.top_dir, 'linux')
mbligha2508052006-05-28 21:29:53 +000059 # created by get_kernel_tree
mblighc49af7b2006-11-24 04:02:21 +000060 system("ls -l %s" % self.top_dir)
mbligh93057012006-08-06 15:51:56 +000061 self.src_dir = os.path.join(self.top_dir, 'src')
62 self.patch_dir = os.path.join(self.top_dir, 'patches')
63 self.config_dir = os.path.join(self.top_dir, 'config')
64 self.log_dir = os.path.join(self.top_dir, 'log')
mblighf4c35322006-03-13 01:01:10 +000065 os.mkdir(self.src_dir)
66 os.mkdir(self.patch_dir)
67 os.mkdir(self.config_dir)
68 os.mkdir(self.log_dir)
69
mbligh4426de02006-10-10 07:18:28 +000070 logpath = os.path.join(self.log_dir, 'build_log')
71 self.logfile = open(logpath, 'w+')
72
mbligh5970cf02006-08-06 15:39:22 +000073 self.target_arch = None
mblighfdbcaec2006-10-01 23:28:57 +000074 self.build_target = 'bzImage'
75
76 if leave:
77 return
mbligh534015f2006-09-15 03:28:56 +000078
mbligh4426de02006-10-10 07:18:28 +000079 self.logfile.write('BASE: %s\n' % base_tree)
mbligh534015f2006-09-15 03:28:56 +000080 if os.path.exists(base_tree):
81 self.get_kernel_tree(base_tree)
82 else:
apw8ad56be2006-11-06 17:49:54 +000083 args = self.job.config_get('mirror.ftp_kernel_org')
mbligh548f29a2006-10-17 04:55:12 +000084 if args:
85 args = '-l ' + args
86 base_components = kernelexpand(base_tree, args)
mbligh534015f2006-09-15 03:28:56 +000087 print 'kernelexpand: '
88 print base_components
89 self.get_kernel_tree(base_components.pop(0))
90 if base_components: # apply remaining patches
91 self.patch(*base_components)
mblighf4c35322006-03-13 01:01:10 +000092
93
apwfa526952006-04-21 17:27:09 +000094 def patch(self, *patches):
mblighc86b0b42006-07-28 17:35:28 +000095 """Apply a list of patches (in order)"""
mbligh534015f2006-09-15 03:28:56 +000096 print 'Applying patches: ', patches
97 # self.job.stdout.redirect(os.path.join(self.log_dir, 'stdout'))
mblighf4c35322006-03-13 01:01:10 +000098 local_patches = self.get_patches(patches)
mbligh4426de02006-10-10 07:18:28 +000099 for patch in patches:
100 self.logfile.write('PATCH: %s\n' % patch)
mblighf4c35322006-03-13 01:01:10 +0000101 self.apply_patches(local_patches)
mbligh534015f2006-09-15 03:28:56 +0000102 # self.job.stdout.restore()
mblighf4c35322006-03-13 01:01:10 +0000103
104
apwfa526952006-04-21 17:27:09 +0000105 def config(self, config_file, config_list = None):
mbligh93057012006-08-06 15:51:56 +0000106 self.job.stdout.redirect(os.path.join(self.log_dir, 'stdout'))
mblighf4c35322006-03-13 01:01:10 +0000107 config = kernel_config.kernel_config(self.build_dir, self.config_dir, config_file, config_list)
108 self.job.stdout.restore()
109
110
111 def get_patches(self, patches):
mblighc86b0b42006-07-28 17:35:28 +0000112 """fetch the patches to the local patch_dir"""
mblighf4c35322006-03-13 01:01:10 +0000113 local_patches = []
114 for patch in patches:
mbligh93057012006-08-06 15:51:56 +0000115 dest = os.path.join(self.patch_dir, basename(patch))
mblighf4c35322006-03-13 01:01:10 +0000116 get_file(patch, dest)
117 local_patches.append(dest)
mbligh534015f2006-09-15 03:28:56 +0000118 return local_patches
mblighf4c35322006-03-13 01:01:10 +0000119
120
mbligh534015f2006-09-15 03:28:56 +0000121 def apply_patches(self, local_patches):
mblighc86b0b42006-07-28 17:35:28 +0000122 """apply the list of patches, in order"""
mblighf4c35322006-03-13 01:01:10 +0000123 builddir = self.build_dir
124 os.chdir(builddir)
125
mbligh534015f2006-09-15 03:28:56 +0000126 print "apply_patches: ", local_patches
127 if not local_patches:
mblighf4c35322006-03-13 01:01:10 +0000128 return None
mbligh534015f2006-09-15 03:28:56 +0000129 for patch in local_patches:
mblighf4c35322006-03-13 01:01:10 +0000130 print 'Patching from', basename(patch), '...'
mbligh4426de02006-10-10 07:18:28 +0000131 cat_file_to_cmd(patch, 'patch -p1 > /dev/null')
mblighf4c35322006-03-13 01:01:10 +0000132
133
mbligh5970cf02006-08-06 15:39:22 +0000134 def get_kernel_tree(self, base_tree):
mblighc49af7b2006-11-24 04:02:21 +0000135 """Extract/link base_tree to self.build_dir"""
mbligh5970cf02006-08-06 15:39:22 +0000136
137 # if base_tree is a dir, assume uncompressed kernel
138 if os.path.isdir(base_tree):
139 print 'Symlinking existing kernel source'
mblighc49af7b2006-11-24 04:02:21 +0000140 os.symlink(base_tree, self.build_dir)
mblighf4c35322006-03-13 01:01:10 +0000141
mbligh5970cf02006-08-06 15:39:22 +0000142 # otherwise, extract tarball
143 else:
mblighc49af7b2006-11-24 04:02:21 +0000144 os.chdir(os.path.dirname(self.src_dir))
145 # Figure out local destination for tarball
146 tarball = os.path.join(self.src_dir, os.path.basename(base_tree))
mbligh5970cf02006-08-06 15:39:22 +0000147 get_file(base_tree, tarball)
mbligh5970cf02006-08-06 15:39:22 +0000148 print 'Extracting kernel tarball:', tarball, '...'
mblighc49af7b2006-11-24 04:02:21 +0000149 extract_tarball_to_dir(tarball, self.build_dir)
mbligh5970cf02006-08-06 15:39:22 +0000150
mblighf4c35322006-03-13 01:01:10 +0000151
mblighfdbcaec2006-10-01 23:28:57 +0000152 def extraversion(self, tag, append=1):
153 os.chdir(self.build_dir)
mbligh4426de02006-10-10 07:18:28 +0000154 extraversion_sub = r's/^EXTRAVERSION =\s*\(.*\)/EXTRAVERSION = '
mblighfdbcaec2006-10-01 23:28:57 +0000155 if append:
mbligh4426de02006-10-10 07:18:28 +0000156 p = extraversion_sub + '\\1-%s/' % tag
mblighfdbcaec2006-10-01 23:28:57 +0000157 else:
mbligh4426de02006-10-10 07:18:28 +0000158 p = extraversion_sub + '-%s/' % tag
159 system('sed -i.old "%s" Makefile' % p)
mblighfdbcaec2006-10-01 23:28:57 +0000160
161
162 def build(self, make_opts = '', logfile = '', extraversion='autotest'):
mblighc86b0b42006-07-28 17:35:28 +0000163 """build the kernel
164
165 make_opts
166 additional options to make, if any
167 """
mbligh10038012006-10-19 03:32:45 +0000168 os_dep.commands('gcc', 'make')
mbligh6d4c9412006-09-13 23:08:44 +0000169 if logfile == '':
170 logfile = os.path.join(self.log_dir, 'kernel_build')
mblighf4c35322006-03-13 01:01:10 +0000171 os.chdir(self.build_dir)
mblighfdbcaec2006-10-01 23:28:57 +0000172 if extraversion:
173 self.extraversion(extraversion)
mbligh93057012006-08-06 15:51:56 +0000174 print os.path.join(self.log_dir, 'stdout')
mbligh6d4c9412006-09-13 23:08:44 +0000175 self.job.stdout.redirect(logfile + '.stdout')
176 self.job.stderr.redirect(logfile + '.stderr')
mbligha2508052006-05-28 21:29:53 +0000177 self.set_cross_cc()
mblighf4c35322006-03-13 01:01:10 +0000178 # setup_config_file(config_file, config_overrides)
apwc7846102006-04-06 18:22:13 +0000179
180 # Not needed on 2.6, but hard to tell -- handle failure
mblighfdbcaec2006-10-01 23:28:57 +0000181 system('make dep', ignorestatus=1)
mblighf4c35322006-03-13 01:01:10 +0000182 threads = 2 * count_cpus()
mbligh6d4c9412006-09-13 23:08:44 +0000183 build_string = 'make -j %d %s %s' % (threads, make_opts,
184 self.build_target)
185 # eg make bzImage, or make zImage
186 print build_string
187 system(build_string)
mblighf4c35322006-03-13 01:01:10 +0000188 if kernel_config.modules_needed('.config'):
apwd4701972006-10-16 09:24:56 +0000189 system('make -j %d modules' % (threads))
apwc7846102006-04-06 18:22:13 +0000190
mblighf4c35322006-03-13 01:01:10 +0000191 self.job.stdout.restore()
192 self.job.stderr.restore()
mbligh4426de02006-10-10 07:18:28 +0000193
194 kernel_version = self.get_kernel_build_ver()
195 kernel_version = re.sub('-autotest', '', kernel_version)
196 self.logfile.write('BUILD VERSION: %s\n' % kernel_version)
197
mblighf4c35322006-03-13 01:01:10 +0000198
199
200 def build_timed(self, threads, timefile = '/dev/null', make_opts = ''):
mblighc86b0b42006-07-28 17:35:28 +0000201 """time the bulding of the kernel"""
mblighb8a14e32006-05-06 00:17:35 +0000202 os.chdir(self.build_dir)
203 print "make clean"
204 system('make clean')
mbligha2bb9d62006-10-09 16:26:03 +0000205 build_string = "/usr/bin/time -o %s make %s -j %s vmlinux" % (timefile, make_opts, threads)
mblighf4c35322006-03-13 01:01:10 +0000206 print build_string
apwc7846102006-04-06 18:22:13 +0000207 system(build_string)
208 if (not os.path.isfile('vmlinux')):
209 raise TestError("no vmlinux found, kernel build failed")
mblighb8a14e32006-05-06 00:17:35 +0000210
211
212 def clean(self):
mblighc86b0b42006-07-28 17:35:28 +0000213 """make clean in the kernel tree"""
mblighb8a14e32006-05-06 00:17:35 +0000214 os.chdir(self.build_dir)
mblighf4c35322006-03-13 01:01:10 +0000215 print "make clean"
apwc7846102006-04-06 18:22:13 +0000216 system('make clean')
mblighf4c35322006-03-13 01:01:10 +0000217
mbligh50f42ea2006-09-30 22:22:21 +0000218
219 def mkinitrd(self, version, image, system_map, initrd):
220 """Build kernel initrd image.
221 Try to use distro specific way to build initrd image.
222 Parameters:
223 version
224 new kernel version
225 image
226 new kernel image file
227 system_map
228 System.map file
229 initrd
230 initrd image file to build
231 """
232 vendor = get_os_vendor()
233
234 if os.path.isfile(initrd):
mblighfdbcaec2006-10-01 23:28:57 +0000235 print "Existing %s file, will remove it." % initrd
mbligh50f42ea2006-09-30 22:22:21 +0000236 os.remove(initrd)
237
238 if vendor in ['Red Hat', 'Fedora Core']:
mblighfdbcaec2006-10-01 23:28:57 +0000239 system('mkinitrd %s %s' % (initrd, version))
mbligh50f42ea2006-09-30 22:22:21 +0000240 elif vendor in ['SUSE']:
mblighfdbcaec2006-10-01 23:28:57 +0000241 system('mkinitrd -k %s -i %s -M %s' % (image, initrd, system_map))
mbligh50f42ea2006-09-30 22:22:21 +0000242 else:
mblighfdbcaec2006-10-01 23:28:57 +0000243 raise TestError('Unsupported vendor %s' % vendor)
mbligh50f42ea2006-09-30 22:22:21 +0000244
245
mbligh6a1d4db2006-10-06 04:30:16 +0000246 def install(self, tag='autotest', prefix = '/'):
mblighc86b0b42006-07-28 17:35:28 +0000247 """make install in the kernel tree"""
mblighf4c35322006-03-13 01:01:10 +0000248 os.chdir(self.build_dir)
mbligh0ad65582006-10-06 04:16:36 +0000249
mbligh6a1d4db2006-10-06 04:30:16 +0000250 if not os.path.isdir(prefix):
251 os.mkdir(prefix)
mbligha87116f2006-10-10 02:47:08 +0000252 self.boot_dir = os.path.join(prefix, 'boot')
253 if not os.path.isdir(self.boot_dir):
254 os.mkdir(self.boot_dir)
mbligh0ad65582006-10-06 04:16:36 +0000255
apwba5bbfd2006-10-16 09:25:45 +0000256 image = glob.glob('arch/*/boot/' + self.build_target)[0]
mbligha87116f2006-10-10 02:47:08 +0000257
258 # remember installed files
259 self.image = self.boot_dir + '/vmlinuz-' + tag
260 self.vmlinux = self.boot_dir + '/vmlinux-' + tag
261 self.system_map = self.boot_dir + '/System.map-' + tag
262 self.config = self.boot_dir + '/config-' + tag
263 self.initrd = ''
264
265 # copy to boot dir
266 force_copy(image, self.image)
267 force_copy('vmlinux', self.vmlinux)
268 force_copy('System.map', self.system_map)
269 force_copy('.config', self.config)
270
mbligh6a1d4db2006-10-06 04:30:16 +0000271 if not kernel_config.modules_needed('.config'):
272 return
273
274 system('make modules_install INSTALL_MOD_PATH=%s' % prefix)
275 if prefix == '/':
mbligha87116f2006-10-10 02:47:08 +0000276 self.initrd = self.boot_dir + '/initrd-' + tag
277 self.mkinitrd(self.get_kernel_build_ver(), self.image, \
278 self.system_map, self.initrd)
279
280
281 def add_to_bootloader(self, tag='autotest', args=''):
282 """ add this kernel to bootloader, taking an
283 optional parameter of space separated parameters
284 e.g.: kernel.add_to_bootloader('mykernel', 'ro acpi=off')
285 """
286
287 # remove existing entry if present
288 self.job.bootloader.remove_kernel(tag)
289
290 # add the kernel entry
291 # add_kernel(image, title='autotest', inird='')
292 self.job.bootloader.add_kernel(self.image, tag, self.initrd)
293
294 # if no args passed, populate from /proc/cmdline
295 if not args:
296 args = open('/proc/cmdline', 'r').readline().strip()
297
298 # add args to entry one at a time
299 for a in args.split(' '):
300 self.job.bootloader.add_args(tag, a)
mbligh6a1d4db2006-10-06 04:30:16 +0000301
mbligh201aa892006-10-29 04:02:05 +0000302
mbligh548f29a2006-10-17 04:55:12 +0000303 def get_kernel_build_arch(self, arch=None):
mbligh201aa892006-10-29 04:02:05 +0000304 """
305 Work out the current kernel architecture (as a kernel arch)
306 """
mbligh548f29a2006-10-17 04:55:12 +0000307 if not arch:
308 arch = get_current_kernel_arch()
309 if re.match('i.86', arch):
310 return 'i386'
311 elif re.match('sun4u', arch):
312 return 'sparc64'
313 elif re.match('arm.*', arch):
314 return 'arm'
315 elif re.match('sa110', arch):
316 return 'arm'
317 elif re.match('s390x', arch):
318 return 's390'
319 elif re.match('parisc64', arch):
320 return 'parisc'
321 elif re.match('ppc.*', arch):
322 return 'powerpc'
323 elif re.match('mips.*', arch):
324 return 'mips'
325 else:
326 return arch
327
mbligh6a1d4db2006-10-06 04:30:16 +0000328
apw1b5dc362006-10-31 11:24:26 +0000329 def boot(self, args=''):
330 """ install and boot this kernel, do not care how
331 just make it happen.
332 """
333
334 # Install this kernel.
335 self.install()
336 self.add_to_bootloader(args=args)
337
338 # Boot it.
339 self.job.reboot()
340
341
mblighfdbcaec2006-10-01 23:28:57 +0000342 def get_kernel_build_ver(self):
mblighe11f5fc2006-10-04 04:42:22 +0000343 """Check Makefile and .config to return kernel version"""
344 version = patchlevel = sublevel = extraversion = localversion = ''
345
346 for line in open(self.build_dir + '/Makefile', 'r').readlines():
347 if line.startswith('VERSION'):
348 version = line[line.index('=') + 1:].strip()
349 if line.startswith('PATCHLEVEL'):
350 patchlevel = line[line.index('=') + 1:].strip()
351 if line.startswith('SUBLEVEL'):
352 sublevel = line[line.index('=') + 1:].strip()
353 if line.startswith('EXTRAVERSION'):
354 extraversion = line[line.index('=') + 1:].strip()
355
356 for line in open(self.build_dir + '/.config', 'r').readlines():
357 if line.startswith('CONFIG_LOCALVERSION='):
358 localversion = line.rstrip().split('"')[1]
359
360 return "%s.%s.%s%s%s" %(version, patchlevel, sublevel, extraversion, localversion)
mblighfdbcaec2006-10-01 23:28:57 +0000361
362
mbligh5970cf02006-08-06 15:39:22 +0000363 def set_cross_cc(self, target_arch=None, cross_compile=None,
364 build_target='bzImage'):
mblighc86b0b42006-07-28 17:35:28 +0000365 """Set up to cross-compile.
mblighcc2e6662006-09-14 01:24:07 +0000366 This is broken. We need to work out what the default
367 compile produces, and if not, THEN set the cross
368 compiler.
mblighc86b0b42006-07-28 17:35:28 +0000369 """
mblighcc2e6662006-09-14 01:24:07 +0000370
mbligh5970cf02006-08-06 15:39:22 +0000371 if self.target_arch:
372 return
373
374 self.build_target = build_target
375
376 # If no 'target_arch' given assume native compilation
377 if target_arch == None:
mbligh548f29a2006-10-17 04:55:12 +0000378 target_arch = get_current_kernel_arch()
mbligh5970cf02006-08-06 15:39:22 +0000379 if target_arch == 'ppc64':
mbligh5970cf02006-08-06 15:39:22 +0000380 if self.build_target == 'bzImage':
381 self.build_target = 'zImage'
mblighcc2e6662006-09-14 01:24:07 +0000382
383 return # HACK. Crap out for now.
mblighf4c35322006-03-13 01:01:10 +0000384
mblighcc2e6662006-09-14 01:24:07 +0000385 # At this point I know what arch I *want* to build for
386 # but have no way of working out what arch the default
387 # compiler DOES build for.
388
389 # Oh, and BTW, install_package() doesn't exist yet.
390
391 if target_arch == 'ppc64':
392 install_package('ppc64-cross')
393 cross_compile = os.path.join(autodir, 'sources/ppc64-cross/bin')
394
395 elif target_arch == 'x86_64':
396 install_package('x86_64-cross')
397 cross_compile = os.path.join(autodir, 'sources/x86_64-cross/bin')
mblighb8a14e32006-05-06 00:17:35 +0000398
mbligh5970cf02006-08-06 15:39:22 +0000399 os.environ['ARCH'] = self.target_arch = target_arch
400
401 self.cross_compile = cross_compile
402 if self.cross_compile:
403 os.environ['CROSS_COMPILE'] = self.cross_compile
mblighcc2e6662006-09-14 01:24:07 +0000404
405
mblighb8a14e32006-05-06 00:17:35 +0000406 def pickle_dump(self, filename):
mblighc86b0b42006-07-28 17:35:28 +0000407 """dump a pickle of ourself out to the specified filename
408
409 we can't pickle the backreference to job (it contains fd's),
mbligh709bb9b2006-10-12 04:32:16 +0000410 nor would we want to. Same for logfile (fd's).
mblighc86b0b42006-07-28 17:35:28 +0000411 """
mblighb8a14e32006-05-06 00:17:35 +0000412 temp = copy.copy(self)
413 temp.job = None
mbligh709bb9b2006-10-12 04:32:16 +0000414 temp.logfile = None
mblighb8a14e32006-05-06 00:17:35 +0000415 pickle.dump(temp, open(filename, 'w'))
416