Skip to content

Commit c2f4950

Browse files
authored
Merge pull request #1444 from stgraber/main
incusd/storage/lvm: Require 512-bytes physical block size for VM images
2 parents db1a995 + 0118bf8 commit c2f4950

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

internal/server/storage/drivers/driver_lvm_utils.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import (
99
"path/filepath"
1010
"strconv"
1111
"strings"
12+
"unsafe"
13+
14+
"golang.org/x/sys/unix"
1215

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

880883
return false, nil
881884
}
885+
886+
func (d *lvm) getBlockSize(path string) (int, error) {
887+
// Open the block device.
888+
f, err := os.Open(path)
889+
if err != nil {
890+
return -1, err
891+
}
892+
893+
defer func() { _ = f.Close() }()
894+
895+
// Query the physical block size.
896+
var res int32
897+
_, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(f.Fd()), unix.BLKPBSZGET, uintptr(unsafe.Pointer(&res)))
898+
if errno != 0 {
899+
return -1, fmt.Errorf("Failed to BLKPBSZGET: %w", unix.Errno(errno))
900+
}
901+
902+
return int(res), nil
903+
}

internal/server/storage/drivers/driver_lvm_volumes.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,18 @@ func (d *lvm) CreateVolume(vol Volume, filler *VolumeFiller, op *operations.Oper
7070
if err != nil {
7171
return err
7272
}
73+
74+
// Check the block size for image volumes.
75+
if vol.volType == VolumeTypeImage {
76+
blockSize, err := d.getBlockSize(devPath)
77+
if err != nil {
78+
return err
79+
}
80+
81+
if blockSize != 512 {
82+
return fmt.Errorf("Underlying storage uses %d bytes sector size when virtual machine images require 512 bytes", blockSize)
83+
}
84+
}
7385
}
7486

7587
allowUnsafeResize := false

0 commit comments

Comments
 (0)