zoukankan      html  css  js  c++  java
  • exec.Command("/proc/self/exe", "child")

    package main
    
    import (
            "flag"
            "os"
            "os/exec"
            "syscall"
    
            "github.com/sirupsen/logrus"
    )
    
    func main() {
            var nsShell, nsHostName, rootPath string
            flag.StringVar(&nsShell, "nsshell", "/bin/bash", "The path to the shell where the namespace is running")
            flag.StringVar(&nsHostName, "nshostname", "nshost", "Path to the shell to use")
    //~/docker/container/rootfs会报错 flag.StringVar(
    &rootPath, "rootfs", "/root/docker/container/rootfs/", "Path to the root filesystem to use") flag.Parse() switch os.Args[1] { case "run": nsRun(nsShell, nsHostName, rootPath) case "child": chRoot(nsShell, rootPath) default: logrus.Errorf("wrong command") return } } //nsInit ns初始化 func nsInit(command, hostname, newRootPath string) { //check(mountRoot(newRootPath)) nsRun(command, hostname, newRootPath) } func nsRun(command, hostname, newRootPath string) { cmd := exec.Command("/proc/self/exe", "child") cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr cmd.SysProcAttr = &syscall.SysProcAttr{ Cloneflags: syscall.CLONE_NEWUTS | syscall.CLONE_NEWPID, } check(syscall.Sethostname([]byte(hostname))) check(cmd.Run()) } func chRoot(command, newroot string) { cmd := exec.Command(command) cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr check(syscall.Chroot(newroot)) check(os.Chdir("/")) check(syscall.Mount("proc", "proc", "proc", 0, "")) check(cmd.Run()) check(syscall.Unmount("proc", 0)) } func check(err error) { if err != nil { logrus.Errorln(err) } }
    root@ubuntu:/home/ubuntu/test/learning/namespaces/PID_001# go run PID_001.go run
    root@nshost:/# ps -a
    PID   USER     TIME  COMMAND
        1 root      0:00 /proc/self/exe child
        6 root      0:00 /bin/bash
        7 root      0:00 ps -a
    root@nshost:/# hostname
    nshost
    root@nshost:/#

    network没有隔离

    host上

     

    设备隔离了

  • 相关阅读:
    黑客术语1
    leetcode笔记--3 Niim game
    台湾ML笔记--1.2 formalize the learning probelm
    台湾ML笔记--1.1什么时候适合使用ML
    leetcode笔记--2 reverse string
    leetcode笔记--1 two-sum
    数据挖掘导论笔记1
    python基础----ipython快捷键
    记录新的开始
    编译器错误消息: CS1617: 选项“6”对 /langversion 无效
  • 原文地址:https://www.cnblogs.com/dream397/p/14031056.html
Copyright © 2011-2022 走看看