Skip to content

Commit 0f9733b

Browse files
committed
ssh : use forwarded port for ssh
Use localhost instead of guest IP Guerying guest IP takes time
1 parent 1cc55eb commit 0f9733b

File tree

6 files changed

+34
-14
lines changed

6 files changed

+34
-14
lines changed

lib/vagrant_utm/driver/base.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ def import(utm_file_url); end
103103
# @return [void]
104104
def set_name(name); end # rubocop:disable Naming/AccessorMethodName
105105

106+
# Reads the SSH port of this VM.
107+
#
108+
# @param [Integer] expected Expected guest port of SSH.
109+
def ssh_port(expected); end
110+
106111
# Starts the virtual machine referenced by this driver.
107112
# @return [void]
108113
def start; end

lib/vagrant_utm/driver/meta.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ def initialize(uuid = nil) # rubocop:disable Metrics/CyclomaticComplexity,Metric
105105
:read_state,
106106
:restore_snapshot,
107107
:set_name,
108+
:ssh_port,
108109
:start,
109110
:start_disposable,
110111
:suspend,

lib/vagrant_utm/driver/version_4_5.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,17 @@ def read_network_interfaces
191191
nics
192192
end
193193

194+
def ssh_port(expected_port)
195+
@logger.debug("Searching for SSH port: #{expected_port.inspect}")
196+
197+
# Look for the forwarded port only by comparing the guest port
198+
read_forwarded_ports.each do |_, _, hostport, guestport|
199+
return hostport if guestport == expected_port
200+
end
201+
202+
nil
203+
end
204+
194205
# virtualbox plugin style
195206
def read_state
196207
output = execute("status", @uuid)

lib/vagrant_utm/provider.rb

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,24 +78,25 @@ def machine_id_changed
7878

7979
# Returns the SSH info for accessing the UTM VM.
8080
def ssh_info
81-
# If the VM is not running (utm, started) that we can't possibly SSH into it
81+
# If the VM is not running (utm, started) then we can't possibly SSH into it
8282
# TODO: We should use the state 'running', rather than 'started'
83-
# This is a workaround for the UTM provider, which does not expose 'running' state
83+
# UTM provider, which does not expose 'running' state. So we use 'started' state
8484
return nil if state.id != :started
8585

86-
# If there is port forwarding, GuestPort 22 to HostPort XXXX,
87-
# We can use host as "127.0.0.1" and port as XXXX (forwarded ports)
88-
# But we keep it simple for now, and just use the IP address of the VM
89-
#
9086
# Return what we know
91-
# host = get the IP address of the VM from first IP of utmctl ip-address
92-
# port = 22, the default SSH port (Since we use IP address of VM, we don't need to forward ports)
87+
# host = IP address of the VM
88+
# port = the SSH port
9389
# username = vagrant, the default vagrant user
9490
# private_key_path = get the private key of the VM (default ~/.vagrant.d/insecure_private_key)
9591

92+
# Return ssh info for connector to connect to the VM
93+
# If VM has shared network adapter in UTM, then we can use the IP address of the VM
94+
# If we have multiple network adapters, we need to pick the right one, read_guest_ip returns just first IP
95+
# Also, since Vagrant by default adds port forwarding for ssh port 22,
96+
# we might aswell use the forwarded ports to connect to the VM using the localhost.
9697
{
97-
host: @driver.read_guest_ip,
98-
port: "22"
98+
host: "127.0.0.1",
99+
port: @driver.ssh_port(@machine.config.ssh.guest_port)
99100
}
100101
end
101102

locales/en.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ en:
3434
snapshot_multiple_vm_files: |-
3535
Multiple VM files detected. vagrant-utm which uses qemu-img does not support
3636
snapshot feature when multiple VM files are present.
37-
37+
3838
Directory checked: %{directory}
3939
Files found: %{files}
4040
snapshot_vm_file_not_found: |-
@@ -65,7 +65,8 @@ en:
6565
setting_id: |-
6666
Setting the Vagrant machine ID to UTM VM UUID: %{id}
6767
waiting_for_vm: |-
68-
Waiting %{time} secs for the UTM virtual machine to be ready...
68+
UTM does not report when the VM is ready, it only reports when the VM is started.
69+
So, waiting %{time} secs for the UTM virtual machine to be ready...
6970
Default wait time is 20 seconds, you can change it by setting `config.vm.provider.utm.wait_time` in Vagrantfile.
7071
7172
commands:

vagrantfile_examples/Vagrantfile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
Vagrant.configure("2") do |config|
77
# hostname inside the VM
88
config.vm.hostname = "utm"
9+
config.vm.network "forwarded_port", guest: 80, host: 8989
910
config.vm.provider :utm do |utm|
1011
# Name in UTM UI
11-
utm.name = "vagrant"
12+
utm.name = "debian"
1213
# UTM VM file
13-
utm.utm_file_url = "https://github.com/naveenrajm7/utm-box/releases/download/debian-12.6/debian-12.6.0-arm64-netinst-vagrant.zip"
14+
utm.utm_file_url = "http://localhost:8000/vm_utm.zip"
1415
utm.memory = 512
1516
utm.cpus = 1
1617
utm.notes = "Vagrant: For testing plugin development"

0 commit comments

Comments
 (0)