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
- Check disk space (df -h) and free space on the node
- Check for immutable flags: lsattr <unit-file>; chattr -i if set
- Verify the systemd unit directory is writable by the nodeup user (usually root)
- If the path is a directory, remove it and re-run nodeup
- 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
- Keep free disk space headroom on nodes
- Never chattr +i files nodeup manages
- Ensure /etc/systemd/system is writable by the nodeup user
- Use supported base images with standard layouts
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
- Error reading systemd file %q: %v
- error writing temp file: %v
- path: %s already exists but is not a directory
- error building kubelet flags: %v
- failed to read %s: %w
AI-assisted analysis of kubernetes/kops@4c8573c808 (2026-09-05).
Data as JSON: /api/errors/ada52f08a3b7d656.
Report an issue: GitHub.