github.com/containerd/Containerd@v1.4.13/snapshots/devmapper/config.go (about) 1 // +build linux 2 3 /* 4 Copyright The containerd Authors. 5 6 Licensed under the Apache License, Version 2.0 (the "License"); 7 you may not use this file except in compliance with the License. 8 You may obtain a copy of the License at 9 10 http://www.apache.org/licenses/LICENSE-2.0 11 12 Unless required by applicable law or agreed to in writing, software 13 distributed under the License is distributed on an "AS IS" BASIS, 14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 See the License for the specific language governing permissions and 16 limitations under the License. 17 */ 18 19 package devmapper 20 21 import ( 22 "fmt" 23 "os" 24 25 "github.com/BurntSushi/toml" 26 "github.com/docker/go-units" 27 "github.com/hashicorp/go-multierror" 28 "github.com/pkg/errors" 29 ) 30 31 // Config represents device mapper configuration loaded from file. 32 // Size units can be specified in human-readable string format (like "32KIB", "32GB", "32Tb") 33 type Config struct { 34 // Device snapshotter root directory for metadata 35 RootPath string `toml:"root_path"` 36 37 // Name for 'thin-pool' device to be used by snapshotter (without /dev/mapper/ prefix) 38 PoolName string `toml:"pool_name"` 39 40 // Defines how much space to allocate when creating base image for container 41 BaseImageSize string `toml:"base_image_size"` 42 BaseImageSizeBytes uint64 `toml:"-"` 43 44 // Flag to async remove device using Cleanup() callback in snapshots GC 45 AsyncRemove bool `toml:"async_remove"` 46 } 47 48 // LoadConfig reads devmapper configuration file from disk in TOML format 49 func LoadConfig(path string) (*Config, error) { 50 if _, err := os.Stat(path); err != nil { 51 if os.IsNotExist(err) { 52 return nil, os.ErrNotExist 53 } 54 55 return nil, err 56 } 57 58 config := Config{} 59 if _, err := toml.DecodeFile(path, &config); err != nil { 60 return nil, errors.Wrapf(err, "failed to unmarshal data at '%s'", path) 61 } 62 63 if err := config.parse(); err != nil { 64 return nil, err 65 } 66 67 if err := config.Validate(); err != nil { 68 return nil, err 69 } 70 71 return &config, nil 72 } 73 74 func (c *Config) parse() error { 75 baseImageSize, err := units.RAMInBytes(c.BaseImageSize) 76 if err != nil { 77 return errors.Wrapf(err, "failed to parse base image size: '%s'", c.BaseImageSize) 78 } 79 80 c.BaseImageSizeBytes = uint64(baseImageSize) 81 return nil 82 } 83 84 // Validate makes sure configuration fields are valid 85 func (c *Config) Validate() error { 86 var result *multierror.Error 87 88 if c.PoolName == "" { 89 result = multierror.Append(result, fmt.Errorf("pool_name is required")) 90 } 91 92 if c.RootPath == "" { 93 result = multierror.Append(result, fmt.Errorf("root_path is required")) 94 } 95 96 if c.BaseImageSize == "" { 97 result = multierror.Append(result, fmt.Errorf("base_image_size is required")) 98 } 99 100 return result.ErrorOrNil() 101 }