blob: cb0ef9992dee83f93eaa01c19e3cd398cf9a0b3c [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
Eric Li7edb3042011-01-06 17:57:17 -0800335 def extraversion(self, tag, append=True):
jadmanski0afbb632008-06-06 21:10:57 +0000336 os.chdir(self.build_dir)
Eric Li7edb3042011-01-06 17:57:17 -0800337 extraversion_sub = r's/^CONFIG_LOCALVERSION=\s*"\(.*\)"/CONFIG_LOCALVERSION='
338 cfg = self.build_dir + '/.config'
jadmanski0afbb632008-06-06 21:10:57 +0000339 if append:
Eric Li7edb3042011-01-06 17:57:17 -0800340 p = extraversion_sub + '"\\1-%s"/' % tag
jadmanski0afbb632008-06-06 21:10:57 +0000341 else:
Eric Li7edb3042011-01-06 17:57:17 -0800342 p = extraversion_sub + '"-%s"/' % tag
343 utils.system('mv %s %s.old' % (cfg, cfg))
344 utils.system("sed '%s' < %s.old > %s" % (p, cfg, cfg))
345 self.config(make='oldconfig')
mbligh72b88fc2006-12-16 18:41:35 +0000346
apwc7846102006-04-06 18:22:13 +0000347
mbligh1b3b3762008-09-25 02:46:34 +0000348 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000349 @tee_output_logdir_mark
350 def build(self, make_opts = '', logfile = '', extraversion='autotest'):
351 """build the kernel
apwc7846102006-04-06 18:22:13 +0000352
jadmanski0afbb632008-06-06 21:10:57 +0000353 make_opts
354 additional options to make, if any
355 """
356 os_dep.commands('gcc', 'make')
357 if logfile == '':
358 logfile = os.path.join(self.log_dir, 'kernel_build')
359 os.chdir(self.build_dir)
360 if extraversion:
361 self.extraversion(extraversion)
362 self.set_cross_cc()
363 # setup_config_file(config_file, config_overrides)
mbligh1e8858e2006-11-24 22:18:35 +0000364
jadmanski0afbb632008-06-06 21:10:57 +0000365 # Not needed on 2.6, but hard to tell -- handle failure
366 utils.system('make dep', ignore_status=True)
mbligh53da18e2009-01-05 21:13:26 +0000367 threads = 2 * utils.count_cpus()
jadmanski0afbb632008-06-06 21:10:57 +0000368 build_string = 'make -j %d %s %s' % (threads, make_opts,
369 self.build_target)
370 # eg make bzImage, or make zImage
371 print build_string
mbligh925e1b12008-06-12 17:48:38 +0000372 utils.system(build_string)
jadmanski0afbb632008-06-06 21:10:57 +0000373 if kernel_config.modules_needed('.config'):
374 utils.system('make -j %d modules' % (threads))
mblighf4c35322006-03-13 01:01:10 +0000375
jadmanski0afbb632008-06-06 21:10:57 +0000376 kernel_version = self.get_kernel_build_ver()
377 kernel_version = re.sub('-autotest', '', kernel_version)
378 self.logfile.write('BUILD VERSION: %s\n' % kernel_version)
mblighf4c35322006-03-13 01:01:10 +0000379
mbligh53da18e2009-01-05 21:13:26 +0000380 utils.force_copy(self.build_dir+'/System.map',
mbligh925e1b12008-06-12 17:48:38 +0000381 self.results_dir)
mbligh30f28c52007-10-11 18:35:35 +0000382
mbligh30f28c52007-10-11 18:35:35 +0000383
jadmanski0afbb632008-06-06 21:10:57 +0000384 def build_timed(self, threads, timefile = '/dev/null', make_opts = '',
385 output = '/dev/null'):
386 """time the bulding of the kernel"""
387 os.chdir(self.build_dir)
388 self.set_cross_cc()
389
Eric Lie0493a42010-11-15 13:05:43 -0800390 self.clean()
jadmanski0afbb632008-06-06 21:10:57 +0000391 build_string = "/usr/bin/time -o %s make %s -j %s vmlinux" \
392 % (timefile, make_opts, threads)
393 build_string += ' > %s 2>&1' % output
394 print build_string
395 utils.system(build_string)
396
397 if (not os.path.isfile('vmlinux')):
398 errmsg = "no vmlinux found, kernel build failed"
399 raise error.TestError(errmsg)
400
401
mbligh1b3b3762008-09-25 02:46:34 +0000402 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000403 @tee_output_logdir_mark
404 def clean(self):
405 """make clean in the kernel tree"""
406 os.chdir(self.build_dir)
407 print "make clean"
408 utils.system('make clean > /dev/null 2> /dev/null')
409
410
mbligh1b3b3762008-09-25 02:46:34 +0000411 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000412 @tee_output_logdir_mark
413 def mkinitrd(self, version, image, system_map, initrd):
414 """Build kernel initrd image.
415 Try to use distro specific way to build initrd image.
416 Parameters:
417 version
418 new kernel version
419 image
420 new kernel image file
421 system_map
422 System.map file
423 initrd
424 initrd image file to build
425 """
mbligh53da18e2009-01-05 21:13:26 +0000426 vendor = utils.get_os_vendor()
mblighb8a14e32006-05-06 00:17:35 +0000427
jadmanski0afbb632008-06-06 21:10:57 +0000428 if os.path.isfile(initrd):
429 print "Existing %s file, will remove it." % initrd
430 os.remove(initrd)
mblighb8a14e32006-05-06 00:17:35 +0000431
jadmanski0afbb632008-06-06 21:10:57 +0000432 args = self.job.config_get('kernel.mkinitrd_extra_args')
mblighf4c35322006-03-13 01:01:10 +0000433
jadmanski0afbb632008-06-06 21:10:57 +0000434 # don't leak 'None' into mkinitrd command
435 if not args:
436 args = ''
mbligh50f42ea2006-09-30 22:22:21 +0000437
Eric Lie0493a42010-11-15 13:05:43 -0800438 # It is important to match the version with a real directory inside
439 # /lib/modules
440 real_version_list = glob.glob('/lib/modules/%s*' % version)
441 rl = len(real_version_list)
442 if rl == 0:
443 logging.error("No directory %s found under /lib/modules. Initramfs"
444 "creation will most likely fail and your new kernel"
445 "will fail to build", version)
446 else:
447 if rl > 1:
448 logging.warning("Found more than one possible match for "
449 "kernel version %s under /lib/modules", version)
450 version = os.path.basename(real_version_list[0])
451
jadmanski0afbb632008-06-06 21:10:57 +0000452 if vendor in ['Red Hat', 'Fedora Core']:
Eric Lie0493a42010-11-15 13:05:43 -0800453 try:
454 cmd = os_dep.command('dracut')
455 full_cmd = '%s -f %s %s' % (cmd, initrd, version)
456 except ValueError:
457 cmd = os_dep.command('mkinitrd')
458 full_cmd = '%s %s %s %s' % (cmd, args, initrd, version)
459 utils.system(full_cmd)
jadmanski0afbb632008-06-06 21:10:57 +0000460 elif vendor in ['SUSE']:
jadmanskid524b0e2008-09-15 14:28:20 +0000461 utils.system('mkinitrd %s -k %s -i %s -M %s' %
462 (args, image, initrd, system_map))
jadmanski0afbb632008-06-06 21:10:57 +0000463 elif vendor in ['Debian', 'Ubuntu']:
464 if os.path.isfile('/usr/sbin/mkinitrd'):
465 cmd = '/usr/sbin/mkinitrd'
466 elif os.path.isfile('/usr/sbin/mkinitramfs'):
467 cmd = '/usr/sbin/mkinitramfs'
468 else:
469 raise error.TestError('No Debian initrd builder')
470 utils.system('%s %s -o %s %s' % (cmd, args, initrd, version))
471 else:
472 raise error.TestError('Unsupported vendor %s' % vendor)
mbligh72b88fc2006-12-16 18:41:35 +0000473
apwe43a30b2007-09-25 16:51:30 +0000474
jadmanski0afbb632008-06-06 21:10:57 +0000475 def set_build_image(self, image):
476 self.build_image = image
mbligh3d515d42007-11-09 17:00:36 +0000477
mbligh50f42ea2006-09-30 22:22:21 +0000478
mbligh1b3b3762008-09-25 02:46:34 +0000479 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000480 @tee_output_logdir_mark
481 def install(self, tag='autotest', prefix = '/'):
482 """make install in the kernel tree"""
mbligh50f42ea2006-09-30 22:22:21 +0000483
jadmanski0afbb632008-06-06 21:10:57 +0000484 # Record that we have installed the kernel, and
485 # the tag under which we installed it.
486 self.installed_as = tag
mbligh8baa2ea2006-12-17 23:01:24 +0000487
jadmanski0afbb632008-06-06 21:10:57 +0000488 os.chdir(self.build_dir)
mbligh8baa2ea2006-12-17 23:01:24 +0000489
jadmanski0afbb632008-06-06 21:10:57 +0000490 if not os.path.isdir(prefix):
491 os.mkdir(prefix)
492 self.boot_dir = os.path.join(prefix, 'boot')
493 if not os.path.isdir(self.boot_dir):
494 os.mkdir(self.boot_dir)
apw87c65c12007-09-27 17:19:37 +0000495
jadmanski0afbb632008-06-06 21:10:57 +0000496 if not self.build_image:
497 images = glob.glob('arch/*/boot/' + self.build_target)
498 if len(images):
499 self.build_image = images[0]
500 else:
501 self.build_image = self.build_target
apw87c65c12007-09-27 17:19:37 +0000502
jadmanski0afbb632008-06-06 21:10:57 +0000503 # remember installed files
504 self.vmlinux = self.boot_dir + '/vmlinux-' + tag
505 if (self.build_image != 'vmlinux'):
506 self.image = self.boot_dir + '/vmlinuz-' + tag
507 else:
508 self.image = self.vmlinux
509 self.system_map = self.boot_dir + '/System.map-' + tag
mbligh925e1b12008-06-12 17:48:38 +0000510 self.config_file = self.boot_dir + '/config-' + tag
jadmanski0afbb632008-06-06 21:10:57 +0000511 self.initrd = ''
mbligh72b88fc2006-12-16 18:41:35 +0000512
jadmanski0afbb632008-06-06 21:10:57 +0000513 # copy to boot dir
mbligh53da18e2009-01-05 21:13:26 +0000514 utils.force_copy('vmlinux', self.vmlinux)
jadmanski0afbb632008-06-06 21:10:57 +0000515 if (self.build_image != 'vmlinux'):
mbligh53da18e2009-01-05 21:13:26 +0000516 utils.force_copy(self.build_image, self.image)
517 utils.force_copy('System.map', self.system_map)
518 utils.force_copy('.config', self.config_file)
mbligh0ad65582006-10-06 04:16:36 +0000519
jadmanski0afbb632008-06-06 21:10:57 +0000520 if not kernel_config.modules_needed('.config'):
521 return
mbligha87116f2006-10-10 02:47:08 +0000522
jadmanski0afbb632008-06-06 21:10:57 +0000523 utils.system('make modules_install INSTALL_MOD_PATH=%s' % prefix)
524 if prefix == '/':
525 self.initrd = self.boot_dir + '/initrd-' + tag
526 self.mkinitrd(self.get_kernel_build_ver(), self.image,
527 self.system_map, self.initrd)
mbligha87116f2006-10-10 02:47:08 +0000528
mbligha87116f2006-10-10 02:47:08 +0000529
jadmanski0afbb632008-06-06 21:10:57 +0000530 def get_kernel_build_arch(self, arch=None):
531 """
532 Work out the current kernel architecture (as a kernel arch)
533 """
534 if not arch:
mbligh53da18e2009-01-05 21:13:26 +0000535 arch = utils.get_current_kernel_arch()
jadmanski0afbb632008-06-06 21:10:57 +0000536 if re.match('i.86', arch):
537 return 'i386'
538 elif re.match('sun4u', arch):
539 return 'sparc64'
540 elif re.match('arm.*', arch):
541 return 'arm'
542 elif re.match('sa110', arch):
543 return 'arm'
544 elif re.match('s390x', arch):
545 return 's390'
546 elif re.match('parisc64', arch):
547 return 'parisc'
548 elif re.match('ppc.*', arch):
549 return 'powerpc'
550 elif re.match('mips.*', arch):
551 return 'mips'
552 else:
553 return arch
mbligh6a1d4db2006-10-06 04:30:16 +0000554
mbligh201aa892006-10-29 04:02:05 +0000555
jadmanski0afbb632008-06-06 21:10:57 +0000556 def get_kernel_build_release(self):
557 releasem = re.compile(r'.*UTS_RELEASE\s+"([^"]+)".*');
558 versionm = re.compile(r'.*UTS_VERSION\s+"([^"]+)".*');
mbligh548f29a2006-10-17 04:55:12 +0000559
jadmanski0afbb632008-06-06 21:10:57 +0000560 release = None
561 version = None
mbligh6a1d4db2006-10-06 04:30:16 +0000562
jadmanskid524b0e2008-09-15 14:28:20 +0000563 for f in [self.build_dir + "/include/linux/version.h",
564 self.build_dir + "/include/linux/utsrelease.h",
mbligh508cbf62009-11-06 03:01:50 +0000565 self.build_dir + "/include/linux/compile.h",
566 self.build_dir + "/include/generated/utsrelease.h",
567 self.build_dir + "/include/generated/compile.h"]:
jadmanskid524b0e2008-09-15 14:28:20 +0000568 if os.path.exists(f):
569 fd = open(f, 'r')
jadmanski0afbb632008-06-06 21:10:57 +0000570 for line in fd.readlines():
571 m = releasem.match(line)
572 if m:
573 release = m.groups()[0]
574 m = versionm.match(line)
575 if m:
576 version = m.groups()[0]
577 fd.close()
mbligh237bed32007-09-05 13:05:57 +0000578
jadmanski0afbb632008-06-06 21:10:57 +0000579 return (release, version)
mbligh237bed32007-09-05 13:05:57 +0000580
mbligh237bed32007-09-05 13:05:57 +0000581
jadmanski0afbb632008-06-06 21:10:57 +0000582 def get_kernel_build_ident(self):
583 (release, version) = self.get_kernel_build_release()
mbligh237bed32007-09-05 13:05:57 +0000584
jadmanski0afbb632008-06-06 21:10:57 +0000585 if not release or not version:
586 raise error.JobError('kernel has no identity')
mbligh237bed32007-09-05 13:05:57 +0000587
jadmanski0afbb632008-06-06 21:10:57 +0000588 return release + '::' + version
mbligh237bed32007-09-05 13:05:57 +0000589
mbligh237bed32007-09-05 13:05:57 +0000590
jadmanski067b26c2008-09-25 19:46:56 +0000591 def boot(self, args='', ident=True):
jadmanski0afbb632008-06-06 21:10:57 +0000592 """ install and boot this kernel, do not care how
593 just make it happen.
594 """
mbligh237bed32007-09-05 13:05:57 +0000595
Eric Li6f27d4f2010-09-29 10:55:17 -0700596 # If the kernel has not yet been installed,
597 # install it now as default tag.
jadmanski0afbb632008-06-06 21:10:57 +0000598 if not self.installed_as:
599 self.install()
mbligh237bed32007-09-05 13:05:57 +0000600
Eric Li6f27d4f2010-09-29 10:55:17 -0700601 expected_ident = self.get_kernel_build_ident()
602 self._boot_kernel(args, ident, expected_ident,
603 self.subdir, self.applied_patches)
apw1b5dc362006-10-31 11:24:26 +0000604
apw1b5dc362006-10-31 11:24:26 +0000605
jadmanski0afbb632008-06-06 21:10:57 +0000606 def get_kernel_build_ver(self):
607 """Check Makefile and .config to return kernel version"""
608 version = patchlevel = sublevel = extraversion = localversion = ''
apw1b5dc362006-10-31 11:24:26 +0000609
jadmanski0afbb632008-06-06 21:10:57 +0000610 for line in open(self.build_dir + '/Makefile', 'r').readlines():
611 if line.startswith('VERSION'):
612 version = line[line.index('=') + 1:].strip()
613 if line.startswith('PATCHLEVEL'):
614 patchlevel = line[line.index('=') + 1:].strip()
615 if line.startswith('SUBLEVEL'):
616 sublevel = line[line.index('=') + 1:].strip()
617 if line.startswith('EXTRAVERSION'):
618 extraversion = line[line.index('=') + 1:].strip()
mblighe11f5fc2006-10-04 04:42:22 +0000619
jadmanski0afbb632008-06-06 21:10:57 +0000620 for line in open(self.build_dir + '/.config', 'r').readlines():
621 if line.startswith('CONFIG_LOCALVERSION='):
622 localversion = line.rstrip().split('"')[1]
mblighe11f5fc2006-10-04 04:42:22 +0000623
jadmanski0afbb632008-06-06 21:10:57 +0000624 return "%s.%s.%s%s%s" %(version, patchlevel, sublevel, extraversion, localversion)
mblighe11f5fc2006-10-04 04:42:22 +0000625
mblighfdbcaec2006-10-01 23:28:57 +0000626
jadmanski0afbb632008-06-06 21:10:57 +0000627 def set_build_target(self, build_target):
628 if build_target:
629 self.build_target = build_target
630 print 'BUILD TARGET: %s' % self.build_target
mblighfdbcaec2006-10-01 23:28:57 +0000631
mbligh8baa2ea2006-12-17 23:01:24 +0000632
jadmanski0afbb632008-06-06 21:10:57 +0000633 def set_cross_cc(self, target_arch=None, cross_compile=None,
634 build_target='bzImage'):
635 """Set up to cross-compile.
636 This is broken. We need to work out what the default
637 compile produces, and if not, THEN set the cross
638 compiler.
639 """
mbligh8baa2ea2006-12-17 23:01:24 +0000640
jadmanski0afbb632008-06-06 21:10:57 +0000641 if self.target_arch:
642 return
mblighcc2e6662006-09-14 01:24:07 +0000643
jadmanski0afbb632008-06-06 21:10:57 +0000644 # if someone has set build_target, don't clobber in set_cross_cc
645 # run set_build_target before calling set_cross_cc
646 if not self.build_target:
647 self.set_build_target(build_target)
mbligh678823f2006-12-07 18:49:00 +0000648
jadmanski0afbb632008-06-06 21:10:57 +0000649 # If no 'target_arch' given assume native compilation
mblighd876f452008-12-03 15:09:17 +0000650 if target_arch is None:
mbligh53da18e2009-01-05 21:13:26 +0000651 target_arch = utils.get_current_kernel_arch()
jadmanski0afbb632008-06-06 21:10:57 +0000652 if target_arch == 'ppc64':
653 if self.build_target == 'bzImage':
654 self.build_target = 'vmlinux'
mbligh72b88fc2006-12-16 18:41:35 +0000655
jadmanski0afbb632008-06-06 21:10:57 +0000656 if not cross_compile:
657 cross_compile = self.job.config_get('kernel.cross_cc')
mbligh678823f2006-12-07 18:49:00 +0000658
jadmanski0afbb632008-06-06 21:10:57 +0000659 if cross_compile:
660 os.environ['CROSS_COMPILE'] = cross_compile
661 else:
662 if os.environ.has_key('CROSS_COMPILE'):
663 del os.environ['CROSS_COMPILE']
mbligh678823f2006-12-07 18:49:00 +0000664
jadmanski0afbb632008-06-06 21:10:57 +0000665 return # HACK. Crap out for now.
mblighf4c35322006-03-13 01:01:10 +0000666
jadmanski0afbb632008-06-06 21:10:57 +0000667 # At this point I know what arch I *want* to build for
668 # but have no way of working out what arch the default
669 # compiler DOES build for.
mblighcc2e6662006-09-14 01:24:07 +0000670
mbligh925e1b12008-06-12 17:48:38 +0000671 def install_package(package):
672 raise NotImplementedError("I don't exist yet!")
mbligh72b88fc2006-12-16 18:41:35 +0000673
jadmanski0afbb632008-06-06 21:10:57 +0000674 if target_arch == 'ppc64':
675 install_package('ppc64-cross')
676 cross_compile = os.path.join(self.autodir, 'sources/ppc64-cross/bin')
mblighcc2e6662006-09-14 01:24:07 +0000677
jadmanski0afbb632008-06-06 21:10:57 +0000678 elif target_arch == 'x86_64':
679 install_package('x86_64-cross')
680 cross_compile = os.path.join(self.autodir, 'sources/x86_64-cross/bin')
mblighb8a14e32006-05-06 00:17:35 +0000681
jadmanski0afbb632008-06-06 21:10:57 +0000682 os.environ['ARCH'] = self.target_arch = target_arch
mbligh5970cf02006-08-06 15:39:22 +0000683
jadmanski0afbb632008-06-06 21:10:57 +0000684 self.cross_compile = cross_compile
685 if self.cross_compile:
686 os.environ['CROSS_COMPILE'] = self.cross_compile
mblighcc2e6662006-09-14 01:24:07 +0000687
mbligh72b88fc2006-12-16 18:41:35 +0000688
jadmanski0afbb632008-06-06 21:10:57 +0000689 def pickle_dump(self, filename):
690 """dump a pickle of ourself out to the specified filename
mblighc86b0b42006-07-28 17:35:28 +0000691
jadmanski0afbb632008-06-06 21:10:57 +0000692 we can't pickle the backreference to job (it contains fd's),
693 nor would we want to. Same for logfile (fd's).
694 """
695 temp = copy.copy(self)
696 temp.job = None
697 temp.logfile = None
698 pickle.dump(temp, open(filename, 'w'))
mbligh736adc92007-10-18 03:23:22 +0000699
700
Eric Li6f27d4f2010-09-29 10:55:17 -0700701class rpm_kernel(BootableKernel):
mbligheaa75e52009-11-06 03:08:08 +0000702 """
703 Class for installing a binary rpm kernel package
jadmanski0afbb632008-06-06 21:10:57 +0000704 """
mbligh736adc92007-10-18 03:23:22 +0000705
jadmanski0afbb632008-06-06 21:10:57 +0000706 def __init__(self, job, rpm_package, subdir):
Eric Li6f27d4f2010-09-29 10:55:17 -0700707 super(rpm_kernel, self).__init__(job)
jadmanski0afbb632008-06-06 21:10:57 +0000708 self.rpm_package = rpm_package
709 self.log_dir = os.path.join(subdir, 'debug')
710 self.subdir = os.path.basename(subdir)
711 if os.path.exists(self.log_dir):
712 utils.system('rm -rf ' + self.log_dir)
713 os.mkdir(self.log_dir)
mbligh736adc92007-10-18 03:23:22 +0000714
715
mbligheaa75e52009-11-06 03:08:08 +0000716 def build(self, *args, **dargs):
717 """
718 Dummy function, binary kernel so nothing to build.
719 """
720 pass
721
722
mbligh1b3b3762008-09-25 02:46:34 +0000723 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000724 @tee_output_logdir_mark
mbligha25e8c32009-06-15 21:27:23 +0000725 def install(self, tag='autotest', install_vmlinux=True):
jadmanski0afbb632008-06-06 21:10:57 +0000726 self.installed_as = tag
mblighda0311e2007-10-25 16:03:33 +0000727
mbligh1b160a02009-05-21 01:27:10 +0000728 self.image = None
jadmanski0afbb632008-06-06 21:10:57 +0000729 self.initrd = ''
mbligh1b160a02009-05-21 01:27:10 +0000730 for rpm_pack in self.rpm_package:
731 rpm_name = utils.system_output('rpm -qp ' + rpm_pack)
mbligh736adc92007-10-18 03:23:22 +0000732
mbligh1b160a02009-05-21 01:27:10 +0000733 # install
734 utils.system('rpm -i --force ' + rpm_pack)
735
736 # get file list
737 files = utils.system_output('rpm -ql ' + rpm_name).splitlines()
738
739 # search for vmlinuz
740 for file in files:
741 if file.startswith('/boot/vmlinuz'):
742 self.full_version = file[len('/boot/vmlinuz-'):]
743 self.image = file
744 self.rpm_flavour = rpm_name.split('-')[1]
745
746 # get version and release number
747 self.version, self.release = utils.system_output(
748 'rpm --queryformat="%{VERSION}\\n%{RELEASE}\\n" -q '
749 + rpm_name).splitlines()[0:2]
750
751 # prefer /boot/kernel-version before /boot/kernel
752 if self.full_version:
753 break
754
755 # search for initrd
756 for file in files:
Eric Lie0493a42010-11-15 13:05:43 -0800757 if file.startswith('/boot/init'):
mbligh1b160a02009-05-21 01:27:10 +0000758 self.initrd = file
759 # prefer /boot/initrd-version before /boot/initrd
760 if len(file) > len('/boot/initrd'):
761 break
762
763 if self.image == None:
764 errmsg = "specified rpm file(s) don't contain /boot/vmlinuz"
765 raise error.TestError(errmsg)
mbligh736adc92007-10-18 03:23:22 +0000766
mbligha25e8c32009-06-15 21:27:23 +0000767 # install vmlinux
768 if install_vmlinux:
769 for rpm_pack in self.rpm_package:
770 vmlinux = utils.system_output(
771 'rpm -q -l -p %s | grep /boot/vmlinux' % rpm_pack)
jadmanski19426ea2009-07-28 20:19:40 +0000772 utils.system('cd /; rpm2cpio %s | cpio -imuv .%s 2>&1'
mbligha25e8c32009-06-15 21:27:23 +0000773 % (rpm_pack, vmlinux))
774 if not os.path.exists(vmlinux):
775 raise error.TestError('%s does not exist after installing %s'
776 % (vmlinux, rpm_pack))
777
mbligh736adc92007-10-18 03:23:22 +0000778
jadmanski067b26c2008-09-25 19:46:56 +0000779 def boot(self, args='', ident=True):
jadmanski0afbb632008-06-06 21:10:57 +0000780 """ install and boot this kernel
781 """
mbligh73e82a32007-11-08 21:35:29 +0000782
Eric Li6f27d4f2010-09-29 10:55:17 -0700783 # If the kernel has not yet been installed,
784 # install it now as default tag.
jadmanski0afbb632008-06-06 21:10:57 +0000785 if not self.installed_as:
786 self.install()
mblighda0311e2007-10-25 16:03:33 +0000787
mblighb1887c82009-03-12 00:25:48 +0000788 expected_ident = self.full_version
789 if not expected_ident:
790 expected_ident = '-'.join([self.version,
mbligh1b160a02009-05-21 01:27:10 +0000791 self.rpm_flavour,
mblighb1887c82009-03-12 00:25:48 +0000792 self.release])
mbligh10a24a72007-10-24 21:02:53 +0000793
Eric Li6f27d4f2010-09-29 10:55:17 -0700794 self._boot_kernel(args, ident, expected_ident,
795 None, 'rpm')
mbligh6ee7ee02007-11-13 23:49:05 +0000796
797
mblighe7785cc2009-03-17 17:32:47 +0000798class rpm_kernel_suse(rpm_kernel):
799 """ Class for installing openSUSE/SLE rpm kernel package
800 """
801
802 def install(self):
803 # do not set the new kernel as the default one
804 os.environ['PBL_AUTOTEST'] = '1'
805
806 rpm_kernel.install(self, 'dummy')
807 self.installed_as = self.job.bootloader.get_title_for_kernel(self.image)
808 if not self.installed_as:
809 errmsg = "cannot find installed kernel in bootloader configuration"
810 raise error.TestError(errmsg)
811
812
813 def add_to_bootloader(self, tag='dummy', args=''):
814 """ Set parameters of this kernel in bootloader
815 """
816
817 # pull the base argument set from the job config
818 baseargs = self.job.config_get('boot.default_args')
819 if baseargs:
820 args = baseargs + ' ' + args
821
822 self.job.bootloader.add_args(tag, args)
823
824
825def rpm_kernel_vendor(job, rpm_package, subdir):
mbligh1ef218d2009-08-03 16:57:56 +0000826 vendor = utils.get_os_vendor()
827 if vendor == "SUSE":
828 return rpm_kernel_suse(job, rpm_package, subdir)
829 else:
830 return rpm_kernel(job, rpm_package, subdir)
mblighe7785cc2009-03-17 17:32:47 +0000831
832
mbligh062ed152009-01-13 00:57:14 +0000833# just make the preprocessor a nop
834def _preprocess_path_dummy(path):
835 return path.strip()
836
837
mbligh6ee7ee02007-11-13 23:49:05 +0000838# pull in some optional site-specific path pre-processing
jadmanski19426ea2009-07-28 20:19:40 +0000839preprocess_path = utils.import_site_function(__file__,
mbligh062ed152009-01-13 00:57:14 +0000840 "autotest_lib.client.bin.site_kernel", "preprocess_path",
841 _preprocess_path_dummy)
mbligh6ee7ee02007-11-13 23:49:05 +0000842
mblighc5ddfd12008-08-04 17:15:00 +0000843
mbligh6ee7ee02007-11-13 23:49:05 +0000844def auto_kernel(job, path, subdir, tmp_dir, build_dir, leave=False):
mbligh7aeda672009-01-30 00:35:59 +0000845 """
jadmanski0afbb632008-06-06 21:10:57 +0000846 Create a kernel object, dynamically selecting the appropriate class to use
847 based on the path provided.
848 """
mbligh1b160a02009-05-21 01:27:10 +0000849 kernel_paths = [preprocess_path(path)]
850 if kernel_paths[0].endswith('.list'):
mbligh1ef218d2009-08-03 16:57:56 +0000851 # Fetch the list of packages to install
mbligh1b160a02009-05-21 01:27:10 +0000852 kernel_list = os.path.join(tmp_dir, 'kernel.list')
853 utils.get_file(kernel_paths[0], kernel_list)
854 kernel_paths = [p.strip() for p in open(kernel_list).readlines()]
mblighc5ddfd12008-08-04 17:15:00 +0000855
mbligh1b160a02009-05-21 01:27:10 +0000856 if kernel_paths[0].endswith('.rpm'):
857 rpm_paths = []
858 for kernel_path in kernel_paths:
jadmanski19426ea2009-07-28 20:19:40 +0000859 if os.path.exists(kernel_path):
mbligh1b160a02009-05-21 01:27:10 +0000860 rpm_paths.append(kernel_path)
mbligh1b160a02009-05-21 01:27:10 +0000861 else:
862 # Fetch the rpm into the job's packages directory and pass it to
863 # rpm_kernel
864 rpm_name = os.path.basename(kernel_path)
mbligh7aeda672009-01-30 00:35:59 +0000865
mbligh1b160a02009-05-21 01:27:10 +0000866 # If the preprocessed path (kernel_path) is only a name then
867 # search for the kernel in all the repositories, else fetch the
868 # kernel from that specific path.
869 job.pkgmgr.fetch_pkg(rpm_name, os.path.join(job.pkgdir, rpm_name),
870 repo_url=os.path.dirname(kernel_path))
871
872 rpm_paths.append(os.path.join(job.pkgdir, rpm_name))
873 return rpm_kernel_vendor(job, rpm_paths, subdir)
jadmanski0afbb632008-06-06 21:10:57 +0000874 else:
mbligh1b160a02009-05-21 01:27:10 +0000875 if len(kernel_paths) > 1:
876 raise error.TestError("don't know what to do with more than one non-rpm kernel file")
877 return kernel(job,kernel_paths[0], subdir, tmp_dir, build_dir, leave)