-
Notifications
You must be signed in to change notification settings - Fork 45
Add bootdisk support #188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Add bootdisk support #188
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,61 @@ | ||||||||||||||||
| require "nokogiri" | ||||||||||||||||
|
|
||||||||||||||||
| module Fog | ||||||||||||||||
| module Libvirt | ||||||||||||||||
| class Compute | ||||||||||||||||
| module Shared | ||||||||||||||||
| include Fog::Libvirt::Util | ||||||||||||||||
|
|
||||||||||||||||
| def attach_iso(uuid, iso_path, options = {}) | ||||||||||||||||
| raise ArgumentError, "uuid is a required parameter" if uuid.nil? | ||||||||||||||||
| raise ArgumentError, "iso_path is a required parameter" if iso_path.nil? | ||||||||||||||||
|
|
||||||||||||||||
| options ||= {} | ||||||||||||||||
| raise ArgumentError, "options must be a hash" unless options.is_a?(Hash) | ||||||||||||||||
|
|
||||||||||||||||
| target_dev = options.fetch(:target_dev, DEFAULT_CDROM_TARGET_DEV) | ||||||||||||||||
| bus = options.fetch(:bus, DEFAULT_CDROM_BUS) | ||||||||||||||||
| flags = options.fetch(:flags, ::Libvirt::Domain::AFFECT_CONFIG) | ||||||||||||||||
|
|
||||||||||||||||
| resolved_iso_path = File.absolute_path?(iso_path) ? iso_path : File.join(default_iso_dir, iso_path) | ||||||||||||||||
| xml = attach_cdrom_xml(resolved_iso_path, target_dev, bus) | ||||||||||||||||
|
|
||||||||||||||||
| domain = client.lookup_domain_by_uuid(uuid) | ||||||||||||||||
| begin | ||||||||||||||||
| domain.attach_device(xml, flags) | ||||||||||||||||
| rescue ::Libvirt::Error => e | ||||||||||||||||
| begin | ||||||||||||||||
| domain.update_device(xml, flags) | ||||||||||||||||
| rescue ::Libvirt::Error | ||||||||||||||||
| raise e | ||||||||||||||||
| end | ||||||||||||||||
| end | ||||||||||||||||
|
|
||||||||||||||||
| # if we get no exception, we assume the operation was successful | ||||||||||||||||
| true | ||||||||||||||||
| end | ||||||||||||||||
|
|
||||||||||||||||
| private | ||||||||||||||||
|
|
||||||||||||||||
| def attach_cdrom_xml(iso_path, target_dev, bus) | ||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't actually attach anything and is only the CDROM XML so naming wise I'd suggest.
Suggested change
|
||||||||||||||||
| Nokogiri::XML::Builder.new do |x| | ||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a different implementation than what's default when a host is created: fog-libvirt/lib/fog/libvirt/models/compute/server.rb Lines 410 to 416 in f5495f3
If the host already has a cdrom drive, shouldn't it reuse that? |
||||||||||||||||
| x.disk(:type => "file", :device => "cdrom") do | ||||||||||||||||
| x.driver(:name => "qemu", :type => "raw") | ||||||||||||||||
| x.source(:file => iso_path) | ||||||||||||||||
| x.target(:dev => target_dev, :bus => bus) | ||||||||||||||||
| x.readonly | ||||||||||||||||
| end | ||||||||||||||||
| end.to_xml | ||||||||||||||||
| end | ||||||||||||||||
| end | ||||||||||||||||
|
|
||||||||||||||||
| class Real | ||||||||||||||||
| include Shared | ||||||||||||||||
| end | ||||||||||||||||
|
|
||||||||||||||||
| class Mock | ||||||||||||||||
| include Shared | ||||||||||||||||
| end | ||||||||||||||||
| end | ||||||||||||||||
| end | ||||||||||||||||
| end | ||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| module Fog | ||
| module Libvirt | ||
| class Compute | ||
| module Shared | ||
| def destroy_iso(pool_name, volume_name) | ||
| raise ArgumentError, "pool_name is a required parameter" if pool_name.nil? | ||
| raise ArgumentError, "volume_name is a required parameter" if volume_name.nil? | ||
|
|
||
| pool = client.lookup_storage_pool_by_name(pool_name) | ||
| begin | ||
| pool.lookup_volume_by_name(volume_name).delete | ||
| rescue ::Libvirt::RetrieveError | ||
| # already absent, treat as success if not present afterwards | ||
| end | ||
|
|
||
| # if the ISO is absent, then we are good | ||
| volume_absent?(pool, volume_name) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def volume_absent?(pool, volume_name) | ||
| pool.lookup_volume_by_name(volume_name) | ||
| false | ||
| rescue ::Libvirt::RetrieveError | ||
| true | ||
| end | ||
| end | ||
|
|
||
| class Real | ||
| include Shared | ||
| end | ||
|
|
||
| class Mock | ||
| include Shared | ||
| end | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| require "nokogiri" | ||
|
|
||
| module Fog | ||
| module Libvirt | ||
| class Compute | ||
| module Shared | ||
| include Fog::Libvirt::Util | ||
|
|
||
| def detach_iso(uuid, options = {}) | ||
| raise ArgumentError, "uuid is a required parameter" if uuid.nil? | ||
|
|
||
| options ||= {} | ||
| raise ArgumentError, "options must be a hash" unless options.is_a?(Hash) | ||
|
|
||
| target_dev = options.fetch(:target_dev, DEFAULT_CDROM_TARGET_DEV) | ||
| bus = options.fetch(:bus, DEFAULT_CDROM_BUS) | ||
| flags = options.fetch(:flags, ::Libvirt::Domain::AFFECT_CONFIG) | ||
| domain = client.lookup_domain_by_uuid(uuid) | ||
| domain_active = domain.active? | ||
| flags = effective_detach_iso_flags(flags, domain_active) | ||
|
|
||
| if domain_active | ||
| domain.update_device(eject_cdrom_xml(target_dev, bus), flags) | ||
| begin | ||
| domain.detach_device(detach_cdrom_xml(target_dev, bus), flags) | ||
| rescue ::Libvirt::Error | ||
| # Some backends don't allow to detach the cdrom if the host is running. | ||
| # In this case, we just eject the cdrom and leave it attached to the VM. | ||
| # Return true that maybe the ISO file can be removed in further steps. | ||
| true | ||
| end | ||
| else | ||
| begin | ||
| domain.detach_device(detach_cdrom_xml(target_dev, bus), flags) | ||
| true | ||
| rescue ::Libvirt::Error | ||
| false | ||
| end | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def detach_cdrom_xml(target_dev, bus) | ||
| Nokogiri::XML::Builder.new do |x| | ||
| x.disk(:type => "file", :device => "cdrom") do | ||
| x.target(:dev => target_dev, :bus => bus) | ||
| end | ||
| end.to_xml | ||
| end | ||
|
|
||
| def eject_cdrom_xml(target_dev, bus) | ||
| Nokogiri::XML::Builder.new do |x| | ||
| x.disk(:type => "file", :device => "cdrom", :tray => "open") do | ||
| x.driver(:name => "qemu", :type => "raw") | ||
| x.target(:dev => target_dev, :bus => bus) | ||
| x.readonly | ||
| end | ||
| end.to_xml | ||
| end | ||
|
|
||
| def effective_detach_iso_flags(flags, domain_active) | ||
| return flags unless (flags & ::Libvirt::Domain::AFFECT_CONFIG) == ::Libvirt::Domain::AFFECT_CONFIG | ||
| return flags unless domain_active | ||
|
|
||
| flags | ::Libvirt::Domain::AFFECT_LIVE | ||
| rescue ::Libvirt::Error | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When is this raised? I don't see API calls here so what could raise an exception? |
||
| flags | ||
| end | ||
| end | ||
|
|
||
| class Real | ||
| include Shared | ||
| end | ||
|
|
||
| class Mock | ||
| include Shared | ||
| end | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| require "nokogiri" | ||
|
|
||
| module Fog | ||
| module Libvirt | ||
| class Compute | ||
| module Shared | ||
| def upload_iso(pool_name, volume_name, file_path) | ||
| raise ArgumentError, "pool_name is a required parameter" if pool_name.nil? | ||
| raise ArgumentError, "volume_name is a required parameter" if volume_name.nil? | ||
| raise ArgumentError, "file_path is a required parameter" if file_path.nil? | ||
|
|
||
| pool = client.lookup_storage_pool_by_name(pool_name) | ||
| pool.lookup_volume_by_name(volume_name).delete if pool.list_volumes.include?(volume_name) | ||
|
|
||
| create_volume(pool_name, iso_volume_xml(volume_name, file_path)) | ||
| upload_volume(pool_name, volume_name, file_path) | ||
|
|
||
| volume = pool.lookup_volume_by_name(volume_name) | ||
| { | ||
| :pool_name => pool_name, | ||
| :name => volume_name, | ||
| :key => volume.key, | ||
| :path => volume.path | ||
| } | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def iso_volume_xml(volume_name, file_path) | ||
| iso_size = File.size(file_path) | ||
|
|
||
| Nokogiri::XML::Builder.new do |x| | ||
| x.volume do | ||
| x.name(volume_name) | ||
| x.allocation(0, :unit => "B") | ||
| x.capacity(iso_size, :unit => "B") | ||
| x.target do | ||
| x.format(:type => "raw") | ||
| end | ||
| end | ||
| end.to_xml | ||
| end | ||
| end | ||
|
|
||
| class Real | ||
| include Shared | ||
| end | ||
|
|
||
| class Mock | ||
| include Shared | ||
| end | ||
| end | ||
| end | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a host is created with a cdrom it uses
sdaas the default:fog-libvirt/lib/fog/libvirt/models/compute/server.rb
Lines 410 to 416 in f5495f3