dev-python/josepy: 1.1.0 cleanup
[gentoo.git] / eclass / mount-boot.eclass
1 # Copyright 1999-2019 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: mount-boot.eclass
5 # @MAINTAINER:
6 # base-system@gentoo.org
7 # @BLURB: functions for packages that install files into /boot
8 # @DESCRIPTION:
9 # This eclass is really only useful for bootloaders.
10 #
11 # If the live system has a separate /boot partition configured, then this
12 # function tries to ensure that it's mounted in rw mode, exiting with an
13 # error if it can't.  It does nothing if /boot isn't a separate partition.
14
15 case ${EAPI:-0} in
16         4|5|6|7) ;;
17         *) die "${ECLASS}: EAPI ${EAPI:-0} not supported" ;;
18 esac
19
20 EXPORT_FUNCTIONS pkg_pretend pkg_preinst pkg_postinst pkg_prerm pkg_postrm
21
22 # @FUNCTION: mount-boot_is_disabled
23 # @INTERNAL
24 # @DESCRIPTION:
25 # Detect whether the current environment/build settings are such that we do not
26 # want to mess with any mounts.
27 mount-boot_is_disabled() {
28         # Since this eclass only deals with /boot, skip things when EROOT is active.
29         if [[ ${EROOT:-/} != / ]] ; then
30                 return 0
31         fi
32
33         # If we're only building a package, then there's no need to check things.
34         if [[ ${MERGE_TYPE} == buildonly ]] ; then
35                 return 0
36         fi
37
38         # The user wants us to leave things be.
39         if [[ -n ${DONT_MOUNT_BOOT} ]] ; then
40                 return 0
41         fi
42
43         # OK, we want to handle things ourselves.
44         return 1
45 }
46
47 # @FUNCTION: mount-boot_check_status
48 # @INTERNAL
49 # @DESCRIPTION:
50 # Check if /boot is sane, i.e., mounted as read-write if on a separate
51 # partition.  Die if conditions are not fulfilled.
52 mount-boot_check_status() {
53         # Get out fast if possible.
54         mount-boot_is_disabled && return
55
56         # note that /dev/BOOT is in the Gentoo default /etc/fstab file
57         local fstabstate=$(awk '!/^[[:blank:]]*#|^\/dev\/BOOT/ && $2 == "/boot" \
58                 { print 1; exit }' /etc/fstab || die "awk failed")
59
60         if [[ -z ${fstabstate} ]] ; then
61                 einfo "Assuming you do not have a separate /boot partition."
62                 return
63         fi
64
65         local procstate=$(awk '$2 == "/boot" \
66                 { print gensub(/^(.*,)?(ro|rw)(,.*)?$/, "\\2", 1, $4); exit }' \
67                 /proc/mounts || die "awk failed")
68
69         if [[ -z ${procstate} ]] ; then
70                 eerror "Your boot partition is not mounted at /boot."
71                 eerror "Please mount it and retry."
72                 die "/boot not mounted"
73         fi
74
75         if [[ ${procstate} == ro ]] ; then
76                 eerror "Your boot partition, detected as being mounted at /boot," \
77                         "is read-only."
78                 eerror "Please remount it as read-write and retry."
79                 die "/boot mounted read-only"
80         fi
81
82         einfo "Your boot partition was detected as being mounted at /boot."
83         einfo "Files will be installed there for ${PN} to function correctly."
84 }
85
86 mount-boot_pkg_pretend() {
87         mount-boot_check_status
88 }
89
90 mount-boot_pkg_preinst() {
91         mount-boot_check_status
92 }
93
94 mount-boot_pkg_prerm() {
95         mount-boot_check_status
96
97         if [[ -z ${EPREFIX} ]] \
98                 && ! ( shopt -s failglob; : "${EROOT}"/boot/.keep* ) 2>/dev/null
99         then
100                 # Create a .keep file, in case it is shadowed at the mount point
101                 touch "${EROOT}"/boot/.keep 2>/dev/null
102         fi
103 }
104
105 # No-op phases for backwards compatibility
106 mount-boot_pkg_postinst() { :; }
107
108 mount-boot_pkg_postrm() { :; }