#!/bin/bash # # pacman-cage # # Copyright (c) 2002-2006 by Pablo Nicolas Diaz # Copyright (c) 2002-2006 by Andrew Rose # I used Judds pacman-optimize as a framework. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, # USA. # myver='2.9.8.3' dbroot="/var/lib/pacman/" pacmandb="/var/lib/pacman-X.db" # filesystem used in pacmandb filesystem="ext2" # commandline needed for mkfs.$filesystem and dd BlockSize="1024" Options="-F -O dir_index -b $BlockSize -i 1024 -m 0" # temporal mountpoint pacman_tmp_mnt_point=/mnt/tmp-pacman usage() { echo "pacman-cage $myver" echo "usage: $0 [pacman_db_root]" echo echo "pacman-cage creates a loopbacked filesystem in a contigious file." echo "This will give better response times when using pacman" echo } die() { echo "pacman-cage: $*" >&2 exit 1 } die_r() { rm -f /tmp/pacman.lck die $* } if [ "$1" != "" ]; then if [ "$1" = "-h" -o "$1" = "--help" ]; then usage exit 0 fi dbroot=$1 fi # test if filesystem support is present if ! grep $filesystem /proc/filesystems > /dev/null; then die "filesystem $filesystem is not supported!" fi # added by wain: make sure kernel support loop device if ! zcat /proc/config.gz | grep "BLK_DEV_LOOP=\(y\|m\)" >/dev/null; then die "please enable loop device support in kernel (BLK_DEV_LOOP=y)" elif [ -d /dev/loop/ ]; then die "You must insert the loopback module!" fi if [ "`id -u`" != 0 ]; then die "You must be root to cage the database" fi # make sure pacman isn't running if [ -f /tmp/pacman.lck ]; then die "Pacman lockfile was found. Cannot run while pacman is running." fi # make sure pacman.db hasnt already been made if [ -f $pacmandb ]; then die "$pacmandb already exists!." fi if [ ! -d $dbroot ]; then die "$dbroot does not exist or is not a directory" fi # don't let pacman run while we do this touch /tmp/pacman.lck # step 1: sum the old db echo "==> md5sum'ing the old database..." find $dbroot -type f | sort | xargs md5sum >/tmp/pacsums.old echo "==> creating pacman.db loopback file..." CountSize=$(( $(du -s --block-size=$BlockSize $dbroot | cut -f 1) * 5/3 )) # echo $CountSize dd if=/dev/zero of=$pacmandb bs=$BlockSize count=$CountSize > /dev/null 2>&1 echo "==> creating $filesystem $Options on $pacmandb ..." mkfs.$filesystem $Options $pacmandb echo "==> creating temporary mount point $pacman_tmp_mnt_point.." mkdir $pacman_tmp_mnt_point; dir $pacman_tmp_mnt_point echo "==> mounting pacman.db to temporary mount point..." mount -t $filesystem -o loop $pacmandb $pacman_tmp_mnt_point echo "==> copying pacman database to temporary mount point..." cp -a $dbroot. $pacman_tmp_mnt_point echo "==> unmounting temporary mount point..." umount $pacman_tmp_mnt_point echo "==> removing temporary mount point..." rmdir $pacman_tmp_mnt_point echo "==> moving old /var/lib/pacman to /var/lib/pacman.bak..." mv $dbroot /var/lib/pacman.bak echo "==> createing new pacman db mount point @ $dbroot..." mkdir $dbroot echo "==> Mounting new pacman db..." mount -t $filesystem -o loop $pacmandb $dbroot echo "==> md5sum'ing the new database..." find $dbroot -type f | sort | xargs md5sum >/tmp/pacsums.new echo "==> checking integrity..." diff /tmp/pacsums.old /tmp/pacsums.new >/dev/null 2>&1 if [ $? -ne 0 ]; then # failed, move the old one back into place umount $dbroot rm $pacmandb mv $dbroot.bak $dbroot die_r "integrity check FAILED, reverting to old database" fi rm -f /tmp/pacman.lck /tmp/pacsums.old /tmp/pacsums.new # Replaced by wain: mount is now in /etc/rc.d/pacmandb daemon #echo "==> Updating /etc/fstab to reflect changes..." #echo "$pacmandb $dbroot $filesystem loop,defaults 0 0" >> /etc/fstab echo echo "Finished. Your pacman database has been caged!. May the speedy pacman be with you." echo exit 0