blob: d61a334c0da0def781fa97c3313fa1e751de711e [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)
showard75cdfee2009-06-10 17:40:41 +000041 self.log('log_dir: %s ' % self.log_dir)
42 self.job.logging.tee_redirect_debug_dir(self.log_dir, log_name=logfile)
mbligh8baa2ea2006-12-17 23:01:24 +000043
jadmanski0afbb632008-06-06 21:10:57 +000044 # build xen hypervisor and user-space tools
45 targets = ['xen', 'tools']
mbligh53da18e2009-01-05 21:13:26 +000046 threads = 2 * utils.count_cpus()
jadmanski0afbb632008-06-06 21:10:57 +000047 for t in targets:
48 build_string = 'make -j %d %s %s' % (threads, make_opts, t)
49 self.log('build_string: %s' % build_string)
mbligh23955fc2008-06-12 17:41:18 +000050 utils.system(build_string)
mbligh8baa2ea2006-12-17 23:01:24 +000051
jadmanski0afbb632008-06-06 21:10:57 +000052 # make a kernel job out of the kernel from the xen src if one isn't provided
mblighd876f452008-12-03 15:09:17 +000053 if self.kjob is None:
jadmanski0afbb632008-06-06 21:10:57 +000054 # get xen kernel tree ready
55 self.log("prep-ing xen'ified kernel source tree")
mbligh23955fc2008-06-12 17:41:18 +000056 utils.system('make prep-kernels')
mbligh8baa2ea2006-12-17 23:01:24 +000057
jadmanski0afbb632008-06-06 21:10:57 +000058 v = self.get_xen_kernel_build_ver()
59 self.log('building xen kernel version: %s' % v)
mbligh8baa2ea2006-12-17 23:01:24 +000060
jadmanski0afbb632008-06-06 21:10:57 +000061 # build xen-ified kernel in xen tree
62 kernel_base_tree = os.path.join(self.build_dir, \
63 'linux-%s' % self.get_xen_kernel_build_ver())
mbligh8baa2ea2006-12-17 23:01:24 +000064
jadmanski0afbb632008-06-06 21:10:57 +000065 self.log('kernel_base_tree = %s' % kernel_base_tree)
66 # fix up XENGUEST value in EXTRAVERSION; we can't have
67 # files with '$(XENGEUST)' in the name, =(
68 self.fix_up_xen_kernel_makefile(kernel_base_tree)
mbligh8baa2ea2006-12-17 23:01:24 +000069
jadmanski0afbb632008-06-06 21:10:57 +000070 # make the kernel job
71 self.kjob = self.job.kernel(kernel_base_tree)
mbligh8baa2ea2006-12-17 23:01:24 +000072
jadmanski0afbb632008-06-06 21:10:57 +000073 # hardcoding dom0 config (no modules for testing, yay!)
74 # FIXME: probe host to determine which config to pick
75 c = self.build_dir + '/buildconfigs/linux-defconfig_xen0_x86_32'
76 self.log('using kernel config: %s ' % c)
77 self.kjob.config(c)
mbligh8baa2ea2006-12-17 23:01:24 +000078
jadmanski0afbb632008-06-06 21:10:57 +000079 # Xen's kernel tree sucks; doesn't use bzImage, but vmlinux
80 self.kjob.set_build_target('vmlinuz')
mbligh8baa2ea2006-12-17 23:01:24 +000081
jadmanski0afbb632008-06-06 21:10:57 +000082 # also, the vmlinuz is not out in arch/*/boot, ARGH! more hackery
83 self.kjob.set_build_image(self.job.tmpdir + '/build/linux/vmlinuz')
mbligh8baa2ea2006-12-17 23:01:24 +000084
jadmanski0afbb632008-06-06 21:10:57 +000085 self.kjob.build()
mbligh8baa2ea2006-12-17 23:01:24 +000086
showard75cdfee2009-06-10 17:40:41 +000087 self.job.logging.restore()
mbligh8baa2ea2006-12-17 23:01:24 +000088
jadmanski0afbb632008-06-06 21:10:57 +000089 xen_version = self.get_xen_build_ver()
90 self.log('BUILD VERSION: Xen: %s Kernel:%s' % \
91 (xen_version, self.kjob.get_kernel_build_ver()))
mbligh8baa2ea2006-12-17 23:01:24 +000092
93
jadmanski0afbb632008-06-06 21:10:57 +000094 def build_timed(self, *args, **kwds):
95 raise NotImplementedError('build_timed() not implemented')
mbligh8baa2ea2006-12-17 23:01:24 +000096
97
jadmanski0afbb632008-06-06 21:10:57 +000098 def install(self, tag='', prefix = '/', extraversion='autotest'):
99 """make install in the kernel tree"""
100 self.log('Installing ...')
mbligh8baa2ea2006-12-17 23:01:24 +0000101
jadmanski0afbb632008-06-06 21:10:57 +0000102 os.chdir(self.build_dir)
mbligh8baa2ea2006-12-17 23:01:24 +0000103
jadmanski0afbb632008-06-06 21:10:57 +0000104 if not os.path.isdir(prefix):
105 os.mkdir(prefix)
106 self.boot_dir = os.path.join(prefix, 'boot')
107 if not os.path.isdir(self.boot_dir):
108 os.mkdir(self.boot_dir)
mbligh8baa2ea2006-12-17 23:01:24 +0000109
jadmanski0afbb632008-06-06 21:10:57 +0000110 # remember what we are going to install
111 xen_version = '%s-%s' % (self.get_xen_build_ver(), extraversion)
112 self.xen_image = self.boot_dir + '/xen-' + xen_version + '.gz'
113 self.xen_syms = self.boot_dir + '/xen-syms-' + xen_version
mbligh8baa2ea2006-12-17 23:01:24 +0000114
jadmanski0afbb632008-06-06 21:10:57 +0000115 self.log('Installing Xen ...')
116 os.environ['XEN_EXTRAVERSION'] = '-unstable-%s'% extraversion
mbligh8baa2ea2006-12-17 23:01:24 +0000117
jadmanski0afbb632008-06-06 21:10:57 +0000118 # install xen
mbligh23955fc2008-06-12 17:41:18 +0000119 utils.system('make DESTDIR=%s -C xen install' % prefix)
mbligh8baa2ea2006-12-17 23:01:24 +0000120
jadmanski0afbb632008-06-06 21:10:57 +0000121 # install tools
mbligh23955fc2008-06-12 17:41:18 +0000122 utils.system('make DESTDIR=%s -C tools install' % prefix)
mbligh8baa2ea2006-12-17 23:01:24 +0000123
jadmanski0afbb632008-06-06 21:10:57 +0000124 # install kernel
125 ktag = self.kjob.get_kernel_build_ver()
126 kprefix = prefix
127 self.kjob.install(tag=ktag, prefix=kprefix)
mbligh8baa2ea2006-12-17 23:01:24 +0000128
129
jadmanski0afbb632008-06-06 21:10:57 +0000130 def add_to_bootloader(self, tag='autotest', args=''):
131 """ add this kernel to bootloader, taking an
132 optional parameter of space separated parameters
133 e.g.: kernel.add_to_bootloader('mykernel', 'ro acpi=off')
134 """
mbligh8baa2ea2006-12-17 23:01:24 +0000135
jadmanski0afbb632008-06-06 21:10:57 +0000136 # turn on xen mode
137 self.job.bootloader.enable_xen_mode()
mbligh8baa2ea2006-12-17 23:01:24 +0000138
jadmanski0afbb632008-06-06 21:10:57 +0000139 # remove existing entry if present
140 self.job.bootloader.remove_kernel(tag)
mbligh8baa2ea2006-12-17 23:01:24 +0000141
jadmanski0afbb632008-06-06 21:10:57 +0000142 # add xen and xen kernel
mblighc2ebea02009-10-02 00:02:33 +0000143 self.job.bootloader.add_kernel(
144 self.kjob.image, tag, initrd=self.kjob.initrd,
145 xen_hypervisor=self.xen_image)
mbligh8baa2ea2006-12-17 23:01:24 +0000146
jadmanski0afbb632008-06-06 21:10:57 +0000147 # if no args passed, populate from /proc/cmdline
148 if not args:
149 args = open('/proc/cmdline', 'r').readline().strip()
mbligh8baa2ea2006-12-17 23:01:24 +0000150
jadmanski0afbb632008-06-06 21:10:57 +0000151 # add args to entry one at a time
152 for a in args.split(' '):
153 self.job.bootloader.add_args(tag, a)
mbligh8baa2ea2006-12-17 23:01:24 +0000154
jadmanski0afbb632008-06-06 21:10:57 +0000155 # turn off xen mode
156 self.job.bootloader.disable_xen_mode()
mbligh8baa2ea2006-12-17 23:01:24 +0000157
158
jadmanski0afbb632008-06-06 21:10:57 +0000159 def get_xen_kernel_build_ver(self):
160 """Check xen buildconfig for current kernel version"""
161 version = patchlevel = sublevel = ''
162 extraversion = localversion = ''
mblighf25cde12006-12-17 23:13:48 +0000163
jadmanski0afbb632008-06-06 21:10:57 +0000164 version_file = self.build_dir + '/buildconfigs/mk.linux-2.6-xen'
mbligh8baa2ea2006-12-17 23:01:24 +0000165
jadmanski0afbb632008-06-06 21:10:57 +0000166 for line in open(version_file, 'r').readlines():
167 if line.startswith('LINUX_VER'):
168 start = line.index('=') + 1
169 version = line[start:].strip() + "-xen"
170 break
mbligh8baa2ea2006-12-17 23:01:24 +0000171
jadmanski0afbb632008-06-06 21:10:57 +0000172 return version
mbligh8baa2ea2006-12-17 23:01:24 +0000173
174
jadmanski0afbb632008-06-06 21:10:57 +0000175 def fix_up_xen_kernel_makefile(self, kernel_dir):
176 """Fix up broken EXTRAVERSION in xen-ified Linux kernel Makefile"""
177 xenguest = ''
178 makefile = kernel_dir + '/Makefile'
mbligh8baa2ea2006-12-17 23:01:24 +0000179
jadmanski0afbb632008-06-06 21:10:57 +0000180 for line in open(makefile, 'r').readlines():
181 if line.startswith('XENGUEST'):
182 start = line.index('=') + 1
183 xenguest = line[start:].strip()
184 break;
mbligh8baa2ea2006-12-17 23:01:24 +0000185
jadmanski0afbb632008-06-06 21:10:57 +0000186 # change out $XENGUEST in EXTRAVERSION line
mbligh23955fc2008-06-12 17:41:18 +0000187 utils.system('sed -i.old "s,\$(XENGUEST),%s," %s' % (xenguest,
188 makefile))
mbligh8baa2ea2006-12-17 23:01:24 +0000189
190
jadmanski0afbb632008-06-06 21:10:57 +0000191 def get_xen_build_ver(self):
192 """Check Makefile and .config to return kernel version"""
193 version = patchlevel = sublevel = ''
194 extraversion = localversion = ''
mbligh8baa2ea2006-12-17 23:01:24 +0000195
jadmanski0afbb632008-06-06 21:10:57 +0000196 for line in open(self.build_dir + '/xen/Makefile', 'r').readlines():
197 if line.startswith('export XEN_VERSION'):
198 start = line.index('=') + 1
199 version = line[start:].strip()
200 if line.startswith('export XEN_SUBVERSION'):
201 start = line.index('=') + 1
202 sublevel = line[start:].strip()
203 if line.startswith('export XEN_EXTRAVERSION'):
204 start = line.index('=') + 1
205 extraversion = line[start:].strip()
mbligh8baa2ea2006-12-17 23:01:24 +0000206
jadmanski0afbb632008-06-06 21:10:57 +0000207 return "%s.%s%s" % (version, sublevel, extraversion)