blob: 113203ae8e7e8e1d9867db54b29e96e376772620 [file] [log] [blame]
mblighc86b0b42006-07-28 17:35:28 +00001__author__ = """Copyright Martin J. Bligh, 2006"""
mbligha2508052006-05-28 21:29:53 +00002
mblighc61fb362008-06-05 16:22:15 +00003import os, shutil, copy, pickle, re, glob, time
4from autotest_lib.client.bin.fd_stack import tee_output_logdir_mark
5from autotest_lib.client.bin import kernel_config, os_dep, kernelexpand, test
6from autotest_lib.client.bin import autotest_utils
mbligh33dbc912008-06-03 14:59:28 +00007from autotest_lib.client.common_lib import logging, utils
mblighf4c35322006-03-13 01:01:10 +00008
mblighb8e0a112007-11-05 20:27:36 +00009
mblighf4c35322006-03-13 01:01:10 +000010class kernel:
jadmanski0afbb632008-06-06 21:10:57 +000011 """ Class for compiling kernels.
mblighc86b0b42006-07-28 17:35:28 +000012
jadmanski0afbb632008-06-06 21:10:57 +000013 Data for the object includes the src files
14 used to create the kernel, patches applied, config (base + changes),
15 the build directory itself, and logged output
mblighc86b0b42006-07-28 17:35:28 +000016
jadmanski0afbb632008-06-06 21:10:57 +000017 Properties:
18 job
19 Backpointer to the job object we're part of
20 autodir
21 Path to the top level autotest dir (/usr/local/autotest)
22 src_dir
23 <tmp_dir>/src/
24 build_dir
25 <tmp_dir>/linux/
26 config_dir
27 <results_dir>/config/
28 log_dir
29 <results_dir>/debug/
30 results_dir
31 <results_dir>/results/
32 """
mblighc86b0b42006-07-28 17:35:28 +000033
jadmanski0afbb632008-06-06 21:10:57 +000034 autodir = ''
mbligh8baa2ea2006-12-17 23:01:24 +000035
jadmanski0afbb632008-06-06 21:10:57 +000036 def __init__(self, job, base_tree, subdir, tmp_dir, build_dir, leave = False):
37 """Initialize the kernel build environment
mblighc86b0b42006-07-28 17:35:28 +000038
jadmanski0afbb632008-06-06 21:10:57 +000039 job
40 which job this build is part of
41 base_tree
42 base kernel tree. Can be one of the following:
43 1. A local tarball
44 2. A URL to a tarball
45 3. A local directory (will symlink it)
46 4. A shorthand expandable (eg '2.6.11-git3')
47 subdir
48 subdir in the results directory (eg "build")
49 (holds config/, debug/, results/)
50 tmp_dir
mbligh72b88fc2006-12-16 18:41:35 +000051
jadmanski0afbb632008-06-06 21:10:57 +000052 leave
53 Boolean, whether to leave existing tmpdir or not
54 """
55 self.job = job
56 self.autodir = job.autodir
mblighf4c35322006-03-13 01:01:10 +000057
jadmanski0afbb632008-06-06 21:10:57 +000058 self.src_dir = os.path.join(tmp_dir, 'src')
59 self.build_dir = os.path.join(tmp_dir, build_dir)
60 # created by get_kernel_tree
61 self.config_dir = os.path.join(subdir, 'config')
62 self.log_dir = os.path.join(subdir, 'debug')
63 self.results_dir = os.path.join(subdir, 'results')
64 self.subdir = os.path.basename(subdir)
mbligh1e8858e2006-11-24 22:18:35 +000065
jadmanski0afbb632008-06-06 21:10:57 +000066 self.installed_as = None
apw87c65c12007-09-27 17:19:37 +000067
jadmanski0afbb632008-06-06 21:10:57 +000068 if not leave:
69 if os.path.isdir(self.src_dir):
70 utils.system('rm -rf ' + self.src_dir)
71 if os.path.isdir(self.build_dir):
72 utils.system('rm -rf ' + self.build_dir)
mbligh1e8858e2006-11-24 22:18:35 +000073
jadmanski0afbb632008-06-06 21:10:57 +000074 if not os.path.exists(self.src_dir):
75 os.mkdir(self.src_dir)
76 for path in [self.config_dir, self.log_dir, self.results_dir]:
77 if os.path.exists(path):
78 utils.system('rm -rf ' + path)
79 os.mkdir(path)
mblighf4c35322006-03-13 01:01:10 +000080
jadmanski0afbb632008-06-06 21:10:57 +000081 logpath = os.path.join(self.log_dir, 'build_log')
82 self.logfile = open(logpath, 'w+')
83 self.applied_patches = []
mbligh4426de02006-10-10 07:18:28 +000084
jadmanski0afbb632008-06-06 21:10:57 +000085 self.target_arch = None
86 self.build_target = 'bzImage'
87 self.build_image = None
mblighfdbcaec2006-10-01 23:28:57 +000088
jadmanski0afbb632008-06-06 21:10:57 +000089 arch = autotest_utils.get_current_kernel_arch()
90 if arch == 's390' or arch == 's390x':
91 self.build_target = 'image'
92 elif arch == 'ia64':
93 self.build_target = 'all'
94 self.build_image = 'vmlinux.gz'
mblighcac347a2007-06-02 17:21:48 +000095
jadmanski0afbb632008-06-06 21:10:57 +000096 if leave:
97 return
mbligh534015f2006-09-15 03:28:56 +000098
jadmanski0afbb632008-06-06 21:10:57 +000099 self.logfile.write('BASE: %s\n' % base_tree)
apw2366d992007-03-12 20:35:57 +0000100
jadmanski0afbb632008-06-06 21:10:57 +0000101 # Where we have direct version hint record that
102 # for later configuration selection.
103 shorthand = re.compile(r'^\d+\.\d+\.\d+')
104 if shorthand.match(base_tree):
105 self.base_tree_version = base_tree
106 else:
107 self.base_tree_version = None
apw040dcaa2007-11-21 19:36:55 +0000108
jadmanski0afbb632008-06-06 21:10:57 +0000109 # Actually extract the tree. Make sure we know it occured
110 self.extract(base_tree)
apw040dcaa2007-11-21 19:36:55 +0000111
apw7bae90e2008-03-05 12:18:11 +0000112
jadmanski0afbb632008-06-06 21:10:57 +0000113 def kernelexpand(self, kernel):
114 # If we have something like a path, just use it as it is
115 if '/' in kernel:
116 return [kernel]
apw7bae90e2008-03-05 12:18:11 +0000117
jadmanski0afbb632008-06-06 21:10:57 +0000118 # Find the configured mirror list.
119 mirrors = self.job.config_get('mirror.mirrors')
120 if not mirrors:
121 # LEGACY: convert the kernel.org mirror
122 mirror = self.job.config_get('mirror.ftp_kernel_org')
123 if mirror:
124 korg = 'http://www.kernel.org/pub/linux/kernel'
125 mirrors = [
126 [ korg + '/v2.6', mirror + '/v2.6' ],
mbligh9e6a4f12008-06-06 21:55:12 +0000127 [ korg + '/people/akpm/patches/2.6', mirror + '/akpm' ],
128 [ korg + '/people/mbligh', mirror + '/mbligh' ],
jadmanski0afbb632008-06-06 21:10:57 +0000129 ]
apw7bae90e2008-03-05 12:18:11 +0000130
jadmanski0afbb632008-06-06 21:10:57 +0000131 patches = kernelexpand.expand_classic(kernel, mirrors)
132 print patches
apw7bae90e2008-03-05 12:18:11 +0000133
jadmanski0afbb632008-06-06 21:10:57 +0000134 return patches
apw7bae90e2008-03-05 12:18:11 +0000135
mblighf4c35322006-03-13 01:01:10 +0000136
jadmanski0afbb632008-06-06 21:10:57 +0000137 @logging.record
138 @tee_output_logdir_mark
139 def extract(self, base_tree):
140 if os.path.exists(base_tree):
141 self.get_kernel_tree(base_tree)
142 else:
143 base_components = self.kernelexpand(base_tree)
144 print 'kernelexpand: '
145 print base_components
146 self.get_kernel_tree(base_components.pop(0))
147 if base_components: # apply remaining patches
148 self.patch(*base_components)
mblighf4c35322006-03-13 01:01:10 +0000149
mblighf4c35322006-03-13 01:01:10 +0000150
jadmanski0afbb632008-06-06 21:10:57 +0000151 @logging.record
152 @tee_output_logdir_mark
153 def patch(self, *patches):
154 """Apply a list of patches (in order)"""
155 if not patches:
156 return
157 print 'Applying patches: ', patches
158 self.apply_patches(self.get_patches(patches))
mblighf4c35322006-03-13 01:01:10 +0000159
mblighf4c35322006-03-13 01:01:10 +0000160
jadmanski0afbb632008-06-06 21:10:57 +0000161 @logging.record
162 @tee_output_logdir_mark
163 def config(self, config_file = '', config_list = None, defconfig = False):
164 self.set_cross_cc()
165 config = kernel_config.kernel_config(self.job, self.build_dir,
166 self.config_dir, config_file, config_list,
167 defconfig, self.base_tree_version)
mblighf4c35322006-03-13 01:01:10 +0000168
mblighf4c35322006-03-13 01:01:10 +0000169
jadmanski0afbb632008-06-06 21:10:57 +0000170 def get_patches(self, patches):
171 """fetch the patches to the local src_dir"""
172 local_patches = []
173 for patch in patches:
174 dest = os.path.join(self.src_dir, basename(patch))
175 # FIXME: this isn't unique. Append something to it
176 # like wget does if it's not there?
177 print "get_file %s %s %s %s" % (patch, dest, self.src_dir, basename(patch))
178 utils.get_file(patch, dest)
179 # probably safer to use the command, not python library
180 md5sum = utils.system_output('md5sum ' + dest).split()[0]
181 local_patches.append((patch, dest, md5sum))
182 return local_patches
mbligh72b88fc2006-12-16 18:41:35 +0000183
mblighf4c35322006-03-13 01:01:10 +0000184
jadmanski0afbb632008-06-06 21:10:57 +0000185 def apply_patches(self, local_patches):
186 """apply the list of patches, in order"""
187 builddir = self.build_dir
188 os.chdir(builddir)
mbligh72b88fc2006-12-16 18:41:35 +0000189
jadmanski0afbb632008-06-06 21:10:57 +0000190 if not local_patches:
191 return None
192 for (spec, local, md5sum) in local_patches:
193 if local.endswith('.bz2') or local.endswith('.gz'):
194 ref = spec
195 else:
196 ref = force_copy(local, self.results_dir)
197 ref = self.job.relative_path(ref)
198 patch_id = "%s %s %s" % (spec, ref, md5sum)
199 log = "PATCH: " + patch_id + "\n"
200 print log
201 cat_file_to_cmd(local, 'patch -p1 > /dev/null')
202 self.logfile.write(log)
203 self.applied_patches.append(patch_id)
mbligh72b88fc2006-12-16 18:41:35 +0000204
mblighf4c35322006-03-13 01:01:10 +0000205
jadmanski0afbb632008-06-06 21:10:57 +0000206 def get_kernel_tree(self, base_tree):
207 """Extract/link base_tree to self.build_dir"""
mbligh5970cf02006-08-06 15:39:22 +0000208
jadmanski0afbb632008-06-06 21:10:57 +0000209 # if base_tree is a dir, assume uncompressed kernel
210 if os.path.isdir(base_tree):
211 print 'Symlinking existing kernel source'
212 os.symlink(base_tree, self.build_dir)
mblighf4c35322006-03-13 01:01:10 +0000213
jadmanski0afbb632008-06-06 21:10:57 +0000214 # otherwise, extract tarball
215 else:
216 os.chdir(os.path.dirname(self.src_dir))
217 # Figure out local destination for tarball
218 tarball = os.path.join(self.src_dir, os.path.basename(base_tree))
219 utils.get_file(base_tree, tarball)
220 print 'Extracting kernel tarball:', tarball, '...'
mbligh9e6a4f12008-06-06 21:55:12 +0000221 autotest_utils.extract_tarball_to_dir(tarball, self.build_dir)
mblighfdbcaec2006-10-01 23:28:57 +0000222
223
jadmanski0afbb632008-06-06 21:10:57 +0000224 def extraversion(self, tag, append=1):
225 os.chdir(self.build_dir)
226 extraversion_sub = r's/^EXTRAVERSION =\s*\(.*\)/EXTRAVERSION = '
227 if append:
228 p = extraversion_sub + '\\1-%s/' % tag
229 else:
230 p = extraversion_sub + '-%s/' % tag
231 utils.system('mv Makefile Makefile.old')
232 utils.system('sed "%s" < Makefile.old > Makefile' % p)
mbligh72b88fc2006-12-16 18:41:35 +0000233
apwc7846102006-04-06 18:22:13 +0000234
jadmanski0afbb632008-06-06 21:10:57 +0000235 @logging.record
236 @tee_output_logdir_mark
237 def build(self, make_opts = '', logfile = '', extraversion='autotest'):
238 """build the kernel
apwc7846102006-04-06 18:22:13 +0000239
jadmanski0afbb632008-06-06 21:10:57 +0000240 make_opts
241 additional options to make, if any
242 """
243 os_dep.commands('gcc', 'make')
244 if logfile == '':
245 logfile = os.path.join(self.log_dir, 'kernel_build')
246 os.chdir(self.build_dir)
247 if extraversion:
248 self.extraversion(extraversion)
249 self.set_cross_cc()
250 # setup_config_file(config_file, config_overrides)
mbligh1e8858e2006-11-24 22:18:35 +0000251
jadmanski0afbb632008-06-06 21:10:57 +0000252 # Not needed on 2.6, but hard to tell -- handle failure
253 utils.system('make dep', ignore_status=True)
254 threads = 2 * autotest_utils.count_cpus()
255 build_string = 'make -j %d %s %s' % (threads, make_opts,
256 self.build_target)
257 # eg make bzImage, or make zImage
258 print build_string
259 system(build_string)
260 if kernel_config.modules_needed('.config'):
261 utils.system('make -j %d modules' % (threads))
mblighf4c35322006-03-13 01:01:10 +0000262
jadmanski0afbb632008-06-06 21:10:57 +0000263 kernel_version = self.get_kernel_build_ver()
264 kernel_version = re.sub('-autotest', '', kernel_version)
265 self.logfile.write('BUILD VERSION: %s\n' % kernel_version)
mblighf4c35322006-03-13 01:01:10 +0000266
jadmanski0afbb632008-06-06 21:10:57 +0000267 force_copy(self.build_dir+'/System.map', self.results_dir)
mbligh30f28c52007-10-11 18:35:35 +0000268
mbligh30f28c52007-10-11 18:35:35 +0000269
jadmanski0afbb632008-06-06 21:10:57 +0000270 def build_timed(self, threads, timefile = '/dev/null', make_opts = '',
271 output = '/dev/null'):
272 """time the bulding of the kernel"""
273 os.chdir(self.build_dir)
274 self.set_cross_cc()
275
276 self.clean(logged=False)
277 build_string = "/usr/bin/time -o %s make %s -j %s vmlinux" \
278 % (timefile, make_opts, threads)
279 build_string += ' > %s 2>&1' % output
280 print build_string
281 utils.system(build_string)
282
283 if (not os.path.isfile('vmlinux')):
284 errmsg = "no vmlinux found, kernel build failed"
285 raise error.TestError(errmsg)
286
287
288 @logging.record
289 @tee_output_logdir_mark
290 def clean(self):
291 """make clean in the kernel tree"""
292 os.chdir(self.build_dir)
293 print "make clean"
294 utils.system('make clean > /dev/null 2> /dev/null')
295
296
297 @logging.record
298 @tee_output_logdir_mark
299 def mkinitrd(self, version, image, system_map, initrd):
300 """Build kernel initrd image.
301 Try to use distro specific way to build initrd image.
302 Parameters:
303 version
304 new kernel version
305 image
306 new kernel image file
307 system_map
308 System.map file
309 initrd
310 initrd image file to build
311 """
312 vendor = autotest_utils.get_os_vendor()
mblighb8a14e32006-05-06 00:17:35 +0000313
jadmanski0afbb632008-06-06 21:10:57 +0000314 if os.path.isfile(initrd):
315 print "Existing %s file, will remove it." % initrd
316 os.remove(initrd)
mblighb8a14e32006-05-06 00:17:35 +0000317
jadmanski0afbb632008-06-06 21:10:57 +0000318 args = self.job.config_get('kernel.mkinitrd_extra_args')
mblighf4c35322006-03-13 01:01:10 +0000319
jadmanski0afbb632008-06-06 21:10:57 +0000320 # don't leak 'None' into mkinitrd command
321 if not args:
322 args = ''
mbligh50f42ea2006-09-30 22:22:21 +0000323
jadmanski0afbb632008-06-06 21:10:57 +0000324 if vendor in ['Red Hat', 'Fedora Core']:
325 utils.system('mkinitrd %s %s %s' % (args, initrd, version))
326 elif vendor in ['SUSE']:
327 utils.system('mkinitrd %s -k %s -i %s -M %s' % (args, image, initrd, system_map))
328 elif vendor in ['Debian', 'Ubuntu']:
329 if os.path.isfile('/usr/sbin/mkinitrd'):
330 cmd = '/usr/sbin/mkinitrd'
331 elif os.path.isfile('/usr/sbin/mkinitramfs'):
332 cmd = '/usr/sbin/mkinitramfs'
333 else:
334 raise error.TestError('No Debian initrd builder')
335 utils.system('%s %s -o %s %s' % (cmd, args, initrd, version))
336 else:
337 raise error.TestError('Unsupported vendor %s' % vendor)
mbligh72b88fc2006-12-16 18:41:35 +0000338
apwe43a30b2007-09-25 16:51:30 +0000339
jadmanski0afbb632008-06-06 21:10:57 +0000340 def set_build_image(self, image):
341 self.build_image = image
mbligh3d515d42007-11-09 17:00:36 +0000342
mbligh50f42ea2006-09-30 22:22:21 +0000343
jadmanski0afbb632008-06-06 21:10:57 +0000344 @logging.record
345 @tee_output_logdir_mark
346 def install(self, tag='autotest', prefix = '/'):
347 """make install in the kernel tree"""
mbligh50f42ea2006-09-30 22:22:21 +0000348
jadmanski0afbb632008-06-06 21:10:57 +0000349 # Record that we have installed the kernel, and
350 # the tag under which we installed it.
351 self.installed_as = tag
mbligh8baa2ea2006-12-17 23:01:24 +0000352
jadmanski0afbb632008-06-06 21:10:57 +0000353 os.chdir(self.build_dir)
mbligh8baa2ea2006-12-17 23:01:24 +0000354
jadmanski0afbb632008-06-06 21:10:57 +0000355 if not os.path.isdir(prefix):
356 os.mkdir(prefix)
357 self.boot_dir = os.path.join(prefix, 'boot')
358 if not os.path.isdir(self.boot_dir):
359 os.mkdir(self.boot_dir)
apw87c65c12007-09-27 17:19:37 +0000360
jadmanski0afbb632008-06-06 21:10:57 +0000361 if not self.build_image:
362 images = glob.glob('arch/*/boot/' + self.build_target)
363 if len(images):
364 self.build_image = images[0]
365 else:
366 self.build_image = self.build_target
apw87c65c12007-09-27 17:19:37 +0000367
jadmanski0afbb632008-06-06 21:10:57 +0000368 # remember installed files
369 self.vmlinux = self.boot_dir + '/vmlinux-' + tag
370 if (self.build_image != 'vmlinux'):
371 self.image = self.boot_dir + '/vmlinuz-' + tag
372 else:
373 self.image = self.vmlinux
374 self.system_map = self.boot_dir + '/System.map-' + tag
375 self.config = self.boot_dir + '/config-' + tag
376 self.initrd = ''
mbligh72b88fc2006-12-16 18:41:35 +0000377
jadmanski0afbb632008-06-06 21:10:57 +0000378 # copy to boot dir
379 autotest_utils.force_copy('vmlinux', self.vmlinux)
380 if (self.build_image != 'vmlinux'):
381 force_copy(self.build_image, self.image)
382 autotest_utils.force_copy('System.map', self.system_map)
383 autotest_utils.force_copy('.config', self.config)
mbligh0ad65582006-10-06 04:16:36 +0000384
jadmanski0afbb632008-06-06 21:10:57 +0000385 if not kernel_config.modules_needed('.config'):
386 return
mbligha87116f2006-10-10 02:47:08 +0000387
jadmanski0afbb632008-06-06 21:10:57 +0000388 utils.system('make modules_install INSTALL_MOD_PATH=%s' % prefix)
389 if prefix == '/':
390 self.initrd = self.boot_dir + '/initrd-' + tag
391 self.mkinitrd(self.get_kernel_build_ver(), self.image,
392 self.system_map, self.initrd)
mbligha87116f2006-10-10 02:47:08 +0000393
mbligha87116f2006-10-10 02:47:08 +0000394
jadmanski0afbb632008-06-06 21:10:57 +0000395 def add_to_bootloader(self, tag='autotest', args=''):
396 """ add this kernel to bootloader, taking an
397 optional parameter of space separated parameters
398 e.g.: kernel.add_to_bootloader('mykernel', 'ro acpi=off')
399 """
mbligh6a1d4db2006-10-06 04:30:16 +0000400
jadmanski0afbb632008-06-06 21:10:57 +0000401 # remove existing entry if present
402 self.job.bootloader.remove_kernel(tag)
mbligh5925e962007-08-30 17:05:22 +0000403
jadmanski0afbb632008-06-06 21:10:57 +0000404 # pull the base argument set from the job config,
405 baseargs = self.job.config_get('boot.default_args')
406 if baseargs:
407 args = baseargs + " " + args
mbligh5925e962007-08-30 17:05:22 +0000408
jadmanski0afbb632008-06-06 21:10:57 +0000409 # otherwise populate from /proc/cmdline
410 # if not baseargs:
411 # baseargs = open('/proc/cmdline', 'r').readline().strip()
412 # NOTE: This is unnecessary, because boottool does it.
mbligha87116f2006-10-10 02:47:08 +0000413
jadmanski0afbb632008-06-06 21:10:57 +0000414 root = None
415 roots = [x for x in args.split() if x.startswith('root=')]
416 if roots:
417 root = re.sub('^root=', '', roots[0])
418 arglist = [x for x in args.split() if not x.startswith('root=')]
419 args = ' '.join(arglist)
mbligha87116f2006-10-10 02:47:08 +0000420
jadmanski0afbb632008-06-06 21:10:57 +0000421 # add the kernel entry
422 # add_kernel(image, title='autotest', initrd='')
423 self.job.bootloader.add_kernel(self.image, tag, self.initrd, \
424 args = args, root = root)
apwcbe32572006-11-28 10:00:23 +0000425
mbligha87116f2006-10-10 02:47:08 +0000426
jadmanski0afbb632008-06-06 21:10:57 +0000427 def get_kernel_build_arch(self, arch=None):
428 """
429 Work out the current kernel architecture (as a kernel arch)
430 """
431 if not arch:
432 arch = autotest_utils.get_current_kernel_arch()
433 if re.match('i.86', arch):
434 return 'i386'
435 elif re.match('sun4u', arch):
436 return 'sparc64'
437 elif re.match('arm.*', arch):
438 return 'arm'
439 elif re.match('sa110', arch):
440 return 'arm'
441 elif re.match('s390x', arch):
442 return 's390'
443 elif re.match('parisc64', arch):
444 return 'parisc'
445 elif re.match('ppc.*', arch):
446 return 'powerpc'
447 elif re.match('mips.*', arch):
448 return 'mips'
449 else:
450 return arch
mbligh6a1d4db2006-10-06 04:30:16 +0000451
mbligh201aa892006-10-29 04:02:05 +0000452
jadmanski0afbb632008-06-06 21:10:57 +0000453 def get_kernel_build_release(self):
454 releasem = re.compile(r'.*UTS_RELEASE\s+"([^"]+)".*');
455 versionm = re.compile(r'.*UTS_VERSION\s+"([^"]+)".*');
mbligh548f29a2006-10-17 04:55:12 +0000456
jadmanski0afbb632008-06-06 21:10:57 +0000457 release = None
458 version = None
mbligh6a1d4db2006-10-06 04:30:16 +0000459
jadmanski0afbb632008-06-06 21:10:57 +0000460 for file in [ self.build_dir + "/include/linux/version.h",
461 self.build_dir + "/include/linux/utsrelease.h",
462 self.build_dir + "/include/linux/compile.h" ]:
463 if os.path.exists(file):
464 fd = open(file, 'r')
465 for line in fd.readlines():
466 m = releasem.match(line)
467 if m:
468 release = m.groups()[0]
469 m = versionm.match(line)
470 if m:
471 version = m.groups()[0]
472 fd.close()
mbligh237bed32007-09-05 13:05:57 +0000473
jadmanski0afbb632008-06-06 21:10:57 +0000474 return (release, version)
mbligh237bed32007-09-05 13:05:57 +0000475
mbligh237bed32007-09-05 13:05:57 +0000476
jadmanski0afbb632008-06-06 21:10:57 +0000477 def get_kernel_build_ident(self):
478 (release, version) = self.get_kernel_build_release()
mbligh237bed32007-09-05 13:05:57 +0000479
jadmanski0afbb632008-06-06 21:10:57 +0000480 if not release or not version:
481 raise error.JobError('kernel has no identity')
mbligh237bed32007-09-05 13:05:57 +0000482
jadmanski0afbb632008-06-06 21:10:57 +0000483 return release + '::' + version
mbligh237bed32007-09-05 13:05:57 +0000484
mbligh237bed32007-09-05 13:05:57 +0000485
jadmanski0afbb632008-06-06 21:10:57 +0000486 def boot(self, args='', ident=1):
487 """ install and boot this kernel, do not care how
488 just make it happen.
489 """
mbligh237bed32007-09-05 13:05:57 +0000490
jadmanski0afbb632008-06-06 21:10:57 +0000491 # If we can check the kernel identity do so.
492 if ident:
493 when = int(time.time())
494 ident = self.get_kernel_build_ident()
495 args += " IDENT=%d" % (when)
apw1b5dc362006-10-31 11:24:26 +0000496
mbligh9e6a4f12008-06-06 21:55:12 +0000497 self.job.next_step_prepend(["job.kernel_check_ident", when, ident,
498 self.subdir, self.applied_patches])
mbligh237bed32007-09-05 13:05:57 +0000499
jadmanski0afbb632008-06-06 21:10:57 +0000500 # Check if the kernel has been installed, if not install
501 # as the default tag and boot that.
502 if not self.installed_as:
503 self.install()
mbligh237bed32007-09-05 13:05:57 +0000504
jadmanski0afbb632008-06-06 21:10:57 +0000505 # Boot the selected tag.
506 self.add_to_bootloader(args=args, tag=self.installed_as)
apw87c65c12007-09-27 17:19:37 +0000507
jadmanski0afbb632008-06-06 21:10:57 +0000508 # Boot it.
509 self.job.reboot(tag=self.installed_as)
apw1b5dc362006-10-31 11:24:26 +0000510
apw1b5dc362006-10-31 11:24:26 +0000511
jadmanski0afbb632008-06-06 21:10:57 +0000512 def get_kernel_build_ver(self):
513 """Check Makefile and .config to return kernel version"""
514 version = patchlevel = sublevel = extraversion = localversion = ''
apw1b5dc362006-10-31 11:24:26 +0000515
jadmanski0afbb632008-06-06 21:10:57 +0000516 for line in open(self.build_dir + '/Makefile', 'r').readlines():
517 if line.startswith('VERSION'):
518 version = line[line.index('=') + 1:].strip()
519 if line.startswith('PATCHLEVEL'):
520 patchlevel = line[line.index('=') + 1:].strip()
521 if line.startswith('SUBLEVEL'):
522 sublevel = line[line.index('=') + 1:].strip()
523 if line.startswith('EXTRAVERSION'):
524 extraversion = line[line.index('=') + 1:].strip()
mblighe11f5fc2006-10-04 04:42:22 +0000525
jadmanski0afbb632008-06-06 21:10:57 +0000526 for line in open(self.build_dir + '/.config', 'r').readlines():
527 if line.startswith('CONFIG_LOCALVERSION='):
528 localversion = line.rstrip().split('"')[1]
mblighe11f5fc2006-10-04 04:42:22 +0000529
jadmanski0afbb632008-06-06 21:10:57 +0000530 return "%s.%s.%s%s%s" %(version, patchlevel, sublevel, extraversion, localversion)
mblighe11f5fc2006-10-04 04:42:22 +0000531
mblighfdbcaec2006-10-01 23:28:57 +0000532
jadmanski0afbb632008-06-06 21:10:57 +0000533 def set_build_target(self, build_target):
534 if build_target:
535 self.build_target = build_target
536 print 'BUILD TARGET: %s' % self.build_target
mblighfdbcaec2006-10-01 23:28:57 +0000537
mbligh8baa2ea2006-12-17 23:01:24 +0000538
jadmanski0afbb632008-06-06 21:10:57 +0000539 def set_cross_cc(self, target_arch=None, cross_compile=None,
540 build_target='bzImage'):
541 """Set up to cross-compile.
542 This is broken. We need to work out what the default
543 compile produces, and if not, THEN set the cross
544 compiler.
545 """
mbligh8baa2ea2006-12-17 23:01:24 +0000546
jadmanski0afbb632008-06-06 21:10:57 +0000547 if self.target_arch:
548 return
mblighcc2e6662006-09-14 01:24:07 +0000549
jadmanski0afbb632008-06-06 21:10:57 +0000550 # if someone has set build_target, don't clobber in set_cross_cc
551 # run set_build_target before calling set_cross_cc
552 if not self.build_target:
553 self.set_build_target(build_target)
mbligh678823f2006-12-07 18:49:00 +0000554
jadmanski0afbb632008-06-06 21:10:57 +0000555 # If no 'target_arch' given assume native compilation
556 if target_arch == None:
557 target_arch = autotest_utils.get_current_kernel_arch()
558 if target_arch == 'ppc64':
559 if self.build_target == 'bzImage':
560 self.build_target = 'vmlinux'
mbligh72b88fc2006-12-16 18:41:35 +0000561
jadmanski0afbb632008-06-06 21:10:57 +0000562 if not cross_compile:
563 cross_compile = self.job.config_get('kernel.cross_cc')
mbligh678823f2006-12-07 18:49:00 +0000564
jadmanski0afbb632008-06-06 21:10:57 +0000565 if cross_compile:
566 os.environ['CROSS_COMPILE'] = cross_compile
567 else:
568 if os.environ.has_key('CROSS_COMPILE'):
569 del os.environ['CROSS_COMPILE']
mbligh678823f2006-12-07 18:49:00 +0000570
jadmanski0afbb632008-06-06 21:10:57 +0000571 return # HACK. Crap out for now.
mblighf4c35322006-03-13 01:01:10 +0000572
jadmanski0afbb632008-06-06 21:10:57 +0000573 # At this point I know what arch I *want* to build for
574 # but have no way of working out what arch the default
575 # compiler DOES build for.
mblighcc2e6662006-09-14 01:24:07 +0000576
jadmanski0afbb632008-06-06 21:10:57 +0000577 # Oh, and BTW, install_package() doesn't exist yet.
mbligh72b88fc2006-12-16 18:41:35 +0000578
jadmanski0afbb632008-06-06 21:10:57 +0000579 if target_arch == 'ppc64':
580 install_package('ppc64-cross')
581 cross_compile = os.path.join(self.autodir, 'sources/ppc64-cross/bin')
mblighcc2e6662006-09-14 01:24:07 +0000582
jadmanski0afbb632008-06-06 21:10:57 +0000583 elif target_arch == 'x86_64':
584 install_package('x86_64-cross')
585 cross_compile = os.path.join(self.autodir, 'sources/x86_64-cross/bin')
mblighb8a14e32006-05-06 00:17:35 +0000586
jadmanski0afbb632008-06-06 21:10:57 +0000587 os.environ['ARCH'] = self.target_arch = target_arch
mbligh5970cf02006-08-06 15:39:22 +0000588
jadmanski0afbb632008-06-06 21:10:57 +0000589 self.cross_compile = cross_compile
590 if self.cross_compile:
591 os.environ['CROSS_COMPILE'] = self.cross_compile
mblighcc2e6662006-09-14 01:24:07 +0000592
mbligh72b88fc2006-12-16 18:41:35 +0000593
jadmanski0afbb632008-06-06 21:10:57 +0000594 def pickle_dump(self, filename):
595 """dump a pickle of ourself out to the specified filename
mblighc86b0b42006-07-28 17:35:28 +0000596
jadmanski0afbb632008-06-06 21:10:57 +0000597 we can't pickle the backreference to job (it contains fd's),
598 nor would we want to. Same for logfile (fd's).
599 """
600 temp = copy.copy(self)
601 temp.job = None
602 temp.logfile = None
603 pickle.dump(temp, open(filename, 'w'))
mbligh736adc92007-10-18 03:23:22 +0000604
605
606class rpm_kernel:
jadmanski0afbb632008-06-06 21:10:57 +0000607 """ Class for installing rpm kernel package
608 """
mbligh736adc92007-10-18 03:23:22 +0000609
jadmanski0afbb632008-06-06 21:10:57 +0000610 def __init__(self, job, rpm_package, subdir):
611 self.job = job
612 self.rpm_package = rpm_package
613 self.log_dir = os.path.join(subdir, 'debug')
614 self.subdir = os.path.basename(subdir)
615 if os.path.exists(self.log_dir):
616 utils.system('rm -rf ' + self.log_dir)
617 os.mkdir(self.log_dir)
618 self.installed_as = None
mbligh736adc92007-10-18 03:23:22 +0000619
620
jadmanski0afbb632008-06-06 21:10:57 +0000621 @logging.record
622 @tee_output_logdir_mark
623 def install(self, tag='autotest'):
624 self.installed_as = tag
mblighda0311e2007-10-25 16:03:33 +0000625
jadmanski0afbb632008-06-06 21:10:57 +0000626 self.rpm_name = utils.system_output('rpm -qp ' + self.rpm_package)
mbligh736adc92007-10-18 03:23:22 +0000627
jadmanski0afbb632008-06-06 21:10:57 +0000628 # install
629 utils.system('rpm -i --force ' + self.rpm_package)
mbligh736adc92007-10-18 03:23:22 +0000630
jadmanski0afbb632008-06-06 21:10:57 +0000631 # get file list
632 files = utils.system_output('rpm -ql ' + self.rpm_name).splitlines()
mbligh736adc92007-10-18 03:23:22 +0000633
jadmanski0afbb632008-06-06 21:10:57 +0000634 # search for vmlinuz
635 for file in files:
636 if file.startswith('/boot/vmlinuz'):
637 self.image = file
638 break
639 else:
640 errmsg = "%s doesn't contain /boot/vmlinuz"
641 errmsg %= self.rpm_package
642 raise error.TestError(errmsg)
mblighda0311e2007-10-25 16:03:33 +0000643
jadmanski0afbb632008-06-06 21:10:57 +0000644 # search for initrd
645 self.initrd = ''
646 for file in files:
647 if file.startswith('/boot/initrd'):
648 self.initrd = file
649 break
mbligh736adc92007-10-18 03:23:22 +0000650
jadmanski0afbb632008-06-06 21:10:57 +0000651 # get version and release number
652 self.version, self.release = utils.system_output(
653 'rpm --queryformat="%{VERSION}\\n%{RELEASE}\\n" -q ' + self.rpm_name).splitlines()[0:2]
mbligh736adc92007-10-18 03:23:22 +0000654
655
jadmanski0afbb632008-06-06 21:10:57 +0000656 def add_to_bootloader(self, tag='autotest', args=''):
657 """ Add this kernel to bootloader
658 """
mbligh736adc92007-10-18 03:23:22 +0000659
jadmanski0afbb632008-06-06 21:10:57 +0000660 # remove existing entry if present
661 self.job.bootloader.remove_kernel(tag)
mbligh736adc92007-10-18 03:23:22 +0000662
jadmanski0afbb632008-06-06 21:10:57 +0000663 # pull the base argument set from the job config
664 baseargs = self.job.config_get('boot.default_args')
665 if baseargs:
666 args = baseargs + ' ' + args
mbligh736adc92007-10-18 03:23:22 +0000667
jadmanski0afbb632008-06-06 21:10:57 +0000668 # otherwise populate from /proc/cmdline
669 # if not baseargs:
670 # baseargs = open('/proc/cmdline', 'r').readline().strip()
671 # NOTE: This is unnecessary, because boottool does it.
mbligh736adc92007-10-18 03:23:22 +0000672
jadmanski0afbb632008-06-06 21:10:57 +0000673 root = None
674 roots = [x for x in args.split() if x.startswith('root=')]
675 if roots:
676 root = re.sub('^root=', '', roots[0])
677 arglist = [x for x in args.split() if not x.startswith('root=')]
678 args = ' '.join(arglist)
mbligh736adc92007-10-18 03:23:22 +0000679
jadmanski0afbb632008-06-06 21:10:57 +0000680 # add the kernel entry
mbligh9e6a4f12008-06-06 21:55:12 +0000681 self.job.bootloader.add_kernel(self.image, tag, self.initrd,
682 args = args, root = root)
mbligh10a24a72007-10-24 21:02:53 +0000683
684
jadmanski0afbb632008-06-06 21:10:57 +0000685 def boot(self, args='', ident=1):
686 """ install and boot this kernel
687 """
mbligh73e82a32007-11-08 21:35:29 +0000688
jadmanski0afbb632008-06-06 21:10:57 +0000689 # Check if the kernel has been installed, if not install
690 # as the default tag and boot that.
691 if not self.installed_as:
692 self.install()
mblighda0311e2007-10-25 16:03:33 +0000693
jadmanski0afbb632008-06-06 21:10:57 +0000694 # If we can check the kernel identity do so.
695 if ident:
696 when = int(time.time())
697 ident = '-'.join([self.version,
698 self.rpm_name.split('-')[1],
699 self.release])
700 args += " IDENT=%d" % (when)
mblighda0311e2007-10-25 16:03:33 +0000701
jadmanski0afbb632008-06-06 21:10:57 +0000702 self.job.next_step_prepend(["job.kernel_check_ident",
mbligh9e6a4f12008-06-06 21:55:12 +0000703 when, ident, self.subdir, 'rpm'])
mbligh10a24a72007-10-24 21:02:53 +0000704
jadmanski0afbb632008-06-06 21:10:57 +0000705 # Boot the selected tag.
706 self.add_to_bootloader(args=args, tag=self.installed_as)
mbligh10a24a72007-10-24 21:02:53 +0000707
jadmanski0afbb632008-06-06 21:10:57 +0000708 # Boot it.
709 self.job.reboot(tag=self.installed_as)
mbligh6ee7ee02007-11-13 23:49:05 +0000710
711
712# pull in some optional site-specific path pre-processing
713try:
jadmanski0afbb632008-06-06 21:10:57 +0000714 import site_kernel
715 preprocess_path = site_kernel.preprocess_path
716 del site_kernel
mbligh6ee7ee02007-11-13 23:49:05 +0000717except ImportError:
jadmanski0afbb632008-06-06 21:10:57 +0000718 # just make the preprocessor a nop
719 def preprocess_path(path):
720 return path
mbligh6ee7ee02007-11-13 23:49:05 +0000721
722def auto_kernel(job, path, subdir, tmp_dir, build_dir, leave=False):
jadmanski0afbb632008-06-06 21:10:57 +0000723 """\
724 Create a kernel object, dynamically selecting the appropriate class to use
725 based on the path provided.
726 """
727 path = preprocess_path(path)
728 if path.endswith('.rpm'):
729 return rpm_kernel(job, path, subdir)
730 else:
731 return kernel(job, path, subdir, tmp_dir, build_dir, leave)