blob: adaf18068335939a4af80d9371fd74dda5cef2b6 [file] [log] [blame]
mblighdcd57a82007-07-11 23:06:47 +00001#!/usr/bin/python
2#
3# Copyright 2007 Google Inc. Released under the GPL v2
4
mblighdc735a22007-08-02 16:54:37 +00005"""
6This module defines the Kernel class
mblighdcd57a82007-07-11 23:06:47 +00007
8 Kernel: an os kernel
9"""
10
mblighdc735a22007-08-02 16:54:37 +000011__author__ = """
12mbligh@google.com (Martin J. Bligh),
mblighdcd57a82007-07-11 23:06:47 +000013poirier@google.com (Benjamin Poirier),
14stutsman@google.com (Ryan Stutsman)"""
15
16
mbligh02ff2d52008-06-03 15:00:21 +000017import os, time
mbligh313f12c2008-05-15 23:33:50 +000018from autotest_lib.client.common_lib import error
mblighccb9e182008-04-17 15:42:10 +000019from autotest_lib.server import kernel, utils
mbligh03f4fc72007-11-29 20:56:14 +000020
mblighdcd57a82007-07-11 23:06:47 +000021
22class RPMKernel(kernel.Kernel):
mblighdc735a22007-08-02 16:54:37 +000023 """
24 This class represents a .rpm pre-built kernel.
mbligha25b29e2007-08-26 13:58:04 +000025
mblighdcd57a82007-07-11 23:06:47 +000026 It is used to obtain a built kernel and install it on a Host.
mbligha25b29e2007-08-26 13:58:04 +000027
mblighdcd57a82007-07-11 23:06:47 +000028 Implementation details:
mbligha25b29e2007-08-26 13:58:04 +000029 This is a leaf class in an abstract class hierarchy, it must
mblighdcd57a82007-07-11 23:06:47 +000030 implement the unimplemented methods in parent classes.
31 """
mbligha25b29e2007-08-26 13:58:04 +000032 def __init__(self):
33 super(RPMKernel, self).__init__()
34
35 def install(self, host, label='autoserv',
mbligh28299b32008-02-01 17:34:15 +000036 default=False, kernel_args = '', install_vmlinux=False):
mbligha25b29e2007-08-26 13:58:04 +000037 """
38 Install a kernel on the remote host.
39
40 This will also invoke the guest's bootloader to set this
41 kernel as the default kernel if default=True.
42
43 Args:
44 host: the host on which to install the kernel
45 [kwargs]: remaining keyword arguments will be passed
46 to Bootloader.add_kernel()
47
48 Raises:
49 AutoservError: no package has yet been obtained. Call
50 RPMKernel.get() with a .rpm package.
51 """
52 if len(label) > 15:
mbligh313f12c2008-05-15 23:33:50 +000053 raise error.AutoservError("label for kernel is too long \
mbligha25b29e2007-08-26 13:58:04 +000054 (> 15 chars): %s" % label)
55 if self.source_material is None:
mbligh313f12c2008-05-15 23:33:50 +000056 raise error.AutoservError("A kernel must first be \
mbligha25b29e2007-08-26 13:58:04 +000057 specified via get()")
58 rpm = self.source_material
59
60 remote_tmpdir = host.get_tmp_dir()
61 remote_rpm = os.path.join(remote_tmpdir, os.path.basename(rpm))
62 rpm_package = utils.run('/usr/bin/rpm -q -p %s' % rpm).stdout
63 vmlinuz = self.get_image_name()
64 host.send_file(rpm, remote_rpm)
65 host.run('rpm -e ' + rpm_package, ignore_status = True)
66 host.run('rpm --force -i ' + remote_rpm)
mbligh28299b32008-02-01 17:34:15 +000067
68 # Copy over the uncompressed image if there is one
69 if install_vmlinux:
70 vmlinux = self.get_vmlinux_name()
71 host.run('cd /;rpm2cpio %s | cpio -imuv .%s'
72 % (remote_rpm, vmlinux))
73 host.run('ls ' + vmlinux) # Verify
74
mbligha25b29e2007-08-26 13:58:04 +000075 host.bootloader.remove_kernel(label)
mbligha25b29e2007-08-26 13:58:04 +000076 host.bootloader.add_kernel(vmlinuz, label,
77 args=kernel_args, default=default)
mblighf16d8a82007-09-29 21:59:40 +000078 if kernel_args:
79 host.bootloader.add_args(label, kernel_args)
80 if not default:
81 host.bootloader.boot_once(label)
82
mbligha25b29e2007-08-26 13:58:04 +000083
84 def get_version(self):
85 """Get the version of the kernel to be installed.
86
87 Returns:
88 The version string, as would be returned
89 by 'make kernelrelease'.
90
91 Raises:
92 AutoservError: no package has yet been obtained. Call
93 RPMKernel.get() with a .rpm package.
94 """
95 if self.source_material is None:
mbligh313f12c2008-05-15 23:33:50 +000096 raise error.AutoservError("A kernel must first be \
mbligha25b29e2007-08-26 13:58:04 +000097 specified via get()")
98
99 retval = utils.run('rpm -qpi %s | grep Version | \
100 awk \'{print($3);}\'' % utils.sh_escape(self.source_material))
101 return retval.stdout.strip()
102
103
104 def get_image_name(self):
105 """Get the name of the kernel image to be installed.
106
107 Returns:
108 The full path to the kernel image file as it will be
109 installed on the host.
110
111 Raises:
112 AutoservError: no package has yet been obtained. Call
113 RPMKernel.get() with a .rpm package.
114 """
115 if self.source_material is None:
mbligh313f12c2008-05-15 23:33:50 +0000116 raise error.AutoservError("A kernel must first be \
mbligha25b29e2007-08-26 13:58:04 +0000117 specified via get()")
118
119 vmlinuz = utils.run('rpm -q -l -p %s \
120 | grep /boot/vmlinuz' % self.source_material).stdout.strip()
121 return vmlinuz
122
123
mbligh28299b32008-02-01 17:34:15 +0000124 def get_vmlinux_name(self):
125 """Get the name of the kernel image to be installed.
126
127 Returns:
128 The full path to the kernel image file as it will be
129 installed on the host. It is the uncompressed and
130 unstripped version of the kernel that can be used with
131 oprofile.
132
133 Raises:
134 AutoservError: no package has yet been obtained. Call
135 RPMKernel.get() with a .rpm package.
136 """
137 if self.source_material is None:
mbligh313f12c2008-05-15 23:33:50 +0000138 raise error.AutoservError("A kernel must first be \
mbligh28299b32008-02-01 17:34:15 +0000139 specified via get()")
140
141 vmlinux = utils.run('rpm -q -l -p %s \
142 | grep /boot/vmlinux' % self.source_material).stdout.strip()
143 return vmlinux
144
145
mbligha25b29e2007-08-26 13:58:04 +0000146 def get_initrd_name(self):
147 """Get the name of the initrd file to be installed.
148
149 Returns:
150 The full path to the initrd file as it will be
151 installed on the host. If the package includes no
152 initrd file, None is returned
153
154 Raises:
155 AutoservError: no package has yet been obtained. Call
156 RPMKernel.get() with a .rpm package.
157 """
158 if self.source_material is None:
mbligh313f12c2008-05-15 23:33:50 +0000159 raise error.AutoservError("A kernel must first be \
mbligha25b29e2007-08-26 13:58:04 +0000160 specified via get()")
161
162 res = utils.run('rpm -q -l -p %s \
163 | grep /boot/initrd' % self.source_material, ignore_status=True)
164 if res.exit_status:
165 return None
166 return res.stdout.strip()