Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hard Disk Usage Miscalculation #16

Open
mobejm opened this issue Apr 20, 2023 · 1 comment
Open

Hard Disk Usage Miscalculation #16

mobejm opened this issue Apr 20, 2023 · 1 comment

Comments

@mobejm
Copy link

mobejm commented Apr 20, 2023

Running Ubuntu Server 22.04.2 LTS on an SSD connected using the provided USB adapter results in two partitions being created:

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2       440G  3.5G  419G   1% /
/dev/sda1       253M  149M  104M  59% /boot/firmware

The logic used to calculate the hard disk usage seems to assume there is one single partition which results in the hard disk usage being miscalculated.

/*
* get hard disk memory
*/
uint8_t get_hard_disk_memory(uint16_t *diskMemSize, uint16_t *useMemSize)
{
  *diskMemSize = 0;
  *useMemSize = 0;
  uint8_t diskMembuff[10] = {0};
  uint8_t useMembuff[10] = {0};
  FILE *fd = NULL;
  fd=popen("df -l | grep /dev/sda | awk '{printf \"%s\", $(2)}'","r");     <----------
  fgets(diskMembuff,sizeof(diskMembuff),fd);
  fclose(fd);

  fd=popen("df -l | grep /dev/sda | awk '{printf \"%s\", $(3)}'","r");     <----------
  fgets(useMembuff,sizeof(useMembuff),fd);
  fclose(fd);

  *diskMemSize = atoi(diskMembuff)/1024/1024;
  *useMemSize  = atoi(useMembuff)/1024/1024;
}
@ddelegal
Copy link

This is also an issue with the latest Raspbian (bookworm). Basically, the size of each sda* partition gets concatenated into a long string and throws the entire calculation off:

Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda2      114797372 5308884 103637540   5% /
/dev/sda1         522232   74104    448128  15% /boot/firmware

df -l | grep /dev/sda | awk '{printf "%s", $(2)}'
114797372522232

df -l | grep /dev/sda | awk '{printf "%s", $(3)}'
530888474104

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants