2019-05-21 04:11:48 +00:00
|
|
|
// Copyright 2018 CNI authors
|
|
|
|
// Copyright 2019 HashiCorp
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
// The functions in this file are derived from:
|
|
|
|
// https://github.com/containernetworking/plugins/blob/0950a3607bf5e8a57c6a655c7e573e6aab0dc650/pkg/testutils/netns_linux.go
|
|
|
|
|
|
|
|
package nsutil
|
2019-04-29 17:35:15 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/containernetworking/plugins/pkg/ns"
|
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
)
|
|
|
|
|
2019-05-21 04:11:48 +00:00
|
|
|
// NetNSRunDir is the directory which new network namespaces will be bind mounted
|
|
|
|
const NetNSRunDir = "/var/run/netns"
|
2019-04-29 17:35:15 +00:00
|
|
|
|
2019-05-21 04:11:48 +00:00
|
|
|
// NewNS creates a new persistent (bind-mounted) network namespace and returns
|
|
|
|
// an object representing that namespace, without switching to it.
|
|
|
|
func NewNS(nsName string) (ns.NetNS, error) {
|
2019-04-29 17:35:15 +00:00
|
|
|
|
|
|
|
// Create the directory for mounting network namespaces
|
|
|
|
// This needs to be a shared mountpoint in case it is mounted in to
|
|
|
|
// other namespaces (containers)
|
2019-05-21 04:11:48 +00:00
|
|
|
err := os.MkdirAll(NetNSRunDir, 0755)
|
2019-04-29 17:35:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remount the namespace directory shared. This will fail if it is not
|
|
|
|
// already a mountpoint, so bind-mount it on to itself to "upgrade" it
|
|
|
|
// to a mountpoint.
|
2019-05-08 14:30:50 +00:00
|
|
|
err = unix.Mount("", NetNSRunDir, "none", unix.MS_SHARED|unix.MS_REC, "")
|
2019-04-29 17:35:15 +00:00
|
|
|
if err != nil {
|
|
|
|
if err != unix.EINVAL {
|
2019-05-08 14:30:50 +00:00
|
|
|
return nil, fmt.Errorf("mount --make-rshared %s failed: %q", NetNSRunDir, err)
|
2019-04-29 17:35:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Recursively remount /var/run/netns on itself. The recursive flag is
|
|
|
|
// so that any existing netns bindmounts are carried over.
|
2019-05-08 14:30:50 +00:00
|
|
|
err = unix.Mount(NetNSRunDir, NetNSRunDir, "none", unix.MS_BIND|unix.MS_REC, "")
|
2019-04-29 17:35:15 +00:00
|
|
|
if err != nil {
|
2019-05-08 14:30:50 +00:00
|
|
|
return nil, fmt.Errorf("mount --rbind %s %s failed: %q", NetNSRunDir, NetNSRunDir, err)
|
2019-04-29 17:35:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now we can make it shared
|
2019-05-08 14:30:50 +00:00
|
|
|
err = unix.Mount("", NetNSRunDir, "none", unix.MS_SHARED|unix.MS_REC, "")
|
2019-04-29 17:35:15 +00:00
|
|
|
if err != nil {
|
2019-05-08 14:30:50 +00:00
|
|
|
return nil, fmt.Errorf("mount --make-rshared %s failed: %q", NetNSRunDir, err)
|
2019-04-29 17:35:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// create an empty file at the mount point
|
2019-05-08 14:30:50 +00:00
|
|
|
nsPath := path.Join(NetNSRunDir, nsName)
|
2019-04-29 17:35:15 +00:00
|
|
|
mountPointFd, err := os.Create(nsPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
mountPointFd.Close()
|
|
|
|
|
|
|
|
// Ensure the mount point is cleaned up on errors; if the namespace
|
|
|
|
// was successfully mounted this will have no effect because the file
|
|
|
|
// is in-use
|
|
|
|
defer os.RemoveAll(nsPath)
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
|
|
|
|
// do namespace work in a dedicated goroutine, so that we can safely
|
|
|
|
// Lock/Unlock OSThread without upsetting the lock/unlock state of
|
|
|
|
// the caller of this function
|
|
|
|
go (func() {
|
|
|
|
defer wg.Done()
|
|
|
|
runtime.LockOSThread()
|
|
|
|
// Don't unlock. By not unlocking, golang will kill the OS thread when the
|
|
|
|
// goroutine is done (for go1.10+)
|
|
|
|
|
|
|
|
var origNS ns.NetNS
|
|
|
|
origNS, err = ns.GetNS(getCurrentThreadNetNSPath())
|
|
|
|
if err != nil {
|
2019-05-14 01:26:39 +00:00
|
|
|
err = fmt.Errorf("failed to get the current netns: %v", err)
|
2019-04-29 17:35:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer origNS.Close()
|
|
|
|
|
|
|
|
// create a new netns on the current thread
|
|
|
|
err = unix.Unshare(unix.CLONE_NEWNET)
|
|
|
|
if err != nil {
|
2019-05-14 01:26:39 +00:00
|
|
|
err = fmt.Errorf("error from unshare: %v", err)
|
2019-04-29 17:35:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put this thread back to the orig ns, since it might get reused (pre go1.10)
|
|
|
|
defer origNS.Set()
|
|
|
|
|
|
|
|
// bind mount the netns from the current thread (from /proc) onto the
|
|
|
|
// mount point. This causes the namespace to persist, even when there
|
|
|
|
// are no threads in the ns.
|
|
|
|
err = unix.Mount(getCurrentThreadNetNSPath(), nsPath, "none", unix.MS_BIND, "")
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("failed to bind mount ns at %s: %v", nsPath, err)
|
|
|
|
}
|
|
|
|
})()
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create namespace: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ns.GetNS(nsPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmountNS unmounts the NS held by the netns object
|
2019-05-21 04:11:48 +00:00
|
|
|
func UnmountNS(nsPath string) error {
|
2019-04-29 17:35:15 +00:00
|
|
|
// Only unmount if it's been bind-mounted (don't touch namespaces in /proc...)
|
2019-05-08 14:30:50 +00:00
|
|
|
if strings.HasPrefix(nsPath, NetNSRunDir) {
|
2019-04-29 17:35:15 +00:00
|
|
|
if err := unix.Unmount(nsPath, 0); err != nil {
|
|
|
|
return fmt.Errorf("failed to unmount NS: at %s: %v", nsPath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.Remove(nsPath); err != nil {
|
|
|
|
return fmt.Errorf("failed to remove ns path %s: %v", nsPath, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getCurrentThreadNetNSPath copied from pkg/ns
|
|
|
|
func getCurrentThreadNetNSPath() string {
|
|
|
|
// /proc/self/ns/net returns the namespace of the main thread, not
|
|
|
|
// of whatever thread this goroutine is running on. Make sure we
|
|
|
|
// use the thread's net namespace since the thread is switching around
|
|
|
|
return fmt.Sprintf("/proc/%d/task/%d/ns/net", os.Getpid(), unix.Gettid())
|
|
|
|
}
|