[__lxc_container] create: added backingstore and template options

With some minor changes, the backingstore parameters and some more
template options where added (incl. `man.rst`).
This commit is contained in:
matze 2020-07-04 18:19:46 +02:00
commit e9256b6e8e
6 changed files with 161 additions and 41 deletions

View file

@ -4,9 +4,18 @@
# This uploads the diff of the configuration file, which is applied by gencode-remote
# Check desired state
# Get required parameters
name="$__object/parameter/name"
if [ -f "$name" ]; then
name="$(cat "$name")"
else
name="$__object_id"
fi
state_is="$(cat "$__object/explorer/state")"
state_should="$(cat "$__object/parameter/state")"
# Check the configurations only if the container exists
if [ "$state_should" != "absent" ]; then
# do changes
@ -22,6 +31,13 @@ if [ "$state_should" != "absent" ]; then
my_target_host="${__target_host}"
fi
# create remote container directory if container is not yet available
if [ "$state_is" = "absent" ]; then
# this must be done because the container will be created after the configuration is uploaded
# would also be possible to do everything in gencode-remote to keep the order
$__remote_exec $__target_host "mkdir $(dirname "$container_config")"
fi
# config-present
# remove all config lines from config-present that are already set or absent anyway
@ -31,7 +47,7 @@ if [ "$state_should" != "absent" ]; then
# get temp file name
add_dest="$($__remote_exec $__target_host "mktemp $container_config-add.XXXXXXXXXX")"
printf "%s" "$add_dest" > "$__object/files/remote-tmpfile_add"
# upload diff
# upload delta
cat <<OUT
$__remote_copy "$__object/files/config.add" "$my_target_host:$add_dest"
OUT
@ -45,7 +61,7 @@ OUT
# get temp file name
del_dest="$($__remote_exec $__target_host "mktemp $container_config-del.XXXXXXXXXX")"
printf "%s" "$del_dest" > "$__object/files/remote-tmpfile_del"
# upload diff
# upload delta
cat <<OUT
$__remote_copy "$__object/files/config.del" "$my_target_host:$del_dest"
OUT

View file

