Skip to content

Truncate the block file during custom volume migration #1696

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions internal/server/storage/drivers/generic_vfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,14 @@ func genericVFSCreateVolumeFromMigration(d Driver, initVolume func(vol Volume) (
if err != nil {
return err
}

// During migration (e.g., LVM → dir), the block file may be smaller because
// recvBlockVol uses SparseFileWrapper, which omits trailing zero bytes and does not truncate.
// enlargeVolumeBlockFile ensures the block file matches the source volume size by applying truncation.
err = enlargeVolumeBlockFile(snapVol, pathBlock)
if err != nil {
return err
}
}

// Create the snapshot itself.
Expand Down Expand Up @@ -445,6 +453,14 @@ func genericVFSCreateVolumeFromMigration(d Driver, initVolume func(vol Volume) (
if err != nil {
return err
}

// During migration (e.g., LVM → dir), the block file may be smaller because
// recvBlockVol uses SparseFileWrapper, which omits trailing zero bytes and does not truncate.
// enlargeVolumeBlockFile ensures the block file matches the source volume size by applying truncation.
err = enlargeVolumeBlockFile(vol, pathBlock)
if err != nil {
return err
}
}

return nil
Expand Down
29 changes: 29 additions & 0 deletions internal/server/storage/drivers/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/lxc/incus/v6/shared/idmap"
"github.com/lxc/incus/v6/shared/logger"
"github.com/lxc/incus/v6/shared/subprocess"
"github.com/lxc/incus/v6/shared/units"
"github.com/lxc/incus/v6/shared/util"
)

Expand Down Expand Up @@ -389,6 +390,34 @@ func ensureVolumeBlockFile(vol Volume, path string, sizeBytes int64, allowUnsafe
return false, nil
}

// enlargeVolumeBlockFile enlarges the raw block file for a volume to the specified size.
func enlargeVolumeBlockFile(vol Volume, path string) error {
if linux.IsBlockdevPath(path) {
return nil
}

volSize, err := units.ParseByteSizeString(vol.ConfigSize())
if err != nil {
return err
}

actualSize, err := BlockDiskSizeBytes(path)
if err != nil {
return err
}

if volSize < actualSize {
return fmt.Errorf("Block volumes cannot be shrunk: %w", ErrCannotBeShrunk)
}

err = ensureSparseFile(path, volSize)
if err != nil {
return err
}

return nil
}

// mkfsOptions represents options for filesystem creation.
type mkfsOptions struct {
Label string
Expand Down