如何确定上次启动特定内核版本的时间?

找出上次启动特定内核版本的时间

对于那些手动安装内核版本的人来说, /boot会随着时间的推移而变大。 我想找出哪些内核版本在很长一段时间内没有被启动作为候选删除。

文件上次访问时间

为了方便这个项目,我需要知道每个内核最后一次启动的时间。 我看到了使用atime查找比特定日期更早的文件的问答。 然而,此问答搜索了超过x天的文件。 我正在寻找所有文件,并想知道上次访问时间。

通过bash脚本如何确定给定文件的上次访问时间?

编辑1 – 必须在引导期间设置内核版本的上次访问时间

当grub安装内核时,它处于ro (只读)模式,并且不会更新上次访问时间。

如果您运行update-initramfs -u -k all文件initrd.img ,则为所有内核更新上次访问时间,即使它们尚未在今天启动。

安装新内核时,所有以前的内核版本文件system.map-wxyy-zzz上次访问时间都会更新,即使它们今天尚未启动。

要正确记录内核版本何时被真正启动,我们需要touch文件vmlinuz-wxyy-zzz 。 使用sudo powers在/etc/cron.d/创建这样的文件:

 SHELL=/bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin @reboot root touch "/boot/vmlinuz-"`uname -r` 

现在使用muru的答案在/boot列出文件时:

find / boot / vm * -printf“%Ac%p \ n”

 Thu 21 Jul 2016 05:02:48 AM MDT /boot/vmlinuz-3.13.0-92-generic Wed 26 Oct 2016 05:10:08 PM MDT /boot/vmlinuz-3.2.0-113-generic Sat 15 Oct 2016 10:45:41 AM MDT /boot/vmlinuz-4.4.0-43-generic Thu 20 Oct 2016 06:09:00 PM MDT /boot/vmlinuz-4.4.0-45-generic Sat 06 Aug 2016 09:32:02 PM MDT /boot/vmlinuz-4.6.3-040603-generic Sun 21 Aug 2016 12:59:04 PM MDT /boot/vmlinuz-4.7.1-040701-generic Fri 26 Aug 2016 04:51:04 AM MDT /boot/vmlinuz-4.7.2-040702-generic Thu 08 Sep 2016 06:46:52 PM MDT /boot/vmlinuz-4.7.3-040703-generic Sun 25 Sep 2016 07:25:46 PM MDT /boot/vmlinuz-4.7.5-040705-generic Sat 08 Oct 2016 03:08:45 PM MDT /boot/vmlinuz-4.8.1-040801-generic Sat 22 Oct 2016 08:16:44 AM MDT /boot/vmlinuz-4.8.4-040804-generic Sun 30 Oct 2016 12:56:12 PM MDT /boot/vmlinuz-4.8.5-040805-generic 

在安装新内核版本之前检查可用空间

在安装新内核之前,最好检查/boot可用的空间和/或这些命令已经使用了多少空间:

 rick@dell:~$ df /boot Filesystem 1K-blocks Used Available Use% Mounted on /dev/sdc3 30106300 20449376 8104556 72% / ──────────────────────────────────────────────────────────────── rick@dell:~$ du /boot --max-depth 0 -h 565M /boot 

要通过删除特定的先前内核来查看将节省多少空间,请使用以下命令:

 rick@dell:~$ du /boot/*4.8.1* -h 1.4M /boot/abi-4.8.1-040801-generic 204K /boot/config-4.8.1-040801-generic 44M /boot/initrd.img-4.8.1-040801-generic 3.6M /boot/System.map-4.8.1-040801-generic 4.8M /boot/vmlinuz-4.8.1-040801-generic 

使用stat命令:

  %x time of last access, human-readable %X time of last access, seconds since Epoch 

所以:

 stat -c %X /some/file 

或者find

 find /some/path -printf "%A@ %p\n" 

因为find-printf

  %a File's last access time in the format returned by the C `ctime' function. %Ak File's last access time in the format specified by k, which is either `@' or a directive for the C `strftime' function. The possible values for k are listed below; some of them might not be available on all systems, due to differences in `strftime' between systems. @ seconds since Jan. 1, 1970, 00:00 GMT, with fractional part. 

我接受了muru的答案,因为它正确地显示了如何查找文件的上次访问时间。 但是当grub挂载内核时,它处于ro只读模式,因此不会更新上次访问时间。

此外,当您运行update-initramfs -u -k all文件initrd.img都会更新为所有内核的当前时间,即使它今天尚未启动。

安装新内核时,即使今天尚未启动,也会访问所有内核文件system.map-wxyy-zzz

要正确记录上次使用sudo powers引导内核的时间,请在/etc/cron.d/创建这样的文件:

 SHELL=/bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin @reboot root touch "/boot/vmlinuz-"`uname -r` 

现在,当使用muru的答案列出/boot文件时, vmlinuz-xwyy-zzz会在上次启动该内核时显示。