Skip to content

Commit

Permalink
lxc-local: Add --no-dev option to exclude /dev from the fstree
Browse files Browse the repository at this point in the history
Signed-off-by: Zen <z@pyl.onl>
  • Loading branch information
desultory authored and stgraber committed Dec 11, 2023
1 parent f885a3c commit 86f5c12
Showing 1 changed file with 39 additions and 13 deletions.
52 changes: 39 additions & 13 deletions templates/lxc-local.in
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ LXC_NAME=
LXC_PATH=
LXC_ROOTFS=
LXC_METADATA=
LXC_FSTREE=
MODE="system"
COMPAT_LEVEL=5

Expand Down Expand Up @@ -74,6 +75,7 @@ Special arguments:
[ -h | --help ]: Print this help message and exit.
[ -m | --metadata ]: Path to the image metadata, should be a tar.xz containing the 'config' file.
[ -f | --fstree ]: Path to the image filesystem tree, should be a tar.xz containing the root filesystem.
[ --no-dev ]: Exclude /dev from the fstree tarball.
LXC internal arguments (do not pass manually!):
[ --name <name> ]: The container name
Expand All @@ -86,7 +88,7 @@ EOF
}

# Show usage and exit if invalid arguments are passed
if ! options=$(getopt -o hm:f: -l help,metadata:,fstree:,name:,path:,rootfs:,mapped-uid:,mapped-gid: -- "$@"); then
if ! options=$(getopt -o hm:f: -l help,metadata:,fstree:,no-dev:,name:,path:,rootfs:,mapped-uid:,mapped-gid: -- "$@"); then
usage
exit 1
fi
Expand All @@ -100,6 +102,7 @@ while :; do
--rootfs) LXC_ROOTFS="$2"; shift 2;;
-m|--metadata) LXC_METADATA="$2"; shift 2;;
-f|--fstree) LXC_FSTREE="$2"; shift 2;;
--no-dev) EXCLUDES="${EXCLUDES} --exclude=./dev/*"; shift 1;;
--mapped-uid) LXC_MAPPED_UID="$2"; shift 2;;
--mapped-gid) LXC_MAPPED_GID="$2"; shift 2;;
*) break;;
Expand Down Expand Up @@ -278,18 +281,29 @@ process_templates() {
unpack_metadata() {
# Unpack file that contains the container metadata
# If the file does not exist, just warn and continue.
if [ -n "${LXC_METADATA}" ] && [ -f "${LXC_METADATA}" ]; then
if tar Jxf "${LXC_METADATA}" -C "${LOCAL_TEMP}"; then
echo "Unpacked metadata file: ${LXC_METADATA}"
process_excludes
process_config
process_fstab
process_templates
else
echo "Unable to unpack metadata file: ${LXC_METADATA}" 2>&1
exit 1
fi
if [ -z "${LXC_METADATA}" ]; then
echo "Metadata file was not passed" 2>&1
return
fi

if [ ! -f "${LXC_METADATA}" ]; then
echo "Metadata file does not exist: ${LXC_METADATA}" 2>&1
return
fi

echo "Using metadata file: ${LXC_METADATA}"

if ! tar Jxf "${LXC_METADATA}" -C "${LOCAL_TEMP}"; then
echo "Unable to unpack metadata file: ${LXC_METADATA}" 2>&1
exit 1
fi

echo "Unpacked metadata file: ${LXC_METADATA}"

process_excludes
process_config
process_fstab
process_templates
}

set_utsname() {
Expand All @@ -305,13 +319,25 @@ prepare_rootfs() {

unpack_rootfs() {
# Unpack the rootfs
echo "Unpacking the rootfs to: ${LXC_ROOTFS}"
if [ -z "${LXC_FSTREE}" ]; then
echo "ERROR: Rootfs file was not passed" 2>&1
exit 1
fi
if [ ! -f "${LXC_FSTREE}" ]; then
echo "ERROR: Rootfs file does not exist: ${LXC_FSTREE}" 2>&1
exit 1
fi
echo "Using rootfs file: ${LXC_FSTREE}"

# Do not surround ${EXCLUDES} by quotes. This does not work. The solution could
# use array but this is not POSIX compliant. The only POSIX compliant solution
# is to use a function wrapper, but the latter can't be used here as the args
# are dynamic. We thus need to ignore the warning brought by shellcheck.
# shellcheck disable=SC2086
if [ -n "${EXCLUDES}" ]; then
echo "Excludes: ${EXCLUDES}"
fi

tar --anchored ${EXCLUDES} --numeric-owner -xpJf "${LXC_FSTREE}" -C "${LXC_ROOTFS}"

prepare_rootfs
Expand Down

0 comments on commit 86f5c12

Please sign in to comment.