gitee.com/leisunstar/runtime@v0.0.0-20200521203717-5cef3e7b53f9/pkg/katautils/oci.go (about)

     1  // Copyright (c) 2018 Intel Corporation
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  //
     5  
     6  package katautils
     7  
     8  import (
     9  	"context"
    10  	"fmt"
    11  	"io/ioutil"
    12  	"os"
    13  	"path/filepath"
    14  	"strings"
    15  
    16  	"github.com/kata-containers/runtime/virtcontainers/pkg/rootless"
    17  )
    18  
    19  const ctrsMappingDirMode = os.FileMode(0750)
    20  
    21  var ctrsMapTreePath = "/var/run/kata-containers/containers-mapping"
    22  
    23  // SetCtrsMapTreePath let the testcases change the ctrsMapTreePath to a test dir
    24  func SetCtrsMapTreePath(path string) {
    25  	ctrsMapTreePath = path
    26  }
    27  
    28  // doUpdatePath returns whether a ctrsMapTreePath needs to be updated with a rootless prefix
    29  func doUpdatePath() bool {
    30  	return rootless.IsRootless() && !strings.HasPrefix(ctrsMapTreePath, rootless.GetRootlessDir())
    31  }
    32  
    33  // FetchContainerIDMapping This function assumes it should find only one file inside the container
    34  // ID directory. If there are several files, we could not determine which
    35  // file name corresponds to the sandbox ID associated, and this would throw
    36  // an error.
    37  func FetchContainerIDMapping(containerID string) (string, error) {
    38  	if containerID == "" {
    39  		return "", fmt.Errorf("Missing container ID")
    40  	}
    41  
    42  	if doUpdatePath() {
    43  		SetCtrsMapTreePath(filepath.Join(rootless.GetRootlessDir(), ctrsMapTreePath))
    44  	}
    45  
    46  	dirPath := filepath.Join(ctrsMapTreePath, containerID)
    47  
    48  	files, err := ioutil.ReadDir(dirPath)
    49  	if err != nil {
    50  		if os.IsNotExist(err) {
    51  			return "", nil
    52  		}
    53  
    54  		return "", err
    55  	}
    56  
    57  	if len(files) != 1 {
    58  		return "", fmt.Errorf("Too many files (%d) in %q", len(files), dirPath)
    59  	}
    60  
    61  	return files[0].Name(), nil
    62  }
    63  
    64  // AddContainerIDMapping add a container id mapping to sandbox id
    65  func AddContainerIDMapping(ctx context.Context, containerID, sandboxID string) error {
    66  	span, _ := Trace(ctx, "addContainerIDMapping")
    67  	defer span.Finish()
    68  
    69  	if containerID == "" {
    70  		return fmt.Errorf("Missing container ID")
    71  	}
    72  
    73  	if sandboxID == "" {
    74  		return fmt.Errorf("Missing sandbox ID")
    75  	}
    76  
    77  	if doUpdatePath() {
    78  		SetCtrsMapTreePath(filepath.Join(rootless.GetRootlessDir(), ctrsMapTreePath))
    79  	}
    80  	parentPath := filepath.Join(ctrsMapTreePath, containerID)
    81  
    82  	if err := os.RemoveAll(parentPath); err != nil {
    83  		return err
    84  	}
    85  
    86  	path := filepath.Join(parentPath, sandboxID)
    87  
    88  	if err := os.MkdirAll(path, ctrsMappingDirMode); err != nil {
    89  		return err
    90  	}
    91  
    92  	return nil
    93  }
    94  
    95  // DelContainerIDMapping delete container id mapping from a sandbox
    96  func DelContainerIDMapping(ctx context.Context, containerID string) error {
    97  	span, _ := Trace(ctx, "delContainerIDMapping")
    98  	defer span.Finish()
    99  
   100  	if containerID == "" {
   101  		return fmt.Errorf("Missing container ID")
   102  	}
   103  
   104  	if doUpdatePath() {
   105  		SetCtrsMapTreePath(filepath.Join(rootless.GetRootlessDir(), ctrsMapTreePath))
   106  	}
   107  	path := filepath.Join(ctrsMapTreePath, containerID)
   108  
   109  	return os.RemoveAll(path)
   110  }