[DRE-commits] [vagrant-libvirt] 54/77: Support for adding additional disks through the Vagrantfile.

Antonio Terceiro terceiro at moszumanska.debian.org
Sun Apr 24 13:56:38 UTC 2016


This is an automated email from the git hooks/post-receive script.

terceiro pushed a commit to annotated tag 0.0.17
in repository vagrant-libvirt.

commit be2042537e4f8f3e59a7880bf3e09358252be252
Author: James Shubin <james at shubin.ca>
Date:   Thu Apr 10 15:46:39 2014 -0400

    Support for adding additional disks through the Vagrantfile.
    
    This patch lets you define multiple additional disks in your
    Vagrantfile.
---
 README.md                                    |  7 ++++
 lib/vagrant-libvirt/action/create_domain.rb  | 40 +++++++++++++++++++-
 lib/vagrant-libvirt/config.rb                | 56 ++++++++++++++++++++++++++++
 lib/vagrant-libvirt/templates/domain.xml.erb | 11 ++++++
 4 files changed, 113 insertions(+), 1 deletion(-)

diff --git a/README.md b/README.md
index b085694..89c363a 100644
--- a/README.md
+++ b/README.md
@@ -85,6 +85,13 @@ Vagrant.configure("2") do |config|
     libvirt.connect_via_ssh = true
     libvirt.username = "root"
     libvirt.storage_pool_name = "default"
