-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
validation.go
45 lines (37 loc) · 1.29 KB
/
validation.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
package isolation
import (
"encoding/json"
"fmt"
"os/exec"
v1 "kubevirt.io/api/core/v1"
"kubevirt.io/client-go/log"
virt_chroot "kubevirt.io/kubevirt/pkg/virt-handler/virt-chroot"
containerdisk "kubevirt.io/kubevirt/pkg/container-disk"
)
const (
QEMUIMGPath = "/usr/bin/qemu-img"
)
func GetImageInfo(imagePath string, context IsolationResult, config *v1.DiskVerification) (*containerdisk.DiskInfo, error) {
memoryLimit := fmt.Sprintf("%d", config.MemoryLimit.Value())
// #nosec g204 no risk to use MountNamespace() argument as it returns a fixed string of "/proc/<pid>/ns/mnt"
cmd := virt_chroot.ExecChroot(
"--user", "qemu", "--memory", memoryLimit, "--cpu", "10", "--mount", context.MountNamespace(), "exec", "--",
QEMUIMGPath, "info", imagePath, "--output", "json",
)
log.Log.V(3).Infof("fetching image info. running command: %s", cmd.String())
out, err := cmd.Output()
if err != nil {
if e, ok := err.(*exec.ExitError); ok {
if len(e.Stderr) > 0 {
return nil, fmt.Errorf("failed to invoke qemu-img: %v: '%v'", err, string(e.Stderr))
}
}
return nil, fmt.Errorf("failed to invoke qemu-img: %v", err)
}
info := &containerdisk.DiskInfo{}
err = json.Unmarshal(out, info)
if err != nil {
return nil, fmt.Errorf("failed to parse disk info: %v", err)
}
return info, err
}