github.com/meulengracht/snapd@v0.0.0-20210719210640-8bde69bcc84e/boot/kernel_os.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2014-2019 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 boot
    21  
    22  import (
    23  	"fmt"
    24  
    25  	"github.com/snapcore/snapd/bootloader"
    26  	"github.com/snapcore/snapd/snap"
    27  )
    28  
    29  type coreBootParticipant struct {
    30  	s  snap.PlaceInfo
    31  	bs bootState
    32  }
    33  
    34  // ensure coreBootParticipant is a BootParticipant
    35  var _ BootParticipant = (*coreBootParticipant)(nil)
    36  
    37  func (*coreBootParticipant) IsTrivial() bool { return false }
    38  
    39  func (bp *coreBootParticipant) SetNextBoot() (rebootRequired bool, err error) {
    40  	const errPrefix = "cannot set next boot: %s"
    41  
    42  	rebootRequired, u, err := bp.bs.setNext(bp.s)
    43  	if err != nil {
    44  		return false, fmt.Errorf(errPrefix, err)
    45  	}
    46  
    47  	if u != nil {
    48  		if err := u.commit(); err != nil {
    49  			return false, fmt.Errorf(errPrefix, err)
    50  		}
    51  	}
    52  	return rebootRequired, nil
    53  }
    54  
    55  type coreKernel struct {
    56  	s     snap.PlaceInfo
    57  	bopts *bootloader.Options
    58  }
    59  
    60  // ensure coreKernel is a Kernel
    61  var _ BootKernel = (*coreKernel)(nil)
    62  
    63  func (*coreKernel) IsTrivial() bool { return false }
    64  
    65  func (k *coreKernel) RemoveKernelAssets() error {
    66  	// XXX: shouldn't we check the snap type?
    67  	bootloader, err := bootloader.Find("", k.bopts)
    68  	if err != nil {
    69  		return fmt.Errorf("cannot remove kernel assets: %s", err)
    70  	}
    71  
    72  	// ask bootloader to remove the kernel assets if needed
    73  	return bootloader.RemoveKernelAssets(k.s)
    74  }
    75  
    76  func (k *coreKernel) ExtractKernelAssets(snapf snap.Container) error {
    77  	bootloader, err := bootloader.Find("", k.bopts)
    78  	if err != nil {
    79  		return fmt.Errorf("cannot extract kernel assets: %s", err)
    80  	}
    81  	// ask bootloader to extract the kernel assets if needed
    82  	return bootloader.ExtractKernelAssets(k.s, snapf)
    83  }