Subversion Repositories zfs_utils

Rev

Rev 39 | Rev 41 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

# Simplified BSD License (FreeBSD License)
#
# Copyright (c) 2025, Daily Data Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
#    list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

package ZFS_Utils;

use strict;
use warnings;
use Exporter 'import';
use Data::Dumper;
use POSIX qw(strftime);
use File::Path qw(make_path);

# library of ZFS related utility functions
# Copyright 2024 Daily Data Inc. <rodo@dailydata.net>

# currently used for sneakernet scripts, but plans to expand to other ZFS related tasks
# functions include:
#   runCmd: execute a command and return its output
#   shredFile: securely delete a file using gshred
#   logMsg: log messages to a log file and optionally to console
#   mountDriveByLabel: find and mount a drive by its GPT label
#   loadConfig: load a YAML configuration file into a hashref
#   mountGeli: decrypt and mount a GELI encrypted ZFS pool
#   makeGeliKey: create a GELI key by XOR'ing a remote binary keyfile and a local hex key
#   decryptAndMountGeli: decrypt GELI disks and mount the ZFS pool
#   findGeliDisks: find available disks for GELI/ZFS use
#   makeReplicateCommands: create zfs send commands for replication based on snapshot lists


# Exported functions and variables

our @EXPORT_OK = qw(loadConfig shredFile mountDriveByLabel mountGeli logMsg runCmd makeReplicateCommands sendReport $logFileName $displayLogsOnConsole $lastRunError);


our $VERSION = '0.2';
our $logFileName = '/tmp/zfs_utils.log'; # this can be overridden by the caller, and turned off with empty string
our $displayLogsOnConsole = 1; # if non-zero, log messages are also printed to console
our $merge_stderr = 0; # if set to 1, stderr is captured in runCmd
our $lastRunError = 0; # tracks the last error code from runCmd

# Execute a command and return its output.
# If called in scalar context, returns the full output as a single string.
# If called in list context, returns the output split into lines.
# If $merge_stderr is true (default), stderr is merged into stdout (only for scalar commands).
# returns undef on failure and logs failure message.
sub runCmd {
   my $cmd = join( ' ', @_ );
   $merge_stderr = 1 unless defined $merge_stderr;
   my $output = '';

   logMsg( "Running command [$cmd]" );
   $cmd .= ' 2>&1' if $merge_stderr;
   $output = `$cmd`;
   $lastRunError = $?;
   if ( $lastRunError ) {
      if ($? == -1) {
         logMsg( "failed to execute: $!");
         return '';
      } elsif ($? & 127) { # fatal error, exit program
         logMsg( sprintf( "child died with signal %d, %s coredump\n", ($? & 127),  ($? & 128) ? 'with' : 'without' ) );
         die;
      } elsif ($? >> 8) { # it had some return code other than 0
         logMsg( sprintf( "child exited with value %d\n", $? >> 8 ) );
      }
   }
   $output //= '';

   if (wantarray) {
      return $output eq '' ? () : split(/\n/, $output);
   } else {
      return $output;
   }
}

# this calls gshred which will overwrite the file 3 times, then
# remove it.
# NOTE: this will not work on ZFS, since ZFS is CopyOnWrite (COW)
# so assuming file is on something without COW (ramdisk, UFS, etc)
sub shredFile {
   my $filename = shift;
   `/usr/local/bin/gshred -u -f -s 32 $filename` if -e $filename;
}

sub logMsg {
    my $msg = shift;
    my $filename = shift // $logFileName;
    my $timeStampFormat = shift // '%Y-%m-%d %H:%M:%S';
    my $timestamp = strftime($timeStampFormat, localtime());
    if (defined $filename && $filename ne '' ) {
       open my $logfh, '>>', $filename or die "Could not open log file $filename: $!\n";
       print $logfh "$timestamp\t$msg\n";
       close $logfh;
    }
    print "$timestamp\t$msg\n" if ($displayLogsOnConsole);
}

