Subversion Repositories zfs_utils

Rev

Rev 30 | Rev 33 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
24 rodolico 1
package ZFS_Utils;
2
 
3
use strict;
4
use warnings;
5
use Exporter 'import';
6
use Data::Dumper;
7
use POSIX qw(strftime);
8
use File::Path qw(make_path);
9
 
25 rodolico 10
our @EXPORT_OK = qw(loadConfig shredFile mountDriveByLabel mountGeli logMsg runCmd makeReplicateCommands $logFileName $displayLogsOnConsole);
24 rodolico 11
 
12
 
13
our $VERSION = '0.1';
14
our $logFileName = '/tmp/zfs_utils.log'; # this can be overridden by the caller, and turned off with empty string
15
our $displayLogsOnConsole = 1;
27 rodolico 16
our $merge_stderr = 0; # if set to 1, stderr is captured in runCmd
24 rodolico 17
 
25 rodolico 18
# Execute a command and return its output.
19
# If called in scalar context, returns the full output as a single string.
20
# If called in list context, returns the output split into lines.
21
# If $merge_stderr is true (default), stderr is merged into stdout (only for scalar commands).
22
# returns empty string or empty list on failure and logs failure message.
23
sub runCmd {
27 rodolico 24
   my $cmd = \@_;
25 rodolico 25
   $merge_stderr = 1 unless defined $merge_stderr;
26
   my $output = '';
27
 
28
   if (ref $cmd eq 'ARRAY') {
29
      # Execute without a shell (safer). Note: stderr is not merged in this path.
31 rodolico 30
      logMsg( 'Running command [' . join( ' ', @$cmd ) . ']');
25 rodolico 31
      open my $fh, '-|', @{$cmd} or do {
32
         logMsg("runCmd: failed to exec '@{$cmd}': $!");
33
         return wantarray ? () : '';
34
      };
35
      local $/ = undef;
36
      $output = <$fh>;
37
      close $fh;
38
   } else {
39
      # Scalar command runs via the shell; optionally merge stderr into stdout.
27 rodolico 40
      logMsg( "Scalar running command [$cmd]" );
25 rodolico 41
      my $c = $cmd;
42
      $c .= ' 2>&1' if $merge_stderr;
43
      $output = `$c`;
44
   }
45
 
46
   $output //= '';
47
 
48
   if (wantarray) {
49
      return $output eq '' ? () : split(/\n/, $output);
50
   } else {
51
      return $output;
52
   }
53
}
54
 
24 rodolico 55
# this calls gshred which will overwrite the file 3 times, then
56
# remove it.
57
# NOTE: this will not work on ZFS, since ZFS is CopyOnWrite (COW)
58
# so assuming file is on something without COW (ramdisk, UFS, etc)
59
sub shredFile {
60
   my $filename = shift;
61
   `/usr/local/bin/gshred -u -f -s 32 $filename` if -e $filename;
62
}
63
 
64
sub logMsg {
65
    my $msg = shift;
66
    my $filename = shift // $logFileName;
67
    my $timeStampFormat = shift // '%Y-%m-%d %H:%M:%S';
68
    my $timestamp = strftime($timeStampFormat, localtime());
69
    if (defined $filename && $filename ne '' ) {
70
       open my $logfh, '>>', $filename or die "Could not open log file $filename: $!\n";
71
       print $logfh "$timestamp\t$msg\n";
72
       close $logfh;
73
    }
74
    print "$timestamp\t$msg\n" if ($displayLogsOnConsole);
75
}
76
 
77
# find a drive by it's label by scanning /dev/gpt/ for $timeout seconds.
78
# If the drive is found, mount it on mountPath and return the mountPath.
79
# If not found, return empty string.
80
sub mountDriveByLabel {
81
   my ($label, $mountPath, $timeout, $checkEvery ) = @_;
82
   unless ($label) {
83
      logMsg("mountDriveByLabel: No label provided");
84
      return '';
85
   }
86
   unless ( $label =~ /^[a-zA-Z0-9_\-]+$/ ) {
87
      logMsg("mountDriveByLabel: Invalid label '$label'");
88
      return '';
89
   }
90
 
91
   logMsg("mountDriveByLabel: Looking for drive with label '$label'");
92
   # default to /mnt/label if not provided
93
   $mountPath //= "/mnt/$label"; # this is where we'll mount it if we find it
94
   $label = "/dev/gpt/$label"; #  this is where FreeBSD puts gpt labeled drives
31 rodolico 95
   # drive already mounted, just return the path
96
   return $mountPath if ( runCmd( "mount | grep '$mountPath'" ) );
24 rodolico 97
   # default to 10 minutes (600 seconds) if not provided
98
   $timeout //= 600;
99
   # default to checking every minute if not provided
31 rodolico 100
   $checkEvery //= 15;
24 rodolico 101
   # wait up to $timeout seconds for device to appear, checking every 10 seconds
102
   while ( $timeout > 0 ) {
103
      if ( -e "$label" ) {
104
         last;
105
      } else {
106
         sleep $checkEvery;
107
         $timeout -= $checkEvery;
31 rodolico 108
         print "Waiting for drive labeled $label\n";
24 rodolico 109
      }
110
    }
111
    # if we found it, mount and return mount path
112
    if ( -e "$label" ) {
113
       # ensure mount point
114
       unless ( -d $mountPath || make_path($mountPath) ) {
115
         logMsg("Failed to create $mountPath: $!");
116
         return '';
117
       }
118
       # mount device (let mount detect filesystem)
119
       unless ( system('mount', $label, $mountPath) == 0 ) {
120
         logMsg("Failed to mount $label on $mountPath: $!");
121
         return '';
122
       }
123
       return $mountPath;
124
    } else {
125
       return '';
126
    }
127
}
128
 
129
## Load a YAML configuration file into a hashref.
130
## If the file does not exist, and a default hashref is provided,
131
## create the file by dumping the default to YAML, then return the default.
132
sub loadConfig {
133
    my ($filename, $default) = @_;
134
 
135
    # If no filename was provided, return default or empty hashref
136
    die "No filename provided to loadConfig\n" unless defined $filename;
137
 
138
    # If file doesn't exist but a default hashref was provided, try to
139
    # create the file by dumping the default to YAML, then return the default.
140
    unless (-e $filename) {
141
      logMsg("Config file $filename does not exist. Creating it with default values.");
142
      if ($default && ref $default eq 'HASH') {
143
         my $wrote = 0;
144
         eval {
145
               require YAML::XS;
146
               YAML::XS->import();
147
               YAML::XS::DumpFile($filename, $default);
148
               $wrote = 1;
149
               1;
150
         } or do {
151
               eval {
152
                  require YAML::Tiny;
153
                  YAML::Tiny->import();
154
                  my $yt = YAML::Tiny->new($default);
155
                  $yt->write($filename);
156
                  $wrote = 1;
157
                  1;
158
               } or do {
159
                  logMsg("No YAML writer available (YAML::XS or YAML::Tiny). Could not create $filename");
160
               };
161
         };
162
 
163
         die "Failed to write default config to $filename:$!\n" unless $wrote;
164
        }
165
 
166
        # No default provided; nothing to create
167
        return {};
168
    }
169
 
170
    my $yaml;
171
 
172
    # Try YAML::XS first, fall back to YAML::Tiny
173
    eval {
174
        require YAML::XS;
175
        YAML::XS->import();
176
        $yaml = YAML::XS::LoadFile($filename);
177
        logMsg("using YAML::XS to load $filename");
178
        1;
179
    } or do {
180
        eval {
181
            require YAML::Tiny;
182
            YAML::Tiny->import();
183
            $yaml = YAML::Tiny->read($filename);
184
            $yaml = $yaml->[0] if $yaml;  # YAML::Tiny returns an arrayref of documents
185
            logMsg("using YAML::Tiny to load $filename");
186
            1;
187
        } or do {
188
            logMsg("No YAML parser installed (YAML::XS or YAML::Tiny). Skipping config load from $filename");
189
            return ($default && ref $default eq 'HASH') ? $default : {};
190
        };
191
    };
192
    # Ensure we have a hashref
193
    die "Config file $filename did not produce a HASH.\n" unless (defined $yaml && ref $yaml eq 'HASH');
194
 
195
    return $yaml;
196
}
197
 
198
 
199
 
200
sub mountGeli {
201
   my $geliConfig = shift;
30 rodolico 202
   unless ( $geliConfig->{'localKey'} ) {
203
      logMsg "Could not find local key in configuration file\n";
24 rodolico 204
      return '';
205
   }
206
   # find the keyfile disk and mount it
207
   my $path = mountDriveByLabel( $geliConfig->{'keydiskname'} );
208
   unless ( $path ne '' and -e "$path/" . $geliConfig->{'keyfile'} ) {
209
      logMsg "Could not find or mount keyfile disk with label: " . $geliConfig->{'keydiskname'} . "\n";
210
      return '';
211
   }
212
   # create the combined geli keyfile in target location
213
   unless ( makeGeliKey( "$path/" . $geliConfig->{'keyfile'}, $geliConfig->{'localKey'}, $geliConfig->{'target'} ) ) {
214
         logMsg "Could not create geli keyfile\n";
215
         return '';
216
      }
217
   # decrypt and mount the geli disks and zfs pool
218
   my $poolname = decryptAndMountGeli( $geliConfig );
219
   return $poolname;
220
 
221
}
222
 
