fix-gpg-pacman.sh 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. #!/usr/bin/env bash
  2. set -o errexit #>Exit when a command fails (returns non-zero)
  3. set -o pipefail #>Exit when a command within a pipeline fail (returns non-zero)
  4. set -o nounset #>Forbid to use unset
  5. # Reset language to defaultx
  6. export LANG=C
  7. # Switch to root account
  8. RunasRoot () { sudo --login --user=root "$@"; }
  9. # Colors for messages
  10. PrintInfo () { printf '\e[32m[INFO]\e[0m \e[33m%s\e[0m\n' "$1"; }
  11. PrintError () { printf '\e[31m[ERROR]\e[0m \e[33m%s\e[0m\n' "$1"; }
  12. PrintQuestion () { printf '\e[35m[QUESTION]\e[0m \e[33m%s\e[0m\n' "$1"; }
  13. RmLockFiles ()
  14. {
  15. PrintInfo "Remove lock files of pamac and pacman"
  16. RunasRoot find /var/tmp/pamac/ -type f -iname "*db.lck" -exec rm --force --verbose "{}" \;
  17. RunasRoot find /var/lib/pacman/ -type f -iname "*db.lck" -exec rm --force --verbose "{}" \;
  18. }
  19. AskForUpgrade ()
  20. {
  21. PrintInfo "Performing a full upgrade with pacman"
  22. while true; do
  23. PrintQuestion "Do you want to continue? [Yy/Nn] (Be aware that a full upgrade needs enough ram on a live session)"
  24. read -p "> [Yy/Nn] " yn
  25. case $yn in
  26. [Yy]*)
  27. RunasRoot pacman --sync --refresh --refresh --sysupgrade --sysupgrade --noconfirm
  28. PrintInfo "Done. Note that you need to refresh the database for pamac also."
  29. break
  30. ;;
  31. [Nn]*)
  32. break
  33. ;;
  34. *)
  35. PrintError "No valid answer. Continue." && continue ;;
  36. esac
  37. done
  38. }
  39. AskForRefreshKeys ()
  40. {
  41. PrintInfo "Refresh GnuPG Database of pacman from the Internet"
  42. while true; do
  43. PrintQuestion "Do you want to continue? [Yy/Nn] (Note that this can take a while.)"
  44. read -p "> [Yy/Nn] " yn
  45. case $yn in
  46. [Yy]*)
  47. RunasRoot pacman-key --refresh-keys && break
  48. ;;
  49. [Nn]*)
  50. break
  51. ;;
  52. *)
  53. PrintError "No valid answer. Continue." && continue ;;
  54. esac
  55. done
  56. }
  57. AskForRemoveCache ()
  58. {
  59. PrintInfo "Remove cached software packages [optional]"
  60. while true; do
  61. PrintQuestion "Delete them? [Yy/Nn]"
  62. read -p "[Yy/Nn] > " yn
  63. case $yn in
  64. [Yy]*)
  65. PrintInfo "Removing package cache"
  66. RunasRoot pacman --sync --clean --clean --noconfirm
  67. if [[ $(ls /var/cache/pacman/pkg/ | wc -l) != 0 ]]; then
  68. RunasRoot find /var/cache/pacman/pkg/ -type f -exec rm --force --verbose "{}" \;
  69. fi
  70. ;;
  71. [Nn]*)
  72. break
  73. ;;
  74. *)
  75. PrintError "No valid answer." && continue ;;
  76. esac
  77. done
  78. }
  79. BasicMethod ()
  80. {
  81. RmLockFiles
  82. PrintInfo "Refresh Package Database"
  83. RunasRoot pacman --sync --refresh --refresh
  84. PrintInfo "Populate local keyrings to the GnuPG Database of pacman"
  85. RunasRoot pacman-key --populate archlinux manjaro
  86. AskForRefreshKeys
  87. AskForUpgrade
  88. }
  89. ModerateMethod ()
  90. {
  91. RmLockFiles
  92. PrintInfo "Remove Pacman's GnuPG Database"
  93. RunasRoot find /etc/pacman.d/gnupg/ -exec rm --recursive --force --verbose "{}" \;
  94. PrintInfo "Initilize Pacman's GnuPG Database"
  95. RunasRoot pacman-key --init
  96. PrintInfo "Populate local keyrings to Pacman's GnuPG Database"
  97. RunasRoot pacman-key --populate archlinux manjaro
  98. AskForRefreshKeys
  99. AskForRemoveCache
  100. AskForUpgrade
  101. }
  102. AggressiveMethod ()
  103. {
  104. RmLockFiles
  105. PrintInfo "Switch to global mirror (Manjaro's CDN)"
  106. RunasRoot pacman-mirrors --country Global
  107. RunasRoot pacman-mirrors --fasttrack 5
  108. PrintInfo "Remove Pacman's GnuPG Database"
  109. RunasRoot find /etc/pacman.d/gnupg/ -exec rm --recursive --force --verbose "{}" \;
  110. PrintInfo "Initilize Pacman's GnuPG Database"
  111. RunasRoot pacman-key --init
  112. PrintInfo "Removing package cache"
  113. RunasRoot pacman --sync --clean --clean --noconfirm
  114. if [[ $(ls /var/cache/pacman/pkg/ | wc -l) != 0 ]]; then
  115. RunasRoot find /var/cache/pacman/pkg/ -type f -exec rm --force --verbose "{}" \;
  116. fi
  117. PrintInfo "Create a temporary folder in /tmp"
  118. TMPDIR="$(mktemp -d)"
  119. PrintInfo "${TMPDIR}"
  120. PrintInfo "Copy /etc/pacman.conf to ${TMPDIR}/pacman.conf and disable temporarily gpg verification."
  121. RunasRoot cp "/etc/pacman.conf" "${TMPDIR}/pacman.conf"
  122. RunasRoot sed --in-place --regexp-extended 's/^(SigLevel).+$/\1 = Never/g' "${TMPDIR}/pacman.conf"
  123. PrintInfo "Download the newest packages which contains the gpg keyrings in ${TMPDIR}"
  124. RunasRoot pacman --sync --refresh --downloadonly --noconfirm --cachedir "${TMPDIR}" --config "${TMPDIR}/pacman.conf" archlinux-keyring manjaro-keyring gnupg --overwrite "*"
  125. PrintInfo "Install temporarily downloaded keyring packages"
  126. RunasRoot pacman --upgrade --noconfirm --config "${TMPDIR}/pacman.conf" $(find ${TMPDIR} -type f -name "*.tar.*")
  127. PrintInfo "Remove temporary directory: ${TMPDIR}"
  128. RunasRoot find "${TMPDIR}" -type f -exec rm --recursive --force --verbose "{}" \;
  129. RunasRoot rmdir --verbose "${TMPDIR}"
  130. PrintInfo "Switch to a local mirror by Geolocation"
  131. RunasRoot pacman-mirrors --geoip
  132. RunasRoot pacman-mirrors --fasttrack 5
  133. AskForRefreshKeys
  134. AskForUpgrade
  135. }
  136. HelpPage ()
  137. {
  138. printf "\n\t%s\n" "Method 1 - Basic"
  139. printf "\t%s\n" "1. Remove lock files of pamac and pacman"
  140. printf "\t%s\n" "2. Refresh Package Database"
  141. printf "\t%s\n" "3. Populate local keyrings to the GnuPG Database of pacman"
  142. printf "\t%s\n" "4. Refresh GnuPG Database of pacman from the Internet [optional]"
  143. printf "\t%s\n" "5. Performing a full upgrade with pacman [optional]"
  144. printf "\n\t%s\n" "Method 2 - Moderate"
  145. printf "\t%s\n" "1. Remove lock files of pamac and pacman"
  146. printf "\t%s\n" "2. Remove Pacman's GnuPG Database"
  147. printf "\t%s\n" "3. Initilize Pacman's GnuPG Database"
  148. printf "\t%s\n" "4. Populate local keyrings to Pacman's GnuPG Database"
  149. printf "\t%s\n" "5. Refresh GnuPG Database of pacman from the Internet [optional]"
  150. printf "\t%s\n" "6. Remove cached software packages [optional]"
  151. printf "\t%s\n" "7. Performing a full upgrade with pacman [optional]"
  152. printf "\n\t%s\n" "Method 3 - Aggressive"
  153. printf "\t%s\n" "1. Remove lock files of pamac and pacman"
  154. printf "\t%s\n" "2. Switch to global mirror (Manjaro's CDN)"
  155. printf "\t%s\n" "3. Remove Pacman's GnuPG Database"
  156. printf "\t%s\n" "4. Initilize Pacman's GnuPG Database"
  157. printf "\t%s\n" "5. Remove cached software packages"
  158. printf "\t%s\n" "6. Create a temporary folder in /tmp"
  159. printf "\t%s\n" "7. Copy /etc/pacman.conf to TMPDIR/pacman.conf and disable temporarily gpg verification."
  160. printf "\t%s\n" "8. Download the newest packages which contains the gpg keyrings in TMPDIR"
  161. printf "\t%s\n" "9. Install temporarily downloaded keyring packages"
  162. printf "\t%s\n" "10. Remove temporary directory TMPDIR"
  163. printf "\t%s\n" "11. Switch to a local mirror by Geolocation"
  164. printf "\t%s\n" "12. Refresh GnuPG Database of pacman from the Internet [optional]"
  165. printf "\t%s\n" "13. Performing a full upgrade with pacman [optional]"
  166. printf "\n\t%s\n\n" "Note: That tool is not designed to fix gpg issues with your local user GnuPG Database, which is commonly used for AUR Packages as an example."
  167. }
  168. HelpUsage () { printf '\e[32m%s\e[0m\n' "$0 [--usage|--help|--basic|--moderate|--aggressive|--ask] (default: [--basic])"; }
  169. Menu ()
  170. {
  171. clear
  172. while true; do
  173. printf '\e[33m%s\e[0m\n' "1. Basic"
  174. printf '\e[33m%s\e[0m\n' "2. Moderate"
  175. printf '\e[33m%s\e[0m\n' "3. Aggressive"
  176. printf '\e[33m%s\e[0m\n' "4. Explanations of the methods"
  177. printf '\n\e[33m%s\e[0m\n\n' "Type [q] to quit."
  178. PrintQuestion "Choose a Method by typing the number and press ENTER on you keyboard."
  179. read -p "[1,2,3,4,q]> " choice
  180. case $choice in
  181. 1)
  182. BasicMethod && exit
  183. ;;
  184. 2)
  185. ModerateMethod && exit
  186. ;;
  187. 3)
  188. AggressiveMethod && exit
  189. ;;
  190. 4)
  191. HelpPage && continue
  192. ;;
  193. q)
  194. exit
  195. ;;
  196. *)
  197. clear && PrintError "No valid answer. Continue." && continue ;;
  198. esac
  199. done
  200. }
  201. [[ -z $@ ]] && BasicMethod && exit
  202. case $1 in
  203. --basic)
  204. BasicMethod && exit
  205. ;;
  206. --moderate)
  207. ModerateMethod && exit
  208. ;;
  209. --aggressive)
  210. AggressiveMethod && exit
  211. ;;
  212. --help)
  213. HelpUsage && HelpPage && exit
  214. ;;
  215. --usage)
  216. HelpUsage && exit
  217. ;;
  218. --ask)
  219. Menu && exit
  220. ;;
  221. *)
  222. PrintError "\"$1\" is not a valid parameter. See \"$0 --usage\""
  223. ;;
  224. esac