blob: 9ec59a929f98dc1e9b090d086ee7364dc362fd49 [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
437 # add_kernel(image, title='autotest', initrd='')
438 self.job.bootloader.add_kernel(self.image, tag, self.initrd, \
439 args = args, root = root)
apwcbe32572006-11-28 10:00:23 +0000440
mbligha87116f2006-10-10 02:47:08 +0000441
jadmanski0afbb632008-06-06 21:10:57 +0000442 def get_kernel_build_arch(self, arch=None):
443 """
444 Work out the current kernel architecture (as a kernel arch)
445 """
446 if not arch:
mbligh53da18e2009-01-05 21:13:26 +0000447 arch = utils.get_current_kernel_arch()
jadmanski0afbb632008-06-06 21:10:57 +0000448 if re.match('i.86', arch):
449 return 'i386'
450 elif re.match('sun4u', arch):
451 return 'sparc64'
452 elif re.match('arm.*', arch):
453 return 'arm'
454 elif re.match('sa110', arch):
455 return 'arm'
456 elif re.match('s390x', arch):
457 return 's390'
458 elif re.match('parisc64', arch):
459 return 'parisc'
460 elif re.match('ppc.*', arch):
461 return 'powerpc'
462 elif re.match('mips.*', arch):
463 return 'mips'
464 else:
465 return arch
mbligh6a1d4db2006-10-06 04:30:16 +0000466
mbligh201aa892006-10-29 04:02:05 +0000467
jadmanski0afbb632008-06-06 21:10:57 +0000468 def get_kernel_build_release(self):
469 releasem = re.compile(r'.*UTS_RELEASE\s+"([^"]+)".*');
470 versionm = re.compile(r'.*UTS_VERSION\s+"([^"]+)".*');
mbligh548f29a2006-10-17 04:55:12 +0000471
jadmanski0afbb632008-06-06 21:10:57 +0000472 release = None
473 version = None
mbligh6a1d4db2006-10-06 04:30:16 +0000474
jadmanskid524b0e2008-09-15 14:28:20 +0000475 for f in [self.build_dir + "/include/linux/version.h",
476 self.build_dir + "/include/linux/utsrelease.h",
477 self.build_dir + "/include/linux/compile.h"]:
478 if os.path.exists(f):
479 fd = open(f, 'r')
jadmanski0afbb632008-06-06 21:10:57 +0000480 for line in fd.readlines():
481 m = releasem.match(line)
482 if m:
483 release = m.groups()[0]
484 m = versionm.match(line)
485 if m:
486 version = m.groups()[0]
487 fd.close()
mbligh237bed32007-09-05 13:05:57 +0000488
jadmanski0afbb632008-06-06 21:10:57 +0000489 return (release, version)
mbligh237bed32007-09-05 13:05:57 +0000490
mbligh237bed32007-09-05 13:05:57 +0000491
jadmanski0afbb632008-06-06 21:10:57 +0000492 def get_kernel_build_ident(self):
493 (release, version) = self.get_kernel_build_release()
mbligh237bed32007-09-05 13:05:57 +0000494
jadmanski0afbb632008-06-06 21:10:57 +0000495 if not release or not version:
496 raise error.JobError('kernel has no identity')
mbligh237bed32007-09-05 13:05:57 +0000497
jadmanski0afbb632008-06-06 21:10:57 +0000498 return release + '::' + version
mbligh237bed32007-09-05 13:05:57 +0000499
mbligh237bed32007-09-05 13:05:57 +0000500
jadmanski067b26c2008-09-25 19:46:56 +0000501 def boot(self, args='', ident=True):
jadmanski0afbb632008-06-06 21:10:57 +0000502 """ install and boot this kernel, do not care how
503 just make it happen.
504 """
mbligh237bed32007-09-05 13:05:57 +0000505
jadmanski0afbb632008-06-06 21:10:57 +0000506 # If we can check the kernel identity do so.
jadmanski067b26c2008-09-25 19:46:56 +0000507 expected_ident = self.get_kernel_build_ident()
jadmanski0afbb632008-06-06 21:10:57 +0000508 if ident:
509 when = int(time.time())
jadmanski0afbb632008-06-06 21:10:57 +0000510 args += " IDENT=%d" % (when)
jadmanski067b26c2008-09-25 19:46:56 +0000511 self.job.next_step_prepend(["job.end_reboot_and_verify", when,
512 expected_ident, self.subdir,
513 self.applied_patches])
514 else:
515 self.job.next_step_prepend(["job.end_reboot", self.subdir,
516 expected_ident, self.applied_patches])
mbligh237bed32007-09-05 13:05:57 +0000517
jadmanski0afbb632008-06-06 21:10:57 +0000518 # Check if the kernel has been installed, if not install
519 # as the default tag and boot that.
520 if not self.installed_as:
521 self.install()
mbligh237bed32007-09-05 13:05:57 +0000522
jadmanski0afbb632008-06-06 21:10:57 +0000523 # Boot the selected tag.
524 self.add_to_bootloader(args=args, tag=self.installed_as)
apw87c65c12007-09-27 17:19:37 +0000525
jadmanski0afbb632008-06-06 21:10:57 +0000526 # Boot it.
jadmanski02c0e452008-10-21 16:33:44 +0000527 self.job.start_reboot()
jadmanski0afbb632008-06-06 21:10:57 +0000528 self.job.reboot(tag=self.installed_as)
apw1b5dc362006-10-31 11:24:26 +0000529
apw1b5dc362006-10-31 11:24:26 +0000530
jadmanski0afbb632008-06-06 21:10:57 +0000531 def get_kernel_build_ver(self):
532 """Check Makefile and .config to return kernel version"""
533 version = patchlevel = sublevel = extraversion = localversion = ''
apw1b5dc362006-10-31 11:24:26 +0000534
jadmanski0afbb632008-06-06 21:10:57 +0000535 for line in open(self.build_dir + '/Makefile', 'r').readlines():
536 if line.startswith('VERSION'):
537 version = line[line.index('=') + 1:].strip()
538 if line.startswith('PATCHLEVEL'):
539 patchlevel = line[line.index('=') + 1:].strip()
540 if line.startswith('SUBLEVEL'):
541 sublevel = line[line.index('=') + 1:].strip()
542 if line.startswith('EXTRAVERSION'):
543 extraversion = line[line.index('=') + 1:].strip()
mblighe11f5fc2006-10-04 04:42:22 +0000544
jadmanski0afbb632008-06-06 21:10:57 +0000545 for line in open(self.build_dir + '/.config', 'r').readlines():
546 if line.startswith('CONFIG_LOCALVERSION='):
547 localversion = line.rstrip().split('"')[1]
mblighe11f5fc2006-10-04 04:42:22 +0000548
jadmanski0afbb632008-06-06 21:10:57 +0000549 return "%s.%s.%s%s%s" %(version, patchlevel, sublevel, extraversion, localversion)
mblighe11f5fc2006-10-04 04:42:22 +0000550
mblighfdbcaec2006-10-01 23:28:57 +0000551
jadmanski0afbb632008-06-06 21:10:57 +0000552 def set_build_target(self, build_target):
553 if build_target:
554 self.build_target = build_target
555 print 'BUILD TARGET: %s' % self.build_target
mblighfdbcaec2006-10-01 23:28:57 +0000556
mbligh8baa2ea2006-12-17 23:01:24 +0000557
jadmanski0afbb632008-06-06 21:10:57 +0000558 def set_cross_cc(self, target_arch=None, cross_compile=None,
559 build_target='bzImage'):
560 """Set up to cross-compile.
561 This is broken. We need to work out what the default
562 compile produces, and if not, THEN set the cross
563 compiler.
564 """
mbligh8baa2ea2006-12-17 23:01:24 +0000565
jadmanski0afbb632008-06-06 21:10:57 +0000566 if self.target_arch:
567 return
mblighcc2e6662006-09-14 01:24:07 +0000568
jadmanski0afbb632008-06-06 21:10:57 +0000569 # if someone has set build_target, don't clobber in set_cross_cc
570 # run set_build_target before calling set_cross_cc
571 if not self.build_target:
572 self.set_build_target(build_target)
mbligh678823f2006-12-07 18:49:00 +0000573
jadmanski0afbb632008-06-06 21:10:57 +0000574 # If no 'target_arch' given assume native compilation
mblighd876f452008-12-03 15:09:17 +0000575 if target_arch is None:
mbligh53da18e2009-01-05 21:13:26 +0000576 target_arch = utils.get_current_kernel_arch()
jadmanski0afbb632008-06-06 21:10:57 +0000577 if target_arch == 'ppc64':
578 if self.build_target == 'bzImage':
579 self.build_target = 'vmlinux'
mbligh72b88fc2006-12-16 18:41:35 +0000580
jadmanski0afbb632008-06-06 21:10:57 +0000581 if not cross_compile:
582 cross_compile = self.job.config_get('kernel.cross_cc')
mbligh678823f2006-12-07 18:49:00 +0000583
jadmanski0afbb632008-06-06 21:10:57 +0000584 if cross_compile:
585 os.environ['CROSS_COMPILE'] = cross_compile
586 else:
587 if os.environ.has_key('CROSS_COMPILE'):
588 del os.environ['CROSS_COMPILE']
mbligh678823f2006-12-07 18:49:00 +0000589
jadmanski0afbb632008-06-06 21:10:57 +0000590 return # HACK. Crap out for now.
mblighf4c35322006-03-13 01:01:10 +0000591
jadmanski0afbb632008-06-06 21:10:57 +0000592 # At this point I know what arch I *want* to build for
593 # but have no way of working out what arch the default
594 # compiler DOES build for.
mblighcc2e6662006-09-14 01:24:07 +0000595
mbligh925e1b12008-06-12 17:48:38 +0000596 def install_package(package):
597 raise NotImplementedError("I don't exist yet!")
mbligh72b88fc2006-12-16 18:41:35 +0000598
jadmanski0afbb632008-06-06 21:10:57 +0000599 if target_arch == 'ppc64':
600 install_package('ppc64-cross')
601 cross_compile = os.path.join(self.autodir, 'sources/ppc64-cross/bin')
mblighcc2e6662006-09-14 01:24:07 +0000602
jadmanski0afbb632008-06-06 21:10:57 +0000603 elif target_arch == 'x86_64':
604 install_package('x86_64-cross')
605 cross_compile = os.path.join(self.autodir, 'sources/x86_64-cross/bin')
mblighb8a14e32006-05-06 00:17:35 +0000606
jadmanski0afbb632008-06-06 21:10:57 +0000607 os.environ['ARCH'] = self.target_arch = target_arch
mbligh5970cf02006-08-06 15:39:22 +0000608
jadmanski0afbb632008-06-06 21:10:57 +0000609 self.cross_compile = cross_compile
610 if self.cross_compile:
611 os.environ['CROSS_COMPILE'] = self.cross_compile
mblighcc2e6662006-09-14 01:24:07 +0000612
mbligh72b88fc2006-12-16 18:41:35 +0000613
jadmanski0afbb632008-06-06 21:10:57 +0000614 def pickle_dump(self, filename):
615 """dump a pickle of ourself out to the specified filename
mblighc86b0b42006-07-28 17:35:28 +0000616
jadmanski0afbb632008-06-06 21:10:57 +0000617 we can't pickle the backreference to job (it contains fd's),
618 nor would we want to. Same for logfile (fd's).
619 """
620 temp = copy.copy(self)
621 temp.job = None
622 temp.logfile = None
623 pickle.dump(temp, open(filename, 'w'))
mbligh736adc92007-10-18 03:23:22 +0000624
625
jadmanski6ca37b62008-06-30 21:17:07 +0000626class rpm_kernel(object):
jadmanski0afbb632008-06-06 21:10:57 +0000627 """ Class for installing rpm kernel package
628 """
mbligh736adc92007-10-18 03:23:22 +0000629
jadmanski0afbb632008-06-06 21:10:57 +0000630 def __init__(self, job, rpm_package, subdir):
631 self.job = job
632 self.rpm_package = rpm_package
633 self.log_dir = os.path.join(subdir, 'debug')
634 self.subdir = os.path.basename(subdir)
635 if os.path.exists(self.log_dir):
636 utils.system('rm -rf ' + self.log_dir)
637 os.mkdir(self.log_dir)
638 self.installed_as = None
mbligh736adc92007-10-18 03:23:22 +0000639
640
mbligh1b3b3762008-09-25 02:46:34 +0000641 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000642 @tee_output_logdir_mark
mbligha25e8c32009-06-15 21:27:23 +0000643 def install(self, tag='autotest', install_vmlinux=True):
jadmanski0afbb632008-06-06 21:10:57 +0000644 self.installed_as = tag
mblighda0311e2007-10-25 16:03:33 +0000645
mbligh1b160a02009-05-21 01:27:10 +0000646 self.image = None
jadmanski0afbb632008-06-06 21:10:57 +0000647 self.initrd = ''
mbligh1b160a02009-05-21 01:27:10 +0000648 for rpm_pack in self.rpm_package:
649 rpm_name = utils.system_output('rpm -qp ' + rpm_pack)
mbligh736adc92007-10-18 03:23:22 +0000650
mbligh1b160a02009-05-21 01:27:10 +0000651 # install
652 utils.system('rpm -i --force ' + rpm_pack)
653
654 # get file list
655 files = utils.system_output('rpm -ql ' + rpm_name).splitlines()
656
657 # search for vmlinuz
658 for file in files:
659 if file.startswith('/boot/vmlinuz'):
660 self.full_version = file[len('/boot/vmlinuz-'):]
661 self.image = file
662 self.rpm_flavour = rpm_name.split('-')[1]
663
664 # get version and release number
665 self.version, self.release = utils.system_output(
666 'rpm --queryformat="%{VERSION}\\n%{RELEASE}\\n" -q '
667 + rpm_name).splitlines()[0:2]
668
669 # prefer /boot/kernel-version before /boot/kernel
670 if self.full_version:
671 break
672
673 # search for initrd
674 for file in files:
675 if file.startswith('/boot/initrd'):
676 self.initrd = file
677 # prefer /boot/initrd-version before /boot/initrd
678 if len(file) > len('/boot/initrd'):
679 break
680
681 if self.image == None:
682 errmsg = "specified rpm file(s) don't contain /boot/vmlinuz"
683 raise error.TestError(errmsg)
mbligh736adc92007-10-18 03:23:22 +0000684
mbligha25e8c32009-06-15 21:27:23 +0000685 # install vmlinux
686 if install_vmlinux:
687 for rpm_pack in self.rpm_package:
688 vmlinux = utils.system_output(
689 'rpm -q -l -p %s | grep /boot/vmlinux' % rpm_pack)
jadmanski19426ea2009-07-28 20:19:40 +0000690 utils.system('cd /; rpm2cpio %s | cpio -imuv .%s 2>&1'
mbligha25e8c32009-06-15 21:27:23 +0000691 % (rpm_pack, vmlinux))
692 if not os.path.exists(vmlinux):
693 raise error.TestError('%s does not exist after installing %s'
694 % (vmlinux, rpm_pack))
695
mbligh736adc92007-10-18 03:23:22 +0000696
jadmanski0afbb632008-06-06 21:10:57 +0000697 def add_to_bootloader(self, tag='autotest', args=''):
698 """ Add this kernel to bootloader
699 """
mbligh736adc92007-10-18 03:23:22 +0000700
jadmanski0afbb632008-06-06 21:10:57 +0000701 # remove existing entry if present
702 self.job.bootloader.remove_kernel(tag)
mbligh736adc92007-10-18 03:23:22 +0000703
jadmanski0afbb632008-06-06 21:10:57 +0000704 # pull the base argument set from the job config
705 baseargs = self.job.config_get('boot.default_args')
706 if baseargs:
707 args = baseargs + ' ' + args
mbligh736adc92007-10-18 03:23:22 +0000708
jadmanski0afbb632008-06-06 21:10:57 +0000709 # otherwise populate from /proc/cmdline
710 # if not baseargs:
711 # baseargs = open('/proc/cmdline', 'r').readline().strip()
712 # NOTE: This is unnecessary, because boottool does it.
mbligh736adc92007-10-18 03:23:22 +0000713
jadmanski0afbb632008-06-06 21:10:57 +0000714 root = None
715 roots = [x for x in args.split() if x.startswith('root=')]
716 if roots:
717 root = re.sub('^root=', '', roots[0])
718 arglist = [x for x in args.split() if not x.startswith('root=')]
719 args = ' '.join(arglist)
mbligh736adc92007-10-18 03:23:22 +0000720
jadmanski0afbb632008-06-06 21:10:57 +0000721 # add the kernel entry
mbligh9e6a4f12008-06-06 21:55:12 +0000722 self.job.bootloader.add_kernel(self.image, tag, self.initrd,
723 args = args, root = root)
mbligh10a24a72007-10-24 21:02:53 +0000724
725
jadmanski067b26c2008-09-25 19:46:56 +0000726 def boot(self, args='', ident=True):
jadmanski0afbb632008-06-06 21:10:57 +0000727 """ install and boot this kernel
728 """
mbligh73e82a32007-11-08 21:35:29 +0000729
jadmanski0afbb632008-06-06 21:10:57 +0000730 # Check if the kernel has been installed, if not install
731 # as the default tag and boot that.
732 if not self.installed_as:
733 self.install()
mblighda0311e2007-10-25 16:03:33 +0000734
jadmanski0afbb632008-06-06 21:10:57 +0000735 # If we can check the kernel identity do so.
mblighb1887c82009-03-12 00:25:48 +0000736 expected_ident = self.full_version
737 if not expected_ident:
738 expected_ident = '-'.join([self.version,
mbligh1b160a02009-05-21 01:27:10 +0000739 self.rpm_flavour,
mblighb1887c82009-03-12 00:25:48 +0000740 self.release])
jadmanski0afbb632008-06-06 21:10:57 +0000741 if ident:
742 when = int(time.time())
jadmanski0afbb632008-06-06 21:10:57 +0000743 args += " IDENT=%d" % (when)
jadmanski067b26c2008-09-25 19:46:56 +0000744 self.job.next_step_prepend(["job.end_reboot_and_verify",
745 when, expected_ident, None, 'rpm'])
746 else:
747 self.job.next_step_prepend(["job.end_reboot", None,
748 expected_ident, []])
mbligh10a24a72007-10-24 21:02:53 +0000749
jadmanski0afbb632008-06-06 21:10:57 +0000750 # Boot the selected tag.
751 self.add_to_bootloader(args=args, tag=self.installed_as)
mbligh10a24a72007-10-24 21:02:53 +0000752
jadmanski0afbb632008-06-06 21:10:57 +0000753 # Boot it.
jadmanski02c0e452008-10-21 16:33:44 +0000754 self.job.start_reboot()
jadmanski0afbb632008-06-06 21:10:57 +0000755 self.job.reboot(tag=self.installed_as)
mbligh6ee7ee02007-11-13 23:49:05 +0000756
757
mblighe7785cc2009-03-17 17:32:47 +0000758class rpm_kernel_suse(rpm_kernel):
759 """ Class for installing openSUSE/SLE rpm kernel package
760 """
761
762 def install(self):
763 # do not set the new kernel as the default one
764 os.environ['PBL_AUTOTEST'] = '1'
765
766 rpm_kernel.install(self, 'dummy')
767 self.installed_as = self.job.bootloader.get_title_for_kernel(self.image)
768 if not self.installed_as:
769 errmsg = "cannot find installed kernel in bootloader configuration"
770 raise error.TestError(errmsg)
771
772
773 def add_to_bootloader(self, tag='dummy', args=''):
774 """ Set parameters of this kernel in bootloader
775 """
776
777 # pull the base argument set from the job config
778 baseargs = self.job.config_get('boot.default_args')
779 if baseargs:
780 args = baseargs + ' ' + args
781
782 self.job.bootloader.add_args(tag, args)
783
784
785def rpm_kernel_vendor(job, rpm_package, subdir):
786 vendor = utils.get_os_vendor()
787 if vendor == "SUSE":
788 return rpm_kernel_suse(job, rpm_package, subdir)
789 else:
790 return rpm_kernel(job, rpm_package, subdir)
791
792
mbligh062ed152009-01-13 00:57:14 +0000793# just make the preprocessor a nop
794def _preprocess_path_dummy(path):
795 return path.strip()
796
797
mbligh6ee7ee02007-11-13 23:49:05 +0000798# pull in some optional site-specific path pre-processing
jadmanski19426ea2009-07-28 20:19:40 +0000799preprocess_path = utils.import_site_function(__file__,
mbligh062ed152009-01-13 00:57:14 +0000800 "autotest_lib.client.bin.site_kernel", "preprocess_path",
801 _preprocess_path_dummy)
mbligh6ee7ee02007-11-13 23:49:05 +0000802
mblighc5ddfd12008-08-04 17:15:00 +0000803
mbligh6ee7ee02007-11-13 23:49:05 +0000804def auto_kernel(job, path, subdir, tmp_dir, build_dir, leave=False):
mbligh7aeda672009-01-30 00:35:59 +0000805 """
jadmanski0afbb632008-06-06 21:10:57 +0000806 Create a kernel object, dynamically selecting the appropriate class to use
807 based on the path provided.
808 """
mbligh1b160a02009-05-21 01:27:10 +0000809 kernel_paths = [preprocess_path(path)]
810 if kernel_paths[0].endswith('.list'):
811 # Fetch the list of packages to install
812 kernel_list = os.path.join(tmp_dir, 'kernel.list')
813 utils.get_file(kernel_paths[0], kernel_list)
814 kernel_paths = [p.strip() for p in open(kernel_list).readlines()]
mblighc5ddfd12008-08-04 17:15:00 +0000815
mbligh1b160a02009-05-21 01:27:10 +0000816 if kernel_paths[0].endswith('.rpm'):
817 rpm_paths = []
818 for kernel_path in kernel_paths:
jadmanski19426ea2009-07-28 20:19:40 +0000819 if os.path.exists(kernel_path):
mbligh1b160a02009-05-21 01:27:10 +0000820 rpm_paths.append(kernel_path)
mbligh1b160a02009-05-21 01:27:10 +0000821 else:
822 # Fetch the rpm into the job's packages directory and pass it to
823 # rpm_kernel
824 rpm_name = os.path.basename(kernel_path)
mbligh7aeda672009-01-30 00:35:59 +0000825
mbligh1b160a02009-05-21 01:27:10 +0000826 # If the preprocessed path (kernel_path) is only a name then
827 # search for the kernel in all the repositories, else fetch the
828 # kernel from that specific path.
829 job.pkgmgr.fetch_pkg(rpm_name, os.path.join(job.pkgdir, rpm_name),
830 repo_url=os.path.dirname(kernel_path))
831
832 rpm_paths.append(os.path.join(job.pkgdir, rpm_name))
833 return rpm_kernel_vendor(job, rpm_paths, subdir)
jadmanski0afbb632008-06-06 21:10:57 +0000834 else:
mbligh1b160a02009-05-21 01:27:10 +0000835 if len(kernel_paths) > 1:
836 raise error.TestError("don't know what to do with more than one non-rpm kernel file")
837 return kernel(job,kernel_paths[0], subdir, tmp_dir, build_dir, leave)