Merge pull request #463 from lopsided98/sanoid-cache-file

Move sanoid cache and lock files to subdirectories
This commit is contained in:
Jim Salter 2020-01-22 10:50:33 -05:00 committed by GitHub
commit fee3739110
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 12 deletions

View File

@ -49,6 +49,14 @@ Which would be enough to tell sanoid to take and keep 36 hourly snapshots, 30 da
Specify a location for the config file named sanoid.conf. Defaults to /etc/sanoid Specify a location for the config file named sanoid.conf. Defaults to /etc/sanoid
+ --cache-dir
Specify a directory to store the zfs snapshot cache. Defaults to /var/cache/sanoid
+ --run-dir
Specify a directory for temporary files such as lock files. Defaults to /var/run/sanoid
+ --take-snapshots + --take-snapshots
This will process your sanoid.conf file, create snapshots, but it will NOT purge expired ones. (Note that snapshots taken are atomic in an individual dataset context, <i>not</i> a global context - snapshots of pool/dataset1 and pool/dataset2 will each be internally consistent and atomic, but one may be a few filesystem transactions "newer" than the other.) This will process your sanoid.conf file, create snapshots, but it will NOT purge expired ones. (Note that snapshots taken are atomic in an individual dataset context, <i>not</i> a global context - snapshots of pool/dataset1 and pool/dataset2 will each be internally consistent and atomic, but one may be a few filesystem transactions "newer" than the other.)

37
sanoid
View File

@ -11,15 +11,20 @@ use strict;
use warnings; use warnings;
use Config::IniFiles; # read samba-style conf file use Config::IniFiles; # read samba-style conf file
use Data::Dumper; # debugging - print contents of hash use Data::Dumper; # debugging - print contents of hash
use File::Path; # for rmtree command in use_prune use File::Path 'make_path';
use Getopt::Long qw(:config auto_version auto_help); use Getopt::Long qw(:config auto_version auto_help);
use Pod::Usage; # pod2usage use Pod::Usage; # pod2usage
use Time::Local; # to parse dates in reverse use Time::Local; # to parse dates in reverse
use Capture::Tiny ':all'; use Capture::Tiny ':all';
my %args = ("configdir" => "/etc/sanoid"); my %args = (
"configdir" => "/etc/sanoid",
"cache-dir" => "/var/cache/sanoid",
"run-dir" => "/var/run/sanoid"
);
GetOptions(\%args, "verbose", "debug", "cron", "readonly", "quiet", GetOptions(\%args, "verbose", "debug", "cron", "readonly", "quiet",
"monitor-health", "force-update", "configdir=s", "configdir=s", "cache-dir=s", "run-dir=s",
"monitor-health", "force-update",
"monitor-snapshots", "take-snapshots", "prune-snapshots", "force-prune", "monitor-snapshots", "take-snapshots", "prune-snapshots", "force-prune",
"monitor-capacity" "monitor-capacity"
) or pod2usage(2); ) or pod2usage(2);
@ -41,9 +46,15 @@ my $default_conf_file = "$args{'configdir'}/sanoid.defaults.conf";
# parse config file # parse config file
my %config = init($conf_file,$default_conf_file); my %config = init($conf_file,$default_conf_file);
my $cache_dir = $args{'cache-dir'};
my $run_dir = $args{'run-dir'};
make_path($cache_dir);
make_path($run_dir);
# if we call getsnaps(%config,1) it will forcibly update the cache, TTL or no TTL # if we call getsnaps(%config,1) it will forcibly update the cache, TTL or no TTL
my $forcecacheupdate = 0; my $forcecacheupdate = 0;
my $cache = '/var/cache/sanoidsnapshots.txt'; my $cache = "$cache_dir/snapshots.txt";
my $cacheTTL = 900; # 15 minutes my $cacheTTL = 900; # 15 minutes
my %snaps = getsnaps( \%config, $cacheTTL, $forcecacheupdate ); my %snaps = getsnaps( \%config, $cacheTTL, $forcecacheupdate );
my %pruned; my %pruned;
@ -1365,10 +1376,10 @@ sub get_zpool_capacity {
sub checklock { sub checklock {
# take argument $lockname. # take argument $lockname.
# #
# read /var/run/$lockname.lock for a pid on first line and a mutex on second line. # read $run_dir/$lockname.lock for a pid on first line and a mutex on second line.
# #
# check process list to see if the pid from /var/run/$lockname.lock is still active with # check process list to see if the pid from $run_dir/$lockname.lock is still active with
# the original mutex found in /var/run/$lockname.lock. # the original mutex found in $run_dir/$lockname.lock.
# #
# return: # return:
# 0 if lock is present and valid for another process # 0 if lock is present and valid for another process
@ -1380,7 +1391,7 @@ sub checklock {
# #
my $lockname = shift; my $lockname = shift;
my $lockfile = "/var/run/$lockname.lock"; my $lockfile = "$run_dir/$lockname.lock";
if (! -e $lockfile) { if (! -e $lockfile) {
# no lockfile # no lockfile
@ -1437,11 +1448,11 @@ sub checklock {
sub removelock { sub removelock {
# take argument $lockname. # take argument $lockname.
# #
# make sure /var/run/$lockname.lock actually belongs to me (contains my pid and mutex) # make sure $run_dir/$lockname.lock actually belongs to me (contains my pid and mutex)
# and remove it if it does, die if it doesn't. # and remove it if it does, die if it doesn't.
my $lockname = shift; my $lockname = shift;
my $lockfile = "/var/run/$lockname.lock"; my $lockfile = "$run_dir/$lockname.lock";
if (checklock($lockname) == 2) { if (checklock($lockname) == 2) {
unlink $lockfile; unlink $lockfile;
@ -1456,11 +1467,11 @@ sub removelock {
sub writelock { sub writelock {
# take argument $lockname. # take argument $lockname.
# #
# write a lockfile to /var/run/$lockname.lock with first line # write a lockfile to $run_dir/$lockname.lock with first line
# being my pid and second line being my mutex. # being my pid and second line being my mutex.
my $lockname = shift; my $lockname = shift;
my $lockfile = "/var/run/$lockname.lock"; my $lockfile = "$run_dir/$lockname.lock";
# die honorably rather than overwriting a valid, existing lock # die honorably rather than overwriting a valid, existing lock
if (! checklock($lockname)) { if (! checklock($lockname)) {
@ -1663,6 +1674,8 @@ Assumes --cron --verbose if no other arguments (other than configdir) are specif
Options: Options:
--configdir=DIR Specify a directory to find config file sanoid.conf --configdir=DIR Specify a directory to find config file sanoid.conf
--cache-dir=DIR Specify a directory to store the zfs snapshot cache
--run-dir=DIR Specify a directory for temporary files such as lock files
--cron Creates snapshots and purges expired snapshots --cron Creates snapshots and purges expired snapshots
--verbose Prints out additional information during a sanoid run --verbose Prints out additional information during a sanoid run