github.com/rigado/snapd@v2.42.5-go-mod+incompatible/boot/kernel_os.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2014-2015 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  	"path/filepath"
    25  
    26  	"github.com/snapcore/snapd/bootloader"
    27  	"github.com/snapcore/snapd/logger"
    28  	"github.com/snapcore/snapd/snap"
    29  )
    30  
    31  type coreBootParticipant struct {
    32  	s snap.PlaceInfo
    33  	t snap.Type
    34  }
    35  
    36  // ensure coreBootParticipant is a BootParticipant
    37  var _ BootParticipant = (*coreBootParticipant)(nil)
    38  
    39  func (*coreBootParticipant) IsTrivial() bool { return false }
    40  
    41  func (bs *coreBootParticipant) SetNextBoot() error {
    42  	bootloader, err := bootloader.Find("", nil)
    43  	if err != nil {
    44  		return fmt.Errorf("cannot set next boot: %s", err)
    45  	}
    46  
    47  	var nextBoot, goodBoot string
    48  	switch bs.t {
    49  	case snap.TypeOS, snap.TypeBase:
    50  		nextBoot = "snap_try_core"
    51  		goodBoot = "snap_core"
    52  	case snap.TypeKernel:
    53  		nextBoot = "snap_try_kernel"
    54  		goodBoot = "snap_kernel"
    55  	}
    56  	blobName := filepath.Base(bs.s.MountFile())
    57  
    58  	// check if we actually need to do anything, i.e. the exact same
    59  	// kernel/core revision got installed again (e.g. firstboot)
    60  	// and we are not in any special boot mode
    61  	m, err := bootloader.GetBootVars("snap_mode", goodBoot)
    62  	if err != nil {
    63  		return fmt.Errorf("cannot set next boot: %s", err)
    64  	}
    65  	if m[goodBoot] == blobName {
    66  		// If we were in anything but default ("") mode before
    67  		// and now switch to the good core/kernel again, make
    68  		// sure to clean the snap_mode here. This also
    69  		// mitigates https://forum.snapcraft.io/t/5253
    70  		if m["snap_mode"] != "" {
    71  			return bootloader.SetBootVars(map[string]string{
    72  				"snap_mode": "",
    73  				nextBoot:    "",
    74  			})
    75  		}
    76  		return nil
    77  	}
    78  
    79  	return bootloader.SetBootVars(map[string]string{
    80  		nextBoot:    blobName,
    81  		"snap_mode": "try",
    82  	})
    83  }
    84  
    85  func (bs *coreBootParticipant) ChangeRequiresReboot() bool {
    86  	bootloader, err := bootloader.Find("", nil)
    87  	if err != nil {
    88  		logger.Noticef("cannot get boot settings: %s", err)
    89  		return false
    90  	}
    91  
    92  	var nextBoot, goodBoot string
    93  	switch bs.t {
    94  	case snap.TypeKernel:
    95  		nextBoot = "snap_try_kernel"
    96  		goodBoot = "snap_kernel"
    97  	case snap.TypeOS, snap.TypeBase:
    98  		nextBoot = "snap_try_core"
    99  		goodBoot = "snap_core"
   100  	}
   101  
   102  	m, err := bootloader.GetBootVars(nextBoot, goodBoot)
   103  	if err != nil {
   104  		logger.Noticef("cannot get boot variables: %s", err)
   105  		return false
   106  	}
   107  
   108  	squashfsName := filepath.Base(bs.s.MountFile())
   109  	if m[nextBoot] == squashfsName && m[goodBoot] != m[nextBoot] {
   110  		return true
   111  	}
   112  
   113  	return false
   114  }
   115  
   116  type coreKernel struct {
   117  	s snap.PlaceInfo
   118  }
   119  
   120  // ensure coreKernel is a Kernel
   121  var _ BootKernel = (*coreKernel)(nil)
   122  
   123  func (*coreKernel) IsTrivial() bool { return false }
   124  
   125  func (k *coreKernel) RemoveKernelAssets() error {
   126  	// XXX: shouldn't we check the snap type?
   127  	bootloader, err := bootloader.Find("", nil)
   128  	if err != nil {
   129  		return fmt.Errorf("cannot remove kernel assets: %s", err)
   130  	}
   131  
   132  	// ask bootloader to remove the kernel assets if needed
   133  	return bootloader.RemoveKernelAssets(k.s)
   134  }
   135  
   136  func (k *coreKernel) ExtractKernelAssets(snapf snap.Container) error {
   137  	bootloader, err := bootloader.Find("", nil)
   138  	if err != nil {
   139  		return fmt.Errorf("cannot extract kernel assets: %s", err)
   140  	}
   141  
   142  	// ask bootloader to extract the kernel assets if needed
   143  	return bootloader.ExtractKernelAssets(k.s, snapf)
   144  }