码迷,mamicode.com
首页 > 其他好文 > 详细

kata namespace

时间:2020-12-07 12:33:25      阅读:6      评论:0      收藏:0      [点我收藏+]

标签:ring   lag   style   gns   led   runtime   erro   share   return   

 

 

// setupPersistentNs creates persistent namespace without switchin to it.
// Note, pid namespaces cannot be persisted.
func setupPersistentNs(namespaceType nsType) (*namespace, error) {

        err := os.MkdirAll(persistentNsDir, 0755)
        if err != nil {
                return nil, err
        }

        // Create an empty file at the mount point.
        nsPath := filepath.Join(persistentNsDir, string(namespaceType))

        mountFd, err := os.Create(nsPath)
        if err != nil {
                return nil, err
        }
        mountFd.Close()

        var wg sync.WaitGroup
        wg.Add(1)

        go (func() {
                defer wg.Done()
                runtime.LockOSThread()

                var origNsFd *os.File
                origNsPath := getCurrentThreadNSPath(namespaceType)
                origNsFd, err = os.Open(origNsPath)
                if err != nil {
                        return
                }
                defer origNsFd.Close()

                // Create a new netns on the current thread.
                err = unix.Unshare(cloneFlagsTable[namespaceType])
                if err != nil {
                        return
                }

                // Bind mount the new namespace from the current thread onto the mount point to persist it.
                err = unix.Mount(getCurrentThreadNSPath(namespaceType), nsPath, "none", unix.MS_BIND, "")
                if err != nil {
                        return
                }

                // Switch back to original namespace.
                if err = unix.Setns(int(origNsFd.Fd()), cloneFlagsTable[namespaceType]); err != nil {
                        return
                }

        })()
        wg.Wait()

        if err != nil {
                unix.Unmount(nsPath, unix.MNT_DETACH)
                return nil, fmt.Errorf("failed to create namespace: %v", err)
        }

        return &namespace{path: nsPath}, nil
}

 

kata namespace

标签:ring   lag   style   gns   led   runtime   erro   share   return   

原文地址:https://www.cnblogs.com/dream397/p/14073906.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!