blob: f3f5a38c540c3f6741e0d4489f7143c56c451947 [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
Eric Lie0493a42010-11-15 13:05:43 -080062 # Add the kernel entry. it will keep all arguments from the default entry.
63 # args='_dummy_' is used to workaround a boottool limitation of not being
64 # able to add arguments to a kernel that does not already have any of its
65 # own by way of its own append= section below the image= line in lilo.conf.
66 bootloader.add_kernel(image, tag, initrd=initrd, root=root, args='_dummy_')
67 # Now, for each argument in arglist, try to add it to the kernel that was
68 # just added. In each step, if the arg already existed on the args string,
69 # that particular arg will be skipped
70 for a in arglist:
71 bootloader.add_args(kernel=tag, args=a)
72 bootloader.remove_args(kernel=tag, args='_dummy_')
mbligh516c8df2010-02-12 18:48:52 +000073
74
Eric Li6f27d4f2010-09-29 10:55:17 -070075class BootableKernel(object):
76
77 def __init__(self, job):
78 self.job = job
79 self.installed_as = None # kernel choice in bootloader menu
80 self.image = None
81 self.initrd = ''
82
83
84 def _boot_kernel(self, args, ident_check, expected_ident, subdir, notes):
85 """
86 Boot a kernel, with post-boot kernel id check
87
88 @param args: kernel cmdline arguments
89 @param ident_check: check kernel id after boot
90 @param expected_ident:
91 @param subdir: job-step qualifier in status log
92 @param notes: additional comment in status log
93 """
Eric Li6f27d4f2010-09-29 10:55:17 -070094 # If we can check the kernel identity do so.
95 if ident_check:
96 when = int(time.time())
97 args += " IDENT=%d" % when
98 self.job.next_step_prepend(["job.end_reboot_and_verify", when,
99 expected_ident, subdir, notes])
100 else:
101 self.job.next_step_prepend(["job.end_reboot", subdir,
102 expected_ident, notes])
103
Eric Lie0493a42010-11-15 13:05:43 -0800104 self.add_to_bootloader(args)
Eric Li6f27d4f2010-09-29 10:55:17 -0700105
106 # defer fsck for next reboot, to avoid reboots back to default kernel
107 utils.system('touch /fastboot') # this file is removed automatically
108
109 # Boot it.
110 self.job.start_reboot()
111 self.job.reboot(tag=self.installed_as)
112
113
Eric Lie0493a42010-11-15 13:05:43 -0800114 def add_to_bootloader(self, args=''):
115 # Point bootloader to the selected tag.
116 _add_kernel_to_bootloader(self.job.bootloader,
117 self.job.config_get('boot.default_args'),
118 self.installed_as, args, self.image,
119 self.initrd)
120
121
Eric Li6f27d4f2010-09-29 10:55:17 -0700122class kernel(BootableKernel):
jadmanski0afbb632008-06-06 21:10:57 +0000123 """ Class for compiling kernels.
mblighc86b0b42006-07-28 17:35:28 +0000124
jadmanski0afbb632008-06-06 21:10:57 +0000125 Data for the object includes the src files
126 used to create the kernel, patches applied, config (base + changes),
127 the build directory itself, and logged output
mblighc86b0b42006-07-28 17:35:28 +0000128
jadmanski0afbb632008-06-06 21:10:57 +0000129 Properties:
130 job
131 Backpointer to the job object we're part of
132 autodir
133 Path to the top level autotest dir (/usr/local/autotest)
134 src_dir
135 <tmp_dir>/src/
136 build_dir
137 <tmp_dir>/linux/
138 config_dir
139 <results_dir>/config/
140 log_dir
141 <results_dir>/debug/
142 results_dir
143 <results_dir>/results/
144 """
mblighc86b0b42006-07-28 17:35:28 +0000145
jadmanski0afbb632008-06-06 21:10:57 +0000146 autodir = ''
mbligh8baa2ea2006-12-17 23:01:24 +0000147
mbligh925e1b12008-06-12 17:48:38 +0000148 def __init__(self, job, base_tree, subdir, tmp_dir, build_dir, leave=False):
jadmanski0afbb632008-06-06 21:10:57 +0000149 """Initialize the kernel build environment
mblighc86b0b42006-07-28 17:35:28 +0000150
jadmanski0afbb632008-06-06 21:10:57 +0000151 job
152 which job this build is part of
153 base_tree
154 base kernel tree. Can be one of the following:
155 1. A local tarball
156 2. A URL to a tarball
157 3. A local directory (will symlink it)
158 4. A shorthand expandable (eg '2.6.11-git3')
159 subdir
160 subdir in the results directory (eg "build")
161 (holds config/, debug/, results/)
162 tmp_dir
mbligh72b88fc2006-12-16 18:41:35 +0000163
jadmanski0afbb632008-06-06 21:10:57 +0000164 leave
165 Boolean, whether to leave existing tmpdir or not
166 """
Eric Li6f27d4f2010-09-29 10:55:17 -0700167 super(kernel, self).__init__(job)
jadmanski0afbb632008-06-06 21:10:57 +0000168 self.autodir = job.autodir
mblighf4c35322006-03-13 01:01:10 +0000169
jadmanski0afbb632008-06-06 21:10:57 +0000170 self.src_dir = os.path.join(tmp_dir, 'src')
171 self.build_dir = os.path.join(tmp_dir, build_dir)
172 # created by get_kernel_tree
173 self.config_dir = os.path.join(subdir, 'config')
174 self.log_dir = os.path.join(subdir, 'debug')
175 self.results_dir = os.path.join(subdir, 'results')
176 self.subdir = os.path.basename(subdir)
mbligh1e8858e2006-11-24 22:18:35 +0000177
jadmanski0afbb632008-06-06 21:10:57 +0000178 if not leave:
179 if os.path.isdir(self.src_dir):
180 utils.system('rm -rf ' + self.src_dir)
181 if os.path.isdir(self.build_dir):
182 utils.system('rm -rf ' + self.build_dir)
mbligh1e8858e2006-11-24 22:18:35 +0000183
jadmanski0afbb632008-06-06 21:10:57 +0000184 if not os.path.exists(self.src_dir):
185 os.mkdir(self.src_dir)
186 for path in [self.config_dir, self.log_dir, self.results_dir]:
187 if os.path.exists(path):
188 utils.system('rm -rf ' + path)
189 os.mkdir(path)
mblighf4c35322006-03-13 01:01:10 +0000190
jadmanski0afbb632008-06-06 21:10:57 +0000191 logpath = os.path.join(self.log_dir, 'build_log')
192 self.logfile = open(logpath, 'w+')
193 self.applied_patches = []
mbligh4426de02006-10-10 07:18:28 +0000194
jadmanski0afbb632008-06-06 21:10:57 +0000195 self.target_arch = None
196 self.build_target = 'bzImage'
197 self.build_image = None
mblighfdbcaec2006-10-01 23:28:57 +0000198
mbligh53da18e2009-01-05 21:13:26 +0000199 arch = utils.get_current_kernel_arch()
jadmanski0afbb632008-06-06 21:10:57 +0000200 if arch == 's390' or arch == 's390x':
201 self.build_target = 'image'
202 elif arch == 'ia64':
203 self.build_target = 'all'
204 self.build_image = 'vmlinux.gz'
mblighcac347a2007-06-02 17:21:48 +0000205
mbligh925e1b12008-06-12 17:48:38 +0000206 if not leave:
207 self.logfile.write('BASE: %s\n' % base_tree)
mbligh534015f2006-09-15 03:28:56 +0000208
mbligh925e1b12008-06-12 17:48:38 +0000209 # Where we have direct version hint record that
210 # for later configuration selection.
211 shorthand = re.compile(r'^\d+\.\d+\.\d+')
212 if shorthand.match(base_tree):
213 self.base_tree_version = base_tree
214 else:
215 self.base_tree_version = None
apw2366d992007-03-12 20:35:57 +0000216
mbligh925e1b12008-06-12 17:48:38 +0000217 # Actually extract the tree. Make sure we know it occured
218 self.extract(base_tree)
apw040dcaa2007-11-21 19:36:55 +0000219
apw7bae90e2008-03-05 12:18:11 +0000220
jadmanski0afbb632008-06-06 21:10:57 +0000221 def kernelexpand(self, kernel):
222 # If we have something like a path, just use it as it is
223 if '/' in kernel:
224 return [kernel]
apw7bae90e2008-03-05 12:18:11 +0000225
jadmanski0afbb632008-06-06 21:10:57 +0000226 # Find the configured mirror list.
227 mirrors = self.job.config_get('mirror.mirrors')
228 if not mirrors:
229 # LEGACY: convert the kernel.org mirror
230 mirror = self.job.config_get('mirror.ftp_kernel_org')
231 if mirror:
232 korg = 'http://www.kernel.org/pub/linux/kernel'
233 mirrors = [
234 [ korg + '/v2.6', mirror + '/v2.6' ],
mbligh9e6a4f12008-06-06 21:55:12 +0000235 [ korg + '/people/akpm/patches/2.6', mirror + '/akpm' ],
236 [ korg + '/people/mbligh', mirror + '/mbligh' ],
jadmanski0afbb632008-06-06 21:10:57 +0000237 ]
apw7bae90e2008-03-05 12:18:11 +0000238
jadmanski0afbb632008-06-06 21:10:57 +0000239 patches = kernelexpand.expand_classic(kernel, mirrors)
240 print patches
apw7bae90e2008-03-05 12:18:11 +0000241
jadmanski0afbb632008-06-06 21:10:57 +0000242 return patches
apw7bae90e2008-03-05 12:18:11 +0000243
mblighf4c35322006-03-13 01:01:10 +0000244
mbligh1b3b3762008-09-25 02:46:34 +0000245 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000246 @tee_output_logdir_mark
247 def extract(self, base_tree):
248 if os.path.exists(base_tree):
249 self.get_kernel_tree(base_tree)
250 else:
251 base_components = self.kernelexpand(base_tree)
252 print 'kernelexpand: '
253 print base_components
254 self.get_kernel_tree(base_components.pop(0))
255 if base_components: # apply remaining patches
256 self.patch(*base_components)
mblighf4c35322006-03-13 01:01:10 +0000257
mblighf4c35322006-03-13 01:01:10 +0000258
mbligh1b3b3762008-09-25 02:46:34 +0000259 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000260 @tee_output_logdir_mark
261 def patch(self, *patches):
262 """Apply a list of patches (in order)"""
263 if not patches:
264 return
265 print 'Applying patches: ', patches
266 self.apply_patches(self.get_patches(patches))
mblighf4c35322006-03-13 01:01:10 +0000267
mblighf4c35322006-03-13 01:01:10 +0000268
mbligh1b3b3762008-09-25 02:46:34 +0000269 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000270 @tee_output_logdir_mark
mblighb3400e02008-11-06 15:44:25 +0000271 def config(self, config_file = '', config_list = None, defconfig = False, make = None):
jadmanski0afbb632008-06-06 21:10:57 +0000272 self.set_cross_cc()
273 config = kernel_config.kernel_config(self.job, self.build_dir,
274 self.config_dir, config_file, config_list,
mblighb3400e02008-11-06 15:44:25 +0000275 defconfig, self.base_tree_version, make)
mblighf4c35322006-03-13 01:01:10 +0000276
mblighf4c35322006-03-13 01:01:10 +0000277
jadmanski0afbb632008-06-06 21:10:57 +0000278 def get_patches(self, patches):
279 """fetch the patches to the local src_dir"""
280 local_patches = []
281 for patch in patches:
mbligh1c9b1d22008-06-12 17:46:12 +0000282 dest = os.path.join(self.src_dir, os.path.basename(patch))
jadmanski0afbb632008-06-06 21:10:57 +0000283 # FIXME: this isn't unique. Append something to it
284 # like wget does if it's not there?
jadmanskie3f2f712008-06-12 17:54:56 +0000285 print "get_file %s %s %s %s" % (patch, dest, self.src_dir,
286 os.path.basename(patch))
jadmanski0afbb632008-06-06 21:10:57 +0000287 utils.get_file(patch, dest)
288 # probably safer to use the command, not python library
289 md5sum = utils.system_output('md5sum ' + dest).split()[0]
290 local_patches.append((patch, dest, md5sum))
291 return local_patches
mbligh72b88fc2006-12-16 18:41:35 +0000292
mblighf4c35322006-03-13 01:01:10 +0000293
jadmanski0afbb632008-06-06 21:10:57 +0000294 def apply_patches(self, local_patches):
295 """apply the list of patches, in order"""
296 builddir = self.build_dir
297 os.chdir(builddir)
mbligh72b88fc2006-12-16 18:41:35 +0000298
jadmanski0afbb632008-06-06 21:10:57 +0000299 if not local_patches:
300 return None
301 for (spec, local, md5sum) in local_patches:
302 if local.endswith('.bz2') or local.endswith('.gz'):
303 ref = spec
304 else:
mbligh53da18e2009-01-05 21:13:26 +0000305 ref = utils.force_copy(local, self.results_dir)
jadmanski0afbb632008-06-06 21:10:57 +0000306 ref = self.job.relative_path(ref)
307 patch_id = "%s %s %s" % (spec, ref, md5sum)
308 log = "PATCH: " + patch_id + "\n"
309 print log
mbligh53da18e2009-01-05 21:13:26 +0000310 utils.cat_file_to_cmd(local, 'patch -p1 > /dev/null')
jadmanski0afbb632008-06-06 21:10:57 +0000311 self.logfile.write(log)
312 self.applied_patches.append(patch_id)
mbligh72b88fc2006-12-16 18:41:35 +0000313
mblighf4c35322006-03-13 01:01:10 +0000314
jadmanski0afbb632008-06-06 21:10:57 +0000315 def get_kernel_tree(self, base_tree):
316 """Extract/link base_tree to self.build_dir"""
mbligh5970cf02006-08-06 15:39:22 +0000317
jadmanski0afbb632008-06-06 21:10:57 +0000318 # if base_tree is a dir, assume uncompressed kernel
319 if os.path.isdir(base_tree):
320 print 'Symlinking existing kernel source'
Eric Lie0493a42010-11-15 13:05:43 -0800321 if os.path.islink(self.build_dir):
322 os.remove(self.build_dir)
jadmanski0afbb632008-06-06 21:10:57 +0000323 os.symlink(base_tree, self.build_dir)
mblighf4c35322006-03-13 01:01:10 +0000324
jadmanski0afbb632008-06-06 21:10:57 +0000325 # otherwise, extract tarball
326 else:
327 os.chdir(os.path.dirname(self.src_dir))
328 # Figure out local destination for tarball
mblighfef5ce22010-04-08 17:59:52 +0000329 tarball = os.path.join(self.src_dir, os.path.basename(base_tree.split(';')[0]))
jadmanski0afbb632008-06-06 21:10:57 +0000330 utils.get_file(base_tree, tarball)
331 print 'Extracting kernel tarball:', tarball, '...'
mbligh53da18e2009-01-05 21:13:26 +0000332 utils.extract_tarball_to_dir(tarball, self.build_dir)
mblighfdbcaec2006-10-01 23:28:57 +0000333
334
jadmanski0afbb632008-06-06 21:10:57 +0000335 def extraversion(self, tag, append=1):
336 os.chdir(self.build_dir)
337 extraversion_sub = r's/^EXTRAVERSION =\s*\(.*\)/EXTRAVERSION = '
338 if append:
339 p = extraversion_sub + '\\1-%s/' % tag
340 else:
341 p = extraversion_sub + '-%s/' % tag
342 utils.system('mv Makefile Makefile.old')
343 utils.system('sed "%s" < Makefile.old > Makefile' % p)
mbligh72b88fc2006-12-16 18:41:35 +0000344
apwc7846102006-04-06 18:22:13 +0000345
mbligh1b3b3762008-09-25 02:46:34 +0000346 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000347 @tee_output_logdir_mark
348 def build(self, make_opts = '', logfile = '', extraversion='autotest'):
349 """build the kernel
apwc7846102006-04-06 18:22:13 +0000350
jadmanski0afbb632008-06-06 21:10:57 +0000351 make_opts
352 additional options to make, if any
353 """
354 os_dep.commands('gcc', 'make')
355 if logfile == '':
356 logfile = os.path.join(self.log_dir, 'kernel_build')
357 os.chdir(self.build_dir)
358 if extraversion:
359 self.extraversion(extraversion)
360 self.set_cross_cc()
361 # setup_config_file(config_file, config_overrides)
mbligh1e8858e2006-11-24 22:18:35 +0000362
jadmanski0afbb632008-06-06 21:10:57 +0000363 # Not needed on 2.6, but hard to tell -- handle failure
364 utils.system('make dep', ignore_status=True)
mbligh53da18e2009-01-05 21:13:26 +0000365 threads = 2 * utils.count_cpus()
jadmanski0afbb632008-06-06 21:10:57 +0000366 build_string = 'make -j %d %s %s' % (threads, make_opts,
367 self.build_target)
368 # eg make bzImage, or make zImage
369 print build_string
mbligh925e1b12008-06-12 17:48:38 +0000370 utils.system(build_string)
jadmanski0afbb632008-06-06 21:10:57 +0000371 if kernel_config.modules_needed('.config'):
372 utils.system('make -j %d modules' % (threads))
mblighf4c35322006-03-13 01:01:10 +0000373
jadmanski0afbb632008-06-06 21:10:57 +0000374 kernel_version = self.get_kernel_build_ver()
375 kernel_version = re.sub('-autotest', '', kernel_version)
376 self.logfile.write('BUILD VERSION: %s\n' % kernel_version)
mblighf4c35322006-03-13 01:01:10 +0000377
mbligh53da18e2009-01-05 21:13:26 +0000378 utils.force_copy(self.build_dir+'/System.map',
mbligh925e1b12008-06-12 17:48:38 +0000379 self.results_dir)
mbligh30f28c52007-10-11 18:35:35 +0000380
mbligh30f28c52007-10-11 18:35:35 +0000381
jadmanski0afbb632008-06-06 21:10:57 +0000382 def build_timed(self, threads, timefile = '/dev/null', make_opts = '',
383 output = '/dev/null'):
384 """time the bulding of the kernel"""
385 os.chdir(self.build_dir)
386 self.set_cross_cc()
387
Eric Lie0493a42010-11-15 13:05:43 -0800388 self.clean()
jadmanski0afbb632008-06-06 21:10:57 +0000389 build_string = "/usr/bin/time -o %s make %s -j %s vmlinux" \
390 % (timefile, make_opts, threads)
391 build_string += ' > %s 2>&1' % output
392 print build_string
393 utils.system(build_string)
394
395 if (not os.path.isfile('vmlinux')):
396 errmsg = "no vmlinux found, kernel build failed"
397 raise error.TestError(errmsg)
398
399
mbligh1b3b3762008-09-25 02:46:34 +0000400 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000401 @tee_output_logdir_mark
402 def clean(self):
403 """make clean in the kernel tree"""
404 os.chdir(self.build_dir)
405 print "make clean"
406 utils.system('make clean > /dev/null 2> /dev/null')
407
408
mbligh1b3b3762008-09-25 02:46:34 +0000409 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000410 @tee_output_logdir_mark
411 def mkinitrd(self, version, image, system_map, initrd):
412 """Build kernel initrd image.
413 Try to use distro specific way to build initrd image.
414 Parameters:
415 version
416 new kernel version
417 image
418 new kernel image file
419 system_map
420 System.map file
421 initrd
422 initrd image file to build
423 """
mbligh53da18e2009-01-05 21:13:26 +0000424 vendor = utils.get_os_vendor()
mblighb8a14e32006-05-06 00:17:35 +0000425
jadmanski0afbb632008-06-06 21:10:57 +0000426 if os.path.isfile(initrd):
427 print "Existing %s file, will remove it." % initrd
428 os.remove(initrd)
mblighb8a14e32006-05-06 00:17:35 +0000429
jadmanski0afbb632008-06-06 21:10:57 +0000430 args = self.job.config_get('kernel.mkinitrd_extra_args')
mblighf4c35322006-03-13 01:01:10 +0000431
jadmanski0afbb632008-06-06 21:10:57 +0000432 # don't leak 'None' into mkinitrd command
433 if not args:
434 args = ''
mbligh50f42ea2006-09-30 22:22:21 +0000435
Eric Lie0493a42010-11-15 13:05:43 -0800436 # It is important to match the version with a real directory inside
437 # /lib/modules
438 real_version_list = glob.glob('/lib/modules/%s*' % version)
439 rl = len(real_version_list)
440 if rl == 0:
441 logging.error("No directory %s found under /lib/modules. Initramfs"
442 "creation will most likely fail and your new kernel"
443 "will fail to build", version)
444 else:
445 if rl > 1:
446 logging.warning("Found more than one possible match for "
447 "kernel version %s under /lib/modules", version)
448 version = os.path.basename(real_version_list[0])
449
jadmanski0afbb632008-06-06 21:10:57 +0000450 if vendor in ['Red Hat', 'Fedora Core']:
Eric Lie0493a42010-11-15 13:05:43 -0800451 try:
452 cmd = os_dep.command('dracut')
453 full_cmd = '%s -f %s %s' % (cmd, initrd, version)
454 except ValueError:
455 cmd = os_dep.command('mkinitrd')
456 full_cmd = '%s %s %s %s' % (cmd, args, initrd, version)
457 utils.system(full_cmd)
jadmanski0afbb632008-06-06 21:10:57 +0000458 elif vendor in ['SUSE']:
jadmanskid524b0e2008-09-15 14:28:20 +0000459 utils.system('mkinitrd %s -k %s -i %s -M %s' %
460 (args, image, initrd, system_map))
jadmanski0afbb632008-06-06 21:10:57 +0000461 elif vendor in ['Debian', 'Ubuntu']:
462 if os.path.isfile('/usr/sbin/mkinitrd'):
463 cmd = '/usr/sbin/mkinitrd'
464 elif os.path.isfile('/usr/sbin/mkinitramfs'):
465 cmd = '/usr/sbin/mkinitramfs'
466 else:
467 raise error.TestError('No Debian initrd builder')
468 utils.system('%s %s -o %s %s' % (cmd, args, initrd, version))
469 else:
470 raise error.TestError('Unsupported vendor %s' % vendor)
mbligh72b88fc2006-12-16 18:41:35 +0000471
apwe43a30b2007-09-25 16:51:30 +0000472
jadmanski0afbb632008-06-06 21:10:57 +0000473 def set_build_image(self, image):
474 self.build_image = image
mbligh3d515d42007-11-09 17:00:36 +0000475
mbligh50f42ea2006-09-30 22:22:21 +0000476
mbligh1b3b3762008-09-25 02:46:34 +0000477 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000478 @tee_output_logdir_mark
479 def install(self, tag='autotest', prefix = '/'):
480 """make install in the kernel tree"""
mbligh50f42ea2006-09-30 22:22:21 +0000481
jadmanski0afbb632008-06-06 21:10:57 +0000482 # Record that we have installed the kernel, and
483 # the tag under which we installed it.
484 self.installed_as = tag
mbligh8baa2ea2006-12-17 23:01:24 +0000485
jadmanski0afbb632008-06-06 21:10:57 +0000486 os.chdir(self.build_dir)
mbligh8baa2ea2006-12-17 23:01:24 +0000487
jadmanski0afbb632008-06-06 21:10:57 +0000488 if not os.path.isdir(prefix):
489 os.mkdir(prefix)
490 self.boot_dir = os.path.join(prefix, 'boot')
491 if not os.path.isdir(self.boot_dir):
492 os.mkdir(self.boot_dir)
apw87c65c12007-09-27 17:19:37 +0000493
jadmanski0afbb632008-06-06 21:10:57 +0000494 if not self.build_image:
495 images = glob.glob('arch/*/boot/' + self.build_target)
496 if len(images):
497 self.build_image = images[0]
498 else:
499 self.build_image = self.build_target
apw87c65c12007-09-27 17:19:37 +0000500
jadmanski0afbb632008-06-06 21:10:57 +0000501 # remember installed files
502 self.vmlinux = self.boot_dir + '/vmlinux-' + tag
503 if (self.build_image != 'vmlinux'):
504 self.image = self.boot_dir + '/vmlinuz-' + tag
505 else:
506 self.image = self.vmlinux
507 self.system_map = self.boot_dir + '/System.map-' + tag
mbligh925e1b12008-06-12 17:48:38 +0000508 self.config_file = self.boot_dir + '/config-' + tag
jadmanski0afbb632008-06-06 21:10:57 +0000509 self.initrd = ''
mbligh72b88fc2006-12-16 18:41:35 +0000510
jadmanski0afbb632008-06-06 21:10:57 +0000511 # copy to boot dir
mbligh53da18e2009-01-05 21:13:26 +0000512 utils.force_copy('vmlinux', self.vmlinux)
jadmanski0afbb632008-06-06 21:10:57 +0000513 if (self.build_image != 'vmlinux'):
mbligh53da18e2009-01-05 21:13:26 +0000514 utils.force_copy(self.build_image, self.image)
515 utils.force_copy('System.map', self.system_map)
516 utils.force_copy('.config', self.config_file)
mbligh0ad65582006-10-06 04:16:36 +0000517
jadmanski0afbb632008-06-06 21:10:57 +0000518 if not kernel_config.modules_needed('.config'):
519 return
mbligha87116f2006-10-10 02:47:08 +0000520
jadmanski0afbb632008-06-06 21:10:57 +0000521 utils.system('make modules_install INSTALL_MOD_PATH=%s' % prefix)
522 if prefix == '/':
523 self.initrd = self.boot_dir + '/initrd-' + tag
524 self.mkinitrd(self.get_kernel_build_ver(), self.image,
525 self.system_map, self.initrd)
mbligha87116f2006-10-10 02:47:08 +0000526
mbligha87116f2006-10-10 02:47:08 +0000527
jadmanski0afbb632008-06-06 21:10:57 +0000528 def get_kernel_build_arch(self, arch=None):
529 """
530 Work out the current kernel architecture (as a kernel arch)
531 """
532 if not arch:
mbligh53da18e2009-01-05 21:13:26 +0000533 arch = utils.get_current_kernel_arch()
jadmanski0afbb632008-06-06 21:10:57 +0000534 if re.match('i.86', arch):
535 return 'i386'
536 elif re.match('sun4u', arch):
537 return 'sparc64'
538 elif re.match('arm.*', arch):
539 return 'arm'
540 elif re.match('sa110', arch):
541 return 'arm'
542 elif re.match('s390x', arch):
543 return 's390'
544 elif re.match('parisc64', arch):
545 return 'parisc'
546 elif re.match('ppc.*', arch):
547 return 'powerpc'
548 elif re.match('mips.*', arch):
549 return 'mips'
550 else:
551 return arch
mbligh6a1d4db2006-10-06 04:30:16 +0000552
mbligh201aa892006-10-29 04:02:05 +0000553
jadmanski0afbb632008-06-06 21:10:57 +0000554 def get_kernel_build_release(self):
555 releasem = re.compile(r'.*UTS_RELEASE\s+"([^"]+)".*');
556 versionm = re.compile(r'.*UTS_VERSION\s+"([^"]+)".*');
mbligh548f29a2006-10-17 04:55:12 +0000557
jadmanski0afbb632008-06-06 21:10:57 +0000558 release = None
559 version = None
mbligh6a1d4db2006-10-06 04:30:16 +0000560
jadmanskid524b0e2008-09-15 14:28:20 +0000561 for f in [self.build_dir + "/include/linux/version.h",
562 self.build_dir + "/include/linux/utsrelease.h",
mbligh508cbf62009-11-06 03:01:50 +0000563 self.build_dir + "/include/linux/compile.h",
564 self.build_dir + "/include/generated/utsrelease.h",
565 self.build_dir + "/include/generated/compile.h"]:
jadmanskid524b0e2008-09-15 14:28:20 +0000566 if os.path.exists(f):
567 fd = open(f, 'r')
jadmanski0afbb632008-06-06 21:10:57 +0000568 for line in fd.readlines():
569 m = releasem.match(line)
570 if m:
571 release = m.groups()[0]
572 m = versionm.match(line)
573 if m:
574 version = m.groups()[0]
575 fd.close()
mbligh237bed32007-09-05 13:05:57 +0000576
jadmanski0afbb632008-06-06 21:10:57 +0000577 return (release, version)
mbligh237bed32007-09-05 13:05:57 +0000578
mbligh237bed32007-09-05 13:05:57 +0000579
jadmanski0afbb632008-06-06 21:10:57 +0000580 def get_kernel_build_ident(self):
581 (release, version) = self.get_kernel_build_release()
mbligh237bed32007-09-05 13:05:57 +0000582
jadmanski0afbb632008-06-06 21:10:57 +0000583 if not release or not version:
584 raise error.JobError('kernel has no identity')
mbligh237bed32007-09-05 13:05:57 +0000585
jadmanski0afbb632008-06-06 21:10:57 +0000586 return release + '::' + version
mbligh237bed32007-09-05 13:05:57 +0000587
mbligh237bed32007-09-05 13:05:57 +0000588
jadmanski067b26c2008-09-25 19:46:56 +0000589 def boot(self, args='', ident=True):
jadmanski0afbb632008-06-06 21:10:57 +0000590 """ install and boot this kernel, do not care how
591 just make it happen.
592 """
mbligh237bed32007-09-05 13:05:57 +0000593
Eric Li6f27d4f2010-09-29 10:55:17 -0700594 # If the kernel has not yet been installed,
595 # install it now as default tag.
jadmanski0afbb632008-06-06 21:10:57 +0000596 if not self.installed_as:
597 self.install()
mbligh237bed32007-09-05 13:05:57 +0000598
Eric Li6f27d4f2010-09-29 10:55:17 -0700599 expected_ident = self.get_kernel_build_ident()
600 self._boot_kernel(args, ident, expected_ident,
601 self.subdir, self.applied_patches)
apw1b5dc362006-10-31 11:24:26 +0000602
apw1b5dc362006-10-31 11:24:26 +0000603
jadmanski0afbb632008-06-06 21:10:57 +0000604 def get_kernel_build_ver(self):
605 """Check Makefile and .config to return kernel version"""
606 version = patchlevel = sublevel = extraversion = localversion = ''
apw1b5dc362006-10-31 11:24:26 +0000607
jadmanski0afbb632008-06-06 21:10:57 +0000608 for line in open(self.build_dir + '/Makefile', 'r').readlines():
609 if line.startswith('VERSION'):
610 version = line[line.index('=') + 1:].strip()
611 if line.startswith('PATCHLEVEL'):
612 patchlevel = line[line.index('=') + 1:].strip()
613 if line.startswith('SUBLEVEL'):
614 sublevel = line[line.index('=') + 1:].strip()
615 if line.startswith('EXTRAVERSION'):
616 extraversion = line[line.index('=') + 1:].strip()
mblighe11f5fc2006-10-04 04:42:22 +0000617
jadmanski0afbb632008-06-06 21:10:57 +0000618 for line in open(self.build_dir + '/.config', 'r').readlines():
619 if line.startswith('CONFIG_LOCALVERSION='):
620 localversion = line.rstrip().split('"')[1]
mblighe11f5fc2006-10-04 04:42:22 +0000621
jadmanski0afbb632008-06-06 21:10:57 +0000622 return "%s.%s.%s%s%s" %(version, patchlevel, sublevel, extraversion, localversion)
mblighe11f5fc2006-10-04 04:42:22 +0000623
mblighfdbcaec2006-10-01 23:28:57 +0000624
jadmanski0afbb632008-06-06 21:10:57 +0000625 def set_build_target(self, build_target):
626 if build_target:
627 self.build_target = build_target
628 print 'BUILD TARGET: %s' % self.build_target
mblighfdbcaec2006-10-01 23:28:57 +0000629
mbligh8baa2ea2006-12-17 23:01:24 +0000630
jadmanski0afbb632008-06-06 21:10:57 +0000631 def set_cross_cc(self, target_arch=None, cross_compile=None,
632 build_target='bzImage'):
633 """Set up to cross-compile.
634 This is broken. We need to work out what the default
635 compile produces, and if not, THEN set the cross
636 compiler.
637 """
mbligh8baa2ea2006-12-17 23:01:24 +0000638
jadmanski0afbb632008-06-06 21:10:57 +0000639 if self.target_arch:
640 return
mblighcc2e6662006-09-14 01:24:07 +0000641
jadmanski0afbb632008-06-06 21:10:57 +0000642 # if someone has set build_target, don't clobber in set_cross_cc
643 # run set_build_target before calling set_cross_cc
644 if not self.build_target:
645 self.set_build_target(build_target)
mbligh678823f2006-12-07 18:49:00 +0000646
jadmanski0afbb632008-06-06 21:10:57 +0000647 # If no 'target_arch' given assume native compilation
mblighd876f452008-12-03 15:09:17 +0000648 if target_arch is None:
mbligh53da18e2009-01-05 21:13:26 +0000649 target_arch = utils.get_current_kernel_arch()
jadmanski0afbb632008-06-06 21:10:57 +0000650 if target_arch == 'ppc64':
651 if self.build_target == 'bzImage':
652 self.build_target = 'vmlinux'
mbligh72b88fc2006-12-16 18:41:35 +0000653
jadmanski0afbb632008-06-06 21:10:57 +0000654 if not cross_compile:
655 cross_compile = self.job.config_get('kernel.cross_cc')
mbligh678823f2006-12-07 18:49:00 +0000656
jadmanski0afbb632008-06-06 21:10:57 +0000657 if cross_compile:
658 os.environ['CROSS_COMPILE'] = cross_compile
659 else:
660 if os.environ.has_key('CROSS_COMPILE'):
661 del os.environ['CROSS_COMPILE']
mbligh678823f2006-12-07 18:49:00 +0000662
jadmanski0afbb632008-06-06 21:10:57 +0000663 return # HACK. Crap out for now.
mblighf4c35322006-03-13 01:01:10 +0000664
jadmanski0afbb632008-06-06 21:10:57 +0000665 # At this point I know what arch I *want* to build for
666 # but have no way of working out what arch the default
667 # compiler DOES build for.
mblighcc2e6662006-09-14 01:24:07 +0000668
mbligh925e1b12008-06-12 17:48:38 +0000669 def install_package(package):
670 raise NotImplementedError("I don't exist yet!")
mbligh72b88fc2006-12-16 18:41:35 +0000671
jadmanski0afbb632008-06-06 21:10:57 +0000672 if target_arch == 'ppc64':
673 install_package('ppc64-cross')
674 cross_compile = os.path.join(self.autodir, 'sources/ppc64-cross/bin')
mblighcc2e6662006-09-14 01:24:07 +0000675
jadmanski0afbb632008-06-06 21:10:57 +0000676 elif target_arch == 'x86_64':
677 install_package('x86_64-cross')
678 cross_compile = os.path.join(self.autodir, 'sources/x86_64-cross/bin')
mblighb8a14e32006-05-06 00:17:35 +0000679
jadmanski0afbb632008-06-06 21:10:57 +0000680 os.environ['ARCH'] = self.target_arch = target_arch
mbligh5970cf02006-08-06 15:39:22 +0000681
jadmanski0afbb632008-06-06 21:10:57 +0000682 self.cross_compile = cross_compile
683 if self.cross_compile:
684 os.environ['CROSS_COMPILE'] = self.cross_compile
mblighcc2e6662006-09-14 01:24:07 +0000685
mbligh72b88fc2006-12-16 18:41:35 +0000686
jadmanski0afbb632008-06-06 21:10:57 +0000687 def pickle_dump(self, filename):
688 """dump a pickle of ourself out to the specified filename
mblighc86b0b42006-07-28 17:35:28 +0000689
jadmanski0afbb632008-06-06 21:10:57 +0000690 we can't pickle the backreference to job (it contains fd's),
691 nor would we want to. Same for logfile (fd's).
692 """
693 temp = copy.copy(self)
694 temp.job = None
695 temp.logfile = None
696 pickle.dump(temp, open(filename, 'w'))
mbligh736adc92007-10-18 03:23:22 +0000697
698
Eric Li6f27d4f2010-09-29 10:55:17 -0700699class rpm_kernel(BootableKernel):
mbligheaa75e52009-11-06 03:08:08 +0000700 """
701 Class for installing a binary rpm kernel package
jadmanski0afbb632008-06-06 21:10:57 +0000702 """
mbligh736adc92007-10-18 03:23:22 +0000703
jadmanski0afbb632008-06-06 21:10:57 +0000704 def __init__(self, job, rpm_package, subdir):
Eric Li6f27d4f2010-09-29 10:55:17 -0700705 super(rpm_kernel, self).__init__(job)
jadmanski0afbb632008-06-06 21:10:57 +0000706 self.rpm_package = rpm_package
707 self.log_dir = os.path.join(subdir, 'debug')
708 self.subdir = os.path.basename(subdir)
709 if os.path.exists(self.log_dir):
710 utils.system('rm -rf ' + self.log_dir)
711 os.mkdir(self.log_dir)
mbligh736adc92007-10-18 03:23:22 +0000712
713
mbligheaa75e52009-11-06 03:08:08 +0000714 def build(self, *args, **dargs):
715 """
716 Dummy function, binary kernel so nothing to build.
717 """
718 pass
719
720
mbligh1b3b3762008-09-25 02:46:34 +0000721 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000722 @tee_output_logdir_mark
mbligha25e8c32009-06-15 21:27:23 +0000723 def install(self, tag='autotest', install_vmlinux=True):
jadmanski0afbb632008-06-06 21:10:57 +0000724 self.installed_as = tag
mblighda0311e2007-10-25 16:03:33 +0000725
mbligh1b160a02009-05-21 01:27:10 +0000726 self.image = None
jadmanski0afbb632008-06-06 21:10:57 +0000727 self.initrd = ''
mbligh1b160a02009-05-21 01:27:10 +0000728 for rpm_pack in self.rpm_package:
729 rpm_name = utils.system_output('rpm -qp ' + rpm_pack)
mbligh736adc92007-10-18 03:23:22 +0000730
mbligh1b160a02009-05-21 01:27:10 +0000731 # install
732 utils.system('rpm -i --force ' + rpm_pack)
733
734 # get file list
735 files = utils.system_output('rpm -ql ' + rpm_name).splitlines()
736
737 # search for vmlinuz
738 for file in files:
739 if file.startswith('/boot/vmlinuz'):
740 self.full_version = file[len('/boot/vmlinuz-'):]
741 self.image = file
742 self.rpm_flavour = rpm_name.split('-')[1]
743
744 # get version and release number
745 self.version, self.release = utils.system_output(
746 'rpm --queryformat="%{VERSION}\\n%{RELEASE}\\n" -q '
747 + rpm_name).splitlines()[0:2]
748
749 # prefer /boot/kernel-version before /boot/kernel
750 if self.full_version:
751 break
752
753 # search for initrd
754 for file in files:
Eric Lie0493a42010-11-15 13:05:43 -0800755 if file.startswith('/boot/init'):
mbligh1b160a02009-05-21 01:27:10 +0000756 self.initrd = file
757 # prefer /boot/initrd-version before /boot/initrd
758 if len(file) > len('/boot/initrd'):
759 break
760
761 if self.image == None:
762 errmsg = "specified rpm file(s) don't contain /boot/vmlinuz"
763 raise error.TestError(errmsg)
mbligh736adc92007-10-18 03:23:22 +0000764
mbligha25e8c32009-06-15 21:27:23 +0000765 # install vmlinux
766 if install_vmlinux:
767 for rpm_pack in self.rpm_package:
768 vmlinux = utils.system_output(
769 'rpm -q -l -p %s | grep /boot/vmlinux' % rpm_pack)
jadmanski19426ea2009-07-28 20:19:40 +0000770 utils.system('cd /; rpm2cpio %s | cpio -imuv .%s 2>&1'
mbligha25e8c32009-06-15 21:27:23 +0000771 % (rpm_pack, vmlinux))
772 if not os.path.exists(vmlinux):
773 raise error.TestError('%s does not exist after installing %s'
774 % (vmlinux, rpm_pack))
775
mbligh736adc92007-10-18 03:23:22 +0000776
jadmanski067b26c2008-09-25 19:46:56 +0000777 def boot(self, args='', ident=True):
jadmanski0afbb632008-06-06 21:10:57 +0000778 """ install and boot this kernel
779 """
mbligh73e82a32007-11-08 21:35:29 +0000780
Eric Li6f27d4f2010-09-29 10:55:17 -0700781 # If the kernel has not yet been installed,
782 # install it now as default tag.
jadmanski0afbb632008-06-06 21:10:57 +0000783 if not self.installed_as:
784 self.install()
mblighda0311e2007-10-25 16:03:33 +0000785
mblighb1887c82009-03-12 00:25:48 +0000786 expected_ident = self.full_version
787 if not expected_ident:
788 expected_ident = '-'.join([self.version,
mbligh1b160a02009-05-21 01:27:10 +0000789 self.rpm_flavour,
mblighb1887c82009-03-12 00:25:48 +0000790 self.release])
mbligh10a24a72007-10-24 21:02:53 +0000791
Eric Li6f27d4f2010-09-29 10:55:17 -0700792 self._boot_kernel(args, ident, expected_ident,
793 None, 'rpm')
mbligh6ee7ee02007-11-13 23:49:05 +0000794
795
mblighe7785cc2009-03-17 17:32:47 +0000796class rpm_kernel_suse(rpm_kernel):
797 """ Class for installing openSUSE/SLE rpm kernel package
798 """
799
800 def install(self):
801 # do not set the new kernel as the default one
802 os.environ['PBL_AUTOTEST'] = '1'
803
804 rpm_kernel.install(self, 'dummy')
805 self.installed_as = self.job.bootloader.get_title_for_kernel(self.image)
806 if not self.installed_as:
807 errmsg = "cannot find installed kernel in bootloader configuration"
808 raise error.TestError(errmsg)
809
810
811 def add_to_bootloader(self, tag='dummy', args=''):
812 """ Set parameters of this kernel in bootloader
813 """
814
815 # pull the base argument set from the job config
816 baseargs = self.job.config_get('boot.default_args')
817 if baseargs:
818 args = baseargs + ' ' + args
819
820 self.job.bootloader.add_args(tag, args)
821
822
823def rpm_kernel_vendor(job, rpm_package, subdir):
mbligh1ef218d2009-08-03 16:57:56 +0000824 vendor = utils.get_os_vendor()
825 if vendor == "SUSE":
826 return rpm_kernel_suse(job, rpm_package, subdir)
827 else:
828 return rpm_kernel(job, rpm_package, subdir)
mblighe7785cc2009-03-17 17:32:47 +0000829
830
mbligh062ed152009-01-13 00:57:14 +0000831# just make the preprocessor a nop
832def _preprocess_path_dummy(path):
833 return path.strip()
834
835
mbligh6ee7ee02007-11-13 23:49:05 +0000836# pull in some optional site-specific path pre-processing
jadmanski19426ea2009-07-28 20:19:40 +0000837preprocess_path = utils.import_site_function(__file__,
mbligh062ed152009-01-13 00:57:14 +0000838 "autotest_lib.client.bin.site_kernel", "preprocess_path",
839 _preprocess_path_dummy)
mbligh6ee7ee02007-11-13 23:49:05 +0000840
mblighc5ddfd12008-08-04 17:15:00 +0000841
mbligh6ee7ee02007-11-13 23:49:05 +0000842def auto_kernel(job, path, subdir, tmp_dir, build_dir, leave=False):
mbligh7aeda672009-01-30 00:35:59 +0000843 """
jadmanski0afbb632008-06-06 21:10:57 +0000844 Create a kernel object, dynamically selecting the appropriate class to use
845 based on the path provided.
846 """
mbligh1b160a02009-05-21 01:27:10 +0000847 kernel_paths = [preprocess_path(path)]
848 if kernel_paths[0].endswith('.list'):
mbligh1ef218d2009-08-03 16:57:56 +0000849 # Fetch the list of packages to install
mbligh1b160a02009-05-21 01:27:10 +0000850 kernel_list = os.path.join(tmp_dir, 'kernel.list')
851 utils.get_file(kernel_paths[0], kernel_list)
852 kernel_paths = [p.strip() for p in open(kernel_list).readlines()]
mblighc5ddfd12008-08-04 17:15:00 +0000853
mbligh1b160a02009-05-21 01:27:10 +0000854 if kernel_paths[0].endswith('.rpm'):
855 rpm_paths = []
856 for kernel_path in kernel_paths:
jadmanski19426ea2009-07-28 20:19:40 +0000857 if os.path.exists(kernel_path):
mbligh1b160a02009-05-21 01:27:10 +0000858 rpm_paths.append(kernel_path)
mbligh1b160a02009-05-21 01:27:10 +0000859 else:
860 # Fetch the rpm into the job's packages directory and pass it to
861 # rpm_kernel
862 rpm_name = os.path.basename(kernel_path)
mbligh7aeda672009-01-30 00:35:59 +0000863
mbligh1b160a02009-05-21 01:27:10 +0000864 # If the preprocessed path (kernel_path) is only a name then
865 # search for the kernel in all the repositories, else fetch the
866 # kernel from that specific path.
867 job.pkgmgr.fetch_pkg(rpm_name, os.path.join(job.pkgdir, rpm_name),
868 repo_url=os.path.dirname(kernel_path))
869
870 rpm_paths.append(os.path.join(job.pkgdir, rpm_name))
871 return rpm_kernel_vendor(job, rpm_paths, subdir)
jadmanski0afbb632008-06-06 21:10:57 +0000872 else:
mbligh1b160a02009-05-21 01:27:10 +0000873 if len(kernel_paths) > 1:
874 raise error.TestError("don't know what to do with more than one non-rpm kernel file")
875 return kernel(job,kernel_paths[0], subdir, tmp_dir, build_dir, leave)