Compare commits

..

144 commits

Author SHA1 Message Date
a47660a1e5 Update 'README.md' 2023-02-03 19:28:15 +00:00
3575d65ffb Update 'README.md' 2023-02-03 19:08:48 +00:00
Glenn Y. Rolland
938a29ed96
Merge branch 'fgrehm:master' into master 2023-02-03 19:56:32 +01:00
Fabio
ff58ecd5f2
Update README.md 2022-11-24 20:41:12 -03:00
Fabio
df6b78c4ed
Update README.md 2022-11-24 20:40:54 -03:00
Fabio
4d53acb9fb
Update README.md 2022-11-24 20:40:34 -03:00
Fabio
b88d50fb68
Update README.md 2022-11-16 22:39:19 -03:00
4e3306ed74 Fix the 'eval: Address:: not found' error that may appear 2022-09-28 15:01:04 +02:00
Fabio Rehm
c167ac7f83
Make it clear that project is looking for maintainers
Ref https://github.com/fgrehm/vagrant-lxc/issues/492
2021-01-12 11:15:50 -03:00
Virgil Dupras
a112b072aa
Merge pull request #473 from ebobby/fix-redir3
Fix redir version fetching.
2018-09-26 19:53:14 -04:00
Francisco Soto
f750bf406c Fix redir version fetching.
"" || "something" doesn't really work. Have to check for the actual string
length to determine if got something or not out of the pipe.
2018-09-24 18:43:03 -07:00
Virgil Dupras
2a5510b34c Fix "vargant package" for LXC 2.1+ config format
New config format would be under `lxc.rootfs.path` and be prefixed by
`dir:`.
2018-07-24 12:23:25 -04:00
Virgil Dupras
a1aa60ded5 Remove supports_attach? and call to /sbin/halt
Hosts without support for lxc-attach are ancien and can always use old
versions of vagrant-lxc. To be able to move forward more easily, we
should be able to assume a functional `lxc-attach`.

As for `/sbin/halt`, I'm really not sure it's needed anymore. Let's see
if its removal causes problems.
2018-07-24 11:21:09 -04:00
Virgil Dupras
61c921ac6f Use require_relative everywhere in plugin entry point 2018-07-24 10:25:08 -04:00
Virgil Dupras
437b5e7a2e README: base boxes status update 2018-07-24 10:08:07 -04:00
Virgil Dupras
2c2630a788 Bump minimum requirements for next vagrant-lxc release (v1.5)
We now depend on vagrant 1.9+ and LXC 2.0+, following bundled version in
debian stretch.

Also, update Gemfile to target latest vagrant/vagrant-spec. It's too
complicated to test against old vagrant versions. Vagrant 1.8.x didn't
specify an upper bound for supported versions but vagrant 1.9 does
(<2.4). This breaks out CI testing matrix which tests against 2.3, 2.4
and 2.5.

So let's make our live easier and just test against the latest vagrant
version and try not to mistakenly drop support for our lowest supported
vagrant version...
2018-07-24 09:37:34 -04:00
Virgil Dupras
5a1f6ab2cb
Merge pull request #469 from worxli/feature-add-ssh-ip
Add config for container SSH IP
2018-07-23 06:27:50 -04:00
Lukas Bischofberger
f71de429a1 Add config for container ssh IP 2018-07-21 14:52:26 +02:00
Virgil Dupras
50cbe5a0d6 v1.4.2 2018-07-17 14:37:18 -04:00
Virgil Dupras
44eefd81fb Merge tag 'v1.4.1' 2018-07-17 14:34:58 -04:00
Virgil Dupras
a84f8b59bf redir: adjust failing tests 2018-07-04 15:00:46 -04:00
Virgil Dupras
c1dd7baf2a redir: start in foreground mode under v3.x
ref #467
2018-07-04 14:34:26 -04:00
Virgil Dupras
8a1e1863a7 redir: fix version parsing
At some point in v 3.x, it outputted version in stderr, then went back
to stdout...
2018-07-04 14:33:51 -04:00
Virgil Dupras
b812a6934a v1.4.1 2018-04-30 21:32:33 -04:00
Virgil Dupras
8cfe577b17 Fix LXC 3.0 compatibility with old boxes
Creating a new vagrant machine with a box created with an old LXC config
format would fail with LXC 3.0. We now update the LXC config of the box
itself before calling `lxc-create`.
2018-04-30 20:45:25 -04:00
Virgil Dupras
dd24cdc3d6 Fix incompatibility with LXC 3.0
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
2018-04-29 22:57:31 -04:00
Virgil Dupras
208edd49e5
Merge pull request #461 from nicolasleger/patch-1
[CI] Test against Ruby 2.5
2018-03-18 16:41:55 -04:00
Virgil Dupras
484b868100
Merge pull request #460 from lucaskanashiro/support_redir-3.x
Support the new redir 3.x command line interface
2018-03-18 16:41:18 -04:00
Nicolas Leger
b5f2a9006c
[CI] Test against Ruby 2.5 2018-03-15 14:52:34 +01:00
Lucas Kanashiro
2e20f96fec Support the new redir 3.x command line interface
Since redir 2.x upstream is not evolving it, Joachim Nilsson adopted it
and have started to work on it and publish a new command line interface
(check out https://github.com/troglobit/redir). The redir 3.1 is already
available in Debian and Ubuntu in their stable releases.
2018-03-12 13:09:06 -03:00
Virgil Dupras
d3409ace2a v1.4.0 2018-03-04 09:22:58 -05:00
Virgil Dupras
530a899467 Add support for unprivileged containers
fixes #312

I've also remove the "EXPERIMENTAL" flag to bridge support in the README.
It's been there long enough.
2018-02-17 21:53:16 -05:00
Virgil Dupras
9e215ae1a1 v1.3.1 2018-02-06 22:02:29 -05:00
Cam Cope
8fa42b1ab4
Merge pull request #456 from fgrehm/tmpfs-revival
Revert tmpfs mount removal
2018-02-04 00:04:39 -08:00
Virgil Dupras
2ae84fcc51 Revert tmpfs mount removal
To fix #406, I reverted the tmpfs mount we add at boot time. As we can
see in #455, it was a bad idea.

In addition to bringing back that mount, I've also added a
`tmpfs_mount_size` config that allows to change the size of the mount
from its default `2G`. It's also possible to disable the mount
altogether.

fixes #455
2018-02-02 16:02:17 -05:00
Virgil Dupras
582e3b368a halt: check that machine is running before attempting to clear tmpfiles
Otherwise, running `vagrant halt` on a stopped machine would make us
crash. Particularly annoying in multi-machines environments.

ref #406
2018-01-27 14:32:42 -05:00
Virgil Dupras
6738febaa2 v1.3.0 2018-01-20 13:29:46 -05:00
Virgil Dupras
49ebf3488e sudoers: fix typo
fixes #451
2018-01-15 21:17:36 -05:00
Virgil Dupras
bd42317ec2 Update README
Fixes #431
2018-01-15 08:25:27 -05:00
Virgil Dupras
1c27047f4b Remove tmpfs mount on /tmp and bring back /tmp cleanup on halt
fixes #406 because the `tmpfs` mount isn't needed anymore.

Careful considerations had to be taken because #68 mentions host-side
data loss when cleaning up `/tmp`. We mitigate this by ensuring that all
mounts under `/tmp` are unmounted before we proceed with our `rm -rf`
operation.

More context about this issue can be found in #360.
2018-01-13 23:00:46 -05:00
Virgil Dupras
fef11bf7ef Update README 2018-01-13 21:06:45 -05:00
Virgil Dupras
c74ddbf2fc create: with LXC 2.1+ run lxc-update-config on new boxes
When running with LXC 2.1+, ensure that our boxes have up-to-date config
keys by running the `lxc-update-config` utility that ships with LXC
2.1+.

When the command doesn't exist (LXC <2.1), we do nothing.

ref #445
2018-01-13 21:03:34 -05:00
Virgil Dupras
2b08ae199f driver: little cleanup after LXC 1.0+ requirement bump
The conditional `lxc-version` and `lxc-config` mechanisms aren't needed
anymore. They were for pre-1.0 LXC versions.
2018-01-13 20:34:41 -05:00
Virgil Dupras
aa777653f4 Use lxc-info instead of lxc-attach to retrieve container IP
`lxc-info -iH` to retrieve IP address was not available in early LXC
development but was there at LXC 1.0. Because we've bumped our minimum
LXC requirement to v1.0 recently, we can simplify the IP retrieval
process and also get rid of the `dnsmasq` fallback.
2018-01-13 15:53:28 -05:00
Virgil Dupras
7e1eb1d373 Change version to 1.3.0-dev 2018-01-13 15:30:39 -05:00
Virgil Dupras
4ff412e2d2 Fix travis for real this time
Sorry for the commit noise, I didn't think I'd need this much fiddling
and I don't have push-force privileges on the repo.
2018-01-13 09:06:45 -05:00
Virgil Dupras
bd4aa8167c Fix travis (again) 2018-01-13 08:56:17 -05:00
Virgil Dupras
2423464cdb Fix travis (again) 2018-01-13 08:54:06 -05:00
Virgil Dupras
0b5087f72e Fix travis builds 2018-01-13 08:43:00 -05:00
Virgil Dupras
c8801ba8b2
Merge pull request #447 from hsoft/unprivileged-template
lxc-template: make runnable by unprivileged users
2018-01-13 08:35:43 -05:00
Virgil Dupras
97b5882262 Refactoring: make SudoWrapper a bit more self-contained
By looking at the code, it seems that it was a goal to make the sudo
wrapper path configurable through the Vagrantfile, but it wasn't
effective and didn't make much sense (that kind of config is a per-host
config, not a per-guest one).

This caused the cause to be needlessly complex by giving the Provider
the responsibility of instanciating the wrapper. This commit gets rid of
that.

I didn't get rid of `sudo_wrapper` injection in `Driver` and
`Driver::CLI` constructors because they're needed for tests. I'm not
ready to tackle this yet.
2018-01-13 08:32:27 -05:00
Virgil Dupras
6eb7ec1a2e Bump Vagrant and LXC requirements 2018-01-13 08:32:27 -05:00
Virgil Dupras
fe7d638b35 Remove Gemfile.lock and properly lock stuff in Gemfile
No other vagrant plugin use `Gemfile.lock`. It doesn't feel right.

When removing it, however, we end up with many dependencies conflicts.
It then becomes necessary to freeze dependencies in `Gemfile`.
2018-01-13 08:32:27 -05:00
Virgil Dupras
f8a78e520b Adjust changelog for v1.2.4 2017-12-20 15:45:31 -05:00
Virgil Dupras
dbf2dfb8ff
Merge pull request #448 from hsoft/master
v1.2.4
2017-12-20 10:33:49 -05:00
Virgil Dupras
d3788f55f3
Merge pull request #449 from nicobrevin/nicobrevin-master
Fix for #336
2017-12-16 19:15:16 -05:00
Nick Griffiths
f224fc5ea5 Fix for #336:
Subprocessed being interrupted resulted in it looking
like those commands were executing successfully but with
zero output.  Interrupting the sudo prompt would result
in any command running in sudo returning nothing and looking
like it had succeeded.  There was some clean up code in
the lxc provider that nuked vagrant container state in
the .vagrant directory if it looked like the container no
longer existed based on the result of lxc-ls.  Interrupting
this check resulted in it looking like the container not
existing, resulting in the provider code nuking the lxc dir
in .vagrant.  Voila
2017-12-16 12:07:11 +13:00
Virgil Dupras
1c64ea1b2c v1.2.4 2017-12-12 11:02:57 -05:00
Fabio Rehm
b96ba86f72
Update CHANGELOG.md 2017-12-12 13:55:25 -02:00
Virgil Dupras
8b93206c18
Merge pull request #435 from brandon-rhodes/fix-umask
Avoid `Timed out` error when umask is 027 or 077
2017-12-11 19:35:22 -05:00
Virgil Dupras
e4c566ebc9
Merge pull request #421 from TobleMiner/lxc-template-option-fix
Fixes #420 (blaze it)
2017-12-11 17:21:00 -05:00
Virgil Dupras
d35ee4aed0
Merge pull request #410 from ChaosCloud/abk
vagrant-lxc-wrapper need to allow sudoer first-found binary path
2017-12-11 17:07:21 -05:00
Virgil Dupras
03c23d7df8 README: mention incompatibility with LXC v2.1
see #445
2017-12-11 14:51:09 -05:00
Virgil Dupras
10c53c54d7 Support alternative lxcpath in sudo wrapper
The previously hardcoded lxc path prevented the sudo wrapper from
working in environment with alternative `lxcpath`.

I had to move `sudo_wrapper` from `provider` to `LXC` because the
concept of "provider" is tied to a machine when a command sush as
`sudoers` is not.

Fixes #413 and #399
2017-12-11 11:48:19 -05:00
Virgil Dupras
2ce460bc30
Merge pull request #438 from fkleon/fix-sudoers-vagrant-1.9.x
Update pipework regexp for sudo wrapper
2017-12-11 10:20:36 -05:00
Virgil Dupras
553f1b5ed0 lxc-template: make runnable by unprivileged users
lxc-template needlessly require root privileges in two places:

1. lock file location for `flock`
2. failing on `tar` failure during rootfs extraction

For `flock`, it's not necessary that the lock file be in `/var/lock`, it
can be anywhere. Why not put it in `LXC_PATH`?

For the failing `tar` thing, that's because some device are created with
`mknod` which unprivileged users can't do. These device, however, are
not necessary for the container to run well. We can ignore `tar`'s error
exit code.

I replaced the exist code check by a check for the existence of
`/bin/true` in rootfs. I think that it's a good indication of whether
the rootfs was extracted.

Why am I making this change? Because I'd like to add support for
unprivileged containers in `vagrant-lxc` but it's kind of a big change
to make at once, so I thought I'd go incrementally.
2017-12-09 19:36:54 -05:00
Frederik Leonhardt
1f805f6deb Update pipework regexp for sudo wrapper
On Vagrant 1.9+ plugin gems are installed into a different folder, their path
containing the ruby version. This updates the regular expression whitelisting
the pipework script to reflect this change.
2017-02-09 15:55:32 +13:00
Brandon Rhodes
43aa9bfb3e Avoid Timed out error when umask is 027 or 077
If LXC commands are run with a restrictive umask like 027 or 077, then
the root directory of new containers will lack read `r` and access `x`
permission for non-root users.  The first failure to result from this
during `vagrant up` is that the SSH daemon cannot read the crucial file
`/home/vagrant/.ssh/authorized_keys` after it drops privileges to the
level of the `vagrant` user.  The result is the familiar:

```
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
Timed out while waiting for the machine to boot. This means that
Vagrant was unable to communicate with the guest machine within
the configured ("config.vm.boot_timeout" value) time period.
```

So we should make sure that we run all LXC commands with a umask that at
least does not prevent group and world `r` and `x` bits from being set
in newly created files and directories.
2017-01-29 13:53:09 -05:00
Cam Cope
851f58d42a v1.2.3 2016-12-20 20:20:38 -08:00
Cam Cope
fdb7222965 Fix bad version in Gemfile.lock 2016-12-20 20:20:07 -08:00
Cam Cope
4fd0635204 v.1.2.2 2016-12-20 20:08:47 -08:00
Cam Cope
ae18c05b2b Merge pull request #426 from fonsecas72/patch-1
Make fetch_ip_tries configurable
2016-11-11 12:33:18 -05:00
Hugo Fonseca
6d8580a294 default values goes to finalize 2016-11-11 08:34:42 +00:00
Hugo Fonseca
cac4910296 tries amount of fetching ip is now configurable 2016-11-09 14:38:05 +00:00
Hugo Fonseca
2d63603d85 Increase the re-tries amount 2016-11-07 15:34:48 +00:00
Cam Cope
a68a1de21f Merge pull request #423 from sergle/master
Load locale file only once
2016-10-21 00:54:57 -07:00
Sergey Leschenko
671bd55cea Make code similar to other providers 2016-10-20 13:39:33 +03:00
Sergey Leschenko
1c2208a30e Load locale file only once 2016-10-18 14:40:37 +03:00
Tobias Schramm
0cdd4d352c Fixes #420 (blaze it) 2016-08-22 22:27:00 +02:00
Cam Cope
62535b6465 Merge pull request #411 from ccope/preserve-xattrs
Preserve xattrs in container rootfs
2016-06-02 00:16:40 -07:00
Cam Cope
932a7808be Merge pull request #408 from swettk/master
Forward port latest pipework script
2016-05-30 16:35:59 -07:00
Cam Cope
9dd12bfcdf update dependencies in the README 2016-05-11 16:54:12 -07:00
Cam Cope
6dcf584b25 preserve xattrs 2016-05-11 16:54:12 -07:00
Robert Heinzmann
544c061e65 Fix handling of non-fatal lxc-stop return code
Fixes #405
2016-05-11 16:50:29 -07:00
AbhishekKr
848383e081 vagrant-lxc-wrapper need to allow sudoer first-found binary path
sudoer PATH may have different order than current user
2016-05-11 23:51:29 +05:30
Keith Swett
d4e5122c6c Forward port latest pipework script 2016-04-26 19:52:57 +00:00
Robin Gloster
aa5fb7a932 1.2.1 2015-09-24 14:14:26 +00:00
Franz Pletz
81d70d26d3 Merge pull request #393 from mayflower/fix-sudo-wrapper
fix sudo wrapper
2015-09-24 15:27:40 +02:00
Robin Gloster
4c969a6ae7 fix sudo wrapper 2015-09-23 18:15:28 +00:00
Franz Pletz
1fb64dbbdf Merge pull request #392 from danielhodder/improve-lxc-missing-message
Added lxc-create to the error message
2015-09-21 01:16:21 +02:00
danielh
d11b6f2dc7 Added lxc-create to the error message
Added the command that vagrant-lxc is actually trying to run when it can't find the lxc-create command
2015-09-20 15:30:09 +12:00
Fabio Rehm
ad39aa9645 Update README.md 2015-09-17 12:43:57 -03:00
Fabio Rehm
050c160de2 v1.2.0 2015-09-15 09:57:48 -03:00
Robin Gloster
3d924985dd Prepare 1.2.0 2015-09-14 09:33:25 +00:00
Robin Gloster
95bfdfd54d explicitly add overlayfs as backingstore to readme
fixes #351
2015-09-08 21:00:26 +00:00
Robin Gloster
25d10cd3bc fix shebang in pipework 2015-09-08 20:54:29 +00:00
Franz Pletz
86381bfeee Merge branch 'stucki-create-bridge-if-missing' (PR #380) 2015-08-28 15:01:30 +02:00
Michael Stucki
7c778dfb4d Fix warning message 2015-08-28 15:00:37 +02:00
Michael Stucki
9e5637a9fe Do not remove system bridge virbr0 2015-08-28 15:00:37 +02:00
Michael Stucki
3257bd25bb Fix variable name 2015-08-28 15:00:37 +02:00
Andriy
06bbc7a5ef Update gc_private_network_bridges.rb 2015-08-28 15:00:37 +02:00
Andriy
6136ebb966 Update driver.rb
Fixing https://github.com/fgrehm/vagrant-lxc/issues/353 issue
2015-08-28 15:00:37 +02:00
Cam Cope
9b70f3daab make /tmp a tmpfs 2015-08-28 14:58:16 +02:00
Cam Cope
2accd886fa remove /tmp cleanup, fixes lvm rootfs 2015-08-28 14:58:16 +02:00
Robin Gloster
993e430ccc Use /usr/bin/env CMD instead of hardcoded paths
This fixes commands that currently rely on hardcoded paths and break
on systems like NixOS that don't have all binaries in /usr/bin etc.
2015-08-28 14:58:16 +02:00
Robin Gloster
ea99d13f76 Fix test on NixOS
Tempfile can be created in /run, too.
2015-08-28 14:58:16 +02:00
Robin Gloster
fddd8cc257 Update gems to resolve dependencies on NixOS 2015-08-28 14:58:16 +02:00
Robin Gloster
611a86ce84 do not copy the lxc template instead use the full path 2015-08-28 14:58:16 +02:00
Robin Gloster
e5a55d1020 fix shebang in template 2015-08-28 14:58:16 +02:00
Antonio Terceiro
64f561073c forward_ports: bind to localhost only by default
This has been raised as security concern on vagrant itself, and fixed
there some time ago.
2015-08-28 14:58:16 +02:00
Franz Pletz
0c353598f8 Merge pull request #362 from ccope/halt-lvm-fix
remove /tmp cleanup command, fixes lvm rootfs
2015-08-28 14:50:51 +02:00
Franz Pletz
78ef85aa97 Merge pull request #383 from stucki/use-system-bridge
Use system bridge
2015-08-25 15:20:11 +02:00
Michael Stucki
366a98a052 Fix warning message 2015-08-25 15:13:03 +02:00
Michael Stucki
40ced9d3d1 Do not remove system bridge virbr0 2015-08-25 15:12:32 +02:00
Franz Pletz
bbff802b5d Merge pull request #376 from globin/nixos-current
NixOS compatibility
2015-08-25 14:53:03 +02:00
Michael Stucki
eb503e42fd Fix variable name 2015-08-25 14:38:44 +02:00
Andriy
5329e8837e Update gc_private_network_bridges.rb 2015-08-25 14:38:31 +02:00
Andriy
d2c032a073 Update driver.rb
Fixing https://github.com/fgrehm/vagrant-lxc/issues/353 issue
2015-08-25 14:38:28 +02:00
Michael Stucki
7eb3be37de driver.rb: Check for correct bridge device name 2015-08-25 10:20:21 +02:00
Michael Stucki
31bb4eadf9 Move away from using ifconfig to ip link for enabling / disabling bridge devices 2015-08-25 10:18:14 +02:00
Cam Cope
5fb0bcbcbc make /tmp a tmpfs 2015-08-17 17:43:45 -07:00
Cam Cope
eba671c54d remove /tmp cleanup, fixes lvm rootfs 2015-08-17 17:43:24 -07:00
Robin Gloster
4b78c04a47 Use /usr/bin/env CMD instead of hardcoded paths
This fixes commands that currently rely on hardcoded paths and break
on systems like NixOS that don't have all binaries in /usr/bin etc.
2015-08-02 10:15:55 +00:00
Robin Gloster
9c67e13fb8 Fix test on NixOS
Tempfile can be created in /run, too.
2015-08-02 10:15:55 +00:00
Robin Gloster
5cd32c76df Update gems to resolve dependencies on NixOS 2015-08-02 10:15:55 +00:00
Robin Gloster
2b91983cae do not copy the lxc template instead use the full path 2015-08-02 10:15:55 +00:00
Robin Gloster
64240323f0 fix shebang in template 2015-08-02 09:58:36 +00:00
Franz Pletz
6718fed241 Merge pull request #381 from terceiro/bind-localhost
forward_ports: bind to localhost only by default
2015-07-31 19:04:12 +02:00
Antonio Terceiro
81f2c13541 forward_ports: bind to localhost only by default
This has been raised as security concern on vagrant itself, and fixed
there some time ago.
2015-07-19 12:29:27 -03:00
Michael Stucki
c3508870fd Private networking: Create bridge if it is missing 2015-07-06 18:01:18 +02:00
Michael Stucki
fb661300e7 Move bridge_exists check into a function 2015-07-06 18:01:15 +02:00
Fabio Rehm
bb21906ec4 Update README.md 2015-05-08 19:49:30 -03:00
Fabio Rehm
0365ad4a17 Update CHANGELOG.md 2015-04-06 11:02:18 -03:00
Fabio Rehm
fd22ccc073 Update CHANGELOG.md 2015-04-06 10:59:57 -03:00
Fabio Rehm
52cca8e7f6 Merge pull request #355 from lfaraone/use-current-ruby
vagrant-lxc-wrapper: Use correct ruby interpreter
2015-04-06 10:58:53 -03:00
Fabio Rehm
55bbadef6f Merge pull request #352 from ccope/private_dhcp
Support private networking using DHCP
2015-04-06 10:56:38 -03:00
Fabio Rehm
f52df5df14 Update CHANGELOG.md 2015-04-06 10:54:36 -03:00
Fabio Rehm
8cb0afeb39 Merge pull request #357 from agriffis/fedora-host-selinux-ro
Mount the selinux sys dir read-only [GH-301]
2015-04-06 10:51:30 -03:00
Fabio Rehm
8ccafecd9e Update CHANGELOG.md 2015-04-06 10:30:27 -03:00
Fabio Rehm
497f750248 Merge pull request #361 from ccope/autocreate-mountpoint
move mountpoint creation to lxc template for lvm rootfs support
2015-04-06 10:27:18 -03:00
Cam Cope
caa3c53a8f move mountpoint creation to lxc template for lvm rootfs support 2015-04-05 17:46:22 -07:00
Aron Griffis
7d017ada1e Mount the selinux sys dir read-only [GH-301] 2015-03-24 17:42:11 -04:00
Luke Faraone
05fdb3e000 vagrant-lxc-wrapper: Use correct ruby interpreter
Previously, we hardcoded to using the ruby binary in /opt/vagrant[..].
On some systems, this path is incorrect, so instead we use the
path of the interpreter that is executing the `vagrant lxc sudoers`
command.
2015-03-19 16:22:00 -07:00
Cam Cope
5da3fc8be5 support containers with dhcp private networking 2015-03-08 19:58:29 -07:00
34 changed files with 858 additions and 892 deletions

1
.gitignore vendored
View file

@ -19,6 +19,7 @@ doc/
/tags /tags
/gems.tags /gems.tags
/Gemfile.lock
.vagrant .vagrant
/cache /cache

View file

@ -1,8 +1,10 @@
language: ruby language: ruby
rvm: rvm:
- 2.0.0 - 2.2
- 2.1.1 - 2.3
matrix: - 2.4
allow_failures: - 2.5
- rvm: 2.1.1 install:
- gem install -v 1.12.5 bundler
- bundle _1.12.5_ install --jobs=3 --retry=3
script: "bundle exec rake ci" script: "bundle exec rake ci"

View file

@ -1,4 +1,116 @@
## [1.1.0](https://github.com/fgrehm/vagrant-lxc/compare/v1.0.1...1.1.0) (Jan 14, 2015) ## [1.4.2](https://github.com/fgrehm/vagrant-lxc/compare/v1.4.1...v1.4.2) (Jul 17, 2018)
FIXES:
- Fix problems with `redir` 3.x command line. [[GH-467]]
## [1.4.1](https://github.com/fgrehm/vagrant-lxc/compare/v1.4.0...v1.4.1) (Apr 30, 2018)
FEATURES:
- Add support for LXC v3.0
- Add support for `redir` 3.x command line. [[GH-460]]
[GH-460]: https://github.com/fgrehm/vagrant-lxc/issues/460
## [1.4.0](https://github.com/fgrehm/vagrant-lxc/compare/v1.3.1...v1.4.0) (Mar 04, 2018)
FEATURES:
- Add support for unprivileged containers. [[GH-312]]
[GH-312]: https://github.com/fgrehm/vagrant-lxc/issues/312
## [1.3.1](https://github.com/fgrehm/vagrant-lxc/compare/v1.3.0...v1.3.1) (Fev 06, 2018)
FIXES:
- Fix problems with `tmpfs` fiddling in v1.3.0. [[GH-455]]
[GH-455]: https://github.com/fgrehm/vagrant-lxc/pull/455
## [1.3.0](https://github.com/fgrehm/vagrant-lxc/compare/v1.2.4...v1.3.0) (Jan 20, 2018)
FEATURES:
- lxc-template: make runnable by unprivileged users [[GH-447]]
- Use `lxc-info` instead of `lxc-attach` to retrieve container IP
- Add support for LXC v2.1+ [[GH-445]]
- Remove 2Gb limitation on `/tmp`. [[GH-406]]
OTHERS:
- Bump Vagrant requirements to v1.8+
- Bump LXC requirements to v1.0+
[GH-447]: https://github.com/fgrehm/vagrant-lxc/pull/447
[GH-445]: https://github.com/fgrehm/vagrant-lxc/pull/445
[GH-406]: https://github.com/fgrehm/vagrant-lxc/pull/406
## [1.2.4](https://github.com/fgrehm/vagrant-lxc/compare/v1.2.3...v1.2.4) (Dec 20, 2017)
BUGFIX:
- Support alternative `lxcpath` [[GH-413]]
- Update `pipework` regexp in sudo wrapper for Vagrant 1.9+ [[GH-438]]
- Work around restrictive `umask` values [[GH-435]]
- Make `--config` in `lxc-template` optional [[GH-421]]
- Fix sudo wrapper binpath construction logic [[GH-410]]
- Fix bug causing CTRL-C on `vagrant up` to destroy the VM [[GH-449]]
[GH-413]: https://github.com/fgrehm/vagrant-lxc/pull/413
[GH-438]: https://github.com/fgrehm/vagrant-lxc/pull/438
[GH-435]: https://github.com/fgrehm/vagrant-lxc/pull/435
[GH-421]: https://github.com/fgrehm/vagrant-lxc/pull/421
[GH-410]: https://github.com/fgrehm/vagrant-lxc/pull/410
[GH-449]: https://github.com/fgrehm/vagrant-lxc/pull/449
## [1.2.3](https://github.com/fgrehm/vagrant-lxc/compare/v1.2.2...v1.2.3) (Dec 20, 2016)
- Fix bug in Gemfile.lock
## [1.2.2](https://github.com/fgrehm/vagrant-lxc/compare/v1.2.1...v1.2.2) (Dec 20, 2016)
BUGFIX:
- Make the timeout for fetching container IP's configurable [[GH-426]]
- Load locale file only once [[GH-423]]
- Preserve xattrs in container filesystems [[GH-411]]
- Forward port latest pipework script [[GH-408]]
- Fix handling of non-fatal lxc-stop return code [[GH-405]]
[GH-426]: https://github.com/fgrehm/vagrant-lxc/pull/426
[GH-423]: https://github.com/fgrehm/vagrant-lxc/pull/423
[GH-411]: https://github.com/fgrehm/vagrant-lxc/pull/411
[GH-408]: https://github.com/fgrehm/vagrant-lxc/pull/408
[GH-405]: https://github.com/fgrehm/vagrant-lxc/pull/405
## [1.2.1](https://github.com/fgrehm/vagrant-lxc/compare/v1.2.0...v1.2.1) (Sep 24, 2015)
BUGFIX:
- Fix sudo Wrapper [[GH-393]]
[GH-393]: https://github.com/fgrehm/vagrant-lxc/pull/393
## [1.2.0](https://github.com/fgrehm/vagrant-lxc/compare/v1.1.0...v1.2.0) (Sep 15, 2015)
FEATURES:
- Support private networking using DHCP [[GH-352]]
[GH-352]: https://github.com/fgrehm/vagrant-lxc/pull/352
IMPROVEMENTS:
- Move mountpoint creation to lxc template for lvm rootfs support [[GH-361]] / [[GH-359]]
- Mount selinux sys dir read-only [[GH-357]] / [[GH-301]]
- Use correct ruby interpreter when generating sudoers file [[GH-355]]
- Fix shebangs to be more portable [[GH-376]]
- Fix removal of lxcbr0/virbr0 when using private networking [[GH-383]]
- Improve /tmp handling by using tmpfs [[GH-362]]
[GH-301]: https://github.com/fgrehm/vagrant-lxc/issues/301
[GH-355]: https://github.com/fgrehm/vagrant-lxc/pull/355
[GH-357]: https://github.com/fgrehm/vagrant-lxc/pull/357
[GH-359]: https://github.com/fgrehm/vagrant-lxc/issues/359
[GH-361]: https://github.com/fgrehm/vagrant-lxc/pull/361
[GH-376]: https://github.com/fgrehm/vagrant-lxc/pull/376
[GH-383]: https://github.com/fgrehm/vagrant-lxc/pull/383
[GH-362]: https://github.com/fgrehm/vagrant-lxc/pull/362
## [1.1.0](https://github.com/fgrehm/vagrant-lxc/compare/v1.0.1...v1.1.0) (Jan 14, 2015)
BACKWARDS INCOMPATIBILITIES: BACKWARDS INCOMPATIBILITIES:

View file

@ -1,16 +1,16 @@
source 'https://rubygems.org' source 'https://rubygems.org'
group :development do group :development do
gem 'vagrant', git: 'https://github.com/mitchellh/vagrant.git', tag: 'v1.7.2' gem 'vagrant', git: 'https://github.com/mitchellh/vagrant.git'
gem 'guard' gem 'guard'
gem 'guard-rspec' gem 'guard-rspec'
gem 'rb-inotify' gem 'rb-inotify'
end end
group :development, :test do group :development, :test do
gem 'rake' gem 'rake', '~> 10.4.2'
gem 'rspec', '2.99.0' gem 'rspec', '~> 3.5.0'
gem 'coveralls', require: (ENV['COVERAGE'] == 'true') gem 'coveralls', '~> 0.7.2', require: (ENV['COVERAGE'] == 'true')
gem 'vagrant-spec', git: 'https://github.com/mitchellh/vagrant-spec.git' gem 'vagrant-spec', git: 'https://github.com/mitchellh/vagrant-spec.git'
end end

View file

@ -1,197 +0,0 @@
GIT
remote: https://github.com/fgrehm/vagrant-cachier.git
revision: 9f6b615e84364b851939a8e7ee8229fc0d276c73
specs:
vagrant-cachier (1.1.0)
GIT
remote: https://github.com/fgrehm/vagrant-pristine.git
revision: 503dbc47848c81d0fbfa6840491856f518d244a1
specs:
vagrant-pristine (0.3.0)
GIT
remote: https://github.com/mitchellh/vagrant-spec.git
revision: 1df5a3af81cb7cce568b2eac52b8f6822bcb1d8e
specs:
vagrant-spec (0.0.1)
childprocess (~> 0.5.0)
log4r (~> 1.1.9)
rspec (~> 2.14)
thor (~> 0.18.1)
GIT
remote: https://github.com/mitchellh/vagrant.git
revision: 1cd667b243f4a263cd5322b6455165cc676b6f7f
tag: v1.7.2
specs:
vagrant (1.7.2)
bundler (>= 1.5.2, < 1.8.0)
childprocess (~> 0.5.0)
erubis (~> 2.7.0)
hashicorp-checkpoint (~> 0.1.1)
i18n (~> 0.6.0)
listen (~> 2.8.0)
log4r (~> 1.1.9, < 1.1.11)
net-scp (~> 1.1.0)
net-sftp (~> 2.1)
net-ssh (>= 2.6.6, < 2.10.0)
nokogiri (= 1.6.3.1)
rb-kqueue (~> 0.2.0)
rest-client (>= 1.6.0, < 2.0)
wdm (~> 0.1.0)
winrm (~> 1.1.3)
PATH
remote: .
specs:
vagrant-lxc (1.1.0)
GEM
remote: https://rubygems.org/
specs:
akami (1.2.2)
gyoku (>= 0.4.0)
nokogiri
builder (3.2.2)
celluloid (0.16.0)
timers (~> 4.0.0)
childprocess (0.5.5)
ffi (~> 1.0, >= 1.0.11)
coderay (1.1.0)
coveralls (0.7.1)
multi_json (~> 1.3)
rest-client
simplecov (>= 0.7)
term-ansicolor
thor
diff-lcs (1.2.5)
docile (1.1.5)
erubis (2.7.0)
ffi (1.9.6)
formatador (0.2.5)
gssapi (1.0.3)
ffi (>= 1.0.1)
guard (2.11.1)
formatador (>= 0.2.4)
listen (~> 2.7)
lumberjack (~> 1.0)
nenv (~> 0.1)
notiffany (~> 0.0)
pry (>= 0.9.12)
shellany (~> 0.0)
thor (>= 0.18.1)
guard-compat (1.2.0)
guard-rspec (4.5.0)
guard (~> 2.1)
guard-compat (~> 1.1)
rspec (>= 2.99.0, < 4.0)
gyoku (1.2.2)
builder (>= 2.1.2)
hashicorp-checkpoint (0.1.4)
hitimes (1.2.2)
httpclient (2.6.0.1)
httpi (0.9.7)
rack
i18n (0.6.11)
listen (2.8.5)
celluloid (>= 0.15.2)
rb-fsevent (>= 0.9.3)
rb-inotify (>= 0.9)
little-plugger (1.1.3)
log4r (1.1.10)
logging (1.8.2)
little-plugger (>= 1.1.3)
multi_json (>= 1.8.4)
lumberjack (1.0.9)
method_source (0.8.2)
mime-types (2.4.3)
mini_portile (0.6.0)
multi_json (1.10.1)
nenv (0.1.1)
net-scp (1.1.2)
net-ssh (>= 2.6.5)
net-sftp (2.1.2)
net-ssh (>= 2.6.5)
net-ssh (2.9.1)
netrc (0.10.2)
nokogiri (1.6.3.1)
mini_portile (= 0.6.0)
nori (1.1.5)
notiffany (0.0.2)
nenv (~> 0.1)
shellany (~> 0.0)
pry (0.10.1)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
rack (1.6.0)
rake (10.4.2)
rb-fsevent (0.9.4)
rb-inotify (0.9.5)
ffi (>= 0.5.0)
rb-kqueue (0.2.3)
ffi (>= 0.5.0)
rest-client (1.7.2)
mime-types (>= 1.16, < 3.0)
netrc (~> 0.7)
rspec (2.99.0)
rspec-core (~> 2.99.0)
rspec-expectations (~> 2.99.0)
rspec-mocks (~> 2.99.0)
rspec-core (2.99.2)
rspec-expectations (2.99.2)
diff-lcs (>= 1.1.3, < 2.0)
rspec-mocks (2.99.2)
rubyntlm (0.1.1)
savon (0.9.5)
akami (~> 1.0)
builder (>= 2.1.2)
gyoku (>= 0.4.0)
httpi (~> 0.9)
nokogiri (>= 1.4.0)
nori (~> 1.0)
wasabi (~> 1.0)
shellany (0.0.1)
simplecov (0.9.1)
docile (~> 1.1.0)
multi_json (~> 1.0)
simplecov-html (~> 0.8.0)
simplecov-html (0.8.0)
slop (3.6.0)
term-ansicolor (1.3.0)
tins (~> 1.0)
thor (0.18.1)
timers (4.0.1)
hitimes
tins (1.3.3)
uuidtools (2.1.5)
vagrant-omnibus (1.4.1)
wasabi (1.0.0)
nokogiri (>= 1.4.0)
wdm (0.1.0)
winrm (1.1.3)
gssapi (~> 1.0.0)
httpclient (~> 2.2, >= 2.2.0.2)
logging (~> 1.6, >= 1.6.1)
nokogiri (~> 1.5)
rubyntlm (~> 0.1.1)
savon (= 0.9.5)
uuidtools (~> 2.1.2)
PLATFORMS
ruby
DEPENDENCIES
coveralls
guard
guard-rspec
rake
rb-inotify
rspec (= 2.99.0)
vagrant!
vagrant-cachier!
vagrant-lxc!
vagrant-omnibus
vagrant-pristine!
vagrant-spec!

View file

@ -1,33 +1,34 @@
🟢 We plan to support and maintain vagrant-lxc, as well as clean it up.<br/>
🟢 Please feel free to contribute Issues and pull requests.<br/>
🟢 P.S: Thanks [Fabio Rehm](https://fabiorehm.com) for the amazing initial project.
# vagrant-lxc # vagrant-lxc
[![Build Status](https://travis-ci.org/fgrehm/vagrant-lxc.png?branch=master)](https://travis-ci.org/fgrehm/vagrant-lxc) [![Gem Version](https://badge.fury.io/rb/vagrant-lxc.png)](http://badge.fury.io/rb/vagrant-lxc) [![Code Climate](https://codeclimate.com/github/fgrehm/vagrant-lxc.png)](https://codeclimate.com/github/fgrehm/vagrant-lxc) [![Coverage Status](https://coveralls.io/repos/fgrehm/vagrant-lxc/badge.png?branch=master)](https://coveralls.io/r/fgrehm/vagrant-lxc) [![Gittip](http://img.shields.io/gittip/fgrehm.svg)](https://www.gittip.com/fgrehm/) [![Gitter chat](https://badges.gitter.im/fgrehm/vagrant-lxc.png)](https://gitter.im/fgrehm/vagrant-lxc) [![Build Status](https://travis-ci.org/fgrehm/vagrant-lxc.png?branch=master)](https://travis-ci.org/fgrehm/vagrant-lxc) [![Gem Version](https://badge.fury.io/rb/vagrant-lxc.png)](http://badge.fury.io/rb/vagrant-lxc) [![Code Climate](https://codeclimate.com/github/fgrehm/vagrant-lxc.png)](https://codeclimate.com/github/fgrehm/vagrant-lxc) [![Coverage Status](https://coveralls.io/repos/fgrehm/vagrant-lxc/badge.png?branch=master)](https://coveralls.io/r/fgrehm/vagrant-lxc) [![Gitter chat](https://badges.gitter.im/fgrehm/vagrant-lxc.png)](https://gitter.im/fgrehm/vagrant-lxc)
[LXC](http://lxc.sourceforge.net/) provider for [Vagrant](http://www.vagrantup.com/) 1.6+ [LXC](http://lxc.sourceforge.net/) provider for [Vagrant](http://www.vagrantup.com/) 1.9+
This is a Vagrant plugin that allows it to control and provision Linux Containers This is a Vagrant plugin that allows it to control and provision Linux Containers
as an alternative to the built in VirtualBox provider for Linux hosts. Check out as an alternative to the built in VirtualBox provider for Linux hosts. Check out
[this blog post](http://fabiorehm.com/blog/2013/04/28/lxc-provider-for-vagrant/) [this blog post](http://fabiorehm.com/blog/2013/04/28/lxc-provider-for-vagrant/)
to see it in action. to see it in action.
## Features ## Features
* Provides the same workflow as the Vagrant VirtualBox provider * Provides the same workflow as the Vagrant VirtualBox provider
* Port forwarding via [`redir`](http://linux.die.net/man/1/redir) * Port forwarding via [`redir`](https://github.com/troglobit/redir)
* Private networking via [`pipework`](https://github.com/jpetazzo/pipework) * Private networking via [`pipework`](https://github.com/jpetazzo/pipework)
## Requirements ## Requirements
* [Vagrant 1.5+](http://www.vagrantup.com/downloads.html) (tested with 1.7.2) * [Vagrant 1.9+](http://www.vagrantup.com/downloads.html)
* lxc 0.7.5+ * lxc >=2.1
* `redir` (if you are planning to use port forwarding) * `redir` (if you are planning to use port forwarding)
* `brctl` (if you are planning to use private networks, on Ubuntu this means `apt-get install bridge-utils`) * `brctl` (if you are planning to use private networks, on Ubuntu this means `apt-get install bridge-utils`)
* A [kernel != 3.5.0-17.28](https://github.com/fgrehm/vagrant-lxc/wiki/Troubleshooting#wiki-im-unable-to-restart-containers)
The plugin is known to work better and pretty much out of the box on Ubuntu 14.04+ The plugin is known to work better and pretty much out of the box on Ubuntu 14.04+
hosts and installing the dependencies on it basically means a `apt-get install lxc lxc-templates cgroup-lite redir` hosts and installing the dependencies on it basically means a
(older LXC versions like 0.7.5 shipped with Ubuntu 12.04 by default might require `apt-get install lxc lxc-templates cgroup-lite redir`. For setting up other
[additional configurations to work](#backingstore-options)). For setting up other
types of hosts please have a look at the [Wiki](https://github.com/fgrehm/vagrant-lxc/wiki). types of hosts please have a look at the [Wiki](https://github.com/fgrehm/vagrant-lxc/wiki).
If you are on a Mac or Windows machine, you might want to have a look at [this](http://the.taoofmac.com/space/HOWTO/Vagrant) If you are on a Mac or Windows machine, you might want to have a look at [this](http://the.taoofmac.com/space/HOWTO/Vagrant)
@ -54,8 +55,9 @@ _More information about skipping the `--provider` argument can be found at the
## Base boxes ## Base boxes
Base boxes can be found on [Atlas](https://atlas.hashicorp.com/boxes/search?provider=lxc) Base boxes provided on Atlas haven't been refreshed for a good while and shouldn't be relied on.
and some scripts to build your own are available at [fgrehm/vagrant-lxc-base-boxes](https://github.com/fgrehm/vagrant-lxc-base-boxes). Your best best is to build your boxes yourself. Some scripts to build your own are available at
[hsoft/vagrant-lxc-base-boxes](https://github.com/hsoft/vagrant-lxc-base-boxes).
If you want to build your own boxes, please have a look at [`BOXES.md`](https://github.com/fgrehm/vagrant-lxc/tree/master/BOXES.md) If you want to build your own boxes, please have a look at [`BOXES.md`](https://github.com/fgrehm/vagrant-lxc/tree/master/BOXES.md)
for more information. for more information.
@ -81,7 +83,7 @@ prior to starting it.
For other configuration options, please check the [lxc.conf manpages](http://manpages.ubuntu.com/manpages/precise/man5/lxc.conf.5.html). For other configuration options, please check the [lxc.conf manpages](http://manpages.ubuntu.com/manpages/precise/man5/lxc.conf.5.html).
### Private Networks [EXPERIMENTAL] ### Private Networks
Starting with vagrant-lxc 1.1.0, there is some rudimentary support for configuring Starting with vagrant-lxc 1.1.0, there is some rudimentary support for configuring
[Private Networks](https://docs.vagrantup.com/v2/networking/private_network.html) [Private Networks](https://docs.vagrantup.com/v2/networking/private_network.html)
@ -131,7 +133,7 @@ specified from the provider block and it defaults to `best`, to change it:
```ruby ```ruby
Vagrant.configure("2") do |config| Vagrant.configure("2") do |config|
config.vm.provider :lxc do |lxc| config.vm.provider :lxc do |lxc|
lxc.backingstore = 'lvm' # or 'btrfs',... lxc.backingstore = 'lvm' # or 'btrfs', 'overlayfs', ...
# lvm specific options # lvm specific options
lxc.backingstore_option '--vgname', 'schroots' lxc.backingstore_option '--vgname', 'schroots'
lxc.backingstore_option '--fssize', '5G' lxc.backingstore_option '--fssize', '5G'
@ -140,31 +142,33 @@ Vagrant.configure("2") do |config|
end end
``` ```
For old versions of lxc (like 0.7.5 shipped with Ubuntu 12.04 by default) that ## Unprivileged containers support
does not support `best` for the backingstore option, changing it to `none` is
required and a default for all Vagrant environments can be set from your Since v1.4.0, `vagrant-lxc` gained support for unprivileged containers. For now, since it's a new
`~/.vagrant.d/Vagrantfile` using the same `provider` block: feature, privileged containers are still the default, but you can have your `Vagrantfile` use
unprivileged containers with the `privileged` flag (which defaults to `true`). Example:
```ruby ```ruby
Vagrant.configure("2") do |config| Vagrant.configure("2") do |config|
config.vm.provider :lxc do |lxc| config.vm.provider :lxc do |lxc|
lxc.backingstore = 'none' lxc.privileged = false
end end
end end
``` ```
For unprivileged containers to work with `vagrant-lxc`, you need a properly configured system. On
some distros, it can be somewhat of a challenge. Your journey to configuring your system can start
with [Stéphane Graber's blog post about it](https://stgraber.org/2014/01/17/lxc-1-0-unprivileged-containers/).
## Avoiding `sudo` passwords ## Avoiding `sudo` passwords
This plugin requires **a lot** of `sudo`ing since [user namespaces](https://wiki.ubuntu.com/UserNamespace) If you're not using unprivileged containers, this plugin requires **a lot** of `sudo`ing To work
is not supported yet. To work around that, you can use the `vagrant lxc sudoers` around that, you can use the `vagrant lxc sudoers` command which will create a file under
command which will create a file under `/etc/sudoers.d/vagrant-lxc` whitelisting `/etc/sudoers.d/vagrant-lxc` whitelisting all commands required by `vagrant-lxc` to run.
all commands required by `vagrant-lxc` to run.
If you are interested on what will be generated by that command, please check If you are interested on what will be generated by that command, please check
[this code](lib/vagrant-lxc/command/sudoers.rb). [this code](lib/vagrant-lxc/command/sudoers.rb).
_vagrant-lxc < 1.0.0 users, please check this [Wiki page](https://github.com/fgrehm/vagrant-lxc/wiki/Avoiding-%27sudo%27-passwords)_
## More information ## More information

View file

@ -6,9 +6,5 @@ module Vagrant
def self.source_root def self.source_root
@source_root ||= Pathname.new(File.dirname(__FILE__)).join('..').expand_path @source_root ||= Pathname.new(File.dirname(__FILE__)).join('..').expand_path
end end
def self.sudo_wrapper_path
"/usr/local/bin/vagrant-lxc-wrapper"
end
end end
end end

View file

@ -4,8 +4,7 @@ require 'vagrant-lxc/action/create'
require 'vagrant-lxc/action/destroy' require 'vagrant-lxc/action/destroy'
require 'vagrant-lxc/action/destroy_confirm' require 'vagrant-lxc/action/destroy_confirm'
require 'vagrant-lxc/action/compress_rootfs' require 'vagrant-lxc/action/compress_rootfs'
require 'vagrant-lxc/action/fetch_ip_with_lxc_attach' require 'vagrant-lxc/action/fetch_ip_with_lxc_info'
require 'vagrant-lxc/action/fetch_ip_from_dnsmasq_leases'
require 'vagrant-lxc/action/forced_halt' require 'vagrant-lxc/action/forced_halt'
require 'vagrant-lxc/action/forward_ports' require 'vagrant-lxc/action/forward_ports'
require 'vagrant-lxc/action/gc_private_network_bridges' require 'vagrant-lxc/action/gc_private_network_bridges'
@ -13,7 +12,6 @@ require 'vagrant-lxc/action/handle_box_metadata'
require 'vagrant-lxc/action/prepare_nfs_settings' require 'vagrant-lxc/action/prepare_nfs_settings'
require 'vagrant-lxc/action/prepare_nfs_valid_ids' require 'vagrant-lxc/action/prepare_nfs_valid_ids'
require 'vagrant-lxc/action/private_networks' require 'vagrant-lxc/action/private_networks'
require 'vagrant-lxc/action/remove_temporary_files'
require 'vagrant-lxc/action/setup_package_files' require 'vagrant-lxc/action/setup_package_files'
require 'vagrant-lxc/action/warn_networks' require 'vagrant-lxc/action/warn_networks'
@ -126,7 +124,6 @@ module Vagrant
end end
b2.use ClearForwardedPorts b2.use ClearForwardedPorts
b2.use RemoveTemporaryFiles
b2.use GcPrivateNetworkBridges b2.use GcPrivateNetworkBridges
b2.use Builtin::Call, Builtin::GracefulHalt, :stopped, :running do |env2, b3| b2.use Builtin::Call, Builtin::GracefulHalt, :stopped, :running do |env2, b3|
if !env2[:result] if !env2[:result]
@ -184,8 +181,7 @@ module Vagrant
def self.action_ssh_ip def self.action_ssh_ip
Builder.new.tap do |b| Builder.new.tap do |b|
b.use Builtin::Call, Builtin::ConfigValidate do |env, b2| b.use Builtin::Call, Builtin::ConfigValidate do |env, b2|
b2.use FetchIpWithLxcAttach if env[:machine].provider.driver.supports_attach? b2.use FetchIpWithLxcInfo
b2.use FetchIpFromDnsmasqLeases
end end
end end
end end

View file

@ -8,11 +8,15 @@ module Vagrant
def call(env) def call(env)
@env = env @env = env
driver = env[:machine].provider.driver
config = env[:machine].provider_config config = env[:machine].provider_config
utsname = env[:machine].config.vm.hostname || env[:machine].id 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 # Fix apparmor issues when starting Ubuntu 14.04 containers
# See https://github.com/fgrehm/vagrant-lxc/issues/278 for more information # See https://github.com/fgrehm/vagrant-lxc/issues/278 for more information
@ -20,8 +24,19 @@ module Vagrant
config.customize 'mount.entry', '/sys/fs/pstore sys/fs/pstore none bind,optional 0 0' config.customize 'mount.entry', '/sys/fs/pstore sys/fs/pstore none bind,optional 0 0'
end end
# Make selinux read-only, see
# https://github.com/fgrehm/vagrant-lxc/issues/301
if Dir.exists?('/sys/fs/selinux')
config.customize 'mount.entry', '/sys/fs/selinux sys/fs/selinux none bind,ro 0 0'
end
if config.tmpfs_mount_size && !config.tmpfs_mount_size.empty?
# Make /tmp a tmpfs to prevent init scripts from nuking synced folders mounted in here
config.customize 'mount.entry', "tmpfs tmp tmpfs nodev,nosuid,size=#{config.tmpfs_mount_size} 0 0"
end
env[:ui].info I18n.t("vagrant_lxc.messages.starting") env[:ui].info I18n.t("vagrant_lxc.messages.starting")
env[:machine].provider.driver.start(config.customizations) driver.start(config.customizations)
@app.call env @app.call env
end end

View file

@ -19,14 +19,28 @@ module Vagrant
container_name = generate_container_name(env) container_name = generate_container_name(env)
end end
env[:machine].provider.driver.create( backingstore = config.backingstore
if backingstore.nil?
backingstore = config.privileged ? "best" : "dir"
end
driver = env[:machine].provider.driver
template_options = env[:lxc_template_opts]
if driver.supports_new_config_format
if env[:lxc_box_config]
driver.update_config_keys(env[:lxc_box_config])
end
else
template_options['--oldconfig'] = ''
end
driver.create(
container_name, container_name,
config.backingstore, backingstore,
config.backingstore_options, config.backingstore_options,
env[:lxc_template_src], env[:lxc_template_src],
env[:lxc_template_config], env[:lxc_template_config],
env[:lxc_template_opts] template_options
) )
driver.update_config_keys
env[:machine].id = container_name env[:machine].id = container_name

View file

@ -1,49 +0,0 @@
module Vagrant
module LXC
module Action
class FetchIpFromDnsmasqLeases
def initialize(app, env)
@app = app
@logger = Log4r::Logger.new("vagrant::lxc::action::fetch_ip_from_dnsmasq_leases")
end
def call(env)
env[:machine_ip] ||= assigned_ip(env)
@app.call(env)
end
def assigned_ip(env)
mac_address = env[:machine].provider.driver.mac_address
ip = nil
10.times do
dnsmasq_leases = read_dnsmasq_leases
@logger.debug "Attempting to load ip from dnsmasq leases (mac: #{mac_address})"
@logger.debug dnsmasq_leases
if dnsmasq_leases =~ /#{Regexp.escape mac_address.to_s}\s+([0-9.]+)\s+/i
ip = $1.to_s
break
else
@logger.debug 'Ip could not be parsed from dnsmasq leases file'
sleep 2
end
end
ip
end
LEASES_PATHS = %w(
/var/lib/misc/dnsmasq.*.leases
/var/lib/misc/dnsmasq.leases
/var/lib/dnsmasq/dnsmasq.leases
/var/db/dnsmasq.leases
/var/lib/libvirt/dnsmasq/*.leases
)
def read_dnsmasq_leases
Dir["{#{LEASES_PATHS.join(',')}}"].map do |file|
File.read(file)
end.join("\n")
end
end
end
end
end

View file

@ -1,30 +1,31 @@
module Vagrant module Vagrant
module LXC module LXC
module Action module Action
class FetchIpWithLxcAttach class FetchIpWithLxcInfo
# Include this so we can use `Subprocess` more easily. # Include this so we can use `Subprocess` more easily.
include Vagrant::Util::Retryable include Vagrant::Util::Retryable
def initialize(app, env) def initialize(app, env)
@app = app @app = app
@logger = Log4r::Logger.new("vagrant::lxc::action::fetch_ip_with_lxc_attach") @logger = Log4r::Logger.new("vagrant::lxc::action::fetch_ip_with_lxc_info")
end end
def call(env) def call(env)
env[:machine_ip] ||= assigned_ip(env) env[:machine_ip] ||= assigned_ip(env)
rescue LXC::Errors::NamespacesNotSupported
@logger.info 'The `lxc-attach` command available does not support the --namespaces parameter, falling back to dnsmasq leases to fetch container ip'
ensure ensure
@app.call(env) @app.call(env)
end end
def assigned_ip(env) def assigned_ip(env)
config = env[:machine].provider_config
fetch_ip_tries = config.fetch_ip_tries
driver = env[:machine].provider.driver driver = env[:machine].provider.driver
ip = '' ip = ''
retryable(:on => LXC::Errors::ExecuteError, :tries => 10, :sleep => 3) do return config.ssh_ip_addr if not config.ssh_ip_addr.nil?
retryable(:on => LXC::Errors::ExecuteError, :tries => fetch_ip_tries, :sleep => 3) do
unless ip = get_container_ip_from_ip_addr(driver) unless ip = get_container_ip_from_ip_addr(driver)
# retry # retry
raise LXC::Errors::ExecuteError, :command => "lxc-attach" raise LXC::Errors::ExecuteError, :command => "lxc-info"
end end
end end
ip ip
@ -32,8 +33,8 @@ module Vagrant
# From: https://github.com/lxc/lxc/blob/staging/src/python-lxc/lxc/__init__.py#L371-L385 # From: https://github.com/lxc/lxc/blob/staging/src/python-lxc/lxc/__init__.py#L371-L385
def get_container_ip_from_ip_addr(driver) def get_container_ip_from_ip_addr(driver)
output = driver.attach '/sbin/ip', '-4', 'addr', 'show', 'scope', 'global', 'eth0', namespaces: ['network', 'mount'] output = driver.info '-iH'
if output =~ /^\s+inet ([0-9.]+)\/[0-9]+\s+/ if output =~ /^([0-9.]+)/
return $1.to_s return $1.to_s
end end
end end

View file

@ -1,3 +1,5 @@
require 'open3'
module Vagrant module Vagrant
module LXC module LXC
module Action module Action
@ -67,7 +69,9 @@ module Vagrant
# TODO: Deprecate this behavior of "automagically" skipping ssh forwarded ports # TODO: Deprecate this behavior of "automagically" skipping ssh forwarded ports
if type == :forwarded_port && options[:id] != 'ssh' if type == :forwarded_port && options[:id] != 'ssh'
options.delete(:host_ip) if options.fetch(:host_ip, '').to_s.strip.empty? if options.fetch(:host_ip, '').to_s.strip.empty?
options[:host_ip] = '127.0.0.1'
end
mappings[options[:host]] = options mappings[options[:host]] = options
end end
end end
@ -76,8 +80,12 @@ module Vagrant
end end
def redirect_port(host_ip, host_port, guest_ip, guest_port) def redirect_port(host_ip, host_port, guest_ip, guest_port)
params = %W( --lport=#{host_port} --caddr=#{guest_ip} --cport=#{guest_port} ) if redir_version >= 3
params.unshift "--laddr=#{host_ip}" if host_ip params = %W( -n #{host_ip}:#{host_port} #{guest_ip}:#{guest_port} )
else
params = %W( --lport=#{host_port} --caddr=#{guest_ip} --cport=#{guest_port} )
params.unshift "--laddr=#{host_ip}" if host_ip
end
params << '--syslog' if ENV['REDIR_LOG'] params << '--syslog' if ENV['REDIR_LOG']
if host_port < 1024 if host_port < 1024
redir_cmd = "sudo redir #{params.join(' ')} 2>/dev/null" redir_cmd = "sudo redir #{params.join(' ')} 2>/dev/null"
@ -97,6 +105,13 @@ module Vagrant
end end
end end
def redir_version
stdout, stderr, _ = Open3.capture3 "redir --version"
# For some weird reason redir printed version information in STDERR prior to 3.2
version = stdout.empty? ? stderr : stdout
version.split('.')[0].to_i
end
def redir_installed? def redir_installed?
system "which redir > /dev/null" system "which redir > /dev/null"
end end

View file

@ -35,8 +35,9 @@ module Vagrant
if ! driver.bridge_is_in_use?(bridge) if ! driver.bridge_is_in_use?(bridge)
env[:ui].info I18n.t("vagrant_lxc.messages.remove_bridge", name: bridge) env[:ui].info I18n.t("vagrant_lxc.messages.remove_bridge", name: bridge)
# TODO: Output that bridge is being removed unless ['lxcbr0', 'virbr0'].include? bridge
driver.remove_bridge(bridge) driver.remove_bridge(bridge)
end
end end
end end
end end

View file

@ -33,8 +33,10 @@ module Vagrant
end end
if template_config_file.exist? if template_config_file.exist?
@env[:lxc_box_config] = template_config_file.to_s
@env[:lxc_template_opts].merge!('--config' => template_config_file.to_s) @env[:lxc_template_opts].merge!('--config' => template_config_file.to_s)
elsif old_template_config_file.exist? elsif old_template_config_file.exist?
@env[:lxc_box_config] = old_template_config_file.to_s
@env[:lxc_template_config] = old_template_config_file.to_s @env[:lxc_template_config] = old_template_config_file.to_s
end end

View file

@ -26,16 +26,19 @@ module Vagrant
next if type.to_sym != :private_network next if type.to_sym != :private_network
container_name = env[:machine].provider.driver.container_name container_name = env[:machine].provider.driver.container_name
address_type = config[:type]
ip = config[:ip] ip = config[:ip]
bridge_ip = config.fetch(:lxc__bridge_ip) { build_bridge_ip(ip) } bridge_ip = config.fetch(:lxc__bridge_ip) { build_bridge_ip(ip) }
bridge = config.fetch(:lxc__bridge_name) bridge = config.fetch(:lxc__bridge_name)
env[:machine].provider.driver.configure_private_network(bridge, bridge_ip, container_name, ip) env[:machine].provider.driver.configure_private_network(bridge, bridge_ip, container_name, address_type, ip)
end end
end end
def build_bridge_ip(ip) def build_bridge_ip(ip)
ip.sub(/^(\d+\.\d+\.\d+)\.\d+/, '\1.254') if ip
ip.sub(/^(\d+\.\d+\.\d+)\.\d+/, '\1.254')
end
end end
end end
end end

View file

@ -1,23 +0,0 @@
module Vagrant
module LXC
module Action
class RemoveTemporaryFiles
def initialize(app, env)
@app = app
@logger = Log4r::Logger.new("vagrant::lxc::action::remove_tmp_files")
end
def call(env)
# Continue execution, we need the container to be stopped
@app.call env
if env[:machine].state.id == :stopped
@logger.debug 'Removing temporary files'
tmp_path = env[:machine].provider.driver.rootfs_path.join('tmp')
env[:machine].provider.sudo_wrapper.run('rm', '-rf', "#{tmp_path}/*")
end
end
end
end
end
end

View file

@ -1,5 +1,8 @@
require 'tempfile' require 'tempfile'
require "vagrant-lxc/driver"
require "vagrant-lxc/sudo_wrapper"
module Vagrant module Vagrant
module LXC module LXC
module Command module Command
@ -25,7 +28,7 @@ module Vagrant
argv = parse_options(opts) argv = parse_options(opts)
return unless argv return unless argv
wrapper_path = Vagrant::LXC.sudo_wrapper_path wrapper_path = SudoWrapper.dest_path
wrapper = create_wrapper! wrapper = create_wrapper!
sudoers = create_sudoers!(options[:user], wrapper_path) sudoers = create_sudoers!(options[:user], wrapper_path)
@ -43,12 +46,14 @@ module Vagrant
# This requires vagrant 1.5.2+ https://github.com/mitchellh/vagrant/commit/3371c3716278071680af9b526ba19235c79c64cb # This requires vagrant 1.5.2+ https://github.com/mitchellh/vagrant/commit/3371c3716278071680af9b526ba19235c79c64cb
def create_wrapper! def create_wrapper!
lxc_base_path = Driver.new("").containers_path
wrapper = Tempfile.new('lxc-wrapper').tap do |file| wrapper = Tempfile.new('lxc-wrapper').tap do |file|
template = Vagrant::Util::TemplateRenderer.new( template = Vagrant::Util::TemplateRenderer.new(
'sudoers.rb', 'sudoers.rb',
:template_root => Vagrant::LXC.source_root.join('templates').to_s, :template_root => Vagrant::LXC.source_root.join('templates').to_s,
:cmd_paths => build_cmd_paths_hash, :cmd_paths => build_cmd_paths_hash,
:pipework_regex => "#{ENV['HOME']}/\.vagrant\.d/gems/gems/vagrant-lxc.+/scripts/pipework" :lxc_base_path => lxc_base_path,
:pipework_regex => "#{ENV['HOME']}/\.vagrant\.d/gems/(?:\\d+?\\.\\d+?\\.\\d+?/)?gems/vagrant-lxc.+/scripts/pipework"
) )
file.puts template.render file.puts template.render
end end
@ -80,9 +85,10 @@ module Vagrant
def build_cmd_paths_hash def build_cmd_paths_hash
{}.tap do |hash| {}.tap do |hash|
%w( which cat mkdir cp chown chmod rm tar chown ip ifconfig brctl ).each do |cmd| %w( which cat mkdir cp chown chmod rm tar chown ip ifconfig brctl ).each do |cmd|
hash[cmd] = `which #{cmd}`.strip hash[cmd] = `sudo which #{cmd}`.strip
end end
hash['lxc_bin'] = Pathname(`which lxc-create`.strip).parent.to_s hash['lxc_bin'] = Pathname(`sudo which lxc-create`.strip).parent.to_s
hash['ruby'] = Gem.ruby
end end
end end
end end

View file

@ -18,12 +18,29 @@ module Vagrant
# machine name, set this to :machine # machine name, set this to :machine
attr_accessor :container_name attr_accessor :container_name
# Size (as a string like '400M') of the tmpfs to mount at /tmp on boot.
# Set to false or nil to disable the tmpfs mount altogether. Defaults to '2G'.
attr_accessor :tmpfs_mount_size
attr_accessor :fetch_ip_tries
attr_accessor :ssh_ip_addr
# Whether the container needs to be privileged. Defaults to true (unprivileged containers
# is a very new feature in vagrant-lxc). If false, will try creating an unprivileged
# container. If it can't, will revert to the old "sudo wrapper" method to create a privileged
# container.
attr_accessor :privileged
def initialize def initialize
@customizations = [] @customizations = []
@backingstore = UNSET_VALUE @backingstore = UNSET_VALUE
@backingstore_options = [] @backingstore_options = []
@sudo_wrapper = UNSET_VALUE
@container_name = UNSET_VALUE @container_name = UNSET_VALUE
@tmpfs_mount_size = UNSET_VALUE
@fetch_ip_tries = UNSET_VALUE
@ssh_ip_addr = UNSET_VALUE
@privileged = UNSET_VALUE
end end
# Customize the container by calling `lxc-start` with the given # Customize the container by calling `lxc-start` with the given
@ -47,10 +64,13 @@ module Vagrant
end end
def finalize! def finalize!
@sudo_wrapper = nil if @sudo_wrapper == UNSET_VALUE
@container_name = nil if @container_name == UNSET_VALUE @container_name = nil if @container_name == UNSET_VALUE
@backingstore = "best" if @backingstore == UNSET_VALUE @backingstore = nil if @backingstore == UNSET_VALUE
@existing_container_name = nil if @existing_container_name == UNSET_VALUE @existing_container_name = nil if @existing_container_name == UNSET_VALUE
@tmpfs_mount_size = '2G' if @tmpfs_mount_size == UNSET_VALUE
@fetch_ip_tries = 10 if @fetch_ip_tries == UNSET_VALUE
@ssh_ip_addr = nil if @ssh_ip_addr == UNSET_VALUE
@privileged = true if @privileged == UNSET_VALUE
end end
end end
end end

View file

@ -3,6 +3,7 @@ require "vagrant/util/subprocess"
require "vagrant-lxc/errors" require "vagrant-lxc/errors"
require "vagrant-lxc/driver/cli" require "vagrant-lxc/driver/cli"
require "vagrant-lxc/sudo_wrapper"
require "etc" require "etc"
@ -16,15 +17,13 @@ module Vagrant
class ContainerNotFound < StandardError; end class ContainerNotFound < StandardError; end
# Default root folder where container configs are stored # Default root folder where container configs are stored
DEFAULT_CONTAINERS_PATH = '/var/lib/lxc'
attr_reader :container_name, attr_reader :container_name,
:customizations :customizations
def initialize(container_name, sudo_wrapper, cli = nil) def initialize(container_name, sudo_wrapper = nil, cli = nil, privileged: true)
@container_name = container_name @container_name = container_name
@sudo_wrapper = sudo_wrapper @sudo_wrapper = sudo_wrapper || SudoWrapper.new(privileged: privileged)
@cli = cli || CLI.new(sudo_wrapper, container_name) @cli = cli || CLI.new(@sudo_wrapper, container_name)
@logger = Log4r::Logger.new("vagrant::provider::lxc::driver") @logger = Log4r::Logger.new("vagrant::provider::lxc::driver")
@customizations = [] @customizations = []
end end
@ -35,7 +34,7 @@ module Vagrant
# Root folder where container configs are stored # Root folder where container configs are stored
def containers_path def containers_path
@containers_path ||= @cli.support_config_command? ? @cli.config('lxc.lxcpath') : DEFAULT_CONTAINERS_PATH @containers_path ||= @cli.config('lxc.lxcpath')
end end
def all_containers def all_containers
@ -46,21 +45,25 @@ module Vagrant
Pathname.new("#{containers_path}/#{@container_name}") Pathname.new("#{containers_path}/#{@container_name}")
end end
def config_path
base_path.join('config').to_s
end
def rootfs_path def rootfs_path
config_entry = config_string.match(/^lxc\.rootfs\s+=\s+(.+)$/)[1] pathtype, path = config_string.match(/^lxc\.rootfs(?:\.path)?\s+=\s+(.+:)?(.+)$/)[1..2]
case config_entry case pathtype
when /^overlayfs:/ when 'overlayfs:'
# Split on colon (:), ignoring any colon escaped by an escape character ( \ ) # Split on colon (:), ignoring any colon escaped by an escape character ( \ )
# Pays attention to when the escape character is itself escaped. # Pays attention to when the escape character is itself escaped.
fs_type, master_path, overlay_path = config_entry.split(/(?<!\\)(?:\\\\)*:/) _, overlay_path = config_entry.split(/(?<!\\)(?:\\\\)*:/)
if overlay_path if overlay_path
Pathname.new(overlay_path) Pathname.new(overlay_path)
else else
# Malformed: fall back to prior behaviour # Malformed: fall back to prior behaviour
Pathname.new(config_entry) Pathname.new(path)
end end
else else
Pathname.new(config_entry) Pathname.new(path)
end end
end end
@ -73,40 +76,27 @@ module Vagrant
end end
def config_string def config_string
@sudo_wrapper.run('cat', base_path.join('config').to_s) @sudo_wrapper.run('cat', config_path)
end end
def create(name, backingstore, backingstore_options, template_path, config_file, template_options = {}) def create(name, backingstore, backingstore_options, template_path, config_file, template_options = {})
@cli.name = @container_name = name @cli.name = @container_name = name
import_template(template_path) do |template_name| @logger.debug "Creating container..."
@logger.debug "Creating container..."
@cli.create template_name, backingstore, backingstore_options, config_file, template_options @cli.create template_path, backingstore, backingstore_options, config_file, template_options
end
end end
def share_folders(folders) def share_folders(folders)
folders.each do |f| folders.each do |f|
share_folder(f[:hostpath], f[:guestpath], f.fetch(:mount_options, 'bind')) share_folder(f[:hostpath], f[:guestpath], f.fetch(:mount_options, nil))
end end
end end
def share_folder(host_path, guest_path, mount_options = nil) def share_folder(host_path, guest_path, mount_options = nil)
guest_path = guest_path.gsub(/^\//, '') guest_path = guest_path.gsub(/^\//, '').gsub(' ', '\\\040')
guest_full_path = rootfs_path.join(guest_path) mount_options = Array(mount_options || ['bind', 'create=dir'])
unless guest_full_path.directory?
begin
@logger.debug("Guest path doesn't exist, creating: #{guest_full_path}")
@sudo_wrapper.run('mkdir', '-p', guest_full_path.to_s)
rescue Errno::EACCES
raise Vagrant::Errors::SharedFolderCreateFailed, :path => guest_path.to_s
end
end
mount_options = Array(mount_options || ['bind'])
host_path = host_path.to_s.gsub(' ', '\\\040') host_path = host_path.to_s.gsub(' ', '\\\040')
guest_path = guest_path.gsub(' ', '\\\040')
@customizations << ['mount.entry', "#{host_path} #{guest_path} none #{mount_options.join(',')} 0 0"] @customizations << ['mount.entry', "#{host_path} #{guest_path} none #{mount_options.join(',')} 0 0"]
end end
@ -132,25 +122,38 @@ module Vagrant
@cli.destroy @cli.destroy
end end
def supports_attach?
@cli.supports_attach?
end
def attach(*command) def attach(*command)
@cli.attach(*command) @cli.attach(*command)
end end
def configure_private_network(bridge_name, bridge_ip, container_name, ip) def info(*command)
@cli.info(*command)
end
def configure_private_network(bridge_name, bridge_ip, container_name, address_type, ip)
@logger.info "Configuring network interface for #{container_name} using #{ip} and bridge #{bridge_name}" @logger.info "Configuring network interface for #{container_name} using #{ip} and bridge #{bridge_name}"
cmd = [ if ip
Vagrant::LXC.source_root.join('scripts/pipework').to_s, ip += '/24'
bridge_name, end
container_name,
"#{ip}/24" if ! bridge_exists?(bridge_name)
] if not bridge_ip
@sudo_wrapper.run(*cmd) raise "Bridge is missing and no IP was specified!"
end
@logger.info "Creating the bridge #{bridge_name}"
cmd = [
'brctl',
'addbr',
bridge_name
]
@sudo_wrapper.run(*cmd)
end
if ! bridge_has_an_ip?(bridge_name) if ! bridge_has_an_ip?(bridge_name)
if not bridge_ip
raise "Bridge has no IP and none was specified!"
end
@logger.info "Adding #{bridge_ip} to the bridge #{bridge_name}" @logger.info "Adding #{bridge_ip} to the bridge #{bridge_name}"
cmd = [ cmd = [
'ip', 'ip',
@ -161,7 +164,16 @@ module Vagrant
bridge_name bridge_name
] ]
@sudo_wrapper.run(*cmd) @sudo_wrapper.run(*cmd)
@sudo_wrapper.run('ip', 'link', 'set', bridge_name, 'up')
end end
cmd = [
Vagrant::LXC.source_root.join('scripts/pipework').to_s,
bridge_name,
container_name,
ip ||= "dhcp"
]
@sudo_wrapper.run(*cmd)
end end
def bridge_has_an_ip?(bridge_name) def bridge_has_an_ip?(bridge_name)
@ -169,6 +181,12 @@ module Vagrant
`ip -4 addr show scope global #{bridge_name}` =~ /^\s+inet ([0-9.]+)\/[0-9]+\s+/ `ip -4 addr show scope global #{bridge_name}` =~ /^\s+inet ([0-9.]+)\/[0-9]+\s+/
end end
def bridge_exists?(bridge_name)
@logger.info "Checking whether bridge #{bridge_name} exists"
brctl_output = `ip link | egrep -q " #{bridge_name}:"`
$?.to_i == 0
end
def bridge_is_in_use?(bridge_name) def bridge_is_in_use?(bridge_name)
# REFACTOR: This method is **VERY** hacky # REFACTOR: This method is **VERY** hacky
@logger.info "Checking if bridge #{bridge_name} is in use" @logger.info "Checking if bridge #{bridge_name} is in use"
@ -177,12 +195,15 @@ module Vagrant
end end
def remove_bridge(bridge_name) def remove_bridge(bridge_name)
@logger.info "Checking whether bridge #{bridge_name} exists" if ['lxcbr0', 'virbr0'].include? bridge_name
brctl_output = `ifconfig -a | grep -q #{bridge_name}` @logger.info "Skipping removal of system bridge #{bridge_name}"
return if $?.to_i != 0 return
end
return unless bridge_exists?(bridge_name)
@logger.info "Removing bridge #{bridge_name}" @logger.info "Removing bridge #{bridge_name}"
@sudo_wrapper.run('ifconfig', bridge_name, 'down') @sudo_wrapper.run('ip', 'link', 'set', bridge_name, 'down')
@sudo_wrapper.run('brctl', 'delbr', bridge_name) @sudo_wrapper.run('brctl', 'delbr', bridge_name)
end end
@ -190,6 +211,10 @@ module Vagrant
@version ||= @cli.version @version ||= @cli.version
end 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 # TODO: This needs to be reviewed and specs needs to be written
def compress_rootfs def compress_rootfs
# TODO: Pass in tmpdir so we can clean up from outside # TODO: Pass in tmpdir so we can clean up from outside
@ -220,6 +245,13 @@ module Vagrant
write_config(contents) write_config(contents)
end end
def update_config_keys(path = nil)
path = path || config_path
@cli.update_config(path)
rescue Errors::ExecuteError
# not on LXC 2.1+. Doesn't matter, ignore.
end
protected protected
def write_customizations(customizations) def write_customizations(customizations)
@ -236,47 +268,23 @@ module Vagrant
end end
def write_config(contents) def write_config(contents)
Tempfile.new('lxc-config').tap do |file| confpath = base_path.join('config').to_s
file.chmod 0644 begin
file.write contents File.open(confpath, File::RDWR) do |file|
file.close file.write contents
@sudo_wrapper.run 'cp', '-f', file.path, base_path.join('config').to_s end
@sudo_wrapper.run 'chown', 'root:root', base_path.join('config').to_s rescue
# We don't have permissions to write in the conf file. That's probably because it's a
# privileged container. Work around that through sudo_wrapper.
Tempfile.new('lxc-config').tap do |file|
file.chmod 0644
file.write contents
file.close
@sudo_wrapper.run 'cp', '-f', file.path, confpath
@sudo_wrapper.run 'chown', 'root:root', confpath
end
end end
end end
def import_template(path)
template_name = "vagrant-tmp-#{@container_name}"
tmp_template_path = templates_path.join("lxc-#{template_name}").to_s
@logger.info 'Copying LXC template into place'
@sudo_wrapper.run('cp', path, tmp_template_path)
@sudo_wrapper.run('chmod', '+x', tmp_template_path)
yield template_name
ensure
@logger.info 'Removing LXC template'
if tmp_template_path
@sudo_wrapper.run('rm', tmp_template_path)
end
end
TEMPLATES_PATH_LOOKUP = %w(
/usr/share/lxc/templates
/usr/lib/lxc/templates
/usr/lib64/lxc/templates
/usr/local/lib/lxc/templates
)
def templates_path
return @templates_path if @templates_path
path = TEMPLATES_PATH_LOOKUP.find { |candidate| File.directory?(candidate) }
if !path
raise Errors::TemplatesDirMissing.new paths: TEMPLATES_PATH_LOOKUP.inspect
end
@templates_path = Pathname(path)
end
end end
end end
end end

View file

@ -29,7 +29,7 @@ module Vagrant
def version def version
return @version if @version return @version if @version
@version = support_version_command? ? run(:version) : run(:create, '--version') @version = run(:create, '--version')
if @version =~ /(lxc version:\s+|)(.+)\s*$/ if @version =~ /(lxc version:\s+|)(.+)\s*$/
@version = $2.downcase @version = $2.downcase
else else
@ -39,11 +39,11 @@ module Vagrant
end end
def config(param) def config(param)
if support_config_command? run(:config, param).gsub("\n", '')
run(:config, param).gsub("\n", '') end
else
raise Errors::CommandNotSupported, name: 'config', available_version: '> 1.x.x', version: version def update_config(path)
end run('update-config', '-c', path)
end end
def state def state
@ -59,7 +59,7 @@ module Vagrant
config_opts = ['-f', config_file] config_opts = ['-f', config_file]
end end
extra = template_opts.to_a.flatten extra = template_opts.to_a.flatten.reject { |elem| elem.empty? }
extra.unshift '--' unless extra.empty? extra.unshift '--' unless extra.empty?
run :create, run :create,
@ -85,9 +85,19 @@ module Vagrant
run :start, '-d', '--name', @name, *Array(options) run :start, '-d', '--name', @name, *Array(options)
end end
## lxc-stop will exit 2 if machine was already stopped
# Man Page:
# 2 The specified container exists but was not running.
def stop def stop
attach '/sbin/halt' if supports_attach? begin
run :stop, '--name', @name run :stop, '--name', @name
rescue LXC::Errors::ExecuteError => e
if e.exitcode == 2
@logger.debug "Machine already stopped, lxc-stop returned 2"
else
raise e
end
end
end end
def attach(*cmd) def attach(*cmd)
@ -103,17 +113,17 @@ module Vagrant
end end
if namespaces if namespaces
if supports_attach_with_namespaces? extra = ['--namespaces', namespaces]
extra = ['--namespaces', namespaces]
else
raise LXC::Errors::NamespacesNotSupported
end
end end
end end
run :attach, '--name', @name, *((extra || []) + cmd) run :attach, '--name', @name, *((extra || []) + cmd)
end end
def info(*cmd)
run(:info, '--name', @name, *cmd)
end
def transition_to(target_state, tries = 30, timeout = 1, &block) def transition_to(target_state, tries = 30, timeout = 1, &block)
raise TransitionBlockNotProvided unless block_given? raise TransitionBlockNotProvided unless block_given?
@ -131,42 +141,11 @@ module Vagrant
end end
end end
def supports_attach?
unless defined?(@supports_attach)
begin
@supports_attach = true
run(:attach, '--name', @name, '--', '/bin/true')
rescue LXC::Errors::ExecuteError
@supports_attach = false
end
end
return @supports_attach
end
def support_config_command?
version[0].to_i >= 1
end
def support_version_command?
@sudo_wrapper.run('which', 'lxc-version').strip.chomp != ''
rescue Vagrant::LXC::Errors::ExecuteError
return false
end
private private
def run(command, *args) def run(command, *args)
@sudo_wrapper.run("lxc-#{command}", *args) @sudo_wrapper.run("lxc-#{command}", *args)
end end
def supports_attach_with_namespaces?
unless defined?(@supports_attach_with_namespaces)
@supports_attach_with_namespaces = run(:attach, '-h', :show_stderr => true).values.join.include?('--namespaces')
end
return @supports_attach_with_namespaces
end
end end
end end
end end

View file

@ -5,19 +5,26 @@ module Vagrant
module Errors module Errors
class ExecuteError < Vagrant::Errors::VagrantError class ExecuteError < Vagrant::Errors::VagrantError
error_key(:lxc_execute_error) error_key(:lxc_execute_error)
attr_reader :stderr, :stdout attr_reader :stderr, :stdout, :exitcode
def initialize(message, *args) def initialize(message, *args)
super super
if message.is_a?(Hash) if message.is_a?(Hash)
@stderr = message[:stderr] @stderr = message[:stderr]
@stdout = message[:stdout] @stdout = message[:stdout]
@exitcode = message[:exitcode]
end end
end end
end end
class NamespacesNotSupported < Vagrant::Errors::VagrantError # Raised when user interrupts a subprocess
class SubprocessInterruptError < Vagrant::Errors::VagrantError
error_key(:lxc_interrupt_error)
def initialize(message, *args)
super
end
end end
class LxcLinuxRequired < Vagrant::Errors::VagrantError class LxcLinuxRequired < Vagrant::Errors::VagrantError
error_key(:lxc_linux_required) error_key(:lxc_linux_required)
end end

View file

@ -10,26 +10,25 @@ module Vagrant
EOF EOF
provider(:lxc, parallel: true, priority: 7) do provider(:lxc, parallel: true, priority: 7) do
require File.expand_path("../provider", __FILE__) require_relative 'provider'
init!
I18n.load_path << File.expand_path(File.dirname(__FILE__) + '/../../locales/en.yml')
I18n.reload!
Provider Provider
end end
command "lxc" do command "lxc" do
require_relative 'command/root' require_relative 'command/root'
init!
Command::Root Command::Root
end end
config(:lxc, :provider) do config(:lxc, :provider) do
require File.expand_path("../config", __FILE__) require_relative 'config'
init!
Config Config
end end
synced_folder(:lxc) do synced_folder(:lxc) do
require File.expand_path("../synced_folder", __FILE__) require_relative 'synced_folder'
SyncedFolder SyncedFolder
end end
@ -37,6 +36,16 @@ module Vagrant
require_relative "provider/cap/public_address" require_relative "provider/cap/public_address"
Provider::Cap::PublicAddress Provider::Cap::PublicAddress
end end
protected
def self.init!
return if defined?(@_init)
I18n.load_path << File.expand_path(File.dirname(__FILE__) + '/../../locales/en.yml')
I18n.reload!
@_init = true
end
end end
end end
end end

View file

@ -2,7 +2,6 @@ require "log4r"
require "vagrant-lxc/action" require "vagrant-lxc/action"
require "vagrant-lxc/driver" require "vagrant-lxc/driver"
require "vagrant-lxc/sudo_wrapper"
module Vagrant module Vagrant
module LXC module LXC
@ -25,18 +24,9 @@ module Vagrant
machine_id_changed machine_id_changed
end end
def sudo_wrapper
@shell ||= begin
wrapper = Pathname.new(LXC.sudo_wrapper_path).exist? &&
LXC.sudo_wrapper_path || nil
@logger.debug("Found sudo wrapper : #{wrapper}") if wrapper
SudoWrapper.new(wrapper)
end
end
def ensure_lxc_installed! def ensure_lxc_installed!
begin begin
sudo_wrapper.run("/usr/bin/which", "lxc-create") SudoWrapper.new(privileged: @machine.provider_config.privileged).run("which", "lxc-create")
rescue Vagrant::LXC::Errors::ExecuteError rescue Vagrant::LXC::Errors::ExecuteError
raise Errors::LxcNotInstalled raise Errors::LxcNotInstalled
end end
@ -49,7 +39,7 @@ module Vagrant
begin begin
@logger.debug("Instantiating the container for: #{id.inspect}") @logger.debug("Instantiating the container for: #{id.inspect}")
@driver = Driver.new(id, self.sudo_wrapper) @driver = Driver.new(id, privileged: @machine.provider_config.privileged)
@driver.validate! @driver.validate!
rescue Driver::ContainerNotFound rescue Driver::ContainerNotFound
# The container doesn't exist, so we probably have a stale # The container doesn't exist, so we probably have a stale

View file

@ -6,15 +6,41 @@ module Vagrant
attr_reader :wrapper_path attr_reader :wrapper_path
def initialize(wrapper_path = nil) def self.dest_path
@wrapper_path = wrapper_path "/usr/local/bin/vagrant-lxc-wrapper"
end
def initialize(privileged: true)
@wrapper_path = Pathname.new(SudoWrapper.dest_path).exist? && SudoWrapper.dest_path || nil
@privileged = privileged
@logger = Log4r::Logger.new("vagrant::lxc::sudo_wrapper") @logger = Log4r::Logger.new("vagrant::lxc::sudo_wrapper")
end end
def run(*command) def run(*command)
options = command.last.is_a?(Hash) ? command.last : {} options = command.last.is_a?(Hash) ? command.last : {}
command.unshift @wrapper_path if @wrapper_path && !options[:no_wrapper]
execute *(['sudo'] + command) # Avoid running LXC commands with a restrictive umask.
# Otherwise disasters occur, like the container root directory
# having permissions `rwxr-x---` which prevents the `vagrant`
# user from accessing its own home directory; among other
# problems, SSH cannot then read `authorized_keys`!
old_mask = File.umask
File.umask(old_mask & 022) # allow all `r` and `x` bits
begin
if @privileged
if @wrapper_path && !options[:no_wrapper]
command.unshift @wrapper_path
execute *(['sudo'] + command)
else
execute *(['sudo', '/usr/bin/env'] + command)
end
else
execute *(['/usr/bin/env'] + command)
end
ensure
File.umask(old_mask)
end
end end
private private
@ -42,10 +68,10 @@ module Vagrant
# nicely handled by Vagrant. # nicely handled by Vagrant.
if r.exit_code != 0 if r.exit_code != 0
if @interrupted if @interrupted
@logger.info("Exit code != 0, but interrupted. Ignoring.") raise LXC::Errors::SubprocessInterruptError, command.inspect
else else
raise LXC::Errors::ExecuteError, raise LXC::Errors::ExecuteError,
command: command.inspect, stderr: r.stderr, stdout: r.stdout command: command.inspect, stderr: r.stderr, stdout: r.stdout, exitcode: r.exit_code
end end
end end
end end

View file

@ -1,5 +1,5 @@
module Vagrant module Vagrant
module LXC module LXC
VERSION = "1.1.0" VERSION = "1.4.2"
end end
end end

View file

@ -40,6 +40,9 @@ en:
preparing: Setting up mount entries for shared folders... preparing: Setting up mount entries for shared folders...
errors: errors:
lxc_interrupt_error: |-
Interrupted
lxc_execute_error: |- lxc_execute_error: |-
There was an error executing %{command} There was an error executing %{command}
@ -64,7 +67,7 @@ en:
another provider. another provider.
lxc_not_installed: |- lxc_not_installed: |-
The `lxc` package does not seem to be installed or is not accessible on the PATH. The `lxc` package does not seem to be installed or `lxc-create` is not accessible on the PATH.
lxc_redir_not_installed: |- lxc_redir_not_installed: |-
`redir` is not installed or is not accessible on the PATH. `redir` is not installed or is not accessible on the PATH.

View file

@ -1,4 +1,4 @@
#!/bin/bash #!/usr/bin/env bash
# This is a modified version of /usr/share/lxc/templates/lxc-download # This is a modified version of /usr/share/lxc/templates/lxc-download
# that comes with ubuntu-lxc 1.0.0 stable from ppa changed to suit vagrant-lxc needs # that comes with ubuntu-lxc 1.0.0 stable from ppa changed to suit vagrant-lxc needs
@ -33,8 +33,10 @@ LXC_PATH=
LXC_ROOTFS= LXC_ROOTFS=
LXC_TARBALL= LXC_TARBALL=
LXC_CONFIG= LXC_CONFIG=
LXC_USE_OLDCONFIG=
LXC_STRIP_COMPONENTS=2 LXC_STRIP_COMPONENTS=2
usage() { usage() {
cat <<EOF cat <<EOF
vagrant-lxc default template vagrant-lxc default template
@ -44,6 +46,7 @@ Required arguments:
Optional arguments: Optional arguments:
[ --config ]: Configuration file to be used when building the container [ --config ]: Configuration file to be used when building the container
[ --oldconfig ]: Use pre LXC 2.1 config format
[ -h | --help ]: This help message [ -h | --help ]: This help message
LXC internal arguments (do not pass manually!): LXC internal arguments (do not pass manually!):
@ -57,7 +60,7 @@ EOF
return 0 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 if [ $? -ne 0 ]; then
usage $(basename $0) usage $(basename $0)
@ -70,6 +73,7 @@ do
case "$1" in case "$1" in
-h|--help) usage $0 && exit 0;; -h|--help) usage $0 && exit 0;;
--config) LXC_CONFIG=$2; shift 2;; --config) LXC_CONFIG=$2; shift 2;;
--oldconfig) LXC_USE_OLDCONFIG=1; shift 1;;
--tarball) LXC_TARBALL=$2; shift 2;; --tarball) LXC_TARBALL=$2; shift 2;;
--name) LXC_NAME=$2; shift 2;; --name) LXC_NAME=$2; shift 2;;
--path) LXC_PATH=$2; shift 2;; --path) LXC_PATH=$2; shift 2;;
@ -96,11 +100,6 @@ if [ -z "${LXC_PATH}" ]; then
exit 1 exit 1
fi fi
if [ -z "${LXC_CONFIG}" ]; then
echo "'config' parameter is required"
exit 1
fi
# if $LXC_ROOTFS exists here, it was passed in with --rootfs # if $LXC_ROOTFS exists here, it was passed in with --rootfs
if [ -z "${LXC_ROOTFS}" ]; then if [ -z "${LXC_ROOTFS}" ]; then
config=${LXC_PATH}/config config=${LXC_PATH}/config
@ -115,7 +114,6 @@ fi
# Unpack the rootfs # Unpack the rootfs
echo "Unpacking the rootfs" echo "Unpacking the rootfs"
mkdir -p /var/lock/subsys
( (
flock -x 200 flock -x 200
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
@ -124,13 +122,14 @@ mkdir -p /var/lock/subsys
fi fi
mkdir -p ${LXC_ROOTFS} mkdir -p ${LXC_ROOTFS}
(cd ${LXC_ROOTFS} && tar xfz ${LXC_TARBALL} --strip-components=${LXC_STRIP_COMPONENTS}) (cd ${LXC_ROOTFS} && tar xfz ${LXC_TARBALL} --strip-components=${LXC_STRIP_COMPONENTS} --xattrs --xattrs-include=* || true)
if [ $? -ne 0 ]; then if [ ! -f ${LXC_ROOTFS}/bin/true ]; then
echo "Failed to extract rootfs" echo "Failed to extract rootfs"
exit 1 exit 1
fi fi
) 200>/var/lock/subsys/lxc ) 200>${LXC_PATH}/vagrant_lock
rm ${LXC_PATH}/vagrant_lock
mkdir -p ${LXC_ROOTFS}/dev/pts/ mkdir -p ${LXC_ROOTFS}/dev/pts/
@ -149,7 +148,12 @@ if [ -e "${LXC_PATH}/config-auto" ]; then
cat ${LXC_PATH}/config-auto >> ${LXC_PATH}/config cat ${LXC_PATH}/config-auto >> ${LXC_PATH}/config
rm ${LXC_PATH}/config-auto rm ${LXC_PATH}/config-auto
fi 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 ## Re-add the previously removed network config
if [ -e "${LXC_PATH}/config-network" ]; then if [ -e "${LXC_PATH}/config-network" ]; then
@ -160,11 +164,13 @@ if [ -e "${LXC_PATH}/config-network" ]; then
rm ${LXC_PATH}/config-network rm ${LXC_PATH}/config-network
fi fi
## Append the defaults if [ -n "${LXC_CONFIG}" ]; then
echo "" >> ${LXC_PATH}/config ## Append the defaults
echo "##############################################" >> ${LXC_PATH}/config echo "" >> ${LXC_PATH}/config
echo "# vagrant-lxc base box specific configuration" >> ${LXC_PATH}/config echo "##############################################" >> ${LXC_PATH}/config
cat ${LXC_CONFIG} >> ${LXC_PATH}/config echo "# vagrant-lxc base box specific configuration" >> ${LXC_PATH}/config
cat ${LXC_CONFIG} >> ${LXC_PATH}/config
fi
# Empty section for lxc.customize calls from vagrantfile # Empty section for lxc.customize calls from vagrantfile
echo "" >> ${LXC_PATH}/config echo "" >> ${LXC_PATH}/config

View file

@ -1,13 +1,12 @@
#!/bin/bash #!/bin/sh
# This code should (try to) follow Google's Shell Style Guide
# Borrowed from https://github.com/jpetazzo/pipework # (https://google-styleguide.googlecode.com/svn/trunk/shell.xml)
set -e set -e
case "$1" in case "$1" in
--wait) --wait)
WAIT=1 WAIT=1
;; ;;
esac esac
IFNAME=$1 IFNAME=$1
@ -19,280 +18,405 @@ if [ "$2" = "-i" ]; then
shift 2 shift 2
fi fi
if [ "$2" = "-l" ]; then
LOCAL_IFNAME=$3
shift 2
fi
GUESTNAME=$2 GUESTNAME=$2
IPADDR=$3 IPADDR=$3
MACADDR=$4 MACADDR=$4
if echo $MACADDR | grep -q @ case "$MACADDR" in
then *@*)
VLAN=$(echo $MACADDR | cut -d@ -f2) VLAN="${MACADDR#*@}"
MACADDR=$(echo $MACADDR | cut -d@ -f1) VLAN="${VLAN%%@*}"
else MACADDR="${MACADDR%%@*}"
VLAN= ;;
fi *)
VLAN=
;;
esac
# did they ask to generate a custom MACADDR?
# generate the unique string
case "$MACADDR" in
U:*)
macunique="${MACADDR#*:}"
# now generate a 48-bit hash string from $macunique
MACADDR=$(echo $macunique|md5sum|sed 's/^\(..\)\(..\)\(..\)\(..\)\(..\).*$/02:\1:\2:\3:\4:\5/')
;;
esac
[ "$IPADDR" ] || [ "$WAIT" ] || { [ "$IPADDR" ] || [ "$WAIT" ] || {
echo "Syntax:" echo "Syntax:"
echo "pipework <hostinterface> [-i containerinterface] <guest> <ipaddr>/<subnet>[@default_gateway] [macaddr][@vlan]" echo "pipework <hostinterface> [-i containerinterface] [-l localinterfacename] <guest> <ipaddr>/<subnet>[@default_gateway] [macaddr][@vlan]"
echo "pipework <hostinterface> [-i containerinterface] <guest> dhcp [macaddr][@vlan]" echo "pipework <hostinterface> [-i containerinterface] [-l localinterfacename] <guest> dhcp [macaddr][@vlan]"
echo "pipework --wait [-i containerinterface]" echo "pipework route <guest> <route_command>"
exit 1 echo "pipework --wait [-i containerinterface]"
exit 1
} }
# First step: determine type of first argument (bridge, physical interface...), skip if --wait set # Succeed if the given utility is installed. Fail otherwise.
# For explanations about `which` vs `type` vs `command`, see:
# http://stackoverflow.com/questions/592620/check-if-a-program-exists-from-a-bash-script/677212#677212
# (Thanks to @chenhanxiao for pointing this out!)
installed () {
command -v "$1" >/dev/null 2>&1
}
# Google Styleguide says error messages should go to standard error.
warn () {
echo "$@" >&2
}
die () {
status="$1"
shift
warn "$@"
exit "$status"
}
# First step: determine type of first argument (bridge, physical interface...),
# Unless "--wait" is set (then skip the whole section)
if [ -z "$WAIT" ]; then if [ -z "$WAIT" ]; then
if [ -d /sys/class/net/$IFNAME ] if [ -d "/sys/class/net/$IFNAME" ]
then then
if [ -d /sys/class/net/$IFNAME/bridge ] if [ -d "/sys/class/net/$IFNAME/bridge" ]; then
then IFTYPE=bridge
IFTYPE=bridge BRTYPE=linux
BRTYPE=linux elif installed ovs-vsctl && ovs-vsctl list-br|grep -q "^${IFNAME}$"; then
elif $(which ovs-vsctl >/dev/null 2>&1) && $(ovs-vsctl list-br|grep -q ^$IFNAME$) IFTYPE=bridge
then BRTYPE=openvswitch
IFTYPE=bridge elif [ "$(cat "/sys/class/net/$IFNAME/type")" -eq 32 ]; then # InfiniBand IPoIB interface type 32
BRTYPE=openvswitch IFTYPE=ipoib
elif [ $(cat /sys/class/net/$IFNAME/type) -eq 32 ]; # Infiniband IPoIB interface type 32 # The IPoIB kernel module is fussy, set device name to ib0 if not overridden
then CONTAINER_IFNAME=${CONTAINER_IFNAME:-ib0}
IFTYPE=ipoib PKEY=$VLAN
# The IPoIB kernel module is fussy, set device name to ib0 if not overridden else IFTYPE=phys
CONTAINER_IFNAME=${CONTAINER_IFNAME:-ib0}
else IFTYPE=phys
fi
else
# case "$IFNAME" in
# br*)
IFTYPE=bridge
BRTYPE=linux
# ;;
# ovs*)
# if ! $(which ovs-vsctl >/dev/null)
# then
# echo "Need OVS installed on the system to create an ovs bridge"
# exit 1
# fi
# IFTYPE=bridge
# BRTYPE=openvswitch
# ;;
# *)
# echo "I do not know how to setup interface $IFNAME."
# exit 1
# ;;
# esac
fi fi
else
case "$IFNAME" in
br*)
IFTYPE=bridge
BRTYPE=linux
;;
ovs*)
if ! installed ovs-vsctl; then
die 1 "Need OVS installed on the system to create an ovs bridge"
fi
IFTYPE=bridge
BRTYPE=openvswitch
;;
route*)
IFTYPE=route
;;
dummy*)
IFTYPE=dummy
;;
*) die 1 "I do not know how to setup interface $IFNAME." ;;
esac
fi
fi fi
# Set the default container interface name to eth1 if not already set # Set the default container interface name to eth1 if not already set
CONTAINER_IFNAME=${CONTAINER_IFNAME:-eth1} CONTAINER_IFNAME=${CONTAINER_IFNAME:-eth1}
[ "$WAIT" ] && { [ "$WAIT" ] && {
while ! grep -q ^1$ /sys/class/net/$CONTAINER_IFNAME/carrier 2>/dev/null while true; do
do sleep 1 # This first method works even without `ip` or `ifconfig` installed,
done # but doesn't work on older kernels (e.g. CentOS 6.X). See #128.
grep -q '^1$' "/sys/class/net/$CONTAINER_IFNAME/carrier" && break
# This method hopefully works on those older kernels.
ip link ls dev "$CONTAINER_IFNAME" && break
sleep 1
done > /dev/null 2>&1
exit 0 exit 0
} }
[ $IFTYPE = bridge ] && [ $BRTYPE = linux ] && [ "$VLAN" ] && { [ "$IFTYPE" = bridge ] && [ "$BRTYPE" = linux ] && [ "$VLAN" ] && {
echo "VLAN configuration currently unsupported for Linux bridge." die 1 "VLAN configuration currently unsupported for Linux bridge."
exit 1
} }
[ $IFTYPE = ipoib ] && [ $MACADDR ] && { [ "$IFTYPE" = ipoib ] && [ "$MACADDR" ] && {
echo "MACADDR configuration unsupported for IPoIB interfaces." die 1 "MACADDR configuration unsupported for IPoIB interfaces."
exit 1
} }
# Second step: find the guest (for now, we only support LXC containers) # Second step: find the guest (for now, we only support LXC containers)
while read dev mnt fstype options dump fsck while read _ mnt fstype options _; do
do [ "$fstype" != "cgroup" ] && continue
[ "$fstype" != "cgroup" ] && continue echo "$options" | grep -qw devices || continue
echo $options | grep -qw devices || continue CGROUPMNT=$mnt
CGROUPMNT=$mnt
done < /proc/mounts done < /proc/mounts
[ "$CGROUPMNT" ] || { [ "$CGROUPMNT" ] || {
echo "Could not locate cgroup mount point." die 1 "Could not locate cgroup mount point."
exit 1
} }
# Try to find a cgroup matching exactly the provided name. # Try to find a cgroup matching exactly the provided name.
N=$(find "$CGROUPMNT" -name "$GUESTNAME" | wc -l) N=$(find "$CGROUPMNT" -name "$GUESTNAME" | wc -l)
case "$N" in case "$N" in
0) 0)
# If we didn't find anything, try to lookup the container with Docker. # If we didn't find anything, try to lookup the container with Docker.
if which docker >/dev/null if installed docker; then
then RETRIES=3
RETRIES=3 while [ "$RETRIES" -gt 0 ]; do
while [ $RETRIES -gt 0 ]; do DOCKERPID=$(docker inspect --format='{{ .State.Pid }}' "$GUESTNAME")
DOCKERPID=$(docker inspect --format='{{ .State.Pid }}' $GUESTNAME) [ "$DOCKERPID" != 0 ] && break
[ $DOCKERPID != 0 ] && break sleep 1
sleep 1 RETRIES=$((RETRIES - 1))
RETRIES=$((RETRIES - 1)) done
done
[ "$DOCKERPID" = 0 ] && { [ "$DOCKERPID" = 0 ] && {
echo "Docker inspect returned invalid PID 0" die 1 "Docker inspect returned invalid PID 0"
exit 1 }
}
[ "$DOCKERPID" = "<no value>" ] && { [ "$DOCKERPID" = "<no value>" ] && {
echo "Container $GUESTNAME not found, and unknown to Docker." die 1 "Container $GUESTNAME not found, and unknown to Docker."
exit 1 }
} else
else die 1 "Container $GUESTNAME not found, and Docker not installed."
echo "Container $GUESTNAME not found, and Docker not installed." fi
exit 1 ;;
fi 1) true ;;
;; *) die 1 "Found more than one container matching $GUESTNAME." ;;
1)
true
;;
*)
echo "Found more than one container matching $GUESTNAME."
exit 1
;;
esac esac
if [ "$IPADDR" = "dhcp" ] # only check IPADDR if we are not in a route mode
then [ "$IFTYPE" != route ] && {
# Check for first available dhcp client case "$IPADDR" in
DHCP_CLIENT_LIST="udhcpc dhcpcd dhclient" # Let's check first if the user asked for DHCP allocation.
for CLIENT in $DHCP_CLIENT_LIST; do dhcp|dhcp:*)
which $CLIENT >/dev/null && { # Use Docker-specific strategy to run the DHCP client
DHCP_CLIENT=$CLIENT # from the busybox image, in the network namespace of
break # the container.
} if ! [ "$DOCKERPID" ]; then
done warn "You asked for a Docker-specific DHCP method."
[ -z $DHCP_CLIENT ] && { warn "However, $GUESTNAME doesn't seem to be a Docker container."
echo "You asked for DHCP; but no DHCP client could be found." warn "Try to replace 'dhcp' with another option?"
exit 1 die 1 "Aborting."
} fi
else DHCP_CLIENT=${IPADDR%%:*}
# Check if a subnet mask was provided. ;;
echo $IPADDR | grep -q / || { udhcpc|udhcpc:*|udhcpc-f|udhcpc-f:*|dhcpcd|dhcpcd:*|dhclient|dhclient:*|dhclient-f|dhclient-f:*)
echo "The IP address should include a netmask." DHCP_CLIENT=${IPADDR%%:*}
echo "Maybe you meant $IPADDR/24 ?" # did they ask for the client to remain?
exit 1 DHCP_FOREGROUND=
} [ "${DHCP_CLIENT: -2}" = '-f' ] && {
# Check if a gateway address was provided. DHCP_FOREGROUND=true
if echo $IPADDR | grep -q @ }
then DHCP_CLIENT=${DHCP_CLIENT%-f}
GATEWAY=$(echo $IPADDR | cut -d@ -f2) if ! installed "$DHCP_CLIENT"; then
IPADDR=$(echo $IPADDR | cut -d@ -f1) die 1 "You asked for DHCP client $DHCP_CLIENT, but I can't find it."
else fi
GATEWAY= ;;
fi # Alright, no DHCP? Then let's see if we have a subnet *and* gateway.
*/*@*)
GATEWAY="${IPADDR#*@}" GATEWAY="${GATEWAY%%@*}"
IPADDR="${IPADDR%%@*}"
;;
# No gateway? We need at least a subnet, anyway!
*/*) : ;;
# ... No? Then stop right here.
*)
warn "The IP address should include a netmask."
die 1 "Maybe you meant $IPADDR/24 ?"
;;
esac
}
# If a DHCP method was specified, extract the DHCP options.
if [ "$DHCP_CLIENT" ]; then
case "$IPADDR" in
*:*) DHCP_OPTIONS="${IPADDR#*:}" ;;
esac
fi fi
if [ $DOCKERPID ]; then if [ "$DOCKERPID" ]; then
NSPID=$DOCKERPID NSPID=$DOCKERPID
else else
NSPID=$(head -n 1 $(find "$CGROUPMNT" -name "$GUESTNAME" | head -n 1)/tasks) NSPID=$(head -n 1 "$(find "$CGROUPMNT" -name "$GUESTNAME" | head -n 1)/tasks")
[ "$NSPID" ] || { [ "$NSPID" ] || {
echo "Could not find a process inside container $GUESTNAME." # it is an alternative way to get the pid
exit 1 NSPID=$(lxc-info -n "$GUESTNAME" | grep PID | grep -Eo '[0-9]+')
[ "$NSPID" ] || {
die 1 "Could not find a process inside container $GUESTNAME."
}
} }
fi fi
# Check if an incompatible VLAN device already exists # Check if an incompatible VLAN device already exists
[ $IFTYPE = phys ] && [ "$VLAN" ] && [ -d /sys/class/net/$IFNAME.VLAN ] && { [ "$IFTYPE" = phys ] && [ "$VLAN" ] && [ -d "/sys/class/net/$IFNAME.VLAN" ] && {
[ -z "$(ip -d link show $IFNAME.$VLAN | grep "vlan.*id $VLAN")" ] && { ip -d link show "$IFNAME.$VLAN" | grep -q "vlan.*id $VLAN" || {
echo "$IFNAME.VLAN already exists but is not a VLAN device for tag $VLAN" die 1 "$IFNAME.VLAN already exists but is not a VLAN device for tag $VLAN"
exit 1 }
}
} }
[ ! -d /var/run/netns ] && mkdir -p /var/run/netns [ ! -d /var/run/netns ] && mkdir -p /var/run/netns
[ -f /var/run/netns/$NSPID ] && rm -f /var/run/netns/$NSPID rm -f "/var/run/netns/$NSPID"
ln -s /proc/$NSPID/ns/net /var/run/netns/$NSPID ln -s "/proc/$NSPID/ns/net" "/var/run/netns/$NSPID"
# Check if we need to create a bridge. # Check if we need to create a bridge.
[ $IFTYPE = bridge ] && [ ! -d /sys/class/net/$IFNAME ] && { [ "$IFTYPE" = bridge ] && [ ! -d "/sys/class/net/$IFNAME" ] && {
[ $BRTYPE = linux ] && { [ "$BRTYPE" = linux ] && {
(ip link add dev $IFNAME type bridge > /dev/null 2>&1) || (brctl addbr $IFNAME) (ip link add dev "$IFNAME" type bridge > /dev/null 2>&1) || (brctl addbr "$IFNAME")
ip link set $IFNAME up ip link set "$IFNAME" up
} }
[ $BRTYPE = openvswitch ] && { [ "$BRTYPE" = openvswitch ] && {
ovs-vsctl add-br $IFNAME ovs-vsctl add-br "$IFNAME"
} }
} }
MTU=$(ip link show $IFNAME | awk '{print $5}') [ "$IFTYPE" != "route" ] && [ "$IFTYPE" != "dummy" ] && MTU=$(ip link show "$IFNAME" | awk '{print $5}')
# If it's a bridge, we need to create a veth pair # If it's a bridge, we need to create a veth pair
[ $IFTYPE = bridge ] && { [ "$IFTYPE" = bridge ] && {
if [ -z "$LOCAL_IFNAME" ]; then
LOCAL_IFNAME="v${CONTAINER_IFNAME}pl${NSPID}" LOCAL_IFNAME="v${CONTAINER_IFNAME}pl${NSPID}"
GUEST_IFNAME="v${CONTAINER_IFNAME}pg${NSPID}" fi
ip link add name $LOCAL_IFNAME mtu $MTU type veth peer name $GUEST_IFNAME mtu $MTU GUEST_IFNAME="v${CONTAINER_IFNAME}pg${NSPID}"
case "$BRTYPE" in # Does the link already exist?
linux) if ip link show "$LOCAL_IFNAME" >/dev/null 2>&1; then
(ip link set $LOCAL_IFNAME master $IFNAME > /dev/null 2>&1) || (brctl addif $IFNAME $LOCAL_IFNAME) # link exists, is it in use?
;; if ip link show "$LOCAL_IFNAME" up | grep -q "UP"; then
openvswitch) echo "Link $LOCAL_IFNAME exists and is up"
ovs-vsctl add-port $IFNAME $LOCAL_IFNAME ${VLAN:+"tag=$VLAN"} exit 1
;; fi
esac # delete the link so we can re-add it afterwards
ip link set $LOCAL_IFNAME up ip link del "$LOCAL_IFNAME"
} fi
ip link add name "$LOCAL_IFNAME" mtu "$MTU" type veth peer name "$GUEST_IFNAME" mtu "$MTU"
# Note: if no container interface name was specified, pipework will default to ib0 case "$BRTYPE" in
# Note: no macvlan subinterface or ethernet bridge can be created against an linux)
# ipoib interface. Infiniband is not ethernet. ipoib is an IP layer for it. (ip link set "$LOCAL_IFNAME" master "$IFNAME" > /dev/null 2>&1) || (brctl addif "$IFNAME" "$LOCAL_IFNAME")
# To provide additional ipoib interfaces to containers use SR-IOV and pipework ;;
# to assign them. openvswitch)
[ $IFTYPE = ipoib ] && { if ! ovs-vsctl list-ports "$IFNAME" | grep -q "^${LOCAL_IFNAME}$"; then
GUEST_IFNAME=$CONTAINER_IFNAME ovs-vsctl add-port "$IFNAME" "$LOCAL_IFNAME" ${VLAN:+tag="$VLAN"}
fi
;;
esac
ip link set "$LOCAL_IFNAME" up
} }
# If it's a physical interface, create a macvlan subinterface # If it's a physical interface, create a macvlan subinterface
[ $IFTYPE = phys ] && { [ "$IFTYPE" = phys ] && {
[ "$VLAN" ] && { [ "$VLAN" ] && {
[ ! -d /sys/class/net/$IFNAME.$VLAN ] && { [ ! -d "/sys/class/net/${IFNAME}.${VLAN}" ] && {
ip link add link $IFNAME name $IFNAME.$VLAN mtu $MTU type vlan id $VLAN ip link add link "$IFNAME" name "$IFNAME.$VLAN" mtu "$MTU" type vlan id "$VLAN"
}
ip link set $IFNAME up
IFNAME=$IFNAME.$VLAN
} }
GUEST_IFNAME=ph$NSPID$CONTAINER_IFNAME ip link set "$IFNAME" up
ip link add link $IFNAME dev $GUEST_IFNAME mtu $MTU type macvlan mode bridge IFNAME=$IFNAME.$VLAN
ip link set $IFNAME up }
GUEST_IFNAME=ph$NSPID$CONTAINER_IFNAME
ip link add link "$IFNAME" dev "$GUEST_IFNAME" mtu "$MTU" type macvlan mode bridge
ip link set "$IFNAME" up
} }
ip link set $GUEST_IFNAME netns $NSPID # If it's an IPoIB interface, create a virtual IPoIB interface (the IPoIB
ip netns exec $NSPID ip link set $GUEST_IFNAME name $CONTAINER_IFNAME # equivalent of a macvlan device)
[ "$MACADDR" ] && ip netns exec $NSPID ip link set dev $CONTAINER_IFNAME address $MACADDR #
if [ "$IPADDR" = "dhcp" ] # Note: no macvlan subinterface nor Ethernet bridge can be created on top of an
then # IPoIB interface. InfiniBand is not Ethernet. IPoIB is an IP layer on top of
[ $DHCP_CLIENT = "udhcpc" ] && ip netns exec $NSPID $DHCP_CLIENT -qi $CONTAINER_IFNAME -x hostname:$GUESTNAME # InfiniBand, without an intermediate Ethernet layer.
if [ $DHCP_CLIENT = "dhclient" ] [ "$IFTYPE" = ipoib ] && {
then GUEST_IFNAME="${IFNAME}.${NSPID}"
# kill dhclient after get ip address to prevent device be used after container close
ip netns exec $NSPID $DHCP_CLIENT -pf "/var/run/dhclient.$NSPID.pid" $CONTAINER_IFNAME
kill "$(cat "/var/run/dhclient.$NSPID.pid")"
rm "/var/run/dhclient.$NSPID.pid"
fi
[ $DHCP_CLIENT = "dhcpcd" ] && ip netns exec $NSPID $DHCP_CLIENT -q $CONTAINER_IFNAME -h $GUESTNAME
else
ip netns exec $NSPID ip addr add $IPADDR dev $CONTAINER_IFNAME
[ "$GATEWAY" ] && {
ip netns exec $NSPID ip route delete default >/dev/null 2>&1 && true
}
ip netns exec $NSPID ip link set $CONTAINER_IFNAME up
[ "$GATEWAY" ] && {
ip netns exec $NSPID ip route get $GATEWAY >/dev/null 2>&1 || \
ip netns exec $NSPID ip route add $GATEWAY/32 dev $CONTAINER_IFNAME
ip netns exec $NSPID ip route replace default via $GATEWAY
}
fi
# Give our ARP neighbors a nudge about the new interface # If a partition key is provided, use it
if which arping > /dev/null 2>&1 [ "$PKEY" ] && {
then GUEST_IFNAME="${IFNAME}.${PKEY}.${NSPID}"
IPADDR=$(echo $IPADDR | cut -d/ -f1) PKEY="pkey 0x$PKEY"
ip netns exec $NSPID arping -c 1 -A -I $CONTAINER_IFNAME $IPADDR > /dev/null 2>&1 || true }
ip link add link "$IFNAME" name "$GUEST_IFNAME" type ipoib $PKEY
ip link set "$IFNAME" up
}
# If its a dummy interface, create a dummy interface.
[ "$IFTYPE" = dummy ] && {
GUEST_IFNAME=du$NSPID$CONTAINER_IFNAME
ip link add dev "$GUEST_IFNAME" type dummy
}
# If the `route` command was specified ...
if [ "$IFTYPE" = route ]; then
# ... discard the first two arguments and pass the rest to the route command.
shift 2
ip netns exec "$NSPID" ip route "$@"
else else
# Otherwise, run normally.
ip link set "$GUEST_IFNAME" netns "$NSPID"
ip netns exec "$NSPID" ip link set "$GUEST_IFNAME" name "$CONTAINER_IFNAME"
[ "$MACADDR" ] && ip netns exec "$NSPID" ip link set dev "$CONTAINER_IFNAME" address "$MACADDR"
# When using any of the DHCP methods, we start a DHCP client in the
# network namespace of the container. With the 'dhcp' method, the
# client used is taken from the Docker busybox image (therefore
# requiring no specific client installed on the host). Other methods
# use a locally installed client.
case "$DHCP_CLIENT" in
dhcp)
docker run -d --net container:$GUESTNAME --cap-add NET_ADMIN \
busybox udhcpc -i "$CONTAINER_IFNAME" -x "hostname:$GUESTNAME" \
$DHCP_OPTIONS \
>/dev/null
;;
udhcpc)
DHCP_Q="-q"
[ "$DHCP_FOREGROUND" ] && {
DHCP_OPTIONS="$DHCP_OPTIONS -f"
}
ip netns exec "$NSPID" "$DHCP_CLIENT" -qi "$CONTAINER_IFNAME" \
-x "hostname:$GUESTNAME" \
-p "/var/run/udhcpc.$GUESTNAME.pid" \
$DHCP_OPTIONS
[ ! "$DHCP_FOREGROUND" ] && {
rm "/var/run/udhcpc.$GUESTNAME.pid"
}
;;
dhclient)
ip netns exec "$NSPID" "$DHCP_CLIENT" "$CONTAINER_IFNAME" \
-pf "/var/run/dhclient.$GUESTNAME.pid" \
-lf "/etc/dhclient/dhclient.$GUESTNAME.leases" \
$DHCP_OPTIONS
# kill dhclient after get ip address to prevent device be used after container close
[ ! "$DHCP_FOREGROUND" ] && {
kill "$(cat "/var/run/dhclient.$GUESTNAME.pid")"
rm "/var/run/dhclient.$GUESTNAME.pid"
}
;;
dhcpcd)
ip netns exec "$NSPID" "$DHCP_CLIENT" -q "$CONTAINER_IFNAME" -h "$GUESTNAME"
;;
"")
if installed ipcalc; then
eval "$(ipcalc -b $IPADDR)"
ip netns exec "$NSPID" ip addr add "$IPADDR" brd "$BROADCAST" dev "$CONTAINER_IFNAME"
else
ip netns exec "$NSPID" ip addr add "$IPADDR" dev "$CONTAINER_IFNAME"
fi
[ "$GATEWAY" ] && {
ip netns exec "$NSPID" ip route delete default >/dev/null 2>&1 && true
}
ip netns exec "$NSPID" ip link set "$CONTAINER_IFNAME" up
[ "$GATEWAY" ] && {
ip netns exec "$NSPID" ip route get "$GATEWAY" >/dev/null 2>&1 || \
ip netns exec "$NSPID" ip route add "$GATEWAY/32" dev "$CONTAINER_IFNAME"
ip netns exec "$NSPID" ip route replace default via "$GATEWAY"
}
;;
esac
# Give our ARP neighbors a nudge about the new interface
if installed arping; then
IPADDR=$(echo "$IPADDR" | cut -d/ -f1)
ip netns exec "$NSPID" arping -c 1 -A -I "$CONTAINER_IFNAME" "$IPADDR" > /dev/null 2>&1 || true
else
echo "Warning: arping not found; interface may not be immediately reachable" echo "Warning: arping not found; interface may not be immediately reachable"
fi
fi fi
# Remove NSPID to avoid `ip netns` catch it. # Remove NSPID to avoid `ip netns` catch it.
[ -f /var/run/netns/$NSPID ] && rm -f /var/run/netns/$NSPID rm -f "/var/run/netns/$NSPID"
exit 0
# vim: set tabstop=2 shiftwidth=2 softtabstop=2 expandtab :

4
spec/Vagrantfile vendored
View file

@ -15,10 +15,6 @@ Vagrant.configure("2") do |config|
config.cache.auto_detect = true config.cache.auto_detect = true
config.vm.provider :lxc do |lxc|
# lxc.sudo_wrapper = '/usr/bin/lxc-vagrant-wrapper'
end
config.vm.provision :shell, config.vm.provision :shell,
inline: 'mkdir -p /vagrant/tmp && echo -n "Provisioned" > /vagrant/tmp/provisioning' inline: 'mkdir -p /vagrant/tmp && echo -n "Provisioned" > /vagrant/tmp/provisioning'

View file

@ -24,6 +24,7 @@ describe Vagrant::LXC::Action::ForwardPorts do
machine.stub_chain(:config, :vm, :networks).and_return(networks) machine.stub_chain(:config, :vm, :networks).and_return(networks)
machine.stub(provider: provider, data_dir: data_dir) machine.stub(provider: provider, data_dir: data_dir)
subject.stub(redir_version: 3)
subject.stub(exec: true) subject.stub(exec: true)
subject.stub(spawn: pid) subject.stub(spawn: pid)
end end
@ -34,25 +35,25 @@ describe Vagrant::LXC::Action::ForwardPorts do
subject.stub(system: true) subject.stub(system: true)
subject.call(env) subject.call(env)
expect(subject).to have_received(:spawn).with( expect(subject).to have_received(:spawn).with(
"redir --laddr=#{host_ip} --lport=#{host_port} --caddr=#{container_ip} --cport=#{guest_port} 2>/dev/null" "redir -n #{host_ip}:#{host_port} #{container_ip}:#{guest_port} 2>/dev/null"
) )
end end
it 'skips --laddr parameter if host_ip is nil' do it 'Uses 127.0.0.1 as default if host_ip is nil' do
forward_conf.delete(:host_ip) forward_conf.delete(:host_ip)
subject.stub(system: true) subject.stub(system: true)
subject.call(env) subject.call(env)
expect(subject).to have_received(:spawn).with( expect(subject).to have_received(:spawn).with(
"redir --lport=#{host_port} --caddr=#{container_ip} --cport=#{guest_port} 2>/dev/null" "redir -n 127.0.0.1:#{host_port} #{container_ip}:#{guest_port} 2>/dev/null"
) )
end end
it 'skips --laddr parameter if host_ip is a blank string' do it 'Uses 127.0.0.1 by default if host_ip is a blank string' do
forward_conf[:host_ip] = ' ' forward_conf[:host_ip] = ' '
subject.stub(system: true) subject.stub(system: true)
subject.call(env) subject.call(env)
expect(subject).to have_received(:spawn).with( expect(subject).to have_received(:spawn).with(
"redir --lport=#{host_port} --caddr=#{container_ip} --cport=#{guest_port} 2>/dev/null" "redir -n 127.0.0.1:#{host_port} #{container_ip}:#{guest_port} 2>/dev/null"
) )
end end
@ -70,6 +71,15 @@ describe Vagrant::LXC::Action::ForwardPorts do
expect(subject).not_to have_received(:spawn) expect(subject).not_to have_received(:spawn)
end end
it 'uses redir 2.x command line interface' do
subject.stub(system: true)
subject.stub(redir_version: 2)
subject.call(env)
expect(subject).to have_received(:spawn).with(
"redir --laddr=#{host_ip} --lport=#{host_port} --caddr=#{container_ip} --cport=#{guest_port} 2>/dev/null"
)
end
it 'raises RedirNotInstalled error if `redir` is not installed' do it 'raises RedirNotInstalled error if `redir` is not installed' do
subject.stub(system: false) subject.stub(system: false)
expect { subject.call(env) }.to raise_error(Vagrant::LXC::Errors::RedirNotInstalled) expect { subject.call(env) }.to raise_error(Vagrant::LXC::Errors::RedirNotInstalled)
@ -82,25 +92,25 @@ describe Vagrant::LXC::Action::ForwardPorts do
subject.stub(system: true) subject.stub(system: true)
subject.call(env) subject.call(env)
expect(subject).to have_received(:spawn).with( expect(subject).to have_received(:spawn).with(
"sudo redir --laddr=#{host_ip} --lport=#{host_port} --caddr=#{container_ip} --cport=#{guest_port} 2>/dev/null" "sudo redir -n #{host_ip}:#{host_port} #{container_ip}:#{guest_port} 2>/dev/null"
) )
end end
it 'skips --laddr parameter if host_ip is nil' do it 'Uses 127.0.0.1 by default if host_ip is nil' do
forward_conf.delete(:host_ip) forward_conf.delete(:host_ip)
subject.stub(system: true) subject.stub(system: true)
subject.call(env) subject.call(env)
expect(subject).to have_received(:spawn).with( expect(subject).to have_received(:spawn).with(
"sudo redir --lport=#{host_port} --caddr=#{container_ip} --cport=#{guest_port} 2>/dev/null" "sudo redir -n 127.0.0.1:#{host_port} #{container_ip}:#{guest_port} 2>/dev/null"
) )
end end
it 'skips --laddr parameter if host_ip is a blank string' do it 'Uses 127.0.0.1 by default if host_ip is a blank string' do
forward_conf[:host_ip] = ' ' forward_conf[:host_ip] = ' '
subject.stub(system: true) subject.stub(system: true)
subject.call(env) subject.call(env)
expect(subject).to have_received(:spawn).with( expect(subject).to have_received(:spawn).with(
"sudo redir --lport=#{host_port} --caddr=#{container_ip} --cport=#{guest_port} 2>/dev/null" "sudo redir -n 127.0.0.1:#{host_port} #{container_ip}:#{guest_port} 2>/dev/null"
) )
end end
end end

View file

@ -30,16 +30,7 @@ describe Vagrant::LXC::Driver::CLI do
describe 'version' do describe 'version' do
before do before do
allow(subject).to receive(:support_version_command?).and_return(true) allow(subject).to receive(:run).with(:create, '--version').and_return(lxc_version_out)
allow(subject).to receive(:run).with(:version).and_return(lxc_version_out)
end
describe 'lxc version before 1.x.x' do
let(:lxc_version_out) { "lxc version: 0.x.y-rc1\n" }
it 'parses the version from the output' do
expect(subject.version).to eq('0.x.y-rc1')
end
end end
describe 'lxc version after 1.x.x' do describe 'lxc version after 1.x.x' do
@ -53,24 +44,11 @@ describe Vagrant::LXC::Driver::CLI do
describe 'config' do describe 'config' do
before do before do
allow(subject).to receive(:support_version_command?).and_return(support_version_command?)
allow(subject).to receive(:run).with(:config, 'lxc.lxcpath').and_return(lxc_config_out) allow(subject).to receive(:run).with(:config, 'lxc.lxcpath').and_return(lxc_config_out)
allow(subject).to receive(:run).with(:version).and_return(lxc_version_out)
allow(subject).to receive(:run).with(:create, '--version').and_return(lxc_version_out) allow(subject).to receive(:run).with(:create, '--version').and_return(lxc_version_out)
end end
describe 'lxc version before 1.x.x' do describe 'lxc version after 1.x.x'do
let(:support_version_command?) { true }
let(:lxc_config_out) { "/var/lib/lxc\n" }
let(:lxc_version_out) { "lxc version: 0.x.y-rc1\n" }
it 'not supported' do
expect{subject.config('lxc.lxcpath')}.to raise_error(Vagrant::LXC::Errors::CommandNotSupported)
end
end
describe 'lxc version before after 1.x.x'do
let(:support_version_command?) { false }
let(:lxc_config_out) { "/var/lib/lxc\n" } let(:lxc_config_out) { "/var/lib/lxc\n" }
let(:lxc_version_out) { "1.0.0\n" } let(:lxc_version_out) { "1.0.0\n" }
@ -156,36 +134,11 @@ describe Vagrant::LXC::Driver::CLI do
before do before do
allow(subject).to receive(:run) allow(subject).to receive(:run)
subject.stop
end end
context 'lxc-attach is supported' do it 'issues a lxc-stop with provided container name' do
before do expect(subject).to have_received(:run).with(:stop, '--name', name)
subject.stub(attach: true, supports_attach?: true)
subject.stop
end
it 'runs a /sbin/halt within the container' do
expect(subject).to have_received(:attach).with('/sbin/halt')
end
it 'issues a lxc-stop with provided container name' do
expect(subject).to have_received(:run).with(:stop, '--name', name)
end
end
context 'lxc-attach is not supported' do
before do
subject.stub(attach: false, supports_attach?: false)
subject.stop
end
it 'runs a /sbin/halt within the container' do
expect(subject).to_not have_received(:attach)
end
it 'issues a lxc-stop with provided container name' do
expect(subject).to have_received(:run).with(:stop, '--name', name)
end
end end
end end
@ -232,13 +185,6 @@ describe Vagrant::LXC::Driver::CLI do
subject.attach *(command + [{namespaces: ['network', 'mount']}]) subject.attach *(command + [{namespaces: ['network', 'mount']}])
expect(subject).to have_received(:run).with(:attach, '--name', name, '--namespaces', 'NETWORK|MOUNT', '--', *command) expect(subject).to have_received(:run).with(:attach, '--name', name, '--namespaces', 'NETWORK|MOUNT', '--', *command)
end end
it 'raises a NamespacesNotSupported error if not supported' do
allow(subject).to receive(:run).with(:attach, '-h', :show_stderr => true).and_return({:stdout => '', :stderr => 'not supported'})
expect {
subject.attach *(command + [{namespaces: ['network', 'mount']}])
}.to raise_error(Vagrant::LXC::Errors::NamespacesNotSupported)
end
end end
describe 'transition block' do describe 'transition block' do
@ -260,33 +206,4 @@ describe Vagrant::LXC::Driver::CLI do
skip 'waits for the expected container state' skip 'waits for the expected container state'
end end
describe 'check for whether lxc-attach is supported' do
let(:name) { 'a-running-container' }
subject { described_class.new(sudo_wrapper, name) }
context 'lxc-attach is present on system' do
before { subject.stub(run: true) }
it 'returns true if `lxc-attach --name CNAME -- /bin/true` works' do
expect(subject.supports_attach?).to be_truthy
expect(subject).to have_received(:run).with(
:attach, '--name', name, '--', '/bin/true'
)
end
end
context 'lxc-attach is not present on system' do
before do
allow(subject).to receive(:run).and_raise(Vagrant::LXC::Errors::ExecuteError.new('msg'))
end
it 'returns true if `lxc-attach --name CNAME -- /bin/true` works' do
expect(subject.supports_attach?).to be_falsy
expect(subject).to have_received(:run).with(
:attach, '--name', name, '--', '/bin/true'
)
end
end
end
end end

View file

@ -54,7 +54,7 @@ describe Vagrant::LXC::Driver do
it 'creates container with the right arguments' do it 'creates container with the right arguments' do
expect(cli).to have_received(:create).with( expect(cli).to have_received(:create).with(
template_name, template_path,
backingstore, backingstore,
backingstore_opts, backingstore_opts,
config_file, config_file,
@ -75,21 +75,10 @@ describe Vagrant::LXC::Driver do
end end
end end
describe 'supports_attach?' do
let(:cli) { double(Vagrant::LXC::Driver::CLI, supports_attach?: true) }
subject { described_class.new('name', nil, cli) }
it 'delegates to cli object' do
expect(subject.supports_attach?).to be_truthy
expect(cli).to have_received(:supports_attach?)
end
end
describe 'start' do describe 'start' do
let(:customizations) { [['a', '1'], ['b', '2']] } let(:customizations) { [['a', '1'], ['b', '2']] }
let(:internal_customization) { ['internal', 'customization'] } let(:internal_customization) { ['internal', 'customization'] }
let(:cli) { double(Vagrant::LXC::Driver::CLI, start: true, support_config_command?: false) } let(:cli) { double(Vagrant::LXC::Driver::CLI, start: true) }
let(:sudo) { double(Vagrant::LXC::SudoWrapper) } let(:sudo) { double(Vagrant::LXC::SudoWrapper) }
subject { described_class.new('name', sudo, cli) } subject { described_class.new('name', sudo, cli) }
@ -97,8 +86,9 @@ describe Vagrant::LXC::Driver do
before do before do
sudo.should_receive(:run).with('cat', '/var/lib/lxc/name/config').exactly(2).times. sudo.should_receive(:run).with('cat', '/var/lib/lxc/name/config').exactly(2).times.
and_return('# CONFIGURATION') and_return('# CONFIGURATION')
sudo.should_receive(:run).twice.with('cp', '-f', %r{/tmp/.*}, '/var/lib/lxc/name/config') sudo.should_receive(:run).twice.with('cp', '-f', %r{/(run|tmp)/.*}, '/var/lib/lxc/name/config')
sudo.should_receive(:run).twice.with('chown', 'root:root', '/var/lib/lxc/name/config') sudo.should_receive(:run).twice.with('chown', 'root:root', '/var/lib/lxc/name/config')
expect(cli).to receive(:config).with("lxc.lxcpath").and_return("/var/lib/lxc")
subject.customizations << internal_customization subject.customizations << internal_customization
subject.start(customizations) subject.start(customizations)
@ -152,21 +142,11 @@ describe Vagrant::LXC::Driver do
end end
describe 'containers_path' do describe 'containers_path' do
let(:cli) { double(Vagrant::LXC::Driver::CLI, config: cli_config_value, support_config_command?: cli_support_config_command_value) } let(:cli) { double(Vagrant::LXC::Driver::CLI, config: cli_config_value) }
subject { described_class.new('name', nil, cli) } subject { described_class.new('name', nil, cli) }
describe 'lxc version before 1.x.x' do
let(:cli_support_config_command_value) { false }
let(:cli_config_value) { '/var/lib/lxc' }
it 'delegates to cli' do
expect(subject.containers_path).to eq(cli_config_value)
end
end
describe 'lxc version after 1.x.x' do describe 'lxc version after 1.x.x' do
let(:cli_support_config_command_value) { true }
let(:cli_config_value) { '/etc/lxc' } let(:cli_config_value) { '/etc/lxc' }
it 'delegates to cli' do it 'delegates to cli' do
@ -192,14 +172,10 @@ describe Vagrant::LXC::Driver do
subject.share_folders(folders) subject.share_folders(folders)
end end
it "creates guest folder under container's rootfs" do
expect(sudo_wrapper).to have_received(:run).with("mkdir", "-p", "#{rootfs_path}/#{expected_guest_path}")
end
it 'adds a mount.entry to its local customizations' do it 'adds a mount.entry to its local customizations' do
expect(subject.customizations).to include [ expect(subject.customizations).to include [
'mount.entry', 'mount.entry',
"#{shared_folder[:hostpath]} #{expected_guest_path} none bind 0 0" "#{shared_folder[:hostpath]} #{expected_guest_path} none bind,create=dir 0 0"
] ]
end end
@ -213,7 +189,7 @@ describe Vagrant::LXC::Driver do
it 'supports directories with spaces' do it 'supports directories with spaces' do
expect(subject.customizations).to include [ expect(subject.customizations).to include [
'mount.entry', 'mount.entry',
"/path/with\\040space tmp/with\\040space none bind 0 0" "/path/with\\040space tmp/with\\040space none bind,create=dir 0 0"
] ]
end end
end end
@ -224,9 +200,9 @@ describe Vagrant::LXC::Driver do
# Blah blah comment # Blah blah comment
lxc.mount.entry = proc proc proc nodev,noexec,nosuid 0 0 lxc.mount.entry = proc proc proc nodev,noexec,nosuid 0 0
lxc.mount.entry = sysfs sys sysfs defaults 0 0 lxc.mount.entry = sysfs sys sysfs defaults 0 0
lxc.tty = 4 lxc.tty.max = 4
lxc.pts = 1024 lxc.pty.max = 1024
lxc.rootfs = #{rootfs_path} lxc.rootfs.path = #{rootfs_path}
# VAGRANT-BEGIN # VAGRANT-BEGIN
lxc.network.type=veth lxc.network.type=veth
lxc.network.name=eth1 lxc.network.name=eth1
@ -243,7 +219,7 @@ describe Vagrant::LXC::Driver do
it 'adds a mount.entry to its local customizations' do it 'adds a mount.entry to its local customizations' do
expect(subject.customizations).to include [ expect(subject.customizations).to include [
'mount.entry', 'mount.entry',
"#{shared_folder[:hostpath]} #{expected_guest_path} none bind 0 0" "#{shared_folder[:hostpath]} #{expected_guest_path} none bind,create=dir 0 0"
] ]
end end
end end
@ -254,9 +230,9 @@ describe Vagrant::LXC::Driver do
# Blah blah comment # Blah blah comment
lxc.mount.entry = proc proc proc nodev,noexec,nosuid 0 0 lxc.mount.entry = proc proc proc nodev,noexec,nosuid 0 0
lxc.mount.entry = sysfs sys sysfs defaults 0 0 lxc.mount.entry = sysfs sys sysfs defaults 0 0
lxc.tty = 4 lxc.tty.max = 4
lxc.pts = 1024 lxc.pty.max = 1024
lxc.rootfs = overlayfs:/path/to/master/directory:#{rootfs_path} lxc.rootfs.path = overlayfs:/path/to/master/directory:#{rootfs_path}
# VAGRANT-BEGIN # VAGRANT-BEGIN
lxc.network.type=veth lxc.network.type=veth
lxc.network.name=eth1 lxc.network.name=eth1
@ -273,7 +249,7 @@ describe Vagrant::LXC::Driver do
it 'adds a mount.entry to its local customizations' do it 'adds a mount.entry to its local customizations' do
expect(subject.customizations).to include [ expect(subject.customizations).to include [
'mount.entry', 'mount.entry',
"#{shared_folder[:hostpath]} #{expected_guest_path} none bind 0 0" "#{shared_folder[:hostpath]} #{expected_guest_path} none bind,create=dir 0 0"
] ]
end end
end end

View file

@ -1,4 +1,4 @@
#!/opt/vagrant/embedded/bin/ruby #!<%= cmd_paths['ruby'] %>
# Automatically created by vagrant-lxc # Automatically created by vagrant-lxc
class Whitelist class Whitelist
@ -78,9 +78,8 @@ class Whitelist
end end
end end
base = "/var/lib/lxc" base = "<%= lxc_base_path %>"
base_path = %r{\A#{base}/.*\z} base_path = %r{\A#{base}/.*\z}
templates_path = %r{\A/usr/(share|lib|lib64|local/lib)/lxc/templates/.*\z}
## ##
# Commands from provider.rb # Commands from provider.rb
@ -96,18 +95,13 @@ Whitelist.add '<%= cmd_paths['mkdir'] %>', '-p', base_path
# - Container config customizations and pruning # - Container config customizations and pruning
Whitelist.add '<%= cmd_paths['cp'] %>', '-f', %r{/tmp/.*}, base_path Whitelist.add '<%= cmd_paths['cp'] %>', '-f', %r{/tmp/.*}, base_path
Whitelist.add '<%= cmd_paths['chown'] %>', 'root:root', base_path Whitelist.add '<%= cmd_paths['chown'] %>', 'root:root', base_path
# - Template import
Whitelist.add '<%= cmd_paths['cp'] %>', %r{\A.*\z}, templates_path
Whitelist.add '<%= cmd_paths['chmod'] %>', '+x', templates_path
# - Template removal
Whitelist.add '<%= cmd_paths['rm'] %>', templates_path
# - Packaging # - Packaging
Whitelist.add '<%= cmd_paths['tar'] %>', '--numeric-owner', '-cvzf', %r{/tmp/.*/rootfs.tar.gz}, '-C', base_path, './rootfs' Whitelist.add '<%= cmd_paths['tar'] %>', '--numeric-owner', '-cvzf', %r{/tmp/.*/rootfs.tar.gz}, '-C', base_path, './rootfs'
Whitelist.add '<%= cmd_paths['chown'] %>', /\A\d+:\d+\z/, %r{\A/tmp/.*/rootfs\.tar\.gz\z} Whitelist.add '<%= cmd_paths['chown'] %>', /\A\d+:\d+\z/, %r{\A/tmp/.*/rootfs\.tar\.gz\z}
# - Private network script and commands # - Private network script and commands
Whitelist.add '<%= cmd_paths['ip'] %>', 'addr', 'add', /(\d+|\.)+\/24/, 'dev', /.+/ Whitelist.add '<%= cmd_paths['ip'] %>', 'addr', 'add', /(\d+|\.)+\/24/, 'dev', /.+/
Whitelist.add '<%= cmd_paths['ifconfig'] %>', /.+/, 'down' Whitelist.add '<%= cmd_paths['ip'] %>', 'link', 'set', /.+/, /(up|down)/
Whitelist.add '<%= cmd_paths['brctl'] %>', 'delbr', /.+/ Whitelist.add '<%= cmd_paths['brctl'] %>', /(addbr|delbr)/, /.+/
Whitelist.add_regex %r{<%= pipework_regex %>}, '**' Whitelist.add_regex %r{<%= pipework_regex %>}, '**'
## ##
@ -115,6 +109,7 @@ Whitelist.add_regex %r{<%= pipework_regex %>}, '**'
Whitelist.add '<%= cmd_paths['lxc_bin'] %>/lxc-version' Whitelist.add '<%= cmd_paths['lxc_bin'] %>/lxc-version'
Whitelist.add '<%= cmd_paths['lxc_bin'] %>/lxc-ls' Whitelist.add '<%= cmd_paths['lxc_bin'] %>/lxc-ls'
Whitelist.add '<%= cmd_paths['lxc_bin'] %>/lxc-info', '--name', /.*/ Whitelist.add '<%= cmd_paths['lxc_bin'] %>/lxc-info', '--name', /.*/
Whitelist.add '<%= cmd_paths['lxc_bin'] %>/lxc-info', '--name', /.*/, '-iH'
Whitelist.add '<%= cmd_paths['lxc_bin'] %>/lxc-create', '-B', /.*/, '--template', /.*/, '--name', /.*/, '**' Whitelist.add '<%= cmd_paths['lxc_bin'] %>/lxc-create', '-B', /.*/, '--template', /.*/, '--name', /.*/, '**'
Whitelist.add '<%= cmd_paths['lxc_bin'] %>/lxc-create', '--version' Whitelist.add '<%= cmd_paths['lxc_bin'] %>/lxc-create', '--version'
Whitelist.add '<%= cmd_paths['lxc_bin'] %>/lxc-destroy', '--name', /.*/ Whitelist.add '<%= cmd_paths['lxc_bin'] %>/lxc-destroy', '--name', /.*/
@ -124,6 +119,7 @@ Whitelist.add '<%= cmd_paths['lxc_bin'] %>/lxc-shutdown', '--name', /.*/
Whitelist.add '<%= cmd_paths['lxc_bin'] %>/lxc-attach', '--name', /.*/, '**' Whitelist.add '<%= cmd_paths['lxc_bin'] %>/lxc-attach', '--name', /.*/, '**'
Whitelist.add '<%= cmd_paths['lxc_bin'] %>/lxc-attach', '-h' Whitelist.add '<%= cmd_paths['lxc_bin'] %>/lxc-attach', '-h'
Whitelist.add '<%= cmd_paths['lxc_bin'] %>/lxc-config', 'lxc.lxcpath' Whitelist.add '<%= cmd_paths['lxc_bin'] %>/lxc-config', 'lxc.lxcpath'
Whitelist.add '<%= cmd_paths['lxc_bin'] %>/lxc-update-config', '-c', /.*/
## ##
# Commands from driver/action/remove_temporary_files.rb # Commands from driver/action/remove_temporary_files.rb