github.com/blixtra/rkt@v0.8.1-0.20160204105720-ab0d1add1a43/stage1/init/common/mount.go (about)

     1  // Copyright 2015 The rkt Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package common
    16  
    17  import (
    18  	"github.com/appc/spec/schema"
    19  	"github.com/appc/spec/schema/types"
    20  )
    21  
    22  func isMPReadOnly(mountPoints []types.MountPoint, name types.ACName) bool {
    23  	for _, mp := range mountPoints {
    24  		if mp.Name == name {
    25  			return mp.ReadOnly
    26  		}
    27  	}
    28  
    29  	return false
    30  }
    31  
    32  // IsMountReadOnly returns if a mount should be readOnly.
    33  // If the readOnly flag in the pod manifest is not nil, it overrides the
    34  // readOnly flag in the image manifest.
    35  func IsMountReadOnly(vol types.Volume, mountPoints []types.MountPoint) bool {
    36  	if vol.ReadOnly != nil {
    37  		return *vol.ReadOnly
    38  	}
    39  
    40  	return isMPReadOnly(mountPoints, vol.Name)
    41  }
    42  
    43  // GenerateMounts maps MountPoint paths to volumes, returning a list of Mounts.
    44  func GenerateMounts(ra *schema.RuntimeApp, volumes map[types.ACName]types.Volume) []schema.Mount {
    45  	app := ra.App
    46  
    47  	mnts := make(map[string]schema.Mount)
    48  	for _, m := range ra.Mounts {
    49  		mnts[m.Path] = m
    50  	}
    51  
    52  	for _, mp := range app.MountPoints {
    53  		// there's already an injected mount for this target path, skip
    54  		if _, ok := mnts[mp.Path]; ok {
    55  			continue
    56  		}
    57  		vol, ok := volumes[mp.Name]
    58  		// there is no volume for this mount point, creating an "empty" volume
    59  		// implicitly
    60  		if !ok {
    61  			defaultMode := "0755"
    62  			defaultUID := 0
    63  			defaultGID := 0
    64  			emptyVol := types.Volume{
    65  				Name: mp.Name,
    66  				Kind: "empty",
    67  				Mode: &defaultMode,
    68  				UID:  &defaultUID,
    69  				GID:  &defaultGID,
    70  			}
    71  
    72  			diag.Printf("warning: no volume specified for mount point %q, implicitly creating an \"empty\" volume. This volume will be removed when the pod is garbage-collected.", mp.Name)
    73  
    74  			volumes[mp.Name] = emptyVol
    75  			ra.Mounts = append(ra.Mounts, schema.Mount{Volume: mp.Name, Path: mp.Path})
    76  		} else {
    77  			ra.Mounts = append(ra.Mounts, schema.Mount{Volume: vol.Name, Path: mp.Path})
    78  		}
    79  	}
    80  
    81  	return ra.Mounts
    82  }