blob: dd6513bd5f0c6823f2f4a584954b5d037bab0f9f [file] [log] [blame]
mblighc61fb362008-06-05 16:22:15 +00001import os, shutil, copy, pickle, re, glob, time
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 _mark(filename, msg):
8 file = open(filename, 'a')
9 file.write(msg)
10 file.close()
11
12
13def tee_output_logdir_mark(fn):
14 def tee_logdir_mark_wrapper(self, *args, **dargs):
15 mark = self.__class__.__name__ + "." + fn.__name__
16 outfile = os.path.join(self.log_dir, 'client.log')
17 _mark(outfile, "--- START " + mark + " ---\n")
18 self.job.logging.tee_redirect_debug_dir(self.log_dir)
19 try:
20 result = fn(self, *args, **dargs)
21 finally:
22 self.job.logging.restore()
23 _mark(outfile, "--- END " + mark + " ---\n")
24
25 return result
26
27 tee_logdir_mark_wrapper.__name__ = fn.__name__
28 return tee_logdir_mark_wrapper
29
30
jadmanski6ca37b62008-06-30 21:17:07 +000031class kernel(object):
jadmanski0afbb632008-06-06 21:10:57 +000032 """ Class for compiling kernels.
mblighc86b0b42006-07-28 17:35:28 +000033
jadmanski0afbb632008-06-06 21:10:57 +000034 Data for the object includes the src files
35 used to create the kernel, patches applied, config (base + changes),
36 the build directory itself, and logged output
mblighc86b0b42006-07-28 17:35:28 +000037
jadmanski0afbb632008-06-06 21:10:57 +000038 Properties:
39 job
40 Backpointer to the job object we're part of
41 autodir
42 Path to the top level autotest dir (/usr/local/autotest)
43 src_dir
44 <tmp_dir>/src/
45 build_dir
46 <tmp_dir>/linux/
47 config_dir
48 <results_dir>/config/
49 log_dir
50 <results_dir>/debug/
51 results_dir
52 <results_dir>/results/
53 """
mblighc86b0b42006-07-28 17:35:28 +000054
jadmanski0afbb632008-06-06 21:10:57 +000055 autodir = ''
mbligh8baa2ea2006-12-17 23:01:24 +000056
mbligh925e1b12008-06-12 17:48:38 +000057 def __init__(self, job, base_tree, subdir, tmp_dir, build_dir, leave=False):
jadmanski0afbb632008-06-06 21:10:57 +000058 """Initialize the kernel build environment
mblighc86b0b42006-07-28 17:35:28 +000059
jadmanski0afbb632008-06-06 21:10:57 +000060 job
61 which job this build is part of
62 base_tree
63 base kernel tree. Can be one of the following:
64 1. A local tarball
65 2. A URL to a tarball
66 3. A local directory (will symlink it)
67 4. A shorthand expandable (eg '2.6.11-git3')
68 subdir
69 subdir in the results directory (eg "build")
70 (holds config/, debug/, results/)
71 tmp_dir
mbligh72b88fc2006-12-16 18:41:35 +000072
jadmanski0afbb632008-06-06 21:10:57 +000073 leave
74 Boolean, whether to leave existing tmpdir or not
75 """
76 self.job = job
77 self.autodir = job.autodir
mblighf4c35322006-03-13 01:01:10 +000078
jadmanski0afbb632008-06-06 21:10:57 +000079 self.src_dir = os.path.join(tmp_dir, 'src')
80 self.build_dir = os.path.join(tmp_dir, build_dir)
81 # created by get_kernel_tree
82 self.config_dir = os.path.join(subdir, 'config')
83 self.log_dir = os.path.join(subdir, 'debug')
84 self.results_dir = os.path.join(subdir, 'results')
85 self.subdir = os.path.basename(subdir)
mbligh1e8858e2006-11-24 22:18:35 +000086
jadmanski0afbb632008-06-06 21:10:57 +000087 self.installed_as = None
apw87c65c12007-09-27 17:19:37 +000088
jadmanski0afbb632008-06-06 21:10:57 +000089 if not leave:
90 if os.path.isdir(self.src_dir):
91 utils.system('rm -rf ' + self.src_dir)
92 if os.path.isdir(self.build_dir):
93 utils.system('rm -rf ' + self.build_dir)
mbligh1e8858e2006-11-24 22:18:35 +000094
jadmanski0afbb632008-06-06 21:10:57 +000095 if not os.path.exists(self.src_dir):
96 os.mkdir(self.src_dir)
97 for path in [self.config_dir, self.log_dir, self.results_dir]:
98 if os.path.exists(path):
99 utils.system('rm -rf ' + path)
100 os.mkdir(path)
mblighf4c35322006-03-13 01:01:10 +0000101
jadmanski0afbb632008-06-06 21:10:57 +0000102 logpath = os.path.join(self.log_dir, 'build_log')
103 self.logfile = open(logpath, 'w+')
104 self.applied_patches = []
mbligh4426de02006-10-10 07:18:28 +0000105
jadmanski0afbb632008-06-06 21:10:57 +0000106 self.target_arch = None
107 self.build_target = 'bzImage'
108 self.build_image = None
mblighfdbcaec2006-10-01 23:28:57 +0000109
mbligh53da18e2009-01-05 21:13:26 +0000110 arch = utils.get_current_kernel_arch()
jadmanski0afbb632008-06-06 21:10:57 +0000111 if arch == 's390' or arch == 's390x':
112 self.build_target = 'image'
113 elif arch == 'ia64':
114 self.build_target = 'all'
115 self.build_image = 'vmlinux.gz'
mblighcac347a2007-06-02 17:21:48 +0000116
mbligh925e1b12008-06-12 17:48:38 +0000117 if not leave:
118 self.logfile.write('BASE: %s\n' % base_tree)
mbligh534015f2006-09-15 03:28:56 +0000119
mbligh925e1b12008-06-12 17:48:38 +0000120 # Where we have direct version hint record that
121 # for later configuration selection.
122 shorthand = re.compile(r'^\d+\.\d+\.\d+')
123 if shorthand.match(base_tree):
124 self.base_tree_version = base_tree
125 else:
126 self.base_tree_version = None
apw2366d992007-03-12 20:35:57 +0000127
mbligh925e1b12008-06-12 17:48:38 +0000128 # Actually extract the tree. Make sure we know it occured
129 self.extract(base_tree)
apw040dcaa2007-11-21 19:36:55 +0000130
apw7bae90e2008-03-05 12:18:11 +0000131
jadmanski0afbb632008-06-06 21:10:57 +0000132 def kernelexpand(self, kernel):
133 # If we have something like a path, just use it as it is
134 if '/' in kernel:
135 return [kernel]
apw7bae90e2008-03-05 12:18:11 +0000136
jadmanski0afbb632008-06-06 21:10:57 +0000137 # Find the configured mirror list.
138 mirrors = self.job.config_get('mirror.mirrors')
139 if not mirrors:
140 # LEGACY: convert the kernel.org mirror
141 mirror = self.job.config_get('mirror.ftp_kernel_org')
142 if mirror:
143 korg = 'http://www.kernel.org/pub/linux/kernel'
144 mirrors = [
145 [ korg + '/v2.6', mirror + '/v2.6' ],
mbligh9e6a4f12008-06-06 21:55:12 +0000146 [ korg + '/people/akpm/patches/2.6', mirror + '/akpm' ],
147 [ korg + '/people/mbligh', mirror + '/mbligh' ],
jadmanski0afbb632008-06-06 21:10:57 +0000148 ]
apw7bae90e2008-03-05 12:18:11 +0000149
jadmanski0afbb632008-06-06 21:10:57 +0000150 patches = kernelexpand.expand_classic(kernel, mirrors)
151 print patches
apw7bae90e2008-03-05 12:18:11 +0000152
jadmanski0afbb632008-06-06 21:10:57 +0000153 return patches
apw7bae90e2008-03-05 12:18:11 +0000154
mblighf4c35322006-03-13 01:01:10 +0000155
mbligh1b3b3762008-09-25 02:46:34 +0000156 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000157 @tee_output_logdir_mark
158 def extract(self, base_tree):
159 if os.path.exists(base_tree):
160 self.get_kernel_tree(base_tree)
161 else:
162 base_components = self.kernelexpand(base_tree)
163 print 'kernelexpand: '
164 print base_components
165 self.get_kernel_tree(base_components.pop(0))
166 if base_components: # apply remaining patches
167 self.patch(*base_components)
mblighf4c35322006-03-13 01:01:10 +0000168
mblighf4c35322006-03-13 01:01:10 +0000169
mbligh1b3b3762008-09-25 02:46:34 +0000170 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000171 @tee_output_logdir_mark
172 def patch(self, *patches):
173 """Apply a list of patches (in order)"""
174 if not patches:
175 return
176 print 'Applying patches: ', patches
177 self.apply_patches(self.get_patches(patches))
mblighf4c35322006-03-13 01:01:10 +0000178
mblighf4c35322006-03-13 01:01:10 +0000179
mbligh1b3b3762008-09-25 02:46:34 +0000180 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000181 @tee_output_logdir_mark
mblighb3400e02008-11-06 15:44:25 +0000182 def config(self, config_file = '', config_list = None, defconfig = False, make = None):
jadmanski0afbb632008-06-06 21:10:57 +0000183 self.set_cross_cc()
184 config = kernel_config.kernel_config(self.job, self.build_dir,
185 self.config_dir, config_file, config_list,
mblighb3400e02008-11-06 15:44:25 +0000186 defconfig, self.base_tree_version, make)
mblighf4c35322006-03-13 01:01:10 +0000187
mblighf4c35322006-03-13 01:01:10 +0000188
jadmanski0afbb632008-06-06 21:10:57 +0000189 def get_patches(self, patches):
190 """fetch the patches to the local src_dir"""
191 local_patches = []
192 for patch in patches:
mbligh1c9b1d22008-06-12 17:46:12 +0000193 dest = os.path.join(self.src_dir, os.path.basename(patch))
jadmanski0afbb632008-06-06 21:10:57 +0000194 # FIXME: this isn't unique. Append something to it
195 # like wget does if it's not there?
jadmanskie3f2f712008-06-12 17:54:56 +0000196 print "get_file %s %s %s %s" % (patch, dest, self.src_dir,
197 os.path.basename(patch))
jadmanski0afbb632008-06-06 21:10:57 +0000198 utils.get_file(patch, dest)
199 # probably safer to use the command, not python library
200 md5sum = utils.system_output('md5sum ' + dest).split()[0]
201 local_patches.append((patch, dest, md5sum))
202 return local_patches
mbligh72b88fc2006-12-16 18:41:35 +0000203
mblighf4c35322006-03-13 01:01:10 +0000204
jadmanski0afbb632008-06-06 21:10:57 +0000205 def apply_patches(self, local_patches):
206 """apply the list of patches, in order"""
207 builddir = self.build_dir
208 os.chdir(builddir)
mbligh72b88fc2006-12-16 18:41:35 +0000209
jadmanski0afbb632008-06-06 21:10:57 +0000210 if not local_patches:
211 return None
212 for (spec, local, md5sum) in local_patches:
213 if local.endswith('.bz2') or local.endswith('.gz'):
214 ref = spec
215 else:
mbligh53da18e2009-01-05 21:13:26 +0000216 ref = utils.force_copy(local, self.results_dir)
jadmanski0afbb632008-06-06 21:10:57 +0000217 ref = self.job.relative_path(ref)
218 patch_id = "%s %s %s" % (spec, ref, md5sum)
219 log = "PATCH: " + patch_id + "\n"
220 print log
mbligh53da18e2009-01-05 21:13:26 +0000221 utils.cat_file_to_cmd(local, 'patch -p1 > /dev/null')
jadmanski0afbb632008-06-06 21:10:57 +0000222 self.logfile.write(log)
223 self.applied_patches.append(patch_id)
mbligh72b88fc2006-12-16 18:41:35 +0000224
mblighf4c35322006-03-13 01:01:10 +0000225
jadmanski0afbb632008-06-06 21:10:57 +0000226 def get_kernel_tree(self, base_tree):
227 """Extract/link base_tree to self.build_dir"""
mbligh5970cf02006-08-06 15:39:22 +0000228
jadmanski0afbb632008-06-06 21:10:57 +0000229 # if base_tree is a dir, assume uncompressed kernel
230 if os.path.isdir(base_tree):
231 print 'Symlinking existing kernel source'
232 os.symlink(base_tree, self.build_dir)
mblighf4c35322006-03-13 01:01:10 +0000233
jadmanski0afbb632008-06-06 21:10:57 +0000234 # otherwise, extract tarball
235 else:
236 os.chdir(os.path.dirname(self.src_dir))
237 # Figure out local destination for tarball
238 tarball = os.path.join(self.src_dir, os.path.basename(base_tree))
239 utils.get_file(base_tree, tarball)
240 print 'Extracting kernel tarball:', tarball, '...'
mbligh53da18e2009-01-05 21:13:26 +0000241 utils.extract_tarball_to_dir(tarball, self.build_dir)
mblighfdbcaec2006-10-01 23:28:57 +0000242
243
jadmanski0afbb632008-06-06 21:10:57 +0000244 def extraversion(self, tag, append=1):
245 os.chdir(self.build_dir)
246 extraversion_sub = r's/^EXTRAVERSION =\s*\(.*\)/EXTRAVERSION = '
247 if append:
248 p = extraversion_sub + '\\1-%s/' % tag
249 else:
250 p = extraversion_sub + '-%s/' % tag
251 utils.system('mv Makefile Makefile.old')
252 utils.system('sed "%s" < Makefile.old > Makefile' % p)
mbligh72b88fc2006-12-16 18:41:35 +0000253
apwc7846102006-04-06 18:22:13 +0000254
mbligh1b3b3762008-09-25 02:46:34 +0000255 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000256 @tee_output_logdir_mark
257 def build(self, make_opts = '', logfile = '', extraversion='autotest'):
258 """build the kernel
apwc7846102006-04-06 18:22:13 +0000259
jadmanski0afbb632008-06-06 21:10:57 +0000260 make_opts
261 additional options to make, if any
262 """
263 os_dep.commands('gcc', 'make')
264 if logfile == '':
265 logfile = os.path.join(self.log_dir, 'kernel_build')
266 os.chdir(self.build_dir)
267 if extraversion:
268 self.extraversion(extraversion)
269 self.set_cross_cc()
270 # setup_config_file(config_file, config_overrides)
mbligh1e8858e2006-11-24 22:18:35 +0000271
jadmanski0afbb632008-06-06 21:10:57 +0000272 # Not needed on 2.6, but hard to tell -- handle failure
273 utils.system('make dep', ignore_status=True)
mbligh53da18e2009-01-05 21:13:26 +0000274 threads = 2 * utils.count_cpus()
jadmanski0afbb632008-06-06 21:10:57 +0000275 build_string = 'make -j %d %s %s' % (threads, make_opts,
276 self.build_target)
277 # eg make bzImage, or make zImage
278 print build_string
mbligh925e1b12008-06-12 17:48:38 +0000279 utils.system(build_string)
jadmanski0afbb632008-06-06 21:10:57 +0000280 if kernel_config.modules_needed('.config'):
281 utils.system('make -j %d modules' % (threads))
mblighf4c35322006-03-13 01:01:10 +0000282
jadmanski0afbb632008-06-06 21:10:57 +0000283 kernel_version = self.get_kernel_build_ver()
284 kernel_version = re.sub('-autotest', '', kernel_version)
285 self.logfile.write('BUILD VERSION: %s\n' % kernel_version)
mblighf4c35322006-03-13 01:01:10 +0000286
mbligh53da18e2009-01-05 21:13:26 +0000287 utils.force_copy(self.build_dir+'/System.map',
mbligh925e1b12008-06-12 17:48:38 +0000288 self.results_dir)
mbligh30f28c52007-10-11 18:35:35 +0000289
mbligh30f28c52007-10-11 18:35:35 +0000290
jadmanski0afbb632008-06-06 21:10:57 +0000291 def build_timed(self, threads, timefile = '/dev/null', make_opts = '',
292 output = '/dev/null'):
293 """time the bulding of the kernel"""
294 os.chdir(self.build_dir)
295 self.set_cross_cc()
296
297 self.clean(logged=False)
298 build_string = "/usr/bin/time -o %s make %s -j %s vmlinux" \
299 % (timefile, make_opts, threads)
300 build_string += ' > %s 2>&1' % output
301 print build_string
302 utils.system(build_string)
303
304 if (not os.path.isfile('vmlinux')):
305 errmsg = "no vmlinux found, kernel build failed"
306 raise error.TestError(errmsg)
307
308
mbligh1b3b3762008-09-25 02:46:34 +0000309 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000310 @tee_output_logdir_mark
311 def clean(self):
312 """make clean in the kernel tree"""
313 os.chdir(self.build_dir)
314 print "make clean"
315 utils.system('make clean > /dev/null 2> /dev/null')
316
317
mbligh1b3b3762008-09-25 02:46:34 +0000318 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000319 @tee_output_logdir_mark
320 def mkinitrd(self, version, image, system_map, initrd):
321 """Build kernel initrd image.
322 Try to use distro specific way to build initrd image.
323 Parameters:
324 version
325 new kernel version
326 image
327 new kernel image file
328 system_map
329 System.map file
330 initrd
331 initrd image file to build
332 """
mbligh53da18e2009-01-05 21:13:26 +0000333 vendor = utils.get_os_vendor()
mblighb8a14e32006-05-06 00:17:35 +0000334
jadmanski0afbb632008-06-06 21:10:57 +0000335 if os.path.isfile(initrd):
336 print "Existing %s file, will remove it." % initrd
337 os.remove(initrd)
mblighb8a14e32006-05-06 00:17:35 +0000338
jadmanski0afbb632008-06-06 21:10:57 +0000339 args = self.job.config_get('kernel.mkinitrd_extra_args')
mblighf4c35322006-03-13 01:01:10 +0000340
jadmanski0afbb632008-06-06 21:10:57 +0000341 # don't leak 'None' into mkinitrd command
342 if not args:
343 args = ''
mbligh50f42ea2006-09-30 22:22:21 +0000344
jadmanski0afbb632008-06-06 21:10:57 +0000345 if vendor in ['Red Hat', 'Fedora Core']:
346 utils.system('mkinitrd %s %s %s' % (args, initrd, version))
347 elif vendor in ['SUSE']:
jadmanskid524b0e2008-09-15 14:28:20 +0000348 utils.system('mkinitrd %s -k %s -i %s -M %s' %
349 (args, image, initrd, system_map))
jadmanski0afbb632008-06-06 21:10:57 +0000350 elif vendor in ['Debian', 'Ubuntu']:
351 if os.path.isfile('/usr/sbin/mkinitrd'):
352 cmd = '/usr/sbin/mkinitrd'
353 elif os.path.isfile('/usr/sbin/mkinitramfs'):
354 cmd = '/usr/sbin/mkinitramfs'
355 else:
356 raise error.TestError('No Debian initrd builder')
357 utils.system('%s %s -o %s %s' % (cmd, args, initrd, version))
358 else:
359 raise error.TestError('Unsupported vendor %s' % vendor)
mbligh72b88fc2006-12-16 18:41:35 +0000360
apwe43a30b2007-09-25 16:51:30 +0000361
jadmanski0afbb632008-06-06 21:10:57 +0000362 def set_build_image(self, image):
363 self.build_image = image
mbligh3d515d42007-11-09 17:00:36 +0000364
mbligh50f42ea2006-09-30 22:22:21 +0000365
mbligh1b3b3762008-09-25 02:46:34 +0000366 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000367 @tee_output_logdir_mark
368 def install(self, tag='autotest', prefix = '/'):
369 """make install in the kernel tree"""
mbligh50f42ea2006-09-30 22:22:21 +0000370
jadmanski0afbb632008-06-06 21:10:57 +0000371 # Record that we have installed the kernel, and
372 # the tag under which we installed it.
373 self.installed_as = tag
mbligh8baa2ea2006-12-17 23:01:24 +0000374
jadmanski0afbb632008-06-06 21:10:57 +0000375 os.chdir(self.build_dir)
mbligh8baa2ea2006-12-17 23:01:24 +0000376
jadmanski0afbb632008-06-06 21:10:57 +0000377 if not os.path.isdir(prefix):
378 os.mkdir(prefix)
379 self.boot_dir = os.path.join(prefix, 'boot')
380 if not os.path.isdir(self.boot_dir):
381 os.mkdir(self.boot_dir)
apw87c65c12007-09-27 17:19:37 +0000382
jadmanski0afbb632008-06-06 21:10:57 +0000383 if not self.build_image:
384 images = glob.glob('arch/*/boot/' + self.build_target)
385 if len(images):
386 self.build_image = images[0]
387 else:
388 self.build_image = self.build_target
apw87c65c12007-09-27 17:19:37 +0000389
jadmanski0afbb632008-06-06 21:10:57 +0000390 # remember installed files
391 self.vmlinux = self.boot_dir + '/vmlinux-' + tag
392 if (self.build_image != 'vmlinux'):
393 self.image = self.boot_dir + '/vmlinuz-' + tag
394 else:
395 self.image = self.vmlinux
396 self.system_map = self.boot_dir + '/System.map-' + tag
mbligh925e1b12008-06-12 17:48:38 +0000397 self.config_file = self.boot_dir + '/config-' + tag
jadmanski0afbb632008-06-06 21:10:57 +0000398 self.initrd = ''
mbligh72b88fc2006-12-16 18:41:35 +0000399
jadmanski0afbb632008-06-06 21:10:57 +0000400 # copy to boot dir
mbligh53da18e2009-01-05 21:13:26 +0000401 utils.force_copy('vmlinux', self.vmlinux)
jadmanski0afbb632008-06-06 21:10:57 +0000402 if (self.build_image != 'vmlinux'):
mbligh53da18e2009-01-05 21:13:26 +0000403 utils.force_copy(self.build_image, self.image)
404 utils.force_copy('System.map', self.system_map)
405 utils.force_copy('.config', self.config_file)
mbligh0ad65582006-10-06 04:16:36 +0000406
jadmanski0afbb632008-06-06 21:10:57 +0000407 if not kernel_config.modules_needed('.config'):
408 return
mbligha87116f2006-10-10 02:47:08 +0000409
jadmanski0afbb632008-06-06 21:10:57 +0000410 utils.system('make modules_install INSTALL_MOD_PATH=%s' % prefix)
411 if prefix == '/':
412 self.initrd = self.boot_dir + '/initrd-' + tag
413 self.mkinitrd(self.get_kernel_build_ver(), self.image,
414 self.system_map, self.initrd)
mbligha87116f2006-10-10 02:47:08 +0000415
mbligha87116f2006-10-10 02:47:08 +0000416
jadmanski0afbb632008-06-06 21:10:57 +0000417 def add_to_bootloader(self, tag='autotest', args=''):
418 """ add this kernel to bootloader, taking an
419 optional parameter of space separated parameters
420 e.g.: kernel.add_to_bootloader('mykernel', 'ro acpi=off')
421 """
mbligh6a1d4db2006-10-06 04:30:16 +0000422
jadmanski0afbb632008-06-06 21:10:57 +0000423 # remove existing entry if present
424 self.job.bootloader.remove_kernel(tag)
mbligh5925e962007-08-30 17:05:22 +0000425
jadmanski0afbb632008-06-06 21:10:57 +0000426 # pull the base argument set from the job config,
427 baseargs = self.job.config_get('boot.default_args')
428 if baseargs:
429 args = baseargs + " " + args
mbligh5925e962007-08-30 17:05:22 +0000430
jadmanski0afbb632008-06-06 21:10:57 +0000431 # otherwise populate from /proc/cmdline
432 # if not baseargs:
433 # baseargs = open('/proc/cmdline', 'r').readline().strip()
434 # NOTE: This is unnecessary, because boottool does it.
mbligha87116f2006-10-10 02:47:08 +0000435
jadmanski0afbb632008-06-06 21:10:57 +0000436 root = None
437 roots = [x for x in args.split() if x.startswith('root=')]
438 if roots:
439 root = re.sub('^root=', '', roots[0])
440 arglist = [x for x in args.split() if not x.startswith('root=')]
441 args = ' '.join(arglist)
mbligha87116f2006-10-10 02:47:08 +0000442
jadmanski0afbb632008-06-06 21:10:57 +0000443 # add the kernel entry
444 # add_kernel(image, title='autotest', initrd='')
445 self.job.bootloader.add_kernel(self.image, tag, self.initrd, \
446 args = args, root = root)
apwcbe32572006-11-28 10:00:23 +0000447
mbligha87116f2006-10-10 02:47:08 +0000448
jadmanski0afbb632008-06-06 21:10:57 +0000449 def get_kernel_build_arch(self, arch=None):
450 """
451 Work out the current kernel architecture (as a kernel arch)
452 """
453 if not arch:
mbligh53da18e2009-01-05 21:13:26 +0000454 arch = utils.get_current_kernel_arch()
jadmanski0afbb632008-06-06 21:10:57 +0000455 if re.match('i.86', arch):
456 return 'i386'
457 elif re.match('sun4u', arch):
458 return 'sparc64'
459 elif re.match('arm.*', arch):
460 return 'arm'
461 elif re.match('sa110', arch):
462 return 'arm'
463 elif re.match('s390x', arch):
464 return 's390'
465 elif re.match('parisc64', arch):
466 return 'parisc'
467 elif re.match('ppc.*', arch):
468 return 'powerpc'
469 elif re.match('mips.*', arch):
470 return 'mips'
471 else:
472 return arch
mbligh6a1d4db2006-10-06 04:30:16 +0000473
mbligh201aa892006-10-29 04:02:05 +0000474
jadmanski0afbb632008-06-06 21:10:57 +0000475 def get_kernel_build_release(self):
476 releasem = re.compile(r'.*UTS_RELEASE\s+"([^"]+)".*');
477 versionm = re.compile(r'.*UTS_VERSION\s+"([^"]+)".*');
mbligh548f29a2006-10-17 04:55:12 +0000478
jadmanski0afbb632008-06-06 21:10:57 +0000479 release = None
480 version = None
mbligh6a1d4db2006-10-06 04:30:16 +0000481
jadmanskid524b0e2008-09-15 14:28:20 +0000482 for f in [self.build_dir + "/include/linux/version.h",
483 self.build_dir + "/include/linux/utsrelease.h",
484 self.build_dir + "/include/linux/compile.h"]:
485 if os.path.exists(f):
486 fd = open(f, 'r')
jadmanski0afbb632008-06-06 21:10:57 +0000487 for line in fd.readlines():
488 m = releasem.match(line)
489 if m:
490 release = m.groups()[0]
491 m = versionm.match(line)
492 if m:
493 version = m.groups()[0]
494 fd.close()
mbligh237bed32007-09-05 13:05:57 +0000495
jadmanski0afbb632008-06-06 21:10:57 +0000496 return (release, version)
mbligh237bed32007-09-05 13:05:57 +0000497
mbligh237bed32007-09-05 13:05:57 +0000498
jadmanski0afbb632008-06-06 21:10:57 +0000499 def get_kernel_build_ident(self):
500 (release, version) = self.get_kernel_build_release()
mbligh237bed32007-09-05 13:05:57 +0000501
jadmanski0afbb632008-06-06 21:10:57 +0000502 if not release or not version:
503 raise error.JobError('kernel has no identity')
mbligh237bed32007-09-05 13:05:57 +0000504
jadmanski0afbb632008-06-06 21:10:57 +0000505 return release + '::' + version
mbligh237bed32007-09-05 13:05:57 +0000506
mbligh237bed32007-09-05 13:05:57 +0000507
jadmanski067b26c2008-09-25 19:46:56 +0000508 def boot(self, args='', ident=True):
jadmanski0afbb632008-06-06 21:10:57 +0000509 """ install and boot this kernel, do not care how
510 just make it happen.
511 """
mbligh237bed32007-09-05 13:05:57 +0000512
jadmanski0afbb632008-06-06 21:10:57 +0000513 # If we can check the kernel identity do so.
jadmanski067b26c2008-09-25 19:46:56 +0000514 expected_ident = self.get_kernel_build_ident()
jadmanski0afbb632008-06-06 21:10:57 +0000515 if ident:
516 when = int(time.time())
jadmanski0afbb632008-06-06 21:10:57 +0000517 args += " IDENT=%d" % (when)
jadmanski067b26c2008-09-25 19:46:56 +0000518 self.job.next_step_prepend(["job.end_reboot_and_verify", when,
519 expected_ident, self.subdir,
520 self.applied_patches])
521 else:
522 self.job.next_step_prepend(["job.end_reboot", self.subdir,
523 expected_ident, self.applied_patches])
mbligh237bed32007-09-05 13:05:57 +0000524
jadmanski0afbb632008-06-06 21:10:57 +0000525 # Check if the kernel has been installed, if not install
526 # as the default tag and boot that.
527 if not self.installed_as:
528 self.install()
mbligh237bed32007-09-05 13:05:57 +0000529
jadmanski0afbb632008-06-06 21:10:57 +0000530 # Boot the selected tag.
531 self.add_to_bootloader(args=args, tag=self.installed_as)
apw87c65c12007-09-27 17:19:37 +0000532
jadmanski0afbb632008-06-06 21:10:57 +0000533 # Boot it.
jadmanski02c0e452008-10-21 16:33:44 +0000534 self.job.start_reboot()
jadmanski0afbb632008-06-06 21:10:57 +0000535 self.job.reboot(tag=self.installed_as)
apw1b5dc362006-10-31 11:24:26 +0000536
apw1b5dc362006-10-31 11:24:26 +0000537
jadmanski0afbb632008-06-06 21:10:57 +0000538 def get_kernel_build_ver(self):
539 """Check Makefile and .config to return kernel version"""
540 version = patchlevel = sublevel = extraversion = localversion = ''
apw1b5dc362006-10-31 11:24:26 +0000541
jadmanski0afbb632008-06-06 21:10:57 +0000542 for line in open(self.build_dir + '/Makefile', 'r').readlines():
543 if line.startswith('VERSION'):
544 version = line[line.index('=') + 1:].strip()
545 if line.startswith('PATCHLEVEL'):
546 patchlevel = line[line.index('=') + 1:].strip()
547 if line.startswith('SUBLEVEL'):
548 sublevel = line[line.index('=') + 1:].strip()
549 if line.startswith('EXTRAVERSION'):
550 extraversion = line[line.index('=') + 1:].strip()
mblighe11f5fc2006-10-04 04:42:22 +0000551
jadmanski0afbb632008-06-06 21:10:57 +0000552 for line in open(self.build_dir + '/.config', 'r').readlines():
553 if line.startswith('CONFIG_LOCALVERSION='):
554 localversion = line.rstrip().split('"')[1]
mblighe11f5fc2006-10-04 04:42:22 +0000555
jadmanski0afbb632008-06-06 21:10:57 +0000556 return "%s.%s.%s%s%s" %(version, patchlevel, sublevel, extraversion, localversion)
mblighe11f5fc2006-10-04 04:42:22 +0000557
mblighfdbcaec2006-10-01 23:28:57 +0000558
jadmanski0afbb632008-06-06 21:10:57 +0000559 def set_build_target(self, build_target):
560 if build_target:
561 self.build_target = build_target
562 print 'BUILD TARGET: %s' % self.build_target
mblighfdbcaec2006-10-01 23:28:57 +0000563
mbligh8baa2ea2006-12-17 23:01:24 +0000564
jadmanski0afbb632008-06-06 21:10:57 +0000565 def set_cross_cc(self, target_arch=None, cross_compile=None,
566 build_target='bzImage'):
567 """Set up to cross-compile.
568 This is broken. We need to work out what the default
569 compile produces, and if not, THEN set the cross
570 compiler.
571 """
mbligh8baa2ea2006-12-17 23:01:24 +0000572
jadmanski0afbb632008-06-06 21:10:57 +0000573 if self.target_arch:
574 return
mblighcc2e6662006-09-14 01:24:07 +0000575
jadmanski0afbb632008-06-06 21:10:57 +0000576 # if someone has set build_target, don't clobber in set_cross_cc
577 # run set_build_target before calling set_cross_cc
578 if not self.build_target:
579 self.set_build_target(build_target)
mbligh678823f2006-12-07 18:49:00 +0000580
jadmanski0afbb632008-06-06 21:10:57 +0000581 # If no 'target_arch' given assume native compilation
mblighd876f452008-12-03 15:09:17 +0000582 if target_arch is None:
mbligh53da18e2009-01-05 21:13:26 +0000583 target_arch = utils.get_current_kernel_arch()
jadmanski0afbb632008-06-06 21:10:57 +0000584 if target_arch == 'ppc64':
585 if self.build_target == 'bzImage':
586 self.build_target = 'vmlinux'
mbligh72b88fc2006-12-16 18:41:35 +0000587
jadmanski0afbb632008-06-06 21:10:57 +0000588 if not cross_compile:
589 cross_compile = self.job.config_get('kernel.cross_cc')
mbligh678823f2006-12-07 18:49:00 +0000590
jadmanski0afbb632008-06-06 21:10:57 +0000591 if cross_compile:
592 os.environ['CROSS_COMPILE'] = cross_compile
593 else:
594 if os.environ.has_key('CROSS_COMPILE'):
595 del os.environ['CROSS_COMPILE']
mbligh678823f2006-12-07 18:49:00 +0000596
jadmanski0afbb632008-06-06 21:10:57 +0000597 return # HACK. Crap out for now.
mblighf4c35322006-03-13 01:01:10 +0000598
jadmanski0afbb632008-06-06 21:10:57 +0000599 # At this point I know what arch I *want* to build for
600 # but have no way of working out what arch the default
601 # compiler DOES build for.
mblighcc2e6662006-09-14 01:24:07 +0000602
mbligh925e1b12008-06-12 17:48:38 +0000603 def install_package(package):
604 raise NotImplementedError("I don't exist yet!")
mbligh72b88fc2006-12-16 18:41:35 +0000605
jadmanski0afbb632008-06-06 21:10:57 +0000606 if target_arch == 'ppc64':
607 install_package('ppc64-cross')
608 cross_compile = os.path.join(self.autodir, 'sources/ppc64-cross/bin')
mblighcc2e6662006-09-14 01:24:07 +0000609
jadmanski0afbb632008-06-06 21:10:57 +0000610 elif target_arch == 'x86_64':
611 install_package('x86_64-cross')
612 cross_compile = os.path.join(self.autodir, 'sources/x86_64-cross/bin')
mblighb8a14e32006-05-06 00:17:35 +0000613
jadmanski0afbb632008-06-06 21:10:57 +0000614 os.environ['ARCH'] = self.target_arch = target_arch
mbligh5970cf02006-08-06 15:39:22 +0000615
jadmanski0afbb632008-06-06 21:10:57 +0000616 self.cross_compile = cross_compile
617 if self.cross_compile:
618 os.environ['CROSS_COMPILE'] = self.cross_compile
mblighcc2e6662006-09-14 01:24:07 +0000619
mbligh72b88fc2006-12-16 18:41:35 +0000620
jadmanski0afbb632008-06-06 21:10:57 +0000621 def pickle_dump(self, filename):
622 """dump a pickle of ourself out to the specified filename
mblighc86b0b42006-07-28 17:35:28 +0000623
jadmanski0afbb632008-06-06 21:10:57 +0000624 we can't pickle the backreference to job (it contains fd's),
625 nor would we want to. Same for logfile (fd's).
626 """
627 temp = copy.copy(self)
628 temp.job = None
629 temp.logfile = None
630 pickle.dump(temp, open(filename, 'w'))
mbligh736adc92007-10-18 03:23:22 +0000631
632
jadmanski6ca37b62008-06-30 21:17:07 +0000633class rpm_kernel(object):
jadmanski0afbb632008-06-06 21:10:57 +0000634 """ Class for installing rpm kernel package
635 """
mbligh736adc92007-10-18 03:23:22 +0000636
jadmanski0afbb632008-06-06 21:10:57 +0000637 def __init__(self, job, rpm_package, subdir):
638 self.job = job
639 self.rpm_package = rpm_package
640 self.log_dir = os.path.join(subdir, 'debug')
641 self.subdir = os.path.basename(subdir)
642 if os.path.exists(self.log_dir):
643 utils.system('rm -rf ' + self.log_dir)
644 os.mkdir(self.log_dir)
645 self.installed_as = None
mbligh736adc92007-10-18 03:23:22 +0000646
647
mbligh1b3b3762008-09-25 02:46:34 +0000648 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000649 @tee_output_logdir_mark
650 def install(self, tag='autotest'):
651 self.installed_as = tag
mblighda0311e2007-10-25 16:03:33 +0000652
mbligh1b160a02009-05-21 01:27:10 +0000653 self.image = None
jadmanski0afbb632008-06-06 21:10:57 +0000654 self.initrd = ''
mbligh1b160a02009-05-21 01:27:10 +0000655 for rpm_pack in self.rpm_package:
656 rpm_name = utils.system_output('rpm -qp ' + rpm_pack)
mbligh736adc92007-10-18 03:23:22 +0000657
mbligh1b160a02009-05-21 01:27:10 +0000658 # install
659 utils.system('rpm -i --force ' + rpm_pack)
660
661 # get file list
662 files = utils.system_output('rpm -ql ' + rpm_name).splitlines()
663
664 # search for vmlinuz
665 for file in files:
666 if file.startswith('/boot/vmlinuz'):
667 self.full_version = file[len('/boot/vmlinuz-'):]
668 self.image = file
669 self.rpm_flavour = rpm_name.split('-')[1]
670
671 # get version and release number
672 self.version, self.release = utils.system_output(
673 'rpm --queryformat="%{VERSION}\\n%{RELEASE}\\n" -q '
674 + rpm_name).splitlines()[0:2]
675
676 # prefer /boot/kernel-version before /boot/kernel
677 if self.full_version:
678 break
679
680 # search for initrd
681 for file in files:
682 if file.startswith('/boot/initrd'):
683 self.initrd = file
684 # prefer /boot/initrd-version before /boot/initrd
685 if len(file) > len('/boot/initrd'):
686 break
687
688 if self.image == None:
689 errmsg = "specified rpm file(s) don't contain /boot/vmlinuz"
690 raise error.TestError(errmsg)
mbligh736adc92007-10-18 03:23:22 +0000691
692
jadmanski0afbb632008-06-06 21:10:57 +0000693 def add_to_bootloader(self, tag='autotest', args=''):
694 """ Add this kernel to bootloader
695 """
mbligh736adc92007-10-18 03:23:22 +0000696
jadmanski0afbb632008-06-06 21:10:57 +0000697 # remove existing entry if present
698 self.job.bootloader.remove_kernel(tag)
mbligh736adc92007-10-18 03:23:22 +0000699
jadmanski0afbb632008-06-06 21:10:57 +0000700 # pull the base argument set from the job config
701 baseargs = self.job.config_get('boot.default_args')
702 if baseargs:
703 args = baseargs + ' ' + args
mbligh736adc92007-10-18 03:23:22 +0000704
jadmanski0afbb632008-06-06 21:10:57 +0000705 # otherwise populate from /proc/cmdline
706 # if not baseargs:
707 # baseargs = open('/proc/cmdline', 'r').readline().strip()
708 # NOTE: This is unnecessary, because boottool does it.
mbligh736adc92007-10-18 03:23:22 +0000709
jadmanski0afbb632008-06-06 21:10:57 +0000710 root = None
711 roots = [x for x in args.split() if x.startswith('root=')]
712 if roots:
713 root = re.sub('^root=', '', roots[0])
714 arglist = [x for x in args.split() if not x.startswith('root=')]
715 args = ' '.join(arglist)
mbligh736adc92007-10-18 03:23:22 +0000716
jadmanski0afbb632008-06-06 21:10:57 +0000717 # add the kernel entry
mbligh9e6a4f12008-06-06 21:55:12 +0000718 self.job.bootloader.add_kernel(self.image, tag, self.initrd,
719 args = args, root = root)
mbligh10a24a72007-10-24 21:02:53 +0000720
721
jadmanski067b26c2008-09-25 19:46:56 +0000722 def boot(self, args='', ident=True):
jadmanski0afbb632008-06-06 21:10:57 +0000723 """ install and boot this kernel
724 """
mbligh73e82a32007-11-08 21:35:29 +0000725
jadmanski0afbb632008-06-06 21:10:57 +0000726 # Check if the kernel has been installed, if not install
727 # as the default tag and boot that.
728 if not self.installed_as:
729 self.install()
mblighda0311e2007-10-25 16:03:33 +0000730
jadmanski0afbb632008-06-06 21:10:57 +0000731 # If we can check the kernel identity do so.
mblighb1887c82009-03-12 00:25:48 +0000732 expected_ident = self.full_version
733 if not expected_ident:
734 expected_ident = '-'.join([self.version,
mbligh1b160a02009-05-21 01:27:10 +0000735 self.rpm_flavour,
mblighb1887c82009-03-12 00:25:48 +0000736 self.release])
jadmanski0afbb632008-06-06 21:10:57 +0000737 if ident:
738 when = int(time.time())
jadmanski0afbb632008-06-06 21:10:57 +0000739 args += " IDENT=%d" % (when)
jadmanski067b26c2008-09-25 19:46:56 +0000740 self.job.next_step_prepend(["job.end_reboot_and_verify",
741 when, expected_ident, None, 'rpm'])
742 else:
743 self.job.next_step_prepend(["job.end_reboot", None,
744 expected_ident, []])
mbligh10a24a72007-10-24 21:02:53 +0000745
jadmanski0afbb632008-06-06 21:10:57 +0000746 # Boot the selected tag.
747 self.add_to_bootloader(args=args, tag=self.installed_as)
mbligh10a24a72007-10-24 21:02:53 +0000748
jadmanski0afbb632008-06-06 21:10:57 +0000749 # Boot it.
jadmanski02c0e452008-10-21 16:33:44 +0000750 self.job.start_reboot()
jadmanski0afbb632008-06-06 21:10:57 +0000751 self.job.reboot(tag=self.installed_as)
mbligh6ee7ee02007-11-13 23:49:05 +0000752
753
mblighe7785cc2009-03-17 17:32:47 +0000754class rpm_kernel_suse(rpm_kernel):
755 """ Class for installing openSUSE/SLE rpm kernel package
756 """
757
758 def install(self):
759 # do not set the new kernel as the default one
760 os.environ['PBL_AUTOTEST'] = '1'
761
762 rpm_kernel.install(self, 'dummy')
763 self.installed_as = self.job.bootloader.get_title_for_kernel(self.image)
764 if not self.installed_as:
765 errmsg = "cannot find installed kernel in bootloader configuration"
766 raise error.TestError(errmsg)
767
768
769 def add_to_bootloader(self, tag='dummy', args=''):
770 """ Set parameters of this kernel in bootloader
771 """
772
773 # pull the base argument set from the job config
774 baseargs = self.job.config_get('boot.default_args')
775 if baseargs:
776 args = baseargs + ' ' + args
777
778 self.job.bootloader.add_args(tag, args)
779
780
781def rpm_kernel_vendor(job, rpm_package, subdir):
782 vendor = utils.get_os_vendor()
783 if vendor == "SUSE":
784 return rpm_kernel_suse(job, rpm_package, subdir)
785 else:
786 return rpm_kernel(job, rpm_package, subdir)
787
788
mbligh062ed152009-01-13 00:57:14 +0000789# just make the preprocessor a nop
790def _preprocess_path_dummy(path):
791 return path.strip()
792
793
mbligh6ee7ee02007-11-13 23:49:05 +0000794# pull in some optional site-specific path pre-processing
mbligh062ed152009-01-13 00:57:14 +0000795preprocess_path = utils.import_site_function(__file__,
796 "autotest_lib.client.bin.site_kernel", "preprocess_path",
797 _preprocess_path_dummy)
mbligh6ee7ee02007-11-13 23:49:05 +0000798
mblighc5ddfd12008-08-04 17:15:00 +0000799
mbligh6ee7ee02007-11-13 23:49:05 +0000800def auto_kernel(job, path, subdir, tmp_dir, build_dir, leave=False):
mbligh7aeda672009-01-30 00:35:59 +0000801 """
jadmanski0afbb632008-06-06 21:10:57 +0000802 Create a kernel object, dynamically selecting the appropriate class to use
803 based on the path provided.
804 """
mbligh1b160a02009-05-21 01:27:10 +0000805 kernel_paths = [preprocess_path(path)]
806 if kernel_paths[0].endswith('.list'):
807 # Fetch the list of packages to install
808 kernel_list = os.path.join(tmp_dir, 'kernel.list')
809 utils.get_file(kernel_paths[0], kernel_list)
810 kernel_paths = [p.strip() for p in open(kernel_list).readlines()]
mblighc5ddfd12008-08-04 17:15:00 +0000811
mbligh1b160a02009-05-21 01:27:10 +0000812 if kernel_paths[0].endswith('.rpm'):
813 rpm_paths = []
814 for kernel_path in kernel_paths:
815 if utils.is_url(kernel_path) or os.path.exists(kernel_path):
816 rpm_paths.append(kernel_path)
mblighc5ddfd12008-08-04 17:15:00 +0000817
mbligh1b160a02009-05-21 01:27:10 +0000818 else:
819 # Fetch the rpm into the job's packages directory and pass it to
820 # rpm_kernel
821 rpm_name = os.path.basename(kernel_path)
mbligh7aeda672009-01-30 00:35:59 +0000822
mbligh1b160a02009-05-21 01:27:10 +0000823 # If the preprocessed path (kernel_path) is only a name then
824 # search for the kernel in all the repositories, else fetch the
825 # kernel from that specific path.
826 job.pkgmgr.fetch_pkg(rpm_name, os.path.join(job.pkgdir, rpm_name),
827 repo_url=os.path.dirname(kernel_path))
828
829 rpm_paths.append(os.path.join(job.pkgdir, rpm_name))
830 return rpm_kernel_vendor(job, rpm_paths, subdir)
jadmanski0afbb632008-06-06 21:10:57 +0000831 else:
mbligh1b160a02009-05-21 01:27:10 +0000832 if len(kernel_paths) > 1:
833 raise error.TestError("don't know what to do with more than one non-rpm kernel file")
834 return kernel(job,kernel_paths[0], subdir, tmp_dir, build_dir, leave)