# find a drive by it's label by scanning /dev/gpt/
# driveInfo is a hashref with the following keys:
# label - the GPT label of the drive (required)
# filesystem - the filesystem type (default: ufs)
# mountPath - where to mount the drive (default: /mnt/label)
# timeout - how long to wait for the drive (default: 600 seconds)
# check_interval - how often to check for the drive (default: 15 seconds)
# If the drive is found, mount it on mountPath and return the mountPath.
# If not found, return empty string.
sub mountDriveByLabel {
   my ( $driveInfo ) = @_;
   unless ($driveInfo->{label}) {
      logMsg("mountDriveByLabel: No drive label provided");
      return '';
   }
   unless ( $driveInfo->{label} =~ /^[a-zA-Z0-9_\-]+$/ ) {
      logMsg("mountDriveByLabel: Invalid label '$driveInfo->{label}'");
      return '';
   }

   logMsg("mountDriveByLabel: Looking for drive with label '$driveInfo->{label}'");
   # default to /mnt/label if not provided
   $driveInfo->{mountPath} //= "/mnt/$driveInfo->{label}"; # this is where we'll mount it if we find it
   $driveInfo->{filesystem} //= 'ufs'; # default to mounting ufs
   # The location for the label depends on filesystem. Only providing access to ufs and msdos here for safety.
   # gpt labeled drives for ufs are in /dev/gpt/, for msdosfs in /dev/msdosfs/
   $driveInfo->{label} = $driveInfo->{filesystem} eq 'msdos' ? "/dev/msdosfs/$driveInfo->{label}" : "/dev/gpt/$driveInfo->{label}"; 
   # drive already mounted, just return the path
   my $output = runCmd( "mount | grep '$driveInfo->{mountPath}'" );
   return $driveInfo->{mountPath} if ( $lastRunError == 0 ); # grep found it for us
   # default to 10 minutes (600 seconds) if not provided
   $driveInfo->{timeout} //= 600;
   # default to checking every minute if not provided
   $driveInfo->{check_interval} //= 15;
   # wait up to $timeout seconds for device to appear, checking every 10 seconds
   while ( $driveInfo->{timeout} > 0 ) {
      if ( -e "$driveInfo->{label}" ) {
         last;
      } else {
         print "Waiting for drive labeled $driveInfo->{label}\n";
         sleep $driveInfo->{check_interval};
         $driveInfo->{timeout} -= $driveInfo->{check_interval};
      }
    }
    # if we found it, mount and return mount path
    if ( -e "$driveInfo->{label}" ) {
       # ensure mount point
       unless ( -d $driveInfo->{mountPath} || make_path($driveInfo->{mountPath}) ) {
         logMsg("Failed to create $driveInfo->{mountPath}: $!");
         return '';
       }
       # mount device
       runCmd( "mount -t $driveInfo->{filesystem} $driveInfo->{label} $driveInfo->{mountPath}" );
       if ( $lastRunError ) {
         logMsg("Failed to mount $driveInfo->{label} on $driveInfo->{mountPath}: $!");
         return '';
       }
       return $driveInfo->{mountPath};
    } else {
       return '';
    }
}

## Load a YAML configuration file into a hashref.
## If the file does not exist, and a default hashref is provided,
## create the file by dumping the default to YAML, then return the default.
sub loadConfig {
    my ($filename, $default) = @_;

    # If no filename was provided, return default or empty hashref
    die "No filename provided to loadConfig\n" unless defined $filename;

    # If file doesn't exist but a default hashref was provided, try to
    # create the file by dumping the default to YAML, then return the default.
    unless (-e $filename) {
      logMsg("Config file $filename does not exist. Creating it with default values.");
      if ($default && ref $default eq 'HASH') {
         my $wrote = 0;
         eval {
               require YAML::XS;
               YAML::XS->import();
               YAML::XS::DumpFile($filename, $default);
               $wrote = 1;
               1;
         } or do {
               eval {
                  require YAML::Tiny;
                  YAML::Tiny->import();
                  my $yt = YAML::Tiny->new($default);
                  $yt->write($filename);
                  $wrote = 1;
                  1;
               } or do {
                  logMsg("No YAML writer available (YAML::XS or YAML::Tiny). Could not create $filename");
               };
         };

         die "Failed to write default config to $filename:$!\n" unless $wrote;
        }

        # No default provided; nothing to create
        return {};
    }

    my $yaml;

    # Try YAML::XS first, fall back to YAML::Tiny
    eval {
        require YAML::XS;
        YAML::XS->import();
        $yaml = YAML::XS::LoadFile($filename);
        logMsg("using YAML::XS to load $filename");
        1;
    } or do {
        eval {
            require YAML::Tiny;
            YAML::Tiny->import();
            $yaml = YAML::Tiny->read($filename);
            $yaml = $yaml->[0] if $yaml;  # YAML::Tiny returns an arrayref of documents
            logMsg("using YAML::Tiny to load $filename");
            1;
        } or do {
            logMsg("No YAML parser installed (YAML::XS or YAML::Tiny). Skipping config load from $filename");
            return ($default && ref $default eq 'HASH') ? $default : {};
        };
    };
    # Ensure we have a hashref
    die "Config file $filename did not produce a HASH.\n" unless (defined $yaml && ref $yaml eq 'HASH');

    return $yaml;
}


