Subversion Repositories zfs_utils

Rev

Rev 46 | 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
48 rodolico 37
# Copyright 2025 Daily Data Inc. <rodo@dailydata.net>
34 rodolico 38
 
39
# currently used for sneakernet scripts, but plans to expand to other ZFS related tasks
40
# functions include:
48 rodolico 41
#   runCmd: execute a command and return its output (captures exit status in $lastRunError;
42
#           supports optional stderr merge via $merge_stderr)
43
#   shredFile: securely delete a file using gshred (note: not effective on ZFS due to COW)
44
#   logMsg: timestamped logging to a file and optionally to console
45
#   loadConfig: load a YAML configuration file into a hashref; will create the file from a
46
#           provided default hashref if the file does not exist (uses YAML::XS or YAML::Tiny)
47
#   mountDriveByLabel: find and mount a drive by its GPT label (supports ufs/msdos; waits
48
#           for device and creates mountpoint)
49
#   unmountDriveByLabel: unmount a drive found by GPT label and remove the mountpoint if empty
50
#   mountGeli: high level orchestrator to decrypt multiple GELI devices and import/mount a ZFS pool
51
#   decryptAndMountGeli: attach GELI devices, optionally build a combined key, import the pool
52
#           and mount ZFS datasets
53
#   makeGeliKey: create a GELI key by XOR'ing a remote binary keyfile and a local 256-bit hex key;
54
#           writes a 32-byte binary key file with mode 0600
55
#   findGeliDisks: discover candidate disks suitable for GELI on the host
56
#   makeReplicateCommands: build zfs send/receive command lists from snapshot lists and prior status
57
#   sendReport: helper to deliver replication reports (email/file) — exported for scripts to implement
58
#   fatalError: helper to log a fatal condition and die (convenience wrapper)
59
#   getDirectoryList: utility to list directory contents with optional filters
60
#   cleanDirectory: safe directory cleaning utility used by snapshot pruning helpers
61
#   exported package variables: $logFileName, $displayLogsOnConsole, $lastRunError
34 rodolico 62
 
48 rodolico 63
# v1.0 RWR 20251215
64
# This is the initial, tested release
34 rodolico 65
 
48 rodolico 66
 
34 rodolico 67
# Exported functions and variables
68
 
42 rodolico 69
our @EXPORT_OK = qw(loadConfig shredFile mountDriveByLabel unmountDriveByLabel mountGeli logMsg runCmd makeReplicateCommands sendReport fatalError getDirectoryList cleanDirectory $logFileName $displayLogsOnConsole $lastRunError);
24 rodolico 70
 
48 rodolico 71
our $VERSION = '1.0';
24 rodolico 72
 
48 rodolico 73
# these are variables which affect the flow of the program and are exported so they can be modified by the caller
24 rodolico 74
our $logFileName = '/tmp/zfs_utils.log'; # this can be overridden by the caller, and turned off with empty string
34 rodolico 75
our $displayLogsOnConsole = 1; # if non-zero, log messages are also printed to console
27 rodolico 76
our $merge_stderr = 0; # if set to 1, stderr is captured in runCmd
37 rodolico 77
our $lastRunError = 0; # tracks the last error code from runCmd
24 rodolico 78
 
25 rodolico 79
# Execute a command and return its output.
80
# If called in scalar context, returns the full output as a single string.
81
# If called in list context, returns the output split into lines.
82
# If $merge_stderr is true (default), stderr is merged into stdout (only for scalar commands).
34 rodolico 83
# returns undef on failure and logs failure message.
25 rodolico 84
sub runCmd {
33 rodolico 85
   my $cmd = join( ' ', @_ );
25 rodolico 86
   $merge_stderr = 1 unless defined $merge_stderr;
87
   my $output = '';
88
 
34 rodolico 89
   logMsg( "Running command [$cmd]" );
90
   $cmd .= ' 2>&1' if $merge_stderr;
91
   $output = `$cmd`;
37 rodolico 92
   $lastRunError = $?;
93
   if ( $lastRunError ) {
94
      if ($? == -1) {
95
         logMsg( "failed to execute: $!");
96
         return '';
97
      } elsif ($? & 127) { # fatal error, exit program
98
         logMsg( sprintf( "child died with signal %d, %s coredump\n", ($? & 127),  ($? & 128) ? 'with' : 'without' ) );
99
         die;
100
      } elsif ($? >> 8) { # it had some return code other than 0
101
         logMsg( sprintf( "child exited with value %d\n", $? >> 8 ) );
102
      }
34 rodolico 103
   }
25 rodolico 104
   $output //= '';
105
 
106
   if (wantarray) {
107
      return $output eq '' ? () : split(/\n/, $output);
108
   } else {
109
      return $output;
110
   }
111
}
112
 
24 rodolico 113
# this calls gshred which will overwrite the file 3 times, then
114
# remove it.
115
# NOTE: this will not work on ZFS, since ZFS is CopyOnWrite (COW)
116
# so assuming file is on something without COW (ramdisk, UFS, etc)
117
sub shredFile {
118
   my $filename = shift;
119
   `/usr/local/bin/gshred -u -f -s 32 $filename` if -e $filename;
120
}
121
 
122
sub logMsg {
123
    my $msg = shift;
124
    my $filename = shift // $logFileName;
125
    my $timeStampFormat = shift // '%Y-%m-%d %H:%M:%S';
126
    my $timestamp = strftime($timeStampFormat, localtime());
127
    if (defined $filename && $filename ne '' ) {
128
       open my $logfh, '>>', $filename or die "Could not open log file $filename: $!\n";
129
       print $logfh "$timestamp\t$msg\n";
130
       close $logfh;
131
    }
132
    print "$timestamp\t$msg\n" if ($displayLogsOnConsole);
133
}
134
 
35 rodolico 135
# find a drive by it's label by scanning /dev/gpt/
136
# driveInfo is a hashref with the following keys:
137
# label - the GPT label of the drive (required)
138
# filesystem - the filesystem type (default: ufs)
139
# mountPath - where to mount the drive (default: /mnt/label)
140
# timeout - how long to wait for the drive (default: 600 seconds)
141
# check_interval - how often to check for the drive (default: 15 seconds)
24 rodolico 142
# If the drive is found, mount it on mountPath and return the mountPath.
143
# If not found, return empty string.
144
sub mountDriveByLabel {
35 rodolico 145
   my ( $driveInfo ) = @_;
146
   unless ($driveInfo->{label}) {
147
      logMsg("mountDriveByLabel: No drive label provided");
24 rodolico 148
      return '';
149
   }
35 rodolico 150
   unless ( $driveInfo->{label} =~ /^[a-zA-Z0-9_\-]+$/ ) {
151
      logMsg("mountDriveByLabel: Invalid label '$driveInfo->{label}'");
24 rodolico 152
      return '';
153
   }
154
 
35 rodolico 155
   logMsg("mountDriveByLabel: Looking for drive with label '$driveInfo->{label}'");
24 rodolico 156
   # default to /mnt/label if not provided
35 rodolico 157
   $driveInfo->{mountPath} //= "/mnt/$driveInfo->{label}"; # this is where we'll mount it if we find it
46 rodolico 158
   $driveInfo->{fstype} //= 'ufs'; # default to mounting ufs
34 rodolico 159
   # The location for the label depends on filesystem. Only providing access to ufs and msdos here for safety.
160
   # gpt labeled drives for ufs are in /dev/gpt/, for msdosfs in /dev/msdosfs/
46 rodolico 161
   my $labelPath = $driveInfo->{fstype} eq 'msdos' ? "/dev/msdosfs/$driveInfo->{label}" : "/dev/gpt/$driveInfo->{label}"; 
31 rodolico 162
   # drive already mounted, just return the path
37 rodolico 163
   my $output = runCmd( "mount | grep '$driveInfo->{mountPath}'" );
164
   return $driveInfo->{mountPath} if ( $lastRunError == 0 ); # grep found it for us
24 rodolico 165
   # default to 10 minutes (600 seconds) if not provided
35 rodolico 166
   $driveInfo->{timeout} //= 600;
24 rodolico 167
   # default to checking every minute if not provided
35 rodolico 168
   $driveInfo->{check_interval} //= 15;
24 rodolico 169
   # wait up to $timeout seconds for device to appear, checking every 10 seconds
35 rodolico 170
   while ( $driveInfo->{timeout} > 0 ) {
46 rodolico 171
      if ( -e "$labelPath" ) {
24 rodolico 172
         last;
173
      } else {
46 rodolico 174
         print "Waiting for drive labeled $driveInfo->{label}, looking in $labelPath\n";
35 rodolico 175
         sleep $driveInfo->{check_interval};
176
         $driveInfo->{timeout} -= $driveInfo->{check_interval};
24 rodolico 177
      }
178
    }
179
    # if we found it, mount and return mount path
46 rodolico 180
    if ( -e "$labelPath" ) {
24 rodolico 181
       # ensure mount point
35 rodolico 182
       unless ( -d $driveInfo->{mountPath} || make_path($driveInfo->{mountPath}) ) {
183
         logMsg("Failed to create $driveInfo->{mountPath}: $!");
24 rodolico 184
         return '';
185
       }
37 rodolico 186
       # mount device
46 rodolico 187
       runCmd( "mount -t $driveInfo->{fstype} $labelPath $driveInfo->{mountPath}" );
37 rodolico 188
       if ( $lastRunError ) {
46 rodolico 189
         logMsg("Failed to mount $labelPath on $driveInfo->{mountPath}: $!");
24 rodolico 190
         return '';
191
       }
35 rodolico 192
       return $driveInfo->{mountPath};
24 rodolico 193
    } else {
194
       return '';
195
    }
196
}
197
 
