Gentoo Archives: gentoo-dev

From: "Ulrich Müller" <ulm@g.o>
To: gentoo-dev@l.g.o
Subject: [gentoo-dev] [PATCH v4] mount-boot.eclass: Check if /boot is sane, but don't try to mount it.
Date: Sat, 07 Dec 2019 09:10:29
Message-Id: w6gh82c4i2x.fsf@kph.uni-mainz.de
1 The eclass failed to remount a read-only mounted /boot, because package
2 collision sanity checks in recent Portage versions prevented it from
3 reaching pkg_preinst() at all. Furthermore, with the "mount-sandbox"
4 feature enabled, the mount won't be propagated past pkg_preinst() and
5 installed files would end up under the (shadowed) mount point.
6
7 Therefore don't even attempt to mount /boot ourselves, but error out
8 if it isn't mounted read/write and ask the user to mount /boot.
9
10 Also clean up and simplify. (For example, awk is a grown-up program
11 which doesn't need any help from egrep or sed. :-)
12
13 Closes: https://bugs.gentoo.org/532264
14 See-also: https://bugs.gentoo.org/274130#c5
15 Signed-off-by: Ulrich Müller <ulm@g.o>
16 ---
17 v3: Exit awk commands on first match.
18
19 v4: Added die statements after awk commands
20 Fixed typo in mount-boot_is_disabled function documentation
21 Reverted renaming of I_KNOW_WHAT_I_AM_DOING variable
22
23 eclass/mount-boot.eclass | 144 +++++++++++++--------------------------
24 1 file changed, 48 insertions(+), 96 deletions(-)
25
26 diff --git a/eclass/mount-boot.eclass b/eclass/mount-boot.eclass
27 index 938df6732f43..ca27aca7efbd 100644
28 --- a/eclass/mount-boot.eclass
29 +++ b/eclass/mount-boot.eclass
30 @@ -1,156 +1,108 @@
31 -# Copyright 1999-2015 Gentoo Foundation
32 +# Copyright 1999-2019 Gentoo Authors
33 # Distributed under the terms of the GNU General Public License v2
34
35 # @ECLASS: mount-boot.eclass
36 # @MAINTAINER:
37 # base-system@g.o
38 # @BLURB: functions for packages that install files into /boot
39 # @DESCRIPTION:
40 # This eclass is really only useful for bootloaders.
41 #
42 # If the live system has a separate /boot partition configured, then this
43 # function tries to ensure that it's mounted in rw mode, exiting with an
44 -# error if it can't. It does nothing if /boot isn't a separate partition.
45 +# error if it can't. It does nothing if /boot isn't a separate partition.
46 +
47 +case ${EAPI:-0} in
48 + 4|5|6|7) ;;
49 + *) die "${ECLASS}: EAPI ${EAPI:-0} not supported" ;;
50 +esac
51
52 EXPORT_FUNCTIONS pkg_pretend pkg_preinst pkg_postinst pkg_prerm pkg_postrm
53
54 -# @FUNCTION: mount-boot_disabled
55 +# @FUNCTION: mount-boot_is_disabled
56 # @INTERNAL
57 # @DESCRIPTION:
58 # Detect whether the current environment/build settings are such that we do not
59 # want to mess with any mounts.
60 mount-boot_is_disabled() {
61 - # Since this eclass only deals with /boot, skip things when ROOT is active.
62 - if [[ "${ROOT:-/}" != "/" ]] ; then
63 + # Since this eclass only deals with /boot, skip things when EROOT is active.
64 + if [[ ${EROOT:-/} != / ]] ; then
65 return 0
66 fi
67
68 # If we're only building a package, then there's no need to check things.
69 - if [[ "${MERGE_TYPE}" == "buildonly" ]] ; then
70 + if [[ ${MERGE_TYPE} == buildonly ]] ; then
71 return 0
72 fi
73
74 # The user wants us to leave things be.
75 if [[ -n ${DONT_MOUNT_BOOT} ]] ; then
76 return 0
77 fi
78
79 # OK, we want to handle things ourselves.
80 return 1
81 }
82
83 # @FUNCTION: mount-boot_check_status
84 # @INTERNAL
85 # @DESCRIPTION:
86 -# Figure out what kind of work we need to do in order to have /boot be sane.
87 -# Return values are:
88 -# 0 - Do nothing at all!
89 -# 1 - It's mounted, but is currently ro, so need to remount rw.
90 -# 2 - It's not mounted, so need to mount it rw.
91 +# Check if /boot is sane, i.e., mounted read/write if on a separate
92 +# partition. Die if conditions are not fulfilled.
93 mount-boot_check_status() {
94 # Get out fast if possible.
95 - mount-boot_is_disabled && return 0
96 + mount-boot_is_disabled && return
97
98 # note that /dev/BOOT is in the Gentoo default /etc/fstab file
99 - local fstabstate=$(awk '!/^#|^[[:blank:]]+#|^\/dev\/BOOT/ {print $2}' /etc/fstab | egrep "^/boot$" )
100 - local procstate=$(awk '$2 ~ /^\/boot$/ {print $2}' /proc/mounts)
101 - local proc_ro=$(awk '{ print $2 " ," $4 "," }' /proc/mounts | sed -n '/^\/boot .*,ro,/p')
102 -
103 - if [ -n "${fstabstate}" ] && [ -n "${procstate}" ] ; then
104 - if [ -n "${proc_ro}" ] ; then
105 - echo
106 - einfo "Your boot partition, detected as being mounted at /boot, is read-only."
107 - einfo "It will be remounted in read-write mode temporarily."
108 - return 1
109 - else
110 - echo
111 - einfo "Your boot partition was detected as being mounted at /boot."
112 - einfo "Files will be installed there for ${PN} to function correctly."
113 - return 0
114 - fi
115 - elif [ -n "${fstabstate}" ] && [ -z "${procstate}" ] ; then
116 - echo
117 - einfo "Your boot partition was not mounted at /boot, so it will be automounted for you."
118 - einfo "Files will be installed there for ${PN} to function correctly."
119 - return 2
120 - else
121 - echo
122 + local fstabstate=$(awk '!/^[[:blank:]]*#|^\/dev\/BOOT/ && $2 == "/boot" \
123 + { print 1; exit }' /etc/fstab || die "awk failed")
124 +
125 + if [[ -z ${fstabstate} ]] ; then
126 einfo "Assuming you do not have a separate /boot partition."
127 - return 0
128 + return
129 fi
130 -}
131
132 -mount-boot_pkg_pretend() {
133 - # Get out fast if possible.
134 - mount-boot_is_disabled && return 0
135 + local procstate=$(awk '$2 == "/boot" \
136 + { print gensub(/^(.*,)?(ro|rw)(,.*)?$/, "\\2", 1, $4); exit }' \
137 + /proc/mounts || die "awk failed")
138
139 - elog "To avoid automounting and auto(un)installing with /boot,"
140 - elog "just export the DONT_MOUNT_BOOT variable."
141 - mount-boot_check_status
142 + if [[ -z ${procstate} ]] ; then
143 + eerror "Your boot partition is not mounted at /boot."
144 + eerror "Please mount it and retry."
145 + die "/boot not mounted"
146 + fi
147 +
148 + if [[ ${procstate} == ro ]] ; then
149 + eerror "Your boot partition, detected as being mounted at /boot," \
150 + "is read-only."
151 + eerror "Please remount it read/write and retry."
152 + die "/boot mounted read-only"
153 + fi
154 +
155 + einfo "Your boot partition was detected as being mounted at /boot."
156 + einfo "Files will be installed there for ${PN} to function correctly."
157 }
158
159 -mount-boot_mount_boot_partition() {
160 +mount-boot_pkg_pretend() {
161 mount-boot_check_status
162 - case $? in
163 - 0) # Nothing to do.
164 - ;;
165 - 1) # Remount it rw.
166 - mount -o remount,rw /boot
167 - if [ $? -ne 0 ] ; then
168 - echo
169 - eerror "Unable to remount in rw mode. Please do it manually!"
170 - die "Can't remount in rw mode. Please do it manually!"
171 - fi
172 - touch /boot/.e.remount
173 - ;;
174 - 2) # Mount it rw.
175 - mount /boot -o rw
176 - if [ $? -ne 0 ] ; then
177 - echo
178 - eerror "Cannot automatically mount your /boot partition."
179 - eerror "Your boot partition has to be mounted rw before the installation"
180 - eerror "can continue. ${PN} needs to install important files there."
181 - die "Please mount your /boot partition manually!"
182 - fi
183 - touch /boot/.e.mount
184 - ;;
185 - esac
186 }
187
188 mount-boot_pkg_preinst() {
189 - # Handle older EAPIs.
190 - case ${EAPI:-0} in
191 - [0-3]) mount-boot_pkg_pretend ;;
192 - esac
193 -
194 - mount-boot_mount_boot_partition
195 + mount-boot_check_status
196 }
197
198 mount-boot_pkg_prerm() {
199 - touch "${ROOT}"/boot/.keep 2>/dev/null
200 - mount-boot_mount_boot_partition
201 - touch "${ROOT}"/boot/.keep 2>/dev/null
202 -}
203 + mount-boot_check_status
204
205 -mount-boot_umount_boot_partition() {
206 - # Get out fast if possible.
207 - mount-boot_is_disabled && return 0
208 -
209 - if [ -e /boot/.e.remount ] ; then
210 - einfo "Automatically remounting /boot as ro as it was previously."
211 - rm -f /boot/.e.remount
212 - mount -o remount,ro /boot
213 - elif [ -e /boot/.e.mount ] ; then
214 - einfo "Automatically unmounting /boot as it was previously."
215 - rm -f /boot/.e.mount
216 - umount /boot
217 + if [[ -z ${EPREFIX} ]] \
218 + && ! ( shopt -s failglob; : "${EROOT}"/boot/.keep* ) 2>/dev/null
219 + then
220 + # Create a .keep file, in case it is shadowed at the mount point
221 + touch "${EROOT}"/boot/.keep 2>/dev/null
222 fi
223 }
224
225 -mount-boot_pkg_postinst() {
226 - mount-boot_umount_boot_partition
227 -}
228 +# No-op phases for backwards compatibility
229 +mount-boot_pkg_postinst() { :; }
230
231 -mount-boot_pkg_postrm() {
232 - mount-boot_umount_boot_partition
233 -}
234 +mount-boot_pkg_postrm() { :; }
235 --
236 2.24.0

Attachments

File name MIME type
signature.asc application/pgp-signature

Replies