Subversion Repositories zfs_utils

Rev

Rev 40 | Rev 42 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
34 rodolico 1
# Simplified BSD License (FreeBSD License)
2
#
3
# Copyright (c) 2025, Daily Data Inc.
4
# All rights reserved.
5
#
6
# Redistribution and use in source and binary forms, with or without
7
# modification, are permitted provided that the following conditions are met:
8
#
9
# 1. Redistributions of source code must retain the above copyright notice, this
10
#    list of conditions and the following disclaimer.
11
#
12
# 2. Redistributions in binary form must reproduce the above copyright notice,
13
#    this list of conditions and the following disclaimer in the documentation
14
#    and/or other materials provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 
24 rodolico 27
package ZFS_Utils;
28
 
29
use strict;
30
use warnings;
31
use Exporter 'import';
32
use Data::Dumper;
33
use POSIX qw(strftime);
34
use File::Path qw(make_path);
35
 
34 rodolico 36
# library of ZFS related utility functions
37
# Copyright 2024 Daily Data Inc. <rodo@dailydata.net>
38
 
39
# currently used for sneakernet scripts, but plans to expand to other ZFS related tasks
40
# functions include:
41
#   runCmd: execute a command and return its output
42
#   shredFile: securely delete a file using gshred
43
#   logMsg: log messages to a log file and optionally to console
44
#   mountDriveByLabel: find and mount a drive by its GPT label
45
#   loadConfig: load a YAML configuration file into a hashref
46
#   mountGeli: decrypt and mount a GELI encrypted ZFS pool
47
#   makeGeliKey: create a GELI key by XOR'ing a remote binary keyfile and a local hex key
48
#   decryptAndMountGeli: decrypt GELI disks and mount the ZFS pool
49
#   findGeliDisks: find available disks for GELI/ZFS use
50
#   makeReplicateCommands: create zfs send commands for replication based on snapshot lists
51
 
52
 
53
# Exported functions and variables
54
 
37 rodolico 55
our @EXPORT_OK = qw(loadConfig shredFile mountDriveByLabel mountGeli logMsg runCmd makeReplicateCommands sendReport $logFileName $displayLogsOnConsole $lastRunError);
24 rodolico 56
 
57
 
34 rodolico 58
our $VERSION = '0.2';
24 rodolico 59
our $logFileName = '/tmp/zfs_utils.log'; # this can be overridden by the caller, and turned off with empty string
34 rodolico 60
our $displayLogsOnConsole = 1; # if non-zero, log messages are also printed to console
27 rodolico 61
our $merge_stderr = 0; # if set to 1, stderr is captured in runCmd
37 rodolico 62
our $lastRunError = 0; # tracks the last error code from runCmd
24 rodolico 63
 
25 rodolico 64
# Execute a command and return its output.
65
# If called in scalar context, returns the full output as a single string.
66
# If called in list context, returns the output split into lines.
67
# If $merge_stderr is true (default), stderr is merged into stdout (only for scalar commands).
34 rodolico 68
# returns undef on failure and logs failure message.
25 rodolico 69
sub runCmd {
33 rodolico 70
   my $cmd = join( ' ', @_ );
25 rodolico 71
   $merge_stderr = 1 unless defined $merge_stderr;
72
   my $output = '';
73
 
34 rodolico 74
   logMsg( "Running command [$cmd]" );
75
   $cmd .= ' 2>&1' if $merge_stderr;
76
   $output = `$cmd`;
37 rodolico 77
   $lastRunError = $?;
78
   if ( $lastRunError ) {
79
      if ($? == -1) {
80
         logMsg( "failed to execute: $!");
81
         return '';
82
      } elsif ($? & 127) { # fatal error, exit program
83
         logMsg( sprintf( "child died with signal %d, %s coredump\n", ($? & 127),  ($? & 128) ? 'with' : 'without' ) );
84
         die;
85
      } elsif ($? >> 8) { # it had some return code other than 0
86
         logMsg( sprintf( "child exited with value %d\n", $? >> 8 ) );
87
      }
34 rodolico 88
   }
25 rodolico 89
   $output //= '';
90
 
91
   if (wantarray) {
92
      return $output eq '' ? () : split(/\n/, $output);
93
   } else {
94
      return $output;
95
   }
96
}
97
 