# Mount a GELI-encrypted ZFS pool.
# $geliConfig - hashref containing configuration for geli
# Returns the pool name on success, empty string on error.
sub mountGeli {
   my $geliConfig = shift;

   logMsg( "geli config detected, attempting to mount geli disks" );
   # Can't continue at all if no pool name
   unless ( $geliConfig->{'poolname'} ) {
      logMsg "Could not find pool name in configuration file\n";
      return '';
   }
   # find the keyfile disk and mount it
   $geliConfig->{secureKey}->{path} = mountDriveByLabel( $geliConfig->{secureKey} );
   unless ( $geliConfig->{secureKey}->{path} ) {
      logMsg "Could not find or mount keyfile disk with label: " . $geliConfig->{secureKey}->{label};
      return '';
   }
   # create the combined geli keyfile in target location
   unless ( makeGeliKey( $geliConfig ) ) {
         logMsg "Could not create geli keyfile\n";
         return '';
      }
   # decrypt and mount the geli disks and zfs pool
   my $poolname = decryptAndMountGeli( $geliConfig );
   return $poolname;
                                                
}

# find all disks which are candidates for use with geli/zfs
# Grabs all disks on the system, then removes those with partitions
# and those already used in zpools.
sub findGeliDisks {
   logMsg("Finding available disks for GELI/ZFS use");
   # get all disks in system
   my %allDisks = map{ chomp $_ ; $_ => 1 } runCmd( "geom disk list | grep 'Geom name:' | rev | cut -d' ' -f1 | rev" );
   # get the disks with partitions
   my @temp = runCmd( "gpart show -p | grep '^=>'");  # -p prints just the disks without partitions
   # remove them from the list
   foreach my $disk ( @temp ) {
      $allDisks{$1} = 0 if ( $disk =~ m/^=>[\t\s0-9]+([a-z][a-z0-9]+)/ ) ;
   }

   # get disk which are currently used for zpools
   @temp = runCmd( "zpool status -LP | grep '/dev/'" );
   foreach my $disk ( @temp ) {
      $allDisks{$1} = 0 if  $disk =~ m|/dev/([a-z]+\d+)|;
   }

   # return only the disks which are free (value 1)
   return grep{ $allDisks{$_} == 1 } keys %allDisks;
}

