qubes-download-dom0-updates.sh 5.6 KB

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