Class: VirtualBox::Vm::Disk
- Inherits:
-
Object
- Object
- VirtualBox::Vm::Disk
- Defined in:
- lib/virtual_box/vm/disk.rb
Overview
Descriptor for a VirtualBox hard-disk or DVD image.
Instance Attribute Summary collapse
-
#file ⇒ String
Path to the file storing this disk image.
-
#format ⇒ Symbol
The format of this disk image.
-
#media ⇒ Symbol
The type of media that the image represents.
Class Method Summary collapse
-
.create(options) ⇒ VirtualBox::Vm::Disk
Creates a VirtualBox disk image.
-
.guess_image_format(image_file) ⇒ Object
Disk image format based on the extension in the file name.
-
.guess_media_type(image_file) ⇒ Object
Disk media type on the extension in the file name.
Instance Method Summary collapse
-
#add_to(vm, io_bus, port, device) ⇒ VirtualBox::Vm::Disk
Attaches this disk to a virtual machine.
-
#drop ⇒ VirtualBox::Vm::Disk
Removes the image file backing this disk.
-
#initialize(options) ⇒ Disk
constructor
Creates an image descriptor with the given attributes.
-
#to_hash ⇒ Object
Creates a new image descriptor based on the given attributes.
Constructor Details
#initialize(options) ⇒ Disk
Creates an image descriptor with the given attributes.
38 39 40 41 |
# File 'lib/virtual_box/vm/disk.rb', line 38 def initialize() .each { |k, v| self.send :"#{k}=", v } self.file = File. file end |
Instance Attribute Details
#file ⇒ String
Path to the file storing this disk image.
10 11 12 |
# File 'lib/virtual_box/vm/disk.rb', line 10 def file @file end |
#format ⇒ Symbol
The format of this disk image.
The recognized formats are :raw, :vdi, :vmdk, and :vhd.
16 17 18 |
# File 'lib/virtual_box/vm/disk.rb', line 16 def format @format end |
#media ⇒ Symbol
The type of media that the image represents.
The recognized types are :disk and :dvd.
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.
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/virtual_box/vm/disk.rb', line 83 def self.create() path = [:file] format = [:format] || guess_image_format(path) size_mb = ([:size] / (1024 * 1024)).to_i memo = [:memo] || 'Created with the virtual_box RubyGem' variant = [: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.
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 |
#drop ⇒ VirtualBox::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.
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_hash ⇒ Object
Creates a new image descriptor based on the given attributes.
68 69 70 |
# File 'lib/virtual_box/vm/disk.rb', line 68 def to_hash { :file => file, :format => format, :media => media } end |