github.com/nicocha30/gvisor-ligolo@v0.0.0-20230726075806-989fa2c0a413/runsc/boot/overlay.go (about)

     1  // Copyright 2023 The gVisor 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 boot
    16  
    17  import (
    18  	"fmt"
    19  	"strconv"
    20  	"strings"
    21  )
    22  
    23  // OverlayMedium describes the medium that will be used to back the
    24  // overlay mount's upper layer.
    25  type OverlayMedium int
    26  
    27  const (
    28  	// NoOverlay indicates that this mount should not be overlaid.
    29  	NoOverlay OverlayMedium = iota
    30  
    31  	// MemoryMedium indicates that this mount should be overlaid with an
    32  	// upper layer backed by application memory.
    33  	MemoryMedium
    34  
    35  	// SelfMedium indicates that this mount should be overlaid with an upper
    36  	// layer backed by a host file in the mount's source directory.
    37  	SelfMedium
    38  
    39  	// AnonDirMedium indicates that this mount should be overlaid with an upper
    40  	// layer backed by a host file in an anonymous directory.
    41  	AnonDirMedium
    42  )
    43  
    44  // IsBackedByHostFile returns true if the overlay is backed by a host file.
    45  func (o *OverlayMedium) IsBackedByHostFile() bool {
    46  	return *o == SelfMedium || *o == AnonDirMedium
    47  }
    48  
    49  // IsEnabled returns true if an overlay is applied.
    50  func (o *OverlayMedium) IsEnabled() bool {
    51  	return *o != NoOverlay
    52  }
    53  
    54  // OverlayMediumFlags can be used with OverlayMedium flags that appear
    55  // multiple times.
    56  type OverlayMediumFlags []OverlayMedium
    57  
    58  // String implements flag.Value.
    59  func (o *OverlayMediumFlags) String() string {
    60  	return fmt.Sprintf("%v", *o)
    61  }
    62  
    63  // Get implements flag.Value.
    64  func (o *OverlayMediumFlags) Get() any {
    65  	return o
    66  }
    67  
    68  // GetArray returns an array of mappings.
    69  func (o *OverlayMediumFlags) GetArray() []OverlayMedium {
    70  	return *o
    71  }
    72  
    73  // Set implements flag.Value and appends an overlay medium from the command
    74  // line to the mediums array.
    75  func (o *OverlayMediumFlags) Set(s string) error {
    76  	mediums := strings.Split(s, ",")
    77  	for _, medium := range mediums {
    78  		mediumVal, err := strconv.Atoi(medium)
    79  		if err != nil {
    80  			return fmt.Errorf("invalid OverlayMedium value (%d): %v", mediumVal, err)
    81  		}
    82  		if mediumVal > int(AnonDirMedium) {
    83  			return fmt.Errorf("invalid OverlayMedium value (%d)", mediumVal)
    84  		}
    85  		*o = append(*o, OverlayMedium(mediumVal))
    86  	}
    87  	return nil
    88  }
    89  
    90  // ToOverlayMediumFlags converts []OverlayMedium to string format which can be
    91  // unpacked by OverlayMediumFlags.Set().
    92  func ToOverlayMediumFlags(mediums []OverlayMedium) string {
    93  	mediumVals := make([]string, 0, len(mediums))
    94  	for _, medium := range mediums {
    95  		mediumVals = append(mediumVals, strconv.Itoa(int(medium)))
    96  	}
    97  	return strings.Join(mediumVals, ",")
    98  }