## Decrypt each GELI disk from $geliConfig->{'diskList'} using the keyfile,
## then import and mount the ZFS pool specified in $geliConfig->{'poolname'}.
##
## Returns the pool name on success, empty on error.
sub decryptAndMountGeli {
   my ($geliConfig) = shift;
   
   # if no list of disks provided, try to find them
   $geliConfig->{'diskList'} //= [ findGeliDisks() ];
   
   my $diskList = $geliConfig->{'diskList'};
   my $poolname = $geliConfig->{'poolname'};
   my $keyfile = $geliConfig->{'target'};
   unless ( -e $keyfile ) {
      logMsg "GELI keyfile $keyfile does not exist\n";
      return '';
   }

   my @decrypted_devices;

   # Decrypt each disk in the list
   foreach my $disk (@{$geliConfig->{'diskList'}}) {
      $disk = '/dev/' . $disk unless $disk =~ m|/dev|;
      unless ( -e $disk ) {
         logMsg "Disk $disk does not exist\n";
         return '';
      }

      # Derive the decrypted device name (.eli suffix on FreeBSD)
      my $decrypted = $disk . '.eli';

      # Decrypt using geli attach with the keyfile
      logMsg("Decrypting $disk with keyfile $keyfile");
      runCmd("geli attach -k $geliConfig->{target} $disk");
      if ( $lastRunError)
         logMsg "Failed to decrypt $disk (exit $lastRunError)\n";
         next; # ignore failed disks and continue to see if we can import the pool
      }

      unless ( -e $decrypted ) {
         logMsg "Decrypted device $decrypted does not exist after geli attach\n";
         return '';
      }
      push @decrypted_devices, $decrypted;
   }

   # Import the ZFS pool
   logMsg("Importing ZFS pool $poolname");
   my @import_cmd = ('zpool', 'import');
   # If decrypted devices exist, add their directories to -d list
   #foreach my $dev (@decrypted_devices) {
   #   my $dir = $dev;
   #   $dir =~ s!/[^/]+$!!;  # Remove filename to get directory
   #   push @import_cmd, '-d', $dir;
   #}
   
   push @import_cmd, $poolname;

   runCmd("zpool import $poolname" );
   unless ( $lastRunError == 0 ) {
      logMsg("Failed to import zfs pool $poolname (exit $lastRunError)\n");
      return '';
   }

   # Mount the ZFS pool (zfs mount -a mounts all filesystems in the pool)
   logMsg("Mounting ZFS pool $poolname");
   runCmd('zfs mount -a');
   unless ( $lastRunError == 0 ) {
      logMsg("Failed to mount zfs pool $poolname (exit $lastRunError)\n");
      return '';
   }

   logMsg("Successfully decrypted and mounted pool $poolname");
   return $poolname;
}

## Create a GELI key by XOR'ing a remote binary keyfile and a local key (hex string).
##
## Arguments:
##   $remote_keyfile - path to binary keyfile (32 bytes)
##   $localKeyHexOrPath - hex string (64 hex chars) or path to file containing hex
##   $target - path to write the resulting 32-byte binary key
##
## Returns true on success, dies on fatal error.
sub makeGeliKey {
   my ( $geliConfig ) = @_;

   $geliConfig->{secureKey}->{keyfile} //= '';
   $geliConfig->{localKey} //= '';
   $geliConfig->{target} //= '';

   if ( $geliConfig->{target} && -f $geliConfig->{target} ) {
      logMsg "GELI target keyfile $geliConfig->{target} already exists. Not overwriting.\n";
      return 1;
   }

   my $remote_keyfile = "$geliConfig->{secureKey}->{path}/$geliConfig->{secureKey}->{keyfile}";
   my $localKeyHexOrPath = $geliConfig->{localKey};
   my $target = $geliConfig->{target};

   if ( $geliConfig->{secureKey}->{keyfile} && $geliConfig->{localKey} ) {
      # we have what we need to proceed

      if ( -f $remote_keyfile ) {
         logMsg "Creating GELI keyfile at $geliConfig->{target} using remote keyfile " . $geliConfig->{secureKey}->{keyfile} . " and local key\n";
      } else {
         die "Remote keyfile " . $geliConfig->{secureKey}->{keyfile} . " does not exist\n";
      }
   }

   # Read remote binary key
   open my $rh, '<:raw', $remote_keyfile or die "Unable to open $remote_keyfile: $!\n";
   my $rbuf;
   my $read = read($rh, $rbuf, 32);
   close $rh;
   die "Failed to read 32 bytes from $remote_keyfile (got $read)\n" unless defined $read && $read == 32;

   # Get local hex string (either direct string or file contents)
   my $hex;
   if (-e $localKeyHexOrPath) {
      open my $lh, '<', $localKeyHexOrPath or die "Unable to open local key file $localKeyHexOrPath: $!\n";
      local $/ = undef;
      $hex = <$lh>;
      close $lh;
   } else {
      $hex = $localKeyHexOrPath;
   }
   # clean hex (remove whitespace/newlines and optional 0x)
   $hex =~ s/0x//g;
   $hex =~ s/[^0-9a-fA-F]//g;

   die "Local key must be 64 hex characters (256-bit)\n" unless length($hex) == 64;

   my $lbuf = pack('H*', $hex);
   die "Local key decoded to unexpected length " . length($lbuf) . "\n" unless length($lbuf) == 32;

   # XOR the two buffers
   my $out = '';
   for my $i (0 .. 31) {
      $out .= chr( ord(substr($rbuf, $i, 1)) ^ ord(substr($lbuf, $i, 1)) );
   }

   # Ensure target directory exists
   my ($vol, $dirs, $file) = ($target =~ m{^(/?)(.*/)?([^/]+)$});
   if ($dirs) {
      my $dir = $dirs;
      $dir =~ s{/$}{};
      unless (-d $dir) {
         require File::Path;
         File::Path::make_path($dir) or die "Failed to create directory $dir: $!\n";
      }
   }

   # Write out binary key and protect permissions
   open my $oh, '>:raw', $target or die "Unable to open $target for writing: $!\n";
   print $oh $out or die "Failed to write to $target: $!\n";
   close $oh;
   chmod 0600, $target;

   return 1;
}

