Class: Fog::Hyperv::Compute::Vhd

Inherits:
Model
  • Object
show all
Defined in:
lib/fog/hyperv/compute/models/vhd.rb

Overview

A VM hard drive - VHD - file

Constant Summary collapse

VHD_TYPE_ENUM_VALUES =
Note:

Defined by Microsoft.Vhd.PowerShell.VhdType

VHD types

{
  Unknown:      0,
  Fixed:        2,
  Dynamic:      3,
  Differencing: 4
}.freeze
VHD_FORMAT_ENUM_VALUES =
Note:

Defined by Microsoft.Vhd.PowerShell.VhdFormat

VHD formats

{
  Unknown: 0,
  VHD:     2,
  VHDX:    3,
  VHDSet:  4
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Model

#initialize

Methods included from ModelExtends

#collection

Methods included from ModelIncludes

#cluster, #computer, #dirty, #dirty?, #vm

Constructor Details

This class inherits a constructor from Fog::Hyperv::Model

Instance Attribute Details

#attachedBoolean (readonly)

Returns is the VHD attached to something.

Returns:

  • (Boolean)

    is the VHD attached to something



37
# File 'lib/fog/hyperv/compute/models/vhd.rb', line 37

attribute :attached, type: :boolean

#basenameString (readonly)

Returns the basename of the VHD file, without extension.

Returns:

  • (String)

    the basename of the VHD file, without extension



80
# File 'lib/fog/hyperv/compute/models/vhd.rb', line 80

attribute :basename

#block_sizeInteger (readonly)

Returns the block size of the VHD in bytes.

Returns:

  • (Integer)

    the block size of the VHD in bytes



40
# File 'lib/fog/hyperv/compute/models/vhd.rb', line 40

attribute :block_size, type: :integer

#computer_nameString (readonly)

Returns the name of the computer hosting this VHD.

Returns:

  • (String)

    the name of the computer hosting this VHD



33
# File 'lib/fog/hyperv/compute/models/vhd.rb', line 33

attribute :computer_name

#disk_identifierString (readonly)

Returns the guid of this VHD.

Returns:

  • (String)

    the guid of this VHD



29
# File 'lib/fog/hyperv/compute/models/vhd.rb', line 29

identity :disk_identifier

#disk_numberInteger? (readonly)

Returns the disk number.

Returns:

  • (Integer, nil)

    the disk number



43
# File 'lib/fog/hyperv/compute/models/vhd.rb', line 43

attribute :disk_number

#file_sizeInteger? (readonly)

Returns the size of the VHD file in bytes.

Returns:

  • (Integer, nil)

    the size of the VHD file in bytes



46
# File 'lib/fog/hyperv/compute/models/vhd.rb', line 46

attribute :file_size

#logical_sector_size512, 4096 (readonly)

Returns the logical sector size for the VHD in bytes.

Returns:

  • (512, 4096)

    the logical sector size for the VHD in bytes



49
# File 'lib/fog/hyperv/compute/models/vhd.rb', line 49

attribute :logical_sector_size, type: :integer

#minimum_sizeInteger? (readonly)

Returns the minimum possible size of the VHD in bytes, for shrinking purposes.

Returns:

  • (Integer, nil)

    the minimum possible size of the VHD in bytes, for shrinking purposes



52
# File 'lib/fog/hyperv/compute/models/vhd.rb', line 52

attribute :minimum_size

#parent_pathString? (readonly)

Returns the path to the parent VHD.

Returns:

  • (String, nil)

    the path to the parent VHD



55
# File 'lib/fog/hyperv/compute/models/vhd.rb', line 55

attribute :parent_path

#pathString? (readonly)

Returns the path to the VHD on disk.

Returns:

  • (String, nil)

    the path to the VHD on disk



58
# File 'lib/fog/hyperv/compute/models/vhd.rb', line 58

attribute :path

#physical_sector_size_bytes512, 4096

Returns the physical sector size for the VHD in bytes.

Returns:

  • (512, 4096)

    the physical sector size for the VHD in bytes



64
# File 'lib/fog/hyperv/compute/models/vhd.rb', line 64

attribute :physical_sector_size, type: :integer

#pool_nameString (readonly)

Returns the name of the pool storing this VHD.

Returns:

  • (String)

    the name of the pool storing this VHD



61
# File 'lib/fog/hyperv/compute/models/vhd.rb', line 61

attribute :pool_name

#sizeInteger?

Note:

Defaults to 32GB if not specified

Returns the size of the VHD in bytes.

Returns:

  • (Integer, nil)

    the size of the VHD in bytes



68
# File 'lib/fog/hyperv/compute/models/vhd.rb', line 68

attribute :size, default: 32 * 1024 * 1024 * 1024

#unc_pathString (readonly)

Returns the UNC path to the VHD file in a cluster.

Returns:

  • (String)

    the UNC path to the VHD file in a cluster



84
85
86
87
88
# File 'lib/fog/hyperv/compute/models/vhd.rb', line 84

def unc_path
  requires :path

  "\\\\#{computer_name || 'localhost'}\\#{path.tr ':', '$'}"
end

#vhd_format:Unknown, ... (readonly)

Returns the format of the VHD.

Returns:

  • (:Unknown, :VHD, :VHDX, :VHDSet)

    the format of the VHD

See Also:



72
# File 'lib/fog/hyperv/compute/models/vhd.rb', line 72

attribute :vhd_format, type: :hypervenum, default: :VHDX, values: VHD_FORMAT_ENUM_VALUES

#vhd_type:Unknown, ... (readonly)

Returns the type of the VHD.

Returns:

  • (:Unknown, :Fixed, :Dynamic, :Differencing)

    the type of the VHD

See Also:



76
# File 'lib/fog/hyperv/compute/models/vhd.rb', line 76

attribute :vhd_type, type: :hypervenum, default: :Dynamic, values: VHD_TYPE_ENUM_VALUES

Instance Method Details

#createObject

Save the VHD to Hyper-V



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
# File 'lib/fog/hyperv/compute/models/vhd.rb', line 91

def create
  if basename
    requires :vm

    attributes[:path] ||= vm.build_vhd_path("#{basename}.#{vhd_ext}")
  end

  requires_one :path, :disk_number

  attrs = {
    source_disk: disk_number,
    logical_sector_size_bytes: logical_sector_size,
    physical_sector_size_bytes: physical_sector_size
  }.compact
  case vhd_type
  when :Dynamic
    attrs[:dynamic] = true
    requires :size unless disk_number
  when :Differencing
    requires :parent_path
    attrs[:differencing] = true
    attrs[:parent_path] = parent_path
    attrs.delete :source_disk
    attrs.delete :logical_sector_size_bytes
  when :Fixed
    attrs[:fixed] = true
    requires :size unless disk_number
  else
    raise "Invalid VHD type #{vhd_type.inspect}, must be :Dynamic, :Fixed, or :Differencing"
  end

  merge_attributes(
    service.new_vhd(
      computer_name:,

      path:,
      block_size_bytes: block_size,
      size_bytes: size,
      **attrs,

      _return_fields: self.class.attributes - %i[basename]
    )
  )
end

#destroyObject

Remove the VHD from disk



169
170
171
172
173
174
175
176
177
# File 'lib/fog/hyperv/compute/models/vhd.rb', line 169

def destroy
  requires :path

  service.remove_item(
    computer_name:,
    path:
  )
  true
end

#optimize(mode: nil) ⇒ Object

Optimizes the VHD on disk

Parameters:

  • mode (:full, :pretrimmed, :prezeroed, :quick, :retrim, nil) (defaults to: nil)

    the optimization mode to use, will default to :full/:quick depending on VHD type



183
184
185
186
187
188
189
190
191
192
# File 'lib/fog/hyperv/compute/models/vhd.rb', line 183

def optimize(mode: nil)
  requires :path

  service.optimize_vhd(
    computer_name:,
    path:,
    mode:
  )
  true
end

#reloadObject

Reload the VHD attributes from Hyper-V



153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/fog/hyperv/compute/models/vhd.rb', line 153

def reload
  requires_one :path, :disk_number

  data = service.get_vhd(
    computer_name:,
    path:,
    disk_number:,

    _return_fields: self.class.attributes - %i[basename]
  )
  return unless data

  merge_attributes(data)
end

#updateObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/fog/hyperv/compute/models/vhd.rb', line 136

def update
  requires :path

  return self unless changed?(:size)

  service.resize_vhd(
    computer_name: old.computer_name,
    path: old.path,

    size_bytes: size
  )
  @old.size = size

  self
end