diff --git a/lib/vagrant-libvirt/action/handle_box_image.rb b/lib/vagrant-libvirt/action/handle_box_image.rb index 25b4cab..3c706c1 100644 --- a/lib/vagrant-libvirt/action/handle_box_image.rb +++ b/lib/vagrant-libvirt/action/handle_box_image.rb @@ -34,10 +34,11 @@ module VagrantPlugins box_format = env[:machine].box.metadata['format'] HandleBoxImage.verify_box_format(box_format) + image_path = HandleBoxImage.get_box_image_path(env[:machine].box, 'box.img') env[:box_volume_number] = 1 env[:box_volumes] = [{ - :path => HandleBoxImage.get_box_image_path(env[:machine].box, 'box.img'), - :name => HandleBoxImage.get_volume_name(env[:machine].box, 'box'), + :path => image_path, + :name => HandleBoxImage.get_volume_name(env[:machine].box, 'box', image_path, env[:ui]), :virtual_size => HandleBoxImage.get_virtual_size(env), :format => box_format, }] @@ -58,6 +59,8 @@ module VagrantPlugins volume_name = HandleBoxImage.get_volume_name( env[:machine].box, disks[i].fetch('name', disks[i]['path'].sub(/#{File.extname(disks[i]['path'])}$/, '')), + image_path, + env[:ui], ) # allowing name means needing to check that it doesn't cause a clash @@ -122,15 +125,21 @@ module VagrantPlugins protected - def self.get_volume_name(box, name) + def self.get_volume_name(box, name, path, ui) + version = begin + box.version.to_s + rescue + '' + end + + if version.empty? + ui.warn(I18n.t('vagrant_libvirt.box_version_missing', name: box.name.to_s)) + + version = "0_#{File.mtime(path).to_i}" + end + vol_name = box.name.to_s.dup.gsub('/', '-VAGRANTSLASH-') - vol_name << "_vagrant_box_image_#{ - begin - box.version.to_s - rescue - '' - end - }_#{name.dup.gsub('/', '-SLASH-')}.img" + vol_name << "_vagrant_box_image_#{version}_#{name.dup.gsub('/', '-SLASH-')}.img" end def self.get_virtual_size(env) diff --git a/locales/en.yml b/locales/en.yml index 0b1786e..d9f345c 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -15,6 +15,10 @@ en: manual_resize_required: |- Created volume larger than box defaults, will require manual resizing of filesystems to utilize. + box_version_missing: |- + No verison detected for %{name}, using timestamp to watch for modifications. Consider + generating a local metadata for the box with a version to allow better handling. + See https://www.vagrantup.com/docs/boxes/format#box-metadata for further details. uploading_volume: |- Uploading base box image as volume into Libvirt storage... creating_domain_volume: |- diff --git a/spec/unit/action/handle_box_image_spec.rb b/spec/unit/action/handle_box_image_spec.rb index 0a320bd..8c0b9a2 100644 --- a/spec/unit/action/handle_box_image_spec.rb +++ b/spec/unit/action/handle_box_image_spec.rb @@ -98,6 +98,36 @@ describe VagrantPlugins::ProviderLibvirt::Action::HandleBoxImage do ) end + context 'when no box version set' do + let(:box_mtime) { Time.now } + + before do + expect(env[:machine]).to receive_message_chain("box.version") { nil } + expect(File).to receive(:mtime).and_return(box_mtime) + end + + it 'should use the box file timestamp' do + expect(ui).to receive(:warn).with( + "No verison detected for test, using timestamp to watch for modifications. Consider\n" + + "generating a local metadata for the box with a version to allow better handling.\n" + + 'See https://www.vagrantup.com/docs/boxes/format#box-metadata for further details.' + ) + + expect(subject.call(env)).to be_nil + expect(env[:box_volume_number]).to eq(1) + expect(env[:box_volumes]).to eq( + [ + { + :path=>"/test/box.img", + :name=>"test_vagrant_box_image_0_#{box_mtime.to_i}_box.img", + :virtual_size=>byte_number_5G, + :format=>"qcow2" + } + ] + ) + end + end + context 'When config.machine_virtual_size is set and smaller than box_virtual_size' do before do allow(env[:machine]).to receive_message_chain("provider_config.machine_virtual_size").and_return(1)