Skip to content

Commit

Permalink
go/runtime/host/tdx: Resize overlay image if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
kostko committed Feb 21, 2025
1 parent a5ceacf commit 90ff4c1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions .changelog/6077.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
go/runtime/host/tdx: Resize overlay image if needed
36 changes: 35 additions & 1 deletion go/runtime/host/tdx/qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tdx

import (
"context"
"encoding/json"
"errors"
"fmt"
"net"
Expand Down Expand Up @@ -283,6 +284,25 @@ func (p *qemuProvisioner) createPersistentOverlayImage(
// Image already exists, perform a rebase operation to account for the backing file location
// changing (e.g. due to an upgrade).
cmd := exec.Command(
defaultQemuImgPath,
"info",
"--output", "json",
image,
)
var out strings.Builder
cmd.Stderr = &out
cmd.Stdout = &out
if err := cmd.Run(); err != nil {
return "", fmt.Errorf("failed to query base image: %s\n%w", out.String(), err)

Check warning on line 296 in go/runtime/host/tdx/qemu.go

View check run for this annotation

Codecov / codecov/patch

go/runtime/host/tdx/qemu.go#L287-L296

Added lines #L287 - L296 were not covered by tests
}
var info struct {
VirtualSize int `json:"virtual-size"`

Check warning on line 299 in go/runtime/host/tdx/qemu.go

View check run for this annotation

Codecov / codecov/patch

go/runtime/host/tdx/qemu.go#L298-L299

Added lines #L298 - L299 were not covered by tests
}
if err := json.Unmarshal([]byte(out.String()), &info); err != nil {
return "", fmt.Errorf("malformed base image metadata: %w", err)

Check warning on line 302 in go/runtime/host/tdx/qemu.go

View check run for this annotation

Codecov / codecov/patch

go/runtime/host/tdx/qemu.go#L301-L302

Added lines #L301 - L302 were not covered by tests
}

cmd = exec.Command(

Check warning on line 305 in go/runtime/host/tdx/qemu.go

View check run for this annotation

Codecov / codecov/patch

go/runtime/host/tdx/qemu.go#L305

Added line #L305 was not covered by tests
defaultQemuImgPath,
"rebase",
"-u",
Expand All @@ -291,12 +311,26 @@ func (p *qemuProvisioner) createPersistentOverlayImage(
"-F", format,
imageFn,
)
var out strings.Builder
out.Reset()

Check warning on line 314 in go/runtime/host/tdx/qemu.go

View check run for this annotation

Codecov / codecov/patch

go/runtime/host/tdx/qemu.go#L314

Added line #L314 was not covered by tests
cmd.Stderr = &out
cmd.Stdout = &out
if err := cmd.Run(); err != nil {
return "", fmt.Errorf("failed to rebase persistent overlay image: %s\n%w", out.String(), err)
}

// Perform a resize if needed.
cmd = exec.Command(
defaultQemuImgPath,
"resize",
imageFn,
fmt.Sprintf("%d", info.VirtualSize),
)
out.Reset()
cmd.Stderr = &out
cmd.Stdout = &out
if err := cmd.Run(); err != nil {
return "", fmt.Errorf("failed to resize persistent overlay image: %s\n%w", out.String(), err)

Check warning on line 332 in go/runtime/host/tdx/qemu.go

View check run for this annotation

Codecov / codecov/patch

go/runtime/host/tdx/qemu.go#L322-L332

Added lines #L322 - L332 were not covered by tests
}
case errors.Is(err, os.ErrNotExist):
// Create image directory if it doesn't yet exist.
if err := common.Mkdir(imageDir); err != nil {
Expand Down

0 comments on commit 90ff4c1

Please sign in to comment.