Linux

LVM 會吃掉我的磁碟空間還是 df 撒謊?

  • March 16, 2016

請看下面的輸出:

bob ~ # df -h
Filesystem                 Size  Used Avail Use% Mounted on
udev                       5,7G  4,0K  5,7G   1% /dev
tmpfs                      1,2G  1,5M  1,2G   1% /run
/dev/mapper/mint--vg-root  218G   66G  142G  32% /
none                       4,0K     0  4,0K   0% /sys/fs/cgroup
tmpfs                      5,7G  528M  5,2G  10% /tmp
none                       5,0M     0  5,0M   0% /run/lock
none                       5,7G   99M  5,6G   2% /run/shm
none                       100M   48K  100M   1% /run/user
tmpfs                      5,7G   44K  5,7G   1% /var/tmp
/dev/sda1                  236M  132M   93M  59% /boot

df報告 LVM 分區有 218G,而它必須是 250G,如果用 1024 重新計算則為 232G。那麼 14G 在哪裡?但即使是 218-66=152 而不是 142!那是 10 多千兆字節,也無處可去?

其他實用程序輸出:

bob ~ # pvs
 PV         VG      Fmt  Attr PSize   PFree
 /dev/sda5  mint-vg lvm2 a--  232,64g    0 

bob ~ # pvdisplay
 --- Physical volume ---
 PV Name               /dev/sda5
 VG Name               mint-vg
 PV Size               232,65 GiB / not usable 2,00 MiB
 Allocatable           yes (but full)
 PE Size               4,00 MiB
 Total PE              59557
 Free PE               0
 Allocated PE          59557
 PV UUID               3FA5KG-Dtp4-Kfyf-STAZ-K6Qe-ojkB-Tagr83

bob ~ # fdisk -l /dev/sda

Disk /dev/sda: 250.1 GB, 250059350016 bytes
255 heads, 63 sectors/track, 30401 cylinders, total 488397168 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00097b2a

  Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      499711      248832   83  Linux
/dev/sda2          501758   488396799   243947521    5  Extended
/dev/sda5          501760   488396799   243947520   8e  Linux LVM

# sfdisk -l -uM

Disk /dev/sda: 30401 cylinders, 255 heads, 63 sectors/track
Warning: extended partition does not start at a cylinder boundary.
DOS and Linux will interpret the contents differently.
Units = mebibytes of 1048576 bytes, blocks of 1024 bytes, counting from 0

  Device Boot Start   End    MiB    #blocks   Id  System
/dev/sda1   *     1    243    243     248832   83  Linux
/dev/sda2       244+ 238474  238231- 243947521    5  Extended
/dev/sda3         0      -      0          0    0  Empty
/dev/sda4         0      -      0          0    0  Empty
/dev/sda5       245  238474  238230  243947520   8e  Linux LVM

Disk /dev/mapper/mint--vg-root: 30369 cylinders, 255 heads, 63 sectors/track

sfdisk: ERROR: sector 0 does not have an msdos signature
/dev/mapper/mint--vg-root: unrecognized partition table type
No partitions found

Linux Mint 17.3

更新

# lvdisplay
 --- Logical volume ---
 LV Path                /dev/mint-vg/root
 LV Name                root
 VG Name                mint-vg
 LV UUID                ew9fDY-oykM-Nekj-icXn-FQ1T-fiaC-0Jw2v6
 LV Write Access        read/write
 LV Creation host, time mint, 2016-02-18 14:52:15 +0200
 LV Status              available
 # open                 1
 LV Size                232,64 GiB
 Current LE             59557
 Segments               1
 Allocation             inherit
 Read ahead sectors     auto
 - currently set to     256
 Block device           252:0

關於交換。最初它在 LVM 中。然後我刪除它並使用交換使用的空間擴展根分區(大約 12G)

更新2

