mount-boot.eclass 4.4 KB

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