@ -41,9 +41,8 @@ if [ -n "$lxcpath" ]; then
fi
# shortcut
# shortcut for checking config changes
tmppath="$__object/files/remote-tmpfile"
# shortcut function
config_changes() {
if ( [ -f "${tmppath}_add" ] || [ -f "${tmppath}_del" ] ); then
return 0
@ -52,6 +51,26 @@ config_changes() {
fi
}
# shortcut to add bdev parameters if available
# $1: $__object parameter name without "bdev-"
# $2: target parameter flag (like --foo), else "--$1" will be used
bdev_add_if_available() {
_param_path="$__object/parameter/bdev-$1"
if [ -f "$_param_path" ]; then
backingstore_opts="$backingstore_opts ${2:---$1} '$(cat "$_param_path")'"
fi
}
# shortcut to add template parameters if available
# $1: $__object parameter
# $2: target parameter flag (like --foo), else "--$1" will be used
template_add_if_available() {
_param_path="$__object/parameter/$1"
if [ -f "$_param_path" ]; then
template_opts="$template_opts ${2:---$1} '$(cat "$_param_path")'"
fi
}
# Short curcit if nothing changed
if [ "$state_is" = "$state_should" ] && ( ! config_changes ); then exit; fi
@ -68,6 +87,21 @@ case "$state_should" in
su "$user" - <<SU
USER
# backingstore options
backingstore_opts=""
if [ -f "$__object/parameter/backingstore" ]; then
# -B works for both types, they have different long opts
backingstore_opts="-B '$(cat "$__object/parameter/backingstore")'"
# add parameters if available
bdev_add_if_available dir
bdev_add_if_available fstype
bdev_add_if_available fssize
bdev_add_if_available lvname
bdev_add_if_available vgname
bdev_add_if_available thinpool
fi
# check, if the container should be created or cloned
if [ -f "$__object/parameter/clone" ]; then
copy_from="$(cat "$__object/parameter/clone")"
@ -86,7 +120,7 @@ USER
# print lxc-copy code (because of the lxcpath thing, $LXC_PARAM conflicts)
cat <<LXC
lxc-copy $lxc_opts -n "$copy_from" --newname "$name"
lxc-copy $lxc_opts $backingstore_opts -n "$copy_from" --newname "$name"
LXC
else
template="$(cat "$__object/parameter/template")"
@ -95,7 +129,7 @@ LXC
create_opts=""
if [ -f "$__object/parameter/no-default-config" ]; then
# generate a random empty file and append
empty_config="$($__remote_exec $__target_host "mktemp \${TMPDIR:-/tmp}/cdist_lxc-empty.XXXXXXXXXX")"
empty_config="$($__remote_exec $__target_host "mktemp \${TMPDIR:-/tmp}/cdist_lxc-empty-conf.XXXXXXXXXX")"
create_opts="$create_opts -f \"$empty_config\""
elif [ -f "$__object/parameter/default-config" ]; then
@ -105,13 +139,13 @@ LXC
# assemble template options
template_opts=""
# add common options
if [ -f "$__object/parameter/release" ]; then
template_opts="$template_opts -r \"$(cat "$__object/parameter/release")\""
fi
if [ -f "$__object/parameter/arch" ]; then
template_opts="$template_opts -a \"$(cat "$__object/parameter/arch")\""
# add common options (may require different options for different templates)
template_add_if_available release -r
template_add_if_available arch -a
if [ -f "$__object/parameter/mirror" ]; then
template_opts="$template_opts --mirror='$(cat "$__object/parameter/mirror")'"
fi
template_add_if_available ssh-key -S
# at last, apply custom options
if [ -f "$__object/parameter/template-opts" ]; then
@ -122,7 +156,7 @@ LXC
# print lxc-create code
cat <<LXC
lxc-create $LXC_PARAM -n "$name" -t "$template" $create_opts -- $template_opts
lxc-create $LXC_PARAM $backingstore_opts -n "$name" -t "$template" $create_opts -- $template_opts
LXC
# remove empty tempfile
if [ "$empty_config" ]; then

View file

@ -62,6 +62,45 @@ BOOLEAN PARAMETERS
None.
CREATE PARAMETERS
-----------------
This parameters will be used with *TEMPLATE PARAMETERS* or *CLONE PARAMETERS* to create the container
if the container is absent. All of these parameters will only used if the container will be created,
in all other cases, these parameters will be ignored.
All backingstore parameters (incl. all bdev-* ones) can be read from the manual lxc-create(1) at option
`--bdev` or lxc-copy(1) at option `--backingstore`. The possibilities may differ from creating and cloning.
The bdev-* parameters are only available for some backingstores. If used for other backingstores, the
type aborts.
backingstore
Set the storage for the container root partition. Commonly, there should be at minimum `dir` (or `none`),
`lvm`, `loop`, `btrfs`, `zfs` or `best` be possible.
bdev-dir
Specifies an other path for the rootfs directory instead in the lxc configuration directoy. Only
available for the backingstore `dir` or `none`.
bdev-fstype
Specifies the filesystem to be created instead of the default value. Only available for the
backingstore `lvm` and `loop`.
bdev-fssize
Specifies the filesystem size to be created instead of the default value. Only available for the
backingstore `lvm` and `loop`.
bdev-lvname
The custom LVM logical volume that will be created for the new container. Only availabe for the
backingstore `lvm`.
bdev-vgname
The custon LVM volume group in there the logical volume will be created. Only available for the
backingstore `lvm`.
bdev-thinpool
The custom thinpool the logical volume will created in. Only available for the backingstore `lvm`.
TEMPLATE PARAMETERS
-------------------
This or the *CLONE PARAMETERS* are required to create an container and must be present if the container
@ -88,15 +127,25 @@ no-default-config
**(Boolean value)** This parameter avoids using a default config file by using an empty file instead.
template-opts
Raw options which get passed to the template directly. Can be used multiple times. The argument must
contain a single string (*at best, everything is inside one quote*), else it will interpreted as an
further argument for the type. Some values should be quoted inside the quotes, too.
Raw options which get passed to the template directly. Can be used multiple times. If the argument
contain spaces, it will be interpreted as multiple arguments and must be escaped for an normal posix
shell if this is not wanted.
The following parameters are shortcuts for some template options. As templates may support it or not, you
should be careful when using them. You can check all available template options with
`lxc-create -t $template -h` (incl. lxc-create help, too) or `/usr/share/lxc/templates/lxc-$template -h`.
release
TBA.
Sets the release for the wanted distribution. Uses the `-r` option.
arch
TBA.
Sets the architecture used for the creating container. Uses the `-a` option.
mirror
Sets the mirror to download from. Uses the `--mirror=` option.
ssh-key
Sets the ssh key for the root user- Uses the `-S` option.
CLONE PARAMETERS
----------------

View file

@ -11,11 +11,6 @@
param="$__object/parameter/"
# ====================== #
# == WARNING CHECKS == #
# ====================== #
# ==================== #
# == ERROR CHECKS == #
# ==================== #
@ -38,7 +33,7 @@ if [ -f "$param/template" ]; then
fi
# no template options if --clone given
if [ -f "$__object/parameter/clone" ]; then
if [ -f "$param/clone" ]; then
if [ -f "$param/template-opts" ] \
|| [ -f "$param/default-config" ] \
|| [ -f "$param/no-default-config" ] \
@ -50,6 +45,38 @@ if [ -f "$__object/parameter/clone" ]; then
fi
fi
# check backingstore values
if [ -f "$param/backingstore" ]; then
backingstore="$(cat "$param/backingstore")"
if [ "$backingstore" != "dir" ] && [ "$backingstore" != "none" ]; then
if [ -f "$param/bdev-dir" ]
then
>&2 echo "error: --bdev-dir is only possible for backingstore 'dir' or 'none'"
exit 2
fi
fi
if [ "$backingstore" != "lvm" ] && [ "$backingstore" != "loop" ]; then
if [ -f "$param/bdev-fstype" ] \
|| [ -f "$param/bdev-fssize" ]
then
>&2 echo "error: --bdev-fstype and --bdev-fssize only available for backingstore 'lvm' or 'loop'"
exit 2
fi
fi
if [ "$backingstore" != "lvm" ]; then
if [ -f "$param/bdev-lvname" ] \
|| [ -f "$param/bdev-vgname" ] \
|| [ -f "$param/bdev-thinpool" ]
then
>&2 echo "error: --bdev-{lv,vg}name and --bdev-thinpool only available for backingstore 'lvm'"
exit 2
fi
fi
fi

View file

@ -6,5 +6,14 @@ template
default-config
release
arch
mirror
ssh-key
clone
clonepath
backingstore
bdev-dir
bdev-fstype
bdev-fssize
bdev-lvname
bdev-vgname
bdev-thinpool

View file

@ -1,28 +1,13 @@
# List of all featues that should be implemented
storage:
- backing_storage
- other settings prior to storage
template:
- type: a.E. debian, ubuntu -> what on change?
- release
- arch
- possible ssh auth key?
- possible root_password?
- possible packages?
- possible mirrors?
- other options??
auto..:
auto.. (lxc autostart):
- group
- start
lxc config ..
how to find lxc containers path?
lxc-copy: ephemeral containers? -> seperate type if it makes sense
If config already created, it's considered the container is already created
-> first create; then setup
detection if template changed??
lxc configuration at startup:
flag -f can used to set a default config (instead the system default)
there is no problem to change config before the first startup