From 1ab0655d906f53e08cc205f1236c0025af6bbc68 Mon Sep 17 00:00:00 2001 From: Cam Cope Date: Tue, 18 Feb 2014 04:52:54 -0500 Subject: [PATCH 1/2] add backingstore parameters --- lib/vagrant-lxc/config.rb | 15 +++++++++++++++ lib/vagrant-lxc/driver.rb | 4 ++-- lib/vagrant-lxc/driver/cli.rb | 4 +++- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/vagrant-lxc/config.rb b/lib/vagrant-lxc/config.rb index a780a817..afac611d 100644 --- a/lib/vagrant-lxc/config.rb +++ b/lib/vagrant-lxc/config.rb @@ -6,6 +6,12 @@ class Config < Vagrant.plugin("2", :config) # @return [Array] attr_reader :customizations + # A string that contains the backing store type used with lxc-create -B + attr_accessor :backingstore + + # Optional arguments for the backing store, such as --fssize, --fstype, ... + attr_accessor :backingstore_options + # A String that points to a file that acts as a wrapper for sudo commands. # # This allows us to have a single entry when whitelisting NOPASSWD commands @@ -18,6 +24,8 @@ class Config < Vagrant.plugin("2", :config) def initialize @customizations = [] + @backingstore = UNSET_VALUE + @backingstore_options = [] @sudo_wrapper = UNSET_VALUE @use_machine_name = UNSET_VALUE @container_name = UNSET_VALUE @@ -38,10 +46,17 @@ def customize(key, value) @customizations << [key, value] end + # Stores options for backingstores like lvm, btrfs, etc + def backingstore_option(key, value) + @backingstore_options << [key, value] + end + def finalize! @sudo_wrapper = nil if @sudo_wrapper == UNSET_VALUE @use_machine_name = false if @use_machine_name == UNSET_VALUE @container_name = nil if @container_name == UNSET_VALUE + @backingstore = "none" if @backingstore == UNSET_VALUE + @existing_container_name = nil if @existing_container_name == UNSET_VALUE end def validate(machine) diff --git a/lib/vagrant-lxc/driver.rb b/lib/vagrant-lxc/driver.rb index dbedcfb7..1fa859f0 100644 --- a/lib/vagrant-lxc/driver.rb +++ b/lib/vagrant-lxc/driver.rb @@ -47,12 +47,12 @@ def config_string @sudo_wrapper.run('cat', base_path.join('config').to_s) end - def create(name, template_path, config_file, template_options = {}) + def create(name, backingstore, backingstore_options, template_path, config_file, template_options = {}) @cli.name = @container_name = name import_template(template_path) do |template_name| @logger.debug "Creating container..." - @cli.create template_name, config_file, template_options + @cli.create template_name, backingstore, backingstore_options, config_file, template_options end end diff --git a/lib/vagrant-lxc/driver/cli.rb b/lib/vagrant-lxc/driver/cli.rb index f672bfda..d04a1d9f 100644 --- a/lib/vagrant-lxc/driver/cli.rb +++ b/lib/vagrant-lxc/driver/cli.rb @@ -45,7 +45,7 @@ def state end end - def create(template, config_file, template_opts = {}) + def create(template, backingstore, backingstore_options, config_file, template_opts = {}) if config_file config_opts = ['-f', config_file] end @@ -54,6 +54,8 @@ def create(template, config_file, template_opts = {}) extra.unshift '--' unless extra.empty? run :create, + '-B', backingstore, + *(backingstore_options.to_a.flatten), '--template', template, '--name', @name, *(config_opts), From 05a0f3c54165263a930e350424958168a00db785 Mon Sep 17 00:00:00 2001 From: Cam Cope Date: Tue, 18 Feb 2014 04:55:44 -0500 Subject: [PATCH 2/2] add clone support, add backingstore params to create action --- lib/vagrant-lxc/action/create.rb | 21 ++++++++++++++------- lib/vagrant-lxc/config.rb | 8 ++++++++ lib/vagrant-lxc/driver.rb | 6 ++++++ lib/vagrant-lxc/driver/cli.rb | 7 +++++++ 4 files changed, 35 insertions(+), 7 deletions(-) diff --git a/lib/vagrant-lxc/action/create.rb b/lib/vagrant-lxc/action/create.rb index 1676e21c..72539f84 100644 --- a/lib/vagrant-lxc/action/create.rb +++ b/lib/vagrant-lxc/action/create.rb @@ -7,7 +7,9 @@ def initialize(app, env) end def call(env) - container_name = env[:machine].provider_config.container_name + config = env[:machine].provider_config + + container_name = config.container_name case container_name when :machine @@ -22,12 +24,17 @@ def call(env) container_name << "_#{(Time.now.to_f * 1000.0).to_i}_#{rand(100000)}" end - env[:machine].provider.driver.create( - container_name, - env[:lxc_template_src], - env[:lxc_template_config], - env[:lxc_template_opts] - ) + if config.existing_container_name + env[:machine].provider.driver.clone(config.existing_container_name, container_name) + else + env[:machine].provider.driver.create( + container_name, + config.backingstore, + config.backingstore_options, + env[:lxc_template_src], + env[:lxc_template_config], + env[:lxc_template_opts]) + end env[:machine].id = container_name diff --git a/lib/vagrant-lxc/config.rb b/lib/vagrant-lxc/config.rb index afac611d..f514c2a4 100644 --- a/lib/vagrant-lxc/config.rb +++ b/lib/vagrant-lxc/config.rb @@ -22,7 +22,15 @@ class Config < Vagrant.plugin("2", :config) # to the corresponding machine name. attr_accessor :container_name + attr_accessor :existing_container_name + + # A String that names the container to clone from + def clone_container_from(name) + @existing_container_name = name + end + def initialize + @existing_container_name = UNSET_VALUE @customizations = [] @backingstore = UNSET_VALUE @backingstore_options = [] diff --git a/lib/vagrant-lxc/driver.rb b/lib/vagrant-lxc/driver.rb index 1fa859f0..ab9a488c 100644 --- a/lib/vagrant-lxc/driver.rb +++ b/lib/vagrant-lxc/driver.rb @@ -56,6 +56,12 @@ def create(name, backingstore, backingstore_options, template_path, config_file, end end + def clone(existing_container_name, name) + @cli.name = name + @logger.debug "Cloning container..." + @cli.clone(existing_container_name, name) + end + def share_folders(folders) folders.each do |folder| guestpath = rootfs_path.join(folder[:guestpath].gsub(/^\//, '')) diff --git a/lib/vagrant-lxc/driver/cli.rb b/lib/vagrant-lxc/driver/cli.rb index d04a1d9f..2eae2ad6 100644 --- a/lib/vagrant-lxc/driver/cli.rb +++ b/lib/vagrant-lxc/driver/cli.rb @@ -68,6 +68,13 @@ def create(template, backingstore, backingstore_options, config_file, template_o end end + def clone(existing_container_name, name) + run :clone, + '-s', + '-o', existing_container_name, + '-n', @name + end + def destroy run :destroy, '--name', @name end