Enable the use of original device block size

This commit is contained in:
AlexSSD7 2023-09-27 10:48:23 +01:00
commit a5038eb957
3 changed files with 38 additions and 4 deletions

View file

@ -264,8 +264,14 @@ func getDevicePassthroughConfig(val string) (*vm.PassthroughConfig, error) {
return nil, errors.Wrapf(err, "check whether device path is valid '%v'", devPath)
}
blockSize, err := osspecifics.GetDeviceLogicalBlockSize(devPath)
if err != nil {
return nil, errors.Wrapf(err, "get logical block size for device '%v'", devPath)
}
return &vm.PassthroughConfig{Block: []vm.BlockDevicePassthroughConfig{{
Path: devPath,
Path: devPath,
BlockSize: blockSize,
}}}, nil
default:
return nil, fmt.Errorf("unknown device passthrough type '%v'", val)

View file

@ -24,6 +24,7 @@ import (
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"
@ -325,18 +326,44 @@ func configureVMCmdBlockDevicePassthrough(logger *slog.Logger, cfg Config) ([]qe
}
}
if dev.BlockSize == 0 {
return nil, fmt.Errorf("invalid zero block size specified for device '%v'", dev.Path)
}
if dev.BlockSize > 65536 {
return nil, fmt.Errorf("block size specified for device '%v' is too large (max is 65536): '%v'", dev.Path, dev.BlockSize)
}
if dev.BlockSize/512*512 != dev.BlockSize {
return nil, fmt.Errorf("unaligned block size specified for device '%v' (must be in increments of 512): '%v'", dev.Path, dev.BlockSize)
}
strBlockSize := strconv.FormatUint(dev.BlockSize, 10)
devPath := cleanQEMUPath(dev.Path)
driveID := getUniqueQEMUDriveID()
driveDevArg, err := qemucli.NewKeyValueArg("device", []qemucli.KeyValueArgItem{
{Key: "driver", Value: "virtio-blk-pci"},
{Key: "drive", Value: driveID},
{Key: "logical_block_size", Value: strBlockSize},
{Key: "physical_block_size", Value: strBlockSize},
})
if err != nil {
return nil, errors.Wrapf(err, "create drive device key-value arg (path '%v')", devPath)
}
driveArg, err := qemucli.NewKeyValueArg("drive", []qemucli.KeyValueArgItem{
{Key: "file", Value: devPath},
{Key: "format", Value: "raw"},
{Key: "if", Value: "virtio"},
{Key: "if", Value: "none"},
{Key: "id", Value: driveID},
})
if err != nil {
return nil, errors.Wrapf(err, "create drive key-value arg (path '%v')", devPath)
}
args = append(args, driveArg)
args = append(args, driveDevArg, driveArg)
}
return args, nil

View file

@ -22,7 +22,8 @@ type USBDevicePassthroughConfig struct {
}
type BlockDevicePassthroughConfig struct {
Path string
Path string
BlockSize uint64
}
type PassthroughConfig struct {