Subversion Repositories zfs_utils

Rev

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