blob: fd70728dd3ab72f28005f8107b59faf1b5d37888 [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
mbligh10038012006-10-19 03:32:45 +00007import kernel_config, test, os_dep
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
mblighcac347a2007-06-02 17:21:48 +000088 if get_current_kernel_arch() == 'ia64':
89 self.build_target = 'all'
90 self.build_image = 'vmlinux.gz'
91
mblighfdbcaec2006-10-01 23:28:57 +000092 if leave:
93 return
mbligh534015f2006-09-15 03:28:56 +000094
mbligh4426de02006-10-10 07:18:28 +000095 self.logfile.write('BASE: %s\n' % base_tree)
apw2366d992007-03-12 20:35:57 +000096
97 # Where we have direct version hint record that
98 # for later configuration selection.
99 shorthand = re.compile(r'^\d+\.\d+\.\d+')
100 if shorthand.match(base_tree):
101 self.base_tree_version = base_tree
102 else:
103 self.base_tree_version = None
104
mbligh534015f2006-09-15 03:28:56 +0000105 if os.path.exists(base_tree):
106 self.get_kernel_tree(base_tree)
107 else:
apw8ad56be2006-11-06 17:49:54 +0000108 args = self.job.config_get('mirror.ftp_kernel_org')
mbligh548f29a2006-10-17 04:55:12 +0000109 if args:
110 args = '-l ' + args
mbligh72b88fc2006-12-16 18:41:35 +0000111 base_components = kernelexpand(base_tree, args)
mbligh534015f2006-09-15 03:28:56 +0000112 print 'kernelexpand: '
113 print base_components
114 self.get_kernel_tree(base_components.pop(0))
115 if base_components: # apply remaining patches
116 self.patch(*base_components)
mblighf4c35322006-03-13 01:01:10 +0000117
118
mbligh119c12a2007-11-12 22:13:44 +0000119 @logging.record
apw001e40a2007-11-21 19:24:37 +0000120 @tee_output_logdir_mark
mblighb8e0a112007-11-05 20:27:36 +0000121 def patch(self, *patches):
mblighc86b0b42006-07-28 17:35:28 +0000122 """Apply a list of patches (in order)"""
mbligh1e8858e2006-11-24 22:18:35 +0000123 if not patches:
124 return
mbligh534015f2006-09-15 03:28:56 +0000125 print 'Applying patches: ', patches
mbligh0763e732007-08-30 16:35:06 +0000126 self.apply_patches(self.get_patches(patches))
mblighf4c35322006-03-13 01:01:10 +0000127
128
mbligh119c12a2007-11-12 22:13:44 +0000129 @logging.record
apw001e40a2007-11-21 19:24:37 +0000130 @tee_output_logdir_mark
mblighb8e0a112007-11-05 20:27:36 +0000131 def config(self, config_file = '', config_list = None, defconfig = False):
mbligh678823f2006-12-07 18:49:00 +0000132 self.set_cross_cc()
apwb449c7d2006-12-05 12:21:44 +0000133 config = kernel_config.kernel_config(self.job, self.build_dir,
apw2366d992007-03-12 20:35:57 +0000134 self.config_dir, config_file, config_list,
135 defconfig, self.base_tree_version)
mblighf4c35322006-03-13 01:01:10 +0000136
137
138 def get_patches(self, patches):
mbligh1e8858e2006-11-24 22:18:35 +0000139 """fetch the patches to the local src_dir"""
mblighf4c35322006-03-13 01:01:10 +0000140 local_patches = []
141 for patch in patches:
mbligh1e8858e2006-11-24 22:18:35 +0000142 dest = os.path.join(self.src_dir, basename(patch))
mbligh0763e732007-08-30 16:35:06 +0000143 # FIXME: this isn't unique. Append something to it
144 # like wget does if it's not there?
mbligh1e8858e2006-11-24 22:18:35 +0000145 print "get_file %s %s %s %s" % (patch, dest, self.src_dir, basename(patch))
mblighf4c35322006-03-13 01:01:10 +0000146 get_file(patch, dest)
mbligh0763e732007-08-30 16:35:06 +0000147 # probably safer to use the command, not python library
148 md5sum = system_output('md5sum ' + dest).split()[0]
149 local_patches.append((patch, dest, md5sum))
mbligh534015f2006-09-15 03:28:56 +0000150 return local_patches
mblighf4c35322006-03-13 01:01:10 +0000151
mbligh72b88fc2006-12-16 18:41:35 +0000152
mbligh534015f2006-09-15 03:28:56 +0000153 def apply_patches(self, local_patches):
mblighc86b0b42006-07-28 17:35:28 +0000154 """apply the list of patches, in order"""
mblighf4c35322006-03-13 01:01:10 +0000155 builddir = self.build_dir
156 os.chdir(builddir)
157
mbligh534015f2006-09-15 03:28:56 +0000158 if not local_patches:
mblighf4c35322006-03-13 01:01:10 +0000159 return None
mbligh0763e732007-08-30 16:35:06 +0000160 for (spec, local, md5sum) in local_patches:
161 if local.endswith('.bz2') or local.endswith('.gz'):
162 ref = spec
163 else:
164 ref = force_copy(local, self.results_dir)
165 ref = self.job.relative_path(ref)
166 log = 'PATCH: %s %s %s\n' % (spec, ref, md5sum)
167 print log
168 cat_file_to_cmd(local, 'patch -p1 > /dev/null')
169 self.logfile.write(log)
mbligh72b88fc2006-12-16 18:41:35 +0000170
171
172 def get_kernel_tree(self, base_tree):
mblighc49af7b2006-11-24 04:02:21 +0000173 """Extract/link base_tree to self.build_dir"""
mbligh5970cf02006-08-06 15:39:22 +0000174
175 # if base_tree is a dir, assume uncompressed kernel
176 if os.path.isdir(base_tree):
177 print 'Symlinking existing kernel source'
mblighc49af7b2006-11-24 04:02:21 +0000178 os.symlink(base_tree, self.build_dir)
mblighf4c35322006-03-13 01:01:10 +0000179
mbligh5970cf02006-08-06 15:39:22 +0000180 # otherwise, extract tarball
181 else:
mblighc49af7b2006-11-24 04:02:21 +0000182 os.chdir(os.path.dirname(self.src_dir))
183 # Figure out local destination for tarball
184 tarball = os.path.join(self.src_dir, os.path.basename(base_tree))
mbligh5970cf02006-08-06 15:39:22 +0000185 get_file(base_tree, tarball)
mbligh5970cf02006-08-06 15:39:22 +0000186 print 'Extracting kernel tarball:', tarball, '...'
mblighc49af7b2006-11-24 04:02:21 +0000187 extract_tarball_to_dir(tarball, self.build_dir)
mbligh5970cf02006-08-06 15:39:22 +0000188
mblighf4c35322006-03-13 01:01:10 +0000189
mblighfdbcaec2006-10-01 23:28:57 +0000190 def extraversion(self, tag, append=1):
191 os.chdir(self.build_dir)
mbligh4426de02006-10-10 07:18:28 +0000192 extraversion_sub = r's/^EXTRAVERSION =\s*\(.*\)/EXTRAVERSION = '
mblighfdbcaec2006-10-01 23:28:57 +0000193 if append:
mbligh4426de02006-10-10 07:18:28 +0000194 p = extraversion_sub + '\\1-%s/' % tag
mblighfdbcaec2006-10-01 23:28:57 +0000195 else:
mbligh4426de02006-10-10 07:18:28 +0000196 p = extraversion_sub + '-%s/' % tag
mbligh4c3fe4a2007-07-31 17:59:21 +0000197 system('mv Makefile Makefile.old')
198 system('sed "%s" < Makefile.old > Makefile' % p)
mblighfdbcaec2006-10-01 23:28:57 +0000199
200
mbligh119c12a2007-11-12 22:13:44 +0000201 @logging.record
apw001e40a2007-11-21 19:24:37 +0000202 @tee_output_logdir_mark
mblighb8e0a112007-11-05 20:27:36 +0000203 def build(self, make_opts = '', logfile = '', extraversion='autotest'):
mblighc86b0b42006-07-28 17:35:28 +0000204 """build the kernel
mbligh72b88fc2006-12-16 18:41:35 +0000205
mblighc86b0b42006-07-28 17:35:28 +0000206 make_opts
207 additional options to make, if any
208 """
mbligh10038012006-10-19 03:32:45 +0000209 os_dep.commands('gcc', 'make')
mbligh6d4c9412006-09-13 23:08:44 +0000210 if logfile == '':
211 logfile = os.path.join(self.log_dir, 'kernel_build')
mblighf4c35322006-03-13 01:01:10 +0000212 os.chdir(self.build_dir)
mblighfdbcaec2006-10-01 23:28:57 +0000213 if extraversion:
214 self.extraversion(extraversion)
mbligha2508052006-05-28 21:29:53 +0000215 self.set_cross_cc()
mblighf4c35322006-03-13 01:01:10 +0000216 # setup_config_file(config_file, config_overrides)
apwc7846102006-04-06 18:22:13 +0000217
218 # Not needed on 2.6, but hard to tell -- handle failure
mblighfdbcaec2006-10-01 23:28:57 +0000219 system('make dep', ignorestatus=1)
mblighf4c35322006-03-13 01:01:10 +0000220 threads = 2 * count_cpus()
mbligh6d4c9412006-09-13 23:08:44 +0000221 build_string = 'make -j %d %s %s' % (threads, make_opts,
222 self.build_target)
223 # eg make bzImage, or make zImage
224 print build_string
225 system(build_string)
mblighf4c35322006-03-13 01:01:10 +0000226 if kernel_config.modules_needed('.config'):
apwd4701972006-10-16 09:24:56 +0000227 system('make -j %d modules' % (threads))
apwc7846102006-04-06 18:22:13 +0000228
mbligh4426de02006-10-10 07:18:28 +0000229 kernel_version = self.get_kernel_build_ver()
230 kernel_version = re.sub('-autotest', '', kernel_version)
231 self.logfile.write('BUILD VERSION: %s\n' % kernel_version)
mbligh1e8858e2006-11-24 22:18:35 +0000232
233 force_copy(self.build_dir+'/System.map', self.results_dir)
mblighf4c35322006-03-13 01:01:10 +0000234
235
mbligh30f28c52007-10-11 18:35:35 +0000236 def build_timed(self, threads, timefile = '/dev/null', make_opts = '',
mbligh2e793a42007-10-12 23:58:35 +0000237 output = '/dev/null'):
mblighc86b0b42006-07-28 17:35:28 +0000238 """time the bulding of the kernel"""
mblighb8a14e32006-05-06 00:17:35 +0000239 os.chdir(self.build_dir)
mbligh678823f2006-12-07 18:49:00 +0000240 self.set_cross_cc()
mbligh30f28c52007-10-11 18:35:35 +0000241
mblighd79b2b42007-11-05 20:38:15 +0000242 self.clean(logged=False)
mbligh30f28c52007-10-11 18:35:35 +0000243 build_string = "/usr/bin/time -o %s make %s -j %s vmlinux" \
244 % (timefile, make_opts, threads)
mbligh2e793a42007-10-12 23:58:35 +0000245 build_string += ' > %s 2>&1' % output
mblighf4c35322006-03-13 01:01:10 +0000246 print build_string
apwc7846102006-04-06 18:22:13 +0000247 system(build_string)
mbligh30f28c52007-10-11 18:35:35 +0000248
apwc7846102006-04-06 18:22:13 +0000249 if (not os.path.isfile('vmlinux')):
250 raise TestError("no vmlinux found, kernel build failed")
mblighb8a14e32006-05-06 00:17:35 +0000251
252
mbligh119c12a2007-11-12 22:13:44 +0000253 @logging.record
apw001e40a2007-11-21 19:24:37 +0000254 @tee_output_logdir_mark
mblighb8e0a112007-11-05 20:27:36 +0000255 def clean(self):
mblighc86b0b42006-07-28 17:35:28 +0000256 """make clean in the kernel tree"""
mblighb8a14e32006-05-06 00:17:35 +0000257 os.chdir(self.build_dir)
mblighf4c35322006-03-13 01:01:10 +0000258 print "make clean"
mbligh30f28c52007-10-11 18:35:35 +0000259 system('make clean > /dev/null 2> /dev/null')
mblighf4c35322006-03-13 01:01:10 +0000260
mbligh50f42ea2006-09-30 22:22:21 +0000261
mbligh119c12a2007-11-12 22:13:44 +0000262 @logging.record
apw001e40a2007-11-21 19:24:37 +0000263 @tee_output_logdir_mark
mblighb8e0a112007-11-05 20:27:36 +0000264 def mkinitrd(self, version, image, system_map, initrd):
mbligh50f42ea2006-09-30 22:22:21 +0000265 """Build kernel initrd image.
266 Try to use distro specific way to build initrd image.
267 Parameters:
268 version
269 new kernel version
270 image
271 new kernel image file
272 system_map
273 System.map file
274 initrd
275 initrd image file to build
276 """
277 vendor = get_os_vendor()
278
279 if os.path.isfile(initrd):
mblighfdbcaec2006-10-01 23:28:57 +0000280 print "Existing %s file, will remove it." % initrd
mbligh50f42ea2006-09-30 22:22:21 +0000281 os.remove(initrd)
mbligh72b88fc2006-12-16 18:41:35 +0000282
apwe43a30b2007-09-25 16:51:30 +0000283 args = self.job.config_get('kernel.mkinitrd_extra_args')
284
mbligh3d515d42007-11-09 17:00:36 +0000285 # don't leak 'None' into mkinitrd command
286 if not args:
287 args = ''
288
mbligh50f42ea2006-09-30 22:22:21 +0000289 if vendor in ['Red Hat', 'Fedora Core']:
apwe43a30b2007-09-25 16:51:30 +0000290 system('mkinitrd %s %s %s' % (args, initrd, version))
mbligh50f42ea2006-09-30 22:22:21 +0000291 elif vendor in ['SUSE']:
apwe43a30b2007-09-25 16:51:30 +0000292 system('mkinitrd %s -k %s -i %s -M %s' % (args, image, initrd, system_map))
apwc898a1f2007-02-28 15:27:22 +0000293 elif vendor in ['Debian', 'Ubuntu']:
294 if os.path.isfile('/usr/sbin/mkinitrd'):
295 cmd = '/usr/sbin/mkinitrd'
296 elif os.path.isfile('/usr/sbin/mkinitramfs'):
297 cmd = '/usr/sbin/mkinitramfs'
298 else:
299 raise TestError('No Debian initrd builder')
apwe43a30b2007-09-25 16:51:30 +0000300 system('%s %s -o %s %s' % (cmd, args, initrd, version))
mbligh50f42ea2006-09-30 22:22:21 +0000301 else:
mblighfdbcaec2006-10-01 23:28:57 +0000302 raise TestError('Unsupported vendor %s' % vendor)
mbligh50f42ea2006-09-30 22:22:21 +0000303
304
mbligh8baa2ea2006-12-17 23:01:24 +0000305 def set_build_image(self, image):
306 self.build_image = image
307
308
mbligh119c12a2007-11-12 22:13:44 +0000309 @logging.record
apw001e40a2007-11-21 19:24:37 +0000310 @tee_output_logdir_mark
mblighb8e0a112007-11-05 20:27:36 +0000311 def install(self, tag='autotest', prefix = '/'):
mblighc86b0b42006-07-28 17:35:28 +0000312 """make install in the kernel tree"""
apw87c65c12007-09-27 17:19:37 +0000313
314 # Record that we have installed the kernel, and
315 # the tag under which we installed it.
316 self.installed_as = tag
317
mblighf4c35322006-03-13 01:01:10 +0000318 os.chdir(self.build_dir)
mbligh72b88fc2006-12-16 18:41:35 +0000319
mbligh6a1d4db2006-10-06 04:30:16 +0000320 if not os.path.isdir(prefix):
321 os.mkdir(prefix)
mbligha87116f2006-10-10 02:47:08 +0000322 self.boot_dir = os.path.join(prefix, 'boot')
323 if not os.path.isdir(self.boot_dir):
324 os.mkdir(self.boot_dir)
mbligh0ad65582006-10-06 04:16:36 +0000325
mbligh8baa2ea2006-12-17 23:01:24 +0000326 if not self.build_image:
327 images = glob.glob('arch/*/boot/' + self.build_target)
328 if len(images):
329 self.build_image = images[0]
330 else:
331 self.build_image = self.build_target
mbligha87116f2006-10-10 02:47:08 +0000332
333 # remember installed files
mbligha87116f2006-10-10 02:47:08 +0000334 self.vmlinux = self.boot_dir + '/vmlinux-' + tag
mbligh8baa2ea2006-12-17 23:01:24 +0000335 if (self.build_image != 'vmlinux'):
apw9a61c5b2006-11-28 10:03:15 +0000336 self.image = self.boot_dir + '/vmlinuz-' + tag
337 else:
338 self.image = self.vmlinux
mbligha87116f2006-10-10 02:47:08 +0000339 self.system_map = self.boot_dir + '/System.map-' + tag
340 self.config = self.boot_dir + '/config-' + tag
341 self.initrd = ''
342
343 # copy to boot dir
mbligha87116f2006-10-10 02:47:08 +0000344 force_copy('vmlinux', self.vmlinux)
mbligh8baa2ea2006-12-17 23:01:24 +0000345 if (self.build_image != 'vmlinux'):
346 force_copy(self.build_image, self.image)
mbligha87116f2006-10-10 02:47:08 +0000347 force_copy('System.map', self.system_map)
348 force_copy('.config', self.config)
349
mbligh6a1d4db2006-10-06 04:30:16 +0000350 if not kernel_config.modules_needed('.config'):
351 return
352
353 system('make modules_install INSTALL_MOD_PATH=%s' % prefix)
354 if prefix == '/':
mbligha87116f2006-10-10 02:47:08 +0000355 self.initrd = self.boot_dir + '/initrd-' + tag
mblighb8e0a112007-11-05 20:27:36 +0000356 self.mkinitrd(self.get_kernel_build_ver(), self.image,
357 self.system_map, self.initrd)
mbligh5925e962007-08-30 17:05:22 +0000358
359
mbligha87116f2006-10-10 02:47:08 +0000360 def add_to_bootloader(self, tag='autotest', args=''):
361 """ add this kernel to bootloader, taking an
362 optional parameter of space separated parameters
363 e.g.: kernel.add_to_bootloader('mykernel', 'ro acpi=off')
364 """
365
366 # remove existing entry if present
367 self.job.bootloader.remove_kernel(tag)
368
apwcbe32572006-11-28 10:00:23 +0000369 # pull the base argument set from the job config,
apwcbe32572006-11-28 10:00:23 +0000370 baseargs = self.job.config_get('boot.default_args')
mbligh78bc05e2006-12-25 02:29:59 +0000371 if baseargs:
372 args = baseargs + " " + args
373
374 # otherwise populate from /proc/cmdline
375 # if not baseargs:
376 # baseargs = open('/proc/cmdline', 'r').readline().strip()
377 # NOTE: This is unnecessary, because boottool does it.
apwcbe32572006-11-28 10:00:23 +0000378
mbligh78bc05e2006-12-25 02:29:59 +0000379 root = None
380 roots = [x for x in args.split() if x.startswith('root=')]
381 if roots:
382 root = re.sub('^root=', '', roots[0])
383 arglist = [x for x in args.split() if not x.startswith('root=')]
384 args = ' '.join(arglist)
mbligha87116f2006-10-10 02:47:08 +0000385
mbligh78bc05e2006-12-25 02:29:59 +0000386 # add the kernel entry
387 # add_kernel(image, title='autotest', initrd='')
388 self.job.bootloader.add_kernel(self.image, tag, self.initrd, \
389 args = args, root = root)
mbligh6a1d4db2006-10-06 04:30:16 +0000390
mbligh201aa892006-10-29 04:02:05 +0000391
mbligh548f29a2006-10-17 04:55:12 +0000392 def get_kernel_build_arch(self, arch=None):
mbligh201aa892006-10-29 04:02:05 +0000393 """
394 Work out the current kernel architecture (as a kernel arch)
395 """
mbligh548f29a2006-10-17 04:55:12 +0000396 if not arch:
397 arch = get_current_kernel_arch()
398 if re.match('i.86', arch):
399 return 'i386'
400 elif re.match('sun4u', arch):
401 return 'sparc64'
402 elif re.match('arm.*', arch):
403 return 'arm'
404 elif re.match('sa110', arch):
405 return 'arm'
406 elif re.match('s390x', arch):
407 return 's390'
408 elif re.match('parisc64', arch):
409 return 'parisc'
410 elif re.match('ppc.*', arch):
411 return 'powerpc'
412 elif re.match('mips.*', arch):
413 return 'mips'
414 else:
415 return arch
416
mbligh6a1d4db2006-10-06 04:30:16 +0000417
mbligh237bed32007-09-05 13:05:57 +0000418 def get_kernel_build_release(self):
419 releasem = re.compile(r'.*UTS_RELEASE\s+"([^"]+)".*');
420 versionm = re.compile(r'.*UTS_VERSION\s+"([^"]+)".*');
421
422 release = None
423 version = None
424
425 for file in [ self.build_dir + "/include/linux/version.h",
426 self.build_dir + "/include/linux/utsrelease.h",
427 self.build_dir + "/include/linux/compile.h" ]:
428 if os.path.exists(file):
429 fd = open(file, 'r')
430 for line in fd.readlines():
431 m = releasem.match(line)
432 if m:
433 release = m.groups()[0]
434 m = versionm.match(line)
435 if m:
436 version = m.groups()[0]
437 fd.close()
438
439 return (release, version)
440
441
442 def get_kernel_build_ident(self):
443 (release, version) = self.get_kernel_build_release()
444
445 if not release or not version:
446 raise JobError('kernel has no identity')
447
448 return release + '::' + version
449
450
apw11985b72007-10-04 15:44:47 +0000451 def boot(self, args='', ident=1):
apw1b5dc362006-10-31 11:24:26 +0000452 """ install and boot this kernel, do not care how
453 just make it happen.
454 """
455
mbligh237bed32007-09-05 13:05:57 +0000456 # If we can check the kernel identity do so.
457 if ident:
458 when = int(time.time())
459 ident = self.get_kernel_build_ident()
460 args += " IDENT=%d" % (when)
461
mblighda0311e2007-10-25 16:03:33 +0000462 # TODO: how do we get the changelist number here?
apwce73d892007-09-25 16:53:05 +0000463 self.job.next_step_prepend(["job.kernel_check_ident",
mblighda0311e2007-10-25 16:03:33 +0000464 when, ident, None, self.subdir])
mbligh237bed32007-09-05 13:05:57 +0000465
apw87c65c12007-09-27 17:19:37 +0000466 # Check if the kernel has been installed, if not install
467 # as the default tag and boot that.
468 if not self.installed_as:
469 self.install()
470
471 # Boot the selected tag.
472 self.add_to_bootloader(args=args, tag=self.installed_as)
apw1b5dc362006-10-31 11:24:26 +0000473
474 # Boot it.
apw11985b72007-10-04 15:44:47 +0000475 self.job.reboot(tag=self.installed_as)
apw1b5dc362006-10-31 11:24:26 +0000476
477
mblighfdbcaec2006-10-01 23:28:57 +0000478 def get_kernel_build_ver(self):
mblighe11f5fc2006-10-04 04:42:22 +0000479 """Check Makefile and .config to return kernel version"""
480 version = patchlevel = sublevel = extraversion = localversion = ''
481
482 for line in open(self.build_dir + '/Makefile', 'r').readlines():
483 if line.startswith('VERSION'):
484 version = line[line.index('=') + 1:].strip()
485 if line.startswith('PATCHLEVEL'):
486 patchlevel = line[line.index('=') + 1:].strip()
487 if line.startswith('SUBLEVEL'):
488 sublevel = line[line.index('=') + 1:].strip()
489 if line.startswith('EXTRAVERSION'):
490 extraversion = line[line.index('=') + 1:].strip()
491
492 for line in open(self.build_dir + '/.config', 'r').readlines():
493 if line.startswith('CONFIG_LOCALVERSION='):
494 localversion = line.rstrip().split('"')[1]
495
mbligh72b88fc2006-12-16 18:41:35 +0000496 return "%s.%s.%s%s%s" %(version, patchlevel, sublevel, extraversion, localversion)
mblighfdbcaec2006-10-01 23:28:57 +0000497
498
mbligh8baa2ea2006-12-17 23:01:24 +0000499 def set_build_target(self, build_target):
500 if build_target:
501 self.build_target = build_target
502 print 'BUILD TARGET: %s' % self.build_target
503
504
mbligh5970cf02006-08-06 15:39:22 +0000505 def set_cross_cc(self, target_arch=None, cross_compile=None,
506 build_target='bzImage'):
mblighc86b0b42006-07-28 17:35:28 +0000507 """Set up to cross-compile.
mblighcc2e6662006-09-14 01:24:07 +0000508 This is broken. We need to work out what the default
509 compile produces, and if not, THEN set the cross
510 compiler.
mblighc86b0b42006-07-28 17:35:28 +0000511 """
mblighcc2e6662006-09-14 01:24:07 +0000512
mbligh5970cf02006-08-06 15:39:22 +0000513 if self.target_arch:
514 return
mbligh678823f2006-12-07 18:49:00 +0000515
mbligh8baa2ea2006-12-17 23:01:24 +0000516 # if someone has set build_target, don't clobber in set_cross_cc
517 # run set_build_target before calling set_cross_cc
518 if not self.build_target:
519 self.set_build_target(build_target)
mbligh72b88fc2006-12-16 18:41:35 +0000520
mbligh5970cf02006-08-06 15:39:22 +0000521 # If no 'target_arch' given assume native compilation
522 if target_arch == None:
mbligh548f29a2006-10-17 04:55:12 +0000523 target_arch = get_current_kernel_arch()
mbligh5970cf02006-08-06 15:39:22 +0000524 if target_arch == 'ppc64':
mbligh5970cf02006-08-06 15:39:22 +0000525 if self.build_target == 'bzImage':
apw9a61c5b2006-11-28 10:03:15 +0000526 self.build_target = 'vmlinux'
mbligh678823f2006-12-07 18:49:00 +0000527
528 if not cross_compile:
529 cross_compile = self.job.config_get('kernel.cross_cc')
530
531 if cross_compile:
532 os.environ['CROSS_COMPILE'] = cross_compile
533 else:
534 if os.environ.has_key('CROSS_COMPILE'):
535 del os.environ['CROSS_COMPILE']
536
mblighcc2e6662006-09-14 01:24:07 +0000537 return # HACK. Crap out for now.
mblighf4c35322006-03-13 01:01:10 +0000538
mblighcc2e6662006-09-14 01:24:07 +0000539 # At this point I know what arch I *want* to build for
540 # but have no way of working out what arch the default
541 # compiler DOES build for.
542
543 # Oh, and BTW, install_package() doesn't exist yet.
mbligh72b88fc2006-12-16 18:41:35 +0000544
mblighcc2e6662006-09-14 01:24:07 +0000545 if target_arch == 'ppc64':
546 install_package('ppc64-cross')
mbligh678823f2006-12-07 18:49:00 +0000547 cross_compile = os.path.join(self.autodir, 'sources/ppc64-cross/bin')
mblighcc2e6662006-09-14 01:24:07 +0000548
549 elif target_arch == 'x86_64':
550 install_package('x86_64-cross')
mbligh678823f2006-12-07 18:49:00 +0000551 cross_compile = os.path.join(self.autodir, 'sources/x86_64-cross/bin')
mblighb8a14e32006-05-06 00:17:35 +0000552
mbligh5970cf02006-08-06 15:39:22 +0000553 os.environ['ARCH'] = self.target_arch = target_arch
554
555 self.cross_compile = cross_compile
556 if self.cross_compile:
557 os.environ['CROSS_COMPILE'] = self.cross_compile
mblighcc2e6662006-09-14 01:24:07 +0000558
mbligh72b88fc2006-12-16 18:41:35 +0000559
mblighb8a14e32006-05-06 00:17:35 +0000560 def pickle_dump(self, filename):
mblighc86b0b42006-07-28 17:35:28 +0000561 """dump a pickle of ourself out to the specified filename
562
563 we can't pickle the backreference to job (it contains fd's),
mbligh709bb9b2006-10-12 04:32:16 +0000564 nor would we want to. Same for logfile (fd's).
mblighc86b0b42006-07-28 17:35:28 +0000565 """
mblighb8a14e32006-05-06 00:17:35 +0000566 temp = copy.copy(self)
567 temp.job = None
mbligh709bb9b2006-10-12 04:32:16 +0000568 temp.logfile = None
mblighb8a14e32006-05-06 00:17:35 +0000569 pickle.dump(temp, open(filename, 'w'))
mbligh736adc92007-10-18 03:23:22 +0000570
571
572class rpm_kernel:
573 """ Class for installing rpm kernel package
574 """
575
mblighda0311e2007-10-25 16:03:33 +0000576 def __init__(self, job, rpm_package, subdir):
mbligh736adc92007-10-18 03:23:22 +0000577 self.job = job
mbligh6ee7ee02007-11-13 23:49:05 +0000578 self.rpm_package = rpm_package
mblighda0311e2007-10-25 16:03:33 +0000579 self.log_dir = os.path.join(subdir, 'debug')
580 self.subdir = os.path.basename(subdir)
mbligh736adc92007-10-18 03:23:22 +0000581 if os.path.exists(self.log_dir):
582 system('rm -rf ' + self.log_dir)
583 os.mkdir(self.log_dir)
mblighda0311e2007-10-25 16:03:33 +0000584 self.installed_as = None
585 cl_re = re.compile(r'[-.](\d{7,})\.rpm$')
586 match = cl_re.findall(rpm_package)
587 if match:
588 self.changelist = match[0]
589 else:
590 self.changelist = None
mbligh736adc92007-10-18 03:23:22 +0000591
592
mbligh119c12a2007-11-12 22:13:44 +0000593 @logging.record
apw001e40a2007-11-21 19:24:37 +0000594 @tee_output_logdir_mark
mblighda0311e2007-10-25 16:03:33 +0000595 def install(self, tag='autotest'):
596 self.installed_as = tag
597
mblighda0311e2007-10-25 16:03:33 +0000598 self.rpm_name = system_output('rpm -qp ' + self.rpm_package)
mbligh736adc92007-10-18 03:23:22 +0000599
600 # install
601 system('rpm -i --force ' + self.rpm_package)
602
603 # get file list
mblighda0311e2007-10-25 16:03:33 +0000604 files = system_output('rpm -ql ' + self.rpm_name).splitlines()
mbligh736adc92007-10-18 03:23:22 +0000605
mbligh736adc92007-10-18 03:23:22 +0000606 # search for vmlinuz
607 for file in files:
608 if file.startswith('/boot/vmlinuz'):
609 self.image = file
610 break
611 else:
612 raise TestError(self.rpm_package + " doesn't contain /boot/vmlinuz")
mblighda0311e2007-10-25 16:03:33 +0000613
mbligh736adc92007-10-18 03:23:22 +0000614 # search for initrd
615 self.initrd = ''
616 for file in files:
617 if file.startswith('/boot/initrd'):
618 self.initrd = file
619 break
620
621 # get version and release number
622 self.version, self.release = system_output(
mblighda0311e2007-10-25 16:03:33 +0000623 'rpm --queryformat="%{VERSION}\\n%{RELEASE}\\n" -q ' + self.rpm_name).splitlines()[0:2]
mbligh736adc92007-10-18 03:23:22 +0000624
625
626 def add_to_bootloader(self, tag='autotest', args=''):
627 """ Add this kernel to bootloader
628 """
629
630 # remove existing entry if present
631 self.job.bootloader.remove_kernel(tag)
632
633 # pull the base argument set from the job config
634 baseargs = self.job.config_get('boot.default_args')
635 if baseargs:
636 args = baseargs + ' ' + args
637
638 # otherwise populate from /proc/cmdline
639 # if not baseargs:
640 # baseargs = open('/proc/cmdline', 'r').readline().strip()
641 # NOTE: This is unnecessary, because boottool does it.
642
643 root = None
644 roots = [x for x in args.split() if x.startswith('root=')]
645 if roots:
646 root = re.sub('^root=', '', roots[0])
647 arglist = [x for x in args.split() if not x.startswith('root=')]
648 args = ' '.join(arglist)
649
650 # add the kernel entry
651 self.job.bootloader.add_kernel(self.image, tag, self.initrd, args = args, root = root)
mbligh10a24a72007-10-24 21:02:53 +0000652
653
mblighda0311e2007-10-25 16:03:33 +0000654 def boot(self, args='', ident=1):
655 """ install and boot this kernel
656 """
mbligh73e82a32007-11-08 21:35:29 +0000657
mbligh10a24a72007-10-24 21:02:53 +0000658 # Check if the kernel has been installed, if not install
659 # as the default tag and boot that.
mblighda0311e2007-10-25 16:03:33 +0000660 if not self.installed_as:
mbligh73e82a32007-11-08 21:35:29 +0000661 self.install()
mblighda0311e2007-10-25 16:03:33 +0000662
663 # If we can check the kernel identity do so.
664 if ident:
665 when = int(time.time())
666 ident = '-'.join([self.version, self.rpm_name.split('-')[1], self.release])
667 args += " IDENT=%d" % (when)
668
669 self.job.next_step_prepend(["job.kernel_check_ident",
670 when, ident, self.changelist, self.subdir, 'rpm'])
mbligh10a24a72007-10-24 21:02:53 +0000671
672 # Boot the selected tag.
mblighda0311e2007-10-25 16:03:33 +0000673 self.add_to_bootloader(args=args, tag=self.installed_as)
mbligh10a24a72007-10-24 21:02:53 +0000674
675 # Boot it.
mblighda0311e2007-10-25 16:03:33 +0000676 self.job.reboot(tag=self.installed_as)
mbligh6ee7ee02007-11-13 23:49:05 +0000677
678
679# pull in some optional site-specific path pre-processing
680try:
681 import site_kernel
682 preprocess_path = site_kernel.preprocess_path
683 del site_kernel
684except ImportError:
685 # just make the preprocessor a nop
686 def preprocess_path(path):
687 return path
688
689def auto_kernel(job, path, subdir, tmp_dir, build_dir, leave=False):
690 """\
691 Create a kernel object, dynamically selecting the appropriate class to use
692 based on the path provided.
693 """
694 path = preprocess_path(path)
695 if path.endswith('.rpm'):
696 return rpm_kernel(job, path, subdir)
697 else:
698 return kernel(job, path, subdir, tmp_dir, build_dir, leave)