Subversion Repositories zfs_utils

Rev

Rev 44 | 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
46 rodolico 143
   $driveInfo->{fstype} //= '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/
46 rodolico 146
   my $labelPath = $driveInfo->{fstype} 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 ) {
46 rodolico 156
      if ( -e "$labelPath" ) {
24 rodolico 157
         last;
158
      } else {
46 rodolico 159
         print "Waiting for drive labeled $driveInfo->{label}, looking in $labelPath\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
46 rodolico 165
    if ( -e "$labelPath" ) {
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
46 rodolico 172
       runCmd( "mount -t $driveInfo->{fstype} $labelPath $driveInfo->{mountPath}" );
37 rodolico 173
       if ( $lastRunError ) {
46 rodolico 174
         logMsg("Failed to mount $labelPath 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'};
46 rodolico 352
 
353
   # check if the pool already attached (grep returns 0 on found, something else on not)
354
   runCmd( "zpool list -H -o name | grep $poolname" );
355
   return $poolname unless $lastRunError;
356
 
24 rodolico 357
   unless ( -e $keyfile ) {
358
      logMsg "GELI keyfile $keyfile does not exist\n";
359
      return '';
360
   }
361
 
362
   my @decrypted_devices;
363
 
364
   # Decrypt each disk in the list
30 rodolico 365
   foreach my $disk (@{$geliConfig->{'diskList'}}) {
39 rodolico 366
      $disk = '/dev/' . $disk unless $disk =~ m|/dev|;
24 rodolico 367
      unless ( -e $disk ) {
368
         logMsg "Disk $disk does not exist\n";
369
         return '';
370
      }
371
 
372
      # Derive the decrypted device name (.eli suffix on FreeBSD)
373
      my $decrypted = $disk . '.eli';
374
 
375
      # Decrypt using geli attach with the keyfile
376
      logMsg("Decrypting $disk with keyfile $keyfile");
41 rodolico 377
      runCmd("geli attach -p -k $geliConfig->{target} $disk");
378
      if ( $lastRunError) {
40 rodolico 379
         logMsg "Failed to decrypt $disk (exit $lastRunError)\n";
30 rodolico 380
         next; # ignore failed disks and continue to see if we can import the pool
24 rodolico 381
      }
382
 
383
      unless ( -e $decrypted ) {
384
         logMsg "Decrypted device $decrypted does not exist after geli attach\n";
385
         return '';
386
      }
387
      push @decrypted_devices, $decrypted;
388
   }
389
 
390
   # Import the ZFS pool
391
   logMsg("Importing ZFS pool $poolname");
392
   my @import_cmd = ('zpool', 'import');
393
   # If decrypted devices exist, add their directories to -d list
30 rodolico 394
   #foreach my $dev (@decrypted_devices) {
395
   #   my $dir = $dev;
396
   #   $dir =~ s!/[^/]+$!!;  # Remove filename to get directory
397
   #   push @import_cmd, '-d', $dir;
398
   #}
399
 
24 rodolico 400
   push @import_cmd, $poolname;
401
 
40 rodolico 402
   runCmd("zpool import $poolname" );
403
   unless ( $lastRunError == 0 ) {
404
      logMsg("Failed to import zfs pool $poolname (exit $lastRunError)\n");
24 rodolico 405
      return '';
406
   }
407
 
408
   # Mount the ZFS pool (zfs mount -a mounts all filesystems in the pool)
409
   logMsg("Mounting ZFS pool $poolname");
40 rodolico 410
   runCmd('zfs mount -a');
411
   unless ( $lastRunError == 0 ) {
412
      logMsg("Failed to mount zfs pool $poolname (exit $lastRunError)\n");
24 rodolico 413
      return '';
414
   }
415
 
416
   logMsg("Successfully decrypted and mounted pool $poolname");
417
   return $poolname;
418
}
419
 
420
## Create a GELI key by XOR'ing a remote binary keyfile and a local key (hex string).
421
##
422
## Arguments:
423
##   $remote_keyfile - path to binary keyfile (32 bytes)
424
##   $localKeyHexOrPath - hex string (64 hex chars) or path to file containing hex
425
##   $target - path to write the resulting 32-byte binary key
426
##
427
## Returns true on success, dies on fatal error.
428
sub makeGeliKey {
38 rodolico 429
   my ( $geliConfig ) = @_;
24 rodolico 430
 
38 rodolico 431
   $geliConfig->{secureKey}->{keyfile} //= '';
432
   $geliConfig->{localKey} //= '';
433
   $geliConfig->{target} //= '';
24 rodolico 434
 
38 rodolico 435
   if ( $geliConfig->{target} && -f $geliConfig->{target} ) {
436
      logMsg "GELI target keyfile $geliConfig->{target} already exists. Not overwriting.\n";
437
      return 1;
438
   }
24 rodolico 439
 
38 rodolico 440
   my $remote_keyfile = "$geliConfig->{secureKey}->{path}/$geliConfig->{secureKey}->{keyfile}";
441
   my $localKeyHexOrPath = $geliConfig->{localKey};
442
   my $target = $geliConfig->{target};
40 rodolico 443
 
38 rodolico 444
   if ( $geliConfig->{secureKey}->{keyfile} && $geliConfig->{localKey} ) {
445
      # we have what we need to proceed
446
 
447
      if ( -f $remote_keyfile ) {
448
         logMsg "Creating GELI keyfile at $geliConfig->{target} using remote keyfile " . $geliConfig->{secureKey}->{keyfile} . " and local key\n";
449
      } else {
450
         die "Remote keyfile " . $geliConfig->{secureKey}->{keyfile} . " does not exist\n";
451
      }
452
   }
453
 
24 rodolico 454
   # Read remote binary key
455
   open my $rh, '<:raw', $remote_keyfile or die "Unable to open $remote_keyfile: $!\n";
456
   my $rbuf;
457
   my $read = read($rh, $rbuf, 32);
458
   close $rh;
459
   die "Failed to read 32 bytes from $remote_keyfile (got $read)\n" unless defined $read && $read == 32;
460
 
461
   # Get local hex string (either direct string or file contents)
462
   my $hex;
463
   if (-e $localKeyHexOrPath) {
464
      open my $lh, '<', $localKeyHexOrPath or die "Unable to open local key file $localKeyHexOrPath: $!\n";
465
      local $/ = undef;
466
      $hex = <$lh>;
467
      close $lh;
468
   } else {
469
      $hex = $localKeyHexOrPath;
470
   }
471
   # clean hex (remove whitespace/newlines and optional 0x)
472
   $hex =~ s/0x//g;
473
   $hex =~ s/[^0-9a-fA-F]//g;
474
 
475
   die "Local key must be 64 hex characters (256-bit)\n" unless length($hex) == 64;
476
 
477
   my $lbuf = pack('H*', $hex);
478
   die "Local key decoded to unexpected length " . length($lbuf) . "\n" unless length($lbuf) == 32;
479
 
480
   # XOR the two buffers
481
   my $out = '';
482
   for my $i (0 .. 31) {
483
      $out .= chr( ord(substr($rbuf, $i, 1)) ^ ord(substr($lbuf, $i, 1)) );
484
   }
485
 
486
   # Ensure target directory exists
487
   my ($vol, $dirs, $file) = ($target =~ m{^(/?)(.*/)?([^/]+)$});
488
   if ($dirs) {
489
      my $dir = $dirs;
490
      $dir =~ s{/$}{};
491
      unless (-d $dir) {
492
         require File::Path;
493
         File::Path::make_path($dir) or die "Failed to create directory $dir: $!\n";
494
      }
495
   }
496
 
497
   # Write out binary key and protect permissions
498
   open my $oh, '>:raw', $target or die "Unable to open $target for writing: $!\n";
499
   print $oh $out or die "Failed to write to $target: $!\n";
500
   close $oh;
501
   chmod 0600, $target;
502
 
503
   return 1;
504
}
505
 
42 rodolico 506
# make a bunch of replicate commands and return them to the caller as a list
44 rodolico 507
# $sourceSnapsRef - list of snapshots on source machine
508
# $targetSnapsRef - list of snapshots on target machine
509
# $dataset - The name of the dataset we are working on (same on both source and target)
510
# $sourceParent - The parent dataset of $dataset on source
511
# $targetParent - The parent dataset of $dataset on target
512
# $newStatusRef - A place to put the updated $targetSnapsRef
513
# returns hashref of commands to execute, of form
514
#    {$dataset} = "zfs send command"
515
# where $dataset above can be a child of $dataset
25 rodolico 516
sub makeReplicateCommands {
44 rodolico 517
   my ( $sourceSnapsRef, $targetSnapsRef, $dataset, $sourceParent, $targetParent, $newStatusRef ) = @_;
25 rodolico 518
   $sourceSnapsRef ||= [];
44 rodolico 519
   $targetSnapsRef     ||= [];
25 rodolico 520
   $newStatusRef  ||= [];
44 rodolico 521
   $sourceParent //= '';
522
   $sourceParent .= '/' unless $sourceParent eq '' or substr($sourceParent, -1) eq '/';
523
   $targetParent //= '';
524
   $targetParent .= '/' unless $targetParent eq '' or substr($targetParent, -1) eq '/';
25 rodolico 525
 
44 rodolico 526
   my %commands; # this will hold the commands (and the dataset as key) for return
527
 
528
   fatalError( "No dataset defined in makeReplicateCommands, can not continue") unless $dataset;
529
 
530
   # filter only the target and source snapshots which have this dataset in them, then remove
531
   # the parent of each.
532
   my $targetSnaps = [ map{ s/^$targetParent//r } grep{ /$dataset/ } @$targetSnapsRef ];
533
   my $sourceSnaps = [ map{ s/^$sourceParent//r } grep{ /$dataset/ } @$sourceSnapsRef ];
534
 
535
   #print "Dataset => [$dataset]\nSource Parent => [$sourceParent]\nTarget Parent => [$targetParent]\n";
536
   #print "Source Snaps\n" . Dumper( $sourceSnapsRef) . "\nTarget Snaps\n" . Dumper( $targetSnapsRef) . "\n";
537
 
538
   #print Dumper( $targetSnaps ) . "\n" . Dumper( $sourceSnaps ) . "\n"; die;
539
   #return \%commands;
540
 
25 rodolico 541
   # parse snapshots: each line is expected to have snapshot fullname as first token: pool/fs@snap ...
542
   my %snaps_by_fs;
44 rodolico 543
   foreach my $line (@$sourceSnaps) {
25 rodolico 544
      next unless defined $line && $line =~ /\S/;
545
      my ($tok) = split /\s+/, $line;
546
      next unless $tok && $tok =~ /@/;
547
      my ($fs, $snap) = split /@/, $tok, 2;
548
      push @{ $snaps_by_fs{$fs} }, $snap;
549
   }
550
 
551
   # nothing to do
552
   return [] unless keys %snaps_by_fs;
553
 
554
   # figure root filesystem: first snapshot line's fs is the requested root
44 rodolico 555
   my ($first_line) = grep { defined $_ && $_ =~ /\S/ } @$sourceSnaps;
25 rodolico 556
   my ($root_fs) = $first_line ? (split(/\s+/, $first_line))[0] =~ /@/ ? (split(/@/, (split(/\s+/, $first_line))[0]))[0] : undef : undef;
557
   $root_fs ||= (sort keys %snaps_by_fs)[0];
558
 
559
   # helper: find last status entry for a filesystem (status lines contain full snapshot names pool/fs@snap)
560
   my %last_status_for;
44 rodolico 561
   for my $s (@$targetSnaps) {
25 rodolico 562
      next unless $s && $s =~ /@/;
563
      my ($fs, $snap) = split /@/, $s, 2;
564
      $last_status_for{$fs} = $snap;    # later entries override earlier ones -> last occurrence kept
565
   }
566
 
567
   # build per-filesystem "from" and "to"
568
   my %from_for;
569
   my %to_for;
570
   foreach my $fs (keys %snaps_by_fs) {
571
      my $arr = $snaps_by_fs{$fs};
572
      next unless @$arr;
573
      $to_for{$fs} = $arr->[-1];
574
      $from_for{$fs} = $last_status_for{$fs};    # may be undef -> full send required
575
   }
576
 
577
   # decide if we can do a single recursive send:
578
   # condition: all 'to' snapshot names are identical
579
   my %to_names = map { $_ => 1 } values %to_for;
580
   my $single_to_name = (keys %to_names == 1) ? (keys %to_names)[0] : undef;
581
 
582
   if ($single_to_name) {
583
      # check whether any from is missing
584
      my @from_values = map { $from_for{$_} } sort keys %from_for;
585
      my $any_from_missing = grep { !defined $_ } @from_values;
586
      my %from_names = map { $_ => 1 } grep { defined $_ } @from_values;
587
      my $single_from_name = (keys %from_names == 1) ? (keys %from_names)[0] : undef;
588
 
589
      if ($any_from_missing) {
590
         # full recursive send from root
44 rodolico 591
         $commands{$root_fs} = sprintf('zfs send -R %s%s@%s', $sourceParent, $root_fs, $single_to_name);
25 rodolico 592
      }
593
      elsif ($single_from_name) {
31 rodolico 594
         # incremental recursive send, but don't do it if they are the same
44 rodolico 595
         $commands{$root_fs} = sprintf('zfs send -R -I %s%s@%s %s%s@%s',
596
                           $sourceParent, $root_fs, $single_from_name, $sourceParent, $root_fs, $single_to_name)
31 rodolico 597
                           unless $single_from_name eq $single_to_name;
25 rodolico 598
      }
599
      else {
600
         # from snapshots differ across children -> fall back to per-filesystem sends
601
         foreach my $fs (sort keys %to_for) {
602
            my $to  = $to_for{$fs};
603
            my $from = $from_for{$fs};
604
            if ($from) {
31 rodolico 605
               # if from and to are different, add it
44 rodolico 606
               $commands{$fs} = sprintf('zfs send -I %s%s@%s %s%s@%s', $sourceParent, $fs, $from, $sourceParent, $fs, $to)
31 rodolico 607
                  unless $from eq $to;
25 rodolico 608
            } else {
44 rodolico 609
               $commands{$fs} = sprintf('zfs send %s%s@%s', $sourceParent, $fs, $to);
25 rodolico 610
            }
611
         }
612
      }
613
 
614
      # update new status: record newest snap for every filesystem
615
      foreach my $fs (keys %to_for) {
44 rodolico 616
         push @$newStatusRef, sprintf('%s%s@%s', $targetParent, $fs, $to_for{$fs});
25 rodolico 617
      }
618
   } else {
619
      # not all children share same newest snap -> per-filesystem sends
620
      foreach my $fs (sort keys %to_for) {
621
         my $to  = $to_for{$fs};
622
         my $from = $from_for{$fs};
623
         if ($from) {
44 rodolico 624
            $commands{$fs} = sprintf('zfs send -I %s%s@%s %s%s@%s', $sourceParent, $fs, $from, $sourceParent, $fs, $to);
25 rodolico 625
         } else {
44 rodolico 626
            $commands{$fs} = sprintf('zfs send %s%s@%s', $sourceParent, $fs, $to);
25 rodolico 627
         }
44 rodolico 628
         push @$newStatusRef, sprintf('%s%s@%s', $targetParent, $fs, $to);
25 rodolico 629
      }
630
   }
631
 
632
   # return arrayref of commands (caller can iterate or join with pipes)
31 rodolico 633
   return \%commands;
25 rodolico 634
}
635
 
35 rodolico 636
# Send report via email and/or copy to target drive.
637
# $reportConfig is a hashref with optional keys:
638
#   email - email address to send report to
639
#   targetDrive - hashref with keys:
640
#       label - GPT or msdosfs label of the target drive
641
#       mount_point - optional mount point to use (if not provided, /mnt/label is used)
642
# $subject is the email subject
42 rodolico 643
# $message is the message to include in the email body
644
# $logFile is the path to the log file to include in the report
35 rodolico 645
sub sendReport {
42 rodolico 646
   my ( $reportConfig, $message, $logFile ) = @_;
35 rodolico 647
   return unless defined $reportConfig;
42 rodolico 648
   $logFile //= $reportConfig->{logFile};
37 rodolico 649
   logMsg( "Beginning sendReport" );
650
   # if targetDrive defined and there is a valid label for it, try to mount it and write the report there
651
   if ( defined $reportConfig->{targetDrive} && defined $reportConfig->{targetDrive}->{label} && $reportConfig->{targetDrive}->{label} ) {
652
      logMsg( "Saving report to disk with label $reportConfig->{targetDrive}->{label}" );
46 rodolico 653
      if ( $reportConfig->{targetDrive}->{mountPath} = mountDriveByLabel( $reportConfig->{targetDrive} ) ) {
654
         copyReportToDrive( $logFile, $reportConfig->{targetDrive}->{mountPath} );
655
         unmountDriveByLabel( $reportConfig->{targetDrive} );
35 rodolico 656
      } else {
657
         logMsg( "Warning: could not mount report target drive with label '$reportConfig->{targetDrive}->{label}'" );
658
      }
659
   }
42 rodolico 660
   # if they have set an e-mail address, try to e-mail the report
661
   if ( defined $reportConfig->{email} && $reportConfig->{email} ne '' ) {
662
      logMsg( "Sending report via e-mail to $reportConfig->{email}" );
663
      $reportConfig->{subject} //= 'Replication Report from ' . `hostname`;
664
      sendEmailReport( $reportConfig->{email}, $reportConfig->{subject}, $message, $logFile );
665
   }
35 rodolico 666
}
25 rodolico 667
 
35 rodolico 668
# Copy the report log file to the specified mount point.
669
# $logFile is the path to the log file to copy.
670
# $mountPoint is the mount point of the target drive.
671
# Does nothing if log file or mount point are invalid.
672
sub copyReportToDrive {
673
   my ( $logFile, $mountPoint ) = @_;
674
   return unless defined $logFile && -e $logFile;
675
   return unless defined $mountPoint && -d $mountPoint;
676
 
677
   my $targetFile = "$mountPoint/" . ( split( /\//, $logFile ) )[-1];
678
   logMsg( "Copying report log file $logFile to drive at $mountPoint" );
46 rodolico 679
   use File::Copy;
35 rodolico 680
   unless ( copy( $logFile, $targetFile ) ) {
681
      logMsg( "Could not copy report log file to target drive: $!" );
682
   }
683
}
684
 
685
# Send an email report with the contents of the log file.
686
# $to is the recipient email address.
687
# $subject is the email subject.
688
# $logFile is the path to the log file to send.
689
# Does nothing if any parameter is invalid.
690
sub sendEmailReport {
42 rodolico 691
   my ( $to, $subject, $message, $logFile ) = @_;
35 rodolico 692
   return unless defined $to && $to ne '';
37 rodolico 693
   $subject //= 'Sneakernet Replication Report from ' . `hostname`;
42 rodolico 694
   $message //= '';
37 rodolico 695
   $logFile //= '';
35 rodolico 696
 
697
   logMsg( "Sending email report to $to with subject '$subject'" );
698
   open my $mailfh, '|-', '/usr/sbin/sendmail -t' or do {
699
      logMsg( "Could not open sendmail: $!" );
700
      return;
701
   };
702
   print $mailfh "To: $to\n";
703
   print $mailfh "Subject: $subject\n";
704
   print $mailfh "MIME-Version: 1.0\n";
705
   print $mailfh "Content-Type: text/plain; charset=\"utf-8\"\n";
706
   print $mailfh "\n"; # end of headers
37 rodolico 707
 
42 rodolico 708
   print $mailfh "$message\n";
709
   print $mailfh "\nLog contents:\n\n";
37 rodolico 710
   if ( -e $logFile && open my $logfh, '<', $logFile ) {
711
      while ( my $line = <$logfh> ) {
712
         print $mailfh $line;
713
      }
714
      close $logfh;
715
   } else {
716
      logMsg( "Could not open log file [$logFile] for reading: $!" );
35 rodolico 717
   };
37 rodolico 718
 
35 rodolico 719
   close $mailfh;
720
}  
721
 
42 rodolico 722
# Get all file names (not directories) from a directory
723
# $dirname is directory to scan
724
# returns arrayref
725
sub getDirectoryList {
726
   my $dirname = shift;
727
   opendir( my $dh, $dirname ) || return 0;
728
   # get all file names, but leave directories alone
729
   my @files = map{ $dirname . "/$_" } grep { -f "$dirname/$_" } readdir($dh);
730
   closedir $dh;
731
   return \@files;
732
}
733
 
734
# clean all files from a directory, but not any subdirectories
735
sub cleanDirectory {
736
   my $dirname = shift;
737
   logMsg( "Cleaning up $dirname of all files" );
738
   my $files = getDirectoryList( $dirname );
739
   # clean up a directory
740
   foreach my $file (@$files) {
741
      unlink $file or warn "Could not unlink $file: #!\n";
742
   }
743
   return 1;
744
}
745
 
746
# handle fatal error by logging message and dying
747
# message - message to log, and also sent via email if applicable
748
# config - configuration hashref (optional)
749
# cleanupRoutine - code reference to cleanup routine (optional)
750
# if cleanupRoutine is provided, it will be called before dying passing it the config hashref
751
sub fatalError {
752
   my ( $message, $config, $cleanupRoutine ) = @_;
753
   logMsg( "FATAL ERROR: $message" );
754
   if ( defined $cleanupRoutine && ref $cleanupRoutine eq 'CODE' ) {
755
      logMsg( "Running cleanup routine before fatal error" );
756
      eval {
757
         $cleanupRoutine->( $config, $message );
758
         1;
759
      } or do {
760
         logMsg( "Cleanup routine failed: $@" );
761
      };
762
   }
763
   die;
764
}
765
 
766
 
24 rodolico 767
1;