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

Commit

Permalink
Fix incompatibility with LXC 3.0
Browse files Browse the repository at this point in the history
LXC 3.0, unlike 2.1, doesn't support old configuration formats at all
and because our previous approach to handling old format was incomplete,
running `vagrant up` on a system with LXC 3.0 would fail, even if the
base box itself was made with modernized config.

This commit fixes this problem: is the box has a modern config, it's
going to be properly created and booted under LXC 3.0. It does so
without breaking the support for LXC < 2.1
  • Loading branch information
Virgil Dupras committed Apr 30, 2018
1 parent 208edd4 commit dd24cdc
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 13 deletions.
10 changes: 7 additions & 3 deletions lib/vagrant-lxc/action/boot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ def initialize(app, env)

def call(env)
@env = env

driver = env[:machine].provider.driver
config = env[:machine].provider_config

utsname = env[:machine].config.vm.hostname || env[:machine].id
config.customize 'utsname', utsname
if driver.supports_new_config_format
config.customize 'uts.name', utsname
else
config.customize 'utsname', utsname
end

# Fix apparmor issues when starting Ubuntu 14.04 containers
# See https://github.com/fgrehm/vagrant-lxc/issues/278 for more information
Expand All @@ -32,7 +36,7 @@ def call(env)
end

env[:ui].info I18n.t("vagrant_lxc.messages.starting")
env[:machine].provider.driver.start(config.customizations)
driver.start(config.customizations)

@app.call env
end
Expand Down
6 changes: 5 additions & 1 deletion lib/vagrant-lxc/action/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@ def call(env)
backingstore = config.privileged ? "best" : "dir"
end
driver = env[:machine].provider.driver
template_options = env[:lxc_template_opts]
if ! driver.supports_new_config_format
template_options['--oldconfig'] = ''
end
driver.create(
container_name,
backingstore,
config.backingstore_options,
env[:lxc_template_src],
env[:lxc_template_config],
env[:lxc_template_opts]
template_options
)
driver.update_config_keys

Expand Down
5 changes: 5 additions & 0 deletions lib/vagrant-lxc/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def create(name, backingstore, backingstore_options, template_path, config_file,
@cli.name = @container_name = name

@logger.debug "Creating container..."

@cli.create template_path, backingstore, backingstore_options, config_file, template_options
end

Expand Down Expand Up @@ -214,6 +215,10 @@ def version
@version ||= @cli.version
end

def supports_new_config_format
Gem::Version.new(version) >= Gem::Version.new('2.1.0')
end

# TODO: This needs to be reviewed and specs needs to be written
def compress_rootfs
# TODO: Pass in tmpdir so we can clean up from outside
Expand Down
2 changes: 1 addition & 1 deletion lib/vagrant-lxc/driver/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def create(template, backingstore, backingstore_options, config_file, template_o
config_opts = ['-f', config_file]
end

extra = template_opts.to_a.flatten
extra = template_opts.to_a.flatten.reject { |elem| elem.empty? }
extra.unshift '--' unless extra.empty?

run :create,
Expand Down
13 changes: 11 additions & 2 deletions scripts/lxc-template
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ LXC_PATH=
LXC_ROOTFS=
LXC_TARBALL=
LXC_CONFIG=
LXC_USE_OLDCONFIG=
LXC_STRIP_COMPONENTS=2


usage() {
cat <<EOF
vagrant-lxc default template
Expand All @@ -44,6 +46,7 @@ Required arguments:
Optional arguments:
[ --config ]: Configuration file to be used when building the container
[ --oldconfig ]: Use pre LXC 2.1 config format
[ -h | --help ]: This help message
LXC internal arguments (do not pass manually!):
Expand All @@ -57,7 +60,7 @@ EOF
return 0
}

options=$(getopt -o h -l tarball:,config:,help:,name:,path:,rootfs:,mapped-uid:,mapped-gid:,strip-components: -- "$@")SS
options=$(getopt -o h -l tarball:,config:,oldconfig,help:,name:,path:,rootfs:,mapped-uid:,mapped-gid:,strip-components: -- "$@")SS

if [ $? -ne 0 ]; then
usage $(basename $0)
Expand All @@ -70,6 +73,7 @@ do
case "$1" in
-h|--help) usage $0 && exit 0;;
--config) LXC_CONFIG=$2; shift 2;;
--oldconfig) LXC_USE_OLDCONFIG=1; shift 1;;
--tarball) LXC_TARBALL=$2; shift 2;;
--name) LXC_NAME=$2; shift 2;;
--path) LXC_PATH=$2; shift 2;;
Expand Down Expand Up @@ -144,7 +148,12 @@ if [ -e "${LXC_PATH}/config-auto" ]; then
cat ${LXC_PATH}/config-auto >> ${LXC_PATH}/config
rm ${LXC_PATH}/config-auto
fi
echo "lxc.utsname = ${LXC_NAME}" >> ${LXC_PATH}/config

if [ $LXC_USE_OLDCONFIG ]; then
echo "lxc.utsname = ${LXC_NAME}" >> ${LXC_PATH}/config
else
echo "lxc.uts.name = ${LXC_NAME}" >> ${LXC_PATH}/config
fi

## Re-add the previously removed network config
if [ -e "${LXC_PATH}/config-network" ]; then
Expand Down
12 changes: 6 additions & 6 deletions spec/unit/driver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,9 @@
# Blah blah comment
lxc.mount.entry = proc proc proc nodev,noexec,nosuid 0 0
lxc.mount.entry = sysfs sys sysfs defaults 0 0
lxc.tty = 4
lxc.pts = 1024
lxc.rootfs = #{rootfs_path}
lxc.tty.max = 4
lxc.pty.max = 1024
lxc.rootfs.path = #{rootfs_path}
# VAGRANT-BEGIN
lxc.network.type=veth
lxc.network.name=eth1
Expand Down Expand Up @@ -241,9 +241,9 @@
# Blah blah comment
lxc.mount.entry = proc proc proc nodev,noexec,nosuid 0 0
lxc.mount.entry = sysfs sys sysfs defaults 0 0
lxc.tty = 4
lxc.pts = 1024
lxc.rootfs = overlayfs:/path/to/master/directory:#{rootfs_path}
lxc.tty.max = 4
lxc.pty.max = 1024
lxc.rootfs.path = overlayfs:/path/to/master/directory:#{rootfs_path}
# VAGRANT-BEGIN
lxc.network.type=veth
lxc.network.name=eth1
Expand Down

0 comments on commit dd24cdc

Please sign in to comment.