Fix runlevel fact on distros without /sbin/runlevel#339
Conversation
runlevel fact on distros without /sbin/runlevel
op-ct
left a comment
There was a problem hiding this comment.
systemctl get-default isn't a good equivalent for runlevel; see inline comments.
| 'graphical.target' => '5', | ||
| 'reboot.target' => '6', | ||
| } | ||
| result = Facter::Core::Execution.exec('systemctl get-default') |
There was a problem hiding this comment.
systemctl get-default will return the default boot target/"runlevel", but that's not necessarily its current state (which could include more than one runlevel-mapped target simultaneously; see "Q: How do I figure out the current runlevel?" at https://systemd.io/FAQ/ ).
A better way to approximate the number returned by runlevel in systemd would be to check the targets in target_to_runlevel from highest value to lowest, stopping at the first (highest-numbered) target that is currently active. So return 3 if multi-user.target is active and graphical.target is not. This is consistent with the behavior described in the runlevel man page.
- Use Facter::Core::Execution.which to check for runlevel binary - Fall back to systemctl get-default with target→runlevel mapping - Use Facter::Core::Execution.exec instead of backticks - Add unit tests covering all scenarios Agent-Logs-Url: https://github.com/simp/pupmod-simp-simplib/sessions/7a97386a-2830-4d7b-b32a-861a349ec7bd Co-authored-by: silug <206992+silug@users.noreply.github.com>
2720e19 to
4964507
Compare
On newer distros (Fedora 43+),
/sbin/runlevelno longer exists, causing facter/openfact to emitsh: line 1: /sbin/runlevel: No such file or directoryon every run.Changes
lib/facter/runlevel.rb: Replace hardcoded backtick call to/sbin/runlevelwith a two-stage resolution:Facter::Core::Execution.which('runlevel')— if found, exec it (existing behaviour, now safe)systemctl get-defaultand map the returned systemd target to the equivalent SysV runlevel; returnsnilif neither binary is presentpoweroff.target0rescue.target1multi-user.target3graphical.target5reboot.target6spec/unit/facter/runlevel_spec.rb: New unit tests covering therunlevel-present path, all mapped systemd targets, an unmapped target, and the no-binary-available case.