blob: f7b1528bd249d2b53c6ea6af0e53d4208566e0be [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
mbligh10038012006-10-19 03:32:45 +00006import kernel_config, test, os_dep
mblighf4c35322006-03-13 01:01:10 +00007
mblighb8e0a112007-11-05 20:27:36 +00008
mblighf4c35322006-03-13 01:01:10 +00009class kernel:
mblighc86b0b42006-07-28 17:35:28 +000010 """ Class for compiling kernels.
11
12 Data for the object includes the src files
13 used to create the kernel, patches applied, config (base + changes),
14 the build directory itself, and logged output
15
16 Properties:
17 job
18 Backpointer to the job object we're part of
19 autodir
20 Path to the top level autotest dir (/usr/local/autotest)
mblighc86b0b42006-07-28 17:35:28 +000021 src_dir
mbligh1e8858e2006-11-24 22:18:35 +000022 <tmp_dir>/src/
mblighc49af7b2006-11-24 04:02:21 +000023 build_dir
mbligh1e8858e2006-11-24 22:18:35 +000024 <tmp_dir>/linux/
mblighc86b0b42006-07-28 17:35:28 +000025 config_dir
mbligh1e8858e2006-11-24 22:18:35 +000026 <results_dir>/config/
mblighc86b0b42006-07-28 17:35:28 +000027 log_dir
mbligh1e8858e2006-11-24 22:18:35 +000028 <results_dir>/debug/
29 results_dir
30 <results_dir>/results/
mblighc86b0b42006-07-28 17:35:28 +000031 """
32
mbligh8baa2ea2006-12-17 23:01:24 +000033 autodir = ''
34
mbligh09f288a2007-09-18 21:34:57 +000035 def __init__(self, job, base_tree, subdir, tmp_dir, build_dir, leave = False):
mblighc86b0b42006-07-28 17:35:28 +000036 """Initialize the kernel build environment
37
38 job
39 which job this build is part of
mblighc86b0b42006-07-28 17:35:28 +000040 base_tree
mbligh534015f2006-09-15 03:28:56 +000041 base kernel tree. Can be one of the following:
42 1. A local tarball
43 2. A URL to a tarball
44 3. A local directory (will symlink it)
45 4. A shorthand expandable (eg '2.6.11-git3')
mbligh09f288a2007-09-18 21:34:57 +000046 subdir
47 subdir in the results directory (eg "build")
48 (holds config/, debug/, results/)
mbligh1e8858e2006-11-24 22:18:35 +000049 tmp_dir
mbligh72b88fc2006-12-16 18:41:35 +000050
mbligh1e8858e2006-11-24 22:18:35 +000051 leave
52 Boolean, whether to leave existing tmpdir or not
mblighc86b0b42006-07-28 17:35:28 +000053 """
mblighf4c35322006-03-13 01:01:10 +000054 self.job = job
mbligh678823f2006-12-07 18:49:00 +000055 self.autodir = job.autodir
mblighf4c35322006-03-13 01:01:10 +000056
mbligh1e8858e2006-11-24 22:18:35 +000057 self.src_dir = os.path.join(tmp_dir, 'src')
mbligh8baa2ea2006-12-17 23:01:24 +000058 self.build_dir = os.path.join(tmp_dir, build_dir)
mbligha2508052006-05-28 21:29:53 +000059 # created by get_kernel_tree
mbligh09f288a2007-09-18 21:34:57 +000060 self.config_dir = os.path.join(subdir, 'config')
61 self.log_dir = os.path.join(subdir, 'debug')
62 self.results_dir = os.path.join(subdir, 'results')
63 self.subdir = os.path.basename(subdir)
mbligh1e8858e2006-11-24 22:18:35 +000064
apw87c65c12007-09-27 17:19:37 +000065 self.installed_as = None
66
mbligh1e8858e2006-11-24 22:18:35 +000067 if not leave:
68 if os.path.isdir(self.src_dir):
69 system('rm -rf ' + self.src_dir)
70 if os.path.isdir(self.build_dir):
71 system('rm -rf ' + self.build_dir)
72
mbligh30f28c52007-10-11 18:35:35 +000073 if not os.path.exists(self.src_dir):
74 os.mkdir(self.src_dir)
mbligh1e8858e2006-11-24 22:18:35 +000075 for path in [self.config_dir, self.log_dir, self.results_dir]:
76 if os.path.exists(path):
77 system('rm -rf ' + path)
78 os.mkdir(path)
mblighf4c35322006-03-13 01:01:10 +000079
mbligh4426de02006-10-10 07:18:28 +000080 logpath = os.path.join(self.log_dir, 'build_log')
81 self.logfile = open(logpath, 'w+')
82
mbligh72b88fc2006-12-16 18:41:35 +000083 self.target_arch = None
mblighfdbcaec2006-10-01 23:28:57 +000084 self.build_target = 'bzImage'
mbligh8baa2ea2006-12-17 23:01:24 +000085 self.build_image = None
mblighfdbcaec2006-10-01 23:28:57 +000086
mblighcac347a2007-06-02 17:21:48 +000087 if get_current_kernel_arch() == 'ia64':
88 self.build_target = 'all'
89 self.build_image = 'vmlinux.gz'
90
mblighfdbcaec2006-10-01 23:28:57 +000091 if leave:
92 return
mbligh534015f2006-09-15 03:28:56 +000093
mbligh4426de02006-10-10 07:18:28 +000094 self.logfile.write('BASE: %s\n' % base_tree)
apw2366d992007-03-12 20:35:57 +000095
96 # Where we have direct version hint record that
97 # for later configuration selection.
98 shorthand = re.compile(r'^\d+\.\d+\.\d+')
99 if shorthand.match(base_tree):
100 self.base_tree_version = base_tree
101 else:
102 self.base_tree_version = None
103
mbligh534015f2006-09-15 03:28:56 +0000104 if os.path.exists(base_tree):
105 self.get_kernel_tree(base_tree)
106 else:
apw8ad56be2006-11-06 17:49:54 +0000107 args = self.job.config_get('mirror.ftp_kernel_org')
mbligh548f29a2006-10-17 04:55:12 +0000108 if args:
109 args = '-l ' + args
mbligh72b88fc2006-12-16 18:41:35 +0000110 base_components = kernelexpand(base_tree, args)
mbligh534015f2006-09-15 03:28:56 +0000111 print 'kernelexpand: '
112 print base_components
113 self.get_kernel_tree(base_components.pop(0))
114 if base_components: # apply remaining patches
115 self.patch(*base_components)
mblighf4c35322006-03-13 01:01:10 +0000116
117
mbligh119c12a2007-11-12 22:13:44 +0000118 @logging.record
mblighb8e0a112007-11-05 20:27:36 +0000119 def patch(self, *patches):
mblighc86b0b42006-07-28 17:35:28 +0000120 """Apply a list of patches (in order)"""
mbligh1e8858e2006-11-24 22:18:35 +0000121 if not patches:
122 return
mbligh534015f2006-09-15 03:28:56 +0000123 print 'Applying patches: ', patches
mbligh0763e732007-08-30 16:35:06 +0000124 self.apply_patches(self.get_patches(patches))
mblighf4c35322006-03-13 01:01:10 +0000125
126
mbligh119c12a2007-11-12 22:13:44 +0000127 @logging.record
mblighb8e0a112007-11-05 20:27:36 +0000128 def config(self, config_file = '', config_list = None, defconfig = False):
mbligh44da9372007-11-05 23:30:07 +0000129 self.job.stdout.tee_redirect(os.path.join(self.log_dir, 'stdout'))
mbligh678823f2006-12-07 18:49:00 +0000130 self.set_cross_cc()
apwb449c7d2006-12-05 12:21:44 +0000131 config = kernel_config.kernel_config(self.job, self.build_dir,
apw2366d992007-03-12 20:35:57 +0000132 self.config_dir, config_file, config_list,
133 defconfig, self.base_tree_version)
mblighf4c35322006-03-13 01:01:10 +0000134 self.job.stdout.restore()
135
136
137 def get_patches(self, patches):
mbligh1e8858e2006-11-24 22:18:35 +0000138 """fetch the patches to the local src_dir"""
mblighf4c35322006-03-13 01:01:10 +0000139 local_patches = []
140 for patch in patches:
mbligh1e8858e2006-11-24 22:18:35 +0000141 dest = os.path.join(self.src_dir, basename(patch))
mbligh0763e732007-08-30 16:35:06 +0000142 # FIXME: this isn't unique. Append something to it
143 # like wget does if it's not there?
mbligh1e8858e2006-11-24 22:18:35 +0000144 print "get_file %s %s %s %s" % (patch, dest, self.src_dir, basename(patch))
mblighf4c35322006-03-13 01:01:10 +0000145 get_file(patch, dest)
mbligh0763e732007-08-30 16:35:06 +0000146 # probably safer to use the command, not python library
147 md5sum = system_output('md5sum ' + dest).split()[0]
148 local_patches.append((patch, dest, md5sum))
mbligh534015f2006-09-15 03:28:56 +0000149 return local_patches
mblighf4c35322006-03-13 01:01:10 +0000150
mbligh72b88fc2006-12-16 18:41:35 +0000151
mbligh534015f2006-09-15 03:28:56 +0000152 def apply_patches(self, local_patches):
mblighc86b0b42006-07-28 17:35:28 +0000153 """apply the list of patches, in order"""
mblighf4c35322006-03-13 01:01:10 +0000154 builddir = self.build_dir
155 os.chdir(builddir)
156
mbligh534015f2006-09-15 03:28:56 +0000157 if not local_patches:
mblighf4c35322006-03-13 01:01:10 +0000158 return None
mbligh0763e732007-08-30 16:35:06 +0000159 for (spec, local, md5sum) in local_patches:
160 if local.endswith('.bz2') or local.endswith('.gz'):
161 ref = spec
162 else:
163 ref = force_copy(local, self.results_dir)
164 ref = self.job.relative_path(ref)
165 log = 'PATCH: %s %s %s\n' % (spec, ref, md5sum)
166 print log
167 cat_file_to_cmd(local, 'patch -p1 > /dev/null')
168 self.logfile.write(log)
mbligh72b88fc2006-12-16 18:41:35 +0000169
170
171 def get_kernel_tree(self, base_tree):
mblighc49af7b2006-11-24 04:02:21 +0000172 """Extract/link base_tree to self.build_dir"""
mbligh5970cf02006-08-06 15:39:22 +0000173
174 # if base_tree is a dir, assume uncompressed kernel
175 if os.path.isdir(base_tree):
176 print 'Symlinking existing kernel source'
mblighc49af7b2006-11-24 04:02:21 +0000177 os.symlink(base_tree, self.build_dir)
mblighf4c35322006-03-13 01:01:10 +0000178
mbligh5970cf02006-08-06 15:39:22 +0000179 # otherwise, extract tarball
180 else:
mblighc49af7b2006-11-24 04:02:21 +0000181 os.chdir(os.path.dirname(self.src_dir))
182 # Figure out local destination for tarball
183 tarball = os.path.join(self.src_dir, os.path.basename(base_tree))
mbligh5970cf02006-08-06 15:39:22 +0000184 get_file(base_tree, tarball)
mbligh5970cf02006-08-06 15:39:22 +0000185 print 'Extracting kernel tarball:', tarball, '...'
mblighc49af7b2006-11-24 04:02:21 +0000186 extract_tarball_to_dir(tarball, self.build_dir)
mbligh5970cf02006-08-06 15:39:22 +0000187
mblighf4c35322006-03-13 01:01:10 +0000188
mblighfdbcaec2006-10-01 23:28:57 +0000189 def extraversion(self, tag, append=1):
190 os.chdir(self.build_dir)
mbligh4426de02006-10-10 07:18:28 +0000191 extraversion_sub = r's/^EXTRAVERSION =\s*\(.*\)/EXTRAVERSION = '
mblighfdbcaec2006-10-01 23:28:57 +0000192 if append:
mbligh4426de02006-10-10 07:18:28 +0000193 p = extraversion_sub + '\\1-%s/' % tag
mblighfdbcaec2006-10-01 23:28:57 +0000194 else:
mbligh4426de02006-10-10 07:18:28 +0000195 p = extraversion_sub + '-%s/' % tag
mbligh4c3fe4a2007-07-31 17:59:21 +0000196 system('mv Makefile Makefile.old')
197 system('sed "%s" < Makefile.old > Makefile' % p)
mblighfdbcaec2006-10-01 23:28:57 +0000198
199
mbligh119c12a2007-11-12 22:13:44 +0000200 @logging.record
mblighb8e0a112007-11-05 20:27:36 +0000201 def build(self, make_opts = '', logfile = '', extraversion='autotest'):
mblighc86b0b42006-07-28 17:35:28 +0000202 """build the kernel
mbligh72b88fc2006-12-16 18:41:35 +0000203
mblighc86b0b42006-07-28 17:35:28 +0000204 make_opts
205 additional options to make, if any
206 """
mbligh10038012006-10-19 03:32:45 +0000207 os_dep.commands('gcc', 'make')
mbligh6d4c9412006-09-13 23:08:44 +0000208 if logfile == '':
209 logfile = os.path.join(self.log_dir, 'kernel_build')
mblighf4c35322006-03-13 01:01:10 +0000210 os.chdir(self.build_dir)
mblighfdbcaec2006-10-01 23:28:57 +0000211 if extraversion:
212 self.extraversion(extraversion)
mbligh93057012006-08-06 15:51:56 +0000213 print os.path.join(self.log_dir, 'stdout')
mbligh44da9372007-11-05 23:30:07 +0000214 self.job.stdout.tee_redirect(logfile + '.stdout')
215 self.job.stderr.tee_redirect(logfile + '.stderr')
mbligha2508052006-05-28 21:29:53 +0000216 self.set_cross_cc()
mblighf4c35322006-03-13 01:01:10 +0000217 # setup_config_file(config_file, config_overrides)
apwc7846102006-04-06 18:22:13 +0000218
219 # Not needed on 2.6, but hard to tell -- handle failure
mblighfdbcaec2006-10-01 23:28:57 +0000220 system('make dep', ignorestatus=1)
mblighf4c35322006-03-13 01:01:10 +0000221 threads = 2 * count_cpus()
mbligh6d4c9412006-09-13 23:08:44 +0000222 build_string = 'make -j %d %s %s' % (threads, make_opts,
223 self.build_target)
224 # eg make bzImage, or make zImage
225 print build_string
226 system(build_string)
mblighf4c35322006-03-13 01:01:10 +0000227 if kernel_config.modules_needed('.config'):
apwd4701972006-10-16 09:24:56 +0000228 system('make -j %d modules' % (threads))
apwc7846102006-04-06 18:22:13 +0000229
mblighf4c35322006-03-13 01:01:10 +0000230 self.job.stdout.restore()
231 self.job.stderr.restore()
mbligh72b88fc2006-12-16 18:41:35 +0000232
mbligh4426de02006-10-10 07:18:28 +0000233 kernel_version = self.get_kernel_build_ver()
234 kernel_version = re.sub('-autotest', '', kernel_version)
235 self.logfile.write('BUILD VERSION: %s\n' % kernel_version)
mbligh1e8858e2006-11-24 22:18:35 +0000236
237 force_copy(self.build_dir+'/System.map', self.results_dir)
mblighf4c35322006-03-13 01:01:10 +0000238
239
mbligh30f28c52007-10-11 18:35:35 +0000240 def build_timed(self, threads, timefile = '/dev/null', make_opts = '',
mbligh2e793a42007-10-12 23:58:35 +0000241 output = '/dev/null'):
mblighc86b0b42006-07-28 17:35:28 +0000242 """time the bulding of the kernel"""
mblighb8a14e32006-05-06 00:17:35 +0000243 os.chdir(self.build_dir)
mbligh678823f2006-12-07 18:49:00 +0000244 self.set_cross_cc()
mbligh30f28c52007-10-11 18:35:35 +0000245
mblighd79b2b42007-11-05 20:38:15 +0000246 self.clean(logged=False)
mbligh30f28c52007-10-11 18:35:35 +0000247 build_string = "/usr/bin/time -o %s make %s -j %s vmlinux" \
248 % (timefile, make_opts, threads)
mbligh2e793a42007-10-12 23:58:35 +0000249 build_string += ' > %s 2>&1' % output
mblighf4c35322006-03-13 01:01:10 +0000250 print build_string
apwc7846102006-04-06 18:22:13 +0000251 system(build_string)
mbligh30f28c52007-10-11 18:35:35 +0000252
apwc7846102006-04-06 18:22:13 +0000253 if (not os.path.isfile('vmlinux')):
254 raise TestError("no vmlinux found, kernel build failed")
mblighb8a14e32006-05-06 00:17:35 +0000255
256
mbligh119c12a2007-11-12 22:13:44 +0000257 @logging.record
mblighb8e0a112007-11-05 20:27:36 +0000258 def clean(self):
mblighc86b0b42006-07-28 17:35:28 +0000259 """make clean in the kernel tree"""
mblighb8a14e32006-05-06 00:17:35 +0000260 os.chdir(self.build_dir)
mblighf4c35322006-03-13 01:01:10 +0000261 print "make clean"
mbligh30f28c52007-10-11 18:35:35 +0000262 system('make clean > /dev/null 2> /dev/null')
mblighf4c35322006-03-13 01:01:10 +0000263
mbligh50f42ea2006-09-30 22:22:21 +0000264
mbligh119c12a2007-11-12 22:13:44 +0000265 @logging.record
mblighb8e0a112007-11-05 20:27:36 +0000266 def mkinitrd(self, version, image, system_map, initrd):
mbligh50f42ea2006-09-30 22:22:21 +0000267 """Build kernel initrd image.
268 Try to use distro specific way to build initrd image.
269 Parameters:
270 version
271 new kernel version
272 image
273 new kernel image file
274 system_map
275 System.map file
276 initrd
277 initrd image file to build
278 """
279 vendor = get_os_vendor()
280
281 if os.path.isfile(initrd):
mblighfdbcaec2006-10-01 23:28:57 +0000282 print "Existing %s file, will remove it." % initrd
mbligh50f42ea2006-09-30 22:22:21 +0000283 os.remove(initrd)
mbligh72b88fc2006-12-16 18:41:35 +0000284
apwe43a30b2007-09-25 16:51:30 +0000285 args = self.job.config_get('kernel.mkinitrd_extra_args')
286
mbligh3d515d42007-11-09 17:00:36 +0000287 # don't leak 'None' into mkinitrd command
288 if not args:
289 args = ''
290
mbligh50f42ea2006-09-30 22:22:21 +0000291 if vendor in ['Red Hat', 'Fedora Core']:
apwe43a30b2007-09-25 16:51:30 +0000292 system('mkinitrd %s %s %s' % (args, initrd, version))
mbligh50f42ea2006-09-30 22:22:21 +0000293 elif vendor in ['SUSE']:
apwe43a30b2007-09-25 16:51:30 +0000294 system('mkinitrd %s -k %s -i %s -M %s' % (args, image, initrd, system_map))
apwc898a1f2007-02-28 15:27:22 +0000295 elif vendor in ['Debian', 'Ubuntu']:
296 if os.path.isfile('/usr/sbin/mkinitrd'):
297 cmd = '/usr/sbin/mkinitrd'
298 elif os.path.isfile('/usr/sbin/mkinitramfs'):
299 cmd = '/usr/sbin/mkinitramfs'
300 else:
301 raise TestError('No Debian initrd builder')
apwe43a30b2007-09-25 16:51:30 +0000302 system('%s %s -o %s %s' % (cmd, args, initrd, version))
mbligh50f42ea2006-09-30 22:22:21 +0000303 else:
mblighfdbcaec2006-10-01 23:28:57 +0000304 raise TestError('Unsupported vendor %s' % vendor)
mbligh50f42ea2006-09-30 22:22:21 +0000305
306
mbligh8baa2ea2006-12-17 23:01:24 +0000307 def set_build_image(self, image):
308 self.build_image = image
309
310
mbligh119c12a2007-11-12 22:13:44 +0000311 @logging.record
mblighb8e0a112007-11-05 20:27:36 +0000312 def install(self, tag='autotest', prefix = '/'):
mblighc86b0b42006-07-28 17:35:28 +0000313 """make install in the kernel tree"""
apw87c65c12007-09-27 17:19:37 +0000314
315 # Record that we have installed the kernel, and
316 # the tag under which we installed it.
317 self.installed_as = tag
318
mblighf4c35322006-03-13 01:01:10 +0000319 os.chdir(self.build_dir)
mbligh72b88fc2006-12-16 18:41:35 +0000320
mbligh6a1d4db2006-10-06 04:30:16 +0000321 if not os.path.isdir(prefix):
322 os.mkdir(prefix)
mbligha87116f2006-10-10 02:47:08 +0000323 self.boot_dir = os.path.join(prefix, 'boot')
324 if not os.path.isdir(self.boot_dir):
325 os.mkdir(self.boot_dir)
mbligh0ad65582006-10-06 04:16:36 +0000326
mbligh8baa2ea2006-12-17 23:01:24 +0000327 if not self.build_image:
328 images = glob.glob('arch/*/boot/' + self.build_target)
329 if len(images):
330 self.build_image = images[0]
331 else:
332 self.build_image = self.build_target
mbligha87116f2006-10-10 02:47:08 +0000333
334 # remember installed files
mbligha87116f2006-10-10 02:47:08 +0000335 self.vmlinux = self.boot_dir + '/vmlinux-' + tag
mbligh8baa2ea2006-12-17 23:01:24 +0000336 if (self.build_image != 'vmlinux'):
apw9a61c5b2006-11-28 10:03:15 +0000337 self.image = self.boot_dir + '/vmlinuz-' + tag
338 else:
339 self.image = self.vmlinux
mbligha87116f2006-10-10 02:47:08 +0000340 self.system_map = self.boot_dir + '/System.map-' + tag
341 self.config = self.boot_dir + '/config-' + tag
342 self.initrd = ''
343
344 # copy to boot dir
mbligha87116f2006-10-10 02:47:08 +0000345 force_copy('vmlinux', self.vmlinux)
mbligh8baa2ea2006-12-17 23:01:24 +0000346 if (self.build_image != 'vmlinux'):
347 force_copy(self.build_image, self.image)
mbligha87116f2006-10-10 02:47:08 +0000348 force_copy('System.map', self.system_map)
349 force_copy('.config', self.config)
350
mbligh6a1d4db2006-10-06 04:30:16 +0000351 if not kernel_config.modules_needed('.config'):
352 return
353
354 system('make modules_install INSTALL_MOD_PATH=%s' % prefix)
355 if prefix == '/':
mbligha87116f2006-10-10 02:47:08 +0000356 self.initrd = self.boot_dir + '/initrd-' + tag
mblighb8e0a112007-11-05 20:27:36 +0000357 self.mkinitrd(self.get_kernel_build_ver(), self.image,
358 self.system_map, self.initrd)
mbligh5925e962007-08-30 17:05:22 +0000359
360
mbligha87116f2006-10-10 02:47:08 +0000361 def add_to_bootloader(self, tag='autotest', args=''):
362 """ add this kernel to bootloader, taking an
363 optional parameter of space separated parameters
364 e.g.: kernel.add_to_bootloader('mykernel', 'ro acpi=off')
365 """
366
367 # remove existing entry if present
368 self.job.bootloader.remove_kernel(tag)
369
apwcbe32572006-11-28 10:00:23 +0000370 # pull the base argument set from the job config,
apwcbe32572006-11-28 10:00:23 +0000371 baseargs = self.job.config_get('boot.default_args')
mbligh78bc05e2006-12-25 02:29:59 +0000372 if baseargs:
373 args = baseargs + " " + args
374
375 # otherwise populate from /proc/cmdline
376 # if not baseargs:
377 # baseargs = open('/proc/cmdline', 'r').readline().strip()
378 # NOTE: This is unnecessary, because boottool does it.
apwcbe32572006-11-28 10:00:23 +0000379
mbligh78bc05e2006-12-25 02:29:59 +0000380 root = None
381 roots = [x for x in args.split() if x.startswith('root=')]
382 if roots:
383 root = re.sub('^root=', '', roots[0])
384 arglist = [x for x in args.split() if not x.startswith('root=')]
385 args = ' '.join(arglist)
mbligha87116f2006-10-10 02:47:08 +0000386
mbligh78bc05e2006-12-25 02:29:59 +0000387 # add the kernel entry
388 # add_kernel(image, title='autotest', initrd='')
389 self.job.bootloader.add_kernel(self.image, tag, self.initrd, \
390 args = args, root = root)
mbligh6a1d4db2006-10-06 04:30:16 +0000391
mbligh201aa892006-10-29 04:02:05 +0000392
mbligh548f29a2006-10-17 04:55:12 +0000393 def get_kernel_build_arch(self, arch=None):
mbligh201aa892006-10-29 04:02:05 +0000394 """
395 Work out the current kernel architecture (as a kernel arch)
396 """
mbligh548f29a2006-10-17 04:55:12 +0000397 if not arch:
398 arch = get_current_kernel_arch()
399 if re.match('i.86', arch):
400 return 'i386'
401 elif re.match('sun4u', arch):
402 return 'sparc64'
403 elif re.match('arm.*', arch):
404 return 'arm'
405 elif re.match('sa110', arch):
406 return 'arm'
407 elif re.match('s390x', arch):
408 return 's390'
409 elif re.match('parisc64', arch):
410 return 'parisc'
411 elif re.match('ppc.*', arch):
412 return 'powerpc'
413 elif re.match('mips.*', arch):
414 return 'mips'
415 else:
416 return arch
417
mbligh6a1d4db2006-10-06 04:30:16 +0000418
mbligh237bed32007-09-05 13:05:57 +0000419 def get_kernel_build_release(self):
420 releasem = re.compile(r'.*UTS_RELEASE\s+"([^"]+)".*');
421 versionm = re.compile(r'.*UTS_VERSION\s+"([^"]+)".*');
422
423 release = None
424 version = None
425
426 for file in [ self.build_dir + "/include/linux/version.h",
427 self.build_dir + "/include/linux/utsrelease.h",
428 self.build_dir + "/include/linux/compile.h" ]:
429 if os.path.exists(file):
430 fd = open(file, 'r')
431 for line in fd.readlines():
432 m = releasem.match(line)
433 if m:
434 release = m.groups()[0]
435 m = versionm.match(line)
436 if m:
437 version = m.groups()[0]
438 fd.close()
439
440 return (release, version)
441
442
443 def get_kernel_build_ident(self):
444 (release, version) = self.get_kernel_build_release()
445
446 if not release or not version:
447 raise JobError('kernel has no identity')
448
449 return release + '::' + version
450
451
apw11985b72007-10-04 15:44:47 +0000452 def boot(self, args='', ident=1):
apw1b5dc362006-10-31 11:24:26 +0000453 """ install and boot this kernel, do not care how
454 just make it happen.
455 """
456
mbligh237bed32007-09-05 13:05:57 +0000457 # If we can check the kernel identity do so.
458 if ident:
459 when = int(time.time())
460 ident = self.get_kernel_build_ident()
461 args += " IDENT=%d" % (when)
462
mblighda0311e2007-10-25 16:03:33 +0000463 # TODO: how do we get the changelist number here?
apwce73d892007-09-25 16:53:05 +0000464 self.job.next_step_prepend(["job.kernel_check_ident",
mblighda0311e2007-10-25 16:03:33 +0000465 when, ident, None, self.subdir])
mbligh237bed32007-09-05 13:05:57 +0000466
apw87c65c12007-09-27 17:19:37 +0000467 # Check if the kernel has been installed, if not install
468 # as the default tag and boot that.
469 if not self.installed_as:
470 self.install()
471
472 # Boot the selected tag.
473 self.add_to_bootloader(args=args, tag=self.installed_as)
apw1b5dc362006-10-31 11:24:26 +0000474
475 # Boot it.
apw11985b72007-10-04 15:44:47 +0000476 self.job.reboot(tag=self.installed_as)
apw1b5dc362006-10-31 11:24:26 +0000477
478
mblighfdbcaec2006-10-01 23:28:57 +0000479 def get_kernel_build_ver(self):
mblighe11f5fc2006-10-04 04:42:22 +0000480 """Check Makefile and .config to return kernel version"""
481 version = patchlevel = sublevel = extraversion = localversion = ''
482
483 for line in open(self.build_dir + '/Makefile', 'r').readlines():
484 if line.startswith('VERSION'):
485 version = line[line.index('=') + 1:].strip()
486 if line.startswith('PATCHLEVEL'):
487 patchlevel = line[line.index('=') + 1:].strip()
488 if line.startswith('SUBLEVEL'):
489 sublevel = line[line.index('=') + 1:].strip()
490 if line.startswith('EXTRAVERSION'):
491 extraversion = line[line.index('=') + 1:].strip()
492
493 for line in open(self.build_dir + '/.config', 'r').readlines():
494 if line.startswith('CONFIG_LOCALVERSION='):
495 localversion = line.rstrip().split('"')[1]
496
mbligh72b88fc2006-12-16 18:41:35 +0000497 return "%s.%s.%s%s%s" %(version, patchlevel, sublevel, extraversion, localversion)
mblighfdbcaec2006-10-01 23:28:57 +0000498
499
mbligh8baa2ea2006-12-17 23:01:24 +0000500 def set_build_target(self, build_target):
501 if build_target:
502 self.build_target = build_target
503 print 'BUILD TARGET: %s' % self.build_target
504
505
mbligh5970cf02006-08-06 15:39:22 +0000506 def set_cross_cc(self, target_arch=None, cross_compile=None,
507 build_target='bzImage'):
mblighc86b0b42006-07-28 17:35:28 +0000508 """Set up to cross-compile.
mblighcc2e6662006-09-14 01:24:07 +0000509 This is broken. We need to work out what the default
510 compile produces, and if not, THEN set the cross
511 compiler.
mblighc86b0b42006-07-28 17:35:28 +0000512 """
mblighcc2e6662006-09-14 01:24:07 +0000513
mbligh5970cf02006-08-06 15:39:22 +0000514 if self.target_arch:
515 return
mbligh678823f2006-12-07 18:49:00 +0000516
mbligh8baa2ea2006-12-17 23:01:24 +0000517 # if someone has set build_target, don't clobber in set_cross_cc
518 # run set_build_target before calling set_cross_cc
519 if not self.build_target:
520 self.set_build_target(build_target)
mbligh72b88fc2006-12-16 18:41:35 +0000521
mbligh5970cf02006-08-06 15:39:22 +0000522 # If no 'target_arch' given assume native compilation
523 if target_arch == None:
mbligh548f29a2006-10-17 04:55:12 +0000524 target_arch = get_current_kernel_arch()
mbligh5970cf02006-08-06 15:39:22 +0000525 if target_arch == 'ppc64':
mbligh5970cf02006-08-06 15:39:22 +0000526 if self.build_target == 'bzImage':
apw9a61c5b2006-11-28 10:03:15 +0000527 self.build_target = 'vmlinux'
mbligh678823f2006-12-07 18:49:00 +0000528
529 if not cross_compile:
530 cross_compile = self.job.config_get('kernel.cross_cc')
531
532 if cross_compile:
533 os.environ['CROSS_COMPILE'] = cross_compile
534 else:
535 if os.environ.has_key('CROSS_COMPILE'):
536 del os.environ['CROSS_COMPILE']
537
mblighcc2e6662006-09-14 01:24:07 +0000538 return # HACK. Crap out for now.
mblighf4c35322006-03-13 01:01:10 +0000539
mblighcc2e6662006-09-14 01:24:07 +0000540 # At this point I know what arch I *want* to build for
541 # but have no way of working out what arch the default
542 # compiler DOES build for.
543
544 # Oh, and BTW, install_package() doesn't exist yet.
mbligh72b88fc2006-12-16 18:41:35 +0000545
mblighcc2e6662006-09-14 01:24:07 +0000546 if target_arch == 'ppc64':
547 install_package('ppc64-cross')
mbligh678823f2006-12-07 18:49:00 +0000548 cross_compile = os.path.join(self.autodir, 'sources/ppc64-cross/bin')
mblighcc2e6662006-09-14 01:24:07 +0000549
550 elif target_arch == 'x86_64':
551 install_package('x86_64-cross')
mbligh678823f2006-12-07 18:49:00 +0000552 cross_compile = os.path.join(self.autodir, 'sources/x86_64-cross/bin')
mblighb8a14e32006-05-06 00:17:35 +0000553
mbligh5970cf02006-08-06 15:39:22 +0000554 os.environ['ARCH'] = self.target_arch = target_arch
555
556 self.cross_compile = cross_compile
557 if self.cross_compile:
558 os.environ['CROSS_COMPILE'] = self.cross_compile
mblighcc2e6662006-09-14 01:24:07 +0000559
mbligh72b88fc2006-12-16 18:41:35 +0000560
mblighb8a14e32006-05-06 00:17:35 +0000561 def pickle_dump(self, filename):
mblighc86b0b42006-07-28 17:35:28 +0000562 """dump a pickle of ourself out to the specified filename
563
564 we can't pickle the backreference to job (it contains fd's),
mbligh709bb9b2006-10-12 04:32:16 +0000565 nor would we want to. Same for logfile (fd's).
mblighc86b0b42006-07-28 17:35:28 +0000566 """
mblighb8a14e32006-05-06 00:17:35 +0000567 temp = copy.copy(self)
568 temp.job = None
mbligh709bb9b2006-10-12 04:32:16 +0000569 temp.logfile = None
mblighb8a14e32006-05-06 00:17:35 +0000570 pickle.dump(temp, open(filename, 'w'))
mbligh736adc92007-10-18 03:23:22 +0000571
572
573class rpm_kernel:
574 """ Class for installing rpm kernel package
575 """
576
mblighd7318162007-11-08 21:36:36 +0000577 # pull in some optional site-specific rpm pre-processing
578 try:
579 import site_kernel
580 preprocess_rpm = staticmethod(site_kernel.preprocess_rpm)
581 del site_kernel
582 except ImportError:
583 # just make the preprocessor a nop
584 @staticmethod
585 def preprocess_rpm(rpm_package):
586 return rpm_package
587
588
mblighda0311e2007-10-25 16:03:33 +0000589 def __init__(self, job, rpm_package, subdir):
mbligh736adc92007-10-18 03:23:22 +0000590 self.job = job
mblighd7318162007-11-08 21:36:36 +0000591 self.rpm_package = self.preprocess_rpm(rpm_package)
mblighda0311e2007-10-25 16:03:33 +0000592 self.log_dir = os.path.join(subdir, 'debug')
593 self.subdir = os.path.basename(subdir)
mbligh736adc92007-10-18 03:23:22 +0000594 if os.path.exists(self.log_dir):
595 system('rm -rf ' + self.log_dir)
596 os.mkdir(self.log_dir)
mblighda0311e2007-10-25 16:03:33 +0000597 self.installed_as = None
598 cl_re = re.compile(r'[-.](\d{7,})\.rpm$')
599 match = cl_re.findall(rpm_package)
600 if match:
601 self.changelist = match[0]
602 else:
603 self.changelist = None
mbligh736adc92007-10-18 03:23:22 +0000604
605
mbligh119c12a2007-11-12 22:13:44 +0000606 @logging.record
mblighda0311e2007-10-25 16:03:33 +0000607 def install(self, tag='autotest'):
608 self.installed_as = tag
609
mbligh736adc92007-10-18 03:23:22 +0000610 logfile = os.path.join(self.log_dir, 'rpm_install')
mbligha60795f2007-11-05 18:55:54 +0000611 self.job.stdout.tee_redirect(logfile + '.stdout')
612 self.job.stderr.tee_redirect(logfile + '.stderr')
mbligh736adc92007-10-18 03:23:22 +0000613
mblighda0311e2007-10-25 16:03:33 +0000614 self.rpm_name = system_output('rpm -qp ' + self.rpm_package)
mbligh736adc92007-10-18 03:23:22 +0000615
616 # install
617 system('rpm -i --force ' + self.rpm_package)
618
619 # get file list
mblighda0311e2007-10-25 16:03:33 +0000620 files = system_output('rpm -ql ' + self.rpm_name).splitlines()
mbligh736adc92007-10-18 03:23:22 +0000621
622 self.job.stdout.restore()
623 self.job.stderr.restore()
624
625 # search for vmlinuz
626 for file in files:
627 if file.startswith('/boot/vmlinuz'):
628 self.image = file
629 break
630 else:
631 raise TestError(self.rpm_package + " doesn't contain /boot/vmlinuz")
mblighda0311e2007-10-25 16:03:33 +0000632
mbligh736adc92007-10-18 03:23:22 +0000633 # search for initrd
634 self.initrd = ''
635 for file in files:
636 if file.startswith('/boot/initrd'):
637 self.initrd = file
638 break
639
640 # get version and release number
641 self.version, self.release = system_output(
mblighda0311e2007-10-25 16:03:33 +0000642 'rpm --queryformat="%{VERSION}\\n%{RELEASE}\\n" -q ' + self.rpm_name).splitlines()[0:2]
mbligh736adc92007-10-18 03:23:22 +0000643
644
645 def add_to_bootloader(self, tag='autotest', args=''):
646 """ Add this kernel to bootloader
647 """
648
649 # remove existing entry if present
650 self.job.bootloader.remove_kernel(tag)
651
652 # pull the base argument set from the job config
653 baseargs = self.job.config_get('boot.default_args')
654 if baseargs:
655 args = baseargs + ' ' + args
656
657 # otherwise populate from /proc/cmdline
658 # if not baseargs:
659 # baseargs = open('/proc/cmdline', 'r').readline().strip()
660 # NOTE: This is unnecessary, because boottool does it.
661
662 root = None
663 roots = [x for x in args.split() if x.startswith('root=')]
664 if roots:
665 root = re.sub('^root=', '', roots[0])
666 arglist = [x for x in args.split() if not x.startswith('root=')]
667 args = ' '.join(arglist)
668
669 # add the kernel entry
670 self.job.bootloader.add_kernel(self.image, tag, self.initrd, args = args, root = root)
mbligh10a24a72007-10-24 21:02:53 +0000671
672
mblighda0311e2007-10-25 16:03:33 +0000673 def boot(self, args='', ident=1):
674 """ install and boot this kernel
675 """
mbligh73e82a32007-11-08 21:35:29 +0000676
mbligh10a24a72007-10-24 21:02:53 +0000677 # Check if the kernel has been installed, if not install
678 # as the default tag and boot that.
mblighda0311e2007-10-25 16:03:33 +0000679 if not self.installed_as:
mbligh73e82a32007-11-08 21:35:29 +0000680 self.install()
mblighda0311e2007-10-25 16:03:33 +0000681
682 # If we can check the kernel identity do so.
683 if ident:
684 when = int(time.time())
685 ident = '-'.join([self.version, self.rpm_name.split('-')[1], self.release])
686 args += " IDENT=%d" % (when)
687
688 self.job.next_step_prepend(["job.kernel_check_ident",
689 when, ident, self.changelist, self.subdir, 'rpm'])
mbligh10a24a72007-10-24 21:02:53 +0000690
691 # Boot the selected tag.
mblighda0311e2007-10-25 16:03:33 +0000692 self.add_to_bootloader(args=args, tag=self.installed_as)
mbligh10a24a72007-10-24 21:02:53 +0000693
694 # Boot it.
mblighda0311e2007-10-25 16:03:33 +0000695 self.job.reboot(tag=self.installed_as)