24 rodolico 98
# this calls gshred which will overwrite the file 3 times, then
99
# remove it.
100
# NOTE: this will not work on ZFS, since ZFS is CopyOnWrite (COW)
101
# so assuming file is on something without COW (ramdisk, UFS, etc)
102
sub shredFile {
103
   my $filename = shift;
104
   `/usr/local/bin/gshred -u -f -s 32 $filename` if -e $filename;
105
}
106
 
107
sub logMsg {
108
    my $msg = shift;
109
    my $filename = shift // $logFileName;
110
    my $timeStampFormat = shift // '%Y-%m-%d %H:%M:%S';
111
    my $timestamp = strftime($timeStampFormat, localtime());
112
    if (defined $filename && $filename ne '' ) {
113
       open my $logfh, '>>', $filename or die "Could not open log file $filename: $!\n";
114
       print $logfh "$timestamp\t$msg\n";
115
       close $logfh;
116
    }
117
    print "$timestamp\t$msg\n" if ($displayLogsOnConsole);
118
}
119
 
35 rodolico 120
# find a drive by it's label by scanning /dev/gpt/
121
# driveInfo is a hashref with the following keys:
122
# label - the GPT label of the drive (required)
123
# filesystem - the filesystem type (default: ufs)
124
# mountPath - where to mount the drive (default: /mnt/label)
125
# timeout - how long to wait for the drive (default: 600 seconds)
126
# check_interval - how often to check for the drive (default: 15 seconds)
24 rodolico 127
# If the drive is found, mount it on mountPath and return the mountPath.
128
# If not found, return empty string.
129
sub mountDriveByLabel {
35 rodolico 130
   my ( $driveInfo ) = @_;
131
   unless ($driveInfo->{label}) {
132
      logMsg("mountDriveByLabel: No drive label provided");
24 rodolico 133
      return '';
134
   }
35 rodolico 135
   unless ( $driveInfo->{label} =~ /^[a-zA-Z0-9_\-]+$/ ) {
136
      logMsg("mountDriveByLabel: Invalid label '$driveInfo->{label}'");
24 rodolico 137
      return '';
138
   }
139
 
35 rodolico 140
   logMsg("mountDriveByLabel: Looking for drive with label '$driveInfo->{label}'");
24 rodolico 141
   # default to /mnt/label if not provided
35 rodolico 142
   $driveInfo->{mountPath} //= "/mnt/$driveInfo->{label}"; # this is where we'll mount it if we find it
143
   $driveInfo->{filesystem} //= 'ufs'; # default to mounting ufs
34 rodolico 144
   # The location for the label depends on filesystem. Only providing access to ufs and msdos here for safety.
145
   # gpt labeled drives for ufs are in /dev/gpt/, for msdosfs in /dev/msdosfs/
37 rodolico 146
   $driveInfo->{label} = $driveInfo->{filesystem} eq 'msdos' ? "/dev/msdosfs/$driveInfo->{label}" : "/dev/gpt/$driveInfo->{label}"; 
31 rodolico 147
   # drive already mounted, just return the path
37 rodolico 148
   my $output = runCmd( "mount | grep '$driveInfo->{mountPath}'" );
149
   return $driveInfo->{mountPath} if ( $lastRunError == 0 ); # grep found it for us
24 rodolico 150
   # default to 10 minutes (600 seconds) if not provided
35 rodolico 151
   $driveInfo->{timeout} //= 600;
24 rodolico 152
   # default to checking every minute if not provided
35 rodolico 153
   $driveInfo->{check_interval} //= 15;
24 rodolico 154
   # wait up to $timeout seconds for device to appear, checking every 10 seconds
35 rodolico 155
   while ( $driveInfo->{timeout} > 0 ) {
156
      if ( -e "$driveInfo->{label}" ) {
24 rodolico 157
         last;
158
      } else {
37 rodolico 159
         print "Waiting for drive labeled $driveInfo->{label}\n";
35 rodolico 160
         sleep $driveInfo->{check_interval};
161
         $driveInfo->{timeout} -= $driveInfo->{check_interval};
24 rodolico 162
      }
163
    }
164
    # if we found it, mount and return mount path
35 rodolico 165
    if ( -e "$driveInfo->{label}" ) {
24 rodolico 166
       # ensure mount point
35 rodolico 167
       unless ( -d $driveInfo->{mountPath} || make_path($driveInfo->{mountPath}) ) {
168
         logMsg("Failed to create $driveInfo->{mountPath}: $!");
24 rodolico 169
         return '';
170
       }
37 rodolico 171
       # mount device
172
       runCmd( "mount -t $driveInfo->{filesystem} $driveInfo->{label} $driveInfo->{mountPath}" );
173
       if ( $lastRunError ) {
35 rodolico 174
         logMsg("Failed to mount $driveInfo->{label} on $driveInfo->{mountPath}: $!");
24 rodolico 175
         return '';
176
       }
35 rodolico 177
       return $driveInfo->{mountPath};
24 rodolico 178
    } else {
179
       return '';
180
    }
181
}
182
 
