Pino Toscano
2020-Feb-26 16:15 UTC
[Libguestfs] [PATCH] filesystems: fix size reporting for filesystems
The current way to get the size of a filesystem is to query the size in bytes of the device. However, this gives the whole size of the device where a filesystem is stored, and it does not consider the actual size for which the filesystem is configured (e.g. in case it was shrunk). A simple reproducer for this is: $ guestfish -N test.img=fs:ext4:2G resize2fs-size /dev/sda1 1073741824 As result, try to mount the filesystem, and get its actual statistics to determine its full size. In case mounting fails, fall back to the previous method, which is still a good value in the majority of the cases. Thanks to: Erik Skultety. --- cat/filesystems.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/cat/filesystems.c b/cat/filesystems.c index cec3c4e47..2b95d3112 100644 --- a/cat/filesystems.c +++ b/cat/filesystems.c @@ -508,7 +508,23 @@ do_output_filesystems (void) guestfs_pop_error_handler (g); if (!device || !subvolume) { - size = guestfs_blockdev_getsize64 (g, fses[i]); + /* Try mounting and stating the device. This might reasonably + * fail, so don't show errors. + */ + guestfs_push_error_handler (g, NULL, NULL); + + if (guestfs_mount_ro (g, fses[i], "/") == 0) { + CLEANUP_FREE_STATVFS struct guestfs_statvfs *stat = NULL; + + stat = guestfs_statvfs (g, "/"); + size = stat->blocks * stat->bsize; + guestfs_umount_all (g); + } else { + size = guestfs_blockdev_getsize64 (g, fses[i]); + } + + guestfs_pop_error_handler (g); + if (size == -1) exit (EXIT_FAILURE); } -- 2.24.1
Richard W.M. Jones
2020-Feb-26 18:42 UTC
Re: [Libguestfs] [PATCH] filesystems: fix size reporting for filesystems
On Wed, Feb 26, 2020 at 05:15:06PM +0100, Pino Toscano wrote:> The current way to get the size of a filesystem is to query the size in > bytes of the device. However, this gives the whole size of the device > where a filesystem is stored, and it does not consider the actual size > for which the filesystem is configured (e.g. in case it was shrunk). > A simple reproducer for this is: > $ guestfish -N test.img=fs:ext4:2G resize2fs-size /dev/sda1 1073741824 > > As result, try to mount the filesystem, and get its actual statistics to > determine its full size. In case mounting fails, fall back to the > previous method, which is still a good value in the majority of the > cases. > > Thanks to: Erik Skultety. > --- > cat/filesystems.c | 18 +++++++++++++++++- > 1 file changed, 17 insertions(+), 1 deletion(-) > > diff --git a/cat/filesystems.c b/cat/filesystems.c > index cec3c4e47..2b95d3112 100644 > --- a/cat/filesystems.c > +++ b/cat/filesystems.c > @@ -508,7 +508,23 @@ do_output_filesystems (void) > guestfs_pop_error_handler (g); > > if (!device || !subvolume) { > - size = guestfs_blockdev_getsize64 (g, fses[i]); > + /* Try mounting and stating the device. This might reasonably > + * fail, so don't show errors. > + */ > + guestfs_push_error_handler (g, NULL, NULL); > + > + if (guestfs_mount_ro (g, fses[i], "/") == 0) { > + CLEANUP_FREE_STATVFS struct guestfs_statvfs *stat = NULL; > + > + stat = guestfs_statvfs (g, "/"); > + size = stat->blocks * stat->bsize; > + guestfs_umount_all (g); > + } else { > + size = guestfs_blockdev_getsize64 (g, fses[i]); > + } > + > + guestfs_pop_error_handler (g); > + > if (size == -1) > exit (EXIT_FAILURE); > }Yes this is sensible, ACK. Thanks, Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-p2v converts physical machines to virtual machines. Boot with a live CD or over the network (PXE) and turn machines into KVM guests. http://libguestfs.org/virt-v2v
Maybe Matching Threads
- [PATCH] filesystems: don't try to get the size of btrfs subvolume
- [PATCH v2] filesystems: don't try to get the size of btrfs subvolume
- [PATCH 0/6] Update the way that API versions are generated for the man page.
- buffer overflow detected in collectd using libguestfs
- [PATCH 1/2] Fix bogus partition number passed to guestfs___check_for_filesystem_on