mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 1 | __author__ = """Copyright Martin J. Bligh, 2006""" |
mbligh | a250805 | 2006-05-28 21:29:53 +0000 | [diff] [blame] | 2 | |
apw | ba5bbfd | 2006-10-16 09:25:45 +0000 | [diff] [blame] | 3 | import os,os.path,shutil,urllib,copy,pickle,re,glob |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 4 | from autotest_utils import * |
mbligh | 1003801 | 2006-10-19 03:32:45 +0000 | [diff] [blame] | 5 | import kernel_config, test, os_dep |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 6 | |
| 7 | class kernel: |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 8 | """ Class for compiling kernels. |
| 9 | |
| 10 | Data for the object includes the src files |
| 11 | used to create the kernel, patches applied, config (base + changes), |
| 12 | the build directory itself, and logged output |
| 13 | |
| 14 | Properties: |
| 15 | job |
| 16 | Backpointer to the job object we're part of |
| 17 | autodir |
| 18 | Path to the top level autotest dir (/usr/local/autotest) |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 19 | src_dir |
mbligh | 1e8858e | 2006-11-24 22:18:35 +0000 | [diff] [blame] | 20 | <tmp_dir>/src/ |
mbligh | c49af7b | 2006-11-24 04:02:21 +0000 | [diff] [blame] | 21 | build_dir |
mbligh | 1e8858e | 2006-11-24 22:18:35 +0000 | [diff] [blame] | 22 | <tmp_dir>/linux/ |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 23 | config_dir |
mbligh | 1e8858e | 2006-11-24 22:18:35 +0000 | [diff] [blame] | 24 | <results_dir>/config/ |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 25 | log_dir |
mbligh | 1e8858e | 2006-11-24 22:18:35 +0000 | [diff] [blame] | 26 | <results_dir>/debug/ |
| 27 | results_dir |
| 28 | <results_dir>/results/ |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 29 | """ |
| 30 | |
mbligh | 8baa2ea | 2006-12-17 23:01:24 +0000 | [diff] [blame] | 31 | autodir = '' |
| 32 | |
| 33 | def __init__(self, job, base_tree, results_dir, tmp_dir, build_dir, leave = False): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 34 | """Initialize the kernel build environment |
| 35 | |
| 36 | job |
| 37 | which job this build is part of |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 38 | base_tree |
mbligh | 534015f | 2006-09-15 03:28:56 +0000 | [diff] [blame] | 39 | base kernel tree. Can be one of the following: |
| 40 | 1. A local tarball |
| 41 | 2. A URL to a tarball |
| 42 | 3. A local directory (will symlink it) |
| 43 | 4. A shorthand expandable (eg '2.6.11-git3') |
mbligh | 1e8858e | 2006-11-24 22:18:35 +0000 | [diff] [blame] | 44 | results_dir |
| 45 | Results directory (holds config/, debug/, results/) |
| 46 | tmp_dir |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 47 | |
mbligh | 1e8858e | 2006-11-24 22:18:35 +0000 | [diff] [blame] | 48 | leave |
| 49 | Boolean, whether to leave existing tmpdir or not |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 50 | """ |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 51 | self.job = job |
mbligh | 678823f | 2006-12-07 18:49:00 +0000 | [diff] [blame] | 52 | self.autodir = job.autodir |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 53 | |
mbligh | 1e8858e | 2006-11-24 22:18:35 +0000 | [diff] [blame] | 54 | self.src_dir = os.path.join(tmp_dir, 'src') |
mbligh | 8baa2ea | 2006-12-17 23:01:24 +0000 | [diff] [blame] | 55 | self.build_dir = os.path.join(tmp_dir, build_dir) |
mbligh | a250805 | 2006-05-28 21:29:53 +0000 | [diff] [blame] | 56 | # created by get_kernel_tree |
mbligh | 1e8858e | 2006-11-24 22:18:35 +0000 | [diff] [blame] | 57 | self.config_dir = os.path.join(results_dir, 'config') |
| 58 | self.log_dir = os.path.join(results_dir, 'debug') |
| 59 | self.results_dir = os.path.join(results_dir, 'results') |
| 60 | |
| 61 | if not leave: |
| 62 | if os.path.isdir(self.src_dir): |
| 63 | system('rm -rf ' + self.src_dir) |
| 64 | if os.path.isdir(self.build_dir): |
| 65 | system('rm -rf ' + self.build_dir) |
| 66 | |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 67 | os.mkdir(self.src_dir) |
mbligh | 1e8858e | 2006-11-24 22:18:35 +0000 | [diff] [blame] | 68 | for path in [self.config_dir, self.log_dir, self.results_dir]: |
| 69 | if os.path.exists(path): |
| 70 | system('rm -rf ' + path) |
| 71 | os.mkdir(path) |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 72 | |
mbligh | 4426de0 | 2006-10-10 07:18:28 +0000 | [diff] [blame] | 73 | logpath = os.path.join(self.log_dir, 'build_log') |
| 74 | self.logfile = open(logpath, 'w+') |
| 75 | |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 76 | self.target_arch = None |
mbligh | fdbcaec | 2006-10-01 23:28:57 +0000 | [diff] [blame] | 77 | self.build_target = 'bzImage' |
mbligh | 8baa2ea | 2006-12-17 23:01:24 +0000 | [diff] [blame] | 78 | self.build_image = None |
mbligh | fdbcaec | 2006-10-01 23:28:57 +0000 | [diff] [blame] | 79 | |
mbligh | cac347a | 2007-06-02 17:21:48 +0000 | [diff] [blame] | 80 | if get_current_kernel_arch() == 'ia64': |
| 81 | self.build_target = 'all' |
| 82 | self.build_image = 'vmlinux.gz' |
| 83 | |
mbligh | fdbcaec | 2006-10-01 23:28:57 +0000 | [diff] [blame] | 84 | if leave: |
| 85 | return |
mbligh | 534015f | 2006-09-15 03:28:56 +0000 | [diff] [blame] | 86 | |
mbligh | 4426de0 | 2006-10-10 07:18:28 +0000 | [diff] [blame] | 87 | self.logfile.write('BASE: %s\n' % base_tree) |
apw | 2366d99 | 2007-03-12 20:35:57 +0000 | [diff] [blame] | 88 | |
| 89 | # Where we have direct version hint record that |
| 90 | # for later configuration selection. |
| 91 | shorthand = re.compile(r'^\d+\.\d+\.\d+') |
| 92 | if shorthand.match(base_tree): |
| 93 | self.base_tree_version = base_tree |
| 94 | else: |
| 95 | self.base_tree_version = None |
| 96 | |
mbligh | 534015f | 2006-09-15 03:28:56 +0000 | [diff] [blame] | 97 | if os.path.exists(base_tree): |
| 98 | self.get_kernel_tree(base_tree) |
| 99 | else: |
apw | 8ad56be | 2006-11-06 17:49:54 +0000 | [diff] [blame] | 100 | args = self.job.config_get('mirror.ftp_kernel_org') |
mbligh | 548f29a | 2006-10-17 04:55:12 +0000 | [diff] [blame] | 101 | if args: |
| 102 | args = '-l ' + args |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 103 | base_components = kernelexpand(base_tree, args) |
mbligh | 534015f | 2006-09-15 03:28:56 +0000 | [diff] [blame] | 104 | print 'kernelexpand: ' |
| 105 | print base_components |
| 106 | self.get_kernel_tree(base_components.pop(0)) |
| 107 | if base_components: # apply remaining patches |
| 108 | self.patch(*base_components) |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 109 | |
| 110 | |
mbligh | 5925e96 | 2007-08-30 17:05:22 +0000 | [diff] [blame] | 111 | def __record(self, fn, name, *args, **dargs): |
| 112 | try: |
| 113 | fn(*args, **dargs) |
| 114 | self.job.record("GOOD " + name + \ |
| 115 | " completed successfully\n") |
| 116 | except Exception, detail: |
| 117 | self.job.record("FAIL " + name + \ |
| 118 | detail.__str__() + "\n") |
| 119 | raise |
| 120 | |
| 121 | |
| 122 | def _patch(self, *patches): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 123 | """Apply a list of patches (in order)""" |
mbligh | 1e8858e | 2006-11-24 22:18:35 +0000 | [diff] [blame] | 124 | if not patches: |
| 125 | return |
mbligh | d016ecc | 2006-11-25 21:41:07 +0000 | [diff] [blame] | 126 | # if isinstance(patches, basestring): |
| 127 | # patches = [patches] |
mbligh | 534015f | 2006-09-15 03:28:56 +0000 | [diff] [blame] | 128 | print 'Applying patches: ', patches |
| 129 | # self.job.stdout.redirect(os.path.join(self.log_dir, 'stdout')) |
mbligh | 0763e73 | 2007-08-30 16:35:06 +0000 | [diff] [blame] | 130 | self.apply_patches(self.get_patches(patches)) |
mbligh | 534015f | 2006-09-15 03:28:56 +0000 | [diff] [blame] | 131 | # self.job.stdout.restore() |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 132 | |
| 133 | |
mbligh | 5925e96 | 2007-08-30 17:05:22 +0000 | [diff] [blame] | 134 | def patch(self, *args, **dargs): |
| 135 | self.__record(self._patch, "kernel.patch", *args, **dargs) |
| 136 | |
| 137 | |
| 138 | def _config(self, config_file = '', config_list = None, |
apw | b449c7d | 2006-12-05 12:21:44 +0000 | [diff] [blame] | 139 | defconfig = False): |
mbligh | 9305701 | 2006-08-06 15:51:56 +0000 | [diff] [blame] | 140 | self.job.stdout.redirect(os.path.join(self.log_dir, 'stdout')) |
mbligh | 678823f | 2006-12-07 18:49:00 +0000 | [diff] [blame] | 141 | self.set_cross_cc() |
apw | b449c7d | 2006-12-05 12:21:44 +0000 | [diff] [blame] | 142 | config = kernel_config.kernel_config(self.job, self.build_dir, |
apw | 2366d99 | 2007-03-12 20:35:57 +0000 | [diff] [blame] | 143 | self.config_dir, config_file, config_list, |
| 144 | defconfig, self.base_tree_version) |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 145 | self.job.stdout.restore() |
| 146 | |
| 147 | |
mbligh | 5925e96 | 2007-08-30 17:05:22 +0000 | [diff] [blame] | 148 | def config(self, *args, **dargs): |
| 149 | self.__record(self._config, "kernel.config", *args, **dargs) |
| 150 | |
| 151 | |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 152 | def get_patches(self, patches): |
mbligh | 1e8858e | 2006-11-24 22:18:35 +0000 | [diff] [blame] | 153 | """fetch the patches to the local src_dir""" |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 154 | local_patches = [] |
| 155 | for patch in patches: |
mbligh | 1e8858e | 2006-11-24 22:18:35 +0000 | [diff] [blame] | 156 | dest = os.path.join(self.src_dir, basename(patch)) |
mbligh | 0763e73 | 2007-08-30 16:35:06 +0000 | [diff] [blame] | 157 | # FIXME: this isn't unique. Append something to it |
| 158 | # like wget does if it's not there? |
mbligh | 1e8858e | 2006-11-24 22:18:35 +0000 | [diff] [blame] | 159 | print "get_file %s %s %s %s" % (patch, dest, self.src_dir, basename(patch)) |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 160 | get_file(patch, dest) |
mbligh | 0763e73 | 2007-08-30 16:35:06 +0000 | [diff] [blame] | 161 | # probably safer to use the command, not python library |
| 162 | md5sum = system_output('md5sum ' + dest).split()[0] |
| 163 | local_patches.append((patch, dest, md5sum)) |
mbligh | 534015f | 2006-09-15 03:28:56 +0000 | [diff] [blame] | 164 | return local_patches |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 165 | |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 166 | |
mbligh | 534015f | 2006-09-15 03:28:56 +0000 | [diff] [blame] | 167 | def apply_patches(self, local_patches): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 168 | """apply the list of patches, in order""" |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 169 | builddir = self.build_dir |
| 170 | os.chdir(builddir) |
| 171 | |
mbligh | 534015f | 2006-09-15 03:28:56 +0000 | [diff] [blame] | 172 | if not local_patches: |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 173 | return None |
mbligh | 0763e73 | 2007-08-30 16:35:06 +0000 | [diff] [blame] | 174 | for (spec, local, md5sum) in local_patches: |
| 175 | if local.endswith('.bz2') or local.endswith('.gz'): |
| 176 | ref = spec |
| 177 | else: |
| 178 | ref = force_copy(local, self.results_dir) |
| 179 | ref = self.job.relative_path(ref) |
| 180 | log = 'PATCH: %s %s %s\n' % (spec, ref, md5sum) |
| 181 | print log |
| 182 | cat_file_to_cmd(local, 'patch -p1 > /dev/null') |
| 183 | self.logfile.write(log) |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 184 | |
| 185 | |
| 186 | def get_kernel_tree(self, base_tree): |
mbligh | c49af7b | 2006-11-24 04:02:21 +0000 | [diff] [blame] | 187 | """Extract/link base_tree to self.build_dir""" |
mbligh | 5970cf0 | 2006-08-06 15:39:22 +0000 | [diff] [blame] | 188 | |
| 189 | # if base_tree is a dir, assume uncompressed kernel |
| 190 | if os.path.isdir(base_tree): |
| 191 | print 'Symlinking existing kernel source' |
mbligh | c49af7b | 2006-11-24 04:02:21 +0000 | [diff] [blame] | 192 | os.symlink(base_tree, self.build_dir) |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 193 | |
mbligh | 5970cf0 | 2006-08-06 15:39:22 +0000 | [diff] [blame] | 194 | # otherwise, extract tarball |
| 195 | else: |
mbligh | c49af7b | 2006-11-24 04:02:21 +0000 | [diff] [blame] | 196 | os.chdir(os.path.dirname(self.src_dir)) |
| 197 | # Figure out local destination for tarball |
| 198 | tarball = os.path.join(self.src_dir, os.path.basename(base_tree)) |
mbligh | 5970cf0 | 2006-08-06 15:39:22 +0000 | [diff] [blame] | 199 | get_file(base_tree, tarball) |
mbligh | 5970cf0 | 2006-08-06 15:39:22 +0000 | [diff] [blame] | 200 | print 'Extracting kernel tarball:', tarball, '...' |
mbligh | c49af7b | 2006-11-24 04:02:21 +0000 | [diff] [blame] | 201 | extract_tarball_to_dir(tarball, self.build_dir) |
mbligh | 5970cf0 | 2006-08-06 15:39:22 +0000 | [diff] [blame] | 202 | |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 203 | |
mbligh | fdbcaec | 2006-10-01 23:28:57 +0000 | [diff] [blame] | 204 | def extraversion(self, tag, append=1): |
| 205 | os.chdir(self.build_dir) |
mbligh | 4426de0 | 2006-10-10 07:18:28 +0000 | [diff] [blame] | 206 | extraversion_sub = r's/^EXTRAVERSION =\s*\(.*\)/EXTRAVERSION = ' |
mbligh | fdbcaec | 2006-10-01 23:28:57 +0000 | [diff] [blame] | 207 | if append: |
mbligh | 4426de0 | 2006-10-10 07:18:28 +0000 | [diff] [blame] | 208 | p = extraversion_sub + '\\1-%s/' % tag |
mbligh | fdbcaec | 2006-10-01 23:28:57 +0000 | [diff] [blame] | 209 | else: |
mbligh | 4426de0 | 2006-10-10 07:18:28 +0000 | [diff] [blame] | 210 | p = extraversion_sub + '-%s/' % tag |
mbligh | 4c3fe4a | 2007-07-31 17:59:21 +0000 | [diff] [blame] | 211 | system('mv Makefile Makefile.old') |
| 212 | system('sed "%s" < Makefile.old > Makefile' % p) |
mbligh | fdbcaec | 2006-10-01 23:28:57 +0000 | [diff] [blame] | 213 | |
| 214 | |
mbligh | 5925e96 | 2007-08-30 17:05:22 +0000 | [diff] [blame] | 215 | def _build(self, make_opts = '', logfile = '', extraversion='autotest'): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 216 | """build the kernel |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 217 | |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 218 | make_opts |
| 219 | additional options to make, if any |
| 220 | """ |
mbligh | 1003801 | 2006-10-19 03:32:45 +0000 | [diff] [blame] | 221 | os_dep.commands('gcc', 'make') |
mbligh | 6d4c941 | 2006-09-13 23:08:44 +0000 | [diff] [blame] | 222 | if logfile == '': |
| 223 | logfile = os.path.join(self.log_dir, 'kernel_build') |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 224 | os.chdir(self.build_dir) |
mbligh | fdbcaec | 2006-10-01 23:28:57 +0000 | [diff] [blame] | 225 | if extraversion: |
| 226 | self.extraversion(extraversion) |
mbligh | 9305701 | 2006-08-06 15:51:56 +0000 | [diff] [blame] | 227 | print os.path.join(self.log_dir, 'stdout') |
mbligh | 6d4c941 | 2006-09-13 23:08:44 +0000 | [diff] [blame] | 228 | self.job.stdout.redirect(logfile + '.stdout') |
| 229 | self.job.stderr.redirect(logfile + '.stderr') |
mbligh | a250805 | 2006-05-28 21:29:53 +0000 | [diff] [blame] | 230 | self.set_cross_cc() |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 231 | # setup_config_file(config_file, config_overrides) |
apw | c784610 | 2006-04-06 18:22:13 +0000 | [diff] [blame] | 232 | |
| 233 | # Not needed on 2.6, but hard to tell -- handle failure |
mbligh | fdbcaec | 2006-10-01 23:28:57 +0000 | [diff] [blame] | 234 | system('make dep', ignorestatus=1) |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 235 | threads = 2 * count_cpus() |
mbligh | 6d4c941 | 2006-09-13 23:08:44 +0000 | [diff] [blame] | 236 | build_string = 'make -j %d %s %s' % (threads, make_opts, |
| 237 | self.build_target) |
| 238 | # eg make bzImage, or make zImage |
| 239 | print build_string |
| 240 | system(build_string) |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 241 | if kernel_config.modules_needed('.config'): |
apw | d470197 | 2006-10-16 09:24:56 +0000 | [diff] [blame] | 242 | system('make -j %d modules' % (threads)) |
apw | c784610 | 2006-04-06 18:22:13 +0000 | [diff] [blame] | 243 | |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 244 | self.job.stdout.restore() |
| 245 | self.job.stderr.restore() |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 246 | |
mbligh | 4426de0 | 2006-10-10 07:18:28 +0000 | [diff] [blame] | 247 | kernel_version = self.get_kernel_build_ver() |
| 248 | kernel_version = re.sub('-autotest', '', kernel_version) |
| 249 | self.logfile.write('BUILD VERSION: %s\n' % kernel_version) |
mbligh | 1e8858e | 2006-11-24 22:18:35 +0000 | [diff] [blame] | 250 | |
| 251 | force_copy(self.build_dir+'/System.map', self.results_dir) |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 252 | |
| 253 | |
mbligh | 5925e96 | 2007-08-30 17:05:22 +0000 | [diff] [blame] | 254 | def build(self, *args, **dargs): |
| 255 | self.__record(self._build, "kernel.build", *args, **dargs) |
| 256 | |
| 257 | |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 258 | def build_timed(self, threads, timefile = '/dev/null', make_opts = ''): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 259 | """time the bulding of the kernel""" |
mbligh | b8a14e3 | 2006-05-06 00:17:35 +0000 | [diff] [blame] | 260 | os.chdir(self.build_dir) |
mbligh | 678823f | 2006-12-07 18:49:00 +0000 | [diff] [blame] | 261 | self.set_cross_cc() |
mbligh | b8a14e3 | 2006-05-06 00:17:35 +0000 | [diff] [blame] | 262 | print "make clean" |
| 263 | system('make clean') |
mbligh | a2bb9d6 | 2006-10-09 16:26:03 +0000 | [diff] [blame] | 264 | build_string = "/usr/bin/time -o %s make %s -j %s vmlinux" % (timefile, make_opts, threads) |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 265 | print build_string |
apw | c784610 | 2006-04-06 18:22:13 +0000 | [diff] [blame] | 266 | system(build_string) |
| 267 | if (not os.path.isfile('vmlinux')): |
| 268 | raise TestError("no vmlinux found, kernel build failed") |
mbligh | b8a14e3 | 2006-05-06 00:17:35 +0000 | [diff] [blame] | 269 | |
| 270 | |
mbligh | 5925e96 | 2007-08-30 17:05:22 +0000 | [diff] [blame] | 271 | def _clean(self): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 272 | """make clean in the kernel tree""" |
mbligh | b8a14e3 | 2006-05-06 00:17:35 +0000 | [diff] [blame] | 273 | os.chdir(self.build_dir) |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 274 | print "make clean" |
apw | c784610 | 2006-04-06 18:22:13 +0000 | [diff] [blame] | 275 | system('make clean') |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 276 | |
mbligh | 50f42ea | 2006-09-30 22:22:21 +0000 | [diff] [blame] | 277 | |
mbligh | 5925e96 | 2007-08-30 17:05:22 +0000 | [diff] [blame] | 278 | def clean(self, *args, **dargs): |
| 279 | self.__record(self._clean, "kernel.clean", *args, **dargs) |
| 280 | |
| 281 | |
| 282 | def _mkinitrd(self, version, image, system_map, initrd): |
mbligh | 50f42ea | 2006-09-30 22:22:21 +0000 | [diff] [blame] | 283 | """Build kernel initrd image. |
| 284 | Try to use distro specific way to build initrd image. |
| 285 | Parameters: |
| 286 | version |
| 287 | new kernel version |
| 288 | image |
| 289 | new kernel image file |
| 290 | system_map |
| 291 | System.map file |
| 292 | initrd |
| 293 | initrd image file to build |
| 294 | """ |
| 295 | vendor = get_os_vendor() |
| 296 | |
| 297 | if os.path.isfile(initrd): |
mbligh | fdbcaec | 2006-10-01 23:28:57 +0000 | [diff] [blame] | 298 | print "Existing %s file, will remove it." % initrd |
mbligh | 50f42ea | 2006-09-30 22:22:21 +0000 | [diff] [blame] | 299 | os.remove(initrd) |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 300 | |
mbligh | 50f42ea | 2006-09-30 22:22:21 +0000 | [diff] [blame] | 301 | if vendor in ['Red Hat', 'Fedora Core']: |
mbligh | fdbcaec | 2006-10-01 23:28:57 +0000 | [diff] [blame] | 302 | system('mkinitrd %s %s' % (initrd, version)) |
mbligh | 50f42ea | 2006-09-30 22:22:21 +0000 | [diff] [blame] | 303 | elif vendor in ['SUSE']: |
mbligh | fdbcaec | 2006-10-01 23:28:57 +0000 | [diff] [blame] | 304 | system('mkinitrd -k %s -i %s -M %s' % (image, initrd, system_map)) |
apw | c898a1f | 2007-02-28 15:27:22 +0000 | [diff] [blame] | 305 | elif vendor in ['Debian', 'Ubuntu']: |
| 306 | if os.path.isfile('/usr/sbin/mkinitrd'): |
| 307 | cmd = '/usr/sbin/mkinitrd' |
| 308 | elif os.path.isfile('/usr/sbin/mkinitramfs'): |
| 309 | cmd = '/usr/sbin/mkinitramfs' |
| 310 | else: |
| 311 | raise TestError('No Debian initrd builder') |
| 312 | system('%s -o %s %s' % (cmd, initrd, version)) |
mbligh | 50f42ea | 2006-09-30 22:22:21 +0000 | [diff] [blame] | 313 | else: |
mbligh | fdbcaec | 2006-10-01 23:28:57 +0000 | [diff] [blame] | 314 | raise TestError('Unsupported vendor %s' % vendor) |
mbligh | 50f42ea | 2006-09-30 22:22:21 +0000 | [diff] [blame] | 315 | |
| 316 | |
mbligh | 5925e96 | 2007-08-30 17:05:22 +0000 | [diff] [blame] | 317 | def mkinitrd(self, *args, **dargs): |
| 318 | self.__record(self._mkinitrd, "kernel.mkinitrd", *args, **dargs) |
| 319 | |
| 320 | |
mbligh | 8baa2ea | 2006-12-17 23:01:24 +0000 | [diff] [blame] | 321 | def set_build_image(self, image): |
| 322 | self.build_image = image |
| 323 | |
| 324 | |
mbligh | 5925e96 | 2007-08-30 17:05:22 +0000 | [diff] [blame] | 325 | def _install(self, tag='autotest', prefix = '/'): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 326 | """make install in the kernel tree""" |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 327 | os.chdir(self.build_dir) |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 328 | |
mbligh | 6a1d4db | 2006-10-06 04:30:16 +0000 | [diff] [blame] | 329 | if not os.path.isdir(prefix): |
| 330 | os.mkdir(prefix) |
mbligh | a87116f | 2006-10-10 02:47:08 +0000 | [diff] [blame] | 331 | self.boot_dir = os.path.join(prefix, 'boot') |
| 332 | if not os.path.isdir(self.boot_dir): |
| 333 | os.mkdir(self.boot_dir) |
mbligh | 0ad6558 | 2006-10-06 04:16:36 +0000 | [diff] [blame] | 334 | |
mbligh | 8baa2ea | 2006-12-17 23:01:24 +0000 | [diff] [blame] | 335 | if not self.build_image: |
| 336 | images = glob.glob('arch/*/boot/' + self.build_target) |
| 337 | if len(images): |
| 338 | self.build_image = images[0] |
| 339 | else: |
| 340 | self.build_image = self.build_target |
mbligh | a87116f | 2006-10-10 02:47:08 +0000 | [diff] [blame] | 341 | |
| 342 | # remember installed files |
mbligh | a87116f | 2006-10-10 02:47:08 +0000 | [diff] [blame] | 343 | self.vmlinux = self.boot_dir + '/vmlinux-' + tag |
mbligh | 8baa2ea | 2006-12-17 23:01:24 +0000 | [diff] [blame] | 344 | if (self.build_image != 'vmlinux'): |
apw | 9a61c5b | 2006-11-28 10:03:15 +0000 | [diff] [blame] | 345 | self.image = self.boot_dir + '/vmlinuz-' + tag |
| 346 | else: |
| 347 | self.image = self.vmlinux |
mbligh | a87116f | 2006-10-10 02:47:08 +0000 | [diff] [blame] | 348 | self.system_map = self.boot_dir + '/System.map-' + tag |
| 349 | self.config = self.boot_dir + '/config-' + tag |
| 350 | self.initrd = '' |
| 351 | |
| 352 | # copy to boot dir |
mbligh | a87116f | 2006-10-10 02:47:08 +0000 | [diff] [blame] | 353 | force_copy('vmlinux', self.vmlinux) |
mbligh | 8baa2ea | 2006-12-17 23:01:24 +0000 | [diff] [blame] | 354 | if (self.build_image != 'vmlinux'): |
| 355 | force_copy(self.build_image, self.image) |
mbligh | a87116f | 2006-10-10 02:47:08 +0000 | [diff] [blame] | 356 | force_copy('System.map', self.system_map) |
| 357 | force_copy('.config', self.config) |
| 358 | |
mbligh | 6a1d4db | 2006-10-06 04:30:16 +0000 | [diff] [blame] | 359 | if not kernel_config.modules_needed('.config'): |
| 360 | return |
| 361 | |
| 362 | system('make modules_install INSTALL_MOD_PATH=%s' % prefix) |
| 363 | if prefix == '/': |
mbligh | a87116f | 2006-10-10 02:47:08 +0000 | [diff] [blame] | 364 | self.initrd = self.boot_dir + '/initrd-' + tag |
| 365 | self.mkinitrd(self.get_kernel_build_ver(), self.image, \ |
| 366 | self.system_map, self.initrd) |
| 367 | |
| 368 | |
mbligh | 5925e96 | 2007-08-30 17:05:22 +0000 | [diff] [blame] | 369 | def install(self, *args, **dargs): |
| 370 | self.__record(self._install, "kernel.install", *args, **dargs) |
| 371 | |
| 372 | |
mbligh | a87116f | 2006-10-10 02:47:08 +0000 | [diff] [blame] | 373 | def add_to_bootloader(self, tag='autotest', args=''): |
| 374 | """ add this kernel to bootloader, taking an |
| 375 | optional parameter of space separated parameters |
| 376 | e.g.: kernel.add_to_bootloader('mykernel', 'ro acpi=off') |
| 377 | """ |
| 378 | |
| 379 | # remove existing entry if present |
| 380 | self.job.bootloader.remove_kernel(tag) |
| 381 | |
apw | cbe3257 | 2006-11-28 10:00:23 +0000 | [diff] [blame] | 382 | # pull the base argument set from the job config, |
apw | cbe3257 | 2006-11-28 10:00:23 +0000 | [diff] [blame] | 383 | baseargs = self.job.config_get('boot.default_args') |
mbligh | 78bc05e | 2006-12-25 02:29:59 +0000 | [diff] [blame] | 384 | if baseargs: |
| 385 | args = baseargs + " " + args |
| 386 | |
| 387 | # otherwise populate from /proc/cmdline |
| 388 | # if not baseargs: |
| 389 | # baseargs = open('/proc/cmdline', 'r').readline().strip() |
| 390 | # NOTE: This is unnecessary, because boottool does it. |
apw | cbe3257 | 2006-11-28 10:00:23 +0000 | [diff] [blame] | 391 | |
mbligh | 78bc05e | 2006-12-25 02:29:59 +0000 | [diff] [blame] | 392 | root = None |
| 393 | roots = [x for x in args.split() if x.startswith('root=')] |
| 394 | if roots: |
| 395 | root = re.sub('^root=', '', roots[0]) |
| 396 | arglist = [x for x in args.split() if not x.startswith('root=')] |
| 397 | args = ' '.join(arglist) |
mbligh | a87116f | 2006-10-10 02:47:08 +0000 | [diff] [blame] | 398 | |
mbligh | 78bc05e | 2006-12-25 02:29:59 +0000 | [diff] [blame] | 399 | # add the kernel entry |
| 400 | # add_kernel(image, title='autotest', initrd='') |
| 401 | self.job.bootloader.add_kernel(self.image, tag, self.initrd, \ |
| 402 | args = args, root = root) |
mbligh | 6a1d4db | 2006-10-06 04:30:16 +0000 | [diff] [blame] | 403 | |
mbligh | 201aa89 | 2006-10-29 04:02:05 +0000 | [diff] [blame] | 404 | |
mbligh | 548f29a | 2006-10-17 04:55:12 +0000 | [diff] [blame] | 405 | def get_kernel_build_arch(self, arch=None): |
mbligh | 201aa89 | 2006-10-29 04:02:05 +0000 | [diff] [blame] | 406 | """ |
| 407 | Work out the current kernel architecture (as a kernel arch) |
| 408 | """ |
mbligh | 548f29a | 2006-10-17 04:55:12 +0000 | [diff] [blame] | 409 | if not arch: |
| 410 | arch = get_current_kernel_arch() |
| 411 | if re.match('i.86', arch): |
| 412 | return 'i386' |
| 413 | elif re.match('sun4u', arch): |
| 414 | return 'sparc64' |
| 415 | elif re.match('arm.*', arch): |
| 416 | return 'arm' |
| 417 | elif re.match('sa110', arch): |
| 418 | return 'arm' |
| 419 | elif re.match('s390x', arch): |
| 420 | return 's390' |
| 421 | elif re.match('parisc64', arch): |
| 422 | return 'parisc' |
| 423 | elif re.match('ppc.*', arch): |
| 424 | return 'powerpc' |
| 425 | elif re.match('mips.*', arch): |
| 426 | return 'mips' |
| 427 | else: |
| 428 | return arch |
| 429 | |
mbligh | 6a1d4db | 2006-10-06 04:30:16 +0000 | [diff] [blame] | 430 | |
mbligh | da76b21 | 2007-07-19 16:50:41 +0000 | [diff] [blame] | 431 | def boot(self, args='', once=True): |
apw | 1b5dc36 | 2006-10-31 11:24:26 +0000 | [diff] [blame] | 432 | """ install and boot this kernel, do not care how |
| 433 | just make it happen. |
| 434 | """ |
| 435 | |
| 436 | # Install this kernel. |
| 437 | self.install() |
mbligh | da76b21 | 2007-07-19 16:50:41 +0000 | [diff] [blame] | 438 | self.add_to_bootloader(args=args, tag='autotest') |
| 439 | if once: |
| 440 | self.job.bootloader.boot_once('autotest') |
apw | 1b5dc36 | 2006-10-31 11:24:26 +0000 | [diff] [blame] | 441 | |
| 442 | # Boot it. |
| 443 | self.job.reboot() |
| 444 | |
| 445 | |
mbligh | fdbcaec | 2006-10-01 23:28:57 +0000 | [diff] [blame] | 446 | def get_kernel_build_ver(self): |
mbligh | e11f5fc | 2006-10-04 04:42:22 +0000 | [diff] [blame] | 447 | """Check Makefile and .config to return kernel version""" |
| 448 | version = patchlevel = sublevel = extraversion = localversion = '' |
| 449 | |
| 450 | for line in open(self.build_dir + '/Makefile', 'r').readlines(): |
| 451 | if line.startswith('VERSION'): |
| 452 | version = line[line.index('=') + 1:].strip() |
| 453 | if line.startswith('PATCHLEVEL'): |
| 454 | patchlevel = line[line.index('=') + 1:].strip() |
| 455 | if line.startswith('SUBLEVEL'): |
| 456 | sublevel = line[line.index('=') + 1:].strip() |
| 457 | if line.startswith('EXTRAVERSION'): |
| 458 | extraversion = line[line.index('=') + 1:].strip() |
| 459 | |
| 460 | for line in open(self.build_dir + '/.config', 'r').readlines(): |
| 461 | if line.startswith('CONFIG_LOCALVERSION='): |
| 462 | localversion = line.rstrip().split('"')[1] |
| 463 | |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 464 | return "%s.%s.%s%s%s" %(version, patchlevel, sublevel, extraversion, localversion) |
mbligh | fdbcaec | 2006-10-01 23:28:57 +0000 | [diff] [blame] | 465 | |
| 466 | |
mbligh | 8baa2ea | 2006-12-17 23:01:24 +0000 | [diff] [blame] | 467 | def set_build_target(self, build_target): |
| 468 | if build_target: |
| 469 | self.build_target = build_target |
| 470 | print 'BUILD TARGET: %s' % self.build_target |
| 471 | |
| 472 | |
mbligh | 5970cf0 | 2006-08-06 15:39:22 +0000 | [diff] [blame] | 473 | def set_cross_cc(self, target_arch=None, cross_compile=None, |
| 474 | build_target='bzImage'): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 475 | """Set up to cross-compile. |
mbligh | cc2e666 | 2006-09-14 01:24:07 +0000 | [diff] [blame] | 476 | This is broken. We need to work out what the default |
| 477 | compile produces, and if not, THEN set the cross |
| 478 | compiler. |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 479 | """ |
mbligh | cc2e666 | 2006-09-14 01:24:07 +0000 | [diff] [blame] | 480 | |
mbligh | 5970cf0 | 2006-08-06 15:39:22 +0000 | [diff] [blame] | 481 | if self.target_arch: |
| 482 | return |
mbligh | 678823f | 2006-12-07 18:49:00 +0000 | [diff] [blame] | 483 | |
mbligh | 8baa2ea | 2006-12-17 23:01:24 +0000 | [diff] [blame] | 484 | # if someone has set build_target, don't clobber in set_cross_cc |
| 485 | # run set_build_target before calling set_cross_cc |
| 486 | if not self.build_target: |
| 487 | self.set_build_target(build_target) |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 488 | |
mbligh | 5970cf0 | 2006-08-06 15:39:22 +0000 | [diff] [blame] | 489 | # If no 'target_arch' given assume native compilation |
| 490 | if target_arch == None: |
mbligh | 548f29a | 2006-10-17 04:55:12 +0000 | [diff] [blame] | 491 | target_arch = get_current_kernel_arch() |
mbligh | 5970cf0 | 2006-08-06 15:39:22 +0000 | [diff] [blame] | 492 | if target_arch == 'ppc64': |
mbligh | 5970cf0 | 2006-08-06 15:39:22 +0000 | [diff] [blame] | 493 | if self.build_target == 'bzImage': |
apw | 9a61c5b | 2006-11-28 10:03:15 +0000 | [diff] [blame] | 494 | self.build_target = 'vmlinux' |
mbligh | 678823f | 2006-12-07 18:49:00 +0000 | [diff] [blame] | 495 | |
| 496 | if not cross_compile: |
| 497 | cross_compile = self.job.config_get('kernel.cross_cc') |
| 498 | |
| 499 | if cross_compile: |
| 500 | os.environ['CROSS_COMPILE'] = cross_compile |
| 501 | else: |
| 502 | if os.environ.has_key('CROSS_COMPILE'): |
| 503 | del os.environ['CROSS_COMPILE'] |
| 504 | |
mbligh | cc2e666 | 2006-09-14 01:24:07 +0000 | [diff] [blame] | 505 | return # HACK. Crap out for now. |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 506 | |
mbligh | cc2e666 | 2006-09-14 01:24:07 +0000 | [diff] [blame] | 507 | # At this point I know what arch I *want* to build for |
| 508 | # but have no way of working out what arch the default |
| 509 | # compiler DOES build for. |
| 510 | |
| 511 | # Oh, and BTW, install_package() doesn't exist yet. |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 512 | |
mbligh | cc2e666 | 2006-09-14 01:24:07 +0000 | [diff] [blame] | 513 | if target_arch == 'ppc64': |
| 514 | install_package('ppc64-cross') |
mbligh | 678823f | 2006-12-07 18:49:00 +0000 | [diff] [blame] | 515 | cross_compile = os.path.join(self.autodir, 'sources/ppc64-cross/bin') |
mbligh | cc2e666 | 2006-09-14 01:24:07 +0000 | [diff] [blame] | 516 | |
| 517 | elif target_arch == 'x86_64': |
| 518 | install_package('x86_64-cross') |
mbligh | 678823f | 2006-12-07 18:49:00 +0000 | [diff] [blame] | 519 | cross_compile = os.path.join(self.autodir, 'sources/x86_64-cross/bin') |
mbligh | b8a14e3 | 2006-05-06 00:17:35 +0000 | [diff] [blame] | 520 | |
mbligh | 5970cf0 | 2006-08-06 15:39:22 +0000 | [diff] [blame] | 521 | os.environ['ARCH'] = self.target_arch = target_arch |
| 522 | |
| 523 | self.cross_compile = cross_compile |
| 524 | if self.cross_compile: |
| 525 | os.environ['CROSS_COMPILE'] = self.cross_compile |
mbligh | cc2e666 | 2006-09-14 01:24:07 +0000 | [diff] [blame] | 526 | |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 527 | |
mbligh | b8a14e3 | 2006-05-06 00:17:35 +0000 | [diff] [blame] | 528 | def pickle_dump(self, filename): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 529 | """dump a pickle of ourself out to the specified filename |
| 530 | |
| 531 | we can't pickle the backreference to job (it contains fd's), |
mbligh | 709bb9b | 2006-10-12 04:32:16 +0000 | [diff] [blame] | 532 | nor would we want to. Same for logfile (fd's). |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 533 | """ |
mbligh | b8a14e3 | 2006-05-06 00:17:35 +0000 | [diff] [blame] | 534 | temp = copy.copy(self) |
| 535 | temp.job = None |
mbligh | 709bb9b | 2006-10-12 04:32:16 +0000 | [diff] [blame] | 536 | temp.logfile = None |
mbligh | b8a14e3 | 2006-05-06 00:17:35 +0000 | [diff] [blame] | 537 | pickle.dump(temp, open(filename, 'w')) |