183
## Load a YAML configuration file into a hashref.
184
## If the file does not exist, and a default hashref is provided,
185
## create the file by dumping the default to YAML, then return the default.
186
sub loadConfig {
187
    my ($filename, $default) = @_;
188
 
189
    # If no filename was provided, return default or empty hashref
190
    die "No filename provided to loadConfig\n" unless defined $filename;
191
 
192
    # If file doesn't exist but a default hashref was provided, try to
193
    # create the file by dumping the default to YAML, then return the default.
194
    unless (-e $filename) {
195
      logMsg("Config file $filename does not exist. Creating it with default values.");
196
      if ($default && ref $default eq 'HASH') {
197
         my $wrote = 0;
198
         eval {
199
               require YAML::XS;
200
               YAML::XS->import();
201
               YAML::XS::DumpFile($filename, $default);
202
               $wrote = 1;
203
               1;
204
         } or do {
205
               eval {
206
                  require YAML::Tiny;
207
                  YAML::Tiny->import();
208
                  my $yt = YAML::Tiny->new($default);
209
                  $yt->write($filename);
210
                  $wrote = 1;
211
                  1;
212
               } or do {
213
                  logMsg("No YAML writer available (YAML::XS or YAML::Tiny). Could not create $filename");
214
               };
215
         };
216
 
217
         die "Failed to write default config to $filename:$!\n" unless $wrote;
218
        }
219
 
220
        # No default provided; nothing to create
221
        return {};
222
    }
223
 
224
    my $yaml;
225
 
226
    # Try YAML::XS first, fall back to YAML::Tiny
227
    eval {
228
        require YAML::XS;
229
        YAML::XS->import();
230
        $yaml = YAML::XS::LoadFile($filename);
231
        logMsg("using YAML::XS to load $filename");
232
        1;
233
    } or do {
234
        eval {
235
            require YAML::Tiny;
236
            YAML::Tiny->import();
237
            $yaml = YAML::Tiny->read($filename);
238
            $yaml = $yaml->[0] if $yaml;  # YAML::Tiny returns an arrayref of documents
239
            logMsg("using YAML::Tiny to load $filename");
240
            1;
241
        } or do {
242
            logMsg("No YAML parser installed (YAML::XS or YAML::Tiny). Skipping config load from $filename");
243
            return ($default && ref $default eq 'HASH') ? $default : {};
244
        };
245
    };
246
    # Ensure we have a hashref
247
    die "Config file $filename did not produce a HASH.\n" unless (defined $yaml && ref $yaml eq 'HASH');
248
 
249
    return $yaml;
250
}
251
 
252
 
