blob: 6b31501d9b9aa1a047225b626ec824f54a693ae5 [file] [log] [blame]
mblighc61fb362008-06-05 16:22:15 +00001import os, shutil, copy, pickle, re, glob, time
2from autotest_lib.client.bin.fd_stack import tee_output_logdir_mark
3from autotest_lib.client.bin import kernel_config, os_dep, kernelexpand, test
mbligh53da18e2009-01-05 21:13:26 +00004from autotest_lib.client.bin import utils
5from autotest_lib.client.common_lib import log, error, packages
mblighf4c35322006-03-13 01:01:10 +00006
mblighb8e0a112007-11-05 20:27:36 +00007
jadmanski6ca37b62008-06-30 21:17:07 +00008class kernel(object):
jadmanski0afbb632008-06-06 21:10:57 +00009 """ Class for compiling kernels.
mblighc86b0b42006-07-28 17:35:28 +000010
jadmanski0afbb632008-06-06 21:10:57 +000011 Data for the object includes the src files
12 used to create the kernel, patches applied, config (base + changes),
13 the build directory itself, and logged output
mblighc86b0b42006-07-28 17:35:28 +000014
jadmanski0afbb632008-06-06 21:10:57 +000015 Properties:
16 job
17 Backpointer to the job object we're part of
18 autodir
19 Path to the top level autotest dir (/usr/local/autotest)
20 src_dir
21 <tmp_dir>/src/
22 build_dir
23 <tmp_dir>/linux/
24 config_dir
25 <results_dir>/config/
26 log_dir
27 <results_dir>/debug/
28 results_dir
29 <results_dir>/results/
30 """
mblighc86b0b42006-07-28 17:35:28 +000031
jadmanski0afbb632008-06-06 21:10:57 +000032 autodir = ''
mbligh8baa2ea2006-12-17 23:01:24 +000033
mbligh925e1b12008-06-12 17:48:38 +000034 def __init__(self, job, base_tree, subdir, tmp_dir, build_dir, leave=False):
jadmanski0afbb632008-06-06 21:10:57 +000035 """Initialize the kernel build environment
mblighc86b0b42006-07-28 17:35:28 +000036
jadmanski0afbb632008-06-06 21:10:57 +000037 job
38 which job this build is part of
39 base_tree
40 base kernel tree. Can be one of the following:
41 1. A local tarball
42 2. A URL to a tarball
43 3. A local directory (will symlink it)
44 4. A shorthand expandable (eg '2.6.11-git3')
45 subdir
46 subdir in the results directory (eg "build")
47 (holds config/, debug/, results/)
48 tmp_dir
mbligh72b88fc2006-12-16 18:41:35 +000049
jadmanski0afbb632008-06-06 21:10:57 +000050 leave
51 Boolean, whether to leave existing tmpdir or not
52 """
53 self.job = job
54 self.autodir = job.autodir
mblighf4c35322006-03-13 01:01:10 +000055
jadmanski0afbb632008-06-06 21:10:57 +000056 self.src_dir = os.path.join(tmp_dir, 'src')
57 self.build_dir = os.path.join(tmp_dir, build_dir)
58 # created by get_kernel_tree
59 self.config_dir = os.path.join(subdir, 'config')
60 self.log_dir = os.path.join(subdir, 'debug')
61 self.results_dir = os.path.join(subdir, 'results')
62 self.subdir = os.path.basename(subdir)
mbligh1e8858e2006-11-24 22:18:35 +000063
jadmanski0afbb632008-06-06 21:10:57 +000064 self.installed_as = None
apw87c65c12007-09-27 17:19:37 +000065
jadmanski0afbb632008-06-06 21:10:57 +000066 if not leave:
67 if os.path.isdir(self.src_dir):
68 utils.system('rm -rf ' + self.src_dir)
69 if os.path.isdir(self.build_dir):
70 utils.system('rm -rf ' + self.build_dir)
mbligh1e8858e2006-11-24 22:18:35 +000071
jadmanski0afbb632008-06-06 21:10:57 +000072 if not os.path.exists(self.src_dir):
73 os.mkdir(self.src_dir)
74 for path in [self.config_dir, self.log_dir, self.results_dir]:
75 if os.path.exists(path):
76 utils.system('rm -rf ' + path)
77 os.mkdir(path)
mblighf4c35322006-03-13 01:01:10 +000078
jadmanski0afbb632008-06-06 21:10:57 +000079 logpath = os.path.join(self.log_dir, 'build_log')
80 self.logfile = open(logpath, 'w+')
81 self.applied_patches = []
mbligh4426de02006-10-10 07:18:28 +000082
jadmanski0afbb632008-06-06 21:10:57 +000083 self.target_arch = None
84 self.build_target = 'bzImage'
85 self.build_image = None
mblighfdbcaec2006-10-01 23:28:57 +000086
mbligh53da18e2009-01-05 21:13:26 +000087 arch = utils.get_current_kernel_arch()
jadmanski0afbb632008-06-06 21:10:57 +000088 if arch == 's390' or arch == 's390x':
89 self.build_target = 'image'
90 elif arch == 'ia64':
91 self.build_target = 'all'
92 self.build_image = 'vmlinux.gz'
mblighcac347a2007-06-02 17:21:48 +000093
mbligh925e1b12008-06-12 17:48:38 +000094 if not leave:
95 self.logfile.write('BASE: %s\n' % base_tree)
mbligh534015f2006-09-15 03:28:56 +000096
mbligh925e1b12008-06-12 17:48:38 +000097 # Where we have direct version hint record that
98 # for later configuration selection.
99 shorthand = re.compile(r'^\d+\.\d+\.\d+')
100 if shorthand.match(base_tree):
101 self.base_tree_version = base_tree
102 else:
103 self.base_tree_version = None
apw2366d992007-03-12 20:35:57 +0000104
mbligh925e1b12008-06-12 17:48:38 +0000105 # Actually extract the tree. Make sure we know it occured
106 self.extract(base_tree)
apw040dcaa2007-11-21 19:36:55 +0000107
apw7bae90e2008-03-05 12:18:11 +0000108
jadmanski0afbb632008-06-06 21:10:57 +0000109 def kernelexpand(self, kernel):
110 # If we have something like a path, just use it as it is
111 if '/' in kernel:
112 return [kernel]
apw7bae90e2008-03-05 12:18:11 +0000113
jadmanski0afbb632008-06-06 21:10:57 +0000114 # Find the configured mirror list.
115 mirrors = self.job.config_get('mirror.mirrors')
116 if not mirrors:
117 # LEGACY: convert the kernel.org mirror
118 mirror = self.job.config_get('mirror.ftp_kernel_org')
119 if mirror:
120 korg = 'http://www.kernel.org/pub/linux/kernel'
121 mirrors = [
122 [ korg + '/v2.6', mirror + '/v2.6' ],
mbligh9e6a4f12008-06-06 21:55:12 +0000123 [ korg + '/people/akpm/patches/2.6', mirror + '/akpm' ],
124 [ korg + '/people/mbligh', mirror + '/mbligh' ],
jadmanski0afbb632008-06-06 21:10:57 +0000125 ]
apw7bae90e2008-03-05 12:18:11 +0000126
jadmanski0afbb632008-06-06 21:10:57 +0000127 patches = kernelexpand.expand_classic(kernel, mirrors)
128 print patches
apw7bae90e2008-03-05 12:18:11 +0000129
jadmanski0afbb632008-06-06 21:10:57 +0000130 return patches
apw7bae90e2008-03-05 12:18:11 +0000131
mblighf4c35322006-03-13 01:01:10 +0000132
mbligh1b3b3762008-09-25 02:46:34 +0000133 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000134 @tee_output_logdir_mark
135 def extract(self, base_tree):
136 if os.path.exists(base_tree):
137 self.get_kernel_tree(base_tree)
138 else:
139 base_components = self.kernelexpand(base_tree)
140 print 'kernelexpand: '
141 print base_components
142 self.get_kernel_tree(base_components.pop(0))
143 if base_components: # apply remaining patches
144 self.patch(*base_components)
mblighf4c35322006-03-13 01:01:10 +0000145
mblighf4c35322006-03-13 01:01:10 +0000146
mbligh1b3b3762008-09-25 02:46:34 +0000147 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000148 @tee_output_logdir_mark
149 def patch(self, *patches):
150 """Apply a list of patches (in order)"""
151 if not patches:
152 return
153 print 'Applying patches: ', patches
154 self.apply_patches(self.get_patches(patches))
mblighf4c35322006-03-13 01:01:10 +0000155
mblighf4c35322006-03-13 01:01:10 +0000156
mbligh1b3b3762008-09-25 02:46:34 +0000157 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000158 @tee_output_logdir_mark
mblighb3400e02008-11-06 15:44:25 +0000159 def config(self, config_file = '', config_list = None, defconfig = False, make = None):
jadmanski0afbb632008-06-06 21:10:57 +0000160 self.set_cross_cc()
161 config = kernel_config.kernel_config(self.job, self.build_dir,
162 self.config_dir, config_file, config_list,
mblighb3400e02008-11-06 15:44:25 +0000163 defconfig, self.base_tree_version, make)
mblighf4c35322006-03-13 01:01:10 +0000164
mblighf4c35322006-03-13 01:01:10 +0000165
jadmanski0afbb632008-06-06 21:10:57 +0000166 def get_patches(self, patches):
167 """fetch the patches to the local src_dir"""
168 local_patches = []
169 for patch in patches:
mbligh1c9b1d22008-06-12 17:46:12 +0000170 dest = os.path.join(self.src_dir, os.path.basename(patch))
jadmanski0afbb632008-06-06 21:10:57 +0000171 # FIXME: this isn't unique. Append something to it
172 # like wget does if it's not there?
jadmanskie3f2f712008-06-12 17:54:56 +0000173 print "get_file %s %s %s %s" % (patch, dest, self.src_dir,
174 os.path.basename(patch))
jadmanski0afbb632008-06-06 21:10:57 +0000175 utils.get_file(patch, dest)
176 # probably safer to use the command, not python library
177 md5sum = utils.system_output('md5sum ' + dest).split()[0]
178 local_patches.append((patch, dest, md5sum))
179 return local_patches
mbligh72b88fc2006-12-16 18:41:35 +0000180
mblighf4c35322006-03-13 01:01:10 +0000181
jadmanski0afbb632008-06-06 21:10:57 +0000182 def apply_patches(self, local_patches):
183 """apply the list of patches, in order"""
184 builddir = self.build_dir
185 os.chdir(builddir)
mbligh72b88fc2006-12-16 18:41:35 +0000186
jadmanski0afbb632008-06-06 21:10:57 +0000187 if not local_patches:
188 return None
189 for (spec, local, md5sum) in local_patches:
190 if local.endswith('.bz2') or local.endswith('.gz'):
191 ref = spec
192 else:
mbligh53da18e2009-01-05 21:13:26 +0000193 ref = utils.force_copy(local, self.results_dir)
jadmanski0afbb632008-06-06 21:10:57 +0000194 ref = self.job.relative_path(ref)
195 patch_id = "%s %s %s" % (spec, ref, md5sum)
196 log = "PATCH: " + patch_id + "\n"
197 print log
mbligh53da18e2009-01-05 21:13:26 +0000198 utils.cat_file_to_cmd(local, 'patch -p1 > /dev/null')
jadmanski0afbb632008-06-06 21:10:57 +0000199 self.logfile.write(log)
200 self.applied_patches.append(patch_id)
mbligh72b88fc2006-12-16 18:41:35 +0000201
mblighf4c35322006-03-13 01:01:10 +0000202
jadmanski0afbb632008-06-06 21:10:57 +0000203 def get_kernel_tree(self, base_tree):
204 """Extract/link base_tree to self.build_dir"""
mbligh5970cf02006-08-06 15:39:22 +0000205
jadmanski0afbb632008-06-06 21:10:57 +0000206 # if base_tree is a dir, assume uncompressed kernel
207 if os.path.isdir(base_tree):
208 print 'Symlinking existing kernel source'
209 os.symlink(base_tree, self.build_dir)
mblighf4c35322006-03-13 01:01:10 +0000210
jadmanski0afbb632008-06-06 21:10:57 +0000211 # otherwise, extract tarball
212 else:
213 os.chdir(os.path.dirname(self.src_dir))
214 # Figure out local destination for tarball
215 tarball = os.path.join(self.src_dir, os.path.basename(base_tree))
216 utils.get_file(base_tree, tarball)
217 print 'Extracting kernel tarball:', tarball, '...'
mbligh53da18e2009-01-05 21:13:26 +0000218 utils.extract_tarball_to_dir(tarball, self.build_dir)
mblighfdbcaec2006-10-01 23:28:57 +0000219
220
jadmanski0afbb632008-06-06 21:10:57 +0000221 def extraversion(self, tag, append=1):
222 os.chdir(self.build_dir)
223 extraversion_sub = r's/^EXTRAVERSION =\s*\(.*\)/EXTRAVERSION = '
224 if append:
225 p = extraversion_sub + '\\1-%s/' % tag
226 else:
227 p = extraversion_sub + '-%s/' % tag
228 utils.system('mv Makefile Makefile.old')
229 utils.system('sed "%s" < Makefile.old > Makefile' % p)
mbligh72b88fc2006-12-16 18:41:35 +0000230
apwc7846102006-04-06 18:22:13 +0000231
mbligh1b3b3762008-09-25 02:46:34 +0000232 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000233 @tee_output_logdir_mark
234 def build(self, make_opts = '', logfile = '', extraversion='autotest'):
235 """build the kernel
apwc7846102006-04-06 18:22:13 +0000236
jadmanski0afbb632008-06-06 21:10:57 +0000237 make_opts
238 additional options to make, if any
239 """
240 os_dep.commands('gcc', 'make')
241 if logfile == '':
242 logfile = os.path.join(self.log_dir, 'kernel_build')
243 os.chdir(self.build_dir)
244 if extraversion:
245 self.extraversion(extraversion)
246 self.set_cross_cc()
247 # setup_config_file(config_file, config_overrides)
mbligh1e8858e2006-11-24 22:18:35 +0000248
jadmanski0afbb632008-06-06 21:10:57 +0000249 # Not needed on 2.6, but hard to tell -- handle failure
250 utils.system('make dep', ignore_status=True)
mbligh53da18e2009-01-05 21:13:26 +0000251 threads = 2 * utils.count_cpus()
jadmanski0afbb632008-06-06 21:10:57 +0000252 build_string = 'make -j %d %s %s' % (threads, make_opts,
253 self.build_target)
254 # eg make bzImage, or make zImage
255 print build_string
mbligh925e1b12008-06-12 17:48:38 +0000256 utils.system(build_string)
jadmanski0afbb632008-06-06 21:10:57 +0000257 if kernel_config.modules_needed('.config'):
258 utils.system('make -j %d modules' % (threads))
mblighf4c35322006-03-13 01:01:10 +0000259
jadmanski0afbb632008-06-06 21:10:57 +0000260 kernel_version = self.get_kernel_build_ver()
261 kernel_version = re.sub('-autotest', '', kernel_version)
262 self.logfile.write('BUILD VERSION: %s\n' % kernel_version)
mblighf4c35322006-03-13 01:01:10 +0000263
mbligh53da18e2009-01-05 21:13:26 +0000264 utils.force_copy(self.build_dir+'/System.map',
mbligh925e1b12008-06-12 17:48:38 +0000265 self.results_dir)
mbligh30f28c52007-10-11 18:35:35 +0000266
mbligh30f28c52007-10-11 18:35:35 +0000267
jadmanski0afbb632008-06-06 21:10:57 +0000268 def build_timed(self, threads, timefile = '/dev/null', make_opts = '',
269 output = '/dev/null'):
270 """time the bulding of the kernel"""
271 os.chdir(self.build_dir)
272 self.set_cross_cc()
273
274 self.clean(logged=False)
275 build_string = "/usr/bin/time -o %s make %s -j %s vmlinux" \
276 % (timefile, make_opts, threads)
277 build_string += ' > %s 2>&1' % output
278 print build_string
279 utils.system(build_string)
280
281 if (not os.path.isfile('vmlinux')):
282 errmsg = "no vmlinux found, kernel build failed"
283 raise error.TestError(errmsg)
284
285
mbligh1b3b3762008-09-25 02:46:34 +0000286 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000287 @tee_output_logdir_mark
288 def clean(self):
289 """make clean in the kernel tree"""
290 os.chdir(self.build_dir)
291 print "make clean"
292 utils.system('make clean > /dev/null 2> /dev/null')
293
294
mbligh1b3b3762008-09-25 02:46:34 +0000295 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000296 @tee_output_logdir_mark
297 def mkinitrd(self, version, image, system_map, initrd):
298 """Build kernel initrd image.
299 Try to use distro specific way to build initrd image.
300 Parameters:
301 version
302 new kernel version
303 image
304 new kernel image file
305 system_map
306 System.map file
307 initrd
308 initrd image file to build
309 """
mbligh53da18e2009-01-05 21:13:26 +0000310 vendor = utils.get_os_vendor()
mblighb8a14e32006-05-06 00:17:35 +0000311
jadmanski0afbb632008-06-06 21:10:57 +0000312 if os.path.isfile(initrd):
313 print "Existing %s file, will remove it." % initrd
314 os.remove(initrd)
mblighb8a14e32006-05-06 00:17:35 +0000315
jadmanski0afbb632008-06-06 21:10:57 +0000316 args = self.job.config_get('kernel.mkinitrd_extra_args')
mblighf4c35322006-03-13 01:01:10 +0000317
jadmanski0afbb632008-06-06 21:10:57 +0000318 # don't leak 'None' into mkinitrd command
319 if not args:
320 args = ''
mbligh50f42ea2006-09-30 22:22:21 +0000321
jadmanski0afbb632008-06-06 21:10:57 +0000322 if vendor in ['Red Hat', 'Fedora Core']:
323 utils.system('mkinitrd %s %s %s' % (args, initrd, version))
324 elif vendor in ['SUSE']:
jadmanskid524b0e2008-09-15 14:28:20 +0000325 utils.system('mkinitrd %s -k %s -i %s -M %s' %
326 (args, image, initrd, system_map))
jadmanski0afbb632008-06-06 21:10:57 +0000327 elif vendor in ['Debian', 'Ubuntu']:
328 if os.path.isfile('/usr/sbin/mkinitrd'):
329 cmd = '/usr/sbin/mkinitrd'
330 elif os.path.isfile('/usr/sbin/mkinitramfs'):
331 cmd = '/usr/sbin/mkinitramfs'
332 else:
333 raise error.TestError('No Debian initrd builder')
334 utils.system('%s %s -o %s %s' % (cmd, args, initrd, version))
335 else:
336 raise error.TestError('Unsupported vendor %s' % vendor)
mbligh72b88fc2006-12-16 18:41:35 +0000337
apwe43a30b2007-09-25 16:51:30 +0000338
jadmanski0afbb632008-06-06 21:10:57 +0000339 def set_build_image(self, image):
340 self.build_image = image
mbligh3d515d42007-11-09 17:00:36 +0000341
mbligh50f42ea2006-09-30 22:22:21 +0000342
mbligh1b3b3762008-09-25 02:46:34 +0000343 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000344 @tee_output_logdir_mark
345 def install(self, tag='autotest', prefix = '/'):
346 """make install in the kernel tree"""
mbligh50f42ea2006-09-30 22:22:21 +0000347
jadmanski0afbb632008-06-06 21:10:57 +0000348 # Record that we have installed the kernel, and
349 # the tag under which we installed it.
350 self.installed_as = tag
mbligh8baa2ea2006-12-17 23:01:24 +0000351
jadmanski0afbb632008-06-06 21:10:57 +0000352 os.chdir(self.build_dir)
mbligh8baa2ea2006-12-17 23:01:24 +0000353
jadmanski0afbb632008-06-06 21:10:57 +0000354 if not os.path.isdir(prefix):
355 os.mkdir(prefix)
356 self.boot_dir = os.path.join(prefix, 'boot')
357 if not os.path.isdir(self.boot_dir):
358 os.mkdir(self.boot_dir)
apw87c65c12007-09-27 17:19:37 +0000359
jadmanski0afbb632008-06-06 21:10:57 +0000360 if not self.build_image:
361 images = glob.glob('arch/*/boot/' + self.build_target)
362 if len(images):
363 self.build_image = images[0]
364 else:
365 self.build_image = self.build_target
apw87c65c12007-09-27 17:19:37 +0000366
jadmanski0afbb632008-06-06 21:10:57 +0000367 # remember installed files
368 self.vmlinux = self.boot_dir + '/vmlinux-' + tag
369 if (self.build_image != 'vmlinux'):
370 self.image = self.boot_dir + '/vmlinuz-' + tag
371 else:
372 self.image = self.vmlinux
373 self.system_map = self.boot_dir + '/System.map-' + tag
mbligh925e1b12008-06-12 17:48:38 +0000374 self.config_file = self.boot_dir + '/config-' + tag
jadmanski0afbb632008-06-06 21:10:57 +0000375 self.initrd = ''
mbligh72b88fc2006-12-16 18:41:35 +0000376
jadmanski0afbb632008-06-06 21:10:57 +0000377 # copy to boot dir
mbligh53da18e2009-01-05 21:13:26 +0000378 utils.force_copy('vmlinux', self.vmlinux)
jadmanski0afbb632008-06-06 21:10:57 +0000379 if (self.build_image != 'vmlinux'):
mbligh53da18e2009-01-05 21:13:26 +0000380 utils.force_copy(self.build_image, self.image)
381 utils.force_copy('System.map', self.system_map)
382 utils.force_copy('.config', self.config_file)
mbligh0ad65582006-10-06 04:16:36 +0000383
jadmanski0afbb632008-06-06 21:10:57 +0000384 if not kernel_config.modules_needed('.config'):
385 return
mbligha87116f2006-10-10 02:47:08 +0000386
jadmanski0afbb632008-06-06 21:10:57 +0000387 utils.system('make modules_install INSTALL_MOD_PATH=%s' % prefix)
388 if prefix == '/':
389 self.initrd = self.boot_dir + '/initrd-' + tag
390 self.mkinitrd(self.get_kernel_build_ver(), self.image,
391 self.system_map, self.initrd)
mbligha87116f2006-10-10 02:47:08 +0000392
mbligha87116f2006-10-10 02:47:08 +0000393
jadmanski0afbb632008-06-06 21:10:57 +0000394 def add_to_bootloader(self, tag='autotest', args=''):
395 """ add this kernel to bootloader, taking an
396 optional parameter of space separated parameters
397 e.g.: kernel.add_to_bootloader('mykernel', 'ro acpi=off')
398 """
mbligh6a1d4db2006-10-06 04:30:16 +0000399
jadmanski0afbb632008-06-06 21:10:57 +0000400 # remove existing entry if present
401 self.job.bootloader.remove_kernel(tag)
mbligh5925e962007-08-30 17:05:22 +0000402
jadmanski0afbb632008-06-06 21:10:57 +0000403 # pull the base argument set from the job config,
404 baseargs = self.job.config_get('boot.default_args')
405 if baseargs:
406 args = baseargs + " " + args
mbligh5925e962007-08-30 17:05:22 +0000407
jadmanski0afbb632008-06-06 21:10:57 +0000408 # otherwise populate from /proc/cmdline
409 # if not baseargs:
410 # baseargs = open('/proc/cmdline', 'r').readline().strip()
411 # NOTE: This is unnecessary, because boottool does it.
mbligha87116f2006-10-10 02:47:08 +0000412
jadmanski0afbb632008-06-06 21:10:57 +0000413 root = None
414 roots = [x for x in args.split() if x.startswith('root=')]
415 if roots:
416 root = re.sub('^root=', '', roots[0])
417 arglist = [x for x in args.split() if not x.startswith('root=')]
418 args = ' '.join(arglist)
mbligha87116f2006-10-10 02:47:08 +0000419
jadmanski0afbb632008-06-06 21:10:57 +0000420 # add the kernel entry
421 # add_kernel(image, title='autotest', initrd='')
422 self.job.bootloader.add_kernel(self.image, tag, self.initrd, \
423 args = args, root = root)
apwcbe32572006-11-28 10:00:23 +0000424
mbligha87116f2006-10-10 02:47:08 +0000425
jadmanski0afbb632008-06-06 21:10:57 +0000426 def get_kernel_build_arch(self, arch=None):
427 """
428 Work out the current kernel architecture (as a kernel arch)
429 """
430 if not arch:
mbligh53da18e2009-01-05 21:13:26 +0000431 arch = utils.get_current_kernel_arch()
jadmanski0afbb632008-06-06 21:10:57 +0000432 if re.match('i.86', arch):
433 return 'i386'
434 elif re.match('sun4u', arch):
435 return 'sparc64'
436 elif re.match('arm.*', arch):
437 return 'arm'
438 elif re.match('sa110', arch):
439 return 'arm'
440 elif re.match('s390x', arch):
441 return 's390'
442 elif re.match('parisc64', arch):
443 return 'parisc'
444 elif re.match('ppc.*', arch):
445 return 'powerpc'
446 elif re.match('mips.*', arch):
447 return 'mips'
448 else:
449 return arch
mbligh6a1d4db2006-10-06 04:30:16 +0000450
mbligh201aa892006-10-29 04:02:05 +0000451
jadmanski0afbb632008-06-06 21:10:57 +0000452 def get_kernel_build_release(self):
453 releasem = re.compile(r'.*UTS_RELEASE\s+"([^"]+)".*');
454 versionm = re.compile(r'.*UTS_VERSION\s+"([^"]+)".*');
mbligh548f29a2006-10-17 04:55:12 +0000455
jadmanski0afbb632008-06-06 21:10:57 +0000456 release = None
457 version = None
mbligh6a1d4db2006-10-06 04:30:16 +0000458
jadmanskid524b0e2008-09-15 14:28:20 +0000459 for f in [self.build_dir + "/include/linux/version.h",
460 self.build_dir + "/include/linux/utsrelease.h",
461 self.build_dir + "/include/linux/compile.h"]:
462 if os.path.exists(f):
463 fd = open(f, 'r')
jadmanski0afbb632008-06-06 21:10:57 +0000464 for line in fd.readlines():
465 m = releasem.match(line)
466 if m:
467 release = m.groups()[0]
468 m = versionm.match(line)
469 if m:
470 version = m.groups()[0]
471 fd.close()
mbligh237bed32007-09-05 13:05:57 +0000472
jadmanski0afbb632008-06-06 21:10:57 +0000473 return (release, version)
mbligh237bed32007-09-05 13:05:57 +0000474
mbligh237bed32007-09-05 13:05:57 +0000475
jadmanski0afbb632008-06-06 21:10:57 +0000476 def get_kernel_build_ident(self):
477 (release, version) = self.get_kernel_build_release()
mbligh237bed32007-09-05 13:05:57 +0000478
jadmanski0afbb632008-06-06 21:10:57 +0000479 if not release or not version:
480 raise error.JobError('kernel has no identity')
mbligh237bed32007-09-05 13:05:57 +0000481
jadmanski0afbb632008-06-06 21:10:57 +0000482 return release + '::' + version
mbligh237bed32007-09-05 13:05:57 +0000483
mbligh237bed32007-09-05 13:05:57 +0000484
jadmanski067b26c2008-09-25 19:46:56 +0000485 def boot(self, args='', ident=True):
jadmanski0afbb632008-06-06 21:10:57 +0000486 """ install and boot this kernel, do not care how
487 just make it happen.
488 """
mbligh237bed32007-09-05 13:05:57 +0000489
jadmanski0afbb632008-06-06 21:10:57 +0000490 # If we can check the kernel identity do so.
jadmanski067b26c2008-09-25 19:46:56 +0000491 expected_ident = self.get_kernel_build_ident()
jadmanski0afbb632008-06-06 21:10:57 +0000492 if ident:
493 when = int(time.time())
jadmanski0afbb632008-06-06 21:10:57 +0000494 args += " IDENT=%d" % (when)
jadmanski067b26c2008-09-25 19:46:56 +0000495 self.job.next_step_prepend(["job.end_reboot_and_verify", when,
496 expected_ident, self.subdir,
497 self.applied_patches])
498 else:
499 self.job.next_step_prepend(["job.end_reboot", self.subdir,
500 expected_ident, self.applied_patches])
mbligh237bed32007-09-05 13:05:57 +0000501
jadmanski0afbb632008-06-06 21:10:57 +0000502 # Check if the kernel has been installed, if not install
503 # as the default tag and boot that.
504 if not self.installed_as:
505 self.install()
mbligh237bed32007-09-05 13:05:57 +0000506
jadmanski0afbb632008-06-06 21:10:57 +0000507 # Boot the selected tag.
508 self.add_to_bootloader(args=args, tag=self.installed_as)
apw87c65c12007-09-27 17:19:37 +0000509
jadmanski0afbb632008-06-06 21:10:57 +0000510 # Boot it.
jadmanski02c0e452008-10-21 16:33:44 +0000511 self.job.start_reboot()
jadmanski0afbb632008-06-06 21:10:57 +0000512 self.job.reboot(tag=self.installed_as)
apw1b5dc362006-10-31 11:24:26 +0000513
apw1b5dc362006-10-31 11:24:26 +0000514
jadmanski0afbb632008-06-06 21:10:57 +0000515 def get_kernel_build_ver(self):
516 """Check Makefile and .config to return kernel version"""
517 version = patchlevel = sublevel = extraversion = localversion = ''
apw1b5dc362006-10-31 11:24:26 +0000518
jadmanski0afbb632008-06-06 21:10:57 +0000519 for line in open(self.build_dir + '/Makefile', 'r').readlines():
520 if line.startswith('VERSION'):
521 version = line[line.index('=') + 1:].strip()
522 if line.startswith('PATCHLEVEL'):
523 patchlevel = line[line.index('=') + 1:].strip()
524 if line.startswith('SUBLEVEL'):
525 sublevel = line[line.index('=') + 1:].strip()
526 if line.startswith('EXTRAVERSION'):
527 extraversion = line[line.index('=') + 1:].strip()
mblighe11f5fc2006-10-04 04:42:22 +0000528
jadmanski0afbb632008-06-06 21:10:57 +0000529 for line in open(self.build_dir + '/.config', 'r').readlines():
530 if line.startswith('CONFIG_LOCALVERSION='):
531 localversion = line.rstrip().split('"')[1]
mblighe11f5fc2006-10-04 04:42:22 +0000532
jadmanski0afbb632008-06-06 21:10:57 +0000533 return "%s.%s.%s%s%s" %(version, patchlevel, sublevel, extraversion, localversion)
mblighe11f5fc2006-10-04 04:42:22 +0000534
mblighfdbcaec2006-10-01 23:28:57 +0000535
jadmanski0afbb632008-06-06 21:10:57 +0000536 def set_build_target(self, build_target):
537 if build_target:
538 self.build_target = build_target
539 print 'BUILD TARGET: %s' % self.build_target
mblighfdbcaec2006-10-01 23:28:57 +0000540
mbligh8baa2ea2006-12-17 23:01:24 +0000541
jadmanski0afbb632008-06-06 21:10:57 +0000542 def set_cross_cc(self, target_arch=None, cross_compile=None,
543 build_target='bzImage'):
544 """Set up to cross-compile.
545 This is broken. We need to work out what the default
546 compile produces, and if not, THEN set the cross
547 compiler.
548 """
mbligh8baa2ea2006-12-17 23:01:24 +0000549
jadmanski0afbb632008-06-06 21:10:57 +0000550 if self.target_arch:
551 return
mblighcc2e6662006-09-14 01:24:07 +0000552
jadmanski0afbb632008-06-06 21:10:57 +0000553 # if someone has set build_target, don't clobber in set_cross_cc
554 # run set_build_target before calling set_cross_cc
555 if not self.build_target:
556 self.set_build_target(build_target)
mbligh678823f2006-12-07 18:49:00 +0000557
jadmanski0afbb632008-06-06 21:10:57 +0000558 # If no 'target_arch' given assume native compilation
mblighd876f452008-12-03 15:09:17 +0000559 if target_arch is None:
mbligh53da18e2009-01-05 21:13:26 +0000560 target_arch = utils.get_current_kernel_arch()
jadmanski0afbb632008-06-06 21:10:57 +0000561 if target_arch == 'ppc64':
562 if self.build_target == 'bzImage':
563 self.build_target = 'vmlinux'
mbligh72b88fc2006-12-16 18:41:35 +0000564
jadmanski0afbb632008-06-06 21:10:57 +0000565 if not cross_compile:
566 cross_compile = self.job.config_get('kernel.cross_cc')
mbligh678823f2006-12-07 18:49:00 +0000567
jadmanski0afbb632008-06-06 21:10:57 +0000568 if cross_compile:
569 os.environ['CROSS_COMPILE'] = cross_compile
570 else:
571 if os.environ.has_key('CROSS_COMPILE'):
572 del os.environ['CROSS_COMPILE']
mbligh678823f2006-12-07 18:49:00 +0000573
jadmanski0afbb632008-06-06 21:10:57 +0000574 return # HACK. Crap out for now.
mblighf4c35322006-03-13 01:01:10 +0000575
jadmanski0afbb632008-06-06 21:10:57 +0000576 # At this point I know what arch I *want* to build for
577 # but have no way of working out what arch the default
578 # compiler DOES build for.
mblighcc2e6662006-09-14 01:24:07 +0000579
mbligh925e1b12008-06-12 17:48:38 +0000580 def install_package(package):
581 raise NotImplementedError("I don't exist yet!")
mbligh72b88fc2006-12-16 18:41:35 +0000582
jadmanski0afbb632008-06-06 21:10:57 +0000583 if target_arch == 'ppc64':
584 install_package('ppc64-cross')
585 cross_compile = os.path.join(self.autodir, 'sources/ppc64-cross/bin')
mblighcc2e6662006-09-14 01:24:07 +0000586
jadmanski0afbb632008-06-06 21:10:57 +0000587 elif target_arch == 'x86_64':
588 install_package('x86_64-cross')
589 cross_compile = os.path.join(self.autodir, 'sources/x86_64-cross/bin')
mblighb8a14e32006-05-06 00:17:35 +0000590
jadmanski0afbb632008-06-06 21:10:57 +0000591 os.environ['ARCH'] = self.target_arch = target_arch
mbligh5970cf02006-08-06 15:39:22 +0000592
jadmanski0afbb632008-06-06 21:10:57 +0000593 self.cross_compile = cross_compile
594 if self.cross_compile:
595 os.environ['CROSS_COMPILE'] = self.cross_compile
mblighcc2e6662006-09-14 01:24:07 +0000596
mbligh72b88fc2006-12-16 18:41:35 +0000597
jadmanski0afbb632008-06-06 21:10:57 +0000598 def pickle_dump(self, filename):
599 """dump a pickle of ourself out to the specified filename
mblighc86b0b42006-07-28 17:35:28 +0000600
jadmanski0afbb632008-06-06 21:10:57 +0000601 we can't pickle the backreference to job (it contains fd's),
602 nor would we want to. Same for logfile (fd's).
603 """
604 temp = copy.copy(self)
605 temp.job = None
606 temp.logfile = None
607 pickle.dump(temp, open(filename, 'w'))
mbligh736adc92007-10-18 03:23:22 +0000608
609
jadmanski6ca37b62008-06-30 21:17:07 +0000610class rpm_kernel(object):
jadmanski0afbb632008-06-06 21:10:57 +0000611 """ Class for installing rpm kernel package
612 """
mbligh736adc92007-10-18 03:23:22 +0000613
jadmanski0afbb632008-06-06 21:10:57 +0000614 def __init__(self, job, rpm_package, subdir):
615 self.job = job
616 self.rpm_package = rpm_package
617 self.log_dir = os.path.join(subdir, 'debug')
618 self.subdir = os.path.basename(subdir)
619 if os.path.exists(self.log_dir):
620 utils.system('rm -rf ' + self.log_dir)
621 os.mkdir(self.log_dir)
622 self.installed_as = None
mbligh736adc92007-10-18 03:23:22 +0000623
624
mbligh1b3b3762008-09-25 02:46:34 +0000625 @log.record
jadmanski0afbb632008-06-06 21:10:57 +0000626 @tee_output_logdir_mark
627 def install(self, tag='autotest'):
628 self.installed_as = tag
mblighda0311e2007-10-25 16:03:33 +0000629
jadmanski0afbb632008-06-06 21:10:57 +0000630 self.rpm_name = utils.system_output('rpm -qp ' + self.rpm_package)
mbligh736adc92007-10-18 03:23:22 +0000631
jadmanski0afbb632008-06-06 21:10:57 +0000632 # install
633 utils.system('rpm -i --force ' + self.rpm_package)
mbligh736adc92007-10-18 03:23:22 +0000634
jadmanski0afbb632008-06-06 21:10:57 +0000635 # get file list
636 files = utils.system_output('rpm -ql ' + self.rpm_name).splitlines()
mbligh736adc92007-10-18 03:23:22 +0000637
jadmanski0afbb632008-06-06 21:10:57 +0000638 # search for vmlinuz
639 for file in files:
640 if file.startswith('/boot/vmlinuz'):
mblighb1887c82009-03-12 00:25:48 +0000641 self.full_version = file[len('/boot/vmlinuz-'):]
jadmanski0afbb632008-06-06 21:10:57 +0000642 self.image = file
mblighb1887c82009-03-12 00:25:48 +0000643 # prefer /boot/kernel-version before /boot/kernel
644 if self.full_version:
645 break
jadmanski0afbb632008-06-06 21:10:57 +0000646 else:
647 errmsg = "%s doesn't contain /boot/vmlinuz"
648 errmsg %= self.rpm_package
649 raise error.TestError(errmsg)
mblighda0311e2007-10-25 16:03:33 +0000650
jadmanski0afbb632008-06-06 21:10:57 +0000651 # search for initrd
652 self.initrd = ''
653 for file in files:
654 if file.startswith('/boot/initrd'):
655 self.initrd = file
mblighb1887c82009-03-12 00:25:48 +0000656 # prefer /boot/initrd-version before /boot/initrd
657 if len(file) > len('/boot/initrd'):
658 break
mbligh736adc92007-10-18 03:23:22 +0000659
jadmanski0afbb632008-06-06 21:10:57 +0000660 # get version and release number
661 self.version, self.release = utils.system_output(
jadmanskid524b0e2008-09-15 14:28:20 +0000662 'rpm --queryformat="%{VERSION}\\n%{RELEASE}\\n" -q '
663 + self.rpm_name).splitlines()[0:2]
mbligh736adc92007-10-18 03:23:22 +0000664
665
jadmanski0afbb632008-06-06 21:10:57 +0000666 def add_to_bootloader(self, tag='autotest', args=''):
667 """ Add this kernel to bootloader
668 """
mbligh736adc92007-10-18 03:23:22 +0000669
jadmanski0afbb632008-06-06 21:10:57 +0000670 # remove existing entry if present
671 self.job.bootloader.remove_kernel(tag)
mbligh736adc92007-10-18 03:23:22 +0000672
jadmanski0afbb632008-06-06 21:10:57 +0000673 # pull the base argument set from the job config
674 baseargs = self.job.config_get('boot.default_args')
675 if baseargs:
676 args = baseargs + ' ' + args
mbligh736adc92007-10-18 03:23:22 +0000677
jadmanski0afbb632008-06-06 21:10:57 +0000678 # otherwise populate from /proc/cmdline
679 # if not baseargs:
680 # baseargs = open('/proc/cmdline', 'r').readline().strip()
681 # NOTE: This is unnecessary, because boottool does it.
mbligh736adc92007-10-18 03:23:22 +0000682
jadmanski0afbb632008-06-06 21:10:57 +0000683 root = None
684 roots = [x for x in args.split() if x.startswith('root=')]
685 if roots:
686 root = re.sub('^root=', '', roots[0])
687 arglist = [x for x in args.split() if not x.startswith('root=')]
688 args = ' '.join(arglist)
mbligh736adc92007-10-18 03:23:22 +0000689
jadmanski0afbb632008-06-06 21:10:57 +0000690 # add the kernel entry
mbligh9e6a4f12008-06-06 21:55:12 +0000691 self.job.bootloader.add_kernel(self.image, tag, self.initrd,
692 args = args, root = root)
mbligh10a24a72007-10-24 21:02:53 +0000693
694
jadmanski067b26c2008-09-25 19:46:56 +0000695 def boot(self, args='', ident=True):
jadmanski0afbb632008-06-06 21:10:57 +0000696 """ install and boot this kernel
697 """
mbligh73e82a32007-11-08 21:35:29 +0000698
jadmanski0afbb632008-06-06 21:10:57 +0000699 # Check if the kernel has been installed, if not install
700 # as the default tag and boot that.
701 if not self.installed_as:
702 self.install()
mblighda0311e2007-10-25 16:03:33 +0000703
jadmanski0afbb632008-06-06 21:10:57 +0000704 # If we can check the kernel identity do so.
mblighb1887c82009-03-12 00:25:48 +0000705 expected_ident = self.full_version
706 if not expected_ident:
707 expected_ident = '-'.join([self.version,
708 self.rpm_name.split('-')[1],
709 self.release])
jadmanski0afbb632008-06-06 21:10:57 +0000710 if ident:
711 when = int(time.time())
jadmanski0afbb632008-06-06 21:10:57 +0000712 args += " IDENT=%d" % (when)
jadmanski067b26c2008-09-25 19:46:56 +0000713 self.job.next_step_prepend(["job.end_reboot_and_verify",
714 when, expected_ident, None, 'rpm'])
715 else:
716 self.job.next_step_prepend(["job.end_reboot", None,
717 expected_ident, []])
mbligh10a24a72007-10-24 21:02:53 +0000718
jadmanski0afbb632008-06-06 21:10:57 +0000719 # Boot the selected tag.
720 self.add_to_bootloader(args=args, tag=self.installed_as)
mbligh10a24a72007-10-24 21:02:53 +0000721
jadmanski0afbb632008-06-06 21:10:57 +0000722 # Boot it.
jadmanski02c0e452008-10-21 16:33:44 +0000723 self.job.start_reboot()
jadmanski0afbb632008-06-06 21:10:57 +0000724 self.job.reboot(tag=self.installed_as)
mbligh6ee7ee02007-11-13 23:49:05 +0000725
726
mbligh062ed152009-01-13 00:57:14 +0000727# just make the preprocessor a nop
728def _preprocess_path_dummy(path):
729 return path.strip()
730
731
mbligh6ee7ee02007-11-13 23:49:05 +0000732# pull in some optional site-specific path pre-processing
mbligh062ed152009-01-13 00:57:14 +0000733preprocess_path = utils.import_site_function(__file__,
734 "autotest_lib.client.bin.site_kernel", "preprocess_path",
735 _preprocess_path_dummy)
mbligh6ee7ee02007-11-13 23:49:05 +0000736
mblighc5ddfd12008-08-04 17:15:00 +0000737
mbligh6ee7ee02007-11-13 23:49:05 +0000738def auto_kernel(job, path, subdir, tmp_dir, build_dir, leave=False):
mbligh7aeda672009-01-30 00:35:59 +0000739 """
jadmanski0afbb632008-06-06 21:10:57 +0000740 Create a kernel object, dynamically selecting the appropriate class to use
741 based on the path provided.
742 """
mblighc5ddfd12008-08-04 17:15:00 +0000743 kernel_path = preprocess_path(path)
744 if kernel_path.endswith('.rpm'):
mbligh7aeda672009-01-30 00:35:59 +0000745 if utils.is_url(kernel_path) or os.path.exists(kernel_path):
746 return rpm_kernel(job, kernel_path, subdir)
mblighc5ddfd12008-08-04 17:15:00 +0000747
mbligh7aeda672009-01-30 00:35:59 +0000748 else:
749 # Fetch the rpm into the job's packages directory and pass it to
750 # rpm_kernel
751 rpm_name = os.path.basename(kernel_path)
mblighc5ddfd12008-08-04 17:15:00 +0000752
mbligh7aeda672009-01-30 00:35:59 +0000753 # If the preprocessed path (kernel_path) is only a name then
754 # search for the kernel in all the repositories, else fetch the
755 # kernel from that specific path.
756 job.pkgmgr.fetch_pkg(rpm_name, os.path.join(job.pkgdir, rpm_name),
757 repo_url=os.path.dirname(kernel_path))
758
759 return rpm_kernel(job, os.path.join(job.pkgdir, rpm_name), subdir)
jadmanski0afbb632008-06-06 21:10:57 +0000760 else:
mblighc5ddfd12008-08-04 17:15:00 +0000761 return kernel(job,kernel_path, subdir, tmp_dir, build_dir, leave)