Skip to content

Commit e1c0b0c

Browse files
fix igvmmeasure build
1 parent 1a850f5 commit e1c0b0c

4 files changed

Lines changed: 61 additions & 9 deletions

File tree

init/init.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ MNT_DIR=/root
2525
BASE=$(pwd)
2626

2727
SRC=/dev/nbd2
28-
SRC_IP=147.91.12.238
28+
SRC_IP=10.172.192.30
2929
SRC_PORT=10809
3030

3131
DST=/dev/sda

manager/qemu/config.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,11 @@ type IGVMConfig struct {
7474
}
7575

7676
type DiskConfig struct {
77-
File string `env:"DISK_FILE" envDefault:"img/enc_os.qcow2"`
78-
ID string `env:"DISK_ID" envDefault:"disk0"`
79-
Format string `env:"DISK_FORMAT" envDefault:"qcow2"`
80-
SCSIID string `env:"DISK_SCSI_ID" envDefault:"scsi0"`
77+
SrcFile string `env:"SRC_DISK_FILE" envDefault:"img/enc_os.qcow2"`
78+
DstFile string `env:"DST_DISK_FILE" envDefault:"img/enc_os.qcow2"`
79+
ID string `env:"DISK_ID" envDefault:"disk0"`
80+
Format string `env:"DISK_FORMAT" envDefault:"qcow2"`
81+
SCSIID string `env:"DISK_SCSI_ID" envDefault:"scsi0"`
8182
}
8283

8384
type Config struct {
@@ -192,7 +193,7 @@ func (config Config) ConstructQemuArgs() []string {
192193
// disk image
193194
args = append(args, "-drive",
194195
fmt.Sprintf("file=%s,if=none,id=%s,format=%s",
195-
config.DiskConfig.File,
196+
config.DiskConfig.DstFile,
196197
config.DiskConfig.ID,
197198
config.DiskConfig.Format))
198199
args = append(args, "-device",

manager/qemu/vm.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package qemu
44

55
import (
6+
"encoding/json"
67
"fmt"
78
"log/slog"
89
"os"
@@ -22,6 +23,7 @@ const (
2223
KernelFile = "bzImage"
2324
rootfsFile = "rootfs.cpio"
2425
tmpDir = "/tmp"
26+
diskDstName = "cvmDisk"
2527
interval = 5 * time.Second
2628
shutdownTimeout = 30 * time.Second
2729
)
@@ -39,6 +41,10 @@ type qemuVM struct {
3941
vm.StateMachine
4042
}
4143

44+
type qemuInfo struct {
45+
VirtualSize int64 `json:"virtual-size"`
46+
}
47+
4248
func NewVM(config any, cvmId string, logger *slog.Logger) vm.VM {
4349
return &qemuVM{
4450
vmi: config.(VMInfo),
@@ -75,6 +81,22 @@ func (v *qemuVM) Start() (err error) {
7581
v.vmi.Config.OVMFVarsConfig.File = dstFile
7682
}
7783

84+
if v.vmi.Config.EnableDisk {
85+
sizeGB, err := GetVirtualSizeGB(v.vmi.Config.SrcFile)
86+
if err != nil {
87+
return err
88+
}
89+
90+
dstDiskFile := fmt.Sprintf("%s/%s-%s.qcow2", tmpDir, diskDstName, id)
91+
sizeArg := fmt.Sprintf("%dG", sizeGB)
92+
93+
cmd := exec.Command("qemu-img", "create", "-f", "qcow2", dstDiskFile, sizeArg)
94+
if out, err := cmd.CombinedOutput(); err != nil {
95+
return fmt.Errorf("qemu-img create failed: %w: %s", err, string(out))
96+
}
97+
v.vmi.Config.DstFile = dstDiskFile
98+
}
99+
78100
exe, args, err := v.executableAndArgs()
79101
if err != nil {
80102
return err
@@ -231,3 +253,32 @@ func TDXEnabledOnHost() bool {
231253

232254
return TDXEnabled(string(cpuinfo), string(kernelParam))
233255
}
256+
257+
func GetVirtualSizeBytes(path string) (int64, error) {
258+
cmd := exec.Command("qemu-img", "info", "--output=json", path)
259+
out, err := cmd.Output()
260+
if err != nil {
261+
return 0, fmt.Errorf("qemu-img info failed: %w", err)
262+
}
263+
264+
var info qemuInfo
265+
if err := json.Unmarshal(out, &info); err != nil {
266+
return 0, fmt.Errorf("failed to parse qemu-img JSON: %w", err)
267+
}
268+
269+
if info.VirtualSize <= 0 {
270+
return 0, fmt.Errorf("invalid virtual size: %d", info.VirtualSize)
271+
}
272+
273+
return info.VirtualSize, nil
274+
}
275+
276+
func GetVirtualSizeGB(path string) (int, error) {
277+
bytes, err := GetVirtualSizeBytes(path)
278+
if err != nil {
279+
return 0, err
280+
}
281+
282+
gb := (bytes + (1<<30 - 1)) >> 30
283+
return int(gb), nil
284+
}

scripts/igvmmeasure/igvm.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ mkdir -p "$BUILD_DIR"
88

99
# Define the target directory for cloning inside the build directory
1010
TARGET_DIR="$BUILD_DIR/svsm"
11-
SUBDIR="igvmmeasure"
11+
SUBDIR="tools/igvmmeasure"
1212

1313
# Clone the repository if it doesn't exist
1414
if [ -d "$TARGET_DIR" ]; then
@@ -33,8 +33,8 @@ fi
3333

3434
echo "Building the Rust crate..."
3535

36-
RELEASE=1 make bin/igvmmeasure BUILDDIR="$BUILD_DIR"
36+
RELEASE=1 make bin/igvmmeasure
3737

38-
mv bin/igvmmeasure "$BUILD_DIR/"
38+
mv "$BUILD_DIR"/svsm/bin/igvmmeasure "$BUILD_DIR/"
3939

4040
echo "Binary stored in: $BUILD_DIR/igvmmeasure"

0 commit comments

Comments
 (0)