Gentoo Archives: gentoo-dev

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

Attachments

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