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

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

Instance Method Summary collapse

Instance Method Details

#find_first_iso(vm_info) ⇒ Object

Raises:

  • (KeyError)


74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/vagrant-mount/actions/providers/virtualbox/driver/base.rb', line 74

def find_first_iso(vm_info)
  device_id = port_id = nil
  controller = vm_info[:storagecontrollers].find do |ctrl|
    device_id = ctrl[:devices].find_index do |device|
      port_id = device[:ports].find_index do |port|
        port[:mount] =~ /\.iso$/i if port
      end
    end
  end
  raise KeyError unless controller
  @logger.debug "Found ISO on Storage: #{controller[:name]}, device ##{device_id}, port ##{port_id}"
  return [controller[:name], device_id, port_id]
end

#find_free_port(vm_info, controller_types) ⇒ Object

Raises:

  • (KeyError)


88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/vagrant-mount/actions/providers/virtualbox/driver/base.rb', line 88

def find_free_port(vm_info, controller_types)
  device_id = port_id = nil
  controller = vm_info[:storagecontrollers].find do |ctrl|
    device_id = ctrl[:devices].find_index do |device|
      port_id = device[:ports].find_index do |port|
        port.nil? || port[:mount] == 'emptydrive'
      end
    end
  end
  raise KeyError, "No suitable Controller on VM #{@uuid}" unless controller
  @logger.debug "Found a suitable Storage: #{controller[:name]}, device ##{device_id}, port ##{port_id}"
  return [controller[:name], device_id, port_id]
end

#find_iso(vm_info, mount_point) ⇒ Object

Raises:

  • (KeyError)


60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/vagrant-mount/actions/providers/virtualbox/driver/base.rb', line 60

def find_iso(vm_info, mount_point)
  device_id = port_id = nil
  controller = vm_info[:storagecontrollers].find do |ctrl|
    device_id = ctrl[:devices].find_index do |device|
      port_id = device[:ports].find_index do |port|
        port[:mount].include? mount_point if port
      end
    end
  end
  raise KeyError unless controller
  @logger.debug "Found ISO on Storage: #{controller[:name]}, device ##{device_id}, port ##{port_id}"
  return [controller[:name], device_id, port_id]
end

#get_vm_info(name_or_uuid) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/vagrant-mount/actions/providers/virtualbox/driver/base.rb', line 102

def get_vm_info(name_or_uuid)
  @logger.debug "Fetching info for VM #{name_or_uuid}"
  output = execute('showvminfo', name_or_uuid, '--machinereadable', retryable: true)
  nic_index = nil
  output.split("\n").inject({storagecontrollers: [], nics: [], lpts: [], uarts: []}) do |hash, line|
    if line =~ /^"?([^="]+)"?="?(|[^"]+)"?$/
      key   = $1
      value = $2.to_s
      case key
        when /^storagecontroller([a-z]+)(\d+)$/
          key   = $1.tr('- ', '__').to_sym
          controller = hash[:storagecontrollers][$2.to_i]
          if controller
            controller[key] = value
            if key == :portcount
              controller[:devices] = value.to_i.times.inject([]) {|array| array << { ports: [nil, nil] } }
            end
          else
            hash[:storagecontrollers] << { key => value }
          end
        when /^([a-zA-Z ]+)(?:-([a-zA-Z]+))?-(\d+)-(\d+)$/
          unless value == 'none'
            key        = ($2 || 'mount').tr('- ', '__').to_sym
            controller = hash[:storagecontrollers].find { |s| s[:name] == $1 }
            raise KeyError, $1 unless controller
            device = controller[:devices][$4.to_i]
            raise IndexError, $4.to_i unless device
            raise IndexError, $3.to_i unless $3.to_i < 2
            device[:ports][$3.to_i] ||= {}
            device[:ports][$3.to_i][key] = value
          end
        when /^natnet(\d+)$/
          nic_index = $1.to_i - 1
          hash[:nics][nic_index] ||= { forwardings: [] }
          hash[:nics][nic_index][:natnet] = value
        when /^(cableconnected|macaddress|nic|nictype|nicspeed)(\d+)$/
          hash[:nics][$2.to_i - 1][$1.to_sym] = value unless value == 'none'
        when /^(mtu|sockSnd|sockRcv|tcpWndSnd|tcpWndRcv)$/
          hash[:nics][nic_index][$1.to_sym] = value
        when /^Forwarding\((\d+)\)$/
          hash[:nics][nic_index][:forwardings][$1.to_i] = value
        when /^(lpt|uart)(\d+)$/
          hash[($1 << 's').to_sym][$2.to_i - 1] = value
        else
          hash[key.tr('- ','__').to_sym] = value
      end
    end
    hash
  end
end

#mount(mount_point) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/vagrant-mount/actions/providers/virtualbox/driver/base.rb', line 5

def mount(mount_point)
  # Find an IDE or SCSI controller
  @logger.debug "Mounting #{mount_point}"
  info = get_vm_info(@uuid)
  begin
    controller_name, device_id, port_id = find_iso(info, mount_point)
    @logger.debug "Already mounted on #{controller_name}, device: #{device_id}, port: #{port_id}"
    return 1
  rescue KeyError
    @logger.debug "Not mounted yet, we are good to go"
  end
  ide_types  = ['PIIX3', 'PIIX4', 'ICH6']
  controller_name, device_id, port_id = find_free_port(info, ide_types)
  execute('storageattach', @uuid,
          '--storagectl', controller_name,
          '--device', device_id.to_s,
          '--port', port_id.to_s,
          '--type', 'dvddrive',
          '--medium', mount_point
         )
end

#unmount(mount_point, remove_device = false) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/vagrant-mount/actions/providers/virtualbox/driver/base.rb', line 27

def unmount(mount_point, remove_device=false)
  # Find an IDE or SCSI controller
  @logger.debug "Mounting #{mount_point}"
  info = get_vm_info(@uuid)
  begin
    if mount_point
      controller_name, device_id, port_id = find_iso(info, mount_point)
    else
      controller_name, device_id, port_id = find_first_iso(info)
    end
    @logger.debug "Mounted on #{controller_name}, device: #{device_id}, port: #{port_id}"
    if remove_device
      execute('storageattach', @uuid,
              '--storagectl', controller_name,
              '--device', device_id.to_s,
              '--port', port_id.to_s,
              '--medium', 'none'
            )
    else
      execute('storageattach', @uuid,
              '--storagectl', controller_name,
              '--device', device_id.to_s,
              '--port', port_id.to_s,
              '--type', 'dvddrive',
              '--medium', 'emptydrive'
            )
    end
  rescue KeyError
    @logger.debug "Not mounted, we cannot proceed"
    return 0
  end
end