Class: VirtualBox::Vm::Disk

Inherits:
Object
  • Object
show all
Defined in:
lib/virtual_box/vm/disk.rb

Overview

Descriptor for a VirtualBox hard-disk or DVD image.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Disk

Creates an image descriptor with the given attributes.

Parameters:

  • options (Hash<Symbol, Object>)

    ActiveRecord-style initial values for attributes; can be used together with Disk#to_hash to save and restore



38
39
40
41
# File 'lib/virtual_box/vm/disk.rb', line 38

def initialize(options)
  options.each { |k, v| self.send :"#{k}=", v }
  self.file = File.expand_path file
end

Instance Attribute Details

#fileString

Path to the file storing this disk image.

Returns:

  • (String)


10
11
12
# File 'lib/virtual_box/vm/disk.rb', line 10

def file
  @file
end

#formatSymbol

The format of this disk image.

The recognized formats are :raw, :vdi, :vmdk, and :vhd.

Returns:

  • (Symbol)


16
17
18
# File 'lib/virtual_box/vm/disk.rb', line 16

def format
  @format
end

#mediaSymbol

The type of media that the image represents.

The recognized types are :disk and :dvd.

Returns:

  • (Symbol)


22
23
24
# File 'lib/virtual_box/vm/disk.rb', line 22

def media
  @media
end

Class Method Details

.create(options) ⇒ VirtualBox::Vm::Disk

Creates a VirtualBox disk image.

Parameters:

  • options (Hash)

    one or many of the options documented below

Options Hash (options):

  • file (String)

    path to the file that will hold the disk image

  • size (Integer)

    the image size, in bytes

  • format (Symbol)

    the image format; if not provided, an intelligent guess is made, based on the file extension

  • prealloc (Boolean)

    unless explicitly set to true, the image file will grow in size as the disk’s blocks are used

Returns:



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/virtual_box/vm/disk.rb', line 83

def self.create(options)
  path = options[:file]
  format = options[:format] || guess_image_format(path)
  size_mb = (options[:size] / (1024 * 1024)).to_i
  memo = options[:memo] || 'Created with the virtual_box RubyGem'
  variant = options[:prealloc] ? 'Fixed' : 'Standard'
  
  VirtualBox.run_command! ['VBoxManage', '--nologo', 'createhd',
      '--filename', path, '--size', size_mb.to_s, '--format', format.to_s,
      '--variant', variant]
  new :file => path, :format => format, :media => :disk
end

.guess_image_format(image_file) ⇒ Object

Disk image format based on the extension in the file name.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/virtual_box/vm/disk.rb', line 107

def self.guess_image_format(image_file)
  parts = File.basename(image_file).split('.')
  if parts.length >= 2
    case parts.last
    when 'vdi'
      :vdi
    when 'vmdk'
      :vmdk
    when 'vhd'
      :vhd
    when 'iso'
      :raw
    else
      :vdi
    end
  else
    :vdi
  end
end

.guess_media_type(image_file) ⇒ Object

Disk media type on the extension in the file name.



128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/virtual_box/vm/disk.rb', line 128

def self.guess_media_type(image_file)
  parts = File.basename(image_file).split('.')
  if parts.length >= 2
    case parts.last
    when 'iso'
      :dvd
    else
      :disk
    end
  else
    :disk
  end
end

Instance Method Details

#add_to(vm, io_bus, port, device) ⇒ VirtualBox::Vm::Disk

Attaches this disk to a virtual machine.

Parameters:

  • vm (VirtualBox::Vm)

    the VM that this image will be attached to

  • io_bus (VirtualBox::Vm::IoBus)

    the IO controller that this disk will be attached to

  • port (Integer)

    the IO bus port this disk will be connected to

  • device (Integer)

    number indicating the device’s ordering on its port

Returns:



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/virtual_box/vm/disk.rb', line 51

def add_to(vm, io_bus, port, device)
  media_arg = case media
  when :disk
    'hdd'
  when :dvd
    'dvddrive'
  end
  VirtualBox.run_command! ['VBoxManage', '--nologo', 'storageattach',
      vm.uid, '--storagectl', io_bus.name, '--port', port.to_s,
      '--device', device.to_s, '--type', media_arg, '--medium', file]
  self
end

#dropVirtualBox::Vm::Disk

Removes the image file backing this disk.

The method name is drop, as in “DROP TABLE”. It doesn’t remove the disk from any VM, it just removes the file.

Returns:



101
102
103
104
# File 'lib/virtual_box/vm/disk.rb', line 101

def drop
  File.unlink @file if File.exist?(@file)
  self
end

#to_hashObject

Creates a new image descriptor based on the given attributes.

Parameters:

  • Hash (Symbol, Object)

    options ActiveRecord-style initial values for attributes; can be used together with Disk#to_hash to save and restore



68
69
70
# File 'lib/virtual_box/vm/disk.rb', line 68

def to_hash
  { :file => file, :format => format, :media => media }
end