| 1 |
#!/usr/bin/env bash |
| 2 |
set -Eeuo pipefail |
| 3 |
|
| 4 |
thisDir="$(dirname "$(readlink -vf "$BASH_SOURCE")")" |
| 5 |
source "$thisDir/.constants.sh" \ |
| 6 |
--flags 'debian,debian-eol,debian-ports,non-debian' \ |
| 7 |
--flags 'debootstrap:' \ |
| 8 |
--flags 'debootstrap-script:' \ |
| 9 |
--flags 'keyring:,arch:,include:,exclude:' \ |
| 10 |
--flags 'merged-usr,no-merged-usr' \ |
| 11 |
--flags 'check-gpg,no-check-gpg' \ |
| 12 |
-- \ |
| 13 |
'<target-dir> <suite> <timestamp>' \ |
| 14 |
'rootfs stretch 2017-05-08T00:00:00Z |
| 15 |
--debian-eol rootfs squeeze 2016-03-14T00:00:00Z' \ |
| 16 |
\ |
| 17 |
'--non-debian [--debootstrap-script=xyz] <target-dir> <suite> <mirror>' \ |
| 18 |
'--non-debian rootfs xenial http://archive.ubuntu.com/ubuntu' |
| 19 |
|
| 20 |
eval "$dgetopt" |
| 21 |
nonDebian= |
| 22 |
debianEol= |
| 23 |
debianPorts= |
| 24 |
debootstrap= |
| 25 |
script= |
| 26 |
keyring= |
| 27 |
arch= |
| 28 |
include= |
| 29 |
exclude= |
| 30 |
noMergedUsr= |
| 31 |
noCheckGpg= |
| 32 |
while true; do |
| 33 |
flag="$1"; shift |
| 34 |
dgetopt-case "$flag" |
| 35 |
case "$flag" in |
| 36 |
--debian) nonDebian= ;; |
| 37 |
--debian-eol) nonDebian= ; debianEol=1 ;; |
| 38 |
--debian-ports) nonDebian= ; debianPorts=1 ;; |
| 39 |
--non-debian) nonDebian=1 ;; |
| 40 |
--debootstrap) debootstrap="$1"; shift ;; |
| 41 |
--debootstrap-script) script="$1"; shift ;; |
| 42 |
--keyring) keyring="$1"; shift ;; |
| 43 |
--arch) arch="$1"; shift ;; |
| 44 |
--include) include="${include:+$include,}$1"; shift ;; |
| 45 |
--exclude) exclude="${exclude:+$exclude,}$1"; shift ;; |
| 46 |
--merged-usr) noMergedUsr= ;; |
| 47 |
--no-merged-usr) noMergedUsr=1 ;; |
| 48 |
--check-gpg) noCheckGpg= ;; |
| 49 |
--no-check-gpg) noCheckGpg=1 ;; |
| 50 |
--) break ;; |
| 51 |
*) eusage "unknown flag '$flag'" ;; |
| 52 |
esac |
| 53 |
done |
| 54 |
|
| 55 |
targetDir="${1:-}"; shift || eusage 'missing target-dir' |
| 56 |
[ -n "$targetDir" ] || eusage 'target-dir required' # must be non-empty |
| 57 |
if [ -e "$targetDir" ] && [ -z "$(find "$targetDir" -maxdepth 0 -empty)" ]; then |
| 58 |
echo >&2 "error: '$targetDir' already exists (and isn't empty)!" |
| 59 |
exit 1 |
| 60 |
fi |
| 61 |
|
| 62 |
suite="${1:-}"; shift || eusage 'missing suite' |
| 63 |
|
| 64 |
timestamp= |
| 65 |
mirror= |
| 66 |
if [ -z "$nonDebian" ]; then |
| 67 |
timestamp="${1:-}"; shift || eusage 'missing timestamp' |
| 68 |
else |
| 69 |
mirror="${1:-}"; shift || eusage 'missing mirror' |
| 70 |
|
| 71 |
timestamp="$( |
| 72 |
{ |
| 73 |
wget -qO- "$mirror/dists/$suite/InRelease" 2>/dev/null \ |
| 74 |
|| wget -qO- "$mirror/dists/$suite/Release" 2>/dev/null |
| 75 |
} | awk -F ': ' '$1 == "Date" { print $2; exit }' |
| 76 |
)" || : |
| 77 |
[ -n "$timestamp" ] |
| 78 |
|
| 79 |
# see "debuerreotype-recalculate-epoch" for a simple way to update this value appropriately after "sources.list" is updated and "apt-get update" has been run (which will be more accurate) |
| 80 |
fi |
| 81 |
|
| 82 |
epoch="$(date --date "$timestamp" '+%s')" |
| 83 |
export SOURCE_DATE_EPOCH="$epoch" |
| 84 |
|
| 85 |
if [ -z "$nonDebian" ]; then |
| 86 |
dpkgArch="${arch:-$(dpkg --print-architecture | awk -F- '{ print $NF }')}" |
| 87 |
mirrorArgs=() |
| 88 |
if [ -n "$debianPorts" ]; then |
| 89 |
mirrorArgs+=( --ports ) |
| 90 |
fi |
| 91 |
if [ -n "$debianEol" ]; then |
| 92 |
mirrorArgs+=( --eol ) |
| 93 |
fi |
| 94 |
mirrorArgs+=( "@$epoch" "$suite" "$dpkgArch" main ) |
| 95 |
mirrors="$("$thisDir/.debian-mirror.sh" "${mirrorArgs[@]}")" |
| 96 |
eval "$mirrors" |
| 97 |
[ -n "$snapshotMirror" ] |
| 98 |
mirror="$snapshotMirror" |
| 99 |
fi |
| 100 |
|
| 101 |
debootstrapArgs=() |
| 102 |
|
| 103 |
if [ -z "$noCheckGpg" ]; then |
| 104 |
debootstrapArgs+=( --force-check-gpg ) |
| 105 |
else |
| 106 |
debootstrapArgs+=( --no-check-gpg ) |
| 107 |
fi |
| 108 |
|
| 109 |
: "${script:=$suite}" |
| 110 |
script="$( |
| 111 |
if [ -s "$thisDir/.debootstrap-scripts/$script" ]; then |
| 112 |
readlink -vf "$thisDir/.debootstrap-scripts/$script" |
| 113 |
elif [ -s "/usr/share/debootstrap/scripts/$script" ]; then |
| 114 |
readlink -vf "/usr/share/debootstrap/scripts/$script" |
| 115 |
else |
| 116 |
readlink -vf "$script" |
| 117 |
fi |
| 118 |
)" |
| 119 |
if grep -q 'minbase' "$script"; then |
| 120 |
# --debian-eol sarge and older do not support minbase |
| 121 |
debootstrapArgs+=( --variant=minbase ) |
| 122 |
fi |
| 123 |
|
| 124 |
[ -n "$noMergedUsr" ] && debootstrapArgs+=( --no-merged-usr ) || debootstrapArgs+=( --merged-usr ) |
| 125 |
[ -z "$keyring" ] || debootstrapArgs+=( --keyring="$keyring" ) |
| 126 |
[ -z "$arch" ] || debootstrapArgs+=( --arch="$arch" ) |
| 127 |
[ -z "$include" ] || debootstrapArgs+=( --include="$include" ) |
| 128 |
[ -z "$exclude" ] || debootstrapArgs+=( --exclude="$exclude" ) |
| 129 |
|
| 130 |
debootstrapArgs+=( |
| 131 |
"$suite" "$targetDir" "$mirror" "$script" |
| 132 |
) |
| 133 |
|
| 134 |
unshare-debootstrap() { |
| 135 |
# avoid bugs in packages that might leave things mounted ("/run/shm" is a common one that sometimes ends up dangling) |
| 136 |
unshare --mount debootstrap "$@" |
| 137 |
} |
| 138 |
|
| 139 |
: "${debootstrap:=unshare-debootstrap}" |
| 140 |
if ! "$debootstrap" "${debootstrapArgs[@]}"; then |
| 141 |
if [ -f "$targetDir/debootstrap/debootstrap.log" ]; then |
| 142 |
echo >&2 |
| 143 |
echo >&2 "error: '$debootstrap' failed!" |
| 144 |
echo >&2 |
| 145 |
echo >&2 ' Full command:' |
| 146 |
echo >&2 |
| 147 |
echo >&2 " $(printf ' %q' "$debootstrap" "${debootstrapArgs[@]}")" |
| 148 |
echo >&2 |
| 149 |
echo >&2 ' Logs:' |
| 150 |
echo >&2 |
| 151 |
cat >&2 "$targetDir/debootstrap/debootstrap.log" |
| 152 |
echo >&2 |
| 153 |
fi |
| 154 |
exit 1 |
| 155 |
fi |
| 156 |
echo "$epoch" > "$targetDir/debuerreotype-epoch" |
| 157 |
|
| 158 |
if [ -z "$nonDebian" ]; then |
| 159 |
"$thisDir/debuerreotype-debian-sources-list" --snapshot \ |
| 160 |
$([ -z "$debianEol" ] || echo '--eol') \ |
| 161 |
$([ -z "$debianPorts" ] || echo '--ports') \ |
| 162 |
"$targetDir" "$suite" |
| 163 |
"$thisDir/debuerreotype-apt-get" "$targetDir" update -qq |
| 164 |
fi |
| 165 |
|
| 166 |
# since we're minbase, we know everything included is either essential, or a dependency of essential, so let's get clean "apt-mark showmanual" output |
| 167 |
"$thisDir/debuerreotype-chroot" "$targetDir" bash -c ' |
| 168 |
# --debian-eol squeeze and below do not have python in minbase, thus "apt-mark" fails to run |
| 169 |
# bash: /usr/bin/apt-mark: /usr/bin/python: bad interpreter: No such file or directory |
| 170 |
# (also, squeeze APT does not treat essential packages as special, and will offer to purge them if they get marked as auto-installed) |
| 171 |
if apt-mark --help &> /dev/null; then |
| 172 |
apt-mark auto ".*" > /dev/null |
| 173 |
if [ -n "$1" ]; then |
| 174 |
# if the user asked for anything to be included extra (like "xyz-archive-keyring"), mark those packages as manually installed |
| 175 |
IFS=","; includePackages=( $1 ); unset IFS |
| 176 |
apt-mark manual "${includePackages[@]}" |
| 177 |
fi |
| 178 |
fi |
| 179 |
' -- "$include" |
| 180 |
|
| 181 |
echo 'debuerreotype' > "$targetDir/etc/hostname" |
| 182 |
{ |
| 183 |
echo '# https://1.1.1.1 (privacy-focused, highly-available DNS service)' |
| 184 |
echo 'nameserver 1.1.1.1' |
| 185 |
echo 'nameserver 1.0.0.1' |
| 186 |
} > "$targetDir/etc/resolv.conf" |
| 187 |
chmod 0644 \ |
| 188 |
"$targetDir/etc/hostname" \ |
| 189 |
"$targetDir/etc/resolv.conf" |
| 190 |
|
| 191 |
# fix ownership/permissions on / (otherwise "debootstrap" leaves them as-is which causes reproducibility issues) |
| 192 |
chown 0:0 "$targetDir" |
| 193 |
chmod 0755 "$targetDir" |
| 194 |
|
| 195 |
# https://bugs.debian.org/857803 |
| 196 |
# adjust field 3 in /etc/shadow and /etc/shadow- to $(( epoch / 60 / 60 / 24 )), if it's larger |
| 197 |
sp_lstchg="$(( epoch / 60 / 60 / 24 ))" |
| 198 |
for shadowFile in etc/shadow etc/shadow-; do |
| 199 |
# --debian-eol etch and older do not include /etc/shadow- |
| 200 |
[ -e "$targetDir/$shadowFile" ] || continue |
| 201 |
|
| 202 |
newShadowFile="$shadowFile.debuerreotype" |
| 203 |
awk -F ':' \ |
| 204 |
-v OFS=':' \ |
| 205 |
-v sp_lstchg="$sp_lstchg" \ |
| 206 |
'{ |
| 207 |
if ($3 > sp_lstchg) { |
| 208 |
$3 = sp_lstchg |
| 209 |
} |
| 210 |
print |
| 211 |
}' "$targetDir/$shadowFile" > "$targetDir/$newShadowFile" |
| 212 |
if [ "$(< "$targetDir/$shadowFile")" != "$(< "$targetDir/$newShadowFile")" ]; then |
| 213 |
# use "cat" instead of "mv" so permissions don't change |
| 214 |
cat "$targetDir/$newShadowFile" > "$targetDir/$shadowFile" |
| 215 |
fi |
| 216 |
rm -f "$targetDir/$newShadowFile" |
| 217 |
done |