42 rodolico 198
# finds and unmounts a drive defined by $driveInfo.
199
# on success, removes the mount point if empty.
200
sub unmountDriveByLabel {
201
   my ( $driveInfo ) = @_;
202
   unless ($driveInfo->{label}) {
203
      logMsg("unmountDriveByLabel: No drive label provided");
204
      return '';
205
   }
206
   unless ( $driveInfo->{label} =~ /^[a-zA-Z0-9_\-]+$/ ) {
207
      logMsg("unmountDriveByLabel: Invalid label '$driveInfo->{label}'");
208
      return '';
209
   }
210
 
211
   logMsg("unmountDriveByLabel: Looking for drive with label '$driveInfo->{label}'");
212
   # default to /mnt/label if not provided
213
   $driveInfo->{mountPath} //= "/mnt/$driveInfo->{label}"; # this is where we'll mount it if we find it
214
 
215
   runCmd( "mount | grep '$driveInfo->{mountPath}'" );
216
   if ( $lastRunError ) {
217
     logMsg("Drive with label '$driveInfo->{label}' is not mounted");
218
     return '';
219
   }
220
 
221
   # unmount device
222
   runCmd( "umount $driveInfo->{mountPath}" );
223
   if ( $lastRunError ) {
224
     logMsg("Failed to unmount $driveInfo->{mountPath}: $!");
225
     return '';
226
   }
227
 
228
   # and remove the directory if empty (find command will return empty string or one filename)
229
   rmdir $driveInfo->{mountPath} unless runCmd( "find $driveInfo->{mountPath} -mindepth 1 -print -quit");
230
   return $driveInfo->{mountPath};
231
}
232
 
24 rodolico 233
## Load a YAML configuration file into a hashref.
234
## If the file does not exist, and a default hashref is provided,
235
## create the file by dumping the default to YAML, then return the default.
236
sub loadConfig {
237
    my ($filename, $default) = @_;
238
 
239
    # If no filename was provided, return default or empty hashref
240
    die "No filename provided to loadConfig\n" unless defined $filename;
241
 
242
    # If file doesn't exist but a default hashref was provided, try to
243
    # create the file by dumping the default to YAML, then return the default.
244
    unless (-e $filename) {
245
      logMsg("Config file $filename does not exist. Creating it with default values.");
246
      if ($default && ref $default eq 'HASH') {
247
         my $wrote = 0;
248
         eval {
249
               require YAML::XS;
250
               YAML::XS->import();
251
               YAML::XS::DumpFile($filename, $default);
252
               $wrote = 1;
253
               1;
254
         } or do {
255
               eval {
256
                  require YAML::Tiny;
257
                  YAML::Tiny->import();
258
                  my $yt = YAML::Tiny->new($default);
259
                  $yt->write($filename);
260
                  $wrote = 1;
261
                  1;
262
               } or do {
263
                  logMsg("No YAML writer available (YAML::XS or YAML::Tiny). Could not create $filename");
264
               };
265
         };
266
         die "Failed to write default config to $filename:$!\n" unless $wrote;
42 rodolico 267
      } # if default
268
      # No default provided; nothing to create
269
      return {};
270
   } # unless -e $filename
24 rodolico 271
 
42 rodolico 272
   my $yaml;
24 rodolico 273
 
42 rodolico 274
   # Try YAML::XS first, fall back to YAML::Tiny
275
   eval {
276
      require YAML::XS;
277
      YAML::XS->import();
278
      $yaml = YAML::XS::LoadFile($filename);
279
      logMsg("using YAML::XS to load $filename");
280
      1;
281
   } or do {
282
      eval {
283
         require YAML::Tiny;
284
         YAML::Tiny->import();
285
         $yaml = YAML::Tiny->read($filename);
286
         $yaml = $yaml->[0] if $yaml;  # YAML::Tiny returns an arrayref of documents
287
         logMsg("using YAML::Tiny to load $filename");
288
         1;
289
      } or do {
290
         logMsg("No YAML parser installed (YAML::XS or YAML::Tiny). Skipping config load from $filename");
291
         return ($default && ref $default eq 'HASH') ? $default : {};
292
      };
293
   };
294
   # Ensure we have a hashref
295
   die "Config file $filename did not produce a HASH.\n" unless (defined $yaml && ref $yaml eq 'HASH');
24 rodolico 296
 
42 rodolico 297
   return $yaml;
24 rodolico 298
}
299
 
300
 
48 rodolico 301
## Mount a GELI-encrypted ZFS pool (high-level orchestration).
302
##
303
## Arguments:
304
##   $geliConfig - HASHREF containing GELI/ZFS mounting configuration. Expected keys include:
305
##       poolname        - name of the zpool to import
306
##       secureKey       - HASHREF with { label, keyfile, path } describing the keyfile disk
307
##       target          - path where the combined keyfile will be written
308
##       diskList        - OPTIONAL arrayref of disk device names (eg: ['ada0','ada1'])
309
##
310
## Behavior:
311
##   - Mounts the keyfile disk (using mountDriveByLabel), builds the combined key (makeGeliKey),
312
##     then calls decryptAndMountGeli to attach geli devices and import/mount the zpool.
313
##
314
## Returns:
315
##   Pool name (string) on success, empty string on error.
24 rodolico 316
sub mountGeli {
317
   my $geliConfig = shift;
38 rodolico 318
 
39 rodolico 319
   logMsg( "geli config detected, attempting to mount geli disks" );
38 rodolico 320
   # Can't continue at all if no pool name
321
   unless ( $geliConfig->{'poolname'} ) {
322
      logMsg "Could not find pool name in configuration file\n";
24 rodolico 323
      return '';
324
   }
325
   # find the keyfile disk and mount it
39 rodolico 326
   $geliConfig->{secureKey}->{path} = mountDriveByLabel( $geliConfig->{secureKey} );
327
   unless ( $geliConfig->{secureKey}->{path} ) {
38 rodolico 328
      logMsg "Could not find or mount keyfile disk with label: " . $geliConfig->{secureKey}->{label};
24 rodolico 329
      return '';
330
   }
331
   # create the combined geli keyfile in target location
38 rodolico 332
   unless ( makeGeliKey( $geliConfig ) ) {
24 rodolico 333
         logMsg "Could not create geli keyfile\n";
334
         return '';
335
      }
336
   # decrypt and mount the geli disks and zfs pool
337
   my $poolname = decryptAndMountGeli( $geliConfig );
338
   return $poolname;
339
 
340
}
341
 
