updated on Thu Jan 12 04:00:44 UTC 2012
[aur-mirror.git] / pactools / pt-pacman-cage
blobf9248e4963a81728cb05eea82301a1bb0a95619e
1 #!/bin/bash
3 # pacman-cage
5 # Copyright (c) 2002-2006 by Andrew Rose <rose.andrew@gmail.com>
6 # I used Judds pacman-optimise as a framework.
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 # USA.
24 myver='2.9.8.1'
25 dbroot="/var/lib/pacman"
26 pacmandb="/var/lib/pacman.db"
28 usage() {
29 echo "pacman-cage $myver"
30 echo "usage: $0 pacman_db_size(MB)"
31 echo
32 echo "pacman-cage creates a loopbacked filesystem in a contigious file."
33 echo "This will give better response times when using pacman"
34 echo "A safe value for pacman_db_size should be > 40"
35 echo
36 echo "If you are unsure, use this:"
37 echo " $0 60"
38 echo
41 die() {
42 echo "pacman-cage: $*" >&2
43 exit 1
46 die_r() {
47 rm -f /tmp/pacman.lck
48 die $*
51 loop_check=`zcat /proc/config.gz | grep CONFIG_BLK_DEV_LOOP | cut -d\= -f2`
53 if [ "$loop_check" == "m" ]; then
54 if [ `lsmod | grep loop | cut -d\ -f1` != "loop" ]; then
55 echo "Error. You have to load the module 'loop' in rc.conf."
56 exit 1
60 if [ "$loop_check" == "CONFIG_BLK_DEV_LOOP" ]; then
61 echo "Error. Your kernel config doesn't include CONFIG_BLK_DEV_LOOP."
62 exit 1
66 if [ "$#" != "1" ]; then echo "wrong number of parameters" 1>&2 ; usage; exit 0; fi
68 if [ "$1" != "" ]; then
69 if [ "$1" = "-h" -o "$1" = "--help" ]; then
70 usage
71 exit 0
73 dbsize=$1
76 if [ "`id -u`" != 0 ]; then
77 die "You must be root to cage the database"
80 # make sure pacman isn't running
81 if [ -f /tmp/pacman.lck ]; then
82 die "Pacman lockfile was found. Cannot run while pacman is running."
84 # make sure pacman.db hasnt already been made
85 if [ -f $pacmandb ]; then
86 die "$pacmandb already exists!."
89 if [ ! -d $dbroot ]; then
90 die "$dbroot does not exist or is not a directory"
93 # don't let pacman run while we do this
94 touch /tmp/pacman.lck
96 # step 1: sum the old db
97 echo "==> md5sum'ing the old database..."
98 find $dbroot -type f | sort | xargs md5sum >/tmp/pacsums.old
100 echo "==> creating pacman.db loopback file..."
101 dd if=/dev/zero of=$pacmandb bs=1M count=$dbsize > /dev/null 2>&1
103 echo "==> creating ext2 -O dir_index -b 1024 -m 0 on $pacmandb..."
104 yes | mkfs.ext2 -O dir_index -b 1024 -i 1024 -m 0 -F $pacmandb > /dev/null 2>&1
106 echo "==> creating temporary mount point /mnt/tmp-pacman.."
107 mkdir /mnt/tmp-pacman
109 echo "==> mounting pacman.db to temporary mount point..."
110 mount -o loop $pacmandb /mnt/tmp-pacman
112 echo "==> copying pacman database to temporary mount point..."
113 cp -a /var/lib/pacman/. /mnt/tmp-pacman
115 echo "==> unmounting temporary mount point..."
116 umount /mnt/tmp-pacman
118 echo "==> removing temporary mount point..."
119 rmdir /mnt/tmp-pacman
121 echo "==> moving old /var/lib/pacman to /var/lib/pacman.bak..."
122 mv /var/lib/pacman /var/lib/pacman.bak
124 echo "==> createing new pacman db mount point @ $dbroot..."
125 mkdir $dbroot
127 echo "==> Mounting new pacman db..."
128 mount -o loop $pacmandb $dbroot
130 echo "==> md5sum'ing the new database..."
131 find $dbroot -type f | sort | xargs md5sum >/tmp/pacsums.new
133 echo "==> checking integrity..."
134 diff /tmp/pacsums.old /tmp/pacsums.new >/dev/null 2>&1
135 if [ $? -ne 0 ]; then
136 # failed, move the old one back into place
137 umount $dbroot
138 rm $pacmandb
139 mv $dbroot.bak $dbroot
140 die_r "integrity check FAILED, reverting to old database"
143 echo "==> Updating /etc/fstab to reflect changes..."
144 echo "$pacmandb $dbroot ext2 loop,defaults 0 0" >> /etc/fstab
146 rm -f /tmp/pacman.lck /tmp/pacsums.old /tmp/pacsums.new
148 echo
149 echo "Finished. Your pacman database has been caged!. May the speedy pacman be with you."
150 echo
152 exit 0