blob: d9d818a75e314a5527b41b6baf676454ec2b594e [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 *
mbligh10038012006-10-19 03:32:45 +00005import kernel_config, test, os_dep
mblighf4c35322006-03-13 01:01:10 +00006
mblighb8e0a112007-11-05 20:27:36 +00007
8def record(fn):
9 """ Decorator for logging calls to specific kernel methods.
mblighd79b2b42007-11-05 20:38:15 +000010 It also accepts a "logged=False" keyword argument to disable
11 the logging for particular calls.
mblighb8e0a112007-11-05 20:27:36 +000012
13 Classes that make use of this dectorator will need to have job
14 and subdir attributes for the logging to function correctly.
15 """
16 def recorded_func(self, *args, **dargs):
mblighd79b2b42007-11-05 20:38:15 +000017 logged = dargs.pop('logged', True)
18 if not logged:
19 return fn(self, *args, **dargs)
20 # wrap the method call in success/failure logging
mblighb8e0a112007-11-05 20:27:36 +000021 name = "kernel.%s" % fn.__name__
22 try:
mblighd79b2b42007-11-05 20:38:15 +000023 result = fn(self, *args, **dargs)
mblighb8e0a112007-11-05 20:27:36 +000024 self.job.record('GOOD', self.subdir, name)
25 except Exception, detail:
26 self.job.record('FAIL', self.subdir, name, str(detail))
27 raise
mblighd79b2b42007-11-05 20:38:15 +000028 return result
mblighb8e0a112007-11-05 20:27:36 +000029 return recorded_func
30
31
mblighf4c35322006-03-13 01:01:10 +000032class kernel:
mblighc86b0b42006-07-28 17:35:28 +000033 """ Class for compiling kernels.
34
35 Data for the object includes the src files
36 used to create the kernel, patches applied, config (base + changes),
37 the build directory itself, and logged output
38
39 Properties:
40 job
41 Backpointer to the job object we're part of
42 autodir
43 Path to the top level autotest dir (/usr/local/autotest)
mblighc86b0b42006-07-28 17:35:28 +000044 src_dir
mbligh1e8858e2006-11-24 22:18:35 +000045 <tmp_dir>/src/
mblighc49af7b2006-11-24 04:02:21 +000046 build_dir
mbligh1e8858e2006-11-24 22:18:35 +000047 <tmp_dir>/linux/
mblighc86b0b42006-07-28 17:35:28 +000048 config_dir
mbligh1e8858e2006-11-24 22:18:35 +000049 <results_dir>/config/
mblighc86b0b42006-07-28 17:35:28 +000050 log_dir
mbligh1e8858e2006-11-24 22:18:35 +000051 <results_dir>/debug/
52 results_dir
53 <results_dir>/results/
mblighc86b0b42006-07-28 17:35:28 +000054 """
55
mbligh8baa2ea2006-12-17 23:01:24 +000056 autodir = ''
57
mbligh09f288a2007-09-18 21:34:57 +000058 def __init__(self, job, base_tree, subdir, tmp_dir, build_dir, leave = False):
mblighc86b0b42006-07-28 17:35:28 +000059 """Initialize the kernel build environment
60
61 job
62 which job this build is part of
mblighc86b0b42006-07-28 17:35:28 +000063 base_tree
mbligh534015f2006-09-15 03:28:56 +000064 base kernel tree. Can be one of the following:
65 1. A local tarball
66 2. A URL to a tarball
67 3. A local directory (will symlink it)
68 4. A shorthand expandable (eg '2.6.11-git3')
mbligh09f288a2007-09-18 21:34:57 +000069 subdir
70 subdir in the results directory (eg "build")
71 (holds config/, debug/, results/)
mbligh1e8858e2006-11-24 22:18:35 +000072 tmp_dir
mbligh72b88fc2006-12-16 18:41:35 +000073
mbligh1e8858e2006-11-24 22:18:35 +000074 leave
75 Boolean, whether to leave existing tmpdir or not
mblighc86b0b42006-07-28 17:35:28 +000076 """
mblighf4c35322006-03-13 01:01:10 +000077 self.job = job
mbligh678823f2006-12-07 18:49:00 +000078 self.autodir = job.autodir
mblighf4c35322006-03-13 01:01:10 +000079
mbligh1e8858e2006-11-24 22:18:35 +000080 self.src_dir = os.path.join(tmp_dir, 'src')
mbligh8baa2ea2006-12-17 23:01:24 +000081 self.build_dir = os.path.join(tmp_dir, build_dir)
mbligha2508052006-05-28 21:29:53 +000082 # created by get_kernel_tree
mbligh09f288a2007-09-18 21:34:57 +000083 self.config_dir = os.path.join(subdir, 'config')
84 self.log_dir = os.path.join(subdir, 'debug')
85 self.results_dir = os.path.join(subdir, 'results')
86 self.subdir = os.path.basename(subdir)
mbligh1e8858e2006-11-24 22:18:35 +000087
apw87c65c12007-09-27 17:19:37 +000088 self.installed_as = None
89
mbligh1e8858e2006-11-24 22:18:35 +000090 if not leave:
91 if os.path.isdir(self.src_dir):
92 system('rm -rf ' + self.src_dir)
93 if os.path.isdir(self.build_dir):
94 system('rm -rf ' + self.build_dir)
95
mbligh30f28c52007-10-11 18:35:35 +000096 if not os.path.exists(self.src_dir):
97 os.mkdir(self.src_dir)
mbligh1e8858e2006-11-24 22:18:35 +000098 for path in [self.config_dir, self.log_dir, self.results_dir]:
99 if os.path.exists(path):
100 system('rm -rf ' + path)
101 os.mkdir(path)
mblighf4c35322006-03-13 01:01:10 +0000102
mbligh4426de02006-10-10 07:18:28 +0000103 logpath = os.path.join(self.log_dir, 'build_log')
104 self.logfile = open(logpath, 'w+')
105
mbligh72b88fc2006-12-16 18:41:35 +0000106 self.target_arch = None
mblighfdbcaec2006-10-01 23:28:57 +0000107 self.build_target = 'bzImage'
mbligh8baa2ea2006-12-17 23:01:24 +0000108 self.build_image = None
mblighfdbcaec2006-10-01 23:28:57 +0000109
mblighcac347a2007-06-02 17:21:48 +0000110 if get_current_kernel_arch() == 'ia64':
111 self.build_target = 'all'
112 self.build_image = 'vmlinux.gz'
113
mblighfdbcaec2006-10-01 23:28:57 +0000114 if leave:
115 return
mbligh534015f2006-09-15 03:28:56 +0000116
mbligh4426de02006-10-10 07:18:28 +0000117 self.logfile.write('BASE: %s\n' % base_tree)
apw2366d992007-03-12 20:35:57 +0000118
119 # Where we have direct version hint record that
120 # for later configuration selection.
121 shorthand = re.compile(r'^\d+\.\d+\.\d+')
122 if shorthand.match(base_tree):
123 self.base_tree_version = base_tree
124 else:
125 self.base_tree_version = None
126
mbligh534015f2006-09-15 03:28:56 +0000127 if os.path.exists(base_tree):
128 self.get_kernel_tree(base_tree)
129 else:
apw8ad56be2006-11-06 17:49:54 +0000130 args = self.job.config_get('mirror.ftp_kernel_org')
mbligh548f29a2006-10-17 04:55:12 +0000131 if args:
132 args = '-l ' + args
mbligh72b88fc2006-12-16 18:41:35 +0000133 base_components = kernelexpand(base_tree, args)
mbligh534015f2006-09-15 03:28:56 +0000134 print 'kernelexpand: '
135 print base_components
136 self.get_kernel_tree(base_components.pop(0))
137 if base_components: # apply remaining patches
138 self.patch(*base_components)
mblighf4c35322006-03-13 01:01:10 +0000139
140
mblighb8e0a112007-11-05 20:27:36 +0000141 @record
142 def patch(self, *patches):
mblighc86b0b42006-07-28 17:35:28 +0000143 """Apply a list of patches (in order)"""
mbligh1e8858e2006-11-24 22:18:35 +0000144 if not patches:
145 return
mbligh534015f2006-09-15 03:28:56 +0000146 print 'Applying patches: ', patches
mbligh0763e732007-08-30 16:35:06 +0000147 self.apply_patches(self.get_patches(patches))
mblighf4c35322006-03-13 01:01:10 +0000148
149
mblighb8e0a112007-11-05 20:27:36 +0000150 @record
151 def config(self, config_file = '', config_list = None, defconfig = False):
mbligh44da9372007-11-05 23:30:07 +0000152 self.job.stdout.tee_redirect(os.path.join(self.log_dir, 'stdout'))
mbligh678823f2006-12-07 18:49:00 +0000153 self.set_cross_cc()
apwb449c7d2006-12-05 12:21:44 +0000154 config = kernel_config.kernel_config(self.job, self.build_dir,
apw2366d992007-03-12 20:35:57 +0000155 self.config_dir, config_file, config_list,
156 defconfig, self.base_tree_version)
mblighf4c35322006-03-13 01:01:10 +0000157 self.job.stdout.restore()
158
159
160 def get_patches(self, patches):
mbligh1e8858e2006-11-24 22:18:35 +0000161 """fetch the patches to the local src_dir"""
mblighf4c35322006-03-13 01:01:10 +0000162 local_patches = []
163 for patch in patches:
mbligh1e8858e2006-11-24 22:18:35 +0000164 dest = os.path.join(self.src_dir, basename(patch))
mbligh0763e732007-08-30 16:35:06 +0000165 # FIXME: this isn't unique. Append something to it
166 # like wget does if it's not there?
mbligh1e8858e2006-11-24 22:18:35 +0000167 print "get_file %s %s %s %s" % (patch, dest, self.src_dir, basename(patch))
mblighf4c35322006-03-13 01:01:10 +0000168 get_file(patch, dest)
mbligh0763e732007-08-30 16:35:06 +0000169 # probably safer to use the command, not python library
170 md5sum = system_output('md5sum ' + dest).split()[0]
171 local_patches.append((patch, dest, md5sum))
mbligh534015f2006-09-15 03:28:56 +0000172 return local_patches
mblighf4c35322006-03-13 01:01:10 +0000173
mbligh72b88fc2006-12-16 18:41:35 +0000174
mbligh534015f2006-09-15 03:28:56 +0000175 def apply_patches(self, local_patches):
mblighc86b0b42006-07-28 17:35:28 +0000176 """apply the list of patches, in order"""
mblighf4c35322006-03-13 01:01:10 +0000177 builddir = self.build_dir
178 os.chdir(builddir)
179
mbligh534015f2006-09-15 03:28:56 +0000180 if not local_patches:
mblighf4c35322006-03-13 01:01:10 +0000181 return None
mbligh0763e732007-08-30 16:35:06 +0000182 for (spec, local, md5sum) in local_patches:
183 if local.endswith('.bz2') or local.endswith('.gz'):
184 ref = spec
185 else:
186 ref = force_copy(local, self.results_dir)
187 ref = self.job.relative_path(ref)
188 log = 'PATCH: %s %s %s\n' % (spec, ref, md5sum)
189 print log
190 cat_file_to_cmd(local, 'patch -p1 > /dev/null')
191 self.logfile.write(log)
mbligh72b88fc2006-12-16 18:41:35 +0000192
193
194 def get_kernel_tree(self, base_tree):
mblighc49af7b2006-11-24 04:02:21 +0000195 """Extract/link base_tree to self.build_dir"""
mbligh5970cf02006-08-06 15:39:22 +0000196
197 # if base_tree is a dir, assume uncompressed kernel
198 if os.path.isdir(base_tree):
199 print 'Symlinking existing kernel source'
mblighc49af7b2006-11-24 04:02:21 +0000200 os.symlink(base_tree, self.build_dir)
mblighf4c35322006-03-13 01:01:10 +0000201
mbligh5970cf02006-08-06 15:39:22 +0000202 # otherwise, extract tarball
203 else:
mblighc49af7b2006-11-24 04:02:21 +0000204 os.chdir(os.path.dirname(self.src_dir))
205 # Figure out local destination for tarball
206 tarball = os.path.join(self.src_dir, os.path.basename(base_tree))
mbligh5970cf02006-08-06 15:39:22 +0000207 get_file(base_tree, tarball)
mbligh5970cf02006-08-06 15:39:22 +0000208 print 'Extracting kernel tarball:', tarball, '...'
mblighc49af7b2006-11-24 04:02:21 +0000209 extract_tarball_to_dir(tarball, self.build_dir)
mbligh5970cf02006-08-06 15:39:22 +0000210
mblighf4c35322006-03-13 01:01:10 +0000211
mblighfdbcaec2006-10-01 23:28:57 +0000212 def extraversion(self, tag, append=1):
213 os.chdir(self.build_dir)
mbligh4426de02006-10-10 07:18:28 +0000214 extraversion_sub = r's/^EXTRAVERSION =\s*\(.*\)/EXTRAVERSION = '
mblighfdbcaec2006-10-01 23:28:57 +0000215 if append:
mbligh4426de02006-10-10 07:18:28 +0000216 p = extraversion_sub + '\\1-%s/' % tag
mblighfdbcaec2006-10-01 23:28:57 +0000217 else:
mbligh4426de02006-10-10 07:18:28 +0000218 p = extraversion_sub + '-%s/' % tag
mbligh4c3fe4a2007-07-31 17:59:21 +0000219 system('mv Makefile Makefile.old')
220 system('sed "%s" < Makefile.old > Makefile' % p)
mblighfdbcaec2006-10-01 23:28:57 +0000221
222
mblighb8e0a112007-11-05 20:27:36 +0000223 @record
224 def build(self, make_opts = '', logfile = '', extraversion='autotest'):
mblighc86b0b42006-07-28 17:35:28 +0000225 """build the kernel
mbligh72b88fc2006-12-16 18:41:35 +0000226
mblighc86b0b42006-07-28 17:35:28 +0000227 make_opts
228 additional options to make, if any
229 """
mbligh10038012006-10-19 03:32:45 +0000230 os_dep.commands('gcc', 'make')
mbligh6d4c9412006-09-13 23:08:44 +0000231 if logfile == '':
232 logfile = os.path.join(self.log_dir, 'kernel_build')
mblighf4c35322006-03-13 01:01:10 +0000233 os.chdir(self.build_dir)
mblighfdbcaec2006-10-01 23:28:57 +0000234 if extraversion:
235 self.extraversion(extraversion)
mbligh93057012006-08-06 15:51:56 +0000236 print os.path.join(self.log_dir, 'stdout')
mbligh44da9372007-11-05 23:30:07 +0000237 self.job.stdout.tee_redirect(logfile + '.stdout')
238 self.job.stderr.tee_redirect(logfile + '.stderr')
mbligha2508052006-05-28 21:29:53 +0000239 self.set_cross_cc()
mblighf4c35322006-03-13 01:01:10 +0000240 # setup_config_file(config_file, config_overrides)
apwc7846102006-04-06 18:22:13 +0000241
242 # Not needed on 2.6, but hard to tell -- handle failure
mblighfdbcaec2006-10-01 23:28:57 +0000243 system('make dep', ignorestatus=1)
mblighf4c35322006-03-13 01:01:10 +0000244 threads = 2 * count_cpus()
mbligh6d4c9412006-09-13 23:08:44 +0000245 build_string = 'make -j %d %s %s' % (threads, make_opts,
246 self.build_target)
247 # eg make bzImage, or make zImage
248 print build_string
249 system(build_string)
mblighf4c35322006-03-13 01:01:10 +0000250 if kernel_config.modules_needed('.config'):
apwd4701972006-10-16 09:24:56 +0000251 system('make -j %d modules' % (threads))
apwc7846102006-04-06 18:22:13 +0000252
mblighf4c35322006-03-13 01:01:10 +0000253 self.job.stdout.restore()
254 self.job.stderr.restore()
mbligh72b88fc2006-12-16 18:41:35 +0000255
mbligh4426de02006-10-10 07:18:28 +0000256 kernel_version = self.get_kernel_build_ver()
257 kernel_version = re.sub('-autotest', '', kernel_version)
258 self.logfile.write('BUILD VERSION: %s\n' % kernel_version)
mbligh1e8858e2006-11-24 22:18:35 +0000259
260 force_copy(self.build_dir+'/System.map', self.results_dir)
mblighf4c35322006-03-13 01:01:10 +0000261
262
mbligh30f28c52007-10-11 18:35:35 +0000263 def build_timed(self, threads, timefile = '/dev/null', make_opts = '',
mbligh2e793a42007-10-12 23:58:35 +0000264 output = '/dev/null'):
mblighc86b0b42006-07-28 17:35:28 +0000265 """time the bulding of the kernel"""
mblighb8a14e32006-05-06 00:17:35 +0000266 os.chdir(self.build_dir)
mbligh678823f2006-12-07 18:49:00 +0000267 self.set_cross_cc()
mbligh30f28c52007-10-11 18:35:35 +0000268
mblighd79b2b42007-11-05 20:38:15 +0000269 self.clean(logged=False)
mbligh30f28c52007-10-11 18:35:35 +0000270 build_string = "/usr/bin/time -o %s make %s -j %s vmlinux" \
271 % (timefile, make_opts, threads)
mbligh2e793a42007-10-12 23:58:35 +0000272 build_string += ' > %s 2>&1' % output
mblighf4c35322006-03-13 01:01:10 +0000273 print build_string
apwc7846102006-04-06 18:22:13 +0000274 system(build_string)
mbligh30f28c52007-10-11 18:35:35 +0000275
apwc7846102006-04-06 18:22:13 +0000276 if (not os.path.isfile('vmlinux')):
277 raise TestError("no vmlinux found, kernel build failed")
mblighb8a14e32006-05-06 00:17:35 +0000278
279
mblighb8e0a112007-11-05 20:27:36 +0000280 @record
281 def clean(self):
mblighc86b0b42006-07-28 17:35:28 +0000282 """make clean in the kernel tree"""
mblighb8a14e32006-05-06 00:17:35 +0000283 os.chdir(self.build_dir)
mblighf4c35322006-03-13 01:01:10 +0000284 print "make clean"
mbligh30f28c52007-10-11 18:35:35 +0000285 system('make clean > /dev/null 2> /dev/null')
mblighf4c35322006-03-13 01:01:10 +0000286
mbligh50f42ea2006-09-30 22:22:21 +0000287
mblighb8e0a112007-11-05 20:27:36 +0000288 @record
289 def mkinitrd(self, version, image, system_map, initrd):
mbligh50f42ea2006-09-30 22:22:21 +0000290 """Build kernel initrd image.
291 Try to use distro specific way to build initrd image.
292 Parameters:
293 version
294 new kernel version
295 image
296 new kernel image file
297 system_map
298 System.map file
299 initrd
300 initrd image file to build
301 """
302 vendor = get_os_vendor()
303
304 if os.path.isfile(initrd):
mblighfdbcaec2006-10-01 23:28:57 +0000305 print "Existing %s file, will remove it." % initrd
mbligh50f42ea2006-09-30 22:22:21 +0000306 os.remove(initrd)
mbligh72b88fc2006-12-16 18:41:35 +0000307
apwe43a30b2007-09-25 16:51:30 +0000308 args = self.job.config_get('kernel.mkinitrd_extra_args')
309
mbligh3d515d42007-11-09 17:00:36 +0000310 # don't leak 'None' into mkinitrd command
311 if not args:
312 args = ''
313
mbligh50f42ea2006-09-30 22:22:21 +0000314 if vendor in ['Red Hat', 'Fedora Core']:
apwe43a30b2007-09-25 16:51:30 +0000315 system('mkinitrd %s %s %s' % (args, initrd, version))
mbligh50f42ea2006-09-30 22:22:21 +0000316 elif vendor in ['SUSE']:
apwe43a30b2007-09-25 16:51:30 +0000317 system('mkinitrd %s -k %s -i %s -M %s' % (args, image, initrd, system_map))
apwc898a1f2007-02-28 15:27:22 +0000318 elif vendor in ['Debian', 'Ubuntu']:
319 if os.path.isfile('/usr/sbin/mkinitrd'):
320 cmd = '/usr/sbin/mkinitrd'
321 elif os.path.isfile('/usr/sbin/mkinitramfs'):
322 cmd = '/usr/sbin/mkinitramfs'
323 else:
324 raise TestError('No Debian initrd builder')
apwe43a30b2007-09-25 16:51:30 +0000325 system('%s %s -o %s %s' % (cmd, args, initrd, version))
mbligh50f42ea2006-09-30 22:22:21 +0000326 else:
mblighfdbcaec2006-10-01 23:28:57 +0000327 raise TestError('Unsupported vendor %s' % vendor)
mbligh50f42ea2006-09-30 22:22:21 +0000328
329
mbligh8baa2ea2006-12-17 23:01:24 +0000330 def set_build_image(self, image):
331 self.build_image = image
332
333
mblighb8e0a112007-11-05 20:27:36 +0000334 @record
335 def install(self, tag='autotest', prefix = '/'):
mblighc86b0b42006-07-28 17:35:28 +0000336 """make install in the kernel tree"""
apw87c65c12007-09-27 17:19:37 +0000337
338 # Record that we have installed the kernel, and
339 # the tag under which we installed it.
340 self.installed_as = tag
341
mblighf4c35322006-03-13 01:01:10 +0000342 os.chdir(self.build_dir)
mbligh72b88fc2006-12-16 18:41:35 +0000343
mbligh6a1d4db2006-10-06 04:30:16 +0000344 if not os.path.isdir(prefix):
345 os.mkdir(prefix)
mbligha87116f2006-10-10 02:47:08 +0000346 self.boot_dir = os.path.join(prefix, 'boot')
347 if not os.path.isdir(self.boot_dir):
348 os.mkdir(self.boot_dir)
mbligh0ad65582006-10-06 04:16:36 +0000349
mbligh8baa2ea2006-12-17 23:01:24 +0000350 if not self.build_image:
351 images = glob.glob('arch/*/boot/' + self.build_target)
352 if len(images):
353 self.build_image = images[0]
354 else:
355 self.build_image = self.build_target
mbligha87116f2006-10-10 02:47:08 +0000356
357 # remember installed files
mbligha87116f2006-10-10 02:47:08 +0000358 self.vmlinux = self.boot_dir + '/vmlinux-' + tag
mbligh8baa2ea2006-12-17 23:01:24 +0000359 if (self.build_image != 'vmlinux'):
apw9a61c5b2006-11-28 10:03:15 +0000360 self.image = self.boot_dir + '/vmlinuz-' + tag
361 else:
362 self.image = self.vmlinux
mbligha87116f2006-10-10 02:47:08 +0000363 self.system_map = self.boot_dir + '/System.map-' + tag
364 self.config = self.boot_dir + '/config-' + tag
365 self.initrd = ''
366
367 # copy to boot dir
mbligha87116f2006-10-10 02:47:08 +0000368 force_copy('vmlinux', self.vmlinux)
mbligh8baa2ea2006-12-17 23:01:24 +0000369 if (self.build_image != 'vmlinux'):
370 force_copy(self.build_image, self.image)
mbligha87116f2006-10-10 02:47:08 +0000371 force_copy('System.map', self.system_map)
372 force_copy('.config', self.config)
373
mbligh6a1d4db2006-10-06 04:30:16 +0000374 if not kernel_config.modules_needed('.config'):
375 return
376
377 system('make modules_install INSTALL_MOD_PATH=%s' % prefix)
378 if prefix == '/':
mbligha87116f2006-10-10 02:47:08 +0000379 self.initrd = self.boot_dir + '/initrd-' + tag
mblighb8e0a112007-11-05 20:27:36 +0000380 self.mkinitrd(self.get_kernel_build_ver(), self.image,
381 self.system_map, self.initrd)
mbligh5925e962007-08-30 17:05:22 +0000382
383
mbligha87116f2006-10-10 02:47:08 +0000384 def add_to_bootloader(self, tag='autotest', args=''):
385 """ add this kernel to bootloader, taking an
386 optional parameter of space separated parameters
387 e.g.: kernel.add_to_bootloader('mykernel', 'ro acpi=off')
388 """
389
390 # remove existing entry if present
391 self.job.bootloader.remove_kernel(tag)
392
apwcbe32572006-11-28 10:00:23 +0000393 # pull the base argument set from the job config,
apwcbe32572006-11-28 10:00:23 +0000394 baseargs = self.job.config_get('boot.default_args')
mbligh78bc05e2006-12-25 02:29:59 +0000395 if baseargs:
396 args = baseargs + " " + args
397
398 # otherwise populate from /proc/cmdline
399 # if not baseargs:
400 # baseargs = open('/proc/cmdline', 'r').readline().strip()
401 # NOTE: This is unnecessary, because boottool does it.
apwcbe32572006-11-28 10:00:23 +0000402
mbligh78bc05e2006-12-25 02:29:59 +0000403 root = None
404 roots = [x for x in args.split() if x.startswith('root=')]
405 if roots:
406 root = re.sub('^root=', '', roots[0])
407 arglist = [x for x in args.split() if not x.startswith('root=')]
408 args = ' '.join(arglist)
mbligha87116f2006-10-10 02:47:08 +0000409
mbligh78bc05e2006-12-25 02:29:59 +0000410 # add the kernel entry
411 # add_kernel(image, title='autotest', initrd='')
412 self.job.bootloader.add_kernel(self.image, tag, self.initrd, \
413 args = args, root = root)
mbligh6a1d4db2006-10-06 04:30:16 +0000414
mbligh201aa892006-10-29 04:02:05 +0000415
mbligh548f29a2006-10-17 04:55:12 +0000416 def get_kernel_build_arch(self, arch=None):
mbligh201aa892006-10-29 04:02:05 +0000417 """
418 Work out the current kernel architecture (as a kernel arch)
419 """
mbligh548f29a2006-10-17 04:55:12 +0000420 if not arch:
421 arch = get_current_kernel_arch()
422 if re.match('i.86', arch):
423 return 'i386'
424 elif re.match('sun4u', arch):
425 return 'sparc64'
426 elif re.match('arm.*', arch):
427 return 'arm'
428 elif re.match('sa110', arch):
429 return 'arm'
430 elif re.match('s390x', arch):
431 return 's390'
432 elif re.match('parisc64', arch):
433 return 'parisc'
434 elif re.match('ppc.*', arch):
435 return 'powerpc'
436 elif re.match('mips.*', arch):
437 return 'mips'
438 else:
439 return arch
440
mbligh6a1d4db2006-10-06 04:30:16 +0000441
mbligh237bed32007-09-05 13:05:57 +0000442 def get_kernel_build_release(self):
443 releasem = re.compile(r'.*UTS_RELEASE\s+"([^"]+)".*');
444 versionm = re.compile(r'.*UTS_VERSION\s+"([^"]+)".*');
445
446 release = None
447 version = None
448
449 for file in [ self.build_dir + "/include/linux/version.h",
450 self.build_dir + "/include/linux/utsrelease.h",
451 self.build_dir + "/include/linux/compile.h" ]:
452 if os.path.exists(file):
453 fd = open(file, 'r')
454 for line in fd.readlines():
455 m = releasem.match(line)
456 if m:
457 release = m.groups()[0]
458 m = versionm.match(line)
459 if m:
460 version = m.groups()[0]
461 fd.close()
462
463 return (release, version)
464
465
466 def get_kernel_build_ident(self):
467 (release, version) = self.get_kernel_build_release()
468
469 if not release or not version:
470 raise JobError('kernel has no identity')
471
472 return release + '::' + version
473
474
apw11985b72007-10-04 15:44:47 +0000475 def boot(self, args='', ident=1):
apw1b5dc362006-10-31 11:24:26 +0000476 """ install and boot this kernel, do not care how
477 just make it happen.
478 """
479
mbligh237bed32007-09-05 13:05:57 +0000480 # If we can check the kernel identity do so.
481 if ident:
482 when = int(time.time())
483 ident = self.get_kernel_build_ident()
484 args += " IDENT=%d" % (when)
485
mblighda0311e2007-10-25 16:03:33 +0000486 # TODO: how do we get the changelist number here?
apwce73d892007-09-25 16:53:05 +0000487 self.job.next_step_prepend(["job.kernel_check_ident",
mblighda0311e2007-10-25 16:03:33 +0000488 when, ident, None, self.subdir])
mbligh237bed32007-09-05 13:05:57 +0000489
apw87c65c12007-09-27 17:19:37 +0000490 # Check if the kernel has been installed, if not install
491 # as the default tag and boot that.
492 if not self.installed_as:
493 self.install()
494
495 # Boot the selected tag.
496 self.add_to_bootloader(args=args, tag=self.installed_as)
apw1b5dc362006-10-31 11:24:26 +0000497
498 # Boot it.
apw11985b72007-10-04 15:44:47 +0000499 self.job.reboot(tag=self.installed_as)
apw1b5dc362006-10-31 11:24:26 +0000500
501
mblighfdbcaec2006-10-01 23:28:57 +0000502 def get_kernel_build_ver(self):
mblighe11f5fc2006-10-04 04:42:22 +0000503 """Check Makefile and .config to return kernel version"""
504 version = patchlevel = sublevel = extraversion = localversion = ''
505
506 for line in open(self.build_dir + '/Makefile', 'r').readlines():
507 if line.startswith('VERSION'):
508 version = line[line.index('=') + 1:].strip()
509 if line.startswith('PATCHLEVEL'):
510 patchlevel = line[line.index('=') + 1:].strip()
511 if line.startswith('SUBLEVEL'):
512 sublevel = line[line.index('=') + 1:].strip()
513 if line.startswith('EXTRAVERSION'):
514 extraversion = line[line.index('=') + 1:].strip()
515
516 for line in open(self.build_dir + '/.config', 'r').readlines():
517 if line.startswith('CONFIG_LOCALVERSION='):
518 localversion = line.rstrip().split('"')[1]
519
mbligh72b88fc2006-12-16 18:41:35 +0000520 return "%s.%s.%s%s%s" %(version, patchlevel, sublevel, extraversion, localversion)
mblighfdbcaec2006-10-01 23:28:57 +0000521
522
mbligh8baa2ea2006-12-17 23:01:24 +0000523 def set_build_target(self, build_target):
524 if build_target:
525 self.build_target = build_target
526 print 'BUILD TARGET: %s' % self.build_target
527
528
mbligh5970cf02006-08-06 15:39:22 +0000529 def set_cross_cc(self, target_arch=None, cross_compile=None,
530 build_target='bzImage'):
mblighc86b0b42006-07-28 17:35:28 +0000531 """Set up to cross-compile.
mblighcc2e6662006-09-14 01:24:07 +0000532 This is broken. We need to work out what the default
533 compile produces, and if not, THEN set the cross
534 compiler.
mblighc86b0b42006-07-28 17:35:28 +0000535 """
mblighcc2e6662006-09-14 01:24:07 +0000536
mbligh5970cf02006-08-06 15:39:22 +0000537 if self.target_arch:
538 return
mbligh678823f2006-12-07 18:49:00 +0000539
mbligh8baa2ea2006-12-17 23:01:24 +0000540 # if someone has set build_target, don't clobber in set_cross_cc
541 # run set_build_target before calling set_cross_cc
542 if not self.build_target:
543 self.set_build_target(build_target)
mbligh72b88fc2006-12-16 18:41:35 +0000544
mbligh5970cf02006-08-06 15:39:22 +0000545 # If no 'target_arch' given assume native compilation
546 if target_arch == None:
mbligh548f29a2006-10-17 04:55:12 +0000547 target_arch = get_current_kernel_arch()
mbligh5970cf02006-08-06 15:39:22 +0000548 if target_arch == 'ppc64':
mbligh5970cf02006-08-06 15:39:22 +0000549 if self.build_target == 'bzImage':
apw9a61c5b2006-11-28 10:03:15 +0000550 self.build_target = 'vmlinux'
mbligh678823f2006-12-07 18:49:00 +0000551
552 if not cross_compile:
553 cross_compile = self.job.config_get('kernel.cross_cc')
554
555 if cross_compile:
556 os.environ['CROSS_COMPILE'] = cross_compile
557 else:
558 if os.environ.has_key('CROSS_COMPILE'):
559 del os.environ['CROSS_COMPILE']
560
mblighcc2e6662006-09-14 01:24:07 +0000561 return # HACK. Crap out for now.
mblighf4c35322006-03-13 01:01:10 +0000562
mblighcc2e6662006-09-14 01:24:07 +0000563 # At this point I know what arch I *want* to build for
564 # but have no way of working out what arch the default
565 # compiler DOES build for.
566
567 # Oh, and BTW, install_package() doesn't exist yet.
mbligh72b88fc2006-12-16 18:41:35 +0000568
mblighcc2e6662006-09-14 01:24:07 +0000569 if target_arch == 'ppc64':
570 install_package('ppc64-cross')
mbligh678823f2006-12-07 18:49:00 +0000571 cross_compile = os.path.join(self.autodir, 'sources/ppc64-cross/bin')
mblighcc2e6662006-09-14 01:24:07 +0000572
573 elif target_arch == 'x86_64':
574 install_package('x86_64-cross')
mbligh678823f2006-12-07 18:49:00 +0000575 cross_compile = os.path.join(self.autodir, 'sources/x86_64-cross/bin')
mblighb8a14e32006-05-06 00:17:35 +0000576
mbligh5970cf02006-08-06 15:39:22 +0000577 os.environ['ARCH'] = self.target_arch = target_arch
578
579 self.cross_compile = cross_compile
580 if self.cross_compile:
581 os.environ['CROSS_COMPILE'] = self.cross_compile
mblighcc2e6662006-09-14 01:24:07 +0000582
mbligh72b88fc2006-12-16 18:41:35 +0000583
mblighb8a14e32006-05-06 00:17:35 +0000584 def pickle_dump(self, filename):
mblighc86b0b42006-07-28 17:35:28 +0000585 """dump a pickle of ourself out to the specified filename
586
587 we can't pickle the backreference to job (it contains fd's),
mbligh709bb9b2006-10-12 04:32:16 +0000588 nor would we want to. Same for logfile (fd's).
mblighc86b0b42006-07-28 17:35:28 +0000589 """
mblighb8a14e32006-05-06 00:17:35 +0000590 temp = copy.copy(self)
591 temp.job = None
mbligh709bb9b2006-10-12 04:32:16 +0000592 temp.logfile = None
mblighb8a14e32006-05-06 00:17:35 +0000593 pickle.dump(temp, open(filename, 'w'))
mbligh736adc92007-10-18 03:23:22 +0000594
595
596class rpm_kernel:
597 """ Class for installing rpm kernel package
598 """
599
mblighd7318162007-11-08 21:36:36 +0000600 # pull in some optional site-specific rpm pre-processing
601 try:
602 import site_kernel
603 preprocess_rpm = staticmethod(site_kernel.preprocess_rpm)
604 del site_kernel
605 except ImportError:
606 # just make the preprocessor a nop
607 @staticmethod
608 def preprocess_rpm(rpm_package):
609 return rpm_package
610
611
mblighda0311e2007-10-25 16:03:33 +0000612 def __init__(self, job, rpm_package, subdir):
mbligh736adc92007-10-18 03:23:22 +0000613 self.job = job
mblighd7318162007-11-08 21:36:36 +0000614 self.rpm_package = self.preprocess_rpm(rpm_package)
mblighda0311e2007-10-25 16:03:33 +0000615 self.log_dir = os.path.join(subdir, 'debug')
616 self.subdir = os.path.basename(subdir)
mbligh736adc92007-10-18 03:23:22 +0000617 if os.path.exists(self.log_dir):
618 system('rm -rf ' + self.log_dir)
619 os.mkdir(self.log_dir)
mblighda0311e2007-10-25 16:03:33 +0000620 self.installed_as = None
621 cl_re = re.compile(r'[-.](\d{7,})\.rpm$')
622 match = cl_re.findall(rpm_package)
623 if match:
624 self.changelist = match[0]
625 else:
626 self.changelist = None
mbligh736adc92007-10-18 03:23:22 +0000627
628
mblighb8e0a112007-11-05 20:27:36 +0000629 @record
mblighda0311e2007-10-25 16:03:33 +0000630 def install(self, tag='autotest'):
631 self.installed_as = tag
632
mbligh736adc92007-10-18 03:23:22 +0000633 logfile = os.path.join(self.log_dir, 'rpm_install')
mbligha60795f2007-11-05 18:55:54 +0000634 self.job.stdout.tee_redirect(logfile + '.stdout')
635 self.job.stderr.tee_redirect(logfile + '.stderr')
mbligh736adc92007-10-18 03:23:22 +0000636
mblighda0311e2007-10-25 16:03:33 +0000637 self.rpm_name = system_output('rpm -qp ' + self.rpm_package)
mbligh736adc92007-10-18 03:23:22 +0000638
639 # install
640 system('rpm -i --force ' + self.rpm_package)
641
642 # get file list
mblighda0311e2007-10-25 16:03:33 +0000643 files = system_output('rpm -ql ' + self.rpm_name).splitlines()
mbligh736adc92007-10-18 03:23:22 +0000644
645 self.job.stdout.restore()
646 self.job.stderr.restore()
647
648 # search for vmlinuz
649 for file in files:
650 if file.startswith('/boot/vmlinuz'):
651 self.image = file
652 break
653 else:
654 raise TestError(self.rpm_package + " doesn't contain /boot/vmlinuz")
mblighda0311e2007-10-25 16:03:33 +0000655
mbligh736adc92007-10-18 03:23:22 +0000656 # search for initrd
657 self.initrd = ''
658 for file in files:
659 if file.startswith('/boot/initrd'):
660 self.initrd = file
661 break
662
663 # get version and release number
664 self.version, self.release = system_output(
mblighda0311e2007-10-25 16:03:33 +0000665 'rpm --queryformat="%{VERSION}\\n%{RELEASE}\\n" -q ' + self.rpm_name).splitlines()[0:2]
mbligh736adc92007-10-18 03:23:22 +0000666
667
668 def add_to_bootloader(self, tag='autotest', args=''):
669 """ Add this kernel to bootloader
670 """
671
672 # remove existing entry if present
673 self.job.bootloader.remove_kernel(tag)
674
675 # pull the base argument set from the job config
676 baseargs = self.job.config_get('boot.default_args')
677 if baseargs:
678 args = baseargs + ' ' + args
679
680 # otherwise populate from /proc/cmdline
681 # if not baseargs:
682 # baseargs = open('/proc/cmdline', 'r').readline().strip()
683 # NOTE: This is unnecessary, because boottool does it.
684
685 root = None
686 roots = [x for x in args.split() if x.startswith('root=')]
687 if roots:
688 root = re.sub('^root=', '', roots[0])
689 arglist = [x for x in args.split() if not x.startswith('root=')]
690 args = ' '.join(arglist)
691
692 # add the kernel entry
693 self.job.bootloader.add_kernel(self.image, tag, self.initrd, args = args, root = root)
mbligh10a24a72007-10-24 21:02:53 +0000694
695
mblighda0311e2007-10-25 16:03:33 +0000696 def boot(self, args='', ident=1):
697 """ install and boot this kernel
698 """
mbligh73e82a32007-11-08 21:35:29 +0000699
mbligh10a24a72007-10-24 21:02:53 +0000700 # Check if the kernel has been installed, if not install
701 # as the default tag and boot that.
mblighda0311e2007-10-25 16:03:33 +0000702 if not self.installed_as:
mbligh73e82a32007-11-08 21:35:29 +0000703 self.install()
mblighda0311e2007-10-25 16:03:33 +0000704
705 # If we can check the kernel identity do so.
706 if ident:
707 when = int(time.time())
708 ident = '-'.join([self.version, self.rpm_name.split('-')[1], self.release])
709 args += " IDENT=%d" % (when)
710
711 self.job.next_step_prepend(["job.kernel_check_ident",
712 when, ident, self.changelist, self.subdir, 'rpm'])
mbligh10a24a72007-10-24 21:02:53 +0000713
714 # Boot the selected tag.
mblighda0311e2007-10-25 16:03:33 +0000715 self.add_to_bootloader(args=args, tag=self.installed_as)
mbligh10a24a72007-10-24 21:02:53 +0000716
717 # Boot it.
mblighda0311e2007-10-25 16:03:33 +0000718 self.job.reboot(tag=self.installed_as)