blob: 11e79b71af849afeafe274c1e84d234ca8e3dcd3 [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
7class kernel:
mblighc86b0b42006-07-28 17:35:28 +00008 """ Class for compiling kernels.
9
10 Data for the object includes the src files
11 used to create the kernel, patches applied, config (base + changes),
12 the build directory itself, and logged output
13
14 Properties:
15 job
16 Backpointer to the job object we're part of
17 autodir
18 Path to the top level autotest dir (/usr/local/autotest)
mblighc86b0b42006-07-28 17:35:28 +000019 src_dir
mbligh1e8858e2006-11-24 22:18:35 +000020 <tmp_dir>/src/
mblighc49af7b2006-11-24 04:02:21 +000021 build_dir
mbligh1e8858e2006-11-24 22:18:35 +000022 <tmp_dir>/linux/
mblighc86b0b42006-07-28 17:35:28 +000023 config_dir
mbligh1e8858e2006-11-24 22:18:35 +000024 <results_dir>/config/
mblighc86b0b42006-07-28 17:35:28 +000025 log_dir
mbligh1e8858e2006-11-24 22:18:35 +000026 <results_dir>/debug/
27 results_dir
28 <results_dir>/results/
mblighc86b0b42006-07-28 17:35:28 +000029 """
30
mbligh8baa2ea2006-12-17 23:01:24 +000031 autodir = ''
32
mbligh09f288a2007-09-18 21:34:57 +000033 def __init__(self, job, base_tree, subdir, tmp_dir, build_dir, leave = False):
mblighc86b0b42006-07-28 17:35:28 +000034 """Initialize the kernel build environment
35
36 job
37 which job this build is part of
mblighc86b0b42006-07-28 17:35:28 +000038 base_tree
mbligh534015f2006-09-15 03:28:56 +000039 base kernel tree. Can be one of the following:
40 1. A local tarball
41 2. A URL to a tarball
42 3. A local directory (will symlink it)
43 4. A shorthand expandable (eg '2.6.11-git3')
mbligh09f288a2007-09-18 21:34:57 +000044 subdir
45 subdir in the results directory (eg "build")
46 (holds config/, debug/, results/)
mbligh1e8858e2006-11-24 22:18:35 +000047 tmp_dir
mbligh72b88fc2006-12-16 18:41:35 +000048
mbligh1e8858e2006-11-24 22:18:35 +000049 leave
50 Boolean, whether to leave existing tmpdir or not
mblighc86b0b42006-07-28 17:35:28 +000051 """
mblighf4c35322006-03-13 01:01:10 +000052 self.job = job
mbligh678823f2006-12-07 18:49:00 +000053 self.autodir = job.autodir
mblighf4c35322006-03-13 01:01:10 +000054
mbligh1e8858e2006-11-24 22:18:35 +000055 self.src_dir = os.path.join(tmp_dir, 'src')
mbligh8baa2ea2006-12-17 23:01:24 +000056 self.build_dir = os.path.join(tmp_dir, build_dir)
mbligha2508052006-05-28 21:29:53 +000057 # created by get_kernel_tree
mbligh09f288a2007-09-18 21:34:57 +000058 self.config_dir = os.path.join(subdir, 'config')
59 self.log_dir = os.path.join(subdir, 'debug')
60 self.results_dir = os.path.join(subdir, 'results')
61 self.subdir = os.path.basename(subdir)
mbligh1e8858e2006-11-24 22:18:35 +000062
apw87c65c12007-09-27 17:19:37 +000063 self.installed_as = None
64
mbligh1e8858e2006-11-24 22:18:35 +000065 if not leave:
66 if os.path.isdir(self.src_dir):
67 system('rm -rf ' + self.src_dir)
68 if os.path.isdir(self.build_dir):
69 system('rm -rf ' + self.build_dir)
70
mbligh30f28c52007-10-11 18:35:35 +000071 if not os.path.exists(self.src_dir):
72 os.mkdir(self.src_dir)
mbligh1e8858e2006-11-24 22:18:35 +000073 for path in [self.config_dir, self.log_dir, self.results_dir]:
74 if os.path.exists(path):
75 system('rm -rf ' + path)
76 os.mkdir(path)
mblighf4c35322006-03-13 01:01:10 +000077
mbligh4426de02006-10-10 07:18:28 +000078 logpath = os.path.join(self.log_dir, 'build_log')
79 self.logfile = open(logpath, 'w+')
80
mbligh72b88fc2006-12-16 18:41:35 +000081 self.target_arch = None
mblighfdbcaec2006-10-01 23:28:57 +000082 self.build_target = 'bzImage'
mbligh8baa2ea2006-12-17 23:01:24 +000083 self.build_image = None
mblighfdbcaec2006-10-01 23:28:57 +000084
mblighcac347a2007-06-02 17:21:48 +000085 if get_current_kernel_arch() == 'ia64':
86 self.build_target = 'all'
87 self.build_image = 'vmlinux.gz'
88
mblighfdbcaec2006-10-01 23:28:57 +000089 if leave:
90 return
mbligh534015f2006-09-15 03:28:56 +000091
mbligh4426de02006-10-10 07:18:28 +000092 self.logfile.write('BASE: %s\n' % base_tree)
apw2366d992007-03-12 20:35:57 +000093
94 # Where we have direct version hint record that
95 # for later configuration selection.
96 shorthand = re.compile(r'^\d+\.\d+\.\d+')
97 if shorthand.match(base_tree):
98 self.base_tree_version = base_tree
99 else:
100 self.base_tree_version = None
101
mbligh534015f2006-09-15 03:28:56 +0000102 if os.path.exists(base_tree):
103 self.get_kernel_tree(base_tree)
104 else:
apw8ad56be2006-11-06 17:49:54 +0000105 args = self.job.config_get('mirror.ftp_kernel_org')
mbligh548f29a2006-10-17 04:55:12 +0000106 if args:
107 args = '-l ' + args
mbligh72b88fc2006-12-16 18:41:35 +0000108 base_components = kernelexpand(base_tree, args)
mbligh534015f2006-09-15 03:28:56 +0000109 print 'kernelexpand: '
110 print base_components
111 self.get_kernel_tree(base_components.pop(0))
112 if base_components: # apply remaining patches
113 self.patch(*base_components)
mblighf4c35322006-03-13 01:01:10 +0000114
115
mbligh5925e962007-08-30 17:05:22 +0000116 def __record(self, fn, name, *args, **dargs):
117 try:
118 fn(*args, **dargs)
mbligh09f288a2007-09-18 21:34:57 +0000119 self.job.record('GOOD', self.subdir, name)
mbligh5925e962007-08-30 17:05:22 +0000120 except Exception, detail:
mbligh09f288a2007-09-18 21:34:57 +0000121 err = detail.__str__()
122 self.job.record('FAIL', self.subdir, name, err)
mbligh5925e962007-08-30 17:05:22 +0000123 raise
124
125
126 def _patch(self, *patches):
mblighc86b0b42006-07-28 17:35:28 +0000127 """Apply a list of patches (in order)"""
mbligh1e8858e2006-11-24 22:18:35 +0000128 if not patches:
129 return
mblighd016ecc2006-11-25 21:41:07 +0000130 # if isinstance(patches, basestring):
131 # patches = [patches]
mbligh534015f2006-09-15 03:28:56 +0000132 print 'Applying patches: ', patches
133 # self.job.stdout.redirect(os.path.join(self.log_dir, 'stdout'))
mbligh0763e732007-08-30 16:35:06 +0000134 self.apply_patches(self.get_patches(patches))
mbligh534015f2006-09-15 03:28:56 +0000135 # self.job.stdout.restore()
mblighf4c35322006-03-13 01:01:10 +0000136
137
mbligh5925e962007-08-30 17:05:22 +0000138 def patch(self, *args, **dargs):
139 self.__record(self._patch, "kernel.patch", *args, **dargs)
140
141
142 def _config(self, config_file = '', config_list = None,
apwb449c7d2006-12-05 12:21:44 +0000143 defconfig = False):
mbligh93057012006-08-06 15:51:56 +0000144 self.job.stdout.redirect(os.path.join(self.log_dir, 'stdout'))
mbligh678823f2006-12-07 18:49:00 +0000145 self.set_cross_cc()
apwb449c7d2006-12-05 12:21:44 +0000146 config = kernel_config.kernel_config(self.job, self.build_dir,
apw2366d992007-03-12 20:35:57 +0000147 self.config_dir, config_file, config_list,
148 defconfig, self.base_tree_version)
mblighf4c35322006-03-13 01:01:10 +0000149 self.job.stdout.restore()
150
151
mbligh5925e962007-08-30 17:05:22 +0000152 def config(self, *args, **dargs):
153 self.__record(self._config, "kernel.config", *args, **dargs)
154
155
mblighf4c35322006-03-13 01:01:10 +0000156 def get_patches(self, patches):
mbligh1e8858e2006-11-24 22:18:35 +0000157 """fetch the patches to the local src_dir"""
mblighf4c35322006-03-13 01:01:10 +0000158 local_patches = []
159 for patch in patches:
mbligh1e8858e2006-11-24 22:18:35 +0000160 dest = os.path.join(self.src_dir, basename(patch))
mbligh0763e732007-08-30 16:35:06 +0000161 # FIXME: this isn't unique. Append something to it
162 # like wget does if it's not there?
mbligh1e8858e2006-11-24 22:18:35 +0000163 print "get_file %s %s %s %s" % (patch, dest, self.src_dir, basename(patch))
mblighf4c35322006-03-13 01:01:10 +0000164 get_file(patch, dest)
mbligh0763e732007-08-30 16:35:06 +0000165 # probably safer to use the command, not python library
166 md5sum = system_output('md5sum ' + dest).split()[0]
167 local_patches.append((patch, dest, md5sum))
mbligh534015f2006-09-15 03:28:56 +0000168 return local_patches
mblighf4c35322006-03-13 01:01:10 +0000169
mbligh72b88fc2006-12-16 18:41:35 +0000170
mbligh534015f2006-09-15 03:28:56 +0000171 def apply_patches(self, local_patches):
mblighc86b0b42006-07-28 17:35:28 +0000172 """apply the list of patches, in order"""
mblighf4c35322006-03-13 01:01:10 +0000173 builddir = self.build_dir
174 os.chdir(builddir)
175
mbligh534015f2006-09-15 03:28:56 +0000176 if not local_patches:
mblighf4c35322006-03-13 01:01:10 +0000177 return None
mbligh0763e732007-08-30 16:35:06 +0000178 for (spec, local, md5sum) in local_patches:
179 if local.endswith('.bz2') or local.endswith('.gz'):
180 ref = spec
181 else:
182 ref = force_copy(local, self.results_dir)
183 ref = self.job.relative_path(ref)
184 log = 'PATCH: %s %s %s\n' % (spec, ref, md5sum)
185 print log
186 cat_file_to_cmd(local, 'patch -p1 > /dev/null')
187 self.logfile.write(log)
mbligh72b88fc2006-12-16 18:41:35 +0000188
189
190 def get_kernel_tree(self, base_tree):
mblighc49af7b2006-11-24 04:02:21 +0000191 """Extract/link base_tree to self.build_dir"""
mbligh5970cf02006-08-06 15:39:22 +0000192
193 # if base_tree is a dir, assume uncompressed kernel
194 if os.path.isdir(base_tree):
195 print 'Symlinking existing kernel source'
mblighc49af7b2006-11-24 04:02:21 +0000196 os.symlink(base_tree, self.build_dir)
mblighf4c35322006-03-13 01:01:10 +0000197
mbligh5970cf02006-08-06 15:39:22 +0000198 # otherwise, extract tarball
199 else:
mblighc49af7b2006-11-24 04:02:21 +0000200 os.chdir(os.path.dirname(self.src_dir))
201 # Figure out local destination for tarball
202 tarball = os.path.join(self.src_dir, os.path.basename(base_tree))
mbligh5970cf02006-08-06 15:39:22 +0000203 get_file(base_tree, tarball)
mbligh5970cf02006-08-06 15:39:22 +0000204 print 'Extracting kernel tarball:', tarball, '...'
mblighc49af7b2006-11-24 04:02:21 +0000205 extract_tarball_to_dir(tarball, self.build_dir)
mbligh5970cf02006-08-06 15:39:22 +0000206
mblighf4c35322006-03-13 01:01:10 +0000207
mblighfdbcaec2006-10-01 23:28:57 +0000208 def extraversion(self, tag, append=1):
209 os.chdir(self.build_dir)
mbligh4426de02006-10-10 07:18:28 +0000210 extraversion_sub = r's/^EXTRAVERSION =\s*\(.*\)/EXTRAVERSION = '
mblighfdbcaec2006-10-01 23:28:57 +0000211 if append:
mbligh4426de02006-10-10 07:18:28 +0000212 p = extraversion_sub + '\\1-%s/' % tag
mblighfdbcaec2006-10-01 23:28:57 +0000213 else:
mbligh4426de02006-10-10 07:18:28 +0000214 p = extraversion_sub + '-%s/' % tag
mbligh4c3fe4a2007-07-31 17:59:21 +0000215 system('mv Makefile Makefile.old')
216 system('sed "%s" < Makefile.old > Makefile' % p)
mblighfdbcaec2006-10-01 23:28:57 +0000217
218
mbligh5925e962007-08-30 17:05:22 +0000219 def _build(self, make_opts = '', logfile = '', extraversion='autotest'):
mblighc86b0b42006-07-28 17:35:28 +0000220 """build the kernel
mbligh72b88fc2006-12-16 18:41:35 +0000221
mblighc86b0b42006-07-28 17:35:28 +0000222 make_opts
223 additional options to make, if any
224 """
mbligh10038012006-10-19 03:32:45 +0000225 os_dep.commands('gcc', 'make')
mbligh6d4c9412006-09-13 23:08:44 +0000226 if logfile == '':
227 logfile = os.path.join(self.log_dir, 'kernel_build')
mblighf4c35322006-03-13 01:01:10 +0000228 os.chdir(self.build_dir)
mblighfdbcaec2006-10-01 23:28:57 +0000229 if extraversion:
230 self.extraversion(extraversion)
mbligh93057012006-08-06 15:51:56 +0000231 print os.path.join(self.log_dir, 'stdout')
mbligh6d4c9412006-09-13 23:08:44 +0000232 self.job.stdout.redirect(logfile + '.stdout')
233 self.job.stderr.redirect(logfile + '.stderr')
mbligha2508052006-05-28 21:29:53 +0000234 self.set_cross_cc()
mblighf4c35322006-03-13 01:01:10 +0000235 # setup_config_file(config_file, config_overrides)
apwc7846102006-04-06 18:22:13 +0000236
237 # Not needed on 2.6, but hard to tell -- handle failure
mblighfdbcaec2006-10-01 23:28:57 +0000238 system('make dep', ignorestatus=1)
mblighf4c35322006-03-13 01:01:10 +0000239 threads = 2 * count_cpus()
mbligh6d4c9412006-09-13 23:08:44 +0000240 build_string = 'make -j %d %s %s' % (threads, make_opts,
241 self.build_target)
242 # eg make bzImage, or make zImage
243 print build_string
244 system(build_string)
mblighf4c35322006-03-13 01:01:10 +0000245 if kernel_config.modules_needed('.config'):
apwd4701972006-10-16 09:24:56 +0000246 system('make -j %d modules' % (threads))
apwc7846102006-04-06 18:22:13 +0000247
mblighf4c35322006-03-13 01:01:10 +0000248 self.job.stdout.restore()
249 self.job.stderr.restore()
mbligh72b88fc2006-12-16 18:41:35 +0000250
mbligh4426de02006-10-10 07:18:28 +0000251 kernel_version = self.get_kernel_build_ver()
252 kernel_version = re.sub('-autotest', '', kernel_version)
253 self.logfile.write('BUILD VERSION: %s\n' % kernel_version)
mbligh1e8858e2006-11-24 22:18:35 +0000254
255 force_copy(self.build_dir+'/System.map', self.results_dir)
mblighf4c35322006-03-13 01:01:10 +0000256
257
mbligh5925e962007-08-30 17:05:22 +0000258 def build(self, *args, **dargs):
259 self.__record(self._build, "kernel.build", *args, **dargs)
260
261
mbligh30f28c52007-10-11 18:35:35 +0000262 def build_timed(self, threads, timefile = '/dev/null', make_opts = '',
mbligh2e793a42007-10-12 23:58:35 +0000263 output = '/dev/null'):
mblighc86b0b42006-07-28 17:35:28 +0000264 """time the bulding of the kernel"""
mblighb8a14e32006-05-06 00:17:35 +0000265 os.chdir(self.build_dir)
mbligh678823f2006-12-07 18:49:00 +0000266 self.set_cross_cc()
mbligh30f28c52007-10-11 18:35:35 +0000267
268 self._clean()
269 build_string = "/usr/bin/time -o %s make %s -j %s vmlinux" \
270 % (timefile, make_opts, threads)
mbligh2e793a42007-10-12 23:58:35 +0000271 build_string += ' > %s 2>&1' % output
mblighf4c35322006-03-13 01:01:10 +0000272 print build_string
apwc7846102006-04-06 18:22:13 +0000273 system(build_string)
mbligh30f28c52007-10-11 18:35:35 +0000274
apwc7846102006-04-06 18:22:13 +0000275 if (not os.path.isfile('vmlinux')):
276 raise TestError("no vmlinux found, kernel build failed")
mblighb8a14e32006-05-06 00:17:35 +0000277
278
mbligh5925e962007-08-30 17:05:22 +0000279 def _clean(self):
mblighc86b0b42006-07-28 17:35:28 +0000280 """make clean in the kernel tree"""
mblighb8a14e32006-05-06 00:17:35 +0000281 os.chdir(self.build_dir)
mblighf4c35322006-03-13 01:01:10 +0000282 print "make clean"
mbligh30f28c52007-10-11 18:35:35 +0000283 system('make clean > /dev/null 2> /dev/null')
mblighf4c35322006-03-13 01:01:10 +0000284
mbligh50f42ea2006-09-30 22:22:21 +0000285
mbligh5925e962007-08-30 17:05:22 +0000286 def clean(self, *args, **dargs):
287 self.__record(self._clean, "kernel.clean", *args, **dargs)
288
289
290 def _mkinitrd(self, version, image, system_map, initrd):
mbligh50f42ea2006-09-30 22:22:21 +0000291 """Build kernel initrd image.
292 Try to use distro specific way to build initrd image.
293 Parameters:
294 version
295 new kernel version
296 image
297 new kernel image file
298 system_map
299 System.map file
300 initrd
301 initrd image file to build
302 """
303 vendor = get_os_vendor()
304
305 if os.path.isfile(initrd):
mblighfdbcaec2006-10-01 23:28:57 +0000306 print "Existing %s file, will remove it." % initrd
mbligh50f42ea2006-09-30 22:22:21 +0000307 os.remove(initrd)
mbligh72b88fc2006-12-16 18:41:35 +0000308
apwe43a30b2007-09-25 16:51:30 +0000309 args = self.job.config_get('kernel.mkinitrd_extra_args')
310
mbligh50f42ea2006-09-30 22:22:21 +0000311 if vendor in ['Red Hat', 'Fedora Core']:
apwe43a30b2007-09-25 16:51:30 +0000312 system('mkinitrd %s %s %s' % (args, initrd, version))
mbligh50f42ea2006-09-30 22:22:21 +0000313 elif vendor in ['SUSE']:
apwe43a30b2007-09-25 16:51:30 +0000314 system('mkinitrd %s -k %s -i %s -M %s' % (args, image, initrd, system_map))
apwc898a1f2007-02-28 15:27:22 +0000315 elif vendor in ['Debian', 'Ubuntu']:
316 if os.path.isfile('/usr/sbin/mkinitrd'):
317 cmd = '/usr/sbin/mkinitrd'
318 elif os.path.isfile('/usr/sbin/mkinitramfs'):
319 cmd = '/usr/sbin/mkinitramfs'
320 else:
321 raise TestError('No Debian initrd builder')
apwe43a30b2007-09-25 16:51:30 +0000322 system('%s %s -o %s %s' % (cmd, args, initrd, version))
mbligh50f42ea2006-09-30 22:22:21 +0000323 else:
mblighfdbcaec2006-10-01 23:28:57 +0000324 raise TestError('Unsupported vendor %s' % vendor)
mbligh50f42ea2006-09-30 22:22:21 +0000325
326
mbligh5925e962007-08-30 17:05:22 +0000327 def mkinitrd(self, *args, **dargs):
328 self.__record(self._mkinitrd, "kernel.mkinitrd", *args, **dargs)
329
330
mbligh8baa2ea2006-12-17 23:01:24 +0000331 def set_build_image(self, image):
332 self.build_image = image
333
334
mbligh5925e962007-08-30 17:05:22 +0000335 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
380 self.mkinitrd(self.get_kernel_build_ver(), self.image, \
381 self.system_map, self.initrd)
382
383
mbligh5925e962007-08-30 17:05:22 +0000384 def install(self, *args, **dargs):
385 self.__record(self._install, "kernel.install", *args, **dargs)
386
387
mbligha87116f2006-10-10 02:47:08 +0000388 def add_to_bootloader(self, tag='autotest', args=''):
389 """ add this kernel to bootloader, taking an
390 optional parameter of space separated parameters
391 e.g.: kernel.add_to_bootloader('mykernel', 'ro acpi=off')
392 """
393
394 # remove existing entry if present
395 self.job.bootloader.remove_kernel(tag)
396
apwcbe32572006-11-28 10:00:23 +0000397 # pull the base argument set from the job config,
apwcbe32572006-11-28 10:00:23 +0000398 baseargs = self.job.config_get('boot.default_args')
mbligh78bc05e2006-12-25 02:29:59 +0000399 if baseargs:
400 args = baseargs + " " + args
401
402 # otherwise populate from /proc/cmdline
403 # if not baseargs:
404 # baseargs = open('/proc/cmdline', 'r').readline().strip()
405 # NOTE: This is unnecessary, because boottool does it.
apwcbe32572006-11-28 10:00:23 +0000406
mbligh78bc05e2006-12-25 02:29:59 +0000407 root = None
408 roots = [x for x in args.split() if x.startswith('root=')]
409 if roots:
410 root = re.sub('^root=', '', roots[0])
411 arglist = [x for x in args.split() if not x.startswith('root=')]
412 args = ' '.join(arglist)
mbligha87116f2006-10-10 02:47:08 +0000413
mbligh78bc05e2006-12-25 02:29:59 +0000414 # add the kernel entry
415 # add_kernel(image, title='autotest', initrd='')
416 self.job.bootloader.add_kernel(self.image, tag, self.initrd, \
417 args = args, root = root)
mbligh6a1d4db2006-10-06 04:30:16 +0000418
mbligh201aa892006-10-29 04:02:05 +0000419
mbligh548f29a2006-10-17 04:55:12 +0000420 def get_kernel_build_arch(self, arch=None):
mbligh201aa892006-10-29 04:02:05 +0000421 """
422 Work out the current kernel architecture (as a kernel arch)
423 """
mbligh548f29a2006-10-17 04:55:12 +0000424 if not arch:
425 arch = get_current_kernel_arch()
426 if re.match('i.86', arch):
427 return 'i386'
428 elif re.match('sun4u', arch):
429 return 'sparc64'
430 elif re.match('arm.*', arch):
431 return 'arm'
432 elif re.match('sa110', arch):
433 return 'arm'
434 elif re.match('s390x', arch):
435 return 's390'
436 elif re.match('parisc64', arch):
437 return 'parisc'
438 elif re.match('ppc.*', arch):
439 return 'powerpc'
440 elif re.match('mips.*', arch):
441 return 'mips'
442 else:
443 return arch
444
mbligh6a1d4db2006-10-06 04:30:16 +0000445
mbligh237bed32007-09-05 13:05:57 +0000446 def get_kernel_build_release(self):
447 releasem = re.compile(r'.*UTS_RELEASE\s+"([^"]+)".*');
448 versionm = re.compile(r'.*UTS_VERSION\s+"([^"]+)".*');
449
450 release = None
451 version = None
452
453 for file in [ self.build_dir + "/include/linux/version.h",
454 self.build_dir + "/include/linux/utsrelease.h",
455 self.build_dir + "/include/linux/compile.h" ]:
456 if os.path.exists(file):
457 fd = open(file, 'r')
458 for line in fd.readlines():
459 m = releasem.match(line)
460 if m:
461 release = m.groups()[0]
462 m = versionm.match(line)
463 if m:
464 version = m.groups()[0]
465 fd.close()
466
467 return (release, version)
468
469
470 def get_kernel_build_ident(self):
471 (release, version) = self.get_kernel_build_release()
472
473 if not release or not version:
474 raise JobError('kernel has no identity')
475
476 return release + '::' + version
477
478
apw11985b72007-10-04 15:44:47 +0000479 def boot(self, args='', ident=1):
apw1b5dc362006-10-31 11:24:26 +0000480 """ install and boot this kernel, do not care how
481 just make it happen.
482 """
483
mbligh237bed32007-09-05 13:05:57 +0000484 # If we can check the kernel identity do so.
485 if ident:
486 when = int(time.time())
487 ident = self.get_kernel_build_ident()
488 args += " IDENT=%d" % (when)
489
apwce73d892007-09-25 16:53:05 +0000490 self.job.next_step_prepend(["job.kernel_check_ident",
491 when, ident, self.subdir])
mbligh237bed32007-09-05 13:05:57 +0000492
apw87c65c12007-09-27 17:19:37 +0000493 # Check if the kernel has been installed, if not install
494 # as the default tag and boot that.
495 if not self.installed_as:
496 self.install()
497
498 # Boot the selected tag.
499 self.add_to_bootloader(args=args, tag=self.installed_as)
apw1b5dc362006-10-31 11:24:26 +0000500
501 # Boot it.
apw11985b72007-10-04 15:44:47 +0000502 self.job.reboot(tag=self.installed_as)
apw1b5dc362006-10-31 11:24:26 +0000503
504
mblighfdbcaec2006-10-01 23:28:57 +0000505 def get_kernel_build_ver(self):
mblighe11f5fc2006-10-04 04:42:22 +0000506 """Check Makefile and .config to return kernel version"""
507 version = patchlevel = sublevel = extraversion = localversion = ''
508
509 for line in open(self.build_dir + '/Makefile', 'r').readlines():
510 if line.startswith('VERSION'):
511 version = line[line.index('=') + 1:].strip()
512 if line.startswith('PATCHLEVEL'):
513 patchlevel = line[line.index('=') + 1:].strip()
514 if line.startswith('SUBLEVEL'):
515 sublevel = line[line.index('=') + 1:].strip()
516 if line.startswith('EXTRAVERSION'):
517 extraversion = line[line.index('=') + 1:].strip()
518
519 for line in open(self.build_dir + '/.config', 'r').readlines():
520 if line.startswith('CONFIG_LOCALVERSION='):
521 localversion = line.rstrip().split('"')[1]
522
mbligh72b88fc2006-12-16 18:41:35 +0000523 return "%s.%s.%s%s%s" %(version, patchlevel, sublevel, extraversion, localversion)
mblighfdbcaec2006-10-01 23:28:57 +0000524
525
mbligh8baa2ea2006-12-17 23:01:24 +0000526 def set_build_target(self, build_target):
527 if build_target:
528 self.build_target = build_target
529 print 'BUILD TARGET: %s' % self.build_target
530
531
mbligh5970cf02006-08-06 15:39:22 +0000532 def set_cross_cc(self, target_arch=None, cross_compile=None,
533 build_target='bzImage'):
mblighc86b0b42006-07-28 17:35:28 +0000534 """Set up to cross-compile.
mblighcc2e6662006-09-14 01:24:07 +0000535 This is broken. We need to work out what the default
536 compile produces, and if not, THEN set the cross
537 compiler.
mblighc86b0b42006-07-28 17:35:28 +0000538 """
mblighcc2e6662006-09-14 01:24:07 +0000539
mbligh5970cf02006-08-06 15:39:22 +0000540 if self.target_arch:
541 return
mbligh678823f2006-12-07 18:49:00 +0000542
mbligh8baa2ea2006-12-17 23:01:24 +0000543 # if someone has set build_target, don't clobber in set_cross_cc
544 # run set_build_target before calling set_cross_cc
545 if not self.build_target:
546 self.set_build_target(build_target)
mbligh72b88fc2006-12-16 18:41:35 +0000547
mbligh5970cf02006-08-06 15:39:22 +0000548 # If no 'target_arch' given assume native compilation
549 if target_arch == None:
mbligh548f29a2006-10-17 04:55:12 +0000550 target_arch = get_current_kernel_arch()
mbligh5970cf02006-08-06 15:39:22 +0000551 if target_arch == 'ppc64':
mbligh5970cf02006-08-06 15:39:22 +0000552 if self.build_target == 'bzImage':
apw9a61c5b2006-11-28 10:03:15 +0000553 self.build_target = 'vmlinux'
mbligh678823f2006-12-07 18:49:00 +0000554
555 if not cross_compile:
556 cross_compile = self.job.config_get('kernel.cross_cc')
557
558 if cross_compile:
559 os.environ['CROSS_COMPILE'] = cross_compile
560 else:
561 if os.environ.has_key('CROSS_COMPILE'):
562 del os.environ['CROSS_COMPILE']
563
mblighcc2e6662006-09-14 01:24:07 +0000564 return # HACK. Crap out for now.
mblighf4c35322006-03-13 01:01:10 +0000565
mblighcc2e6662006-09-14 01:24:07 +0000566 # At this point I know what arch I *want* to build for
567 # but have no way of working out what arch the default
568 # compiler DOES build for.
569
570 # Oh, and BTW, install_package() doesn't exist yet.
mbligh72b88fc2006-12-16 18:41:35 +0000571
mblighcc2e6662006-09-14 01:24:07 +0000572 if target_arch == 'ppc64':
573 install_package('ppc64-cross')
mbligh678823f2006-12-07 18:49:00 +0000574 cross_compile = os.path.join(self.autodir, 'sources/ppc64-cross/bin')
mblighcc2e6662006-09-14 01:24:07 +0000575
576 elif target_arch == 'x86_64':
577 install_package('x86_64-cross')
mbligh678823f2006-12-07 18:49:00 +0000578 cross_compile = os.path.join(self.autodir, 'sources/x86_64-cross/bin')
mblighb8a14e32006-05-06 00:17:35 +0000579
mbligh5970cf02006-08-06 15:39:22 +0000580 os.environ['ARCH'] = self.target_arch = target_arch
581
582 self.cross_compile = cross_compile
583 if self.cross_compile:
584 os.environ['CROSS_COMPILE'] = self.cross_compile
mblighcc2e6662006-09-14 01:24:07 +0000585
mbligh72b88fc2006-12-16 18:41:35 +0000586
mblighb8a14e32006-05-06 00:17:35 +0000587 def pickle_dump(self, filename):
mblighc86b0b42006-07-28 17:35:28 +0000588 """dump a pickle of ourself out to the specified filename
589
590 we can't pickle the backreference to job (it contains fd's),
mbligh709bb9b2006-10-12 04:32:16 +0000591 nor would we want to. Same for logfile (fd's).
mblighc86b0b42006-07-28 17:35:28 +0000592 """
mblighb8a14e32006-05-06 00:17:35 +0000593 temp = copy.copy(self)
594 temp.job = None
mbligh709bb9b2006-10-12 04:32:16 +0000595 temp.logfile = None
mblighb8a14e32006-05-06 00:17:35 +0000596 pickle.dump(temp, open(filename, 'w'))
mbligh736adc92007-10-18 03:23:22 +0000597
598
599class rpm_kernel:
600 """ Class for installing rpm kernel package
601 """
602
603 def __init__(self, job, rpm_package, results_dir):
604 self.job = job
mbligh10a24a72007-10-24 21:02:53 +0000605 self.installed = False
mbligh736adc92007-10-18 03:23:22 +0000606 self.rpm_package = rpm_package
607 self.log_dir = os.path.join(results_dir, 'debug')
608 if os.path.exists(self.log_dir):
609 system('rm -rf ' + self.log_dir)
610 os.mkdir(self.log_dir)
611
612
613 def install(self):
614 logfile = os.path.join(self.log_dir, 'rpm_install')
615 self.job.stdout.redirect(logfile + '.stdout')
616 self.job.stderr.redirect(logfile + '.stderr')
617
618 rpm_name = system_output('rpm -qp ' + self.rpm_package)
619
620 # install
621 system('rpm -i --force ' + self.rpm_package)
622
623 # get file list
624 files = system_output('rpm -ql ' + rpm_name).splitlines()
625
626 self.job.stdout.restore()
627 self.job.stderr.restore()
628
629 # search for vmlinuz
630 for file in files:
631 if file.startswith('/boot/vmlinuz'):
632 self.image = file
633 break
634 else:
635 raise TestError(self.rpm_package + " doesn't contain /boot/vmlinuz")
636
637 # search for initrd
638 self.initrd = ''
639 for file in files:
640 if file.startswith('/boot/initrd'):
641 self.initrd = file
642 break
643
644 # get version and release number
645 self.version, self.release = system_output(
mbligh10a24a72007-10-24 21:02:53 +0000646 'rpm --queryformat="%{VERSION}\\n%{RELEASE}\\n" -q ' + rpm_name).splitlines()[0:2]
647 self.installed = True
mbligh736adc92007-10-18 03:23:22 +0000648
649
650 def add_to_bootloader(self, tag='autotest', args=''):
651 """ Add this kernel to bootloader
652 """
653
654 # remove existing entry if present
655 self.job.bootloader.remove_kernel(tag)
656
657 # pull the base argument set from the job config
658 baseargs = self.job.config_get('boot.default_args')
659 if baseargs:
660 args = baseargs + ' ' + args
661
662 # otherwise populate from /proc/cmdline
663 # if not baseargs:
664 # baseargs = open('/proc/cmdline', 'r').readline().strip()
665 # NOTE: This is unnecessary, because boottool does it.
666
667 root = None
668 roots = [x for x in args.split() if x.startswith('root=')]
669 if roots:
670 root = re.sub('^root=', '', roots[0])
671 arglist = [x for x in args.split() if not x.startswith('root=')]
672 args = ' '.join(arglist)
673
674 # add the kernel entry
675 self.job.bootloader.add_kernel(self.image, tag, self.initrd, args = args, root = root)
mbligh10a24a72007-10-24 21:02:53 +0000676
677
678 def boot(self, args=''):
679 # Check if the kernel has been installed, if not install
680 # as the default tag and boot that.
681 if not self.installed:
682 self.install()
683
684 # Boot the selected tag.
685 self.add_to_bootloader(args=args)
686
687 # Boot it.
688 self.job.reboot()