# tune2fs -l /dev/mapper/mint--vg-root
tune2fs 1.42.9 (4-Feb-2014)
Filesystem volume name:   <none>
Last mounted on:          /
Filesystem UUID:          0b5ecf9b-a763-4371-b4e7-01c36c47b5cc
Filesystem magic number:  0xEF53
Filesystem revision #:    1 (dynamic)
Filesystem features:      has_journal ext_attr resize_inode dir_index filetype needs_recovery extent flex_bg sparse_super large_file huge_file uninit_bg dir_nlink extra_isize
Filesystem flags:         signed_directory_hash 
Default mount options:    user_xattr acl
Filesystem state:         clean
Errors behavior:          Continue
Filesystem OS type:       Linux
Inode count:              14491648
Block count:              57952256
Reserved block count:     2897612
Free blocks:              40041861
Free inodes:              13997980
First block:              0
Block size:               4096
Fragment size:            4096
Reserved GDT blocks:      1010
Blocks per group:         32768
Fragments per group:      32768
Inodes per group:         8192
Inode blocks per group:   512
Flex block group size:    16
Filesystem created:       Thu Feb 18 14:52:49 2016
Last mount time:          Sun Mar 13 16:49:48 2016
Last write time:          Sun Mar 13 16:49:48 2016
Mount count:              22
Maximum mount count:      -1
Last checked:             Thu Feb 18 14:52:49 2016
Check interval:           0 (<none>)
Lifetime writes:          774 GB
Reserved blocks uid:      0 (user root)
Reserved blocks gid:      0 (group root)
First inode:              11
Inode size:           256
Required extra isize:     28
Desired extra isize:      28
Journal inode:            8
First orphan inode:       6160636
Default directory hash:   half_md4
Directory Hash Seed:      51743315-0555-474b-8a5a-bbf470e3ca9f
Journal backup:           inode blocks

更新3(最終)

多虧了喬納斯,找到了空間損失

# df -h
Filesystem                 Size  Used Avail Use% Mounted on
/dev/mapper/mint--vg-root  218G   65G  142G  32% /


# resize2fs /dev/mapper/mint--vg-root
resize2fs 1.42.9 (4-Feb-2014)
Filesystem at /dev/mapper/mint--vg-root is mounted on /; on-line resizing required
old_desc_blocks = 14, new_desc_blocks = 15
The filesystem on /dev/mapper/mint--vg-root is now 60986368 blocks long.

# df -h
Filesystem                 Size  Used Avail Use% Mounted on
/dev/mapper/mint--vg-root  229G   65G  153G  30% /

這是在 resize2fs 執行之前和之後的 tune2fs 命令輸出的差異

# diff /tmp/tune2fs_before_resize2fs /tmp/tune2fs2_after_resize2fs
13,17c13,17
< Inode count:              14491648
< Block count:              57952256
< Reserved block count:     2897612
< Free blocks:              40041861
< Free inodes:              13997980
---
> Inode count:              15253504
> Block count:              60986368
> Reserved block count:     3018400
> Free blocks:              43028171
> Free inodes:              14759836
21c21
< Reserved GDT blocks:      1010
---
> Reserved GDT blocks:      1009
38c38
< Inode size:           256
---
> Inode size:             256
42c42
< First orphan inode:       6160636
---
> First orphan inode:       5904187

讓我們做一些研究。我之前註意到了這種差異,但從未詳細檢查過將損失歸因於什麼。看看我的比較方案: fdisk 顯示以下分區:

/dev/sda3       35657728 1000214527 964556800  460G 83 Linux

由於我的文件系統位於 luks 容器中,因此會有一些損失,但這應該只有幾個 MiB。df 顯示:

Filesystem      Size  Used Avail Use% Mounted on
/dev/dm-1       453G  373G   58G  87% /

(luks 容器也是 /dev/sda3 不匹配 /dev/dm-1 的原因,但它們確實是同一個設備,中間有加密,沒有 LVM。這也表明 LVM 不對你的損失負責,我有他們也是。)

現在讓我們就此事詢問文件系統本身。呼叫tune2fs -l,它輸出了很多關於 ext-family 文件系統的有趣資訊,我們得到:

