perl-functions.eclass: should 'just work' in EAPI=6
[gentoo.git] / eclass / mount-boot.eclass
1 # Copyright 1999-2015 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 # $Id$
4
5 # @ECLASS: mount-boot.eclass
6 # @MAINTAINER:
7 # base-system@gentoo.org
8 # @BLURB: functions for packages that install files into /boot
9 # @DESCRIPTION:
10 # This eclass is really only useful for bootloaders.
11 #
12 # If the live system has a separate /boot partition configured, then this
13 # function tries to ensure that it's mounted in rw mode, exiting with an
14 # error if it can't. It does nothing if /boot isn't a separate partition.
15
16 EXPORT_FUNCTIONS pkg_pretend pkg_preinst pkg_postinst pkg_prerm pkg_postrm
17
18 # @FUNCTION: mount-boot_disabled
19 # @INTERNAL
20 # @DESCRIPTION:
21 # Detect whether the current environment/build settings are such that we do not
22 # want to mess with any mounts.
23 mount-boot_is_disabled() {
24         # Since this eclass only deals with /boot, skip things when ROOT is active.
25         if [[ "${ROOT:-/}" != "/" ]] ; then
26                 return 0
27         fi
28
29         # If we're only building a package, then there's no need to check things.
30         if [[ "${MERGE_TYPE}" == "buildonly" ]] ; then
31                 return 0
32         fi
33
34         # The user wants us to leave things be.
35         if [[ -n ${DONT_MOUNT_BOOT} ]] ; then
36                 return 0
37         fi
38
39         # OK, we want to handle things ourselves.
40         return 1
41 }
42
43 # @FUNCTION: mount-boot_check_status
44 # @INTERNAL
45 # @DESCRIPTION:
46 # Figure out what kind of work we need to do in order to have /boot be sane.
47 # Return values are:
48 # 0 - Do nothing at all!
49 # 1 - It's mounted, but is currently ro, so need to remount rw.
50 # 2 - It's not mounted, so need to mount it rw.
51 mount-boot_check_status() {
52         # Get out fast if possible.
53         mount-boot_is_disabled && return 0
54
55         # note that /dev/BOOT is in the Gentoo default /etc/fstab file
56         local fstabstate=$(awk '!/^#|^[[:blank:]]+#|^\/dev\/BOOT/ {print $2}' /etc/fstab | egrep "^/boot$" )
57         local procstate=$(awk '$2 ~ /^\/boot$/ {print $2}' /proc/mounts)
58         local proc_ro=$(awk '{ print $2 " ," $4 "," }' /proc/mounts | sed -n '/\/boot .*,ro,/p')
59
60         if [ -n "${fstabstate}" ] && [ -n "${procstate}" ] ; then
61                 if [ -n "${proc_ro}" ] ; then
62                         echo
63                         einfo "Your boot partition, detected as being mounted at /boot, is read-only."
64                         einfo "It will be remounted in read-write mode temporarily."
65                         return 1
66                 else
67                         echo
68                         einfo "Your boot partition was detected as being mounted at /boot."
69                         einfo "Files will be installed there for ${PN} to function correctly."
70                         return 0
71                 fi
72         elif [ -n "${fstabstate}" ] && [ -z "${procstate}" ] ; then
73                 echo
74                 einfo "Your boot partition was not mounted at /boot, so it will be automounted for you."
75                 einfo "Files will be installed there for ${PN} to function correctly."
76                 return 2
77         else
78                 echo
79                 einfo "Assuming you do not have a separate /boot partition."
80                 return 0
81         fi
82 }
83
84 mount-boot_pkg_pretend() {
85         # Get out fast if possible.
86         mount-boot_is_disabled && return 0
87
88         elog "To avoid automounting and auto(un)installing with /boot,"
89         elog "just export the DONT_MOUNT_BOOT variable."
90         mount-boot_check_status
91 }
92
93 mount-boot_mount_boot_partition() {
94         mount-boot_check_status
95         case $? in
96         0)      # Nothing to do.
97                 ;;
98         1)      # Remount it rw.
99                 mount -o remount,rw /boot
100                 if [ $? -ne 0 ] ; then
101                         echo
102                         eerror "Unable to remount in rw mode. Please do it manually!"
103                         die "Can't remount in rw mode. Please do it manually!"
104                 fi
105                 touch /boot/.e.remount
106                 ;;
107         2)      # Mount it rw.
108                 mount /boot -o rw
109                 if [ $? -ne 0 ] ; then
110                         echo
111                         eerror "Cannot automatically mount your /boot partition."
112                         eerror "Your boot partition has to be mounted rw before the installation"
113                         eerror "can continue. ${PN} needs to install important files there."
114                         die "Please mount your /boot partition manually!"
115                 fi
116                 touch /boot/.e.mount
117                 ;;
118         esac
119 }
120
121 mount-boot_pkg_preinst() {
122         # Handle older EAPIs.
123         case ${EAPI:-0} in
124         [0-3]) mount-boot_pkg_pretend ;;
125         esac
126
127         mount-boot_mount_boot_partition
128 }
129
130 mount-boot_pkg_prerm() {
131         touch "${ROOT}"/boot/.keep 2>/dev/null
132         mount-boot_mount_boot_partition
133         touch "${ROOT}"/boot/.keep 2>/dev/null
134 }
135
136 mount-boot_umount_boot_partition() {
137         # Get out fast if possible.
138         mount-boot_is_disabled && return 0
139
140         if [ -e /boot/.e.remount ] ; then
141                 einfo "Automatically remounting /boot as ro as it was previously."
142                 rm -f /boot/.e.remount
143                 mount -o remount,ro /boot
144         elif [ -e /boot/.e.mount ] ; then
145                 einfo "Automatically unmounting /boot as it was previously."
146                 rm -f /boot/.e.mount
147                 umount /boot
148         fi
149 }
150
151 mount-boot_pkg_postinst() {
152         mount-boot_umount_boot_partition
153 }
154
155 mount-boot_pkg_postrm() {
156         mount-boot_umount_boot_partition
157 }