github.com/hanks177/podman/v4@v4.1.3-0.20220613032544-16d90015bc83/pkg/bindings/containers/mount.go (about)

     1  package containers
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  
     7  	"github.com/hanks177/podman/v4/pkg/bindings"
     8  )
     9  
    10  // Mount mounts an existing container to the filesystem. It returns the path
    11  // of the mounted container in string format.
    12  func Mount(ctx context.Context, nameOrID string, options *MountOptions) (string, error) {
    13  	if options == nil {
    14  		options = new(MountOptions)
    15  	}
    16  	_ = options
    17  	conn, err := bindings.GetClient(ctx)
    18  	if err != nil {
    19  		return "", err
    20  	}
    21  	var (
    22  		path string
    23  	)
    24  	response, err := conn.DoRequest(ctx, nil, http.MethodPost, "/containers/%s/mount", nil, nil, nameOrID)
    25  	if err != nil {
    26  		return path, err
    27  	}
    28  	defer response.Body.Close()
    29  
    30  	return path, response.Process(&path)
    31  }
    32  
    33  // Unmount unmounts a container from the filesystem.  The container must not be running
    34  // or the unmount will fail.
    35  func Unmount(ctx context.Context, nameOrID string, options *UnmountOptions) error {
    36  	if options == nil {
    37  		options = new(UnmountOptions)
    38  	}
    39  	_ = options
    40  	conn, err := bindings.GetClient(ctx)
    41  	if err != nil {
    42  		return err
    43  	}
    44  	response, err := conn.DoRequest(ctx, nil, http.MethodPost, "/containers/%s/unmount", nil, nil, nameOrID)
    45  	if err != nil {
    46  		return err
    47  	}
    48  	defer response.Body.Close()
    49  
    50  	return response.Process(nil)
    51  }
    52  
    53  // GetMountedContainerPaths returns a map of mounted containers and their mount locations.
    54  func GetMountedContainerPaths(ctx context.Context, options *MountedContainerPathsOptions) (map[string]string, error) {
    55  	if options == nil {
    56  		options = new(MountedContainerPathsOptions)
    57  	}
    58  	_ = options
    59  	conn, err := bindings.GetClient(ctx)
    60  	if err != nil {
    61  		return nil, err
    62  	}
    63  	mounts := make(map[string]string)
    64  	response, err := conn.DoRequest(ctx, nil, http.MethodGet, "/containers/showmounted", nil, nil)
    65  	if err != nil {
    66  		return mounts, err
    67  	}
    68  	defer response.Body.Close()
    69  
    70  	return mounts, response.Process(&mounts)
    71  }