Subversion Repositories zfs_utils

Rev

Rev 37 | Rev 39 | 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
 
259
   # Can't continue at all if no pool name
260
   unless ( $geliConfig->{'poolname'} ) {
261
      logMsg "Could not find pool name in configuration file\n";
24 rodolico 262
      return '';
263
   }
38 rodolico 264
 
24 rodolico 265
   # find the keyfile disk and mount it
38 rodolico 266
   $geliConfig->{secureKey}->{path} = mountDriveByLabel( $geliConfig->{secureKey}->{label} );
267
   unless ( $geliConfig->{secureKey}->{path} ne '' ) {
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
38 rodolico 277
   die;
24 rodolico 278
   my $poolname = decryptAndMountGeli( $geliConfig );
279
   return $poolname;
280
 
281
}
282
 
30 rodolico 283
# find all disks which are candidates for use with geli/zfs
284
# Grabs all disks on the system, then removes those with partitions
285
# and those already used in zpools.
286
sub findGeliDisks {
287
   logMsg("Finding available disks for GELI/ZFS use");
288
   # get all disks in system
289
   my %allDisks = map{ chomp $_ ; $_ => 1 } runCmd( "geom disk list | grep 'Geom name:' | rev | cut -d' ' -f1 | rev" );
290
   # get the disks with partitions
291
   my @temp = runCmd( "gpart show -p | grep '^=>'");  # -p prints just the disks without partitions
292
   # remove them from the list
293
   foreach my $disk ( @temp ) {
294
      $allDisks{$1} = 0 if ( $disk =~ m/^=>[\t\s0-9]+([a-z][a-z0-9]+)/ ) ;
295
   }
296
 
297
   # get disk which are currently used for zpools
298
   @temp = runCmd( "zpool status -LP | grep '/dev/'" );
299
   foreach my $disk ( @temp ) {
300
      $allDisks{$1} = 0 if  $disk =~ m|/dev/([a-z]+\d+)|;
301
   }
302
 
303
   # return only the disks which are free (value 1)
304
   return grep{ $allDisks{$_} == 1 } keys %allDisks;
305
}
306
 
24 rodolico 307
## Decrypt each GELI disk from $geliConfig->{'diskList'} using the keyfile,
308
## then import and mount the ZFS pool specified in $geliConfig->{'poolname'}.
309
##
310
## Returns the pool name on success, empty on error.
311
sub decryptAndMountGeli {
38 rodolico 312
   my ($geliConfig) = shift;
30 rodolico 313
 
314
   # if no list of disks provided, try to find them
315
   $geliConfig->{'diskList'} //= findGeliDisks();
316
 
24 rodolico 317
   my $diskList = $geliConfig->{'diskList'};
318
   my $poolname = $geliConfig->{'poolname'};
319
   my $keyfile = $geliConfig->{'target'};
320
   unless ( -e $keyfile ) {
321
      logMsg "GELI keyfile $keyfile does not exist\n";
322
      return '';
323
   }
324
 
325
   my @decrypted_devices;
326
 
327
   # Decrypt each disk in the list
30 rodolico 328
   foreach my $disk (@{$geliConfig->{'diskList'}}) {
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");
30 rodolico 339
      if ( my $result = system('geli', 'attach', '-k', $geliConfig->{'target'}, $disk) == 0 ) {
24 rodolico 340
         logMsg "Failed to decrypt $disk (exit $result)\n";
30 rodolico 341
         next; # ignore failed disks and continue to see if we can import the pool
24 rodolico 342
      }
343
 
344
      unless ( -e $decrypted ) {
345
         logMsg "Decrypted device $decrypted does not exist after geli attach\n";
346
         return '';
347
      }
348
      push @decrypted_devices, $decrypted;
349
   }
350
 
351
   # Import the ZFS pool
352
   logMsg("Importing ZFS pool $poolname");
353
   my @import_cmd = ('zpool', 'import');
354
   # If decrypted devices exist, add their directories to -d list
30 rodolico 355
   #foreach my $dev (@decrypted_devices) {
356
   #   my $dir = $dev;
357
   #   $dir =~ s!/[^/]+$!!;  # Remove filename to get directory
358
   #   push @import_cmd, '-d', $dir;
359
   #}
360
 
24 rodolico 361
   push @import_cmd, $poolname;
362
 
363
   my $result = system(@import_cmd);
364
   unless ( $result == 0 ) {
365
      logMsg("Failed to import zfs pool $poolname (exit $result)\n");
366
      return '';
367
   }
368
 
369
   # Mount the ZFS pool (zfs mount -a mounts all filesystems in the pool)
370
   logMsg("Mounting ZFS pool $poolname");
371
   $result = system('zfs', 'mount', '-a');
372
   unless ( $result == 0 ) {
373
      logMsg("Failed to mount zfs pool $poolname (exit $result)\n");
374
      return '';
375
   }
376
 
377
   logMsg("Successfully decrypted and mounted pool $poolname");
378
   return $poolname;
379
}
380
 
