Add overlay for roll_dataset

Signed-off-by: magic_rb <richard@brezak.sk>
This commit is contained in:
magic_rb 2023-10-07 22:40:05 +02:00
parent a4156b2309
commit 706d3bb1b1
No known key found for this signature in database
GPG key ID: 08D5287CC5DDCA0E
4 changed files with 102 additions and 0 deletions

View file

@ -103,6 +103,7 @@
overlays/itp overlays/itp
overlays/virtiofsd-zfs overlays/virtiofsd-zfs
overlays/show-files-to-be-deleted overlays/show-files-to-be-deleted
overlays/rolling_datasets
inputs.uterranix.flakeModule inputs.uterranix.flakeModule
]; ];

View file

@ -0,0 +1,28 @@
# -*- mode: shell-script -*-
source @out@/share/functions/get_old_datasets
if [[ "$#" != 3 ]] ; then
echo "This script requires precisely 2 arguments!"
exit 1
fi
dataset="$1"
generation="$(zfs get :generation "$dataset" -H -o value)"
generation="$(("$generation" + 1))"
zfs set ":generation=$generation" "$dataset"
zfs send "$dataset" | zfs recv "$dataset/$generation"
zfs rollback "$dataset@blank" && echo "rollback complete"
while IFS="\n" read gen
do
printf "$gen < $(("$generation" - 10)) => "
if [ "$gen" -lt "$(("$generation" - 10))" ]; then
zfs destroy -r "$dataset/$gen"
echo "destroyed"
else
echo "kept"
fi
done <<< "$(get_old_datasets "$dataset")"

View file

@ -0,0 +1,67 @@
{ inputs, ... }:
{
flake.overlays.rolling_datasets =
final: prev:
let
writeTextFile =
{ name # the name of the derivation
, text
, executable ? false # run chmod +x ?
, destination ? "" # relative path appended to $out eg "/bin/foo"
, checkPhase ? "" # syntax checks, e.g. for scripts
, env ? {}
}:
final.runCommand name
({ inherit text executable;
passAsFile = [ "text" ];
# Pointless to do this on a remote machine.
preferLocalBuild = true;
allowSubstitutes = false;
} // env)
''
n=$out${destination}
mkdir -p "$(dirname "$n")"
if [ -e "$textPath" ]; then
mv "$textPath" "$n"
else
echo -n "$text" > "$n"
fi
${checkPhase}
(test -n "$executable" && chmod +x "$n") || true
'';
writeShellScriptBin = name : text : env :
writeTextFile {
inherit name;
executable = true;
destination = "/bin/${name}";
text = ''
#!${final.runtimeShell}
${text}
'';
checkPhase = ''
${final.stdenv.shell} -n $out/bin/${name}
'';
};
in
{
rolling_datasets =
final.runCommand "rolling_datasets" {
} ''
mkdir -p $out/bin
mkdir -p $out/share/functions
for binary in ${./bin}/* ; do
substituteAll "$binary" "$out/bin/$(basename "$binary")"
done
for function in ${./functions}/* ; do
substituteAll "$function" "$out/share/functions/$(basename "$function")"
done
'';
};
}

View file

@ -0,0 +1,6 @@
# -*- mode: shell-script -*-
function get_old_datasets()
{
dataset="$1"
zfs list "$dataset" -t filesystem -r -H -o name | xargs -I {} ${final.runtimeShell} -c 'echo "$1" | rev | cut -f 1 -d "/" | rev' sh {} | grep -v "$dataset"
}