我长期以来一直在寻找这个问题的答案,尽管我见过一些提出的解决方案,但没有一个能起作用。
我可以使用以下命令查看所有准备更新的软件包:
apt list --upgradable
有些因为分阶段更新而被搁置。我想知道哪些会升级,哪些被搁置。
到目前为止我所做的就是发出这个命令(每个包一次):
apt-cache policy [packagename]
如果输出包含字符串(phased …%)
(百分号前一位、两位或三位数字),则表示该软件包是分阶段更新的一部分。但它不会告诉我它是否会立即安装在我的计算机上。
我如何知道哪些待处理的软件包可以立即安装,哪些软件包会由于分阶段更新而被阻止?
我正在运行 Ubuntu 22.04,以防万一。
谢谢
答案1
我还拼凑了一些命令。我主要apt
使用来解析输出awk
,但如果其他人知道更多本机命令就太好了。
如果你想找到全部正在分阶段更新的软件包及其相关的阶段百分比。
apt-cache dumpavail | awk '/^Package: /{p=$2} /^Phased-Update-Percentage/{print p,$2}'
然而,根据你可能只关心的问题已安装正在分阶段更新的软件包。
apt-cache dumpavail | awk '/^Package: /{p=$2} /^Phased-Update-Percentage/{cmd="dpkg -l "p" 2>&1 | grep -c ^ii";cmd | getline out;close(cmd); if(out > 0){print p,$2}}'
您还可以找到将被扣留的包裹。在这种情况下,在 期间full-upgrade
。
apt-get --simulate full-upgrade | awk '/^The following packages have been kept back:/{save=1;next} /^[[:graph:]]/{save=0;next} save{kp=kp$0} END{split(kp,kpa);for(kpi in kpa){print kpa[kpi]}}'
但是然后你想“告诉哪些待安装的软件包可以立即安装,哪些软件包会因为分阶段更新而被阻止”。把它们放在一起。
apt-cache dumpavail |
awk -v OFS="," -v kp="$(
apt-get --simulate full-upgrade | awk '/^The following packages have been kept back:/{save=1;next} /^[[:graph:]]/{save=0;next} save{kp=kp$0} END{print kp}'
)" \
'BEGIN{split(kp,kpa);for(kpi in kpa){kpd[kpa[kpi]]=""}} /^Package: /{p=$2} /^Phased-Update-Percentage/{cmd="dpkg -l "p" 2>&1 | grep -c ^ii";cmd | getline out;close(cmd); if(out > 0){if(p in kpd){print p,"phased "$2,"held back"}else{print p,"phased "$2,"not held back"}}}'
下面是最近修补的 22.04 服务器 (管道进入column
) 上的示例输出。
root@ubuntu:~# apt-cache dumpavail | awk -v OFS="," -v kp="$(apt-get --simulate full-upgrade | awk '/^The following packages have been kept back:/{save=1;next} /^[[:graph:]]/{save=0;next} save{kp=kp$0} END{print kp}')" 'BEGIN{split(kp,kpa);for(kpi in kpa){kpd[kpa[kpi]]=""}} /^Package: /{p=$2} /^Phased-Update-Percentage/{cmd="dpkg -l "p" 2>&1 | grep -c ^ii";cmd | getline out;close(cmd); if(out > 0){if(p in kpd){print p,"phased "$2,"held back"}else{print p,"phased "$2,"not held back"}}}' | column -t -s ,
grub-efi-amd64-bin phased 0 held back
grub-efi-amd64-signed phased 0 held back
libmbim-glib4 phased 70 held back
libmbim-proxy phased 70 held back
libmm-glib0 phased 70 not held back
libqmi-glib5 phased 70 held back
libqmi-proxy phased 70 held back
libsasl2-2 phased 70 held back
libsasl2-modules phased 70 held back
libsasl2-modules-db phased 70 held back
modemmanager phased 70 held back
python3-software-properties phased 0 held back
shim-signed phased 62 held back
software-properties-common phased 0 held back
tcpdump phased 20 not held back
ubuntu-advantage-tools phased 20 held back
结果与full-upgrade
输出相符
root@ubuntu:~# apt-get --simulate full-upgrade
...
The following packages have been kept back:
grub-efi-amd64-bin grub-efi-amd64-signed libmbim-glib4 libmbim-proxy libqmi-glib5 libqmi-proxy libsasl2-2 libsasl2-modules libsasl2-modules-db modemmanager python3-software-properties shim-signed software-properties-common ubuntu-advantage-tools
The following packages will be upgraded:
isc-dhcp-client isc-dhcp-common libmm-glib0 tcpdump
笔记
- 我使用 apt 在 Ubuntu 22.04 上测试了这些命令
2.4.8
。 - 有一些情况很可能是这些命令无法处理的。
- 这不是一个快速的解决方案,在我的最低规格虚拟机上运行需要花费 10 秒以上。
- 分阶段更新的处理方式似乎正在发生变化。在进入这个兔子洞之前,我曾经
apt-cache policy
确定过一个包是否被阻止。这似乎不再可靠。我找到了一个APT::Get::Phase-Policy=True
有用的 apt 配置选项,但结果与实际的 apt 行为不一致。这是我在解析 的输出时生成的输出apt-cache policy
,但它确实不是匹配 apt 会做的事情。
root@ubuntu:~# for i in $(apt-cache dumpavail | awk '/^Package: /{p=$2} /^Phased-Update-Percentage/{cmd="dpkg -l "p" 2>&1 | grep -c ^ii";cmd | getline out;close(cmd); if(out > 0){print p}}'); do apt-cache -o=APT::Get::Phase-Policy=True policy $i | awk -v p=$i 'NR==2{iv=$2} NR==3{cv=$2} /phased/{pv=$1;pp=$3" "$4;exit} END{if( cv == pv ) { printf "%s,%s,not held back\n",p,pp }else{printf "%s,%s,held back\n",p,pp}}'; done | column -t -s ,
grub-efi-amd64-bin (phased 0%) held back
grub-efi-amd64-signed (phased 0%) held back
libmbim-glib4 (phased 70%) held back
libmbim-proxy (phased 70%) held back
libmm-glib0 (phased 70%) not held back
libqmi-glib5 (phased 70%) not held back
libqmi-proxy (phased 70%) not held back
libsasl2-2 (phased 70%) held back
libsasl2-modules (phased 70%) held back
libsasl2-modules-db (phased 70%) held back
modemmanager (phased 70%) not held back
python3-software-properties (phased 0%) held back
shim-signed (phased 62%) held back
software-properties-common (phased 0%) held back
tcpdump (phased 20%) not held back
ubuntu-advantage-tools (phased 20%) held back
答案2
以下是一种方法:
//The list of all upgradeable packages
$ apt list --upgradeable | cut -d'/' -f1
Listing...
tcpdump
ubuntu-advantage-tools
//The list of upgradeable packages that are NOT phased
//...or are phasing, and it's your turn for install
$ apt upgrade --simulate | grep Inst | cut -d' ' -f2
ubuntu-advantage-tools
当然,分阶段套餐的清单是清单之间的差异。
答案3
输出apt upgrade
将准确地告诉您哪些软件包将被升级以及哪些软件包将被保留。
您还可以使用apt -s upgrade
它来运行模拟,它将为您提供相同的所需信息。
$ apt -s upgrade
NOTE: This is only a simulation!
apt needs root privileges for real execution.
Keep also in mind that locking is deactivated,
so don't depend on the relevance to the real current situation!
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Calculating upgrade... Done
Get more security updates through Ubuntu Pro with 'esm-apps' enabled:
libimage-magick-perl imagemagick libdcmtk16 libopenexr25 libmagick++-6.q16-8
libmagickcore-6.q16-6-extra libimage-magick-q16-perl libmagickwand-6.q16-6
imagemagick-6.q16 libeditorconfig0 libmagickcore-6.q16-6
imagemagick-6-common
Learn more about Ubuntu Pro at https://ubuntu.com/pro
The following packages have been kept back:
grub-efi-amd64 grub-efi-amd64-bin grub-efi-amd64-signed libsasl2-2 libsasl2-modules libsasl2-modules-db
libsnmp-base libsnmp40 shim-signed tcpdump
0 upgraded, 0 newly installed, 0 to remove and 10 not upgraded.
由于apt 2.7.11
的输出apt upgrade
已经得到改进,并且准确地告诉我们由于分阶段更新而保留了哪些包:
Calculating upgrade... Done
The following upgrades have been deferred due to phasing:
接下来是受影响软件包的列表。
Ubuntu 24.04apt 2.7.14
是第一个引入此行为的 Ubuntu 版本。