38 rodolico 253
# Mount a GELI-encrypted ZFS pool.
254
# $geliConfig - hashref containing configuration for geli
255
# Returns the pool name on success, empty string on error.
24 rodolico 256
sub mountGeli {
257
   my $geliConfig = shift;
38 rodolico 258
 
39 rodolico 259
   logMsg( "geli config detected, attempting to mount geli disks" );
38 rodolico 260
   # Can't continue at all if no pool name
261
   unless ( $geliConfig->{'poolname'} ) {
262
      logMsg "Could not find pool name in configuration file\n";
24 rodolico 263
      return '';
264
   }
265
   # find the keyfile disk and mount it
39 rodolico 266
   $geliConfig->{secureKey}->{path} = mountDriveByLabel( $geliConfig->{secureKey} );
267
   unless ( $geliConfig->{secureKey}->{path} ) {
38 rodolico 268
      logMsg "Could not find or mount keyfile disk with label: " . $geliConfig->{secureKey}->{label};
24 rodolico 269
      return '';
270
   }
271
   # create the combined geli keyfile in target location
38 rodolico 272
   unless ( makeGeliKey( $geliConfig ) ) {
24 rodolico 273
         logMsg "Could not create geli keyfile\n";
274
         return '';
275
      }
276
   # decrypt and mount the geli disks and zfs pool
277
   my $poolname = decryptAndMountGeli( $geliConfig );
278
   return $poolname;
279
 
280
}
281
 
30 rodolico 282
# find all disks which are candidates for use with geli/zfs
283
# Grabs all disks on the system, then removes those with partitions
284
# and those already used in zpools.
285
sub findGeliDisks {
286
   logMsg("Finding available disks for GELI/ZFS use");
287
   # get all disks in system
288
   my %allDisks = map{ chomp $_ ; $_ => 1 } runCmd( "geom disk list | grep 'Geom name:' | rev | cut -d' ' -f1 | rev" );
289
   # get the disks with partitions
290
   my @temp = runCmd( "gpart show -p | grep '^=>'");  # -p prints just the disks without partitions
291
   # remove them from the list
292
   foreach my $disk ( @temp ) {
293
      $allDisks{$1} = 0 if ( $disk =~ m/^=>[\t\s0-9]+([a-z][a-z0-9]+)/ ) ;
294
   }
295
 
296
   # get disk which are currently used for zpools
297
   @temp = runCmd( "zpool status -LP | grep '/dev/'" );
298
   foreach my $disk ( @temp ) {
299
      $allDisks{$1} = 0 if  $disk =~ m|/dev/([a-z]+\d+)|;
300
   }
301
 
302
   # return only the disks which are free (value 1)
303
   return grep{ $allDisks{$_} == 1 } keys %allDisks;
304
}
305
 
