Skip to content

Commit

Permalink
make disk driver cache attribute configurable
Browse files Browse the repository at this point in the history
In some typical virter usage scenarios, the consistency of guest disks
after a host crash is unimportant. Hence setting the cache to "unsafe"
is acceptable. This improves disk performance.
  • Loading branch information
JoelColledge committed Dec 1, 2021
1 parent 50840c1 commit 4db9439
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 7 deletions.
6 changes: 6 additions & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ network = "{{ get "libvirt.network" }}"
# Default value: "{{ get "libvirt.static_dhcp" }}"
static_dhcp = "{{ get "libvirt.static_dhcp" }}"
# disk_cache is passed to libvirt as the disk driver cache attribute. See
# https://libvirt.org/formatdomain.html#hard-drives-floppy-disks-cdroms.
# Default value: "{{ get "libvirt.disk_cache" }}"
disk_cache = "{{ get "libvirt.disk_cache" }}"
[time]
# ssh_ping_count is the number of times virter will try to connect to a VM's
# ssh port after starting it.
Expand Down Expand Up @@ -94,6 +99,7 @@ func initConfig() {
viper.SetDefault("libvirt.pool", "default")
viper.SetDefault("libvirt.network", "default")
viper.SetDefault("libvirt.static_dhcp", false)
viper.SetDefault("libvirt.disk_cache", "")
viper.SetDefault("time.ssh_ping_count", 60)
viper.SetDefault("time.ssh_ping_period", time.Second)
viper.SetDefault("time.shutdown_timeout", 20*time.Second)
Expand Down
1 change: 1 addition & 0 deletions cmd/image_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ func imageBuildCommand() *cobra.Command {
StaticDHCP: viper.GetBool("libvirt.static_dhcp"),
ExtraSSHPublicKeys: extraAuthorizedKeys,
ConsolePath: consolePath,
DiskCache: viper.GetString("libvirt.disk_cache"),
Mounts: mounts,
}

Expand Down
1 change: 1 addition & 0 deletions cmd/vm_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ func vmRunCommand() *cobra.Command {
ExtraSSHPublicKeys: extraAuthorizedKeys,
ConsolePath: consolePath,
Disks: disks,
DiskCache: viper.GetString("libvirt.disk_cache"),
Mounts: mounts,
ExtraNics: nics,
GDBPort: thisGDBPort,
Expand Down
10 changes: 6 additions & 4 deletions internal/virter/libvirtxml.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,22 @@ type VMDisk struct {
format string
}

func vmDisksToLibvirtDisks(vmDisks []VMDisk) ([]lx.DomainDisk, error) {
func vmDisksToLibvirtDisks(vmDisks []VMDisk, diskCache string) ([]lx.DomainDisk, error) {
devCounts := map[string]*driveletter.DriveLetter{}

var result []lx.DomainDisk
for _, d := range vmDisks {
driver := map[VMDiskDevice]lx.DomainDiskDriver{
VMDiskDeviceDisk: lx.DomainDiskDriver{
Name: "qemu",
Cache: diskCache,
Discard: "unmap",
Type: d.format,
},
VMDiskDeviceCDROM: lx.DomainDiskDriver{
Name: "qemu",
Type: d.format,
Name: "qemu",
Cache: diskCache,
Type: d.format,
},
}[d.device]

Expand Down Expand Up @@ -104,7 +106,7 @@ func (v *Virter) vmXML(poolName string, vm VMConfig, mac string, meta *VMMeta) (
}

log.Debugf("input are these vmdisks: %+v", vmDisks)
disks, err := vmDisksToLibvirtDisks(vmDisks)
disks, err := vmDisksToLibvirtDisks(vmDisks, vm.DiskCache)
if err != nil {
return "", fmt.Errorf("failed to build libvirt disks: %w", err)
}
Expand Down
10 changes: 7 additions & 3 deletions internal/virter/libvirtxml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ func TestVmDisksToLibvirtDisks(t *testing.T) {
cases := []struct {
descr string
input []VMDisk
cache string
expect []lx.DomainDisk
expectError bool
}{
Expand All @@ -21,11 +22,13 @@ func TestVmDisksToLibvirtDisks(t *testing.T) {
VMDisk{device: VMDiskDeviceDisk, poolName: "pool", volumeName: "vol1", bus: "virtio", format: "qcow2"},
VMDisk{device: VMDiskDeviceCDROM, poolName: "pool", volumeName: "vol2", bus: "ide", format: "raw"},
},
cache: "unsafe",
expect: []lx.DomainDisk{
lx.DomainDisk{
Device: "disk",
Driver: &lx.DomainDiskDriver{
Name: "qemu",
Cache: "unsafe",
Discard: "unmap",
Type: "qcow2",
},
Expand All @@ -43,8 +46,9 @@ func TestVmDisksToLibvirtDisks(t *testing.T) {
lx.DomainDisk{
Device: "cdrom",
Driver: &lx.DomainDiskDriver{
Name: "qemu",
Type: "raw",
Name: "qemu",
Cache: "unsafe",
Type: "raw",
},
Source: &lx.DomainDiskSource{
Volume: &lx.DomainDiskSourceVolume{
Expand All @@ -68,7 +72,7 @@ func TestVmDisksToLibvirtDisks(t *testing.T) {
}

for _, c := range cases {
actual, err := vmDisksToLibvirtDisks(c.input)
actual, err := vmDisksToLibvirtDisks(c.input, c.cache)
if !c.expectError && err != nil {
t.Errorf("on input '%s':", c.input)
t.Fatalf("unexpected error: %+v", err)
Expand Down
1 change: 1 addition & 0 deletions internal/virter/virter.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ type VMConfig struct {
ExtraSSHPublicKeys []string
ConsolePath string
Disks []Disk
DiskCache string
ExtraNics []NIC
Mounts []Mount
GDBPort uint
Expand Down

0 comments on commit 4db9439

Please sign in to comment.