home-impermanence/impermanence.pl

250 lines
7.0 KiB
Perl
Raw Normal View History

2022-03-14 07:23:00 +00:00
#!/usr/bin/env perl
use strict;
use warnings;
use YAML;
use File::Basename qw(fileparse);
use List::MoreUtils qw(uniq);
use Getopt::Std;;
use Data::Dumper;
use File::HomeDir;
use File::Path qw(make_path);
use 5.010;
my $verbose = 0;
my ($start, $stop) = (0, 0);
# display usage and quit
sub usage {
say "usage: $0 [-v] -d directory -u user (start|stop|restart)";
say " $0 -t conf";
exit 1;
}
# display a message only if verbose flag is used
sub saydebug {
my $msg = shift;
say STDERR "DEBUG: $msg" if $verbose;
}
# display a message before exiting with status 2
sub trap_error {
my $msg = shift;
say STDERR "FATAL: $msg";
exit 2;
}
2022-03-14 07:23:00 +00:00
# return a perl structure by reader a YAML file
sub read_yml_file_to_struct {
my $file = shift;
open my $fh, '<', $file or die;
$/ = undef;
my $data = <$fh>;
close $fh;
return Load($data);
}
# some files or directories may be included in a listed directory
# remove those from the list and print a warning
sub remove_transclusion {
my $data = shift;
my @files = uniq @{$data->{"files"}};
my @directories = uniq @{$data->{"directories"}};
for(my $i=0; $i <= $#directories; $i++) {
my $dir = $directories[$i];
next if ( $dir eq "") ;
2022-03-14 07:23:00 +00:00
# check if a file is contained by a directory
foreach my $file (@files) {
my($filename, $dirs, $suffix) = fileparse($file);
if( $dirs =~ m/^$dir/ ) {
2022-03-14 07:23:00 +00:00
say STDERR "WARNING: $dir contains $file";
$file = "";
}
}
# check if a directory is contained by another directory
# if so, mark them for deletion later
# we can't remove directly from the array because we
# are iterating in it in the main loop
for(my $j=0; $j <= $#directories; $j++) {
if( $j != $i ) {
2022-03-14 07:23:00 +00:00
my $dir2 = $directories[$j];
next if ( $dir2 eq "" );
if( $dir =~ m/$dir2/ ) {
2022-03-14 07:23:00 +00:00
say STDERR "WARNING: $dir2 contains $dir";
$directories[$i] = "";
}
}
}
}
# remove empty entries
@directories = sort grep { /^./ } @directories;
@files = sort grep { /^./ } @files;
$data->{"files"} = \@files;
$data->{"directories"} = \@directories;
return $data;
}
# create all the links in the ramdisk
sub populate_ramdisk {
my ($data, $persist_home, $impermanence_home, $user) = @_;
saydebug("create the symlinks for files set");
create_links(@{$data->{"files"}}, $persist_home, $impermanence_home, $user);
2022-03-14 07:23:00 +00:00
saydebug("create the symlinks for directories set");
create_links(@{$data->{"directories"}}, $persist_home, $impermanence_home, $user);
}
2022-03-14 07:23:00 +00:00
# check if the mountpoint is already mounted with mfs
sub is_mounted {
my $impermanence_home = shift;
my $ret;
my $mounted = 0;
2022-03-14 07:23:00 +00:00
# is this already mounted?
$ret = `df $impermanence_home | awk -v dest=$impermanence_home '/^mfs/ && \$NF == dest { print }'`;
if ( $ret =~ m/^mfs/ ) {
$mounted = 1;
2022-03-14 07:23:00 +00:00
}
return $mounted;
2022-03-14 07:23:00 +00:00
}
# mount the destination with a ramdisk only if not currently mounted
sub mount_mfs {
my ($user, $impermanence_home, $data) = @_;
my $ret;
saydebug("finding a ffs mountpoint to use for mfs");
if( is_mounted($impermanence_home) ) {
trap_error("ERROR: $impermanence_home is already mounted with MFS");
}
my $fs = `swapctl | tail -n 1 | cut -d ' ' -f 1 | tr -d '\n'`;
saydebug "mount the destination using mount_mfs from $fs";
$ret = system("mount_mfs", "-s", $data->{size}, $fs, $impermanence_home);
if( $ret != 0 ) {
trap_error("ERROR: mounting the mfs filesystem errored with error $ret");
} else {
saydebug("mount_mfs done on $impermanence_home");
}
2022-03-14 07:23:00 +00:00
}
# create the symbolic links listed in the yml file into the ramdisk destination
sub create_links {
my ($list, $persist_home, $impermanence_home, $user) = @_;
foreach ($list) {
my $old_file = $persist_home."/".$user."/".$_;
my $new_file = $impermanence_home."/".$_;
2022-03-14 07:23:00 +00:00
my ($filename, $dirs, $suffix) = fileparse($new_file);
2022-03-14 07:23:00 +00:00
# recursively create missing directories to hold files
# give ownership to the user and apply chmod 700
if ( ! -e $dirs ) {
make_path($dirs, { chmod => 0700, owner => $user });
}
2022-03-14 07:23:00 +00:00
if( symlink($old_file, $new_file) == 0 ) {
trap_error("symlink $old_file to $new_file");
} else {
saydebug("ln -s $old_file $new_file");
}
}
2022-03-14 07:23:00 +00:00
}
sub main {
my %opts;
my ($impermanence_home, $persist_home, $configuration_file, $data, $ret);
2022-03-14 07:23:00 +00:00
# define command line parameters
getopts("vt:d:u:", \%opts);
2022-03-14 07:23:00 +00:00
# verbose mode for debug output
if( defined $opts{v} ) {
$verbose = 1;
2022-03-14 07:23:00 +00:00
}
# check if using test mode to validate a configuration file
if( defined $opts{t} ) {
say STDERR "WARNING: test mode enabled";
$configuration_file = $opts{t};
# non-test mode, mount the ramdisk and populates it
} else {
# -d and -u flags are mandatory
if( ! defined $opts{d} || ! defined $opts{u} ) {
usage();
}
# test if the script is running as root
if( $< != 0 ) {
trap_error("$0 must be run as root.");
}
$impermanence_home = File::HomeDir->users_home($opts{u});
if( $impermanence_home !~ m|^/| ) {
trap_error("The user \$HOME doesn't start with / , its value is $impermanence_home");
}
if( ! -d $impermanence_home ) {
trap_error("The user \$HOME $impermanence_home doesn't exist");
}
$persist_home = $opts{d};
if( $persist_home !~ m|^/| ) {
trap_error("The persistent directory $persist_home must be an absolute path");
}
if( ! -d $persist_home ) {
trap_error("The persistent directory $persist_home doesn't exist");
}
$configuration_file = $persist_home."/".$opts{u}."/impermanence.yml";
}
# exit if the configuration file is not available
if( ! -f $configuration_file ) {
trap_error("The file ".$configuration_file." can't be found");
}
# read file and sanitize content
$data = read_yml_file_to_struct($configuration_file);
$data = remove_transclusion($data);
# display result and stop if in test mode
if( defined $opts{t} ) {
print Dumper $data;
exit 0;
}
if( $ARGV[0] eq "start" ) {
$start = 1;
}elsif( $ARGV[0] eq "restart" ) {
$stop = 1;
$start = 1;
}elsif( $ARGV[0] eq "stop" ) {
$stop = 1;
}else{
usage();
}
2022-03-14 07:23:00 +00:00
if( $stop && is_mounted($impermanence_home) ) {
my $status = system("umount", $impermanence_home);
if( $status != 0 ) {
trap_error("umount did exit with status $status");
}
}
2022-03-14 07:23:00 +00:00
if( $start ) {
mount_mfs($opts{u}, $impermanence_home, $data);
populate_ramdisk($data, $persist_home, $impermanence_home, $opts{u});
2022-03-14 07:23:00 +00:00
}
2022-03-14 07:23:00 +00:00
}
main()