From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id 2D40C15802E for ; Wed, 26 Jun 2024 20:07:07 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 3B3162BC045; Wed, 26 Jun 2024 20:07:03 +0000 (UTC) Received: from smtp.gentoo.org (woodpecker.gentoo.org [140.211.166.183]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id D55D12BC041 for ; Wed, 26 Jun 2024 20:07:02 +0000 (UTC) Message-ID: <476674df-a57c-4c4d-ad1e-487211a4daf0@gentoo.org> Date: Wed, 26 Jun 2024 22:06:59 +0200 Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-dev@lists.gentoo.org Reply-to: gentoo-dev@lists.gentoo.org X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: [gentoo-dev] [PATCH 1/5] mount-boot.eclass: check for ESP as well as /boot, split, eclass To: gentoo-dev@lists.gentoo.org References: <38ed17cb-950a-4abd-b904-537388dbe612@gentoo.org> Content-Language: en-US, nl-NL From: Andrew Nowa Ammerlaan Autocrypt: addr=andrewammerlaan@gentoo.org; keydata= xsBNBF3n3cUBCAC6uoDZ0XzaO29l8AzUblXQ5rxZI7nbGEnfFqjEQCK3oEXxsDa9Ez1myx3M ir53Vyx64Iz1Bq/TOS/PttgguPpiLggCpTTD2vavp5SwFmg272+P8bUJVJF2mMRm0OR/YPiA B5dNfcoLqKIj+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R5OizbqK 2CRAJY7la7TSv1lTD1cAEQEAAcLAfAQYAQgAJgIbDBYhBAb/U0G9gF2wvH0HpqGfY2zU7bzR BQJlNiHABQkJL3d7AAoJEKGfY2zU7bzRjDwH/1fp/87km2YYVgrfP1aWLjAA/TwcEVycRJQQ S9Q6xuzgD5AYhjzBSONoN46cwf+gla6xndY0lCawsZN7whtJ/DhqSZEfL0HgHkJ6T8FCXexf n1s6XmIAxqIrMmfsuOkAPLJIHzAAGzQX8DXcRSj1cIDUpa1Uy7ncVvI4EzJBRtJVJXIbl+53 NGauXU8ZuprPYkMSPuW3eHATFc0F5DhmlFUXh+HYYK+2QTO73TENMhngkrYcw63je5bRp/+f 72XFKlf1gXHK1ivg8nYueyUfrxZTBGKagusOiQeOao2I1uYcHoFhPYJrQWePMyZiYyB6PR0K DR4B/Ulo3v0eBXaaYzo= Organization: Gentoo Linux In-Reply-To: <38ed17cb-950a-4abd-b904-537388dbe612@gentoo.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Archives-Salt: b14f3175-6e7b-4506-8aa7-c64355d25e2e X-Archives-Hash: 135057ce1c985402358331eebf1d28db From 53f844361df57d480480b5e0ab0f35d2788ebf6a Mon Sep 17 00:00:00 2001 From: Andrew Ammerlaan Date: Tue, 25 Jun 2024 15:08:49 +0200 Subject: [PATCH] mount-boot.eclass: check for ESP as well as /boot, split eclass This eclass is used by when the dist-kernel has to re-installed. Depending on the configuration of sys-kernel/installkernel, the files may be installed to /boot or to the EFI System partition. Therefore, extend this eclass to check if the ESP is mounted read-write as well on UEFI platforms. Split off the main functions into a separate "inherit-safe" eclass so we can safely use it in dist-kernel-utils.eclass and linux-mod-r1.eclass. Signed-off-by: Andrew Ammerlaan --- eclass/mount-boot-utils.eclass | 109 +++++++++++++++++++++++++++++++++ eclass/mount-boot.eclass | 81 +++--------------------- 2 files changed, 118 insertions(+), 72 deletions(-) create mode 100644 eclass/mount-boot-utils.eclass diff --git a/eclass/mount-boot-utils.eclass b/eclass/mount-boot-utils.eclass new file mode 100644 index 0000000000000..06ea2254b0013 --- /dev/null +++ b/eclass/mount-boot-utils.eclass @@ -0,0 +1,109 @@ +# Copyright 1999-2024 Gentoo Authors +# Distributed under the terms of the GNU General Public License v2 + +# @ECLASS: mount-boot-utils.eclass +# @MAINTAINER: +# base-system@gentoo.org +# @SUPPORTED_EAPIS: 6 7 8 +# @BLURB: functions for packages that install files into /boot +# @DESCRIPTION: +# This eclass is really only useful for bootloaders and kernel installation. +# +# If the live system has a separate /boot partition or ESP configured, then this +# function tries to ensure that it's mounted in rw mode, exiting with an error +# if it can't. It does nothing if /boot and ESP isn't a separate partition. +# +# This eclass provides the functions used by mount-boot.eclass in an "inherit- +# safe" way. This allows these functions to be used in other eclasses cleanly. + +case ${EAPI} in + 6|7|8) ;; + *) die "${ECLASS}: EAPI ${EAPI:-0} not supported" ;; +esac + +# @FUNCTION: mount-boot_is_disabled +# @INTERNAL +# @DESCRIPTION: +# Detect whether the current environment/build settings are such that we do not +# want to mess with any mounts. +mount-boot_is_disabled() { + # Since this eclass only deals with /boot, skip things when EROOT is active. + if [[ ${EROOT:-/} != / ]] ; then + return 0 + fi + + # If we're only building a package, then there's no need to check things. + if [[ ${MERGE_TYPE} == buildonly ]] ; then + return 0 + fi + + # The user wants us to leave things be. + if [[ -n ${DONT_MOUNT_BOOT} ]] ; then + return 0 + fi + + # OK, we want to handle things ourselves. + return 1 +} + +# @FUNCTION: mount-boot_check_status +# @INTERNAL +# @DESCRIPTION: +# Check if /boot and ESP is sane, i.e., mounted as read-write if on a separate +# partition. Die if conditions are not fulfilled. If nonfatal is used, +# the function will return a non-zero status instead. +mount-boot_check_status() { + # Get out fast if possible. + mount-boot_is_disabled && return 0 + + local partition= + local part_is_not_mounted= + local part_is_read_only= + local candidates=( /boot ) + + # If system is booted with UEFI, check for ESP as well + if [[ -d /sys/firmware/efi ]]; then + # Use same candidates for ESP as installkernel and eclean-kernel + candidates+=( /efi /boot/efi /boot/EFI ) + fi + + for partition in ${candidates[@]}; do + # note that /dev/BOOT is in the Gentoo default /etc/fstab file + local fstabstate=$(awk "!/^[[:blank:]]*#|^\/dev\/BOOT/ && \$2 == \"${partition}\" \ + { print 1; exit }" /etc/fstab || die "awk failed") + + if [[ -z ${fstabstate} ]] ; then + einfo "Assuming you do not have a separate ${partition} partition." + else + local procstate=$(awk "\$2 == \"${partition}\" { split(\$4, a, \",\"); \ + for (i in a) if (a[i] ~ /^r[ow]\$/) { print a[i]; break }; exit }" \ + /proc/mounts || die "awk failed") + + if [[ -z ${procstate} ]] ; then + eerror "Your ${partition} partition is not mounted" + eerror "Please mount it and retry." + die -n "${partition} not mounted" + part_is_not_mounted=1 + else + if [[ ${procstate} == ro ]] ; then + eerror "Your ${partition} partition, was detected as being mounted," \ + "but is mounted read-only." + eerror "Please remount it as read-write and retry." + die -n "${partition} mounted read-only" + part_is_read_only=1 + else + einfo "Your ${partition} partition was detected as being mounted." + einfo "Files will be installed there for ${PN} to function correctly." + fi + fi + fi + done + + if [[ -n ${part_is_not_mounted} ]]; then + return 1 + elif [[ -n ${part_is_read_only} ]]; then + return 2 + else + return 0 + fi +} diff --git a/eclass/mount-boot.eclass b/eclass/mount-boot.eclass index 73beb9adea670..f4626ccb97a9c 100644 --- a/eclass/mount-boot.eclass +++ b/eclass/mount-boot.eclass @@ -1,4 +1,4 @@ -# Copyright 1999-2023 Gentoo Authors +# Copyright 1999-2024 Gentoo Authors # Distributed under the terms of the GNU General Public License v2 # @ECLASS: mount-boot.eclass @@ -7,84 +7,21 @@ # @SUPPORTED_EAPIS: 6 7 8 # @BLURB: functions for packages that install files into /boot # @DESCRIPTION: -# This eclass is really only useful for bootloaders. +# This eclass is really only useful for bootloaders and kernel installation. # -# If the live system has a separate /boot partition configured, then this -# function tries to ensure that it's mounted in rw mode, exiting with an -# error if it can't. It does nothing if /boot isn't a separate partition. +# If the live system has a separate /boot partition or ESP configured, then this +# function tries to ensure that it's mounted in rw mode, exiting with an error +# if it can't. It does nothing if /boot and ESP isn't a separate partition. +# +# This eclass exports the functions provided by mount-boot-utils.eclass to +# the pkg_pretend and pkg_{pre,post}{inst,rm} phases. case ${EAPI} in 6|7|8) ;; *) die "${ECLASS}: EAPI ${EAPI:-0} not supported" ;; esac -# @FUNCTION: mount-boot_is_disabled -# @INTERNAL -# @DESCRIPTION: -# Detect whether the current environment/build settings are such that we do not -# want to mess with any mounts. -mount-boot_is_disabled() { - # Since this eclass only deals with /boot, skip things when EROOT is active. - if [[ ${EROOT:-/} != / ]] ; then - return 0 - fi - - # If we're only building a package, then there's no need to check things. - if [[ ${MERGE_TYPE} == buildonly ]] ; then - return 0 - fi - - # The user wants us to leave things be. - if [[ -n ${DONT_MOUNT_BOOT} ]] ; then - return 0 - fi - - # OK, we want to handle things ourselves. - return 1 -} - -# @FUNCTION: mount-boot_check_status -# @INTERNAL -# @DESCRIPTION: -# Check if /boot is sane, i.e., mounted as read-write if on a separate -# partition. Die if conditions are not fulfilled. If nonfatal is used, -# the function will return a non-zero status instead. -mount-boot_check_status() { - # Get out fast if possible. - mount-boot_is_disabled && return 0 - - # note that /dev/BOOT is in the Gentoo default /etc/fstab file - local fstabstate=$(awk '!/^[[:blank:]]*#|^\/dev\/BOOT/ && $2 == "/boot" \ - { print 1; exit }' /etc/fstab || die "awk failed") - - if [[ -z ${fstabstate} ]] ; then - einfo "Assuming you do not have a separate /boot partition." - return 0 - fi - - local procstate=$(awk '$2 == "/boot" { split($4, a, ","); \ - for (i in a) if (a[i] ~ /^r[ow]$/) { print a[i]; break }; exit }' \ - /proc/mounts || die "awk failed") - - if [[ -z ${procstate} ]] ; then - eerror "Your boot partition is not mounted at /boot." - eerror "Please mount it and retry." - die -n "/boot not mounted" - return 1 - fi - - if [[ ${procstate} == ro ]] ; then - eerror "Your boot partition, detected as being mounted at /boot," \ - "is read-only." - eerror "Please remount it as read-write and retry." - die -n "/boot mounted read-only" - return 2 - fi - - einfo "Your boot partition was detected as being mounted at /boot." - einfo "Files will be installed there for ${PN} to function correctly." - return 0 -} +inherit mount-boot-utils mount-boot_pkg_pretend() { mount-boot_check_status