Support for adding additional disks through the Vagrantfile.

This patch lets you define multiple additional disks in your
Vagrantfile.
This commit is contained in:
James Shubin
2014-04-10 15:46:39 -04:00
parent f13807614a
commit be2042537e
4 changed files with 113 additions and 1 deletions

View File

@@ -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.

View File

@@ -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)

View File

@@ -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>