blob: b39869710c83b935416bc43cbb843c345d69df40 [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
jadmanski6ca37b62008-06-30 21:17:07 +000067class kernel(object):
jadmanski0afbb632008-06-06 21:10:57 +000068 """ Class for compiling kernels.
mblighc86b0b42006-07-28 17:35:28 +000069
jadmanski0afbb632008-06-06 21:10:57 +000070 Data for the object includes the src files
71 used to create the kernel, patches applied, config (base + changes),
72 the build directory itself, and logged output
mblighc86b0b42006-07-28 17:35:28 +000073
jadmanski0afbb632008-06-06 21:10:57 +000074 Properties:
75 job
76 Backpointer to the job object we're part of
77 autodir
78 Path to the top level autotest dir (/usr/local/autotest)
79 src_dir
80 <tmp_dir>/src/
81 build_dir
82 <tmp_dir>/linux/
83 config_dir
84 <results_dir>/config/
85 log_dir
86 <results_dir>/debug/
87 results_dir
88 <results_dir>/results/
89 """
mblighc86b0b42006-07-28 17:35:28 +000090
jadmanski0afbb632008-06-06 21:10:57 +000091 autodir = ''
mbligh8baa2ea2006-12-17 23:01:24 +000092
mbligh925e1b12008-06-12 17:48:38 +000093 def __init__(self, job, base_tree, subdir, tmp_dir, build_dir, leave=False):
jadmanski0afbb632008-06-06 21:10:57 +000094 """Initialize the kernel build environment
mblighc86b0b42006-07-28 17:35:28 +000095
jadmanski0afbb632008-06-06 21:10:57 +000096 job
97 which job this build is part of
98 base_tree
99 base kernel tree. Can be one of the following:
100 1. A local tarball
101 2. A URL to a tarball
102 3. A local directory (will symlink it)
103 4. A shorthand expandable (eg '2.6.11-git3')
104 subdir
105 subdir in the results directory (eg "build")
106 (holds config/, debug/, results/)
107 tmp_dir
mbligh72b88fc2006-12-16 18:41:35 +0000108
jadmanski0afbb632008-06-06 21:10:57 +0000109 leave
110 Boolean, whether to leave existing tmpdir or not
111 """
112 self.job = job
113 self.autodir = job.autodir
mblighf4c35322006-03-13 01:01:10 +0000114
jadmanski0afbb632008-06-06 21:10:57 +0000115 self.src_dir = os.path.join(tmp_dir, 'src')
116 self.build_dir = os.path.join(tmp_dir, build_dir)
117 # created by get_kernel_tree
118 self.config_dir = os.path.join(subdir, 'config')
119 self.log_dir = os.path.join(subdir, 'debug')
120 self.results_dir = os.path.join(subdir, 'results')
121 self.subdir = os.path.basename(subdir)
mbligh1e8858e2006-11-24 22:18:35 +0000122
jadmanski0afbb632008-06-06 21:10:57 +0000123 self.installed_as = None
apw87c65c12007-09-27 17:19:37 +0000124
jadmanski0afbb632008-06-06 21:10:57 +0000125 if not leave:
126 if os.path.isdir(self.src_dir):
127 utils.system('rm -rf ' + self.src_dir)
128 if os.path.isdir(self.build_dir):
129 utils.system('rm -rf ' + self.build_dir)
mbligh1e8858e2006-11-24 22:18:35 +0000130
jadmanski0afbb632008-06-06 21:10:57 +0000131 if not os.path.exists(self.src_dir):
132 os.mkdir(self.src_dir)
133 for path in [self.config_dir, self.log_dir, self.results_dir]:
134 if os.path.exists(path):
135 utils.system('rm -rf ' + path)
136 os.mkdir(path)
mblighf4c35322006-03-13 01:01:10 +0000137
jadmanski0afbb632008-06-06 21:10:57 +0000138 logpath = os.path.join(self.log_dir, 'build_log')
139 self.logfile = open(logpath, 'w+')
140 self.applied_patches = []
mbligh4426de02006-10-10 07:18:28 +0000141
jadmanski0afbb632008-06-06 21:10:57 +0000142 self.target_arch = None
143 self.build_target = 'bzImage'
144 self.build_image = None
mblighfdbcaec2006-10-01 23:28:57 +0000145
mbligh53da18e2009-01-05 21:13:26 +0000146 arch = utils.get_current_kernel_arch()
jadmanski0afbb632008-06-06 21:10:57 +0000147 if arch == 's390' or arch == 's390x':
148 self.build_target = 'image'
149 elif arch == 'ia64':
150 self.build_target = 'all'
151 self.build_image = 'vmlinux.gz'
mblighcac347a2007-06-02 17:21:48 +0000152
mbligh925e1b12008-06-12 17:48:38 +0000153 if not leave:
154 self.logfile.write('BASE: %s\n' % base_tree)
mbligh534015f2006-09-15 03:28:56 +0000155
mbligh925e1b12008-06-12 17:48:38 +0000156 # Where we have direct version hint record that
157 # for later configuration selection.
158 shorthand = re.compile(r'^\d+\.\d+\.\d+')
159 if shorthand.match(base_tree):
160 self.base_tree_version = base_tree
161 else:
162 self.base_tree_version = None
apw2366d992007-03-12 20:35:57 +0000163
mbligh925e1b12008-06-12 17:48:38 +0000164 # Actually extract the tree. Make sure we know it occured
165 self.extract(base_tree)
apw040dcaa2007-11-21 19:36:55 +0000166
apw7bae90e2008-03-05 12:18:11 +0000167
jadmanski0afbb632008-06-06 21:10:57 +0000168 def kernelexpand(self, kernel):
169 # If we have something like a path, just use it as it is
170 if '/' in kernel:
171 return [kernel]
apw7bae90e2008-03-05 12:18:11 +0000172
jadmanski0afbb632008-06-06 21:10:57 +0000173 # Find the configured mirror list.
174 mirrors = self.job.config_get('mirror.mirrors')
175 if not mirrors:
176 # LEGACY: convert the kernel.org mirror
177 mirror = self.job.config_get('mirror.ftp_kernel_org')
178 if mirror:
179 korg = 'http://www.kernel.org/pub/linux/kernel'
180 mirrors = [
181 [ korg + '/v2.6', mirror + '/v2.6' ],
mbligh9e6a4f12008-06-06 21:55:12 +0000182 [ korg + '/people/akpm/patches/2.6', mirror + '/akpm' ],
183 [ korg + '/people/mbligh', mirror + '/mbligh' ],
jadmanski0afbb632008-06-06 21:10:57 +0000184 ]
apw7bae90e2008-03-05 12:18:11 +0000185
jadmanski0afbb632008-06-06 21:10:57 +0000186 patches = kernelexpand.expand_classic(kernel, mirrors)
187 print patches
apw7bae90e2008-03-05 12:18:11 +0000188
jadmanski0afbb632008-06-06 21:10:57 +0000189 return patches
apw7bae90e2008-03-05 12:18:11 +0000190
mblighf4c35322006-03-13 01:01:10 +0000191
mbligh1b3b3762008-09-25 02:46:34 +0000192 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000193 @tee_output_logdir_mark
194 def extract(self, base_tree):
195 if os.path.exists(base_tree):
196 self.get_kernel_tree(base_tree)
197 else:
198 base_components = self.kernelexpand(base_tree)
199 print 'kernelexpand: '
200 print base_components
201 self.get_kernel_tree(base_components.pop(0))
202 if base_components: # apply remaining patches
203 self.patch(*base_components)
mblighf4c35322006-03-13 01:01:10 +0000204
mblighf4c35322006-03-13 01:01:10 +0000205
mbligh1b3b3762008-09-25 02:46:34 +0000206 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000207 @tee_output_logdir_mark
208 def patch(self, *patches):
209 """Apply a list of patches (in order)"""
210 if not patches:
211 return
212 print 'Applying patches: ', patches
213 self.apply_patches(self.get_patches(patches))
mblighf4c35322006-03-13 01:01:10 +0000214
mblighf4c35322006-03-13 01:01:10 +0000215
mbligh1b3b3762008-09-25 02:46:34 +0000216 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000217 @tee_output_logdir_mark
mblighb3400e02008-11-06 15:44:25 +0000218 def config(self, config_file = '', config_list = None, defconfig = False, make = None):
jadmanski0afbb632008-06-06 21:10:57 +0000219 self.set_cross_cc()
220 config = kernel_config.kernel_config(self.job, self.build_dir,
221 self.config_dir, config_file, config_list,
mblighb3400e02008-11-06 15:44:25 +0000222 defconfig, self.base_tree_version, make)
mblighf4c35322006-03-13 01:01:10 +0000223
mblighf4c35322006-03-13 01:01:10 +0000224
jadmanski0afbb632008-06-06 21:10:57 +0000225 def get_patches(self, patches):
226 """fetch the patches to the local src_dir"""
227 local_patches = []
228 for patch in patches:
mbligh1c9b1d22008-06-12 17:46:12 +0000229 dest = os.path.join(self.src_dir, os.path.basename(patch))
jadmanski0afbb632008-06-06 21:10:57 +0000230 # FIXME: this isn't unique. Append something to it
231 # like wget does if it's not there?
jadmanskie3f2f712008-06-12 17:54:56 +0000232 print "get_file %s %s %s %s" % (patch, dest, self.src_dir,
233 os.path.basename(patch))
jadmanski0afbb632008-06-06 21:10:57 +0000234 utils.get_file(patch, dest)
235 # probably safer to use the command, not python library
236 md5sum = utils.system_output('md5sum ' + dest).split()[0]
237 local_patches.append((patch, dest, md5sum))
238 return local_patches
mbligh72b88fc2006-12-16 18:41:35 +0000239
mblighf4c35322006-03-13 01:01:10 +0000240
jadmanski0afbb632008-06-06 21:10:57 +0000241 def apply_patches(self, local_patches):
242 """apply the list of patches, in order"""
243 builddir = self.build_dir
244 os.chdir(builddir)
mbligh72b88fc2006-12-16 18:41:35 +0000245
jadmanski0afbb632008-06-06 21:10:57 +0000246 if not local_patches:
247 return None
248 for (spec, local, md5sum) in local_patches:
249 if local.endswith('.bz2') or local.endswith('.gz'):
250 ref = spec
251 else:
mbligh53da18e2009-01-05 21:13:26 +0000252 ref = utils.force_copy(local, self.results_dir)
jadmanski0afbb632008-06-06 21:10:57 +0000253 ref = self.job.relative_path(ref)
254 patch_id = "%s %s %s" % (spec, ref, md5sum)
255 log = "PATCH: " + patch_id + "\n"
256 print log
mbligh53da18e2009-01-05 21:13:26 +0000257 utils.cat_file_to_cmd(local, 'patch -p1 > /dev/null')
jadmanski0afbb632008-06-06 21:10:57 +0000258 self.logfile.write(log)
259 self.applied_patches.append(patch_id)
mbligh72b88fc2006-12-16 18:41:35 +0000260
mblighf4c35322006-03-13 01:01:10 +0000261
jadmanski0afbb632008-06-06 21:10:57 +0000262 def get_kernel_tree(self, base_tree):
263 """Extract/link base_tree to self.build_dir"""
mbligh5970cf02006-08-06 15:39:22 +0000264
jadmanski0afbb632008-06-06 21:10:57 +0000265 # if base_tree is a dir, assume uncompressed kernel
266 if os.path.isdir(base_tree):
267 print 'Symlinking existing kernel source'
268 os.symlink(base_tree, self.build_dir)
mblighf4c35322006-03-13 01:01:10 +0000269
jadmanski0afbb632008-06-06 21:10:57 +0000270 # otherwise, extract tarball
271 else:
272 os.chdir(os.path.dirname(self.src_dir))
273 # Figure out local destination for tarball
mblighfef5ce22010-04-08 17:59:52 +0000274 tarball = os.path.join(self.src_dir, os.path.basename(base_tree.split(';')[0]))
jadmanski0afbb632008-06-06 21:10:57 +0000275 utils.get_file(base_tree, tarball)
276 print 'Extracting kernel tarball:', tarball, '...'
mbligh53da18e2009-01-05 21:13:26 +0000277 utils.extract_tarball_to_dir(tarball, self.build_dir)
mblighfdbcaec2006-10-01 23:28:57 +0000278
279
jadmanski0afbb632008-06-06 21:10:57 +0000280 def extraversion(self, tag, append=1):
281 os.chdir(self.build_dir)
282 extraversion_sub = r's/^EXTRAVERSION =\s*\(.*\)/EXTRAVERSION = '
283 if append:
284 p = extraversion_sub + '\\1-%s/' % tag
285 else:
286 p = extraversion_sub + '-%s/' % tag
287 utils.system('mv Makefile Makefile.old')
288 utils.system('sed "%s" < Makefile.old > Makefile' % p)
mbligh72b88fc2006-12-16 18:41:35 +0000289
apwc7846102006-04-06 18:22:13 +0000290
mbligh1b3b3762008-09-25 02:46:34 +0000291 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000292 @tee_output_logdir_mark
293 def build(self, make_opts = '', logfile = '', extraversion='autotest'):
294 """build the kernel
apwc7846102006-04-06 18:22:13 +0000295
jadmanski0afbb632008-06-06 21:10:57 +0000296 make_opts
297 additional options to make, if any
298 """
299 os_dep.commands('gcc', 'make')
300 if logfile == '':
301 logfile = os.path.join(self.log_dir, 'kernel_build')
302 os.chdir(self.build_dir)
303 if extraversion:
304 self.extraversion(extraversion)
305 self.set_cross_cc()
306 # setup_config_file(config_file, config_overrides)
mbligh1e8858e2006-11-24 22:18:35 +0000307
jadmanski0afbb632008-06-06 21:10:57 +0000308 # Not needed on 2.6, but hard to tell -- handle failure
309 utils.system('make dep', ignore_status=True)
mbligh53da18e2009-01-05 21:13:26 +0000310 threads = 2 * utils.count_cpus()
jadmanski0afbb632008-06-06 21:10:57 +0000311 build_string = 'make -j %d %s %s' % (threads, make_opts,
312 self.build_target)
313 # eg make bzImage, or make zImage
314 print build_string
mbligh925e1b12008-06-12 17:48:38 +0000315 utils.system(build_string)
jadmanski0afbb632008-06-06 21:10:57 +0000316 if kernel_config.modules_needed('.config'):
317 utils.system('make -j %d modules' % (threads))
mblighf4c35322006-03-13 01:01:10 +0000318
jadmanski0afbb632008-06-06 21:10:57 +0000319 kernel_version = self.get_kernel_build_ver()
320 kernel_version = re.sub('-autotest', '', kernel_version)
321 self.logfile.write('BUILD VERSION: %s\n' % kernel_version)
mblighf4c35322006-03-13 01:01:10 +0000322
mbligh53da18e2009-01-05 21:13:26 +0000323 utils.force_copy(self.build_dir+'/System.map',
mbligh925e1b12008-06-12 17:48:38 +0000324 self.results_dir)
mbligh30f28c52007-10-11 18:35:35 +0000325
mbligh30f28c52007-10-11 18:35:35 +0000326
jadmanski0afbb632008-06-06 21:10:57 +0000327 def build_timed(self, threads, timefile = '/dev/null', make_opts = '',
328 output = '/dev/null'):
329 """time the bulding of the kernel"""
330 os.chdir(self.build_dir)
331 self.set_cross_cc()
332
333 self.clean(logged=False)
334 build_string = "/usr/bin/time -o %s make %s -j %s vmlinux" \
335 % (timefile, make_opts, threads)
336 build_string += ' > %s 2>&1' % output
337 print build_string
338 utils.system(build_string)
339
340 if (not os.path.isfile('vmlinux')):
341 errmsg = "no vmlinux found, kernel build failed"
342 raise error.TestError(errmsg)
343
344
mbligh1b3b3762008-09-25 02:46:34 +0000345 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000346 @tee_output_logdir_mark
347 def clean(self):
348 """make clean in the kernel tree"""
349 os.chdir(self.build_dir)
350 print "make clean"
351 utils.system('make clean > /dev/null 2> /dev/null')
352
353
mbligh1b3b3762008-09-25 02:46:34 +0000354 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000355 @tee_output_logdir_mark
356 def mkinitrd(self, version, image, system_map, initrd):
357 """Build kernel initrd image.
358 Try to use distro specific way to build initrd image.
359 Parameters:
360 version
361 new kernel version
362 image
363 new kernel image file
364 system_map
365 System.map file
366 initrd
367 initrd image file to build
368 """
mbligh53da18e2009-01-05 21:13:26 +0000369 vendor = utils.get_os_vendor()
mblighb8a14e32006-05-06 00:17:35 +0000370
jadmanski0afbb632008-06-06 21:10:57 +0000371 if os.path.isfile(initrd):
372 print "Existing %s file, will remove it." % initrd
373 os.remove(initrd)
mblighb8a14e32006-05-06 00:17:35 +0000374
jadmanski0afbb632008-06-06 21:10:57 +0000375 args = self.job.config_get('kernel.mkinitrd_extra_args')
mblighf4c35322006-03-13 01:01:10 +0000376
jadmanski0afbb632008-06-06 21:10:57 +0000377 # don't leak 'None' into mkinitrd command
378 if not args:
379 args = ''
mbligh50f42ea2006-09-30 22:22:21 +0000380
jadmanski0afbb632008-06-06 21:10:57 +0000381 if vendor in ['Red Hat', 'Fedora Core']:
382 utils.system('mkinitrd %s %s %s' % (args, initrd, version))
383 elif vendor in ['SUSE']:
jadmanskid524b0e2008-09-15 14:28:20 +0000384 utils.system('mkinitrd %s -k %s -i %s -M %s' %
385 (args, image, initrd, system_map))
jadmanski0afbb632008-06-06 21:10:57 +0000386 elif vendor in ['Debian', 'Ubuntu']:
387 if os.path.isfile('/usr/sbin/mkinitrd'):
388 cmd = '/usr/sbin/mkinitrd'
389 elif os.path.isfile('/usr/sbin/mkinitramfs'):
390 cmd = '/usr/sbin/mkinitramfs'
391 else:
392 raise error.TestError('No Debian initrd builder')
393 utils.system('%s %s -o %s %s' % (cmd, args, initrd, version))
394 else:
395 raise error.TestError('Unsupported vendor %s' % vendor)
mbligh72b88fc2006-12-16 18:41:35 +0000396
apwe43a30b2007-09-25 16:51:30 +0000397
jadmanski0afbb632008-06-06 21:10:57 +0000398 def set_build_image(self, image):
399 self.build_image = image
mbligh3d515d42007-11-09 17:00:36 +0000400
mbligh50f42ea2006-09-30 22:22:21 +0000401
mbligh1b3b3762008-09-25 02:46:34 +0000402 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000403 @tee_output_logdir_mark
404 def install(self, tag='autotest', prefix = '/'):
405 """make install in the kernel tree"""
mbligh50f42ea2006-09-30 22:22:21 +0000406
jadmanski0afbb632008-06-06 21:10:57 +0000407 # Record that we have installed the kernel, and
408 # the tag under which we installed it.
409 self.installed_as = tag
mbligh8baa2ea2006-12-17 23:01:24 +0000410
jadmanski0afbb632008-06-06 21:10:57 +0000411 os.chdir(self.build_dir)
mbligh8baa2ea2006-12-17 23:01:24 +0000412
jadmanski0afbb632008-06-06 21:10:57 +0000413 if not os.path.isdir(prefix):
414 os.mkdir(prefix)
415 self.boot_dir = os.path.join(prefix, 'boot')
416 if not os.path.isdir(self.boot_dir):
417 os.mkdir(self.boot_dir)
apw87c65c12007-09-27 17:19:37 +0000418
jadmanski0afbb632008-06-06 21:10:57 +0000419 if not self.build_image:
420 images = glob.glob('arch/*/boot/' + self.build_target)
421 if len(images):
422 self.build_image = images[0]
423 else:
424 self.build_image = self.build_target
apw87c65c12007-09-27 17:19:37 +0000425
jadmanski0afbb632008-06-06 21:10:57 +0000426 # remember installed files
427 self.vmlinux = self.boot_dir + '/vmlinux-' + tag
428 if (self.build_image != 'vmlinux'):
429 self.image = self.boot_dir + '/vmlinuz-' + tag
430 else:
431 self.image = self.vmlinux
432 self.system_map = self.boot_dir + '/System.map-' + tag
mbligh925e1b12008-06-12 17:48:38 +0000433 self.config_file = self.boot_dir + '/config-' + tag
jadmanski0afbb632008-06-06 21:10:57 +0000434 self.initrd = ''
mbligh72b88fc2006-12-16 18:41:35 +0000435
jadmanski0afbb632008-06-06 21:10:57 +0000436 # copy to boot dir
mbligh53da18e2009-01-05 21:13:26 +0000437 utils.force_copy('vmlinux', self.vmlinux)
jadmanski0afbb632008-06-06 21:10:57 +0000438 if (self.build_image != 'vmlinux'):
mbligh53da18e2009-01-05 21:13:26 +0000439 utils.force_copy(self.build_image, self.image)
440 utils.force_copy('System.map', self.system_map)
441 utils.force_copy('.config', self.config_file)
mbligh0ad65582006-10-06 04:16:36 +0000442
jadmanski0afbb632008-06-06 21:10:57 +0000443 if not kernel_config.modules_needed('.config'):
444 return
mbligha87116f2006-10-10 02:47:08 +0000445
jadmanski0afbb632008-06-06 21:10:57 +0000446 utils.system('make modules_install INSTALL_MOD_PATH=%s' % prefix)
447 if prefix == '/':
448 self.initrd = self.boot_dir + '/initrd-' + tag
449 self.mkinitrd(self.get_kernel_build_ver(), self.image,
450 self.system_map, self.initrd)
mbligha87116f2006-10-10 02:47:08 +0000451
mbligha87116f2006-10-10 02:47:08 +0000452
jadmanski0afbb632008-06-06 21:10:57 +0000453 def add_to_bootloader(self, tag='autotest', args=''):
454 """ add this kernel to bootloader, taking an
455 optional parameter of space separated parameters
456 e.g.: kernel.add_to_bootloader('mykernel', 'ro acpi=off')
457 """
mbligh516c8df2010-02-12 18:48:52 +0000458 _add_kernel_to_bootloader(self.job.bootloader,
459 self.job.config_get('boot.default_args'),
460 tag, args, self.image, self.initrd)
apwcbe32572006-11-28 10:00:23 +0000461
mbligha87116f2006-10-10 02:47:08 +0000462
jadmanski0afbb632008-06-06 21:10:57 +0000463 def get_kernel_build_arch(self, arch=None):
464 """
465 Work out the current kernel architecture (as a kernel arch)
466 """
467 if not arch:
mbligh53da18e2009-01-05 21:13:26 +0000468 arch = utils.get_current_kernel_arch()
jadmanski0afbb632008-06-06 21:10:57 +0000469 if re.match('i.86', arch):
470 return 'i386'
471 elif re.match('sun4u', arch):
472 return 'sparc64'
473 elif re.match('arm.*', arch):
474 return 'arm'
475 elif re.match('sa110', arch):
476 return 'arm'
477 elif re.match('s390x', arch):
478 return 's390'
479 elif re.match('parisc64', arch):
480 return 'parisc'
481 elif re.match('ppc.*', arch):
482 return 'powerpc'
483 elif re.match('mips.*', arch):
484 return 'mips'
485 else:
486 return arch
mbligh6a1d4db2006-10-06 04:30:16 +0000487
mbligh201aa892006-10-29 04:02:05 +0000488
jadmanski0afbb632008-06-06 21:10:57 +0000489 def get_kernel_build_release(self):
490 releasem = re.compile(r'.*UTS_RELEASE\s+"([^"]+)".*');
491 versionm = re.compile(r'.*UTS_VERSION\s+"([^"]+)".*');
mbligh548f29a2006-10-17 04:55:12 +0000492
jadmanski0afbb632008-06-06 21:10:57 +0000493 release = None
494 version = None
mbligh6a1d4db2006-10-06 04:30:16 +0000495
jadmanskid524b0e2008-09-15 14:28:20 +0000496 for f in [self.build_dir + "/include/linux/version.h",
497 self.build_dir + "/include/linux/utsrelease.h",
mbligh508cbf62009-11-06 03:01:50 +0000498 self.build_dir + "/include/linux/compile.h",
499 self.build_dir + "/include/generated/utsrelease.h",
500 self.build_dir + "/include/generated/compile.h"]:
jadmanskid524b0e2008-09-15 14:28:20 +0000501 if os.path.exists(f):
502 fd = open(f, 'r')
jadmanski0afbb632008-06-06 21:10:57 +0000503 for line in fd.readlines():
504 m = releasem.match(line)
505 if m:
506 release = m.groups()[0]
507 m = versionm.match(line)
508 if m:
509 version = m.groups()[0]
510 fd.close()
mbligh237bed32007-09-05 13:05:57 +0000511
jadmanski0afbb632008-06-06 21:10:57 +0000512 return (release, version)
mbligh237bed32007-09-05 13:05:57 +0000513
mbligh237bed32007-09-05 13:05:57 +0000514
jadmanski0afbb632008-06-06 21:10:57 +0000515 def get_kernel_build_ident(self):
516 (release, version) = self.get_kernel_build_release()
mbligh237bed32007-09-05 13:05:57 +0000517
jadmanski0afbb632008-06-06 21:10:57 +0000518 if not release or not version:
519 raise error.JobError('kernel has no identity')
mbligh237bed32007-09-05 13:05:57 +0000520
jadmanski0afbb632008-06-06 21:10:57 +0000521 return release + '::' + version
mbligh237bed32007-09-05 13:05:57 +0000522
mbligh237bed32007-09-05 13:05:57 +0000523
jadmanski067b26c2008-09-25 19:46:56 +0000524 def boot(self, args='', ident=True):
jadmanski0afbb632008-06-06 21:10:57 +0000525 """ install and boot this kernel, do not care how
526 just make it happen.
527 """
mbligh237bed32007-09-05 13:05:57 +0000528
jadmanski0afbb632008-06-06 21:10:57 +0000529 # If we can check the kernel identity do so.
jadmanski067b26c2008-09-25 19:46:56 +0000530 expected_ident = self.get_kernel_build_ident()
jadmanski0afbb632008-06-06 21:10:57 +0000531 if ident:
532 when = int(time.time())
jadmanski0afbb632008-06-06 21:10:57 +0000533 args += " IDENT=%d" % (when)
jadmanski067b26c2008-09-25 19:46:56 +0000534 self.job.next_step_prepend(["job.end_reboot_and_verify", when,
535 expected_ident, self.subdir,
536 self.applied_patches])
537 else:
538 self.job.next_step_prepend(["job.end_reboot", self.subdir,
539 expected_ident, self.applied_patches])
mbligh237bed32007-09-05 13:05:57 +0000540
jadmanski0afbb632008-06-06 21:10:57 +0000541 # Check if the kernel has been installed, if not install
542 # as the default tag and boot that.
543 if not self.installed_as:
544 self.install()
mbligh237bed32007-09-05 13:05:57 +0000545
jadmanski0afbb632008-06-06 21:10:57 +0000546 # Boot the selected tag.
547 self.add_to_bootloader(args=args, tag=self.installed_as)
apw87c65c12007-09-27 17:19:37 +0000548
jadmanski0afbb632008-06-06 21:10:57 +0000549 # Boot it.
jadmanski02c0e452008-10-21 16:33:44 +0000550 self.job.start_reboot()
jadmanski0afbb632008-06-06 21:10:57 +0000551 self.job.reboot(tag=self.installed_as)
apw1b5dc362006-10-31 11:24:26 +0000552
apw1b5dc362006-10-31 11:24:26 +0000553
jadmanski0afbb632008-06-06 21:10:57 +0000554 def get_kernel_build_ver(self):
555 """Check Makefile and .config to return kernel version"""
556 version = patchlevel = sublevel = extraversion = localversion = ''
apw1b5dc362006-10-31 11:24:26 +0000557
jadmanski0afbb632008-06-06 21:10:57 +0000558 for line in open(self.build_dir + '/Makefile', 'r').readlines():
559 if line.startswith('VERSION'):
560 version = line[line.index('=') + 1:].strip()
561 if line.startswith('PATCHLEVEL'):
562 patchlevel = line[line.index('=') + 1:].strip()
563 if line.startswith('SUBLEVEL'):
564 sublevel = line[line.index('=') + 1:].strip()
565 if line.startswith('EXTRAVERSION'):
566 extraversion = line[line.index('=') + 1:].strip()
mblighe11f5fc2006-10-04 04:42:22 +0000567
jadmanski0afbb632008-06-06 21:10:57 +0000568 for line in open(self.build_dir + '/.config', 'r').readlines():
569 if line.startswith('CONFIG_LOCALVERSION='):
570 localversion = line.rstrip().split('"')[1]
mblighe11f5fc2006-10-04 04:42:22 +0000571
jadmanski0afbb632008-06-06 21:10:57 +0000572 return "%s.%s.%s%s%s" %(version, patchlevel, sublevel, extraversion, localversion)
mblighe11f5fc2006-10-04 04:42:22 +0000573
mblighfdbcaec2006-10-01 23:28:57 +0000574
jadmanski0afbb632008-06-06 21:10:57 +0000575 def set_build_target(self, build_target):
576 if build_target:
577 self.build_target = build_target
578 print 'BUILD TARGET: %s' % self.build_target
mblighfdbcaec2006-10-01 23:28:57 +0000579
mbligh8baa2ea2006-12-17 23:01:24 +0000580
jadmanski0afbb632008-06-06 21:10:57 +0000581 def set_cross_cc(self, target_arch=None, cross_compile=None,
582 build_target='bzImage'):
583 """Set up to cross-compile.
584 This is broken. We need to work out what the default
585 compile produces, and if not, THEN set the cross
586 compiler.
587 """
mbligh8baa2ea2006-12-17 23:01:24 +0000588
jadmanski0afbb632008-06-06 21:10:57 +0000589 if self.target_arch:
590 return
mblighcc2e6662006-09-14 01:24:07 +0000591
jadmanski0afbb632008-06-06 21:10:57 +0000592 # if someone has set build_target, don't clobber in set_cross_cc
593 # run set_build_target before calling set_cross_cc
594 if not self.build_target:
595 self.set_build_target(build_target)
mbligh678823f2006-12-07 18:49:00 +0000596
jadmanski0afbb632008-06-06 21:10:57 +0000597 # If no 'target_arch' given assume native compilation
mblighd876f452008-12-03 15:09:17 +0000598 if target_arch is None:
mbligh53da18e2009-01-05 21:13:26 +0000599 target_arch = utils.get_current_kernel_arch()
jadmanski0afbb632008-06-06 21:10:57 +0000600 if target_arch == 'ppc64':
601 if self.build_target == 'bzImage':
602 self.build_target = 'vmlinux'
mbligh72b88fc2006-12-16 18:41:35 +0000603
jadmanski0afbb632008-06-06 21:10:57 +0000604 if not cross_compile:
605 cross_compile = self.job.config_get('kernel.cross_cc')
mbligh678823f2006-12-07 18:49:00 +0000606
jadmanski0afbb632008-06-06 21:10:57 +0000607 if cross_compile:
608 os.environ['CROSS_COMPILE'] = cross_compile
609 else:
610 if os.environ.has_key('CROSS_COMPILE'):
611 del os.environ['CROSS_COMPILE']
mbligh678823f2006-12-07 18:49:00 +0000612
jadmanski0afbb632008-06-06 21:10:57 +0000613 return # HACK. Crap out for now.
mblighf4c35322006-03-13 01:01:10 +0000614
jadmanski0afbb632008-06-06 21:10:57 +0000615 # At this point I know what arch I *want* to build for
616 # but have no way of working out what arch the default
617 # compiler DOES build for.
mblighcc2e6662006-09-14 01:24:07 +0000618
mbligh925e1b12008-06-12 17:48:38 +0000619 def install_package(package):
620 raise NotImplementedError("I don't exist yet!")
mbligh72b88fc2006-12-16 18:41:35 +0000621
jadmanski0afbb632008-06-06 21:10:57 +0000622 if target_arch == 'ppc64':
623 install_package('ppc64-cross')
624 cross_compile = os.path.join(self.autodir, 'sources/ppc64-cross/bin')
mblighcc2e6662006-09-14 01:24:07 +0000625
jadmanski0afbb632008-06-06 21:10:57 +0000626 elif target_arch == 'x86_64':
627 install_package('x86_64-cross')
628 cross_compile = os.path.join(self.autodir, 'sources/x86_64-cross/bin')
mblighb8a14e32006-05-06 00:17:35 +0000629
jadmanski0afbb632008-06-06 21:10:57 +0000630 os.environ['ARCH'] = self.target_arch = target_arch
mbligh5970cf02006-08-06 15:39:22 +0000631
jadmanski0afbb632008-06-06 21:10:57 +0000632 self.cross_compile = cross_compile
633 if self.cross_compile:
634 os.environ['CROSS_COMPILE'] = self.cross_compile
mblighcc2e6662006-09-14 01:24:07 +0000635
mbligh72b88fc2006-12-16 18:41:35 +0000636
jadmanski0afbb632008-06-06 21:10:57 +0000637 def pickle_dump(self, filename):
638 """dump a pickle of ourself out to the specified filename
mblighc86b0b42006-07-28 17:35:28 +0000639
jadmanski0afbb632008-06-06 21:10:57 +0000640 we can't pickle the backreference to job (it contains fd's),
641 nor would we want to. Same for logfile (fd's).
642 """
643 temp = copy.copy(self)
644 temp.job = None
645 temp.logfile = None
646 pickle.dump(temp, open(filename, 'w'))
mbligh736adc92007-10-18 03:23:22 +0000647
648
jadmanski6ca37b62008-06-30 21:17:07 +0000649class rpm_kernel(object):
mbligheaa75e52009-11-06 03:08:08 +0000650 """
651 Class for installing a binary rpm kernel package
jadmanski0afbb632008-06-06 21:10:57 +0000652 """
mbligh736adc92007-10-18 03:23:22 +0000653
jadmanski0afbb632008-06-06 21:10:57 +0000654 def __init__(self, job, rpm_package, subdir):
655 self.job = job
656 self.rpm_package = rpm_package
657 self.log_dir = os.path.join(subdir, 'debug')
658 self.subdir = os.path.basename(subdir)
659 if os.path.exists(self.log_dir):
660 utils.system('rm -rf ' + self.log_dir)
661 os.mkdir(self.log_dir)
662 self.installed_as = None
mbligh736adc92007-10-18 03:23:22 +0000663
664
mbligheaa75e52009-11-06 03:08:08 +0000665 def build(self, *args, **dargs):
666 """
667 Dummy function, binary kernel so nothing to build.
668 """
669 pass
670
671
mbligh1b3b3762008-09-25 02:46:34 +0000672 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000673 @tee_output_logdir_mark
mbligha25e8c32009-06-15 21:27:23 +0000674 def install(self, tag='autotest', install_vmlinux=True):
jadmanski0afbb632008-06-06 21:10:57 +0000675 self.installed_as = tag
mblighda0311e2007-10-25 16:03:33 +0000676
mbligh1b160a02009-05-21 01:27:10 +0000677 self.image = None
jadmanski0afbb632008-06-06 21:10:57 +0000678 self.initrd = ''
mbligh1b160a02009-05-21 01:27:10 +0000679 for rpm_pack in self.rpm_package:
680 rpm_name = utils.system_output('rpm -qp ' + rpm_pack)
mbligh736adc92007-10-18 03:23:22 +0000681
mbligh1b160a02009-05-21 01:27:10 +0000682 # install
683 utils.system('rpm -i --force ' + rpm_pack)
684
685 # get file list
686 files = utils.system_output('rpm -ql ' + rpm_name).splitlines()
687
688 # search for vmlinuz
689 for file in files:
690 if file.startswith('/boot/vmlinuz'):
691 self.full_version = file[len('/boot/vmlinuz-'):]
692 self.image = file
693 self.rpm_flavour = rpm_name.split('-')[1]
694
695 # get version and release number
696 self.version, self.release = utils.system_output(
697 'rpm --queryformat="%{VERSION}\\n%{RELEASE}\\n" -q '
698 + rpm_name).splitlines()[0:2]
699
700 # prefer /boot/kernel-version before /boot/kernel
701 if self.full_version:
702 break
703
704 # search for initrd
705 for file in files:
706 if file.startswith('/boot/initrd'):
707 self.initrd = file
708 # prefer /boot/initrd-version before /boot/initrd
709 if len(file) > len('/boot/initrd'):
710 break
711
712 if self.image == None:
713 errmsg = "specified rpm file(s) don't contain /boot/vmlinuz"
714 raise error.TestError(errmsg)
mbligh736adc92007-10-18 03:23:22 +0000715
mbligha25e8c32009-06-15 21:27:23 +0000716 # install vmlinux
717 if install_vmlinux:
718 for rpm_pack in self.rpm_package:
719 vmlinux = utils.system_output(
720 'rpm -q -l -p %s | grep /boot/vmlinux' % rpm_pack)
jadmanski19426ea2009-07-28 20:19:40 +0000721 utils.system('cd /; rpm2cpio %s | cpio -imuv .%s 2>&1'
mbligha25e8c32009-06-15 21:27:23 +0000722 % (rpm_pack, vmlinux))
723 if not os.path.exists(vmlinux):
724 raise error.TestError('%s does not exist after installing %s'
725 % (vmlinux, rpm_pack))
726
mbligh736adc92007-10-18 03:23:22 +0000727
jadmanski0afbb632008-06-06 21:10:57 +0000728 def add_to_bootloader(self, tag='autotest', args=''):
729 """ Add this kernel to bootloader
730 """
mbligh516c8df2010-02-12 18:48:52 +0000731 _add_kernel_to_bootloader(self.job.bootloader,
732 self.job.config_get('boot.default_args'),
733 tag, args, self.image, self.initrd)
mbligh10a24a72007-10-24 21:02:53 +0000734
735
jadmanski067b26c2008-09-25 19:46:56 +0000736 def boot(self, args='', ident=True):
jadmanski0afbb632008-06-06 21:10:57 +0000737 """ install and boot this kernel
738 """
mbligh73e82a32007-11-08 21:35:29 +0000739
jadmanski0afbb632008-06-06 21:10:57 +0000740 # Check if the kernel has been installed, if not install
741 # as the default tag and boot that.
742 if not self.installed_as:
743 self.install()
mblighda0311e2007-10-25 16:03:33 +0000744
jadmanski0afbb632008-06-06 21:10:57 +0000745 # If we can check the kernel identity do so.
mblighb1887c82009-03-12 00:25:48 +0000746 expected_ident = self.full_version
747 if not expected_ident:
748 expected_ident = '-'.join([self.version,
mbligh1b160a02009-05-21 01:27:10 +0000749 self.rpm_flavour,
mblighb1887c82009-03-12 00:25:48 +0000750 self.release])
jadmanski0afbb632008-06-06 21:10:57 +0000751 if ident:
752 when = int(time.time())
jadmanski0afbb632008-06-06 21:10:57 +0000753 args += " IDENT=%d" % (when)
jadmanski067b26c2008-09-25 19:46:56 +0000754 self.job.next_step_prepend(["job.end_reboot_and_verify",
755 when, expected_ident, None, 'rpm'])
756 else:
757 self.job.next_step_prepend(["job.end_reboot", None,
758 expected_ident, []])
mbligh10a24a72007-10-24 21:02:53 +0000759
jadmanski0afbb632008-06-06 21:10:57 +0000760 # Boot the selected tag.
761 self.add_to_bootloader(args=args, tag=self.installed_as)
mbligh10a24a72007-10-24 21:02:53 +0000762
jadmanski0afbb632008-06-06 21:10:57 +0000763 # Boot it.
jadmanski02c0e452008-10-21 16:33:44 +0000764 self.job.start_reboot()
jadmanski0afbb632008-06-06 21:10:57 +0000765 self.job.reboot(tag=self.installed_as)
mbligh6ee7ee02007-11-13 23:49:05 +0000766
767
mblighe7785cc2009-03-17 17:32:47 +0000768class rpm_kernel_suse(rpm_kernel):
769 """ Class for installing openSUSE/SLE rpm kernel package
770 """
771
772 def install(self):
773 # do not set the new kernel as the default one
774 os.environ['PBL_AUTOTEST'] = '1'
775
776 rpm_kernel.install(self, 'dummy')
777 self.installed_as = self.job.bootloader.get_title_for_kernel(self.image)
778 if not self.installed_as:
779 errmsg = "cannot find installed kernel in bootloader configuration"
780 raise error.TestError(errmsg)
781
782
783 def add_to_bootloader(self, tag='dummy', args=''):
784 """ Set parameters of this kernel in bootloader
785 """
786
787 # pull the base argument set from the job config
788 baseargs = self.job.config_get('boot.default_args')
789 if baseargs:
790 args = baseargs + ' ' + args
791
792 self.job.bootloader.add_args(tag, args)
793
794
795def rpm_kernel_vendor(job, rpm_package, subdir):
mbligh1ef218d2009-08-03 16:57:56 +0000796 vendor = utils.get_os_vendor()
797 if vendor == "SUSE":
798 return rpm_kernel_suse(job, rpm_package, subdir)
799 else:
800 return rpm_kernel(job, rpm_package, subdir)
mblighe7785cc2009-03-17 17:32:47 +0000801
802
mbligh062ed152009-01-13 00:57:14 +0000803# just make the preprocessor a nop
804def _preprocess_path_dummy(path):
805 return path.strip()
806
807
mbligh6ee7ee02007-11-13 23:49:05 +0000808# pull in some optional site-specific path pre-processing
jadmanski19426ea2009-07-28 20:19:40 +0000809preprocess_path = utils.import_site_function(__file__,
mbligh062ed152009-01-13 00:57:14 +0000810 "autotest_lib.client.bin.site_kernel", "preprocess_path",
811 _preprocess_path_dummy)
mbligh6ee7ee02007-11-13 23:49:05 +0000812
mblighc5ddfd12008-08-04 17:15:00 +0000813
mbligh6ee7ee02007-11-13 23:49:05 +0000814def auto_kernel(job, path, subdir, tmp_dir, build_dir, leave=False):
mbligh7aeda672009-01-30 00:35:59 +0000815 """
jadmanski0afbb632008-06-06 21:10:57 +0000816 Create a kernel object, dynamically selecting the appropriate class to use
817 based on the path provided.
818 """
mbligh1b160a02009-05-21 01:27:10 +0000819 kernel_paths = [preprocess_path(path)]
820 if kernel_paths[0].endswith('.list'):
mbligh1ef218d2009-08-03 16:57:56 +0000821 # Fetch the list of packages to install
mbligh1b160a02009-05-21 01:27:10 +0000822 kernel_list = os.path.join(tmp_dir, 'kernel.list')
823 utils.get_file(kernel_paths[0], kernel_list)
824 kernel_paths = [p.strip() for p in open(kernel_list).readlines()]
mblighc5ddfd12008-08-04 17:15:00 +0000825
mbligh1b160a02009-05-21 01:27:10 +0000826 if kernel_paths[0].endswith('.rpm'):
827 rpm_paths = []
828 for kernel_path in kernel_paths:
jadmanski19426ea2009-07-28 20:19:40 +0000829 if os.path.exists(kernel_path):
mbligh1b160a02009-05-21 01:27:10 +0000830 rpm_paths.append(kernel_path)
mbligh1b160a02009-05-21 01:27:10 +0000831 else:
832 # Fetch the rpm into the job's packages directory and pass it to
833 # rpm_kernel
834 rpm_name = os.path.basename(kernel_path)
mbligh7aeda672009-01-30 00:35:59 +0000835
mbligh1b160a02009-05-21 01:27:10 +0000836 # If the preprocessed path (kernel_path) is only a name then
837 # search for the kernel in all the repositories, else fetch the
838 # kernel from that specific path.
839 job.pkgmgr.fetch_pkg(rpm_name, os.path.join(job.pkgdir, rpm_name),
840 repo_url=os.path.dirname(kernel_path))
841
842 rpm_paths.append(os.path.join(job.pkgdir, rpm_name))
843 return rpm_kernel_vendor(job, rpm_paths, subdir)
jadmanski0afbb632008-06-06 21:10:57 +0000844 else:
mbligh1b160a02009-05-21 01:27:10 +0000845 if len(kernel_paths) > 1:
846 raise error.TestError("don't know what to do with more than one non-rpm kernel file")
847 return kernel(job,kernel_paths[0], subdir, tmp_dir, build_dir, leave)