Skip to content
This repository has been archived by the owner on Nov 24, 2022. It is now read-only.

network.ipv4 does not work for debian squeeze #106

Closed
paneq opened this issue Jul 5, 2013 · 5 comments
Closed

network.ipv4 does not work for debian squeeze #106

paneq opened this issue Jul 5, 2013 · 5 comments

Comments

@paneq
Copy link

paneq commented Jul 5, 2013

This is related to #31 which however is a very big ticket so I wanted to create a smaller one. Feel free to close it if you already know about this.

  • According to my tests this does not work yet:
 config.vm.network :private_network, ip: "10.0.3.137"
  • This works for me on Ubuntu Quantal but not for Debian Squeeze container:
config.vm.provider :lxc do |lxc|
  lxc.customize "network.ipv4", "10.0.3.137"
end

Host is Ubuntu 12.04.

@fgrehm
Copy link
Owner

fgrehm commented Jul 5, 2013

@paneq that is pretty weird, I'll try to reproduce it on a VBox VM and will get back to you. tks for reporting

@paneq
Copy link
Author

paneq commented Jul 6, 2013

Here is my workaround that I put into Vagrantfile. Definitely not a perfect solution but seems to be working with Debian Squeeze:

  require 'vagrant-lxc/provider'
  require 'vagrant-lxc/action'
  module Vagrant
    module LXC
      class Provider
        def ssh_info
          # If the Container is not created then we cannot possibly SSH into it, so
          # we return nil.
          return nil if state == :not_created

          ip = @machine.config.vm.networks.map do |type, options|
            type == :private_network && options[:ip] || nil
          end.compact.first || @driver.assigned_ip
          {
            :host => ip,
            :port => @machine.config.ssh.guest_port
          }
        end
      end
      module Action
        class << self
          alias_method :original_action_boot, :action_boot
          def action_boot
            action_builder = original_action_boot
            action_builder.insert_before(Vagrant::LXC::Action::Boot, Vagrant::Action::Builder.new.tap{|b| b.use Vagrant::LXC::Action::Magic } )
            action_builder
          end
        end
        class Magic
          def initialize(app, env)
            @app = app
          end
          def call(env)
            @env = env
            env[:machine].config.vm.networks.each do |type, options|
              next unless type == :private_network && options[:ip]
              try ||= 0
              try  += 1
              inter = Pathname.new(@env[:machine].provider.driver.rootfs_path).join("etc/network/interfaces")
              begin
                inter.open("w") do |f|
                  f << <<-INTER_CONTENT
                    auto lo
                    iface lo inet loopback

                    auto eth0
                    iface eth0 inet static
                      address #{options.fetch(:ip)}
                      netmask 255.255.255.0
                      gateway 0.0.0.0
                  INTER_CONTENT
                end
              rescue Errno::EACCES
                puts "Need to use sudo to configure network interface"
                command = "sudo chmod 666 #{inter}"
                puts command
                `#{command}`
                redo if try == 1
              end
              break
            end
            @app.call env
          end
        end
      end
    end
  end

@fgrehm
Copy link
Owner

fgrehm commented Jul 6, 2013

Tks Robert!
Is that needed for other debian versions? Are up for sending a PR? We'd
just need to add a check if the guest type is debian && is the expected
version before writing the file on the guest container ;)
Oh, and another nice to have would be to remove the file on machine halt :)

Fábio Rehm
Sent from my phone
On Jul 6, 2013 7:55 PM, "Robert Pankowecki" notifications@github.com
wrote:

Here is my workaround that I put into Vagrantfile. Definitely not a
perfect solution but seems to be working with Debian Squeeze:

require 'vagrant-lxc/provider'
require 'vagrant-lxc/action'
module Vagrant
module LXC
class Provider
def ssh_info
# If the Container is not created then we cannot possibly SSH into it, so
# we return nil.
return nil if state == :not_created

      ip = @machine.config.vm.networks.map do |type, options|
        type == :private_network && options[:ip] || nil
      end.compact.first || @driver.assigned_ip
      {
        :host => ip,
        :port => @machine.config.ssh.guest_port
      }
    end
  end
  module Action
    class << self
      alias_method :original_action_boot, :action_boot
      def action_boot
        action_builder = original_action_boot
        action_builder.insert_before(Vagrant::LXC::Action::Boot, Vagrant::Action::Builder.new.tap{|b| b.use Vagrant::LXC::Action::Magic } )
        action_builder
      end
    end
    class Magic
      def initialize(app, env)
        @app = app
      end
      def call(env)
        @env = env
        env[:machine].config.vm.networks.each do |type, options|
          next unless type == :private_network && options[:ip]
          try ||= 0
          try  += 1
          inter = Pathname.new(@env[:machine].provider.driver.rootfs_path).join("etc/network/interfaces")
          begin
            inter.open("w") do |f|
              f << <<-INTER_CONTENT                    auto lo                    iface lo inet loopback
                auto eth0                    iface eth0 inet static                      address #{options.fetch(:ip)}                      netmask 255.255.255.0                      gateway 0.0.0.0                  INTER_CONTENT
            end
          rescue Errno::EACCES
            puts "Need to use sudo to configure network interface"
            command = "sudo chmod 666 #{inter}"
            puts command
            `#{command}`
            redo if try == 1
          end
          break
        end
        @app.call env
      end
    end
  end
end

end


Reply to this email directly or view it on GitHubhttps://github.com//issues/106#issuecomment-20562506
.

@tripledes
Copy link

Seems to be also needed for Debian 7 (wheezy) as it always starts dhcp no matter what network configuration I set in my Vagrantfile.

@fgrehm
Copy link
Owner

fgrehm commented Jul 29, 2013

Closing in favor of #120

@fgrehm fgrehm closed this as completed Jul 29, 2013
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants