Class: VagrantPlugins::ProviderVirtualBox::Driver::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-persistent-storage/providers/virtualbox/driver/base.rb

Instance Method Summary collapse

Instance Method Details

#attach_storage(location) ⇒ Object



22
23
24
# File 'lib/vagrant-persistent-storage/providers/virtualbox/driver/base.rb', line 22

def attach_storage(location)
  execute("storageattach", @uuid, "--storagectl", get_controller_name, "--port", "1", "--device", "0", "--type", "hdd", "--medium", "#{location}")
end

#create_adapterObject



8
9
10
11
12
13
14
15
16
# File 'lib/vagrant-persistent-storage/providers/virtualbox/driver/base.rb', line 8

def create_adapter
  controller_name = get_controller_name
  if controller_name.nil?
    controller_name = "SATA Controller"
    execute("storagectl", @uuid, "--name", controller_name, "--" + ((@version.start_with?("4.3") || @version.start_with?("5.")) ? "" : "sata") + "portcount", "2", "--add", "sata")
  else
    execute("storagectl", @uuid, "--name", controller_name, "--" + ((@version.start_with?("4.3") || @version.start_with?("5.")) ? "" : "sata") + "portcount", "2")
  end
end

#create_storage(location, size) ⇒ Object



18
19
20
# File 'lib/vagrant-persistent-storage/providers/virtualbox/driver/base.rb', line 18

def create_storage(location, size)
  execute("createhd", "--filename", location, "--size", "#{size}")
end

#detach_storage(location) ⇒ Object



26
27
28
29
30
31
# File 'lib/vagrant-persistent-storage/providers/virtualbox/driver/base.rb', line 26

def detach_storage(location)
  persistent_storage = read_persistent_storage()
  if location and persistent_storage and persistent_storage != "none" and identical_files(persistent_storage, location)
    execute("storageattach", @uuid, "--storagectl", get_controller_name, "--port", "1", "--device", "0", "--type", "hdd", "--medium", "none")
  end
end

#get_controller_nameObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/vagrant-persistent-storage/providers/virtualbox/driver/base.rb', line 47

def get_controller_name
controller_number = nil
  controllers = Hash.new
  info = execute("showvminfo", @uuid, "--machinereadable", :retryable => true)
  info.split("\n").each do |line|
    controllers[$1] = $2 if line =~ /^storagecontrollername(\d+)="(.*)"/
    controller_number = $1 if line =~ /^storagecontrollertype(\d+)="(IntelAhci|PIIX4)"/
  end

  if controller_number.nil?
    return nil
  end

  return controllers[controller_number]
end

#identical_files(file1, file2) ⇒ Object



43
44
45
# File 'lib/vagrant-persistent-storage/providers/virtualbox/driver/base.rb', line 43

def identical_files(file1, file2)
  return File.identical?(Pathname.new(file1).realpath, Pathname.new(file2).realpath)
end

#read_persistent_storageObject



33
34
35
36
37
38
39
40
41
# File 'lib/vagrant-persistent-storage/providers/virtualbox/driver/base.rb', line 33

def read_persistent_storage()
  ## Ensure previous operations are complete - bad practise yes, not sure how to avoid this:
  sleep 3
  info = execute("showvminfo", @uuid, "--machinereadable", :retryable => true)
  info.split("\n").each do |line|
    return $1.to_s if line =~ /^"#{get_controller_name}-1-0"="(.+?)"$/
  end
  nil
end