root@altair ~ › tune2fs -l /dev/dm-1
tune2fs 1.42.12 (29-Aug-2014)
Filesystem volume name:   <none>
Last mounted on:          /
Filesystem UUID:          0de04278-5eb0-44b1-9258-e4d7cd978768
Filesystem magic number:  0xEF53
Filesystem revision #:    1 (dynamic)
Filesystem features:      has_journal ext_attr resize_inode dir_index filetype needs_recovery extent flex_bg sparse_super large_file huge_file uninit_bg dir_nlink extra_isize
Filesystem flags:         signed_directory_hash 
Default mount options:    user_xattr acl
Filesystem state:         clean
Errors behavior:          Continue
Filesystem OS type:       Linux
Inode count:              30146560
Block count:              120569088
Reserved block count:     6028454
Free blocks:              23349192
Free inodes:              28532579
First block:              0
Block size:               4096
Fragment size:            4096
Reserved GDT blocks:      995
Blocks per group:         32768
Fragments per group:      32768
Inodes per group:         8192
Inode blocks per group:   512
Flex block group size:    16
Filesystem created:       Wed Oct 14 09:27:52 2015
Last mount time:          Sun Mar 13 12:25:50 2016
Last write time:          Sun Mar 13 12:25:48 2016
Mount count:              23
Maximum mount count:      -1
Last checked:             Wed Oct 14 09:27:52 2015
Check interval:           0 (<none>)
Lifetime writes:          1426 GB
Reserved blocks uid:      0 (user root)
Reserved blocks gid:      0 (group root)
First inode:              11
Inode size:           256
Required extra isize:     28
Desired extra isize:      28
Journal inode:            8
First orphan inode:       26747912
Default directory hash:   half_md4
Directory Hash Seed:      4723240b-9056-4f5f-8de2-d8536e35d183
Journal backup:           inode blocks

掃了一眼,首先映入眼簾的應該是Reserved blocks。將其與(也來自輸出)相乘Block size,我們得到 df Used+Avail 和 Size 之間的差異:

453GiB - (373GiB+58GiB) = 22 GiB
6028454*4096 Bytes = 24692547584 Bytes ~= 23 GiB

足夠接近,特別是考慮到 df 輪次(使用不帶 -h 的 df 並重複計算僅留下 16 MiB 的 Used+Avail 和 Size 之間的差異無法解釋)。保留塊被保留給誰也被寫入 tune2fs 輸出。它是根。這是一個安全網,可確保非 root 使用者無法通過填充磁碟使系統完全無法使用,並且保留百分之幾的磁碟空間未使用也有助於防止碎片。

現在來看 df 報告的大小與分區大小之間的差異。這可以通過查看 inode 來解釋。ext4 預先分配 inode,因此空間不能用於文件數據。乘以Inode countInode size得到:

30146560*256 Bytes = 7717519360 Bytes ~= 7 GiB
453 GiB + 7 GiB = 460 GiB

索引節點基本上是目錄條目。讓我們向 mkfs.ext4 詢問細節(來自 man mkfs.ext4):

-一世bytes-per-inode

指定字節/inode 比率。bytes-per-inode mke2fs 為磁碟上的每個空間字節創建一個 inode 。比率越大 bytes-per-inode ,創建的 inode 就越少。該值通常不應小於文件系統的塊大小,因為在這種情況下,將產生比以往更多的 inode。請注意,文件系統創建後無法更改此比率,因此請謹慎確定此參數的正確值。請注意,調整文件系統的大小會更改 inode 的數量以保持此比率。

有不同的預設可用於不同的場景。在具有大量 linux 分發映像的文件伺服器上,傳遞 eg-T largefile甚至-T largefile4. 在這些範例和我的系統中-T定義了什麼意思:/etc/mke2fs.conf

largefile = {
   inode_ratio = 1048576
}
largefile4 = {
   inode_ratio = 4194304
}

所以有了-T largefile4,數量比預設的要少很多(我的預設比例是16384 /etc/mke2fs.conf)。這意味著為目錄條目保留的空間更少,而為數據保留更多空間。當您用完 inode 時,您將無法創建新文件。增加現有文件系統中的 inode 數量似乎是不可能的。因此,預設的 inode 數量是相當保守地選擇的,以確保普通使用者不會過早地用完 inode。

我只是在戳我的數字時發現了這一點,如果它(不)對你有用,請告訴我☺。

引用自:https://unix.stackexchange.com/questions/270075