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