24 rodolico 306
## Decrypt each GELI disk from $geliConfig->{'diskList'} using the keyfile,
307
## then import and mount the ZFS pool specified in $geliConfig->{'poolname'}.
308
##
309
## Returns the pool name on success, empty on error.
310
sub decryptAndMountGeli {
38 rodolico 311
   my ($geliConfig) = shift;
30 rodolico 312
 
313
   # if no list of disks provided, try to find them
39 rodolico 314
   $geliConfig->{'diskList'} //= [ findGeliDisks() ];
30 rodolico 315
 
24 rodolico 316
   my $diskList = $geliConfig->{'diskList'};
317
   my $poolname = $geliConfig->{'poolname'};
318
   my $keyfile = $geliConfig->{'target'};
319
   unless ( -e $keyfile ) {
320
      logMsg "GELI keyfile $keyfile does not exist\n";
321
      return '';
322
   }
323
 
324
   my @decrypted_devices;
325
 
326
   # Decrypt each disk in the list
30 rodolico 327
   foreach my $disk (@{$geliConfig->{'diskList'}}) {
39 rodolico 328
      $disk = '/dev/' . $disk unless $disk =~ m|/dev|;
24 rodolico 329
      unless ( -e $disk ) {
330
         logMsg "Disk $disk does not exist\n";
331
         return '';
332
      }
333
 
334
      # Derive the decrypted device name (.eli suffix on FreeBSD)
335
      my $decrypted = $disk . '.eli';
336
 
337
      # Decrypt using geli attach with the keyfile
338
      logMsg("Decrypting $disk with keyfile $keyfile");
41 rodolico 339
      runCmd("geli attach -p -k $geliConfig->{target} $disk");
340
      if ( $lastRunError) {
40 rodolico 341
         logMsg "Failed to decrypt $disk (exit $lastRunError)\n";
30 rodolico 342
         next; # ignore failed disks and continue to see if we can import the pool
24 rodolico 343
      }
344
 
345
      unless ( -e $decrypted ) {
346
         logMsg "Decrypted device $decrypted does not exist after geli attach\n";
347
         return '';
348
      }
349
      push @decrypted_devices, $decrypted;
350
   }
351
 
352
   # Import the ZFS pool
353
   logMsg("Importing ZFS pool $poolname");
354
   my @import_cmd = ('zpool', 'import');
355
   # If decrypted devices exist, add their directories to -d list
30 rodolico 356
   #foreach my $dev (@decrypted_devices) {
357
   #   my $dir = $dev;
358
   #   $dir =~ s!/[^/]+$!!;  # Remove filename to get directory
359
   #   push @import_cmd, '-d', $dir;
360
   #}
361
 
24 rodolico 362
   push @import_cmd, $poolname;
363
 
40 rodolico 364
   runCmd("zpool import $poolname" );
365
   unless ( $lastRunError == 0 ) {
366
      logMsg("Failed to import zfs pool $poolname (exit $lastRunError)\n");
24 rodolico 367
      return '';
368
   }
369
 
370
   # Mount the ZFS pool (zfs mount -a mounts all filesystems in the pool)
371
   logMsg("Mounting ZFS pool $poolname");
40 rodolico 372
   runCmd('zfs mount -a');
373
   unless ( $lastRunError == 0 ) {
374
      logMsg("Failed to mount zfs pool $poolname (exit $lastRunError)\n");
24 rodolico 375
      return '';
376
   }
377
 
378
   logMsg("Successfully decrypted and mounted pool $poolname");
379
   return $poolname;
380
}
381
 
