github.com/Cloud-Foundations/Dominator@v0.3.4/hypervisor/manager/vmVolumes.go (about)

     1  package manager
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	"github.com/Cloud-Foundations/Dominator/lib/format"
     9  	"github.com/Cloud-Foundations/Dominator/lib/fsutil"
    10  	proto "github.com/Cloud-Foundations/Dominator/proto/hypervisor"
    11  )
    12  
    13  func (vm *vmInfoType) checkVolumes(grabLock bool) error {
    14  	if grabLock {
    15  		vm.mutex.RLock()
    16  		defer vm.mutex.RUnlock()
    17  	}
    18  	for index, volume := range vm.VolumeLocations {
    19  		expectedSize := vm.Volumes[index].Size
    20  		if fi, err := os.Stat(volume.Filename); err != nil {
    21  			return fmt.Errorf("error stating volume[%d]: %s", index, err)
    22  		} else if fi.Size() != int64(expectedSize) {
    23  			return fmt.Errorf("volume[%d] size expected: %s, found: %s",
    24  				index, format.FormatBytes(expectedSize),
    25  				format.FormatBytes(uint64(fi.Size())))
    26  		}
    27  	}
    28  	return nil
    29  }
    30  
    31  func (vm *vmInfoType) setupVolumes(rootSize uint64,
    32  	rootVolumeType proto.VolumeType, secondaryVolumes []proto.Volume,
    33  	spreadVolumes bool) error {
    34  	volumeDirectories, err := vm.manager.getVolumeDirectories(rootSize,
    35  		rootVolumeType, secondaryVolumes, spreadVolumes)
    36  	if err != nil {
    37  		return err
    38  	}
    39  	volumeDirectory := filepath.Join(volumeDirectories[0], vm.ipAddress)
    40  	os.RemoveAll(volumeDirectory)
    41  	if err := os.MkdirAll(volumeDirectory, fsutil.DirPerms); err != nil {
    42  		return err
    43  	}
    44  	filename := filepath.Join(volumeDirectory, "root")
    45  	vm.VolumeLocations = append(vm.VolumeLocations,
    46  		proto.LocalVolume{volumeDirectory, filename})
    47  	for index := range secondaryVolumes {
    48  		volumeDirectory := filepath.Join(volumeDirectories[index+1],
    49  			vm.ipAddress)
    50  		os.RemoveAll(volumeDirectory)
    51  		if err := os.MkdirAll(volumeDirectory, fsutil.DirPerms); err != nil {
    52  			return err
    53  		}
    54  		filename := filepath.Join(volumeDirectory, indexToName(index+1))
    55  		vm.VolumeLocations = append(vm.VolumeLocations,
    56  			proto.LocalVolume{volumeDirectory, filename})
    57  	}
    58  	return nil
    59  }