Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Is it possible to create an LXC container that you maintain only for the purpose of copying as a template? If so, how does one properly copy an LXC container?

share|improve this question

3 Answers 3

up vote 4 down vote accepted

Yes, an LXC container is some config files and a directory within the whole server. If you copy this directory and config files and adjust the parameters you can use it as a template. Just tar it and untar it to the new machine's directory.

share|improve this answer
    
good to know. i'll have to test it. thanks. –  Larry G. Wapnitsky Dec 20 '11 at 16:48
    
Where do I configure the cgroup? The copy is trying to use the same one as my "original" –  Larry G. Wapnitsky Dec 21 '11 at 16:16
    
nevermind...i shouldn't copy live machines :) –  Larry G. Wapnitsky Dec 21 '11 at 18:21

This can be easily obtained using BTRFS filesystem in combination with snapshots.

The template filesystem must reside in a subvolume on a btrfs filesystem, say

btrfs subvolume create /mnt/btrfs/template

After which you can populate it.

To create the snapshot:

btrfs subvolume snapshot /mnt/btrfs/template /mnt/btrfs/node-1

Afterwards remove the snapshot with:

btrfs subvolume delete delete /mnt/btrfs/node-1

Advantages are that:

  • creation and removal is fast
  • no extra space is needed, only changed files (blocks) occupy extra space

Further on, if you want to provide your container with some writable private directories on top of a read-only template filesystem you could use mount -o bind from within the container

 mount -o bind /mnt/btrfs/node-1/var /var
share|improve this answer
    
Edited the post with usage examples. –  Zabuzzman Apr 22 '13 at 18:51

I also use a main template for all my containers.

If you are using Ubuntu 14.04, lxc-clone is your friend to create new container based off a template.

lxc-clone -o template_container -n new_container
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.