boxcutter / centos

Virtual machine templates for CentOS written in legacy JSON
Apache License 2.0
424 stars 199 forks source link

Wrong interface in Centos72 box gets the vagrant dhcp configuration #52

Closed isharacomix closed 7 years ago

isharacomix commented 7 years ago

This may be a configuration problem on my end, but whenever I try to vagrant up a boxcutter/centos72 box with more than one interface, I get this output:

==> server01: Importing base box 'boxcutter/centos72'...
==> server01: Matching MAC address for NAT networking...
==> server01: Checking if box 'boxcutter/centos72' is up to date...
==> server01: Setting the name of the VM: 1_server01
==> server01: Fixed port collision for 22 => 2222. Now on port 2201.
==> server01: Clearing any previously set network interfaces...
==> server01: Preparing network interfaces based on configuration...
    server01: Adapter 1: nat
    server01: Adapter 2: intnet
    server01: Adapter 3: intnet
    server01: Adapter 4: intnet
==> server01: Forwarding ports...
    server01: 22 (guest) => 2201 (host) (adapter 1)
==> server01: Running 'pre-boot' VM customizations...
==> server01: Booting VM...
==> server01: Waiting for machine to boot. This may take a few minutes...
    server01: SSH address: 127.0.0.1:2201
    server01: SSH username: vagrant
    server01: SSH auth method: private key
    server01: Warning: Remote connection disconnect. Retrying...
    server01: Warning: Remote connection disconnect. Retrying...
    server01: Warning: Remote connection disconnect. Retrying...
Timed out while waiting for the machine to boot. This means that
Vagrant was unable to communicate with the guest machine within
the configured ("config.vm.boot_timeout" value) time period.

If you look above, you should be able to see the error(s) that
Vagrant had when attempting to connect to the machine. These errors
are usually good hints as to what may be wrong.

If you're using a custom box, make sure that networking is properly
working and you're able to connect to the machine. It is a common
problem that networking isn't setup properly in these boxes.
Verify that authentication configurations are also setup properly,
as well.

If the box appears to be booting properly, you may want to increase
the timeout ("config.vm.boot_timeout") value.

My vagrant config is as below:

config.vm.define "server01" do |device|
    device.vm.hostname = "server01"
    device.vm.box = "boxcutter/centos72"

    device.vm.provider "virtualbox" do |v|
      v.name = "#{wbid}_server01"
      v.customize ["modifyvm", :id, '--audiocontroller', 'AC97', '--audio', 'Null']
      v.memory = 512
    end
    device.vm.synced_folder ".", "/vagrant", disabled: true

      # NETWORK INTERFACES
      device.vm.network "private_network", virtualbox__intnet: "#{wbid}_net42", auto_config: false , :mac => "a00000000031"

      device.vm.network "private_network", virtualbox__intnet: "#{wbid}_net2", auto_config: false , :mac => "443839000003"

      device.vm.network "private_network", virtualbox__intnet: "#{wbid}_net13", auto_config: false , :mac => "443839000017"

    device.vm.provider "virtualbox" do |vbox|
      vbox.customize ['modifyvm', :id, '--nicpromisc2', 'allow-all']
      vbox.customize ['modifyvm', :id, '--nicpromisc3', 'allow-all']
      vbox.customize ['modifyvm', :id, '--nicpromisc4', 'allow-all']
   end
  end

When I go in through the console to investigate what's going on, it looks like DHCP is configured by default on the LAST interface Adapter 4 instead of the first Adapter 1 (the default Vagrant NAT interface). The only script in /etc/sysconfig/network-scripts is the device of Adapter 4, even though you can see it's explicitly set as auto-config=False. If I manually activate DHCP on Adapter 1 through the console, everything (such as Vagrant ssh) starts working as would expect.

Does anyone here have any insight?

isharacomix commented 7 years ago

Found the culprit. It's this line: https://github.com/boxcutter/centos/blob/master/script/cleanup.sh#L43

gwdev=\nmcli dev | grep ethernet | egrep -v 'unmanaged' | head -n 1 | awk '{print \$1}'`

nmcli dev lists the devices in alphabetical order. These are my adapters and their names:

enp0s10 comes before enp0s3 alphabetically, and the script assumes that the correct interface will be listed first. In our scripts, we usually use the output of ip link show and assume that the interface numbered 2: is the correct interface to use for dhcp (interface 1 is the loopback).

ip link show | grep '2: ' | awk '{print substr($2, 1, length($2)-1)}'

Would that be appropriate?

misheska commented 7 years ago

Should be sorted in the new boxes.