/[fai]/trunk/bin/fai-mirror
ViewVC logotype

Contents of /trunk/bin/fai-mirror

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5612 - (hide annotations) (download)
Wed Oct 28 10:55:50 2009 UTC (3 years, 6 months ago) by lange
File size: 10724 byte(s)
set default values using bash : ${variable:=value}
1 lange 2566 #! /bin/bash
2    
3     # $Id$
4     #*********************************************************************
5     #
6 lange 5222 # fai-mirror -- create and manage a partial mirror for FAI
7 lange 2566 #
8     # This script is part of FAI (Fully Automatic Installation)
9 lange 5329 # (c) 2004-2009, Thomas Lange, lange@informatik.uni-koeln.de
10 lange 2566 #
11     #*********************************************************************
12     # This program is free software; you can redistribute it and/or modify
13     # it under the terms of the GNU General Public License as published by
14     # the Free Software Foundation; either version 2 of the License, or
15     # (at your option) any later version.
16     #
17     # This program is distributed in the hope that it will be useful, but
18     # WITHOUT ANY WARRANTY; without even the implied warranty of
19     # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20     # General Public License for more details.
21     #
22     # You should have received a copy of the GNU General Public License
23     # along with this program; see the file COPYING. If not, write to the
24     # Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25     # MA 02111-1307, USA.
26     #*********************************************************************
27    
28 lange 5587 version="Version 1.11, 22-october-2009"
29 lange 2566
30 lange 4556 # variables: NFSROOT, FAI_CONFIGDIR, FAI_ETC_DIR
31 lange 2566
32 lange 4779 export FAI_ROOT=/ # do not execute in chroot, needed for install_packages call
33 lange 4243 export PATH=$PATH:/usr/sbin
34 lange 3399
35 lange 2566 trap "umount_dirs" EXIT ERR
36     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
37     usage() {
38    
39 lange 5222 echo "fai-mirror -- create and manage a partial mirror for FAI."
40 lange 2588 echo "$version"
41 lange 5097 echo "Please read the manual page fai-mirror(1)."
42 lange 2566 exit
43     }
44 lange 2590 # - - - - - - - - - - - - - - - - - - - - - - - - - -
45     die() {
46    
47     local e=$1 # first parameter is the exit code
48     shift
49    
50     echo "$@" # print error message
51     exit $e
52     }
53 lange 2566 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
54     excludeclass() {
55    
56     # removes/excludes all classes in $* from $classes
57     local insert eclasses newclass c e
58    
59 lange 2724 eclasses="$*"
60     eclasses=${eclasses//,/ }
61    
62 lange 2566 for c in $classes; do
63     insert=1
64     for e in $eclasses; do
65     [ $c = $e ] && insert=0
66     done
67     [ $insert = 1 ] && newclass="$newclass $c"
68     done
69     classes="$newclass"
70     }
71     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
72     umount_dirs() {
73    
74 lange 4704 [ "$FAI_DEBMIRROR" ] && umount $MNTPOINT 2>/dev/null 1>&2 || true
75 lange 2566 }
76     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
77     cleandirs() {
78    
79 lange 2576 return # currently nothing to do
80 lange 2566 # rm $statefile
81     # rm -rf $mirrordir/.apt-move $statefile
82     # [ $debug -eq 1 ] || rm -rf $aptcache $archivedir
83     # rm -rf $aptcache $archivedir
84     }
85     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
86     initialize() {
87    
88     # TODO: root is only needed when FAI_DEBMIRROR is defined. Then we
89     # must mount a directory
90    
91 lange 3915 debdist=$(echo "$FAI_DEBOOTSTRAP" | awk '{print $1}')
92 lange 2566 # store all packages temporary in the mirror partition so we need only a move,
93     # not a copy later during apt-move
94     aptcache=$mirrordir/aptcache # holds the package cache data
95     archivedir=$aptcache/var/cache/apt/archives
96     aptmovefile=$aptcache/etc/apt-move.conf # stores apt-move.conf
97 lange 2631 statefile=$aptcache/statefile
98 lange 2566
99 lange 4470 # also used in install_packages.conf
100 lange 2566 export aptoptions=" \
101 lange 2650 -o Aptitude::Log=/dev/null \
102 lange 4366 -o Aptitude::CmdLine::Ignore-Trust-Violations=yes\
103     -o APT::Get::AllowUnauthenticated=true \
104 lange 3073 -o DPkg::force-conflicts::=yes \
105 lange 2566 -o Dir::State::status=$statefile \
106 lange 4366 -o APT::Get::Force-Yes=true \
107 lange 2566 -o Dir::Cache=$aptcache/var/cache/apt \
108     -o Dir::State=$aptcache/var/cache/apt \
109     -o Dir::Cache::Archives=$archivedir \
110 lange 5359 -o Dir::Etc=$aptcache/etc/apt/ \
111 lange 2566 -o Dir::State::Lists=$aptcache/var/lib/apt/lists/"
112    
113 lange 5585 if [ -n "$arch" ]; then
114     aptoptions="$aptoptions -o APT::Architecture=$arch" # add architecture
115     fi
116    
117 lange 2566 # not needed
118     # -o APT::Get::ReInstall
119     # ."-o APT::Get::Download-Only=true -o Aptitude::Cmd-Line::Download-Only=true "
120    
121     # we only need some empty dirs
122 lange 4470 set -e
123     mkdir -p $archivedir/partial $aptcache/etc/apt $aptcache/var/lib/apt/lists/partial
124 lange 2566 > $statefile
125 lange 4470 set +e
126 lange 2566 }
127     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
128     delete_base_packages() {
129    
130     # now delete all packages that are already included in base.tgz
131     local p
132    
133 lange 2590 if [ ! -f $NFSROOT/var/tmp/base-pkgs.lis ]; then
134     echo "$NFSROOT/var/tmp/base-pkgs.lis not available."
135     echo "Can't remove wasteful packages that are already in base.tgz."
136     return
137     fi
138 lange 2566 echo "Removing packages that are already included in base.tgz"
139     for p in $(cat $NFSROOT/var/tmp/base-pkgs.lis); do
140     if [ -f $archivedir/${p}_*.deb ]; then
141     [ $verbose -eq 1 ] && echo "deleting package $p"
142     rm $archivedir/${p}_*.deb
143     # else commands only for debugging
144     # else
145     # echo "package $p not found"
146     # ls $archivedir/${p}_*.deb
147     fi
148     done
149     }
150     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
151 lange 2631 add_base_packages() {
152    
153     local plist
154     # add packages from base.tgz and additional packages in nfsroot
155    
156 lange 2675 # arch dependent packages defined in make-fai-nfsroot
157 lange 3838 echo "Adding packages of $cfdir/NFSROOT."
158     if [ -f $NFSROOT/var/tmp/packages.nfsroot ]; then
159     plist=$(< $NFSROOT/var/tmp/packages.nfsroot)
160 lange 2861 apt-get $qflag -d $aptoptions -y --force-yes --fix-missing install $plist
161 lange 3331 else
162 lange 3838 echo "WARNING: $NFSROOT/var/tmp/packages.nfsroot does not exists."
163 lange 3331 echo "Can't add those packages. Maybe the nfsroot is not yet created."
164 lange 2675 fi
165    
166 lange 3395 # now use sources.list for debootstrap packages
167     echo "$FAI_DEBOOTSTRAP" | awk '{printf "deb %s %s main\n",$2,$1}' | perl -p -e 's/file:/copy:/' > $aptcache/etc/apt/sources.list
168 lange 5334 echo "Adding packages from $NFSROOT/var/tmp/base-pkgs.lis"
169 lange 3395 if [ -f $NFSROOT/var/tmp/base-pkgs.lis ]; then
170     plist=$(< $NFSROOT/var/tmp/base-pkgs.lis)
171 holger 5272 apt-get $qflag $aptoptions update >/dev/null
172 lange 3395 apt-get $qflag -d $aptoptions -y --force-yes --fix-missing install $plist
173     else
174     echo "WARNING: $NFSROOT/var/tmp/base-pkgs.lis does not exists."
175     echo "Can't add those packages. Maybe the nfsroot is not yet created."
176     fi
177 lange 2631 }
178     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
179 lange 2590 set-classes() {
180 lange 2566
181 lange 2590 # if -c is given, ignore -x
182 lange 2592 if [ -n "$cclasses" ]; then
183 lange 2590 export classes=${cclasses//,/ }
184     return
185     fi
186    
187 lange 2592 set +e
188 lange 2590 # all available file names are classes
189 lange 4535 classes=$(cd $FAI_CONFIGDIR/package_config; ls -1 | egrep -i "^[a-zA-Z0-9_-]+$")
190 lange 3398 addclasses=$(grep -h PACKAGES $FAI_CONFIGDIR/package_config/* | sed -e 's/#.*//' | awk '{printf $3"\n"$4"\n"$5"\n"$6"\n"}')
191 lange 2590 export classes=$(echo -e "$classes\n$addclasses\n" | sort | uniq)
192     [ -n "$exclasses" ] && excludeclass $exclasses
193 lange 2592 set -e
194 lange 2590 }
195     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
196    
197 lange 2780 [ -x "$(which apt-move)" ] || die 5 "apt-move not found. Please install package."
198 lange 2663
199 lange 2566 preserve=0
200     verbose=0
201 lange 3928 add=1
202 lange 2861 qflag=-qq
203 lange 5585 while getopts "a:Bvhx:pc:C:" opt ; do
204 lange 2566 case "$opt" in
205 lange 5585 a) arch=$OPTARG ;;
206 lange 3928 B) add=0 ;;
207 lange 3399 C) cfdir=$OPTARG ;;
208 lange 2566 h) usage ;;
209     x) exclasses="$OPTARG";;
210 lange 2590 c) cclasses="$OPTARG";;
211 lange 2566 p) preserve=1;;
212 lange 2861 v) verbose=1; vflag=-v; qflag='';;
213 lange 2631 ?) die 1 "Unknown option";;
214 lange 2566 esac
215     done
216     shift $(($OPTIND - 1))
217    
218 lange 3949 # use FAI_ETC_DIR from environment variable
219     if [ -n "$FAI_ETC_DIR" -a -z "$cfdir" ]; then
220     echo "Using environment variable \$FAI_ETC_DIR."
221     fi
222 lange 5329 # use -C option if present otherwise use $FAI_ETC_DIR or default to /etc/fai
223     if [ -z "$cfdir" ]; then
224     cfdir=${FAI_ETC_DIR:=/etc/fai}
225     fi
226 lange 3949 cfdir=$(readlink -f $cfdir) # canonicalize path
227 lange 3399 if [ ! -d "$cfdir" ]; then
228     echo "$cfdir is not a directory"
229     exit 6
230     fi
231 holger 5272 [ "$verbose" -eq 1 ] && echo "Using configuration files from $cfdir"
232 lange 3399 . $cfdir/fai.conf
233     . $cfdir/make-fai-nfsroot.conf
234 lange 4386 export NFSROOT="$NFSROOT/live/filesystem.dir"
235 lange 3399
236 lange 3838 [ -n "$packages" ] && die "WARNING: The use of \$packages in make-fai-nfsroot.conf is now deprecated. Please include this information into $cfdir/NFSROOT."
237     [ -n "$NFSROOT_PACKAGES" ] && die "WARNING: The use of \$packages in make-fai-nfsroot.conf is now deprecated. Please include this information into $cfdir/NFSROOT."
238    
239 lange 2590 [ -n "$exclasses" -a -n "$cclasses" ] && die 3 "Options -x and -c not allowed at the same time."
240 lange 2566
241     mirrordir=$1
242 lange 2576 if [ -z "$mirrordir" ]; then
243 lange 3087 die 2 "Please give the absolute path to the mirror."
244 lange 2576 fi
245 lange 3087 { echo $mirrordir | egrep -q '^/'; } || die 4 "Mirrordir must start with a slash /."
246 lange 2566 if [ -d $mirrordir/pool -o -d $mirrordir/dists ]; then
247 lange 2631 die 3 "Please first remove $mirrordir/pool and $mirrordir/dists"
248 lange 2566 fi
249    
250 lange 3134 [ -d $FAI_CONFIGDIR/package_config ] || die 6 "Can't find package config files in $FAI_CONFIGDIR."
251    
252 lange 4813 # set default if undefined
253 lange 5612 : ${MAXPACKAGES:=1}
254     export MAXPACKAGES
255 lange 4813
256 lange 2566 initialize
257    
258     # if we are using nfs mounts for Debian mirror, this may fail here, since inside a chroot environment different dir are used
259    
260     # if sources.list includes file AND FAI_DEBMIRROR is defined we have to mount
261     # otherwise mounting is not needed. call task_mirror
262    
263 lange 2631 if [ "$FAI_DEBMIRROR" ]; then
264 lange 4704 mkdir -p $MNTPOINT
265     mount -r $FAI_DEBMIRROR $MNTPOINT || exit 9
266 lange 2631 fi
267    
268 lange 2566 # TODO: use -p to preserve sources.list
269 lange 4556 perl -p -e 's/file:/copy:/' $cfdir/apt/sources.list > $aptcache/etc/apt/sources.list
270 lange 2566
271     echo "Getting package information"
272 holger 5272 apt-get $qflag $aptoptions update >/dev/null
273 lange 2566
274 lange 2590 set-classes
275 lange 2566 echo "Downloading packages for classes:" $classes
276 lange 2631 FAI=$FAI_CONFIGDIR install_packages -d $vflag
277     [ $add -eq 1 ] && add_base_packages
278 lange 2566 umount_dirs
279 lange 2576 trap "" EXIT ERR
280 lange 2631 [ $add -eq 0 ] && delete_base_packages
281 lange 2566
282     # create mirror directory structure
283     echo "Calling apt-move"
284     cat > $aptmovefile <<EOF # generate apt-move.conf
285     APTSITES=*
286     LOCALDIR=$mirrordir
287 lange 2734 DIST=$debdist
288 lange 2566 FILECACHE=$archivedir
289     LISTSTATE=$aptcache/var/lib/apt/lists
290     DELETE=no
291     CONTENTS=no
292 lange 2631 PKGCOMP='none gzip'
293 lange 2566 EOF
294 lange 5586 # due to bug #441231 in apt-move, we have to specify the arhcitecture
295 lange 5585 [ -n "$arch" ] && echo "ARCH=$arch" >> $aptmovefile # append architecture
296 holger 5272 apt-move $qflag -c $aptmovefile update
297 lange 3452 # since Packages.gz from apt-move does not include packages from my
298 lange 3438 # repository, let's use apt-ftparchive for generiating correct index
299     # files
300 lange 3452 pfilegz=$(find $mirrordir/dists -name Packages.gz)
301     pfile=$(find $mirrordir/dists -name Packages)
302 lange 3438 pdist=$(cd $mirrordir/dists ; ls)
303     cd $mirrordir
304 lange 3452 # md5sums of apt-move are not valid, when we recreate Packages.gz using
305     # apt-ftparchive, but we can use the header of the Release file
306     grep -B99 MD5Sum: $mirrordir/dists/$pdist/Release | grep -v MD5Sum: > $mirrordir/tmpfile
307     rm $mirrordir/dists/$pdist/Release
308     apt-ftparchive packages pool > $pfile
309     gzip -c $pfile > $pfilegz
310 lange 3445 apt-ftparchive release dists/$pdist >> tmpfile
311 lange 3443 mv tmpfile dists/$pdist/Release
312 lange 3438
313 lange 2566 echo "$0 finished."
314     echo -n "Mirror size and location: ";du -sh $mirrordir
315     cleandirs

Properties

Name Value
svn:eol-style native
svn:executable *
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.5