github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/cmd/snap-bootstrap/initramfs_mounts_state.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2020 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 main
    21  
    22  import (
    23  	"errors"
    24  	"fmt"
    25  	"os"
    26  	"path/filepath"
    27  
    28  	"github.com/snapcore/snapd/asserts"
    29  	"github.com/snapcore/snapd/boot"
    30  	"github.com/snapcore/snapd/seed"
    31  	"github.com/snapcore/snapd/snap"
    32  	"github.com/snapcore/snapd/timings"
    33  )
    34  
    35  // initramfsMountsState helps tracking the state and progress
    36  // of the mounting driving process.
    37  type initramfsMountsState struct {
    38  	mode           string
    39  	recoverySystem string
    40  }
    41  
    42  var errRunModeNoImpliedRecoverySystem = errors.New("internal error: no implied recovery system in run mode")
    43  
    44  // ReadEssential returns the model and verified essential
    45  // snaps from the recoverySystem. If recoverySystem is "" the
    46  // implied one will be used (only for modes other than run).
    47  func (mst *initramfsMountsState) ReadEssential(recoverySystem string, essentialTypes []snap.Type) (*asserts.Model, []*seed.Snap, error) {
    48  	if recoverySystem == "" {
    49  		if mst.mode == "run" {
    50  			return nil, nil, errRunModeNoImpliedRecoverySystem
    51  		}
    52  		recoverySystem = mst.recoverySystem
    53  	}
    54  
    55  	perf := timings.New(nil)
    56  	return seed.ReadSystemEssential(boot.InitramfsUbuntuSeedDir, recoverySystem, essentialTypes, perf)
    57  }
    58  
    59  // UnverifiedBootModel returns the unverified model from the
    60  // boot partition for run mode. The current and only use case
    61  // is measuring the model for run mode. Otherwise no decisions
    62  // should be based on an unverified model. Note that the model
    63  // is verified at the time the key auth policy is computed.
    64  func (mst *initramfsMountsState) UnverifiedBootModel() (*asserts.Model, error) {
    65  	if mst.mode != "run" {
    66  		return nil, fmt.Errorf("internal error: unverified boot model access is for limited run mode use")
    67  	}
    68  
    69  	mf, err := os.Open(filepath.Join(boot.InitramfsUbuntuBootDir, "model"))
    70  	if err != nil {
    71  		return nil, fmt.Errorf("cannot read model assertion: %v", err)
    72  	}
    73  	defer mf.Close()
    74  	ma, err := asserts.NewDecoder(mf).Decode()
    75  	if err != nil {
    76  		return nil, fmt.Errorf("cannot decode assertion: %v", err)
    77  	}
    78  	if ma.Type() != asserts.ModelType {
    79  		return nil, fmt.Errorf("unexpected assertion: %q", ma.Type().Name)
    80  	}
    81  	return ma.(*asserts.Model), nil
    82  }