[Lxc-users] Solution (wrappers) for managing LXC VPS templates and domains

Tony Risinger sweetsinsemilla at gmail.com
Thu Feb 11 01:36:08 UTC 2010


well i was going to do a handful more things before releasing/sharing
this, but i think it's more than complete enough for others to start
tampering with if they wish.  this description will be a little brief,
i will document better soon.

for some time i have been working on a LXC system for my own use.
there has been some talk about consolidating efforts for various
tools.  this system can be easily extended and is already rather
complete, and should make a good base for creating your own custom
"subcommands".

FEATURES

1) create profiles for any distribution during the template and domain
creation stages
2) option to use an existing template as a base, fork it, and
"upgrade" the copy into a new template
3) standard messages info/warn/fatal/debug
4) registrable fatal function stack to "undo" failed commands
(transactional subcommands possible), even on INT/TERM
5) full/extensive usage/help for each subcommand
6) proper bash completion for most subcommands (coming to all)
7) incorporates the inotifywait and INT/PWR tricks
8) uses BTRFS subvolumes and snapshots when building/forking
domains/templates if available, else cp, (lvm later?)
9) prototype subcommand ready to be made into additional subcommands
10) profile.d script
11) host/bridge/dnsmasq auto configuration coming (taken from how
libvirt does it [iptables/brctl])
12) selective overriding/configuration options
13) custom PS1 when entering from the host, uses /proc/self/cgroup to
show VPS name
14) eventually colorize/log everything
15) ...

HOW TO RETRIEVE AND USE

you will need git and the lxc-* tools.  only the archlinux templates
works ATM, i am working on ubuntu and debian.  however, all you need
to do is create (pre|post)?(build|upgrade) files for any distro you
want.  the scripts are pretty commented and consistent.

-------------------------------------

# clone the project into /vps
$ git clone git://devel.extof.me/vps-lxc /vps

# git doesn't track empty folders, build the folder structure
$ /vps/struct

# mount cgroup (must be here for now, doesn't scan ATM)
$ mount -t cgroup lxc /vps/dev/vps-lxc

# link to profile.d (or source it)
$ ln -s /vps/etc/profile.d/vps-lxc.sh /etc/profile.d

# link to bash_completion.d (or source it)
$ ln -s /vps/etc/bash_completion.d/vps-lxc /etc/bash_completion.d

# get started!
$ vps-lxc help
Usage: vps-lxc COMMAND [help|OPTION]...
Create, destroy, interact with, and manage LXC based Virtual Private Server
(VPS/container) domains and templates.

Creation/destruction COMMANDs:
  mktpl    create a new template from scratch or based off an existing template
  mkdom    create a new domain based off a pre-created template
  rmtpl    remove a created template
  rmdom    remove a defined/created, STOPPED domain and it's configuration
Definition/edit COMMANDs:
  define   initialize a domain after editing it's configuration (once)
  edit     edit the persistent configuration of a VPS (requires reboot)
Control/interaction COMMANDs:
  start    send a STOPPED domain a signal to start
  stop     send a RUNNING domain a signal to stop
  reboot   send a RUNNING domain a signal to reboot
  enter    enter a domain and interact via console (limited, config specific)
Query COMMANDs:
  help     what you are seeing right now
  tree     view the VPS system's directories/files in a tree
  ls       list each domain and/or template and their status

-------------------------------------

each subcommand is fully implemented with the exception of ls, and
edit (and init for auto config).  some commands will show more usage
information depending on whether help or usage keyword was used (help
is more).

example:

-------------------------------------

# usage
$ vps-lxc mkdom usage
Usage: vps-lxc mkdom -b BASE [-p PROFILE] [-h HOSTNAME] [-t TTY] [-s DEVPTS]
      [-n type=TYPE,flags=FLAGS,link=LINK,name=NAME,hwaddr=HWADDR,ipv4=IPV4,ipv6=IPV6]...
      [-c subsys.param=VALUE]... TARGET
Generate a VPS domain using an existing template as a base.  The
resultant domain
configuration is altered according to the selected profile and any
options passed

# help
$ vps-lxc mkdom help
Usage: vps-lxc mkdom -b BASE [-p PROFILE] [-h HOSTNAME] [-t TTY] [-s DEVPTS]
      [-n type=TYPE,flags=FLAGS,link=LINK,name=NAME,hwaddr=HWADDR,ipv4=IPV4,ipv6=IPV6]...
      [-c subsys.param=VALUE]... TARGET
Generate a VPS domain using an existing template as a base.  The
resultant domain
configuration is altered according to the selected profile and any
options passed

Options:
  -b   specifies the template used as a base for this domain
  -p   the profile to use; default if not provided
  -h   the hostname assigned to this domain; defaults to NAME
       if not provided
  -t   number of ttys to allocate to this domain
  -s   use a private devpts instance if possible, and restrict
       ptys to this number; not implemented upstream
  -n   append a network interface to this domain in addition
       to any defined by the profile
  -c   append cgroup subsystem values to this domain in
       addition to any defined by the profile
Arguments:
  BASE       name of an existing template
  PROFILE    an existing folder in include/mkdom
  HOSTNAME   a valid hostname; default TARGET
  TTY        positive integer; default 2
  DEVPTS     positive integer; default 1024
  TYPE       empty, veth, macvlan, or phys
  FLAGS      up; or do not specify
  LINK       an existing interface on the host
  NAME       the interface name assigned internally
             to the container
  HWADDR     a valid MAC address; do not specify
             for dynamic
  IPV4       a valid IPV4 address in the form
             x.y.z.t/m, eg. 192.168.1.123/24
  IPV6       a valid IPV6 address in the form x::y/m,
             eg. 2003:db8:1:0:214:1234:fe0b:3596/64
  VALUE      set VALUE to the specified cgroup subsystem
             parameter, e.g devices.deny=a; if VALUE has
             spaces, quotes are needed
