Class: Vhd::Library
- Inherits:
-
Object
- Object
- Vhd::Library
- Defined in:
- lib/vhd/library.rb
Constant Summary collapse
- VALID_TYPES =
[:fixed]
Instance Attribute Summary collapse
-
#footer ⇒ Object
readonly
Returns the value of attribute footer.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Instance Method Summary collapse
- #checksum ⇒ Object
- #create ⇒ Object
- #create_fixed_disk ⇒ Object
- #generate_footer(options = {}) ⇒ Object
- #geometry ⇒ Object
-
#initialize(options = {}) ⇒ Library
constructor
A new instance of Library.
- #size_in_bytes ⇒ Object
- #size_in_hex ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Library
Returns a new instance of Library.
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/vhd/library.rb', line 6 def initialize(={}) raise "Invalid vhd type" unless VALID_TYPES.include?([:type]) raise "Name is required" unless [:name] raise "Size is required" unless [:size] @name = [:name] = {} @size = [:size] self. end |
Instance Attribute Details
#footer ⇒ Object (readonly)
Returns the value of attribute footer.
4 5 6 |
# File 'lib/vhd/library.rb', line 4 def end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
4 5 6 |
# File 'lib/vhd/library.rb', line 4 def name @name end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
4 5 6 |
# File 'lib/vhd/library.rb', line 4 def size @size end |
Instance Method Details
#checksum ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/vhd/library.rb', line 85 def checksum checksum = 0 .each do |k,v| next if k == :checksum checksum += v.codepoints.inject(0) { |r,c| r += c } end [:checksum] = ["%08x" % ((~checksum) & 0xFFFFFFFF)].pack("H*") end |
#create ⇒ Object
17 18 19 |
# File 'lib/vhd/library.rb', line 17 def create File.open(@name, "wb") { |f| f.print .values.join } end |
#create_fixed_disk ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/vhd/library.rb', line 21 def create_fixed_disk File.open(@name, "wb") do |f| f.truncate(size_in_bytes + 512) f.seek(size_in_bytes) f.write(.values.join) end end |
#generate_footer(options = {}) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/vhd/library.rb', line 29 def (={}) [:cookie] = "conectix".force_encoding("BINARY") [:features] = ["00000002"].pack("H*") [:ff] = ["00010000"].pack("H*") [:offset] = ["FFFFFFFFFFFFFFFF"].pack("H*") [:time] = [(Time.now - Time.parse("Jan 1, 2000 12:00:00 AM GMT")).to_i.to_s(16)].pack("H*") [:creator_app] = "rvhd".force_encoding("UTF-8") [:creator_ver] = ["00060002"].pack("H*") [:creator_host] = "Wi2k".force_encoding("UTF-8") [:orig_size] = size_in_hex [:curr_size] = size_in_hex [:geometry] = nil [:disk_type] = ["00000002"].pack("H*") [:checksum] = nil [:uuid] = SecureRandom.hex.scan(/../).map { |c| c.hex.chr.force_encoding("BINARY") }.join [:state] = ["0"].pack("H*") [:reserved] = Array("00"*427).pack("H*") self.geometry self.checksum end |
#geometry ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/vhd/library.rb', line 61 def geometry max_size = 65535 * 16 * 255 * 512 capacity = size_in_bytes > max_size ? max_size : size_in_bytes total_sectors = capacity / 512 if total_sectors > (65535 * 16 * 63) sectors_per_track = 255 heads_per_cylinder = 16 else sectors_per_track = 17 cylinders_times_heads = total_sectors / sectors_per_track heads_per_cylinder = (cylinders_times_heads + 1023) / 1024 heads_per_cylinder = 4 if heads_per_cylinder < 4 cylinders_times_heads >= ((heads_per_cylinder * 1024) or (heads_per_cylinder > 16)) sectors_per_track = 63 heads_per_cylinder = 16 end cylinders = (total_sectors / sectors_per_track) / heads_per_cylinder [:geometry] = [cylinders, heads_per_cylinder, sectors_per_track].pack("nCC") end |
#size_in_bytes ⇒ Object
51 52 53 |
# File 'lib/vhd/library.rb', line 51 def size_in_bytes (((@size * 1024) * 1024) * 1024).to_i end |
#size_in_hex ⇒ Object
55 56 57 58 59 |
# File 'lib/vhd/library.rb', line 55 def size_in_hex hex_size = size_in_bytes.to_s(16) hex_size = "0" + hex_size until hex_size.length == 16 [hex_size].pack("H*") end |