blob: 94fadc3ad0cbf242aa2165e902b31ceb550069df [file] [log] [blame]
mblighc86b0b42006-07-28 17:35:28 +00001__author__ = """Copyright Martin J. Bligh, 2006"""
mbligha2508052006-05-28 21:29:53 +00002
mbligh33dbc912008-06-03 14:59:28 +00003import os, os.path, shutil, copy, pickle, re, glob, time
apw7bae90e2008-03-05 12:18:11 +00004import kernel_config, test, os_dep, kernelexpand
mbligh33dbc912008-06-03 14:59:28 +00005from fd_stack import tee_output_logdir_mark
6import autotest_utils
7from autotest_lib.client.common_lib import logging, utils
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):
mbligh33dbc912008-06-03 14:59:28 +000070 utils.system('rm -rf ' + self.src_dir)
mbligh1e8858e2006-11-24 22:18:35 +000071 if os.path.isdir(self.build_dir):
mbligh33dbc912008-06-03 14:59:28 +000072 utils.system('rm -rf ' + self.build_dir)
mbligh1e8858e2006-11-24 22:18:35 +000073
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):
mbligh33dbc912008-06-03 14:59:28 +000078 utils.system('rm -rf ' + path)
mbligh1e8858e2006-11-24 22:18:35 +000079 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+')
jadmanskia9c75c42008-05-01 22:05:31 +000083 self.applied_patches = []
mbligh4426de02006-10-10 07:18:28 +000084
mbligh72b88fc2006-12-16 18:41:35 +000085 self.target_arch = None
mblighfdbcaec2006-10-01 23:28:57 +000086 self.build_target = 'bzImage'
mbligh8baa2ea2006-12-17 23:01:24 +000087 self.build_image = None
mblighfdbcaec2006-10-01 23:28:57 +000088
apwe8956122007-12-13 19:00:15 +000089 arch = get_current_kernel_arch()
90 if arch == 's390' or arch == 's390x':
91 self.build_target = 'image'
92 elif arch == 'ia64':
mblighcac347a2007-06-02 17:21:48 +000093 self.build_target = 'all'
94 self.build_image = 'vmlinux.gz'
95
mblighfdbcaec2006-10-01 23:28:57 +000096 if leave:
97 return
mbligh534015f2006-09-15 03:28:56 +000098
mbligh4426de02006-10-10 07:18:28 +000099 self.logfile.write('BASE: %s\n' % base_tree)
apw2366d992007-03-12 20:35:57 +0000100
101 # Where we have direct version hint record that
102 # for later configuration selection.
103 shorthand = re.compile(r'^\d+\.\d+\.\d+')
104 if shorthand.match(base_tree):
105 self.base_tree_version = base_tree
106 else:
107 self.base_tree_version = None
108
apw040dcaa2007-11-21 19:36:55 +0000109 # Actually extract the tree. Make sure we know it occured
110 self.extract(base_tree)
111
112
apw7bae90e2008-03-05 12:18:11 +0000113 def kernelexpand(self, kernel):
114 # If we have something like a path, just use it as it is
mbligh6dee37f2008-03-10 18:09:13 +0000115 if '/' in kernel:
116 return [kernel]
apw7bae90e2008-03-05 12:18:11 +0000117
118 # Find the configured mirror list.
119 mirrors = self.job.config_get('mirror.mirrors')
120 if not mirrors:
121 # LEGACY: convert the kernel.org mirror
122 mirror = self.job.config_get('mirror.ftp_kernel_org')
apw37451692008-03-28 12:25:50 +0000123 if mirror:
124 korg = 'http://www.kernel.org/pub/linux/kernel'
125 mirrors = [
126 [ korg + '/v2.6', mirror + '/v2.6' ],
127 [ korg + '/people/akpm/patches/2.6',
128 mirror + '/akpm' ],
129 [ korg + '/people/mbligh',
130 mirror + '/mbligh' ],
131 ]
apw7bae90e2008-03-05 12:18:11 +0000132
133 patches = kernelexpand.expand_classic(kernel, mirrors)
134 print patches
135
136 return patches
137
138
apw040dcaa2007-11-21 19:36:55 +0000139 @logging.record
140 @tee_output_logdir_mark
141 def extract(self, base_tree):
mbligh534015f2006-09-15 03:28:56 +0000142 if os.path.exists(base_tree):
143 self.get_kernel_tree(base_tree)
144 else:
apw7bae90e2008-03-05 12:18:11 +0000145 base_components = self.kernelexpand(base_tree)
mbligh534015f2006-09-15 03:28:56 +0000146 print 'kernelexpand: '
147 print base_components
148 self.get_kernel_tree(base_components.pop(0))
149 if base_components: # apply remaining patches
150 self.patch(*base_components)
mblighf4c35322006-03-13 01:01:10 +0000151
152
mbligh119c12a2007-11-12 22:13:44 +0000153 @logging.record
apw001e40a2007-11-21 19:24:37 +0000154 @tee_output_logdir_mark
mblighb8e0a112007-11-05 20:27:36 +0000155 def patch(self, *patches):
mblighc86b0b42006-07-28 17:35:28 +0000156 """Apply a list of patches (in order)"""
mbligh1e8858e2006-11-24 22:18:35 +0000157 if not patches:
158 return
mbligh534015f2006-09-15 03:28:56 +0000159 print 'Applying patches: ', patches
mbligh0763e732007-08-30 16:35:06 +0000160 self.apply_patches(self.get_patches(patches))
mblighf4c35322006-03-13 01:01:10 +0000161
162
mbligh119c12a2007-11-12 22:13:44 +0000163 @logging.record
apw001e40a2007-11-21 19:24:37 +0000164 @tee_output_logdir_mark
mblighb8e0a112007-11-05 20:27:36 +0000165 def config(self, config_file = '', config_list = None, defconfig = False):
mbligh678823f2006-12-07 18:49:00 +0000166 self.set_cross_cc()
apwb449c7d2006-12-05 12:21:44 +0000167 config = kernel_config.kernel_config(self.job, self.build_dir,
apw2366d992007-03-12 20:35:57 +0000168 self.config_dir, config_file, config_list,
169 defconfig, self.base_tree_version)
mblighf4c35322006-03-13 01:01:10 +0000170
171
172 def get_patches(self, patches):
mbligh1e8858e2006-11-24 22:18:35 +0000173 """fetch the patches to the local src_dir"""
mblighf4c35322006-03-13 01:01:10 +0000174 local_patches = []
175 for patch in patches:
mbligh1e8858e2006-11-24 22:18:35 +0000176 dest = os.path.join(self.src_dir, basename(patch))
mbligh0763e732007-08-30 16:35:06 +0000177 # FIXME: this isn't unique. Append something to it
178 # like wget does if it's not there?
mbligh1e8858e2006-11-24 22:18:35 +0000179 print "get_file %s %s %s %s" % (patch, dest, self.src_dir, basename(patch))
mblighf4c35322006-03-13 01:01:10 +0000180 get_file(patch, dest)
mbligh0763e732007-08-30 16:35:06 +0000181 # probably safer to use the command, not python library
mbligh33dbc912008-06-03 14:59:28 +0000182 md5sum = utils.system_output('md5sum ' + dest).split()[0]
mbligh0763e732007-08-30 16:35:06 +0000183 local_patches.append((patch, dest, md5sum))
mbligh534015f2006-09-15 03:28:56 +0000184 return local_patches
mblighf4c35322006-03-13 01:01:10 +0000185
mbligh72b88fc2006-12-16 18:41:35 +0000186
mbligh534015f2006-09-15 03:28:56 +0000187 def apply_patches(self, local_patches):
mblighc86b0b42006-07-28 17:35:28 +0000188 """apply the list of patches, in order"""
mblighf4c35322006-03-13 01:01:10 +0000189 builddir = self.build_dir
190 os.chdir(builddir)
191
mbligh534015f2006-09-15 03:28:56 +0000192 if not local_patches:
mblighf4c35322006-03-13 01:01:10 +0000193 return None
mbligh0763e732007-08-30 16:35:06 +0000194 for (spec, local, md5sum) in local_patches:
195 if local.endswith('.bz2') or local.endswith('.gz'):
196 ref = spec
197 else:
198 ref = force_copy(local, self.results_dir)
199 ref = self.job.relative_path(ref)
jadmanskia9c75c42008-05-01 22:05:31 +0000200 patch_id = "%s %s %s" % (spec, ref, md5sum)
201 log = "PATCH: " + patch_id + "\n"
mbligh0763e732007-08-30 16:35:06 +0000202 print log
203 cat_file_to_cmd(local, 'patch -p1 > /dev/null')
204 self.logfile.write(log)
jadmanskia9c75c42008-05-01 22:05:31 +0000205 self.applied_patches.append(patch_id)
mbligh72b88fc2006-12-16 18:41:35 +0000206
207
208 def get_kernel_tree(self, base_tree):
mblighc49af7b2006-11-24 04:02:21 +0000209 """Extract/link base_tree to self.build_dir"""
mbligh5970cf02006-08-06 15:39:22 +0000210
211 # if base_tree is a dir, assume uncompressed kernel
212 if os.path.isdir(base_tree):
213 print 'Symlinking existing kernel source'
mblighc49af7b2006-11-24 04:02:21 +0000214 os.symlink(base_tree, self.build_dir)
mblighf4c35322006-03-13 01:01:10 +0000215
mbligh5970cf02006-08-06 15:39:22 +0000216 # otherwise, extract tarball
217 else:
mblighc49af7b2006-11-24 04:02:21 +0000218 os.chdir(os.path.dirname(self.src_dir))
219 # Figure out local destination for tarball
220 tarball = os.path.join(self.src_dir, os.path.basename(base_tree))
mbligh5970cf02006-08-06 15:39:22 +0000221 get_file(base_tree, tarball)
mbligh5970cf02006-08-06 15:39:22 +0000222 print 'Extracting kernel tarball:', tarball, '...'
mbligh33dbc912008-06-03 14:59:28 +0000223 autotest_utils.extract_tarball_to_dir(tarball,
224 self.build_dir)
mbligh5970cf02006-08-06 15:39:22 +0000225
mblighf4c35322006-03-13 01:01:10 +0000226
mblighfdbcaec2006-10-01 23:28:57 +0000227 def extraversion(self, tag, append=1):
228 os.chdir(self.build_dir)
mbligh4426de02006-10-10 07:18:28 +0000229 extraversion_sub = r's/^EXTRAVERSION =\s*\(.*\)/EXTRAVERSION = '
mblighfdbcaec2006-10-01 23:28:57 +0000230 if append:
mbligh4426de02006-10-10 07:18:28 +0000231 p = extraversion_sub + '\\1-%s/' % tag
mblighfdbcaec2006-10-01 23:28:57 +0000232 else:
mbligh4426de02006-10-10 07:18:28 +0000233 p = extraversion_sub + '-%s/' % tag
mbligh33dbc912008-06-03 14:59:28 +0000234 utils.system('mv Makefile Makefile.old')
235 utils.system('sed "%s" < Makefile.old > Makefile' % p)
mblighfdbcaec2006-10-01 23:28:57 +0000236
237
mbligh119c12a2007-11-12 22:13:44 +0000238 @logging.record
apw001e40a2007-11-21 19:24:37 +0000239 @tee_output_logdir_mark
mblighb8e0a112007-11-05 20:27:36 +0000240 def build(self, make_opts = '', logfile = '', extraversion='autotest'):
mblighc86b0b42006-07-28 17:35:28 +0000241 """build the kernel
mbligh72b88fc2006-12-16 18:41:35 +0000242
mblighc86b0b42006-07-28 17:35:28 +0000243 make_opts
244 additional options to make, if any
245 """
mbligh10038012006-10-19 03:32:45 +0000246 os_dep.commands('gcc', 'make')
mbligh6d4c9412006-09-13 23:08:44 +0000247 if logfile == '':
248 logfile = os.path.join(self.log_dir, 'kernel_build')
mblighf4c35322006-03-13 01:01:10 +0000249 os.chdir(self.build_dir)
mblighfdbcaec2006-10-01 23:28:57 +0000250 if extraversion:
251 self.extraversion(extraversion)
mbligha2508052006-05-28 21:29:53 +0000252 self.set_cross_cc()
mblighf4c35322006-03-13 01:01:10 +0000253 # setup_config_file(config_file, config_overrides)
apwc7846102006-04-06 18:22:13 +0000254
255 # Not needed on 2.6, but hard to tell -- handle failure
mbligh33dbc912008-06-03 14:59:28 +0000256 utils.system('make dep', ignore_status=True)
257 threads = 2 * autotest_utils.count_cpus()
mbligh6d4c9412006-09-13 23:08:44 +0000258 build_string = 'make -j %d %s %s' % (threads, make_opts,
259 self.build_target)
260 # eg make bzImage, or make zImage
261 print build_string
262 system(build_string)
mblighf4c35322006-03-13 01:01:10 +0000263 if kernel_config.modules_needed('.config'):
mbligh33dbc912008-06-03 14:59:28 +0000264 utils.system('make -j %d modules' % (threads))
apwc7846102006-04-06 18:22:13 +0000265
mbligh4426de02006-10-10 07:18:28 +0000266 kernel_version = self.get_kernel_build_ver()
267 kernel_version = re.sub('-autotest', '', kernel_version)
268 self.logfile.write('BUILD VERSION: %s\n' % kernel_version)
mbligh1e8858e2006-11-24 22:18:35 +0000269
270 force_copy(self.build_dir+'/System.map', self.results_dir)
mblighf4c35322006-03-13 01:01:10 +0000271
272
mbligh30f28c52007-10-11 18:35:35 +0000273 def build_timed(self, threads, timefile = '/dev/null', make_opts = '',
mbligh2e793a42007-10-12 23:58:35 +0000274 output = '/dev/null'):
mblighc86b0b42006-07-28 17:35:28 +0000275 """time the bulding of the kernel"""
mblighb8a14e32006-05-06 00:17:35 +0000276 os.chdir(self.build_dir)
mbligh678823f2006-12-07 18:49:00 +0000277 self.set_cross_cc()
mbligh30f28c52007-10-11 18:35:35 +0000278
mblighd79b2b42007-11-05 20:38:15 +0000279 self.clean(logged=False)
mbligh30f28c52007-10-11 18:35:35 +0000280 build_string = "/usr/bin/time -o %s make %s -j %s vmlinux" \
281 % (timefile, make_opts, threads)
mbligh2e793a42007-10-12 23:58:35 +0000282 build_string += ' > %s 2>&1' % output
mblighf4c35322006-03-13 01:01:10 +0000283 print build_string
mbligh33dbc912008-06-03 14:59:28 +0000284 utils.system(build_string)
mbligh30f28c52007-10-11 18:35:35 +0000285
apwc7846102006-04-06 18:22:13 +0000286 if (not os.path.isfile('vmlinux')):
mbligh33dbc912008-06-03 14:59:28 +0000287 errmsg = "no vmlinux found, kernel build failed"
288 raise error.TestError(errmsg)
mblighb8a14e32006-05-06 00:17:35 +0000289
290
mbligh119c12a2007-11-12 22:13:44 +0000291 @logging.record
apw001e40a2007-11-21 19:24:37 +0000292 @tee_output_logdir_mark
mblighb8e0a112007-11-05 20:27:36 +0000293 def clean(self):
mblighc86b0b42006-07-28 17:35:28 +0000294 """make clean in the kernel tree"""
mblighb8a14e32006-05-06 00:17:35 +0000295 os.chdir(self.build_dir)
mblighf4c35322006-03-13 01:01:10 +0000296 print "make clean"
mbligh33dbc912008-06-03 14:59:28 +0000297 utils.system('make clean > /dev/null 2> /dev/null')
mblighf4c35322006-03-13 01:01:10 +0000298
mbligh50f42ea2006-09-30 22:22:21 +0000299
mbligh119c12a2007-11-12 22:13:44 +0000300 @logging.record
apw001e40a2007-11-21 19:24:37 +0000301 @tee_output_logdir_mark
mblighb8e0a112007-11-05 20:27:36 +0000302 def mkinitrd(self, version, image, system_map, initrd):
mbligh50f42ea2006-09-30 22:22:21 +0000303 """Build kernel initrd image.
304 Try to use distro specific way to build initrd image.
305 Parameters:
306 version
307 new kernel version
308 image
309 new kernel image file
310 system_map
311 System.map file
312 initrd
313 initrd image file to build
314 """
mbligh33dbc912008-06-03 14:59:28 +0000315 vendor = autotest_utils.get_os_vendor()
mbligh50f42ea2006-09-30 22:22:21 +0000316
317 if os.path.isfile(initrd):
mblighfdbcaec2006-10-01 23:28:57 +0000318 print "Existing %s file, will remove it." % initrd
mbligh50f42ea2006-09-30 22:22:21 +0000319 os.remove(initrd)
mbligh72b88fc2006-12-16 18:41:35 +0000320
apwe43a30b2007-09-25 16:51:30 +0000321 args = self.job.config_get('kernel.mkinitrd_extra_args')
322
mbligh3d515d42007-11-09 17:00:36 +0000323 # don't leak 'None' into mkinitrd command
324 if not args:
325 args = ''
326
mbligh50f42ea2006-09-30 22:22:21 +0000327 if vendor in ['Red Hat', 'Fedora Core']:
mbligh33dbc912008-06-03 14:59:28 +0000328 utils.system('mkinitrd %s %s %s' % (args, initrd, version))
mbligh50f42ea2006-09-30 22:22:21 +0000329 elif vendor in ['SUSE']:
mbligh33dbc912008-06-03 14:59:28 +0000330 utils.system('mkinitrd %s -k %s -i %s -M %s' % (args, image, initrd, system_map))
apwc898a1f2007-02-28 15:27:22 +0000331 elif vendor in ['Debian', 'Ubuntu']:
332 if os.path.isfile('/usr/sbin/mkinitrd'):
333 cmd = '/usr/sbin/mkinitrd'
334 elif os.path.isfile('/usr/sbin/mkinitramfs'):
335 cmd = '/usr/sbin/mkinitramfs'
336 else:
mbligh33dbc912008-06-03 14:59:28 +0000337 raise error.TestError('No Debian initrd builder')
338 utils.system('%s %s -o %s %s' % (cmd, args, initrd, version))
mbligh50f42ea2006-09-30 22:22:21 +0000339 else:
mbligh33dbc912008-06-03 14:59:28 +0000340 raise error.TestError('Unsupported vendor %s' % vendor)
mbligh50f42ea2006-09-30 22:22:21 +0000341
342
mbligh8baa2ea2006-12-17 23:01:24 +0000343 def set_build_image(self, image):
344 self.build_image = image
345
346
mbligh119c12a2007-11-12 22:13:44 +0000347 @logging.record
apw001e40a2007-11-21 19:24:37 +0000348 @tee_output_logdir_mark
mblighb8e0a112007-11-05 20:27:36 +0000349 def install(self, tag='autotest', prefix = '/'):
mblighc86b0b42006-07-28 17:35:28 +0000350 """make install in the kernel tree"""
apw87c65c12007-09-27 17:19:37 +0000351
352 # Record that we have installed the kernel, and
353 # the tag under which we installed it.
354 self.installed_as = tag
355
mblighf4c35322006-03-13 01:01:10 +0000356 os.chdir(self.build_dir)
mbligh72b88fc2006-12-16 18:41:35 +0000357
mbligh6a1d4db2006-10-06 04:30:16 +0000358 if not os.path.isdir(prefix):
359 os.mkdir(prefix)
mbligha87116f2006-10-10 02:47:08 +0000360 self.boot_dir = os.path.join(prefix, 'boot')
361 if not os.path.isdir(self.boot_dir):
362 os.mkdir(self.boot_dir)
mbligh0ad65582006-10-06 04:16:36 +0000363
mbligh8baa2ea2006-12-17 23:01:24 +0000364 if not self.build_image:
365 images = glob.glob('arch/*/boot/' + self.build_target)
366 if len(images):
367 self.build_image = images[0]
368 else:
369 self.build_image = self.build_target
mbligha87116f2006-10-10 02:47:08 +0000370
371 # remember installed files
mbligha87116f2006-10-10 02:47:08 +0000372 self.vmlinux = self.boot_dir + '/vmlinux-' + tag
mbligh8baa2ea2006-12-17 23:01:24 +0000373 if (self.build_image != 'vmlinux'):
apw9a61c5b2006-11-28 10:03:15 +0000374 self.image = self.boot_dir + '/vmlinuz-' + tag
375 else:
376 self.image = self.vmlinux
mbligha87116f2006-10-10 02:47:08 +0000377 self.system_map = self.boot_dir + '/System.map-' + tag
378 self.config = self.boot_dir + '/config-' + tag
379 self.initrd = ''
380
381 # copy to boot dir
mbligh33dbc912008-06-03 14:59:28 +0000382 autotest_utils.force_copy('vmlinux', self.vmlinux)
mbligh8baa2ea2006-12-17 23:01:24 +0000383 if (self.build_image != 'vmlinux'):
384 force_copy(self.build_image, self.image)
mbligh33dbc912008-06-03 14:59:28 +0000385 autotest_utils.force_copy('System.map', self.system_map)
386 autotest_utils.force_copy('.config', self.config)
mbligha87116f2006-10-10 02:47:08 +0000387
mbligh6a1d4db2006-10-06 04:30:16 +0000388 if not kernel_config.modules_needed('.config'):
389 return
390
mbligh33dbc912008-06-03 14:59:28 +0000391 utils.system('make modules_install INSTALL_MOD_PATH=%s' % prefix)
mbligh6a1d4db2006-10-06 04:30:16 +0000392 if prefix == '/':
mbligha87116f2006-10-10 02:47:08 +0000393 self.initrd = self.boot_dir + '/initrd-' + tag
mblighb8e0a112007-11-05 20:27:36 +0000394 self.mkinitrd(self.get_kernel_build_ver(), self.image,
395 self.system_map, self.initrd)
mbligh5925e962007-08-30 17:05:22 +0000396
397
mbligha87116f2006-10-10 02:47:08 +0000398 def add_to_bootloader(self, tag='autotest', args=''):
399 """ add this kernel to bootloader, taking an
400 optional parameter of space separated parameters
401 e.g.: kernel.add_to_bootloader('mykernel', 'ro acpi=off')
402 """
403
404 # remove existing entry if present
405 self.job.bootloader.remove_kernel(tag)
406
apwcbe32572006-11-28 10:00:23 +0000407 # pull the base argument set from the job config,
apwcbe32572006-11-28 10:00:23 +0000408 baseargs = self.job.config_get('boot.default_args')
mbligh78bc05e2006-12-25 02:29:59 +0000409 if baseargs:
410 args = baseargs + " " + args
411
412 # otherwise populate from /proc/cmdline
413 # if not baseargs:
414 # baseargs = open('/proc/cmdline', 'r').readline().strip()
415 # NOTE: This is unnecessary, because boottool does it.
apwcbe32572006-11-28 10:00:23 +0000416
mbligh78bc05e2006-12-25 02:29:59 +0000417 root = None
418 roots = [x for x in args.split() if x.startswith('root=')]
419 if roots:
420 root = re.sub('^root=', '', roots[0])
421 arglist = [x for x in args.split() if not x.startswith('root=')]
422 args = ' '.join(arglist)
mbligha87116f2006-10-10 02:47:08 +0000423
mbligh78bc05e2006-12-25 02:29:59 +0000424 # add the kernel entry
425 # add_kernel(image, title='autotest', initrd='')
426 self.job.bootloader.add_kernel(self.image, tag, self.initrd, \
427 args = args, root = root)
mbligh6a1d4db2006-10-06 04:30:16 +0000428
mbligh201aa892006-10-29 04:02:05 +0000429
mbligh548f29a2006-10-17 04:55:12 +0000430 def get_kernel_build_arch(self, arch=None):
mbligh201aa892006-10-29 04:02:05 +0000431 """
432 Work out the current kernel architecture (as a kernel arch)
433 """
mbligh548f29a2006-10-17 04:55:12 +0000434 if not arch:
mbligh33dbc912008-06-03 14:59:28 +0000435 arch = autotest_utils.get_current_kernel_arch()
mbligh548f29a2006-10-17 04:55:12 +0000436 if re.match('i.86', arch):
437 return 'i386'
438 elif re.match('sun4u', arch):
439 return 'sparc64'
440 elif re.match('arm.*', arch):
441 return 'arm'
442 elif re.match('sa110', arch):
443 return 'arm'
444 elif re.match('s390x', arch):
445 return 's390'
446 elif re.match('parisc64', arch):
447 return 'parisc'
448 elif re.match('ppc.*', arch):
449 return 'powerpc'
450 elif re.match('mips.*', arch):
451 return 'mips'
452 else:
453 return arch
454
mbligh6a1d4db2006-10-06 04:30:16 +0000455
mbligh237bed32007-09-05 13:05:57 +0000456 def get_kernel_build_release(self):
457 releasem = re.compile(r'.*UTS_RELEASE\s+"([^"]+)".*');
458 versionm = re.compile(r'.*UTS_VERSION\s+"([^"]+)".*');
459
460 release = None
461 version = None
462
463 for file in [ self.build_dir + "/include/linux/version.h",
464 self.build_dir + "/include/linux/utsrelease.h",
465 self.build_dir + "/include/linux/compile.h" ]:
466 if os.path.exists(file):
467 fd = open(file, 'r')
468 for line in fd.readlines():
469 m = releasem.match(line)
470 if m:
471 release = m.groups()[0]
472 m = versionm.match(line)
473 if m:
474 version = m.groups()[0]
475 fd.close()
476
477 return (release, version)
478
479
480 def get_kernel_build_ident(self):
481 (release, version) = self.get_kernel_build_release()
482
483 if not release or not version:
mbligh33dbc912008-06-03 14:59:28 +0000484 raise error.JobError('kernel has no identity')
mbligh237bed32007-09-05 13:05:57 +0000485
486 return release + '::' + version
487
488
apw11985b72007-10-04 15:44:47 +0000489 def boot(self, args='', ident=1):
apw1b5dc362006-10-31 11:24:26 +0000490 """ install and boot this kernel, do not care how
491 just make it happen.
492 """
493
mbligh237bed32007-09-05 13:05:57 +0000494 # If we can check the kernel identity do so.
495 if ident:
496 when = int(time.time())
497 ident = self.get_kernel_build_ident()
498 args += " IDENT=%d" % (when)
499
apwce73d892007-09-25 16:53:05 +0000500 self.job.next_step_prepend(["job.kernel_check_ident",
jadmanskia9c75c42008-05-01 22:05:31 +0000501 when, ident, self.subdir,
502 self.applied_patches])
mbligh237bed32007-09-05 13:05:57 +0000503
apw87c65c12007-09-27 17:19:37 +0000504 # Check if the kernel has been installed, if not install
505 # as the default tag and boot that.
506 if not self.installed_as:
507 self.install()
508
509 # Boot the selected tag.
510 self.add_to_bootloader(args=args, tag=self.installed_as)
apw1b5dc362006-10-31 11:24:26 +0000511
512 # Boot it.
apw11985b72007-10-04 15:44:47 +0000513 self.job.reboot(tag=self.installed_as)
apw1b5dc362006-10-31 11:24:26 +0000514
515
mblighfdbcaec2006-10-01 23:28:57 +0000516 def get_kernel_build_ver(self):
mblighe11f5fc2006-10-04 04:42:22 +0000517 """Check Makefile and .config to return kernel version"""
518 version = patchlevel = sublevel = extraversion = localversion = ''
519
520 for line in open(self.build_dir + '/Makefile', 'r').readlines():
521 if line.startswith('VERSION'):
522 version = line[line.index('=') + 1:].strip()
523 if line.startswith('PATCHLEVEL'):
524 patchlevel = line[line.index('=') + 1:].strip()
525 if line.startswith('SUBLEVEL'):
526 sublevel = line[line.index('=') + 1:].strip()
527 if line.startswith('EXTRAVERSION'):
528 extraversion = line[line.index('=') + 1:].strip()
529
530 for line in open(self.build_dir + '/.config', 'r').readlines():
531 if line.startswith('CONFIG_LOCALVERSION='):
532 localversion = line.rstrip().split('"')[1]
533
mbligh72b88fc2006-12-16 18:41:35 +0000534 return "%s.%s.%s%s%s" %(version, patchlevel, sublevel, extraversion, localversion)
mblighfdbcaec2006-10-01 23:28:57 +0000535
536
mbligh8baa2ea2006-12-17 23:01:24 +0000537 def set_build_target(self, build_target):
538 if build_target:
539 self.build_target = build_target
540 print 'BUILD TARGET: %s' % self.build_target
541
542
mbligh5970cf02006-08-06 15:39:22 +0000543 def set_cross_cc(self, target_arch=None, cross_compile=None,
544 build_target='bzImage'):
mblighc86b0b42006-07-28 17:35:28 +0000545 """Set up to cross-compile.
mblighcc2e6662006-09-14 01:24:07 +0000546 This is broken. We need to work out what the default
547 compile produces, and if not, THEN set the cross
548 compiler.
mblighc86b0b42006-07-28 17:35:28 +0000549 """
mblighcc2e6662006-09-14 01:24:07 +0000550
mbligh5970cf02006-08-06 15:39:22 +0000551 if self.target_arch:
552 return
mbligh678823f2006-12-07 18:49:00 +0000553
mbligh8baa2ea2006-12-17 23:01:24 +0000554 # if someone has set build_target, don't clobber in set_cross_cc
555 # run set_build_target before calling set_cross_cc
556 if not self.build_target:
557 self.set_build_target(build_target)
mbligh72b88fc2006-12-16 18:41:35 +0000558
mbligh5970cf02006-08-06 15:39:22 +0000559 # If no 'target_arch' given assume native compilation
560 if target_arch == None:
mbligh548f29a2006-10-17 04:55:12 +0000561 target_arch = get_current_kernel_arch()
mbligh5970cf02006-08-06 15:39:22 +0000562 if target_arch == 'ppc64':
mbligh5970cf02006-08-06 15:39:22 +0000563 if self.build_target == 'bzImage':
apw9a61c5b2006-11-28 10:03:15 +0000564 self.build_target = 'vmlinux'
mbligh678823f2006-12-07 18:49:00 +0000565
566 if not cross_compile:
567 cross_compile = self.job.config_get('kernel.cross_cc')
568
569 if cross_compile:
570 os.environ['CROSS_COMPILE'] = cross_compile
571 else:
572 if os.environ.has_key('CROSS_COMPILE'):
573 del os.environ['CROSS_COMPILE']
574
mblighcc2e6662006-09-14 01:24:07 +0000575 return # HACK. Crap out for now.
mblighf4c35322006-03-13 01:01:10 +0000576
mblighcc2e6662006-09-14 01:24:07 +0000577 # At this point I know what arch I *want* to build for
578 # but have no way of working out what arch the default
579 # compiler DOES build for.
580
581 # Oh, and BTW, install_package() doesn't exist yet.
mbligh72b88fc2006-12-16 18:41:35 +0000582
mblighcc2e6662006-09-14 01:24:07 +0000583 if target_arch == 'ppc64':
584 install_package('ppc64-cross')
mbligh678823f2006-12-07 18:49:00 +0000585 cross_compile = os.path.join(self.autodir, 'sources/ppc64-cross/bin')
mblighcc2e6662006-09-14 01:24:07 +0000586
587 elif target_arch == 'x86_64':
588 install_package('x86_64-cross')
mbligh678823f2006-12-07 18:49:00 +0000589 cross_compile = os.path.join(self.autodir, 'sources/x86_64-cross/bin')
mblighb8a14e32006-05-06 00:17:35 +0000590
mbligh5970cf02006-08-06 15:39:22 +0000591 os.environ['ARCH'] = self.target_arch = target_arch
592
593 self.cross_compile = cross_compile
594 if self.cross_compile:
595 os.environ['CROSS_COMPILE'] = self.cross_compile
mblighcc2e6662006-09-14 01:24:07 +0000596
mbligh72b88fc2006-12-16 18:41:35 +0000597
mblighb8a14e32006-05-06 00:17:35 +0000598 def pickle_dump(self, filename):
mblighc86b0b42006-07-28 17:35:28 +0000599 """dump a pickle of ourself out to the specified filename
600
601 we can't pickle the backreference to job (it contains fd's),
mbligh709bb9b2006-10-12 04:32:16 +0000602 nor would we want to. Same for logfile (fd's).
mblighc86b0b42006-07-28 17:35:28 +0000603 """
mblighb8a14e32006-05-06 00:17:35 +0000604 temp = copy.copy(self)
605 temp.job = None
mbligh709bb9b2006-10-12 04:32:16 +0000606 temp.logfile = None
mblighb8a14e32006-05-06 00:17:35 +0000607 pickle.dump(temp, open(filename, 'w'))
mbligh736adc92007-10-18 03:23:22 +0000608
609
610class rpm_kernel:
611 """ Class for installing rpm kernel package
612 """
613
mblighda0311e2007-10-25 16:03:33 +0000614 def __init__(self, job, rpm_package, subdir):
mbligh736adc92007-10-18 03:23:22 +0000615 self.job = job
mbligh6ee7ee02007-11-13 23:49:05 +0000616 self.rpm_package = rpm_package
mblighda0311e2007-10-25 16:03:33 +0000617 self.log_dir = os.path.join(subdir, 'debug')
618 self.subdir = os.path.basename(subdir)
mbligh736adc92007-10-18 03:23:22 +0000619 if os.path.exists(self.log_dir):
mbligh33dbc912008-06-03 14:59:28 +0000620 utils.system('rm -rf ' + self.log_dir)
mbligh736adc92007-10-18 03:23:22 +0000621 os.mkdir(self.log_dir)
mblighda0311e2007-10-25 16:03:33 +0000622 self.installed_as = None
mbligh736adc92007-10-18 03:23:22 +0000623
624
mbligh119c12a2007-11-12 22:13:44 +0000625 @logging.record
apw001e40a2007-11-21 19:24:37 +0000626 @tee_output_logdir_mark
mblighda0311e2007-10-25 16:03:33 +0000627 def install(self, tag='autotest'):
628 self.installed_as = tag
629
mbligh33dbc912008-06-03 14:59:28 +0000630 self.rpm_name = utils.system_output('rpm -qp ' + self.rpm_package)
mbligh736adc92007-10-18 03:23:22 +0000631
632 # install
mbligh33dbc912008-06-03 14:59:28 +0000633 utils.system('rpm -i --force ' + self.rpm_package)
mbligh736adc92007-10-18 03:23:22 +0000634
635 # get file list
mbligh33dbc912008-06-03 14:59:28 +0000636 files = utils.system_output('rpm -ql ' + self.rpm_name).splitlines()
mbligh736adc92007-10-18 03:23:22 +0000637
mbligh736adc92007-10-18 03:23:22 +0000638 # search for vmlinuz
639 for file in files:
640 if file.startswith('/boot/vmlinuz'):
641 self.image = file
642 break
643 else:
mbligh33dbc912008-06-03 14:59:28 +0000644 errmsg = "%s doesn't contain /boot/vmlinuz"
645 errmsg %= self.rpm_package
646 raise error.TestError(errmsg)
mblighda0311e2007-10-25 16:03:33 +0000647
mbligh736adc92007-10-18 03:23:22 +0000648 # search for initrd
649 self.initrd = ''
650 for file in files:
651 if file.startswith('/boot/initrd'):
652 self.initrd = file
653 break
654
655 # get version and release number
mbligh33dbc912008-06-03 14:59:28 +0000656 self.version, self.release = utils.system_output(
mblighda0311e2007-10-25 16:03:33 +0000657 'rpm --queryformat="%{VERSION}\\n%{RELEASE}\\n" -q ' + self.rpm_name).splitlines()[0:2]
mbligh736adc92007-10-18 03:23:22 +0000658
659
660 def add_to_bootloader(self, tag='autotest', args=''):
661 """ Add this kernel to bootloader
662 """
663
664 # remove existing entry if present
665 self.job.bootloader.remove_kernel(tag)
666
667 # pull the base argument set from the job config
668 baseargs = self.job.config_get('boot.default_args')
669 if baseargs:
670 args = baseargs + ' ' + args
671
672 # otherwise populate from /proc/cmdline
673 # if not baseargs:
674 # baseargs = open('/proc/cmdline', 'r').readline().strip()
675 # NOTE: This is unnecessary, because boottool does it.
676
677 root = None
678 roots = [x for x in args.split() if x.startswith('root=')]
679 if roots:
680 root = re.sub('^root=', '', roots[0])
681 arglist = [x for x in args.split() if not x.startswith('root=')]
682 args = ' '.join(arglist)
683
684 # add the kernel entry
685 self.job.bootloader.add_kernel(self.image, tag, self.initrd, args = args, root = root)
mbligh10a24a72007-10-24 21:02:53 +0000686
687
mblighda0311e2007-10-25 16:03:33 +0000688 def boot(self, args='', ident=1):
689 """ install and boot this kernel
690 """
mbligh73e82a32007-11-08 21:35:29 +0000691
mbligh10a24a72007-10-24 21:02:53 +0000692 # Check if the kernel has been installed, if not install
693 # as the default tag and boot that.
mblighda0311e2007-10-25 16:03:33 +0000694 if not self.installed_as:
mbligh73e82a32007-11-08 21:35:29 +0000695 self.install()
mblighda0311e2007-10-25 16:03:33 +0000696
697 # If we can check the kernel identity do so.
698 if ident:
699 when = int(time.time())
mbligh38a4a112008-03-19 13:11:34 +0000700 ident = '-'.join([self.version,
701 self.rpm_name.split('-')[1],
702 self.release])
mblighda0311e2007-10-25 16:03:33 +0000703 args += " IDENT=%d" % (when)
704
705 self.job.next_step_prepend(["job.kernel_check_ident",
mbligh38a4a112008-03-19 13:11:34 +0000706 when, ident, self.subdir, 'rpm'])
mbligh10a24a72007-10-24 21:02:53 +0000707
708 # Boot the selected tag.
mblighda0311e2007-10-25 16:03:33 +0000709 self.add_to_bootloader(args=args, tag=self.installed_as)
mbligh10a24a72007-10-24 21:02:53 +0000710
711 # Boot it.
mblighda0311e2007-10-25 16:03:33 +0000712 self.job.reboot(tag=self.installed_as)
mbligh6ee7ee02007-11-13 23:49:05 +0000713
714
715# pull in some optional site-specific path pre-processing
716try:
717 import site_kernel
718 preprocess_path = site_kernel.preprocess_path
719 del site_kernel
720except ImportError:
721 # just make the preprocessor a nop
722 def preprocess_path(path):
723 return path
724
725def auto_kernel(job, path, subdir, tmp_dir, build_dir, leave=False):
726 """\
727 Create a kernel object, dynamically selecting the appropriate class to use
728 based on the path provided.
729 """
730 path = preprocess_path(path)
731 if path.endswith('.rpm'):
732 return rpm_kernel(job, path, subdir)
733 else:
734 return kernel(job, path, subdir, tmp_dir, build_dir, leave)