github.com/rigado/snapd@v2.42.5-go-mod+incompatible/overlord/configstate/configcore/network.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2018 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package configcore 21 22 import ( 23 "bytes" 24 "fmt" 25 "os/exec" 26 "path/filepath" 27 28 "github.com/snapcore/snapd/dirs" 29 "github.com/snapcore/snapd/osutil" 30 "github.com/snapcore/snapd/overlord/configstate/config" 31 ) 32 33 func init() { 34 // add supported configuration of this module 35 supportedConfigurations["core.network.disable-ipv6"] = true 36 } 37 38 func validateNetworkSettings(tr config.Conf) error { 39 return validateBoolFlag(tr, "network.disable-ipv6") 40 } 41 42 func handleNetworkConfiguration(tr config.Conf) error { 43 dir := filepath.Join(dirs.GlobalRootDir, "/etc/sysctl.d") 44 name := "10-snapd-network.conf" 45 content := bytes.NewBuffer(nil) 46 47 output, err := coreCfg(tr, "network.disable-ipv6") 48 if err != nil { 49 return nil 50 } 51 52 var sysctl string 53 switch output { 54 case "true": 55 sysctl = "net.ipv6.conf.all.disable_ipv6=1" 56 content.WriteString(sysctl + "\n") 57 case "false", "": 58 // Store the sysctl for the code below but don't write it to 59 // content so that the file setting this option gets removed. 60 sysctl = "net.ipv6.conf.all.disable_ipv6=0" 61 default: 62 return fmt.Errorf("unsupported disable-ipv6 option: %q", output) 63 } 64 dirContent := map[string]*osutil.FileState{} 65 if content.Len() > 0 { 66 dirContent[name] = &osutil.FileState{ 67 Content: content.Bytes(), 68 Mode: 0644, 69 } 70 } 71 72 // write the new config 73 glob := name 74 changed, removed, err := osutil.EnsureDirState(dir, glob, dirContent) 75 if err != nil { 76 return err 77 } 78 79 // load the new config into the kernel 80 if len(changed) > 0 || len(removed) > 0 { 81 output, err := exec.Command("sysctl", "-w", sysctl).CombinedOutput() 82 if err != nil { 83 return osutil.OutputErr(output, err) 84 } 85 } 86 87 return nil 88 }