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 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
|
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2014-2023 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 devicestate
import (
"context"
"errors"
"fmt"
"runtime"
"sort"
"github.com/snapcore/snapd/asserts"
"github.com/snapcore/snapd/client"
"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/i18n"
"github.com/snapcore/snapd/logger"
"github.com/snapcore/snapd/overlord/assertstate"
"github.com/snapcore/snapd/overlord/devicestate/internal"
"github.com/snapcore/snapd/overlord/snapstate"
"github.com/snapcore/snapd/overlord/state"
"github.com/snapcore/snapd/release"
"github.com/snapcore/snapd/seed"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/timings"
)
var errNothingToDo = errors.New("nothing to do")
var runtimeNumCPU = runtime.NumCPU
func installSeedSnap(st *state.State, sn *seed.Snap, flags snapstate.Flags, prqt snapstate.PrereqTracker) (*state.TaskSet, *snap.Info, error) {
if sn.Required {
flags.Required = true
}
if sn.Classic {
flags.Classic = true
}
if sn.DevMode {
flags.DevMode = true
}
components := make([]snapstate.PathComponent, 0, len(sn.Components))
for _, comp := range sn.Components {
// Prevent reusing loop variable
comp := comp
components = append(components, snapstate.PathComponent{
Path: comp.Path,
SideInfo: &comp.CompSideInfo,
})
}
goal := snapstate.PathInstallGoal(snapstate.PathSnap{
Path: sn.Path,
SideInfo: sn.SideInfo,
Components: components,
RevOpts: snapstate.RevisionOptions{Channel: sn.Channel},
})
info, ts, err := snapstate.InstallOne(context.Background(), st, goal, snapstate.Options{
Flags: flags,
PrereqTracker: prqt,
Seed: true,
})
if err != nil {
return nil, nil, err
}
return ts, info, nil
}
func criticalTaskEdges(ts *state.TaskSet) (beginEdge, beforeHooksEdge, hooksEdge *state.Task, err error) {
// we expect all three edges, or none (the latter is the case with config tasksets).
beginEdge, err = ts.Edge(snapstate.BeginEdge)
if err != nil {
return nil, nil, nil, nil
}
beforeHooksEdge, err = ts.Edge(snapstate.BeforeHooksEdge)
if err != nil {
return nil, nil, nil, err
}
hooksEdge, err = ts.Edge(snapstate.HooksEdge)
if err != nil {
return nil, nil, nil, err
}
return beginEdge, beforeHooksEdge, hooksEdge, nil
}
// maybeEnforceValidationSetsTask returns a task for tracking validation-sets. This may
// return nil if no validation-sets are present.
func maybeEnforceValidationSetsTask(st *state.State, model *asserts.Model, mode string) (*state.Task, error) {
// Only enforce validation-sets in run-mode after installing all required snaps
if mode != "run" {
logger.Debugf("Postponing enforcement of validation-sets in mode %s", mode)
return nil, nil
}
// Encode validation-sets included in the seed
db := assertstate.DB(st)
as, err := db.FindMany(asserts.ValidationSetType, nil)
if err != nil {
// If none are included, then skip this
if errors.Is(err, &asserts.NotFoundError{}) {
return nil, nil
}
return nil, err
}
vsKeys := make(map[string][]string)
for _, a := range as {
vsa := a.(*asserts.ValidationSet)
vsKeys[vsa.SequenceKey()] = a.Ref().PrimaryKey
}
// Set up pins from the model
pins := make(map[string]int)
for _, vs := range model.ValidationSets() {
if vs.Sequence > 0 {
pins[vs.SequenceKey()] = vs.Sequence
}
}
t := st.NewTask("enforce-validation-sets", i18n.G("Track validation sets"))
t.Set("validation-set-keys", vsKeys)
t.Set("pinned-sequence-numbers", pins)
return t, nil
}
func markSeededTask(st *state.State) *state.Task {
return st.NewTask("mark-seeded", i18n.G("Mark system seeded"))
}
func trivialSeeding(st *state.State) []*state.TaskSet {
// give the internal core config a chance to run (even if core is
// not used at all we put system configuration there)
configTs := snapstate.ConfigureSnap(st, "core", 0)
markSeeded := markSeededTask(st)
markSeeded.WaitAll(configTs)
return []*state.TaskSet{configTs, state.NewTaskSet(markSeeded)}
}
func (m *DeviceManager) populateStateFromSeedImpl(tm timings.Measurer) ([]*state.TaskSet, error) {
st := m.state
// check that the state is empty
var seeded bool
err := st.Get("seeded", &seeded)
if err != nil && !errors.Is(err, state.ErrNoState) {
return nil, err
}
if seeded {
return nil, fmt.Errorf("cannot populate state: already seeded")
}
preseed := m.preseed
sysLabel, mode, err := m.seedLabelAndMode()
if err != nil {
return nil, err
}
hasModeenv := false
if mode != "" {
hasModeenv = true
} else {
mode = "run"
}
var deviceSeed seed.Seed
// ack all initial assertions
timings.Run(tm, "import-assertions[finish]", "finish importing assertions from seed", func(nested timings.Measurer) {
isCoreBoot := hasModeenv || !release.OnClassic
deviceSeed, err = m.importAssertionsFromSeed(mode, isCoreBoot)
})
if err != nil && err != errNothingToDo {
return nil, err
}
if err == errNothingToDo {
return trivialSeeding(st), nil
}
timings.Run(tm, "load-verified-snap-metadata", "load verified snap metadata from seed", func(nested timings.Measurer) {
err = deviceSeed.LoadMeta(mode, nil, nested)
})
// ErrNoMeta can happen only with Core 16/18-style seeds
if err == seed.ErrNoMeta && release.OnClassic {
if preseed {
return nil, fmt.Errorf("no snaps to preseed")
}
// on classic it is ok to not seed any snaps
return trivialSeeding(st), nil
}
if err != nil {
return nil, err
}
model := deviceSeed.Model()
essentialSeedSnaps := deviceSeed.EssentialSnaps()
seedSnaps, err := deviceSeed.ModeSnaps(mode)
if err != nil {
return nil, err
}
tsAll := []*state.TaskSet{}
configTss := []*state.TaskSet{}
var lastBeforeHooksTask *state.Task
var chainTs func(all []*state.TaskSet, ts *state.TaskSet) []*state.TaskSet
var preseedDoneTask *state.Task
if preseed {
preseedDoneTask = st.NewTask("mark-preseeded", i18n.G("Mark system pre-seeded"))
}
chainTsPreseeding := func(all []*state.TaskSet, ts *state.TaskSet) []*state.TaskSet {
// mark-preseeded task needs to be inserted between preliminary setup and hook tasks
beginTask, beforeHooksTask, hooksTask, err := criticalTaskEdges(ts)
if err != nil {
// XXX: internal error?
panic(err)
}
// we either have all edges or none
if beginTask != nil {
// hooks must wait for mark-preseeded
hooksTask.WaitFor(preseedDoneTask)
if n := len(all); n > 0 {
// the first hook of the snap waits for all tasks of previous snap
hooksTask.WaitAll(all[n-1])
}
if lastBeforeHooksTask != nil {
beginTask.WaitFor(lastBeforeHooksTask)
}
preseedDoneTask.WaitFor(beforeHooksTask)
lastBeforeHooksTask = beforeHooksTask
} else {
n := len(all)
// no edges: it is a configure snap taskset for core/gadget/kernel
if n != 0 {
ts.WaitAll(all[n-1])
}
}
return append(all, ts)
}
chainTsFullSeeding := func(all []*state.TaskSet, ts *state.TaskSet) []*state.TaskSet {
n := len(all)
if n != 0 {
ts.WaitAll(all[n-1])
}
return append(all, ts)
}
if preseed {
chainTs = chainTsPreseeding
} else {
chainTs = chainTsFullSeeding
}
chainSorted := func(infos []*snap.Info, infoToTs map[*snap.Info]*state.TaskSet) {
// This is the order in which snaps will be installed in the
// system. We want the boot base to be installed before the
// kernel so any existing kernel hook can execute with the boot
// base as rootfs.
effectiveType := func(info *snap.Info) snap.Type {
typ := info.Type()
if info.RealName == model.Base() {
typ = snap.InternalTypeBootBase
}
return typ
}
sort.SliceStable(infos, func(i, j int) bool {
return effectiveType(infos[i]).SortsBefore(effectiveType(infos[j]))
})
for _, info := range infos {
ts := infoToTs[info]
tsAll = chainTs(tsAll, ts)
}
}
// collected snap infos
infos := make([]*snap.Info, 0, len(essentialSeedSnaps)+len(seedSnaps))
infoToTs := make(map[*snap.Info]*state.TaskSet, len(essentialSeedSnaps))
prqt := snap.NewSelfContainedSetPrereqTracker()
if len(essentialSeedSnaps) != 0 {
// we *always* configure "core" here even if bases are used
// for booting. "core" is where the system config lives.
configTss = chainTs(configTss, snapstate.ConfigureSnap(st, "core", snapstate.UseConfigDefaults))
}
modelIsDangerous := model.Grade() == asserts.ModelDangerous
essentialLane := st.NewLane()
for _, seedSnap := range essentialSeedSnaps {
flags := snapstate.Flags{
SkipConfigure: true,
// The kernel is already there either from ubuntu-image or from "install"
// mode so skip extract.
SkipKernelExtraction: true,
// for dangerous models, allow all devmode snaps
// XXX: eventually we may need to allow specific snaps to be devmode for
// non-dangerous models, we can do that here since that information will
// probably be in the model assertion which we have here
ApplySnapDevMode: modelIsDangerous,
Lane: essentialLane,
Transaction: client.TransactionAllSnaps,
}
ts, info, err := installSeedSnap(st, seedSnap, flags, prqt)
if err != nil {
return nil, err
}
if info.Type() == snap.TypeKernel || info.Type() == snap.TypeGadget {
configTs := snapstate.ConfigureSnap(st, info.SnapName(), snapstate.UseConfigDefaults)
// wait for the previous configTss
configTss = chainTs(configTss, configTs)
}
infos = append(infos, info)
infoToTs[info] = ts
}
// now add/chain the tasksets in the right order based on essential
// snap types
chainSorted(infos, infoToTs)
// chain together configuring core, kernel, and gadget after
// installing them so that defaults are available from gadget
if len(configTss) > 0 {
if preseed {
configTss[0].WaitFor(preseedDoneTask)
}
configTss[0].WaitAll(tsAll[len(tsAll)-1])
tsAll = append(tsAll, configTss...)
}
// all of the configure tasks are related to essential snaps, so they should
// also be in the essential snap lane
for _, ts := range configTss {
ts.JoinLane(essentialLane)
}
// ensure we install in the right order
infoToTs = make(map[*snap.Info]*state.TaskSet, len(seedSnaps))
// note, we use separate lanes for essential and non-essential snaps so that
// failures installing non-essential snaps do not cause essential snap
// installations to be undone.
nonEssentialLane := st.NewLane()
for _, seedSnap := range seedSnaps {
flags := snapstate.Flags{
// for dangerous models, allow all devmode snaps
// XXX: eventually we may need to allow specific snaps to be devmode for
// non-dangerous models, we can do that here since that information will
// probably be in the model assertion which we have here
ApplySnapDevMode: modelIsDangerous,
// for non-dangerous models snaps need to opt-in explicitly
// Classic is simply ignored for non-classic snaps, so we do not need to check further
Classic: release.OnClassic && modelIsDangerous,
Lane: nonEssentialLane,
Transaction: client.TransactionAllSnaps,
}
ts, info, err := installSeedSnap(st, seedSnap, flags, prqt)
if err != nil {
return nil, err
}
infos = append(infos, info)
infoToTs[info] = ts
}
// validate that all snaps have bases and providers are fulfilled
// using the PrereqTracker
warns, errs := prqt.Check()
if errs != nil {
// only report the first error encountered
return nil, errs[0]
}
// XXX do better, use the warnings to setup checks at end of the seeding
// and log only plug not connected or explicitly disconnected there
for _, w := range warns {
logger.Noticef("seed prerequisites: %v", w)
}
// now add/chain the tasksets in the right order, note that we
// only have tasksets that we did not already seeded
chainSorted(infos[len(essentialSeedSnaps):], infoToTs)
if len(tsAll) == 0 {
return nil, fmt.Errorf("cannot proceed, no snaps to seed")
}
// ts is the taskset of the last snap
ts := tsAll[len(tsAll)-1]
endTs := state.NewTaskSet()
// Start tracking any validation sets included in the seed after
// installing the included snaps.
if trackVss, err := maybeEnforceValidationSetsTask(st, model, mode); err != nil {
return nil, err
} else if trackVss != nil {
trackVss.WaitAll(ts)
// if this validation fails, we want to undo the tasks in the
// non-essential lane.
//
// TODO: currently, undoing seeding the essential set of snaps does not
// work properly. that's why we only add this to the non-essential lane.
trackVss.JoinLane(nonEssentialLane)
endTs.AddTask(trackVss)
}
markSeeded := markSeededTask(st)
if preseed {
endTs.AddTask(preseedDoneTask)
}
whatSeeds := &seededSystem{
System: sysLabel,
Model: model.Model(),
BrandID: model.BrandID(),
Revision: model.Revision(),
Timestamp: model.Timestamp(),
}
markSeeded.Set("seed-system", whatSeeds)
// mark-seeded waits for the taskset of last snap, and
// for all the tasks in the endTs as well.
markSeeded.WaitAll(ts)
markSeeded.WaitAll(endTs)
endTs.AddTask(markSeeded)
tsAll = append(tsAll, endTs)
return tsAll, nil
}
func (m *DeviceManager) importAssertionsFromSeed(mode string, isCoreBoot bool) (seed.Seed, error) {
st := m.state
// TODO: use some kind of context for Device/SetDevice?
device, err := internal.Device(st)
if err != nil {
return nil, err
}
// collect and
// set device,model from the model assertion
_, deviceSeed, err := m.earlyLoadDeviceSeed(nil)
if err == seed.ErrNoAssertions && !isCoreBoot {
// if classic boot seeding is optional
// set the fallback model
err := setClassicFallbackModel(st, device)
if err != nil {
return nil, err
}
return nil, errNothingToDo
}
if err != nil {
return nil, err
}
modelAssertion := deviceSeed.Model()
if release.OnClassic && !modelAssertion.Classic() {
return nil, errors.New("cannot seed a classic system with an all-snaps model")
}
if !release.OnClassic && modelAssertion.Classic() && mode != "recover" {
return nil, errors.New("can only seed an all-snaps system with a classic model in recovery mode")
}
// set device,model from the model assertion
if err := setDeviceFromModelAssertion(st, device, modelAssertion); err != nil {
return nil, err
}
return deviceSeed, nil
}
// processAutoImportAssertions attempts to load the auto import assertions
// and create all knows system users, if and only if the model grade is dangerous.
// Processing of the auto-import assertion is opportunistic and can fail
// for example if system-user-as is serial bound and there is no serial-as yet
func processAutoImportAssertions(st *state.State, deviceSeed seed.Seed, db asserts.RODatabase, commitTo func(batch *asserts.Batch) error) error {
// only proceed for dangerous model
if deviceSeed.Model().Grade() != asserts.ModelDangerous {
return nil
}
seed20AssertionsLoader, ok := deviceSeed.(seed.AutoImportAssertionsLoaderSeed)
if !ok {
return fmt.Errorf("failed to auto-import assertions, invalid loader")
}
err := seed20AssertionsLoader.LoadAutoImportAssertions(commitTo)
if err != nil {
return err
}
// automatic user creation is meant to imply sudoers
const sudoer = true
_, err = createAllKnownSystemUsers(st, db, deviceSeed.Model(), nil, sudoer)
return err
}
// loadDeviceSeed loads the seed based on sysLabel,
// It is meant to be called by DeviceManager.earlyLoadDeviceSeed.
var loadDeviceSeed = func(st *state.State, sysLabel string) (deviceSeed seed.Seed, err error) {
deviceSeed, err = seed.Open(dirs.SnapSeedDir, sysLabel)
if err != nil {
return nil, err
}
if runtimeNumCPU() > 1 {
// XXX set parallelism experimentally to 2 as I/O
// itself becomes a bottleneck ultimately
deviceSeed.SetParallelism(2)
}
// collect and
// set device,model from the model assertion
commitTo := func(batch *asserts.Batch) error {
return assertstate.AddBatch(st, batch, nil)
}
if err := deviceSeed.LoadAssertions(assertstate.DB(st), commitTo); err != nil {
return nil, err
}
return deviceSeed, nil
}
|