382
## Create a GELI key by XOR'ing a remote binary keyfile and a local key (hex string).
383
##
384
## Arguments:
385
##   $remote_keyfile - path to binary keyfile (32 bytes)
386
##   $localKeyHexOrPath - hex string (64 hex chars) or path to file containing hex
387
##   $target - path to write the resulting 32-byte binary key
388
##
389
## Returns true on success, dies on fatal error.
390
sub makeGeliKey {
38 rodolico 391
   my ( $geliConfig ) = @_;
24 rodolico 392
 
38 rodolico 393
   $geliConfig->{secureKey}->{keyfile} //= '';
394
   $geliConfig->{localKey} //= '';
395
   $geliConfig->{target} //= '';
24 rodolico 396
 
38 rodolico 397
   if ( $geliConfig->{target} && -f $geliConfig->{target} ) {
398
      logMsg "GELI target keyfile $geliConfig->{target} already exists. Not overwriting.\n";
399
      return 1;
400
   }
24 rodolico 401
 
38 rodolico 402
   my $remote_keyfile = "$geliConfig->{secureKey}->{path}/$geliConfig->{secureKey}->{keyfile}";
403
   my $localKeyHexOrPath = $geliConfig->{localKey};
404
   my $target = $geliConfig->{target};
40 rodolico 405
 
38 rodolico 406
   if ( $geliConfig->{secureKey}->{keyfile} && $geliConfig->{localKey} ) {
407
      # we have what we need to proceed
408
 
409
      if ( -f $remote_keyfile ) {
410
         logMsg "Creating GELI keyfile at $geliConfig->{target} using remote keyfile " . $geliConfig->{secureKey}->{keyfile} . " and local key\n";
411
      } else {
412
         die "Remote keyfile " . $geliConfig->{secureKey}->{keyfile} . " does not exist\n";
413
      }
414
   }
415
 
24 rodolico 416
   # Read remote binary key
417
   open my $rh, '<:raw', $remote_keyfile or die "Unable to open $remote_keyfile: $!\n";
418
   my $rbuf;
419
   my $read = read($rh, $rbuf, 32);
420
   close $rh;
421
   die "Failed to read 32 bytes from $remote_keyfile (got $read)\n" unless defined $read && $read == 32;
422
 
423
   # Get local hex string (either direct string or file contents)
424
   my $hex;
425
   if (-e $localKeyHexOrPath) {
426
      open my $lh, '<', $localKeyHexOrPath or die "Unable to open local key file $localKeyHexOrPath: $!\n";
427
      local $/ = undef;
428
      $hex = <$lh>;
429
      close $lh;
430
   } else {
431
      $hex = $localKeyHexOrPath;
432
   }
433
   # clean hex (remove whitespace/newlines and optional 0x)
434
   $hex =~ s/0x//g;
435
   $hex =~ s/[^0-9a-fA-F]//g;
436
 
437
   die "Local key must be 64 hex characters (256-bit)\n" unless length($hex) == 64;
438
 
439
   my $lbuf = pack('H*', $hex);
440
   die "Local key decoded to unexpected length " . length($lbuf) . "\n" unless length($lbuf) == 32;
441
 
442
   # XOR the two buffers
443
   my $out = '';
444
   for my $i (0 .. 31) {
445
      $out .= chr( ord(substr($rbuf, $i, 1)) ^ ord(substr($lbuf, $i, 1)) );
446
   }
447
 
448
   # Ensure target directory exists
449
   my ($vol, $dirs, $file) = ($target =~ m{^(/?)(.*/)?([^/]+)$});
450
   if ($dirs) {
451
      my $dir = $dirs;
452
      $dir =~ s{/$}{};
453
      unless (-d $dir) {
454
         require File::Path;
455
         File::Path::make_path($dir) or die "Failed to create directory $dir: $!\n";
456
      }
457
   }
458
 
459
   # Write out binary key and protect permissions
460
   open my $oh, '>:raw', $target or die "Unable to open $target for writing: $!\n";
461
   print $oh $out or die "Failed to write to $target: $!\n";
462
   close $oh;
463
   chmod 0600, $target;
464
 
465
   return 1;
466
}
467
 
