| 1 |
#! /bin/bash
|
| 2 |
|
| 3 |
# $Id$
|
| 4 |
#*********************************************************************
|
| 5 |
#
|
| 6 |
# fai-mirror -- create and manage a partitial mirror for FAI
|
| 7 |
#
|
| 8 |
# This script is part of FAI (Fully Automatic Installation)
|
| 9 |
# (c) 2004-2005, Thomas Lange, lange@informatik.uni-koeln.de
|
| 10 |
#
|
| 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 |
version="Version 1.2, 13-jan-2005"
|
| 29 |
|
| 30 |
# additional files: install_packages.conf
|
| 31 |
# variables: NFSROOT, FAI_CONFIGDIR
|
| 32 |
|
| 33 |
# TODO:
|
| 34 |
# - add support for package that will be removed like: lilo-
|
| 35 |
# currently you must remove those package from you package_config files
|
| 36 |
|
| 37 |
set -a
|
| 38 |
. /etc/fai/fai.conf
|
| 39 |
FAI_ROOT=/ # do not execute in chroot
|
| 40 |
[ -z "$MAXPACKAGES" ] && MAXPACKAGES=9999
|
| 41 |
set +a
|
| 42 |
|
| 43 |
trap "umount_dirs" EXIT ERR
|
| 44 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 45 |
usage() {
|
| 46 |
|
| 47 |
echo "fai-mirror -- create and manage a partitial mirror for FAI. $version"
|
| 48 |
echo "Please the the manual page fai-mirror(1)."
|
| 49 |
exit
|
| 50 |
}
|
| 51 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 52 |
excludeclass() {
|
| 53 |
|
| 54 |
# removes/excludes all classes in $* from $classes
|
| 55 |
local insert eclasses newclass c e
|
| 56 |
|
| 57 |
eclasses=$(echo $* | sed "s/,/ /g")
|
| 58 |
|
| 59 |
for c in $classes; do
|
| 60 |
insert=1
|
| 61 |
for e in $eclasses; do
|
| 62 |
[ $c = $e ] && insert=0
|
| 63 |
done
|
| 64 |
[ $insert = 1 ] && newclass="$newclass $c"
|
| 65 |
done
|
| 66 |
classes="$newclass"
|
| 67 |
}
|
| 68 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 69 |
get_addpackages() {
|
| 70 |
|
| 71 |
# add packages (mostly kernels) which are defined in the variable $addpackages
|
| 72 |
# TODO: maybe better to create a tmp file with all these package
|
| 73 |
# names, then to call install_packages for the tmp file
|
| 74 |
|
| 75 |
local f p pkg pkglist
|
| 76 |
|
| 77 |
echo -n "Adding packages from variable \$addpackages:"
|
| 78 |
for f in $(echo $FAI_CONFIGDIR/class/*.var); do
|
| 79 |
pkg=$(egrep ^addpackages= $f | sed 's/addpackages=//'|sed 's/"//g'| perl -pe 's/\$[\w_-]+//g')
|
| 80 |
pkglist="$pkglist $pkg"
|
| 81 |
done
|
| 82 |
|
| 83 |
# loop over the list, because maybe some packages doesn't exist in the
|
| 84 |
# partitial mirror, but only in the local repository. These can't be
|
| 85 |
# downloaded via the normal method
|
| 86 |
for p in $pkglist; do
|
| 87 |
echo -n " $p"
|
| 88 |
# test if .deb file is available
|
| 89 |
if [ ! -f $FAI_CONFIG/files/packages/${p}_*.deb ]; then
|
| 90 |
apt-get -qq -d $aptoptions -y --force-yes --fix-missing install $p
|
| 91 |
fi
|
| 92 |
done
|
| 93 |
echo ""
|
| 94 |
}
|
| 95 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 96 |
umount_dirs() {
|
| 97 |
|
| 98 |
[ "$FAI_DEBMIRROR" ] && umount $FAI_ROOT/$MNTPOINT 2>/dev/null 1>&2
|
| 99 |
}
|
| 100 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 101 |
cleandirs() {
|
| 102 |
|
| 103 |
return # currently nothing to do
|
| 104 |
# rm $statefile
|
| 105 |
# rm -rf $mirrordir/.apt-move $statefile
|
| 106 |
# [ $debug -eq 1 ] || rm -rf $aptcache $archivedir
|
| 107 |
# rm -rf $aptcache $archivedir
|
| 108 |
}
|
| 109 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 110 |
initialize() {
|
| 111 |
|
| 112 |
# TODO: root is only needed when FAI_DEBMIRROR is defined. Then we
|
| 113 |
# must mount a directory
|
| 114 |
|
| 115 |
if [ ! -f /etc/fai/install_packages.conf ]; then
|
| 116 |
echo "/etc/fai/install_packages.conf not found. Aborting."
|
| 117 |
exit 3
|
| 118 |
fi
|
| 119 |
|
| 120 |
# store all packages temporary in the mirror partition so we need only a move,
|
| 121 |
# not a copy later during apt-move
|
| 122 |
aptcache=$mirrordir/aptcache # holds the package cache data
|
| 123 |
archivedir=$aptcache/var/cache/apt/archives
|
| 124 |
aptmovefile=$aptcache/etc/apt-move.conf # stores apt-move.conf
|
| 125 |
statefile=$aptcache/statefile # also used in install_packages.conf
|
| 126 |
|
| 127 |
# also used in install_pacakges.conf
|
| 128 |
export aptoptions=" \
|
| 129 |
-o Dir::State::status=$statefile \
|
| 130 |
-o Dir::Cache=$aptcache/var/cache/apt \
|
| 131 |
-o Dir::State=$aptcache/var/cache/apt \
|
| 132 |
-o Dir::Cache::Archives=$archivedir \
|
| 133 |
-o Dir::Etc::sourcelist=$aptcache/etc/apt/sources.list \
|
| 134 |
-o Dir::State::Lists=$aptcache/var/lib/apt/lists/"
|
| 135 |
|
| 136 |
# not needed
|
| 137 |
# -o APT::Get::ReInstall
|
| 138 |
# ."-o APT::Get::Download-Only=true -o Aptitude::Cmd-Line::Download-Only=true "
|
| 139 |
|
| 140 |
# we only need some empty dirs
|
| 141 |
mkdir -p $archivedir/partial $aptcache/etc/apt
|
| 142 |
mkdir -p $aptcache/var/lib/apt/lists/partial
|
| 143 |
> $statefile
|
| 144 |
}
|
| 145 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 146 |
delete_base_packages() {
|
| 147 |
|
| 148 |
# now delete all packages that are already included in base.tgz
|
| 149 |
local p
|
| 150 |
|
| 151 |
echo "Removing packages that are already included in base.tgz"
|
| 152 |
for p in $(cat $NFSROOT/var/tmp/base-pkgs.lis); do
|
| 153 |
if [ -f $archivedir/${p}_*.deb ]; then
|
| 154 |
[ $verbose -eq 1 ] && echo "deleting package $p"
|
| 155 |
rm $archivedir/${p}_*.deb
|
| 156 |
# else commands only for debugging
|
| 157 |
# else
|
| 158 |
# echo "package $p not found"
|
| 159 |
# ls $archivedir/${p}_*.deb
|
| 160 |
fi
|
| 161 |
done
|
| 162 |
}
|
| 163 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 164 |
|
| 165 |
preserve=0
|
| 166 |
verbose=0
|
| 167 |
while getopts "vhx:p" opt ; do
|
| 168 |
case "$opt" in
|
| 169 |
h) usage ;;
|
| 170 |
x) exclasses="$OPTARG";;
|
| 171 |
p) preserve=1;;
|
| 172 |
v) verbose=1; vflag=-v;;
|
| 173 |
?) echo "Unknown option"; exit 1;;
|
| 174 |
esac
|
| 175 |
done
|
| 176 |
shift $(($OPTIND - 1))
|
| 177 |
|
| 178 |
debug=0
|
| 179 |
[ $debug -eq 0 ] && quiet=-q
|
| 180 |
|
| 181 |
mirrordir=$1
|
| 182 |
if [ -z "$mirrordir" ]; then
|
| 183 |
echo "Please give the directory for the mirror."
|
| 184 |
exit 2
|
| 185 |
fi
|
| 186 |
if [ -d $mirrordir/pool -o -d $mirrordir/dists ]; then
|
| 187 |
echo "Please first remove $mirrordir/pool and $mirrordir/dists"
|
| 188 |
exit 3
|
| 189 |
fi
|
| 190 |
|
| 191 |
initialize
|
| 192 |
|
| 193 |
# if we are using nfs mounts for Debian mirror, this may fail here, since inside a chroot environment different dir are used
|
| 194 |
|
| 195 |
# if sources.list includes file AND FAI_DEBMIRROR is defined we have to mount
|
| 196 |
# otherwise mounting is not needed. call task_mirror
|
| 197 |
|
| 198 |
[ "$FAI_DEBMIRROR" ] && mount -r $FAI_DEBMIRROR $FAI_ROOT/$MNTPOINT
|
| 199 |
# TODO: use -p to preserve sources.list
|
| 200 |
perl -p -e 's/file:/copy:/' /etc/fai/sources.list > $aptcache/etc/apt/sources.list
|
| 201 |
|
| 202 |
echo "Getting package information"
|
| 203 |
apt-get $quiet $aptoptions update >/dev/null
|
| 204 |
|
| 205 |
# all available file names are classes
|
| 206 |
classes=$(cd $FAI_CONFIGDIR/package_config; ls -1 | egrep "^[A-Z0-9_]+$")
|
| 207 |
addclasses=$(grep -h PACKAGES $FAI_CONFIGDIR/package_config/* | awk '{print $3}')
|
| 208 |
export classes=$(echo -e "$classes\n$addclasses\n" | sort | uniq)
|
| 209 |
[ -n "$exclasses" ] && excludeclass $exclasses
|
| 210 |
|
| 211 |
echo "Downloading packages for classes:" $classes
|
| 212 |
FAI=$FAI_CONFIGDIR install_packages -i $vflag -f /etc/fai
|
| 213 |
get_addpackages
|
| 214 |
umount_dirs
|
| 215 |
trap "" EXIT ERR
|
| 216 |
delete_base_packages
|
| 217 |
|
| 218 |
# create mirror directory structure
|
| 219 |
echo "Calling apt-move"
|
| 220 |
cat > $aptmovefile <<EOF # generate apt-move.conf
|
| 221 |
APTSITES=*
|
| 222 |
LOCALDIR=$mirrordir
|
| 223 |
DIST=sarge
|
| 224 |
FILECACHE=$archivedir
|
| 225 |
LISTSTATE=$aptcache/var/lib/apt/lists
|
| 226 |
DELETE=no
|
| 227 |
CONTENTS=no
|
| 228 |
EOF
|
| 229 |
apt-move $quiet -c $aptmovefile update
|
| 230 |
echo "$0 finished."
|
| 231 |
echo -n "Mirror size and location: ";du -sh $mirrordir
|
| 232 |
cleandirs
|