Class: VagrantPlugins::ProviderBhyve::Driver

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-bhyve/driver.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(machine) ⇒ Driver

Returns a new instance of Driver.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/vagrant-bhyve/driver.rb', line 16

def initialize(machine)
	@logger = Log4r::Logger.new("vagrant_bhyve::driver")
	@machine = machine
	@data_dir = @machine.data_dir
	@executor = Executor::Exec.new

	# if vagrant is excecuted by root (or with sudo) then the variable
	# will be empty string, otherwise it will be 'sudo' to make sure we
	# can run bhyve, bhyveload and pf with sudo privilege
	if Process.uid == 0
	  @sudo = ''
	else
	  @sudo = 'sudo'
	end
end

Instance Attribute Details

#executorObject

This executor is responsible for actually executing commands, including bhyve, dnsmasq and other shell utils used to get VM’s state



14
15
16
# File 'lib/vagrant-bhyve/driver.rb', line 14

def executor
  @executor
end

Instance Method Details

#boot(machine, ui) ⇒ Object



350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# File 'lib/vagrant-bhyve/driver.rb', line 350

def boot(machine, ui)
	firmware	= get_attr('firmware')
	loader		= get_attr('bootloader')
	directory	= @data_dir
	config		= machine.provider_config

	# Run in bhyve in background
	bhyve_cmd = "sudo -b"
	# Prevent virtual CPU use 100% of host CPU
	bhyve_cmd += " bhyve -HP"

	# Configure for hostbridge & lpc device, Windows need slot 0 and 31
	# while others don't care, so we use slot 0 and 31
	case config.hostbridge
	when 'amd'
	  bhyve_cmd += " -s 0,amd_hostbridge"
	when 'no'
	else
	  bhyve_cmd += " -s 0,hostbridge"
	end
	bhyve_cmd += " -s 31,lpc"

	# Generate ACPI tables for FreeBSD guest
	bhyve_cmd += " -A" if loader == 'bhyveload'

	# For UEFI, we need to point a UEFI firmware which should be 
	# included in the box.
	bhyve_cmd += " -l bootrom,#{directory.join('uefi.fd').to_s}" if firmware == "uefi"

	# TODO Enable graphics if the box is configed so

	uuid = get_attr('id')
	bhyve_cmd += " -U #{uuid}"

	# Allocate resources
	bhyve_cmd += " -c #{config.cpus}"
	bhyve_cmd += " -m #{config.memory}"

	# Disk(if any)
	bhyve_cmd += " -s 1:0,ahci-hd,#{directory.join("disk.img").to_s}"
	disk_id = 1
	config.disks.each do |disk|
	  if disk[:format] == "raw"
	    if disk[:path]
 path = disk[:path]
	    else
 path = directory.join(disk[:name].to_s).to_s + ".img"
	    end
	    execute(false, "truncate -s #{disk[:size]} #{path}")
	    bhyve_cmd += " -s 1:#{disk_id.to_s},ahci-hd,#{path.to_s}"
	  end
	  disk_id += 1
	end

	# CDROM(if any)
	cdrom_id = 0
	config.cdroms.each do |cdrom|
	  path = File.realpath(cdrom[:path])
	  bhyve_cmd += " -s 2:#{cdrom_id.to_s},ahci-cd,#{path.to_s}"
	  cdrom_id += 1
	end
	

	# Tap device
	tap_device  = get_attr('tap')
	mac_address = get_attr('mac')
	bhyve_cmd += " -s 3:0,virtio-net,#{tap_device},mac=#{mac_address}"

	# Console
	nmdm_num = find_available_nmdm
	@data_dir.join('nmdm_num').open('w') { |nmdm_file| nmdm_file.write nmdm_num }
	bhyve_cmd += " -l com1,/dev/nmdm#{nmdm_num}A"

	vm_name = get_attr('vm_name')
	bhyve_cmd += " #{vm_name} >/dev/null 2>&1"

	execute(false, bhyve_cmd)
	while state(vm_name) != :running
	  sleep 0.5
	end
end

#check_and_install(command, package, ui) ⇒ Object



624
625
626
627
628
629
630
# File 'lib/vagrant-bhyve/driver.rb', line 624

