version 0.23.91
[util-vserver.git] / util-vserver / scripts / vunify.old.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 if [ $# = 0 ] ; then
37         echo vunify [ --undo ] ref-vserver vservers -- packages
38 else
39         undo=0
40         if [ "$1" == "--undo" ] ; then
41                 undo=1
42                 shift
43         fi
44         ref=$1
45         shift
46         servers=
47         while [ "$1" != "" -a "$1" != "--" ]
48         do
49                 servers="$servers $1"
50                 shift
51         done
52         if [ "$servers" = "" ] ; then
53                 echo No vserver specified >&2
54                 exit 1
55         elif [ "$1" != "--" ] ; then
56                 echo Missing -- marker >&2
57                 exit 1
58         else
59                 shift
60                 if [ $# = 0 ] ; then
61                         echo No package specified >&2
62                         exit 1
63                 else
64                         if [ ! -d /vservers/$ref/. ] ; then
65                                 echo No vserver $ref >&2
66                                 exit 1
67                         else
68                                 #echo ref=$ref
69                                 #echo servers=$servers
70                                 #echo packages=$*
71                                 tmpfile=/var/run/vunifi.$$
72                                 rm -f $tmpfile
73                                 echo Extracting list of file to unify in $tmpfile
74                                 for pkg in $*
75                                 do
76                                         /vservers/$ref/bin/rpm --root /vservers/$ref -ql --dump $pkg | \
77                                         while read path size mtime md5 \
78                                                 mode owner group isconfig isdoc rdev symlink
79                                         do
80                                                 if [ "$isconfig" = 0 ] ; then
81                                                         echo $path >>$tmpfile
82                                                 fi
83                                         done
84                                 done
85                                 for serv in $servers
86                                 do
87                                         if [ "$undo" = 0 ] ; then
88                                                 echo Unifying server $serv
89                                                 cat $tmpfile | while read file
90                                                 do
91                                                         if [ ! -d /vservers/$ref/$file -a ! -L /vservers/$ref/$file ] ; then
92                                                                 ln -f /vservers/$ref/$file /vservers/$serv/$file
93                                                         fi
94                                                 done
95                                                 cat $tmpfile | while read file
96                                                 do
97                                                         chattr +i /vservers/$ref/$file
98                                                 done
99                                         else
100                                                 echo Differencing server $serv
101                                                 cat $tmpfile | while read file
102                                                 do
103                                                         chattr -i /vservers/$ref/$file
104                                                         if [ ! -d /vservers/$ref/$file ] ; then
105                                                                 rm -f /vservers/$serv/$file
106                                                                 cp -a /vservers/$ref/$file /vservers/$serv/$file
107                                                         fi
108                                                 done
109                                         fi
110                                 done
111                                 rm -f $tmpfile 
112                         fi
113                 fi
114         fi
115 fi
116