github.com/MerlinKodo/gvisor@v0.0.0-20231110090155-957f62ecf90e/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 mediumVals := make([]string, 0, len(*o)) 61 for _, medium := range *o { 62 mediumVals = append(mediumVals, strconv.Itoa(int(medium))) 63 } 64 return strings.Join(mediumVals, ",") 65 } 66 67 // Get implements flag.Value. 68 func (o *OverlayMediumFlags) Get() any { 69 return o 70 } 71 72 // GetArray returns an array of mappings. 73 func (o *OverlayMediumFlags) GetArray() []OverlayMedium { 74 return *o 75 } 76 77 // Set implements flag.Value and appends an overlay medium from the command 78 // line to the mediums array. Set(String()) should be idempotent. 79 func (o *OverlayMediumFlags) Set(s string) error { 80 mediums := strings.Split(s, ",") 81 for _, medium := range mediums { 82 mediumVal, err := strconv.Atoi(medium) 83 if err != nil { 84 return fmt.Errorf("invalid OverlayMedium value (%d): %v", mediumVal, err) 85 } 86 if mediumVal > int(AnonDirMedium) { 87 return fmt.Errorf("invalid OverlayMedium value (%d)", mediumVal) 88 } 89 *o = append(*o, OverlayMedium(mediumVal)) 90 } 91 return nil 92 }