Skip to content

Commit 7bd677a

Browse files
committed
many: some more docs and improvements for readability
1 parent 1ad6e4b commit 7bd677a

4 files changed

Lines changed: 50 additions & 4 deletions

File tree

boot/assets_piboot.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,16 @@ func NewPibootConfigUpdateObserver() gadget.ContentUpdateObserver {
3838
return &pibootConfigUpdateObserver{}
3939
}
4040

41+
func isPibootConfigUpdate(op gadget.ContentOperation, partRole, relativeTarget string) bool {
42+
// We only care about updates to config.txt in the system seed,
43+
// and only if there is a before value to check for an os_prefix.
44+
return op == gadget.ContentUpdate &&
45+
(partRole == gadget.SystemSeed || partRole == gadget.SystemSeedNull) &&
46+
relativeTarget == "config.txt"
47+
}
48+
4149
func (o *pibootConfigUpdateObserver) Observe(op gadget.ContentOperation, partRole, root, relativeTarget string, data *gadget.ContentChange) (gadget.ContentChangeAction, error) {
42-
if op != gadget.ContentUpdate || (partRole != gadget.SystemSeed && partRole != gadget.SystemSeedNull) || relativeTarget != "config.txt" || data == nil || data.Before == "" {
50+
if !isPibootConfigUpdate(op, partRole, relativeTarget) || data == nil || data.Before == "" {
4351
return gadget.ChangeApply, nil
4452
}
4553

gadget/update_observer.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,13 @@ type compositeContentUpdateObserver struct {
2626
var _ ContentUpdateObserver = (*compositeContentUpdateObserver)(nil)
2727

2828
// NewCompositeContentUpdateObserver combines multiple content update observers
29-
// into one. Nil observers are ignored. When a single non-nil observer is
30-
// provided, it is returned unchanged.
29+
// into one. Ignores nil observers. If no observers are provided, returns nil.
30+
// If a single non-nil observer is provided, it is returned unchanged.
31+
//
32+
// The composite Observe method calls observers in order. It returns
33+
// immediately on the first error or ChangeAbort. ChangeIgnore is sticky but
34+
// does not stop iteration: remaining observers are still called, and the
35+
// final result is ChangeIgnore unless a later observer aborts or errors.
3136
func NewCompositeContentUpdateObserver(observers ...ContentUpdateObserver) ContentUpdateObserver {
3237
filtered := make([]ContentUpdateObserver, 0, len(observers))
3338
for _, observer := range observers {

gadget/update_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,35 @@ func (u *updateTestSuite) TestNewCompositeContentUpdateObserver(c *C) {
754754
c.Check(obs2.doneCalled, Equals, 1)
755755
}
756756

757+
func (u *updateTestSuite) TestNewCompositeContentUpdateObserverObserveContinuesAfterIgnore(c *C) {
758+
obs1 := &mockPostApplyObserver{observeAction: gadget.ChangeIgnore}
759+
obs2 := &mockPostApplyObserver{observeAction: gadget.ChangeApply}
760+
761+
combined := gadget.NewCompositeContentUpdateObserver(obs1, obs2)
762+
763+
action, err := combined.Observe(gadget.ContentUpdate, gadget.SystemBoot, "/run/mnt/ubuntu-boot", "config.txt", nil)
764+
c.Assert(err, IsNil)
765+
c.Check(action, Equals, gadget.ChangeIgnore)
766+
c.Check(obs1.observeCalled, Equals, 1)
767+
c.Check(obs2.observeCalled, Equals, 1)
768+
}
769+
770+
func (u *updateTestSuite) TestNewCompositeContentUpdateObserverObserveStopsOnAbort(c *C) {
771+
expectedErr := errors.New("boom")
772+
obs1 := &mockPostApplyObserver{observeAction: gadget.ChangeApply}
773+
obs2 := &mockPostApplyObserver{observeAction: gadget.ChangeAbort, observeErr: expectedErr}
774+
obs3 := &mockPostApplyObserver{observeAction: gadget.ChangeApply}
775+
776+
combined := gadget.NewCompositeContentUpdateObserver(obs1, obs2, obs3)
777+
778+
action, err := combined.Observe(gadget.ContentUpdate, gadget.SystemBoot, "/run/mnt/ubuntu-boot", "config.txt", nil)
779+
c.Assert(err, Equals, expectedErr)
780+
c.Check(action, Equals, gadget.ChangeAbort)
781+
c.Check(obs1.observeCalled, Equals, 1)
782+
c.Check(obs2.observeCalled, Equals, 1)
783+
c.Check(obs3.observeCalled, Equals, 0)
784+
}
785+
757786
func (u *updateTestSuite) TestUpdateApplyHappy(c *C) {
758787
oldData, newData, rollbackDir := u.updateDataSet(c)
759788
// update two structs

overlord/devicestate/handlers_gadget.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,11 @@ func (m *DeviceManager) doUpdateGadgetAssets(t *state.Task, _ *tomb.Tomb) error
199199
if err == nil {
200200
observers = append(observers, observeTrustedBootAssets)
201201
}
202-
if snapsup.Type == snap.TypeGadget && currentData.Info != nil && currentData.Info.HasBootloader("piboot") {
202+
203+
// On raspberry pi devices with piboot, we need to observe updates to config.txt
204+
// in the system seed so that we can extract the os_prefix and preserve the value
205+
// across gadget updates.
206+
if snapsup.Type == snap.TypeGadget && currentData.Info.HasBootloader("piboot") {
203207
observers = append(observers, boot.NewPibootConfigUpdateObserver())
204208
}
205209

0 commit comments

Comments
 (0)