qubes-download-dom0-updates.sh 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #!/bin/bash
  2. DOM0_UPDATES_DIR=/var/lib/qubes/dom0-updates
  3. DOIT=0
  4. GUI=1
  5. CLEAN=0
  6. CHECK_ONLY=0
  7. OPTS="--installroot $DOM0_UPDATES_DIR --config=$DOM0_UPDATES_DIR/etc/yum.conf"
  8. PKGLIST=
  9. YUM_ACTION=
  10. export LC_ALL=C
  11. while [ -n "$1" ]; do
  12. case "$1" in
  13. --doit)
  14. DOIT=1
  15. ;;
  16. --nogui)
  17. GUI=0
  18. ;;
  19. --gui)
  20. GUI=1
  21. ;;
  22. --clean)
  23. CLEAN=1
  24. ;;
  25. --check-only)
  26. CHECK_ONLY=1
  27. ;;
  28. --action=*)
  29. YUM_ACTION=${1#--action=}
  30. ;;
  31. -*)
  32. OPTS="$OPTS $1"
  33. ;;
  34. *)
  35. PKGLIST="$PKGLIST $1"
  36. if [ -z "$YUM_ACTION" ]; then
  37. YUM_ACTION=install
  38. fi
  39. ;;
  40. esac
  41. shift
  42. done
  43. if [ -z "$YUM_ACTION" ]; then
  44. YUM_ACTION=upgrade
  45. fi
  46. if ! [ -d "$DOM0_UPDATES_DIR" ]; then
  47. echo "Dom0 updates dir does not exists: $DOM0_UPDATES_DIR" >&2
  48. exit 1
  49. fi
  50. mkdir -p $DOM0_UPDATES_DIR/etc
  51. sed -i '/^reposdir\s*=/d' $DOM0_UPDATES_DIR/etc/yum.conf
  52. if [ -e /etc/debian_version ]; then
  53. # Default rpm configuration on Debian uses ~/.rpmdb for rpm database (as
  54. # rpm isn't native package manager there)
  55. mkdir -p "$DOM0_UPDATES_DIR$HOME"
  56. ln -nsf "$DOM0_UPDATES_DIR/var/lib/rpm" "$DOM0_UPDATES_DIR$HOME/.rpmdb"
  57. fi
  58. # Rebuild rpm database in case of different rpm version
  59. rm -f $DOM0_UPDATES_DIR/var/lib/rpm/__*
  60. rpm --root=$DOM0_UPDATES_DIR --rebuilddb
  61. if [ "$CLEAN" = "1" ]; then
  62. yum $OPTS clean all
  63. rm -f $DOM0_UPDATES_DIR/packages/*
  64. fi
  65. if [ "x$PKGLIST" = "x" ]; then
  66. echo "Checking for dom0 updates..." >&2
  67. UPDATES_FULL=`yum $OPTS check-update -q`
  68. if [ $? -eq 1 ]; then
  69. # Exit here if yum have reported an error. Exit code 100 isn't an
  70. # error, it's "updates available" info, so check specifically for exit code 1
  71. exit 1
  72. fi
  73. UPDATES=`echo "$UPDATES_FULL" | cut -f 1 -d ' ' | grep -v "^Obsoleting"`
  74. else
  75. PKGS_FROM_CMDLINE=1
  76. fi
  77. if [ -z "$PKGLIST" -a -z "$UPDATES" ]; then
  78. echo "No new updates available"
  79. if [ "$GUI" = 1 ]; then
  80. zenity --info --text="No new updates available"
  81. fi
  82. exit 0
  83. fi
  84. if [ "$CHECK_ONLY" = "1" ]; then
  85. echo "Available updates: $UPDATES_FULL"
  86. exit 100
  87. fi
  88. if [ "$DOIT" != "1" -a "$PKGS_FROM_CMDLINE" != "1" ]; then
  89. zenity --question --title="Qubes Dom0 updates" \
  90. --text="There are updates for dom0 available, do you want to download them now?" || exit 0
  91. fi
  92. YUM_COMMAND="fakeroot yum $YUM_ACTION -y --downloadonly --downloaddir=$DOM0_UPDATES_DIR/packages"
  93. # prefer yum-deprecated over dnf, because of still missing features in dnf (at least --downloaddir)
  94. if type dnf >/dev/null 2>&1 && type yum-deprecated >/dev/null 2>&1; then
  95. YUM_COMMAND="fakeroot yum-deprecated $YUM_ACTION -y --downloadonly --downloaddir=$DOM0_UPDATES_DIR/packages"
  96. # check for --downloadonly option - if not supported (Debian), fallback to
  97. # yumdownloader
  98. elif ! yum --help | grep -q downloadonly; then
  99. if [ "$YUM_ACTION" != "install" -a "$YUM_ACTION" != "upgrade" ]; then
  100. echo "ERROR: yum version installed in VM `hostname` does not suppport --downloadonly option" >&2
  101. echo "ERROR: only 'install' and 'upgrade' actions supported ($YUM_ACTION not)" >&2
  102. if [ "$GUI" = 1 ]; then
  103. zenity --error --text="yum version too old for '$YUM_ACTION' action, see console for details"
  104. fi
  105. exit 1
  106. fi
  107. if [ "$YUM_ACTION" = "upgrade" ]; then
  108. PKGLIST=$UPDATES
  109. fi
  110. YUM_COMMAND="yumdownloader --destdir=$DOM0_UPDATES_DIR/packages --resolve"
  111. fi
  112. mkdir -p "$DOM0_UPDATES_DIR/packages"
  113. set -e
  114. if [ "$GUI" = 1 ]; then
  115. ( echo "1"
  116. $YUM_COMMAND $OPTS $PKGLIST
  117. echo 100 ) | zenity --progress --pulsate --auto-close --auto-kill \
  118. --text="Downloading updates for Dom0, please wait..." --title="Qubes Dom0 updates"
  119. else
  120. $YUM_COMMAND $OPTS $PKGLIST
  121. fi
  122. if ls $DOM0_UPDATES_DIR/packages/*.rpm > /dev/null 2>&1; then
  123. cmd="/usr/lib/qubes/qrexec-client-vm dom0 qubes.ReceiveUpdates /usr/lib/qubes/qfile-agent"
  124. qrexec_exit_code=0
  125. $cmd $DOM0_UPDATES_DIR/packages/*.rpm || { qrexec_exit_code=$? ; true; };
  126. if [ ! "$qrexec_exit_code" = "0" ]; then
  127. echo "'$cmd $DOM0_UPDATES_DIR/packages/*.rpm' failed with exit code ${qrexec_exit_code}!" >&2
  128. exit $qrexec_exit_code
  129. fi
  130. else
  131. echo "No packages downloaded"
  132. fi