2015-08-14 09:32:44 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
set -ex
|
|
|
|
|
|
|
|
add_overlayfs(){
|
2015-09-16 21:34:04 +00:00
|
|
|
local base_rootfs="$1"
|
|
|
|
local root_path="$2"
|
|
|
|
local rootfs="$root_path/rootfs"
|
|
|
|
local path="$3"
|
2015-08-14 09:32:44 +00:00
|
|
|
local lower="$base_rootfs/${path}"
|
|
|
|
local upper="$root_path/.${path}-delta"
|
|
|
|
local work="$root_path/.${path}-work"
|
|
|
|
|
|
|
|
[[ -d "$rootfs" ]] || mkdir -p "$rootfs"
|
|
|
|
[[ -d "$work" ]] || mkdir -p "$work"
|
|
|
|
[[ -d "$upper" ]] || mkdir -p "$upper"
|
2015-09-16 21:34:04 +00:00
|
|
|
[[ -d "$lower" ]] || mkdir -p "$lower"
|
2015-08-14 09:32:44 +00:00
|
|
|
|
2015-09-16 21:34:04 +00:00
|
|
|
echo "overlay $path overlay lowerdir=$lower,upperdir=$upper,workdir=$work 0 0" >> "$root_path/fstab"
|
2015-08-14 09:32:44 +00:00
|
|
|
}
|
|
|
|
|
2015-09-16 21:34:04 +00:00
|
|
|
echo args $@
|
|
|
|
|
|
|
|
options=$(getopt -o n:4:6: -l name:,path:,rootfs:,mapped-uid:,mapped-gid: -- "$@")
|
2015-08-14 09:32:44 +00:00
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
usage $(basename $0)
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
eval set -- "$options"
|
|
|
|
while true
|
|
|
|
do
|
|
|
|
case "$1" in
|
|
|
|
-n|--name) name=$2; shift 2;;
|
|
|
|
--path) path=$2; shift 2;;
|
2015-09-16 21:34:04 +00:00
|
|
|
--rootfs) rootfs=$2; shift 2;;
|
|
|
|
--mapped-uid) uid=$2; shift 2;;
|
|
|
|
--mapped-gid) gid=$2; shift 2;;
|
2015-08-14 09:32:44 +00:00
|
|
|
--) shift 1; break ;;
|
|
|
|
*) break ;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
if [ -z "${name}" ]; then
|
|
|
|
echo "missing required 'name' parameter"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z "${path}" ]; then
|
|
|
|
echo "missing required 'path' parameter"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2015-09-16 21:34:04 +00:00
|
|
|
if [ -z "${rootfs}" ]; then
|
2015-08-14 09:32:44 +00:00
|
|
|
echo "missing required 'rootfs' parameter"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2015-09-16 21:34:04 +00:00
|
|
|
overlay_conf="/etc/lxc/overlay.conf"
|
|
|
|
|
|
|
|
if [ ! -e "$overlay_conf" ]; then
|
|
|
|
echo "Configuration at /etc/lxc/overlay.conf does not exists"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
base_rootfs=$(perl -ne 'print $1 if /lxc.rootfs\s*=\s*(.*)/' "$overlay_conf")
|
2015-08-14 09:32:44 +00:00
|
|
|
config="${path}/config"
|
|
|
|
|
|
|
|
touch "$path/fstab"
|
2015-09-16 21:34:04 +00:00
|
|
|
add_overlayfs "$base_rootfs" "$path" "var"
|
|
|
|
add_overlayfs "$base_rootfs" "$path" "etc"
|
|
|
|
add_overlayfs "$base_rootfs" "$path" "home"
|
|
|
|
add_overlayfs "$base_rootfs" "$path" "srv"
|
|
|
|
add_overlayfs "$base_rootfs" "$path" "mnt"
|
|
|
|
add_overlayfs "$base_rootfs" "$path" "root"
|
2015-08-14 09:32:44 +00:00
|
|
|
|
|
|
|
cat > "$config" <<EOF
|
2015-09-16 21:34:04 +00:00
|
|
|
lxc.include = $overlay_conf
|
2015-08-14 09:32:44 +00:00
|
|
|
lxc.include = ${path}/local.conf
|
2015-09-16 21:34:04 +00:00
|
|
|
lxc.mount = ${path}/fstab
|
2015-08-14 09:32:44 +00:00
|
|
|
lxc.utsname = $name
|
|
|
|
EOF
|
|
|
|
touch "$path/local.conf"
|
|
|
|
|
|
|
|
export LXC_ROOTFS_PATH="$path/rootfs"
|
|
|
|
export LXC_NAME=$name
|
|
|
|
export LXC_CONFIG_FILE="$config"
|
2015-09-16 21:34:04 +00:00
|
|
|
|
|
|
|
cat > "/tmp/${LXC_NAME}-hooks" <<EOF
|
|
|
|
#!/usr/bin/env bash
|
|
|
|
mount --bind "$path/fstab" /etc/fstab
|
|
|
|
mount --bind "$base_rootfs" "$LXC_ROOTFS_PATH" -o ro
|
|
|
|
cd "$LXC_ROOTFS_PATH"
|
|
|
|
mount -a
|
|
|
|
perl -n \
|
|
|
|
-e'/CREATE_HOOKS="([^"]+)"/ && map { system("/etc/lxc/hooks/\$_") == 0 or print("executing \$_ failed\n") } split(/\s+/, \$1)' \
|
|
|
|
"$overlay_conf"
|
|
|
|
EOF
|
|
|
|
chmod +x "/tmp/${LXC_NAME}-hooks"
|
|
|
|
unshare -m "/tmp/${LXC_NAME}-hooks"
|
|
|
|
rm "/tmp/${LXC_NAME}-hooks"
|
|
|
|
|
|
|
|
chown -R "$uid:$gid" "${path}"/* "${path}"/.*
|
|
|
|
chown -R "0:0" "${path}/config" "${path}/local.conf"
|
|
|
|
chmod +x "${path}"
|