381
## Create a GELI key by XOR'ing a remote binary keyfile and a local key (hex string).
382
##
383
## Arguments:
384
##   $remote_keyfile - path to binary keyfile (32 bytes)
385
##   $localKeyHexOrPath - hex string (64 hex chars) or path to file containing hex
386
##   $target - path to write the resulting 32-byte binary key
387
##
388
## Returns true on success, dies on fatal error.
389
sub makeGeliKey {
38 rodolico 390
   my ( $geliConfig ) = @_;
24 rodolico 391
 
38 rodolico 392
   $geliConfig->{secureKey}->{keyfile} //= '';
393
   $geliConfig->{localKey} //= '';
394
   $geliConfig->{target} //= '';
24 rodolico 395
 
38 rodolico 396
   if ( $geliConfig->{target} && -f $geliConfig->{target} ) {
397
      logMsg "GELI target keyfile $geliConfig->{target} already exists. Not overwriting.\n";
398
      return 1;
399
   }
24 rodolico 400
 
38 rodolico 401
   my $remote_keyfile = "$geliConfig->{secureKey}->{path}/$geliConfig->{secureKey}->{keyfile}";
402
   my $localKeyHexOrPath = $geliConfig->{localKey};
403
   my $target = $geliConfig->{target};
404
 
405
   if ( $geliConfig->{secureKey}->{keyfile} && $geliConfig->{localKey} ) {
406
      # we have what we need to proceed
407
 
408
      if ( -f $remote_keyfile ) {
409
         logMsg "Creating GELI keyfile at $geliConfig->{target} using remote keyfile " . $geliConfig->{secureKey}->{keyfile} . " and local key\n";
410
      } else {
411
         die "Remote keyfile " . $geliConfig->{secureKey}->{keyfile} . " does not exist\n";
412
      }
413
   }
414
 
24 rodolico 415
   # Read remote binary key
416
   open my $rh, '<:raw', $remote_keyfile or die "Unable to open $remote_keyfile: $!\n";
417
   my $rbuf;
418
   my $read = read($rh, $rbuf, 32);
419
   close $rh;
420
   die "Failed to read 32 bytes from $remote_keyfile (got $read)\n" unless defined $read && $read == 32;
421
 
422
   # Get local hex string (either direct string or file contents)
423
   my $hex;
424
   if (-e $localKeyHexOrPath) {
425
      open my $lh, '<', $localKeyHexOrPath or die "Unable to open local key file $localKeyHexOrPath: $!\n";
426
      local $/ = undef;
427
      $hex = <$lh>;
428
      close $lh;
429
   } else {
430
      $hex = $localKeyHexOrPath;
431
   }
432
   # clean hex (remove whitespace/newlines and optional 0x)
433
   $hex =~ s/0x//g;
434
   $hex =~ s/[^0-9a-fA-F]//g;
435
 
436
   die "Local key must be 64 hex characters (256-bit)\n" unless length($hex) == 64;
437
 
438
   my $lbuf = pack('H*', $hex);
439
   die "Local key decoded to unexpected length " . length($lbuf) . "\n" unless length($lbuf) == 32;
440
 
441
   # XOR the two buffers
442
   my $out = '';
443
   for my $i (0 .. 31) {
444
      $out .= chr( ord(substr($rbuf, $i, 1)) ^ ord(substr($lbuf, $i, 1)) );
445
   }
446
 
447
   # Ensure target directory exists
448
   my ($vol, $dirs, $file) = ($target =~ m{^(/?)(.*/)?([^/]+)$});
449
   if ($dirs) {
450
      my $dir = $dirs;
451
      $dir =~ s{/$}{};
452
      unless (-d $dir) {
453
         require File::Path;
454
         File::Path::make_path($dir) or die "Failed to create directory $dir: $!\n";
455
      }
456
   }
457
 
458
   # Write out binary key and protect permissions
459
   open my $oh, '>:raw', $target or die "Unable to open $target for writing: $!\n";
460
   print $oh $out or die "Failed to write to $target: $!\n";
461
   close $oh;
462
   chmod 0600, $target;
463
 
464
   return 1;
465
}
466
 
