kubernetes/kops · error

error writing systemd service file: %v

Error message

error writing systemd service file: %v

What it means

When a service definition changes, RenderLocal writes the new unit file with fi.WriteFile and then reloads systemd. If writing the unit file fails, the change cannot be applied and this error is returned, aborting the task so systemd never sees a half-applied definition.

Source

Thrown at upup/pkg/fi/nodeup/nodetasks/service.go:328

	}

	serviceName := e.Name

	action := ""

	if changes.Running != nil && fi.ValueOf(e.ManageState) {
		if fi.ValueOf(e.Running) {
			action = "restart"
		} else {
			action = "stop"
		}
	}

	if changes.Definition != nil {
		servicePath := path.Join(systemdSystemPath, serviceName)
		err := fi.WriteFile(servicePath, fi.NewStringResource(*e.Definition), 0o644, 0o755, "", "")
		if err != nil {
			return fmt.Errorf("error writing systemd service file: %v", err)
		}

		klog.Infof("Reloading systemd configuration")
		cmd := exec.Command("systemctl", "daemon-reload")
		output, err := cmd.CombinedOutput()
		if err != nil {
			return fmt.Errorf("error doing systemd daemon-reload: %v\nOutput: %s", err, output)
		}
	}

	// "SmartRestart" - look at the obvious dependencies in the systemd service, restart if start time older
	if fi.ValueOf(e.ManageState) && fi.ValueOf(e.SmartRestart) {
		definition := fi.ValueOf(e.Definition)
		if definition == "" && a != nil {
			definition = fi.ValueOf(a.Definition)
		}

		if action == "" && fi.ValueOf(e.Running) && definition != "" {

View on GitHub (pinned to 4c8573c808)

Solutions

  1. Check disk space (df -h) and free space on the node
  2. Check for immutable flags: lsattr <unit-file>; chattr -i if set
  3. Verify the systemd unit directory is writable by the nodeup user (usually root)
  4. If the path is a directory, remove it and re-run nodeup
  5. Reboot or remount the node's filesystem if it is read-only due to disk errors

Example fix

# before
$ df -h /etc
/dev/root  100%  ...  # disk full
# after: free space (e.g. clean logs), then re-run nodeup
$ journalctl --vacuum-size=50M
Defensive patterns

Strategy: validation

Validate before calling

// Before nodeup, on the node:
df -h /etc && sudo test -w /etc/systemd/system && echo writable || echo 'not writable'
lsattr /etc/systemd/system/<unit> 2>/dev/null | grep -q i && echo 'immutable!' || true

Try / catch

if err := task.RenderLocal(target, a, e, changes); err != nil {
	if strings.Contains(err.Error(), "error writing systemd service file") {
		return fmt.Errorf("check disk space, immutable flags, and /etc/systemd/system perms: %w", err)
	}
	return err
}

Prevention

When it happens

Trigger: fi.WriteFile(servicePath, ...) fails: /etc/systemd/system (or distro-specific dir) is read-only or full, the unit path is a directory, or the parent dir has wrong permissions for the nodeup user.

Common situations: Node disk full (no space left on device), immutable unit file (chattr +i left from hardening), read-only /usr on some images, or COS/Flatcar layouts where the target dir differs from expectations.

Related errors


AI-assisted analysis of kubernetes/kops@4c8573c808 (2026-09-05). Data as JSON: /api/errors/ada52f08a3b7d656. Report an issue: GitHub.