blob: 6371b51328d0ea4e15655cf03e9d6ec286e3ab61 [file] [log] [blame]
mbligh8baa2ea2006-12-17 23:01:24 +00001__author__ = """Copyright Martin J. Bligh, 2006,
2 Copyright IBM Corp. 2006, Ryan Harper <ryanh@us.ibm.com>"""
3
mbligh02ff2d52008-06-03 15:00:21 +00004import os, shutil, copy, pickle, re, glob
mblighc61fb362008-06-05 16:22:15 +00005from autotest_lib.client.bin import kernel, kernel_config, os_dep, test
mbligh53da18e2009-01-05 21:13:26 +00006from autotest_lib.client.bin import utils
mbligh8baa2ea2006-12-17 23:01:24 +00007
8
9class xen(kernel.kernel):
10
jadmanski0afbb632008-06-06 21:10:57 +000011 def log(self, msg):
12 print msg
13 self.logfile.write('%s\n' % msg)
mbligh8baa2ea2006-12-17 23:01:24 +000014
15
mbligh8b352852008-06-07 01:07:08 +000016 def __init__(self, job, base_tree, results_dir, tmp_dir, build_dir,
17 leave = False, kjob = None):
jadmanski0afbb632008-06-06 21:10:57 +000018 # call base-class
mbligh8b352852008-06-07 01:07:08 +000019 kernel.kernel.__init__(self, job, base_tree, results_dir,
jadmanski0afbb632008-06-06 21:10:57 +000020 tmp_dir, build_dir, leave)
21 self.kjob = kjob
mbligh8baa2ea2006-12-17 23:01:24 +000022
23
jadmanski0afbb632008-06-06 21:10:57 +000024 def config(self, config_file, config_list = None):
25 raise NotImplementedError('config() not implemented for xen')
mbligh8baa2ea2006-12-17 23:01:24 +000026
27
jadmanski0afbb632008-06-06 21:10:57 +000028 def build(self, make_opts = '', logfile = '', extraversion='autotest'):
29 """build xen
mbligh8baa2ea2006-12-17 23:01:24 +000030
jadmanski0afbb632008-06-06 21:10:57 +000031 make_opts
32 additional options to make, if any
33 """
34 self.log('running build')
35 os_dep.commands('gcc', 'make')
36 # build xen with extraversion flag
37 os.environ['XEN_EXTRAVERSION'] = '-unstable-%s'% extraversion
38 if logfile == '':
39 logfile = os.path.join(self.log_dir, 'xen_build')
40 os.chdir(self.build_dir)
41 self.log('log_dir: %s ' % os.path.join(self.log_dir, 'stdout'))
42 self.job.stdout.tee_redirect(logfile + '.stdout')
43 self.job.stderr.tee_redirect(logfile + '.stderr')
mbligh8baa2ea2006-12-17 23:01:24 +000044
jadmanski0afbb632008-06-06 21:10:57 +000045 # build xen hypervisor and user-space tools
46 targets = ['xen', 'tools']
mbligh53da18e2009-01-05 21:13:26 +000047 threads = 2 * utils.count_cpus()
jadmanski0afbb632008-06-06 21:10:57 +000048 for t in targets:
49 build_string = 'make -j %d %s %s' % (threads, make_opts, t)
50 self.log('build_string: %s' % build_string)
mbligh23955fc2008-06-12 17:41:18 +000051 utils.system(build_string)
mbligh8baa2ea2006-12-17 23:01:24 +000052
jadmanski0afbb632008-06-06 21:10:57 +000053 # make a kernel job out of the kernel from the xen src if one isn't provided
mblighd876f452008-12-03 15:09:17 +000054 if self.kjob is None:
jadmanski0afbb632008-06-06 21:10:57 +000055 # get xen kernel tree ready
56 self.log("prep-ing xen'ified kernel source tree")
mbligh23955fc2008-06-12 17:41:18 +000057 utils.system('make prep-kernels')
mbligh8baa2ea2006-12-17 23:01:24 +000058
jadmanski0afbb632008-06-06 21:10:57 +000059 v = self.get_xen_kernel_build_ver()
60 self.log('building xen kernel version: %s' % v)
mbligh8baa2ea2006-12-17 23:01:24 +000061
jadmanski0afbb632008-06-06 21:10:57 +000062 # build xen-ified kernel in xen tree
63 kernel_base_tree = os.path.join(self.build_dir, \
64 'linux-%s' % self.get_xen_kernel_build_ver())
mbligh8baa2ea2006-12-17 23:01:24 +000065
jadmanski0afbb632008-06-06 21:10:57 +000066 self.log('kernel_base_tree = %s' % kernel_base_tree)
67 # fix up XENGUEST value in EXTRAVERSION; we can't have
68 # files with '$(XENGEUST)' in the name, =(
69 self.fix_up_xen_kernel_makefile(kernel_base_tree)
mbligh8baa2ea2006-12-17 23:01:24 +000070
jadmanski0afbb632008-06-06 21:10:57 +000071 # make the kernel job
72 self.kjob = self.job.kernel(kernel_base_tree)
mbligh8baa2ea2006-12-17 23:01:24 +000073
jadmanski0afbb632008-06-06 21:10:57 +000074 # hardcoding dom0 config (no modules for testing, yay!)
75 # FIXME: probe host to determine which config to pick
76 c = self.build_dir + '/buildconfigs/linux-defconfig_xen0_x86_32'
77 self.log('using kernel config: %s ' % c)
78 self.kjob.config(c)
mbligh8baa2ea2006-12-17 23:01:24 +000079
jadmanski0afbb632008-06-06 21:10:57 +000080 # Xen's kernel tree sucks; doesn't use bzImage, but vmlinux
81 self.kjob.set_build_target('vmlinuz')
mbligh8baa2ea2006-12-17 23:01:24 +000082
jadmanski0afbb632008-06-06 21:10:57 +000083 # also, the vmlinuz is not out in arch/*/boot, ARGH! more hackery
84 self.kjob.set_build_image(self.job.tmpdir + '/build/linux/vmlinuz')
mbligh8baa2ea2006-12-17 23:01:24 +000085
jadmanski0afbb632008-06-06 21:10:57 +000086 self.kjob.build()
mbligh8baa2ea2006-12-17 23:01:24 +000087
jadmanski0afbb632008-06-06 21:10:57 +000088 self.job.stdout.restore()
89 self.job.stderr.restore()
mbligh8baa2ea2006-12-17 23:01:24 +000090
jadmanski0afbb632008-06-06 21:10:57 +000091 xen_version = self.get_xen_build_ver()
92 self.log('BUILD VERSION: Xen: %s Kernel:%s' % \
93 (xen_version, self.kjob.get_kernel_build_ver()))
mbligh8baa2ea2006-12-17 23:01:24 +000094
95
jadmanski0afbb632008-06-06 21:10:57 +000096 def build_timed(self, *args, **kwds):
97 raise NotImplementedError('build_timed() not implemented')
mbligh8baa2ea2006-12-17 23:01:24 +000098
99
jadmanski0afbb632008-06-06 21:10:57 +0000100 def install(self, tag='', prefix = '/', extraversion='autotest'):
101 """make install in the kernel tree"""
102 self.log('Installing ...')
mbligh8baa2ea2006-12-17 23:01:24 +0000103
jadmanski0afbb632008-06-06 21:10:57 +0000104 os.chdir(self.build_dir)
mbligh8baa2ea2006-12-17 23:01:24 +0000105
jadmanski0afbb632008-06-06 21:10:57 +0000106 if not os.path.isdir(prefix):
107 os.mkdir(prefix)
108 self.boot_dir = os.path.join(prefix, 'boot')
109 if not os.path.isdir(self.boot_dir):
110 os.mkdir(self.boot_dir)
mbligh8baa2ea2006-12-17 23:01:24 +0000111
jadmanski0afbb632008-06-06 21:10:57 +0000112 # remember what we are going to install
113 xen_version = '%s-%s' % (self.get_xen_build_ver(), extraversion)
114 self.xen_image = self.boot_dir + '/xen-' + xen_version + '.gz'
115 self.xen_syms = self.boot_dir + '/xen-syms-' + xen_version
mbligh8baa2ea2006-12-17 23:01:24 +0000116
jadmanski0afbb632008-06-06 21:10:57 +0000117 self.log('Installing Xen ...')
118 os.environ['XEN_EXTRAVERSION'] = '-unstable-%s'% extraversion
mbligh8baa2ea2006-12-17 23:01:24 +0000119
jadmanski0afbb632008-06-06 21:10:57 +0000120 # install xen
mbligh23955fc2008-06-12 17:41:18 +0000121 utils.system('make DESTDIR=%s -C xen install' % prefix)
mbligh8baa2ea2006-12-17 23:01:24 +0000122
jadmanski0afbb632008-06-06 21:10:57 +0000123 # install tools
mbligh23955fc2008-06-12 17:41:18 +0000124 utils.system('make DESTDIR=%s -C tools install' % prefix)
mbligh8baa2ea2006-12-17 23:01:24 +0000125
jadmanski0afbb632008-06-06 21:10:57 +0000126 # install kernel
127 ktag = self.kjob.get_kernel_build_ver()
128 kprefix = prefix
129 self.kjob.install(tag=ktag, prefix=kprefix)
mbligh8baa2ea2006-12-17 23:01:24 +0000130
131
jadmanski0afbb632008-06-06 21:10:57 +0000132 def add_to_bootloader(self, tag='autotest', args=''):
133 """ add this kernel to bootloader, taking an
134 optional parameter of space separated parameters
135 e.g.: kernel.add_to_bootloader('mykernel', 'ro acpi=off')
136 """
mbligh8baa2ea2006-12-17 23:01:24 +0000137
jadmanski0afbb632008-06-06 21:10:57 +0000138 # turn on xen mode
139 self.job.bootloader.enable_xen_mode()
mbligh8baa2ea2006-12-17 23:01:24 +0000140
jadmanski0afbb632008-06-06 21:10:57 +0000141 # remove existing entry if present
142 self.job.bootloader.remove_kernel(tag)
mbligh8baa2ea2006-12-17 23:01:24 +0000143
jadmanski0afbb632008-06-06 21:10:57 +0000144 # add xen and xen kernel
mbligh8b352852008-06-07 01:07:08 +0000145 self.job.bootloader.add_kernel(self.kjob.image, tag,
146 self.kjob.initrd, self.xen_image)
mbligh8baa2ea2006-12-17 23:01:24 +0000147
jadmanski0afbb632008-06-06 21:10:57 +0000148 # if no args passed, populate from /proc/cmdline
149 if not args:
150 args = open('/proc/cmdline', 'r').readline().strip()
mbligh8baa2ea2006-12-17 23:01:24 +0000151
jadmanski0afbb632008-06-06 21:10:57 +0000152 # add args to entry one at a time
153 for a in args.split(' '):
154 self.job.bootloader.add_args(tag, a)
mbligh8baa2ea2006-12-17 23:01:24 +0000155
jadmanski0afbb632008-06-06 21:10:57 +0000156 # turn off xen mode
157 self.job.bootloader.disable_xen_mode()
mbligh8baa2ea2006-12-17 23:01:24 +0000158
159
jadmanski0afbb632008-06-06 21:10:57 +0000160 def get_xen_kernel_build_ver(self):
161 """Check xen buildconfig for current kernel version"""
162 version = patchlevel = sublevel = ''
163 extraversion = localversion = ''
mblighf25cde12006-12-17 23:13:48 +0000164
jadmanski0afbb632008-06-06 21:10:57 +0000165 version_file = self.build_dir + '/buildconfigs/mk.linux-2.6-xen'
mbligh8baa2ea2006-12-17 23:01:24 +0000166
jadmanski0afbb632008-06-06 21:10:57 +0000167 for line in open(version_file, 'r').readlines():
168 if line.startswith('LINUX_VER'):
169 start = line.index('=') + 1
170 version = line[start:].strip() + "-xen"
171 break
mbligh8baa2ea2006-12-17 23:01:24 +0000172
jadmanski0afbb632008-06-06 21:10:57 +0000173 return version
mbligh8baa2ea2006-12-17 23:01:24 +0000174
175
jadmanski0afbb632008-06-06 21:10:57 +0000176 def fix_up_xen_kernel_makefile(self, kernel_dir):
177 """Fix up broken EXTRAVERSION in xen-ified Linux kernel Makefile"""
178 xenguest = ''
179 makefile = kernel_dir + '/Makefile'
mbligh8baa2ea2006-12-17 23:01:24 +0000180
jadmanski0afbb632008-06-06 21:10:57 +0000181 for line in open(makefile, 'r').readlines():
182 if line.startswith('XENGUEST'):
183 start = line.index('=') + 1
184 xenguest = line[start:].strip()
185 break;
mbligh8baa2ea2006-12-17 23:01:24 +0000186
jadmanski0afbb632008-06-06 21:10:57 +0000187 # change out $XENGUEST in EXTRAVERSION line
mbligh23955fc2008-06-12 17:41:18 +0000188 utils.system('sed -i.old "s,\$(XENGUEST),%s," %s' % (xenguest,
189 makefile))
mbligh8baa2ea2006-12-17 23:01:24 +0000190
191
jadmanski0afbb632008-06-06 21:10:57 +0000192 def get_xen_build_ver(self):
193 """Check Makefile and .config to return kernel version"""
194 version = patchlevel = sublevel = ''
195 extraversion = localversion = ''
mbligh8baa2ea2006-12-17 23:01:24 +0000196
jadmanski0afbb632008-06-06 21:10:57 +0000197 for line in open(self.build_dir + '/xen/Makefile', 'r').readlines():
198 if line.startswith('export XEN_VERSION'):
199 start = line.index('=') + 1
200 version = line[start:].strip()
201 if line.startswith('export XEN_SUBVERSION'):
202 start = line.index('=') + 1
203 sublevel = line[start:].strip()
204 if line.startswith('export XEN_EXTRAVERSION'):
205 start = line.index('=') + 1
206 extraversion = line[start:].strip()
mbligh8baa2ea2006-12-17 23:01:24 +0000207
jadmanski0afbb632008-06-06 21:10:57 +0000208 return "%s.%s%s" % (version, sublevel, extraversion)