github.com/demonoid81/containerd@v1.3.4/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  
    45  // LoadConfig reads devmapper configuration file from disk in TOML format
    46  func LoadConfig(path string) (*Config, error) {
    47  	if _, err := os.Stat(path); err != nil {
    48  		if os.IsNotExist(err) {
    49  			return nil, os.ErrNotExist
    50  		}
    51  
    52  		return nil, err
    53  	}
    54  
    55  	config := Config{}
    56  	if _, err := toml.DecodeFile(path, &config); err != nil {
    57  		return nil, errors.Wrapf(err, "failed to unmarshal data at '%s'", path)
    58  	}
    59  
    60  	if err := config.parse(); err != nil {
    61  		return nil, err
    62  	}
    63  
    64  	if err := config.Validate(); err != nil {
    65  		return nil, err
    66  	}
    67  
    68  	return &config, nil
    69  }
    70  
    71  func (c *Config) parse() error {
    72  	baseImageSize, err := units.RAMInBytes(c.BaseImageSize)
    73  	if err != nil {
    74  		return errors.Wrapf(err, "failed to parse base image size: '%s'", c.BaseImageSize)
    75  	}
    76  
    77  	c.BaseImageSizeBytes = uint64(baseImageSize)
    78  	return nil
    79  }
    80  
    81  // Validate makes sure configuration fields are valid
    82  func (c *Config) Validate() error {
    83  	var result *multierror.Error
    84  
    85  	if c.PoolName == "" {
    86  		result = multierror.Append(result, fmt.Errorf("pool_name is required"))
    87  	}
    88  
    89  	if c.RootPath == "" {
    90  		result = multierror.Append(result, fmt.Errorf("root_path is required"))
    91  	}
    92  
    93  	if c.BaseImageSize == "" {
    94  		result = multierror.Append(result, fmt.Errorf("base_image_size is required"))
    95  	}
    96  
    97  	return result.ErrorOrNil()
    98  }