3 # Client script for LXC container images.
5 # Copyright © 2018 Stéphane Graber <stgraber@ubuntu.com>
6 # Copyright © 2018 Christian Brauner <christian.brauner@ubuntu.com>
8 # This library is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Lesser General Public
10 # License as published by the Free Software Foundation; either
11 # version 2.1 of the License, or (at your option) any later version.
13 # This library is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # Lesser General Public License for more details.
18 # You should have received a copy of the GNU Lesser General Public
19 # License along with this library; if not, write to the Free Software
20 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
25 LXC_HOOK_DIR
="@LXCHOOKDIR@"
26 LXC_TEMPLATE_CONFIG
="@LXCTEMPLATECONFIG@"
35 # Make sure the usual locations are in PATH
36 export PATH
=$PATH:/usr
/sbin
:/usr
/bin
:/sbin
:/bin
39 [ -e /proc
/self
/uid_map
] ||
{ echo no
; return; }
41 while read -r line
; do
42 fields
="$(echo "$line" | awk '{ print $1 " " $2 " " $3 }')"
43 if [ "${fields}" = "0 0 4294967295" ]; then
48 if echo "${fields}" |
grep -q " 0 1$"; then
52 done < /proc
/self
/uid_map
54 [ "$(cat /proc/self/uid_map)" = "$(cat /proc/1/uid_map)" ] && { echo userns-root
; return; }
60 LXC container image downloader
63 [ -h | --help ]: Print this help message and exit.
64 [ -l | --list ]: List all available images and exit.
65 [ -m | --metadata ]: Path to the metadata for the image.
66 [ -f | --fstree ]: Path to the filesystem tree for the image.
68 LXC internal arguments (do not pass manually!):
69 [ --name <name> ]: The container name
70 [ --path <path> ]: The path to the container
71 [ --rootfs <rootfs> ]: The path to the container's rootfs
72 [ --mapped-uid <map> ]: A uid map (user namespaces)
73 [ --mapped-gid <map> ]: A gid map (user namespaces)
78 if ! options
=$
(getopt
-o hm
:f
: -l help,metadata
:,fstree
:,name
:,path
:,rootfs
:,mapped-uid
:,mapped-gid
: -- "$@"); then
82 eval set -- "$options"
86 -h|
--help) usage
&& exit 1;;
87 --name) LXC_NAME
="$2"; shift 2;;
88 --path) LXC_PATH
="$2"; shift 2;;
89 --rootfs) LXC_ROOTFS
="$2"; shift 2;;
90 -m|
--metadata) LXC_CONFIG
="$2"; shift 2;;
91 -f|
--fstree) LXC_FSTREE
="$2"; shift 2;;
92 --mapped-uid) LXC_MAPPED_UID
="$2"; shift 2;;
93 --mapped-gid) LXC_MAPPED_GID
="$2"; shift 2;;
98 # Check for required binaries
100 if ! command -V "${bin}" >/dev
/null
2>&1; then
101 echo "ERROR: Missing required tool: ${bin}" 1>&2
107 if [ -d "${LOCAL_TEMP}" ]; then
108 rm -Rf "${LOCAL_TEMP}"
112 # Trap all exit signals
113 trap cleanup EXIT HUP INT TERM
115 USERNS
="$(in_userns)"
117 if [ "${USERNS}" != "no" ]; then
118 if [ "${USERNS}" = "yes" ]; then
119 if [ -z "${LXC_MAPPED_UID}" ] ||
[ "${LXC_MAPPED_UID}" = "-1" ]; then
120 echo "ERROR: In a user namespace without a map" 1>&2
130 FILE_PATH
="${LOCAL_TEMP}/$1"
132 if [ -e "${FILE_PATH}-${MODE}" ]; then
133 FILE_PATH
="${FILE_PATH}-${MODE}"
136 if [ -e "${FILE_PATH}.${COMPAT_LEVEL}" ]; then
137 FILE_PATH
="${FILE_PATH}.${COMPAT_LEVEL}"
145 echo "Unpacking the rootfs"
147 # Create temporary directory to
148 if ! command -V mktemp
>/dev
/null
2>&1; then
149 LOCAL_TEMP
=/tmp
/lxc-local.$$
150 mkdir
-p "${LOCAL_TEMP}"
152 LOCAL_TEMP
=$
(mktemp
-d)
155 # Unpack file that contains meta.tar.xz
156 if ! tar Jxf
"${LXC_CONFIG}" -C "${LOCAL_TEMP}"; then
157 echo "ERROR: Invalid metadata file" 2>&1
162 excludelist
=$
(relevant_file excludes
)
163 if [ -f "${excludelist}" ]; then
164 while read -r line
; do
165 EXCLUDES
="${EXCLUDES} --exclude=${line}"
166 done < "${excludelist}"
169 # Do not surround ${EXCLUDES} by quotes. This does not work. The solution could
170 # use array but this is not POSIX compliant. The only POSIX compliant solution
171 # is to use a function wrapper, but the latter can't be used here as the args
172 # are dynamic. We thus need to ignore the warning brought by shellcheck.
173 # shellcheck disable=SC2086
174 tar --anchored ${EXCLUDES} --numeric-owner -xpJf "${LXC_FSTREE}" -C "${LXC_ROOTFS}"
176 mkdir -p "${LXC_ROOTFS}/dev
/pts
/"
178 # Setup the configuration
179 # Setup the configuration
180 configfile="$
(relevant_file config
)"
181 if [ ! -e "${configfile}" ]; then
182 echo "ERROR
: meta tarball is missing the configuration
file" 1>&2
186 ## Extract all the network config entries
187 sed -i -e "/lxc.net
.0/{w
${LXC_PATH}/config-network
" -e "d
}" "${LXC_PATH}/config
"
189 ## Extract any other config entry
190 sed -i -e "/lxc.
/{w
${LXC_PATH}/config-auto
" -e "d
}" "${LXC_PATH}/config
"
192 ## Append the defaults
195 echo "# Distribution configuration"
197 } >> "${LXC_PATH}/config"
199 ## Add the container-specific config
202 echo "# Container specific configuration"
203 if [ -e "${LXC_PATH}/config-auto" ]; then
204 cat "${LXC_PATH}/config-auto"
205 rm "${LXC_PATH}/config-auto"
207 } >> "${LXC_PATH}/config"
209 fstab
="$(relevant_file fstab)"
210 if [ -e "${fstab}" ]; then
211 echo "lxc.mount.fstab = ${LXC_PATH}/fstab" >> "${LXC_PATH}/config"
213 echo "lxc.uts.name = ${LXC_NAME}" >> "${LXC_PATH}/config"
215 ## Re-add the previously removed network config
216 if [ -e "${LXC_PATH}/config-network" ]; then
219 echo "# Network configuration"
220 cat "${LXC_PATH}/config-network"
221 rm "${LXC_PATH}/config-network"
222 } >> "${LXC_PATH}/config"
225 TEMPLATE_FILES
="${LXC_PATH}/config"
228 if [ -e "${fstab}" ]; then
229 cp "${fstab}" "${LXC_PATH}/fstab"
230 TEMPLATE_FILES
="${TEMPLATE_FILES};${LXC_PATH}/fstab"
233 # Look for extra templates
234 if [ -e "$(relevant_file templates)" ]; then
235 while read -r line
; do
236 fullpath
="${LXC_ROOTFS}/${line}"
237 [ ! -e "${fullpath}" ] && continue
238 TEMPLATE_FILES
="${TEMPLATE_FILES};${fullpath}"
239 done < "$(relevant_file templates)"
243 # Replace variables in all templates
246 for file in ${TEMPLATE_FILES}; do
247 [ ! -f "${file}" ] && continue
249 sed -i "s#LXC_NAME#${LXC_NAME}#g" "${file}"
250 sed -i "s#LXC_PATH#${LXC_PATH}#g" "${file}"
251 sed -i "s#LXC_ROOTFS#${LXC_ROOTFS}#g" "${file}"
252 sed -i "s#LXC_TEMPLATE_CONFIG#${LXC_TEMPLATE_CONFIG}#g" "${file}"
253 sed -i "s#LXC_HOOK_DIR#${LXC_HOOK_DIR}#g" "${file}"
257 # prevent mingetty from calling vhangup(2) since it fails with userns on CentOS / Oracle
258 if [ -f "${LXC_ROOTFS}/etc/init/tty.conf" ]; then
259 sed -i 's|mingetty|mingetty --nohangup|' "${LXC_ROOTFS}/etc/init/tty.conf"
263 if [ -e "$(relevant_file create-message)" ]; then
266 cat "$(relevant_file create-message)"