最後更新: 2017-11-20
目錄
Debian package
/usr/bin/fallocate
- package: util-linux
/usr/bin/truncate
- package: coreutils
/bin/dd
- package: coreutils
ls Opts
truncate
說明
shrink or extend the size of a file
For filesystems which support the fallocate system call (btrfs, ext4, ocfs2, and xfs)
* no IO to the data blocks (much faster than creating a file by filling it with zeros("dd"))
* shrink: If a FILE is larger than the specified size, the extra data is lost.
* extend: If a FILE is shorter, it extended part (hole) reads as zero bytes
Opts
-s[+/-]N(K,M,G) # size
i.e.
truncate -s 1g iscsi.img
# "ls -s" print the allocated size of each file, in blocks
ls -slh iscsi.img
0 -rw-r--r--. 1 root root 1.0G Nov 19 23:33 iscsi.img
fallocate
fallocate is used to manipulate the allocated disk space for a file, either to deallocate or preallocate it.
說明
Preallocate space to a file # 須要 fs (ext4, btrfs ...) 支援 fallocate system call
preallocation is done quickly by allocating blocks and marking them as uninitialized
no IO to the data blocks => faster than creating a file by filling it with zeros.
fallocate 沒有 overcommit 效果 (因為 allocating blocks and marking them as uninitialized)
fallocate: test.bin: fallocate failed: No space left on device
Opts
-l, --length length # Specifies the length of the range, in bytes.
- KiB (=1024), MiB (=1024*1024) # The "iB" is optional
- KB (=1000), MB (=1000*1000)
no overcommit test
# fallocate 前
df -h /ssd
Filesystem Size Used Avail Use% Mounted on
/dev/sdc1 230G 96G 122G 45% /ssd
# fallocate 1G iscsi.img
fallocate -l 1G 1G.img
# df -h 會見到 "Used" 多了 1G
df -h /ssd
Filesystem Size Used Avail Use% Mounted on
/dev/sdc1 230G 97G 121G 45% /ssd
ls -sl *.img # "-s" print the allocated size of each file, in blocks
1025M -rw-r--r-- 1 root root 1024M Sep 20 14:42 1G.img
holes & zero opts
支持的FS: EXT4, XFS, BTRFS
punch a hole
a portion of the file can be marked as unwanted and the associated storage released
-d, --dig-holes
Detect and dig holes. This makes the file sparse in-place, without using extra disk space.
You can think of this option as doing a "cp --sparse" and then renaming the destination file to the original,
without the need for extra disk space.
If no range is specified by --offset and --length, then the entire file is analyzed for holes
The minimum size of the hole depends on filesystem I/O block size (usually 4096 bytes)
ie.
dd if=/dev/zero of=zero.bin bs=1M count=100
100+0 records in 100+0 records out 104857600 bytes (105 MB, 100 MiB) copied, 0.0447315 s, 2.3 GB/s
ll -sh zero.bin
100M -rw-r--r-- 1 root root 100M Feb 21 14:39 zero.bin
fallocate -d zero.bin
ll -sh zero.bin
0 -rw-r--r-- 1 root root 100M Feb 21 14:39 zero.bin
-p, --punch-hole
Deallocates space (i.e., creates a hole) in the byte range starting at offset and continuing for length bytes.
Within the specified range, partial filesystem blocks are zeroed, and whole filesystem blocks are removed from the file.
* regardless of what the file contains at that offset: it works even if the file contains non-zeroes there
ie.
echo -n 1111111111111111 > test.txt
hexdump -C test.txt
00000000 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 |1111111111111111| 00000010
fallocate -p -o 8 -l 4 zero.bin
fallocate -p -o 8 -l 4 test.txt
hexdump -C test.txt
00000000 31 31 31 31 31 31 31 31 00 00 00 00 31 31 31 31 |11111111....1111|
00000010
-z, --zero-range
Zeroes space in the byte range starting at offset and continuing for length bytes.
Zeroing is done within the filesystem preferably by converting the range into unwritten extents.
I/O is required only to update metadata. (will not be physically zeroed out on the device)
dd
# Create 1G file (fill with zero)
dd bs=1M count=1024 if=/dev/zero of=test.bin