-
Notifications
You must be signed in to change notification settings - Fork 584
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
c/snap-bootstrap: split CVM related functionality in separate files #14789
Open
sespiros
wants to merge
2
commits into
canonical:master
Choose a base branch
from
sespiros:snap-bootstrap-split-cvm-mode
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+284
−203
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// -*- Mode: Go; indent-tabs-mode: t -*- | ||
|
||
/* | ||
* Copyright (C) 2019-2024 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 main | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
|
||
"github.com/snapcore/snapd/asserts" | ||
"github.com/snapcore/snapd/boot" | ||
"github.com/snapcore/snapd/osutil/disks" | ||
"github.com/snapcore/snapd/secboot" | ||
) | ||
|
||
var ( | ||
secbootProvisionForCVM func(initramfsUbuntuSeedDir string) error | ||
) | ||
|
||
// XXX: workaround for the lack of model in CVM systems | ||
type genericCVMModel struct{} | ||
|
||
func (*genericCVMModel) Classic() bool { | ||
return true | ||
} | ||
|
||
func (*genericCVMModel) Grade() asserts.ModelGrade { | ||
return "signed" | ||
} | ||
|
||
func generateMountsModeRunCVM(mst *initramfsMountsState) error { | ||
// Mount ESP as UbuntuSeedDir which has UEFI label | ||
if err := mountNonDataPartitionMatchingKernelDisk(boot.InitramfsUbuntuSeedDir, "UEFI"); err != nil { | ||
return err | ||
} | ||
|
||
// get the disk that we mounted the ESP from as a reference | ||
// point for future mounts | ||
disk, err := disks.DiskFromMountPoint(boot.InitramfsUbuntuSeedDir, nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Mount rootfs | ||
if err := secbootProvisionForCVM(boot.InitramfsUbuntuSeedDir); err != nil { | ||
return err | ||
} | ||
runModeCVMKey := filepath.Join(boot.InitramfsSeedEncryptionKeyDir, "cloudimg-rootfs.sealed-key") | ||
opts := &secboot.UnlockVolumeUsingSealedKeyOptions{ | ||
AllowRecoveryKey: true, | ||
} | ||
unlockRes, err := secbootUnlockVolumeUsingSealedKeyIfEncrypted(disk, "cloudimg-rootfs", runModeCVMKey, opts) | ||
if err != nil { | ||
return err | ||
} | ||
fsckSystemdOpts := &systemdMountOptions{ | ||
NeedsFsck: true, | ||
Ephemeral: true, | ||
} | ||
if err := doSystemdMount(unlockRes.FsDevice, boot.InitramfsDataDir, fsckSystemdOpts); err != nil { | ||
return err | ||
} | ||
|
||
// Verify that cloudimg-rootfs comes from where we expect it to | ||
diskOpts := &disks.Options{} | ||
if unlockRes.IsEncrypted { | ||
// then we need to specify that the data mountpoint is | ||
// expected to be a decrypted device | ||
diskOpts.IsDecryptedDevice = true | ||
} | ||
|
||
matches, err := disk.MountPointIsFromDisk(boot.InitramfsDataDir, diskOpts) | ||
if err != nil { | ||
return err | ||
} | ||
if !matches { | ||
// failed to verify that cloudimg-rootfs mountpoint | ||
// comes from the same disk as ESP | ||
return fmt.Errorf("cannot validate boot: cloudimg-rootfs mountpoint is expected to be from disk %s but is not", disk.Dev()) | ||
} | ||
|
||
// Unmount ESP because otherwise unmounting is racy and results in booted systems without ESP | ||
if err := doSystemdMount("", boot.InitramfsUbuntuSeedDir, &systemdMountOptions{Umount: true, Ephemeral: true}); err != nil { | ||
return err | ||
} | ||
|
||
// There is no real model on a CVM device but minimal model | ||
// information is required by the later code | ||
mst.SetVerifiedBootModel(&genericCVMModel{}) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
// -*- Mode: Go; indent-tabs-mode: t -*- | ||
|
||
/* | ||
* Copyright (C) 2019-2024 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 main_test | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
|
||
. "gopkg.in/check.v1" | ||
|
||
"github.com/snapcore/snapd/boot" | ||
main "github.com/snapcore/snapd/cmd/snap-bootstrap" | ||
"github.com/snapcore/snapd/osutil/disks" | ||
"github.com/snapcore/snapd/secboot" | ||
"github.com/snapcore/snapd/testutil" | ||
) | ||
|
||
var ( | ||
defaultCVMDisk = &disks.MockDiskMapping{ | ||
Structure: []disks.Partition{ | ||
seedPart, | ||
cvmEncPart, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we move cvmEncPart to this file too? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, I missed that one. Added. |
||
}, | ||
DiskHasPartitions: true, | ||
DevNum: "defaultCVMDev", | ||
} | ||
) | ||
|
||
type initramfsCVMMountsSuite struct { | ||
baseInitramfsMountsSuite | ||
} | ||
|
||
var _ = Suite(&initramfsCVMMountsSuite{}) | ||
|
||
func (s *initramfsCVMMountsSuite) SetUpTest(c *C) { | ||
s.baseInitramfsMountsSuite.SetUpTest(c) | ||
s.AddCleanup(main.MockSecbootProvisionForCVM(func(_ string) error { | ||
return nil | ||
})) | ||
} | ||
|
||
func (s *initramfsCVMMountsSuite) TestInitramfsMountsRunCVMModeHappy(c *C) { | ||
s.mockProcCmdlineContent(c, "snapd_recovery_mode=cloudimg-rootfs") | ||
|
||
restore := main.MockPartitionUUIDForBootedKernelDisk("specific-ubuntu-seed-partuuid") | ||
defer restore() | ||
|
||
restore = disks.MockMountPointDisksToPartitionMapping( | ||
map[disks.Mountpoint]*disks.MockDiskMapping{ | ||
{Mountpoint: boot.InitramfsUbuntuSeedDir}: defaultCVMDisk, | ||
{Mountpoint: boot.InitramfsDataDir, IsDecryptedDevice: true}: defaultCVMDisk, | ||
}, | ||
) | ||
defer restore() | ||
|
||
// don't do anything from systemd-mount, we verify the arguments passed at | ||
// the end with cmd.Calls | ||
cmd := testutil.MockCommand(c, "systemd-mount", ``) | ||
defer cmd.Restore() | ||
|
||
// mock that in turn, /run/mnt/ubuntu-boot, /run/mnt/ubuntu-seed, etc. are | ||
// mounted | ||
n := 0 | ||
restore = main.MockOsutilIsMounted(func(where string) (bool, error) { | ||
n++ | ||
switch n { | ||
// first call for each mount returns false, then returns true, this | ||
// tests in the case where systemd is racy / inconsistent and things | ||
// aren't mounted by the time systemd-mount returns | ||
case 1, 2: | ||
c.Assert(where, Equals, boot.InitramfsUbuntuSeedDir) | ||
case 3, 4: | ||
c.Assert(where, Equals, boot.InitramfsDataDir) | ||
case 5, 6: | ||
c.Assert(where, Equals, boot.InitramfsUbuntuSeedDir) | ||
default: | ||
c.Errorf("unexpected IsMounted check on %s", where) | ||
return false, fmt.Errorf("unexpected IsMounted check on %s", where) | ||
} | ||
return n%2 == 0, nil | ||
}) | ||
defer restore() | ||
|
||
// Mock the call to TPMCVM, to ensure that TPM provisioning is | ||
// done before unlock attempt | ||
provisionTPMCVMCalled := false | ||
restore = main.MockSecbootProvisionForCVM(func(_ string) error { | ||
// Ensure this function is only called once | ||
c.Assert(provisionTPMCVMCalled, Equals, false) | ||
provisionTPMCVMCalled = true | ||
return nil | ||
}) | ||
defer restore() | ||
|
||
cloudimgActivated := false | ||
restore = main.MockSecbootUnlockVolumeUsingSealedKeyIfEncrypted(func(disk disks.Disk, name string, sealedEncryptionKeyFile string, opts *secboot.UnlockVolumeUsingSealedKeyOptions) (secboot.UnlockResult, error) { | ||
c.Assert(provisionTPMCVMCalled, Equals, true) | ||
c.Assert(name, Equals, "cloudimg-rootfs") | ||
c.Assert(sealedEncryptionKeyFile, Equals, filepath.Join(s.tmpDir, "run/mnt/ubuntu-seed/device/fde/cloudimg-rootfs.sealed-key")) | ||
c.Assert(opts.AllowRecoveryKey, Equals, true) | ||
c.Assert(opts.WhichModel, IsNil) | ||
|
||
cloudimgActivated = true | ||
// return true because we are using an encrypted device | ||
return happyUnlocked("cloudimg-rootfs", secboot.UnlockedWithSealedKey), nil | ||
}) | ||
defer restore() | ||
|
||
_, err := main.Parser().ParseArgs([]string{"initramfs-mounts"}) | ||
c.Assert(err, IsNil) | ||
c.Check(s.Stdout.String(), Equals, "") | ||
|
||
// 2 per mountpoint + 1 more for cross check | ||
c.Assert(n, Equals, 5) | ||
|
||
// failed to use mockSystemdMountSequence way of asserting this | ||
// note that other test cases also mix & match using | ||
// mockSystemdMountSequence & DeepEquals | ||
c.Assert(cmd.Calls(), DeepEquals, [][]string{ | ||
{ | ||
"systemd-mount", | ||
"/dev/disk/by-partuuid/specific-ubuntu-seed-partuuid", | ||
boot.InitramfsUbuntuSeedDir, | ||
"--no-pager", | ||
"--no-ask-password", | ||
"--fsck=yes", | ||
"--options=private", | ||
"--property=Before=initrd-fs.target", | ||
}, | ||
{ | ||
"systemd-mount", | ||
"/dev/mapper/cloudimg-rootfs-random", | ||
boot.InitramfsDataDir, | ||
"--no-pager", | ||
"--no-ask-password", | ||
"--fsck=yes", | ||
}, | ||
{ | ||
"systemd-mount", | ||
boot.InitramfsUbuntuSeedDir, | ||
"--umount", | ||
"--no-pager", | ||
"--no-ask-password", | ||
"--fsck=no", | ||
}, | ||
}) | ||
|
||
c.Check(provisionTPMCVMCalled, Equals, true) | ||
c.Check(cloudimgActivated, Equals, true) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably a good idea to have a doc comment attached to this one
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added.