sub makeReplicateCommands {
   my ($sourceSnapsRef, $statusRef, $newStatusRef) = @_;
   $sourceSnapsRef ||= [];
   $statusRef     ||= [];
   $newStatusRef  ||= [];

   # parse snapshots: each line is expected to have snapshot fullname as first token: pool/fs@snap ...
   my %snaps_by_fs;
   foreach my $line (@$sourceSnapsRef) {
      next unless defined $line && $line =~ /\S/;
      my ($tok) = split /\s+/, $line;
      next unless $tok && $tok =~ /@/;
      my ($fs, $snap) = split /@/, $tok, 2;
      push @{ $snaps_by_fs{$fs} }, $snap;
   }

   # nothing to do
   return [] unless keys %snaps_by_fs;

   # figure root filesystem: first snapshot line's fs is the requested root
   my ($first_line) = grep { defined $_ && $_ =~ /\S/ } @$sourceSnapsRef;
   my ($root_fs) = $first_line ? (split(/\s+/, $first_line))[0] =~ /@/ ? (split(/@/, (split(/\s+/, $first_line))[0]))[0] : undef : undef;
   $root_fs ||= (sort keys %snaps_by_fs)[0];

   # helper: find last status entry for a filesystem (status lines contain full snapshot names pool/fs@snap)
   my %last_status_for;
   for my $s (@$statusRef) {
      next unless $s && $s =~ /@/;
      my ($fs, $snap) = split /@/, $s, 2;
      $last_status_for{$fs} = $snap;    # later entries override earlier ones -> last occurrence kept
   }

   # build per-filesystem "from" and "to"
   my %from_for;
   my %to_for;
   foreach my $fs (keys %snaps_by_fs) {
      my $arr = $snaps_by_fs{$fs};
      next unless @$arr;
      $to_for{$fs} = $arr->[-1];
      $from_for{$fs} = $last_status_for{$fs};    # may be undef -> full send required
   }

   # decide if we can do a single recursive send:
   # condition: all 'to' snapshot names are identical
   my %to_names = map { $_ => 1 } values %to_for;
   my $single_to_name = (keys %to_names == 1) ? (keys %to_names)[0] : undef;

   my %commands;

   if ($single_to_name) {
      # check whether any from is missing
      my @from_values = map { $from_for{$_} } sort keys %from_for;
      my $any_from_missing = grep { !defined $_ } @from_values;
      my %from_names = map { $_ => 1 } grep { defined $_ } @from_values;
      my $single_from_name = (keys %from_names == 1) ? (keys %from_names)[0] : undef;

      if ($any_from_missing) {
         # full recursive send from root
         $commands{$root_fs} = sprintf('zfs send -R %s@%s', $root_fs, $single_to_name);
      }
      elsif ($single_from_name) {
         # incremental recursive send, but don't do it if they are the same
         $commands{$root_fs} = sprintf('zfs send -R -I %s@%s %s@%s',
                           $root_fs, $single_from_name, $root_fs, $single_to_name)
                           unless $single_from_name eq $single_to_name;
      }
      else {
         # from snapshots differ across children -> fall back to per-filesystem sends
         foreach my $fs (sort keys %to_for) {
            my $to  = $to_for{$fs};
            my $from = $from_for{$fs};
            if ($from) {
               # if from and to are different, add it
               $commands{$fs} = sprintf('zfs send -I %s@%s %s@%s', $fs, $from, $fs, $to)
                  unless $from eq $to;
            } else {
               $commands{$fs} = sprintf('zfs send %s@%s', $fs, $to);
            }
         }
      }

      # update new status: record newest snap for every filesystem
      foreach my $fs (keys %to_for) {
         push @$newStatusRef, sprintf('%s@%s', $fs, $to_for{$fs});
      }
   } else {
      # not all children share same newest snap -> per-filesystem sends
      foreach my $fs (sort keys %to_for) {
         my $to  = $to_for{$fs};
         my $from = $from_for{$fs};
         if ($from) {
            $commands{$fs} = sprintf('zfs send -I %s@%s %s@%s', $fs, $from, $fs, $to);
         } else {
            $commands{$fs} = sprintf('zfs send %s@%s', $fs, $to);
         }
         push @$newStatusRef, sprintf('%s@%s', $fs, $to);
      }
   }

   # return arrayref of commands (caller can iterate or join with pipes)
   return \%commands;
}

