Skip to content

Commit

Permalink
Merge pull request #485 from deric/udev
Browse files Browse the repository at this point in the history
Reload udev rules after change
  • Loading branch information
kenyon authored Aug 27, 2024
2 parents 7e2e187 + 5ad232f commit 2b232ef
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 2 deletions.
9 changes: 9 additions & 0 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ The following parameters are available in the `systemd` class:
* [`udev_resolve_names`](#-systemd--udev_resolve_names)
* [`udev_timeout_signal`](#-systemd--udev_timeout_signal)
* [`udev_rules`](#-systemd--udev_rules)
* [`udev_reload`](#-systemd--udev_reload)
* [`machine_info_settings`](#-systemd--machine_info_settings)
* [`manage_logind`](#-systemd--manage_logind)
* [`logind_settings`](#-systemd--logind_settings)
Expand Down Expand Up @@ -525,6 +526,14 @@ Config Hash that is used to generate instances of our

Default value: `{}`

##### <a name="-systemd--udev_reload"></a>`udev_reload`

Data type: `Boolean`

Whether udev rules should be automatically reloaded upon change.

Default value: `false`

##### <a name="-systemd--machine_info_settings"></a>`machine_info_settings`

Data type: `Systemd::MachineInfoSettings`
Expand Down
4 changes: 4 additions & 0 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@
# Config Hash that is used to generate instances of our
# `udev::rule` define.
#
# @param udev_reload
# Whether udev rules should be automatically reloaded upon change.
#
# @param machine_info_settings
# Settings to place into /etc/machine-info (hostnamectl)
#
Expand Down Expand Up @@ -261,6 +264,7 @@
Optional[Integer] $udev_event_timeout = undef,
Optional[Enum['early', 'late', 'never']] $udev_resolve_names = undef,
Optional[Variant[Integer,String]] $udev_timeout_signal = undef,
Boolean $udev_reload = false,
Boolean $manage_logind = false,
Systemd::LogindSettings $logind_settings = {},
Boolean $manage_all_network_files = false,
Expand Down
12 changes: 11 additions & 1 deletion manifests/udev/rule.pp
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,22 @@
fail("systemd::udev::rule - ${name}: param rules is empty, you need to pass rules")
}

$_notify = if $systemd::udev_reload {
if $notify_services =~ Array {
$notify_services << 'Exec[systemd-udev_reload]'
} else {
[$notify_services, 'Exec[systemd-udev_reload]']
}
} else {
$notify_services
}

file { join([$path, $name], '/'):
ensure => $ensure,
owner => 'root',
group => 'root',
mode => '0444',
notify => $notify_services,
notify => $_notify,
selinux_ignore_defaults => $selinux_ignore_defaults,
content => epp("${module_name}/udev_rule.epp", { 'rules' => $rules }),
}
Expand Down
6 changes: 6 additions & 0 deletions manifests/udevd.pp
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,10 @@
* => $udev_rule,
}
}

exec { 'systemd-udev_reload':
command => 'udevadm control --reload-rules && udevadm trigger',
refreshonly => true,
path => $facts['path'],
}
}
5 changes: 5 additions & 0 deletions spec/classes/init_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,7 @@
let(:params) do
{
manage_udevd: true,
udev_reload: true,
udev_log: 'daemon',
udev_children_max: 1,
udev_exec_delay: 2,
Expand All @@ -664,6 +665,7 @@
{
manage_udevd: true,
udev_purge_rules: true,
udev_reload: true,
}
end

Expand Down Expand Up @@ -701,6 +703,9 @@
'ACTION=="add", KERNEL=="sdb", RUN+="/bin/raw /dev/raw/raw2 %N"',
])
}

it { is_expected.to contain_exec('systemd-udev_reload') }
it { is_expected.to contain_file('/etc/udev/rules.d/example_raw.rules').that_notifies('Exec[systemd-udev_reload]') }
end

context 'with machine-info' do
Expand Down
19 changes: 18 additions & 1 deletion spec/defines/udev_rules_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,24 @@ class { 'systemd': manage_udevd => true, manage_journald => false }
it { is_expected.to contain_file("/etc/udev/rules.d/#{title}").with_ensure('absent').with_notify([]) }
end

describe 'ensured absent with notify' do
describe 'ensured absent with automatic reload' do
let(:pre_condition) do
<<-PUPPET
class { 'systemd': manage_udevd => true, manage_journald => false, udev_reload => true }
PUPPET
end
let(:params) do
{
ensure: 'absent',
}
end

it { is_expected.to compile.with_all_deps }

it { is_expected.to contain_file("/etc/udev/rules.d/#{title}").with_ensure('absent').with_notify(['Exec[systemd-udev_reload]']) }
end

describe 'ensured absent with custom notify' do
let(:params) { { ensure: 'absent', notify_services: 'Service[systemd-udevd]', } }

it { is_expected.to compile.with_all_deps }
Expand Down

0 comments on commit 2b232ef

Please sign in to comment.