#! /usr/bin/perl # # 2003 (c) Gergely Szász # This software is under GNU license # sub mcfstabfs_list { # # print "-r--r--r-- 1 root root 0 $date README\n"; print "-r--r--r-- 1 root root 0 $date TODO\n"; print "-r--r--r-- 1 root root 0 $date BUGS\n"; print "-r--r--r-- 1 root root 0 $date read_fstab\n"; print "-r--r--r-- 1 root root 0 $date pipe_mount\n"; print "-r--r--r-- 1 root root 0 $date read_mtab\n"; $i = 0; foreach (@f) { @_ = @{$_}; print "-r-xr-xr-x 1 root root 0 $date $_[0] on $_[1]/info\n"; if ($_[6] eq 'Yes (not in fstab)') { printf "lr-xr-xr-x 1 root root 0 $date _mount_%03d -> $_[0] on $_[1]\n", $i; } else { printf "lr-xr-xr-x 1 root root 0 $date _fstab_%03d -> $_[0] on $_[1]\n", $i; print "---x--x--x 1 root root 0 $date $_[0] on $_[1]/mount\n"; print "---x--x--x 1 root root 0 $date $_[0] on $_[1]/umount\n"; } $i++; } } sub mcfstabfs_dumpfile # $destfile $string { if (open (FILEOUT,">$_[0]")) { print FILEOUT $_[1]; close FILEOUT; # system ("chmod a+x $_[0]"); #we need it? } } sub mcfstab_uline { @_ = split //, $_[0]; return '_' . join ('_', @_); } sub mcfstab_bold { my $r; @_ = split //, $_[0]; foreach (@_) { $r .= $_ . '' . $_; } return $r; } sub mcfstab_red { return '' . $_[0] . ''; } sub mcfstab_yellow { return '' . $_[0] . ''; } sub mcfstabfs_copyout { local($archive,$filename,$destfile)=@_; if ($filename eq 'README') { mcfstabfs_dumpfile ($destfile, $readme_str); exit 0; } if ($filename eq 'BUGS') { mcfstabfs_dumpfile ($destfile, $bugs_str); exit 0; } if ($filename eq 'TODO') { mcfstabfs_dumpfile ($destfile, $todo_str); exit 0; } if ($filename eq 'read_fstab') { open (FSTAB, "); close FSTAB; mcfstabfs_dumpfile ($destfile, $fstab); exit 0; } if ($filename eq 'pipe_mount') { open (FSTAB, "/bin/mount |"); $fstab .= $_ while (); close FSTAB; mcfstabfs_dumpfile ($destfile, $fstab); exit 0; } if ($filename eq 'read_mtab') { open (FSTAB, "); close FSTAB; mcfstabfs_dumpfile ($destfile, $fstab); exit 0; } unless ($filename =~ /_mount_([0-9]+)/) { $fs =~ /$filename ([^\n]+)/m; } @_ = @{@f[$1]}; if ($filename =~ s/\/info$//) { mcfstabfs_dumpfile ($destfile, sprintf ($info_str, mcfstab_uline ($_[0]), mcfstab_uline ($_[1]))); exit 0; } if ($filename =~ s/\/umount$//) { ($_, $filename) = split / on /, $filename; mcfstabfs_dumpfile ($destfile, sprintf ($umount_str, mcfstab_uline ($_[0]), mcfstab_uline ($_[1]))); exit 0; } if ($filename =~ s/\/mount$//) { ($_, $filename) = split / on /, $filename; mcfstabfs_dumpfile ($destfile, sprintf ($mount_str, mcfstab_uline ($_[0]), mcfstab_uline ($_[1]))); exit 0; } if ($filename eq 'mount-OPEN-ERROR') { mcfstabfs_dumpfile ($destfile, "*** ERROR***\n\nCannot execute '/bin/mount' system utility!\n"); exit 0; } if ($filename eq 'fstab-OPEN-ERROR') { mcfstabfs_dumpfile ($destfile, "*** ERROR***\n\nCannot read the '/etc/fstab' file!\n"); exit 0; } } sub mcfstabfs_run { local($archive,$filename)=@_; unless ($filename =~ /_mount_([0-9]+)/) { $fs =~ /$filename ([^\n]+)/m; } @_ = @{@f[$1]}; if($filename =~ /\/info$/) { $_ = "\n\nDevice:\t\t" . mcfstab_yellow ($_[0]) . "\nMount point:\t" . mcfstab_yellow ($_[1]) . "\nType:\t\t" . mcfstab_red ($_[2]) . "\nOptions:\t" . mcfstab_red ($_[3]) . "\nDump:\t\t" . $_[4] . "\nPass:\t\t" . $_[5] . "\nMounted:\t" . mcfstab_yellow ($_[6]); if ($_[7] ne undef) { $_ .= "\nSize:\t\t" . mcfstab_yellow ($_[7]) . "k\nUsed:\t\t" . mcfstab_yellow ($_[8]) . "k\nFree:\t\t" . mcfstab_yellow ($_[9]) . "k\nUsage:\t\t" . mcfstab_yellow ($_[10]); } print $_ . "\n\n"; } elsif($filename =~ /\/umount$/) { if ($_[6] eq 'Yes') { print "Umounting: $_[0] from $_[1]...\n"; system("umount $_[1]"); } else { print "This filesystem " . mcfstab_red ('do not') . " mounted yet.\n"; } } else { if ($_[6] eq 'No') { print "Mounting: $_[0] to $_[1]...\n"; system("mount $_[1]"); # system("mount -t $_[2] -o $_[4] $_[0] $_[1]"); } else { print "This filesystem " . mcfstab_red ('already') . " mounted.\n"; } } } $readme_str=< EOS $bugs_str=<... EOS $info_str=<... EOS umask 077; chop($date=`LC_ALL=C date "+%b %d %Y %H:%M"`); unless (-x '/bin/mount') { print "-r--r--r-- 1 root root 0 $date mount-EXEC-ERROR\n"; exit 0; } unless (-r '/etc/fstab') { print "-r--r--r-- 1 root root 0 $date fstab-OPEN-ERROR\n"; exit 0; } unless (-r '/etc/mtab') { print "-r--r--r-- 1 root root 0 $date mtab-OPEN-ERROR\n"; exit 0; } undef $/; open (IN, "; open (IN, "mount |") and $mn = ; open (IN, "df |") and $df = ; close IN; $/ = "\n"; $fs =~ s/^#.*\n|^[ \t]*\n//mg; $fs =~ s/[ \t]+/ /mg; $mn =~ s/ on | type / /mg; $mn =~ s/\((.*?)\)\n/$1\n/mg; $df =~ s/File.*?\n//g; $df =~ s/[ \t]+/ /mg; @fs = split /\n/, $fs; @mn = split /\n/, $mn; @df = split /\n/, $df; $i = 0; foreach (@fs) #process predefined ones { split; #get pieces $_[0] = filesys, 1 = mountp, 2 = type, # 3 = options, 4 = dump, 5 = pass if ($mn =~ /^$_[0] $_[1] ([^ ]+) ([^\n]+)/m) #ok! mounted # get actual options... { $_[6] = 'Yes'; #mounted $_[2] = $1; $_[3] = $2; #type and options if ($df =~ /^$_[0] ([^ ]+) ([^ ]+) ([^ ]+) ([^\n]+) $_[1]$/m) # # ok, have size info!!! { $_[7] = $1; $_[8] = $2; $_[9] = $3; $_[10] = $4; #size, used, free, use% } } else { $_[6] = 'No'; #umounted } push (@f, [@_]); #an array $_[0] =~ s/^\///; #we throw it away... :-) $fs .= join (' ', $_[0], 'on', $_[1], $i) . "\n"; #a matchable var with linenum $i++; } foreach (@mn) #process actual mounts { split; #get pieces $_[0] = filesys, 1 = mountp, 2 = type, # 3 = options, 4 = dump, 5 = pass next if ($fs =~ /^$_[0] $_[1]/m); #ok! already have # get actual options... $_[4] = '-'; $_[5] = '-'; $_[6] = 'Yes (not in fstab)'; #mounted if ($df =~ /^$_[0] ([^ ]+) ([^ ]+) ([^ ]+) ([^\n]+) $_[1]$/m) # # ok, have size info!!! { $_[7] = $1; $_[8] = $2; $_[9] = $3; $_[10] = $4; #size, used, free, use% } push (@f, [@_]); #an array $_[0] =~ s/^\///; #we throw it away... :-) $fs .= join (' ', $_[0], 'on', $_[1], $i) . "\n"; #a matchable var with linenum $i++; } if($ARGV[0] eq "list") { shift; &mcfstabfs_list(@ARGV); exit 0; } elsif($ARGV[0] eq "copyout") { shift; &mcfstabfs_copyout(@ARGV); exit 0; } elsif($ARGV[0] eq "run") { shift; &mcfstabfs_run(@ARGV); exit 0; } exit 1;