Prevent pool spinup for snapshot deletion

This commit is contained in:
2021-10-03 11:28:09 +02:00
parent 9d92d78c9e
commit 79bda932b3

View File

@@ -122,6 +122,9 @@ def written_since_last(fs, snap):
return res > 0
def get_pool(fs):
return fs.split('/')[0]
def main():
now = datetime.now(timezone.utc).replace(second=0, microsecond=0)
if verbose:
@@ -133,8 +136,8 @@ def main():
latest_snaps = find_latest_snapshots(existing_snaps)
fs_to_really_snap = [fs for fs in fs_to_snap if fs not in latest_snaps or
written_since_last(fs, latest_snaps[fs])]
pools = {get_pool(fs) for fs in fs_to_really_snap}
if fs_to_really_snap:
pools = {fs.split('/')[0] for fs in fs_to_really_snap}
snapname = 'zfs-smart-snap-' + now.strftime('%Y-%m-%d-%H%M')
snaps_to_create = [f'{fs}@{snapname}' for fs in fs_to_really_snap]
if dry_run or verbose:
@@ -154,6 +157,12 @@ def main():
if existing_snaps[fs][-1] not in to_keep:
print(f"Warning: Latest snap {fs}@{existing_snaps[fs][-1]} not preserved by retention policy, keeping anyways", file=sys.stderr)
to_keep.add(existing_snaps[fs][-1])
# Don't cleanup old snapshots if no new snapshots were created on the pool (to prevent disk spinup only for snapshot deletion)
# The old snapshots will be cleaned once any dataset on the pool is changed and a new snapshot is created.
if get_pool(fs) not in pools:
if verbose:
print(f"Skipping snapshot cleaning for {fs} because pool {get_pool(fs)} was not written")
continue
to_delete += (f'{fs}@{snap}' for snap in set(existing_snaps[fs]).difference(to_keep))
if not dry_run: