Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add journalctl logs to failed systemctl commands #17659

Merged
merged 1 commit into from
Nov 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions pkg/minikube/sysinit/systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (s *Systemd) Enable(svc string) error {
return errors.New("please don't enable kubelet as it creates a race condition; if it starts on systemd boot it will pick up /etc/hosts before we have time to configure /etc/hosts")
}
_, err := s.r.RunCmd(exec.Command("sudo", "systemctl", "enable", svc))
return err
return s.appendJournalctlLogsOnFailure(svc, err)
}

// EnableNow enables a service and then activates it too (not waiting for next start)
Expand All @@ -87,7 +87,7 @@ func (s *Systemd) EnableNow(svc string) error {
return errors.New("please don't enable kubelet as it creates a race condition; if it starts on systemd boot it will pick up /etc/hosts before we have time to configure /etc/hosts")
}
_, err := s.r.RunCmd(exec.Command("sudo", "systemctl", "enable", "--now", svc))
return err
return s.appendJournalctlLogsOnFailure(svc, err)
}

// Unmask allows a service to be started
Expand All @@ -102,7 +102,7 @@ func (s *Systemd) Start(svc string) error {
return err
}
_, err := s.r.RunCmd(exec.Command("sudo", "systemctl", "start", svc))
return err
return s.appendJournalctlLogsOnFailure(svc, err)
}

// Restart restarts a service
Expand All @@ -111,7 +111,7 @@ func (s *Systemd) Restart(svc string) error {
return err
}
_, err := s.r.RunCmd(exec.Command("sudo", "systemctl", "restart", svc))
return err
return s.appendJournalctlLogsOnFailure(svc, err)
}

// Reload reloads a service
Expand All @@ -120,13 +120,13 @@ func (s *Systemd) Reload(svc string) error {
return err
}
_, err := s.r.RunCmd(exec.Command("sudo", "systemctl", "reload", svc))
return err
return s.appendJournalctlLogsOnFailure(svc, err)
}

// Stop stops a service
func (s *Systemd) Stop(svc string) error {
_, err := s.r.RunCmd(exec.Command("sudo", "systemctl", "stop", svc))
return err
return s.appendJournalctlLogsOnFailure(svc, err)
}

// ForceStop terminates a service with prejudice
Expand All @@ -151,3 +151,16 @@ func usesSystemd(r Runner) bool {
_, err := r.RunCmd(exec.Command("systemctl", "--version"))
return err == nil
}

// appendJournalctlLogsOnFailure appends journalctl logs for the service to the error if err is not nil
func (s *Systemd) appendJournalctlLogsOnFailure(svc string, err error) error {
if err == nil {
return nil
}
rr, logErr := s.r.RunCmd(exec.Command("sudo", "journalctl", "--no-pager", "-u", svc))
if logErr != nil {
return fmt.Errorf("%v\nfailed to get journalctl logs: %v", err, logErr)
}

return fmt.Errorf("%v\n%s:\n%s", err, rr.Command(), rr.Output())
}
Loading