def check_and_install(command, package, ui)
	command_exist = execute(true, "which #{command}")
	if command_exist != 0
	  ui.warn "We need #{command} in #{package} package, installing with pkg..."
	  pkg_install(package)
	end
end

#check_bhyve_supportObject



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/vagrant-bhyve/driver.rb', line 123

def check_bhyve_support
	# Check whether FreeBSD version is lower than 10
	result = execute(true, "test $(uname -K) -lt 1000000")
	raise Errors::SystemVersionIsTooLow if result == 0

	# Check whether POPCNT is supported
	result = execute(false, "#{@sudo} grep -E '^[ ] +Features2' /var/run/dmesg.boot | tail -n 1")
	raise Errors::MissingPopcnt unless result =~ /POPCNT/

	# Check whether EPT is supported for Intel
	result = execute(false, "#{@sudo} grep -E '^[ ]+VT-x' /var/run/dmesg.boot | tail -n 1")
	raise Errors::MissingEpt unless result =~ /EPT/

	# Check VT-d 
	#result = execute(false, "#{@sudo} acpidump -t | grep DMAR")
	#raise Errors::MissingIommu if result.length == 0 
end

#cleanupObject



487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
# File 'lib/vagrant-bhyve/driver.rb', line 487

def cleanup
	bridge		= get_attr('bridge')
	tap		= get_attr('tap')
	vm_name		= get_attr('vm_name')
	id		= get_attr('id')
	mac		= get_attr('mac')
	directory	= @data_dir

	return unless bridge && tap
	# Destroy vmm device
	execute(false, "#{@sudo} bhyvectl --destroy --vm=#{vm_name} >/dev/null 2>&1") if state(vm_name) == :uncleaned

	# Clean instance-specific pf rules
	#execute(false, "#{@sudo} pfctl -a '/vagrant_#{id}' -F all")
	comment_mark_tap = "# vagrant-bhyve #{tap}"
	if execute(true, "grep \"#{comment_mark_tap}\" /etc/pf.conf") == 0
	  execute(false, "#{@sudo} sed -i '' '/#{comment_mark_tap}/,+1d' /etc/pf.conf")
	end
	# Destory tap interfaces
	execute(false, "#{@sudo} ifconfig #{tap} destroy") if execute(true, "ifconfig #{tap}") == 0
	execute(false, "#{@sudo} sed -i '' '/#{mac}/d' /var/run/dnsmasq.#{bridge}.leases") if execute(true, "grep \"#{mac}\" /var/run/dnsmasq.#{bridge}.leases") == 0

	# Delete configure files
	#FileUtils.rm directory.join('dnsmasq.conf').to_s if directory.join('dnsmasq.conf').exist?
	#FileUtils.rm directory.join('pf.conf').to_s if directory.join('pf.conf').exist?

	# Clean nat configurations if there is no VMS is using the bridge
	member_num = 3
	bridge_exist = execute(true, "ifconfig #{bridge}")
	member_num = execute(false, "ifconfig #{bridge} | grep -c 'member' || true") if bridge_exist == 0

	if bridge_exist != 0 || member_num.to_i < 2
	  #execute(false, "#{@sudo} pfctl -a '/vagrant_#{bridge}' -F all")
	  comment_mark_bridge = "# vagrant-bhyve #{bridge}"
	  if execute(true, "grep \"#{comment_mark_bridge}\" /etc/pf.conf") == 0
	    execute(false, "#{@sudo} sed -i '' '/#{comment_mark_bridge}/,+1d' /etc/pf.conf")
	  end
	  restart_service('pf')
	  #if directory.join('pf_disabled').exist?
	  #  FileUtils.rm directory.join('pf_disabled')
	  #  execute(false, "#{@sudo} pfctl -d")
	  #end
	  execute(false, "#{@sudo} ifconfig #{bridge} destroy") if bridge_exist == 0
	  pf_conf = "/usr/local/etc/pf.#{bridge}.conf"
	  execute(false, "#{@sudo} rm #{pf_conf}") if execute(true, "test -e #{pf_conf}") == 0
	  if execute(true, "test -e /var/run/dnsmasq.#{bridge}.pid") == 0
	    dnsmasq_cmd = "dnsmasq -C /usr/local/etc/dnsmasq.#{bridge}.conf -l /var/run/dnsmasq.#{bridge}.leases -x /var/run/dnsmasq.#{bridge}.pid"
	    dnsmasq_conf    = "/var/run/dnsmasq.#{bridge}.leases"
	    dnsmasq_leases  = "/var/run/dnsmasq.#{bridge}.pid"
	    dnsmasq_pid     = "/usr/local/etc/dnsmasq.#{bridge}.conf"
	    execute(false, "#{@sudo} kill -9 $(pgrep -fx \"#{dnsmasq_cmd}\")")
	    execute(false, "#{@sudo} rm #{dnsmasq_leases}") if execute(true, "test -e #{dnsmasq_leases}") == 0
	    execute(false, "#{@sudo} rm #{dnsmasq_pid}") if execute(true, "test -e #{dnsmasq_pid}") == 0
	    execute(false, "#{@sudo} rm #{dnsmasq_conf}") if execute(true, "test -e #{dnsmasq_conf}") == 0
	  end
	end
end

#create_network_device(device_name, device_type) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/vagrant-bhyve/driver.rb', line 149

def create_network_device(device_name, device_type)
	return if device_name.length == 0

	# Check whether the bridge has been created
	interface_name = get_interface_name(device_name)
	interface_name = execute(false, "#{@sudo} ifconfig #{device_type} create") if interface_name.length == 0
	raise Errors::UnableToCreateInterface if interface_name.length == 0
	# Add new created device's description
	execute(false, "#{@sudo} ifconfig #{interface_name} description #{device_name} up")

	# Store the new created network device's name
	store_attr(device_type, interface_name)

	# Configure tap device
	if device_type == 'tap'
	  # Add the tap device as bridge's member
	  bridge = get_attr('bridge')
	  # Make sure the tap deivce has the same mtu value
	  # with the bridge
	  mtu = execute(false, "ifconfig #{bridge} | head -n1 | awk '{print $NF}'")
	  execute(false, "#{@sudo} ifconfig #{interface_name} mtu #{mtu}") if mtu.length != 0 and mtu != '1500'
	  execute(false, "#{@sudo} ifconfig #{bridge} addm #{interface_name}")
	  # Setup VM-specific pf rules
	  id		= get_attr('id')
	  pf_conf	= @data_dir.join('pf.conf')
	  pf_conf.open('w') do |f|
	    f.puts "set skip on #{interface_name}" 
	  end
	  comment_mark = "# vagrant-bhyve #{interface_name}"
	  if execute(true, "test -s /etc/pf.conf") == 0
	    if execute(true, "grep \"#{comment_mark}\" /etc/pf.conf") != 0
 comment_mark_bridge = "# vagrant-bhyve #{bridge}"
 if execute(true, "grep \"#{comment_mark_bridge}\" /etc/pf.conf") != 0
		execute(false, "#{@sudo} sed -i '' '1i\\\n#{comment_mark}\n' /etc/pf.conf")
		execute(false, "#{@sudo} sed -i '' '2i\\\ninclude \"#{pf_conf.to_s}\"\n' /etc/pf.conf")
 else
		bridge_line = execute(false, "grep -A 1 \"#{comment_mark_bridge}\" /etc/pf.conf | tail -1")
		bridge_line = bridge_line.gsub("\"", "\\\"")
		bridge_line = bridge_line.gsub("/", "\\/")
		execute(false, "#{@sudo} sed -i '' '/#{bridge_line}/a\\\n#{comment_mark}\n' /etc/pf.conf")
		execute(false, "#{@sudo} sed -i '' '/#{comment_mark}/a\\\ninclude \"#{pf_conf.to_s}\"\n' /etc/pf.conf")
 end
	    end
	  else
	    execute(false, "echo \"#{comment_mark}\" | #{@sudo} tee -a /etc/pf.conf")
	    execute(false, "echo \"include \\\"#{pf_conf.to_s}\\\"\" | #{@sudo} tee -a /etc/pf.conf")
	  end
	  restart_service('pf')
	  #execute(false, "#{@sudo} pfctl -a '/vagrant_#{id}' -f #{pf_conf.to_s}") 
	  #if !pf_enabled?
	  #  execute(false, "#{@sudo} pfctl -e")
	  #end
	end
end

#destroyObject



119
120
121
# File 'lib/vagrant-bhyve/driver.rb', line 119

def destroy
	FileUtils.rm_rf(Dir.glob(@data_dir.join('*').to_s))
end

#enable_nat(bridge, ui) ⇒ Object

For now, only IPv4 is supported



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/vagrant-bhyve/driver.rb', line 205

def enable_nat(bridge, ui)
	bridge_name 	= get_interface_name(bridge)
	return if execute(true, "ifconfig #{bridge_name} | grep inet") == 0

	directory	= @data_dir
	# Choose a subnet for this bridge
	index = bridge_name =~ /\d/
	bridge_num = bridge_name[index..-1]
	sub_net = "172.16." + bridge_num

	# Config IP for the bridge
	execute(false, "#{@sudo} ifconfig #{bridge_name} #{sub_net}.1/24")

	# Get default gateway
	gateway = execute(false, "netstat -4rn | grep default | awk '{print $4}'")
	store_attr('gateway', gateway)
	# Add gateway as a bridge member
	#execute(false, "#{@sudo} ifconfig #{bridge_name} addm #{gateway}")

	# Enable forwarding
	execute(false, "#{@sudo} sysctl net.inet.ip.forwarding=1 >/dev/null 2>&1")
	execute(false, "#{@sudo} sysctl net.inet6.ip6.forwarding=1 >/dev/null 2>&1")

	# Change pf's configuration
	pf_conf = directory.join("pf.conf")
	pf_conf.open("w") do |pf_file|
	  pf_file.puts "set skip on #{bridge_name}"
	  pf_file.puts "nat on #{gateway} from {#{sub_net}.0/24} to any -> (#{gateway})"
	end
	pf_bridge_conf = "/usr/local/etc/pf.#{bridge_name}.conf"
	comment_mark = "# vagrant-bhyve #{bridge_name}"
	execute(false, "#{@sudo} mv #{pf_conf.to_s} #{pf_bridge_conf}")
	if execute(true, "test -s /etc/pf.conf") == 0
	  if execute(true, "grep \"#{comment_mark}\" /etc/pf.conf") != 0
	    execute(false, "#{@sudo} sed -i '' '1i\\\n#{comment_mark}\n' /etc/pf.conf")
	    execute(false, "#{@sudo} sed -i '' '2i\\\ninclude \"#{pf_bridge_conf}\"\n' /etc/pf.conf")
	  end
	else
	  execute(false, "echo \"#{comment_mark}\" | #{@sudo} tee -a /etc/pf.conf")
	  execute(false, "echo \"include \\\"#{pf_bridge_conf}\\\"\" | #{@sudo} tee -a /etc/pf.conf")
	end
	restart_service('pf')
	# Use pfctl to enable pf rules
	#execute(false, "#{@sudo} cp #{pf_conf.to_s} /usr/local/etc/pf.#{bridge_name}.conf")
	#execute(false, "#{@sudo} pfctl -a '/vagrant_#{bridge_name}' -f /usr/local/etc/pf.#{bridge_name}.conf")
	# execute(false, "#{@sudo} pfctl -a '/vagrant_#{bridge_name}' -sr")

	# Create a basic dnsmasq setting
	# Basic settings
	check_and_install('dnsmasq', 'dnsmasq', ui)
	dnsmasq_conf = directory.join("dnsmasq.conf")
	dnsmasq_conf.open("w") do |dnsmasq_file|
	  dnsmasq_file.puts <<-EOF
	  domain-needed
	  except-interface=lo0
	  bind-interfaces
	  local-service
	  dhcp-authoritative
	  EOF
	  # DHCP part
	  dnsmasq_file.puts "interface=#{bridge_name}"
	  dnsmasq_file.puts "dhcp-range=#{sub_net + ".10," + sub_net + ".254"}"
	  dnsmasq_file.puts "dhcp-option=option:dns-server,#{sub_net + ".1"}"
	end
	execute(false, "#{@sudo} cp #{dnsmasq_conf.to_s} /usr/local/etc/dnsmasq.#{bridge_name}.conf")
	dnsmasq_cmd = "dnsmasq -C /usr/local/etc/dnsmasq.#{bridge_name}.conf -l /var/run/dnsmasq.#{bridge_name}.leases -x /var/run/dnsmasq.#{bridge_name}.pid"
	execute(false, "#{@sudo} #{dnsmasq_cmd}")

end

#execute(*cmd, **opts, &block) ⇒ Object



558
559
560
# File 'lib/vagrant-bhyve/driver.rb', line 558

def execute(*cmd, **opts, &block)
	@executor.execute(*cmd, **opts, &block)
end

#find_available_nmdmObject



597
598
599
600
601
602
603
604
605
# File 'lib/vagrant-bhyve/driver.rb', line 597

def find_available_nmdm
	nmdm_num = 0
	while true
	  result = execute(true, "ls -l /dev/ | grep 'nmdm#{nmdm_num}A'")
	  break if result != 0
	  nmdm_num += 1
	end
	nmdm_num
end

#forward_port(forward_information, tap_device) ⇒ Object



460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
# File 'lib/vagrant-bhyve/driver.rb', line 460

def forward_port(forward_information, tap_device)
	id		= get_attr('id')
	ip_address	= get_ip_address(tap_device)
	pf_conf 	= @data_dir.join('pf.conf')
	rule 		= "rdr on #{forward_information[:adapter]} proto {udp, tcp} from any to any port #{forward_information[:host_port]} -> #{ip_address} port #{forward_information[:guest_port]}"

	pf_conf.open('a') do |pf_file|
	  pf_file.puts rule
	end
	# Update pf rules
	comment_mark = "# vagrant-bhyve #{tap_device}"
	if execute(true, "test -s /etc/pf.conf") == 0
	  if execute(true, "grep \"#{comment_mark}\" /etc/pf.conf") != 0
	    execute(false, "#{@sudo} sed -i '' '1i\\\n#{comment_mark}\n' /etc/pf.conf")
	    execute(false, "#{@sudo} sed -i '' '2i\\\ninclude \"#{pf_conf.to_s}\"\n' /etc/pf.conf")
	  end
	else
	  execute(false, "echo \"#{comment_mark}\" | #{@sudo} tee -a /etc/pf.conf")
	  execute(false, "echo \"include \\\"#{pf_conf.to_s}\\\"\" | #{@sudo} tee -a /etc/pf.conf")
	end
	restart_service('pf')
	#execute(false, "#{@sudo} pfctl -a '/vagrant_#{id}' -f #{pf_conf.to_s}")
	#execute(false, "#{@sudo} pfctl -a '/vagrant_#{id}' -sr")
	#execute(false, "#{@sudo} pfctl -a vagrant_#{id} -F all")

end

#get_attr(attr) ⇒ Object



607
608
609
610
611
612
613
614
# File 'lib/vagrant-bhyve/driver.rb', line 607

def get_attr(attr)
	name_file = @data_dir.join(attr)
	if File.exist?(name_file)
	  name_file.open('r') { |f| f.readline }
	else
	  nil
	end
end

#get_interface_name(device_name) ⇒ Object

Get the interface name for a bridge(like ‘bridge0’)



570
571
572
573
574
# File 'lib/vagrant-bhyve/driver.rb', line 570

def get_interface_name(device_name)
	desc = device_name + '\$'
	cmd = "ifconfig -a | grep -B 1 #{desc} | head -n1 | awk -F: '{print $1}'"
	result = execute(false, cmd)
end

#get_ip_address(interface_name, type = :guest) ⇒ Object



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/vagrant-bhyve/driver.rb', line 275

def get_ip_address(interface_name, type=:guest)
	bridge_name = get_attr('bridge')
	if type == :guest
	  return nil if execute(true, "test -e /var/run/dnsmasq.#{bridge_name}.pid") != 0
	  mac         = get_attr('mac')
	  leases_file = Pathname.new("/var/run/dnsmasq.#{bridge_name}.leases")
	  leases_info = leases_file.open('r'){|f| f.readlines}.select{|line| line.match(mac)}
	  raise Errors::NotFoundLeasesInfo if leases_info == []
	  # IP address for a device is on third coloum
	  ip = leases_info[0].split[2]
	elsif type == :host
	  return nil if execute(true, "ifconfig #{bridge_name}")
	  ip = execute(false, "ifconfig #{bridge_name} | grep -i inet").split[1]
	end
end

#get_mac_address(vm_name) ⇒ Object



562
563
564
565
566
567
# File 'lib/vagrant-bhyve/driver.rb', line 562

def get_mac_address(vm_name)
	# Generate a mac address for this tap device from its vm_name
	# IEEE Standards OUI for bhyve
	mac = "58:9c:fc:0"
	mac += Digest::MD5.hexdigest(vm_name).scan(/../).select.with_index{ |_, i| i.even? }[0..2].join(':')[1..-1]
end

#grub_bhyve_execute(command, password, member) ⇒ Object



632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
# File 'lib/vagrant-bhyve/driver.rb', line 632

def grub_bhyve_execute(command, password, member)
	vm_name	= get_attr('vm_name')
	exp = RubyExpect::Expect.spawn("sudo grub-bhyve -m #{@data_dir.join('device.map').to_s} -M 128M #{vm_name}")
	if password == ''
	  exp.procedure do
	    each do
 expect /grub> / do
		send command
 end
 expect /.*(grub> )$/ do
		send 'exit'
 end
	    end
	  end
	else 
	  exp.procedure do
	    each do
 expect /Password:/ do
		send password
 end
 expect /grub> / do
		send command
 end
 expect /.*(grub> )$/ do
		send 'exit'
 end
	    end
	  end
	end
	execute(false, "#{@sudo} bhyvectl --destroy --vm=#{vm_name}")
	case member
	when :match
	  return exp.match.to_s
	when :before
	  return exp.before.to_s
	when :last_match
	  return exp.last_match.to_s
	end
end

#import(machine, ui) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
# File 'lib/vagrant-bhyve/driver.rb', line 32

def import(machine, ui)
	box_dir		= machine.box.directory
	instance_dir	= @data_dir
	store_attr('id', machine.id)
	password = ''
	check_and_install('gcp', 'coreutils', ui)
	check_and_install('fdisk-linux', 'linuxfdisk', ui)
	execute(false, "gcp --sparse=always #{box_dir.join('disk.img').to_s} #{instance_dir.to_s}")
	if box_dir.join('uefi.fd').exist?
	  FileUtils.copy(box_dir.join('uefi.fd'), instance_dir) 
	  store_attr('firmware', 'uefi')
	else
	  store_attr('firmware', 'bios')
	  boot_partition = execute(false, "cd #{instance_dir.to_s} && fdisk-linux -lu disk.img | grep 'disk.img' | grep -E '\\*' | awk '{print $1}'")
	  if boot_partition == ''
	    store_attr('bootloader', 'bhyveload')
	  else
	    if execute(true, "sudo -n grub-bhyve --help") != 0
 ui.warn "We need to use your password to commmunicate with grub-bhyve, please make sure the password you input is correct."
 password = ui.ask("Password:", echo: false)
	    end
	    store_attr('bootloader', 'grub-bhyve')
	    # We need vmm module to be loaded to use grub-bhyve
	    load_module('vmm')
	    # Check whether grub-bhyve is installed
	    check_and_install('grub-bhyve', 'grub2-bhyve', ui)
	    instance_dir.join('device.map').open('w') do |f|
 f.puts "(hd0) #{instance_dir.join('disk.img').to_s}"
	    end
	    partition_index	= boot_partition =~ /\d/
	    partition_id	= boot_partition[partition_index..-1]
	    grub_run_partition	= "msdos#{partition_id}"
	    files		= grub_bhyve_execute("ls (hd0,#{grub_run_partition})/", password, :match)
	    if files =~ /grub2\//
 grub_run_dir	= "/grub2"
 store_attr('grub_run_partition', grub_run_partition)
 store_attr('grub_run_dir', grub_run_dir)
	    elsif files =~ /grub\//
 files		= grub_bhyve_execute("ls (hd0,#{grub_run_partition})/grub/", password, :match)
 if files =~ /grub\.conf/
		grub_conf 		= grub_bhyve_execute("cat (hd0,#{grub_run_partition})/grub/grub.conf", password, :before)
		info_index		= grub_conf =~ /title/
		boot_info		= grub_conf[info_index..-1]
		kernel_info_index	= boot_info =~ /kernel/
		initrd_info_index	= boot_info =~ /initrd/
		kernel_info		= boot_info[kernel_info_index..initrd_info_index - 1].gsub("\r\e[1B", "").gsub("kernel ", "linux (hd0,#{grub_run_partition})")
		initrd_info 		= boot_info[initrd_info_index..-1].gsub("\r\e[1B", "").gsub("initrd ", "initrd (hd0,#{grub_run_partition})")
		instance_dir.join('grub.cfg').open('w') do |f|
		  f.puts kernel_info
		  f.puts initrd_info
		  f.puts  "boot"
		end
 elsif files =~ /grub\.cfg/
		store_attr('grub_run_partition', grub_run_partition)
 end
	    else
 if files =~ /boot\//
		files = grub_bhyve_execute("ls (hd0,#{grub_run_partition})/boot/", password, :match)
		if files =~ /grub2/
		  grub_run_dir	= "/boot/grub2"
		  store_attr('grub_run_partition', grub_run_partition)
		  store_attr('grub_run_dir', grub_run_dir)
		elsif files =~ /grub/
		  files		= grub_bhyve_execute("ls (hd0,#{grub_run_partition})/boot/grub/", password, :match)
		  if files =~ /grub\.conf/
grub_conf 		= grub_bhyve_execute("cat (hd0,#{grub_run_partition})/boot/grub/grub.conf", password, :before)
info_index		= grub_conf =~ /title/
boot_info		= grub_conf[info_index..-1]
kernel_info_index	= boot_info =~ /kernel/
initrd_info_index	= boot_info =~ /initrd/
kernel_info		= boot_info[kernel_info_index..initrd_info_index - 1].gsub("\r\e[1B", "").gsub("kernel ","linux (hd0,#{grub_run_partition})/boot")
initrd_info 	= boot_info[initrd_info_index..-1].gsub("\r\e[1B", "").gsub("initrd ", "initrd (hd0,#{grub_run_partition})/boot")
instance_dir.join('grub.cfg').open('w') do |f|
  f.puts kernel_info
  f.puts initrd_info
  f.puts  "boot"
end
		  elsif files =~ /grub\.cfg/
store_attr('grub_run_partition', grub_run_partition)
		  end
		end
 end
	    end
	  end
	end
end

#ip_ready?Boolean

Returns:

  • (Boolean)


291
292
293
294
295
296
# File 'lib/vagrant-bhyve/driver.rb', line 291

def ip_ready?
	bridge_name = get_attr('bridge')
	mac         = get_attr('mac')
	leases_file = Pathname.new("/var/run/dnsmasq.#{bridge_name}.leases")
	return (leases_file.open('r'){|f| f.readlines}.select{|line| line.match(mac)} != [])
end

#load(machine, ui) ⇒ Object



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/vagrant-bhyve/driver.rb', line 305

def load(machine, ui)
	loader_cmd	= @sudo
	directory	= @data_dir
	config		= machine.provider_config
	loader		= get_attr('bootloader')
	case loader
	when 'bhyveload'
	  loader_cmd += ' bhyveload'
	  # Set autoboot, and memory and disk
	  loader_cmd += " -m #{config.memory}"
	  loader_cmd += " -d #{directory.join('disk.img').to_s}"
	  loader_cmd += " -e autoboot_delay=0"
	when 'grub-bhyve'
	  loader_cmd += " grub-bhyve"
	  loader_cmd += " -m #{directory.join('device.map').to_s}"
	  loader_cmd += " -M #{config.memory}"
	  # Maybe there should be some grub config in Vagrantfile, for now
	  # we just use this hd0,1 as default root and don't use -d -g 
	  # argument
	  grub_cfg		= directory.join('grub.cfg')
	  grub_run_partition	= get_attr('grub_run_partition')
	  grub_run_dir		= get_attr('grub_run_dir')
	  if grub_cfg.exist?
	    loader_cmd += " -r host -d #{directory.to_s}"
	  else
	    if grub_run_partition
 loader_cmd += " -r hd0,#{grub_run_partition}"
	    else
 loader_cmd += " -r hd0,1"
	    end

	    if grub_run_dir
 loader_cmd += " -d #{grub_run_dir}"
	    end
	    # Find an available nmdm device and add it as loader's -m argument
	    nmdm_num = find_available_nmdm
	    loader_cmd += " -c /dev/nmdm#{nmdm_num}A"
	  end
	end

	vm_name = get_attr('vm_name')
	loader_cmd += " #{vm_name}"
	execute(false, loader_cmd)
end

#load_module(module_name) ⇒ Object



141
142
143
144
145
146
147
# File 'lib/vagrant-bhyve/driver.rb', line 141

def load_module(module_name)
	result = execute(true, "#{@sudo} kldstat -qm #{module_name} >/dev/null 2>&1")
	if result != 0
	  result = execute(true, "#{@sudo} kldload #{module_name} >/dev/null 2>&1")
	  raise Errors::UnableToLoadModule if result != 0
	end
end

#pf_enabled?Boolean

Returns:

  • (Boolean)


587
588
589
590
591
592
593
594
595
# File 'lib/vagrant-bhyve/driver.rb', line 587

def pf_enabled?
	status = execute(true, "#{@sudo} pfctl -s all | grep -i disabled")
	if status == 0
	  store_attr('pf_disabled', 'yes')
	  false
	else
	  true
	end
end

#pkg_install(package) ⇒ Object



616
617
618
# File 'lib/vagrant-bhyve/driver.rb', line 616

def pkg_install(package)
	execute(false, "#{@sudo} ASSUME_ALWAYS_YES=yes pkg install #{package}")
end

#restart_service(service_name) ⇒ Object



576
577
578
579
580
581
582
583
584
585
# File 'lib/vagrant-bhyve/driver.rb', line 576

def restart_service(service_name)
	status = execute(true, "#{@sudo} pfctl -s all | grep -i disabled")
	if status == 0
	  cmd = "onerestart"
	else
	  cmd = "onestart"
	end
	status = execute(true, "#{@sudo} service #{service_name} #{cmd} >/dev/null 2>&1")
	raise Errors::RestartServiceFailed if status != 0
end

#shutdown(ui) ⇒ Object



432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
# File 'lib/vagrant-bhyve/driver.rb', line 432

def shutdown(ui)
	vm_name = get_attr('vm_name')
	if state(vm_name) == :not_running
	  ui.warn "You are trying to shutdown a VM which is not running"
	else
	  bhyve_pid = execute(false, "pgrep -fx 'bhyve: #{vm_name}'")
	  loader_pid = execute(false, "pgrep -fl 'grub-bhyve|bhyveload' | grep #{vm_name} | cut -d' ' -f1")
	  if bhyve_pid.length != 0
	    # We need to kill bhyve process twice and wait some time to make
	    # sure VM is shuted down.
	    while bhyve_pid.length != 0
 begin
		execute(false, "#{@sudo} kill -s TERM #{bhyve_pid}")
		sleep 1
		bhyve_pid = execute(false, "pgrep -fx 'bhyve: #{vm_name}'")
 rescue Errors::ExecuteError
		break
 end
	    end
	  elsif loader_pid.length != 0
	    ui.warn "Guest is going to be exit in bootloader stage"
	    execute(false, "#{@sudo} kill #{loader_pid}")
	  else
	    ui.warn "Unable to locate process id for #{vm_name}"
	  end
	end
end

#ssh_ready?(ssh_info) ⇒ Boolean

Returns:

  • (Boolean)


298
299
300
301
302
303
# File 'lib/vagrant-bhyve/driver.rb', line 298

def ssh_ready?(ssh_info)
	if ssh_info
	  return execute(true, "nc -z #{ssh_info[:host]} #{ssh_info[:port]}") == 0
	end
	return false
end

#state(vm_name) ⇒ Object



545
546
547
548
549
550
551
552
553
554
555
556
# File 'lib/vagrant-bhyve/driver.rb', line 545

def state(vm_name)
	vmm_exist = execute(true, "test -e /dev/vmm/#{vm_name}") == 0
	if vmm_exist
	  if execute(true, "pgrep -fx \"bhyve: #{vm_name}\"") == 0
	    :running
	  else
	    :uncleaned
	  end
	else
	  :stopped
	end
end

#store_attr(name, value) ⇒ Object



620
621
622
# File 'lib/vagrant-bhyve/driver.rb', line 620

def store_attr(name, value)
	@data_dir.join(name).open('w') { |f| f.write value }
end