forked from snapcore/snapd
/
sysctl.go
119 lines (102 loc) · 3.25 KB
/
sysctl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2020 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package configcore
import (
"bytes"
"fmt"
"os"
"path/filepath"
"strconv"
"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/overlord/configstate/config"
"github.com/snapcore/snapd/sysconfig"
"github.com/snapcore/snapd/systemd"
)
// see https://www.kernel.org/doc/Documentation/sysctl/kernel.txt
// The idea is that options of the form system.kernel. should
// match the (/proc/)sys/kernel hierarchy etc.
func init() {
// add supported configuration of this module
supportedConfigurations["core.system.kernel.printk.console-loglevel"] = true
}
const (
sysctlConfsDir = "/etc/sysctl.d"
snapdSysctlConf = "99-snapd.conf"
)
// these are the sysctl parameters prefixes we handle
var sysctlPrefixes = []string{"kernel.printk"}
func validateSysctlOptions(tr config.ConfGetter) error {
consoleLoglevelStr, err := coreCfg(tr, "system.kernel.printk.console-loglevel")
if err != nil {
return err
}
if consoleLoglevelStr != "" {
if n, err := strconv.ParseUint(consoleLoglevelStr, 10, 8); err != nil || (n < 0 || n > 7) {
return fmt.Errorf("console-loglevel must be a number between 0 and 7, not: %s", consoleLoglevelStr)
}
}
return nil
}
func handleSysctlConfiguration(_ sysconfig.Device, tr config.ConfGetter, opts *fsOnlyContext) error {
root := dirs.GlobalRootDir
if opts != nil {
root = opts.RootDir
}
consoleLoglevelStr, err := coreCfg(tr, "system.kernel.printk.console-loglevel")
if err != nil {
return nil
}
content := bytes.NewBuffer(nil)
if consoleLoglevelStr != "" {
content.WriteString(fmt.Sprintf("kernel.printk = %s 4 1 7\n", consoleLoglevelStr))
} else {
// Don't write values to content so that the config
// file gets removed and console-loglevel gets reset
// to default value from 10-console-messages.conf
// TODO: this logic will need more non-obvious work to support
// kernel parameters that don't have already on-disk defaults.
}
dirContent := map[string]osutil.FileState{}
if content.Len() > 0 {
dirContent[snapdSysctlConf] = &osutil.MemoryFileState{
Content: content.Bytes(),
Mode: 0644,
}
}
dir := filepath.Join(root, sysctlConfsDir)
if opts != nil {
if err := os.MkdirAll(dir, 0755); err != nil {
return err
}
}
// write the new config
glob := snapdSysctlConf
changed, removed, err := osutil.EnsureDirState(dir, glob, dirContent)
if err != nil {
return err
}
if opts == nil {
if len(changed) > 0 || len(removed) > 0 {
// apply our configuration or default configuration
// via systemd-sysctl for the relevant prefixes
return systemd.Sysctl(sysctlPrefixes)
}
}
return nil
}