From a14d3814ce0fedd9ed1c20713340f0f91f624b0e Mon Sep 17 00:00:00 2001 From: Pavel Balaev Date: Fri, 10 Jan 2025 17:30:42 +0300 Subject: [PATCH] img: fix incorrect disk sizes in raw format We cannot calculate the actual size of the raw image based on `.st_size` because it will show the virtual size and not the actual one. --- src/nm_core.h | 4 ++++ src/nm_vm_control.c | 4 ++-- src/nm_window.c | 4 ++-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/nm_core.h b/src/nm_core.h index d58ed07..12bc01b 100644 --- a/src/nm_core.h +++ b/src/nm_core.h @@ -51,6 +51,10 @@ #define NM_UNUSED __attribute__((__unused__)) +#ifndef S_BLKSIZE +#define S_BLKSIZE 512 +#endif + static const int NM_OK; static const int NM_ERR = -1; diff --git a/src/nm_vm_control.c b/src/nm_vm_control.c index 393a69f..56ff5b3 100644 --- a/src/nm_vm_control.c +++ b/src/nm_vm_control.c @@ -1121,10 +1121,10 @@ nm_str_t nm_vmctl_info(const nm_str_t *name) memset(&img_info, 0, sizeof(img_info)); stat(drive_path.data, &img_info); - nm_str_append_format(&info, "disk%zu%-7s%s [%.2gGb/%sGb real/virt, %s, " + nm_str_append_format(&info, "disk%zu%-7s%s [%.3gGb/%sGb real/virt, %s, " "%s] %s\n", n, ":", nm_vect_str_ctx(&vm.drives, NM_SQL_DRV_NAME + idx_shift), - (double) img_info.st_size / 1073741824, + (double) img_info.st_blocks * S_BLKSIZE / 1073741824, nm_vect_str_ctx(&vm.drives, NM_SQL_DRV_SIZE + idx_shift), nm_vect_str_ctx(&vm.drives, NM_SQL_DRV_TYPE + idx_shift), nm_vect_str_ctx(&vm.drives, NM_SQL_DRV_FMT + idx_shift), diff --git a/src/nm_window.c b/src/nm_window.c index 29f2914..3986b07 100644 --- a/src/nm_window.c +++ b/src/nm_window.c @@ -527,10 +527,10 @@ nm_print_vm_info(const nm_str_t *name, const nm_vmctl_data_t *vm, int status) stat(drive_path.data, &img_info); nm_str_format(&buf, - "disk%zu%-7s%s [%.2gGb/%sGb real/virt, %s, %s, discard=%s] %s", + "disk%zu%-7s%s [%.3gGb/%sGb real/virt, %s, %s, discard=%s] %s", n, ":", nm_vect_str_ctx(&vm_->drives, NM_SQL_DRV_NAME + idx_shift), - (double) img_info.st_size / 1073741824, + (double) img_info.st_blocks * S_BLKSIZE / 1073741824, nm_vect_str_ctx(&vm_->drives, NM_SQL_DRV_SIZE + idx_shift), nm_vect_str_ctx(&vm_->drives, NM_SQL_DRV_TYPE + idx_shift), nm_vect_str_ctx(&vm_->drives, NM_SQL_DRV_FMT + idx_shift),