Skip to content

Commit e9ebc2d

Browse files
committed
dmesg
1 parent 10a48a6 commit e9ebc2d

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
// This Source Code Form is subject to the terms of the Mozilla Public
2+
// License, v. 2.0. If a copy of the MPL was not distributed with this
3+
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4+
5+
package runtime
6+
7+
import (
8+
"context"
9+
"fmt"
10+
"io"
11+
"time"
12+
13+
"github.com/cosi-project/runtime/pkg/controller"
14+
"github.com/siderolabs/go-kmsg"
15+
"go.uber.org/zap"
16+
17+
"github.com/siderolabs/talos/internal/app/machined/pkg/runtime"
18+
)
19+
20+
// KmsgLogStorageController watches events and forwards them to the system logger.
21+
type KmsgLogStorageController struct {
22+
Drainer *runtime.Drainer
23+
V1Alpha1Logging runtime.LoggingManager
24+
25+
drainSub *runtime.DrainSubscription
26+
logWriter io.WriteCloser
27+
}
28+
29+
// Name implements controller.Controller interface.
30+
func (ctrl *KmsgLogStorageController) Name() string {
31+
return "runtime.KmsgLogStorageController"
32+
}
33+
34+
// Inputs implements controller.Controller interface.
35+
func (ctrl *KmsgLogStorageController) Inputs() []controller.Input {
36+
return nil
37+
}
38+
39+
// Outputs implements controller.Controller interface.
40+
func (ctrl *KmsgLogStorageController) Outputs() []controller.Output {
41+
return nil
42+
}
43+
44+
// Run implements controller.Controller interface.
45+
func (ctrl *KmsgLogStorageController) Run(ctx context.Context, r controller.Runtime, logger *zap.Logger) error {
46+
var err error
47+
48+
ctrl.logWriter, err = ctrl.V1Alpha1Logging.ServiceLog("kernel").Writer()
49+
if err != nil {
50+
return fmt.Errorf("error opening logger: %w", err)
51+
}
52+
53+
// initilalize kmsg reader early, so that we don't lose position on config changes
54+
reader, err := kmsg.NewReader(kmsg.Follow())
55+
if err != nil {
56+
return fmt.Errorf("error reading kernel messages: %w", err)
57+
}
58+
59+
defer reader.Close() //nolint:errcheck
60+
61+
kmsgCh := reader.Scan(ctx)
62+
63+
for {
64+
select {
65+
case <-ctx.Done():
66+
return nil
67+
case <-r.EventCh():
68+
}
69+
70+
if err = ctrl.deliverLogs(ctx, r, kmsgCh); err != nil {
71+
return fmt.Errorf("error delivering logs: %w", err)
72+
}
73+
74+
r.ResetRestartBackoff()
75+
}
76+
}
77+
78+
//nolint:gocyclo
79+
func (ctrl *KmsgLogStorageController) deliverLogs(ctx context.Context, r controller.Runtime, kmsgCh <-chan kmsg.Packet) error {
80+
if ctrl.drainSub == nil {
81+
ctrl.drainSub = ctrl.Drainer.Subscribe()
82+
}
83+
84+
var (
85+
drainTimer *time.Timer
86+
drainTimerCh <-chan time.Time
87+
)
88+
89+
for {
90+
var msg kmsg.Packet
91+
92+
select {
93+
case <-ctx.Done():
94+
ctrl.drainSub.Cancel()
95+
96+
return nil
97+
case <-r.EventCh():
98+
// config changed, restart the loop
99+
return nil
100+
case <-ctrl.drainSub.EventCh():
101+
// drain started, assume that ksmg is drained if there're no new messages in drainTimeout
102+
drainTimer = time.NewTimer(drainTimeout)
103+
drainTimerCh = drainTimer.C
104+
105+
continue
106+
case <-drainTimerCh:
107+
ctrl.drainSub.Cancel()
108+
109+
return nil
110+
case msg = <-kmsgCh:
111+
if drainTimer != nil {
112+
// if draining, reset the timer as there's a new message
113+
if !drainTimer.Stop() {
114+
<-drainTimer.C
115+
}
116+
117+
drainTimer.Reset(drainTimeout)
118+
}
119+
}
120+
121+
if msg.Err != nil {
122+
return fmt.Errorf("error receiving kernel logs: %w", msg.Err)
123+
}
124+
125+
ctrl.logWriter.Write([]byte(
126+
msg.Message.Timestamp.String() + ": " + msg.Message.Facility.String() + ": " + msg.Message.Message,
127+
))
128+
}
129+
}

internal/app/machined/pkg/runtime/v1alpha2/v1alpha2_controller.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,10 @@ func (ctrl *Controller) Run(ctx context.Context, drainer *runtime.Drainer) error
423423
&runtimecontrollers.KmsgLogDeliveryController{
424424
Drainer: drainer,
425425
},
426+
&runtimecontrollers.KmsgLogStorageController{
427+
Drainer: drainer,
428+
V1Alpha1Logging: ctrl.v1alpha1Runtime.Logging(),
429+
},
426430
&runtimecontrollers.LogPersistenceController{
427431
V1Alpha1Logging: ctrl.v1alpha1Runtime.Logging(),
428432
},

0 commit comments

Comments
 (0)