Class: VagrantZFS::VBoxManage

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

Class Method Summary collapse

Class Method Details

.add_disk(uuid, hddfile) ⇒ Object



79
80
81
# File 'lib/vagrant_zfs/vboxmanage.rb', line 79

def self.add_disk uuid, hddfile
  self.exec "storageattach #{uuid} --storagectl 'SATA Controller' --port 0 --type hdd --nonrotational on --medium #{hddfile}"
end

.add_nat(uuid) ⇒ Object



25
26
27
# File 'lib/vagrant_zfs/vboxmanage.rb', line 25

def self.add_nat uuid
  self.modifyvm  uuid, "--nic1 nat"
end

.add_storagectl(uuid) ⇒ Object



75
76
77
# File 'lib/vagrant_zfs/vboxmanage.rb', line 75

def self.add_storagectl uuid
  self.exec "storagectl #{uuid} --name 'SATA Controller' --add sata --controller IntelAHCI --sataportcount 4 --hostiocache on  --bootable on"
end

.cpus(uuid, cpus) ⇒ Object



29
30
31
# File 'lib/vagrant_zfs/vboxmanage.rb', line 29

def self.cpus uuid, cpus
  self.modifyvm  uuid, "--cpus #{cpus}"
end

.createvm(instance_name, instance_root) ⇒ Object



15
16
17
18
19
# File 'lib/vagrant_zfs/vboxmanage.rb', line 15

def self.createvm instance_name, instance_root
  cmd = "createvm --name #{instance_name} --register --basefolder #{instance_root}"
  lines = self.exec(cmd).split(/\n/)
  uuid = lines.grep(/^UUID:\s*([-0-9a-z]+)/){$1}.first
end

.exec(cmd) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/vagrant_zfs/vboxmanage.rb', line 4

def self.exec cmd
  cmd = "VBoxManage " + cmd
  stdout, stderr, status = Open3.capture3(cmd)

  if status.success? and stderr.empty?
    return stdout
  else
    raise Exception, "VBoxManage Error: #{cmd}\n #{stdout}\n #{stderr}\n #{status}"
  end
end

.gethduuid(file) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/vagrant_zfs/vboxmanage.rb', line 37

def self.gethduuid file
  # I found the line number may be 23, but search 40 just to be sure.
  cmd="head -n 40 #{file}|grep --text --byte-offset --max-count=1 ddb.uuid.image="
  stdout, stderr, status = Open3.capture3(cmd)
  unless status.success? and stderr.empty?
    raise Exception, "gethduuid Error: #{cmd}\n #{stdout}\n #{stderr}\n #{status}"
  end
  # UUID format is a string of hyphen-punctuated character groups of 8-4-4-4-12.
  uuid = stdout.match(/ddb.uuid.image="(\w{8}-\w{4}-\w{4}-\w{4}-\w{12})"/).captures.first
end

.memory(uuid, memory) ⇒ Object



33
34
35
# File 'lib/vagrant_zfs/vboxmanage.rb', line 33

def self.memory uuid, memory
  self.modifyvm uuid, "--memory #{memory}"
end

.modifyvm(uuid, arg) ⇒ Object



21
22
23
# File 'lib/vagrant_zfs/vboxmanage.rb', line 21

def self.modifyvm uuid, arg
  self.exec "modifyvm #{uuid} #{arg}"
end

.sethduuid(file) ⇒ Object



71
72
73
# File 'lib/vagrant_zfs/vboxmanage.rb', line 71

def self.sethduuid file
  self.exec "internalcommands sethduuid #{file}"
end

.sethduuid_shell(file) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/vagrant_zfs/vboxmanage.rb', line 48

def self.sethduuid_shell file
  # I found the line number may be 23, but search 40 just to be sure.
  cmd="head -n 40 #{file}|grep --text --byte-offset --max-count=1 ddb.uuid.image="
  stdout, stderr, status = Open3.capture3(cmd)
  unless status.success? and stderr.empty?
    raise Exception, "gethduuid Error: #{cmd}\n #{stdout}\n #{stderr}\n #{status}"
  end
  # The number before : is the byte offset number.
  uuid_offset = stdout.split(':').first.to_i + 'ddb.uuid.image="'.length
  uuid_length = 36

  # Generate a new UUID
  stdout, stderr, status = Open3.capture3("uuidgen")
  unless status.success? and stderr.empty?
    raise Exception, "gethduuid Error: #{cmd}\n #{stdout}\n #{stderr}\n #{status}"
  end
  new_uuid = stdout.chomp

  # Edit the vmdk-file inplace.
  cmd = "echo #{new_uuid} | dd of=#{file} seek=#{uuid_offset} bs=1 count=#{uuid_length} conv=notrunc"
  `#{cmd}` ? new_uuid : nil
end

.setup(uuid, hddfile) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/vagrant_zfs/vboxmanage.rb', line 83

def self.setup uuid, hddfile
  uuid_pre  = self.gethduuid(hddfile)
  self.sethduuid hddfile
  uuid_post = self.gethduuid(hddfile)

  # Did self.sethduuid work?
  unless uuid_pre != uuid_post
    puts "VBoxManage internalcommands sethduuid did not work. Trying other method with dd"
    self.sethduuid_shell hddfile
    uuid_post = self.gethduuid(hddfile)
    unless uuid_pre != uuid_post
      puts "Error: Did not succeed to sethduuid."
    end
  end

  self.add_storagectl uuid
  self.add_disk       uuid, hddfile
  self.add_nat        uuid
  self.cpus           uuid, "1"
  self.memory         uuid, "512"
end