3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 // based on vbuild.cc by Jacques Gelinas
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)
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.
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.
21 This utility is used to build a new vserver using a reference vserver.
22 It uses hard link whenever possible instead of duplicating files.
23 Once done, it sets the immutable bits.
27 #include <sys/ioctl.h>
44 EXCLDIR(const char *s)
51 static vector<EXCLDIR> excldirs;
54 static int ext2flags = EXT2_IMMUTABLE_FILE_FL | EXT2_IMMUTABLE_LINK_FL;
68 "vbuild version " << VERSION <<
70 "vbuild [ options ] reference-server new-vservers\n"
72 "--test: Show what will be done, do not do it.\n"
73 "--debug: Prints some debugging messages.\n"
75 "--excldir: None of the files under a given directory will be copied\n"
76 "\tThe directory is expressed in absolute/logical form (relative\n"
77 "\tto the vserver root (ex: /var/log)\n"
80 "--noflags: Do not put any immutable flags on the file\n"
81 "--immutable: Set the immutable_file bit on the files.\n"
82 "--immutable-mayunlink: Sets the immutable_link flag on files.\n"
83 "--stats: Produce statistics on the number of file linked\n"
84 " copied and so on.\n"
86 "By default, the immutable_file and immutable_link flags are\n"
87 "set on the files. So if you want no immutable flags, you must\n"
88 "use --noflags. If you want a single flag, you must use\n"
89 "--noflags first, then the --immutable or --immutable-mayunlink\n"
95 Return true if a directory lies inside a directory set
97 static bool vbuild_inside (vector<EXCLDIR> &dirs, const char *path)
100 for (unsigned i=0; i<dirs.size(); i++){
101 if (strncmp(dirs[i].prefix.c_str(),path,dirs[i].len)==0){
111 static int vbuild_copy (
114 dev_t dev, // We stay on the same volume
119 if (debug > 0) printf ("Copying directory %s\n",logical_dir.c_str());
120 DIR *dir = opendir (refserv.c_str());
122 fprintf (stderr,"Can't open directory %s (%s)\n",refserv.c_str()
126 bool copy_files = !vbuild_inside(excldirs,logical_dir.c_str());
129 while (ret == 0 && (ent=readdir(dir))!=NULL){
130 if (strcmp(ent->d_name,".")==0 || strcmp(ent->d_name,"..")==0){
133 string file = refserv + "/" + ent->d_name;
135 if (vutil_lstat(file.c_str(),st) == -1){
137 }else if (st.st_dev != dev){
138 if (debug > 0) printf ("Ignore sub-directory %s\n",file.c_str());
140 string newfile = newserv + "/" + ent->d_name;
141 if (S_ISDIR(st.st_mode)){
142 if (vbuild_mkdir (newfile.c_str(),st.st_mode)==-1){
143 fprintf (stderr,"Can't mkdir %s (%s)\n"
144 ,newfile.c_str(),strerror(errno));
148 if (vbuild_chown(newfile.c_str(),st.st_uid,st.st_gid)==-1){
149 fprintf (stderr,"Can't chown %s (%s)\n"
150 ,newfile.c_str(),strerror(errno));
153 ret |= vbuild_copy (file,newfile,dev
154 ,logical_dir + ent->d_name,files);
156 }else if (S_ISLNK(st.st_mode)){
158 int len = readlink(file.c_str(),path,sizeof(path)-1);
160 fprintf (stderr,"Can't readlink %s (%s)\n"
161 ,file.c_str(),strerror(errno));
166 if (vbuild_symlink (path,newfile.c_str())==-1){
167 fprintf (stderr,"Can't symlink %s to %s (%s)\n",
168 newfile.c_str(),path,strerror(errno));
171 }else if (S_ISBLK(st.st_mode)
172 || S_ISCHR(st.st_mode)
173 || S_ISFIFO(st.st_mode)){
175 if (vbuild_mknod (newfile.c_str(),st.st_mode,st.st_rdev)==-1){
176 fprintf (stderr,"Can't mknod %s (%s)\n"
177 ,newfile.c_str(),strerror(errno));
180 }else if (S_ISSOCK(st.st_mode)){
182 }else if (copy_files){
183 // Ok, this is a file. We either copy it or do a link
184 string logical_file = logical_dir + ent->d_name;
185 if (files.find (logical_file)==files.end()){
186 if (debug > 1) printf ("Copying file %s\n",file.c_str());
187 if (vbuild_file_copy (file.c_str(),newfile.c_str(),st)==-1){
188 fprintf (stderr,"Can't copy %s to %s (%s)\n",
189 file.c_str(),newfile.c_str(),strerror(errno));
192 stats.size_copy += st.st_size;
196 if (debug > 2) printf ("Linking file %s\n",file.c_str());
197 setext2flag (file.c_str(),false,ext2flags);
199 if (vbuild_link (file.c_str(),newfile.c_str())==-1){
200 fprintf (stderr,"Can't link %s to %s (%s)\n",
201 file.c_str(),newfile.c_str(),strerror(errno));
204 setext2flag (file.c_str(),true,ext2flags);
214 int main (int argc, char *argv[])
217 bool statistics = false;
219 for (i=1; i<argc; i++){
220 const char *arg = argv[i];
221 //const char *opt = argv[i+1];
222 if (strcmp(arg,"--test")==0){
224 }else if (strcmp(arg,"--debug")==0){
226 }else if (strcmp(arg,"--stats")==0){
228 }else if (strcmp(arg,"--noflags")==0){
230 }else if (strcmp(arg,"--immutable")==0){
231 ext2flags |= EXT2_IMMUTABLE_FILE_FL;
232 }else if (strcmp(arg,"--immutable-mayunlink")==0){
233 ext2flags |= EXT2_IMMUTABLE_LINK_FL;
234 }else if (strcmp(arg,"--excldir")==0){
236 excldirs.push_back (EXCLDIR(argv[i]));
244 string refserv = argv[i++];
245 string newserv = argv[i];
246 list<PACKAGE> packages;
247 // Load the files which are not configuration files from
249 vutil_loadallpkg (refserv,packages);
251 for (list<PACKAGE>::iterator it=packages.begin(); it!=packages.end(); it++){
252 (*it).loadfiles(refserv,files);
254 // Now, we do a recursive copy of refserv into newserv
256 mkdir (newserv.c_str(),0755);
257 // Check if it is on the same volume
258 struct stat refst,newst;
259 if (vutil_lstat(refserv,refst)!=-1
260 && vutil_lstat(newserv,newst)!=1){
261 if (refst.st_dev != newst.st_dev){
262 fprintf (stderr,"Can't vbuild %s because it is not on the same volume as %s\n"
263 ,newserv.c_str(),refserv.c_str());
265 stats.nbdir = stats.nblink = stats.nbcopy = stats.nbsymlink = 0;
268 ret = vbuild_copy (refserv,newserv,refst.st_dev,"",files);
270 printf ("Directory created: %d\n",stats.nbdir);
271 printf ("Files copied : %d\n",stats.nbcopy);
272 printf ("Bytes copied : %ld\n",stats.size_copy);
273 printf ("Files linked : %d\n",stats.nblink);
274 printf ("Files symlinked : %d\n",stats.nbsymlink);
275 printf ("Special files : %d\n",stats.nbspc);