github.com/rigado/snapd@v2.42.5-go-mod+incompatible/overlord/snapstate/booted.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2014-2015 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package snapstate 21 22 import ( 23 "fmt" 24 25 "github.com/snapcore/snapd/boot" 26 "github.com/snapcore/snapd/logger" 27 "github.com/snapcore/snapd/overlord/state" 28 "github.com/snapcore/snapd/release" 29 "github.com/snapcore/snapd/snap" 30 ) 31 32 // UpdateBootRevisions synchronizes the active kernel and OS snap versions 33 // with the versions that actually booted. This is needed because a 34 // system may install "os=v2" but that fails to boot. The bootloader 35 // fallback logic will revert to "os=v1" but on the filesystem snappy 36 // still has the "active" version set to "v2" which is 37 // misleading. This code will check what kernel/os booted and set 38 // those versions active.To do this it creates a Change and kicks 39 // start it directly. 40 func UpdateBootRevisions(st *state.State) error { 41 const errorPrefix = "cannot update revisions after boot changes: " 42 43 if release.OnClassic { 44 return nil 45 } 46 47 // nothing to check if there's no kernel 48 ok, err := HasSnapOfType(st, snap.TypeKernel) 49 if err != nil { 50 return fmt.Errorf(errorPrefix+"%s", err) 51 } 52 if !ok { 53 return nil 54 } 55 56 kernel, err := boot.GetCurrentBoot(snap.TypeKernel) 57 if err != nil { 58 return fmt.Errorf(errorPrefix+"%s", err) 59 } 60 base, err := boot.GetCurrentBoot(snap.TypeBase) 61 if err != nil { 62 return fmt.Errorf(errorPrefix+"%s", err) 63 } 64 65 var tsAll []*state.TaskSet 66 for _, actual := range []*boot.NameAndRevision{kernel, base} { 67 info, err := CurrentInfo(st, actual.Name) 68 if err != nil { 69 logger.Noticef("cannot get info for %q: %s", actual.Name, err) 70 continue 71 } 72 if actual.Revision != info.SideInfo.Revision { 73 // FIXME: check that there is no task 74 // for this already in progress 75 ts, err := RevertToRevision(st, actual.Name, actual.Revision, Flags{}) 76 if err != nil { 77 return err 78 } 79 tsAll = append(tsAll, ts) 80 } 81 } 82 83 if len(tsAll) == 0 { 84 return nil 85 } 86 87 msg := fmt.Sprintf("Update kernel and core snap revisions") 88 chg := st.NewChange("update-revisions", msg) 89 for _, ts := range tsAll { 90 chg.AddAll(ts) 91 } 92 st.EnsureBefore(0) 93 94 return nil 95 }