Prevent partial VM startup failures from leaking loop devices and dm state on the host. Move root snapshot setup into a rollback-safe helper that records loop and mapper handles incrementally, tears them down in reverse order on failure, and reuses the same dm/loop cleanup path during normal runtime teardown. Also switch the daemon runner field to a small command-runner interface so the snapshot path can be tested with injected failures. Add failure-injection coverage for losetup, blockdev, dmsetup, partial teardown, and joined rollback errors. Validated with go test ./... and make build.
79 lines
2.1 KiB
Go
79 lines
2.1 KiB
Go
package daemon
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
type dmSnapshotHandles struct {
|
|
BaseLoop string
|
|
COWLoop string
|
|
DMName string
|
|
DMDev string
|
|
}
|
|
|
|
func (d *Daemon) createDMSnapshot(ctx context.Context, rootfsPath, cowPath, dmName string) (handles dmSnapshotHandles, err error) {
|
|
defer func() {
|
|
if err == nil {
|
|
return
|
|
}
|
|
if cleanupErr := d.cleanupDMSnapshot(context.Background(), handles); cleanupErr != nil {
|
|
err = errors.Join(err, cleanupErr)
|
|
}
|
|
}()
|
|
|
|
baseBytes, err := d.runner.RunSudo(ctx, "losetup", "-f", "--show", "--read-only", rootfsPath)
|
|
if err != nil {
|
|
return handles, err
|
|
}
|
|
handles.BaseLoop = strings.TrimSpace(string(baseBytes))
|
|
|
|
cowBytes, err := d.runner.RunSudo(ctx, "losetup", "-f", "--show", cowPath)
|
|
if err != nil {
|
|
return handles, err
|
|
}
|
|
handles.COWLoop = strings.TrimSpace(string(cowBytes))
|
|
|
|
sectorsBytes, err := d.runner.RunSudo(ctx, "blockdev", "--getsz", handles.BaseLoop)
|
|
if err != nil {
|
|
return handles, err
|
|
}
|
|
sectors := strings.TrimSpace(string(sectorsBytes))
|
|
|
|
if _, err := d.runner.RunSudo(ctx, "dmsetup", "create", dmName, "--table", fmt.Sprintf("0 %s snapshot %s %s P 8", sectors, handles.BaseLoop, handles.COWLoop)); err != nil {
|
|
return handles, err
|
|
}
|
|
handles.DMName = dmName
|
|
handles.DMDev = "/dev/mapper/" + dmName
|
|
return handles, nil
|
|
}
|
|
|
|
func (d *Daemon) cleanupDMSnapshot(ctx context.Context, handles dmSnapshotHandles) error {
|
|
var cleanupErr error
|
|
|
|
switch {
|
|
case handles.DMName != "":
|
|
if _, err := d.runner.RunSudo(ctx, "dmsetup", "remove", handles.DMName); err != nil {
|
|
cleanupErr = errors.Join(cleanupErr, err)
|
|
}
|
|
case handles.DMDev != "":
|
|
if _, err := d.runner.RunSudo(ctx, "dmsetup", "remove", handles.DMDev); err != nil {
|
|
cleanupErr = errors.Join(cleanupErr, err)
|
|
}
|
|
}
|
|
|
|
if handles.COWLoop != "" {
|
|
if _, err := d.runner.RunSudo(ctx, "losetup", "-d", handles.COWLoop); err != nil {
|
|
cleanupErr = errors.Join(cleanupErr, err)
|
|
}
|
|
}
|
|
if handles.BaseLoop != "" {
|
|
if _, err := d.runner.RunSudo(ctx, "losetup", "-d", handles.BaseLoop); err != nil {
|
|
cleanupErr = errors.Join(cleanupErr, err)
|
|
}
|
|
}
|
|
|
|
return cleanupErr
|
|
}
|