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 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
|
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2014-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"
"io/ioutil"
"os"
"path/filepath"
. "gopkg.in/check.v1"
"github.com/snapcore/snapd/asserts"
"github.com/snapcore/snapd/boot"
"github.com/snapcore/snapd/boot/boottest"
"github.com/snapcore/snapd/bootloader"
"github.com/snapcore/snapd/bootloader/assets"
"github.com/snapcore/snapd/bootloader/bootloadertest"
"github.com/snapcore/snapd/bootloader/grubenv"
"github.com/snapcore/snapd/bootloader/ubootenv"
"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/gadget"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/secboot"
"github.com/snapcore/snapd/seed"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/snap/snapfile"
"github.com/snapcore/snapd/snap/snaptest"
"github.com/snapcore/snapd/testutil"
"github.com/snapcore/snapd/timings"
)
type makeBootableSuite struct {
baseBootenvSuite
bootloader *bootloadertest.MockBootloader
}
var _ = Suite(&makeBootableSuite{})
func (s *makeBootableSuite) SetUpTest(c *C) {
s.baseBootenvSuite.SetUpTest(c)
s.bootloader = bootloadertest.Mock("mock", c.MkDir())
s.forceBootloader(s.bootloader)
}
func makeSnap(c *C, name, yaml string, revno snap.Revision) (fn string, info *snap.Info) {
return makeSnapWithFiles(c, name, yaml, revno, nil)
}
func makeSnapWithFiles(c *C, name, yaml string, revno snap.Revision, files [][]string) (fn string, info *snap.Info) {
si := &snap.SideInfo{
RealName: name,
Revision: revno,
}
fn = snaptest.MakeTestSnapWithFiles(c, yaml, files)
snapf, err := snapfile.Open(fn)
c.Assert(err, IsNil)
info, err = snap.ReadInfoFromSnapFile(snapf, si)
c.Assert(err, IsNil)
return fn, info
}
func (s *makeBootableSuite) TestMakeBootable(c *C) {
bootloader.Force(nil)
model := boottest.MakeMockModel()
grubCfg := []byte("#grub cfg")
unpackedGadgetDir := c.MkDir()
err := ioutil.WriteFile(filepath.Join(unpackedGadgetDir, "grub.conf"), grubCfg, 0644)
c.Assert(err, IsNil)
seedSnapsDirs := filepath.Join(s.rootdir, "/var/lib/snapd/seed", "snaps")
err = os.MkdirAll(seedSnapsDirs, 0755)
c.Assert(err, IsNil)
baseFn, baseInfo := makeSnap(c, "core18", `name: core18
type: base
version: 4.0
`, snap.R(3))
baseInSeed := filepath.Join(seedSnapsDirs, baseInfo.Filename())
err = os.Rename(baseFn, baseInSeed)
c.Assert(err, IsNil)
kernelFn, kernelInfo := makeSnap(c, "pc-kernel", `name: pc-kernel
type: kernel
version: 4.0
`, snap.R(5))
kernelInSeed := filepath.Join(seedSnapsDirs, kernelInfo.Filename())
err = os.Rename(kernelFn, kernelInSeed)
c.Assert(err, IsNil)
bootWith := &boot.BootableSet{
Base: baseInfo,
BasePath: baseInSeed,
Kernel: kernelInfo,
KernelPath: kernelInSeed,
UnpackedGadgetDir: unpackedGadgetDir,
}
err = boot.MakeBootable(model, s.rootdir, bootWith, nil)
c.Assert(err, IsNil)
// check the bootloader config
seedGenv := grubenv.NewEnv(filepath.Join(s.rootdir, "boot/grub/grubenv"))
c.Assert(seedGenv.Load(), IsNil)
c.Check(seedGenv.Get("snap_kernel"), Equals, "pc-kernel_5.snap")
c.Check(seedGenv.Get("snap_core"), Equals, "core18_3.snap")
c.Check(seedGenv.Get("snap_menuentry"), Equals, "My Model")
// check symlinks from snap blob dir
kernelBlob := filepath.Join(dirs.SnapBlobDirUnder(s.rootdir), kernelInfo.Filename())
dst, err := os.Readlink(filepath.Join(dirs.SnapBlobDirUnder(s.rootdir), kernelInfo.Filename()))
c.Assert(err, IsNil)
c.Check(dst, Equals, "../seed/snaps/pc-kernel_5.snap")
c.Check(kernelBlob, testutil.FilePresent)
baseBlob := filepath.Join(dirs.SnapBlobDirUnder(s.rootdir), baseInfo.Filename())
dst, err = os.Readlink(filepath.Join(dirs.SnapBlobDirUnder(s.rootdir), baseInfo.Filename()))
c.Assert(err, IsNil)
c.Check(dst, Equals, "../seed/snaps/core18_3.snap")
c.Check(baseBlob, testutil.FilePresent)
// check that the bootloader (grub here) configuration was copied
c.Check(filepath.Join(s.rootdir, "boot", "grub/grub.cfg"), testutil.FileEquals, grubCfg)
}
type makeBootable20Suite struct {
baseBootenvSuite
bootloader *bootloadertest.MockRecoveryAwareBootloader
}
type makeBootable20UbootSuite struct {
baseBootenvSuite
bootloader *bootloadertest.MockExtractedRecoveryKernelImageBootloader
}
var _ = Suite(&makeBootable20Suite{})
var _ = Suite(&makeBootable20UbootSuite{})
func (s *makeBootable20Suite) SetUpTest(c *C) {
s.baseBootenvSuite.SetUpTest(c)
s.bootloader = bootloadertest.Mock("mock", c.MkDir()).RecoveryAware()
s.forceBootloader(s.bootloader)
}
func (s *makeBootable20UbootSuite) SetUpTest(c *C) {
s.baseBootenvSuite.SetUpTest(c)
s.bootloader = bootloadertest.Mock("mock", c.MkDir()).ExtractedRecoveryKernelImage()
s.forceBootloader(s.bootloader)
}
func (s *makeBootable20Suite) TestMakeBootable20(c *C) {
bootloader.Force(nil)
model := boottest.MakeMockUC20Model()
unpackedGadgetDir := c.MkDir()
grubRecoveryCfg := []byte("#grub-recovery cfg")
grubRecoveryCfgAsset := []byte("#grub-recovery cfg from assets")
err := ioutil.WriteFile(filepath.Join(unpackedGadgetDir, "grub-recovery.conf"), grubRecoveryCfg, 0644)
restore := assets.MockInternal("grub-recovery.cfg", grubRecoveryCfgAsset)
defer restore()
c.Assert(err, IsNil)
grubCfg := []byte("#grub cfg")
err = ioutil.WriteFile(filepath.Join(unpackedGadgetDir, "grub.conf"), grubCfg, 0644)
c.Assert(err, IsNil)
// on uc20 the seed layout if different
seedSnapsDirs := filepath.Join(s.rootdir, "/snaps")
err = os.MkdirAll(seedSnapsDirs, 0755)
c.Assert(err, IsNil)
baseFn, baseInfo := makeSnap(c, "core20", `name: core20
type: base
version: 5.0
`, snap.R(3))
baseInSeed := filepath.Join(seedSnapsDirs, baseInfo.Filename())
err = os.Rename(baseFn, baseInSeed)
c.Assert(err, IsNil)
kernelFn, kernelInfo := makeSnapWithFiles(c, "pc-kernel", `name: pc-kernel
type: kernel
version: 5.0
`, snap.R(5), [][]string{
{"kernel.efi", "I'm a kernel.efi"},
})
kernelInSeed := filepath.Join(seedSnapsDirs, kernelInfo.Filename())
err = os.Rename(kernelFn, kernelInSeed)
c.Assert(err, IsNil)
label := "20191209"
recoverySystemDir := filepath.Join("/systems", label)
bootWith := &boot.BootableSet{
Base: baseInfo,
BasePath: baseInSeed,
Kernel: kernelInfo,
KernelPath: kernelInSeed,
RecoverySystemDir: recoverySystemDir,
RecoverySystemLabel: label,
UnpackedGadgetDir: unpackedGadgetDir,
Recovery: true,
}
err = boot.MakeBootable(model, s.rootdir, bootWith, nil)
c.Assert(err, IsNil)
// ensure only a single file got copied (the grub.cfg)
files, err := filepath.Glob(filepath.Join(s.rootdir, "EFI/ubuntu/*"))
c.Assert(err, IsNil)
// grub.cfg and grubenv
c.Check(files, HasLen, 2)
// check that the recovery bootloader configuration was installed with
// the correct content
c.Check(filepath.Join(s.rootdir, "EFI/ubuntu/grub.cfg"), testutil.FileEquals, grubRecoveryCfgAsset)
// ensure no /boot was setup
c.Check(filepath.Join(s.rootdir, "boot"), testutil.FileAbsent)
// ensure the correct recovery system configuration was set
seedGenv := grubenv.NewEnv(filepath.Join(s.rootdir, "EFI/ubuntu/grubenv"))
c.Assert(seedGenv.Load(), IsNil)
c.Check(seedGenv.Get("snapd_recovery_system"), Equals, label)
systemGenv := grubenv.NewEnv(filepath.Join(s.rootdir, recoverySystemDir, "grubenv"))
c.Assert(systemGenv.Load(), IsNil)
c.Check(systemGenv.Get("snapd_recovery_kernel"), Equals, "/snaps/pc-kernel_5.snap")
}
func (s *makeBootable20Suite) TestMakeBootable20UnsetRecoverySystemLabelError(c *C) {
model := boottest.MakeMockUC20Model()
unpackedGadgetDir := c.MkDir()
grubRecoveryCfg := []byte("#grub-recovery cfg")
err := ioutil.WriteFile(filepath.Join(unpackedGadgetDir, "grub-recovery.conf"), grubRecoveryCfg, 0644)
c.Assert(err, IsNil)
grubCfg := []byte("#grub cfg")
err = ioutil.WriteFile(filepath.Join(unpackedGadgetDir, "grub.conf"), grubCfg, 0644)
c.Assert(err, IsNil)
label := "20191209"
recoverySystemDir := filepath.Join("/systems", label)
bootWith := &boot.BootableSet{
RecoverySystemDir: recoverySystemDir,
UnpackedGadgetDir: unpackedGadgetDir,
Recovery: true,
}
err = boot.MakeBootable(model, s.rootdir, bootWith, nil)
c.Assert(err, ErrorMatches, "internal error: recovery system label unset")
}
func (s *makeBootable20Suite) TestMakeBootable20MultipleRecoverySystemsError(c *C) {
model := boottest.MakeMockUC20Model()
bootWith := &boot.BootableSet{Recovery: true}
err := os.MkdirAll(filepath.Join(s.rootdir, "systems/20191204"), 0755)
c.Assert(err, IsNil)
err = os.MkdirAll(filepath.Join(s.rootdir, "systems/20191205"), 0755)
c.Assert(err, IsNil)
err = boot.MakeBootable(model, s.rootdir, bootWith, nil)
c.Assert(err, ErrorMatches, "cannot make multiple recovery systems bootable yet")
}
func (s *makeBootable20Suite) TestMakeBootable20RunMode(c *C) {
bootloader.Force(nil)
model := boottest.MakeMockUC20Model()
seedSnapsDirs := filepath.Join(s.rootdir, "/snaps")
err := os.MkdirAll(seedSnapsDirs, 0755)
c.Assert(err, IsNil)
// grub on ubuntu-seed
mockSeedGrubDir := filepath.Join(boot.InitramfsUbuntuSeedDir, "EFI", "ubuntu")
mockSeedGrubCfg := filepath.Join(mockSeedGrubDir, "grub.cfg")
err = os.MkdirAll(filepath.Dir(mockSeedGrubCfg), 0755)
c.Assert(err, IsNil)
err = ioutil.WriteFile(mockSeedGrubCfg, []byte("# Snapd-Boot-Config-Edition: 1\n"), 0644)
c.Assert(err, IsNil)
// setup recovery boot assets
err = os.MkdirAll(filepath.Join(boot.InitramfsUbuntuSeedDir, "EFI/boot"), 0755)
c.Assert(err, IsNil)
// SHA3-384: 39efae6545f16e39633fbfbef0d5e9fdd45a25d7df8764978ce4d81f255b038046a38d9855e42e5c7c4024e153fd2e37
err = ioutil.WriteFile(filepath.Join(boot.InitramfsUbuntuSeedDir, "EFI/boot/bootx64.efi"),
[]byte("recovery shim content"), 0644)
c.Assert(err, IsNil)
// SHA3-384: aa3c1a83e74bf6dd40dd64e5c5bd1971d75cdf55515b23b9eb379f66bf43d4661d22c4b8cf7d7a982d2013ab65c1c4c5
err = ioutil.WriteFile(filepath.Join(boot.InitramfsUbuntuSeedDir, "EFI/boot/grubx64.efi"),
[]byte("recovery grub content"), 0644)
c.Assert(err, IsNil)
// grub on ubuntu-boot
mockBootGrubDir := filepath.Join(boot.InitramfsUbuntuBootDir, "EFI", "ubuntu")
mockBootGrubCfg := filepath.Join(mockBootGrubDir, "grub.cfg")
err = os.MkdirAll(filepath.Dir(mockBootGrubCfg), 0755)
c.Assert(err, IsNil)
err = ioutil.WriteFile(mockBootGrubCfg, nil, 0644)
c.Assert(err, IsNil)
unpackedGadgetDir := c.MkDir()
grubRecoveryCfg := []byte("#grub-recovery cfg")
grubRecoveryCfgAsset := []byte("#grub-recovery cfg from assets")
err = ioutil.WriteFile(filepath.Join(unpackedGadgetDir, "grub-recovery.conf"), grubRecoveryCfg, 0644)
c.Assert(err, IsNil)
restore := assets.MockInternal("grub-recovery.cfg", grubRecoveryCfgAsset)
defer restore()
grubCfg := []byte("#grub cfg")
err = ioutil.WriteFile(filepath.Join(unpackedGadgetDir, "grub.conf"), grubCfg, 0644)
c.Assert(err, IsNil)
grubCfgAsset := []byte("# Snapd-Boot-Config-Edition: 1\n#grub cfg from assets")
restore = assets.MockInternal("grub.cfg", grubCfgAsset)
defer restore()
err = ioutil.WriteFile(filepath.Join(unpackedGadgetDir, "bootx64.efi"), []byte("shim content"), 0644)
c.Assert(err, IsNil)
err = ioutil.WriteFile(filepath.Join(unpackedGadgetDir, "grubx64.efi"), []byte("grub content"), 0644)
c.Assert(err, IsNil)
// make the snaps symlinks so that we can ensure that makebootable follows
// the symlinks and copies the files and not the symlinks
baseFn, baseInfo := makeSnap(c, "core20", `name: core20
type: base
version: 5.0
`, snap.R(3))
baseInSeed := filepath.Join(seedSnapsDirs, baseInfo.Filename())
err = os.Symlink(baseFn, baseInSeed)
c.Assert(err, IsNil)
kernelFn, kernelInfo := makeSnapWithFiles(c, "pc-kernel", `name: pc-kernel
type: kernel
version: 5.0
`, snap.R(5),
[][]string{
{"kernel.efi", "I'm a kernel.efi"},
},
)
kernelInSeed := filepath.Join(seedSnapsDirs, kernelInfo.Filename())
err = os.Symlink(kernelFn, kernelInSeed)
c.Assert(err, IsNil)
bootWith := &boot.BootableSet{
RecoverySystemDir: "20191216",
BasePath: baseInSeed,
Base: baseInfo,
KernelPath: kernelInSeed,
Kernel: kernelInfo,
Recovery: false,
UnpackedGadgetDir: unpackedGadgetDir,
}
// set up observer state
useEncryption := true
obs, err := boot.TrustedAssetsInstallObserverForModel(model, unpackedGadgetDir, useEncryption)
c.Assert(obs, NotNil)
c.Assert(err, IsNil)
runBootStruct := &gadget.LaidOutStructure{
VolumeStructure: &gadget.VolumeStructure{
Role: gadget.SystemBoot,
},
}
// only grubx64.efi gets installed to system-boot
_, err = obs.Observe(gadget.ContentWrite, runBootStruct, boot.InitramfsUbuntuBootDir, "EFI/boot/grubx64.efi",
&gadget.ContentChange{After: filepath.Join(unpackedGadgetDir, "grubx64.efi")})
c.Assert(err, IsNil)
// observe recovery assets
err = obs.ObserveExistingTrustedRecoveryAssets(boot.InitramfsUbuntuSeedDir)
c.Assert(err, IsNil)
// set encryption key
myKey := secboot.EncryptionKey{}
myKey2 := secboot.EncryptionKey{}
for i := range myKey {
myKey[i] = byte(i)
myKey2[i] = byte(128 + i)
}
obs.ChosenEncryptionKeys(myKey, myKey2)
// set a mock recovery kernel
readSystemEssentialCalls := 0
restore = boot.MockSeedReadSystemEssential(func(seedDir, label string, essentialTypes []snap.Type, tm timings.Measurer) (*asserts.Model, []*seed.Snap, error) {
readSystemEssentialCalls++
kernelSnap := &seed.Snap{
Path: "/var/lib/snapd/seed/snaps/pc-kernel_1.snap",
SideInfo: &snap.SideInfo{
Revision: snap.Revision{N: 1},
RealName: "pc-kernel",
},
}
return model, []*seed.Snap{kernelSnap}, nil
})
defer restore()
// set mock key sealing
sealKeysCalls := 0
restore = boot.MockSecbootSealKeys(func(keys []secboot.SealKeyRequest, params *secboot.SealKeysParams) error {
sealKeysCalls++
switch sealKeysCalls {
case 1:
c.Check(keys, HasLen, 1)
c.Check(keys[0].Key, DeepEquals, myKey)
case 2:
c.Check(keys, HasLen, 2)
c.Check(keys[0].Key, DeepEquals, myKey)
c.Check(keys[1].Key, DeepEquals, myKey2)
default:
c.Errorf("unexpected additional call to secboot.SealKeys (call # %d)", sealKeysCalls)
}
c.Assert(params.ModelParams, HasLen, 1)
shim := bootloader.NewBootFile("", filepath.Join(s.rootdir,
"var/lib/snapd/boot-assets/grub/bootx64.efi-39efae6545f16e39633fbfbef0d5e9fdd45a25d7df8764978ce4d81f255b038046a38d9855e42e5c7c4024e153fd2e37"),
bootloader.RoleRecovery)
grub := bootloader.NewBootFile("", filepath.Join(s.rootdir,
"var/lib/snapd/boot-assets/grub/grubx64.efi-aa3c1a83e74bf6dd40dd64e5c5bd1971d75cdf55515b23b9eb379f66bf43d4661d22c4b8cf7d7a982d2013ab65c1c4c5"),
bootloader.RoleRecovery)
runGrub := bootloader.NewBootFile("", filepath.Join(s.rootdir,
"var/lib/snapd/boot-assets/grub/grubx64.efi-5ee042c15e104b825d6bc15c41cdb026589f1ec57ed966dd3f29f961d4d6924efc54b187743fa3a583b62722882d405d"),
bootloader.RoleRunMode)
kernel := bootloader.NewBootFile("/var/lib/snapd/seed/snaps/pc-kernel_1.snap", "kernel.efi", bootloader.RoleRecovery)
runKernel := bootloader.NewBootFile(filepath.Join(s.rootdir, "var/lib/snapd/snaps/pc-kernel_5.snap"), "kernel.efi", bootloader.RoleRunMode)
switch sealKeysCalls {
case 1:
c.Assert(params.ModelParams[0].EFILoadChains, DeepEquals, []*secboot.LoadChain{
secboot.NewLoadChain(shim, secboot.NewLoadChain(grub, secboot.NewLoadChain(kernel))),
secboot.NewLoadChain(shim, secboot.NewLoadChain(grub, secboot.NewLoadChain(runGrub, secboot.NewLoadChain(runKernel)))),
})
c.Assert(params.ModelParams[0].KernelCmdlines, DeepEquals, []string{
"snapd_recovery_mode=recover snapd_recovery_system=20191216 console=ttyS0 console=tty1 panic=-1",
"snapd_recovery_mode=run console=ttyS0 console=tty1 panic=-1",
})
case 2:
c.Assert(params.ModelParams[0].EFILoadChains, DeepEquals, []*secboot.LoadChain{
secboot.NewLoadChain(shim, secboot.NewLoadChain(grub, secboot.NewLoadChain(kernel))),
})
c.Assert(params.ModelParams[0].KernelCmdlines, DeepEquals, []string{
"snapd_recovery_mode=recover snapd_recovery_system=20191216 console=ttyS0 console=tty1 panic=-1",
})
default:
c.Errorf("unexpected additional call to secboot.SealKeys (call # %d)", sealKeysCalls)
}
c.Assert(params.ModelParams[0].Model.DisplayName(), Equals, "My Model")
return nil
})
defer restore()
err = boot.MakeBootable(model, s.rootdir, bootWith, obs)
c.Assert(err, IsNil)
// ensure grub.cfg in boot was installed from internal assets
c.Check(mockBootGrubCfg, testutil.FileEquals, string(grubCfgAsset))
// ensure base/kernel got copied to /var/lib/snapd/snaps
core20Snap := filepath.Join(dirs.SnapBlobDirUnder(boot.InstallHostWritableDir), "core20_3.snap")
pcKernelSnap := filepath.Join(dirs.SnapBlobDirUnder(boot.InstallHostWritableDir), "pc-kernel_5.snap")
c.Check(core20Snap, testutil.FilePresent)
c.Check(pcKernelSnap, testutil.FilePresent)
c.Check(osutil.IsSymlink(core20Snap), Equals, false)
c.Check(osutil.IsSymlink(pcKernelSnap), Equals, false)
// ensure the bootvars got updated the right way
mockSeedGrubenv := filepath.Join(mockSeedGrubDir, "grubenv")
c.Check(mockSeedGrubenv, testutil.FilePresent)
c.Check(mockSeedGrubenv, testutil.FileContains, "snapd_recovery_mode=run")
mockBootGrubenv := filepath.Join(mockBootGrubDir, "grubenv")
c.Check(mockBootGrubenv, testutil.FilePresent)
// ensure that kernel_status is empty, we specifically want this to be set
// to the empty string
// use (?m) to match multi-line file in the regex here, because the file is
// a grubenv with padding #### blocks
c.Check(mockBootGrubenv, testutil.FileMatches, `(?m)^kernel_status=$`)
// check that we have the extracted kernel in the right places, both in the
// old uc16/uc18 location and the new ubuntu-boot partition grub dir
extractedKernel := filepath.Join(mockBootGrubDir, "pc-kernel_5.snap", "kernel.efi")
c.Check(extractedKernel, testutil.FilePresent)
// the new uc20 location
extractedKernelSymlink := filepath.Join(mockBootGrubDir, "kernel.efi")
c.Check(extractedKernelSymlink, testutil.FilePresent)
// ensure modeenv looks correct
ubuntuDataModeEnvPath := filepath.Join(s.rootdir, "/run/mnt/ubuntu-data/system-data/var/lib/snapd/modeenv")
c.Check(ubuntuDataModeEnvPath, testutil.FileEquals, `mode=run
recovery_system=20191216
current_recovery_systems=20191216
base=core20_3.snap
current_kernels=pc-kernel_5.snap
model=my-brand/my-model-uc20
grade=dangerous
current_trusted_boot_assets={"grubx64.efi":["5ee042c15e104b825d6bc15c41cdb026589f1ec57ed966dd3f29f961d4d6924efc54b187743fa3a583b62722882d405d"]}
current_trusted_recovery_boot_assets={"bootx64.efi":["39efae6545f16e39633fbfbef0d5e9fdd45a25d7df8764978ce4d81f255b038046a38d9855e42e5c7c4024e153fd2e37"],"grubx64.efi":["aa3c1a83e74bf6dd40dd64e5c5bd1971d75cdf55515b23b9eb379f66bf43d4661d22c4b8cf7d7a982d2013ab65c1c4c5"]}
current_kernel_command_lines=["snapd_recovery_mode=run console=ttyS0 console=tty1 panic=-1"]
`)
copiedGrubBin := filepath.Join(
dirs.SnapBootAssetsDirUnder(boot.InstallHostWritableDir),
"grub",
"grubx64.efi-5ee042c15e104b825d6bc15c41cdb026589f1ec57ed966dd3f29f961d4d6924efc54b187743fa3a583b62722882d405d",
)
copiedRecoveryGrubBin := filepath.Join(
dirs.SnapBootAssetsDirUnder(boot.InstallHostWritableDir),
"grub",
"grubx64.efi-aa3c1a83e74bf6dd40dd64e5c5bd1971d75cdf55515b23b9eb379f66bf43d4661d22c4b8cf7d7a982d2013ab65c1c4c5",
)
copiedRecoveryShimBin := filepath.Join(
dirs.SnapBootAssetsDirUnder(boot.InstallHostWritableDir),
"grub",
"bootx64.efi-39efae6545f16e39633fbfbef0d5e9fdd45a25d7df8764978ce4d81f255b038046a38d9855e42e5c7c4024e153fd2e37",
)
// only one file in the cache under new root
checkContentGlob(c, filepath.Join(dirs.SnapBootAssetsDirUnder(boot.InstallHostWritableDir), "grub", "*"), []string{
copiedRecoveryShimBin,
copiedGrubBin,
copiedRecoveryGrubBin,
})
// with the right content
c.Check(copiedGrubBin, testutil.FileEquals, "grub content")
c.Check(copiedRecoveryGrubBin, testutil.FileEquals, "recovery grub content")
c.Check(copiedRecoveryShimBin, testutil.FileEquals, "recovery shim content")
// make sure SealKey was called for the run object and the fallback object
c.Check(sealKeysCalls, Equals, 2)
// make sure the marker file for sealed key was created
c.Check(filepath.Join(dirs.SnapFDEDirUnder(boot.InstallHostWritableDir), "sealed-keys"), testutil.FilePresent)
// make sure we wrote the boot chains data file
c.Check(filepath.Join(dirs.SnapFDEDirUnder(boot.InstallHostWritableDir), "boot-chains"), testutil.FilePresent)
}
func (s *makeBootable20Suite) TestMakeBootable20RunModeInstallBootConfigErr(c *C) {
bootloader.Force(nil)
model := boottest.MakeMockUC20Model()
seedSnapsDirs := filepath.Join(s.rootdir, "/snaps")
err := os.MkdirAll(seedSnapsDirs, 0755)
c.Assert(err, IsNil)
// grub on ubuntu-seed
mockSeedGrubDir := filepath.Join(boot.InitramfsUbuntuSeedDir, "EFI", "ubuntu")
err = os.MkdirAll(mockSeedGrubDir, 0755)
c.Assert(err, IsNil)
// no recovery grub.cfg so that test fails if it ever reaches that point
// grub on ubuntu-boot
mockBootGrubDir := filepath.Join(boot.InitramfsUbuntuBootDir, "EFI", "ubuntu")
mockBootGrubCfg := filepath.Join(mockBootGrubDir, "grub.cfg")
err = os.MkdirAll(filepath.Dir(mockBootGrubCfg), 0755)
c.Assert(err, IsNil)
err = ioutil.WriteFile(mockBootGrubCfg, nil, 0644)
c.Assert(err, IsNil)
unpackedGadgetDir := c.MkDir()
// make the snaps symlinks so that we can ensure that makebootable follows
// the symlinks and copies the files and not the symlinks
baseFn, baseInfo := makeSnap(c, "core20", `name: core20
type: base
version: 5.0
`, snap.R(3))
baseInSeed := filepath.Join(seedSnapsDirs, baseInfo.Filename())
err = os.Symlink(baseFn, baseInSeed)
c.Assert(err, IsNil)
kernelFn, kernelInfo := makeSnapWithFiles(c, "pc-kernel", `name: pc-kernel
type: kernel
version: 5.0
`, snap.R(5),
[][]string{
{"kernel.efi", "I'm a kernel.efi"},
},
)
kernelInSeed := filepath.Join(seedSnapsDirs, kernelInfo.Filename())
err = os.Symlink(kernelFn, kernelInSeed)
c.Assert(err, IsNil)
bootWith := &boot.BootableSet{
RecoverySystemDir: "20191216",
BasePath: baseInSeed,
Base: baseInfo,
KernelPath: kernelInSeed,
Kernel: kernelInfo,
Recovery: false,
UnpackedGadgetDir: unpackedGadgetDir,
}
// no grub marker in gadget directory raises an error
err = boot.MakeBootable(model, s.rootdir, bootWith, nil)
c.Assert(err, ErrorMatches, "internal error: cannot identify run system bootloader: cannot determine bootloader")
// set up grub.cfg in gadget
grubCfg := []byte("#grub cfg")
err = ioutil.WriteFile(filepath.Join(unpackedGadgetDir, "grub.conf"), grubCfg, 0644)
c.Assert(err, IsNil)
// no write access to destination directory
restore := assets.MockInternal("grub.cfg", nil)
defer restore()
err = boot.MakeBootable(model, s.rootdir, bootWith, nil)
c.Assert(err, ErrorMatches, `cannot install managed bootloader assets: internal error: no boot asset for "grub.cfg"`)
}
func (s *makeBootable20Suite) TestMakeBootable20RunModeSealKeyErr(c *C) {
bootloader.Force(nil)
model := boottest.MakeMockUC20Model()
seedSnapsDirs := filepath.Join(s.rootdir, "/snaps")
err := os.MkdirAll(seedSnapsDirs, 0755)
c.Assert(err, IsNil)
// grub on ubuntu-seed
mockSeedGrubDir := filepath.Join(boot.InitramfsUbuntuSeedDir, "EFI", "ubuntu")
mockSeedGrubCfg := filepath.Join(mockSeedGrubDir, "grub.cfg")
err = os.MkdirAll(filepath.Dir(mockSeedGrubCfg), 0755)
c.Assert(err, IsNil)
err = ioutil.WriteFile(mockSeedGrubCfg, []byte("# Snapd-Boot-Config-Edition: 1\n"), 0644)
c.Assert(err, IsNil)
// setup recovery boot assets
err = os.MkdirAll(filepath.Join(boot.InitramfsUbuntuSeedDir, "EFI/boot"), 0755)
c.Assert(err, IsNil)
// SHA3-384: 39efae6545f16e39633fbfbef0d5e9fdd45a25d7df8764978ce4d81f255b038046a38d9855e42e5c7c4024e153fd2e37
err = ioutil.WriteFile(filepath.Join(boot.InitramfsUbuntuSeedDir, "EFI/boot/bootx64.efi"),
[]byte("recovery shim content"), 0644)
c.Assert(err, IsNil)
// SHA3-384: aa3c1a83e74bf6dd40dd64e5c5bd1971d75cdf55515b23b9eb379f66bf43d4661d22c4b8cf7d7a982d2013ab65c1c4c5
err = ioutil.WriteFile(filepath.Join(boot.InitramfsUbuntuSeedDir, "EFI/boot/grubx64.efi"),
[]byte("recovery grub content"), 0644)
c.Assert(err, IsNil)
// grub on ubuntu-boot
mockBootGrubDir := filepath.Join(boot.InitramfsUbuntuBootDir, "EFI", "ubuntu")
mockBootGrubCfg := filepath.Join(mockBootGrubDir, "grub.cfg")
err = os.MkdirAll(filepath.Dir(mockBootGrubCfg), 0755)
c.Assert(err, IsNil)
err = ioutil.WriteFile(mockBootGrubCfg, nil, 0644)
c.Assert(err, IsNil)
unpackedGadgetDir := c.MkDir()
grubRecoveryCfg := []byte("#grub-recovery cfg")
grubRecoveryCfgAsset := []byte("#grub-recovery cfg from assets")
err = ioutil.WriteFile(filepath.Join(unpackedGadgetDir, "grub-recovery.conf"), grubRecoveryCfg, 0644)
c.Assert(err, IsNil)
restore := assets.MockInternal("grub-recovery.cfg", grubRecoveryCfgAsset)
defer restore()
grubCfg := []byte("#grub cfg")
err = ioutil.WriteFile(filepath.Join(unpackedGadgetDir, "grub.conf"), grubCfg, 0644)
c.Assert(err, IsNil)
grubCfgAsset := []byte("# Snapd-Boot-Config-Edition: 1\n#grub cfg from assets")
restore = assets.MockInternal("grub.cfg", grubCfgAsset)
defer restore()
err = ioutil.WriteFile(filepath.Join(unpackedGadgetDir, "bootx64.efi"), []byte("shim content"), 0644)
c.Assert(err, IsNil)
err = ioutil.WriteFile(filepath.Join(unpackedGadgetDir, "grubx64.efi"), []byte("grub content"), 0644)
c.Assert(err, IsNil)
// make the snaps symlinks so that we can ensure that makebootable follows
// the symlinks and copies the files and not the symlinks
baseFn, baseInfo := makeSnap(c, "core20", `name: core20
type: base
version: 5.0
`, snap.R(3))
baseInSeed := filepath.Join(seedSnapsDirs, baseInfo.Filename())
err = os.Symlink(baseFn, baseInSeed)
c.Assert(err, IsNil)
kernelFn, kernelInfo := makeSnapWithFiles(c, "pc-kernel", `name: pc-kernel
type: kernel
version: 5.0
`, snap.R(5),
[][]string{
{"kernel.efi", "I'm a kernel.efi"},
},
)
kernelInSeed := filepath.Join(seedSnapsDirs, kernelInfo.Filename())
err = os.Symlink(kernelFn, kernelInSeed)
c.Assert(err, IsNil)
bootWith := &boot.BootableSet{
RecoverySystemDir: "20191216",
BasePath: baseInSeed,
Base: baseInfo,
KernelPath: kernelInSeed,
Kernel: kernelInfo,
Recovery: false,
UnpackedGadgetDir: unpackedGadgetDir,
}
// set up observer state
useEncryption := true
obs, err := boot.TrustedAssetsInstallObserverForModel(model, unpackedGadgetDir, useEncryption)
c.Assert(obs, NotNil)
c.Assert(err, IsNil)
runBootStruct := &gadget.LaidOutStructure{
VolumeStructure: &gadget.VolumeStructure{
Role: gadget.SystemBoot,
},
}
// only grubx64.efi gets installed to system-boot
_, err = obs.Observe(gadget.ContentWrite, runBootStruct, boot.InitramfsUbuntuBootDir, "EFI/boot/grubx64.efi",
&gadget.ContentChange{After: filepath.Join(unpackedGadgetDir, "grubx64.efi")})
c.Assert(err, IsNil)
// observe recovery assets
err = obs.ObserveExistingTrustedRecoveryAssets(boot.InitramfsUbuntuSeedDir)
c.Assert(err, IsNil)
// set encryption key
myKey := secboot.EncryptionKey{}
myKey2 := secboot.EncryptionKey{}
for i := range myKey {
myKey[i] = byte(i)
myKey2[i] = byte(128 + i)
}
obs.ChosenEncryptionKeys(myKey, myKey2)
// set a mock recovery kernel
readSystemEssentialCalls := 0
restore = boot.MockSeedReadSystemEssential(func(seedDir, label string, essentialTypes []snap.Type, tm timings.Measurer) (*asserts.Model, []*seed.Snap, error) {
readSystemEssentialCalls++
kernelSnap := &seed.Snap{
Path: "/var/lib/snapd/seed/snaps/pc-kernel_1.snap",
SideInfo: &snap.SideInfo{
Revision: snap.Revision{N: 0},
RealName: "pc-kernel",
},
}
return model, []*seed.Snap{kernelSnap}, nil
})
defer restore()
// set mock key sealing
sealKeysCalls := 0
restore = boot.MockSecbootSealKeys(func(keys []secboot.SealKeyRequest, params *secboot.SealKeysParams) error {
sealKeysCalls++
switch sealKeysCalls {
case 1:
c.Check(keys, HasLen, 1)
c.Check(keys[0].Key, DeepEquals, myKey)
case 2:
c.Check(keys, HasLen, 2)
c.Check(keys[0].Key, DeepEquals, myKey)
c.Check(keys[1].Key, DeepEquals, myKey2)
default:
c.Errorf("unexpected additional call to secboot.SealKeys (call # %d)", sealKeysCalls)
}
c.Assert(params.ModelParams, HasLen, 1)
shim := bootloader.NewBootFile("", filepath.Join(s.rootdir,
"var/lib/snapd/boot-assets/grub/bootx64.efi-39efae6545f16e39633fbfbef0d5e9fdd45a25d7df8764978ce4d81f255b038046a38d9855e42e5c7c4024e153fd2e37"),
bootloader.RoleRecovery)
grub := bootloader.NewBootFile("", filepath.Join(s.rootdir,
"var/lib/snapd/boot-assets/grub/grubx64.efi-aa3c1a83e74bf6dd40dd64e5c5bd1971d75cdf55515b23b9eb379f66bf43d4661d22c4b8cf7d7a982d2013ab65c1c4c5"),
bootloader.RoleRecovery)
runGrub := bootloader.NewBootFile("", filepath.Join(s.rootdir,
"var/lib/snapd/boot-assets/grub/grubx64.efi-5ee042c15e104b825d6bc15c41cdb026589f1ec57ed966dd3f29f961d4d6924efc54b187743fa3a583b62722882d405d"),
bootloader.RoleRunMode)
kernel := bootloader.NewBootFile("/var/lib/snapd/seed/snaps/pc-kernel_1.snap", "kernel.efi", bootloader.RoleRecovery)
runKernel := bootloader.NewBootFile(filepath.Join(s.rootdir, "var/lib/snapd/snaps/pc-kernel_5.snap"), "kernel.efi", bootloader.RoleRunMode)
c.Assert(params.ModelParams[0].EFILoadChains, DeepEquals, []*secboot.LoadChain{
secboot.NewLoadChain(shim, secboot.NewLoadChain(grub, secboot.NewLoadChain(kernel))),
secboot.NewLoadChain(shim, secboot.NewLoadChain(grub, secboot.NewLoadChain(runGrub, secboot.NewLoadChain(runKernel)))),
})
c.Assert(params.ModelParams[0].KernelCmdlines, DeepEquals, []string{
"snapd_recovery_mode=recover snapd_recovery_system=20191216 console=ttyS0 console=tty1 panic=-1",
"snapd_recovery_mode=run console=ttyS0 console=tty1 panic=-1",
})
c.Assert(params.ModelParams[0].Model.DisplayName(), Equals, "My Model")
return fmt.Errorf("seal error")
})
defer restore()
err = boot.MakeBootable(model, s.rootdir, bootWith, obs)
c.Assert(err, ErrorMatches, "cannot seal the encryption keys: seal error")
}
func (s *makeBootable20UbootSuite) TestUbootMakeBootable20TraditionalUbootenvFails(c *C) {
bootloader.Force(nil)
model := boottest.MakeMockUC20Model()
unpackedGadgetDir := c.MkDir()
ubootEnv := []byte("#uboot env")
err := ioutil.WriteFile(filepath.Join(unpackedGadgetDir, "uboot.conf"), ubootEnv, 0644)
c.Assert(err, IsNil)
// on uc20 the seed layout if different
seedSnapsDirs := filepath.Join(s.rootdir, "/snaps")
err = os.MkdirAll(seedSnapsDirs, 0755)
c.Assert(err, IsNil)
baseFn, baseInfo := makeSnap(c, "core20", `name: core20
type: base
version: 5.0
`, snap.R(3))
baseInSeed := filepath.Join(seedSnapsDirs, baseInfo.Filename())
err = os.Rename(baseFn, baseInSeed)
c.Assert(err, IsNil)
kernelFn, kernelInfo := makeSnapWithFiles(c, "arm-kernel", `name: arm-kernel
type: kernel
version: 5.0
`, snap.R(5), [][]string{
{"kernel.img", "I'm a kernel"},
{"initrd.img", "...and I'm an initrd"},
{"dtbs/foo.dtb", "foo dtb"},
{"dtbs/bar.dto", "bar dtbo"},
})
kernelInSeed := filepath.Join(seedSnapsDirs, kernelInfo.Filename())
err = os.Rename(kernelFn, kernelInSeed)
c.Assert(err, IsNil)
label := "20191209"
recoverySystemDir := filepath.Join("/systems", label)
bootWith := &boot.BootableSet{
Base: baseInfo,
BasePath: baseInSeed,
Kernel: kernelInfo,
KernelPath: kernelInSeed,
RecoverySystemDir: recoverySystemDir,
RecoverySystemLabel: label,
UnpackedGadgetDir: unpackedGadgetDir,
Recovery: true,
}
// TODO:UC20: enable this use case
err = boot.MakeBootable(model, s.rootdir, bootWith, nil)
c.Assert(err, ErrorMatches, "non-empty uboot.env not supported on UC20 yet")
}
func (s *makeBootable20UbootSuite) TestUbootMakeBootable20BootScr(c *C) {
model := boottest.MakeMockUC20Model()
unpackedGadgetDir := c.MkDir()
// the uboot.conf must be empty for this to work/do the right thing
err := ioutil.WriteFile(filepath.Join(unpackedGadgetDir, "uboot.conf"), nil, 0644)
c.Assert(err, IsNil)
// on uc20 the seed layout if different
seedSnapsDirs := filepath.Join(s.rootdir, "/snaps")
err = os.MkdirAll(seedSnapsDirs, 0755)
c.Assert(err, IsNil)
baseFn, baseInfo := makeSnap(c, "core20", `name: core20
type: base
version: 5.0
`, snap.R(3))
baseInSeed := filepath.Join(seedSnapsDirs, baseInfo.Filename())
err = os.Rename(baseFn, baseInSeed)
c.Assert(err, IsNil)
kernelFn, kernelInfo := makeSnapWithFiles(c, "arm-kernel", `name: arm-kernel
type: kernel
version: 5.0
`, snap.R(5), [][]string{
{"kernel.img", "I'm a kernel"},
{"initrd.img", "...and I'm an initrd"},
{"dtbs/foo.dtb", "foo dtb"},
{"dtbs/bar.dto", "bar dtbo"},
})
kernelInSeed := filepath.Join(seedSnapsDirs, kernelInfo.Filename())
err = os.Rename(kernelFn, kernelInSeed)
c.Assert(err, IsNil)
label := "20191209"
recoverySystemDir := filepath.Join("/systems", label)
bootWith := &boot.BootableSet{
Base: baseInfo,
BasePath: baseInSeed,
Kernel: kernelInfo,
KernelPath: kernelInSeed,
RecoverySystemDir: recoverySystemDir,
RecoverySystemLabel: label,
UnpackedGadgetDir: unpackedGadgetDir,
Recovery: true,
}
err = boot.MakeBootable(model, s.rootdir, bootWith, nil)
c.Assert(err, IsNil)
// since uboot.conf was absent, we won't have installed the uboot.env, as
// it is expected that the gadget assets would have installed boot.scr
// instead
c.Check(filepath.Join(s.rootdir, "uboot.env"), testutil.FileAbsent)
c.Check(s.bootloader.BootVars, DeepEquals, map[string]string{
"snapd_recovery_system": label,
"snapd_recovery_mode": "install",
})
// ensure the correct recovery system configuration was set
c.Check(
s.bootloader.ExtractRecoveryKernelAssetsCalls,
DeepEquals,
[]bootloadertest.ExtractedRecoveryKernelCall{{
RecoverySystemDir: recoverySystemDir,
S: kernelInfo,
}},
)
}
func (s *makeBootable20UbootSuite) TestUbootMakeBootable20RunModeBootSel(c *C) {
bootloader.Force(nil)
model := boottest.MakeMockUC20Model()
seedSnapsDirs := filepath.Join(s.rootdir, "/snaps")
err := os.MkdirAll(seedSnapsDirs, 0755)
c.Assert(err, IsNil)
// uboot on ubuntu-seed
mockSeedUbootBootSel := filepath.Join(boot.InitramfsUbuntuSeedDir, "uboot/ubuntu/boot.sel")
err = os.MkdirAll(filepath.Dir(mockSeedUbootBootSel), 0755)
c.Assert(err, IsNil)
env, err := ubootenv.Create(mockSeedUbootBootSel, 4096)
c.Assert(err, IsNil)
c.Assert(env.Save(), IsNil)
// uboot on ubuntu-boot (as if it was installed when creating the partition)
mockBootUbootBootSel := filepath.Join(boot.InitramfsUbuntuBootDir, "uboot/ubuntu/boot.sel")
err = os.MkdirAll(filepath.Dir(mockBootUbootBootSel), 0755)
c.Assert(err, IsNil)
env, err = ubootenv.Create(mockBootUbootBootSel, 4096)
c.Assert(err, IsNil)
c.Assert(env.Save(), IsNil)
unpackedGadgetDir := c.MkDir()
c.Assert(ioutil.WriteFile(filepath.Join(unpackedGadgetDir, "uboot.conf"), nil, 0644), IsNil)
baseFn, baseInfo := makeSnap(c, "core20", `name: core20
type: base
version: 5.0
`, snap.R(3))
baseInSeed := filepath.Join(seedSnapsDirs, baseInfo.Filename())
err = os.Rename(baseFn, baseInSeed)
c.Assert(err, IsNil)
kernelSnapFiles := [][]string{
{"kernel.img", "I'm a kernel"},
{"initrd.img", "...and I'm an initrd"},
{"dtbs/foo.dtb", "foo dtb"},
{"dtbs/bar.dto", "bar dtbo"},
}
kernelFn, kernelInfo := makeSnapWithFiles(c, "arm-kernel", `name: arm-kernel
type: kernel
version: 5.0
`, snap.R(5), kernelSnapFiles)
kernelInSeed := filepath.Join(seedSnapsDirs, kernelInfo.Filename())
err = os.Rename(kernelFn, kernelInSeed)
c.Assert(err, IsNil)
bootWith := &boot.BootableSet{
RecoverySystemDir: "20191216",
BasePath: baseInSeed,
Base: baseInfo,
KernelPath: kernelInSeed,
Kernel: kernelInfo,
Recovery: false,
UnpackedGadgetDir: unpackedGadgetDir,
}
err = boot.MakeBootable(model, s.rootdir, bootWith, nil)
c.Assert(err, IsNil)
// ensure base/kernel got copied to /var/lib/snapd/snaps
c.Check(filepath.Join(dirs.SnapBlobDirUnder(boot.InstallHostWritableDir), "core20_3.snap"), testutil.FilePresent)
c.Check(filepath.Join(dirs.SnapBlobDirUnder(boot.InstallHostWritableDir), "arm-kernel_5.snap"), testutil.FilePresent)
// ensure the bootvars on ubuntu-seed got updated the right way
mockSeedUbootenv := filepath.Join(boot.InitramfsUbuntuSeedDir, "uboot/ubuntu/boot.sel")
uenvSeed, err := ubootenv.Open(mockSeedUbootenv)
c.Assert(err, IsNil)
c.Assert(uenvSeed.Get("snapd_recovery_mode"), Equals, "run")
// now check ubuntu-boot boot.sel
mockBootUbootenv := filepath.Join(boot.InitramfsUbuntuBootDir, "uboot/ubuntu/boot.sel")
uenvBoot, err := ubootenv.Open(mockBootUbootenv)
c.Assert(err, IsNil)
c.Assert(uenvBoot.Get("snap_try_kernel"), Equals, "")
c.Assert(uenvBoot.Get("snap_kernel"), Equals, "arm-kernel_5.snap")
c.Assert(uenvBoot.Get("kernel_status"), Equals, boot.DefaultStatus)
// check that we have the extracted kernel in the right places, in the
// old uc16/uc18 location
for _, file := range kernelSnapFiles {
fName := file[0]
c.Check(filepath.Join(boot.InitramfsUbuntuBootDir, "uboot/ubuntu/arm-kernel_5.snap", fName), testutil.FilePresent)
}
// ensure modeenv looks correct
ubuntuDataModeEnvPath := filepath.Join(s.rootdir, "/run/mnt/ubuntu-data/system-data/var/lib/snapd/modeenv")
c.Check(ubuntuDataModeEnvPath, testutil.FileEquals, `mode=run
recovery_system=20191216
current_recovery_systems=20191216
base=core20_3.snap
current_kernels=arm-kernel_5.snap
model=my-brand/my-model-uc20
grade=dangerous
`)
}
|