]> git.ipfire.org Git - people/stevee/ipfire-2.x.git/commitdiff
filesystem-functions.pl: Add btrfs_subvolume_snapshot() function
authorStefan Schantl <stefan.schantl@ipfire.org>
Sun, 5 May 2024 14:54:06 +0000 (16:54 +0200)
committerStefan Schantl <stefan.schantl@ipfire.org>
Sun, 5 May 2024 14:55:14 +0000 (16:55 +0200)
Function which calls the btrfs binary to create a snapshot.

Signed-off-by: Stefan Schantl <stefan.schantl@ipfire.org>
config/cfgroot/filesystem-functions.pl

index 02c2e146f2444c8d4db24c8c30bc7fb04a12247b..d48cbe1f68a51b27729c6d78b532ed5b39e69c59 100644 (file)
@@ -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;