66
by Neil Jerram
Pull in latest lp:charm-helpers code |
1 |
#!/usr/bin/env python
|
2 |
# -*- coding: utf-8 -*-
|
|
3 |
||
4 |
# Copyright 2014-2015 Canonical Limited.
|
|
5 |
#
|
|
6 |
# This file is part of charm-helpers.
|
|
7 |
#
|
|
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.
|
|
11 |
#
|
|
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.
|
|
16 |
#
|
|
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/>.
|
|
19 |
||
20 |
__author__ = "Jorge Niedbalski <jorge.niedbalski@canonical.com>" |
|
21 |
||
22 |
from charmhelpers.core.hookenv import ( |
|
23 |
log, |
|
24 |
INFO
|
|
25 |
)
|
|
26 |
||
27 |
from subprocess import check_call, check_output |
|
28 |
import re |
|
29 |
||
30 |
||
31 |
def modprobe(module, persist=True): |
|
32 |
"""Load a kernel module and configure for auto-load on reboot."""
|
|
33 |
cmd = ['modprobe', module] |
|
34 |
||
35 |
log('Loading kernel module %s' % module, level=INFO) |
|
36 |
||
37 |
check_call(cmd) |
|
38 |
if persist: |
|
39 |
with open('/etc/modules', 'r+') as modules: |
|
40 |
if module not in modules.read(): |
|
41 |
modules.write(module) |
|
42 |
||
43 |
||
44 |
def rmmod(module, force=False): |
|
45 |
"""Remove a module from the linux kernel"""
|
|
46 |
cmd = ['rmmod'] |
|
47 |
if force: |
|
48 |
cmd.append('-f') |
|
49 |
cmd.append(module) |
|
50 |
log('Removing kernel module %s' % module, level=INFO) |
|
51 |
return check_call(cmd) |
|
52 |
||
53 |
||
54 |
def lsmod(): |
|
55 |
"""Shows what kernel modules are currently loaded"""
|
|
56 |
return check_output(['lsmod'], |
|
57 |
universal_newlines=True) |
|
58 |
||
59 |
||
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 |
|
64 |
||
65 |
||
66 |
def update_initramfs(version='all'): |
|
67 |
"""Updates an initramfs image"""
|
|
68 |
return check_call(["update-initramfs", "-k", version, "-u"]) |