mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
| 3 | # Copyright 2007 Google Inc. Released under the GPL v2 |
| 4 | |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 5 | """ |
| 6 | This module defines the Kernel class |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 7 | |
| 8 | Kernel: an os kernel |
| 9 | """ |
| 10 | |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 11 | __author__ = """ |
| 12 | mbligh@google.com (Martin J. Bligh), |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 13 | poirier@google.com (Benjamin Poirier), |
| 14 | stutsman@google.com (Ryan Stutsman)""" |
| 15 | |
| 16 | |
mbligh | 02ff2d5 | 2008-06-03 15:00:21 +0000 | [diff] [blame] | 17 | import os, time |
mbligh | 313f12c | 2008-05-15 23:33:50 +0000 | [diff] [blame] | 18 | from autotest_lib.client.common_lib import error |
mbligh | ccb9e18 | 2008-04-17 15:42:10 +0000 | [diff] [blame] | 19 | from autotest_lib.server import kernel, utils |
mbligh | 03f4fc7 | 2007-11-29 20:56:14 +0000 | [diff] [blame] | 20 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 21 | |
| 22 | class RPMKernel(kernel.Kernel): |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 23 | """ |
| 24 | This class represents a .rpm pre-built kernel. |
mbligh | a25b29e | 2007-08-26 13:58:04 +0000 | [diff] [blame] | 25 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 26 | It is used to obtain a built kernel and install it on a Host. |
mbligh | a25b29e | 2007-08-26 13:58:04 +0000 | [diff] [blame] | 27 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 28 | Implementation details: |
mbligh | a25b29e | 2007-08-26 13:58:04 +0000 | [diff] [blame] | 29 | This is a leaf class in an abstract class hierarchy, it must |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 30 | implement the unimplemented methods in parent classes. |
| 31 | """ |
mbligh | a25b29e | 2007-08-26 13:58:04 +0000 | [diff] [blame] | 32 | def __init__(self): |
| 33 | super(RPMKernel, self).__init__() |
| 34 | |
| 35 | def install(self, host, label='autoserv', |
mbligh | 28299b3 | 2008-02-01 17:34:15 +0000 | [diff] [blame] | 36 | default=False, kernel_args = '', install_vmlinux=False): |
mbligh | a25b29e | 2007-08-26 13:58:04 +0000 | [diff] [blame] | 37 | """ |
| 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: |
mbligh | 313f12c | 2008-05-15 23:33:50 +0000 | [diff] [blame] | 53 | raise error.AutoservError("label for kernel is too long \ |
mbligh | a25b29e | 2007-08-26 13:58:04 +0000 | [diff] [blame] | 54 | (> 15 chars): %s" % label) |
| 55 | if self.source_material is None: |
mbligh | 313f12c | 2008-05-15 23:33:50 +0000 | [diff] [blame] | 56 | raise error.AutoservError("A kernel must first be \ |
mbligh | a25b29e | 2007-08-26 13:58:04 +0000 | [diff] [blame] | 57 | 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) |
mbligh | 28299b3 | 2008-02-01 17:34:15 +0000 | [diff] [blame] | 67 | |
| 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 | |
mbligh | a25b29e | 2007-08-26 13:58:04 +0000 | [diff] [blame] | 75 | host.bootloader.remove_kernel(label) |
mbligh | a25b29e | 2007-08-26 13:58:04 +0000 | [diff] [blame] | 76 | host.bootloader.add_kernel(vmlinuz, label, |
| 77 | args=kernel_args, default=default) |
mbligh | f16d8a8 | 2007-09-29 21:59:40 +0000 | [diff] [blame] | 78 | if kernel_args: |
| 79 | host.bootloader.add_args(label, kernel_args) |
| 80 | if not default: |
| 81 | host.bootloader.boot_once(label) |
| 82 | |
mbligh | a25b29e | 2007-08-26 13:58:04 +0000 | [diff] [blame] | 83 | |
| 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: |
mbligh | 313f12c | 2008-05-15 23:33:50 +0000 | [diff] [blame] | 96 | raise error.AutoservError("A kernel must first be \ |
mbligh | a25b29e | 2007-08-26 13:58:04 +0000 | [diff] [blame] | 97 | 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: |
mbligh | 313f12c | 2008-05-15 23:33:50 +0000 | [diff] [blame] | 116 | raise error.AutoservError("A kernel must first be \ |
mbligh | a25b29e | 2007-08-26 13:58:04 +0000 | [diff] [blame] | 117 | 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 | |
mbligh | 28299b3 | 2008-02-01 17:34:15 +0000 | [diff] [blame] | 124 | 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: |
mbligh | 313f12c | 2008-05-15 23:33:50 +0000 | [diff] [blame] | 138 | raise error.AutoservError("A kernel must first be \ |
mbligh | 28299b3 | 2008-02-01 17:34:15 +0000 | [diff] [blame] | 139 | 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 | |
mbligh | a25b29e | 2007-08-26 13:58:04 +0000 | [diff] [blame] | 146 | 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: |
mbligh | 313f12c | 2008-05-15 23:33:50 +0000 | [diff] [blame] | 159 | raise error.AutoservError("A kernel must first be \ |
mbligh | a25b29e | 2007-08-26 13:58:04 +0000 | [diff] [blame] | 160 | 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() |