forked from canonical/snapd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseed16.go
467 lines (387 loc) · 11.1 KB
/
seed16.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2014-2022 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package seed
/* ATTN this should *not* use:
* dirs package: it is passed an explicit directory to work on
* release.OnClassic: it assumes classic based on the model classic
option; consistency between system and model can/must be enforced
elsewhere
*/
import (
"errors"
"fmt"
"path/filepath"
"github.com/snapcore/snapd/asserts"
"github.com/snapcore/snapd/asserts/snapasserts"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/seed/internal"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/snap/channel"
"github.com/snapcore/snapd/snap/naming"
"github.com/snapcore/snapd/timings"
)
type seed16 struct {
seedDir string
db asserts.RODatabase
model *asserts.Model
yamlSnaps []*internal.Snap16
essCache map[string]*Snap
snaps []*Snap
essentialSnapsNum int
usesSnapdSnap bool
}
func (s *seed16) LoadAssertions(db asserts.RODatabase, commitTo func(*asserts.Batch) error) error {
if db == nil {
// a db was not provided, create an internal temporary one
var err error
db, commitTo, err = newMemAssertionsDB(nil)
if err != nil {
return err
}
}
assertSeedDir := filepath.Join(s.seedDir, "assertions")
// collect assertions and find model assertion
var modelRef *asserts.Ref
checkForModel := func(ref *asserts.Ref) error {
if ref.Type == asserts.ModelType {
if modelRef != nil && modelRef.Unique() != ref.Unique() {
return fmt.Errorf("cannot have multiple model assertions in seed")
}
modelRef = ref
}
return nil
}
batch, err := loadAssertions(assertSeedDir, checkForModel)
if err != nil {
return err
}
// verify we have one model assertion
if modelRef == nil {
return fmt.Errorf("seed must have a model assertion")
}
if err := commitTo(batch); err != nil {
return err
}
a, err := modelRef.Resolve(db.Find)
if err != nil {
return fmt.Errorf("internal error: cannot find just added assertion %v: %v", modelRef, err)
}
// remember db for later use
s.db = db
s.model = a.(*asserts.Model)
return nil
}
func (s *seed16) Model() *asserts.Model {
if s.model == nil {
panic("internal error: model assertion unset (LoadAssertions not called)")
}
return s.model
}
func (s *seed16) Brand() (*asserts.Account, error) {
return findBrand(s, s.db)
}
func (s *seed16) SetParallelism(int) {
// ignored
}
func (s *seed16) addSnap(sn *internal.Snap16, essType snap.Type, pinnedTrack string, handler SnapHandler, cache map[string]*Snap, tm timings.Measurer) (*Snap, error) {
path := filepath.Join(s.seedDir, "snaps", sn.File)
_, defaultHandler := handler.(defaultSnapHandler)
seedSnap := cache[path]
// not cached, or ignore the cache if a non-default handler
// was passed, otherwise it would not be called which could be
// unexpected
if seedSnap == nil || !defaultHandler {
snapChannel := sn.Channel
if pinnedTrack != "" {
var err error
snapChannel, err = channel.ResolvePinned(pinnedTrack, snapChannel)
if err != nil {
// fallback to using the pinned track directly
snapChannel = pinnedTrack
}
}
seedSnap = &Snap{
Channel: snapChannel,
Classic: sn.Classic,
DevMode: sn.DevMode,
}
var sideInfo snap.SideInfo
var newPath string
if sn.Unasserted {
var err error
newPath, err = handler.HandleUnassertedSnap(sn.Name, path, tm)
if err != nil {
return nil, err
}
sideInfo.RealName = sn.Name
} else {
var si *snap.SideInfo
var err error
deriveRev := func(snapSHA3_384 string, snapSize uint64) (snap.Revision, error) {
if si == nil {
var err error
si, err = snapasserts.DeriveSideInfoFromDigestAndSize(path, snapSHA3_384, snapSize, s.model, s.db)
if err != nil {
return snap.Revision{}, err
}
}
return si.Revision, nil
}
timings.Run(tm, "derive-side-info", fmt.Sprintf("hash and derive side info for snap %q", sn.Name), func(nested timings.Measurer) {
var snapSHA3_384 string
var snapSize uint64
newPath, snapSHA3_384, snapSize, err = handler.HandleAndDigestAssertedSnap(sn.Name, path, essType, nil, deriveRev, tm)
if err != nil {
return
}
// sets si too
_, err = deriveRev(snapSHA3_384, snapSize)
})
if errors.Is(err, &asserts.NotFoundError{}) {
return nil, fmt.Errorf("cannot find signatures with metadata for snap %q (%q)", sn.Name, path)
}
if err != nil {
return nil, err
}
sideInfo = *si
sideInfo.Private = sn.Private
sideInfo.LegacyEditedContact = sn.Contact
}
origPath := path
if newPath != "" {
path = newPath
}
seedSnap.Path = path
seedSnap.SideInfo = &sideInfo
if cache != nil {
cache[origPath] = seedSnap
}
}
s.snaps = append(s.snaps, seedSnap)
return seedSnap, nil
}
type essentialSnapMissingError struct {
SnapName string
}
func (e *essentialSnapMissingError) Error() string {
return fmt.Sprintf("essential snap %q required by the model is missing in the seed", e.SnapName)
}
func (s *seed16) loadYaml() error {
if s.yamlSnaps != nil {
return nil
}
seedYamlFile := filepath.Join(s.seedDir, "seed.yaml")
if !osutil.FileExists(seedYamlFile) {
return ErrNoMeta
}
seedYaml, err := internal.ReadSeedYaml(seedYamlFile)
if err != nil {
return err
}
s.yamlSnaps = seedYaml.Snaps
return nil
}
func (s *seed16) resetSnaps() {
// setup essential snaps cache
if s.essCache == nil {
// 4 = snapd+base+kernel+gadget
s.essCache = make(map[string]*Snap, 4)
}
s.snaps = nil
s.essentialSnapsNum = 0
}
func (s *seed16) loadEssentialMeta(essentialTypes []snap.Type, required *naming.SnapSet, handler SnapHandler, added map[string]bool, tm timings.Measurer) error {
model := s.Model()
seeding := make(map[string]*internal.Snap16, len(s.yamlSnaps))
for _, sn := range s.yamlSnaps {
seeding[sn.Name] = sn
}
classic := model.Classic()
_, usesSnapdSnap := seeding["snapd"]
usesSnapdSnap = usesSnapdSnap || required.Contains(naming.Snap("snapd"))
s.usesSnapdSnap = usesSnapdSnap
baseSnap := "core"
classicWithSnapd := false
if model.Base() != "" {
baseSnap = model.Base()
}
if classic && s.usesSnapdSnap {
classicWithSnapd = true
// there is no system-wide base as such
// if there is a gadget we will install its base first though
baseSnap = ""
}
// add the essential snaps
addEssential := func(snapName string, pinnedTrack string, essType snap.Type) (*Snap, error) {
// be idempotent
if added[snapName] {
return nil, nil
}
// filter if required
if essentialTypes != nil {
skip := true
for _, t := range essentialTypes {
if t == essType {
skip = false
break
}
}
if skip {
return nil, nil
}
}
yamlSnap := seeding[snapName]
if yamlSnap == nil {
return nil, &essentialSnapMissingError{SnapName: snapName}
}
seedSnap, err := s.addSnap(yamlSnap, essType, pinnedTrack, handler, s.essCache, tm)
if err != nil {
return nil, err
}
if essType == snap.TypeBase && snapName == "core" {
essType = snap.TypeOS
}
seedSnap.EssentialType = essType
seedSnap.Essential = true
seedSnap.Required = true
added[snapName] = true
return seedSnap, nil
}
// if there are snaps to seed, core/base needs to be seeded too
if len(s.yamlSnaps) != 0 {
// ensure "snapd" snap is installed first
if model.Base() != "" || classicWithSnapd {
if _, err := addEssential("snapd", "", snap.TypeSnapd); err != nil {
return err
}
}
if !classicWithSnapd {
if _, err := addEssential(baseSnap, "", snap.TypeBase); err != nil {
return err
}
}
}
if kernelName := model.Kernel(); kernelName != "" {
if _, err := addEssential(kernelName, model.KernelTrack(), snap.TypeKernel); err != nil {
return err
}
}
if gadgetName := model.Gadget(); gadgetName != "" {
gadget, err := addEssential(gadgetName, model.GadgetTrack(), snap.TypeGadget)
if err != nil {
return err
}
// not skipped
if gadget != nil {
// always make sure the base of gadget is installed first
info, err := readInfo(gadget.Path, gadget.SideInfo)
if err != nil {
return err
}
gadgetBase := info.Base
if gadgetBase == "" {
gadgetBase = "core"
}
// Validity check
// TODO: do we want to relax this? the new logic would allow
// but it might just be confusing for now
if baseSnap != "" && gadgetBase != baseSnap {
return fmt.Errorf("cannot use gadget snap because its base %q is different from model base %q", gadgetBase, model.Base())
}
if _, err = addEssential(gadgetBase, "", snap.TypeBase); err != nil {
return err
}
}
}
s.essentialSnapsNum = len(s.snaps)
return nil
}
func (s *seed16) LoadEssentialMeta(essentialTypes []snap.Type, tm timings.Measurer) error {
return s.LoadEssentialMetaWithSnapHandler(essentialTypes, nil, tm)
}
func (s *seed16) LoadEssentialMetaWithSnapHandler(essentialTypes []snap.Type, handler SnapHandler, tm timings.Measurer) error {
model := s.Model()
if err := s.loadYaml(); err != nil {
return err
}
required := naming.NewSnapSet(model.RequiredWithEssentialSnaps())
added := make(map[string]bool, 3)
s.resetSnaps()
if handler == nil {
handler = defaultSnapHandler{}
}
if len(essentialTypes) == 0 {
essentialTypes = nil
}
return s.loadEssentialMeta(essentialTypes, required, handler, added, tm)
}
func (s *seed16) LoadMeta(mode string, handler SnapHandler, tm timings.Measurer) error {
if mode != AllModes && mode != "run" {
return fmt.Errorf("internal error: Core 16/18 have only run mode, got: %s", mode)
}
model := s.Model()
if err := s.loadYaml(); err != nil {
return err
}
required := naming.NewSnapSet(model.RequiredWithEssentialSnaps())
added := make(map[string]bool, 3)
s.resetSnaps()
if handler == nil {
handler = defaultSnapHandler{}
}
if err := s.loadEssentialMeta(nil, required, handler, added, tm); err != nil {
return err
}
// the rest of the snaps
for _, sn := range s.yamlSnaps {
if added[sn.Name] {
continue
}
seedSnap, err := s.addSnap(sn, "", "", handler, nil, tm)
if err != nil {
return err
}
if required.Contains(seedSnap) {
seedSnap.Required = true
}
}
return nil
}
func (s *seed16) UsesSnapdSnap() bool {
return s.usesSnapdSnap
}
func (s *seed16) EssentialSnaps() []*Snap {
return s.snaps[:s.essentialSnapsNum]
}
func (s *seed16) ModeSnaps(mode string) ([]*Snap, error) {
if mode != "run" {
return nil, fmt.Errorf("internal error: Core 16/18 have only run mode, got: %s", mode)
}
return s.snaps[s.essentialSnapsNum:], nil
}
func (s *seed16) NumSnaps() int {
return len(s.snaps)
}
func (s *seed16) Iter(f func(sn *Snap) error) error {
for _, sn := range s.snaps {
if err := f(sn); err != nil {
return err
}
}
return nil
}