2
# -*- coding: utf-8 -*-
4
# Copyright 2014-2015 Canonical Limited.
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
10
# http://www.apache.org/licenses/LICENSE-2.0
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.
18
__author__ = "Jorge Niedbalski <jorge.niedbalski@canonical.com>"
20
from charmhelpers.core.hookenv import (
25
from subprocess import check_call, check_output
29
def modprobe(module, persist=True):
30
"""Load a kernel module and configure for auto-load on reboot."""
31
cmd = ['modprobe', module]
33
log('Loading kernel module %s' % module, level=INFO)
37
with open('/etc/modules', 'r+') as modules:
38
if module not in modules.read():
42
def rmmod(module, force=False):
43
"""Remove a module from the linux kernel"""
48
log('Removing kernel module %s' % module, level=INFO)
49
return check_call(cmd)
53
"""Shows what kernel modules are currently loaded"""
54
return check_output(['lsmod'],
55
universal_newlines=True)
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
64
def update_initramfs(version='all'):
65
"""Updates an initramfs image"""
66
return check_call(["update-initramfs", "-k", version, "-u"])