Skip to content

Commit 9e3221e

Browse files
committed
Add lxc provider and tests
This commit adds initial support for lxc container management.
1 parent 05f86af commit 9e3221e

File tree

5 files changed

+250
-28
lines changed

5 files changed

+250
-28
lines changed

README.markdown

+85-26
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ Puppet Module to manage virtual machines. Provides a the type: `virt`.
66

77
### virt
88

9-
Manage virtual environments. [Xen] [1], [KVM] [2], and [OpenVZ] [3] hypervisors are supported, which of the first three uses [libvirt] [4] as provider.
10-
This module is the result of my work at GSoC 2010. I thank [Reliant Security] [5] for funding the OpenVZ provider development.
9+
Manage virtual environments. [Xen] [1], [KVM] [2], [OpenVZ] [3], and [LXC] [4] hypervisors are supported, which of the first three uses [libvirt] [5] as provider.
10+
This module is the result of my work at GSoC 2010. I thank [Reliant Security] [6] for funding the OpenVZ provider development.
1111

1212
[1]: http://xen.org "Xen® Hypervisor"
1313
[2]: http://www.linux-kvm.org/ "Kernel Based Virtual Machin"
1414
[3]: http://wiki.openvz.org/ "OpenVZ"
15-
[4]: http://www.libvirt.org/ "The Virtualization API"
16-
[5]: http://reliantsecurity.com/ "Reliant Security"
15+
[4]: http://lxc.sourceforge.net/ "LXC"
16+
[5]: http://www.libvirt.org/ "The Virtualization API"
17+
[6]: http://reliantsecurity.com/ "Reliant Security"
1718

1819
**Autorequires:** If Puppet is managing Xen or KVM guests, the virt resource will autorequire `libvirt` library.
1920

@@ -38,6 +39,24 @@ Note that some values can be specified as an array of values:
3839
interfaces => ["eth0", "eth1"]
3940
}
4041

42+
LXC Example:
43+
44+
virt { 'lxc1':
45+
ensure => running,
46+
os_template => 'ubuntu',
47+
provider => 'lxc'
48+
}
49+
50+
# clone from lxc1
51+
virt { 'lxc2':
52+
ensure => running,
53+
clone => 'lxc1',
54+
snapshot => true,
55+
provider => 'lxc',
56+
require => Virt['lxc1']
57+
}
58+
59+
4160
#### Features
4261

4362
- *disabled*: The provider can disable guest start.
@@ -54,29 +73,32 @@ Note that some values can be specified as an array of values:
5473
- *manages_devices*: The provider can give the guest an access to a device.
5574
- *manages_users*: The provider manage guest's users and passwords.
5675
- *manages_behaviour*: The provider manage the quest's behaviour for reboot, crash and shutdown.
57-
- *initial_config*: The provider can receive a config file with default values for VE creation.
76+
- *initial_config*: The provider can receive a config file with default values for VE or lxc creation.
5877
- *storage_path*: The provider can set the path to storage and mount VE files.
59-
60-
61-
Features \ Provider | libvirt | openvz |
62-
-------------------- | ------- | ------ |
63-
disabled | | *X* |
64-
cpu_fair | | *X* |
65-
disk_quota | | *X* |
66-
pxe | *X* | |
67-
iptables | | *X* |
68-
graphics | *X* | |
69-
clocksync | *X* | |
70-
boot_params | *X* | |
71-
manages_resources | | *X* |
72-
manages_capabilities | | *X* |
73-
manages_features | | *X* |
74-
manages_devices | | *X* |
75-
manages_users | | *X* |
76-
manages_behaviour | *X* | |
77-
initial_config | | *X* |
78-
storage_path | | *X* |
79-
78+
- *cloneable*: The provider can create clones of other instances
79+
- *backingstore*: The provider can use a backingstore such as lvm or btrfs
80+
81+
82+
Features \ Provider | libvirt | openvz | lxc |
83+
-------------------- | ------- | ------ | ------|
84+
disabled | | *X* | |
85+
cpu_fair | | *X* | |
86+
disk_quota | | *X* | |
87+
pxe | *X* | | |
88+
iptables | | *X* | |
89+
graphics | *X* | | |
90+
clocksync | *X* | | |
91+
boot_params | *X* | | |
92+
manages_resources | | *X* | |
93+
manages_capabilities | | *X* | |
94+
manages_features | | *X* | |
95+
manages_devices | | *X* | |
96+
manages_users | | *X* | |
97+
manages_behaviour | *X* | | |
98+
initial_config | | *X* | *X* |
99+
storage_path | | *X* | |
100+
cloneable | | | *X* |
101+
backingstore | | | *X* |
80102

81103
#### Parameters
82104

