github.com/Lephar/snapd@v0.0.0-20210825215435-c7fba9cef4d2/boot/booted_kernel_partition_linux.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 boot
    21  
    22  import (
    23  	"fmt"
    24  	"strings"
    25  
    26  	"github.com/snapcore/snapd/bootloader/efi"
    27  )
    28  
    29  const (
    30  	// note the vendor ID 4a67b082-0a4c-41cf-b6c7-440b29bb8c4f is systemd, this
    31  	// variable is populated by shim
    32  	loaderDevicePartUUID = "LoaderDevicePartUUID-4a67b082-0a4c-41cf-b6c7-440b29bb8c4f"
    33  )
    34  
    35  // FindPartitionUUIDForBootedKernelDisk returns the partition uuid for the
    36  // partition that the booted kernel is located on.
    37  func FindPartitionUUIDForBootedKernelDisk() (string, error) {
    38  	// try efi variables first
    39  	partuuid, _, err := efi.ReadVarString(loaderDevicePartUUID)
    40  	if err == nil {
    41  		// the LoaderDevicePartUUID is in all caps, but lsblk,
    42  		// etc. use lower case so for consistency just make it
    43  		// lower case here too
    44  		return strings.ToLower(partuuid), nil
    45  	}
    46  	if err == efi.ErrNoEFISystem {
    47  		return "", err
    48  	}
    49  
    50  	// TODO:UC20: use the kernel command line parameter from the little kernel
    51  	//            bootloader if we have a littlekernel bootloader
    52  
    53  	// TODO:UC20: add more fallbacks here, even on amd64, when we don't have efi
    54  	//            i.e. on bios?
    55  	return "", fmt.Errorf("could not find partition uuid for booted kernel: %v", err)
    56  }