df 中的 1K 區塊是什麼?

df 中的 1K 區塊是什麼?

我搜索了很多,沒有找到1K-blocksdf 命令(gnu)中的含義,但我已經計算並認為它等於1K Byte?有官方解釋嗎?

那麼如何計算呢Used Percentage

例如:

tankywoo@gentoo-jl::~/ » df -h
Filesystem                   Size  Used Avail Use% Mounted on
/dev/sda3                     15G  5.9G  8.2G  42% /

tankywoo@gentoo-jl::~/ » df
Filesystem                  1K-blocks    Used Available Use% Mounted on
/dev/sda3                    15481840 6163320   8532088  42% /

在我的本地機器中,我知道有reserved space.

使用的是 6163320,可用的是 8532088,所以:

我認為Used%應該是(15481840-8532088)/15481740 = 44.88%,而不是42%。

那麼如何得到結果呢42%

答案1

GNU coreutils df(1) 中的 1K 區塊意味著 1024 位元組。透過快速查看 GNU coreutils 版本 8.13 原始碼來確認:

964   if (human_output_opts == -1)
965     {
966       if (posix_format)   
967         {
968           human_output_opts = 0;
969           output_block_size = (getenv ("POSIXLY_CORRECT") ? 512 : 1024);
970         }
971       else             
972         human_options (getenv ("DF_BLOCK_SIZE"),
973                        &human_output_opts, &output_block_size);
974     }

如您所見,除非POSIXLY_CORRECT設定了環境變量,否則預設輸出區塊大小為 1024。

當計算使用百分比時,當底層檔案系統支援保留空間/區塊時(大多數支援),df(1) 會從可用空間中減去 root 使用者的保留空間/區塊:

529   if (known_value (total) && known_value (available_to_root))
530     {
531       used = total - available_to_root;
532       negate_used = (total < available_to_root);
533     }

綜上所述,本次案例的官方權威是原始碼。

相關內容