Skip to content

Commit

Permalink
internal/exec/util: check if unit exists before disabling
Browse files Browse the repository at this point in the history
Ignition depends on `systemctl disable` for disabling units. Currently
if the unit does not exist `systemctl disable` exits 1; however, before
252 `systemctrl disable` exits 0 if `--root` is specified. Since Ignition
depends on systemctrl's exit code this change caused a regression, and causing
the unit.remove.symlinks blackbox test to fail with:

    removing enablement symlink(s) for "enoent.service": cannot remove symlink(s) for enoent.service: exit status 1: "Failed to disable unit, unit enoent.service does not exist.\n"

Before disabling a unit, use `systemctl is-enabled` to verify that the
unit exists and is enabled.

Fixes coreos#1614.
  • Loading branch information
prestist committed May 9, 2023
1 parent 3288c5d commit 08741d9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Starting with this release, ignition-validate binaries are signed with the
- Document that `hash` fields describe decompressed data
- Clarify documentation of `passwordHash` fields
- Correctly document Tang `advertisement` field as optional
- Fix failure disabling nonexistent unit with systemd ≥ 252

### Test changes

Expand Down
6 changes: 6 additions & 0 deletions internal/exec/util/unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@ func (ut Util) EnableUnit(enabledUnit string) error {
}

func (ut Util) DisableUnit(disabledUnit string) error {
// check if the unit is currently enabled to see if we need to disable it
// if it's not enabled or does not exist, we don't need to do anything
args := []string{"--root", ut.DestDir, "is-enabled", disabledUnit}
if err := exec.Command(distro.SystemctlCmd(), args...).Run(); err != nil {
return nil
}
// We need to delete any enablement symlinks for a unit before sending it to a
// preset directive. This will help to disable that unit completely.
// For more information: https://github.com/coreos/fedora-coreos-tracker/issues/392
Expand Down

0 comments on commit 08741d9

Please sign in to comment.