25 rodolico 468
sub makeReplicateCommands {
469
   my ($sourceSnapsRef, $statusRef, $newStatusRef) = @_;
470
   $sourceSnapsRef ||= [];
471
   $statusRef     ||= [];
472
   $newStatusRef  ||= [];
473
 
474
   # parse snapshots: each line is expected to have snapshot fullname as first token: pool/fs@snap ...
475
   my %snaps_by_fs;
476
   foreach my $line (@$sourceSnapsRef) {
477
      next unless defined $line && $line =~ /\S/;
478
      my ($tok) = split /\s+/, $line;
479
      next unless $tok && $tok =~ /@/;
480
      my ($fs, $snap) = split /@/, $tok, 2;
481
      push @{ $snaps_by_fs{$fs} }, $snap;
482
   }
483
 
484
   # nothing to do
485
   return [] unless keys %snaps_by_fs;
486
 
487
   # figure root filesystem: first snapshot line's fs is the requested root
488
   my ($first_line) = grep { defined $_ && $_ =~ /\S/ } @$sourceSnapsRef;
489
   my ($root_fs) = $first_line ? (split(/\s+/, $first_line))[0] =~ /@/ ? (split(/@/, (split(/\s+/, $first_line))[0]))[0] : undef : undef;
490
   $root_fs ||= (sort keys %snaps_by_fs)[0];
491
 
492
   # helper: find last status entry for a filesystem (status lines contain full snapshot names pool/fs@snap)
493
   my %last_status_for;
494
   for my $s (@$statusRef) {
495
      next unless $s && $s =~ /@/;
496
      my ($fs, $snap) = split /@/, $s, 2;
497
      $last_status_for{$fs} = $snap;    # later entries override earlier ones -> last occurrence kept
498
   }
499
 
500
   # build per-filesystem "from" and "to"
501
   my %from_for;
502
   my %to_for;
503
   foreach my $fs (keys %snaps_by_fs) {
504
      my $arr = $snaps_by_fs{$fs};
505
      next unless @$arr;
506
      $to_for{$fs} = $arr->[-1];
507
      $from_for{$fs} = $last_status_for{$fs};    # may be undef -> full send required
508
   }
509
 
510
   # decide if we can do a single recursive send:
511
   # condition: all 'to' snapshot names are identical
512
   my %to_names = map { $_ => 1 } values %to_for;
513
   my $single_to_name = (keys %to_names == 1) ? (keys %to_names)[0] : undef;
514
 
31 rodolico 515
   my %commands;
25 rodolico 516
 
517
   if ($single_to_name) {
518
      # check whether any from is missing
519
      my @from_values = map { $from_for{$_} } sort keys %from_for;
520
      my $any_from_missing = grep { !defined $_ } @from_values;
521
      my %from_names = map { $_ => 1 } grep { defined $_ } @from_values;
522
      my $single_from_name = (keys %from_names == 1) ? (keys %from_names)[0] : undef;
523
 
524
      if ($any_from_missing) {
525
         # full recursive send from root
37 rodolico 526
         $commands{$root_fs} = sprintf('zfs send -R %s@%s', $root_fs, $single_to_name);
25 rodolico 527
      }
528
      elsif ($single_from_name) {
31 rodolico 529
         # incremental recursive send, but don't do it if they are the same
530
         $commands{$root_fs} = sprintf('zfs send -R -I %s@%s %s@%s',
531
                           $root_fs, $single_from_name, $root_fs, $single_to_name)
532
                           unless $single_from_name eq $single_to_name;
25 rodolico 533
      }
534
      else {
535
         # from snapshots differ across children -> fall back to per-filesystem sends
536
         foreach my $fs (sort keys %to_for) {
537
            my $to  = $to_for{$fs};
538
            my $from = $from_for{$fs};
539
            if ($from) {
31 rodolico 540
               # if from and to are different, add it
541
               $commands{$fs} = sprintf('zfs send -I %s@%s %s@%s', $fs, $from, $fs, $to)
542
                  unless $from eq $to;
25 rodolico 543
            } else {
31 rodolico 544
               $commands{$fs} = sprintf('zfs send %s@%s', $fs, $to);
25 rodolico 545
            }
546
         }
547
      }
548
 
549
      # update new status: record newest snap for every filesystem
550
      foreach my $fs (keys %to_for) {
551
         push @$newStatusRef, sprintf('%s@%s', $fs, $to_for{$fs});
552
      }
553
   } else {
554
      # not all children share same newest snap -> per-filesystem sends
555
      foreach my $fs (sort keys %to_for) {
556
         my $to  = $to_for{$fs};
557
         my $from = $from_for{$fs};
558
         if ($from) {
31 rodolico 559
            $commands{$fs} = sprintf('zfs send -I %s@%s %s@%s', $fs, $from, $fs, $to);
25 rodolico 560
         } else {
31 rodolico 561
            $commands{$fs} = sprintf('zfs send %s@%s', $fs, $to);
25 rodolico 562
         }
563
         push @$newStatusRef, sprintf('%s@%s', $fs, $to);
564
      }
565
   }
566
 
567
   # return arrayref of commands (caller can iterate or join with pipes)
31 rodolico 568
   return \%commands;
25 rodolico 569
}
570
 
