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
|
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2020 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 boot_test
import (
"fmt"
. "gopkg.in/check.v1"
"github.com/snapcore/snapd/boot"
"github.com/snapcore/snapd/boot/boottest"
"github.com/snapcore/snapd/bootloader"
"github.com/snapcore/snapd/snap"
)
// TODO:UC20: move this to bootloadertest package and use from i.e. managers_test.go ?
func runBootloaderLogic(c *C, bl bootloader.Bootloader) (snap.PlaceInfo, error) {
// switch on which kind of bootloader we have
ebl, ok := bl.(bootloader.ExtractedRunKernelImageBootloader)
if ok {
return extractedRunKernelImageBootloaderLogic(c, ebl)
}
return pureenvBootloaderLogic(c, "kernel_status", bl)
}
// runBootloaderLogic implements the logic from the gadget snap bootloader,
// namely that we transition kernel_status "try" -> "trying" and "trying" -> ""
// and use try-kernel.efi when kernel_status is "try" and kernel.efi in all
// other situations
func extractedRunKernelImageBootloaderLogic(c *C, ebl bootloader.ExtractedRunKernelImageBootloader) (snap.PlaceInfo, error) {
m, err := ebl.GetBootVars("kernel_status")
c.Assert(err, IsNil)
kernStatus := m["kernel_status"]
kern, err := ebl.Kernel()
c.Assert(err, IsNil)
c.Assert(kern, Not(IsNil))
switch kernStatus {
case boot.DefaultStatus:
case boot.TryStatus:
// move to trying, use the try-kernel
m["kernel_status"] = boot.TryingStatus
// ensure that the try-kernel exists
tryKern, err := ebl.TryKernel()
c.Assert(err, IsNil)
c.Assert(tryKern, Not(IsNil))
kern = tryKern
case boot.TryingStatus:
// boot failed, move back to default
m["kernel_status"] = boot.DefaultStatus
}
err = ebl.SetBootVars(m)
c.Assert(err, IsNil)
return kern, nil
}
func pureenvBootloaderLogic(c *C, modeVar string, bl bootloader.Bootloader) (snap.PlaceInfo, error) {
m, err := bl.GetBootVars(modeVar, "snap_kernel", "snap_try_kernel")
c.Assert(err, IsNil)
var kern snap.PlaceInfo
kernStatus := m[modeVar]
kern, err = snap.ParsePlaceInfoFromSnapFileName(m["snap_kernel"])
c.Assert(err, IsNil)
c.Assert(kern, Not(IsNil))
switch kernStatus {
case boot.DefaultStatus:
// nothing to do, use normal kernel
case boot.TryStatus:
// move to trying, use the try-kernel
m[modeVar] = boot.TryingStatus
tryKern, err := snap.ParsePlaceInfoFromSnapFileName(m["snap_try_kernel"])
c.Assert(err, IsNil)
c.Assert(tryKern, Not(IsNil))
kern = tryKern
case boot.TryingStatus:
// boot failed, move back to default status
m[modeVar] = boot.DefaultStatus
}
err = bl.SetBootVars(m)
c.Assert(err, IsNil)
return kern, nil
}
// note: this could be implemented just as a function which takes a bootloader
// as an argument and then inspect the type of MockBootloader that was passed
// in, but the gains are little, since we don't need to use this function for
// the non-ExtractedRunKernelImageBootloader implementations, as those
// implementations just have one critical function to run which is just
// SetBootVars
func (s *bootenv20Suite) checkBootStateAfterUnexpectedRebootAndCleanup(
c *C,
dev snap.Device,
bootFunc func(snap.Device) error,
panicFunc string,
expectedBootedKernel snap.PlaceInfo,
expectedModeenvCurrentKernels []snap.PlaceInfo,
blKernelAfterReboot snap.PlaceInfo,
comment string,
) {
if panicFunc != "" {
// setup a panic during the given bootloader function
restoreBootloaderPanic := s.bootloader.SetMockToPanic(panicFunc)
// run the boot function that will now panic
c.Assert(
func() { bootFunc(dev) },
PanicMatches,
fmt.Sprintf("mocked reboot panic in %s", panicFunc),
Commentf(comment),
)
// don't panic anymore
restoreBootloaderPanic()
} else {
// just run the function directly
err := bootFunc(dev)
c.Assert(err, IsNil, Commentf(comment))
}
// do the bootloader kernel failover logic handling
nextBootingKernel, err := runBootloaderLogic(c, s.bootloader)
c.Assert(err, IsNil, Commentf(comment))
// check that the kernel we booted now is expected
c.Assert(nextBootingKernel, Equals, expectedBootedKernel, Commentf(comment))
// also check that the normal kernel on the bootloader is what we expect
kern, err := s.bootloader.Kernel()
c.Assert(err, IsNil, Commentf(comment))
c.Assert(kern, Equals, blKernelAfterReboot, Commentf(comment))
// mark the boot successful like we were rebooted
err = boot.MarkBootSuccessful(dev)
c.Assert(err, IsNil, Commentf(comment))
// the boot vars should be empty now too
afterVars, err := s.bootloader.GetBootVars("kernel_status")
c.Assert(err, IsNil, Commentf(comment))
c.Assert(afterVars["kernel_status"], DeepEquals, boot.DefaultStatus, Commentf(comment))
// the modeenv's setting for CurrentKernels also matches
m, err := boot.ReadModeenv("")
c.Assert(err, IsNil, Commentf(comment))
// it's nicer to pass in just the snap.PlaceInfo's, but to compare we need
// the string filenames
currentKernels := make([]string, len(expectedModeenvCurrentKernels))
for i, sn := range expectedModeenvCurrentKernels {
currentKernels[i] = sn.Filename()
}
c.Assert(m.CurrentKernels, DeepEquals, currentKernels, Commentf(comment))
// the final kernel on the bootloader should always match what we booted -
// after MarkSuccessful runs that is
afterKernel, err := s.bootloader.Kernel()
c.Assert(err, IsNil, Commentf(comment))
c.Assert(afterKernel, DeepEquals, expectedBootedKernel, Commentf(comment))
// we should never have a leftover try kernel
_, err = s.bootloader.TryKernel()
c.Assert(err, Equals, bootloader.ErrNoTryKernelRef, Commentf(comment))
}
func (s *bootenv20Suite) TestHappyMarkBootSuccessful20KernelUpgradeUnexpectedReboots(c *C) {
coreDev := boottest.MockUC20Device("", nil)
c.Assert(coreDev.HasModeenv(), Equals, true)
tt := []struct {
rebootBeforeFunc string
expBootKernel snap.PlaceInfo
expModeenvKernels []snap.PlaceInfo
expBlKernel snap.PlaceInfo
comment string
}{
{
"", // don't do any reboots for the happy path
s.kern2, // we should boot the new kernel
[]snap.PlaceInfo{s.kern2}, // expected modeenv kernel is new one
s.kern2, // after reboot, current kernel on bl is new one
"happy path",
},
{
"SetBootVars", // reboot right before SetBootVars
s.kern1, // we should boot the old kernel
[]snap.PlaceInfo{s.kern1}, // expected modeenv kernel is old one
s.kern1, // after reboot, current kernel on bl is old one
"reboot before SetBootVars results in old kernel",
},
{
"EnableKernel", // reboot right before EnableKernel
s.kern1, // we should boot the old kernel
[]snap.PlaceInfo{s.kern1}, // expected modeenv kernel is old one
s.kern1, // after reboot, current kernel on bl is old one
"reboot before EnableKernel results in old kernel",
},
{
"DisableTryKernel", // reboot right before DisableTryKernel
s.kern2, // we should boot the new kernel
[]snap.PlaceInfo{s.kern2}, // expected modeenv kernel is new one
s.kern2, // after reboot, current kernel on bl is new one
"reboot before DisableTryKernel results in new kernel",
},
}
for _, t := range tt {
// setup the bootloader per test
restore := setupUC20Bootenv(
c,
s.bootloader,
s.normalTryingKernelState,
)
s.checkBootStateAfterUnexpectedRebootAndCleanup(
c,
coreDev,
boot.MarkBootSuccessful,
t.rebootBeforeFunc,
t.expBlKernel,
t.expModeenvKernels,
t.expBlKernel,
t.comment,
)
restore()
}
}
func (s *bootenv20Suite) TestHappySetNextBoot20KernelUpgradeUnexpectedReboots(c *C) {
coreDev := boottest.MockUC20Device("", nil)
c.Assert(coreDev.HasModeenv(), Equals, true)
tt := []struct {
rebootBeforeFunc string
expBootKernel snap.PlaceInfo
expModeenvKernels []snap.PlaceInfo
expBlKernel snap.PlaceInfo
comment string
}{
{
"", // don't do any reboots for the happy path
s.kern2, // we should boot the new kernel
[]snap.PlaceInfo{s.kern2}, // final expected modeenv kernel is new one
s.kern1, // after reboot, current kernel on bl is old one
"happy path",
},
{
"EnableTryKernel", // reboot right before EnableTryKernel
s.kern1, // we should boot the old kernel
[]snap.PlaceInfo{s.kern1}, // final expected modeenv kernel is old one
s.kern1, // after reboot, current kernel on bl is old one
"reboot before EnableTryKernel results in old kernel",
},
{
"SetBootVars", // reboot right before SetBootVars
s.kern1, // we should boot the old kernel
[]snap.PlaceInfo{s.kern1}, // final expected modeenv kernel is old one
s.kern1, // after reboot, current kernel on bl is old one
"reboot before SetBootVars results in old kernel",
},
}
for _, t := range tt {
// setup the bootloader per test
restore := setupUC20Bootenv(
c,
s.bootloader,
s.normalDefaultState,
)
// get the boot kernel participant from our new kernel snap
bootKern := boot.Participant(s.kern2, snap.TypeKernel, coreDev)
// make sure it's not a trivial boot participant
c.Assert(bootKern.IsTrivial(), Equals, false)
setNextFunc := func(snap.Device) error {
// we don't care about the reboot required logic here
_, err := bootKern.SetNextBoot(boot.NextBootContext{BootWithoutTry: false})
return err
}
s.checkBootStateAfterUnexpectedRebootAndCleanup(
c,
coreDev,
setNextFunc,
t.rebootBeforeFunc,
t.expBootKernel,
t.expModeenvKernels,
t.expBlKernel,
t.comment,
)
restore()
}
}
|