namespace

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 12, 2021 License: Apache-2.0 Imports: 8 Imported by: 0

README

Linux Namespace Build Status GoDoc License

The operation about linux namespace. Support Go1.7+.

Installation

$ go get -u github.com/xgfone/go-namespace

Example

package main

import (
    "fmt"

    "github.com/xgfone/go-namespace"
)

func main() {
    ns := NewNameSpace("name")

    // Ensure that the namespace exists.
    if exist, err := ns.IsExist(); err != nil {
        fmt.Println(err)
        return
    } else if !exist {
        if err := ns.Create(); err != nil {
            fmt.Println(err)
            return
        }
    }

    // Execute the command in the namespace.
    if output, err := ns.Exec("ip", "a"); err != nil {
        fmt.Println(err)
    } else {
        fmt.Println(output)
    }

    // Delete the namespace.
    if err := ns.Delete(); err != nil {
        fmt.Println(err)
        return
    }
}

Documentation

Overview

Package namespace supplies the operation about linux namespace.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoNameSpace = errors.New("no namespace")

ErrNoNameSpace is returned when no namespace associates with a given process.

Functions

This section is empty.

Types

type NameSpace

type NameSpace struct {
	Name string
}

NameSpace is the Linux NameSpace instance.

func GetAllNameSpace

func GetAllNameSpace() (nss []NameSpace, err error)

GetAllNameSpace returns all the namespace instances.

func NewNameSpace

func NewNameSpace(name string) NameSpace

NewNameSpace returns a new NameSpace instance named name.

func NewNameSpaceFromPid

func NewNameSpaceFromPid(pid int) (NameSpace, error)

NewNameSpaceFromPid returns a new NameSpace instance by the process pid.

func (NameSpace) Create

func (ns NameSpace) Create() (err error)

Create creates the namespace.

func (NameSpace) Delete

func (ns NameSpace) Delete() (err error)

Delete deletes the namespance.

func (NameSpace) Exec

func (ns NameSpace) Exec(cmd string, args ...string) (output string, err error)

Exec executes a shell command in the current namespace.

func (NameSpace) IsExist

func (ns NameSpace) IsExist() (exist bool, err error)

IsExist reports whether the namespace exists or not.

func (NameSpace) Pids

func (ns NameSpace) Pids() (pids []int, err error)

Pids returns all the pids in the current namespace.

func (NameSpace) String

func (ns NameSpace) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL