vagrant-vz 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +142 -0
- data/lib/vagrant-vz/action/boot.rb +46 -0
- data/lib/vagrant-vz/action/destroy.rb +21 -0
- data/lib/vagrant-vz/action/force_halt.rb +22 -0
- data/lib/vagrant-vz/action/import.rb +67 -0
- data/lib/vagrant-vz/action/resize_disk.rb +34 -0
- data/lib/vagrant-vz/action/wait_for_ip.rb +45 -0
- data/lib/vagrant-vz/action.rb +156 -0
- data/lib/vagrant-vz/config.rb +81 -0
- data/lib/vagrant-vz/dhcp_leases.rb +71 -0
- data/lib/vagrant-vz/disk_image.rb +94 -0
- data/lib/vagrant-vz/driver.rb +332 -0
- data/lib/vagrant-vz/errors.rb +39 -0
- data/lib/vagrant-vz/plugin.rb +42 -0
- data/lib/vagrant-vz/provider.rb +63 -0
- data/lib/vagrant-vz/synced_folder.rb +50 -0
- data/lib/vagrant-vz/version.rb +7 -0
- data/lib/vagrant-vz/vfkit_command.rb +70 -0
- data/lib/vagrant-vz.rb +24 -0
- data/locales/en.yml +64 -0
- metadata +60 -0
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module VagrantPlugins
|
|
4
|
+
module VZ
|
|
5
|
+
# Candidate IP addresses for a guest, looked up by MAC address. Guests on
|
|
6
|
+
# vfkit's NAT network get their address from macOS's built-in DHCP server
|
|
7
|
+
# (bootpd), which records leases in /var/db/dhcpd_leases; the ARP table
|
|
8
|
+
# covers guests whose lease is recorded under a DUID instead of the MAC.
|
|
9
|
+
module DhcpLeases
|
|
10
|
+
DEFAULT_PATH = "/var/db/dhcpd_leases"
|
|
11
|
+
|
|
12
|
+
module_function
|
|
13
|
+
|
|
14
|
+
# bootpd writes octets without leading zeros ("52:54:0:a:b:c"), so both
|
|
15
|
+
# sides are normalized to that form before comparing.
|
|
16
|
+
def normalize_mac(mac)
|
|
17
|
+
mac.to_s.strip.downcase.split(":").map { |octet| octet.to_i(16).to_s(16) }.join(":")
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Parses the bootpd lease file into an array of hashes.
|
|
21
|
+
def parse(text)
|
|
22
|
+
leases = []
|
|
23
|
+
current = nil
|
|
24
|
+
text.each_line do |line|
|
|
25
|
+
line = line.strip
|
|
26
|
+
if line == "{"
|
|
27
|
+
current = {}
|
|
28
|
+
elsif line == "}"
|
|
29
|
+
leases << current if current
|
|
30
|
+
current = nil
|
|
31
|
+
elsif current && (idx = line.index("="))
|
|
32
|
+
current[line[0...idx]] = line[(idx + 1)..]
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
leases
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# All addresses leased to `mac`, newest lease first. Clients that
|
|
39
|
+
# identify with a DUID (systemd-networkd, for one) are recorded under
|
|
40
|
+
# the DUID instead, so the ARP table has to cover those.
|
|
41
|
+
def find_ips(mac, path: DEFAULT_PATH)
|
|
42
|
+
return [] unless File.readable?(path)
|
|
43
|
+
|
|
44
|
+
find_ips_in_leases(mac, File.read(path))
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def find_ips_in_leases(mac, text)
|
|
48
|
+
want = normalize_mac(mac)
|
|
49
|
+
parse(text)
|
|
50
|
+
.select do |lease|
|
|
51
|
+
hw = lease["hw_address"].to_s.sub(/\A\d+,/, "")
|
|
52
|
+
!hw.empty? && normalize_mac(hw) == want
|
|
53
|
+
end
|
|
54
|
+
.sort_by { |lease| -lease["lease"].to_s.to_i(16) }
|
|
55
|
+
.filter_map { |lease| lease["ip_address"] }
|
|
56
|
+
.uniq
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Parses `arp -an` output, e.g.
|
|
60
|
+
# ? (192.168.64.3) at 52:54:0:12:34:56 on bridge100 ifscope [bridge]
|
|
61
|
+
# Incomplete entries have no MAC and never match.
|
|
62
|
+
def find_ips_in_arp(mac, arp_output)
|
|
63
|
+
want = normalize_mac(mac)
|
|
64
|
+
arp_output.to_s.each_line.filter_map do |line|
|
|
65
|
+
m = line.match(/\((\d+\.\d+\.\d+\.\d+)\) at (\h{1,2}(?::\h{1,2}){5})/)
|
|
66
|
+
m[1] if m && normalize_mac(m[2]) == want
|
|
67
|
+
end.uniq
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "fileutils"
|
|
4
|
+
require "open3"
|
|
5
|
+
require "pathname"
|
|
6
|
+
|
|
7
|
+
module VagrantPlugins
|
|
8
|
+
module VZ
|
|
9
|
+
# Box disk discovery and conversion. Virtualization.framework boots raw
|
|
10
|
+
# images, so anything else (qcow2 from libvirt boxes, vmdk, ...) is
|
|
11
|
+
# converted with qemu-img. Raw images are cloned with APFS copy-on-write,
|
|
12
|
+
# which is instant and takes no extra space until the guest writes.
|
|
13
|
+
module DiskImage
|
|
14
|
+
class Error < StandardError; end
|
|
15
|
+
|
|
16
|
+
SIGNATURES = [
|
|
17
|
+
[0, "QFI\xFB".b, "qcow2"],
|
|
18
|
+
[0, "KDMV".b, "vmdk"],
|
|
19
|
+
[0, "vhdxfile".b, "vhdx"],
|
|
20
|
+
[0, "conectix".b, "vpc"],
|
|
21
|
+
[64, "\x7F\x10\xDA\xBE".b, "vdi"]
|
|
22
|
+
].freeze
|
|
23
|
+
|
|
24
|
+
module_function
|
|
25
|
+
|
|
26
|
+
# Disks listed in a box's metadata.json, falling back to the
|
|
27
|
+
# conventional single-disk names. The first disk is the boot disk.
|
|
28
|
+
def box_disks(box_dir, metadata)
|
|
29
|
+
box_dir = Pathname.new(box_dir)
|
|
30
|
+
listed = Array(metadata && metadata["disks"]).filter_map do |disk|
|
|
31
|
+
path = disk.is_a?(Hash) ? disk["path"] : disk
|
|
32
|
+
box_dir.join(path) if path
|
|
33
|
+
end
|
|
34
|
+
return listed unless listed.empty?
|
|
35
|
+
|
|
36
|
+
%w[box.img disk.img].map { |name| box_dir.join(name) }.select(&:file?).first(1)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def detect_format(path)
|
|
40
|
+
header = File.binread(path.to_s, 128) || "".b
|
|
41
|
+
SIGNATURES.each do |offset, magic, format|
|
|
42
|
+
return format if header.byteslice(offset, magic.bytesize) == magic
|
|
43
|
+
end
|
|
44
|
+
"raw"
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def import(src, dest, qemu_img: "qemu-img")
|
|
48
|
+
src = src.to_s
|
|
49
|
+
dest = dest.to_s
|
|
50
|
+
raise Error, "box disk not found: #{src}" unless File.file?(src)
|
|
51
|
+
|
|
52
|
+
FileUtils.rm_f(dest)
|
|
53
|
+
format = detect_format(src)
|
|
54
|
+
if format == "raw"
|
|
55
|
+
clone(src, dest)
|
|
56
|
+
else
|
|
57
|
+
run!(qemu_img, "convert", "-f", format, "-O", "raw", src, dest)
|
|
58
|
+
end
|
|
59
|
+
format
|
|
60
|
+
rescue Error
|
|
61
|
+
FileUtils.rm_f(dest)
|
|
62
|
+
raise
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# `cp -c` asks for an APFS clone; plain copy is the fallback for
|
|
66
|
+
# volumes that cannot clone (e.g. box and project on different disks).
|
|
67
|
+
def clone(src, dest)
|
|
68
|
+
_out, _err, status = Open3.capture3("/bin/cp", "-c", src, dest)
|
|
69
|
+
FileUtils.cp(src, dest) unless status.success?
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# Grows a raw image to `gib` GiB. Returns :grown, :unchanged or
|
|
73
|
+
# :smaller (the image is never shrunk).
|
|
74
|
+
def grow(path, gib)
|
|
75
|
+
wanted = gib * 1024**3
|
|
76
|
+
current = File.size(path.to_s)
|
|
77
|
+
return :unchanged if wanted == current
|
|
78
|
+
return :smaller if wanted < current
|
|
79
|
+
|
|
80
|
+
File.truncate(path.to_s, wanted)
|
|
81
|
+
:grown
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def run!(*cmd)
|
|
85
|
+
out, err, status = Open3.capture3(*cmd)
|
|
86
|
+
return out if status.success?
|
|
87
|
+
|
|
88
|
+
raise Error, "#{cmd.join(" ")} failed: #{err.strip.empty? ? out.strip : err.strip}"
|
|
89
|
+
rescue Errno::ENOENT
|
|
90
|
+
raise Error, "#{cmd.first} not found"
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "fileutils"
|
|
4
|
+
require "json"
|
|
5
|
+
require "log4r"
|
|
6
|
+
require "socket"
|
|
7
|
+
require "timeout"
|
|
8
|
+
require "tmpdir"
|
|
9
|
+
require "digest"
|
|
10
|
+
|
|
11
|
+
require_relative "dhcp_leases"
|
|
12
|
+
require_relative "vfkit_command"
|
|
13
|
+
|
|
14
|
+
module VagrantPlugins
|
|
15
|
+
module VZ
|
|
16
|
+
# Runs one machine's vfkit process. Everything about the machine lives in
|
|
17
|
+
# its Vagrant data dir (.vagrant/machines/<name>/vz):
|
|
18
|
+
#
|
|
19
|
+
# disk0.img ... raw disks (disk0 boots)
|
|
20
|
+
# efi-variable-store EFI NVRAM, created by vfkit on first boot
|
|
21
|
+
# mac MAC address of the NAT interface
|
|
22
|
+
# ip guest address found for the current boot
|
|
23
|
+
# vfkit.pid pid of the running vfkit process
|
|
24
|
+
# vfkit.log vfkit's own output
|
|
25
|
+
# console.log guest serial console
|
|
26
|
+
#
|
|
27
|
+
# The REST socket lives in the per-user temp dir instead, because unix
|
|
28
|
+
# socket paths are limited to 104 bytes and project paths can be long.
|
|
29
|
+
class Driver
|
|
30
|
+
STATES = {
|
|
31
|
+
"VirtualMachineStateRunning" => :running,
|
|
32
|
+
"VirtualMachineStateStopped" => :stopped,
|
|
33
|
+
"VirtualMachineStatePaused" => :paused,
|
|
34
|
+
"VirtualMachineStateError" => :error,
|
|
35
|
+
"VirtualMachineStateStarting" => :starting,
|
|
36
|
+
"VirtualMachineStatePausing" => :pausing,
|
|
37
|
+
"VirtualMachineStateResuming" => :resuming,
|
|
38
|
+
"VirtualMachineStateStopping" => :stopping
|
|
39
|
+
}.freeze
|
|
40
|
+
|
|
41
|
+
START_TIMEOUT = 30
|
|
42
|
+
STOP_TIMEOUT = 15
|
|
43
|
+
STATE_TTL = 1.0
|
|
44
|
+
|
|
45
|
+
# macOS Local Network privacy stops non-Apple binaries, Vagrant's own
|
|
46
|
+
# Ruby included, from connecting to guests on the vmnet bridge
|
|
47
|
+
# (EHOSTUNREACH). Apple's nc is exempt, so it carries SSH connections
|
|
48
|
+
# and the reachability probes.
|
|
49
|
+
NC = "/usr/bin/nc"
|
|
50
|
+
SSH_PROXY_COMMAND = "#{NC} %h %p"
|
|
51
|
+
|
|
52
|
+
attr_reader :data_dir
|
|
53
|
+
|
|
54
|
+
def initialize(machine)
|
|
55
|
+
@machine = machine
|
|
56
|
+
@data_dir = Pathname.new(machine.data_dir)
|
|
57
|
+
@logger = Log4r::Logger.new("vagrant::vz::driver")
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def self.generate_mac
|
|
61
|
+
bytes = Array.new(6) { rand(256) }
|
|
62
|
+
bytes[0] = (bytes[0] & 0xFC) | 0x02 # locally administered, unicast
|
|
63
|
+
bytes.map { |b| format("%02x", b) }.join(":")
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# -- paths ---------------------------------------------------------------
|
|
67
|
+
|
|
68
|
+
def disk_path(index) = @data_dir.join("disk#{index}.img")
|
|
69
|
+
def efi_store_path = @data_dir.join("efi-variable-store")
|
|
70
|
+
def mac_path = @data_dir.join("mac")
|
|
71
|
+
def ip_path = @data_dir.join("ip")
|
|
72
|
+
def pid_path = @data_dir.join("vfkit.pid")
|
|
73
|
+
def log_path = @data_dir.join("vfkit.log")
|
|
74
|
+
def console_log_path = @data_dir.join("console.log")
|
|
75
|
+
|
|
76
|
+
def disks
|
|
77
|
+
Dir.glob(@data_dir.join("disk*.img").to_s).sort_by { |p| p[/disk(\d+)\.img\z/, 1].to_i }
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def socket_path
|
|
81
|
+
name = "vagrant-vz-#{Digest::SHA1.hexdigest(@data_dir.to_s)[0, 12]}.sock"
|
|
82
|
+
path = File.join(Dir.tmpdir, name)
|
|
83
|
+
path.bytesize < 100 ? path : File.join("/tmp", name)
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def mac_address
|
|
87
|
+
mac_path.file? ? mac_path.read.strip : nil
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# -- state ---------------------------------------------------------------
|
|
91
|
+
|
|
92
|
+
def created?
|
|
93
|
+
disk_path(0).file?
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def state
|
|
97
|
+
return :not_created unless created?
|
|
98
|
+
|
|
99
|
+
pid = running_pid
|
|
100
|
+
unless pid
|
|
101
|
+
cleanup_runtime_files
|
|
102
|
+
return :stopped
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
STATES.fetch(rest_state, :running)
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def running?
|
|
109
|
+
state == :running
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# The guest's address for this boot. Found once by probing candidates
|
|
113
|
+
# (see #discover_ip) and cached until the VM stops.
|
|
114
|
+
def ip_address(port: 22)
|
|
115
|
+
return ip_path.read.strip if ip_path.file?
|
|
116
|
+
|
|
117
|
+
discover_ip(port: port)
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# Several addresses can point at the same MAC: an early lease from the
|
|
121
|
+
# firmware or initramfs, then the one the booted OS asked for. The live
|
|
122
|
+
# one is whichever answers on the SSH port. ARP entries come first since
|
|
123
|
+
# they reflect current traffic.
|
|
124
|
+
def discover_ip(port: 22)
|
|
125
|
+
mac = mac_address
|
|
126
|
+
return nil unless mac
|
|
127
|
+
|
|
128
|
+
candidates = (DhcpLeases.find_ips_in_arp(mac, arp_table) + DhcpLeases.find_ips(mac)).uniq
|
|
129
|
+
ip = candidates.find { |candidate| port_open?(candidate, port) }
|
|
130
|
+
ip_path.write(ip) if ip
|
|
131
|
+
ip
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
# -- lifecycle -----------------------------------------------------------
|
|
135
|
+
|
|
136
|
+
def start(config, shares: [])
|
|
137
|
+
cmd = VfkitCommand.build(
|
|
138
|
+
vfkit: config.vfkit_path,
|
|
139
|
+
cpus: config.cpus,
|
|
140
|
+
memory: config.memory,
|
|
141
|
+
efi_store: efi_store_path,
|
|
142
|
+
disks: disks,
|
|
143
|
+
mac: mac_address,
|
|
144
|
+
rest_socket: socket_path,
|
|
145
|
+
console_log: console_log_path,
|
|
146
|
+
shares: shares,
|
|
147
|
+
rosetta: config.rosetta || nil,
|
|
148
|
+
gui: config.gui,
|
|
149
|
+
extra_args: config.extra_args
|
|
150
|
+
)
|
|
151
|
+
@logger.info("Starting vfkit: #{cmd.inspect}")
|
|
152
|
+
|
|
153
|
+
FileUtils.rm_f(socket_path)
|
|
154
|
+
FileUtils.rm_f(ip_path)
|
|
155
|
+
log = File.open(log_path, "a")
|
|
156
|
+
log.puts("==> #{Time.now} #{cmd.join(" ")}")
|
|
157
|
+
log.flush
|
|
158
|
+
# A separate process group keeps vfkit alive after Vagrant exits and
|
|
159
|
+
# out of reach of a Ctrl-C aimed at Vagrant.
|
|
160
|
+
pid = Process.spawn(*cmd, in: File::NULL, out: log, err: log,
|
|
161
|
+
pgroup: true, chdir: @data_dir.to_s)
|
|
162
|
+
Process.detach(pid)
|
|
163
|
+
log.close
|
|
164
|
+
pid_path.write(pid.to_s)
|
|
165
|
+
|
|
166
|
+
wait_until_started(pid)
|
|
167
|
+
pid
|
|
168
|
+
rescue Errno::ENOENT
|
|
169
|
+
raise Errors::VfkitNotFound, path: config.vfkit_path
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
# Asks Virtualization.framework to stop the VM: "Stop" sends the guest a
|
|
173
|
+
# power-button request, "HardStop" pulls the plug. Falls back to signals
|
|
174
|
+
# if the REST endpoint is unreachable or vfkit does not exit.
|
|
175
|
+
def stop(force: false)
|
|
176
|
+
pid = running_pid
|
|
177
|
+
return unless pid
|
|
178
|
+
|
|
179
|
+
@cached_state = nil
|
|
180
|
+
begin
|
|
181
|
+
rest_request("POST", "/vm/state", { state: force ? "HardStop" : "Stop" })
|
|
182
|
+
rescue StandardError => e
|
|
183
|
+
@logger.warn("REST stop failed (#{e.message}); signalling vfkit")
|
|
184
|
+
signal(pid, "TERM")
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
unless wait_for_exit(pid, STOP_TIMEOUT)
|
|
188
|
+
signal(pid, "TERM")
|
|
189
|
+
signal(pid, "KILL") unless wait_for_exit(pid, 5)
|
|
190
|
+
end
|
|
191
|
+
ensure
|
|
192
|
+
cleanup_runtime_files
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
def destroy
|
|
196
|
+
stop(force: true)
|
|
197
|
+
disks.each { |d| FileUtils.rm_f(d) }
|
|
198
|
+
[efi_store_path, mac_path, ip_path, log_path, console_log_path].each { |p| FileUtils.rm_f(p) }
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
def log_tail(lines = 20)
|
|
202
|
+
return "" unless log_path.file?
|
|
203
|
+
|
|
204
|
+
log_path.readlines.last(lines).join
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
private
|
|
208
|
+
|
|
209
|
+
def wait_until_started(pid)
|
|
210
|
+
deadline = Time.now + START_TIMEOUT
|
|
211
|
+
loop do
|
|
212
|
+
unless alive?(pid)
|
|
213
|
+
cleanup_runtime_files
|
|
214
|
+
raise Errors::VfkitStartFailed, log: log_tail
|
|
215
|
+
end
|
|
216
|
+
return if rest_state == "VirtualMachineStateRunning"
|
|
217
|
+
break if Time.now > deadline
|
|
218
|
+
|
|
219
|
+
sleep 0.5
|
|
220
|
+
end
|
|
221
|
+
raise Errors::VfkitStartFailed, log: "vfkit did not report a running VM within " \
|
|
222
|
+
"#{START_TIMEOUT}s\n#{log_tail}"
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
def wait_for_exit(pid, timeout)
|
|
226
|
+
deadline = Time.now + timeout
|
|
227
|
+
while Time.now < deadline
|
|
228
|
+
return true unless alive?(pid)
|
|
229
|
+
|
|
230
|
+
sleep 0.5
|
|
231
|
+
end
|
|
232
|
+
!alive?(pid)
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
def running_pid
|
|
236
|
+
return nil unless pid_path.file?
|
|
237
|
+
|
|
238
|
+
pid = pid_path.read.to_i
|
|
239
|
+
return nil unless pid.positive? && alive?(pid) && vfkit_process?(pid)
|
|
240
|
+
|
|
241
|
+
pid
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
def alive?(pid)
|
|
245
|
+
Process.kill(0, pid)
|
|
246
|
+
true
|
|
247
|
+
rescue Errno::ESRCH
|
|
248
|
+
false
|
|
249
|
+
rescue Errno::EPERM
|
|
250
|
+
true
|
|
251
|
+
end
|
|
252
|
+
|
|
253
|
+
# Guards against a stale pid file pointing at a reused pid.
|
|
254
|
+
def vfkit_process?(pid)
|
|
255
|
+
comm = IO.popen(["/bin/ps", "-p", pid.to_s, "-o", "comm="], err: File::NULL, &:read).to_s
|
|
256
|
+
comm.include?("vfkit")
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
def signal(pid, sig)
|
|
260
|
+
Process.kill(sig, pid)
|
|
261
|
+
rescue Errno::ESRCH
|
|
262
|
+
nil
|
|
263
|
+
end
|
|
264
|
+
|
|
265
|
+
def cleanup_runtime_files
|
|
266
|
+
FileUtils.rm_f(pid_path)
|
|
267
|
+
FileUtils.rm_f(socket_path)
|
|
268
|
+
FileUtils.rm_f(ip_path)
|
|
269
|
+
@cached_state = nil
|
|
270
|
+
end
|
|
271
|
+
|
|
272
|
+
def port_open?(ip, port)
|
|
273
|
+
system(NC, "-z", "-G", "2", ip, port.to_s, out: File::NULL, err: File::NULL)
|
|
274
|
+
end
|
|
275
|
+
|
|
276
|
+
def arp_table
|
|
277
|
+
IO.popen(["/usr/sbin/arp", "-an"], err: File::NULL, &:read)
|
|
278
|
+
rescue StandardError
|
|
279
|
+
""
|
|
280
|
+
end
|
|
281
|
+
|
|
282
|
+
# Vagrant asks for the state many times a second while it waits for
|
|
283
|
+
# SSH, so a reply is reused for STATE_TTL seconds.
|
|
284
|
+
def rest_state
|
|
285
|
+
if @cached_state && Time.now - @cached_state[1] < STATE_TTL
|
|
286
|
+
return @cached_state[0]
|
|
287
|
+
end
|
|
288
|
+
|
|
289
|
+
body = rest_request("GET", "/vm/state")
|
|
290
|
+
state = JSON.parse(body)["state"]
|
|
291
|
+
@cached_state = [state, Time.now]
|
|
292
|
+
state
|
|
293
|
+
rescue StandardError => e
|
|
294
|
+
@logger.debug("REST state query failed: #{e.message}")
|
|
295
|
+
nil
|
|
296
|
+
end
|
|
297
|
+
|
|
298
|
+
# Minimal HTTP/1.1 over vfkit's unix socket.
|
|
299
|
+
def rest_request(method, path, body = nil)
|
|
300
|
+
payload = body ? JSON.dump(body) : ""
|
|
301
|
+
response = Timeout.timeout(5) do
|
|
302
|
+
UNIXSocket.open(socket_path) do |sock|
|
|
303
|
+
sock.write("#{method} #{path} HTTP/1.1\r\nHost: localhost\r\n" \
|
|
304
|
+
"Content-Type: application/json\r\nContent-Length: #{payload.bytesize}\r\n" \
|
|
305
|
+
"Connection: close\r\n\r\n#{payload}")
|
|
306
|
+
sock.read
|
|
307
|
+
end
|
|
308
|
+
end
|
|
309
|
+
|
|
310
|
+
head, resp_body = response.split("\r\n\r\n", 2)
|
|
311
|
+
status = head.to_s[%r{\AHTTP/1\.\d (\d{3})}, 1].to_i
|
|
312
|
+
resp_body = dechunk(resp_body.to_s) if head =~ /^transfer-encoding:\s*chunked/i
|
|
313
|
+
raise "vfkit returned HTTP #{status}: #{resp_body}" unless (200..299).cover?(status)
|
|
314
|
+
|
|
315
|
+
resp_body
|
|
316
|
+
end
|
|
317
|
+
|
|
318
|
+
def dechunk(body)
|
|
319
|
+
out = +""
|
|
320
|
+
until body.empty?
|
|
321
|
+
size_line, body = body.split("\r\n", 2)
|
|
322
|
+
size = size_line.to_i(16)
|
|
323
|
+
break if size.zero? || body.nil?
|
|
324
|
+
|
|
325
|
+
out << body.byteslice(0, size)
|
|
326
|
+
body = body.byteslice(size + 2..) || ""
|
|
327
|
+
end
|
|
328
|
+
out
|
|
329
|
+
end
|
|
330
|
+
end
|
|
331
|
+
end
|
|
332
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module VagrantPlugins
|
|
4
|
+
module VZ
|
|
5
|
+
module Errors
|
|
6
|
+
class VZError < Vagrant::Errors::VagrantError
|
|
7
|
+
error_namespace("vagrant_vz.errors")
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
class UnsupportedHost < VZError
|
|
11
|
+
error_key(:unsupported_host)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
class VfkitNotFound < VZError
|
|
15
|
+
error_key(:vfkit_not_found)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
class VfkitStartFailed < VZError
|
|
19
|
+
error_key(:vfkit_start_failed)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
class QemuImgNotFound < VZError
|
|
23
|
+
error_key(:qemu_img_not_found)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
class BoxDiskNotFound < VZError
|
|
27
|
+
error_key(:box_disk_not_found)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
class DiskImportFailed < VZError
|
|
31
|
+
error_key(:disk_import_failed)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
class IPNotFound < VZError
|
|
35
|
+
error_key(:ip_not_found)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module VagrantPlugins
|
|
4
|
+
module VZ
|
|
5
|
+
class Plugin < Vagrant.plugin("2")
|
|
6
|
+
name "vz"
|
|
7
|
+
description <<~DESC
|
|
8
|
+
Provider that runs machines with Apple's Virtualization.framework
|
|
9
|
+
through the vfkit command-line hypervisor.
|
|
10
|
+
DESC
|
|
11
|
+
|
|
12
|
+
config(:vz, :provider) do
|
|
13
|
+
require_relative "config"
|
|
14
|
+
Config
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# Native "vz" boxes are preferred; "libvirt" boxes are accepted too and
|
|
18
|
+
# their qcow2 disks are converted to raw images on first `vagrant up`.
|
|
19
|
+
provider(:vz, box_format: %w[vz libvirt], priority: 5) do
|
|
20
|
+
setup_i18n
|
|
21
|
+
require_relative "provider"
|
|
22
|
+
Provider
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Priority 8 makes virtiofs the default folder type for vz machines,
|
|
26
|
+
# ahead of smb (7), rsync (5) and nfs (5). It is only usable with :vz.
|
|
27
|
+
synced_folder(:virtiofs, 8) do
|
|
28
|
+
setup_i18n
|
|
29
|
+
require_relative "synced_folder"
|
|
30
|
+
SyncedFolder
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def self.setup_i18n
|
|
34
|
+
return if @i18n_loaded
|
|
35
|
+
|
|
36
|
+
I18n.load_path << File.expand_path("locales/en.yml", VZ.source_root)
|
|
37
|
+
I18n.reload!
|
|
38
|
+
@i18n_loaded = true
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "log4r"
|
|
4
|
+
|
|
5
|
+
require_relative "driver"
|
|
6
|
+
|
|
7
|
+
module VagrantPlugins
|
|
8
|
+
module VZ
|
|
9
|
+
class Provider < Vagrant.plugin("2", :provider)
|
|
10
|
+
def self.usable?(raise_error = false)
|
|
11
|
+
return true if RbConfig::CONFIG["host_os"].include?("darwin")
|
|
12
|
+
raise Errors::UnsupportedHost if raise_error
|
|
13
|
+
|
|
14
|
+
false
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def self.installed?
|
|
18
|
+
usable?
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def initialize(machine)
|
|
22
|
+
@machine = machine
|
|
23
|
+
@logger = Log4r::Logger.new("vagrant::vz::provider")
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def action(name)
|
|
27
|
+
method = "action_#{name}"
|
|
28
|
+
Action.send(method) if Action.respond_to?(method)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def driver
|
|
32
|
+
@driver ||= Driver.new(@machine)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def machine_id_changed
|
|
36
|
+
@driver = nil
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def ssh_info
|
|
40
|
+
return nil unless state.id == :running
|
|
41
|
+
|
|
42
|
+
port = @machine.config.ssh.guest_port || 22
|
|
43
|
+
ip = driver.ip_address(port: port)
|
|
44
|
+
return nil unless ip
|
|
45
|
+
|
|
46
|
+
# config.ssh.proxy_command, when set, still takes precedence.
|
|
47
|
+
{ host: ip, port: port, proxy_command: Driver::SSH_PROXY_COMMAND }
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def state
|
|
51
|
+
id = @machine.id.nil? ? :not_created : driver.state
|
|
52
|
+
short = I18n.t("vagrant_vz.states.short_#{id}", default: id.to_s.tr("_", " "))
|
|
53
|
+
long = I18n.t("vagrant_vz.states.long_#{id}", default: "")
|
|
54
|
+
Vagrant::MachineState.new(id, short, long)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def to_s
|
|
58
|
+
id = @machine.id.nil? ? "new" : @machine.id
|
|
59
|
+
"VZ (#{id})"
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "shellwords"
|
|
4
|
+
|
|
5
|
+
require_relative "vfkit_command"
|
|
6
|
+
|
|
7
|
+
module VagrantPlugins
|
|
8
|
+
module VZ
|
|
9
|
+
# Shares host directories over virtio-fs. vfkit attaches the devices when
|
|
10
|
+
# the VM boots (see Action::Boot); this class only mounts them in the
|
|
11
|
+
# guest once SSH is up. Needs a guest kernel with virtiofs (Linux 5.4+).
|
|
12
|
+
class SyncedFolder < Vagrant.plugin("2", :synced_folder)
|
|
13
|
+
def usable?(machine, raise_error = false)
|
|
14
|
+
return true if machine.provider_name == :vz
|
|
15
|
+
raise Vagrant::Errors::SyncedFolderUnusable, type: "virtiofs" if raise_error
|
|
16
|
+
|
|
17
|
+
false
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def prepare(_machine, _folders, _opts); end
|
|
21
|
+
|
|
22
|
+
def enable(machine, folders, _opts)
|
|
23
|
+
machine.ui.info(I18n.t("vagrant_vz.mounting_folders"))
|
|
24
|
+
folders.each do |id, data|
|
|
25
|
+
tag = VfkitCommand.mount_tag(id)
|
|
26
|
+
guestpath = data[:guestpath]
|
|
27
|
+
options = Array(data[:mount_options])
|
|
28
|
+
option_flag = options.empty? ? "" : " -o #{options.join(",").shellescape}"
|
|
29
|
+
|
|
30
|
+
machine.ui.detail("#{data[:hostpath]} => #{guestpath}")
|
|
31
|
+
machine.communicate.sudo(
|
|
32
|
+
"mkdir -p #{guestpath.shellescape} && " \
|
|
33
|
+
"(mountpoint -q #{guestpath.shellescape} || " \
|
|
34
|
+
"mount -t virtiofs#{option_flag} #{tag} #{guestpath.shellescape})"
|
|
35
|
+
)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def disable(machine, folders, _opts)
|
|
40
|
+
return unless machine.communicate.ready?
|
|
41
|
+
|
|
42
|
+
folders.each_value do |data|
|
|
43
|
+
machine.communicate.sudo("umount #{data[:guestpath].shellescape}", error_check: false)
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def cleanup(_machine, _opts); end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|