48 rodolico 342
## Discover disks suitable for GELI/ZFS use on the host.
343
##
344
## Returns an array of device names (eg: qw( ada0 ada1 )) that appear free for use.
345
## The routine collects all disks, excludes disks with existing partitions and those
346
## referenced by active zpools.
30 rodolico 347
sub findGeliDisks {
348
   logMsg("Finding available disks for GELI/ZFS use");
349
   # get all disks in system
350
   my %allDisks = map{ chomp $_ ; $_ => 1 } runCmd( "geom disk list | grep 'Geom name:' | rev | cut -d' ' -f1 | rev" );
351
   # get the disks with partitions
352
   my @temp = runCmd( "gpart show -p | grep '^=>'");  # -p prints just the disks without partitions
353
   # remove them from the list
354
   foreach my $disk ( @temp ) {
355
      $allDisks{$1} = 0 if ( $disk =~ m/^=>[\t\s0-9]+([a-z][a-z0-9]+)/ ) ;
356
   }
357
 
358
   # get disk which are currently used for zpools
359
   @temp = runCmd( "zpool status -LP | grep '/dev/'" );
360
   foreach my $disk ( @temp ) {
361
      $allDisks{$1} = 0 if  $disk =~ m|/dev/([a-z]+\d+)|;
362
   }
363
 
364
   # return only the disks which are free (value 1)
365
   return grep{ $allDisks{$_} == 1 } keys %allDisks;
366
}
367
 
48 rodolico 368
## Decrypt GELI-encrypted disks and import/mount the ZFS pool.
24 rodolico 369
##
48 rodolico 370
## Arguments:
371
##   $geliConfig - HASHREF expected to contain:
372
##       poolname - zpool name to import
373
##       target   - path to the combined GELI keyfile created by makeGeliKey
374
##       diskList - OPTIONAL arrayref of disk device names (if omitted, findGeliDisks() is used)
375
##
376
## Behavior:
377
##   - Ensures the pool is not already imported
378
##   - Attaches (geli attach) each supplied disk using the keyfile
379
##   - Attempts to import the specified pool and runs `zfs mount -a` to mount datasets
380
##
381
## Returns:
382
##   Pool name (string) on success; empty string on failure.
24 rodolico 383
sub decryptAndMountGeli {
38 rodolico 384
   my ($geliConfig) = shift;
30 rodolico 385
 
386
   # if no list of disks provided, try to find them
39 rodolico 387
   $geliConfig->{'diskList'} //= [ findGeliDisks() ];
30 rodolico 388
 
24 rodolico 389
   my $diskList = $geliConfig->{'diskList'};
390
   my $poolname = $geliConfig->{'poolname'};
391
   my $keyfile = $geliConfig->{'target'};
46 rodolico 392
 
393
   # check if the pool already attached (grep returns 0 on found, something else on not)
394
   runCmd( "zpool list -H -o name | grep $poolname" );
395
   return $poolname unless $lastRunError;
396
 
24 rodolico 397
   unless ( -e $keyfile ) {
398
      logMsg "GELI keyfile $keyfile does not exist\n";
399
      return '';
400
   }
401
 
402
   my @decrypted_devices;
403
 
404
   # Decrypt each disk in the list
30 rodolico 405
   foreach my $disk (@{$geliConfig->{'diskList'}}) {
39 rodolico 406
      $disk = '/dev/' . $disk unless $disk =~ m|/dev|;
24 rodolico 407
      unless ( -e $disk ) {
408
         logMsg "Disk $disk does not exist\n";
409
         return '';
410
      }
411
 
412
      # Derive the decrypted device name (.eli suffix on FreeBSD)
413
      my $decrypted = $disk . '.eli';
414
 
415
      # Decrypt using geli attach with the keyfile
416
      logMsg("Decrypting $disk with keyfile $keyfile");
41 rodolico 417
      runCmd("geli attach -p -k $geliConfig->{target} $disk");
418
      if ( $lastRunError) {
40 rodolico 419
         logMsg "Failed to decrypt $disk (exit $lastRunError)\n";
30 rodolico 420
         next; # ignore failed disks and continue to see if we can import the pool
24 rodolico 421
      }
422
 
423
      unless ( -e $decrypted ) {
424
         logMsg "Decrypted device $decrypted does not exist after geli attach\n";
425
         return '';
426
      }
427
      push @decrypted_devices, $decrypted;
428
   }
429
 
430
   # Import the ZFS pool
431
   logMsg("Importing ZFS pool $poolname");
432
   my @import_cmd = ('zpool', 'import');
433
   # If decrypted devices exist, add their directories to -d list
30 rodolico 434
   #foreach my $dev (@decrypted_devices) {
435
   #   my $dir = $dev;
436
   #   $dir =~ s!/[^/]+$!!;  # Remove filename to get directory
437
   #   push @import_cmd, '-d', $dir;
438
   #}
439
 
24 rodolico 440
   push @import_cmd, $poolname;
441
 
40 rodolico 442
   runCmd("zpool import $poolname" );
443
   unless ( $lastRunError == 0 ) {
444
      logMsg("Failed to import zfs pool $poolname (exit $lastRunError)\n");
24 rodolico 445
      return '';
446
   }
447
 
448
   # Mount the ZFS pool (zfs mount -a mounts all filesystems in the pool)
449
   logMsg("Mounting ZFS pool $poolname");
40 rodolico 450
   runCmd('zfs mount -a');
451
   unless ( $lastRunError == 0 ) {
452
      logMsg("Failed to mount zfs pool $poolname (exit $lastRunError)\n");
24 rodolico 453
      return '';
454
   }
455
 
456
   logMsg("Successfully decrypted and mounted pool $poolname");
457
   return $poolname;
458
}
459
 
