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

Clone support #235

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions lib/vagrant-lxc/action/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
23 changes: 23 additions & 0 deletions lib/vagrant-lxc/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -16,8 +22,18 @@ 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 = []
@sudo_wrapper = UNSET_VALUE
@use_machine_name = UNSET_VALUE
@container_name = UNSET_VALUE
Expand All @@ -38,10 +54,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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Despite the man entry, it looks like -B 'none' does not work with lxc-create 1.0.0.alpha1. You'll get an error as follows:

lxc-create -B 'none'  -t ubuntu -n foo
lxc-create: Failed to create backing store type none
lxc-create: Error creating backing store type none for foo
lxc-create: Error creating container foo

Probably better to use 'dir' here, or don't use the -B option at all in lxc-create if @backingstore is undefined

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mowings Ah darn, I didn't go back and actually test "none" after upgrading to 1.0. If "dir" works for both 0.7.5 and 1.0 I'll change it to that, otherwise this might need a bit more refactoring.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe "best" is a better default?

@existing_container_name = nil if @existing_container_name == UNSET_VALUE
end

def validate(machine)
Expand Down
10 changes: 8 additions & 2 deletions lib/vagrant-lxc/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,21 @@ 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

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(/^\//, ''))
Expand Down
11 changes: 10 additions & 1 deletion lib/vagrant-lxc/driver/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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),
Expand All @@ -66,6 +68,13 @@ def create(template, config_file, template_opts = {})
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
Expand Down