@@ -175,6 +197,16 @@ For OpenVZ provider, available values are:
175197
* `ubuntu-10.10`: Ubuntu 10.10
176198
* `ubuntu-11.04`: Ubuntu 11.04
177199

200+
For LXC provider, available values on Ubuntu Precise are:
201+
202+
* `busybox`
203+
* `debian`
204+
* `fedora`
205+
* `opensuse`
206+
* `sshd`
207+
* `ubuntu`
208+
* `ubuntu-cloud`
209+
178210
Also, you can use a custom value with your custom template name. Example: `my-customized-ubuntu-10` or `fedora-mycompany`.
179211

180212
When using OpenVZ provider, the template for the new guest will be automaticaly downloaded if don't already exists. It will download from official OpenVZ repository or from URL specified at `tmpl_repo` parameter.
@@ -186,6 +218,7 @@ Available providers are:
186218

187219
* **openvz**: Guest management for OpenVZ guests. Supported features: `disabled`, `cpu_fair`, `disk_quota`, `iptables`, `manages_resources`, `manages_capabilities`, `manages_features`, `manages_devices` and `manages_users`.
188220
* **libvirt**: Guest management for Xen and KVM guests. Note that you will need to install the `libvirt` Ruby library. Supported features: `pxe`, `graphics`, `clocksync`, `boot_params` and `manages_behaviour`
221+
* **lxc**: Guest management for LXC guests. Supported features: `cloneable`, `initial_config`, `backingstore`
189222

190223
##### virt_type
191224

@@ -196,6 +229,8 @@ Available values:
196229
* `xen_paravirt`: This guest should be a paravirtualized guest. It requires hardware virtualization support
197230
* `kvm`: When installing a QEMU guest, make use of the KVM or KQEMU kernel acceleration capabilities if available. Use of this option is recommended unless a guest OS is known to be incompatible with the accelerators.
198231
* `openvz`: When defining an OpenVZ guest, the `os_template` paramenter must be defined.
232+
* `lxc`: When defining an lxc guest, the `os_template` paramenter must be defined.
233+
199234
The values `xen_fullyvirt`, `xen_paravirt` and `kvm` will use libvirt as provider. `openvz` will use the `openvz` provider.
200235

201236
##### xml_file
@@ -216,8 +251,14 @@ Requires features `storage_path`.
216251

217252
##### configfile
218253

254+
OpenVZ:
255+
219256
If specified, values from example configuration file `/etc/vz/conf/ve-<VALUE>.conf-sample` are put into the container configuration file. If this container configuration file already exists, it will be removed.
220257

258+
LXC:
259+
260+
Specify the configuration file to configure the virtualization and isolation functionalities for the container
261+
221262
Requires features `initial_config`.
222263