30 rodolico 223
# find all disks which are candidates for use with geli/zfs
224
# Grabs all disks on the system, then removes those with partitions
225
# and those already used in zpools.
226
sub findGeliDisks {
227
   logMsg("Finding available disks for GELI/ZFS use");
228
   # get all disks in system
229
   my %allDisks = map{ chomp $_ ; $_ => 1 } runCmd( "geom disk list | grep 'Geom name:' | rev | cut -d' ' -f1 | rev" );
230
   # get the disks with partitions
231
   my @temp = runCmd( "gpart show -p | grep '^=>'");  # -p prints just the disks without partitions
232
   # remove them from the list
233
   foreach my $disk ( @temp ) {
234
      $allDisks{$1} = 0 if ( $disk =~ m/^=>[\t\s0-9]+([a-z][a-z0-9]+)/ ) ;
235
   }
236
 
237
   # get disk which are currently used for zpools
238
   @temp = runCmd( "zpool status -LP | grep '/dev/'" );
239
   foreach my $disk ( @temp ) {
240
      $allDisks{$1} = 0 if  $disk =~ m|/dev/([a-z]+\d+)|;
241
   }
242
 
243
   # return only the disks which are free (value 1)
244
   return grep{ $allDisks{$_} == 1 } keys %allDisks;
245
}
246
 
24 rodolico 247
## Decrypt each GELI disk from $geliConfig->{'diskList'} using the keyfile,
248
## then import and mount the ZFS pool specified in $geliConfig->{'poolname'}.
249
##
250
## Returns the pool name on success, empty on error.
251
sub decryptAndMountGeli {
252
   my ($geliConfig) = @_;
30 rodolico 253
 
254
   # Can't continue at all if no pool name
24 rodolico 255
   die "No pool name specified in config\n" unless $geliConfig->{'poolname'};
30 rodolico 256
   # if no list of disks provided, try to find them
257
   $geliConfig->{'diskList'} //= findGeliDisks();
258
 
24 rodolico 259
   my $diskList = $geliConfig->{'diskList'};
260
   my $poolname = $geliConfig->{'poolname'};
261
   my $keyfile = $geliConfig->{'target'};
262
   unless ( -e $keyfile ) {
263
      logMsg "GELI keyfile $keyfile does not exist\n";
264
      return '';
265
   }
266
 
267
   my @decrypted_devices;
268
 
269
   # Decrypt each disk in the list
30 rodolico 270
   foreach my $disk (@{$geliConfig->{'diskList'}}) {
24 rodolico 271
      unless ( -e $disk ) {
272
         logMsg "Disk $disk does not exist\n";
273
         return '';
274
      }
275
 
276
      # Derive the decrypted device name (.eli suffix on FreeBSD)
277
      my $decrypted = $disk . '.eli';
278
 
279
      # Decrypt using geli attach with the keyfile
280
      logMsg("Decrypting $disk with keyfile $keyfile");
30 rodolico 281
      if ( my $result = system('geli', 'attach', '-k', $geliConfig->{'target'}, $disk) == 0 ) {
24 rodolico 282
         logMsg "Failed to decrypt $disk (exit $result)\n";
30 rodolico 283
         next; # ignore failed disks and continue to see if we can import the pool
24 rodolico 284
      }
285
 
286
      unless ( -e $decrypted ) {
287
         logMsg "Decrypted device $decrypted does not exist after geli attach\n";
288
         return '';
289
      }
290
      push @decrypted_devices, $decrypted;
291
   }
292
 
293
   # Import the ZFS pool
294
   logMsg("Importing ZFS pool $poolname");
295
   my @import_cmd = ('zpool', 'import');
296
   # If decrypted devices exist, add their directories to -d list
30 rodolico 297
   #foreach my $dev (@decrypted_devices) {
298
   #   my $dir = $dev;
299
   #   $dir =~ s!/[^/]+$!!;  # Remove filename to get directory
300
   #   push @import_cmd, '-d', $dir;
301
   #}
302
 
24 rodolico 303
   push @import_cmd, $poolname;
304
 
305
   my $result = system(@import_cmd);
306
   unless ( $result == 0 ) {
307
      logMsg("Failed to import zfs pool $poolname (exit $result)\n");
308
      return '';
309
   }
310
 
311
   # Mount the ZFS pool (zfs mount -a mounts all filesystems in the pool)
312
   logMsg("Mounting ZFS pool $poolname");
313
   $result = system('zfs', 'mount', '-a');
314
   unless ( $result == 0 ) {
315
      logMsg("Failed to mount zfs pool $poolname (exit $result)\n");
316
      return '';
317
   }
318
 
319
   logMsg("Successfully decrypted and mounted pool $poolname");
320
   return $poolname;
321
}
322
 
