blob: 69b59fa786f65c7b1d1865b4b54e3cad11a1ddfd [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),
mblighdc735a22007-08-02 16:54:37 +000014stutsman@google.com (Ryan Stutsman)
15"""
mblighdcd57a82007-07-11 23:06:47 +000016
17
mblighccb9e182008-04-17 15:42:10 +000018import os, os.path, time, urllib
mblighdcd57a82007-07-11 23:06:47 +000019
mblighccb9e182008-04-17 15:42:10 +000020from autotest_lib.client.common_lib.error import *
21from autotest_lib.server import kernel, utils
mbligh03f4fc72007-11-29 20:56:14 +000022
mblighdcd57a82007-07-11 23:06:47 +000023
24class DEBKernel(kernel.Kernel):
mblighdc735a22007-08-02 16:54:37 +000025 """
26 This class represents a .deb pre-built kernel.
mblighdcd57a82007-07-11 23:06:47 +000027
28 It is used to obtain a built kernel and install it on a Host.
29
30 Implementation details:
31 This is a leaf class in an abstract class hierarchy, it must
32 implement the unimplemented methods in parent classes.
33 """
34 def __init__(self):
35 super(DEBKernel, self).__init__()
36
37
mbligh6a641262007-07-23 23:33:31 +000038 def install(self, host, **kwargs):
mblighdc735a22007-08-02 16:54:37 +000039 """
40 Install a kernel on the remote host.
mbligh6a641262007-07-23 23:33:31 +000041
42 This will also invoke the guest's bootloader to set this
43 kernel as the default kernel.
44
45 Args:
46 host: the host on which to install the kernel
47 [kwargs]: remaining keyword arguments will be passed
48 to Bootloader.add_kernel()
49
50 Raises:
51 AutoservError: no package has yet been obtained. Call
52 DEBKernel.get() with a .deb package.
53 """
54 if self.source_material is None:
55 raise AutoservError("A kernel must first be \
56 specified via get()")
57
mblighdcd57a82007-07-11 23:06:47 +000058 remote_tmpdir = host.get_tmp_dir()
mblighc8949b82007-07-23 16:33:58 +000059 basename = os.path.basename(self.source_material)
mblighdcd57a82007-07-11 23:06:47 +000060 remote_filename = os.path.join(remote_tmpdir, basename)
mblighc8949b82007-07-23 16:33:58 +000061 host.send_file(self.source_material, remote_filename)
mbligh6a641262007-07-23 23:33:31 +000062 host.run('dpkg -i "%s"' % (utils.sh_escape(remote_filename),))
63 host.run('mkinitramfs -o "%s" "%s"' % (
64 utils.sh_escape(self.get_initrd_name()),
65 utils.sh_escape(self.get_version()),))
66
67 host.bootloader.add_kernel(self.get_image_name(),
68 initrd=self.get_initrd_name(), **kwargs)
mblighdc735a22007-08-02 16:54:37 +000069
70
mbligh6a641262007-07-23 23:33:31 +000071 def get_version(self):
72 """Get the version of the kernel to be installed.
73
74 Returns:
75 The version string, as would be returned
76 by 'make kernelrelease'.
77
78 Raises:
79 AutoservError: no package has yet been obtained. Call
80 DEBKernel.get() with a .deb package.
81 """
82 if self.source_material is None:
83 raise AutoservError("A kernel must first be \
84 specified via get()")
85
86 retval= utils.run('dpkg-deb -f "%s" version' %
87 utils.sh_escape(self.source_material),)
88 return retval.stdout.strip()
mblighdc735a22007-08-02 16:54:37 +000089
90
mbligh6a641262007-07-23 23:33:31 +000091 def get_image_name(self):
92 """Get the name of the kernel image to be installed.
93
94 Returns:
95 The full path to the kernel image file as it will be
96 installed on the host.
97
98 Raises:
99 AutoservError: no package has yet been obtained. Call
100 DEBKernel.get() with a .deb package.
101 """
102 return "/boot/vmlinuz-%s" % (self.get_version(),)
mblighdc735a22007-08-02 16:54:37 +0000103
104
mbligh6a641262007-07-23 23:33:31 +0000105 def get_initrd_name(self):
106 """Get the name of the initrd file to be installed.
107
108 Returns:
109 The full path to the initrd file as it will be
110 installed on the host. If the package includes no
111 initrd file, None is returned
112
113 Raises:
114 AutoservError: no package has yet been obtained. Call
115 DEBKernel.get() with a .deb package.
116 """
117 if self.source_material is None:
118 raise AutoservError("A kernel must first be \
119 specified via get()")
120
121 return "/boot/initrd.img-%s" % (self.get_version(),)
mbligh7d1872b2007-08-10 19:20:55 +0000122
123 def extract(self, host):
124 """Extract the kernel package.
125
126 This function is only useful to access the content of the
127 package (for example the kernel image) without
128 installing it. It is not necessary to run this function to
129 install the kernel.
130
131 Args:
132 host: the host on which to extract the kernel package.
133
134 Returns:
135 The full path to the temporary directory on host where
136 the package was extracted.
137
138 Raises:
139 AutoservError: no package has yet been obtained. Call
140 DEBKernel.get() with a .deb package.
141 """
142 if self.source_material is None:
143 raise AutoservError("A kernel must first be \
144 specified via get()")
145
146 remote_tmpdir = host.get_tmp_dir()
147 basename = os.path.basename(self.source_material)
148 remote_filename = os.path.join(remote_tmpdir, basename)
149 host.send_file(self.source_material, remote_filename)
150 content_dir= os.path.join(remote_tmpdir, "contents")
151 host.run('dpkg -x "%s" "%s"' % (utils.sh_escape(remote_filename), utils.sh_escape(content_dir),))
152
153 return content_dir