- moved here from sysv/
[util-vserver.git] / scripts / vunify.sh
1 #!/bin/sh
2
3 # Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 # based on vunify by Jacques Gelinas
5 #  
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2, or (at your option)
9 # any later version.
10 #  
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #  
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20 # This scripts is used to unify the disk space used by vservers
21 # It takes various RPM packages and hard link them together so all
22 # vservers are sharing the same exact copy of the files.
23 # After doing so, it set them immutable, so the vserver can't change them
24
25 # This has the following advantages:
26 #       -You save disk space. If you have 100 vservers, each using 500 megs
27 #        (common linux server installation), you can unify 90% of that
28 #       -Memory usage. Since the exact same binary are loaded, including
29 #        the same shared object, you save some memory and this can increase
30 #        performance, especially the memory cache usage.
31 #
32 # On the down side, you are loosing some flexibility. The vserver
33 # administrators can't upgrade package as they see fit, since the
34 # files are immutable. On the other end, just unifying glibc is probably
35 # a win.
36 : ${UTIL_VSERVER_VARS:=$(dirname $0)/util-vserver-vars}
37 test -e "$UTIL_VSERVER_VARS" || {
38     echo "Can not find util-vserver installation; aborting..."
39     exit 1
40 }
41 . "$UTIL_VSERVER_VARS"
42
43 if [ $# = 0 ] ; then
44         echo vunify [ --undo ] ref-vserver vservers -- packages
45 else
46         undo=0
47         if [ "$1" == "--undo" ] ; then
48                 undo=1
49                 shift
50         fi
51         ref=$1
52         shift
53         servers=
54         while [ "$1" != "" -a "$1" != "--" ]
55         do
56                 servers="$servers $1"
57                 shift
58         done
59         if [ "$servers" = "" ] ; then
60                 echo No vserver specified >&2
61                 exit 1
62         elif [ "$1" != "--" ] ; then
63                 echo Missing -- marker >&2
64                 exit 1
65         else
66                 shift
67                 if [ $# = 0 ] ; then
68                         echo No package specified >&2
69                         exit 1
70                 else
71                         if [ ! -d $VROOTDIR/$ref/. ] ; then
72                                 echo No vserver $ref >&2
73                                 exit 1
74                         else
75                                 #echo ref=$ref
76                                 #echo servers=$servers
77                                 #echo packages=$*
78                                 tmpfile=/var/run/vunifi.$$
79                                 rm -f $tmpfile
80                                 echo Extracting list of file to unify in $tmpfile
81                                 for pkg in $*
82                                 do
83                                         $VROOTDIR/$ref/bin/rpm --root $VROOTDIR/$ref -ql --dump $pkg | \
84                                         while read path size mtime md5 \
85                                                 mode owner group isconfig isdoc rdev symlink
86                                         do
87                                                 if [ "$isconfig" = 0 ] ; then
88                                                         echo $path >>$tmpfile
89                                                 fi
90                                         done
91                                 done
92                                 for serv in $servers
93                                 do
94                                         if [ "$undo" = 0 ] ; then
95                                                 echo Unifying server $serv
96                                                 cat $tmpfile | while read file
97                                                 do
98                                                         if [ ! -d $VROOTDIR/$ref/$file -a ! -L $VROOTDIR/$ref/$file ] ; then
99                                                                 ln -f $VROOTDIR/$ref/$file $VROOTDIR/$serv/$file
100                                                         fi
101                                                 done
102                                                 cat $tmpfile | while read file
103                                                 do
104                                                         chattr +i $VROOTDIR/$ref/$file
105                                                 done
106                                         else
107                                                 echo Differencing server $serv
108                                                 cat $tmpfile | while read file
109                                                 do
110                                                         chattr -i $VROOTDIR/$ref/$file
111                                                         if [ ! -d $VROOTDIR/$ref/$file ] ; then
112                                                                 rm -f $VROOTDIR/$serv/$file
113                                                                 cp -a $VROOTDIR/$ref/$file $VROOTDIR/$serv/$file
114                                                         fi
115                                                 done
116                                         fi
117                                 done
118                                 rm -f $tmpfile 
119                         fi
120                 fi
121         fi
122 fi
123