Skip to content

Commit

Permalink
Fix disk type for block devices on QEMU >= 6.0
Browse files Browse the repository at this point in the history
Since QEMU version 6.0 [1], you can't use the file block driver with
block devices. It tries to detect block devices by looking at the file
path.

[1] https://wiki.qemu.org/ChangeLog/6.0#Incompatible_changes
  • Loading branch information
ekohl committed Jul 24, 2023
1 parent 6db6f80 commit 1c7cf79
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/fog/libvirt/models/compute/server.rb
Expand Up @@ -331,9 +331,14 @@ def to_xml
xml.target(:dev => target_device, :bus => args["bus_type"] == "virtio" ? "virtio" : "scsi")
end
else
xml.disk(:type => "file", :device => "disk") do
is_block = volume.path.start_with?("/dev/")
xml.disk(:type => is_block ? "block" : "file", :device => "disk") do
xml.driver(:name => "qemu", :type => volume.format_type)
xml.source(:file => volume.path)
if is_block
xml.source(:dev => volume.path)
else
xml.source(:file => volume.path)
end
xml.target(:dev => target_device, :bus => "virtio")
end
end
Expand Down
16 changes: 16 additions & 0 deletions tests/libvirt/models/compute/server_tests.rb
Expand Up @@ -61,6 +61,22 @@
test('be a kind of Fog::Libvirt::Compute::Server') { server.kind_of? Fog::Libvirt::Compute::Server }
tests("serializes to xml") do
test("with memory") { server.to_xml.match?(%r{<memory>\d+</memory>}) }
test("with disk of type file") do
xml = server.to_xml
xml.match?(/<disk type="file" device="disk">/) && xml.match?(%r{<source file="path/to/disk"/>})
end
test("with disk of type block") do
server = Fog::Libvirt::Compute::Server.new(
{
:nics => [],
:volumes => [
Fog::Libvirt::Compute::Volume.new({ :path => "/dev/sda", :pool_name => "dummy" })
]
}
)
xml = server.to_xml
xml.match?(/<disk type="block" device="disk">/) && xml.match?(%r{<source dev="/dev/sda"/>})
end
end
end
end

1 comment on commit 1c7cf79

@lestercheung
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Will there be a new release/tag in time for Foreman 3.8?

Please sign in to comment.