diff --git a/README.md b/README.md index 2129bbb..5afbefb 100644 --- a/README.md +++ b/README.md @@ -65,9 +65,9 @@ Vagrant.configure("2") do |config| config.vm.provider :lxc do |lxc| # Same as 'customize ["modifyvm", :id, "--memory", "1024"]' for VirtualBox - lxc.start_opts << 'lxc.cgroup.memory.limit_in_bytes=400M' + lxc.customize 'cgroup.memory.limit_in_bytes', '400M' # Limits swap size - lxc.start_opts << 'lxc.cgroup.memory.memsw.limit_in_bytes=500M' + lxc.customize 'cgroup.memory.memsw.limit_in_bytes', '500M' end # ... your puppet / chef / shell provisioner configs here ... diff --git a/development/Vagrantfile b/development/Vagrantfile index 78ef71d..58ab3bf 100644 --- a/development/Vagrantfile +++ b/development/Vagrantfile @@ -63,7 +63,7 @@ Vagrant.configure("2") do |config| lxc_config.vm.provider :lxc do |lxc| # Required to boot nested containers - lxc.start_opts << 'lxc.aa_profile=unconfined' + lxc.customize 'aa_profile', 'unconfined' end end diff --git a/example/Vagrantfile b/example/Vagrantfile index 3328a57..bf8a8c1 100644 --- a/example/Vagrantfile +++ b/example/Vagrantfile @@ -27,8 +27,8 @@ Vagrant.configure("2") do |config| config.vm.synced_folder cache_dir, "/var/cache/apt/archives" config.vm.provider :lxc do |lxc| - lxc.start_opts << 'lxc.cgroup.memory.limit_in_bytes=400M' - lxc.start_opts << 'lxc.cgroup.memory.memsw.limit_in_bytes=500M' + lxc.customize 'cgroup.memory.limit_in_bytes', '400M' + lxc.customize 'cgroup.memory.memsw.limit_in_bytes', '500M' end config.vm.provision :shell, :inline => <<-SCRIPT diff --git a/lib/vagrant-lxc/action/share_folders.rb b/lib/vagrant-lxc/action/share_folders.rb index cfea376..4aefb0e 100644 --- a/lib/vagrant-lxc/action/share_folders.rb +++ b/lib/vagrant-lxc/action/share_folders.rb @@ -9,7 +9,7 @@ module Vagrant def call(env) @env = env prepare_folders - add_start_opts + add_override_configs @app.call env end @@ -47,7 +47,7 @@ module Vagrant end end - def add_start_opts + def add_override_configs @env[:ui].info I18n.t("vagrant.actions.lxc.share_folders.preparing") folders = [] diff --git a/lib/vagrant-lxc/config.rb b/lib/vagrant-lxc/config.rb index ee9d2f1..6095973 100644 --- a/lib/vagrant-lxc/config.rb +++ b/lib/vagrant-lxc/config.rb @@ -1,14 +1,31 @@ module Vagrant module LXC class Config < Vagrant.plugin("2", :config) - # An array of options to be passed to lxc-start when booting the machine. + # An array of container's configuration overrides to be provided to `lxc-start`. # # @return [Array] - attr_reader :start_opts + attr_reader :customizations def initialize - @start_opts = [] + @customizations = [] end + + # Customize the container by calling `lxc-start` with the given + # configuration overrides. + # + # For example, if you want to set the memory limit, you can use it + # like: config.customize 'cgroup.memory.limit_in_bytes', '400M' + # + # When `lxc-start`ing the container, vagrant-lxc will pass in + # "-s lxc.cgroup.memory.limit_in_bytes=400M" to it. + # + # @param [String] key Configuration key to override + # @param [String] value Configuration value to override + def customize(key, value) + @customizations << [key, value] + end + + # TODO: At some point in the future it would be nice to validate these options end end end diff --git a/lib/vagrant-lxc/driver.rb b/lib/vagrant-lxc/driver.rb index f15f629..d3ce2e5 100644 --- a/lib/vagrant-lxc/driver.rb +++ b/lib/vagrant-lxc/driver.rb @@ -55,19 +55,19 @@ module Vagrant end end - config.start_opts << "lxc.mount.entry=#{folder[:hostpath]} #{guestpath} none bind 0 0" + # TODO: Move outside + config.customize 'mount.entry', "#{folder[:hostpath]} #{guestpath} none bind 0 0" end end def start(config) @logger.info('Starting container...') - opts = config.start_opts.dup if ENV['LXC_START_LOG_FILE'] extra = ['-o', ENV['LXC_START_LOG_FILE'], '-l', 'DEBUG'] end - @cli.transition_to(:running) { |c| c.start(opts, (extra || nil)) } + @cli.transition_to(:running) { |c| c.start(config.customizations, (extra || nil)) } end def halt diff --git a/lib/vagrant-lxc/driver/cli.rb b/lib/vagrant-lxc/driver/cli.rb index ac7a00b..e26531c 100644 --- a/lib/vagrant-lxc/driver/cli.rb +++ b/lib/vagrant-lxc/driver/cli.rb @@ -45,10 +45,10 @@ module Vagrant run :destroy, '--name', @name end - def start(configs = [], extra_opts = []) - configs = configs.map { |conf| ["-s", conf] }.flatten - configs += extra_opts if extra_opts - run :start, '-d', '--name', @name, *configs + def start(overrides = [], extra_opts = []) + options = overrides.map { |key, value| ["-s", "lxc.#{key}=#{value}"] }.flatten + options += extra_opts if extra_opts + run :start, '-d', '--name', @name, *options end def shutdown diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 47b184a..9b6ca05 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -17,6 +17,7 @@ Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each { |f| require f } if ENV['VERIFY_CONSTANT_NAMES'] require 'vagrant-lxc/plugin' require 'vagrant-lxc/provider' + require 'vagrant-lxc/config' end require 'rspec/fire' diff --git a/spec/unit/driver/cli_spec.rb b/spec/unit/driver/cli_spec.rb index 2c364a5..41ff7ea 100644 --- a/spec/unit/driver/cli_spec.rb +++ b/spec/unit/driver/cli_spec.rb @@ -80,8 +80,8 @@ describe Vagrant::LXC::Driver::CLI do ) end - it 'uses provided hash to configure the container' do - subject.start(['lxc.config=value', 'lxc.other=value']) + it 'uses provided array to override container configs' do + subject.start([['config', 'value'], ['other', 'value']]) subject.should have_received(:run).with(:start, '-d', '--name', name, '-s', 'lxc.config=value', '-s', 'lxc.other=value' diff --git a/spec/unit/driver_spec.rb b/spec/unit/driver_spec.rb index f593319..ae2b04e 100644 --- a/spec/unit/driver_spec.rb +++ b/spec/unit/driver_spec.rb @@ -70,8 +70,8 @@ describe Vagrant::LXC::Driver do end describe 'start' do - let(:config) { mock(:config, start_opts: ['a=1', 'b=2']) } let(:name) { 'container-name' } + let(:config) { fire_double('Vagrant::LXC::Config', customizations: [['a', '1'], ['b', '2']]) } let(:cli) { fire_double('Vagrant::LXC::Driver::CLI', start: true) } subject { described_class.new(name, cli) } @@ -81,7 +81,7 @@ describe Vagrant::LXC::Driver do end it 'starts container with configured lxc settings' do - cli.should_receive(:start).with(['a=1', 'b=2'], nil) + cli.should_receive(:start).with(config.customizations, nil) subject.start(config) end