Chen Fan
2018-Jan-04 04:58 UTC
[Libguestfs] virtdf outputs on host differs from df in guest
hi guys, happy new year!!! I encountered a problem when we used virt-df command to display my guest disks useage. the virt-df command from pkg libguestfs-tools-c-1.36.3-6.el7_4.3.x86_64,version is virt-df --version : virt-df 1.36.3rhel=7,release=6.el7_4.3,libvirt the output in guest with the method referred in http://libguestfs.org/virt-df.1.html#STATVFS NUMBERS is: [root@localhost ~]# df Filesystem 1K-blocks Used Available Use% Mounted on /dev/mapper/centos-root 23544596 4335172 19209424 19% / devtmpfs 931220 0 931220 0% /dev tmpfs 941860 0 941860 0% /dev/shm tmpfs 941860 8612 933248 1% /run tmpfs 941860 0 941860 0% /sys/fs/cgroup /dev/sda1 508588 133328 375260 27% /boot tmpfs 188376 0 188376 0% /run/user/0 using scripts: python -c 'import os; s = os.statvfs ("/"); print s' posix.statvfs_result(f_bsize=4096, f_frsize=4096, f_blocks=5886149, f_bfree=4802342, f_bavail=4802342, f_files=23556096, f_ffree=23435372, f_favail=23435372, f_flag=4096, f_namemax=255) python -c 'import os; s = os.statvfs ("/boot"); print s' posix.statvfs_result(f_bsize=4096, f_frsize=4096, f_blocks=127147, f_bfree=93815, f_bavail=93815, f_files=512000, f_ffree=511626, f_favail=511626, f_flag=4096, f_namemax=255) and then on host using virt-df showing: # sudo virt-df rpm-build-for-7.2 Filesystem 1K-blocks Used Available Use% rpm-build-for-7.2:/dev/sda1 508588 107728 400860 22% rpm-build-for-7.2:/dev/centos/root 23544596 4298384 19246212 19% using scripts: # guestfish --ro -d rpm-build-for-7.2 -i statvfs / bsize: 4096 frsize: 4096 blocks: 5886149 bfree: 4810534 bavail: 4810534 files: 23556096 ffree: 23435372 favail: 23435372 fsid: 64513 flag: 4097 namemax: 255 # sudo guestfish --ro -d rpm-build-for-7.2 -i statvfs /boot bsize: 4096 frsize: 4096 blocks: 127147 bfree: 100215 bavail: 100215 files: 512000 ffree: 511626 favail: 511626 fsid: 2049 flag: 4097 namemax: 255 we can saw that in guest the statvfs get the value was the same with the "df" command output. but the bfree/bavail values are different between using guestfish statvfs on guest and using statvfs in guest. I want to know whether is this a bug? or Does same reason cause this appearance? Thanks, Chen
Richard W.M. Jones
2018-Jan-04 11:41 UTC
Re: [Libguestfs] virtdf outputs on host differs from df in guest
On Thu, Jan 04, 2018 at 12:58:40PM +0800, Chen Fan wrote: [In guest]> python -c 'import os; s = os.statvfs ("/"); print s' > posix.statvfs_result(f_bsize=4096, f_frsize=4096, f_blocks=5886149, > f_bfree=4802342, f_bavail=4802342, f_files=23556096, > f_ffree=23435372, f_favail=23435372, f_flag=4096, f_namemax=255) > > python -c 'import os; s = os.statvfs ("/boot"); print s' > posix.statvfs_result(f_bsize=4096, f_frsize=4096, f_blocks=127147, > f_bfree=93815, f_bavail=93815, f_files=512000, f_ffree=511626, > f_favail=511626, f_flag=4096, f_namemax=255)[From the host via libguestfs]> # guestfish --ro -d rpm-build-for-7.2 -i statvfs / > > bsize: 4096 > frsize: 4096 > blocks: 5886149 > bfree: 4810534 > bavail: 4810534 > files: 23556096 > ffree: 23435372 > favail: 23435372 > fsid: 64513 > flag: 4097 > namemax: 255 > > # sudo guestfish --ro -d rpm-build-for-7.2 -i statvfs /boot > bsize: 4096 > frsize: 4096 > blocks: 127147 > bfree: 100215 > bavail: 100215 > files: 512000 > ffree: 511626 > favail: 511626 > fsid: 2049 > flag: 4097 > namemax: 255Was the guest quiescent and fully sync()d before you ran the python commands? In any case all that libguestfs is doing is executing the statvfs(2) system call on a copy of the disk: https://github.com/libguestfs/libguestfs/blob/e6e23c4d77ee15779f833fd4fbe6c574940e85a4/daemon/statvfs.c#L52 So if there's any difference then it's something to do with the guest kernel vs the host kernel calculations. I wouldn't expect the numbers to be exactly the same, but libguestfs doesn't alter the numbers, it just passes them through. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com Fedora Windows cross-compiler. Compile Windows programs, test, and build Windows installers. Over 100 libraries supported. http://fedoraproject.org/wiki/MinGW
hi Jones, Thanks for your reply. At 2018-01-04 19:41:27, "Richard W.M. Jones" <rjones@redhat.com> wrote:>On Thu, Jan 04, 2018 at 12:58:40PM +0800, Chen Fan wrote: >[In guest] >> python -c 'import os; s = os.statvfs ("/"); print s' >> posix.statvfs_result(f_bsize=4096, f_frsize=4096, f_blocks=5886149, >> f_bfree=4802342, f_bavail=4802342, f_files=23556096, >> f_ffree=23435372, f_favail=23435372, f_flag=4096, f_namemax=255) >> >> python -c 'import os; s = os.statvfs ("/boot"); print s' >> posix.statvfs_result(f_bsize=4096, f_frsize=4096, f_blocks=127147, >> f_bfree=93815, f_bavail=93815, f_files=512000, f_ffree=511626, >> f_favail=511626, f_flag=4096, f_namemax=255) > >[From the host via libguestfs] >> # guestfish --ro -d rpm-build-for-7.2 -i statvfs / >> >> bsize: 4096 >> frsize: 4096 >> blocks: 5886149 >> bfree: 4810534 >> bavail: 4810534 >> files: 23556096 >> ffree: 23435372 >> favail: 23435372 >> fsid: 64513 >> flag: 4097 >> namemax: 255 >> >> # sudo guestfish --ro -d rpm-build-for-7.2 -i statvfs /boot >> bsize: 4096 >> frsize: 4096 >> blocks: 127147 >> bfree: 100215 >> bavail: 100215 >> files: 512000 >> ffree: 511626 >> favail: 511626 >> fsid: 2049 >> flag: 4097 >> namemax: 255 > >Was the guest quiescent and fully sync()d before you ran the python>commands?yes, almost the guest is quiescent, and after invoke sync(), the results have no different.> >In any case all that libguestfs is doing is executing the >statvfs(2) system call on a copy of the disk: > > https://github.com/libguestfs/libguestfs/blob/e6e23c4d77ee15779f833fd4fbe6c574940e85a4/daemon/statvfs.c#L52 > >So if there's any difference then it's something to do with the guest>kernel vs the host kernel calculations.I saw that when doing virt-df, virt-df created a temporary VM with the host kernel, so I recreate a temporary QEMU VM with command line: /usr/libexec/qemu-kvm -cpu host -m 1024 -nographic -kernel /var/tmp/.guestfs-0/appliance.d/kernel -initrd /var/tmp/.guestfs-0/appliance.d/initrd -append "console=ttyS0 root=/dev/vda guestfs_rescue=1" -device virtio-blk-pci,drive=disk0 -drive file=/var/tmp/.guestfs-0/appliance.d/root,id=disk0,if=none -device virtio-serial -chardev socket,id=charchannel0,path=/tmp/a,server,nowait -device virtserialport,nr=1,chardev=charchannel0,id=channel0,name=org.libguestfs.channel.0 -device virtio-blk-pci,drive=disk1 -drive file=/srv/imgs/centos-7.2.qcow2,if=none,id=disk1 then the console showing:><rescue> mount /dev/vdb1 /sysroot/[ 72.552185] SGI XFS with ACLs, security attributes, no debug enabled [ 72.568377] XFS (vdb1): Mounting V4 Filesystem [ 73.379679] XFS (vdb1): Starting recovery (logdev: internal) [ 73.406398] XFS (vdb1): Ending recovery (logdev: internal)><rescue> statvfsbash: statvfs: command not found><rescue> stat -f /sysrootFile: "/sysroot" ID: fd1100000000 Namelen: 255 Type: xfs Block size: 4096 Fundamental block size: 4096 Blocks: Total: 127147 Free: 93815 Available: 93815 Inodes: Total: 512000 Free: 511626 here the mount /dev/vdb1 device is the /boot partition from my guest, we can saw that the free blocks are the same as python command, so I think the host kernel does not affect the result. if I am wrong, pls correct me :) Thanks, Chen> >I wouldn't expect the numbers to be exactly the same, but libguestfs >doesn't alter the numbers, it just passes them through. > >Rich. > >-- >Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones >Read my programming and virtualization blog: http://rwmj.wordpress.com >Fedora Windows cross-compiler. Compile Windows programs, test, and >build Windows installers. Over 100 libraries supported. >http://fedoraproject.org/wiki/MinGW