Skip to content

Commit

Permalink
Merge pull request #112 from hashicorp/disks-fixup
Browse files Browse the repository at this point in the history
Fix primary disk resize
  • Loading branch information
chrisroberts authored Aug 2, 2024
2 parents 3a1ede2 + 773b0d9 commit be21778
Show file tree
Hide file tree
Showing 7 changed files with 264 additions and 171 deletions.
6 changes: 2 additions & 4 deletions lib/vagrant-vmware-desktop/action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -361,10 +361,8 @@ def self.action_start
b3.use SetHostname
end

Vagrant::Util::Experimental.guard_with(:disks) do
b3.use CleanupDisks
b3.use Disk
end
b3.use CleanupDisks
b3.use Disk
b3.use VMXModify
b3.use PrepareForwardedPortCollisionParams
b3.use HandleForwardedPortCollisions
Expand Down
15 changes: 12 additions & 3 deletions lib/vagrant-vmware-desktop/cap/disk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module Disk
# 5 : compressed disk optimized for streaming
# 6 : thin provisioned virtual disk - ESX 3.x and above
DEFAULT_DISK_TYPE = 0.freeze
PRIMARY_DISK_SLOT = "scsi0:0".freeze
PRIMARY_DISK_SLOTS = ["scsi0:0", "sata0:0", "ide0:0"].map(&:freeze).freeze

def self.set_default_disk_ext(machine)
DEFAULT_DISK_EXT
Expand Down Expand Up @@ -112,7 +112,11 @@ def self.cleanup_disks(machine, defined_disks, disk_meta)
# @return [Hash, nil] - A hash of the current disk, nil if not found
def self.get_disk(disk, all_disks)
if disk.primary
return all_disks[PRIMARY_DISK_SLOT]
PRIMARY_DISK_SLOTS.each do |primary_slot|
disk_info = all_disks[primary_slot]
@@logger.debug("disk info for primary slot #{primary_slot} - #{disk_info}")
return disk_info if disk_info["present"].to_s.upcase == "TRUE"
end
else
if disk.type == :dvd
all_disks.values.detect { |v| v["filename"] == disk.file }
Expand Down Expand Up @@ -153,7 +157,12 @@ def self.setup_disk(machine, disk, attached_disks)

# disk.size is in bytes
if disk.size > machine.provider.driver.get_disk_size(disk_path)
grow_disk(machine, disk_path, disk)
if disk.primary && machine.provider.driver.is_linked_clone?
machine.env.ui.warn(I18n.t("hashicorp.vagrant_vmware_desktop.disk_not_growing_linked_primary"))
@@logger.warn("Not growing primary disk - guest is linked clone")
else
grow_disk(machine, disk_path, disk)
end
elsif disk.size < machine.provider.driver.get_disk_size(disk_path)
machine.env.ui.warn(I18n.t("hashicorp.vagrant_vmware_desktop.disk_not_shrinking", path: disk.name))
@@logger.warn("Not shrinking disk #{disk.name}")
Expand Down
26 changes: 26 additions & 0 deletions lib/vagrant-vmware-desktop/driver/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,32 @@ def get_disk_size(disk_path)
disk_size
end

# @return [Boolean] Instance is a linked clone
def is_linked_clone?
if @vmsd_path.nil?
@vm_dir.children(true).each do |child|
if child.basename.to_s.match(/\.vmsd$/)
@vmsd_path = child
end
end
end

return false if @vmsd_path.nil?

if @is_clone.nil?
@is_clone = false

File.readlines(@vmsd_path).each do |line|
if line.start_with?("cloneOf")
@is_clone = true
break
end
end
end

@is_clone
end

protected

# This reads the latest DHCP lease for a MAC address on the
Expand Down
38 changes: 18 additions & 20 deletions lib/vagrant-vmware-desktop/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,26 +99,24 @@ def self.provider_options
Cap::Provider
end

Vagrant::Util::Experimental.guard_with(:disks) do
provider_capability(p_name, :set_default_disk_ext) do
require File.expand_path("../cap/disk", __FILE__)
Cap::Disk
end

provider_capability(p_name, :default_disk_exts) do
require File.expand_path("../cap/disk", __FILE__)
Cap::Disk
end

provider_capability(p_name, :configure_disks) do
require File.expand_path("../cap/disk", __FILE__)
Cap::Disk
end

provider_capability(p_name, :cleanup_disks) do
require File.expand_path("../cap/disk", __FILE__)
Cap::Disk
end
provider_capability(p_name, :set_default_disk_ext) do
require File.expand_path("../cap/disk", __FILE__)
Cap::Disk
end

provider_capability(p_name, :default_disk_exts) do
require File.expand_path("../cap/disk", __FILE__)
Cap::Disk
end

provider_capability(p_name, :configure_disks) do
require File.expand_path("../cap/disk", __FILE__)
Cap::Disk
end

provider_capability(p_name, :cleanup_disks) do
require File.expand_path("../cap/disk", __FILE__)
Cap::Disk
end
end

Expand Down
3 changes: 3 additions & 0 deletions locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ en:
Configuring network adapters within the VM...
destroying: |-
Deleting the VM...
disk_not_growing_linked_primary: |-
Increasing the size of the primary disk is not allowed for linked
clones. Primary disk of the guest remains unchanged.
disk_not_shrinking: |-
Shrinking disks is not supported. Not shrinking disk %{path}
discarding_suspended_state: |-
Expand Down
27 changes: 25 additions & 2 deletions spec/vagrant-vmware-desktop/cap/disk_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
require "vagrant-vmware-desktop/cap/disk"

describe HashiCorp::VagrantVMwareDesktop::Cap::Disk do
let(:driver) { double("driver") }
let(:driver) { double("driver", 'is_linked_clone?': linked_clone) }
let(:linked_clone) { false }
let(:ui) { double("ui") }
let(:machine) { double("machine", provider: double("provider", driver: driver),
env: double("env", ui: ui))}
Expand Down Expand Up @@ -44,7 +45,6 @@
]}

before do
allow(Vagrant::Util::Experimental).to receive(:feature_enabled?).and_return(true)
allow(driver).to receive(:vmx_path).and_return(vmx_path)
end

Expand Down Expand Up @@ -196,6 +196,29 @@
}.to raise_error(HashiCorp::VagrantVMwareDesktop::Errors::DiskNotResizedSnapshot)
end

context "when guest is a linked clone" do
let(:linked_clone) { true }

before do
allow(ui).to receive(:warn)
allow(driver).to receive(:grow_disk)
allow(driver).to receive(:snapshot_list).and_return([])
end

it "should warn it will not grow linked clone" do
expect(ui).to receive(:warn).with(/Primary disk of the guest remains/)
described_class.configure_disks(machine, defined_disks)
end

it "should not grow the primary disk" do
expect(driver).to receive(:grow_disk).once
expect(described_class).to_not receive(:create_disk)
expect(driver).to_not receive(:add_disk_to_vmx)
configured_disks = described_class.configure_disks(machine, defined_disks)
expect(configured_disks[:disk].map { |d| d.flatten.any?(nil) }.any?(true)).to be(false)
end
end

context "vmx pointing to not root metadata disk" do
let(:defined_disks) { [
double("disk", id: "abcde", name: "disk", size: 196608, primary: true,
Expand Down
Loading

0 comments on commit be21778

Please sign in to comment.