On Linux, statfs syscall reports free space in two fields.
From http://man7.org/linux/man-pages/man2/statfs.2.html:
1. f_bfree => Free blocks in filesystem
2. f_bavail => Free blocks available to unprivileged user
For file systems that maintain reserved space for system
activities f_bfree > f_bavail because f_bfree includes the
reserved space in the file system. The reserved space is
typically 5% for 'ext4' file systems. Newer distros like
RHEL 7.x use 'xfs' by default and report same value for
both the fields.
The Linux 'df' command uses f_bavail in its reporting.
Tools reports, conditionally, f_bfree for root and f_bavail
for non-root. However, since vmtoolsd runs as root, Tools
always reports f_bfree, which is more free space than 'df'
would report (depending on amount of reserved space).
In order to be consistent with Linux 'df' command,
report f_bavail as the disk free space by default. This does
change the behavior a little bit in that Tools will report less
disk free space than before, the difference being the same as
the reserved space on the file system, typically 5%. This
should be OK in general because it makes the space reporting
a bit conservative. If this change in behavior is not desired
for some use cases, the old behavior can be restored by setting
the following newly added configuration in this change:
[guestinfo]
diskinfo-include-reserved=true
The existing callers that are outside the guestInfo plugin
will continue to include reserved space in their space
accounting as before.
Also fixed a few minor stuff/touchups in vmtoolsConfig.c.
/* Now call the wiper lib to get space information. */
Str_Strcpy(p.mountPoint, pathName, sizeof p.mountPoint);
- wiperError = WiperSinglePartition_GetSpace(&p, freeBytes, totalBytes);
+ wiperError = WiperSinglePartition_GetSpace(&p, NULL, freeBytes, totalBytes);
if (strlen(wiperError) > 0) {
LOG(4, ("%s: error using wiper lib: %s\n", __FUNCTION__, wiperError));
/*********************************************************
- * Copyright (C) 2002-2016 VMware, Inc. All rights reserved.
+ * Copyright (C) 2002-2017 VMware, Inc. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published
*
*********************************************************/
-
/*
* conf.h --
*
#define CONFGROUPNAME_GUESTINFO "guestinfo"
/**
- * Lets users disable just the perf monitor.
+ * Lets user disable just the perf monitor.
*/
#define CONFNAME_GUESTINFO_DISABLEPERFMON "disable-perf-mon"
/**
- * Lets users disable just DiskInfo.
+ * Lets user disable just DiskInfo.
*
* If thinking of deprecating this, please read bug 535343 first.
*/
#define CONFNAME_GUESTINFO_EXCLUDENICS "exclude-nics"
+/**
+ * Lets user include reserved space in diskInfo space metrics on Linux.
+ *
+ * @param boolean Set to true to include reserved space.
+ */
+#define CONFNAME_DISKINFO_INCLUDERESERVED "diskinfo-include-reserved"
+
/*
* END GuestInfo goodies.
******************************************************************************
VMTools_ConfigGetBoolean(GKeyFile *config,
const gchar *section,
const gchar *key,
- gboolean defValue);
+ const gboolean defValue);
gint
VMTools_ConfigGetInteger(GKeyFile *config,
const gchar *section,
const gchar *key,
- gint defValue);
+ const gint defValue);
gchar *
VMTools_ConfigGetString(GKeyFile *config,
const gchar *section,
const gchar *key,
- gchar *defValue);
+ const gchar *defValue);
#if defined(G_PLATFORM_WIN32)
/*********************************************************
- * Copyright (C) 2004-2016 VMware, Inc. All rights reserved.
+ * Copyright (C) 2004-2017 VMware, Inc. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published
Bool Wiper_IsWipeSupported(const WiperPartition *);
unsigned char *WiperSinglePartition_GetSpace(const WiperPartition *p,
- uint64 *free,
- uint64 *total);
+ uint64 *avail,
+ uint64 *free,
+ uint64 *total);
/* External definition of the wiper state */
struct Wiper_State;
*/
unsigned char *
-WiperSinglePartition_GetSpace(const WiperPartition *p, // IN
- uint64 *free, // OUT
- uint64 *total) // OUT
+WiperSinglePartition_GetSpace(const WiperPartition *p, // IN
+ uint64 *avail, // OUT/OPT
+ uint64 *free, // OUT/OPT
+ uint64 *total) // OUT
{
#ifdef sun
struct statvfs statfsbuf;
blockSize = statfsbuf.f_bsize;
#endif
- if (geteuid()== 0) {
- *free = (uint64)statfsbuf.f_bfree * blockSize;
- } else {
- *free = (uint64)statfsbuf.f_bavail * blockSize;
+ if (avail) {
+ /*
+ * Free blocks available to non-superuser users.
+ * This excludes reserved blocks. Mostly applicable
+ * to 'ext' file systems. Newer file systems like
+ * 'xfs' report same value for f_bavail and f_bfree.
+ */
+ *avail = (uint64)statfsbuf.f_bavail * blockSize;
+ }
+
+ if (free) {
+ if (geteuid()== 0) {
+ /*
+ * Free blocks in the file system. This includes
+ * the reserved blocks too.
+ */
+ *free = (uint64)statfsbuf.f_bfree * blockSize;
+ } else {
+ /*
+ * Free blocks available to non-superuser users.
+ * This excludes reserved blocks.
+ */
+ *free = (uint64)statfsbuf.f_bavail * blockSize;
+ }
}
+
*total = (uint64)statfsbuf.f_blocks * blockSize;
return "";
uint64 *total) // OUT
{
ASSERT(state);
- return WiperSinglePartition_GetSpace(state->p, free, total);
+ return WiperSinglePartition_GetSpace(state->p, NULL, free, total);
}
/*********************************************************
- * Copyright (C) 2008-2016 VMware, Inc. All rights reserved.
+ * Copyright (C) 2008-2017 VMware, Inc. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published
*
* @param[in] config Config file to read the key from.
* @param[in] section Section to look for in the config file.
+ * @param[in] key Key to look for in the section.
* @param[in] defValue Default value if the key is not found or error.
*
* @return value of the key if value was read successfully, else defValue.
VMTools_ConfigGetBoolean(GKeyFile *config,
const gchar *section,
const gchar *key,
- gboolean defValue)
+ const gboolean defValue)
{
GError *err = NULL;
gboolean value;
*
* @param[in] config Config file to read the key from.
* @param[in] section Section to look for in the config file.
+ * @param[in] key Key to look for in the section.
* @param[in] defValue Default value if the key is not found or error.
*
* @return value of the key if value was read successfully, else defValue.
VMTools_ConfigGetInteger(GKeyFile *config,
const gchar *section,
const gchar *key,
- gint defValue)
+ const gint defValue)
{
GError *err = NULL;
gint value;
*
* @param[in] config Config file to read the key from.
* @param[in] section Section to look for in the config file.
+ * @param[in] key Key to look for in the section.
* @param[in] defValue Default value if the key is not found or error.
*
* @return value of the key if value was read successfully, else a copy
VMTools_ConfigGetString(GKeyFile *config,
const gchar *section,
const gchar *key,
- gchar *defValue)
+ const gchar *defValue)
{
GError *err = NULL;
gchar *value;
/*********************************************************
- * Copyright (C) 2014-2016 VMware, Inc. All rights reserved.
+ * Copyright (C) 2014-2017 VMware, Inc. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published
*/
GuestDiskInfo *
-GuestInfoGetDiskInfoWiper(void)
+GuestInfoGetDiskInfoWiper(Bool includeReserved) // IN
{
WiperPartition_List pl;
DblLnkLst_Links *curr;
PPartitionEntry newPartitionList;
PPartitionEntry partEntry;
unsigned char *error;
-
- error = WiperSinglePartition_GetSpace(part, &freeBytes, &totalBytes);
+ if (includeReserved) {
+ error = WiperSinglePartition_GetSpace(part, NULL,
+ &freeBytes, &totalBytes);
+ } else {
+ error = WiperSinglePartition_GetSpace(part, &freeBytes,
+ NULL, &totalBytes);
+ }
if (strlen(error)) {
- g_warning("GetDiskInfo: ERROR: could not get space for partition %s: %s\n",
- part->mountPoint, error);
+ g_warning("GetDiskInfo: ERROR: could not get space info for "
+ "partition %s: %s\n", part->mountPoint, error);
goto out;
}
/*********************************************************
- * Copyright (C) 2014-2016 VMware, Inc. All rights reserved.
+ * Copyright (C) 2014-2017 VMware, Inc. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published
* Contains POSIX-specific bits of gettting disk information.
*/
+#include "conf.h"
#include "util.h"
#include "vmware.h"
#include "guestInfoInt.h"
*/
GuestDiskInfo *
-GuestInfo_GetDiskInfo(void)
+GuestInfo_GetDiskInfo(const ToolsAppCtx *ctx)
{
- return GuestInfoGetDiskInfoWiper();
+ gboolean includeReserved;
+
+ /*
+ * In order to be consistent with the way 'df' reports
+ * disk free space, we don't include the reserved space
+ * while reporting the disk free space by default.
+ */
+ includeReserved = VMTools_ConfigGetBoolean(ctx->config,
+ CONFGROUPNAME_GUESTINFO,
+ CONFNAME_DISKINFO_INCLUDERESERVED,
+ FALSE);
+ if (includeReserved) {
+ g_debug("Including reserved space in diskInfo stats.\n");
+ } else {
+ g_debug("Excluding reserved space from diskInfo stats.\n");
+ }
+
+ return GuestInfoGetDiskInfoWiper(includeReserved);
}
/*********************************************************
- * Copyright (C) 2008-2016 VMware, Inc. All rights reserved.
+ * Copyright (C) 2008-2017 VMware, Inc. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published
GuestInfo_StatProviderPoll(gpointer data);
GuestDiskInfo *
-GuestInfoGetDiskInfoWiper(void);
+GuestInfoGetDiskInfoWiper(Bool includeReserved);
GuestDiskInfo *
-GuestInfo_GetDiskInfo(void);
+GuestInfo_GetDiskInfo(const ToolsAppCtx *ctx);
void
GuestInfo_FreeDiskInfo(GuestDiskInfo *di);
/*********************************************************
- * Copyright (C) 1998-2016 VMware, Inc. All rights reserved.
+ * Copyright (C) 1998-2017 VMware, Inc. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published
g_key_file_get_boolean(ctx->config, CONFGROUPNAME_GUESTINFO,
CONFNAME_GUESTINFO_DISABLEQUERYDISKINFO, NULL);
if (!disableQueryDiskInfo) {
- if ((diskInfo = GuestInfo_GetDiskInfo()) == NULL) {
+ if ((diskInfo = GuestInfo_GetDiskInfo(ctx)) == NULL) {
g_warning("Failed to get disk info.\n");
} else {
if (GuestInfoUpdateVmdb(ctx, INFO_DISK_FREE_SPACE, diskInfo, 0)) {