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
|
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2021 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"
"os"
"path/filepath"
. "gopkg.in/check.v1"
"github.com/snapcore/snapd/boot"
"github.com/snapcore/snapd/boot/boottest"
"github.com/snapcore/snapd/bootloader"
"github.com/snapcore/snapd/bootloader/bootloadertest"
"github.com/snapcore/snapd/gadget/gadgettest"
"github.com/snapcore/snapd/osutil/kcmdline"
"github.com/snapcore/snapd/snap/snaptest"
"github.com/snapcore/snapd/testutil"
)
var _ = Suite(&kernelCommandLineSuite{})
// baseBootSuite is used to setup the common test environment
type kernelCommandLineSuite struct {
testutil.BaseTest
rootDir string
}
func (s *kernelCommandLineSuite) SetUpTest(c *C) {
s.BaseTest.SetUpTest(c)
s.rootDir = c.MkDir()
err := os.MkdirAll(filepath.Join(s.rootDir, "proc"), 0755)
c.Assert(err, IsNil)
restore := kcmdline.MockProcCmdline(filepath.Join(s.rootDir, "proc/cmdline"))
s.AddCleanup(restore)
}
func (s *kernelCommandLineSuite) mockProcCmdlineContent(c *C, newContent string) {
mockProcCmdline := filepath.Join(s.rootDir, "proc/cmdline")
err := os.WriteFile(mockProcCmdline, []byte(newContent), 0644)
c.Assert(err, IsNil)
}
func (s *kernelCommandLineSuite) TestModeAndLabel(c *C) {
for _, tc := range []struct {
cmd string
mode string
label string
err string
}{{
cmd: "snapd_recovery_mode= snapd_recovery_system=this-is-a-label other-option=foo",
mode: boot.ModeInstall,
label: "this-is-a-label",
}, {
cmd: "snapd_recovery_system=label foo=bar foobaz=\\0\\0123 snapd_recovery_mode=install",
label: "label",
mode: boot.ModeInstall,
}, {
cmd: "snapd_recovery_mode=run snapd_recovery_system=1234",
mode: boot.ModeRun,
}, {
cmd: "snapd_recovery_mode=recover snapd_recovery_system=1234",
label: "1234",
mode: boot.ModeRecover,
}, {
cmd: "snapd_recovery_mode=factory-reset snapd_recovery_system=1234",
label: "1234",
mode: boot.ModeFactoryReset,
}, {
cmd: "option=1 other-option=\0123 none",
err: "cannot detect mode nor recovery system to use",
}, {
cmd: "snapd_recovery_mode=install-foo",
err: `cannot use unknown mode "install-foo"`,
}, {
// no recovery system label
cmd: "snapd_recovery_mode=install foo=bar",
err: `cannot specify install mode without system label`,
}, {
cmd: "snapd_recovery_system=1234",
err: `cannot specify system label without a mode`,
}, {
// multiple kernel command line params end up using the last one - this
// effectively matches the kernel handling too
cmd: "snapd_recovery_mode=install snapd_recovery_system=1234 snapd_recovery_mode=run",
mode: "run",
// label gets unset because it's not used for run mode
label: "",
}, {
cmd: "snapd_recovery_system=not-this-one snapd_recovery_mode=install snapd_recovery_system=1234",
mode: "install",
label: "1234",
}, {
cmd: "snapd_recovery_mode=cloudimg-rootfs",
mode: boot.ModeRunCVM,
}} {
c.Logf("tc: %q", tc)
s.mockProcCmdlineContent(c, tc.cmd)
mode, label, err := boot.ModeAndRecoverySystemFromKernelCommandLine()
if tc.err == "" {
c.Assert(err, IsNil)
c.Check(mode, Equals, tc.mode)
c.Check(label, Equals, tc.label)
} else {
c.Assert(err, ErrorMatches, tc.err)
}
}
}
func (s *kernelCommandLineSuite) TestComposeCommandLineNotManagedHappy(c *C) {
model := boottest.MakeMockUC20Model()
bl := bootloadertest.Mock("btloader", c.MkDir())
bootloader.Force(bl)
defer bootloader.Force(nil)
cmdline, err := boot.ComposeRecoveryCommandLine(model, "20200314", "")
c.Assert(err, IsNil)
c.Assert(cmdline, Equals, "")
cmdline, err = boot.ComposeCommandLine(model, "")
c.Assert(err, IsNil)
c.Assert(cmdline, Equals, "")
tbl := bl.WithTrustedAssets()
bootloader.Force(tbl)
cmdline, err = boot.ComposeRecoveryCommandLine(model, "20200314", "")
c.Assert(err, IsNil)
c.Assert(cmdline, Equals, "snapd_recovery_mode=recover snapd_recovery_system=20200314")
cmdline, err = boot.ComposeCommandLine(model, "")
c.Assert(err, IsNil)
c.Assert(cmdline, Equals, "snapd_recovery_mode=run")
}
func (s *kernelCommandLineSuite) TestComposeCommandLineNotUC20(c *C) {
model := boottest.MakeMockModel()
bl := bootloadertest.Mock("btloader", c.MkDir())
bootloader.Force(bl)
defer bootloader.Force(nil)
cmdline, err := boot.ComposeRecoveryCommandLine(model, "20200314", "")
c.Assert(err, IsNil)
c.Check(cmdline, Equals, "")
cmdline, err = boot.ComposeCommandLine(model, "")
c.Assert(err, IsNil)
c.Check(cmdline, Equals, "")
}
func (s *kernelCommandLineSuite) TestComposeCommandLineManagedHappy(c *C) {
model := boottest.MakeMockUC20Model()
tbl := bootloadertest.Mock("btloader", c.MkDir()).WithTrustedAssets()
bootloader.Force(tbl)
defer bootloader.Force(nil)
tbl.StaticCommandLine = "panic=-1"
cmdline, err := boot.ComposeRecoveryCommandLine(model, "20200314", "")
c.Assert(err, IsNil)
c.Assert(cmdline, Equals, "snapd_recovery_mode=recover snapd_recovery_system=20200314 panic=-1")
cmdline, err = boot.ComposeCommandLine(model, "")
c.Assert(err, IsNil)
c.Assert(cmdline, Equals, "snapd_recovery_mode=run panic=-1")
cmdline, err = boot.ComposeRecoveryCommandLine(model, "20200314", "")
c.Assert(err, IsNil)
c.Assert(cmdline, Equals, "snapd_recovery_mode=recover snapd_recovery_system=20200314 panic=-1")
cmdline, err = boot.ComposeCommandLine(model, "")
c.Assert(err, IsNil)
c.Assert(cmdline, Equals, "snapd_recovery_mode=run panic=-1")
}
func (s *kernelCommandLineSuite) TestComposeCandidateCommandLineManagedHappy(c *C) {
model := boottest.MakeMockUC20Model()
tbl := bootloadertest.Mock("btloader", c.MkDir()).WithTrustedAssets()
bootloader.Force(tbl)
defer bootloader.Force(nil)
tbl.StaticCommandLine = "panic=-1"
tbl.CandidateStaticCommandLine = "candidate panic=0"
cmdline, err := boot.ComposeCandidateCommandLine(model, "")
c.Assert(err, IsNil)
c.Assert(cmdline, Equals, "snapd_recovery_mode=run candidate panic=0")
}
func (s *kernelCommandLineSuite) TestComposeCandidateRecoveryCommandLineManagedHappy(c *C) {
model := boottest.MakeMockUC20Model()
tbl := bootloadertest.Mock("btloader", c.MkDir()).WithTrustedAssets()
bootloader.Force(tbl)
defer bootloader.Force(nil)
tbl.StaticCommandLine = "panic=-1"
tbl.CandidateStaticCommandLine = "candidate panic=0"
cmdline, err := boot.ComposeCandidateRecoveryCommandLine(model, "1234", "")
c.Assert(err, IsNil)
c.Check(cmdline, Equals, "snapd_recovery_mode=recover snapd_recovery_system=1234 candidate panic=0")
cmdline, err = boot.ComposeCandidateRecoveryCommandLine(model, "", "")
c.Assert(err, ErrorMatches, "internal error: system is unset")
c.Check(cmdline, Equals, "")
}
const gadgetSnapYaml = `name: gadget
version: 1.0
type: gadget
`
func (s *kernelCommandLineSuite) TestComposeCommandLineWithGadget(c *C) {
model := boottest.MakeMockUC20Model()
mockGadgetYaml := `
volumes:
volumename:
bootloader: grub
`
tbl := bootloadertest.Mock("btloader", c.MkDir()).WithTrustedAssets()
bootloader.Force(tbl)
defer bootloader.Force(nil)
tbl.StaticCommandLine = "panic=-1"
tbl.CandidateStaticCommandLine = "candidate panic=0"
for _, tc := range []struct {
which string
files [][]string
expCommandLine string
errMsg string
}{{
which: "current",
files: [][]string{
{"cmdline.extra", "cmdline extra"},
},
expCommandLine: "snapd_recovery_mode=run panic=-1 cmdline extra",
}, {
which: "candidate",
files: [][]string{
{"cmdline.extra", "cmdline extra"},
},
expCommandLine: "snapd_recovery_mode=run candidate panic=0 cmdline extra",
}, {
which: "current",
files: [][]string{
{"cmdline.full", "cmdline full"},
},
expCommandLine: "snapd_recovery_mode=run cmdline full",
}, {
which: "candidate",
files: [][]string{
{"cmdline.full", "cmdline full"},
},
expCommandLine: "snapd_recovery_mode=run cmdline full",
}, {
which: "candidate",
files: [][]string{
{"cmdline.extra", `bad-quote="`},
},
errMsg: `cannot use kernel command line from gadget: invalid kernel command line in cmdline.extra: unbalanced quoting`,
}} {
sf := snaptest.MakeTestSnapWithFiles(c, gadgetSnapYaml, append([][]string{
{"meta/snap.yaml", gadgetSnapYaml},
{"meta/gadget.yaml", mockGadgetYaml},
}, tc.files...))
var cmdline string
var err error
switch tc.which {
case "current":
cmdline, err = boot.ComposeCommandLine(model, sf)
case "candidate":
cmdline, err = boot.ComposeCandidateCommandLine(model, sf)
default:
c.Fatalf("unexpected command line type")
}
if tc.errMsg == "" {
c.Assert(err, IsNil)
c.Assert(cmdline, Equals, tc.expCommandLine)
} else {
c.Assert(err, ErrorMatches, tc.errMsg)
}
}
}
func (s *kernelCommandLineSuite) TestComposeRecoveryCommandLineWithGadget(c *C) {
model := boottest.MakeMockUC20Model()
mockGadgetYaml := `
volumes:
volumename:
bootloader: grub
`
tbl := bootloadertest.Mock("btloader", c.MkDir()).WithTrustedAssets()
bootloader.Force(tbl)
defer bootloader.Force(nil)
tbl.StaticCommandLine = "panic=-1"
tbl.CandidateStaticCommandLine = "candidate panic=0"
system := "1234"
for _, tc := range []struct {
which string
files [][]string
expCommandLine string
errMsg string
}{{
which: "current",
files: [][]string{
{"cmdline.extra", "cmdline extra"},
},
expCommandLine: "snapd_recovery_mode=recover snapd_recovery_system=1234 panic=-1 cmdline extra",
}, {
which: "candidate",
files: [][]string{
{"cmdline.extra", "cmdline extra"},
},
expCommandLine: "snapd_recovery_mode=recover snapd_recovery_system=1234 candidate panic=0 cmdline extra",
}, {
which: "current",
files: [][]string{
{"cmdline.full", "cmdline full"},
},
expCommandLine: "snapd_recovery_mode=recover snapd_recovery_system=1234 cmdline full",
}, {
which: "candidate",
files: [][]string{
{"cmdline.full", "cmdline full"},
},
expCommandLine: "snapd_recovery_mode=recover snapd_recovery_system=1234 cmdline full",
}, {
which: "candidate",
files: [][]string{
{"cmdline.extra", `bad-quote="`},
},
errMsg: `cannot use kernel command line from gadget: invalid kernel command line in cmdline.extra: unbalanced quoting`,
}} {
sf := snaptest.MakeTestSnapWithFiles(c, gadgetSnapYaml, append([][]string{
{"meta/snap.yaml", gadgetSnapYaml},
{"meta/gadget.yaml", mockGadgetYaml},
}, tc.files...))
var cmdline string
var err error
switch tc.which {
case "current":
cmdline, err = boot.ComposeRecoveryCommandLine(model, system, sf)
case "candidate":
cmdline, err = boot.ComposeCandidateRecoveryCommandLine(model, system, sf)
default:
c.Fatalf("unexpected command line type")
}
if tc.errMsg == "" {
c.Assert(err, IsNil)
c.Assert(cmdline, Equals, tc.expCommandLine)
} else {
c.Assert(err, ErrorMatches, tc.errMsg)
}
}
}
func (s *kernelCommandLineSuite) TestBootVarsForGadgetCommandLine(c *C) {
model := &gadgettest.ModelCharacteristics{}
mockGadgetYaml := `
volumes:
volumename:
bootloader: grub
`
for _, tc := range []struct {
errMsg string
files [][]string
cmdlineAppend string
expectedVars map[string]string
append []string
remove []string
}{{
files: [][]string{
{"cmdline.extra", "foo bar baz"},
},
expectedVars: map[string]string{
"snapd_extra_cmdline_args": "",
"snapd_full_cmdline_args": "default foo bar baz",
},
}, {
files: [][]string{
{"cmdline.extra", "snapd.debug=1"},
},
expectedVars: map[string]string{
"snapd_extra_cmdline_args": "",
"snapd_full_cmdline_args": "default snapd.debug=1",
},
}, {
files: [][]string{
{"cmdline.extra", "snapd_foo"},
},
errMsg: `cannot use kernel command line from gadget: invalid kernel command line in cmdline.extra: disallowed kernel argument \"snapd_foo\"`,
}, {
files: [][]string{
{"cmdline.full", "full foo bar baz"},
},
expectedVars: map[string]string{
"snapd_extra_cmdline_args": "",
"snapd_full_cmdline_args": "full foo bar baz",
},
}, {
cmdlineAppend: "foo bar baz",
expectedVars: map[string]string{
"snapd_extra_cmdline_args": "",
"snapd_full_cmdline_args": "default foo bar baz",
},
}, {
files: [][]string{
{"cmdline.extra", "foo bar baz"},
},
cmdlineAppend: "x=y z",
expectedVars: map[string]string{
"snapd_extra_cmdline_args": "",
"snapd_full_cmdline_args": "default foo bar baz x=y z",
},
}, {
files: [][]string{
{"cmdline.full", "full foo bar baz"},
},
cmdlineAppend: "x=y z",
expectedVars: map[string]string{
"snapd_extra_cmdline_args": "",
"snapd_full_cmdline_args": "full foo bar baz x=y z",
},
}, {
// with no arguments boot variables should be cleared
files: [][]string{},
expectedVars: map[string]string{
"snapd_extra_cmdline_args": "",
"snapd_full_cmdline_args": "default",
},
}, {
expectedVars: map[string]string{
"snapd_extra_cmdline_args": "",
"snapd_full_cmdline_args": `default bar baz=* "with spaces"`,
},
append: []string{"bar", "baz=*", `'"with spaces"'`},
}, {
expectedVars: map[string]string{
"snapd_extra_cmdline_args": "",
"snapd_full_cmdline_args": "nodefault",
},
append: []string{"nodefault"},
remove: []string{"default"},
}, {
expectedVars: map[string]string{
"snapd_extra_cmdline_args": "",
"snapd_full_cmdline_args": " ",
},
remove: []string{"default"},
}} {
gadgetYaml := mockGadgetYaml
if len(tc.append) > 0 || len(tc.remove) > 0 {
gadgetYaml = fmt.Sprintf("%skernel-cmdline:\n", gadgetYaml)
}
if len(tc.append) > 0 {
gadgetYaml = fmt.Sprintf("%s append:\n", gadgetYaml)
}
for _, append := range tc.append {
gadgetYaml = fmt.Sprintf("%s - %s\n", gadgetYaml, append)
}
if len(tc.remove) > 0 {
gadgetYaml = fmt.Sprintf("%s remove:\n", gadgetYaml)
}
for _, remove := range tc.remove {
gadgetYaml = fmt.Sprintf("%s - %s\n", gadgetYaml, remove)
}
sf := snaptest.MakeTestSnapWithFiles(c, gadgetSnapYaml, append([][]string{
{"meta/snap.yaml", gadgetSnapYaml},
{"meta/gadget.yaml", gadgetYaml},
}, tc.files...))
vars, err := boot.BootVarsForTrustedCommandLineFromGadget(sf, tc.cmdlineAppend, "default", model)
if tc.errMsg == "" {
c.Assert(err, IsNil)
c.Assert(vars, DeepEquals, tc.expectedVars)
} else {
c.Assert(err, ErrorMatches, tc.errMsg)
}
}
}
|