blob: 1e81068ded7c50e25c867ed3f840acaf7542cfc4 [file] [log] [blame]
mblighc86b0b42006-07-28 17:35:28 +00001__author__ = """Copyright Martin J. Bligh, 2006"""
mbligha2508052006-05-28 21:29:53 +00002
mbligh237bed32007-09-05 13:05:57 +00003import os,os.path,shutil,urllib,copy,pickle,re,glob,time
mblighf4c35322006-03-13 01:01:10 +00004from autotest_utils import *
mbligh119c12a2007-11-12 22:13:44 +00005from common import logging
apw001e40a2007-11-21 19:24:37 +00006from fd_stack import tee_output_logdir_mark
apw7bae90e2008-03-05 12:18:11 +00007import kernel_config, test, os_dep, kernelexpand
mblighf4c35322006-03-13 01:01:10 +00008
mblighb8e0a112007-11-05 20:27:36 +00009
mblighf4c35322006-03-13 01:01:10 +000010class kernel:
mblighc86b0b42006-07-28 17:35:28 +000011 """ Class for compiling kernels.
12
13 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
16
17 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)
mblighc86b0b42006-07-28 17:35:28 +000022 src_dir
mbligh1e8858e2006-11-24 22:18:35 +000023 <tmp_dir>/src/
mblighc49af7b2006-11-24 04:02:21 +000024 build_dir
mbligh1e8858e2006-11-24 22:18:35 +000025 <tmp_dir>/linux/
mblighc86b0b42006-07-28 17:35:28 +000026 config_dir
mbligh1e8858e2006-11-24 22:18:35 +000027 <results_dir>/config/
mblighc86b0b42006-07-28 17:35:28 +000028 log_dir
mbligh1e8858e2006-11-24 22:18:35 +000029 <results_dir>/debug/
30 results_dir
31 <results_dir>/results/
mblighc86b0b42006-07-28 17:35:28 +000032 """
33
mbligh8baa2ea2006-12-17 23:01:24 +000034 autodir = ''
35
mbligh09f288a2007-09-18 21:34:57 +000036 def __init__(self, job, base_tree, subdir, tmp_dir, build_dir, leave = False):
mblighc86b0b42006-07-28 17:35:28 +000037 """Initialize the kernel build environment
38
39 job
40 which job this build is part of
mblighc86b0b42006-07-28 17:35:28 +000041 base_tree
mbligh534015f2006-09-15 03:28:56 +000042 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')
mbligh09f288a2007-09-18 21:34:57 +000047 subdir
48 subdir in the results directory (eg "build")
49 (holds config/, debug/, results/)
mbligh1e8858e2006-11-24 22:18:35 +000050 tmp_dir
mbligh72b88fc2006-12-16 18:41:35 +000051
mbligh1e8858e2006-11-24 22:18:35 +000052 leave
53 Boolean, whether to leave existing tmpdir or not
mblighc86b0b42006-07-28 17:35:28 +000054 """
mblighf4c35322006-03-13 01:01:10 +000055 self.job = job
mbligh678823f2006-12-07 18:49:00 +000056 self.autodir = job.autodir
mblighf4c35322006-03-13 01:01:10 +000057
mbligh1e8858e2006-11-24 22:18:35 +000058 self.src_dir = os.path.join(tmp_dir, 'src')
mbligh8baa2ea2006-12-17 23:01:24 +000059 self.build_dir = os.path.join(tmp_dir, build_dir)
mbligha2508052006-05-28 21:29:53 +000060 # created by get_kernel_tree
mbligh09f288a2007-09-18 21:34:57 +000061 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
apw87c65c12007-09-27 17:19:37 +000066 self.installed_as = None
67
mbligh1e8858e2006-11-24 22:18:35 +000068 if not leave:
69 if os.path.isdir(self.src_dir):
70 system('rm -rf ' + self.src_dir)
71 if os.path.isdir(self.build_dir):
72 system('rm -rf ' + self.build_dir)
73
mbligh30f28c52007-10-11 18:35:35 +000074 if not os.path.exists(self.src_dir):
75 os.mkdir(self.src_dir)
mbligh1e8858e2006-11-24 22:18:35 +000076 for path in [self.config_dir, self.log_dir, self.results_dir]:
77 if os.path.exists(path):
78 system('rm -rf ' + path)
79 os.mkdir(path)
mblighf4c35322006-03-13 01:01:10 +000080
mbligh4426de02006-10-10 07:18:28 +000081 logpath = os.path.join(self.log_dir, 'build_log')
82 self.logfile = open(logpath, 'w+')
83
mbligh72b88fc2006-12-16 18:41:35 +000084 self.target_arch = None
mblighfdbcaec2006-10-01 23:28:57 +000085 self.build_target = 'bzImage'
mbligh8baa2ea2006-12-17 23:01:24 +000086 self.build_image = None
mblighfdbcaec2006-10-01 23:28:57 +000087
apwe8956122007-12-13 19:00:15 +000088 arch = get_current_kernel_arch()
89 if arch == 's390' or arch == 's390x':
90 self.build_target = 'image'
91 elif arch == 'ia64':
mblighcac347a2007-06-02 17:21:48 +000092 self.build_target = 'all'
93 self.build_image = 'vmlinux.gz'
94
mblighfdbcaec2006-10-01 23:28:57 +000095 if leave:
96 return
mbligh534015f2006-09-15 03:28:56 +000097
mbligh4426de02006-10-10 07:18:28 +000098 self.logfile.write('BASE: %s\n' % base_tree)
apw2366d992007-03-12 20:35:57 +000099
100 # Where we have direct version hint record that
101 # for later configuration selection.
102 shorthand = re.compile(r'^\d+\.\d+\.\d+')
103 if shorthand.match(base_tree):
104 self.base_tree_version = base_tree
105 else:
106 self.base_tree_version = None
107
apw040dcaa2007-11-21 19:36:55 +0000108 # Actually extract the tree. Make sure we know it occured
109 self.extract(base_tree)
110
111
apw7bae90e2008-03-05 12:18:11 +0000112 def kernelexpand(self, kernel):
113 # If we have something like a path, just use it as it is
mbligh6dee37f2008-03-10 18:09:13 +0000114 if '/' in kernel:
115 return [kernel]
apw7bae90e2008-03-05 12:18:11 +0000116
117 # Find the configured mirror list.
118 mirrors = self.job.config_get('mirror.mirrors')
119 if not mirrors:
120 # LEGACY: convert the kernel.org mirror
121 mirror = self.job.config_get('mirror.ftp_kernel_org')
apw37451692008-03-28 12:25:50 +0000122 if mirror:
123 korg = 'http://www.kernel.org/pub/linux/kernel'
124 mirrors = [
125 [ korg + '/v2.6', mirror + '/v2.6' ],
126 [ korg + '/people/akpm/patches/2.6',
127 mirror + '/akpm' ],
128 [ korg + '/people/mbligh',
129 mirror + '/mbligh' ],
130 ]
apw7bae90e2008-03-05 12:18:11 +0000131
132 patches = kernelexpand.expand_classic(kernel, mirrors)
133 print patches
134
135 return patches
136
137
apw040dcaa2007-11-21 19:36:55 +0000138 @logging.record
139 @tee_output_logdir_mark
140 def extract(self, base_tree):
mbligh534015f2006-09-15 03:28:56 +0000141 if os.path.exists(base_tree):
142 self.get_kernel_tree(base_tree)
143 else:
apw7bae90e2008-03-05 12:18:11 +0000144 base_components = self.kernelexpand(base_tree)
mbligh534015f2006-09-15 03:28:56 +0000145 print 'kernelexpand: '
146 print base_components
147 self.get_kernel_tree(base_components.pop(0))
148 if base_components: # apply remaining patches
149 self.patch(*base_components)
mblighf4c35322006-03-13 01:01:10 +0000150
151
mbligh119c12a2007-11-12 22:13:44 +0000152 @logging.record
apw001e40a2007-11-21 19:24:37 +0000153 @tee_output_logdir_mark
mblighb8e0a112007-11-05 20:27:36 +0000154 def patch(self, *patches):
mblighc86b0b42006-07-28 17:35:28 +0000155 """Apply a list of patches (in order)"""
mbligh1e8858e2006-11-24 22:18:35 +0000156 if not patches:
157 return
mbligh534015f2006-09-15 03:28:56 +0000158 print 'Applying patches: ', patches
mbligh0763e732007-08-30 16:35:06 +0000159 self.apply_patches(self.get_patches(patches))
mblighf4c35322006-03-13 01:01:10 +0000160
161
mbligh119c12a2007-11-12 22:13:44 +0000162 @logging.record
apw001e40a2007-11-21 19:24:37 +0000163 @tee_output_logdir_mark
mblighb8e0a112007-11-05 20:27:36 +0000164 def config(self, config_file = '', config_list = None, defconfig = False):
mbligh678823f2006-12-07 18:49:00 +0000165 self.set_cross_cc()
apwb449c7d2006-12-05 12:21:44 +0000166 config = kernel_config.kernel_config(self.job, self.build_dir,
apw2366d992007-03-12 20:35:57 +0000167 self.config_dir, config_file, config_list,
168 defconfig, self.base_tree_version)
mblighf4c35322006-03-13 01:01:10 +0000169
170
171 def get_patches(self, patches):
mbligh1e8858e2006-11-24 22:18:35 +0000172 """fetch the patches to the local src_dir"""
mblighf4c35322006-03-13 01:01:10 +0000173 local_patches = []
174 for patch in patches:
mbligh1e8858e2006-11-24 22:18:35 +0000175 dest = os.path.join(self.src_dir, basename(patch))
mbligh0763e732007-08-30 16:35:06 +0000176 # FIXME: this isn't unique. Append something to it
177 # like wget does if it's not there?
mbligh1e8858e2006-11-24 22:18:35 +0000178 print "get_file %s %s %s %s" % (patch, dest, self.src_dir, basename(patch))
mblighf4c35322006-03-13 01:01:10 +0000179 get_file(patch, dest)
mbligh0763e732007-08-30 16:35:06 +0000180 # probably safer to use the command, not python library
181 md5sum = system_output('md5sum ' + dest).split()[0]
182 local_patches.append((patch, dest, md5sum))
mbligh534015f2006-09-15 03:28:56 +0000183 return local_patches
mblighf4c35322006-03-13 01:01:10 +0000184
mbligh72b88fc2006-12-16 18:41:35 +0000185
mbligh534015f2006-09-15 03:28:56 +0000186 def apply_patches(self, local_patches):
mblighc86b0b42006-07-28 17:35:28 +0000187 """apply the list of patches, in order"""
mblighf4c35322006-03-13 01:01:10 +0000188 builddir = self.build_dir
189 os.chdir(builddir)
190
mbligh534015f2006-09-15 03:28:56 +0000191 if not local_patches:
mblighf4c35322006-03-13 01:01:10 +0000192 return None
mbligh0763e732007-08-30 16:35:06 +0000193 for (spec, local, md5sum) in local_patches:
194 if local.endswith('.bz2') or local.endswith('.gz'):
195 ref = spec
196 else:
197 ref = force_copy(local, self.results_dir)
198 ref = self.job.relative_path(ref)
199 log = 'PATCH: %s %s %s\n' % (spec, ref, md5sum)
200 print log
201 cat_file_to_cmd(local, 'patch -p1 > /dev/null')
202 self.logfile.write(log)
mbligh72b88fc2006-12-16 18:41:35 +0000203
204
205 def get_kernel_tree(self, base_tree):
mblighc49af7b2006-11-24 04:02:21 +0000206 """Extract/link base_tree to self.build_dir"""
mbligh5970cf02006-08-06 15:39:22 +0000207
208 # if base_tree is a dir, assume uncompressed kernel
209 if os.path.isdir(base_tree):
210 print 'Symlinking existing kernel source'
mblighc49af7b2006-11-24 04:02:21 +0000211 os.symlink(base_tree, self.build_dir)
mblighf4c35322006-03-13 01:01:10 +0000212
mbligh5970cf02006-08-06 15:39:22 +0000213 # otherwise, extract tarball
214 else:
mblighc49af7b2006-11-24 04:02:21 +0000215 os.chdir(os.path.dirname(self.src_dir))
216 # Figure out local destination for tarball
217 tarball = os.path.join(self.src_dir, os.path.basename(base_tree))
mbligh5970cf02006-08-06 15:39:22 +0000218 get_file(base_tree, tarball)
mbligh5970cf02006-08-06 15:39:22 +0000219 print 'Extracting kernel tarball:', tarball, '...'
mblighc49af7b2006-11-24 04:02:21 +0000220 extract_tarball_to_dir(tarball, self.build_dir)
mbligh5970cf02006-08-06 15:39:22 +0000221
mblighf4c35322006-03-13 01:01:10 +0000222
mblighfdbcaec2006-10-01 23:28:57 +0000223 def extraversion(self, tag, append=1):
224 os.chdir(self.build_dir)
mbligh4426de02006-10-10 07:18:28 +0000225 extraversion_sub = r's/^EXTRAVERSION =\s*\(.*\)/EXTRAVERSION = '
mblighfdbcaec2006-10-01 23:28:57 +0000226 if append:
mbligh4426de02006-10-10 07:18:28 +0000227 p = extraversion_sub + '\\1-%s/' % tag
mblighfdbcaec2006-10-01 23:28:57 +0000228 else:
mbligh4426de02006-10-10 07:18:28 +0000229 p = extraversion_sub + '-%s/' % tag
mbligh4c3fe4a2007-07-31 17:59:21 +0000230 system('mv Makefile Makefile.old')
231 system('sed "%s" < Makefile.old > Makefile' % p)
mblighfdbcaec2006-10-01 23:28:57 +0000232
233
mbligh119c12a2007-11-12 22:13:44 +0000234 @logging.record
apw001e40a2007-11-21 19:24:37 +0000235 @tee_output_logdir_mark
mblighb8e0a112007-11-05 20:27:36 +0000236 def build(self, make_opts = '', logfile = '', extraversion='autotest'):
mblighc86b0b42006-07-28 17:35:28 +0000237 """build the kernel
mbligh72b88fc2006-12-16 18:41:35 +0000238
mblighc86b0b42006-07-28 17:35:28 +0000239 make_opts
240 additional options to make, if any
241 """
mbligh10038012006-10-19 03:32:45 +0000242 os_dep.commands('gcc', 'make')
mbligh6d4c9412006-09-13 23:08:44 +0000243 if logfile == '':
244 logfile = os.path.join(self.log_dir, 'kernel_build')
mblighf4c35322006-03-13 01:01:10 +0000245 os.chdir(self.build_dir)
mblighfdbcaec2006-10-01 23:28:57 +0000246 if extraversion:
247 self.extraversion(extraversion)
mbligha2508052006-05-28 21:29:53 +0000248 self.set_cross_cc()
mblighf4c35322006-03-13 01:01:10 +0000249 # setup_config_file(config_file, config_overrides)
apwc7846102006-04-06 18:22:13 +0000250
251 # Not needed on 2.6, but hard to tell -- handle failure
mblighfdbcaec2006-10-01 23:28:57 +0000252 system('make dep', ignorestatus=1)
mblighf4c35322006-03-13 01:01:10 +0000253 threads = 2 * count_cpus()
mbligh6d4c9412006-09-13 23:08:44 +0000254 build_string = 'make -j %d %s %s' % (threads, make_opts,
255 self.build_target)
256 # eg make bzImage, or make zImage
257 print build_string
258 system(build_string)
mblighf4c35322006-03-13 01:01:10 +0000259 if kernel_config.modules_needed('.config'):
apwd4701972006-10-16 09:24:56 +0000260 system('make -j %d modules' % (threads))
apwc7846102006-04-06 18:22:13 +0000261
mbligh4426de02006-10-10 07:18:28 +0000262 kernel_version = self.get_kernel_build_ver()
263 kernel_version = re.sub('-autotest', '', kernel_version)
264 self.logfile.write('BUILD VERSION: %s\n' % kernel_version)
mbligh1e8858e2006-11-24 22:18:35 +0000265
266 force_copy(self.build_dir+'/System.map', self.results_dir)
mblighf4c35322006-03-13 01:01:10 +0000267
268
mbligh30f28c52007-10-11 18:35:35 +0000269 def build_timed(self, threads, timefile = '/dev/null', make_opts = '',
mbligh2e793a42007-10-12 23:58:35 +0000270 output = '/dev/null'):
mblighc86b0b42006-07-28 17:35:28 +0000271 """time the bulding of the kernel"""
mblighb8a14e32006-05-06 00:17:35 +0000272 os.chdir(self.build_dir)
mbligh678823f2006-12-07 18:49:00 +0000273 self.set_cross_cc()
mbligh30f28c52007-10-11 18:35:35 +0000274
mblighd79b2b42007-11-05 20:38:15 +0000275 self.clean(logged=False)
mbligh30f28c52007-10-11 18:35:35 +0000276 build_string = "/usr/bin/time -o %s make %s -j %s vmlinux" \
277 % (timefile, make_opts, threads)
mbligh2e793a42007-10-12 23:58:35 +0000278 build_string += ' > %s 2>&1' % output
mblighf4c35322006-03-13 01:01:10 +0000279 print build_string
apwc7846102006-04-06 18:22:13 +0000280 system(build_string)
mbligh30f28c52007-10-11 18:35:35 +0000281
apwc7846102006-04-06 18:22:13 +0000282 if (not os.path.isfile('vmlinux')):
283 raise TestError("no vmlinux found, kernel build failed")
mblighb8a14e32006-05-06 00:17:35 +0000284
285
mbligh119c12a2007-11-12 22:13:44 +0000286 @logging.record
apw001e40a2007-11-21 19:24:37 +0000287 @tee_output_logdir_mark
mblighb8e0a112007-11-05 20:27:36 +0000288 def clean(self):
mblighc86b0b42006-07-28 17:35:28 +0000289 """make clean in the kernel tree"""
mblighb8a14e32006-05-06 00:17:35 +0000290 os.chdir(self.build_dir)
mblighf4c35322006-03-13 01:01:10 +0000291 print "make clean"
mbligh30f28c52007-10-11 18:35:35 +0000292 system('make clean > /dev/null 2> /dev/null')
mblighf4c35322006-03-13 01:01:10 +0000293
mbligh50f42ea2006-09-30 22:22:21 +0000294
mbligh119c12a2007-11-12 22:13:44 +0000295 @logging.record
apw001e40a2007-11-21 19:24:37 +0000296 @tee_output_logdir_mark
mblighb8e0a112007-11-05 20:27:36 +0000297 def mkinitrd(self, version, image, system_map, initrd):
mbligh50f42ea2006-09-30 22:22:21 +0000298 """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 """
310 vendor = get_os_vendor()
311
312 if os.path.isfile(initrd):
mblighfdbcaec2006-10-01 23:28:57 +0000313 print "Existing %s file, will remove it." % initrd
mbligh50f42ea2006-09-30 22:22:21 +0000314 os.remove(initrd)
mbligh72b88fc2006-12-16 18:41:35 +0000315
apwe43a30b2007-09-25 16:51:30 +0000316 args = self.job.config_get('kernel.mkinitrd_extra_args')
317
mbligh3d515d42007-11-09 17:00:36 +0000318 # don't leak 'None' into mkinitrd command
319 if not args:
320 args = ''
321
mbligh50f42ea2006-09-30 22:22:21 +0000322 if vendor in ['Red Hat', 'Fedora Core']:
apwe43a30b2007-09-25 16:51:30 +0000323 system('mkinitrd %s %s %s' % (args, initrd, version))
mbligh50f42ea2006-09-30 22:22:21 +0000324 elif vendor in ['SUSE']:
apwe43a30b2007-09-25 16:51:30 +0000325 system('mkinitrd %s -k %s -i %s -M %s' % (args, image, initrd, system_map))
apwc898a1f2007-02-28 15:27:22 +0000326 elif vendor in ['Debian', 'Ubuntu']:
327 if os.path.isfile('/usr/sbin/mkinitrd'):
328 cmd = '/usr/sbin/mkinitrd'
329 elif os.path.isfile('/usr/sbin/mkinitramfs'):
330 cmd = '/usr/sbin/mkinitramfs'
331 else:
332 raise TestError('No Debian initrd builder')
apwe43a30b2007-09-25 16:51:30 +0000333 system('%s %s -o %s %s' % (cmd, args, initrd, version))
mbligh50f42ea2006-09-30 22:22:21 +0000334 else:
mblighfdbcaec2006-10-01 23:28:57 +0000335 raise TestError('Unsupported vendor %s' % vendor)
mbligh50f42ea2006-09-30 22:22:21 +0000336
337
mbligh8baa2ea2006-12-17 23:01:24 +0000338 def set_build_image(self, image):
339 self.build_image = image
340
341
mbligh119c12a2007-11-12 22:13:44 +0000342 @logging.record
apw001e40a2007-11-21 19:24:37 +0000343 @tee_output_logdir_mark
mblighb8e0a112007-11-05 20:27:36 +0000344 def install(self, tag='autotest', prefix = '/'):
mblighc86b0b42006-07-28 17:35:28 +0000345 """make install in the kernel tree"""
apw87c65c12007-09-27 17:19:37 +0000346
347 # Record that we have installed the kernel, and
348 # the tag under which we installed it.
349 self.installed_as = tag
350
mblighf4c35322006-03-13 01:01:10 +0000351 os.chdir(self.build_dir)
mbligh72b88fc2006-12-16 18:41:35 +0000352
mbligh6a1d4db2006-10-06 04:30:16 +0000353 if not os.path.isdir(prefix):
354 os.mkdir(prefix)
mbligha87116f2006-10-10 02:47:08 +0000355 self.boot_dir = os.path.join(prefix, 'boot')
356 if not os.path.isdir(self.boot_dir):
357 os.mkdir(self.boot_dir)
mbligh0ad65582006-10-06 04:16:36 +0000358
mbligh8baa2ea2006-12-17 23:01:24 +0000359 if not self.build_image:
360 images = glob.glob('arch/*/boot/' + self.build_target)
361 if len(images):
362 self.build_image = images[0]
363 else:
364 self.build_image = self.build_target
mbligha87116f2006-10-10 02:47:08 +0000365
366 # remember installed files
mbligha87116f2006-10-10 02:47:08 +0000367 self.vmlinux = self.boot_dir + '/vmlinux-' + tag
mbligh8baa2ea2006-12-17 23:01:24 +0000368 if (self.build_image != 'vmlinux'):
apw9a61c5b2006-11-28 10:03:15 +0000369 self.image = self.boot_dir + '/vmlinuz-' + tag
370 else:
371 self.image = self.vmlinux
mbligha87116f2006-10-10 02:47:08 +0000372 self.system_map = self.boot_dir + '/System.map-' + tag
373 self.config = self.boot_dir + '/config-' + tag
374 self.initrd = ''
375
376 # copy to boot dir
mbligha87116f2006-10-10 02:47:08 +0000377 force_copy('vmlinux', self.vmlinux)
mbligh8baa2ea2006-12-17 23:01:24 +0000378 if (self.build_image != 'vmlinux'):
379 force_copy(self.build_image, self.image)
mbligha87116f2006-10-10 02:47:08 +0000380 force_copy('System.map', self.system_map)
381 force_copy('.config', self.config)
382
mbligh6a1d4db2006-10-06 04:30:16 +0000383 if not kernel_config.modules_needed('.config'):
384 return
385
386 system('make modules_install INSTALL_MOD_PATH=%s' % prefix)
387 if prefix == '/':
mbligha87116f2006-10-10 02:47:08 +0000388 self.initrd = self.boot_dir + '/initrd-' + tag
mblighb8e0a112007-11-05 20:27:36 +0000389 self.mkinitrd(self.get_kernel_build_ver(), self.image,
390 self.system_map, self.initrd)
mbligh5925e962007-08-30 17:05:22 +0000391
392
mbligha87116f2006-10-10 02:47:08 +0000393 def add_to_bootloader(self, tag='autotest', args=''):
394 """ add this kernel to bootloader, taking an
395 optional parameter of space separated parameters
396 e.g.: kernel.add_to_bootloader('mykernel', 'ro acpi=off')
397 """
398
399 # remove existing entry if present
400 self.job.bootloader.remove_kernel(tag)
401
apwcbe32572006-11-28 10:00:23 +0000402 # pull the base argument set from the job config,
apwcbe32572006-11-28 10:00:23 +0000403 baseargs = self.job.config_get('boot.default_args')
mbligh78bc05e2006-12-25 02:29:59 +0000404 if baseargs:
405 args = baseargs + " " + args
406
407 # otherwise populate from /proc/cmdline
408 # if not baseargs:
409 # baseargs = open('/proc/cmdline', 'r').readline().strip()
410 # NOTE: This is unnecessary, because boottool does it.
apwcbe32572006-11-28 10:00:23 +0000411
mbligh78bc05e2006-12-25 02:29:59 +0000412 root = None
413 roots = [x for x in args.split() if x.startswith('root=')]
414 if roots:
415 root = re.sub('^root=', '', roots[0])
416 arglist = [x for x in args.split() if not x.startswith('root=')]
417 args = ' '.join(arglist)
mbligha87116f2006-10-10 02:47:08 +0000418
mbligh78bc05e2006-12-25 02:29:59 +0000419 # add the kernel entry
420 # add_kernel(image, title='autotest', initrd='')
421 self.job.bootloader.add_kernel(self.image, tag, self.initrd, \
422 args = args, root = root)
mbligh6a1d4db2006-10-06 04:30:16 +0000423
mbligh201aa892006-10-29 04:02:05 +0000424
mbligh548f29a2006-10-17 04:55:12 +0000425 def get_kernel_build_arch(self, arch=None):
mbligh201aa892006-10-29 04:02:05 +0000426 """
427 Work out the current kernel architecture (as a kernel arch)
428 """
mbligh548f29a2006-10-17 04:55:12 +0000429 if not arch:
430 arch = get_current_kernel_arch()
431 if re.match('i.86', arch):
432 return 'i386'
433 elif re.match('sun4u', arch):
434 return 'sparc64'
435 elif re.match('arm.*', arch):
436 return 'arm'
437 elif re.match('sa110', arch):
438 return 'arm'
439 elif re.match('s390x', arch):
440 return 's390'
441 elif re.match('parisc64', arch):
442 return 'parisc'
443 elif re.match('ppc.*', arch):
444 return 'powerpc'
445 elif re.match('mips.*', arch):
446 return 'mips'
447 else:
448 return arch
449
mbligh6a1d4db2006-10-06 04:30:16 +0000450
mbligh237bed32007-09-05 13:05:57 +0000451 def get_kernel_build_release(self):
452 releasem = re.compile(r'.*UTS_RELEASE\s+"([^"]+)".*');
453 versionm = re.compile(r'.*UTS_VERSION\s+"([^"]+)".*');
454
455 release = None
456 version = None
457
458 for file in [ self.build_dir + "/include/linux/version.h",
459 self.build_dir + "/include/linux/utsrelease.h",
460 self.build_dir + "/include/linux/compile.h" ]:
461 if os.path.exists(file):
462 fd = open(file, 'r')
463 for line in fd.readlines():
464 m = releasem.match(line)
465 if m:
466 release = m.groups()[0]
467 m = versionm.match(line)
468 if m:
469 version = m.groups()[0]
470 fd.close()
471
472 return (release, version)
473
474
475 def get_kernel_build_ident(self):
476 (release, version) = self.get_kernel_build_release()
477
478 if not release or not version:
479 raise JobError('kernel has no identity')
480
481 return release + '::' + version
482
483
apw11985b72007-10-04 15:44:47 +0000484 def boot(self, args='', ident=1):
apw1b5dc362006-10-31 11:24:26 +0000485 """ install and boot this kernel, do not care how
486 just make it happen.
487 """
488
mbligh237bed32007-09-05 13:05:57 +0000489 # If we can check the kernel identity do so.
490 if ident:
491 when = int(time.time())
492 ident = self.get_kernel_build_ident()
493 args += " IDENT=%d" % (when)
494
apwce73d892007-09-25 16:53:05 +0000495 self.job.next_step_prepend(["job.kernel_check_ident",
mbligh38a4a112008-03-19 13:11:34 +0000496 when, ident, self.subdir])
mbligh237bed32007-09-05 13:05:57 +0000497
apw87c65c12007-09-27 17:19:37 +0000498 # Check if the kernel has been installed, if not install
499 # as the default tag and boot that.
500 if not self.installed_as:
501 self.install()
502
503 # Boot the selected tag.
504 self.add_to_bootloader(args=args, tag=self.installed_as)
apw1b5dc362006-10-31 11:24:26 +0000505
506 # Boot it.
apw11985b72007-10-04 15:44:47 +0000507 self.job.reboot(tag=self.installed_as)
apw1b5dc362006-10-31 11:24:26 +0000508
509
mblighfdbcaec2006-10-01 23:28:57 +0000510 def get_kernel_build_ver(self):
mblighe11f5fc2006-10-04 04:42:22 +0000511 """Check Makefile and .config to return kernel version"""
512 version = patchlevel = sublevel = extraversion = localversion = ''
513
514 for line in open(self.build_dir + '/Makefile', 'r').readlines():
515 if line.startswith('VERSION'):
516 version = line[line.index('=') + 1:].strip()
517 if line.startswith('PATCHLEVEL'):
518 patchlevel = line[line.index('=') + 1:].strip()
519 if line.startswith('SUBLEVEL'):
520 sublevel = line[line.index('=') + 1:].strip()
521 if line.startswith('EXTRAVERSION'):
522 extraversion = line[line.index('=') + 1:].strip()
523
524 for line in open(self.build_dir + '/.config', 'r').readlines():
525 if line.startswith('CONFIG_LOCALVERSION='):
526 localversion = line.rstrip().split('"')[1]
527
mbligh72b88fc2006-12-16 18:41:35 +0000528 return "%s.%s.%s%s%s" %(version, patchlevel, sublevel, extraversion, localversion)
mblighfdbcaec2006-10-01 23:28:57 +0000529
530
mbligh8baa2ea2006-12-17 23:01:24 +0000531 def set_build_target(self, build_target):
532 if build_target:
533 self.build_target = build_target
534 print 'BUILD TARGET: %s' % self.build_target
535
536
mbligh5970cf02006-08-06 15:39:22 +0000537 def set_cross_cc(self, target_arch=None, cross_compile=None,
538 build_target='bzImage'):
mblighc86b0b42006-07-28 17:35:28 +0000539 """Set up to cross-compile.
mblighcc2e6662006-09-14 01:24:07 +0000540 This is broken. We need to work out what the default
541 compile produces, and if not, THEN set the cross
542 compiler.
mblighc86b0b42006-07-28 17:35:28 +0000543 """
mblighcc2e6662006-09-14 01:24:07 +0000544
mbligh5970cf02006-08-06 15:39:22 +0000545 if self.target_arch:
546 return
mbligh678823f2006-12-07 18:49:00 +0000547
mbligh8baa2ea2006-12-17 23:01:24 +0000548 # if someone has set build_target, don't clobber in set_cross_cc
549 # run set_build_target before calling set_cross_cc
550 if not self.build_target:
551 self.set_build_target(build_target)
mbligh72b88fc2006-12-16 18:41:35 +0000552
mbligh5970cf02006-08-06 15:39:22 +0000553 # If no 'target_arch' given assume native compilation
554 if target_arch == None:
mbligh548f29a2006-10-17 04:55:12 +0000555 target_arch = get_current_kernel_arch()
mbligh5970cf02006-08-06 15:39:22 +0000556 if target_arch == 'ppc64':
mbligh5970cf02006-08-06 15:39:22 +0000557 if self.build_target == 'bzImage':
apw9a61c5b2006-11-28 10:03:15 +0000558 self.build_target = 'vmlinux'
mbligh678823f2006-12-07 18:49:00 +0000559
560 if not cross_compile:
561 cross_compile = self.job.config_get('kernel.cross_cc')
562
563 if cross_compile:
564 os.environ['CROSS_COMPILE'] = cross_compile
565 else:
566 if os.environ.has_key('CROSS_COMPILE'):
567 del os.environ['CROSS_COMPILE']
568
mblighcc2e6662006-09-14 01:24:07 +0000569 return # HACK. Crap out for now.
mblighf4c35322006-03-13 01:01:10 +0000570
mblighcc2e6662006-09-14 01:24:07 +0000571 # At this point I know what arch I *want* to build for
572 # but have no way of working out what arch the default
573 # compiler DOES build for.
574
575 # Oh, and BTW, install_package() doesn't exist yet.
mbligh72b88fc2006-12-16 18:41:35 +0000576
mblighcc2e6662006-09-14 01:24:07 +0000577 if target_arch == 'ppc64':
578 install_package('ppc64-cross')
mbligh678823f2006-12-07 18:49:00 +0000579 cross_compile = os.path.join(self.autodir, 'sources/ppc64-cross/bin')
mblighcc2e6662006-09-14 01:24:07 +0000580
581 elif target_arch == 'x86_64':
582 install_package('x86_64-cross')
mbligh678823f2006-12-07 18:49:00 +0000583 cross_compile = os.path.join(self.autodir, 'sources/x86_64-cross/bin')
mblighb8a14e32006-05-06 00:17:35 +0000584
mbligh5970cf02006-08-06 15:39:22 +0000585 os.environ['ARCH'] = self.target_arch = target_arch
586
587 self.cross_compile = cross_compile
588 if self.cross_compile:
589 os.environ['CROSS_COMPILE'] = self.cross_compile
mblighcc2e6662006-09-14 01:24:07 +0000590
mbligh72b88fc2006-12-16 18:41:35 +0000591
mblighb8a14e32006-05-06 00:17:35 +0000592 def pickle_dump(self, filename):
mblighc86b0b42006-07-28 17:35:28 +0000593 """dump a pickle of ourself out to the specified filename
594
595 we can't pickle the backreference to job (it contains fd's),
mbligh709bb9b2006-10-12 04:32:16 +0000596 nor would we want to. Same for logfile (fd's).
mblighc86b0b42006-07-28 17:35:28 +0000597 """
mblighb8a14e32006-05-06 00:17:35 +0000598 temp = copy.copy(self)
599 temp.job = None
mbligh709bb9b2006-10-12 04:32:16 +0000600 temp.logfile = None
mblighb8a14e32006-05-06 00:17:35 +0000601 pickle.dump(temp, open(filename, 'w'))
mbligh736adc92007-10-18 03:23:22 +0000602
603
604class rpm_kernel:
605 """ Class for installing rpm kernel package
606 """
607
mblighda0311e2007-10-25 16:03:33 +0000608 def __init__(self, job, rpm_package, subdir):
mbligh736adc92007-10-18 03:23:22 +0000609 self.job = job
mbligh6ee7ee02007-11-13 23:49:05 +0000610 self.rpm_package = rpm_package
mblighda0311e2007-10-25 16:03:33 +0000611 self.log_dir = os.path.join(subdir, 'debug')
612 self.subdir = os.path.basename(subdir)
mbligh736adc92007-10-18 03:23:22 +0000613 if os.path.exists(self.log_dir):
614 system('rm -rf ' + self.log_dir)
615 os.mkdir(self.log_dir)
mblighda0311e2007-10-25 16:03:33 +0000616 self.installed_as = None
mbligh736adc92007-10-18 03:23:22 +0000617
618
mbligh119c12a2007-11-12 22:13:44 +0000619 @logging.record
apw001e40a2007-11-21 19:24:37 +0000620 @tee_output_logdir_mark
mblighda0311e2007-10-25 16:03:33 +0000621 def install(self, tag='autotest'):
622 self.installed_as = tag
623
mblighda0311e2007-10-25 16:03:33 +0000624 self.rpm_name = system_output('rpm -qp ' + self.rpm_package)
mbligh736adc92007-10-18 03:23:22 +0000625
626 # install
627 system('rpm -i --force ' + self.rpm_package)
628
629 # get file list
mblighda0311e2007-10-25 16:03:33 +0000630 files = system_output('rpm -ql ' + self.rpm_name).splitlines()
mbligh736adc92007-10-18 03:23:22 +0000631
mbligh736adc92007-10-18 03:23:22 +0000632 # search for vmlinuz
633 for file in files:
634 if file.startswith('/boot/vmlinuz'):
635 self.image = file
636 break
637 else:
638 raise TestError(self.rpm_package + " doesn't contain /boot/vmlinuz")
mblighda0311e2007-10-25 16:03:33 +0000639
mbligh736adc92007-10-18 03:23:22 +0000640 # search for initrd
641 self.initrd = ''
642 for file in files:
643 if file.startswith('/boot/initrd'):
644 self.initrd = file
645 break
646
647 # get version and release number
648 self.version, self.release = system_output(
mblighda0311e2007-10-25 16:03:33 +0000649 'rpm --queryformat="%{VERSION}\\n%{RELEASE}\\n" -q ' + self.rpm_name).splitlines()[0:2]
mbligh736adc92007-10-18 03:23:22 +0000650
651
652 def add_to_bootloader(self, tag='autotest', args=''):
653 """ Add this kernel to bootloader
654 """
655
656 # remove existing entry if present
657 self.job.bootloader.remove_kernel(tag)
658
659 # pull the base argument set from the job config
660 baseargs = self.job.config_get('boot.default_args')
661 if baseargs:
662 args = baseargs + ' ' + args
663
664 # otherwise populate from /proc/cmdline
665 # if not baseargs:
666 # baseargs = open('/proc/cmdline', 'r').readline().strip()
667 # NOTE: This is unnecessary, because boottool does it.
668
669 root = None
670 roots = [x for x in args.split() if x.startswith('root=')]
671 if roots:
672 root = re.sub('^root=', '', roots[0])
673 arglist = [x for x in args.split() if not x.startswith('root=')]
674 args = ' '.join(arglist)
675
676 # add the kernel entry
677 self.job.bootloader.add_kernel(self.image, tag, self.initrd, args = args, root = root)
mbligh10a24a72007-10-24 21:02:53 +0000678
679
mblighda0311e2007-10-25 16:03:33 +0000680 def boot(self, args='', ident=1):
681 """ install and boot this kernel
682 """
mbligh73e82a32007-11-08 21:35:29 +0000683
mbligh10a24a72007-10-24 21:02:53 +0000684 # Check if the kernel has been installed, if not install
685 # as the default tag and boot that.
mblighda0311e2007-10-25 16:03:33 +0000686 if not self.installed_as:
mbligh73e82a32007-11-08 21:35:29 +0000687 self.install()
mblighda0311e2007-10-25 16:03:33 +0000688
689 # If we can check the kernel identity do so.
690 if ident:
691 when = int(time.time())
mbligh38a4a112008-03-19 13:11:34 +0000692 ident = '-'.join([self.version,
693 self.rpm_name.split('-')[1],
694 self.release])
mblighda0311e2007-10-25 16:03:33 +0000695 args += " IDENT=%d" % (when)
696
697 self.job.next_step_prepend(["job.kernel_check_ident",
mbligh38a4a112008-03-19 13:11:34 +0000698 when, ident, self.subdir, 'rpm'])
mbligh10a24a72007-10-24 21:02:53 +0000699
700 # Boot the selected tag.
mblighda0311e2007-10-25 16:03:33 +0000701 self.add_to_bootloader(args=args, tag=self.installed_as)
mbligh10a24a72007-10-24 21:02:53 +0000702
703 # Boot it.
mblighda0311e2007-10-25 16:03:33 +0000704 self.job.reboot(tag=self.installed_as)
mbligh6ee7ee02007-11-13 23:49:05 +0000705
706
707# pull in some optional site-specific path pre-processing
708try:
709 import site_kernel
710 preprocess_path = site_kernel.preprocess_path
711 del site_kernel
712except ImportError:
713 # just make the preprocessor a nop
714 def preprocess_path(path):
715 return path
716
717def auto_kernel(job, path, subdir, tmp_dir, build_dir, leave=False):
718 """\
719 Create a kernel object, dynamically selecting the appropriate class to use
720 based on the path provided.
721 """
722 path = preprocess_path(path)
723 if path.endswith('.rpm'):
724 return rpm_kernel(job, path, subdir)
725 else:
726 return kernel(job, path, subdir, tmp_dir, build_dir, leave)