25 rodolico 467
sub makeReplicateCommands {
468
   my ($sourceSnapsRef, $statusRef, $newStatusRef) = @_;
469
   $sourceSnapsRef ||= [];
470
   $statusRef     ||= [];
471
   $newStatusRef  ||= [];
472
 
473
   # parse snapshots: each line is expected to have snapshot fullname as first token: pool/fs@snap ...
474
   my %snaps_by_fs;
475
   foreach my $line (@$sourceSnapsRef) {
476
      next unless defined $line && $line =~ /\S/;
477
      my ($tok) = split /\s+/, $line;
478
      next unless $tok && $tok =~ /@/;
479
      my ($fs, $snap) = split /@/, $tok, 2;
480
      push @{ $snaps_by_fs{$fs} }, $snap;
481
   }
482
 
483
   # nothing to do
484
   return [] unless keys %snaps_by_fs;
485
 
486
   # figure root filesystem: first snapshot line's fs is the requested root
487
   my ($first_line) = grep { defined $_ && $_ =~ /\S/ } @$sourceSnapsRef;
488
   my ($root_fs) = $first_line ? (split(/\s+/, $first_line))[0] =~ /@/ ? (split(/@/, (split(/\s+/, $first_line))[0]))[0] : undef : undef;
489
   $root_fs ||= (sort keys %snaps_by_fs)[0];
490
 
491
   # helper: find last status entry for a filesystem (status lines contain full snapshot names pool/fs@snap)
492
   my %last_status_for;
493
   for my $s (@$statusRef) {
494
      next unless $s && $s =~ /@/;
495
      my ($fs, $snap) = split /@/, $s, 2;
496
      $last_status_for{$fs} = $snap;    # later entries override earlier ones -> last occurrence kept
497
   }
498
 
499
   # build per-filesystem "from" and "to"
500
   my %from_for;
501
   my %to_for;
502
   foreach my $fs (keys %snaps_by_fs) {
503
      my $arr = $snaps_by_fs{$fs};
504
      next unless @$arr;
505
      $to_for{$fs} = $arr->[-1];
506
      $from_for{$fs} = $last_status_for{$fs};    # may be undef -> full send required
507
   }
508
 
509
   # decide if we can do a single recursive send:
510
   # condition: all 'to' snapshot names are identical
511
   my %to_names = map { $_ => 1 } values %to_for;
512
   my $single_to_name = (keys %to_names == 1) ? (keys %to_names)[0] : undef;
513
 
31 rodolico 514
   my %commands;
25 rodolico 515
 
516
   if ($single_to_name) {
517
      # check whether any from is missing
518
      my @from_values = map { $from_for{$_} } sort keys %from_for;
519
      my $any_from_missing = grep { !defined $_ } @from_values;
520
      my %from_names = map { $_ => 1 } grep { defined $_ } @from_values;
521
      my $single_from_name = (keys %from_names == 1) ? (keys %from_names)[0] : undef;
522
 
523
      if ($any_from_missing) {
524
         # full recursive send from root
37 rodolico 525
         $commands{$root_fs} = sprintf('zfs send -R %s@%s', $root_fs, $single_to_name);
25 rodolico 526
      }
527
      elsif ($single_from_name) {
31 rodolico 528
         # incremental recursive send, but don't do it if they are the same
529
         $commands{$root_fs} = sprintf('zfs send -R -I %s@%s %s@%s',
530
                           $root_fs, $single_from_name, $root_fs, $single_to_name)
531
                           unless $single_from_name eq $single_to_name;
25 rodolico 532
      }
533
      else {
534
         # from snapshots differ across children -> fall back to per-filesystem sends
535
         foreach my $fs (sort keys %to_for) {
536
            my $to  = $to_for{$fs};
537
            my $from = $from_for{$fs};
538
            if ($from) {
31 rodolico 539
               # if from and to are different, add it
540
               $commands{$fs} = sprintf('zfs send -I %s@%s %s@%s', $fs, $from, $fs, $to)
541
                  unless $from eq $to;
25 rodolico 542
            } else {
31 rodolico 543
               $commands{$fs} = sprintf('zfs send %s@%s', $fs, $to);
25 rodolico 544
            }
545
         }
546
      }
547
 
548
      # update new status: record newest snap for every filesystem
549
      foreach my $fs (keys %to_for) {
550
         push @$newStatusRef, sprintf('%s@%s', $fs, $to_for{$fs});
551
      }
552
   } else {
553
      # not all children share same newest snap -> per-filesystem sends
554
      foreach my $fs (sort keys %to_for) {
555
         my $to  = $to_for{$fs};
556
         my $from = $from_for{$fs};
557
         if ($from) {
31 rodolico 558
            $commands{$fs} = sprintf('zfs send -I %s@%s %s@%s', $fs, $from, $fs, $to);
25 rodolico 559
         } else {
31 rodolico 560
            $commands{$fs} = sprintf('zfs send %s@%s', $fs, $to);
25 rodolico 561
         }
562
         push @$newStatusRef, sprintf('%s@%s', $fs, $to);
563
      }
564
   }
565
 
566
   # return arrayref of commands (caller can iterate or join with pipes)
31 rodolico 567
   return \%commands;
25 rodolico 568
}
569
 
