~josvaz/charms/trusty/bip/charmhelpers-cleanup

« back to all changes in this revision

Viewing changes to lib/charmhelpers/core/kernel.py

  • Committer: Jose Vazquez
  • Date: 2016-07-29 14:51:37 UTC
  • Revision ID: jose.vazquez@canonical.com-20160729145137-0el8wqm1y27s809l
Got charmhelpers for fetch as apt_install is now there

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# -*- coding: utf-8 -*-
 
3
 
 
4
# Copyright 2014-2015 Canonical Limited.
 
5
#
 
6
# Licensed under the Apache License, Version 2.0 (the "License");
 
7
# you may not use this file except in compliance with the License.
 
8
# You may obtain a copy of the License at
 
9
#
 
10
#  http://www.apache.org/licenses/LICENSE-2.0
 
11
#
 
12
# Unless required by applicable law or agreed to in writing, software
 
13
# distributed under the License is distributed on an "AS IS" BASIS,
 
14
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
15
# See the License for the specific language governing permissions and
 
16
# limitations under the License.
 
17
 
 
18
__author__ = "Jorge Niedbalski <jorge.niedbalski@canonical.com>"
 
19
 
 
20
from charmhelpers.core.hookenv import (
 
21
    log,
 
22
    INFO
 
23
)
 
24
 
 
25
from subprocess import check_call, check_output
 
26
import re
 
27
 
 
28
 
 
29
def modprobe(module, persist=True):
 
30
    """Load a kernel module and configure for auto-load on reboot."""
 
31
    cmd = ['modprobe', module]
 
32
 
 
33
    log('Loading kernel module %s' % module, level=INFO)
 
34
 
 
35
    check_call(cmd)
 
36
    if persist:
 
37
        with open('/etc/modules', 'r+') as modules:
 
38
            if module not in modules.read():
 
39
                modules.write(module)
 
40
 
 
41
 
 
42
def rmmod(module, force=False):
 
43
    """Remove a module from the linux kernel"""
 
44
    cmd = ['rmmod']
 
45
    if force:
 
46
        cmd.append('-f')
 
47
    cmd.append(module)
 
48
    log('Removing kernel module %s' % module, level=INFO)
 
49
    return check_call(cmd)
 
50
 
 
51
 
 
52
def lsmod():
 
53
    """Shows what kernel modules are currently loaded"""
 
54
    return check_output(['lsmod'],
 
55
                        universal_newlines=True)
 
56
 
 
57
 
 
58
def is_module_loaded(module):
 
59
    """Checks if a kernel module is already loaded"""
 
60
    matches = re.findall('^%s[ ]+' % module, lsmod(), re.M)
 
61
    return len(matches) > 0
 
62
 
 
63
 
 
64
def update_initramfs(version='all'):
 
65
    """Updates an initramfs image"""
 
66
    return check_call(["update-initramfs", "-k", version, "-u"])