blob: 806649324e87273bccb04a9640184772669ecb66 [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
63 if not leave:
64 if os.path.isdir(self.src_dir):
65 system('rm -rf ' + self.src_dir)
66 if os.path.isdir(self.build_dir):
67 system('rm -rf ' + self.build_dir)
68
mblighf4c35322006-03-13 01:01:10 +000069 os.mkdir(self.src_dir)
mbligh1e8858e2006-11-24 22:18:35 +000070 for path in [self.config_dir, self.log_dir, self.results_dir]:
71 if os.path.exists(path):
72 system('rm -rf ' + path)
73 os.mkdir(path)
mblighf4c35322006-03-13 01:01:10 +000074
mbligh4426de02006-10-10 07:18:28 +000075 logpath = os.path.join(self.log_dir, 'build_log')
76 self.logfile = open(logpath, 'w+')
77
mbligh72b88fc2006-12-16 18:41:35 +000078 self.target_arch = None
mblighfdbcaec2006-10-01 23:28:57 +000079 self.build_target = 'bzImage'
mbligh8baa2ea2006-12-17 23:01:24 +000080 self.build_image = None
mblighfdbcaec2006-10-01 23:28:57 +000081
mblighcac347a2007-06-02 17:21:48 +000082 if get_current_kernel_arch() == 'ia64':
83 self.build_target = 'all'
84 self.build_image = 'vmlinux.gz'
85
mblighfdbcaec2006-10-01 23:28:57 +000086 if leave:
87 return
mbligh534015f2006-09-15 03:28:56 +000088
mbligh4426de02006-10-10 07:18:28 +000089 self.logfile.write('BASE: %s\n' % base_tree)
apw2366d992007-03-12 20:35:57 +000090
91 # Where we have direct version hint record that
92 # for later configuration selection.
93 shorthand = re.compile(r'^\d+\.\d+\.\d+')
94 if shorthand.match(base_tree):
95 self.base_tree_version = base_tree
96 else:
97 self.base_tree_version = None
98
mbligh534015f2006-09-15 03:28:56 +000099 if os.path.exists(base_tree):
100 self.get_kernel_tree(base_tree)
101 else:
apw8ad56be2006-11-06 17:49:54 +0000102 args = self.job.config_get('mirror.ftp_kernel_org')
mbligh548f29a2006-10-17 04:55:12 +0000103 if args:
104 args = '-l ' + args
mbligh72b88fc2006-12-16 18:41:35 +0000105 base_components = kernelexpand(base_tree, args)
mbligh534015f2006-09-15 03:28:56 +0000106 print 'kernelexpand: '
107 print base_components
108 self.get_kernel_tree(base_components.pop(0))
109 if base_components: # apply remaining patches
110 self.patch(*base_components)
mblighf4c35322006-03-13 01:01:10 +0000111
112
mbligh5925e962007-08-30 17:05:22 +0000113 def __record(self, fn, name, *args, **dargs):
114 try:
115 fn(*args, **dargs)
mbligh09f288a2007-09-18 21:34:57 +0000116 self.job.record('GOOD', self.subdir, name)
mbligh5925e962007-08-30 17:05:22 +0000117 except Exception, detail:
mbligh09f288a2007-09-18 21:34:57 +0000118 err = detail.__str__()
119 self.job.record('FAIL', self.subdir, name, err)
mbligh5925e962007-08-30 17:05:22 +0000120 raise
121
122
123 def _patch(self, *patches):
mblighc86b0b42006-07-28 17:35:28 +0000124 """Apply a list of patches (in order)"""
mbligh1e8858e2006-11-24 22:18:35 +0000125 if not patches:
126 return
mblighd016ecc2006-11-25 21:41:07 +0000127 # if isinstance(patches, basestring):
128 # patches = [patches]
mbligh534015f2006-09-15 03:28:56 +0000129 print 'Applying patches: ', patches
130 # self.job.stdout.redirect(os.path.join(self.log_dir, 'stdout'))
mbligh0763e732007-08-30 16:35:06 +0000131 self.apply_patches(self.get_patches(patches))
mbligh534015f2006-09-15 03:28:56 +0000132 # self.job.stdout.restore()
mblighf4c35322006-03-13 01:01:10 +0000133
134
mbligh5925e962007-08-30 17:05:22 +0000135 def patch(self, *args, **dargs):
136 self.__record(self._patch, "kernel.patch", *args, **dargs)
137
138
139 def _config(self, config_file = '', config_list = None,
apwb449c7d2006-12-05 12:21:44 +0000140 defconfig = False):
mbligh93057012006-08-06 15:51:56 +0000141 self.job.stdout.redirect(os.path.join(self.log_dir, 'stdout'))
mbligh678823f2006-12-07 18:49:00 +0000142 self.set_cross_cc()
apwb449c7d2006-12-05 12:21:44 +0000143 config = kernel_config.kernel_config(self.job, self.build_dir,
apw2366d992007-03-12 20:35:57 +0000144 self.config_dir, config_file, config_list,
145 defconfig, self.base_tree_version)
mblighf4c35322006-03-13 01:01:10 +0000146 self.job.stdout.restore()
147
148
mbligh5925e962007-08-30 17:05:22 +0000149 def config(self, *args, **dargs):
150 self.__record(self._config, "kernel.config", *args, **dargs)
151
152
mblighf4c35322006-03-13 01:01:10 +0000153 def get_patches(self, patches):
mbligh1e8858e2006-11-24 22:18:35 +0000154 """fetch the patches to the local src_dir"""
mblighf4c35322006-03-13 01:01:10 +0000155 local_patches = []
156 for patch in patches:
mbligh1e8858e2006-11-24 22:18:35 +0000157 dest = os.path.join(self.src_dir, basename(patch))
mbligh0763e732007-08-30 16:35:06 +0000158 # FIXME: this isn't unique. Append something to it
159 # like wget does if it's not there?
mbligh1e8858e2006-11-24 22:18:35 +0000160 print "get_file %s %s %s %s" % (patch, dest, self.src_dir, basename(patch))
mblighf4c35322006-03-13 01:01:10 +0000161 get_file(patch, dest)
mbligh0763e732007-08-30 16:35:06 +0000162 # probably safer to use the command, not python library
163 md5sum = system_output('md5sum ' + dest).split()[0]
164 local_patches.append((patch, dest, md5sum))
mbligh534015f2006-09-15 03:28:56 +0000165 return local_patches
mblighf4c35322006-03-13 01:01:10 +0000166
mbligh72b88fc2006-12-16 18:41:35 +0000167
mbligh534015f2006-09-15 03:28:56 +0000168 def apply_patches(self, local_patches):
mblighc86b0b42006-07-28 17:35:28 +0000169 """apply the list of patches, in order"""
mblighf4c35322006-03-13 01:01:10 +0000170 builddir = self.build_dir
171 os.chdir(builddir)
172
mbligh534015f2006-09-15 03:28:56 +0000173 if not local_patches:
mblighf4c35322006-03-13 01:01:10 +0000174 return None
mbligh0763e732007-08-30 16:35:06 +0000175 for (spec, local, md5sum) in local_patches:
176 if local.endswith('.bz2') or local.endswith('.gz'):
177 ref = spec
178 else:
179 ref = force_copy(local, self.results_dir)
180 ref = self.job.relative_path(ref)
181 log = 'PATCH: %s %s %s\n' % (spec, ref, md5sum)
182 print log
183 cat_file_to_cmd(local, 'patch -p1 > /dev/null')
184 self.logfile.write(log)
mbligh72b88fc2006-12-16 18:41:35 +0000185
186
187 def get_kernel_tree(self, base_tree):
mblighc49af7b2006-11-24 04:02:21 +0000188 """Extract/link base_tree to self.build_dir"""
mbligh5970cf02006-08-06 15:39:22 +0000189
190 # if base_tree is a dir, assume uncompressed kernel
191 if os.path.isdir(base_tree):
192 print 'Symlinking existing kernel source'
mblighc49af7b2006-11-24 04:02:21 +0000193 os.symlink(base_tree, self.build_dir)
mblighf4c35322006-03-13 01:01:10 +0000194
mbligh5970cf02006-08-06 15:39:22 +0000195 # otherwise, extract tarball
196 else:
mblighc49af7b2006-11-24 04:02:21 +0000197 os.chdir(os.path.dirname(self.src_dir))
198 # Figure out local destination for tarball
199 tarball = os.path.join(self.src_dir, os.path.basename(base_tree))
mbligh5970cf02006-08-06 15:39:22 +0000200 get_file(base_tree, tarball)
mbligh5970cf02006-08-06 15:39:22 +0000201 print 'Extracting kernel tarball:', tarball, '...'
mblighc49af7b2006-11-24 04:02:21 +0000202 extract_tarball_to_dir(tarball, self.build_dir)
mbligh5970cf02006-08-06 15:39:22 +0000203
mblighf4c35322006-03-13 01:01:10 +0000204
mblighfdbcaec2006-10-01 23:28:57 +0000205 def extraversion(self, tag, append=1):
206 os.chdir(self.build_dir)
mbligh4426de02006-10-10 07:18:28 +0000207 extraversion_sub = r's/^EXTRAVERSION =\s*\(.*\)/EXTRAVERSION = '
mblighfdbcaec2006-10-01 23:28:57 +0000208 if append:
mbligh4426de02006-10-10 07:18:28 +0000209 p = extraversion_sub + '\\1-%s/' % tag
mblighfdbcaec2006-10-01 23:28:57 +0000210 else:
mbligh4426de02006-10-10 07:18:28 +0000211 p = extraversion_sub + '-%s/' % tag
mbligh4c3fe4a2007-07-31 17:59:21 +0000212 system('mv Makefile Makefile.old')
213 system('sed "%s" < Makefile.old > Makefile' % p)
mblighfdbcaec2006-10-01 23:28:57 +0000214
215
mbligh5925e962007-08-30 17:05:22 +0000216 def _build(self, make_opts = '', logfile = '', extraversion='autotest'):
mblighc86b0b42006-07-28 17:35:28 +0000217 """build the kernel
mbligh72b88fc2006-12-16 18:41:35 +0000218
mblighc86b0b42006-07-28 17:35:28 +0000219 make_opts
220 additional options to make, if any
221 """
mbligh10038012006-10-19 03:32:45 +0000222 os_dep.commands('gcc', 'make')
mbligh6d4c9412006-09-13 23:08:44 +0000223 if logfile == '':
224 logfile = os.path.join(self.log_dir, 'kernel_build')
mblighf4c35322006-03-13 01:01:10 +0000225 os.chdir(self.build_dir)
mblighfdbcaec2006-10-01 23:28:57 +0000226 if extraversion:
227 self.extraversion(extraversion)
mbligh93057012006-08-06 15:51:56 +0000228 print os.path.join(self.log_dir, 'stdout')
mbligh6d4c9412006-09-13 23:08:44 +0000229 self.job.stdout.redirect(logfile + '.stdout')
230 self.job.stderr.redirect(logfile + '.stderr')
mbligha2508052006-05-28 21:29:53 +0000231 self.set_cross_cc()
mblighf4c35322006-03-13 01:01:10 +0000232 # setup_config_file(config_file, config_overrides)
apwc7846102006-04-06 18:22:13 +0000233
234 # Not needed on 2.6, but hard to tell -- handle failure
mblighfdbcaec2006-10-01 23:28:57 +0000235 system('make dep', ignorestatus=1)
mblighf4c35322006-03-13 01:01:10 +0000236 threads = 2 * count_cpus()
mbligh6d4c9412006-09-13 23:08:44 +0000237 build_string = 'make -j %d %s %s' % (threads, make_opts,
238 self.build_target)
239 # eg make bzImage, or make zImage
240 print build_string
241 system(build_string)
mblighf4c35322006-03-13 01:01:10 +0000242 if kernel_config.modules_needed('.config'):
apwd4701972006-10-16 09:24:56 +0000243 system('make -j %d modules' % (threads))
apwc7846102006-04-06 18:22:13 +0000244
mblighf4c35322006-03-13 01:01:10 +0000245 self.job.stdout.restore()
246 self.job.stderr.restore()
mbligh72b88fc2006-12-16 18:41:35 +0000247
mbligh4426de02006-10-10 07:18:28 +0000248 kernel_version = self.get_kernel_build_ver()
249 kernel_version = re.sub('-autotest', '', kernel_version)
250 self.logfile.write('BUILD VERSION: %s\n' % kernel_version)
mbligh1e8858e2006-11-24 22:18:35 +0000251
252 force_copy(self.build_dir+'/System.map', self.results_dir)
mblighf4c35322006-03-13 01:01:10 +0000253
254
mbligh5925e962007-08-30 17:05:22 +0000255 def build(self, *args, **dargs):
256 self.__record(self._build, "kernel.build", *args, **dargs)
257
258
mblighf4c35322006-03-13 01:01:10 +0000259 def build_timed(self, threads, timefile = '/dev/null', make_opts = ''):
mblighc86b0b42006-07-28 17:35:28 +0000260 """time the bulding of the kernel"""
mblighb8a14e32006-05-06 00:17:35 +0000261 os.chdir(self.build_dir)
mbligh678823f2006-12-07 18:49:00 +0000262 self.set_cross_cc()
mblighb8a14e32006-05-06 00:17:35 +0000263 print "make clean"
264 system('make clean')
mbligha2bb9d62006-10-09 16:26:03 +0000265 build_string = "/usr/bin/time -o %s make %s -j %s vmlinux" % (timefile, make_opts, threads)
mblighf4c35322006-03-13 01:01:10 +0000266 print build_string
apwc7846102006-04-06 18:22:13 +0000267 system(build_string)
268 if (not os.path.isfile('vmlinux')):
269 raise TestError("no vmlinux found, kernel build failed")
mblighb8a14e32006-05-06 00:17:35 +0000270
271
mbligh5925e962007-08-30 17:05:22 +0000272 def _clean(self):
mblighc86b0b42006-07-28 17:35:28 +0000273 """make clean in the kernel tree"""
mblighb8a14e32006-05-06 00:17:35 +0000274 os.chdir(self.build_dir)
mblighf4c35322006-03-13 01:01:10 +0000275 print "make clean"
apwc7846102006-04-06 18:22:13 +0000276 system('make clean')
mblighf4c35322006-03-13 01:01:10 +0000277
mbligh50f42ea2006-09-30 22:22:21 +0000278
mbligh5925e962007-08-30 17:05:22 +0000279 def clean(self, *args, **dargs):
280 self.__record(self._clean, "kernel.clean", *args, **dargs)
281
282
283 def _mkinitrd(self, version, image, system_map, initrd):
mbligh50f42ea2006-09-30 22:22:21 +0000284 """Build kernel initrd image.
285 Try to use distro specific way to build initrd image.
286 Parameters:
287 version
288 new kernel version
289 image
290 new kernel image file
291 system_map
292 System.map file
293 initrd
294 initrd image file to build
295 """
296 vendor = get_os_vendor()
297
298 if os.path.isfile(initrd):
mblighfdbcaec2006-10-01 23:28:57 +0000299 print "Existing %s file, will remove it." % initrd
mbligh50f42ea2006-09-30 22:22:21 +0000300 os.remove(initrd)
mbligh72b88fc2006-12-16 18:41:35 +0000301
mbligh50f42ea2006-09-30 22:22:21 +0000302 if vendor in ['Red Hat', 'Fedora Core']:
mblighfdbcaec2006-10-01 23:28:57 +0000303 system('mkinitrd %s %s' % (initrd, version))
mbligh50f42ea2006-09-30 22:22:21 +0000304 elif vendor in ['SUSE']:
mblighfdbcaec2006-10-01 23:28:57 +0000305 system('mkinitrd -k %s -i %s -M %s' % (image, initrd, system_map))
apwc898a1f2007-02-28 15:27:22 +0000306 elif vendor in ['Debian', 'Ubuntu']:
307 if os.path.isfile('/usr/sbin/mkinitrd'):
308 cmd = '/usr/sbin/mkinitrd'
309 elif os.path.isfile('/usr/sbin/mkinitramfs'):
310 cmd = '/usr/sbin/mkinitramfs'
311 else:
312 raise TestError('No Debian initrd builder')
313 system('%s -o %s %s' % (cmd, initrd, version))
mbligh50f42ea2006-09-30 22:22:21 +0000314 else:
mblighfdbcaec2006-10-01 23:28:57 +0000315 raise TestError('Unsupported vendor %s' % vendor)
mbligh50f42ea2006-09-30 22:22:21 +0000316
317
mbligh5925e962007-08-30 17:05:22 +0000318 def mkinitrd(self, *args, **dargs):
319 self.__record(self._mkinitrd, "kernel.mkinitrd", *args, **dargs)
320
321
mbligh8baa2ea2006-12-17 23:01:24 +0000322 def set_build_image(self, image):
323 self.build_image = image
324
325
mbligh5925e962007-08-30 17:05:22 +0000326 def _install(self, tag='autotest', prefix = '/'):
mblighc86b0b42006-07-28 17:35:28 +0000327 """make install in the kernel tree"""
mblighf4c35322006-03-13 01:01:10 +0000328 os.chdir(self.build_dir)
mbligh72b88fc2006-12-16 18:41:35 +0000329
mbligh6a1d4db2006-10-06 04:30:16 +0000330 if not os.path.isdir(prefix):
331 os.mkdir(prefix)
mbligha87116f2006-10-10 02:47:08 +0000332 self.boot_dir = os.path.join(prefix, 'boot')
333 if not os.path.isdir(self.boot_dir):
334 os.mkdir(self.boot_dir)
mbligh0ad65582006-10-06 04:16:36 +0000335
mbligh8baa2ea2006-12-17 23:01:24 +0000336 if not self.build_image:
337 images = glob.glob('arch/*/boot/' + self.build_target)
338 if len(images):
339 self.build_image = images[0]
340 else:
341 self.build_image = self.build_target
mbligha87116f2006-10-10 02:47:08 +0000342
343 # remember installed files
mbligha87116f2006-10-10 02:47:08 +0000344 self.vmlinux = self.boot_dir + '/vmlinux-' + tag
mbligh8baa2ea2006-12-17 23:01:24 +0000345 if (self.build_image != 'vmlinux'):
apw9a61c5b2006-11-28 10:03:15 +0000346 self.image = self.boot_dir + '/vmlinuz-' + tag
347 else:
348 self.image = self.vmlinux
mbligha87116f2006-10-10 02:47:08 +0000349 self.system_map = self.boot_dir + '/System.map-' + tag
350 self.config = self.boot_dir + '/config-' + tag
351 self.initrd = ''
352
353 # copy to boot dir
mbligha87116f2006-10-10 02:47:08 +0000354 force_copy('vmlinux', self.vmlinux)
mbligh8baa2ea2006-12-17 23:01:24 +0000355 if (self.build_image != 'vmlinux'):
356 force_copy(self.build_image, self.image)
mbligha87116f2006-10-10 02:47:08 +0000357 force_copy('System.map', self.system_map)
358 force_copy('.config', self.config)
359
mbligh6a1d4db2006-10-06 04:30:16 +0000360 if not kernel_config.modules_needed('.config'):
361 return
362
363 system('make modules_install INSTALL_MOD_PATH=%s' % prefix)
364 if prefix == '/':
mbligha87116f2006-10-10 02:47:08 +0000365 self.initrd = self.boot_dir + '/initrd-' + tag
366 self.mkinitrd(self.get_kernel_build_ver(), self.image, \
367 self.system_map, self.initrd)
368
369
mbligh5925e962007-08-30 17:05:22 +0000370 def install(self, *args, **dargs):
371 self.__record(self._install, "kernel.install", *args, **dargs)
372
373
mbligha87116f2006-10-10 02:47:08 +0000374 def add_to_bootloader(self, tag='autotest', args=''):
375 """ add this kernel to bootloader, taking an
376 optional parameter of space separated parameters
377 e.g.: kernel.add_to_bootloader('mykernel', 'ro acpi=off')
378 """
379
380 # remove existing entry if present
381 self.job.bootloader.remove_kernel(tag)
382
apwcbe32572006-11-28 10:00:23 +0000383 # pull the base argument set from the job config,
apwcbe32572006-11-28 10:00:23 +0000384 baseargs = self.job.config_get('boot.default_args')
mbligh78bc05e2006-12-25 02:29:59 +0000385 if baseargs:
386 args = baseargs + " " + args
387
388 # otherwise populate from /proc/cmdline
389 # if not baseargs:
390 # baseargs = open('/proc/cmdline', 'r').readline().strip()
391 # NOTE: This is unnecessary, because boottool does it.
apwcbe32572006-11-28 10:00:23 +0000392
mbligh78bc05e2006-12-25 02:29:59 +0000393 root = None
394 roots = [x for x in args.split() if x.startswith('root=')]
395 if roots:
396 root = re.sub('^root=', '', roots[0])
397 arglist = [x for x in args.split() if not x.startswith('root=')]
398 args = ' '.join(arglist)
mbligha87116f2006-10-10 02:47:08 +0000399
mbligh78bc05e2006-12-25 02:29:59 +0000400 # add the kernel entry
401 # add_kernel(image, title='autotest', initrd='')
402 self.job.bootloader.add_kernel(self.image, tag, self.initrd, \
403 args = args, root = root)
mbligh6a1d4db2006-10-06 04:30:16 +0000404
mbligh201aa892006-10-29 04:02:05 +0000405
mbligh548f29a2006-10-17 04:55:12 +0000406 def get_kernel_build_arch(self, arch=None):
mbligh201aa892006-10-29 04:02:05 +0000407 """
408 Work out the current kernel architecture (as a kernel arch)
409 """
mbligh548f29a2006-10-17 04:55:12 +0000410 if not arch:
411 arch = get_current_kernel_arch()
412 if re.match('i.86', arch):
413 return 'i386'
414 elif re.match('sun4u', arch):
415 return 'sparc64'
416 elif re.match('arm.*', arch):
417 return 'arm'
418 elif re.match('sa110', arch):
419 return 'arm'
420 elif re.match('s390x', arch):
421 return 's390'
422 elif re.match('parisc64', arch):
423 return 'parisc'
424 elif re.match('ppc.*', arch):
425 return 'powerpc'
426 elif re.match('mips.*', arch):
427 return 'mips'
428 else:
429 return arch
430
mbligh6a1d4db2006-10-06 04:30:16 +0000431
mbligh237bed32007-09-05 13:05:57 +0000432 def get_kernel_build_release(self):
433 releasem = re.compile(r'.*UTS_RELEASE\s+"([^"]+)".*');
434 versionm = re.compile(r'.*UTS_VERSION\s+"([^"]+)".*');
435
436 release = None
437 version = None
438
439 for file in [ self.build_dir + "/include/linux/version.h",
440 self.build_dir + "/include/linux/utsrelease.h",
441 self.build_dir + "/include/linux/compile.h" ]:
442 if os.path.exists(file):
443 fd = open(file, 'r')
444 for line in fd.readlines():
445 m = releasem.match(line)
446 if m:
447 release = m.groups()[0]
448 m = versionm.match(line)
449 if m:
450 version = m.groups()[0]
451 fd.close()
452
453 return (release, version)
454
455
456 def get_kernel_build_ident(self):
457 (release, version) = self.get_kernel_build_release()
458
459 if not release or not version:
460 raise JobError('kernel has no identity')
461
462 return release + '::' + version
463
464
465 def boot(self, args='', once=True, ident=1):
apw1b5dc362006-10-31 11:24:26 +0000466 """ install and boot this kernel, do not care how
467 just make it happen.
468 """
469
mbligh237bed32007-09-05 13:05:57 +0000470 # If we can check the kernel identity do so.
471 if ident:
472 when = int(time.time())
473 ident = self.get_kernel_build_ident()
474 args += " IDENT=%d" % (when)
475
476 self.job.next_step_prepend([kernel_check_ident,
477 when, ident])
478
apw1b5dc362006-10-31 11:24:26 +0000479 # Install this kernel.
480 self.install()
mblighda76b212007-07-19 16:50:41 +0000481 self.add_to_bootloader(args=args, tag='autotest')
482 if once:
483 self.job.bootloader.boot_once('autotest')
apw1b5dc362006-10-31 11:24:26 +0000484
485 # Boot it.
486 self.job.reboot()
487
488
mblighfdbcaec2006-10-01 23:28:57 +0000489 def get_kernel_build_ver(self):
mblighe11f5fc2006-10-04 04:42:22 +0000490 """Check Makefile and .config to return kernel version"""
491 version = patchlevel = sublevel = extraversion = localversion = ''
492
493 for line in open(self.build_dir + '/Makefile', 'r').readlines():
494 if line.startswith('VERSION'):
495 version = line[line.index('=') + 1:].strip()
496 if line.startswith('PATCHLEVEL'):
497 patchlevel = line[line.index('=') + 1:].strip()
498 if line.startswith('SUBLEVEL'):
499 sublevel = line[line.index('=') + 1:].strip()
500 if line.startswith('EXTRAVERSION'):
501 extraversion = line[line.index('=') + 1:].strip()
502
503 for line in open(self.build_dir + '/.config', 'r').readlines():
504 if line.startswith('CONFIG_LOCALVERSION='):
505 localversion = line.rstrip().split('"')[1]
506
mbligh72b88fc2006-12-16 18:41:35 +0000507 return "%s.%s.%s%s%s" %(version, patchlevel, sublevel, extraversion, localversion)
mblighfdbcaec2006-10-01 23:28:57 +0000508
509
mbligh8baa2ea2006-12-17 23:01:24 +0000510 def set_build_target(self, build_target):
511 if build_target:
512 self.build_target = build_target
513 print 'BUILD TARGET: %s' % self.build_target
514
515
mbligh5970cf02006-08-06 15:39:22 +0000516 def set_cross_cc(self, target_arch=None, cross_compile=None,
517 build_target='bzImage'):
mblighc86b0b42006-07-28 17:35:28 +0000518 """Set up to cross-compile.
mblighcc2e6662006-09-14 01:24:07 +0000519 This is broken. We need to work out what the default
520 compile produces, and if not, THEN set the cross
521 compiler.
mblighc86b0b42006-07-28 17:35:28 +0000522 """
mblighcc2e6662006-09-14 01:24:07 +0000523
mbligh5970cf02006-08-06 15:39:22 +0000524 if self.target_arch:
525 return
mbligh678823f2006-12-07 18:49:00 +0000526
mbligh8baa2ea2006-12-17 23:01:24 +0000527 # if someone has set build_target, don't clobber in set_cross_cc
528 # run set_build_target before calling set_cross_cc
529 if not self.build_target:
530 self.set_build_target(build_target)
mbligh72b88fc2006-12-16 18:41:35 +0000531
mbligh5970cf02006-08-06 15:39:22 +0000532 # If no 'target_arch' given assume native compilation
533 if target_arch == None:
mbligh548f29a2006-10-17 04:55:12 +0000534 target_arch = get_current_kernel_arch()
mbligh5970cf02006-08-06 15:39:22 +0000535 if target_arch == 'ppc64':
mbligh5970cf02006-08-06 15:39:22 +0000536 if self.build_target == 'bzImage':
apw9a61c5b2006-11-28 10:03:15 +0000537 self.build_target = 'vmlinux'
mbligh678823f2006-12-07 18:49:00 +0000538
539 if not cross_compile:
540 cross_compile = self.job.config_get('kernel.cross_cc')
541
542 if cross_compile:
543 os.environ['CROSS_COMPILE'] = cross_compile
544 else:
545 if os.environ.has_key('CROSS_COMPILE'):
546 del os.environ['CROSS_COMPILE']
547
mblighcc2e6662006-09-14 01:24:07 +0000548 return # HACK. Crap out for now.
mblighf4c35322006-03-13 01:01:10 +0000549
mblighcc2e6662006-09-14 01:24:07 +0000550 # At this point I know what arch I *want* to build for
551 # but have no way of working out what arch the default
552 # compiler DOES build for.
553
554 # Oh, and BTW, install_package() doesn't exist yet.
mbligh72b88fc2006-12-16 18:41:35 +0000555
mblighcc2e6662006-09-14 01:24:07 +0000556 if target_arch == 'ppc64':
557 install_package('ppc64-cross')
mbligh678823f2006-12-07 18:49:00 +0000558 cross_compile = os.path.join(self.autodir, 'sources/ppc64-cross/bin')
mblighcc2e6662006-09-14 01:24:07 +0000559
560 elif target_arch == 'x86_64':
561 install_package('x86_64-cross')
mbligh678823f2006-12-07 18:49:00 +0000562 cross_compile = os.path.join(self.autodir, 'sources/x86_64-cross/bin')
mblighb8a14e32006-05-06 00:17:35 +0000563
mbligh5970cf02006-08-06 15:39:22 +0000564 os.environ['ARCH'] = self.target_arch = target_arch
565
566 self.cross_compile = cross_compile
567 if self.cross_compile:
568 os.environ['CROSS_COMPILE'] = self.cross_compile
mblighcc2e6662006-09-14 01:24:07 +0000569
mbligh72b88fc2006-12-16 18:41:35 +0000570
mblighb8a14e32006-05-06 00:17:35 +0000571 def pickle_dump(self, filename):
mblighc86b0b42006-07-28 17:35:28 +0000572 """dump a pickle of ourself out to the specified filename
573
574 we can't pickle the backreference to job (it contains fd's),
mbligh709bb9b2006-10-12 04:32:16 +0000575 nor would we want to. Same for logfile (fd's).
mblighc86b0b42006-07-28 17:35:28 +0000576 """
mblighb8a14e32006-05-06 00:17:35 +0000577 temp = copy.copy(self)
578 temp.job = None
mbligh709bb9b2006-10-12 04:32:16 +0000579 temp.logfile = None
mblighb8a14e32006-05-06 00:17:35 +0000580 pickle.dump(temp, open(filename, 'w'))