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