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_sata_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
  sata_controller_name = get_sata_controller_name
  if sata_controller_name.nil?
    sata_controller_name = "SATA Controller"
    execute("storagectl", @uuid, "--name", sata_controller_name, "--" + (@version.start_with?("4.3") ? "" : "sata") + "portcount", "2", "--add", "sata")
  else
    execute("storagectl", @uuid, "--name", sata_controller_name, "--" + (@version.start_with?("4.3") ? "" : "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
# File 'lib/vagrant-persistent-storage/providers/virtualbox/driver/base.rb', line 26

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

#get_sata_controller_nameObject



46
47
48
49
50
51
52
53
54
55
# File 'lib/vagrant-persistent-storage/providers/virtualbox/driver/base.rb', line 46

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

#identical_files(file1, file2) ⇒ Object



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

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

#read_persistent_storage(location) ⇒ Object



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

def read_persistent_storage(location)
  ## 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_sata_controller_name}-1-0"="(.+?)"$/
  end
  nil
end