how to get size of folder including apparent size

2019-06-25 01:06发布

I have a folder containing a lot of KVM qcow2 files, they are all sparse files. Now I need to get the total size of folder, the qcow2 file size should be counted as apparent size(not real size).

for example:

image: c9f38caf104b4d338cc1bbdd640dca89.qcow2 file format: qcow2 virtual size: 100G (107374182400 bytes) disk size: 3.3M cluster_size: 65536

the image should be treated as 100G but not 3.3M

originally I use statvfs() but it can only return real size of the folder. then I switch to 'du --apparent-size', but it's too slow given I have 10000+ files and it takes almost 5 minutes to caculate.

anybody knows a fast way that can get the size of folder counting qcow2's virtual size? thank you

标签: linux kvm du
3条回答
冷血范
2楼-- · 2019-06-25 01:54

There is no way to find out this information without stat()ing every file in the directory. It is slow if you have this many files in a single directory. stat() needs to retrieve the inode of every single file.

Adding more memory might help due to caching.

查看更多
仙女界的扛把子
3楼-- · 2019-06-25 01:57

Modern Unix*ish OSes provide a way to retrieve the stats of all entries of a directory in one step. This also needs to look at all inodes but probably it can be done optimized in the file system driver itself and thus might be faster.

Apparently you are not looking for a way to do this using system calls from C, so I guess a feasible approach could be to use Python. There you have access to this feature using the function scandir() in module os.

查看更多
ゆ 、 Hurt°
4楼-- · 2019-06-25 01:59

You could use something like this:

find images/ -name "*.qcow2" -exec qemu-img info {} \; | grep virtual | cut -d"(" -f2 | awk '{ SUM += $1} END { print SUM }'

查看更多
登录 后发表回答