blob: b0ab028ef311f3ac16e6aa4ef9c8c71ebe69da6a [file] [log] [blame]
mblighdcd57a82007-07-11 23:06:47 +00001#
2# Copyright 2007 Google Inc. Released under the GPL v2
3
mblighdc735a22007-08-02 16:54:37 +00004"""
5This module defines the Kernel class
mblighdcd57a82007-07-11 23:06:47 +00006
jadmanski0afbb632008-06-06 21:10:57 +00007 Kernel: an os kernel
mblighdcd57a82007-07-11 23:06:47 +00008"""
9
mblighdc735a22007-08-02 16:54:37 +000010__author__ = """
11mbligh@google.com (Martin J. Bligh),
mblighdcd57a82007-07-11 23:06:47 +000012poirier@google.com (Benjamin Poirier),
13stutsman@google.com (Ryan Stutsman)"""
14
15
mbligh02ff2d52008-06-03 15:00:21 +000016import os, time
mbligh313f12c2008-05-15 23:33:50 +000017from autotest_lib.client.common_lib import error
mblighccb9e182008-04-17 15:42:10 +000018from autotest_lib.server import kernel, utils
mbligh03f4fc72007-11-29 20:56:14 +000019
mblighdcd57a82007-07-11 23:06:47 +000020
21class RPMKernel(kernel.Kernel):
jadmanski0afbb632008-06-06 21:10:57 +000022 """
23 This class represents a .rpm pre-built kernel.
mbligha25b29e2007-08-26 13:58:04 +000024
jadmanski0afbb632008-06-06 21:10:57 +000025 It is used to obtain a built kernel and install it on a Host.
mbligha25b29e2007-08-26 13:58:04 +000026
jadmanski0afbb632008-06-06 21:10:57 +000027 Implementation details:
28 This is a leaf class in an abstract class hierarchy, it must
29 implement the unimplemented methods in parent classes.
30 """
31 def __init__(self):
32 super(RPMKernel, self).__init__()
mbligha25b29e2007-08-26 13:58:04 +000033
mbligh1a639fb2008-09-10 19:59:14 +000034 def install(self, host, label='autotest',
jadmanski5664cff2008-12-03 18:00:03 +000035 default=False, kernel_args = '', install_vmlinux=True):
jadmanski0afbb632008-06-06 21:10:57 +000036 """
37 Install a kernel on the remote host.
mbligha25b29e2007-08-26 13:58:04 +000038
jadmanski0afbb632008-06-06 21:10:57 +000039 This will also invoke the guest's bootloader to set this
40 kernel as the default kernel if default=True.
mbligh28299b32008-02-01 17:34:15 +000041
jadmanski0afbb632008-06-06 21:10:57 +000042 Args:
43 host: the host on which to install the kernel
44 [kwargs]: remaining keyword arguments will be passed
45 to Bootloader.add_kernel()
mbligh28299b32008-02-01 17:34:15 +000046
jadmanski0afbb632008-06-06 21:10:57 +000047 Raises:
48 AutoservError: no package has yet been obtained. Call
49 RPMKernel.get() with a .rpm package.
50 """
51 if len(label) > 15:
52 raise error.AutoservError("label for kernel is too long \
53 (> 15 chars): %s" % label)
54 if self.source_material is None:
55 raise error.AutoservError("A kernel must first be \
56 specified via get()")
57 rpm = self.source_material
58
59 remote_tmpdir = host.get_tmp_dir()
60 remote_rpm = os.path.join(remote_tmpdir, os.path.basename(rpm))
61 rpm_package = utils.run('/usr/bin/rpm -q -p %s' % rpm).stdout
62 vmlinuz = self.get_image_name()
63 host.send_file(rpm, remote_rpm)
64 host.run('rpm -e ' + rpm_package, ignore_status = True)
65 host.run('rpm --force -i ' + remote_rpm)
66
67 # Copy over the uncompressed image if there is one
68 if install_vmlinux:
69 vmlinux = self.get_vmlinux_name()
70 host.run('cd /;rpm2cpio %s | cpio -imuv .%s'
71 % (remote_rpm, vmlinux))
72 host.run('ls ' + vmlinux) # Verify
73
74 host.bootloader.remove_kernel(label)
75 host.bootloader.add_kernel(vmlinuz, label,
76 args=kernel_args, default=default)
77 if kernel_args:
78 host.bootloader.add_args(label, kernel_args)
79 if not default:
80 host.bootloader.boot_once(label)
mblighf16d8a82007-09-29 21:59:40 +000081
mbligha25b29e2007-08-26 13:58:04 +000082
jadmanski0afbb632008-06-06 21:10:57 +000083 def get_version(self):
84 """Get the version of the kernel to be installed.
85
86 Returns:
87 The version string, as would be returned
88 by 'make kernelrelease'.
89
90 Raises:
91 AutoservError: no package has yet been obtained. Call
92 RPMKernel.get() with a .rpm package.
93 """
94 if self.source_material is None:
95 raise error.AutoservError("A kernel must first be \
96 specified via get()")
97
mbligh67b8fbd2008-11-07 01:03:03 +000098 retval = utils.run('rpm -qpi %s | grep Version | awk \'{print($3);}\''
99 % utils.sh_escape(self.source_material))
jadmanski0afbb632008-06-06 21:10:57 +0000100 return retval.stdout.strip()
mbligha25b29e2007-08-26 13:58:04 +0000101
102
jadmanski0afbb632008-06-06 21:10:57 +0000103 def get_image_name(self):
104 """Get the name of the kernel image to be installed.
105
106 Returns:
107 The full path to the kernel image file as it will be
108 installed on the host.
109
110 Raises:
111 AutoservError: no package has yet been obtained. Call
112 RPMKernel.get() with a .rpm package.
113 """
114 if self.source_material is None:
115 raise error.AutoservError("A kernel must first be \
116 specified via get()")
117
mbligh67b8fbd2008-11-07 01:03:03 +0000118 vmlinuz = utils.run('rpm -q -l -p %s | grep /boot/vmlinuz'
119 % self.source_material).stdout.strip()
jadmanski0afbb632008-06-06 21:10:57 +0000120 return vmlinuz
mbligha25b29e2007-08-26 13:58:04 +0000121
122
jadmanski0afbb632008-06-06 21:10:57 +0000123 def get_vmlinux_name(self):
124 """Get the name of the kernel image to be installed.
125
126 Returns:
127 The full path to the kernel image file as it will be
128 installed on the host. It is the uncompressed and
129 unstripped version of the kernel that can be used with
130 oprofile.
131
132 Raises:
133 AutoservError: no package has yet been obtained. Call
134 RPMKernel.get() with a .rpm package.
135 """
136 if self.source_material is None:
137 raise error.AutoservError("A kernel must first be \
138 specified via get()")
139
mbligh67b8fbd2008-11-07 01:03:03 +0000140 vmlinux = utils.run('rpm -q -l -p %s | grep /boot/vmlinux'
141 % self.source_material).stdout.strip()
jadmanski0afbb632008-06-06 21:10:57 +0000142 return vmlinux
mbligh28299b32008-02-01 17:34:15 +0000143
144
jadmanski0afbb632008-06-06 21:10:57 +0000145 def get_initrd_name(self):
146 """Get the name of the initrd file to be installed.
mbligha25b29e2007-08-26 13:58:04 +0000147
jadmanski0afbb632008-06-06 21:10:57 +0000148 Returns:
149 The full path to the initrd file as it will be
150 installed on the host. If the package includes no
151 initrd file, None is returned
152
153 Raises:
154 AutoservError: no package has yet been obtained. Call
155 RPMKernel.get() with a .rpm package.
156 """
157 if self.source_material is None:
158 raise error.AutoservError("A kernel must first be \
159 specified via get()")
160
mbligh67b8fbd2008-11-07 01:03:03 +0000161 res = utils.run('rpm -q -l -p %s | grep /boot/initrd'
162 % self.source_material, ignore_status=True)
jadmanski0afbb632008-06-06 21:10:57 +0000163 if res.exit_status:
164 return None
165 return res.stdout.strip()