| 1 |
#!/bin/bash |
| 2 |
|
| 3 |
# Copyright The containerd Authors. |
| 4 |
|
| 5 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
# you may not use this file except in compliance with the License. |
| 7 |
# You may obtain a copy of the License at |
| 8 |
|
| 9 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
|
| 11 |
# Unless required by applicable law or agreed to in writing, software |
| 12 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
# See the License for the specific language governing permissions and |
| 15 |
# limitations under the License. |
| 16 |
|
| 17 |
set -euo pipefail |
| 18 |
|
| 19 |
PODMAN_CONFIG_DIR=/etc/containers/ |
| 20 |
PODMAN_STORAGE_CONFIG_FILE="${PODMAN_CONFIG_DIR}storage.conf" |
| 21 |
REG_STORAGE_CONFIG_FILE="/etc/stargz-store/config.toml" |
| 22 |
REG_STORAGE_ROOT=/var/lib/stargz-store/ |
| 23 |
REG_STORAGE_DIR="${REG_STORAGE_ROOT}store/" |
| 24 |
REG_STORAGE_POOL_LINK="${REG_STORAGE_ROOT}store/pool" |
| 25 |
REG_STORAGE_MOUNTPOINT="${REG_STORAGE_DIR}" |
| 26 |
|
| 27 |
RETRYNUM=30 |
| 28 |
RETRYINTERVAL=1 |
| 29 |
TIMEOUTSEC=180 |
| 30 |
function retry { |
| 31 |
local SUCCESS=false |
| 32 |
for i in $(seq ${RETRYNUM}) ; do |
| 33 |
if eval "timeout ${TIMEOUTSEC} ${@}" ; then |
| 34 |
SUCCESS=true |
| 35 |
break |
| 36 |
fi |
| 37 |
echo "Fail(${i}). Retrying..." |
| 38 |
sleep ${RETRYINTERVAL} |
| 39 |
done |
| 40 |
if [ "${SUCCESS}" == "true" ] ; then |
| 41 |
return 0 |
| 42 |
else |
| 43 |
return 1 |
| 44 |
fi |
| 45 |
} |
| 46 |
|
| 47 |
function kill_all { |
| 48 |
if [ "${1}" != "" ] ; then |
| 49 |
ps aux | grep "${1}" \ |
| 50 |
| grep -v grep \ |
| 51 |
| grep -v "hello.py" \ |
| 52 |
| grep -v $(basename ${0}) \ |
| 53 |
| sed -E 's/ +/ /g' | cut -f 2 -d ' ' | xargs -I{} kill -9 {} || true |
| 54 |
fi |
| 55 |
} |
| 56 |
|
| 57 |
function cleanup { |
| 58 |
umount "${REG_STORAGE_MOUNTPOINT}" || true |
| 59 |
rm -rf "${REG_STORAGE_DIR}" || true |
| 60 |
if [ -d "${REG_STORAGE_ROOT}pool/" ] ; then |
| 61 |
for POOL in $(ls "${REG_STORAGE_ROOT}pool/") ; do |
| 62 |
umount "${REG_STORAGE_ROOT}pool/${POOL}" || true |
| 63 |
for MP in $(ls "${REG_STORAGE_ROOT}pool/${POOL}") ; do |
| 64 |
umount "${REG_STORAGE_ROOT}pool/${POOL}/${MP}" || true |
| 65 |
done |
| 66 |
done |
| 67 |
fi |
| 68 |
rm -rf "${REG_STORAGE_ROOT}"* |
| 69 |
rm "${PODMAN_STORAGE_CONFIG_FILE}" || true |
| 70 |
podman system reset -f |
| 71 |
} |
| 72 |
|
| 73 |
echo "cleaning up the environment..." |
| 74 |
kill_all "stargz-store" |
| 75 |
cleanup |
| 76 |
|
| 77 |
if [ "${DISABLE_PREFETCH:-}" == "true" ] ; then |
| 78 |
sed -i 's/noprefetch = .*/noprefetch = true/g' "${REG_STORAGE_CONFIG_FILE}" |
| 79 |
else |
| 80 |
sed -i 's/noprefetch = .*/noprefetch = false/g' "${REG_STORAGE_CONFIG_FILE}" |
| 81 |
fi |
| 82 |
|
| 83 |
mkdir -p "${PODMAN_CONFIG_DIR}" |
| 84 |
|
| 85 |
if [ "${DISABLE_ESTARGZ:-}" == "true" ] ; then |
| 86 |
echo "DO NOT RUN additional storage" |
| 87 |
cat <<EOF > "${PODMAN_STORAGE_CONFIG_FILE}" |
| 88 |
[storage] |
| 89 |
driver = "overlay" |
| 90 |
graphroot = "/var/lib/containers/storage" |
| 91 |
runroot = "/run/containers/storage" |
| 92 |
EOF |
| 93 |
else |
| 94 |
echo "running remote snaphsotter..." |
| 95 |
if [ "${LOG_FILE:-}" == "" ] ; then |
| 96 |
LOG_FILE=/dev/null |
| 97 |
fi |
| 98 |
cat <<EOF > "${PODMAN_STORAGE_CONFIG_FILE}" |
| 99 |
[storage] |
| 100 |
driver = "overlay" |
| 101 |
graphroot = "/var/lib/containers/storage" |
| 102 |
runroot = "/run/containers/storage" |
| 103 |
|
| 104 |
[storage.options] |
| 105 |
additionallayerstores = ["${REG_STORAGE_MOUNTPOINT}:ref"] |
| 106 |
EOF |
| 107 |
mkdir -p "${REG_STORAGE_MOUNTPOINT}" |
| 108 |
stargz-store --log-level=debug \ |
| 109 |
--config="${REG_STORAGE_CONFIG_FILE}" \ |
| 110 |
"${REG_STORAGE_MOUNTPOINT}" \ |
| 111 |
2>&1 | tee -a "${LOG_FILE}" & # Dump all log |
| 112 |
retry ls "${REG_STORAGE_POOL_LINK}" > /dev/null |
| 113 |
fi |