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 if [ -e /proc
/1/uid_map
]; then
55 if [ "$(cat /proc/self/uid_map)" = "$(cat /proc/1/uid_map)" ]; then
65 LXC container image downloader
68 [ -h | --help ]: Print this help message and exit.
69 [ -m | --metadata ]: Path to the metadata for the image.
70 [ -f | --fstree ]: Path to the filesystem tree for the image.
72 LXC internal arguments (do not pass manually!):
73 [ --name <name> ]: The container name
74 [ --path <path> ]: The path to the container
75 [ --rootfs <rootfs> ]: The path to the container's rootfs
76 [ --mapped-uid <map> ]: A uid map (user namespaces)
77 [ --mapped-gid <map> ]: A gid map (user namespaces)
82 if ! options
=$
(getopt
-o hm
:f
: -l help,metadata
:,fstree
:,name
:,path
:,rootfs
:,mapped-uid
:,mapped-gid
: -- "$@"); then
86 eval set -- "$options"
90 -h|
--help) usage
&& exit 1;;
91 --name) LXC_NAME
="$2"; shift 2;;
92 --path) LXC_PATH
="$2"; shift 2;;
93 --rootfs) LXC_ROOTFS
="$2"; shift 2;;
94 -m|
--metadata) LXC_CONFIG
="$2"; shift 2;;
95 -f|
--fstree) LXC_FSTREE
="$2"; shift 2;;
96 --mapped-uid) LXC_MAPPED_UID
="$2"; shift 2;;
97 --mapped-gid) LXC_MAPPED_GID
="$2"; shift 2;;
102 # Check for required binaries
103 for bin
in tar xz
; do
104 if ! command -V "${bin}" >/dev
/null
2>&1; then
105 echo "ERROR: Missing required tool: ${bin}" 1>&2
111 if [ -d "${LOCAL_TEMP}" ]; then
112 rm -Rf "${LOCAL_TEMP}"
116 # Trap all exit signals
117 trap cleanup EXIT HUP INT TERM
119 USERNS
="$(in_userns)"
121 if [ "${USERNS}" != "no" ]; then
122 if [ "${USERNS}" = "yes" ]; then
123 if [ -z "${LXC_MAPPED_UID}" ] ||
[ "${LXC_MAPPED_UID}" = "-1" ]; then
124 echo "ERROR: In a user namespace without a map" 1>&2
134 FILE_PATH
="${LOCAL_TEMP}/$1"
136 if [ -e "${FILE_PATH}-${MODE}" ]; then
137 FILE_PATH
="${FILE_PATH}-${MODE}"
140 if [ -e "${FILE_PATH}.${COMPAT_LEVEL}" ]; then
141 FILE_PATH
="${FILE_PATH}.${COMPAT_LEVEL}"
149 echo "Unpacking the rootfs"
151 # Create temporary directory to
152 if ! command -V mktemp
>/dev
/null
2>&1; then
153 LOCAL_TEMP
=/tmp
/lxc-local.$$
154 mkdir
-p "${LOCAL_TEMP}"
156 LOCAL_TEMP
=$
(mktemp
-d)
159 # Unpack file that contains meta.tar.xz
160 if ! tar Jxf
"${LXC_CONFIG}" -C "${LOCAL_TEMP}"; then
161 echo "ERROR: Invalid metadata file" 2>&1
166 excludelist
=$
(relevant_file excludes
)
167 if [ -f "${excludelist}" ]; then
168 while read -r line
; do
169 EXCLUDES
="${EXCLUDES} --exclude=${line}"
170 done < "${excludelist}"
173 # Do not surround ${EXCLUDES} by quotes. This does not work. The solution could
174 # use array but this is not POSIX compliant. The only POSIX compliant solution
175 # is to use a function wrapper, but the latter can't be used here as the args
176 # are dynamic. We thus need to ignore the warning brought by shellcheck.
177 # shellcheck disable=SC2086
178 tar --anchored ${EXCLUDES} --numeric-owner -xpJf "${LXC_FSTREE}" -C "${LXC_ROOTFS}"
180 mkdir -p "${LXC_ROOTFS}/dev
/pts
/"
182 # Setup the configuration
183 # Setup the configuration
184 configfile="$
(relevant_file config
)"
185 if [ ! -e "${configfile}" ]; then
186 echo "ERROR
: meta tarball is missing the configuration
file" 1>&2
190 ## Extract all the network config entries
191 sed -i -e "/lxc.net
.0/{w
${LXC_PATH}/config-network
" -e "d
}" "${LXC_PATH}/config
"
193 ## Extract any other config entry
194 sed -i -e "/lxc.
/{w
${LXC_PATH}/config-auto
" -e "d
}" "${LXC_PATH}/config
"
196 ## Append the defaults
199 echo "# Distribution configuration"
201 } >> "${LXC_PATH}/config"
203 ## Add the container-specific config
206 echo "# Container specific configuration"
207 if [ -e "${LXC_PATH}/config-auto" ]; then
208 cat "${LXC_PATH}/config-auto"
209 rm "${LXC_PATH}/config-auto"
211 } >> "${LXC_PATH}/config"
213 fstab
="$(relevant_file fstab)"
214 if [ -e "${fstab}" ]; then
215 echo "lxc.mount.fstab = ${LXC_PATH}/fstab" >> "${LXC_PATH}/config"
217 echo "lxc.uts.name = ${LXC_NAME}" >> "${LXC_PATH}/config"
219 ## Re-add the previously removed network config
220 if [ -e "${LXC_PATH}/config-network" ]; then
223 echo "# Network configuration"
224 cat "${LXC_PATH}/config-network"
225 rm "${LXC_PATH}/config-network"
226 } >> "${LXC_PATH}/config"
229 TEMPLATE_FILES
="${LXC_PATH}/config"
232 if [ -e "${fstab}" ]; then
233 cp "${fstab}" "${LXC_PATH}/fstab"
234 TEMPLATE_FILES
="${TEMPLATE_FILES};${LXC_PATH}/fstab"
237 # Look for extra templates
238 if [ -e "$(relevant_file templates)" ]; then
239 while read -r line
; do
240 fullpath
="${LXC_ROOTFS}/${line}"
241 [ ! -e "${fullpath}" ] && continue
242 TEMPLATE_FILES
="${TEMPLATE_FILES};${fullpath}"
243 done < "$(relevant_file templates)"
247 # Replace variables in all templates
250 for file in ${TEMPLATE_FILES}; do
251 [ ! -f "${file}" ] && continue
253 sed -i "s#LXC_NAME#${LXC_NAME}#g" "${file}"
254 sed -i "s#LXC_PATH#${LXC_PATH}#g" "${file}"
255 sed -i "s#LXC_ROOTFS#${LXC_ROOTFS}#g" "${file}"
256 sed -i "s#LXC_TEMPLATE_CONFIG#${LXC_TEMPLATE_CONFIG}#g" "${file}"
257 sed -i "s#LXC_HOOK_DIR#${LXC_HOOK_DIR}#g" "${file}"
261 # prevent mingetty from calling vhangup(2) since it fails with userns on CentOS / Oracle
262 if [ -f "${LXC_ROOTFS}/etc/init/tty.conf" ]; then
263 sed -i 's|mingetty|mingetty --nohangup|' "${LXC_ROOTFS}/etc/init/tty.conf"
267 if [ -e "$(relevant_file create-message)" ]; then
270 cat "$(relevant_file create-message)"