35 rodolico 570
# Send report via email and/or copy to target drive.
571
# $reportConfig is a hashref with optional keys:
572
#   email - email address to send report to
573
#   targetDrive - hashref with keys:
574
#       label - GPT or msdosfs label of the target drive
575
#       mount_point - optional mount point to use (if not provided, /mnt/label is used)
576
# $subject is the email subject
577
# $logFile is the path to the log file to send/copy
578
sub sendReport {
579
   my ( $reportConfig, $subject, $logFile ) = @_;
580
   return unless defined $reportConfig;
37 rodolico 581
   logMsg( "Beginning sendReport" );
582
   # if they have set an e-mail address, try to e-mail the report
35 rodolico 583
   if ( defined $reportConfig->{email} && $reportConfig->{email} ne '' ) {
37 rodolico 584
      logMsg( "Sending report via e-mail to $reportConfig->{email}");
35 rodolico 585
      sendEmailReport( $reportConfig->{email}, $subject, $logFile );
586
   }
37 rodolico 587
   # if targetDrive defined and there is a valid label for it, try to mount it and write the report there
588
   if ( defined $reportConfig->{targetDrive} && defined $reportConfig->{targetDrive}->{label} && $reportConfig->{targetDrive}->{label} ) {
589
      logMsg( "Saving report to disk with label $reportConfig->{targetDrive}->{label}" );
35 rodolico 590
      my $mountPoint = mountDriveByLabel( $reportConfig->{targetDrive}->{label}, $reportConfig->{targetDrive}->{mount_point}, 300 );
37 rodolico 591
      if ( defined $mountPoint && $mountPoint ) {
35 rodolico 592
         copyReportToDrive( $logFile, $mountPoint );
593
         `umount $mountPoint`;
594
         rmdir $mountPoint;
595
      } else {
596
         logMsg( "Warning: could not mount report target drive with label '$reportConfig->{targetDrive}->{label}'" );
597
      }
598
   }
599
}
25 rodolico 600
 
35 rodolico 601
# Copy the report log file to the specified mount point.
602
# $logFile is the path to the log file to copy.
603
# $mountPoint is the mount point of the target drive.
604
# Does nothing if log file or mount point are invalid.
605
sub copyReportToDrive {
606
   my ( $logFile, $mountPoint ) = @_;
607
   return unless defined $logFile && -e $logFile;
608
   return unless defined $mountPoint && -d $mountPoint;
609
 
610
   my $targetFile = "$mountPoint/" . ( split( /\//, $logFile ) )[-1];
611
   logMsg( "Copying report log file $logFile to drive at $mountPoint" );
612
   unless ( copy( $logFile, $targetFile ) ) {
613
      logMsg( "Could not copy report log file to target drive: $!" );
614
   }
615
}
616
 
617
# Send an email report with the contents of the log file.
618
# $to is the recipient email address.
619
# $subject is the email subject.
620
# $logFile is the path to the log file to send.
621
# Does nothing if any parameter is invalid.
622
sub sendEmailReport {
623
   my ( $to, $subject, $logFile ) = @_;
624
   return unless defined $to && $to ne '';
37 rodolico 625
   $subject //= 'Sneakernet Replication Report from ' . `hostname`;
626
   $logFile //= '';
35 rodolico 627
 
628
   logMsg( "Sending email report to $to with subject '$subject'" );
629
   open my $mailfh, '|-', '/usr/sbin/sendmail -t' or do {
630
      logMsg( "Could not open sendmail: $!" );
631
      return;
632
   };
633
   print $mailfh "To: $to\n";
634
   print $mailfh "Subject: $subject\n";
635
   print $mailfh "MIME-Version: 1.0\n";
636
   print $mailfh "Content-Type: text/plain; charset=\"utf-8\"\n";
637
   print $mailfh "\n"; # end of headers
37 rodolico 638
 
639
   print $mailfh "Following is the report for replication\n\n";
35 rodolico 640
 
37 rodolico 641
   if ( -e $logFile && open my $logfh, '<', $logFile ) {
642
      while ( my $line = <$logfh> ) {
643
         print $mailfh $line;
644
      }
645
      close $logfh;
646
   } else {
647
      logMsg( "Could not open log file [$logFile] for reading: $!" );
35 rodolico 648
   };
37 rodolico 649
 
35 rodolico 650
   close $mailfh;
651
}  
652
 
24 rodolico 653
1;