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