github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/container/lxd/storage.go (about)

     1  // Copyright 2018 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package lxd
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"github.com/lxc/lxd/shared/api"
     9  )
    10  
    11  func (s *Server) StorageSupported() bool {
    12  	return s.storageAPISupport
    13  }
    14  
    15  func (s *Server) CreatePool(name, driver string, cfg map[string]string) error {
    16  	req := api.StoragePoolsPost{
    17  		Name:           name,
    18  		Driver:         driver,
    19  		StoragePoolPut: api.StoragePoolPut{Config: cfg},
    20  	}
    21  	return errors.Annotatef(s.CreateStoragePool(req), "creating storage pool %q", name)
    22  }
    23  
    24  func (s *Server) CreateVolume(pool, name string, cfg map[string]string) error {
    25  	req := api.StorageVolumesPost{
    26  		Name:             name,
    27  		Type:             "custom",
    28  		StorageVolumePut: api.StorageVolumePut{Config: cfg},
    29  	}
    30  	return errors.Annotatef(s.CreateStoragePoolVolume(pool, req), "creating storage pool volume %q", name)
    31  }
    32  
    33  // EnsureDefaultStorage ensures that the input profile is configured with a
    34  // disk device, creating a new storage pool and a device if required.
    35  func (s *Server) EnsureDefaultStorage(profile *api.Profile, eTag string) error {
    36  	// If there is already a "/" device, we have nothing to do.
    37  	for _, dev := range profile.Devices {
    38  		if dev["path"] == "/" {
    39  			return nil
    40  		}
    41  	}
    42  
    43  	// If there is a "default" pool, use it.
    44  	// Otherwise if there are other pools available, choose the first.
    45  	pools, err := s.GetStoragePoolNames()
    46  	if err != nil {
    47  		return errors.Trace(err)
    48  	}
    49  	poolName := ""
    50  	for _, p := range pools {
    51  		if p == "default" {
    52  			poolName = p
    53  		}
    54  	}
    55  	if poolName == "" && len(pools) > 0 {
    56  		poolName = pools[0]
    57  	}
    58  
    59  	// We need to create a new storage pool.
    60  	if poolName == "" {
    61  		poolName = "default"
    62  		req := api.StoragePoolsPost{
    63  			Name:   poolName,
    64  			Driver: "dir",
    65  		}
    66  		err := s.CreateStoragePool(req)
    67  		if err != nil {
    68  			return errors.Trace(err)
    69  		}
    70  	}
    71  
    72  	// Create a new disk device in the input profile.
    73  	if profile.Devices == nil {
    74  		profile.Devices = map[string]device{}
    75  	}
    76  	profile.Devices["root"] = map[string]string{
    77  		"type": "disk",
    78  		"path": "/",
    79  		"pool": poolName,
    80  	}
    81  
    82  	if err := s.UpdateProfile(profile.Name, profile.Writable(), eTag); err != nil {
    83  		return errors.Trace(err)
    84  	}
    85  	logger.Debugf("created new disk device \"root\" in profile %q", profile.Name)
    86  	return nil
    87  }