323
## Create a GELI key by XOR'ing a remote binary keyfile and a local key (hex string).
324
##
325
## Arguments:
326
##   $remote_keyfile - path to binary keyfile (32 bytes)
327
##   $localKeyHexOrPath - hex string (64 hex chars) or path to file containing hex
328
##   $target - path to write the resulting 32-byte binary key
329
##
330
## Returns true on success, dies on fatal error.
331
sub makeGeliKey {
332
   my ($remote_keyfile, $localKeyHexOrPath, $target) = @_;
333
 
334
   die "remote keyfile not provided" unless defined $remote_keyfile;
335
   die "local key not provided" unless defined $localKeyHexOrPath;
336
   die "target not provided" unless defined $target;
337
 
338
   die "Remote keyfile $remote_keyfile does not exist\n" unless -e $remote_keyfile;
339
 
340
   # Read remote binary key
341
   open my $rh, '<:raw', $remote_keyfile or die "Unable to open $remote_keyfile: $!\n";
342
   my $rbuf;
343
   my $read = read($rh, $rbuf, 32);
344
   close $rh;
345
   die "Failed to read 32 bytes from $remote_keyfile (got $read)\n" unless defined $read && $read == 32;
346
 
347
   # Get local hex string (either direct string or file contents)
348
   my $hex;
349
   if (-e $localKeyHexOrPath) {
350
      open my $lh, '<', $localKeyHexOrPath or die "Unable to open local key file $localKeyHexOrPath: $!\n";
351
      local $/ = undef;
352
      $hex = <$lh>;
353
      close $lh;
354
   } else {
355
      $hex = $localKeyHexOrPath;
356
   }
357
   # clean hex (remove whitespace/newlines and optional 0x)
358
   $hex =~ s/0x//g;
359
   $hex =~ s/[^0-9a-fA-F]//g;
360
 
361
   die "Local key must be 64 hex characters (256-bit)\n" unless length($hex) == 64;
362
 
363
   my $lbuf = pack('H*', $hex);
364
   die "Local key decoded to unexpected length " . length($lbuf) . "\n" unless length($lbuf) == 32;
365
 
366
   # XOR the two buffers
367
   my $out = '';
368
   for my $i (0 .. 31) {
369
      $out .= chr( ord(substr($rbuf, $i, 1)) ^ ord(substr($lbuf, $i, 1)) );
370
   }
371
 
372
   # Ensure target directory exists
373
   my ($vol, $dirs, $file) = ($target =~ m{^(/?)(.*/)?([^/]+)$});
374
   if ($dirs) {
375
      my $dir = $dirs;
376
      $dir =~ s{/$}{};
377
      unless (-d $dir) {
378
         require File::Path;
379
         File::Path::make_path($dir) or die "Failed to create directory $dir: $!\n";
380
      }
381
   }
382
 
383
   # Write out binary key and protect permissions
384
   open my $oh, '>:raw', $target or die "Unable to open $target for writing: $!\n";
385
   print $oh $out or die "Failed to write to $target: $!\n";
386
   close $oh;
387
   chmod 0600, $target;
388
 
389
   return 1;
390
}
391
 
25 rodolico 392
sub makeReplicateCommands {
393
   my ($sourceSnapsRef, $statusRef, $newStatusRef) = @_;
394
   $sourceSnapsRef ||= [];
395
   $statusRef     ||= [];
396
   $newStatusRef  ||= [];
397
 
398
   # parse snapshots: each line is expected to have snapshot fullname as first token: pool/fs@snap ...
399
   my %snaps_by_fs;
400
   foreach my $line (@$sourceSnapsRef) {
401
      next unless defined $line && $line =~ /\S/;
402
      my ($tok) = split /\s+/, $line;
403
      next unless $tok && $tok =~ /@/;
404
      my ($fs, $snap) = split /@/, $tok, 2;
405
      push @{ $snaps_by_fs{$fs} }, $snap;
406
   }
407
 
408
   # nothing to do
409
   return [] unless keys %snaps_by_fs;
410
 
411
   # figure root filesystem: first snapshot line's fs is the requested root
412
   my ($first_line) = grep { defined $_ && $_ =~ /\S/ } @$sourceSnapsRef;
413
   my ($root_fs) = $first_line ? (split(/\s+/, $first_line))[0] =~ /@/ ? (split(/@/, (split(/\s+/, $first_line))[0]))[0] : undef : undef;
414
   $root_fs ||= (sort keys %snaps_by_fs)[0];
415
 
416
   # helper: find last status entry for a filesystem (status lines contain full snapshot names pool/fs@snap)
417
   my %last_status_for;
418
   for my $s (@$statusRef) {
419
      next unless $s && $s =~ /@/;
420
      my ($fs, $snap) = split /@/, $s, 2;
421
      $last_status_for{$fs} = $snap;    # later entries override earlier ones -> last occurrence kept
422
   }
423
 
424
   # build per-filesystem "from" and "to"
425
   my %from_for;
426
   my %to_for;
427
   foreach my $fs (keys %snaps_by_fs) {
428
      my $arr = $snaps_by_fs{$fs};
429
      next unless @$arr;
430
      $to_for{$fs} = $arr->[-1];
431
      $from_for{$fs} = $last_status_for{$fs};    # may be undef -> full send required
432
   }
433
 
434
   # decide if we can do a single recursive send:
435
   # condition: all 'to' snapshot names are identical
436
   my %to_names = map { $_ => 1 } values %to_for;
437
   my $single_to_name = (keys %to_names == 1) ? (keys %to_names)[0] : undef;
438
 
31 rodolico 439
   my %commands;
25 rodolico 440
 
441
   if ($single_to_name) {
442
      # check whether any from is missing
443
      my @from_values = map { $from_for{$_} } sort keys %from_for;
444
      my $any_from_missing = grep { !defined $_ } @from_values;
445
      my %from_names = map { $_ => 1 } grep { defined $_ } @from_values;
446
      my $single_from_name = (keys %from_names == 1) ? (keys %from_names)[0] : undef;
447
 
448
      if ($any_from_missing) {
449
         # full recursive send from root
31 rodolico 450
         $commands{'root_fs'} = sprintf('zfs send -R %s@%s', $root_fs, $single_to_name);
25 rodolico 451
      }
452
      elsif ($single_from_name) {
31 rodolico 453
         # incremental recursive send, but don't do it if they are the same
454
         $commands{$root_fs} = sprintf('zfs send -R -I %s@%s %s@%s',
455
                           $root_fs, $single_from_name, $root_fs, $single_to_name)
456
                           unless $single_from_name eq $single_to_name;
25 rodolico 457
      }
458
      else {
459
         # from snapshots differ across children -> fall back to per-filesystem sends
460
         foreach my $fs (sort keys %to_for) {
461
            my $to  = $to_for{$fs};
462
            my $from = $from_for{$fs};
463
            if ($from) {
31 rodolico 464
               # if from and to are different, add it
465
               $commands{$fs} = sprintf('zfs send -I %s@%s %s@%s', $fs, $from, $fs, $to)
466
                  unless $from eq $to;
25 rodolico 467
            } else {
31 rodolico 468
               $commands{$fs} = sprintf('zfs send %s@%s', $fs, $to);
25 rodolico 469
            }
470
         }
471
      }
472
 
473
      # update new status: record newest snap for every filesystem
474
      foreach my $fs (keys %to_for) {
475
         push @$newStatusRef, sprintf('%s@%s', $fs, $to_for{$fs});
476
      }
477
   } else {
478
      # not all children share same newest snap -> per-filesystem sends
479
      foreach my $fs (sort keys %to_for) {
480
         my $to  = $to_for{$fs};
481
         my $from = $from_for{$fs};
482
         if ($from) {
31 rodolico 483
            $commands{$fs} = sprintf('zfs send -I %s@%s %s@%s', $fs, $from, $fs, $to);
25 rodolico 484
         } else {
31 rodolico 485
            $commands{$fs} = sprintf('zfs send %s@%s', $fs, $to);
25 rodolico 486
         }
487
         push @$newStatusRef, sprintf('%s@%s', $fs, $to);
488
      }
489
   }
490
 
491
   # return arrayref of commands (caller can iterate or join with pipes)
31 rodolico 492
   return \%commands;
25 rodolico 493
}
494
 
495
 
24 rodolico 496
1;