# Send report via email and/or copy to target drive.
# $reportConfig is a hashref with optional keys:
#   email - email address to send report to
#   targetDrive - hashref with keys:
#       label - GPT or msdosfs label of the target drive
#       mount_point - optional mount point to use (if not provided, /mnt/label is used)
# $subject is the email subject
# $logFile is the path to the log file to send/copy
sub sendReport {
   my ( $reportConfig, $subject, $logFile ) = @_;
   return unless defined $reportConfig;
   logMsg( "Beginning sendReport" );
   # if they have set an e-mail address, try to e-mail the report
   if ( defined $reportConfig->{email} && $reportConfig->{email} ne '' ) {
      logMsg( "Sending report via e-mail to $reportConfig->{email}");
      sendEmailReport( $reportConfig->{email}, $subject, $logFile );
   }
   # if targetDrive defined and there is a valid label for it, try to mount it and write the report there
   if ( defined $reportConfig->{targetDrive} && defined $reportConfig->{targetDrive}->{label} && $reportConfig->{targetDrive}->{label} ) {
      logMsg( "Saving report to disk with label $reportConfig->{targetDrive}->{label}" );
      my $mountPoint = mountDriveByLabel( $reportConfig->{targetDrive}->{label}, $reportConfig->{targetDrive}->{mount_point}, 300 );
      if ( defined $mountPoint && $mountPoint ) {
         copyReportToDrive( $logFile, $mountPoint );
         `umount $mountPoint`;
         rmdir $mountPoint;
      } else {
         logMsg( "Warning: could not mount report target drive with label '$reportConfig->{targetDrive}->{label}'" );
      }
   }
}

# Copy the report log file to the specified mount point.
# $logFile is the path to the log file to copy.
# $mountPoint is the mount point of the target drive.
# Does nothing if log file or mount point are invalid.
sub copyReportToDrive {
   my ( $logFile, $mountPoint ) = @_;
   return unless defined $logFile && -e $logFile;
   return unless defined $mountPoint && -d $mountPoint;

   my $targetFile = "$mountPoint/" . ( split( /\//, $logFile ) )[-1];
   logMsg( "Copying report log file $logFile to drive at $mountPoint" );
   unless ( copy( $logFile, $targetFile ) ) {
      logMsg( "Could not copy report log file to target drive: $!" );
   }
}

# Send an email report with the contents of the log file.
# $to is the recipient email address.
# $subject is the email subject.
# $logFile is the path to the log file to send.
# Does nothing if any parameter is invalid.
sub sendEmailReport {
   my ( $to, $subject, $logFile ) = @_;
   return unless defined $to && $to ne '';
   $subject //= 'Sneakernet Replication Report from ' . `hostname`;
   $logFile //= '';

   logMsg( "Sending email report to $to with subject '$subject'" );
   open my $mailfh, '|-', '/usr/sbin/sendmail -t' or do {
      logMsg( "Could not open sendmail: $!" );
      return;
   };
   print $mailfh "To: $to\n";
   print $mailfh "Subject: $subject\n";
   print $mailfh "MIME-Version: 1.0\n";
   print $mailfh "Content-Type: text/plain; charset=\"utf-8\"\n";
   print $mailfh "\n"; # end of headers
   
   print $mailfh "Following is the report for replication\n\n";

   if ( -e $logFile && open my $logfh, '<', $logFile ) {
      while ( my $line = <$logfh> ) {
         print $mailfh $line;
      }
      close $logfh;
   } else {
      logMsg( "Could not open log file [$logFile] for reading: $!" );
   };

   close $mailfh;
}  

1;