Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
Already on GitHub? Sign in to your account
many: move network initialization to a separate service. #1765
Closed
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
Jump to file or symbol
Failed to load files and symbols.
| @@ -0,0 +1,45 @@ | ||
| +// -*- Mode: Go; indent-tabs-mode: t -*- | ||
| + | ||
| +/* | ||
| + * Copyright (C) 2016 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 main | ||
| + | ||
| +import ( | ||
| + "github.com/jessevdk/go-flags" | ||
| + | ||
| + "github.com/snapcore/snapd/overlord/boot" | ||
| +) | ||
| + | ||
| +type cmdInternalInitNetwork struct{} | ||
| + | ||
| +func init() { | ||
| + cmd := addCommand("init-network", | ||
| + "internal", | ||
| + "internal", func() flags.Commander { | ||
| + return &cmdInternalInitNetwork{} | ||
| + }) | ||
| + cmd.hidden = true | ||
| +} | ||
| + | ||
| +func (x *cmdInternalInitNetwork) Execute(args []string) error { | ||
| + if len(args) > 0 { | ||
| + return ErrExtraArgs | ||
| + } | ||
| + | ||
| + return boot.InitialNetworkConfig() | ||
| +} |
| @@ -0,0 +1,14 @@ | ||
| +[Unit] | ||
| +Description=Apply default network configuration if none exists | ||
| +After=local-fs.target | ||
| +Before=network-pre.target snapd.service | ||
| +DefaultDependencies=false | ||
| +ConditionPathExistsGlob=!/etc/netplan/*.yaml | ||
| + | ||
| +[Service] | ||
| +Type=oneshot | ||
| +ExecStart=/usr/bin/snap init-network | ||
| +RemainAfterExit=yes | ||
| + | ||
| +[Install] | ||
| +WantedBy=multi-user.target |
| @@ -1,2 +1,3 @@ | ||
| debian/snapd.boot-ok.service /lib/systemd/system/ | ||
| debian/snapd.firstboot.service /lib/systemd/system/ | ||
| +debian/snapd.initnetwork.service /lib/systemd/system/ |
| @@ -0,0 +1,49 @@ | ||
| +package boot | ||
| + | ||
| +import ( | ||
| + "os" | ||
| + "os/exec" | ||
| + "path/filepath" | ||
| + | ||
| + "github.com/snapcore/snapd/osutil" | ||
| +) | ||
| + | ||
| +var netplanConfigFile = "/run/netplan/00-initial-config.yaml" | ||
| +var enableConfig = []string{"netplan", "apply"} | ||
| + | ||
| +var netplanConfigData = ` | ||
| +network: | ||
| + version: 2 | ||
| + ethernets: | ||
| + all: | ||
| + match: | ||
| + name: "*" | ||
| + dhcp4: true | ||
| +` | ||
| + | ||
| +var osRemove = os.Remove | ||
| + | ||
| +// InitialNetworkConfig writes and applies a netplan config that | ||
| +// enables dhcp on all wired interfaces. In the long run this should | ||
| +// be run as part of the config-changed hook and read the snap's | ||
| +// config to determine the netplan config to write. | ||
| +func InitialNetworkConfig() error { | ||
| + if err := os.MkdirAll(filepath.Dir(netplanConfigFile), 0755); err != nil { | ||
| + return err | ||
| + } | ||
| + if err := osutil.AtomicWriteFile(netplanConfigFile, []byte(netplanConfigData), 0644, 0); err != nil { | ||
| + return err | ||
| + } | ||
| + | ||
| + enable := exec.Command(enableConfig[0], enableConfig[1:]...) | ||
| + enable.Stdout = os.Stdout | ||
| + enable.Stderr = os.Stderr | ||
| + if err := enable.Run(); err != nil { | ||
| + return err | ||
| + } | ||
| + if err := osRemove(netplanConfigFile); err != nil { | ||
| + return err | ||
| + } | ||
| + | ||
| + return nil | ||
| +} |