qubes-download-dom0-updates.sh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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"
  50. fi
  51. if ! [ -d "$DOM0_UPDATES_DIR" ]; then
  52. echo "Dom0 updates dir does not exists: $DOM0_UPDATES_DIR" >&2
  53. exit 1
  54. fi
  55. mkdir -p $DOM0_UPDATES_DIR/etc
  56. sed -i '/^reposdir\s*=/d' $DOM0_UPDATES_DIR/etc/yum.conf
  57. if [ -e /etc/debian_version ]; then
  58. # Default rpm configuration on Debian uses ~/.rpmdb for rpm database (as
  59. # rpm isn't native package manager there)
  60. mkdir -p "$DOM0_UPDATES_DIR$HOME"
  61. ln -nsf "$DOM0_UPDATES_DIR/var/lib/rpm" "$DOM0_UPDATES_DIR$HOME/.rpmdb"
  62. fi
  63. # Rebuild rpm database in case of different rpm version
  64. rm -f $DOM0_UPDATES_DIR/var/lib/rpm/__*
  65. rpm --root=$DOM0_UPDATES_DIR --rebuilddb
  66. if [ "$CLEAN" = "1" ]; then
  67. $YUM $OPTS clean all
  68. rm -f $DOM0_UPDATES_DIR/packages/*
  69. rm -rf $DOM0_UPDATES_DIR/var/cache/yum/*
  70. fi
  71. # just check for updates, but don't download any package
  72. if [ "x$PKGLIST" = "x" -a "$CHECK_ONLY" = "1" ]; then
  73. echo "Checking for dom0 updates..." >&2
  74. UPDATES_FULL=`$YUM $OPTS check-update`
  75. check_update_retcode=$?
  76. if [ $check_update_retcode -eq 1 ]; then
  77. # Exit here if yum have reported an error. Exit code 100 isn't an
  78. # error, it's "updates available" info, so check specifically for exit code 1
  79. exit 1
  80. fi
  81. if [ $check_update_retcode -eq 100 ]; then
  82. echo "Available updates: "
  83. echo "$UPDATES_FULL"
  84. exit 100
  85. else
  86. echo "No new updates available"
  87. if [ "$GUI" = 1 ]; then
  88. zenity --info --text="No new updates available"
  89. fi
  90. exit 0
  91. fi
  92. fi
  93. # now, we will download something
  94. YUM_COMMAND="fakeroot $YUM $YUM_ACTION -y --downloadonly"
  95. # check for --downloadonly option - if not supported (Debian), fallback to
  96. # yumdownloader
  97. if ! $YUM --help | grep -q downloadonly; then
  98. if [ "$YUM_ACTION" != "install" -a "$YUM_ACTION" != "upgrade" ]; then
  99. echo "ERROR: yum version installed in VM `hostname` does not suppport --downloadonly option" >&2
  100. echo "ERROR: only 'install' and 'upgrade' actions supported ($YUM_ACTION not)" >&2
  101. if [ "$GUI" = 1 ]; then
  102. zenity --error --text="yum version too old for '$YUM_ACTION' action, see console for details"
  103. fi
  104. exit 1
  105. fi
  106. if [ "$YUM_ACTION" = "upgrade" ]; then
  107. UPDATES_FULL=`$YUM $OPTS check-update $PKGLIST`
  108. check_update_retcode=$?
  109. UPDATES=`echo "$UPDATES_FULL" | grep -v "^Obsoleting\|Could not" | cut -f 1 -d ' '`
  110. if [ -z "$UPDATES" -a $check_update_retcode -eq 0 ]; then
  111. echo "No new updates available"
  112. exit 0
  113. fi
  114. PKGLIST=$UPDATES
  115. fi
  116. YUM_COMMAND="yumdownloader --destdir=$DOM0_UPDATES_DIR/packages --resolve"
  117. fi
  118. mkdir -p "$DOM0_UPDATES_DIR/packages"
  119. set -e
  120. if [ "$GUI" = 1 ]; then
  121. ( echo "1"
  122. $YUM_COMMAND $OPTS $PKGLIST
  123. echo 100 ) | zenity --progress --pulsate --auto-close --auto-kill \
  124. --text="Downloading updates for Dom0, please wait..." --title="Qubes Dom0 updates"
  125. else
  126. $YUM_COMMAND $OPTS $PKGLIST
  127. fi
  128. find $DOM0_UPDATES_DIR/var/cache/yum -name '*.rpm' -print0 |\
  129. xargs -0 -r ln -f -t $DOM0_UPDATES_DIR/packages/
  130. if ls $DOM0_UPDATES_DIR/packages/*.rpm > /dev/null 2>&1; then
  131. cmd="/usr/lib/qubes/qrexec-client-vm dom0 qubes.ReceiveUpdates /usr/lib/qubes/qfile-agent"
  132. qrexec_exit_code=0
  133. $cmd $DOM0_UPDATES_DIR/packages/*.rpm || { qrexec_exit_code=$? ; true; };
  134. if [ ! "$qrexec_exit_code" = "0" ]; then
  135. echo "'$cmd $DOM0_UPDATES_DIR/packages/*.rpm' failed with exit code ${qrexec_exit_code}!" >&2
  136. exit $qrexec_exit_code
  137. fi
  138. else
  139. echo "No packages downloaded"
  140. fi