如何设置可增长的环回设备?

phu*_*ehe 25 loop-device

我知道我可以创建和使用这样的环回设备:

# Create the file
truncate disk.img --size 2G
# Create a filesystem
mkfs.ext4 disk.img
# Mount to use
mount disk.img /mnt
# Clean up
umount /mnt
Run Code Online (Sandbox Code Playgroud)

但是,在这种情况下,磁盘映像固定为 2GB。空时为2GB,满时为2GB。它不会生长。

有没有一种可以增加大小的环回设备?或者,是否有一种环回设备只需要它存储的空间?

Ste*_*han 18

使用 dd 创建稀疏文件设备。

df -hm # to show where we started
dd of=sparse-file bs=1k seek=102400 count=0 # creates a 100Meg sparsefile
mkfs.ext4 sparse-file
mkdir blah
mount sparse-file blah
cp somefile blah
ls -lahts sparse-file  # The 's' option will report the actual space taken in the first column
ls -lahts blah
df -hm # doublecheck my work
echo 'profit :)'
Run Code Online (Sandbox Code Playgroud)

参考:维基百科稀疏文件文章


phu*_*ehe 10

@jordanm 的评论指出了这一点。当我查看ls -lh disk.img. 当我ls -s disk.img@Stephan 的回答中使用like 时,会显示实际文件大小。作为测试,我创建了一个比我的硬盘大的图像文件:

truncate test.img -s 1000G
Run Code Online (Sandbox Code Playgroud)

它工作得很好,这意味着答案就在问题中:)