35 rodolico 571
# Send report via email and/or copy to target drive.
572
# $reportConfig is a hashref with optional keys:
573
#   email - email address to send report to
574
#   targetDrive - hashref with keys:
575
#       label - GPT or msdosfs label of the target drive
576
#       mount_point - optional mount point to use (if not provided, /mnt/label is used)
577
# $subject is the email subject
578
# $logFile is the path to the log file to send/copy
579
sub sendReport {
580
   my ( $reportConfig, $subject, $logFile ) = @_;
581
   return unless defined $reportConfig;
37 rodolico 582
   logMsg( "Beginning sendReport" );
583
   # if they have set an e-mail address, try to e-mail the report
35 rodolico 584
   if ( defined $reportConfig->{email} && $reportConfig->{email} ne '' ) {
37 rodolico 585
      logMsg( "Sending report via e-mail to $reportConfig->{email}");
35 rodolico 586
      sendEmailReport( $reportConfig->{email}, $subject, $logFile );
587
   }
37 rodolico 588
   # if targetDrive defined and there is a valid label for it, try to mount it and write the report there
589
   if ( defined $reportConfig->{targetDrive} && defined $reportConfig->{targetDrive}->{label} && $reportConfig->{targetDrive}->{label} ) {
590
      logMsg( "Saving report to disk with label $reportConfig->{targetDrive}->{label}" );
35 rodolico 591
      my $mountPoint = mountDriveByLabel( $reportConfig->{targetDrive}->{label}, $reportConfig->{targetDrive}->{mount_point}, 300 );
37 rodolico 592
      if ( defined $mountPoint && $mountPoint ) {
35 rodolico 593
         copyReportToDrive( $logFile, $mountPoint );
594
         `umount $mountPoint`;
595
         rmdir $mountPoint;
596
      } else {
597
         logMsg( "Warning: could not mount report target drive with label '$reportConfig->{targetDrive}->{label}'" );
598
      }
599
   }
600
}
25 rodolico 601
 
35 rodolico 602
# Copy the report log file to the specified mount point.
603
# $logFile is the path to the log file to copy.
604
# $mountPoint is the mount point of the target drive.
605
# Does nothing if log file or mount point are invalid.
606
sub copyReportToDrive {
607
   my ( $logFile, $mountPoint ) = @_;
608
   return unless defined $logFile && -e $logFile;
609
   return unless defined $mountPoint && -d $mountPoint;
610
 
611
   my $targetFile = "$mountPoint/" . ( split( /\//, $logFile ) )[-1];
612
   logMsg( "Copying report log file $logFile to drive at $mountPoint" );
613
   unless ( copy( $logFile, $targetFile ) ) {
614
      logMsg( "Could not copy report log file to target drive: $!" );
615
   }
616
}
617
 
618
# Send an email report with the contents of the log file.
619
# $to is the recipient email address.
620
# $subject is the email subject.
621
# $logFile is the path to the log file to send.
622
# Does nothing if any parameter is invalid.
623
sub sendEmailReport {
624
   my ( $to, $subject, $logFile ) = @_;
625
   return unless defined $to && $to ne '';
37 rodolico 626
   $subject //= 'Sneakernet Replication Report from ' . `hostname`;
627
   $logFile //= '';
35 rodolico 628
 
629
   logMsg( "Sending email report to $to with subject '$subject'" );
630
   open my $mailfh, '|-', '/usr/sbin/sendmail -t' or do {
631
      logMsg( "Could not open sendmail: $!" );
632
      return;
633
   };
634
   print $mailfh "To: $to\n";
635
   print $mailfh "Subject: $subject\n";
636
   print $mailfh "MIME-Version: 1.0\n";
637
   print $mailfh "Content-Type: text/plain; charset=\"utf-8\"\n";
638
   print $mailfh "\n"; # end of headers
37 rodolico 639
 
640
   print $mailfh "Following is the report for replication\n\n";
35 rodolico 641
 
37 rodolico 642
   if ( -e $logFile && open my $logfh, '<', $logFile ) {
643
      while ( my $line = <$logfh> ) {
644
         print $mailfh $line;
645
      }
646
      close $logfh;
647
   } else {
648
      logMsg( "Could not open log file [$logFile] for reading: $!" );
35 rodolico 649
   };
37 rodolico 650
 
35 rodolico 651
   close $mailfh;
652
}  
653
 
24 rodolico 654
1;