223264
##### user
@@ -452,6 +493,24 @@ Valid values are: `vmguarpages`, `physpages`, `oomguarpages`, `lockedpages`, `pr
452493

453494
Requires features `resources_management`.
454495

496+
##### clone
497+
498+
Clone a different container to be the base image for this container
499+
500+
Requires features `cloneable`.
501+
502+
##### snapshot
503+
504+
Make the new rootfs a snapshot of the cloned container
505+
506+
Requires features `cloneable`.
507+
508+
##### backingstore
509+
510+
bacckingstore' is one of 'none', 'lvm', or 'btrfs'
511+
512+
Requires features `backingstore`.
513+
455514
----------------
456515

457516

lib/puppet/provider/virt/lxc.rb

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
Puppet::Type.type(:virt).provide :lxc do
2+
desc 'Manages Linux containers'
3+
4+
commands :lxccreate => 'lxc-create',
5+
:lxcdestroy => 'lxc-destroy',
6+
:lxcclone => 'lxc-clone',
7+
:lxcinfo => 'lxc-info',
8+
:lxcstart => 'lxc-start',
9+
:lxcstop => 'lxc-stop',
10+
:lxcfreeze => 'lxc-freeze',
11+
:lxcunfreeze => 'lxc-unfreeze'
12+
13+
has_features :cloneable
14+
has_features :backingstore
15+
has_features :initial_config
16+
17+
def install
18+
args = ['-n', @resource[:name]]
19+
args.push('-t', @resource[:os_template])
20+
if !@resource[:backingstore].nil?
21+
args.push('-B', @resource[:backingstore])
22+
end
23+
if !@resource[:configfile].nil?
24+
args.push('-f', @resource[:configfile])
25+
end
26+
if !@resource[:clone].nil?
27+
clone
28+
else
29+
lxccreate(*args)
30+
end
31+
end
32+
33+
def clone
34+
args = ['-o', @resource[:clone]]
35+
args.push('-n', @resource[:name])
36+
if @resource[:snapshot]
37+
args.push('-s')
38+
end
39+
lxcclone(*args)
40+
end
41+
42+
def setpresent
43+
install
44+
end
45+
46+
def start
47+
if !exists?
48+
install
49+
elsif status == :freeze
50+
unfreeze
51+
end
52+
lxcstart('-n', @resource[:name], '-d')
53+
end
54+
55+
def stop
56+
if !exists?
57+
install
58+
end
59+
lxcstop('-n', @resource[:name])
60+
end
61+
62+
def freeze
63+
if !exists?
64+
install
65+
start
66+
end
67+
lxcfreeze('-n', @resource[:name])
68+
end
69+
70+
def unfreeze
71+
lxcunfreeze('-n', @resource[:name])
72+
end
73+
74+
def destroy
75+
lxcdestroy('-n', @resource[:name], '-f')
76+
end
77+
78+
# FIXME: This path should be configurable
79+
def exists?
80+
if File.exists? "/var/lib/lxc/#{@resource[:name]}"
81+
true
82+
else
83+
false
84+
end
85+
end
86+
87+
# lxc-info returns stopped if the container
88+
# doesn't exist
89+
def status
90+
stat = lxcinfo('-n', @resource[:name])
91+
if !exists?
92+
:absent
93+
elsif exists? and resource[:ensure].to_s == 'installed'
94+
:installed
95+
elsif stat.include?("STOPPED")
96+
return :stopped
97+
else
98+
stat = stat.split(" ")[1].downcase.to_sym
99+
end
100+
end
101+
102+
# FIXME
103+
def cpus
104+
1
105+
end
106+
107+
end
108+

lib/puppet/type/virt.rb

+34-2
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,17 @@ module Puppet
4545
"Support parameters for the guest boot."
4646

4747
feature :initial_config,
48-
"Config file with default values for VE creation"
48+
"Config file with default values for VE or LXC creation"
4949

5050
feature :storage_path,
5151
"Sets the path to storage VE files"
5252

53+
feature :cloneable,
54+
"Linux container template name"
55+
56+
feature :backingstore,
57+
"'backingstore' is one of 'none', 'lvm', or 'btrfs"
58+
5359
# A base class for numeric Virt parameters validation.
5460
class VirtNumericParam < Puppet::Property
5561

@@ -124,6 +130,10 @@ def munge_boolean(value)
124130
provider.purge
125131
end
126132

133+
newvalue(:freeze) do
134+
provider.freeze
135+
end
136+
127137
defaultto(:running)
128138

129139
def retrieve
@@ -172,8 +182,9 @@ def retrieve
172182
newparam(:configfile, :required_features => :initial_config) do
173183
desc "If specified, values from example configuration file /etc/vz/conf/ve-<VALUE>.conf-sample are put into the container configuration file. If this container configuration file already exists, it will be removed."
174184

185+
# FIXME: VZ validation interferes with lxc configfiles
175186
validate do |file|
176-
unless File.file? "/etc/vz/conf/ve-#{file}.conf-sample"
187+
unless File.file? "#{file}" #"/etc/vz/conf/ve-#{file}.conf-sample"
177188
raise ArgumentError, "Config file \"#{file}\" does not exist."
178189
end
179190
end
@@ -287,6 +298,27 @@ def insync?(current)
287298

288299
# Installation method
289300

301+
newparam(:backingstore, :requires_feature => :backingstore) do
302+
desc 'backingstore is one of none, lvm, or btrfs'
303+
newvalues(:none, :lvm, :btrfs)
304+
end
305+
306+
newparam(:clone, :requires_feature => :cloneable) do
307+
desc 'Name of container to clone'
308+
end
309+
310+
newparam(:snapshot, :requires_feature => :cloneable) do
311+
desc 'make the new rootfs a snapshot of the original'
312+
newvalues(:true)
313+
newvalues(:false)
314+
315+
munge do |value|
316+
@resource.munge_boolean(value)
317+
end
318+
319+
defaultto(:false)
320+
end
321+
290322
# URL repository to download OpenVZ Templates
291323
newparam(:tmpl_repo) do
292324
desc "URL repository to download OpenVZ Templates.

manifests/lxc.pp

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class virt::lxc {
2+
3+
package { 'lxc':
4+
ensure => installed,
5+
}
6+
7+
}

tests/lxc.pp

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
include virt::lxc
2+
3+
virt { 'container1':
4+
ensure => running,
5+
os_template => 'ubuntu',
6+
provider => 'lxc'
7+
}
8+
9+
# clone from container1
10+
virt { 'container2':
11+
ensure => running,
12+
clone => 'container1',
13+
snapshot => true,
14+
provider => 'lxc'
15+
}
16+

0 commit comments

Comments
 (0)