blob: 5087126e387c8ad0b1d55530e08a45e7fec9310a [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
jadmanski6ca37b62008-06-30 21:17:07 +000024class kernel(object):
jadmanski0afbb632008-06-06 21:10:57 +000025 """ Class for compiling kernels.
mblighc86b0b42006-07-28 17:35:28 +000026
jadmanski0afbb632008-06-06 21:10:57 +000027 Data for the object includes the src files
28 used to create the kernel, patches applied, config (base + changes),
29 the build directory itself, and logged output
mblighc86b0b42006-07-28 17:35:28 +000030
jadmanski0afbb632008-06-06 21:10:57 +000031 Properties:
32 job
33 Backpointer to the job object we're part of
34 autodir
35 Path to the top level autotest dir (/usr/local/autotest)
36 src_dir
37 <tmp_dir>/src/
38 build_dir
39 <tmp_dir>/linux/
40 config_dir
41 <results_dir>/config/
42 log_dir
43 <results_dir>/debug/
44 results_dir
45 <results_dir>/results/
46 """
mblighc86b0b42006-07-28 17:35:28 +000047
jadmanski0afbb632008-06-06 21:10:57 +000048 autodir = ''
mbligh8baa2ea2006-12-17 23:01:24 +000049
mbligh925e1b12008-06-12 17:48:38 +000050 def __init__(self, job, base_tree, subdir, tmp_dir, build_dir, leave=False):
jadmanski0afbb632008-06-06 21:10:57 +000051 """Initialize the kernel build environment
mblighc86b0b42006-07-28 17:35:28 +000052
jadmanski0afbb632008-06-06 21:10:57 +000053 job
54 which job this build is part of
55 base_tree
56 base kernel tree. Can be one of the following:
57 1. A local tarball
58 2. A URL to a tarball
59 3. A local directory (will symlink it)
60 4. A shorthand expandable (eg '2.6.11-git3')
61 subdir
62 subdir in the results directory (eg "build")
63 (holds config/, debug/, results/)
64 tmp_dir
mbligh72b88fc2006-12-16 18:41:35 +000065
jadmanski0afbb632008-06-06 21:10:57 +000066 leave
67 Boolean, whether to leave existing tmpdir or not
68 """
69 self.job = job
70 self.autodir = job.autodir
mblighf4c35322006-03-13 01:01:10 +000071
jadmanski0afbb632008-06-06 21:10:57 +000072 self.src_dir = os.path.join(tmp_dir, 'src')
73 self.build_dir = os.path.join(tmp_dir, build_dir)
74 # created by get_kernel_tree
75 self.config_dir = os.path.join(subdir, 'config')
76 self.log_dir = os.path.join(subdir, 'debug')
77 self.results_dir = os.path.join(subdir, 'results')
78 self.subdir = os.path.basename(subdir)
mbligh1e8858e2006-11-24 22:18:35 +000079
jadmanski0afbb632008-06-06 21:10:57 +000080 self.installed_as = None
apw87c65c12007-09-27 17:19:37 +000081
jadmanski0afbb632008-06-06 21:10:57 +000082 if not leave:
83 if os.path.isdir(self.src_dir):
84 utils.system('rm -rf ' + self.src_dir)
85 if os.path.isdir(self.build_dir):
86 utils.system('rm -rf ' + self.build_dir)
mbligh1e8858e2006-11-24 22:18:35 +000087
jadmanski0afbb632008-06-06 21:10:57 +000088 if not os.path.exists(self.src_dir):
89 os.mkdir(self.src_dir)
90 for path in [self.config_dir, self.log_dir, self.results_dir]:
91 if os.path.exists(path):
92 utils.system('rm -rf ' + path)
93 os.mkdir(path)
mblighf4c35322006-03-13 01:01:10 +000094
jadmanski0afbb632008-06-06 21:10:57 +000095 logpath = os.path.join(self.log_dir, 'build_log')
96 self.logfile = open(logpath, 'w+')
97 self.applied_patches = []
mbligh4426de02006-10-10 07:18:28 +000098
jadmanski0afbb632008-06-06 21:10:57 +000099 self.target_arch = None
100 self.build_target = 'bzImage'
101 self.build_image = None
mblighfdbcaec2006-10-01 23:28:57 +0000102
mbligh53da18e2009-01-05 21:13:26 +0000103 arch = utils.get_current_kernel_arch()
jadmanski0afbb632008-06-06 21:10:57 +0000104 if arch == 's390' or arch == 's390x':
105 self.build_target = 'image'
106 elif arch == 'ia64':
107 self.build_target = 'all'
108 self.build_image = 'vmlinux.gz'
mblighcac347a2007-06-02 17:21:48 +0000109
mbligh925e1b12008-06-12 17:48:38 +0000110 if not leave:
111 self.logfile.write('BASE: %s\n' % base_tree)
mbligh534015f2006-09-15 03:28:56 +0000112
mbligh925e1b12008-06-12 17:48:38 +0000113 # Where we have direct version hint record that
114 # for later configuration selection.
115 shorthand = re.compile(r'^\d+\.\d+\.\d+')
116 if shorthand.match(base_tree):
117 self.base_tree_version = base_tree
118 else:
119 self.base_tree_version = None
apw2366d992007-03-12 20:35:57 +0000120
mbligh925e1b12008-06-12 17:48:38 +0000121 # Actually extract the tree. Make sure we know it occured
122 self.extract(base_tree)
apw040dcaa2007-11-21 19:36:55 +0000123
apw7bae90e2008-03-05 12:18:11 +0000124
jadmanski0afbb632008-06-06 21:10:57 +0000125 def kernelexpand(self, kernel):
126 # If we have something like a path, just use it as it is
127 if '/' in kernel:
128 return [kernel]
apw7bae90e2008-03-05 12:18:11 +0000129
jadmanski0afbb632008-06-06 21:10:57 +0000130 # Find the configured mirror list.
131 mirrors = self.job.config_get('mirror.mirrors')
132 if not mirrors:
133 # LEGACY: convert the kernel.org mirror
134 mirror = self.job.config_get('mirror.ftp_kernel_org')
135 if mirror:
136 korg = 'http://www.kernel.org/pub/linux/kernel'
137 mirrors = [
138 [ korg + '/v2.6', mirror + '/v2.6' ],
mbligh9e6a4f12008-06-06 21:55:12 +0000139 [ korg + '/people/akpm/patches/2.6', mirror + '/akpm' ],
140 [ korg + '/people/mbligh', mirror + '/mbligh' ],
jadmanski0afbb632008-06-06 21:10:57 +0000141 ]
apw7bae90e2008-03-05 12:18:11 +0000142
jadmanski0afbb632008-06-06 21:10:57 +0000143 patches = kernelexpand.expand_classic(kernel, mirrors)
144 print patches
apw7bae90e2008-03-05 12:18:11 +0000145
jadmanski0afbb632008-06-06 21:10:57 +0000146 return patches
apw7bae90e2008-03-05 12:18:11 +0000147
mblighf4c35322006-03-13 01:01:10 +0000148
mbligh1b3b3762008-09-25 02:46:34 +0000149 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000150 @tee_output_logdir_mark
151 def extract(self, base_tree):
152 if os.path.exists(base_tree):
153 self.get_kernel_tree(base_tree)
154 else:
155 base_components = self.kernelexpand(base_tree)
156 print 'kernelexpand: '
157 print base_components
158 self.get_kernel_tree(base_components.pop(0))
159 if base_components: # apply remaining patches
160 self.patch(*base_components)
mblighf4c35322006-03-13 01:01:10 +0000161
mblighf4c35322006-03-13 01:01:10 +0000162
mbligh1b3b3762008-09-25 02:46:34 +0000163 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000164 @tee_output_logdir_mark
165 def patch(self, *patches):
166 """Apply a list of patches (in order)"""
167 if not patches:
168 return
169 print 'Applying patches: ', patches
170 self.apply_patches(self.get_patches(patches))
mblighf4c35322006-03-13 01:01:10 +0000171
mblighf4c35322006-03-13 01:01:10 +0000172
mbligh1b3b3762008-09-25 02:46:34 +0000173 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000174 @tee_output_logdir_mark
mblighb3400e02008-11-06 15:44:25 +0000175 def config(self, config_file = '', config_list = None, defconfig = False, make = None):
jadmanski0afbb632008-06-06 21:10:57 +0000176 self.set_cross_cc()
177 config = kernel_config.kernel_config(self.job, self.build_dir,
178 self.config_dir, config_file, config_list,
mblighb3400e02008-11-06 15:44:25 +0000179 defconfig, self.base_tree_version, make)
mblighf4c35322006-03-13 01:01:10 +0000180
mblighf4c35322006-03-13 01:01:10 +0000181
jadmanski0afbb632008-06-06 21:10:57 +0000182 def get_patches(self, patches):
183 """fetch the patches to the local src_dir"""
184 local_patches = []
185 for patch in patches:
mbligh1c9b1d22008-06-12 17:46:12 +0000186 dest = os.path.join(self.src_dir, os.path.basename(patch))
jadmanski0afbb632008-06-06 21:10:57 +0000187 # FIXME: this isn't unique. Append something to it
188 # like wget does if it's not there?
jadmanskie3f2f712008-06-12 17:54:56 +0000189 print "get_file %s %s %s %s" % (patch, dest, self.src_dir,
190 os.path.basename(patch))
jadmanski0afbb632008-06-06 21:10:57 +0000191 utils.get_file(patch, dest)
192 # probably safer to use the command, not python library
193 md5sum = utils.system_output('md5sum ' + dest).split()[0]
194 local_patches.append((patch, dest, md5sum))
195 return local_patches
mbligh72b88fc2006-12-16 18:41:35 +0000196
mblighf4c35322006-03-13 01:01:10 +0000197
jadmanski0afbb632008-06-06 21:10:57 +0000198 def apply_patches(self, local_patches):
199 """apply the list of patches, in order"""
200 builddir = self.build_dir
201 os.chdir(builddir)
mbligh72b88fc2006-12-16 18:41:35 +0000202
jadmanski0afbb632008-06-06 21:10:57 +0000203 if not local_patches:
204 return None
205 for (spec, local, md5sum) in local_patches:
206 if local.endswith('.bz2') or local.endswith('.gz'):
207 ref = spec
208 else:
mbligh53da18e2009-01-05 21:13:26 +0000209 ref = utils.force_copy(local, self.results_dir)
jadmanski0afbb632008-06-06 21:10:57 +0000210 ref = self.job.relative_path(ref)
211 patch_id = "%s %s %s" % (spec, ref, md5sum)
212 log = "PATCH: " + patch_id + "\n"
213 print log
mbligh53da18e2009-01-05 21:13:26 +0000214 utils.cat_file_to_cmd(local, 'patch -p1 > /dev/null')
jadmanski0afbb632008-06-06 21:10:57 +0000215 self.logfile.write(log)
216 self.applied_patches.append(patch_id)
mbligh72b88fc2006-12-16 18:41:35 +0000217
mblighf4c35322006-03-13 01:01:10 +0000218
jadmanski0afbb632008-06-06 21:10:57 +0000219 def get_kernel_tree(self, base_tree):
220 """Extract/link base_tree to self.build_dir"""
mbligh5970cf02006-08-06 15:39:22 +0000221
jadmanski0afbb632008-06-06 21:10:57 +0000222 # if base_tree is a dir, assume uncompressed kernel
223 if os.path.isdir(base_tree):
224 print 'Symlinking existing kernel source'
225 os.symlink(base_tree, self.build_dir)
mblighf4c35322006-03-13 01:01:10 +0000226
jadmanski0afbb632008-06-06 21:10:57 +0000227 # otherwise, extract tarball
228 else:
229 os.chdir(os.path.dirname(self.src_dir))
230 # Figure out local destination for tarball
231 tarball = os.path.join(self.src_dir, os.path.basename(base_tree))
232 utils.get_file(base_tree, tarball)
233 print 'Extracting kernel tarball:', tarball, '...'
mbligh53da18e2009-01-05 21:13:26 +0000234 utils.extract_tarball_to_dir(tarball, self.build_dir)
mblighfdbcaec2006-10-01 23:28:57 +0000235
236
jadmanski0afbb632008-06-06 21:10:57 +0000237 def extraversion(self, tag, append=1):
238 os.chdir(self.build_dir)
239 extraversion_sub = r's/^EXTRAVERSION =\s*\(.*\)/EXTRAVERSION = '
240 if append:
241 p = extraversion_sub + '\\1-%s/' % tag
242 else:
243 p = extraversion_sub + '-%s/' % tag
244 utils.system('mv Makefile Makefile.old')
245 utils.system('sed "%s" < Makefile.old > Makefile' % p)
mbligh72b88fc2006-12-16 18:41:35 +0000246
apwc7846102006-04-06 18:22:13 +0000247
mbligh1b3b3762008-09-25 02:46:34 +0000248 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000249 @tee_output_logdir_mark
250 def build(self, make_opts = '', logfile = '', extraversion='autotest'):
251 """build the kernel
apwc7846102006-04-06 18:22:13 +0000252
jadmanski0afbb632008-06-06 21:10:57 +0000253 make_opts
254 additional options to make, if any
255 """
256 os_dep.commands('gcc', 'make')
257 if logfile == '':
258 logfile = os.path.join(self.log_dir, 'kernel_build')
259 os.chdir(self.build_dir)
260 if extraversion:
261 self.extraversion(extraversion)
262 self.set_cross_cc()
263 # setup_config_file(config_file, config_overrides)
mbligh1e8858e2006-11-24 22:18:35 +0000264
jadmanski0afbb632008-06-06 21:10:57 +0000265 # Not needed on 2.6, but hard to tell -- handle failure
266 utils.system('make dep', ignore_status=True)
mbligh53da18e2009-01-05 21:13:26 +0000267 threads = 2 * utils.count_cpus()
jadmanski0afbb632008-06-06 21:10:57 +0000268 build_string = 'make -j %d %s %s' % (threads, make_opts,
269 self.build_target)
270 # eg make bzImage, or make zImage
271 print build_string
mbligh925e1b12008-06-12 17:48:38 +0000272 utils.system(build_string)
jadmanski0afbb632008-06-06 21:10:57 +0000273 if kernel_config.modules_needed('.config'):
274 utils.system('make -j %d modules' % (threads))
mblighf4c35322006-03-13 01:01:10 +0000275
jadmanski0afbb632008-06-06 21:10:57 +0000276 kernel_version = self.get_kernel_build_ver()
277 kernel_version = re.sub('-autotest', '', kernel_version)
278 self.logfile.write('BUILD VERSION: %s\n' % kernel_version)
mblighf4c35322006-03-13 01:01:10 +0000279
mbligh53da18e2009-01-05 21:13:26 +0000280 utils.force_copy(self.build_dir+'/System.map',
mbligh925e1b12008-06-12 17:48:38 +0000281 self.results_dir)
mbligh30f28c52007-10-11 18:35:35 +0000282
mbligh30f28c52007-10-11 18:35:35 +0000283
jadmanski0afbb632008-06-06 21:10:57 +0000284 def build_timed(self, threads, timefile = '/dev/null', make_opts = '',
285 output = '/dev/null'):
286 """time the bulding of the kernel"""
287 os.chdir(self.build_dir)
288 self.set_cross_cc()
289
290 self.clean(logged=False)
291 build_string = "/usr/bin/time -o %s make %s -j %s vmlinux" \
292 % (timefile, make_opts, threads)
293 build_string += ' > %s 2>&1' % output
294 print build_string
295 utils.system(build_string)
296
297 if (not os.path.isfile('vmlinux')):
298 errmsg = "no vmlinux found, kernel build failed"
299 raise error.TestError(errmsg)
300
301
mbligh1b3b3762008-09-25 02:46:34 +0000302 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000303 @tee_output_logdir_mark
304 def clean(self):
305 """make clean in the kernel tree"""
306 os.chdir(self.build_dir)
307 print "make clean"
308 utils.system('make clean > /dev/null 2> /dev/null')
309
310
mbligh1b3b3762008-09-25 02:46:34 +0000311 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000312 @tee_output_logdir_mark
313 def mkinitrd(self, version, image, system_map, initrd):
314 """Build kernel initrd image.
315 Try to use distro specific way to build initrd image.
316 Parameters:
317 version
318 new kernel version
319 image
320 new kernel image file
321 system_map
322 System.map file
323 initrd
324 initrd image file to build
325 """
mbligh53da18e2009-01-05 21:13:26 +0000326 vendor = utils.get_os_vendor()
mblighb8a14e32006-05-06 00:17:35 +0000327
jadmanski0afbb632008-06-06 21:10:57 +0000328 if os.path.isfile(initrd):
329 print "Existing %s file, will remove it." % initrd
330 os.remove(initrd)
mblighb8a14e32006-05-06 00:17:35 +0000331
jadmanski0afbb632008-06-06 21:10:57 +0000332 args = self.job.config_get('kernel.mkinitrd_extra_args')
mblighf4c35322006-03-13 01:01:10 +0000333
jadmanski0afbb632008-06-06 21:10:57 +0000334 # don't leak 'None' into mkinitrd command
335 if not args:
336 args = ''
mbligh50f42ea2006-09-30 22:22:21 +0000337
jadmanski0afbb632008-06-06 21:10:57 +0000338 if vendor in ['Red Hat', 'Fedora Core']:
339 utils.system('mkinitrd %s %s %s' % (args, initrd, version))
340 elif vendor in ['SUSE']:
jadmanskid524b0e2008-09-15 14:28:20 +0000341 utils.system('mkinitrd %s -k %s -i %s -M %s' %
342 (args, image, initrd, system_map))
jadmanski0afbb632008-06-06 21:10:57 +0000343 elif vendor in ['Debian', 'Ubuntu']:
344 if os.path.isfile('/usr/sbin/mkinitrd'):
345 cmd = '/usr/sbin/mkinitrd'
346 elif os.path.isfile('/usr/sbin/mkinitramfs'):
347 cmd = '/usr/sbin/mkinitramfs'
348 else:
349 raise error.TestError('No Debian initrd builder')
350 utils.system('%s %s -o %s %s' % (cmd, args, initrd, version))
351 else:
352 raise error.TestError('Unsupported vendor %s' % vendor)
mbligh72b88fc2006-12-16 18:41:35 +0000353
apwe43a30b2007-09-25 16:51:30 +0000354
jadmanski0afbb632008-06-06 21:10:57 +0000355 def set_build_image(self, image):
356 self.build_image = image
mbligh3d515d42007-11-09 17:00:36 +0000357
mbligh50f42ea2006-09-30 22:22:21 +0000358
mbligh1b3b3762008-09-25 02:46:34 +0000359 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000360 @tee_output_logdir_mark
361 def install(self, tag='autotest', prefix = '/'):
362 """make install in the kernel tree"""
mbligh50f42ea2006-09-30 22:22:21 +0000363
jadmanski0afbb632008-06-06 21:10:57 +0000364 # Record that we have installed the kernel, and
365 # the tag under which we installed it.
366 self.installed_as = tag
mbligh8baa2ea2006-12-17 23:01:24 +0000367
jadmanski0afbb632008-06-06 21:10:57 +0000368 os.chdir(self.build_dir)
mbligh8baa2ea2006-12-17 23:01:24 +0000369
jadmanski0afbb632008-06-06 21:10:57 +0000370 if not os.path.isdir(prefix):
371 os.mkdir(prefix)
372 self.boot_dir = os.path.join(prefix, 'boot')
373 if not os.path.isdir(self.boot_dir):
374 os.mkdir(self.boot_dir)
apw87c65c12007-09-27 17:19:37 +0000375
jadmanski0afbb632008-06-06 21:10:57 +0000376 if not self.build_image:
377 images = glob.glob('arch/*/boot/' + self.build_target)
378 if len(images):
379 self.build_image = images[0]
380 else:
381 self.build_image = self.build_target
apw87c65c12007-09-27 17:19:37 +0000382
jadmanski0afbb632008-06-06 21:10:57 +0000383 # remember installed files
384 self.vmlinux = self.boot_dir + '/vmlinux-' + tag
385 if (self.build_image != 'vmlinux'):
386 self.image = self.boot_dir + '/vmlinuz-' + tag
387 else:
388 self.image = self.vmlinux
389 self.system_map = self.boot_dir + '/System.map-' + tag
mbligh925e1b12008-06-12 17:48:38 +0000390 self.config_file = self.boot_dir + '/config-' + tag
jadmanski0afbb632008-06-06 21:10:57 +0000391 self.initrd = ''
mbligh72b88fc2006-12-16 18:41:35 +0000392
jadmanski0afbb632008-06-06 21:10:57 +0000393 # copy to boot dir
mbligh53da18e2009-01-05 21:13:26 +0000394 utils.force_copy('vmlinux', self.vmlinux)
jadmanski0afbb632008-06-06 21:10:57 +0000395 if (self.build_image != 'vmlinux'):
mbligh53da18e2009-01-05 21:13:26 +0000396 utils.force_copy(self.build_image, self.image)
397 utils.force_copy('System.map', self.system_map)
398 utils.force_copy('.config', self.config_file)
mbligh0ad65582006-10-06 04:16:36 +0000399
jadmanski0afbb632008-06-06 21:10:57 +0000400 if not kernel_config.modules_needed('.config'):
401 return
mbligha87116f2006-10-10 02:47:08 +0000402
jadmanski0afbb632008-06-06 21:10:57 +0000403 utils.system('make modules_install INSTALL_MOD_PATH=%s' % prefix)
404 if prefix == '/':
405 self.initrd = self.boot_dir + '/initrd-' + tag
406 self.mkinitrd(self.get_kernel_build_ver(), self.image,
407 self.system_map, self.initrd)
mbligha87116f2006-10-10 02:47:08 +0000408
mbligha87116f2006-10-10 02:47:08 +0000409
jadmanski0afbb632008-06-06 21:10:57 +0000410 def add_to_bootloader(self, tag='autotest', args=''):
411 """ add this kernel to bootloader, taking an
412 optional parameter of space separated parameters
413 e.g.: kernel.add_to_bootloader('mykernel', 'ro acpi=off')
414 """
mbligh6a1d4db2006-10-06 04:30:16 +0000415
jadmanski0afbb632008-06-06 21:10:57 +0000416 # remove existing entry if present
417 self.job.bootloader.remove_kernel(tag)
mbligh5925e962007-08-30 17:05:22 +0000418
jadmanski0afbb632008-06-06 21:10:57 +0000419 # pull the base argument set from the job config,
420 baseargs = self.job.config_get('boot.default_args')
421 if baseargs:
422 args = baseargs + " " + args
mbligh5925e962007-08-30 17:05:22 +0000423
jadmanski0afbb632008-06-06 21:10:57 +0000424 # otherwise populate from /proc/cmdline
425 # if not baseargs:
426 # baseargs = open('/proc/cmdline', 'r').readline().strip()
427 # NOTE: This is unnecessary, because boottool does it.
mbligha87116f2006-10-10 02:47:08 +0000428
jadmanski0afbb632008-06-06 21:10:57 +0000429 root = None
430 roots = [x for x in args.split() if x.startswith('root=')]
431 if roots:
432 root = re.sub('^root=', '', roots[0])
433 arglist = [x for x in args.split() if not x.startswith('root=')]
434 args = ' '.join(arglist)
mbligha87116f2006-10-10 02:47:08 +0000435
jadmanski0afbb632008-06-06 21:10:57 +0000436 # add the kernel entry
mblighc2ebea02009-10-02 00:02:33 +0000437 self.job.bootloader.add_kernel(self.image, tag, initrd=self.initrd,
438 args=args, root=root)
apwcbe32572006-11-28 10:00:23 +0000439
mbligha87116f2006-10-10 02:47:08 +0000440
jadmanski0afbb632008-06-06 21:10:57 +0000441 def get_kernel_build_arch(self, arch=None):
442 """
443 Work out the current kernel architecture (as a kernel arch)
444 """
445 if not arch:
mbligh53da18e2009-01-05 21:13:26 +0000446 arch = utils.get_current_kernel_arch()
jadmanski0afbb632008-06-06 21:10:57 +0000447 if re.match('i.86', arch):
448 return 'i386'
449 elif re.match('sun4u', arch):
450 return 'sparc64'
451 elif re.match('arm.*', arch):
452 return 'arm'
453 elif re.match('sa110', arch):
454 return 'arm'
455 elif re.match('s390x', arch):
456 return 's390'
457 elif re.match('parisc64', arch):
458 return 'parisc'
459 elif re.match('ppc.*', arch):
460 return 'powerpc'
461 elif re.match('mips.*', arch):
462 return 'mips'
463 else:
464 return arch
mbligh6a1d4db2006-10-06 04:30:16 +0000465
mbligh201aa892006-10-29 04:02:05 +0000466
jadmanski0afbb632008-06-06 21:10:57 +0000467 def get_kernel_build_release(self):
468 releasem = re.compile(r'.*UTS_RELEASE\s+"([^"]+)".*');
469 versionm = re.compile(r'.*UTS_VERSION\s+"([^"]+)".*');
mbligh548f29a2006-10-17 04:55:12 +0000470
jadmanski0afbb632008-06-06 21:10:57 +0000471 release = None
472 version = None
mbligh6a1d4db2006-10-06 04:30:16 +0000473
jadmanskid524b0e2008-09-15 14:28:20 +0000474 for f in [self.build_dir + "/include/linux/version.h",
475 self.build_dir + "/include/linux/utsrelease.h",
mbligh508cbf62009-11-06 03:01:50 +0000476 self.build_dir + "/include/linux/compile.h",
477 self.build_dir + "/include/generated/utsrelease.h",
478 self.build_dir + "/include/generated/compile.h"]:
jadmanskid524b0e2008-09-15 14:28:20 +0000479 if os.path.exists(f):
480 fd = open(f, 'r')
jadmanski0afbb632008-06-06 21:10:57 +0000481 for line in fd.readlines():
482 m = releasem.match(line)
483 if m:
484 release = m.groups()[0]
485 m = versionm.match(line)
486 if m:
487 version = m.groups()[0]
488 fd.close()
mbligh237bed32007-09-05 13:05:57 +0000489
jadmanski0afbb632008-06-06 21:10:57 +0000490 return (release, version)
mbligh237bed32007-09-05 13:05:57 +0000491
mbligh237bed32007-09-05 13:05:57 +0000492
jadmanski0afbb632008-06-06 21:10:57 +0000493 def get_kernel_build_ident(self):
494 (release, version) = self.get_kernel_build_release()
mbligh237bed32007-09-05 13:05:57 +0000495
jadmanski0afbb632008-06-06 21:10:57 +0000496 if not release or not version:
497 raise error.JobError('kernel has no identity')
mbligh237bed32007-09-05 13:05:57 +0000498
jadmanski0afbb632008-06-06 21:10:57 +0000499 return release + '::' + version
mbligh237bed32007-09-05 13:05:57 +0000500
mbligh237bed32007-09-05 13:05:57 +0000501
jadmanski067b26c2008-09-25 19:46:56 +0000502 def boot(self, args='', ident=True):
jadmanski0afbb632008-06-06 21:10:57 +0000503 """ install and boot this kernel, do not care how
504 just make it happen.
505 """
mbligh237bed32007-09-05 13:05:57 +0000506
jadmanski0afbb632008-06-06 21:10:57 +0000507 # If we can check the kernel identity do so.
jadmanski067b26c2008-09-25 19:46:56 +0000508 expected_ident = self.get_kernel_build_ident()
jadmanski0afbb632008-06-06 21:10:57 +0000509 if ident:
510 when = int(time.time())
jadmanski0afbb632008-06-06 21:10:57 +0000511 args += " IDENT=%d" % (when)
jadmanski067b26c2008-09-25 19:46:56 +0000512 self.job.next_step_prepend(["job.end_reboot_and_verify", when,
513 expected_ident, self.subdir,
514 self.applied_patches])
515 else:
516 self.job.next_step_prepend(["job.end_reboot", self.subdir,
517 expected_ident, self.applied_patches])
mbligh237bed32007-09-05 13:05:57 +0000518
jadmanski0afbb632008-06-06 21:10:57 +0000519 # Check if the kernel has been installed, if not install
520 # as the default tag and boot that.
521 if not self.installed_as:
522 self.install()
mbligh237bed32007-09-05 13:05:57 +0000523
jadmanski0afbb632008-06-06 21:10:57 +0000524 # Boot the selected tag.
525 self.add_to_bootloader(args=args, tag=self.installed_as)
apw87c65c12007-09-27 17:19:37 +0000526
jadmanski0afbb632008-06-06 21:10:57 +0000527 # Boot it.
jadmanski02c0e452008-10-21 16:33:44 +0000528 self.job.start_reboot()
jadmanski0afbb632008-06-06 21:10:57 +0000529 self.job.reboot(tag=self.installed_as)
apw1b5dc362006-10-31 11:24:26 +0000530
apw1b5dc362006-10-31 11:24:26 +0000531
jadmanski0afbb632008-06-06 21:10:57 +0000532 def get_kernel_build_ver(self):
533 """Check Makefile and .config to return kernel version"""
534 version = patchlevel = sublevel = extraversion = localversion = ''
apw1b5dc362006-10-31 11:24:26 +0000535
jadmanski0afbb632008-06-06 21:10:57 +0000536 for line in open(self.build_dir + '/Makefile', 'r').readlines():
537 if line.startswith('VERSION'):
538 version = line[line.index('=') + 1:].strip()
539 if line.startswith('PATCHLEVEL'):
540 patchlevel = line[line.index('=') + 1:].strip()
541 if line.startswith('SUBLEVEL'):
542 sublevel = line[line.index('=') + 1:].strip()
543 if line.startswith('EXTRAVERSION'):
544 extraversion = line[line.index('=') + 1:].strip()
mblighe11f5fc2006-10-04 04:42:22 +0000545
jadmanski0afbb632008-06-06 21:10:57 +0000546 for line in open(self.build_dir + '/.config', 'r').readlines():
547 if line.startswith('CONFIG_LOCALVERSION='):
548 localversion = line.rstrip().split('"')[1]
mblighe11f5fc2006-10-04 04:42:22 +0000549
jadmanski0afbb632008-06-06 21:10:57 +0000550 return "%s.%s.%s%s%s" %(version, patchlevel, sublevel, extraversion, localversion)
mblighe11f5fc2006-10-04 04:42:22 +0000551
mblighfdbcaec2006-10-01 23:28:57 +0000552
jadmanski0afbb632008-06-06 21:10:57 +0000553 def set_build_target(self, build_target):
554 if build_target:
555 self.build_target = build_target
556 print 'BUILD TARGET: %s' % self.build_target
mblighfdbcaec2006-10-01 23:28:57 +0000557
mbligh8baa2ea2006-12-17 23:01:24 +0000558
jadmanski0afbb632008-06-06 21:10:57 +0000559 def set_cross_cc(self, target_arch=None, cross_compile=None,
560 build_target='bzImage'):
561 """Set up to cross-compile.
562 This is broken. We need to work out what the default
563 compile produces, and if not, THEN set the cross
564 compiler.
565 """
mbligh8baa2ea2006-12-17 23:01:24 +0000566
jadmanski0afbb632008-06-06 21:10:57 +0000567 if self.target_arch:
568 return
mblighcc2e6662006-09-14 01:24:07 +0000569
jadmanski0afbb632008-06-06 21:10:57 +0000570 # if someone has set build_target, don't clobber in set_cross_cc
571 # run set_build_target before calling set_cross_cc
572 if not self.build_target:
573 self.set_build_target(build_target)
mbligh678823f2006-12-07 18:49:00 +0000574
jadmanski0afbb632008-06-06 21:10:57 +0000575 # If no 'target_arch' given assume native compilation
mblighd876f452008-12-03 15:09:17 +0000576 if target_arch is None:
mbligh53da18e2009-01-05 21:13:26 +0000577 target_arch = utils.get_current_kernel_arch()
jadmanski0afbb632008-06-06 21:10:57 +0000578 if target_arch == 'ppc64':
579 if self.build_target == 'bzImage':
580 self.build_target = 'vmlinux'
mbligh72b88fc2006-12-16 18:41:35 +0000581
jadmanski0afbb632008-06-06 21:10:57 +0000582 if not cross_compile:
583 cross_compile = self.job.config_get('kernel.cross_cc')
mbligh678823f2006-12-07 18:49:00 +0000584
jadmanski0afbb632008-06-06 21:10:57 +0000585 if cross_compile:
586 os.environ['CROSS_COMPILE'] = cross_compile
587 else:
588 if os.environ.has_key('CROSS_COMPILE'):
589 del os.environ['CROSS_COMPILE']
mbligh678823f2006-12-07 18:49:00 +0000590
jadmanski0afbb632008-06-06 21:10:57 +0000591 return # HACK. Crap out for now.
mblighf4c35322006-03-13 01:01:10 +0000592
jadmanski0afbb632008-06-06 21:10:57 +0000593 # At this point I know what arch I *want* to build for
594 # but have no way of working out what arch the default
595 # compiler DOES build for.
mblighcc2e6662006-09-14 01:24:07 +0000596
mbligh925e1b12008-06-12 17:48:38 +0000597 def install_package(package):
598 raise NotImplementedError("I don't exist yet!")
mbligh72b88fc2006-12-16 18:41:35 +0000599
jadmanski0afbb632008-06-06 21:10:57 +0000600 if target_arch == 'ppc64':
601 install_package('ppc64-cross')
602 cross_compile = os.path.join(self.autodir, 'sources/ppc64-cross/bin')
mblighcc2e6662006-09-14 01:24:07 +0000603
jadmanski0afbb632008-06-06 21:10:57 +0000604 elif target_arch == 'x86_64':
605 install_package('x86_64-cross')
606 cross_compile = os.path.join(self.autodir, 'sources/x86_64-cross/bin')
mblighb8a14e32006-05-06 00:17:35 +0000607
jadmanski0afbb632008-06-06 21:10:57 +0000608 os.environ['ARCH'] = self.target_arch = target_arch
mbligh5970cf02006-08-06 15:39:22 +0000609
jadmanski0afbb632008-06-06 21:10:57 +0000610 self.cross_compile = cross_compile
611 if self.cross_compile:
612 os.environ['CROSS_COMPILE'] = self.cross_compile
mblighcc2e6662006-09-14 01:24:07 +0000613
mbligh72b88fc2006-12-16 18:41:35 +0000614
jadmanski0afbb632008-06-06 21:10:57 +0000615 def pickle_dump(self, filename):
616 """dump a pickle of ourself out to the specified filename
mblighc86b0b42006-07-28 17:35:28 +0000617
jadmanski0afbb632008-06-06 21:10:57 +0000618 we can't pickle the backreference to job (it contains fd's),
619 nor would we want to. Same for logfile (fd's).
620 """
621 temp = copy.copy(self)
622 temp.job = None
623 temp.logfile = None
624 pickle.dump(temp, open(filename, 'w'))
mbligh736adc92007-10-18 03:23:22 +0000625
626
jadmanski6ca37b62008-06-30 21:17:07 +0000627class rpm_kernel(object):
mbligheaa75e52009-11-06 03:08:08 +0000628 """
629 Class for installing a binary rpm kernel package
jadmanski0afbb632008-06-06 21:10:57 +0000630 """
mbligh736adc92007-10-18 03:23:22 +0000631
jadmanski0afbb632008-06-06 21:10:57 +0000632 def __init__(self, job, rpm_package, subdir):
633 self.job = job
634 self.rpm_package = rpm_package
635 self.log_dir = os.path.join(subdir, 'debug')
636 self.subdir = os.path.basename(subdir)
637 if os.path.exists(self.log_dir):
638 utils.system('rm -rf ' + self.log_dir)
639 os.mkdir(self.log_dir)
640 self.installed_as = None
mbligh736adc92007-10-18 03:23:22 +0000641
642
mbligheaa75e52009-11-06 03:08:08 +0000643 def build(self, *args, **dargs):
644 """
645 Dummy function, binary kernel so nothing to build.
646 """
647 pass
648
649
mbligh1b3b3762008-09-25 02:46:34 +0000650 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000651 @tee_output_logdir_mark
mbligha25e8c32009-06-15 21:27:23 +0000652 def install(self, tag='autotest', install_vmlinux=True):
jadmanski0afbb632008-06-06 21:10:57 +0000653 self.installed_as = tag
mblighda0311e2007-10-25 16:03:33 +0000654
mbligh1b160a02009-05-21 01:27:10 +0000655 self.image = None
jadmanski0afbb632008-06-06 21:10:57 +0000656 self.initrd = ''
mbligh1b160a02009-05-21 01:27:10 +0000657 for rpm_pack in self.rpm_package:
658 rpm_name = utils.system_output('rpm -qp ' + rpm_pack)
mbligh736adc92007-10-18 03:23:22 +0000659
mbligh1b160a02009-05-21 01:27:10 +0000660 # install
661 utils.system('rpm -i --force ' + rpm_pack)
662
663 # get file list
664 files = utils.system_output('rpm -ql ' + rpm_name).splitlines()
665
666 # search for vmlinuz
667 for file in files:
668 if file.startswith('/boot/vmlinuz'):
669 self.full_version = file[len('/boot/vmlinuz-'):]
670 self.image = file
671 self.rpm_flavour = rpm_name.split('-')[1]
672
673 # get version and release number
674 self.version, self.release = utils.system_output(
675 'rpm --queryformat="%{VERSION}\\n%{RELEASE}\\n" -q '
676 + rpm_name).splitlines()[0:2]
677
678 # prefer /boot/kernel-version before /boot/kernel
679 if self.full_version:
680 break
681
682 # search for initrd
683 for file in files:
684 if file.startswith('/boot/initrd'):
685 self.initrd = file
686 # prefer /boot/initrd-version before /boot/initrd
687 if len(file) > len('/boot/initrd'):
688 break
689
690 if self.image == None:
691 errmsg = "specified rpm file(s) don't contain /boot/vmlinuz"
692 raise error.TestError(errmsg)
mbligh736adc92007-10-18 03:23:22 +0000693
mbligha25e8c32009-06-15 21:27:23 +0000694 # install vmlinux
695 if install_vmlinux:
696 for rpm_pack in self.rpm_package:
697 vmlinux = utils.system_output(
698 'rpm -q -l -p %s | grep /boot/vmlinux' % rpm_pack)
jadmanski19426ea2009-07-28 20:19:40 +0000699 utils.system('cd /; rpm2cpio %s | cpio -imuv .%s 2>&1'
mbligha25e8c32009-06-15 21:27:23 +0000700 % (rpm_pack, vmlinux))
701 if not os.path.exists(vmlinux):
702 raise error.TestError('%s does not exist after installing %s'
703 % (vmlinux, rpm_pack))
704
mbligh736adc92007-10-18 03:23:22 +0000705
jadmanski0afbb632008-06-06 21:10:57 +0000706 def add_to_bootloader(self, tag='autotest', args=''):
707 """ Add this kernel to bootloader
708 """
mbligh736adc92007-10-18 03:23:22 +0000709
jadmanski0afbb632008-06-06 21:10:57 +0000710 # remove existing entry if present
711 self.job.bootloader.remove_kernel(tag)
mbligh736adc92007-10-18 03:23:22 +0000712
jadmanski0afbb632008-06-06 21:10:57 +0000713 # pull the base argument set from the job config
714 baseargs = self.job.config_get('boot.default_args')
715 if baseargs:
716 args = baseargs + ' ' + args
mbligh736adc92007-10-18 03:23:22 +0000717
jadmanski0afbb632008-06-06 21:10:57 +0000718 # otherwise populate from /proc/cmdline
719 # if not baseargs:
720 # baseargs = open('/proc/cmdline', 'r').readline().strip()
721 # NOTE: This is unnecessary, because boottool does it.
mbligh736adc92007-10-18 03:23:22 +0000722
jadmanski0afbb632008-06-06 21:10:57 +0000723 root = None
724 roots = [x for x in args.split() if x.startswith('root=')]
725 if roots:
726 root = re.sub('^root=', '', roots[0])
727 arglist = [x for x in args.split() if not x.startswith('root=')]
728 args = ' '.join(arglist)
mbligh736adc92007-10-18 03:23:22 +0000729
jadmanski0afbb632008-06-06 21:10:57 +0000730 # add the kernel entry
mblighc2ebea02009-10-02 00:02:33 +0000731 self.job.bootloader.add_kernel(self.image, tag, initrd=self.initrd,
732 args=args, root=root)
mbligh10a24a72007-10-24 21:02:53 +0000733
734
jadmanski067b26c2008-09-25 19:46:56 +0000735 def boot(self, args='', ident=True):
jadmanski0afbb632008-06-06 21:10:57 +0000736 """ install and boot this kernel
737 """
mbligh73e82a32007-11-08 21:35:29 +0000738
jadmanski0afbb632008-06-06 21:10:57 +0000739 # Check if the kernel has been installed, if not install
740 # as the default tag and boot that.
741 if not self.installed_as:
742 self.install()
mblighda0311e2007-10-25 16:03:33 +0000743
jadmanski0afbb632008-06-06 21:10:57 +0000744 # If we can check the kernel identity do so.
mblighb1887c82009-03-12 00:25:48 +0000745 expected_ident = self.full_version
746 if not expected_ident:
747 expected_ident = '-'.join([self.version,
mbligh1b160a02009-05-21 01:27:10 +0000748 self.rpm_flavour,
mblighb1887c82009-03-12 00:25:48 +0000749 self.release])
jadmanski0afbb632008-06-06 21:10:57 +0000750 if ident:
751 when = int(time.time())
jadmanski0afbb632008-06-06 21:10:57 +0000752 args += " IDENT=%d" % (when)
jadmanski067b26c2008-09-25 19:46:56 +0000753 self.job.next_step_prepend(["job.end_reboot_and_verify",
754 when, expected_ident, None, 'rpm'])
755 else:
756 self.job.next_step_prepend(["job.end_reboot", None,
757 expected_ident, []])
mbligh10a24a72007-10-24 21:02:53 +0000758
jadmanski0afbb632008-06-06 21:10:57 +0000759 # Boot the selected tag.
760 self.add_to_bootloader(args=args, tag=self.installed_as)
mbligh10a24a72007-10-24 21:02:53 +0000761
jadmanski0afbb632008-06-06 21:10:57 +0000762 # Boot it.
jadmanski02c0e452008-10-21 16:33:44 +0000763 self.job.start_reboot()
jadmanski0afbb632008-06-06 21:10:57 +0000764 self.job.reboot(tag=self.installed_as)
mbligh6ee7ee02007-11-13 23:49:05 +0000765
766
mblighe7785cc2009-03-17 17:32:47 +0000767class rpm_kernel_suse(rpm_kernel):
768 """ Class for installing openSUSE/SLE rpm kernel package
769 """
770
771 def install(self):
772 # do not set the new kernel as the default one
773 os.environ['PBL_AUTOTEST'] = '1'
774
775 rpm_kernel.install(self, 'dummy')
776 self.installed_as = self.job.bootloader.get_title_for_kernel(self.image)
777 if not self.installed_as:
778 errmsg = "cannot find installed kernel in bootloader configuration"
779 raise error.TestError(errmsg)
780
781
782 def add_to_bootloader(self, tag='dummy', args=''):
783 """ Set parameters of this kernel in bootloader
784 """
785
786 # pull the base argument set from the job config
787 baseargs = self.job.config_get('boot.default_args')
788 if baseargs:
789 args = baseargs + ' ' + args
790
791 self.job.bootloader.add_args(tag, args)
792
793
794def rpm_kernel_vendor(job, rpm_package, subdir):
mbligh1ef218d2009-08-03 16:57:56 +0000795 vendor = utils.get_os_vendor()
796 if vendor == "SUSE":
797 return rpm_kernel_suse(job, rpm_package, subdir)
798 else:
799 return rpm_kernel(job, rpm_package, subdir)
mblighe7785cc2009-03-17 17:32:47 +0000800
801
mbligh062ed152009-01-13 00:57:14 +0000802# just make the preprocessor a nop
803def _preprocess_path_dummy(path):
804 return path.strip()
805
806
mbligh6ee7ee02007-11-13 23:49:05 +0000807# pull in some optional site-specific path pre-processing
jadmanski19426ea2009-07-28 20:19:40 +0000808preprocess_path = utils.import_site_function(__file__,
mbligh062ed152009-01-13 00:57:14 +0000809 "autotest_lib.client.bin.site_kernel", "preprocess_path",
810 _preprocess_path_dummy)
mbligh6ee7ee02007-11-13 23:49:05 +0000811
mblighc5ddfd12008-08-04 17:15:00 +0000812
mbligh6ee7ee02007-11-13 23:49:05 +0000813def auto_kernel(job, path, subdir, tmp_dir, build_dir, leave=False):
mbligh7aeda672009-01-30 00:35:59 +0000814 """
jadmanski0afbb632008-06-06 21:10:57 +0000815 Create a kernel object, dynamically selecting the appropriate class to use
816 based on the path provided.
817 """
mbligh1b160a02009-05-21 01:27:10 +0000818 kernel_paths = [preprocess_path(path)]
819 if kernel_paths[0].endswith('.list'):
mbligh1ef218d2009-08-03 16:57:56 +0000820 # Fetch the list of packages to install
mbligh1b160a02009-05-21 01:27:10 +0000821 kernel_list = os.path.join(tmp_dir, 'kernel.list')
822 utils.get_file(kernel_paths[0], kernel_list)
823 kernel_paths = [p.strip() for p in open(kernel_list).readlines()]
mblighc5ddfd12008-08-04 17:15:00 +0000824
mbligh1b160a02009-05-21 01:27:10 +0000825 if kernel_paths[0].endswith('.rpm'):
826 rpm_paths = []
827 for kernel_path in kernel_paths:
jadmanski19426ea2009-07-28 20:19:40 +0000828 if os.path.exists(kernel_path):
mbligh1b160a02009-05-21 01:27:10 +0000829 rpm_paths.append(kernel_path)
mbligh1b160a02009-05-21 01:27:10 +0000830 else:
831 # Fetch the rpm into the job's packages directory and pass it to
832 # rpm_kernel
833 rpm_name = os.path.basename(kernel_path)
mbligh7aeda672009-01-30 00:35:59 +0000834
mbligh1b160a02009-05-21 01:27:10 +0000835 # If the preprocessed path (kernel_path) is only a name then
836 # search for the kernel in all the repositories, else fetch the
837 # kernel from that specific path.
838 job.pkgmgr.fetch_pkg(rpm_name, os.path.join(job.pkgdir, rpm_name),
839 repo_url=os.path.dirname(kernel_path))
840
841 rpm_paths.append(os.path.join(job.pkgdir, rpm_name))
842 return rpm_kernel_vendor(job, rpm_paths, subdir)
jadmanski0afbb632008-06-06 21:10:57 +0000843 else:
mbligh1b160a02009-05-21 01:27:10 +0000844 if len(kernel_paths) > 1:
845 raise error.TestError("don't know what to do with more than one non-rpm kernel file")
846 return kernel(job,kernel_paths[0], subdir, tmp_dir, build_dir, leave)