From 3483ed4fbbfcfa2c6a981442f2d4f2b4eb02e56e Mon Sep 17 00:00:00 2001 From: Ron Ellis Date: Fri, 20 Apr 2018 19:17:53 -0700 Subject: [PATCH] Check if a given ZFS attribute is already set on the filesystem before calling 'zfs set ...'. This is necessary because zfs set will attempt to set, for example, mountpoint even if the filesystem is already mounted at the correct location. --- metadata.rb | 2 +- resources/default.rb | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/metadata.rb b/metadata.rb index 5319db9..93a67c1 100644 --- a/metadata.rb +++ b/metadata.rb @@ -4,7 +4,7 @@ license 'Apache-2.0' description 'Manage ZFS' long_description IO.read(File.join(File.dirname(__FILE__), 'README.md')) -version '1.0.1' +version '1.0.14' source_url 'https://github.com/chef-cookbooks/chef_zfs' issues_url 'https://github.com/chef-cookbooks/chef_zfs/issues' diff --git a/resources/default.rb b/resources/default.rb index b300ac3..06b32ff 100644 --- a/resources/default.rb +++ b/resources/default.rb @@ -89,6 +89,21 @@ def zfs_get_properties(name) cmd.stdout end +# Helper method to check if a filesystem property is already set +# so that we don't call zfs set when it's unnecessary. +# @param [String] ZFS Name +# @param [Hash] ZFS Property => value +# @return [Boolean] +def zfs_property_already_set?(fs, property) + key = property.keys[0] + new_value = property[property.keys[0]] + cmd = Mixlib::ShellOut.new('zfs', 'get', key.to_s, fs) + cmd.run_command + existing_property = parse_zfs_properties(cmd.stdout)[0] + existing_value = existing_property[existing_property.keys[0]] + existing_value == new_value +end + # TODO: Add support for setting inheritance from parent filesystems. # @param [String] ZFS Name # @param [Hash] ZFS Property => value @@ -102,6 +117,7 @@ def zfs_set_properties(fs, properties) configurable_properties.each do |setting| next if PROPERTIES_VALID_ONLY_AT_CREATE.include?(setting.keys[0]) + next if zfs_property_already_set?(fs, setting) cmd = Mixlib::ShellOut.new('zfs', 'set', "#{setting.keys[0]}=#{setting[setting.keys[0]]}", fs) cmd.environment['PATH'] = "/usr/sbin:#{ENV['PATH']}" if platform_family?('solaris2') cmd.run_command