Debian Codegraph @

bin/fai-mirror

raw symbols SCIP
1 #! /bin/bash
2
3 #*********************************************************************
4 #
5 # fai-mirror -- create and manage a partial mirror for FAI
6 #
7 # This script is part of FAI (Fully Automatic Installation)
8 # (c) 2004-2026, Thomas Lange, [email protected]
9 #
10 #*********************************************************************
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
15 #
16 # This program is distributed in the hope that it will be useful, but
17 # WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 # General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License
22 # along with this program; see the file COPYING. If not, write to the
23 # Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
24 # MA 02111-1307, USA.
25 #*********************************************************************
26
27 # variables: NFSROOT, FAI_CONFIGDIR, FAI_ETC_DIR
28
29 export FAI_ROOT=/ # do not execute in chroot, needed for install_packages call
30 export PATH=$PATH:/usr/sbin
31
32 trap "umount_dirs" EXIT ERR
33 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
34 usage() {
35
36 echo "fai-mirror -- create and manage a partial mirror for FAI."
37 echo "Please read the manual page fai-mirror(1)."
38 exit
39 }
40 # - - - - - - - - - - - - - - - - - - - - - - - - - -
41 die() {
42
43 local e=$1 # first parameter is the exit code
44 shift
45
46 echo "$*" >&2 # print error message
47 exit $e
48 }
49 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
50 excludeclass() {
51
52 # removes/excludes all classes in $* from $classes
53 local insert eclasses newclass c e
54
55 eclasses="$*"
56 eclasses=${eclasses//,/ }
57
58 for c in $classes; do
59 insert=1
60 for e in $eclasses; do
61 [ $c = $e ] && insert=0
62 done
63 [ $insert = 1 ] && newclass="$newclass $c"
64 done
65 classes="$newclass"
66 }
67 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
68 umount_dirs() {
69
70 [ "$FAI_DEBMIRROR" ] && umount $MNTPOINT 2>/dev/null 1>&2 || true
71 }
72 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
73 cleandirs() {
74
75 return # currently nothing to do
76 }
77 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
78 initialize() {
79
80 # TODO: root is only needed when FAI_DEBMIRROR is defined. Then we
81 # must mount a directory
82
83 aptcache=$mirrordir/aptcache # holds the package cache data
84 archivedir=$aptcache/var/cache/apt/archives
85 statefile=$aptcache/statefile
86
87 # also used in install_packages.conf
88 export aptoptions=" \
89 -o Aptitude::Log=/dev/null \
90 -o Aptitude::CmdLine::Ignore-Trust-Violations=yes\
91 -o APT::Get::AllowUnauthenticated=true \
92 -o Acquire::AllowInsecureRepositories=true \
93 -o DPkg::force-conflicts::=yes \
94 -o Acquire::Languages="none" \
95 -o Dir::Cache::srcpkgcache="" \
96 -o Dir::State=$aptcache/var/lib/apt \
97 -o Dir::Log=$aptcache/var/log/apt \
98 -o Dir::State::extended_states=$aptcache/var/lib/apt/lists/extended_states \
99 -o Dir::State::status=$statefile \
100 -o Dir::Cache=$aptcache/var/cache/apt \
101 -o Dir::Etc=$aptcache/etc/apt/"
102
103 # used by install_packages
104 export FAI_ARCHIVE_DIR=$archivedir
105
106 if [ -n "$arch" ]; then
107 aptoptions="$aptoptions -o APT::Architecture=$arch" # add architecture
108 fi
109
110 # we only need some empty dirs
111 set -e
112 mkdir -p $archivedir/partial $aptcache/etc/apt/preferences.d $aptcache/var/lib/apt/lists/partial $aptcache/var/log/apt
113 > $statefile
114 set +e
115 }
116 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
117 delete_base_packages() {
118
119 # now delete all packages that are already included in base.tar.xz
120 local p all arch
121
122 if [ ! -f $NFSROOT/var/tmp/base-pkgs.lis ]; then
123 echo "$NFSROOT/var/tmp/base-pkgs.lis not available."
124 echo "Can't remove wasteful packages that are already in base.tar.xz."
125 return
126 fi
127 echo "Removing packages that are already included in base.tar.xz"
128 for p in $(< $NFSROOT/var/tmp/base-pkgs.lis); do
129
130 all=(${p//:/ })
131 p=${all[0]}
132 arch=${all[1]}
133
134 if [ -f $archivedir/${p}_*$arch.deb ]; then
135 [ $verbose -eq 1 ] && echo "deleting package $p $arch"
136 rm $archivedir/${p}_*$arch.deb
137 fi
138 done
139 }
140 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
141 add_base_packages() {
142
143 local plist
144 # add packages from base.tar.xz and additional packages in nfsroot
145
146 # arch dependent packages defined in fai-make-nfsroot
147 echo "Adding packages of $cfdir/NFSROOT."
148 if [ -f $NFSROOT/var/tmp/packages.nfsroot ]; then
149 plist=$(< $NFSROOT/var/tmp/packages.nfsroot)
150 apt-get $qflag -d $aptoptions -y --fix-missing install $plist
151 else
152 echo "WARNING: $NFSROOT/var/tmp/packages.nfsroot does not exists." >&2
153 echo "Can't add those packages. Maybe the nfsroot is not yet created." >&2
154 fi
155
156 mv $aptcache/etc/apt/sources.list $aptcache # save sources.list
157 # now use different sources.list for debootstrap packages
158 echo "$FAI_DEBOOTSTRAP" | awk '{printf "deb %s %s main\n",$2,$1}' | sed -e 's/file:/copy:/' > $aptcache/etc/apt/sources.list
159 echo "Adding packages from $NFSROOT/var/tmp/base-pkgs.lis"
160 if [ -f $NFSROOT/var/tmp/base-pkgs.lis ]; then
161 plist=$(< $NFSROOT/var/tmp/base-pkgs.lis)
162 apt-get $qflag $aptoptions update >/dev/null
163 apt-get $qflag -d $aptoptions -y --fix-missing install $plist
164 else
165 echo "WARNING: $NFSROOT/var/tmp/base-pkgs.lis does not exists." >&2
166 echo "Can't add those packages. Maybe the nfsroot is not yet created." >&2
167 fi
168 mv $aptcache/sources.list $aptcache/etc/apt/sources.list # restore sources.list
169 apt-get $qflag $aptoptions update >/dev/null
170 }
171 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
172 set-classes() {
173
174 local addclasses
175 # if -c is given, ignore -x
176 if [ -n "$cclasses" ]; then
177 export classes=${cclasses//,/ }
178 return
179 fi
180
181 set +e
182 # all available file names are classes
183 classes=$(cd $FAI_CONFIGDIR/package_config; ls -1 | grep -E -i "^[a-zA-Z0-9_-]+$")
184 addclasses=$(grep -h PACKAGES $FAI_CONFIGDIR/package_config/* | perl -lane 'print join(" ", @F[2..99])' | grep -oP '\w+' )
185 export classes=$(echo -e "$classes\n$addclasses\n" | sort | uniq)
186 [ -n "$exclasses" ] && excludeclass $exclasses
187 set -e
188 }
189 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
190 mk_class_var_list() {
191
192 # create a list of files named class/*.var that contain a definition of a variable that is
193 # used in package_config/<classes>
194
195 # we assume, that variables used in package_config are defined in class/*.var.
196 # For all classes search if there's a variables used in package_config/CLASS
197 # For all variables found look which files class/CLASS.var define these variables.
198
199 local -a variables=()
200 local filelist classlist sourefiles
201 local c i f
202
203 #filelist: all files in package_config which are also a defined class
204 for c in $classes; do
205 if [ -f $FAI_CONFIGDIR/package_config/$c ]; then
206 filelist+=" $c"
207 fi
208 done
209
210 if [ -z "$filelist" ]; then
211 return
212 fi
213
214 # make the list of variables that are used
215 # regex: $abc or ${abc}
216 variables=($(cd $FAI_CONFIGDIR/package_config;grep -h -P -o '\$[A-Za-z0-9_]+|\$\{[A-Za-z0-9_]+\}' $filelist|sort|uniq))
217
218 # debug
219 #for v in ${variables[@]}; do
220 # echo Variable found: $v
221 #done
222
223 # create a regex like varname= for all variables
224 for i in ${!variables[@]}; do
225 variables[i]=$(echo ${variables[i]} | tr -d /{}$/)
226 variables[i]+="="
227 done
228
229 # classlist: make list of files in class, which are also a class
230 for c in $classes; do
231 if [ -f $FAI_CONFIGDIR/class/$c.var ]; then
232 classlist+=" $FAI_CONFIGDIR/class/$c.var"
233 fi
234 done
235 # look for variable definition in this list
236 for f in $classlist; do
237 for i in ${!variables[@]}; do
238 if grep -q -P "${variables[i]}" $f; then
239 sourefiles+=" $f"
240 break
241 fi
242 done
243 done
244
245 # these files contain a variable definition which is needed
246 echo $sourefiles
247 }
248 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
249
250 preserve=0
251 verbose=0
252 add=1
253 qflag=-qq
254 bcount=0
255 setvariables=0
256 ignoreconflicts=0
257 while getopts "a:bBvGhx:pc:C:m:s:P:VI" opt ; do
258 case "$opt" in
259 a) arch=$OPTARG ;;
260 B) add=0 ; ((bcount++)) ;;
261 b) add=2 ; ((bcount++)) ;;
262 C) cfdir=$OPTARG ;;
263 G) generic="-G" ;;
264 h) usage ;;
265 I) ignoreconflicts=1 ;;
266 x) exclasses="$OPTARG";;
267 c) cclasses="$OPTARG";;
268 m) export MAXPACKAGES="$OPTARG";;
269 p) preserve=1;;
270 s) csdir="$OPTARG";;
271 v) verbose=1; vflag=-v; qflag='';;
272 V) setvariables=1 ;;
273 P) aptpref="$OPTARG";;
274 ?) die 1 "Unknown option";;
275 esac
276 done
277 shift $((OPTIND - 1))
278
279 # use FAI_ETC_DIR from environment variable
280 [ -n "$FAI_ETC_DIR" -a -z "$cfdir" ] && echo "Using environment variable \$FAI_ETC_DIR."
281 # use -C option if present otherwise use $FAI_ETC_DIR or default to /etc/fai
282 [ -z "$cfdir" ] && cfdir=${FAI_ETC_DIR:=/etc/fai}
283 cfdir=$(readlink -f $cfdir) # canonicalize path
284 [ ! -d "$cfdir" ] && die 6 "$cfdir is not a directory"
285 [ "$verbose" -eq 1 ] && echo "Using configuration files from $cfdir"
286 [ $bcount -gt 1 ] && die 7 "You can't use -b and -B simultaneously."
287 . $cfdir/fai.conf
288 . $cfdir/nfsroot.conf
289 : ${MNTPOINT:=/media/mirror} # default value
290 export NFSROOT
291
292 if [ -n "$csdir" ]; then
293 FAI_CONFIGDIR=$csdir # override by -s
294 fi
295 command -v reprepro >&/dev/null || die 8 "reprepro not found. Please install the package."
296 [ -n "$exclasses" -a -n "$cclasses" ] && die 3 "Options -x and -c not allowed at the same time."
297
298 # use first argument if given, use variable mirrordir if not argument was given
299 [ -n "$1" ] && mirrordir=$1
300 [ -z "$mirrordir" ] && die 2 "Please give the absolute path to the mirror."
301 { echo $mirrordir | grep -E -q '^/'; } || die 4 "Mirrordir must start with a slash /."
302
303 [ -d $FAI_CONFIGDIR/package_config ] || die 6 "Can't find package config files in $FAI_CONFIGDIR."
304
305 set-classes
306 if [ "$setvariables" -eq 1 ]; then
307 flist=$(mk_class_var_list)
308 set -a
309 for f in $flist; do
310 echo Sourcing variable definitions in $f
311 source $flist
312 done
313 set +a
314 fi
315
316 initialize
317
318 # if we are using nfs mounts for Debian mirror, this may fail here, since inside a chroot environment different dir are used
319 # if sources.list includes file AND FAI_DEBMIRROR is defined we have to mount
320 # otherwise mounting is not needed. call task_mirror
321 if [ "$FAI_DEBMIRROR" ]; then
322 mkdir -p $MNTPOINT
323 mount -r $FAI_DEBMIRROR $MNTPOINT || exit 9
324 fi
325
326 cp -av $cfdir/apt/sources.list.d $aptcache/etc/apt
327 sed -e 's/file:/copy:/' $cfdir/apt/sources.list > $aptcache/etc/apt/sources.list
328 for f in `ls $cfdir/apt/sources.list.d/*.list 2>/dev/null`; do
329 sed -e 's/file:/copy:/' $f > $aptcache/etc/apt/sources.list.d/$f
330 done
331
332 if [ -f "$aptpref" ]; then
333 cp $vflag "$aptpref" $aptcache/etc/apt/preferences
334 fi
335 if [ -d $cfdir/apt/preferences.d ]; then
336 cp -a $vflag $cfdir/apt/preferences.d $aptcache/etc/apt
337 fi
338
339 # use the trusted keys from the host
340 cp -a /etc/apt/trusted.gpg.d $aptcache/etc/apt
341
342 echo "Getting package information"
343 apt-get $qflag $aptoptions update >/dev/null
344
345 if [ "$ignoreconflicts" -eq 1 ]; then
346 # ignore all package conflicts
347 sed -i -e '/^Conflicts:/d' $mirrordir/aptcache/var/lib/apt/lists/*Packages
348 apt-cache $qflag $aptoptions gencaches
349 fi
350
351 [ $add -eq 1 ] && add_base_packages
352 echo "Downloading packages for classes:" $classes
353 if FAI=$FAI_CONFIGDIR install_packages -d $vflag $generic; then
354 :
355 else
356 echo "ERROR when downloading packages. Your mirror may be broken or you have used a negation in a boolean expression in packages_config. Use fai-mirror -v for details."
357 fi
358 umount_dirs
359 trap "" EXIT ERR
360 [ $add -eq 0 ] && delete_base_packages
361
362
363 # in still undefined, use host architecture
364 if [ -z "$arch" ]; then
365 arch=$(dpkg --print-architecture)
366 arch="$arch $(dpkg --print-foreign-architectures)"
367 fi
368
369 # create mirror directory structure
370 echo "Calling reprepro"
371 mkdir $mirrordir/conf
372 cat > $mirrordir/conf/distributions <<EOF # generate config file for reprepro
373 Codename: cskoeln
374 Architectures: $arch
375 Components: main non-free contrib non-free-firmware
376 DebIndices: Packages Release . .xz
377 Description: Package repository created by fai-mirror
378 Label: fai-project.org
379 Origin: fai-mirror
380 EOF
381
382 # check if backports are used
383 bponame=$(grep -E '^deb ' $aptcache/etc/apt/sources.list | grep -P -o '\S+-backports' | head -n 1)
384 if [ -z "$bponame" ]; then
385 bponame=$(grep -hPo '(?<=Suites:.)\S+-backports' $aptcache/etc/apt/sources.list.d/*|head -n 1)
386 fi
387
388 # add entry for backports
389 if [ -n "$bponame" ]; then
390 cat >> $mirrordir/conf/distributions <<EOF
391
392 Codename: $bponame
393 Architectures: $arch
394 Components: main non-free contrib non-free-firmware
395 DebIndices: Packages Release . .xz
396 Description: Package repository created by fai-mirror
397 Label: fai-project.org
398 Origin: fai-mirror
399 EOF
400
401 # maybe using reprepro pulls it's possible to move instead of copy the packages
402 if ls $archivedir/*~bpo[0-9]*+[0-9-]*_*.deb >/dev/null 2>&1; then
403 reprepro -b $mirrordir includedeb $bponame $archivedir/*~bpo[0-9]*+[0-9-]*_*.deb
404 rm -f $archivedir/*~bpo[0-9]*+[0-9-]*_*.deb
405 fi
406 fi
407
408 # allow package without the .deb suffix. At least they must start with a char and contain an underscore or hyphen
409 reprepro --ignore=extension -b $mirrordir includedeb cskoeln $archivedir/[0-9a-zA-Z]*[_-]*
410 rm -r $archivedir/[0-9a-zA-Z]*[_-]*
411
412 echo "$0 finished."
413 echo -n "Number of packages in the mirror: "
414 find $mirrordir -name \*.deb | wc -l
415 echo -n "Mirror size and location: ";du -sh --exclude aptcache --exclude db $mirrordir
416 cleandirs