From 5ba483c68a0c613dbf31a70f432db52acb700c1f Mon Sep 17 00:00:00 2001 From: Ben Vincent Date: Sat, 18 Oct 2025 21:24:33 +1100 Subject: [PATCH] feat: add ZFS facts to prevent zpool disk changes (#410) - add zfs_zpools and zfs_datasets facts to detect existing ZFS resources - skip zpool creation when pools already exist Reviewed-on: https://git.unkin.net/unkin/puppet-prod/pulls/410 --- modules/zfs/lib/facter/zfs_datasets.rb | 19 +++++++++++++++++++ modules/zfs/lib/facter/zfs_zpools.rb | 19 +++++++++++++++++++ modules/zfs/manifests/init.pp | 7 +++++-- 3 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 modules/zfs/lib/facter/zfs_datasets.rb create mode 100644 modules/zfs/lib/facter/zfs_zpools.rb diff --git a/modules/zfs/lib/facter/zfs_datasets.rb b/modules/zfs/lib/facter/zfs_datasets.rb new file mode 100644 index 0000000..ebc01cd --- /dev/null +++ b/modules/zfs/lib/facter/zfs_datasets.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +Facter.add(:zfs_datasets) do + confine kernel: 'Linux' + setcode do + datasets = [] + + if Facter::Core::Execution.which('zfs') + begin + output = Facter::Core::Execution.execute('zfs list -H -o name 2>/dev/null', on_fail: nil) + datasets = output.strip.split("\n") if output && !output.empty? + rescue StandardError => e + Facter.debug("Error getting zfs dataset information: #{e.message}") + end + end + + datasets.empty? ? nil : datasets + end +end diff --git a/modules/zfs/lib/facter/zfs_zpools.rb b/modules/zfs/lib/facter/zfs_zpools.rb new file mode 100644 index 0000000..000a653 --- /dev/null +++ b/modules/zfs/lib/facter/zfs_zpools.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +Facter.add(:zfs_zpools) do + confine kernel: 'Linux' + setcode do + zpools = [] + + if Facter::Core::Execution.which('zpool') + begin + output = Facter::Core::Execution.execute('zpool list -H -o name 2>/dev/null', on_fail: nil) + zpools = output.strip.split("\n") if output && !output.empty? + rescue StandardError => e + Facter.debug("Error getting zpool information: #{e.message}") + end + end + + zpools.empty? ? nil : zpools + end +end diff --git a/modules/zfs/manifests/init.pp b/modules/zfs/manifests/init.pp index 6feeeed..79f95e3 100644 --- a/modules/zfs/manifests/init.pp +++ b/modules/zfs/manifests/init.pp @@ -38,8 +38,11 @@ class zfs ( # create zpools $zpools.each | $zpool, $data | { - zpool { $zpool: - * => $data + # Only create zpool if it doesn't already exist + if $facts['zfs_zpools'] == undef or !($zpool in $facts['zfs_zpools']) { + zpool { $zpool: + * => $data + } } }