Parameters:
  TARGET   the name of the domain to be created

Options not defined can/may be set by the selected profile

-------------------------------------

mkdom and mktpl are the most complex, and a good place to start.  also
try "vps-lxc proto" for a dump of all the variables.  the mkdom
command is mostly capable of creating a fully dynamic lxc config on
the fly with command options only.  take a look at all the build and
upgrade files.

VPS_DEBUG=true vps-lxc; works.  i will be adding more debug stuff.

the structure is designed to run without a "base" (/vps), and
everything will fall into proper locations (/etc, /var, /usr, etc.).
i did this so it can be easily packaged, and i will have a package for
Archlinux up soon.

example of output:

$ vps-lxc start foo
FATAL exception encountered... stacktrace:
  start(vps_cmd_param):59 fatal: TARGET domain (foo) does not exist

$ vps-lxc mktpl -b foo -d archlinux -p awesome -a 64 bar
FATAL exception encountered... stacktrace:
  mktpl(vps_cmd_opt):86 fatal: base template foo does not exist

i will end with a tree dump of what it all looks like running (i am
using it on my server right now).  you can email me directly if any
problems, i intend to be working on/maintaining this, and would like
to make it as flexible as possible for other's use.

thanks

-------------------------------------

/vps
|-- dev
|   `-- vps-lxc
|-- etc
|   |-- bash_completion.d
|   |   `-- vps-lxc
|   |-- profile.d
|   |   `-- vps-lxc.sh
|   `-- vps-lxc
|       |-- conf
|       |   `-- dnsmasq
|       |       `-- dhcpbr0.conf
|       |-- cron
|       `-- vps-lxc.conf
|-- struct
|-- usr
|   |-- bin
|   |   `-- vps-lxc
|   |-- lib
|   |   `-- vps-lxc
|   |       |-- cmd
|   |       |   |-- command.proto
|   |       |   |-- define
|   |       |   |-- edit
|   |       |   |-- enter
|   |       |   |-- init
|   |       |   |-- ls
|   |       |   |-- mkdom
|   |       |   |-- mktpl
|   |       |   |-- monitor
|   |       |   |-- proto -> command.proto
|   |       |   |-- reboot
|   |       |   |-- rmdom
|   |       |   |-- rmtpl
|   |       |   |-- start
|   |       |   |-- stop
|   |       |   `-- tree
|   |       |-- include
|   |       |   |-- mkdom
|   |       |   |   `-- default
|   |       |   |       |-- build
|   |       |   |       `-- prebuild
|   |       |   `-- mktpl
|   |       |       |-- archlinux
|   |       |       |   |-- archlinux
|   |       |       |   |   |-- shared
|   |       |       |   |   |   `-- common
|   |       |       |   |   `-- static
|   |       |       |   |       |-- autologin
|   |       |       |   |       |-- bash.bashrc.local
|   |       |       |   |       |-- rc.conf
|   |       |       |   |       |-- rc.shutdown
|   |       |       |   |       |-- rc.single
|   |       |       |   |       `-- rc.sysinit
|   |       |       |   `-- default
|   |       |       |       |-- build
|   |       |       |       |-- postbuild
|   |       |       |       |-- postupgrade
|   |       |       |       |-- prebuild
|   |       |       |       |-- preupgrade
|   |       |       |       `-- upgrade
|   |       |       `-- ubuntu
|   |       |           `-- default
|   |       |               |-- build
|   |       |               |-- postbuild
|   |       |               `-- prebuild
|   |       |-- init
|   |       |-- shared
|   |       |   |-- color
|   |       |   |-- fs_method
|   |       |   `-- str
|   |       `-- static
|   `-- share
|       `-- doc
|           `-- vps-lxc
|               |-- BUGS
|               |-- INSTALL
|               |-- LICENSE
|               `-- TODO
`-- var
   |-- lib
   |   `-- vps-lxc
   |       |-- def
   |       |   |-- exec
   |       |   |   |-- proc
   |       |   |   `-- sys
   |       |   |       |-- guest-personal-tony.conf
   |       |   |       |-- pycrux.conf
   |       |   |       `-- test.conf
   |       |   `-- mnt
   |       |       |-- guest-personal-tony.conf
   |       |       |-- pycrux.conf
   |       |       `-- test.conf
   |       |-- dom
   |       |   |-- guest-personal-tony
   |       |   |   |-- config -> ../../def/exec/sys/guest-personal-tony.conf
   |       |   |   |-- fstab -> ../../def/mnt/guest-personal-tony.conf
   |       |   |   `-- id
   |       |   |-- pycrux
   |       |   |   |-- config -> ../../def/exec/sys/pycrux.conf
   |       |   |   |-- fstab -> ../../def/mnt/pycrux.conf
   |       |   |   `-- id
   |       |   `-- test
   |       |       |-- config -> ../../def/exec/sys/test.conf
   |       |       |-- fstab -> ../../def/mnt/test.conf
   |       |       `-- id
   |       `-- tpl
   |           `-- test
   |               `-- id
   |-- log
   |   `-- vps-lxc
   |       |-- cmd
   |       |-- dom
   |       |   |-- guest-personal-tony.log
   |       |   |-- pycrux.log
   |       |   `-- test.log
   |       `-- tpl
   `-- run
       `-- vps-lxc




More information about the lxc-users mailing list