Gentoo Archives: gentoo-dev

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

Attachments

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

Replies