Subversion Repositories zfs_utils

Rev

Rev 41 | 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
 
42 rodolico 55
our @EXPORT_OK = qw(loadConfig shredFile mountDriveByLabel unmountDriveByLabel mountGeli logMsg runCmd makeReplicateCommands sendReport fatalError getDirectoryList cleanDirectory $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
 
42 rodolico 183
# finds and unmounts a drive defined by $driveInfo.
184
# on success, removes the mount point if empty.
185
sub unmountDriveByLabel {
186
   my ( $driveInfo ) = @_;
187
   unless ($driveInfo->{label}) {
188
      logMsg("unmountDriveByLabel: No drive label provided");
189
      return '';
190
   }
191
   unless ( $driveInfo->{label} =~ /^[a-zA-Z0-9_\-]+$/ ) {
192
      logMsg("unmountDriveByLabel: Invalid label '$driveInfo->{label}'");
193
      return '';
194
   }
195
 
196
   logMsg("unmountDriveByLabel: Looking for drive with label '$driveInfo->{label}'");
197
   # default to /mnt/label if not provided
198
   $driveInfo->{mountPath} //= "/mnt/$driveInfo->{label}"; # this is where we'll mount it if we find it
199
 
200
   runCmd( "mount | grep '$driveInfo->{mountPath}'" );
201
   if ( $lastRunError ) {
202
     logMsg("Drive with label '$driveInfo->{label}' is not mounted");
203
     return '';
204
   }
205
 
206
   # unmount device
207
   runCmd( "umount $driveInfo->{mountPath}" );
208
   if ( $lastRunError ) {
209
     logMsg("Failed to unmount $driveInfo->{mountPath}: $!");
210
     return '';
211
   }
212
 
213
   # and remove the directory if empty (find command will return empty string or one filename)
214
   rmdir $driveInfo->{mountPath} unless runCmd( "find $driveInfo->{mountPath} -mindepth 1 -print -quit");
215
   return $driveInfo->{mountPath};
216
}
217
 
24 rodolico 218
## Load a YAML configuration file into a hashref.
219
## If the file does not exist, and a default hashref is provided,
220
## create the file by dumping the default to YAML, then return the default.
221
sub loadConfig {
222
    my ($filename, $default) = @_;
223
 
224
    # If no filename was provided, return default or empty hashref
225
    die "No filename provided to loadConfig\n" unless defined $filename;
226
 
227
    # If file doesn't exist but a default hashref was provided, try to
228
    # create the file by dumping the default to YAML, then return the default.
229
    unless (-e $filename) {
230
      logMsg("Config file $filename does not exist. Creating it with default values.");
231
      if ($default && ref $default eq 'HASH') {
232
         my $wrote = 0;
233
         eval {
234
               require YAML::XS;
235
               YAML::XS->import();
236
               YAML::XS::DumpFile($filename, $default);
237
               $wrote = 1;
238
               1;
239
         } or do {
240
               eval {
241
                  require YAML::Tiny;
242
                  YAML::Tiny->import();
243
                  my $yt = YAML::Tiny->new($default);
244
                  $yt->write($filename);
245
                  $wrote = 1;
246
                  1;
247
               } or do {
248
                  logMsg("No YAML writer available (YAML::XS or YAML::Tiny). Could not create $filename");
249
               };
250
         };
251
         die "Failed to write default config to $filename:$!\n" unless $wrote;
42 rodolico 252
      } # if default
253
      # No default provided; nothing to create
254
      return {};
255
   } # unless -e $filename
24 rodolico 256
 
42 rodolico 257
   my $yaml;
24 rodolico 258
 
42 rodolico 259
   # Try YAML::XS first, fall back to YAML::Tiny
260
   eval {
261
      require YAML::XS;
262
      YAML::XS->import();
263
      $yaml = YAML::XS::LoadFile($filename);
264
      logMsg("using YAML::XS to load $filename");
265
      1;
266
   } or do {
267
      eval {
268
         require YAML::Tiny;
269
         YAML::Tiny->import();
270
         $yaml = YAML::Tiny->read($filename);
271
         $yaml = $yaml->[0] if $yaml;  # YAML::Tiny returns an arrayref of documents
272
         logMsg("using YAML::Tiny to load $filename");
273
         1;
274
      } or do {
275
         logMsg("No YAML parser installed (YAML::XS or YAML::Tiny). Skipping config load from $filename");
276
         return ($default && ref $default eq 'HASH') ? $default : {};
277
      };
278
   };
279
   # Ensure we have a hashref
280
   die "Config file $filename did not produce a HASH.\n" unless (defined $yaml && ref $yaml eq 'HASH');
24 rodolico 281
 
42 rodolico 282
   return $yaml;
24 rodolico 283
}
284
 
285
 
38 rodolico 286
# Mount a GELI-encrypted ZFS pool.
287
# $geliConfig - hashref containing configuration for geli
288
# Returns the pool name on success, empty string on error.
24 rodolico 289
sub mountGeli {
290
   my $geliConfig = shift;
38 rodolico 291
 
39 rodolico 292
   logMsg( "geli config detected, attempting to mount geli disks" );
38 rodolico 293
   # Can't continue at all if no pool name
294
   unless ( $geliConfig->{'poolname'} ) {
295
      logMsg "Could not find pool name in configuration file\n";
24 rodolico 296
      return '';
297
   }
298
   # find the keyfile disk and mount it
39 rodolico 299
   $geliConfig->{secureKey}->{path} = mountDriveByLabel( $geliConfig->{secureKey} );
300
   unless ( $geliConfig->{secureKey}->{path} ) {
38 rodolico 301
      logMsg "Could not find or mount keyfile disk with label: " . $geliConfig->{secureKey}->{label};
24 rodolico 302
      return '';
303
   }
304
   # create the combined geli keyfile in target location
38 rodolico 305
   unless ( makeGeliKey( $geliConfig ) ) {
24 rodolico 306
         logMsg "Could not create geli keyfile\n";
307
         return '';
308
      }
309
   # decrypt and mount the geli disks and zfs pool
310
   my $poolname = decryptAndMountGeli( $geliConfig );
311
   return $poolname;
312
 
313
}
314
 
30 rodolico 315
# find all disks which are candidates for use with geli/zfs
316
# Grabs all disks on the system, then removes those with partitions
317
# and those already used in zpools.
318
sub findGeliDisks {
319
   logMsg("Finding available disks for GELI/ZFS use");
320
   # get all disks in system
321
   my %allDisks = map{ chomp $_ ; $_ => 1 } runCmd( "geom disk list | grep 'Geom name:' | rev | cut -d' ' -f1 | rev" );
322
   # get the disks with partitions
323
   my @temp = runCmd( "gpart show -p | grep '^=>'");  # -p prints just the disks without partitions
324
   # remove them from the list
325
   foreach my $disk ( @temp ) {
326
      $allDisks{$1} = 0 if ( $disk =~ m/^=>[\t\s0-9]+([a-z][a-z0-9]+)/ ) ;
327
   }
328
 
329
   # get disk which are currently used for zpools
330
   @temp = runCmd( "zpool status -LP | grep '/dev/'" );
331
   foreach my $disk ( @temp ) {
332
      $allDisks{$1} = 0 if  $disk =~ m|/dev/([a-z]+\d+)|;
333
   }
334
 
335
   # return only the disks which are free (value 1)
336
   return grep{ $allDisks{$_} == 1 } keys %allDisks;
337
}
338
 
24 rodolico 339
## Decrypt each GELI disk from $geliConfig->{'diskList'} using the keyfile,
340
## then import and mount the ZFS pool specified in $geliConfig->{'poolname'}.
341
##
342
## Returns the pool name on success, empty on error.
343
sub decryptAndMountGeli {
38 rodolico 344
   my ($geliConfig) = shift;
30 rodolico 345
 
346
   # if no list of disks provided, try to find them
39 rodolico 347
   $geliConfig->{'diskList'} //= [ findGeliDisks() ];
30 rodolico 348
 
24 rodolico 349
   my $diskList = $geliConfig->{'diskList'};
350
   my $poolname = $geliConfig->{'poolname'};
351
   my $keyfile = $geliConfig->{'target'};
352
   unless ( -e $keyfile ) {
353
      logMsg "GELI keyfile $keyfile does not exist\n";
354
      return '';
355
   }
356
 
357
   my @decrypted_devices;
358
 
359
   # Decrypt each disk in the list
30 rodolico 360
   foreach my $disk (@{$geliConfig->{'diskList'}}) {
39 rodolico 361
      $disk = '/dev/' . $disk unless $disk =~ m|/dev|;
24 rodolico 362
      unless ( -e $disk ) {
363
         logMsg "Disk $disk does not exist\n";
364
         return '';
365
      }
366
 
367
      # Derive the decrypted device name (.eli suffix on FreeBSD)
368
      my $decrypted = $disk . '.eli';
369
 
370
      # Decrypt using geli attach with the keyfile
371
      logMsg("Decrypting $disk with keyfile $keyfile");
41 rodolico 372
      runCmd("geli attach -p -k $geliConfig->{target} $disk");
373
      if ( $lastRunError) {
40 rodolico 374
         logMsg "Failed to decrypt $disk (exit $lastRunError)\n";
30 rodolico 375
         next; # ignore failed disks and continue to see if we can import the pool
24 rodolico 376
      }
377
 
378
      unless ( -e $decrypted ) {
379
         logMsg "Decrypted device $decrypted does not exist after geli attach\n";
380
         return '';
381
      }
382
      push @decrypted_devices, $decrypted;
383
   }
384
 
385
   # Import the ZFS pool
386
   logMsg("Importing ZFS pool $poolname");
387
   my @import_cmd = ('zpool', 'import');
388
   # If decrypted devices exist, add their directories to -d list
30 rodolico 389
   #foreach my $dev (@decrypted_devices) {
390
   #   my $dir = $dev;
391
   #   $dir =~ s!/[^/]+$!!;  # Remove filename to get directory
392
   #   push @import_cmd, '-d', $dir;
393
   #}
394
 
24 rodolico 395
   push @import_cmd, $poolname;
396
 
40 rodolico 397
   runCmd("zpool import $poolname" );
398
   unless ( $lastRunError == 0 ) {
399
      logMsg("Failed to import zfs pool $poolname (exit $lastRunError)\n");
24 rodolico 400
      return '';
401
   }
402
 
403
   # Mount the ZFS pool (zfs mount -a mounts all filesystems in the pool)
404
   logMsg("Mounting ZFS pool $poolname");
40 rodolico 405
   runCmd('zfs mount -a');
406
   unless ( $lastRunError == 0 ) {
407
      logMsg("Failed to mount zfs pool $poolname (exit $lastRunError)\n");
24 rodolico 408
      return '';
409
   }
410
 
411
   logMsg("Successfully decrypted and mounted pool $poolname");
412
   return $poolname;
413
}
414
 
415
## Create a GELI key by XOR'ing a remote binary keyfile and a local key (hex string).
416
##
417
## Arguments:
418
##   $remote_keyfile - path to binary keyfile (32 bytes)
419
##   $localKeyHexOrPath - hex string (64 hex chars) or path to file containing hex
420
##   $target - path to write the resulting 32-byte binary key
421
##
422
## Returns true on success, dies on fatal error.
423
sub makeGeliKey {
38 rodolico 424
   my ( $geliConfig ) = @_;
24 rodolico 425
 
38 rodolico 426
   $geliConfig->{secureKey}->{keyfile} //= '';
427
   $geliConfig->{localKey} //= '';
428
   $geliConfig->{target} //= '';
24 rodolico 429
 
38 rodolico 430
   if ( $geliConfig->{target} && -f $geliConfig->{target} ) {
431
      logMsg "GELI target keyfile $geliConfig->{target} already exists. Not overwriting.\n";
432
      return 1;
433
   }
24 rodolico 434
 
38 rodolico 435
   my $remote_keyfile = "$geliConfig->{secureKey}->{path}/$geliConfig->{secureKey}->{keyfile}";
436
   my $localKeyHexOrPath = $geliConfig->{localKey};
437
   my $target = $geliConfig->{target};
40 rodolico 438
 
38 rodolico 439
   if ( $geliConfig->{secureKey}->{keyfile} && $geliConfig->{localKey} ) {
440
      # we have what we need to proceed
441
 
442
      if ( -f $remote_keyfile ) {
443
         logMsg "Creating GELI keyfile at $geliConfig->{target} using remote keyfile " . $geliConfig->{secureKey}->{keyfile} . " and local key\n";
444
      } else {
445
         die "Remote keyfile " . $geliConfig->{secureKey}->{keyfile} . " does not exist\n";
446
      }
447
   }
448
 
24 rodolico 449
   # Read remote binary key
450
   open my $rh, '<:raw', $remote_keyfile or die "Unable to open $remote_keyfile: $!\n";
451
   my $rbuf;
452
   my $read = read($rh, $rbuf, 32);
453
   close $rh;
454
   die "Failed to read 32 bytes from $remote_keyfile (got $read)\n" unless defined $read && $read == 32;
455
 
456
   # Get local hex string (either direct string or file contents)
457
   my $hex;
458
   if (-e $localKeyHexOrPath) {
459
      open my $lh, '<', $localKeyHexOrPath or die "Unable to open local key file $localKeyHexOrPath: $!\n";
460
      local $/ = undef;
461
      $hex = <$lh>;
462
      close $lh;
463
   } else {
464
      $hex = $localKeyHexOrPath;
465
   }
466
   # clean hex (remove whitespace/newlines and optional 0x)
467
   $hex =~ s/0x//g;
468
   $hex =~ s/[^0-9a-fA-F]//g;
469
 
470
   die "Local key must be 64 hex characters (256-bit)\n" unless length($hex) == 64;
471
 
472
   my $lbuf = pack('H*', $hex);
473
   die "Local key decoded to unexpected length " . length($lbuf) . "\n" unless length($lbuf) == 32;
474
 
475
   # XOR the two buffers
476
   my $out = '';
477
   for my $i (0 .. 31) {
478
      $out .= chr( ord(substr($rbuf, $i, 1)) ^ ord(substr($lbuf, $i, 1)) );
479
   }
480
 
481
   # Ensure target directory exists
482
   my ($vol, $dirs, $file) = ($target =~ m{^(/?)(.*/)?([^/]+)$});
483
   if ($dirs) {
484
      my $dir = $dirs;
485
      $dir =~ s{/$}{};
486
      unless (-d $dir) {
487
         require File::Path;
488
         File::Path::make_path($dir) or die "Failed to create directory $dir: $!\n";
489
      }
490
   }
491
 
492
   # Write out binary key and protect permissions
493
   open my $oh, '>:raw', $target or die "Unable to open $target for writing: $!\n";
494
   print $oh $out or die "Failed to write to $target: $!\n";
495
   close $oh;
496
   chmod 0600, $target;
497
 
498
   return 1;
499
}
500
 
42 rodolico 501
# make a bunch of replicate commands and return them to the caller as a list
502
# $rootDataSet - string, the root of the snapshots exclusive of the dataset itself
503
# $sourceSnapsRef
504
# $statusRef
505
# $newStatusRef
506
# returns
25 rodolico 507
sub makeReplicateCommands {
42 rodolico 508
   my ( $sourceSnapsRef, $statusRef, $newStatusRef) = @_;
25 rodolico 509
   $sourceSnapsRef ||= [];
510
   $statusRef     ||= [];
511
   $newStatusRef  ||= [];
42 rodolico 512
   $rootDataSet .= '/';
25 rodolico 513
 
514
   # parse snapshots: each line is expected to have snapshot fullname as first token: pool/fs@snap ...
515
   my %snaps_by_fs;
516
   foreach my $line (@$sourceSnapsRef) {
517
      next unless defined $line && $line =~ /\S/;
518
      my ($tok) = split /\s+/, $line;
519
      next unless $tok && $tok =~ /@/;
520
      my ($fs, $snap) = split /@/, $tok, 2;
521
      push @{ $snaps_by_fs{$fs} }, $snap;
522
   }
523
 
524
   # nothing to do
525
   return [] unless keys %snaps_by_fs;
526
 
527
   # figure root filesystem: first snapshot line's fs is the requested root
528
   my ($first_line) = grep { defined $_ && $_ =~ /\S/ } @$sourceSnapsRef;
529
   my ($root_fs) = $first_line ? (split(/\s+/, $first_line))[0] =~ /@/ ? (split(/@/, (split(/\s+/, $first_line))[0]))[0] : undef : undef;
530
   $root_fs ||= (sort keys %snaps_by_fs)[0];
531
 
532
   # helper: find last status entry for a filesystem (status lines contain full snapshot names pool/fs@snap)
533
   my %last_status_for;
534
   for my $s (@$statusRef) {
535
      next unless $s && $s =~ /@/;
536
      my ($fs, $snap) = split /@/, $s, 2;
537
      $last_status_for{$fs} = $snap;    # later entries override earlier ones -> last occurrence kept
538
   }
539
 
540
   # build per-filesystem "from" and "to"
541
   my %from_for;
542
   my %to_for;
543
   foreach my $fs (keys %snaps_by_fs) {
544
      my $arr = $snaps_by_fs{$fs};
545
      next unless @$arr;
546
      $to_for{$fs} = $arr->[-1];
547
      $from_for{$fs} = $last_status_for{$fs};    # may be undef -> full send required
548
   }
549
 
550
   # decide if we can do a single recursive send:
551
   # condition: all 'to' snapshot names are identical
552
   my %to_names = map { $_ => 1 } values %to_for;
553
   my $single_to_name = (keys %to_names == 1) ? (keys %to_names)[0] : undef;
554
 
31 rodolico 555
   my %commands;
25 rodolico 556
 
557
   if ($single_to_name) {
558
      # check whether any from is missing
559
      my @from_values = map { $from_for{$_} } sort keys %from_for;
560
      my $any_from_missing = grep { !defined $_ } @from_values;
561
      my %from_names = map { $_ => 1 } grep { defined $_ } @from_values;
562
      my $single_from_name = (keys %from_names == 1) ? (keys %from_names)[0] : undef;
563
 
564
      if ($any_from_missing) {
565
         # full recursive send from root
37 rodolico 566
         $commands{$root_fs} = sprintf('zfs send -R %s@%s', $root_fs, $single_to_name);
25 rodolico 567
      }
568
      elsif ($single_from_name) {
31 rodolico 569
         # incremental recursive send, but don't do it if they are the same
570
         $commands{$root_fs} = sprintf('zfs send -R -I %s@%s %s@%s',
571
                           $root_fs, $single_from_name, $root_fs, $single_to_name)
572
                           unless $single_from_name eq $single_to_name;
25 rodolico 573
      }
574
      else {
575
         # from snapshots differ across children -> fall back to per-filesystem sends
576
         foreach my $fs (sort keys %to_for) {
577
            my $to  = $to_for{$fs};
578
            my $from = $from_for{$fs};
579
            if ($from) {
31 rodolico 580
               # if from and to are different, add it
581
               $commands{$fs} = sprintf('zfs send -I %s@%s %s@%s', $fs, $from, $fs, $to)
582
                  unless $from eq $to;
25 rodolico 583
            } else {
31 rodolico 584
               $commands{$fs} = sprintf('zfs send %s@%s', $fs, $to);
25 rodolico 585
            }
586
         }
587
      }
588
 
589
      # update new status: record newest snap for every filesystem
590
      foreach my $fs (keys %to_for) {
591
         push @$newStatusRef, sprintf('%s@%s', $fs, $to_for{$fs});
592
      }
593
   } else {
594
      # not all children share same newest snap -> per-filesystem sends
595
      foreach my $fs (sort keys %to_for) {
596
         my $to  = $to_for{$fs};
597
         my $from = $from_for{$fs};
598
         if ($from) {
31 rodolico 599
            $commands{$fs} = sprintf('zfs send -I %s@%s %s@%s', $fs, $from, $fs, $to);
25 rodolico 600
         } else {
31 rodolico 601
            $commands{$fs} = sprintf('zfs send %s@%s', $fs, $to);
25 rodolico 602
         }
603
         push @$newStatusRef, sprintf('%s@%s', $fs, $to);
604
      }
605
   }
606
 
607
   # return arrayref of commands (caller can iterate or join with pipes)
31 rodolico 608
   return \%commands;
25 rodolico 609
}
610
 
35 rodolico 611
# Send report via email and/or copy to target drive.
612
# $reportConfig is a hashref with optional keys:
613
#   email - email address to send report to
614
#   targetDrive - hashref with keys:
615
#       label - GPT or msdosfs label of the target drive
616
#       mount_point - optional mount point to use (if not provided, /mnt/label is used)
617
# $subject is the email subject
42 rodolico 618
# $message is the message to include in the email body
619
# $logFile is the path to the log file to include in the report
35 rodolico 620
sub sendReport {
42 rodolico 621
   my ( $reportConfig, $message, $logFile ) = @_;
35 rodolico 622
   return unless defined $reportConfig;
42 rodolico 623
   $logFile //= $reportConfig->{logFile};
37 rodolico 624
   logMsg( "Beginning sendReport" );
625
   # if targetDrive defined and there is a valid label for it, try to mount it and write the report there
626
   if ( defined $reportConfig->{targetDrive} && defined $reportConfig->{targetDrive}->{label} && $reportConfig->{targetDrive}->{label} ) {
627
      logMsg( "Saving report to disk with label $reportConfig->{targetDrive}->{label}" );
35 rodolico 628
      my $mountPoint = mountDriveByLabel( $reportConfig->{targetDrive}->{label}, $reportConfig->{targetDrive}->{mount_point}, 300 );
37 rodolico 629
      if ( defined $mountPoint && $mountPoint ) {
35 rodolico 630
         copyReportToDrive( $logFile, $mountPoint );
631
         `umount $mountPoint`;
632
         rmdir $mountPoint;
633
      } else {
634
         logMsg( "Warning: could not mount report target drive with label '$reportConfig->{targetDrive}->{label}'" );
635
      }
636
   }
42 rodolico 637
   # if they have set an e-mail address, try to e-mail the report
638
   if ( defined $reportConfig->{email} && $reportConfig->{email} ne '' ) {
639
      logMsg( "Sending report via e-mail to $reportConfig->{email}" );
640
      $reportConfig->{subject} //= 'Replication Report from ' . `hostname`;
641
      sendEmailReport( $reportConfig->{email}, $reportConfig->{subject}, $message, $logFile );
642
   }
35 rodolico 643
}
25 rodolico 644
 
35 rodolico 645
# Copy the report log file to the specified mount point.
646
# $logFile is the path to the log file to copy.
647
# $mountPoint is the mount point of the target drive.
648
# Does nothing if log file or mount point are invalid.
649
sub copyReportToDrive {
650
   my ( $logFile, $mountPoint ) = @_;
651
   return unless defined $logFile && -e $logFile;
652
   return unless defined $mountPoint && -d $mountPoint;
653
 
654
   my $targetFile = "$mountPoint/" . ( split( /\//, $logFile ) )[-1];
655
   logMsg( "Copying report log file $logFile to drive at $mountPoint" );
656
   unless ( copy( $logFile, $targetFile ) ) {
657
      logMsg( "Could not copy report log file to target drive: $!" );
658
   }
659
}
660
 
661
# Send an email report with the contents of the log file.
662
# $to is the recipient email address.
663
# $subject is the email subject.
664
# $logFile is the path to the log file to send.
665
# Does nothing if any parameter is invalid.
666
sub sendEmailReport {
42 rodolico 667
   my ( $to, $subject, $message, $logFile ) = @_;
35 rodolico 668
   return unless defined $to && $to ne '';
37 rodolico 669
   $subject //= 'Sneakernet Replication Report from ' . `hostname`;
42 rodolico 670
   $message //= '';
37 rodolico 671
   $logFile //= '';
35 rodolico 672
 
673
   logMsg( "Sending email report to $to with subject '$subject'" );
674
   open my $mailfh, '|-', '/usr/sbin/sendmail -t' or do {
675
      logMsg( "Could not open sendmail: $!" );
676
      return;
677
   };
678
   print $mailfh "To: $to\n";
679
   print $mailfh "Subject: $subject\n";
680
   print $mailfh "MIME-Version: 1.0\n";
681
   print $mailfh "Content-Type: text/plain; charset=\"utf-8\"\n";
682
   print $mailfh "\n"; # end of headers
37 rodolico 683
 
42 rodolico 684
   print $mailfh "$message\n";
685
   print $mailfh "\nLog contents:\n\n";
37 rodolico 686
   if ( -e $logFile && open my $logfh, '<', $logFile ) {
687
      while ( my $line = <$logfh> ) {
688
         print $mailfh $line;
689
      }
690
      close $logfh;
691
   } else {
692
      logMsg( "Could not open log file [$logFile] for reading: $!" );
35 rodolico 693
   };
37 rodolico 694
 
35 rodolico 695
   close $mailfh;
696
}  
697
 
42 rodolico 698
# Get all file names (not directories) from a directory
699
# $dirname is directory to scan
700
# returns arrayref
701
sub getDirectoryList {
702
   my $dirname = shift;
703
   opendir( my $dh, $dirname ) || return 0;
704
   # get all file names, but leave directories alone
705
   my @files = map{ $dirname . "/$_" } grep { -f "$dirname/$_" } readdir($dh);
706
   closedir $dh;
707
   return \@files;
708
}
709
 
710
# clean all files from a directory, but not any subdirectories
711
sub cleanDirectory {
712
   my $dirname = shift;
713
   logMsg( "Cleaning up $dirname of all files" );
714
   my $files = getDirectoryList( $dirname );
715
   # clean up a directory
716
   foreach my $file (@$files) {
717
      unlink $file or warn "Could not unlink $file: #!\n";
718
   }
719
   return 1;
720
}
721
 
722
# handle fatal error by logging message and dying
723
# message - message to log, and also sent via email if applicable
724
# config - configuration hashref (optional)
725
# cleanupRoutine - code reference to cleanup routine (optional)
726
# if cleanupRoutine is provided, it will be called before dying passing it the config hashref
727
sub fatalError {
728
   my ( $message, $config, $cleanupRoutine ) = @_;
729
   logMsg( "FATAL ERROR: $message" );
730
   if ( defined $cleanupRoutine && ref $cleanupRoutine eq 'CODE' ) {
731
      logMsg( "Running cleanup routine before fatal error" );
732
      eval {
733
         $cleanupRoutine->( $config, $message );
734
         1;
735
      } or do {
736
         logMsg( "Cleanup routine failed: $@" );
737
      };
738
   }
739
   die;
740
}
741
 
742
 
24 rodolico 743
1;