实测虚拟机中使用fallocate后是否会真实占用宿主机的空间

文章目录[隐藏]

前言

fallocate使用后会在系统中声明所占用空间从而实现快速新建一个文件的功能。根据官方manual的说法,对于支持fallocate系统调用的文件系统(目前知道我们最爱的ZFS原生格式并不支持,但是挂载成volume格式化成ext4后就支持了),可通过分配块并将其标记为未初始化来快速完成预分配,而无需对数据块进行任何IO操作。 这比通过用零填充文件来创建文件快得多。
参考:https://askubuntu.com/questions/47963/how-can-i-quickly-make-a-large-file



$ time sh -c 'fallocate -l 10G large; sync'

real 0m0.038s user 0m0.000s sys 0m0.016s $ du -B 1 –apparent-size large 10737418240 large $ du -B 1 large 10737422336 large

同时由于这个占位的指令对于系统而言是占用的真实空间,因此会在du和df上体现出来。
那么如果在虚拟机中进行占位操作,会不会真的占用宿主机的磁盘空间呢?这里做一些实际测试以解答这个问题。

lvm磁盘

lvm肯定是占用的,毕竟直通lvm给虚拟机了

LXC虚拟化+raw磁盘

占用空间前

宿主机端,测试前分配磁盘空间8GB,实际占用220M:



root@HPC-HEL1-C103 /mnt/pve/HEL1-S101/images/106 # ls -lh

total 220M

-rw-r--r-- 1 root root 8.0G Dec 23 19:05 vm-106-disk-0.raw

虚拟机端,空;可用空间7G:



[root@lxc-test ~]# ls -lh

total 0

[root@lxc-test ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/loop1 7.9G 465M 7.0G 7% / none 492K 4.0K 488K 1% /dev udev 189G 0 189G 0% /dev/tty tmpfs 189G 0 189G 0% /dev/shm tmpfs 189G 8.1M 189G 1% /run tmpfs 189G 0 189G 0% /sys/fs/cgroup

运行fallocate



[root@lxc-test ~]# fallocate -l 1G test

占用空间后

宿主机端,测试前分配磁盘空间8GB,实际占用220M:



root@HPC-HEL1-C103 /mnt/pve/HEL1-S101/images/106 # ls -lh

total 220M

-rw-r--r-- 1 root root 8.0G Dec 23 19:23 vm-106-disk-0.raw

虚拟机端,文件1G,实际占用1.1G;可用空间6G:



[root@lxc-test ~]# ls -lh

total 1.1G

-rw-r--r-- 1 root root 1.0G Dec 23 18:22 test

[root@lxc-test ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/loop1 7.9G 1.5G 6.0G 20% / none 492K 4.0K 488K 1% /dev udev 189G 0 189G 0% /dev/tty tmpfs 189G 0 189G 0% /dev/shm tmpfs 189G 8.1M 189G 1% /run tmpfs 189G 0 189G 0% /sys/fs/cgroup

KVM虚拟化+raw磁盘

KVM虚拟化+qcow2磁盘


This article is under CC BY-NC-SA 4.0 license.
Please quote the original link:https://www.liujason.com/article/1127.html
Licensed under CC BY-NC-SA 4.0
comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy