From a9267a4ec624df9cb95918def9c42f76b44de474 Mon Sep 17 00:00:00 2001 From: Stefan Schantl Date: Sun, 5 May 2024 16:54:06 +0200 Subject: [PATCH] filesystem-functions.pl: Add btrfs_subvolume_snapshot() function Function which calls the btrfs binary to create a snapshot. Signed-off-by: Stefan Schantl --- config/cfgroot/filesystem-functions.pl | 33 ++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/config/cfgroot/filesystem-functions.pl b/config/cfgroot/filesystem-functions.pl index 02c2e146f..d48cbe1f6 100644 --- a/config/cfgroot/filesystem-functions.pl +++ b/config/cfgroot/filesystem-functions.pl @@ -37,6 +37,9 @@ my $uuid_dir = "/dev/disk/by-uuid"; # Location where the btrfs binary lives. my $btrfs_bin = "/usr/bin/btrfs"; +# Directory where BTRFS snapshots will be stored. +my $btrfs_snapshot_dir = "/.snapshots/"; + # ## Function which returns a lot of details about the known devices ## as a hash of hashes. @@ -581,4 +584,34 @@ sub btrfs_subvolume_list ($) { return @output; } +# +## Function which calls the subvolume snapshot feature of the btrfs tool to create a snapshot. +## +## Possible argument: +## name: The name of the snapshot +## source: If it is different than "/" +## target: If it is different than the defined "$btrfs_snapshot_dir" +# +sub btrfs_subvolume_snapshot ($$$) { + my ($name, $source, $target) = @_; + + # Default to "/" if no source has been given. + $source //= "/"; + + # Default to snapshot dir if no target has been specified. + $target //= "$btrfs_snapshot_dir"; + + # Generate destination path. + my $destination = "$target" . "$name"; + + # Call the btrfs binary to create the snapshot. + my $ret = &General::system("$btrfs_bin", "subvolume", "snapshot", "$source", "$destination"); + + # Return the error code if there was one. + return $ret if($ret); + + # Return nothing on success. + return; +} + 1; -- 2.39.5