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.
31 #include <sys/ioctl.h>
48 EXCLDIR(const char *s)
55 static vector<EXCLDIR> excldirs;
58 static int ext2flags = EXT2_IMMUTABLE_FILE_FL | EXT2_IMMUTABLE_LINK_FL;
72 "vbuild version " << VERSION <<
74 "vbuild [ options ] reference-server new-vservers\n"
76 "--test: Show what will be done, do not do it.\n"
77 "--debug: Prints some debugging messages.\n"
79 "--excldir: None of the files under a given directory will be copied\n"
80 "\tThe directory is expressed in absolute/logical form (relative\n"
81 "\tto the vserver root (ex: /var/log)\n"
84 "--noflags: Do not put any immutable flags on the file\n"
85 "--immutable: Set the immutable_file bit on the files.\n"
86 "--immutable-mayunlink: Sets the immutable_link flag on files.\n"
87 "--stats: Produce statistics on the number of file linked\n"
88 " copied and so on.\n"
90 "By default, the immutable_file and immutable_link flags are\n"
91 "set on the files. So if you want no immutable flags, you must\n"
92 "use --noflags. If you want a single flag, you must use\n"
93 "--noflags first, then the --immutable or --immutable-mayunlink\n"
99 Return true if a directory lies inside a directory set
101 static bool vbuild_inside (vector<EXCLDIR> &dirs, const char *path)
104 for (unsigned i=0; i<dirs.size(); i++){
105 if (strncmp(dirs[i].prefix.c_str(),path,dirs[i].len)==0){
115 static int vbuild_copy (
118 dev_t dev, // We stay on the same volume
123 if (debug > 0) printf ("Copying directory %s\n",logical_dir.c_str());
124 DIR *dir = opendir (refserv.c_str());
126 fprintf (stderr,"Can't open directory %s (%s)\n",refserv.c_str()
130 bool copy_files = !vbuild_inside(excldirs,logical_dir.c_str());
133 while (ret == 0 && (ent=readdir(dir))!=NULL){
134 if (strcmp(ent->d_name,".")==0 || strcmp(ent->d_name,"..")==0){
137 string file = refserv + "/" + ent->d_name;
139 if (vutil_lstat(file.c_str(),st) == -1){
141 }else if (st.st_dev != dev){
142 if (debug > 0) printf ("Ignore sub-directory %s\n",file.c_str());
144 string newfile = newserv + "/" + ent->d_name;
145 if (S_ISDIR(st.st_mode)){
146 if (vbuild_mkdir (newfile.c_str(),st.st_mode)==-1){
147 fprintf (stderr,"Can't mkdir %s (%s)\n"
148 ,newfile.c_str(),strerror(errno));
152 if (vbuild_chown(newfile.c_str(),st.st_uid,st.st_gid)==-1){
153 fprintf (stderr,"Can't chown %s (%s)\n"
154 ,newfile.c_str(),strerror(errno));
157 ret |= vbuild_copy (file,newfile,dev
158 ,logical_dir + ent->d_name,files);
160 }else if (S_ISLNK(st.st_mode)){
162 int len = readlink(file.c_str(),path,sizeof(path)-1);
164 fprintf (stderr,"Can't readlink %s (%s)\n"
165 ,file.c_str(),strerror(errno));
170 if (vbuild_symlink (path,newfile.c_str())==-1){
171 fprintf (stderr,"Can't symlink %s to %s (%s)\n",
172 newfile.c_str(),path,strerror(errno));
175 }else if (S_ISBLK(st.st_mode)
176 || S_ISCHR(st.st_mode)
177 || S_ISFIFO(st.st_mode)){
179 if (vbuild_mknod (newfile.c_str(),st.st_mode,st.st_rdev)==-1){
180 fprintf (stderr,"Can't mknod %s (%s)\n"
181 ,newfile.c_str(),strerror(errno));
184 }else if (S_ISSOCK(st.st_mode)){
186 }else if (copy_files){
187 // Ok, this is a file. We either copy it or do a link
188 string logical_file = logical_dir + ent->d_name;
189 if (files.find (logical_file)==files.end()){
190 if (debug > 1) printf ("Copying file %s\n",file.c_str());
191 if (vbuild_file_copy (file.c_str(),newfile.c_str(),st)==-1){
192 fprintf (stderr,"Can't copy %s to %s (%s)\n",
193 file.c_str(),newfile.c_str(),strerror(errno));
196 stats.size_copy += st.st_size;
200 if (debug > 2) printf ("Linking file %s\n",file.c_str());
201 setext2flag (file.c_str(),false,ext2flags);
203 if (vbuild_link (file.c_str(),newfile.c_str())==-1){
204 fprintf (stderr,"Can't link %s to %s (%s)\n",
205 file.c_str(),newfile.c_str(),strerror(errno));
208 setext2flag (file.c_str(),true,ext2flags);
218 int main (int argc, char *argv[])
221 bool statistics = false;
223 for (i=1; i<argc; i++){
224 const char *arg = argv[i];
225 //const char *opt = argv[i+1];
226 if (strcmp(arg,"--test")==0){
228 }else if (strcmp(arg,"--debug")==0){
230 }else if (strcmp(arg,"--stats")==0){
232 }else if (strcmp(arg,"--noflags")==0){
234 }else if (strcmp(arg,"--immutable")==0){
235 ext2flags |= EXT2_IMMUTABLE_FILE_FL;
236 }else if (strcmp(arg,"--immutable-mayunlink")==0){
237 ext2flags |= EXT2_IMMUTABLE_LINK_FL;
238 }else if (strcmp(arg,"--excldir")==0){
240 excldirs.push_back (EXCLDIR(argv[i]));
248 string refserv = argv[i++];
249 string newserv = argv[i];
250 list<Package> packages;
251 // Load the files which are not configuration files from
253 vutil_loadallpkg (refserv,packages);
255 for (list<Package>::iterator it=packages.begin(); it!=packages.end(); it++){
256 (*it).loadfiles(refserv,files);
258 // Now, we do a recursive copy of refserv into newserv
260 mkdir (newserv.c_str(),0755);
261 // Check if it is on the same volume
262 struct stat refst,newst;
263 if (vutil_lstat(refserv,refst)!=-1
264 && vutil_lstat(newserv,newst)!=1){
265 if (refst.st_dev != newst.st_dev){
266 fprintf (stderr,"Can't vbuild %s because it is not on the same volume as %s\n"
267 ,newserv.c_str(),refserv.c_str());
269 stats.nbdir = stats.nblink = stats.nbcopy = stats.nbsymlink = 0;
272 ret = vbuild_copy (refserv,newserv,refst.st_dev,"",files);
274 printf ("Directory created: %d\n",stats.nbdir);
275 printf ("Files copied : %d\n",stats.nbcopy);
276 printf ("Bytes copied : %ld\n",stats.size_copy);
277 printf ("Files linked : %d\n",stats.nblink);
278 printf ("Files symlinked : %d\n",stats.nbsymlink);
279 printf ("Special files : %d\n",stats.nbspc);