在Linux平台找出某个目录下创建时间最早的文件,测试验证脚本结果是否准确的过程中发现一个很有意思的现象,stat命令在一些平台下Birth字段有值,而在一些平台则为空值,如下所示:
RHEL 8.7下, XFS文件系统
[mysql@mysqlu02 ~]$ more /etc/redhat-release Red Hat Enterprise Linux release 8.7 (Ootpa) [mysql@mysqlu02 ~]$ touch test.txt [mysql@mysqlu02 ~]$ stat test.txt File: test.txt Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: fd05h/64773d Inode: 144 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 801/ mysql) Gid: ( 800/ mysql) Access: 2023-05-06 17:09:44.428050549 +0800 Modify: 2023-05-06 17:09:44.428050549 +0800 Change: 2023-05-06 17:09:44.428050549 +0800 Birth: 2023-05-06 17:09:44.428050549 +0800
RHEL 7.9 XFS文件系统
[oracle@KerryDB ~]$ more /etc/redhat-release Red Hat Enterprise Linux Server release 7.9 (Maipo) [oracle@KerryDB ~]$ touch test.txt [oracle@KerryDB ~]$ stat test.txt File: ‘test.txt’ Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: fd03h/64771d Inode: 4238838 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 700/ oracle) Gid: ( 600/oinstall) Access: 2023-05-06 17:03:42.964517675 +0800 Modify: 2023-05-06 17:03:42.964517675 +0800 Change: 2023-05-06 17:03:42.964517675 +0800 Birth: - [oracle@KerryDB ~]$
stat命令的Birth字段表示文件的创建时间,该属性是ext4的新功能(当然也适用于xfs文件系统),也称为crtime或btime,但是当前两个测试环境的文件系统为xfs,之前也在xfs文件系统中测试过,发现xfs不支持creation time. 但是从xfs v5开始,xfs已经支持creation time,文档[1]中已有阐述,如下所示:
/* version 5 filesystem (inode version 3) fields start here */ __le32 di_crc; __be64 di_changecount; __be64 di_lsn; __be64 di_flags2; __be32 di_cowextsize; __u8 di_pad2[12]; xfs_timestamp_t di_crtime; __be64 di_ino; uuid_t di_uuid; }; *di_crtime*:: Specifies the time when this inode was created.
但是上面这个差异现象,还是让我有点好奇,因为两个测试环境对应的xfs版本都是v5,如下所示。
KerryDB这台机器的xfs版本信息:
Oracle用户:
$ uname -r 3.10.0-1160.80.1.el7.x86_64 $ dmesg | grep -iE 'xfs.*\s+mounting' | head -1 [ 4.041026] XFS (dm-0): Mounting V5 Filesystem
root用户:
# xfs_db -r /dev/mapper/vg00-home xfs_db> version versionnum [0xb4b5+0x18a] = V5,NLINK,DIRV2,ATTR,ALIGN,LOGV2,EXTFLG,MOREBITS,ATTR2,LAZYSBCOUNT,PROJID32BIT,CRC,FTYPE xfs_db>
mysqlu02这台机器的xfs版本信息:
mysql用户:
$ dmesg | grep -iE 'xfs.*\s+mounting' | head -1 [ 7.654324] XFS (dm-0): Mounting V5 Filesystem
root用户:
# xfs_db -r /dev/mapper/vg00-home xfs_db> version versionnum [0xb4b5+0x18a] = V5,NLINK,DIRV2,ATTR,ALIGN,LOGV2,EXTFLG,MOREBITS,ATTR2,LAZYSBCOUNT,PROJID32BIT,CRC,FTYPE,FINOBT,SPARSE_INODES,REFLINK xfs_db>
那么到底是什么原因导致。Google搜索了相关资料,原来,如果stat命令查看xfs文件系统的文件时,如果要Birth字段不显示空置,必须满足几个条件,一个是xfs的版本为v5,另外,对操作系统内核版本也有要求(如下所示)
stat “now prints file creation time when supported by the file system, on GNU Linux systems with glibc >= 2.28 and kernel >= 4.11.”
KerryDB这台服务器内核信息如下所示
$ uname -r 3.10.0-1160.80.1.el7.x86_64
mysqlu02这台服务器的内核信息如下所示:
$ uname -r 4.18.0-425.3.1.el8.x86_64
如上所示,stat命令Birth字段为空置的操作系统内核版本为3.10.0,不满足条件kernerl >=4.11 所以stat命令显示空值。
1: https://git.kernel.org/pub/scm/fs/xfs/xfs-documentation.git/tree/design/XFS_Filesystem_Structure/ondisk_inode.asciidoc