Class: MiqVm

Inherits:
Object
  • Object
show all
Defined in:
lib/MiqVm/MiqVm.rb

Direct Known Subclasses

MiqAzureVm, MiqLocalVm, MiqRhevmVm

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vmCfg, ost = nil) ⇒ MiqVm

Returns a new instance of MiqVm.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
# File 'lib/MiqVm/MiqVm.rb', line 11

def initialize(vmCfg, ost = nil)
  @ost = ost || OpenStruct.new
  $log.debug "MiqVm::initialize: @ost = nil" if $log && !@ost
  @vmDisks = nil
  @wholeDisks = []
  @rootTrees = nil
  @volumeManager = nil
  @applianceVolumeManager = nil
  @vmConfigFile = ""
  @diskInitErrors = {}
  unless vmCfg.kind_of?(Hash)
    @vmConfigFile = vmCfg
    @vmDir = File.dirname(vmCfg)
  end

  $log.debug "MiqVm::initialize: @ost.openParent = #{@ost.openParent}" if $log

  #
  # If we're passed an MiqVim object, then use VIM to obtain the Vm's
  # configuration through the instantiated server.
  # If we're passed a snapshot ID, then obtain the configration of the
  # VM when the snapshot was taken.
  #
  # TODO: move to MiqVmwareVm
  if (@vim = @ost.miqVim)
    $log.debug "MiqVm::initialize: accessing VM through server: #{@vim.server}" if $log.debug?
    @vimVm = @vim.getVimVm(vmCfg)
    $log.debug "MiqVm::initialize: setting @ost.miqVimVm = #{@vimVm.class}" if $log.debug?
    @ost.miqVimVm = @vimVm
    @vmConfig = VmConfig.new(@vimVm.getCfg(@ost.snapId))
  # TODO: move this to MiqRhevmVm.
  elsif (@rhevm = @ost.miqRhevm)
    $log.debug "MiqVm::initialize: accessing VM through RHEVM server" if $log.debug?
    $log.debug "MiqVm::initialize: vmCfg = #{vmCfg}"
    @rhevmVm = @rhevm.vm_or_template_by_path(vmCfg)
    $log.debug "MiqVm::initialize: setting @ost.miqRhevmVm = #{@rhevmVm.class}" if $log.debug?
    @ost.miqRhevmVm = @rhevmVm
    @vmConfig = VmConfig.new(getCfg(@ost.snapId))
    $log.debug "MiqVm::initialize: @vmConfig.getHash = #{@vmConfig.getHash.inspect}"
    $log.debug "MiqVm::initialize: @vmConfig.getDiskFileHash = #{@vmConfig.getDiskFileHash.inspect}"
  else
    @vimVm = nil
    @vmConfig = VmConfig.new(vmCfg)
  end
end

Instance Attribute Details

#diskInitErrorsObject (readonly)

Returns the value of attribute diskInitErrors.



9
10
11
# File 'lib/MiqVm/MiqVm.rb', line 9

def diskInitErrors
  @diskInitErrors
end

#rhevmObject (readonly)

Returns the value of attribute rhevm.



9
10
11
# File 'lib/MiqVm/MiqVm.rb', line 9

def rhevm
  @rhevm
end

#rhevmVmObject (readonly)

Returns the value of attribute rhevmVm.



9
10
11
# File 'lib/MiqVm/MiqVm.rb', line 9

def rhevmVm
  @rhevmVm
end

#vimObject (readonly)

Returns the value of attribute vim.



9
10
11
# File 'lib/MiqVm/MiqVm.rb', line 9

def vim
  @vim
end

#vimVmObject (readonly)

Returns the value of attribute vimVm.



9
10
11
# File 'lib/MiqVm/MiqVm.rb', line 9

def vimVm
  @vimVm
end

#vmConfigObject (readonly)

Returns the value of attribute vmConfig.



9
10
11
# File 'lib/MiqVm/MiqVm.rb', line 9

def vmConfig
  @vmConfig
end

#vmConfigFileObject (readonly)

Returns the value of attribute vmConfigFile.



9
10
11
# File 'lib/MiqVm/MiqVm.rb', line 9

def vmConfigFile
  @vmConfigFile
end

#wholeDisksObject (readonly)

Returns the value of attribute wholeDisks.



9
10
11
# File 'lib/MiqVm/MiqVm.rb', line 9

def wholeDisks
  @wholeDisks
end

Instance Method Details

#applianceVolumeManagerObject



161
162
163
164
# File 'lib/MiqVm/MiqVm.rb', line 161

def applianceVolumeManager
  return nil if @ost.nfs_storage_mounted
  @applianceVolumeManager ||= MiqVolumeManager.fromNativePvs
end

#extract(c) ⇒ Object



195
196
197
198
199
# File 'lib/MiqVm/MiqVm.rb', line 195

def extract(c)
  xml = miq_extract.extract(c)
  raise "Could not extract \"#{c}\" from VM" unless xml
  (xml)
end

#miq_extractObject



191
192
193
# File 'lib/MiqVm/MiqVm.rb', line 191

def miq_extract
  @miq_extract ||= MIQExtract.new(self, @ost)
