blob: da91389b640c20df4a1b162fbcfe1876c13c0595 [file] [log] [blame]
showard4cfdce12009-06-15 20:23:29 +00001import os, shutil, copy, pickle, re, glob, time, logging
mblighc61fb362008-06-05 16:22:15 +00002from autotest_lib.client.bin import kernel_config, os_dep, kernelexpand, test
mbligh53da18e2009-01-05 21:13:26 +00003from autotest_lib.client.bin import utils
4from autotest_lib.client.common_lib import log, error, packages
mblighf4c35322006-03-13 01:01:10 +00005
mblighb8e0a112007-11-05 20:27:36 +00006
showard75cdfee2009-06-10 17:40:41 +00007def tee_output_logdir_mark(fn):
8 def tee_logdir_mark_wrapper(self, *args, **dargs):
9 mark = self.__class__.__name__ + "." + fn.__name__
showard4cfdce12009-06-15 20:23:29 +000010 logging.info("--- START %s ---", mark)
showard75cdfee2009-06-10 17:40:41 +000011 self.job.logging.tee_redirect_debug_dir(self.log_dir)
12 try:
13 result = fn(self, *args, **dargs)
14 finally:
15 self.job.logging.restore()
showard4cfdce12009-06-15 20:23:29 +000016 logging.info("--- END %s ---", mark)
showard75cdfee2009-06-10 17:40:41 +000017
18 return result
19
20 tee_logdir_mark_wrapper.__name__ = fn.__name__
21 return tee_logdir_mark_wrapper
22
23
mbligh516c8df2010-02-12 18:48:52 +000024def _add_kernel_to_bootloader(bootloader, base_args, tag, args, image, initrd):
25 """
26 Add a kernel with the specified tag to the boot config using the given
27 bootloader object. Also process the base_args and args kernel arguments
28 by removing all root= options and give the last root= option value to
29 the bootloader as a root device.
30
31 @param bootloader: bootloader object
32 @param base_args: base cmdline kernel arguments
33 @param tag: kernel tag
34 @param args: kernel cmdline arguments that are merged with base_args; a
35 root= option in "args" will override any from base_args
36 @param image: kernel image file
37 @param initrd: initrd file
38 """
39 # remove existing entry if present
40 bootloader.remove_kernel(tag)
41
42 if base_args:
43 args = ' '.join((base_args, args))
44
45 root_prefix = 'root='
46 # stores the last root= value
47 root = None
48 # a list with all arguments that don't start with root= so we give them
49 # later to bootloader.add_kernel()
50 arglist = []
51
52 for arg in args.split():
53 if arg.startswith(root_prefix):
54 # set the current root value with the one from the argument
55 # thus after processing all the arguments we keep the last
56 # root value (so root= options from args overrides any from
57 # base_args)
58 root = arg[len(root_prefix):]
59 else:
60 arglist.append(arg)
61
62 # add the kernel entry
63 bootloader.add_kernel(image, tag, initrd=initrd, args=' '.join(arglist),
64 root=root)
65
66
Eric Li6f27d4f2010-09-29 10:55:17 -070067class BootableKernel(object):
68
69 def __init__(self, job):
70 self.job = job
71 self.installed_as = None # kernel choice in bootloader menu
72 self.image = None
73 self.initrd = ''
74
75
76 def _boot_kernel(self, args, ident_check, expected_ident, subdir, notes):
77 """
78 Boot a kernel, with post-boot kernel id check
79
80 @param args: kernel cmdline arguments
81 @param ident_check: check kernel id after boot
82 @param expected_ident:
83 @param subdir: job-step qualifier in status log
84 @param notes: additional comment in status log
85 """
86
87 # If we can check the kernel identity do so.
88 if ident_check:
89 when = int(time.time())
90 args += " IDENT=%d" % when
91 self.job.next_step_prepend(["job.end_reboot_and_verify", when,
92 expected_ident, subdir, notes])
93 else:
94 self.job.next_step_prepend(["job.end_reboot", subdir,
95 expected_ident, notes])
96
97 # Point bootloader to the selected tag.
98 _add_kernel_to_bootloader(self.job.bootloader,
99 self.job.config_get('boot.default_args'),
100 self.installed_as, args, self.image,
101 self.initrd)
102
103 # defer fsck for next reboot, to avoid reboots back to default kernel
104 utils.system('touch /fastboot') # this file is removed automatically
105
106 # Boot it.
107 self.job.start_reboot()
108 self.job.reboot(tag=self.installed_as)
109
110
111class kernel(BootableKernel):
jadmanski0afbb632008-06-06 21:10:57 +0000112 """ Class for compiling kernels.
mblighc86b0b42006-07-28 17:35:28 +0000113
jadmanski0afbb632008-06-06 21:10:57 +0000114 Data for the object includes the src files
115 used to create the kernel, patches applied, config (base + changes),
116 the build directory itself, and logged output
mblighc86b0b42006-07-28 17:35:28 +0000117
jadmanski0afbb632008-06-06 21:10:57 +0000118 Properties:
119 job
120 Backpointer to the job object we're part of
121 autodir
122 Path to the top level autotest dir (/usr/local/autotest)
123 src_dir
124 <tmp_dir>/src/
125 build_dir
126 <tmp_dir>/linux/
127 config_dir
128 <results_dir>/config/
129 log_dir
130 <results_dir>/debug/
131 results_dir
132 <results_dir>/results/
133 """
mblighc86b0b42006-07-28 17:35:28 +0000134
jadmanski0afbb632008-06-06 21:10:57 +0000135 autodir = ''
mbligh8baa2ea2006-12-17 23:01:24 +0000136
mbligh925e1b12008-06-12 17:48:38 +0000137 def __init__(self, job, base_tree, subdir, tmp_dir, build_dir, leave=False):
jadmanski0afbb632008-06-06 21:10:57 +0000138 """Initialize the kernel build environment
mblighc86b0b42006-07-28 17:35:28 +0000139
jadmanski0afbb632008-06-06 21:10:57 +0000140 job
141 which job this build is part of
142 base_tree
143 base kernel tree. Can be one of the following:
144 1. A local tarball
145 2. A URL to a tarball
146 3. A local directory (will symlink it)
147 4. A shorthand expandable (eg '2.6.11-git3')
148 subdir
149 subdir in the results directory (eg "build")
150 (holds config/, debug/, results/)
151 tmp_dir
mbligh72b88fc2006-12-16 18:41:35 +0000152
jadmanski0afbb632008-06-06 21:10:57 +0000153 leave
154 Boolean, whether to leave existing tmpdir or not
155 """
Eric Li6f27d4f2010-09-29 10:55:17 -0700156 super(kernel, self).__init__(job)
jadmanski0afbb632008-06-06 21:10:57 +0000157 self.autodir = job.autodir
mblighf4c35322006-03-13 01:01:10 +0000158
jadmanski0afbb632008-06-06 21:10:57 +0000159 self.src_dir = os.path.join(tmp_dir, 'src')
160 self.build_dir = os.path.join(tmp_dir, build_dir)
161 # created by get_kernel_tree
162 self.config_dir = os.path.join(subdir, 'config')
163 self.log_dir = os.path.join(subdir, 'debug')
164 self.results_dir = os.path.join(subdir, 'results')
165 self.subdir = os.path.basename(subdir)
mbligh1e8858e2006-11-24 22:18:35 +0000166
jadmanski0afbb632008-06-06 21:10:57 +0000167 if not leave:
168 if os.path.isdir(self.src_dir):
169 utils.system('rm -rf ' + self.src_dir)
170 if os.path.isdir(self.build_dir):
171 utils.system('rm -rf ' + self.build_dir)
mbligh1e8858e2006-11-24 22:18:35 +0000172
jadmanski0afbb632008-06-06 21:10:57 +0000173 if not os.path.exists(self.src_dir):
174 os.mkdir(self.src_dir)
175 for path in [self.config_dir, self.log_dir, self.results_dir]:
176 if os.path.exists(path):
177 utils.system('rm -rf ' + path)
178 os.mkdir(path)
mblighf4c35322006-03-13 01:01:10 +0000179
jadmanski0afbb632008-06-06 21:10:57 +0000180 logpath = os.path.join(self.log_dir, 'build_log')
181 self.logfile = open(logpath, 'w+')
182 self.applied_patches = []
mbligh4426de02006-10-10 07:18:28 +0000183
jadmanski0afbb632008-06-06 21:10:57 +0000184 self.target_arch = None
185 self.build_target = 'bzImage'
186 self.build_image = None
mblighfdbcaec2006-10-01 23:28:57 +0000187
mbligh53da18e2009-01-05 21:13:26 +0000188 arch = utils.get_current_kernel_arch()
jadmanski0afbb632008-06-06 21:10:57 +0000189 if arch == 's390' or arch == 's390x':
190 self.build_target = 'image'
191 elif arch == 'ia64':
192 self.build_target = 'all'
193 self.build_image = 'vmlinux.gz'
mblighcac347a2007-06-02 17:21:48 +0000194
mbligh925e1b12008-06-12 17:48:38 +0000195 if not leave:
196 self.logfile.write('BASE: %s\n' % base_tree)
mbligh534015f2006-09-15 03:28:56 +0000197
mbligh925e1b12008-06-12 17:48:38 +0000198 # Where we have direct version hint record that
199 # for later configuration selection.
200 shorthand = re.compile(r'^\d+\.\d+\.\d+')
201 if shorthand.match(base_tree):
202 self.base_tree_version = base_tree
203 else:
204 self.base_tree_version = None
apw2366d992007-03-12 20:35:57 +0000205
mbligh925e1b12008-06-12 17:48:38 +0000206 # Actually extract the tree. Make sure we know it occured
207 self.extract(base_tree)
apw040dcaa2007-11-21 19:36:55 +0000208
apw7bae90e2008-03-05 12:18:11 +0000209
jadmanski0afbb632008-06-06 21:10:57 +0000210 def kernelexpand(self, kernel):
211 # If we have something like a path, just use it as it is
212 if '/' in kernel:
213 return [kernel]
apw7bae90e2008-03-05 12:18:11 +0000214
jadmanski0afbb632008-06-06 21:10:57 +0000215 # Find the configured mirror list.
216 mirrors = self.job.config_get('mirror.mirrors')
217 if not mirrors:
218 # LEGACY: convert the kernel.org mirror
219 mirror = self.job.config_get('mirror.ftp_kernel_org')
220 if mirror:
221 korg = 'http://www.kernel.org/pub/linux/kernel'
222 mirrors = [
223 [ korg + '/v2.6', mirror + '/v2.6' ],
mbligh9e6a4f12008-06-06 21:55:12 +0000224 [ korg + '/people/akpm/patches/2.6', mirror + '/akpm' ],
225 [ korg + '/people/mbligh', mirror + '/mbligh' ],
jadmanski0afbb632008-06-06 21:10:57 +0000226 ]
apw7bae90e2008-03-05 12:18:11 +0000227
jadmanski0afbb632008-06-06 21:10:57 +0000228 patches = kernelexpand.expand_classic(kernel, mirrors)
229 print patches
apw7bae90e2008-03-05 12:18:11 +0000230
jadmanski0afbb632008-06-06 21:10:57 +0000231 return patches
apw7bae90e2008-03-05 12:18:11 +0000232
mblighf4c35322006-03-13 01:01:10 +0000233
mbligh1b3b3762008-09-25 02:46:34 +0000234 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000235 @tee_output_logdir_mark
236 def extract(self, base_tree):
237 if os.path.exists(base_tree):
238 self.get_kernel_tree(base_tree)
239 else:
240 base_components = self.kernelexpand(base_tree)
241 print 'kernelexpand: '
242 print base_components
243 self.get_kernel_tree(base_components.pop(0))
244 if base_components: # apply remaining patches
245 self.patch(*base_components)
mblighf4c35322006-03-13 01:01:10 +0000246
mblighf4c35322006-03-13 01:01:10 +0000247
mbligh1b3b3762008-09-25 02:46:34 +0000248 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000249 @tee_output_logdir_mark
250 def patch(self, *patches):
251 """Apply a list of patches (in order)"""
252 if not patches:
253 return
254 print 'Applying patches: ', patches
255 self.apply_patches(self.get_patches(patches))
mblighf4c35322006-03-13 01:01:10 +0000256
mblighf4c35322006-03-13 01:01:10 +0000257
mbligh1b3b3762008-09-25 02:46:34 +0000258 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000259 @tee_output_logdir_mark
mblighb3400e02008-11-06 15:44:25 +0000260 def config(self, config_file = '', config_list = None, defconfig = False, make = None):
jadmanski0afbb632008-06-06 21:10:57 +0000261 self.set_cross_cc()
262 config = kernel_config.kernel_config(self.job, self.build_dir,
263 self.config_dir, config_file, config_list,
mblighb3400e02008-11-06 15:44:25 +0000264 defconfig, self.base_tree_version, make)
mblighf4c35322006-03-13 01:01:10 +0000265
mblighf4c35322006-03-13 01:01:10 +0000266
jadmanski0afbb632008-06-06 21:10:57 +0000267 def get_patches(self, patches):
268 """fetch the patches to the local src_dir"""
269 local_patches = []
270 for patch in patches:
mbligh1c9b1d22008-06-12 17:46:12 +0000271 dest = os.path.join(self.src_dir, os.path.basename(patch))
jadmanski0afbb632008-06-06 21:10:57 +0000272 # FIXME: this isn't unique. Append something to it
273 # like wget does if it's not there?
jadmanskie3f2f712008-06-12 17:54:56 +0000274 print "get_file %s %s %s %s" % (patch, dest, self.src_dir,
275 os.path.basename(patch))
jadmanski0afbb632008-06-06 21:10:57 +0000276 utils.get_file(patch, dest)
277 # probably safer to use the command, not python library
278 md5sum = utils.system_output('md5sum ' + dest).split()[0]
279 local_patches.append((patch, dest, md5sum))
280 return local_patches
mbligh72b88fc2006-12-16 18:41:35 +0000281
mblighf4c35322006-03-13 01:01:10 +0000282
jadmanski0afbb632008-06-06 21:10:57 +0000283 def apply_patches(self, local_patches):
284 """apply the list of patches, in order"""
285 builddir = self.build_dir
286 os.chdir(builddir)
mbligh72b88fc2006-12-16 18:41:35 +0000287
jadmanski0afbb632008-06-06 21:10:57 +0000288 if not local_patches:
289 return None
290 for (spec, local, md5sum) in local_patches:
291 if local.endswith('.bz2') or local.endswith('.gz'):
292 ref = spec
293 else:
mbligh53da18e2009-01-05 21:13:26 +0000294 ref = utils.force_copy(local, self.results_dir)
jadmanski0afbb632008-06-06 21:10:57 +0000295 ref = self.job.relative_path(ref)
296 patch_id = "%s %s %s" % (spec, ref, md5sum)
297 log = "PATCH: " + patch_id + "\n"
298 print log
mbligh53da18e2009-01-05 21:13:26 +0000299 utils.cat_file_to_cmd(local, 'patch -p1 > /dev/null')
jadmanski0afbb632008-06-06 21:10:57 +0000300 self.logfile.write(log)
301 self.applied_patches.append(patch_id)
mbligh72b88fc2006-12-16 18:41:35 +0000302
mblighf4c35322006-03-13 01:01:10 +0000303
jadmanski0afbb632008-06-06 21:10:57 +0000304 def get_kernel_tree(self, base_tree):
305 """Extract/link base_tree to self.build_dir"""
mbligh5970cf02006-08-06 15:39:22 +0000306
jadmanski0afbb632008-06-06 21:10:57 +0000307 # if base_tree is a dir, assume uncompressed kernel
308 if os.path.isdir(base_tree):
309 print 'Symlinking existing kernel source'
310 os.symlink(base_tree, self.build_dir)
mblighf4c35322006-03-13 01:01:10 +0000311
jadmanski0afbb632008-06-06 21:10:57 +0000312 # otherwise, extract tarball
313 else:
314 os.chdir(os.path.dirname(self.src_dir))
315 # Figure out local destination for tarball
mblighfef5ce22010-04-08 17:59:52 +0000316 tarball = os.path.join(self.src_dir, os.path.basename(base_tree.split(';')[0]))
jadmanski0afbb632008-06-06 21:10:57 +0000317 utils.get_file(base_tree, tarball)
318 print 'Extracting kernel tarball:', tarball, '...'
mbligh53da18e2009-01-05 21:13:26 +0000319 utils.extract_tarball_to_dir(tarball, self.build_dir)
mblighfdbcaec2006-10-01 23:28:57 +0000320
321
jadmanski0afbb632008-06-06 21:10:57 +0000322 def extraversion(self, tag, append=1):
323 os.chdir(self.build_dir)
324 extraversion_sub = r's/^EXTRAVERSION =\s*\(.*\)/EXTRAVERSION = '
325 if append:
326 p = extraversion_sub + '\\1-%s/' % tag
327 else:
328 p = extraversion_sub + '-%s/' % tag
329 utils.system('mv Makefile Makefile.old')
330 utils.system('sed "%s" < Makefile.old > Makefile' % p)
mbligh72b88fc2006-12-16 18:41:35 +0000331
apwc7846102006-04-06 18:22:13 +0000332
mbligh1b3b3762008-09-25 02:46:34 +0000333 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000334 @tee_output_logdir_mark
335 def build(self, make_opts = '', logfile = '', extraversion='autotest'):
336 """build the kernel
apwc7846102006-04-06 18:22:13 +0000337
jadmanski0afbb632008-06-06 21:10:57 +0000338 make_opts
339 additional options to make, if any
340 """
341 os_dep.commands('gcc', 'make')
342 if logfile == '':
343 logfile = os.path.join(self.log_dir, 'kernel_build')
344 os.chdir(self.build_dir)
345 if extraversion:
346 self.extraversion(extraversion)
347 self.set_cross_cc()
348 # setup_config_file(config_file, config_overrides)
mbligh1e8858e2006-11-24 22:18:35 +0000349
jadmanski0afbb632008-06-06 21:10:57 +0000350 # Not needed on 2.6, but hard to tell -- handle failure
351 utils.system('make dep', ignore_status=True)
mbligh53da18e2009-01-05 21:13:26 +0000352 threads = 2 * utils.count_cpus()
jadmanski0afbb632008-06-06 21:10:57 +0000353 build_string = 'make -j %d %s %s' % (threads, make_opts,
354 self.build_target)
355 # eg make bzImage, or make zImage
356 print build_string
mbligh925e1b12008-06-12 17:48:38 +0000357 utils.system(build_string)
jadmanski0afbb632008-06-06 21:10:57 +0000358 if kernel_config.modules_needed('.config'):
359 utils.system('make -j %d modules' % (threads))
mblighf4c35322006-03-13 01:01:10 +0000360
jadmanski0afbb632008-06-06 21:10:57 +0000361 kernel_version = self.get_kernel_build_ver()
362 kernel_version = re.sub('-autotest', '', kernel_version)
363 self.logfile.write('BUILD VERSION: %s\n' % kernel_version)
mblighf4c35322006-03-13 01:01:10 +0000364
mbligh53da18e2009-01-05 21:13:26 +0000365 utils.force_copy(self.build_dir+'/System.map',
mbligh925e1b12008-06-12 17:48:38 +0000366 self.results_dir)
mbligh30f28c52007-10-11 18:35:35 +0000367
mbligh30f28c52007-10-11 18:35:35 +0000368
jadmanski0afbb632008-06-06 21:10:57 +0000369 def build_timed(self, threads, timefile = '/dev/null', make_opts = '',
370 output = '/dev/null'):
371 """time the bulding of the kernel"""
372 os.chdir(self.build_dir)
373 self.set_cross_cc()
374
375 self.clean(logged=False)
376 build_string = "/usr/bin/time -o %s make %s -j %s vmlinux" \
377 % (timefile, make_opts, threads)
378 build_string += ' > %s 2>&1' % output
379 print build_string
380 utils.system(build_string)
381
382 if (not os.path.isfile('vmlinux')):
383 errmsg = "no vmlinux found, kernel build failed"
384 raise error.TestError(errmsg)
385
386
mbligh1b3b3762008-09-25 02:46:34 +0000387 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000388 @tee_output_logdir_mark
389 def clean(self):
390 """make clean in the kernel tree"""
391 os.chdir(self.build_dir)
392 print "make clean"
393 utils.system('make clean > /dev/null 2> /dev/null')
394
395
mbligh1b3b3762008-09-25 02:46:34 +0000396 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000397 @tee_output_logdir_mark
398 def mkinitrd(self, version, image, system_map, initrd):
399 """Build kernel initrd image.
400 Try to use distro specific way to build initrd image.
401 Parameters:
402 version
403 new kernel version
404 image
405 new kernel image file
406 system_map
407 System.map file
408 initrd
409 initrd image file to build
410 """
mbligh53da18e2009-01-05 21:13:26 +0000411 vendor = utils.get_os_vendor()
mblighb8a14e32006-05-06 00:17:35 +0000412
jadmanski0afbb632008-06-06 21:10:57 +0000413 if os.path.isfile(initrd):
414 print "Existing %s file, will remove it." % initrd
415 os.remove(initrd)
mblighb8a14e32006-05-06 00:17:35 +0000416
jadmanski0afbb632008-06-06 21:10:57 +0000417 args = self.job.config_get('kernel.mkinitrd_extra_args')
mblighf4c35322006-03-13 01:01:10 +0000418
jadmanski0afbb632008-06-06 21:10:57 +0000419 # don't leak 'None' into mkinitrd command
420 if not args:
421 args = ''
mbligh50f42ea2006-09-30 22:22:21 +0000422
jadmanski0afbb632008-06-06 21:10:57 +0000423 if vendor in ['Red Hat', 'Fedora Core']:
424 utils.system('mkinitrd %s %s %s' % (args, initrd, version))
425 elif vendor in ['SUSE']:
jadmanskid524b0e2008-09-15 14:28:20 +0000426 utils.system('mkinitrd %s -k %s -i %s -M %s' %
427 (args, image, initrd, system_map))
jadmanski0afbb632008-06-06 21:10:57 +0000428 elif vendor in ['Debian', 'Ubuntu']:
429 if os.path.isfile('/usr/sbin/mkinitrd'):
430 cmd = '/usr/sbin/mkinitrd'
431 elif os.path.isfile('/usr/sbin/mkinitramfs'):
432 cmd = '/usr/sbin/mkinitramfs'
433 else:
434 raise error.TestError('No Debian initrd builder')
435 utils.system('%s %s -o %s %s' % (cmd, args, initrd, version))
436 else:
437 raise error.TestError('Unsupported vendor %s' % vendor)
mbligh72b88fc2006-12-16 18:41:35 +0000438
apwe43a30b2007-09-25 16:51:30 +0000439
jadmanski0afbb632008-06-06 21:10:57 +0000440 def set_build_image(self, image):
441 self.build_image = image
mbligh3d515d42007-11-09 17:00:36 +0000442
mbligh50f42ea2006-09-30 22:22:21 +0000443
mbligh1b3b3762008-09-25 02:46:34 +0000444 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000445 @tee_output_logdir_mark
446 def install(self, tag='autotest', prefix = '/'):
447 """make install in the kernel tree"""
mbligh50f42ea2006-09-30 22:22:21 +0000448
jadmanski0afbb632008-06-06 21:10:57 +0000449 # Record that we have installed the kernel, and
450 # the tag under which we installed it.
451 self.installed_as = tag
mbligh8baa2ea2006-12-17 23:01:24 +0000452
jadmanski0afbb632008-06-06 21:10:57 +0000453 os.chdir(self.build_dir)
mbligh8baa2ea2006-12-17 23:01:24 +0000454
jadmanski0afbb632008-06-06 21:10:57 +0000455 if not os.path.isdir(prefix):
456 os.mkdir(prefix)
457 self.boot_dir = os.path.join(prefix, 'boot')
458 if not os.path.isdir(self.boot_dir):
459 os.mkdir(self.boot_dir)
apw87c65c12007-09-27 17:19:37 +0000460
jadmanski0afbb632008-06-06 21:10:57 +0000461 if not self.build_image:
462 images = glob.glob('arch/*/boot/' + self.build_target)
463 if len(images):
464 self.build_image = images[0]
465 else:
466 self.build_image = self.build_target
apw87c65c12007-09-27 17:19:37 +0000467
jadmanski0afbb632008-06-06 21:10:57 +0000468 # remember installed files
469 self.vmlinux = self.boot_dir + '/vmlinux-' + tag
470 if (self.build_image != 'vmlinux'):
471 self.image = self.boot_dir + '/vmlinuz-' + tag
472 else:
473 self.image = self.vmlinux
474 self.system_map = self.boot_dir + '/System.map-' + tag
mbligh925e1b12008-06-12 17:48:38 +0000475 self.config_file = self.boot_dir + '/config-' + tag
jadmanski0afbb632008-06-06 21:10:57 +0000476 self.initrd = ''
mbligh72b88fc2006-12-16 18:41:35 +0000477
jadmanski0afbb632008-06-06 21:10:57 +0000478 # copy to boot dir
mbligh53da18e2009-01-05 21:13:26 +0000479 utils.force_copy('vmlinux', self.vmlinux)
jadmanski0afbb632008-06-06 21:10:57 +0000480 if (self.build_image != 'vmlinux'):
mbligh53da18e2009-01-05 21:13:26 +0000481 utils.force_copy(self.build_image, self.image)
482 utils.force_copy('System.map', self.system_map)
483 utils.force_copy('.config', self.config_file)
mbligh0ad65582006-10-06 04:16:36 +0000484
jadmanski0afbb632008-06-06 21:10:57 +0000485 if not kernel_config.modules_needed('.config'):
486 return
mbligha87116f2006-10-10 02:47:08 +0000487
jadmanski0afbb632008-06-06 21:10:57 +0000488 utils.system('make modules_install INSTALL_MOD_PATH=%s' % prefix)
489 if prefix == '/':
490 self.initrd = self.boot_dir + '/initrd-' + tag
491 self.mkinitrd(self.get_kernel_build_ver(), self.image,
492 self.system_map, self.initrd)
mbligha87116f2006-10-10 02:47:08 +0000493
mbligha87116f2006-10-10 02:47:08 +0000494
jadmanski0afbb632008-06-06 21:10:57 +0000495 def get_kernel_build_arch(self, arch=None):
496 """
497 Work out the current kernel architecture (as a kernel arch)
498 """
499 if not arch:
mbligh53da18e2009-01-05 21:13:26 +0000500 arch = utils.get_current_kernel_arch()
jadmanski0afbb632008-06-06 21:10:57 +0000501 if re.match('i.86', arch):
502 return 'i386'
503 elif re.match('sun4u', arch):
504 return 'sparc64'
505 elif re.match('arm.*', arch):
506 return 'arm'
507 elif re.match('sa110', arch):
508 return 'arm'
509 elif re.match('s390x', arch):
510 return 's390'
511 elif re.match('parisc64', arch):
512 return 'parisc'
513 elif re.match('ppc.*', arch):
514 return 'powerpc'
515 elif re.match('mips.*', arch):
516 return 'mips'
517 else:
518 return arch
mbligh6a1d4db2006-10-06 04:30:16 +0000519
mbligh201aa892006-10-29 04:02:05 +0000520
jadmanski0afbb632008-06-06 21:10:57 +0000521 def get_kernel_build_release(self):
522 releasem = re.compile(r'.*UTS_RELEASE\s+"([^"]+)".*');
523 versionm = re.compile(r'.*UTS_VERSION\s+"([^"]+)".*');
mbligh548f29a2006-10-17 04:55:12 +0000524
jadmanski0afbb632008-06-06 21:10:57 +0000525 release = None
526 version = None
mbligh6a1d4db2006-10-06 04:30:16 +0000527
jadmanskid524b0e2008-09-15 14:28:20 +0000528 for f in [self.build_dir + "/include/linux/version.h",
529 self.build_dir + "/include/linux/utsrelease.h",
mbligh508cbf62009-11-06 03:01:50 +0000530 self.build_dir + "/include/linux/compile.h",
531 self.build_dir + "/include/generated/utsrelease.h",
532 self.build_dir + "/include/generated/compile.h"]:
jadmanskid524b0e2008-09-15 14:28:20 +0000533 if os.path.exists(f):
534 fd = open(f, 'r')
jadmanski0afbb632008-06-06 21:10:57 +0000535 for line in fd.readlines():
536 m = releasem.match(line)
537 if m:
538 release = m.groups()[0]
539 m = versionm.match(line)
540 if m:
541 version = m.groups()[0]
542 fd.close()
mbligh237bed32007-09-05 13:05:57 +0000543
jadmanski0afbb632008-06-06 21:10:57 +0000544 return (release, version)
mbligh237bed32007-09-05 13:05:57 +0000545
mbligh237bed32007-09-05 13:05:57 +0000546
jadmanski0afbb632008-06-06 21:10:57 +0000547 def get_kernel_build_ident(self):
548 (release, version) = self.get_kernel_build_release()
mbligh237bed32007-09-05 13:05:57 +0000549
jadmanski0afbb632008-06-06 21:10:57 +0000550 if not release or not version:
551 raise error.JobError('kernel has no identity')
mbligh237bed32007-09-05 13:05:57 +0000552
jadmanski0afbb632008-06-06 21:10:57 +0000553 return release + '::' + version
mbligh237bed32007-09-05 13:05:57 +0000554
mbligh237bed32007-09-05 13:05:57 +0000555
jadmanski067b26c2008-09-25 19:46:56 +0000556 def boot(self, args='', ident=True):
jadmanski0afbb632008-06-06 21:10:57 +0000557 """ install and boot this kernel, do not care how
558 just make it happen.
559 """
mbligh237bed32007-09-05 13:05:57 +0000560
Eric Li6f27d4f2010-09-29 10:55:17 -0700561 # If the kernel has not yet been installed,
562 # install it now as default tag.
jadmanski0afbb632008-06-06 21:10:57 +0000563 if not self.installed_as:
564 self.install()
mbligh237bed32007-09-05 13:05:57 +0000565
Eric Li6f27d4f2010-09-29 10:55:17 -0700566 expected_ident = self.get_kernel_build_ident()
567 self._boot_kernel(args, ident, expected_ident,
568 self.subdir, self.applied_patches)
apw1b5dc362006-10-31 11:24:26 +0000569
apw1b5dc362006-10-31 11:24:26 +0000570
jadmanski0afbb632008-06-06 21:10:57 +0000571 def get_kernel_build_ver(self):
572 """Check Makefile and .config to return kernel version"""
573 version = patchlevel = sublevel = extraversion = localversion = ''
apw1b5dc362006-10-31 11:24:26 +0000574
jadmanski0afbb632008-06-06 21:10:57 +0000575 for line in open(self.build_dir + '/Makefile', 'r').readlines():
576 if line.startswith('VERSION'):
577 version = line[line.index('=') + 1:].strip()
578 if line.startswith('PATCHLEVEL'):
579 patchlevel = line[line.index('=') + 1:].strip()
580 if line.startswith('SUBLEVEL'):
581 sublevel = line[line.index('=') + 1:].strip()
582 if line.startswith('EXTRAVERSION'):
583 extraversion = line[line.index('=') + 1:].strip()
mblighe11f5fc2006-10-04 04:42:22 +0000584
jadmanski0afbb632008-06-06 21:10:57 +0000585 for line in open(self.build_dir + '/.config', 'r').readlines():
586 if line.startswith('CONFIG_LOCALVERSION='):
587 localversion = line.rstrip().split('"')[1]
mblighe11f5fc2006-10-04 04:42:22 +0000588
jadmanski0afbb632008-06-06 21:10:57 +0000589 return "%s.%s.%s%s%s" %(version, patchlevel, sublevel, extraversion, localversion)
mblighe11f5fc2006-10-04 04:42:22 +0000590
mblighfdbcaec2006-10-01 23:28:57 +0000591
jadmanski0afbb632008-06-06 21:10:57 +0000592 def set_build_target(self, build_target):
593 if build_target:
594 self.build_target = build_target
595 print 'BUILD TARGET: %s' % self.build_target
mblighfdbcaec2006-10-01 23:28:57 +0000596
mbligh8baa2ea2006-12-17 23:01:24 +0000597
jadmanski0afbb632008-06-06 21:10:57 +0000598 def set_cross_cc(self, target_arch=None, cross_compile=None,
599 build_target='bzImage'):
600 """Set up to cross-compile.
601 This is broken. We need to work out what the default
602 compile produces, and if not, THEN set the cross
603 compiler.
604 """
mbligh8baa2ea2006-12-17 23:01:24 +0000605
jadmanski0afbb632008-06-06 21:10:57 +0000606 if self.target_arch:
607 return
mblighcc2e6662006-09-14 01:24:07 +0000608
jadmanski0afbb632008-06-06 21:10:57 +0000609 # if someone has set build_target, don't clobber in set_cross_cc
610 # run set_build_target before calling set_cross_cc
611 if not self.build_target:
612 self.set_build_target(build_target)
mbligh678823f2006-12-07 18:49:00 +0000613
jadmanski0afbb632008-06-06 21:10:57 +0000614 # If no 'target_arch' given assume native compilation
mblighd876f452008-12-03 15:09:17 +0000615 if target_arch is None:
mbligh53da18e2009-01-05 21:13:26 +0000616 target_arch = utils.get_current_kernel_arch()
jadmanski0afbb632008-06-06 21:10:57 +0000617 if target_arch == 'ppc64':
618 if self.build_target == 'bzImage':
619 self.build_target = 'vmlinux'
mbligh72b88fc2006-12-16 18:41:35 +0000620
jadmanski0afbb632008-06-06 21:10:57 +0000621 if not cross_compile:
622 cross_compile = self.job.config_get('kernel.cross_cc')
mbligh678823f2006-12-07 18:49:00 +0000623
jadmanski0afbb632008-06-06 21:10:57 +0000624 if cross_compile:
625 os.environ['CROSS_COMPILE'] = cross_compile
626 else:
627 if os.environ.has_key('CROSS_COMPILE'):
628 del os.environ['CROSS_COMPILE']
mbligh678823f2006-12-07 18:49:00 +0000629
jadmanski0afbb632008-06-06 21:10:57 +0000630 return # HACK. Crap out for now.
mblighf4c35322006-03-13 01:01:10 +0000631
jadmanski0afbb632008-06-06 21:10:57 +0000632 # At this point I know what arch I *want* to build for
633 # but have no way of working out what arch the default
634 # compiler DOES build for.
mblighcc2e6662006-09-14 01:24:07 +0000635
mbligh925e1b12008-06-12 17:48:38 +0000636 def install_package(package):
637 raise NotImplementedError("I don't exist yet!")
mbligh72b88fc2006-12-16 18:41:35 +0000638
jadmanski0afbb632008-06-06 21:10:57 +0000639 if target_arch == 'ppc64':
640 install_package('ppc64-cross')
641 cross_compile = os.path.join(self.autodir, 'sources/ppc64-cross/bin')
mblighcc2e6662006-09-14 01:24:07 +0000642
jadmanski0afbb632008-06-06 21:10:57 +0000643 elif target_arch == 'x86_64':
644 install_package('x86_64-cross')
645 cross_compile = os.path.join(self.autodir, 'sources/x86_64-cross/bin')
mblighb8a14e32006-05-06 00:17:35 +0000646
jadmanski0afbb632008-06-06 21:10:57 +0000647 os.environ['ARCH'] = self.target_arch = target_arch
mbligh5970cf02006-08-06 15:39:22 +0000648
jadmanski0afbb632008-06-06 21:10:57 +0000649 self.cross_compile = cross_compile
650 if self.cross_compile:
651 os.environ['CROSS_COMPILE'] = self.cross_compile
mblighcc2e6662006-09-14 01:24:07 +0000652
mbligh72b88fc2006-12-16 18:41:35 +0000653
jadmanski0afbb632008-06-06 21:10:57 +0000654 def pickle_dump(self, filename):
655 """dump a pickle of ourself out to the specified filename
mblighc86b0b42006-07-28 17:35:28 +0000656
jadmanski0afbb632008-06-06 21:10:57 +0000657 we can't pickle the backreference to job (it contains fd's),
658 nor would we want to. Same for logfile (fd's).
659 """
660 temp = copy.copy(self)
661 temp.job = None
662 temp.logfile = None
663 pickle.dump(temp, open(filename, 'w'))
mbligh736adc92007-10-18 03:23:22 +0000664
665
Eric Li6f27d4f2010-09-29 10:55:17 -0700666class rpm_kernel(BootableKernel):
mbligheaa75e52009-11-06 03:08:08 +0000667 """
668 Class for installing a binary rpm kernel package
jadmanski0afbb632008-06-06 21:10:57 +0000669 """
mbligh736adc92007-10-18 03:23:22 +0000670
jadmanski0afbb632008-06-06 21:10:57 +0000671 def __init__(self, job, rpm_package, subdir):
Eric Li6f27d4f2010-09-29 10:55:17 -0700672 super(rpm_kernel, self).__init__(job)
jadmanski0afbb632008-06-06 21:10:57 +0000673 self.rpm_package = rpm_package
674 self.log_dir = os.path.join(subdir, 'debug')
675 self.subdir = os.path.basename(subdir)
676 if os.path.exists(self.log_dir):
677 utils.system('rm -rf ' + self.log_dir)
678 os.mkdir(self.log_dir)
mbligh736adc92007-10-18 03:23:22 +0000679
680
mbligheaa75e52009-11-06 03:08:08 +0000681 def build(self, *args, **dargs):
682 """
683 Dummy function, binary kernel so nothing to build.
684 """
685 pass
686
687
mbligh1b3b3762008-09-25 02:46:34 +0000688 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000689 @tee_output_logdir_mark
mbligha25e8c32009-06-15 21:27:23 +0000690 def install(self, tag='autotest', install_vmlinux=True):
jadmanski0afbb632008-06-06 21:10:57 +0000691 self.installed_as = tag
mblighda0311e2007-10-25 16:03:33 +0000692
mbligh1b160a02009-05-21 01:27:10 +0000693 self.image = None
jadmanski0afbb632008-06-06 21:10:57 +0000694 self.initrd = ''
mbligh1b160a02009-05-21 01:27:10 +0000695 for rpm_pack in self.rpm_package:
696 rpm_name = utils.system_output('rpm -qp ' + rpm_pack)
mbligh736adc92007-10-18 03:23:22 +0000697
mbligh1b160a02009-05-21 01:27:10 +0000698 # install
699 utils.system('rpm -i --force ' + rpm_pack)
700
701 # get file list
702 files = utils.system_output('rpm -ql ' + rpm_name).splitlines()
703
704 # search for vmlinuz
705 for file in files:
706 if file.startswith('/boot/vmlinuz'):
707 self.full_version = file[len('/boot/vmlinuz-'):]
708 self.image = file
709 self.rpm_flavour = rpm_name.split('-')[1]
710
711 # get version and release number
712 self.version, self.release = utils.system_output(
713 'rpm --queryformat="%{VERSION}\\n%{RELEASE}\\n" -q '
714 + rpm_name).splitlines()[0:2]
715
716 # prefer /boot/kernel-version before /boot/kernel
717 if self.full_version:
718 break
719
720 # search for initrd
721 for file in files:
722 if file.startswith('/boot/initrd'):
723 self.initrd = file
724 # prefer /boot/initrd-version before /boot/initrd
725 if len(file) > len('/boot/initrd'):
726 break
727
728 if self.image == None:
729 errmsg = "specified rpm file(s) don't contain /boot/vmlinuz"
730 raise error.TestError(errmsg)
mbligh736adc92007-10-18 03:23:22 +0000731
mbligha25e8c32009-06-15 21:27:23 +0000732 # install vmlinux
733 if install_vmlinux:
734 for rpm_pack in self.rpm_package:
735 vmlinux = utils.system_output(
736 'rpm -q -l -p %s | grep /boot/vmlinux' % rpm_pack)
jadmanski19426ea2009-07-28 20:19:40 +0000737 utils.system('cd /; rpm2cpio %s | cpio -imuv .%s 2>&1'
mbligha25e8c32009-06-15 21:27:23 +0000738 % (rpm_pack, vmlinux))
739 if not os.path.exists(vmlinux):
740 raise error.TestError('%s does not exist after installing %s'
741 % (vmlinux, rpm_pack))
742
mbligh736adc92007-10-18 03:23:22 +0000743
jadmanski067b26c2008-09-25 19:46:56 +0000744 def boot(self, args='', ident=True):
jadmanski0afbb632008-06-06 21:10:57 +0000745 """ install and boot this kernel
746 """
mbligh73e82a32007-11-08 21:35:29 +0000747
Eric Li6f27d4f2010-09-29 10:55:17 -0700748 # If the kernel has not yet been installed,
749 # install it now as default tag.
jadmanski0afbb632008-06-06 21:10:57 +0000750 if not self.installed_as:
751 self.install()
mblighda0311e2007-10-25 16:03:33 +0000752
mblighb1887c82009-03-12 00:25:48 +0000753 expected_ident = self.full_version
754 if not expected_ident:
755 expected_ident = '-'.join([self.version,
mbligh1b160a02009-05-21 01:27:10 +0000756 self.rpm_flavour,
mblighb1887c82009-03-12 00:25:48 +0000757 self.release])
mbligh10a24a72007-10-24 21:02:53 +0000758
Eric Li6f27d4f2010-09-29 10:55:17 -0700759 self._boot_kernel(args, ident, expected_ident,
760 None, 'rpm')
mbligh6ee7ee02007-11-13 23:49:05 +0000761
762
mblighe7785cc2009-03-17 17:32:47 +0000763class rpm_kernel_suse(rpm_kernel):
764 """ Class for installing openSUSE/SLE rpm kernel package
765 """
766
767 def install(self):
768 # do not set the new kernel as the default one
769 os.environ['PBL_AUTOTEST'] = '1'
770
771 rpm_kernel.install(self, 'dummy')
772 self.installed_as = self.job.bootloader.get_title_for_kernel(self.image)
773 if not self.installed_as:
774 errmsg = "cannot find installed kernel in bootloader configuration"
775 raise error.TestError(errmsg)
776
777
778 def add_to_bootloader(self, tag='dummy', args=''):
779 """ Set parameters of this kernel in bootloader
780 """
781
782 # pull the base argument set from the job config
783 baseargs = self.job.config_get('boot.default_args')
784 if baseargs:
785 args = baseargs + ' ' + args
786
787 self.job.bootloader.add_args(tag, args)
788
789
790def rpm_kernel_vendor(job, rpm_package, subdir):
mbligh1ef218d2009-08-03 16:57:56 +0000791 vendor = utils.get_os_vendor()
792 if vendor == "SUSE":
793 return rpm_kernel_suse(job, rpm_package, subdir)
794 else:
795 return rpm_kernel(job, rpm_package, subdir)
mblighe7785cc2009-03-17 17:32:47 +0000796
797
mbligh062ed152009-01-13 00:57:14 +0000798# just make the preprocessor a nop
799def _preprocess_path_dummy(path):
800 return path.strip()
801
802
mbligh6ee7ee02007-11-13 23:49:05 +0000803# pull in some optional site-specific path pre-processing
jadmanski19426ea2009-07-28 20:19:40 +0000804preprocess_path = utils.import_site_function(__file__,
mbligh062ed152009-01-13 00:57:14 +0000805 "autotest_lib.client.bin.site_kernel", "preprocess_path",
806 _preprocess_path_dummy)
mbligh6ee7ee02007-11-13 23:49:05 +0000807
mblighc5ddfd12008-08-04 17:15:00 +0000808
mbligh6ee7ee02007-11-13 23:49:05 +0000809def auto_kernel(job, path, subdir, tmp_dir, build_dir, leave=False):
mbligh7aeda672009-01-30 00:35:59 +0000810 """
jadmanski0afbb632008-06-06 21:10:57 +0000811 Create a kernel object, dynamically selecting the appropriate class to use
812 based on the path provided.
813 """
mbligh1b160a02009-05-21 01:27:10 +0000814 kernel_paths = [preprocess_path(path)]
815 if kernel_paths[0].endswith('.list'):
mbligh1ef218d2009-08-03 16:57:56 +0000816 # Fetch the list of packages to install
mbligh1b160a02009-05-21 01:27:10 +0000817 kernel_list = os.path.join(tmp_dir, 'kernel.list')
818 utils.get_file(kernel_paths[0], kernel_list)
819 kernel_paths = [p.strip() for p in open(kernel_list).readlines()]
mblighc5ddfd12008-08-04 17:15:00 +0000820
mbligh1b160a02009-05-21 01:27:10 +0000821 if kernel_paths[0].endswith('.rpm'):
822 rpm_paths = []
823 for kernel_path in kernel_paths:
jadmanski19426ea2009-07-28 20:19:40 +0000824 if os.path.exists(kernel_path):
mbligh1b160a02009-05-21 01:27:10 +0000825 rpm_paths.append(kernel_path)
mbligh1b160a02009-05-21 01:27:10 +0000826 else:
827 # Fetch the rpm into the job's packages directory and pass it to
828 # rpm_kernel
829 rpm_name = os.path.basename(kernel_path)
mbligh7aeda672009-01-30 00:35:59 +0000830
mbligh1b160a02009-05-21 01:27:10 +0000831 # If the preprocessed path (kernel_path) is only a name then
832 # search for the kernel in all the repositories, else fetch the
833 # kernel from that specific path.
834 job.pkgmgr.fetch_pkg(rpm_name, os.path.join(job.pkgdir, rpm_name),
835 repo_url=os.path.dirname(kernel_path))
836
837 rpm_paths.append(os.path.join(job.pkgdir, rpm_name))
838 return rpm_kernel_vendor(job, rpm_paths, subdir)
jadmanski0afbb632008-06-06 21:10:57 +0000839 else:
mbligh1b160a02009-05-21 01:27:10 +0000840 if len(kernel_paths) > 1:
841 raise error.TestError("don't know what to do with more than one non-rpm kernel file")
842 return kernel(job,kernel_paths[0], subdir, tmp_dir, build_dir, leave)