+
+    # include as many of these addition disks as you want to
+    libvirt.storage :file,
+      #:path => '',		# automatically chosen if unspecified!
+      #:device => 'vdb',	# automatically chosen if unspecified!
+      #:size => '10G',		# defaults to 10G if unspecified!
+      :type => 'qcow2'		# defaults to 'qcow2' if unspecified!
   end
 end
 ```
diff --git a/lib/vagrant-libvirt/action/create_domain.rb b/lib/vagrant-libvirt/action/create_domain.rb
index 3588dec..4096c12 100644
--- a/lib/vagrant-libvirt/action/create_domain.rb
+++ b/lib/vagrant-libvirt/action/create_domain.rb
@@ -28,6 +28,10 @@ module VagrantPlugins
           @cmd_line = config.cmd_line
           @initrd = config.initrd
 
+          # Storage
+          @storage_pool_name = config.storage_pool_name
+          @disks = config.disks
+
           config = env[:machine].provider_config
           @domain_type = config.driver
 
@@ -39,6 +43,34 @@ module VagrantPlugins
           raise Errors::DomainVolumeExists if domain_volume == nil
           @domain_volume_path = domain_volume.path
 
+          #storage_prefix = '/var/lib/libvirt/images/'
+          storage_prefix = File.dirname(@domain_volume_path)+'/'	# steal
+
+          @disks.each do |disk|
+            dname = "#{@name}-#{disk[:device]}.#{disk[:type]}"	# disk name
+            disk[:name] = dname
+            if disk[:path].nil?
+              disk[:path] = "#{storage_prefix}#{dname}"	# automatically chosen!
+            end
+            #puts "Disk: #{disk[:device]}, #{disk[:type]}, #{disk[:path]}"
+
+            # make the disk. equivalent to:
+            # qemu-img create -f qcow2 <path> 5g
+            begin
+              #puts "Making disk: #{d}, #{t}, #{p}"
+              domain_volume_disk = env[:libvirt_compute].volumes.create(
+                :name => disk[:name],
+                :format_type => disk[:type],
+                :path => disk[:path],
+                :capacity => disk[:size],
+                #:allocation => ?,
+                :pool_name => @storage_pool_name)
+            rescue Fog::Errors::Error => e
+              raise Errors::FogDomainVolumeCreateError,
+                :error_message => e.message
+            end
+          end
+
           # Output the settings we're going to use to the user
           env[:ui].info(I18n.t("vagrant_libvirt.creating_domain"))
           env[:ui].info(" -- Name:          #{@name}")
@@ -46,11 +78,17 @@ module VagrantPlugins
           env[:ui].info(" -- Cpus:          #{@cpus}")
           env[:ui].info(" -- Memory:        #{@memory_size/1024}M")
           env[:ui].info(" -- Base box:      #{env[:machine].box.name}")
-          env[:ui].info(" -- Storage pool:  #{env[:machine].provider_config.storage_pool_name}")
+          env[:ui].info(" -- Storage pool:  #{@storage_pool_name}")
           env[:ui].info(" -- Image:         #{@domain_volume_path}")
           env[:ui].info(" -- Volume Cache:  #{@domain_volume_cache}")
           env[:ui].info(" -- Kernel:        #{@kernel}")
           env[:ui].info(" -- Initrd:        #{@initrd}")
+          if @disks.length > 0
+            env[:ui].info(" -- Disks:         #{@disks.collect{ |x| x[:device]+'('+x[:type]+','+x[:size]+')' }.join(', ')}")
+          end
+          @disks.each do |disk|
+            env[:ui].info(" -- Disk(#{disk[:device]}):     #{disk[:path]}")
+          end
           env[:ui].info(" -- Command line : #{@cmd_line}")
 
           # Create libvirt domain.
diff --git a/lib/vagrant-libvirt/config.rb b/lib/vagrant-libvirt/config.rb
index 5ba127e..eba8899 100644
--- a/lib/vagrant-libvirt/config.rb
+++ b/lib/vagrant-libvirt/config.rb
@@ -1,5 +1,14 @@
 require 'vagrant'
 
+class Numeric
+  Alphabet = ('a'..'z').to_a
+  def vdev
+    s, q = '', self
+    (q, r = (q - 1).divmod(26)) && s.prepend(Alphabet[r]) until q.zero?
+    'vd'+s
+  end
+end
+
 module VagrantPlugins
   module ProviderLibvirt
     class Config < Vagrant.plugin('2', :config)
@@ -46,6 +55,9 @@ module VagrantPlugins
       attr_accessor :cmd_line
       attr_accessor :initrd
 
+      # Storage
+      attr_accessor :disks
+
       def initialize
         @driver            = UNSET_VALUE
         @host              = UNSET_VALUE
@@ -67,6 +79,47 @@ module VagrantPlugins
         @kernel            = UNSET_VALUE
         @initrd            = UNSET_VALUE
         @cmd_line          = UNSET_VALUE
+
+        # Storage
+        @disks             = UNSET_VALUE
+      end
+
+      def __get_device(disks)
+        disks = [] if disks == UNSET_VALUE
+        # skip existing devices and also the first one (vda)
+        exist = disks.collect {|x| x[:device]}+[1.vdev.to_s]
+        skip = 1		# we're 1 based, not 0 based...
+        while true do
+          dev = skip.vdev	# get lettered device
+          if not exist.include?(dev)
+            return dev
+          end
+          skip+=1
+        end
+      end
+
+      # NOTE: this will run twice for each time it's needed- keep it idempotent
+      def storage(storage_type, options={})
+        options = {
+          :device => __get_device(@disks),
+          :type => 'qcow2',
+          :size => '10G',	# matches the fog default
+          :path => nil,
+        }.merge(options)
+
+        #puts "storage(#{storage_type} --- #{options.to_s})"
+        @disks = [] if @disks == UNSET_VALUE
+
+        disk = {
+          :device => options[:device],
+          :type => options[:type],
+          :size => options[:size],
+          :path => options[:path],
+        }
+
+        if storage_type == :file
+          @disks << disk	# append
+        end
       end
 
       def finalize!
@@ -90,6 +143,9 @@ module VagrantPlugins
         @kernel = nil if @kernel == UNSET_VALUE
         @cmd_line = '' if @cmd_line == UNSET_VALUE
         @initrd = '' if @initrd == UNSET_VALUE
+
+        # Storage
+        @disks = [] if @disks == UNSET_VALUE
       end
 
       def validate(machine)
diff --git a/lib/vagrant-libvirt/templates/domain.xml.erb b/lib/vagrant-libvirt/templates/domain.xml.erb
index 3837303..ea35da6 100644
--- a/lib/vagrant-libvirt/templates/domain.xml.erb
+++ b/lib/vagrant-libvirt/templates/domain.xml.erb
@@ -31,6 +31,17 @@
       <%# we need to ensure a unique target dev -%>
       <target dev='vda' bus='<%= @disk_bus %>'/>
     </disk>
+<%# additional disks -%>
+<% @disks.each do |d| -%>
+    <disk type='file' device='disk'>
+      <driver name='qemu' type='<%= d[:type] %>'/>
+      <source file='<%= d[:path] %>'/>
+      <target dev='<%= d[:device] %>' bus='virtio'/>
+<%# this will get auto generated by libvirt
+      <address type='pci' domain='0x0000' bus='0x00' slot='???' function='0x0'/>
+-%>
+    </disk>
+<% end -%>
     <serial type='pty'>
       <target port='0'/>
     </serial>

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-ruby-extras/vagrant-libvirt.git



More information about the Pkg-ruby-extras-commits mailing list