end

#openDisks(diskFiles) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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
# File 'lib/MiqVm/MiqVm.rb', line 66

def openDisks(diskFiles)
  pVolumes = []

  $log.debug "openDisks: no disk files supplied." unless diskFiles

  #
  # Build a list of the VM's physical volumes.
  #
  diskFiles.each do |dtag, df|
    $log.debug "openDisks: processing disk file (#{dtag}): #{df}"
    dInfo = OpenStruct.new

    if @ost.miqVim
      dInfo.vixDiskInfo            = {}
      dInfo.vixDiskInfo[:fileName] = @ost.miqVim.datastorePath(df)
      if @ost.miqVimVm
        @vdlConnection = @ost.miqVimVm.vdlVcConnection unless @vdlConnection
      else
        @vdlConnection = @ost.miqVim.vdlConnection unless @vdlConnection
      end
      $log.debug "openDisks: using disk file path: #{dInfo.vixDiskInfo[:fileName]}"
      dInfo.vixDiskInfo[:connection]  = @vdlConnection
    else
      dInfo.fileName = df
      disk_format = @vmConfig.getHash["#{dtag}.format"]  # Set by rhevm for iscsi and fcp disks
      dInfo.format = disk_format unless disk_format.blank?
    end

    mode = @vmConfig.getHash["#{dtag}.mode"]

    dInfo.hardwareId = dtag
    dInfo.baseOnly = @ost.openParent unless mode && mode["independent"]
    dInfo.rawDisk = @ost.rawDisk
    $log.debug "MiqVm::openDisks: dInfo.baseOnly = #{dInfo.baseOnly}"

    begin
      d = applianceVolumeManager && applianceVolumeManager.lvHash[dInfo.fileName] if @rhevm
      if d
        $log.debug "MiqVm::openDisks: using applianceVolumeManager for #{dInfo.fileName}" if $log.debug?
        d.dInfo.fileName = dInfo.fileName
        d.dInfo.hardwareId = dInfo.hardwareId
        d.dInfo.baseOnly = dInfo.baseOnly
        d.dInfo.format = dInfo.format if dInfo.format
        d.dInfo.applianceVolumeManager = applianceVolumeManager
        #
        # Here, we need to probe the disk to determine its data format,
        # QCOW for example. If the disk format is not flat, push a disk
        # supporting the format on top of this disk. Then set d to point
        # to the new top disk.
        #
        d = d.pushFormatSupport
      else
        d = MiqDisk.getDisk(dInfo)
        # I am not sure if getting a nil handle back should throw an error or not.
        # For now I am just skipping to the next disk.  (GMM)
        next if d.nil?
      end
    rescue => err
      $log.error "Couldn't open disk file: #{df}"
      $log.error err.to_s
      $log.debug err.backtrace.join("\n")
      @diskInitErrors[df] = err.to_s
      next
    end

    @wholeDisks << d
    p = d.getPartitions
    if p.empty?
      #
      # If the disk has no partitions, the whole disk can be a single volume.
      #
      pVolumes << d
    else
      #
      # If the disk is partitioned, the partitions are physical volumes,
      # but not the whild disk.
      #
      pVolumes.concat(p)
    end
  end

  pVolumes
end

#rootTreesObject

def openDisks



150
151
152
153
154
155
# File 'lib/MiqVm/MiqVm.rb', line 150

def rootTrees
  return @rootTrees if @rootTrees
  @rootTrees = MiqMountManager.mountVolumes(volumeManager, @vmConfig, @ost)
  volumeManager.rootTrees = @rootTrees
  @rootTrees
end

#snapshots(refresh = false) ⇒ Object



166
167
168
169
# File 'lib/MiqVm/MiqVm.rb', line 166

def snapshots(refresh = false)
  return nil unless @vimVm
  return @vimVm.snapshotInfo(refresh) if @vimVm
end

#unmountObject



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/MiqVm/MiqVm.rb', line 171

def unmount
  $log.info "MiqVm.unmount called."
  @wholeDisks.each(&:close)
  @wholeDisks.clear
  if @volumeManager
    @volumeManager.close
    @volumeManager = nil
  end
  @applianceVolumeManager.closeAll if @applianceVolumeManager
  @applianceVolumeManager = nil
  @ost.miqVim.closeVdlConnection(@vdlConnection) if @vdlConnection
  if @volMgrPS
    @volMgrPS.postMount
    @volMgrPS = nil
  end
  @vimVm.release if @vimVm
  @rootTrees = nil
  @vmDisks = nil
end

#vmDisksObject

def initialize



57
58
59
60
61
62
63
64
# File 'lib/MiqVm/MiqVm.rb', line 57

def vmDisks
  @vmDisks ||= begin
    @volMgrPS = VolMgrPlatformSupport.new(@vmConfig.configFile, @ost)
    @volMgrPS.preMount

    openDisks(@vmConfig.getDiskFileHash)
  end
end

#volumeManagerObject



157
158
159
# File 'lib/MiqVm/MiqVm.rb', line 157

def volumeManager
  @volumeManager ||= MiqVolumeManager.new(vmDisks)
end