github.com/emc-advanced-dev/unik@v0.0.0-20190717152701-a58d3e8e33b7/pkg/providers/qemu/create_volume.go (about) 1 package qemu 2 3 import ( 4 "os" 5 "path/filepath" 6 "time" 7 8 "github.com/sirupsen/logrus" 9 "github.com/emc-advanced-dev/pkg/errors" 10 "github.com/solo-io/unik/pkg/providers/common" 11 "github.com/solo-io/unik/pkg/types" 12 ) 13 14 func (p *QemuProvider) CreateVolume(params types.CreateVolumeParams) (_ *types.Volume, err error) { 15 if _, volumeErr := p.GetImage(params.Name); volumeErr == nil { 16 return nil, errors.New("volume already exists", nil) 17 } 18 19 volumePath := getVolumePath(params.Name) 20 if err := os.MkdirAll(filepath.Dir(volumePath), 0755); err != nil { 21 return nil, errors.New("creating directory for volume file", err) 22 } 23 defer func() { 24 if err != nil { 25 if params.NoCleanup { 26 logrus.Warnf("because --no-cleanup flag was provided, not cleaning up failed volume %s at %s", params.Name, volumePath) 27 } else { 28 os.RemoveAll(filepath.Dir(volumePath)) 29 } 30 } 31 }() 32 logrus.WithField("raw-image", params.ImagePath).Infof("creating volume from raw image") 33 if err := common.ConvertRawImage(types.ImageFormat_RAW, types.ImageFormat_QCOW2, params.ImagePath, volumePath); err != nil { 34 return nil, errors.New("converting raw image to vmdk", err) 35 } 36 37 rawImageFile, err := os.Stat(params.ImagePath) 38 if err != nil { 39 return nil, errors.New("statting raw image file", err) 40 } 41 sizeMb := rawImageFile.Size() >> 20 42 43 volume := &types.Volume{ 44 Id: params.Name, 45 Name: params.Name, 46 SizeMb: sizeMb, 47 Attachment: "", 48 Infrastructure: types.Infrastructure_QEMU, 49 Created: time.Now(), 50 } 51 52 if err := p.state.ModifyVolumes(func(volumes map[string]*types.Volume) error { 53 volumes[volume.Id] = volume 54 return nil 55 }); err != nil { 56 return nil, errors.New("modifying volume map in state", err) 57 } 58 return volume, nil 59 60 }