460
## Create a GELI key by XOR'ing a remote binary keyfile and a local key (hex string).
461
##
48 rodolico 462
## Expected input (via $geliConfig HASHREF):
463
##   $geliConfig->{secureKey}->{path} - directory where the remote keyfile resides
464
##   $geliConfig->{secureKey}->{keyfile} - filename of the remote 32-byte binary key
465
##   $geliConfig->{localKey} - 64-hex char string OR path to a file containing the hex
466
##   $geliConfig->{target} - path to write the resulting 32-byte binary key
24 rodolico 467
##
48 rodolico 468
## Behavior:
469
##   - Reads 32 bytes from the remote binary key
470
##   - Reads/cleans the 64-hex local key and converts it to 32 bytes
471
##   - XORs the two 32-byte buffers and writes the 32-byte result to $target with mode 0600
472
##
473
## Returns: 1 on success. Dies on unrecoverable errors.
24 rodolico 474
sub makeGeliKey {
38 rodolico 475
   my ( $geliConfig ) = @_;
24 rodolico 476
 
38 rodolico 477
   $geliConfig->{secureKey}->{keyfile} //= '';
478
   $geliConfig->{localKey} //= '';
479
   $geliConfig->{target} //= '';
24 rodolico 480
 
38 rodolico 481
   if ( $geliConfig->{target} && -f $geliConfig->{target} ) {
482
      logMsg "GELI target keyfile $geliConfig->{target} already exists. Not overwriting.\n";
483
      return 1;
484
   }
24 rodolico 485
 
38 rodolico 486
   my $remote_keyfile = "$geliConfig->{secureKey}->{path}/$geliConfig->{secureKey}->{keyfile}";
487
   my $localKeyHexOrPath = $geliConfig->{localKey};
488
   my $target = $geliConfig->{target};
40 rodolico 489
 
38 rodolico 490
   if ( $geliConfig->{secureKey}->{keyfile} && $geliConfig->{localKey} ) {
491
      # we have what we need to proceed
492
 
493
      if ( -f $remote_keyfile ) {
494
         logMsg "Creating GELI keyfile at $geliConfig->{target} using remote keyfile " . $geliConfig->{secureKey}->{keyfile} . " and local key\n";
495
      } else {
496
         die "Remote keyfile " . $geliConfig->{secureKey}->{keyfile} . " does not exist\n";
497
      }
498
   }
499
 
24 rodolico 500
   # Read remote binary key
501
   open my $rh, '<:raw', $remote_keyfile or die "Unable to open $remote_keyfile: $!\n";
502
   my $rbuf;
503
   my $read = read($rh, $rbuf, 32);
504
   close $rh;
505
   die "Failed to read 32 bytes from $remote_keyfile (got $read)\n" unless defined $read && $read == 32;
506
 
507
   # Get local hex string (either direct string or file contents)
508
   my $hex;
509
   if (-e $localKeyHexOrPath) {
510
      open my $lh, '<', $localKeyHexOrPath or die "Unable to open local key file $localKeyHexOrPath: $!\n";
511
      local $/ = undef;
512
      $hex = <$lh>;
513
      close $lh;
514
   } else {
515
      $hex = $localKeyHexOrPath;
516
   }
517
   # clean hex (remove whitespace/newlines and optional 0x)
518
   $hex =~ s/0x//g;
519
   $hex =~ s/[^0-9a-fA-F]//g;
520
 
521
   die "Local key must be 64 hex characters (256-bit)\n" unless length($hex) == 64;
522
 
523
   my $lbuf = pack('H*', $hex);
524
   die "Local key decoded to unexpected length " . length($lbuf) . "\n" unless length($lbuf) == 32;
525
 
526
   # XOR the two buffers
527
   my $out = '';
528
   for my $i (0 .. 31) {
529
      $out .= chr( ord(substr($rbuf, $i, 1)) ^ ord(substr($lbuf, $i, 1)) );
530
   }
531
 
532
   # Ensure target directory exists
533
   my ($vol, $dirs, $file) = ($target =~ m{^(/?)(.*/)?([^/]+)$});
534
   if ($dirs) {
535
      my $dir = $dirs;
536
      $dir =~ s{/$}{};
537
      unless (-d $dir) {
538
         require File::Path;
539
         File::Path::make_path($dir) or die "Failed to create directory $dir: $!\n";
540
      }
541
   }
542
 
543
   # Write out binary key and protect permissions
544
   open my $oh, '>:raw', $target or die "Unable to open $target for writing: $!\n";
545
   print $oh $out or die "Failed to write to $target: $!\n";
546
   close $oh;
547
   chmod 0600, $target;
548
 
549
   return 1;
550
}
551
 
