| 1 |
#!/bin/sh |
| 2 |
# Copyright © 2005-2007 Roger Leigh <[email protected]> |
| 3 |
# |
| 4 |
# schroot is free software: you can redistribute it and/or modify it |
| 5 |
# under the terms of the GNU General Public License as published by |
| 6 |
# the Free Software Foundation, either version 3 of the License, or |
| 7 |
# (at your option) any later version. |
| 8 |
# |
| 9 |
# schroot is distributed in the hope that it will be useful, but |
| 10 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
# General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with this program. If not, see |
| 16 |
# <http://www.gnu.org/licenses/>. |
| 17 |
# |
| 18 |
##################################################################### |
| 19 |
|
| 20 |
set -e |
| 21 |
|
| 22 |
. "$SETUP_DATA_DIR/common-data" |
| 23 |
. "$SETUP_DATA_DIR/common-functions" |
| 24 |
. "$SETUP_DATA_DIR/common-config" |
| 25 |
|
| 26 |
# Check file type |
| 27 |
check_filetype() |
| 28 |
{ |
| 29 |
if echo "$CHROOT_FILE" | grep -q '\.tar$'; then |
| 30 |
filetype="tar" |
| 31 |
elif echo "$CHROOT_FILE" | egrep -q '(\.tar\.gz|\.tgz)$'; then |
| 32 |
filetype="tgz" |
| 33 |
elif echo "$CHROOT_FILE" | egrep -q '(\.tar\.bz2|\.tbz)$'; then |
| 34 |
filetype="tbz" |
| 35 |
elif echo "$CHROOT_FILE" | egrep -q '(\.tar\.xz|\.txz)$'; then |
| 36 |
filetype="txz" |
| 37 |
elif echo "$CHROOT_FILE" | egrep -q '(\.tar\.lzo|\.tzo)$'; then |
| 38 |
filetype="tzo" |
| 39 |
elif echo "$CHROOT_FILE" | egrep -q '(\.tar\.lz4|\.tlz4)$'; then |
| 40 |
filetype="tlz4" |
| 41 |
elif echo "$CHROOT_FILE" | egrep -q '\.tar\.zstd$'; then |
| 42 |
filetype="tzstd" |
| 43 |
else |
| 44 |
fatal "Unsupported filetype for $CHROOT_FILE" |
| 45 |
fi |
| 46 |
} |
| 47 |
|
| 48 |
# Unpack archive |
| 49 |
unpack_file() |
| 50 |
{ |
| 51 |
if [ ! -f "$CHROOT_FILE" ]; then |
| 52 |
fatal "File '$CHROOT_FILE' does not exist" |
| 53 |
fi |
| 54 |
|
| 55 |
if [ "$filetype" = "tar" ]; then |
| 56 |
tar $TAR_VERBOSE -xf "$CHROOT_FILE" |
| 57 |
elif [ "$filetype" = "tgz" ]; then |
| 58 |
tar $TAR_VERBOSE -xzf "$CHROOT_FILE" |
| 59 |
elif [ "$filetype" = "tbz" ]; then |
| 60 |
tar $TAR_VERBOSE -xjf "$CHROOT_FILE" |
| 61 |
elif [ "$filetype" = "txz" ]; then |
| 62 |
tar $TAR_VERBOSE -xJf "$CHROOT_FILE" |
| 63 |
elif [ "$filetype" = "tzo" ]; then |
| 64 |
tar $TAR_VERBOSE --lzop -xf "$CHROOT_FILE" |
| 65 |
elif [ "$filetype" = "tlz4" ]; then |
| 66 |
lz4 -cd "$CHROOT_FILE" | tar $TAR_VERBOSE -x |
| 67 |
elif [ "$filetype" = "tzstd" ]; then |
| 68 |
zstd -cd "$CHROOT_FILE" | tar "$TAR_VERBOSE" -x |
| 69 |
else |
| 70 |
fatal "Unsupported filetype for $CHROOT_FILE" |
| 71 |
fi |
| 72 |
} |
| 73 |
|
| 74 |
# Repack archive |
| 75 |
repack_file() |
| 76 |
{ |
| 77 |
NEWFILE=$(mktemp "${CHROOT_FILE}.XXXXXX") |
| 78 |
|
| 79 |
trap "if [ -f \"$NEWFILE\" ]; then rm -f \"$NEWFILE\"; fi" 0 |
| 80 |
|
| 81 |
if [ "$filetype" = "tar" ]; then |
| 82 |
tar $TAR_VERBOSE -cf "$NEWFILE" . |
| 83 |
elif [ "$filetype" = "tgz" ]; then |
| 84 |
tar $TAR_VERBOSE -czf "$NEWFILE" . |
| 85 |
elif [ "$filetype" = "tbz" ]; then |
| 86 |
tar $TAR_VERBOSE -cjf "$NEWFILE" . |
| 87 |
elif [ "$filetype" = "txz" ]; then |
| 88 |
tar $TAR_VERBOSE -cJf "$NEWFILE" . |
| 89 |
elif [ "$filetype" = "tzo" ]; then |
| 90 |
tar $TAR_VERBOSE --lzop -cf "$NEWFILE" . |
| 91 |
elif [ "$filetype" = "tlz4" ]; then |
| 92 |
tar $TAR_VERBOSE -c . | lz4 > "$NEWFILE" |
| 93 |
elif [ "$filetype" = "tzstd" ]; then |
| 94 |
tar "$TAR_VERBOSE" -c . | zstd > "$NEWFILE" |
| 95 |
else |
| 96 |
fatal "Unsupported filetype for $CHROOT_FILE" |
| 97 |
fi |
| 98 |
|
| 99 |
if [ -f "$CHROOT_FILE" ]; then |
| 100 |
info "Setting ownership and permissions from old archive" |
| 101 |
chown --reference="$CHROOT_FILE" "$NEWFILE" |
| 102 |
chmod --reference="$CHROOT_FILE" "$NEWFILE" |
| 103 |
else |
| 104 |
warn "Old archive no longer exists" |
| 105 |
warn "Setting ownership and permissions to root:root 0600" |
| 106 |
chown root:root "$NEWFILE" |
| 107 |
chmod 0600 "$NEWFILE" |
| 108 |
fi |
| 109 |
mv "$NEWFILE" "$CHROOT_FILE" |
| 110 |
|
| 111 |
trap "" 0 |
| 112 |
} |
| 113 |
|
| 114 |
if [ "$VERBOSE" = "verbose" ]; then |
| 115 |
TAR_VERBOSE="-v" |
| 116 |
fi |
| 117 |
|
| 118 |
if [ "$CHROOT_TYPE" = "file" ]; then |
| 119 |
|
| 120 |
check_filetype |
| 121 |
|
| 122 |
UNPACK_LOCATION="${CHROOT_FILE_UNPACK_DIR}/${SESSION_ID}" |
| 123 |
|
| 124 |
if [ $STAGE = "setup-start" ]; then |
| 125 |
|
| 126 |
info "File unpack directory: $UNPACK_LOCATION" |
| 127 |
|
| 128 |
if [ ! -d "$UNPACK_LOCATION" ]; then |
| 129 |
mkdir -p "$UNPACK_LOCATION" |
| 130 |
info "Created file unpack directory: $UNPACK_LOCATION" |
| 131 |
fi |
| 132 |
cd "$UNPACK_LOCATION" |
| 133 |
info "Changed CWD to $UNPACK_LOCATION" |
| 134 |
|
| 135 |
unpack_file |
| 136 |
|
| 137 |
elif [ "$STAGE" = "setup-stop" ]; then |
| 138 |
|
| 139 |
if [ "$STATUS" = "ok" ] && [ "$CHROOT_FILE_REPACK" = "true" ]; then |
| 140 |
if [ -d "$UNPACK_LOCATION" ]; then |
| 141 |
info "Repacking chroot archive file $CHROOT_FILE from $UNPACK_LOCATION" |
| 142 |
cd "$UNPACK_LOCATION" && repack_file |
| 143 |
else |
| 144 |
warn "Not repacking chroot archive file: $UNPACK_LOCATION does not exist (it may have been removed previously)" |
| 145 |
fi |
| 146 |
fi |
| 147 |
|
| 148 |
if [ "$CHROOT_SESSION_PURGE" = "true" ]; then |
| 149 |
info "Purging $UNPACK_LOCATION" |
| 150 |
if [ -d "$UNPACK_LOCATION" ]; then |
| 151 |
rm -rf "$UNPACK_LOCATION" |
| 152 |
fi |
| 153 |
fi |
| 154 |
|
| 155 |
fi |
| 156 |
|
| 157 |
fi |
| 158 |
|