Skip to content

Commit

Permalink
incusd/storage/lvm: Require 512-bytes physical block size for VM images
Browse files Browse the repository at this point in the history
Closes #1375

Signed-off-by: Stéphane Graber <[email protected]>
  • Loading branch information
stgraber committed Dec 4, 2024
1 parent e48c70c commit c896986
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
22 changes: 22 additions & 0 deletions internal/server/storage/drivers/driver_lvm_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
"path/filepath"
"strconv"
"strings"
"unsafe"

"golang.org/x/sys/unix"

internalInstance "github.com/lxc/incus/v6/internal/instance"
"github.com/lxc/incus/v6/internal/linux"
Expand Down Expand Up @@ -879,3 +882,22 @@ func (d *lvm) deactivateVolume(vol Volume) (bool, error) {

return false, nil
}

func (d *lvm) getBlockSize(path string) (int, error) {
// Open the block device.
f, err := os.Open(path)
if err != nil {
return -1, err
}

defer func() { _ = f.Close() }()

// Query the physical block size.
var res int32
_, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(f.Fd()), unix.BLKPBSZGET, uintptr(unsafe.Pointer(&res)))
if errno != 0 {
return -1, fmt.Errorf("Failed to BLKPBSZGET: %w", unix.Errno(errno))
}

return int(res), nil
}
12 changes: 12 additions & 0 deletions internal/server/storage/drivers/driver_lvm_volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ func (d *lvm) CreateVolume(vol Volume, filler *VolumeFiller, op *operations.Oper
if err != nil {
return err
}

// Check the block size for image volumes.
if vol.volType == VolumeTypeImage {
blockSize, err := d.getBlockSize(devPath)
if err != nil {
return err
}

if blockSize != 512 {
return fmt.Errorf("Underlying storage uses %d bytes sector size when virtual machine images require 512 bytes", blockSize)
}
}
}

allowUnsafeResize := false
Expand Down

0 comments on commit c896986

Please sign in to comment.