2
# -*- coding: utf-8 -*-
4
# Copyright 2014-2015 Canonical Limited.
6
# This file is part of charm-helpers.
8
# charm-helpers is free software: you can redistribute it and/or modify
9
# it under the terms of the GNU Lesser General Public License version 3 as
10
# published by the Free Software Foundation.
12
# charm-helpers is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU Lesser General Public License for more details.
17
# You should have received a copy of the GNU Lesser General Public License
18
# along with charm-helpers. If not, see <http://www.gnu.org/licenses/>.
20
__author__ = "Jorge Niedbalski <jorge.niedbalski@canonical.com>"
22
from charmhelpers.core.hookenv import (
27
from subprocess import check_call, check_output
31
def modprobe(module, persist=True):
32
"""Load a kernel module and configure for auto-load on reboot."""
33
cmd = ['modprobe', module]
35
log('Loading kernel module %s' % module, level=INFO)
39
with open('/etc/modules', 'r+') as modules:
40
if module not in modules.read():
44
def rmmod(module, force=False):
45
"""Remove a module from the linux kernel"""
50
log('Removing kernel module %s' % module, level=INFO)
51
return check_call(cmd)
55
"""Shows what kernel modules are currently loaded"""
56
return check_output(['lsmod'],
57
universal_newlines=True)
60
def is_module_loaded(module):
61
"""Checks if a kernel module is already loaded"""
62
matches = re.findall('^%s[ ]+' % module, lsmod(), re.M)
63
return len(matches) > 0
66
def update_initramfs(version='all'):
67
"""Updates an initramfs image"""
68
return check_call(["update-initramfs", "-k", version, "-u"])