一,fallocate的用途?
1,用途
我們有時需要用大文件來測試下載速度,
有時需要用大文件來覆蓋磁盤空間,
如果在網上搜索,很多文章講的是使用dd等工具,
事實上linux系統已經內置了生成大文件的工具,
fallocate
使用非常的方便
2,fallocate和truncate有所區別
fallocate:為文件預分配物理空間,不是生成空洞文件
而且fallocate分配的空間在磁盤的扇區上是連續的,
能減少后續寫入和讀取文件時的磁盤尋道的資源開銷
truncate:生成的是空洞文件,並不占用實際的磁盤空間,
只是文件看上去邏輯大小比較大,
但實際占用的空間是0
本文中會舉例子來說明
3,什么是空洞文件?
UNIX的文件操作,允許操作的偏移位置超過當前的文件長度,
這樣中間留出的空間的字節會被設置為0,
這部分空間被稱為空洞(hole)
linux系統上空洞不占用實際的硬盤空間
說明:劉宏締的架構森林是一個專注架構的博客,地址:https://www.cnblogs.com/architectforest
對應的源碼可以訪問這里獲取: https://github.com/liuhongdi/
說明:作者:劉宏締 郵箱: 371125307@qq.com
二,查看fallocate命令所屬的包?
[root@centos8 logs]# whereis fallocate fallocate: /usr/bin/fallocate /usr/share/man/man1/fallocate.1.gz /usr/share/man/man2/fallocate.2.gz [root@centos8 logs]# rpm -qf /usr/bin/fallocate util-linux-2.32.1-17.el8.x86_64
fallocate在centos8上默認已內置,
如果有誤刪此命令,可以用dnf安裝
[root@centos8 logs]# dnf install util-linux
三,查看fallocate的版本和幫助
1,查看fallocate的版本
[root@centos8 logs]# fallocate --version fallocate,來自 util-linux 2.32.1
2,查看fallocate的幫助
[root@centos8 logs]# fallocate --help 用法: fallocate [選項] <文件名> 為文件預分配空間或從文件回收空間。 選項: -c, --collapse-range 移除文件中的某個范圍 -d, --dig-holes 檢測零並替換為空洞 -i, --insert-range insert a hole at range, shifting existing data -l, --length <數字> 范圍操作的長度(字節數) -n, --keep-size 保持文件的顯示大小 -o, --offset <數字> 范圍操作的偏移量(字節數) -p, --punch-hole 將某個范圍替換為空洞(連帶打開 -n) -z, --zero-range 清零並保證分配某個范圍 -x, --posix use posix_fallocate(3) instead of fallocate(2) -v, --verbose 詳盡模式 -h, --help display this help -V, --version display version 更多信息請參閱 fallocate(1)。
3,查看fallocate的手冊
[root@centos8 logs]# man fallocate
四,fallocate的使用例子:
1,按指定大小生成文件
#-l: length,指定文件的長度
[root@centos8 logs]# fallocate -l 50M /data/web/www/html/b.zip
查看文件的顯示大小
#-h: human-readable,用人性化的方性顯示結果
[root@centos8 html]# ll -h b.zip -rw-r--r-- 1 root root 50M 5月 8 14:25 b.zip
查看文件占用磁盤空間的大小
[root@centos8 html]# du -sh b.zip 50M b.zip
說明:我們用du查看文件可以看到,
用fallocate生成的文件在磁盤上確實占用了50M的空間
而不是一個空洞文件
2,把前面創建的大文件中的零替換為空洞:
#-d:挖洞,僅替換為0的數據,使不再占用多余的磁盤空間
[root@centos8 html]# fallocate -d b.zip
查看占用磁盤空間的大小
[root@centos8 html]# du -sh b.zip 84K b.zip
查看文件顯示大小
[root@centos8 html]# ll -h b.zip -rw-r--r-- 1 root root 50M 5月 8 15:21 b.zip
邏輯大小還是50M,
但對磁盤的占用變成了84K
3,在文件上挖出指定大小的空洞
生成一個占用30M磁盤空間的文件
生成一個30M大小的文件
[root@centos8 html]# fallocate -l 30M c.zip [root@centos8 html]# ll -h c.zip -rw-r--r-- 1 root root 30M 5月 8 15:33 c.zip [root@centos8 html]# du -sh c.zip 30M c.zip
du命令顯示文件確實是30M大小
從偏移10M的位置挖一個10M大小的洞
應該還剩20M
#-p:挖洞,不管文件中是否有非0數據,會改變文件的內容
#-o: 偏移位置
#-l: 挖洞的大小
[root@centos8 html]# fallocate -p -o 10M -l 10M c.zip
查看文件顯示大小
[root@centos8 html]# ll -h c.zip -rw-r--r-- 1 root root 30M 5月 8 15:38 c.zip
查看文件占用磁盤空間的大小
[root@centos8 html]# du -sh c.zip 20M c.zip
五,看一個truncate的例子:
#-s: 指定文件大小
[root@centos8 logs]# truncate -s 100M ./trunc100
查看文件顯示大小
[root@centos8 logs]# ll -h trunc100 -rw-r--r-- 1 root root 100M 5月 8 14:57 trunc100
查看文件占用磁盤空間的大小
[root@centos8 logs]# du -sh trunc100 0 trunc100
可見文件占用磁盤空間為0
用od把生成的文件打印出來,可以看到中間填充的是\0
#-c, select printable characters or backslash escapes
#-c: 相當於 -t c ,表示輸出可打印字符和轉義字符
[root@centos8 logs]# od -c trunc100 0000000 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 * 620000000
六,查看centos的版本
[root@centos8 logs]# cat /etc/redhat-release CentOS Linux release 8.1.1911 (Core)