-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
emptydisk.go
40 lines (32 loc) · 942 Bytes
/
emptydisk.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
package emptydisk
import (
"os"
"os/exec"
"path"
"strconv"
"kubevirt.io/kubevirt/pkg/api/v1"
)
var EmptyDiskBaseDir = "/var/run/libvirt/empty-disks/"
func CreateTemporaryDisks(vm *v1.VirtualMachine) error {
for _, volume := range vm.Spec.Volumes {
if volume.EmptyDisk != nil {
// qemu-img takes the size in bytes or in Kibibytes/Mebibytes/...; lets take bytes
size := strconv.FormatInt(volume.EmptyDisk.Capacity.ToDec().ScaledValue(0), 10)
file := FilePathForVolumeName(volume.Name)
if err := os.MkdirAll(EmptyDiskBaseDir, 0777); err != nil {
return err
}
if _, err := os.Stat(file); os.IsNotExist(err) {
if err := exec.Command("qemu-img", "create", "-f", "qcow2", file, size).Run(); err != nil {
return err
}
} else if err != nil {
return err
}
}
}
return nil
}
func FilePathForVolumeName(volumeName string) string {
return path.Join(EmptyDiskBaseDir, volumeName+".qcow2")
}