42 rodolico 552
# make a bunch of replicate commands and return them to the caller as a list
44 rodolico 553
# $sourceSnapsRef - list of snapshots on source machine
554
# $targetSnapsRef - list of snapshots on target machine
555
# $dataset - The name of the dataset we are working on (same on both source and target)
556
# $sourceParent - The parent dataset of $dataset on source
557
# $targetParent - The parent dataset of $dataset on target
558
# $newStatusRef - A place to put the updated $targetSnapsRef
559
# returns hashref of commands to execute, of form
560
#    {$dataset} = "zfs send command"
561
# where $dataset above can be a child of $dataset
25 rodolico 562
sub makeReplicateCommands {
44 rodolico 563
   my ( $sourceSnapsRef, $targetSnapsRef, $dataset, $sourceParent, $targetParent, $newStatusRef ) = @_;
25 rodolico 564
   $sourceSnapsRef ||= [];
44 rodolico 565
   $targetSnapsRef     ||= [];
25 rodolico 566
   $newStatusRef  ||= [];
44 rodolico 567
   $sourceParent //= '';
568
   $sourceParent .= '/' unless $sourceParent eq '' or substr($sourceParent, -1) eq '/';
569
   $targetParent //= '';
570
   $targetParent .= '/' unless $targetParent eq '' or substr($targetParent, -1) eq '/';
25 rodolico 571
 
44 rodolico 572
   my %commands; # this will hold the commands (and the dataset as key) for return
573
 
574
   fatalError( "No dataset defined in makeReplicateCommands, can not continue") unless $dataset;
575
 
576
   # filter only the target and source snapshots which have this dataset in them, then remove
577
   # the parent of each.
578
   my $targetSnaps = [ map{ s/^$targetParent//r } grep{ /$dataset/ } @$targetSnapsRef ];
579
   my $sourceSnaps = [ map{ s/^$sourceParent//r } grep{ /$dataset/ } @$sourceSnapsRef ];
580
 
581
   #print "Dataset => [$dataset]\nSource Parent => [$sourceParent]\nTarget Parent => [$targetParent]\n";
582
   #print "Source Snaps\n" . Dumper( $sourceSnapsRef) . "\nTarget Snaps\n" . Dumper( $targetSnapsRef) . "\n";
583
 
584
   #print Dumper( $targetSnaps ) . "\n" . Dumper( $sourceSnaps ) . "\n"; die;
585
   #return \%commands;
586
 
25 rodolico 587
   # parse snapshots: each line is expected to have snapshot fullname as first token: pool/fs@snap ...
588
   my %snaps_by_fs;
44 rodolico 589
   foreach my $line (@$sourceSnaps) {
25 rodolico 590
      next unless defined $line && $line =~ /\S/;
591
      my ($tok) = split /\s+/, $line;
592
      next unless $tok && $tok =~ /@/;
593
      my ($fs, $snap) = split /@/, $tok, 2;
594
      push @{ $snaps_by_fs{$fs} }, $snap;
595
   }
596
 
597
   # nothing to do
598
   return [] unless keys %snaps_by_fs;
599
 
600
   # figure root filesystem: first snapshot line's fs is the requested root
44 rodolico 601
   my ($first_line) = grep { defined $_ && $_ =~ /\S/ } @$sourceSnaps;
25 rodolico 602
   my ($root_fs) = $first_line ? (split(/\s+/, $first_line))[0] =~ /@/ ? (split(/@/, (split(/\s+/, $first_line))[0]))[0] : undef : undef;
603
   $root_fs ||= (sort keys %snaps_by_fs)[0];
604
 
605
   # helper: find last status entry for a filesystem (status lines contain full snapshot names pool/fs@snap)
606
   my %last_status_for;
44 rodolico 607
   for my $s (@$targetSnaps) {
25 rodolico 608
      next unless $s && $s =~ /@/;
609
      my ($fs, $snap) = split /@/, $s, 2;
610
      $last_status_for{$fs} = $snap;    # later entries override earlier ones -> last occurrence kept
611
   }
612
 
613
   # build per-filesystem "from" and "to"
614
   my %from_for;
615
   my %to_for;
616
   foreach my $fs (keys %snaps_by_fs) {
617
      my $arr = $snaps_by_fs{$fs};
618
      next unless @$arr;
619
      $to_for{$fs} = $arr->[-1];
620
      $from_for{$fs} = $last_status_for{$fs};    # may be undef -> full send required
621
   }
622
 
623
   # decide if we can do a single recursive send:
624
   # condition: all 'to' snapshot names are identical
625
   my %to_names = map { $_ => 1 } values %to_for;
626
   my $single_to_name = (keys %to_names == 1) ? (keys %to_names)[0] : undef;
627
 
628
   if ($single_to_name) {
629
      # check whether any from is missing
630
      my @from_values = map { $from_for{$_} } sort keys %from_for;
631
      my $any_from_missing = grep { !defined $_ } @from_values;
632
      my %from_names = map { $_ => 1 } grep { defined $_ } @from_values;
633
      my $single_from_name = (keys %from_names == 1) ? (keys %from_names)[0] : undef;
634
 
635
      if ($any_from_missing) {
636
         # full recursive send from root
44 rodolico 637
         $commands{$root_fs} = sprintf('zfs send -R %s%s@%s', $sourceParent, $root_fs, $single_to_name);
25 rodolico 638
      }
639
      elsif ($single_from_name) {
31 rodolico 640
         # incremental recursive send, but don't do it if they are the same
44 rodolico 641
         $commands{$root_fs} = sprintf('zfs send -R -I %s%s@%s %s%s@%s',
642
                           $sourceParent, $root_fs, $single_from_name, $sourceParent, $root_fs, $single_to_name)
31 rodolico 643
                           unless $single_from_name eq $single_to_name;
25 rodolico 644
      }
645
      else {
646
         # from snapshots differ across children -> fall back to per-filesystem sends
647
         foreach my $fs (sort keys %to_for) {
648
            my $to  = $to_for{$fs};
649
            my $from = $from_for{$fs};
650
            if ($from) {
31 rodolico 651
               # if from and to are different, add it
44 rodolico 652
               $commands{$fs} = sprintf('zfs send -I %s%s@%s %s%s@%s', $sourceParent, $fs, $from, $sourceParent, $fs, $to)
31 rodolico 653
                  unless $from eq $to;
25 rodolico 654
            } else {
44 rodolico 655
               $commands{$fs} = sprintf('zfs send %s%s@%s', $sourceParent, $fs, $to);
25 rodolico 656
            }
657
         }
658
      }
659
 
660
      # update new status: record newest snap for every filesystem
661
      foreach my $fs (keys %to_for) {
44 rodolico 662
         push @$newStatusRef, sprintf('%s%s@%s', $targetParent, $fs, $to_for{$fs});
25 rodolico 663
      }
664
   } else {
665
      # not all children share same newest snap -> per-filesystem sends
666
      foreach my $fs (sort keys %to_for) {
667
         my $to  = $to_for{$fs};
668
         my $from = $from_for{$fs};
669
         if ($from) {
44 rodolico 670
            $commands{$fs} = sprintf('zfs send -I %s%s@%s %s%s@%s', $sourceParent, $fs, $from, $sourceParent, $fs, $to);
25 rodolico 671
         } else {
44 rodolico 672
            $commands{$fs} = sprintf('zfs send %s%s@%s', $sourceParent, $fs, $to);
25 rodolico 673
         }
44 rodolico 674
         push @$newStatusRef, sprintf('%s%s@%s', $targetParent, $fs, $to);
25 rodolico 675
      }
676
   }
677
 
678
   # return arrayref of commands (caller can iterate or join with pipes)
31 rodolico 679
   return \%commands;
25 rodolico 680
}
681
 
35 rodolico 682
# Send report via email and/or copy to target drive.
683
# $reportConfig is a hashref with optional keys:
684
#   email - email address to send report to
685
#   targetDrive - hashref with keys:
686
#       label - GPT or msdosfs label of the target drive
687
#       mount_point - optional mount point to use (if not provided, /mnt/label is used)
688
# $subject is the email subject
42 rodolico 689
# $message is the message to include in the email body
690
# $logFile is the path to the log file to include in the report
35 rodolico 691
sub sendReport {
42 rodolico 692
   my ( $reportConfig, $message, $logFile ) = @_;
35 rodolico 693
   return unless defined $reportConfig;
42 rodolico 694
   $logFile //= $reportConfig->{logFile};
37 rodolico 695
   logMsg( "Beginning sendReport" );
696
   # if targetDrive defined and there is a valid label for it, try to mount it and write the report there
697
   if ( defined $reportConfig->{targetDrive} && defined $reportConfig->{targetDrive}->{label} && $reportConfig->{targetDrive}->{label} ) {
698
      logMsg( "Saving report to disk with label $reportConfig->{targetDrive}->{label}" );
46 rodolico 699
      if ( $reportConfig->{targetDrive}->{mountPath} = mountDriveByLabel( $reportConfig->{targetDrive} ) ) {
700
         copyReportToDrive( $logFile, $reportConfig->{targetDrive}->{mountPath} );
701
         unmountDriveByLabel( $reportConfig->{targetDrive} );
35 rodolico 702
      } else {
703
         logMsg( "Warning: could not mount report target drive with label '$reportConfig->{targetDrive}->{label}'" );
704
      }
705
   }
42 rodolico 706
   # if they have set an e-mail address, try to e-mail the report
707
   if ( defined $reportConfig->{email} && $reportConfig->{email} ne '' ) {
708
      logMsg( "Sending report via e-mail to $reportConfig->{email}" );
709
      $reportConfig->{subject} //= 'Replication Report from ' . `hostname`;
710
      sendEmailReport( $reportConfig->{email}, $reportConfig->{subject}, $message, $logFile );
711
   }
35 rodolico 712
}
25 rodolico 713
 
48 rodolico 714
## Copy the report log file to a mounted target drive.
715
##
716
## Arguments:
717
##   $logFile    - path to the log file to copy (must exist)
718
##   $mountPoint - mount point of the target drive (must be a directory)
719
##
720
## Behavior:
721
##   - Copies the log file into the root of $mountPoint using File::Copy::copy
722
##   - Logs success/failure via logMsg
35 rodolico 723
sub copyReportToDrive {
724
   my ( $logFile, $mountPoint ) = @_;
725
   return unless defined $logFile && -e $logFile;
726
   return unless defined $mountPoint && -d $mountPoint;
727
 
728
   my $targetFile = "$mountPoint/" . ( split( /\//, $logFile ) )[-1];
729
   logMsg( "Copying report log file $logFile to drive at $mountPoint" );
46 rodolico 730
   use File::Copy;
35 rodolico 731
   unless ( copy( $logFile, $targetFile ) ) {
732
      logMsg( "Could not copy report log file to target drive: $!" );
733
   }
734
}
735
 
48 rodolico 736
## Send an email report with an attached log body.
737
##
738
## Arguments:
739
##   $to      - recipient email address (string)
740
##   $subject - subject line (string)
741
##   $message - optional message body (string)
742
##   $logFile - optional path to log file whose contents will be appended to the email body
743
##
744
## Behavior:
745
##   - Opens /usr/sbin/sendmail -t and writes a simple plain-text email including the
746
##     supplied message and the contents of $logFile (if present).
747
##   - Logs failures to open sendmail or read the log file.
35 rodolico 748
sub sendEmailReport {
42 rodolico 749
   my ( $to, $subject, $message, $logFile ) = @_;
35 rodolico 750
   return unless defined $to && $to ne '';
37 rodolico 751
   $subject //= 'Sneakernet Replication Report from ' . `hostname`;
42 rodolico 752
   $message //= '';
37 rodolico 753
   $logFile //= '';
35 rodolico 754
 
755
   logMsg( "Sending email report to $to with subject '$subject'" );
756
   open my $mailfh, '|-', '/usr/sbin/sendmail -t' or do {
757
      logMsg( "Could not open sendmail: $!" );
758
      return;
759
   };
760
   print $mailfh "To: $to\n";
761
   print $mailfh "Subject: $subject\n";
762
   print $mailfh "MIME-Version: 1.0\n";
763
   print $mailfh "Content-Type: text/plain; charset=\"utf-8\"\n";
764
   print $mailfh "\n"; # end of headers
37 rodolico 765
 
42 rodolico 766
   print $mailfh "$message\n";
767
   print $mailfh "\nLog contents:\n\n";
37 rodolico 768
   if ( -e $logFile && open my $logfh, '<', $logFile ) {
769
      while ( my $line = <$logfh> ) {
770
         print $mailfh $line;
771
      }
772
      close $logfh;
773
   } else {
774
      logMsg( "Could not open log file [$logFile] for reading: $!" );
35 rodolico 775
   };
37 rodolico 776
 
35 rodolico 777
   close $mailfh;
778
}  
779
 
48 rodolico 780
## Return list of regular files in a directory (non-recursive).
781
##
782
## Arguments:
783
##   $dirname - directory to scan
784
##
785
## Returns: ARRAYREF of full-path filenames on success, 0 on error (matching prior behavior).
42 rodolico 786
sub getDirectoryList {
787
   my $dirname = shift;
788
   opendir( my $dh, $dirname ) || return 0;
789
   # get all file names, but leave directories alone
790
   my @files = map{ $dirname . "/$_" } grep { -f "$dirname/$_" } readdir($dh);
791
   closedir $dh;
792
   return \@files;
793
}
794
 
48 rodolico 795
## Remove all regular files from the specified directory (non-recursive).
796
##
797
## Arguments:
798
##   $dirname - directory to clean
799
##
800
## Behavior:
801
##   - Calls getDirectoryList to obtain files and unlinks each file. Directories are left untouched.
802
##   - Logs the cleanup operation via logMsg.
803
##
804
## Returns: 1 on completion. Note: individual unlink failures are currently reported via warn.
42 rodolico 805
sub cleanDirectory {
806
   my $dirname = shift;
807
   logMsg( "Cleaning up $dirname of all files" );
808
   my $files = getDirectoryList( $dirname );
809
   # clean up a directory
810
   foreach my $file (@$files) {
811
      unlink $file or warn "Could not unlink $file: #!\n";
812
   }
813
   return 1;
814
}
815
 
48 rodolico 816
## Handle a fatal error: log, optionally run a cleanup routine, then die.
817
##
818
## Arguments:
819
##   $message        - string message describing the fatal condition
820
##   $config         - OPTIONAL configuration HASHREF (passed to cleanupRoutine)
821
##   $cleanupRoutine - OPTIONAL CODE ref to run prior to dying; will be called as
822
##                     $cleanupRoutine->($config, $message)
823
##
824
## Behavior:
825
##   - Logs the fatal message via logMsg, runs the cleanup code if provided (errors in the cleanup
826
##     are logged), then terminates the process via die.
42 rodolico 827
sub fatalError {
828
   my ( $message, $config, $cleanupRoutine ) = @_;
829
   logMsg( "FATAL ERROR: $message" );
830
   if ( defined $cleanupRoutine && ref $cleanupRoutine eq 'CODE' ) {
831
      logMsg( "Running cleanup routine before fatal error" );
832
      eval {
833
         $cleanupRoutine->( $config, $message );
834
         1;
835
      } or do {
836
         logMsg( "Cleanup routine failed: $@" );
837
      };
838
   }
839
   die;
840
}
841
 
842
 
24 rodolico 843
1;