| 1 | #!/usr/bin/perl | ||
| 2 | # | ||
| 3 | # FUSE driver: read-only union of read-only branches | ||
| 4 | # | ||
| 5 | # Driver is calling from mount.fuse wrapper | ||
| 6 | # See --help and "man Fuse" for details. | ||
| 7 | # | ||
| 8 | # Copyright (C) Vladimir V. Kolpakov 2006-2011 | ||
| 9 | # All rights reserved. | ||
| 10 | # | ||
| 11 | #--w 02/2006,03/2010,08/2011############################################ | ||
| 12 | package Fuse::Funifs; | ||
| 13 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | |||
| 15 | 0 | ▶ | use threads; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 16 | use threads::shared; | ||
| 17 | use Fuse; | ||
| 18 | use POSIX qw(setsid O_RDONLY ENOENT EROFS EACCES EINVAL ENOSYS); | ||
| 19 | use Filesys::Statvfs qw(statvfs); | ||
| 20 | use Unix::Syslog qw(:subs LOG_INFO LOG_NDELAY LOG_PID LOG_DAEMON); | ||
| 21 | use Getopt::Long; | ||
| 22 | |||
| 23 | #----------------------------------------------------------------------- | ||
| 24 | #-- FUSE filesystem name: | ||
| 25 | #- package basename is default for $FSname | ||
| 26 | #- script basename (symlink, for instance) alters $FSname | ||
| 27 | my $FSname = ($0 =~ m!([^/]+)$!)[0]; | ||
| 28 | $FSname = (__PACKAGE__ =~ m/::(.*?)$/)[0] unless $FSname; | ||
| 29 | ############ printLog($msg) ############################################ | ||
| 30 | sub printLog { | ||
| 31 | openlog($FSname, LOG_NDELAY | LOG_PID, LOG_DAEMON); | ||
| 32 | syslog(LOG_INFO,$_[0],''); | ||
| 33 | closelog(); | ||
| 34 | } | ||
| 35 | ############ Main ###################################################### | ||
| 36 | my $OpenMode = O_RDONLY | (defined(&O_LARGEFILE) ? &O_LARGEFILE() : 0); | ||
| 37 | my $Fh = {}; #-- open files handles cache | ||
| 38 | my @Fh = (); #-- open files paths list | ||
| 39 | my $MaxFiles = 100; #-- max length of @Fh | ||
| 40 | my @Branch = (); #-- ordered branches list | ||
| 41 | #----------------------------------------------------------------------- | ||
| 42 | my $LogMsg = join(' ',$0,@ARGV); | ||
| 43 | my $opt = { | ||
| 44 | 'help' => undef, | ||
| 45 | 'verbose' => 0, | ||
| 46 | 'opts' => '', | ||
| 47 | 'debug' => undef, | ||
| 48 | }; | ||
| 49 | #--- Consume --option words before any mount.fuse arguments | ||
| 50 | #- Ignore any /bin/mount arguments | ||
| 51 | GetOptions( | ||
| 52 | 'help' => \$opt->{'help'}, | ||
| 53 | 'o=s' => \$opt->{'opts'}, | ||
| 54 | 'd' => \$opt->{'debug'}, #-- to enable FUSE debug, enable debug in Fuse::main call below | ||
| 55 | 'v|verbose+' => \$opt->{'verbose'}, | ||
| 56 | ); | ||
| 57 | sub Say { $opt->{'verbose'} and print STDERR @_; } | ||
| 58 | &Say("---2---$FSname: $LogMsg\n"); | ||
| 59 | my ($fspath #-- fstab FS's "specification" with "$FSname#" stripped out in mount.fuse | ||
| 60 | ,$Union #-- mount point | ||
| 61 | ,$Opts #-- mount options as of fstab(8) | ||
| 62 | ) = @ARGV; #---mount.fuse: ${FSTYPE} ${MOUNTPATH} ${MOUNTPOINT} ${OPTIONS} | ||
| 63 | $Opts ||= $opt->{'opts'} if $opt->{'opts'}; | ||
| 64 | $fspath ||= '/union'; #-- fake default, for --help only | ||
| 65 | $Union ||= '/mnt/union'; #-- fake default, for --help only | ||
| 66 | my $fsSpec = ".$fspath"; #-- re-assemble fstab's spec value back | ||
| 67 | #--- | ||
| 68 | @Branch = split(/:/, $1) if ($Opts =~ s/\bdirs=([^,]+)//); | ||
| 69 | my $dirs = join(':',@Branch); | ||
| 70 | my $opts = {}; | ||
| 71 | $Opts = join(',', | ||
| 72 | map {exists($opts->{$_}) ? () : ($opts->{$_} = $_)} | ||
| 73 | map {$_ ? $_ : ()} | ||
| 74 | split(',',$Opts.',ro,noexec,nosuid,nodev') | ||
| 75 | ).',dirs='.$dirs; | ||
| 76 | ############ Usage() ################################################### | ||
| 77 | sub Usage { | ||
| 78 | $dirs ||= '/delta/www:/srv/www'; | ||
| 79 | print <<"EoD" ; | ||
| 80 | ------------------------------------------------------ | ||
| 81 | FUSE funifs (read-only union) filesystem driver | ||
| 82 | Normally invoking by mount.fuse helper at "mount -a -t fuse" boot call | ||
| 83 | |||
| 84 | Usage: (mount.fuse order): | ||
| 85 | $FSname {FS_spec} {Union_mount_point} {[-o] Fstab_options]} [OPTIONS] | ||
| 86 | |||
| 87 | Options: | ||
| 88 | --help #-- print this info and exit | ||
| 89 | -v, --verbose #-- verbose (incremental option) | ||
| 90 | -d #-- don't fork to daemon mode, stay in foreground | ||
| 91 | |||
| 92 | Example: | ||
| 93 | $FSname $fsSpec $Union -o ro,noexec,nosuid,nodev,dirs=$dirs -v | ||
| 94 | |||
| 95 | /etc/fstab line example: | ||
| 96 | $fsSpec $Union fuse.funifs user,ro,_netdev,dirs=$dirs 0 91 | ||
| 97 | |||
| 98 | /etc/fstab options: | ||
| 99 | user,ro,_netdev #-- [recommended] mount options | ||
| 100 | dirs=$dirs #-- colon-separated list of branches, top layer first | ||
| 101 | ro,noexec,nosuid,nodev #-- [hardcoded] mount options. "ro" again, despite fstab has it | ||
| 102 | allow_other,default_permissions #-- [hardcoded] fuse options; allow_other need flag in /etc/fuse.conf | ||
| 103 | |||
| 104 | /etc/fuse.conf line: | ||
| 105 | user_allow_other #-- flag: regular user is enabled to do mount / fusermount -u | ||
| 106 | |||
| 107 | ------------------------------------------------------ | ||
| 108 | EoD | ||
| 109 | } | ||
| 110 | ############ Main ###################################################### | ||
| 111 | if ($opt->{'help'}) { &Usage(); exit 0; } | ||
| 112 | |||
| 113 | #--- Sanity checks | ||
| 114 | unless ($dirs) { | ||
| 115 | print STDERR "$FSname: try: $0 --help\n"; | ||
| 116 | exit 1; | ||
| 117 | } | ||
| 118 | for ($Union, @Branch) { | ||
| 119 | next if (-d $_); | ||
| 120 | print STDERR "$0: *** Error: fake directory: '$_', bye.\n"; | ||
| 121 | exit 1; | ||
| 122 | } | ||
| 123 | unless ($FSname =~ m![^.]!) { | ||
| 124 | &Usage(); | ||
| 125 | print STDERR "$0: *** Error: invalid FS type name, bye.\n"; | ||
| 126 | exit 1; | ||
| 127 | } | ||
| 128 | unless ($Union =~ m![^.]!) { | ||
| 129 | &Usage(); | ||
| 130 | print STDERR "$0: *** Error: invalid mount point, bye.\n"; | ||
| 131 | exit 1; | ||
| 132 | } | ||
| 133 | unless (scalar(@Branch)) { | ||
| 134 | &Usage(); | ||
| 135 | print STDERR "$0: *** Error: source branch is missing, -- nothing to mount, bye.\n"; | ||
| 136 | exit 1; | ||
| 137 | } | ||
| 138 | #----------------------------------------------------------------------- | ||
| 139 | $LogMsg = "$fsSpec $Union $Opts"; | ||
| 140 | &Say("---3---$FSname: $LogMsg\n"); | ||
| 141 | &printLog('mount.fuse: '.$LogMsg); | ||
| 142 | #Aug 29 01:02:32 kent funifs[23161]: mount.fuse: ./example.com/u/w /web/example.com/u/w ro,dev,suid,noexec,nosuid,nodev,dirs=/web/sites/example.com/u/w:/web/sites/example.com/www | ||
| 143 | ############ Internal functions ######################################## | ||
| 144 | #----------- upath() --------------------------------------- | ||
| 145 | #--- Find top exiting file in union, return real path | ||
| 146 | sub upath { | ||
| 147 | my $file = shift; | ||
| 148 | $file =~ s!^/!!; | ||
| 149 | for (@Branch) { | ||
| 150 | next unless lstat("$_/$file"); | ||
| 151 | return "$_/$file"; | ||
| 152 | } | ||
| 153 | return undef; | ||
| 154 | } | ||
| 155 | ############ Fuse.* callbacks ######################################### | ||
| 156 | #----------- getattr() ------------------------------------- | ||
| 157 | #--- Return stat of top existing file in the union | ||
| 158 | sub u_getattr { | ||
| 159 | my $file = shift(); | ||
| 160 | &Say("---35---u_getattr($file)\n"); | ||
| 161 | my $path = upath($file) or return (); | ||
| 162 | #return lstat($path); | ||
| 163 | #&Say("---35.1---u_getattr($file)\n"); | ||
| 164 | return lstat(_); | ||
| 165 | } | ||
| 166 | #----------- getdir() -------------------------------------- | ||
| 167 | #--- Merge content of all branches directories | ||
| 168 | sub u_getdir { | ||
| 169 | my $dir = shift(); | ||
| 170 | &Say("---36---u_getdir($dir)\n"); | ||
| 171 | $dir =~ s!^/!!; | ||
| 172 | my $files = {}; | ||
| 173 | for my $b(@Branch) { | ||
| 174 | next unless opendir(DIR,"$b/$dir"); | ||
| 175 | for (readdir(DIR)) { $files->{$_} = 1; } | ||
| 176 | closedir(DIR); | ||
| 177 | } | ||
| 178 | return -ENOENT() unless exists($files->{'.'}); | ||
| 179 | return (keys %$files, 0); | ||
| 180 | } | ||
| 181 | #----------- open() ---------------------------------------- | ||
| 182 | #-- modes are from /usr/include/linux/fs.h | ||
| 183 | #-- ~(1+4+8+32) = ~(FMODE_READ | FMODE_LSEEK | FMODE_PREAD | FMODE_EXEC) | ||
| 184 | sub u_open { | ||
| 185 | &Say("---37---u_open(@_)\n"); | ||
| 186 | my ($file,$mode) = @_; | ||
| 187 | #my $write = ($mode & 0x7fff); # ~FMODE_READ | ||
| 188 | my $write = ($mode & 0x7fdf); # ~(FMODE_READ | FMODE_EXEC) | ||
| 189 | #&Say("---37.1---u_open: \$write=<$write>\n"); | ||
| 190 | return -EACCES() if $write; #-- reject anything, except read-only access | ||
| 191 | &upath($file) or return -ENOENT(); | ||
| 192 | return 0; | ||
| 193 | } | ||
| 194 | #----------- release() ------------------------------------- | ||
| 195 | #--- Release open file handle. | ||
| 196 | #- Arguments: Pathname, numeric flags passed to open | ||
| 197 | #- Returns: an errno or 0 on success. | ||
| 198 | # | ||
| 199 | #- Called to indicate that there are no more references to the file. | ||
| 200 | #- Called once for every file with the same pathname and flags as were passed to open. | ||
| 201 | sub u_release { | ||
| 202 | &Say("---39---u_release(@_)\n"); | ||
| 203 | #my ($file,$mode) = @_; | ||
| 204 | return 0; | ||
| 205 | } | ||
| 206 | #----------- read() ---------------------------------------- | ||
| 207 | #--- Return an error numeric, or binary/text string. | ||
| 208 | #--NOTE: 0 means EOF, "0" will | ||
| 209 | #- give a byte (ascii "0") to the reading program. | ||
| 210 | sub u_read { | ||
| 211 | &Say("---40.1---u_read(@_)\n"); | ||
| 212 | my ($file,$size,$off) = @_; | ||
| 213 | my ($fh,$rc,$buf,$path); | ||
| 214 | $path = upath($file) or return -ENOENT(); | ||
| 215 | sysopen($fh,$path,$OpenMode); | ||
| 216 | $fh or return -ENOENT(); | ||
| 217 | if ($off) { | ||
| 218 | $rc = sysseek($fh,$off,0) or return -EINVAL(); | ||
| 219 | } | ||
| 220 | $rc = sysread($fh,$buf,$size); #-- sysread(FILEHANDLE,SCALAR,LENGTH,OFFSET) | ||
| 221 | &Say("---40.2---u_read: length=<".length($buf).">\n"); | ||
| 222 | return defined($rc) ? $buf : -ENOSYS(); | ||
| 223 | } | ||
| 224 | #----------- flush() --------------------------------------- | ||
| 225 | #--- Useless on read-only branches, however called from fuse | ||
| 226 | #- before every close file, opened even for read(). | ||
| 227 | sub u_flush { | ||
| 228 | #&Say("---41---u_flush(@_)\n"); | ||
| 229 | return 0; | ||
| 230 | } | ||
| 231 | #----------- readlink() ------------------------------------ | ||
| 232 | #--- Return the symlink string value | ||
| 233 | sub u_readlink { | ||
| 234 | #&Say("---42---u_readlink(@_)\n"); | ||
| 235 | my $file = shift(); | ||
| 236 | my $path = upath($file) or return -ENOENT(); | ||
| 237 | return readlink($path); | ||
| 238 | #EINVAL | ||
| 239 | } | ||
| 240 | #----------- listxattr() ----------------------------------- | ||
| 241 | #--- Return the list of extended attribute names | ||
| 242 | #--NOTE: do not include this in Fuse::main() args, -- | ||
| 243 | # to supress 2+ extra calls on each file. | ||
| 244 | sub u_listxattr { | ||
| 245 | #&Say("---43---u_listxattr(@_)\n"); | ||
| 246 | my $file = shift(); | ||
| 247 | my $path = upath($file) or return (); | ||
| 248 | #return listxattr($path); #-- no native support in perl | ||
| 249 | return (0); | ||
| 250 | } | ||
| 251 | |||
| 252 | #----------- getxattr() ------------------------------------ | ||
| 253 | #--- Return an extended attribute value | ||
| 254 | #--NOTE: avoid to include this in Fuse::main() args, -- | ||
| 255 | # to supress 2+ extra calls on each file. | ||
| 256 | sub u_getxattr { | ||
| 257 | &Say("---44---u_getxattr(@_)\n"); | ||
| 258 | # my ($file,$name) = @_; | ||
| 259 | # my $path = upath($file) or return (); | ||
| 260 | # #return lgetxattr($path,$name,$value,$size); | ||
| 261 | return (0); | ||
| 262 | } | ||
| 263 | |||
| 264 | #----------- statfs() -------------------------------------- | ||
| 265 | #--- Return an error numeric, or binary/text string. | ||
| 266 | sub u_statfs { | ||
| 267 | #-- No args. | ||
| 268 | my @st = statvfs($Branch[0]) or return -$!; | ||
| 269 | my $statvfs = { map {$_ => shift(@st)} qw( | ||
| 270 | bsize frsize blocks bfree bavail files | ||
| 271 | ffree favail fsid basetype flag namemax fstr | ||
| 272 | ) }; | ||
| 273 | return ( 0, map {$statvfs->{$_}} qw( | ||
| 274 | namemax files ffree blocks bfree bsize | ||
| 275 | ) ); | ||
| 276 | } | ||
| 277 | #----------- rofs() ---------------------------------------- | ||
| 278 | #--- Return "read-only filesystem" error | ||
| 279 | sub rofs { | ||
| 280 | &Say("---45---rofs(@_)\n"); | ||
| 281 | return -EROFS(); | ||
| 282 | } | ||
| 283 | sub u_chmod { &rofs('chmod' ,@_); } | ||
| 284 | sub u_chown { &rofs('chown' ,@_); } | ||
| 285 | sub u_fsync { &rofs('fsync' ,@_); } | ||
| 286 | sub u_link { &rofs('link' ,@_); } | ||
| 287 | sub u_mkdir { &rofs('mkdir' ,@_); } | ||
| 288 | sub u_mknod { &rofs('mknod' ,@_); } | ||
| 289 | sub u_removexattr { &rofs('removexattr' ,@_); } | ||
| 290 | sub u_rename { &rofs('rename' ,@_); } | ||
| 291 | sub u_rmdir { &rofs('rmdir' ,@_); } | ||
| 292 | sub u_setxattr { &rofs('setxattr' ,@_); } | ||
| 293 | sub u_symlink { &rofs('symlink' ,@_); } | ||
| 294 | sub u_truncate { &rofs('truncate' ,@_); } | ||
| 295 | sub u_unlink { &rofs('unlink' ,@_); } | ||
| 296 | sub u_utime { &rofs('utime' ,@_); } | ||
| 297 | sub u_write { &rofs('write' ,@_); } | ||
| 298 | |||
| 299 | #------------------------------------------------------------------# | ||
| 300 | #--- daemon(3) emulation | ||
| 301 | #--- Shrinked down variant of Net::Server::Daemonize | ||
| 302 | sub daemon { #-- dissociate process from terminal | ||
| 303 | my $pid = fork(); | ||
| 304 | exit(0) if $pid; #-- parent process | ||
| 305 | croak("Couldn't fork: [$!]\n") unless defined $pid; | ||
| 306 | #-- child process will continue on | ||
| 307 | #-- close all input/output and separate | ||
| 308 | #-- from the parent process group | ||
| 309 | open STDIN, '</dev/null' or croak("Can't open STDIN from /dev/null: [$!]\n"); | ||
| 310 | open STDOUT, '>/dev/null' or croak("Can't open STDOUT to /dev/null: [$!]\n"); | ||
| 311 | open STDERR, '>&STDOUT' or croak("Can't open STDERR to STDOUT: [$!]\n"); | ||
| 312 | #-- Change to root dir to avoid locking a mounted file system | ||
| 313 | #chdir '/' or croak("Can't chdir to \"/\": [$!]"); | ||
| 314 | #-- Turn process into session leader, and ensure no controlling terminal | ||
| 315 | POSIX::setsid(); | ||
| 316 | return 1; | ||
| 317 | } | ||
| 318 | ############ Fuse::main call ########################################### | ||
| 319 | # If you run the script directly, it will run fusermount, which will in turn | ||
| 320 | # re-run this script. Hence the funky semantics. | ||
| 321 | #print STDERR "---19--- ok.\n"; | ||
| 322 | my $args = { | ||
| 323 | 'mountpoint' => $Union, | ||
| 324 | 'mountopts' => join(',' | ||
| 325 | ,'ro' | ||
| 326 | , 'allow_other' | ||
| 327 | ,'default_permissions' | ||
| 328 | #,'use_ino' | ||
| 329 | ,"fsname=$fsSpec" | ||
| 330 | ), | ||
| 331 | 'debug' => 0, | ||
| 332 | #'debug' => 1, | ||
| 333 | #'threaded' => 0, | ||
| 334 | 'threaded' => 1, | ||
| 335 | #-- Implemented callbacks | ||
| 336 | 'getattr' => 'Fuse::Funifs::u_getattr' , | ||
| 337 | 'getdir' => 'Fuse::Funifs::u_getdir' , | ||
| 338 | 'open' => 'Fuse::Funifs::u_open' , | ||
| 339 | 'read' => 'Fuse::Funifs::u_read' , | ||
| 340 | 'flush' => 'Fuse::Funifs::u_flush' , | ||
| 341 | 'statfs' => 'Fuse::Funifs::u_statfs' , | ||
| 342 | 'listxattr' => 'Fuse::Funifs::u_listxattr' , | ||
| 343 | 'readlink' => 'Fuse::Funifs::u_readlink' , | ||
| 344 | 'release' => 'Fuse::Funifs::u_release' , | ||
| 345 | #-- Not implemented callbacks | ||
| 346 | 'getxattr' => 'Fuse::Funifs::u_getxattr', #--NOTE: avoid (keep this undef) to supress 2+ extra calls on each file | ||
| 347 | #-- Disabled access | ||
| 348 | 'chmod' => 'Fuse::Funifs::u_chmod' , | ||
| 349 | 'chown' => 'Fuse::Funifs::u_chown' , | ||
| 350 | 'fsync' => 'Fuse::Funifs::u_fsync' , | ||
| 351 | 'link' => 'Fuse::Funifs::u_link' , | ||
| 352 | 'mkdir' => 'Fuse::Funifs::u_mkdir' , | ||
| 353 | 'mknod' => 'Fuse::Funifs::u_mknod' , | ||
| 354 | 'removexattr' => 'Fuse::Funifs::u_removexattr', | ||
| 355 | 'rename' => 'Fuse::Funifs::u_rename' , | ||
| 356 | 'rmdir' => 'Fuse::Funifs::u_rmdir' , | ||
| 357 | 'setxattr' => 'Fuse::Funifs::u_setxattr' , | ||
| 358 | 'symlink' => 'Fuse::Funifs::u_symlink' , | ||
| 359 | 'truncate' => 'Fuse::Funifs::u_truncate' , | ||
| 360 | 'unlink' => 'Fuse::Funifs::u_unlink' , | ||
| 361 | 'utime' => 'Fuse::Funifs::u_utime' , | ||
| 362 | 'write' => 'Fuse::Funifs::u_write' , | ||
| 363 | |||
| 364 | }; | ||
| 365 | #use Data::Dumper; | ||
| 366 | #BEGIN { $Data::Dumper::Indent = 1; $Data::Dumper::Terse = 1; } | ||
| 367 | #print STDERR "---21---\$args: ".Dumper($args); | ||
| 368 | #exit 0; | ||
| 369 | |||
| 370 | #-- Change to root dir to avoid locking a mounted file system | ||
| 371 | chdir('/') or croak("Can't chdir to \"/\": [$!]"); | ||
| 372 | |||
| 373 | #-- Ensure consistent behaviour across debug and normal modes | ||
| 374 | daemon() unless $opt->{'debug'}; | ||
| 375 | &Fuse::main(%$args); | ||
| 376 | |||
| 377 | ############ Exit after umount ######################################### | ||
| 378 | #Feb 8 20:37:39 knob funifs[10318]: unmount: funifs#/dev /web/me.nu/dev ro,noexec,nosuid,nodev,dirs=/web/sites/me.nu/Delta/dev:/web/sites/me.nu/www | ||
| 379 | &printLog('unmount: '.$LogMsg); | ||
| 380 | |||
| 381 | 0; | ||
| 382 | __END__ | ||