qubes-download-dom0-updates.sh 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #!/bin/bash
  2. DOM0_UPDATES_DIR=/var/lib/qubes/dom0-updates
  3. GUI=1
  4. CLEAN=0
  5. CHECK_ONLY=0
  6. OPTS="--installroot $DOM0_UPDATES_DIR"
  7. if [ -f "$DOM0_UPDATES_DIR/etc/dnf/dnf.conf" ]; then
  8. OPTS="$OPTS --config=$DOM0_UPDATES_DIR/etc/yum.conf"
  9. elif [ -f "$DOM0_UPDATES_DIR/etc/yum.conf" ]; then
  10. OPTS="$OPTS --config=$DOM0_UPDATES_DIR/etc/yum.conf"
  11. fi
  12. # DNF uses /etc/yum.repos.d, even when --installroot is specified
  13. OPTS="$OPTS --setopt=reposdir=$DOM0_UPDATES_DIR/etc/yum.repos.d"
  14. PKGLIST=()
  15. YUM_ACTION=
  16. export LC_ALL=C
  17. while [ -n "$1" ]; do
  18. case "$1" in
  19. --doit)
  20. # ignore
  21. ;;
  22. --nogui)
  23. GUI=0
  24. ;;
  25. --gui)
  26. GUI=1
  27. ;;
  28. --clean)
  29. CLEAN=1
  30. ;;
  31. --check-only)
  32. CHECK_ONLY=1
  33. ;;
  34. --action=*)
  35. YUM_ACTION=${1#--action=}
  36. ;;
  37. -*)
  38. OPTS="$OPTS $1"
  39. ;;
  40. *)
  41. PKGLIST+=( "${1}" )
  42. if [ -z "$YUM_ACTION" ]; then
  43. YUM_ACTION=install
  44. fi
  45. ;;
  46. esac
  47. shift
  48. done
  49. if [ -z "$YUM_ACTION" ]; then
  50. YUM_ACTION=upgrade
  51. fi
  52. YUM="yum"
  53. if type dnf >/dev/null 2>&1; then
  54. YUM="dnf --best --allowerasing --noplugins"
  55. else
  56. # salt in dom0 thinks it's using dnf but we only have yum so need to remove extra options
  57. OPTS="${OPTS/--best --allowerasing/}"
  58. fi
  59. if ! [ -d "$DOM0_UPDATES_DIR" ]; then
  60. echo "Dom0 updates dir does not exists: $DOM0_UPDATES_DIR" >&2
  61. exit 1
  62. fi
  63. mkdir -p $DOM0_UPDATES_DIR/etc
  64. if [ -e /etc/debian_version ]; then
  65. # Default rpm configuration on Debian uses ~/.rpmdb for rpm database (as
  66. # rpm isn't native package manager there)
  67. mkdir -p "$DOM0_UPDATES_DIR$HOME"
  68. ln -nsf "$DOM0_UPDATES_DIR/var/lib/rpm" "$DOM0_UPDATES_DIR$HOME/.rpmdb"
  69. fi
  70. # Rebuild rpm database in case of different rpm version
  71. rm -f $DOM0_UPDATES_DIR/var/lib/rpm/__*
  72. rpm --root=$DOM0_UPDATES_DIR --rebuilddb
  73. if [ "$CLEAN" = "1" ]; then
  74. # shellcheck disable=SC2086
  75. $YUM $OPTS clean all
  76. rm -f "$DOM0_UPDATES_DIR"/packages/*
  77. rm -rf "$DOM0_UPDATES_DIR"/var/cache/*
  78. fi
  79. # just check for updates, but don't download any package
  80. if [ ${#PKGLIST[@]} -eq 0 ] && [ "$CHECK_ONLY" = "1" ]; then
  81. echo "Checking for dom0 updates..." >&2
  82. # shellcheck disable=SC2086
  83. UPDATES_FULL=$($YUM $OPTS check-update)
  84. check_update_retcode=$?
  85. if [ "$check_update_retcode" -eq 1 ]; then
  86. # Exit here if yum have reported an error. Exit code 100 isn't an
  87. # error, it's "updates available" info, so check specifically for exit code 1
  88. exit 1
  89. fi
  90. if [ $check_update_retcode -eq 100 ]; then
  91. echo "Available updates: "
  92. echo "$UPDATES_FULL"
  93. exit 100
  94. else
  95. echo "No new updates available"
  96. if [ "$GUI" = 1 ]; then
  97. zenity --info --text="No new updates available"
  98. fi
  99. exit 0
  100. fi
  101. fi
  102. # now, we will download something
  103. YUM_COMMAND="fakeroot $YUM $YUM_ACTION -y --downloadonly"
  104. # check for --downloadonly option - if not supported (Debian), fallback to
  105. # yumdownloader
  106. if ! $YUM --help | grep -q downloadonly; then
  107. if [ "$YUM_ACTION" = "install" ]; then
  108. YUM_COMMAND="yumdownloader --destdir=$DOM0_UPDATES_DIR/packages --resolve"
  109. elif [ "$YUM_ACTION" = "upgrade" ]; then
  110. # shellcheck disable=SC2086
  111. UPDATES_FULL=$($YUM $OPTS check-update "${PKGLIST[@]}")
  112. check_update_retcode=$?
  113. UPDATES_FULL=$(echo "$UPDATES_FULL" | grep -v "^Loaded plugins:\|^Last metadata\|^$")
  114. mapfile -t PKGLIST < <(echo "$UPDATES_FULL" | grep -v "^Obsoleting\|Could not" | cut -f 1 -d ' ')
  115. if [ "$check_update_retcode" -eq 0 ]; then
  116. # exit code 0 means no updates available - regardless of stdout messages
  117. echo "No new updates available"
  118. exit 0
  119. fi
  120. YUM_COMMAND="yumdownloader --destdir=$DOM0_UPDATES_DIR/packages --resolve"
  121. elif [ "$YUM_ACTION" == "list" ] || [ "$YUM_ACTION" == "search" ]; then
  122. # those actions do not download any package, so lack of --downloadonly is irrelevant
  123. YUM_COMMAND="$YUM $YUM_ACTION -y"
  124. elif [ "$YUM_ACTION" == "reinstall" ]; then
  125. # this is just approximation of 'reinstall' action...
  126. mapfile -t PKGLIST < <(rpm --root=$DOM0_UPDATES_DIR -q "${PKGLIST[@]}")
  127. YUM_COMMAND="yumdownloader --destdir=$DOM0_UPDATES_DIR/packages --resolve"
  128. else
  129. echo "ERROR: yum version installed in VM $(hostname) does not suppport --downloadonly option" >&2
  130. echo "ERROR: only 'install' and 'upgrade' actions supported ($YUM_ACTION not)" >&2
  131. if [ "$GUI" = 1 ]; then
  132. zenity --error --text="yum version too old for '$YUM_ACTION' action, see console for details"
  133. fi
  134. exit 1
  135. fi
  136. fi
  137. mkdir -p "$DOM0_UPDATES_DIR/packages"
  138. set -e
  139. if [ "$GUI" = 1 ]; then
  140. ( echo "1"
  141. # shellcheck disable=SC2086
  142. $YUM_COMMAND $OPTS "${PKGLIST[@]}"
  143. echo 100 ) | zenity --progress --pulsate --auto-close --auto-kill \
  144. --text="Downloading updates for Dom0, please wait..." --title="Qubes Dom0 updates"
  145. else
  146. # shellcheck disable=SC2086
  147. $YUM_COMMAND $OPTS "${PKGLIST[@]}"
  148. fi
  149. find "$DOM0_UPDATES_DIR/var/cache" -name '*.rpm' -print0 |\
  150. xargs -0 -r ln -f -t "$DOM0_UPDATES_DIR/packages/"
  151. if ls "$DOM0_UPDATES_DIR"/packages/*.rpm > /dev/null 2>&1; then
  152. cmd="/usr/lib/qubes/qrexec-client-vm dom0 qubes.ReceiveUpdates /usr/lib/qubes/qfile-agent"
  153. qrexec_exit_code=0
  154. $cmd "$DOM0_UPDATES_DIR"/packages/*.rpm || { qrexec_exit_code=$? ; true; };
  155. if [ ! "$qrexec_exit_code" = "0" ]; then
  156. echo "'$cmd $DOM0_UPDATES_DIR/packages/*.rpm' failed with exit code ${qrexec_exit_code}!" >&2
  157. exit "$qrexec_exit_code"
  158. fi
  159. else
  160. echo "No packages downloaded"
  161. fi