Files
Obsidian-Main/05. 資料收集/Linux/lvm.md

172 lines
5.0 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# PV(Physical Volume)
PV(Physical Volume) 就是LVM的磁碟分區。
## 建立PV
建立PV的命令如下
```bash
pvcreate /dev/sdb /dev/sdc /dev/sdd
```
```result
`Physical volume "/dev/sdb" successfully created`
`Physical volume "/dev/sdc" successfully created`
`Physical volume "/dev/sdd" successfully created`
```
這樣我們就建立了3個PV。
## 查看 PV
使用 `pvdisplay` 和 `pvs` 來檢視你建立的 PV。
`pvs` 輸出會比較簡短,例:
```
sudo pvs
```
```result
PV VG Fmt Attr PSize PFree
/dev/sda3 ubuntu-vg lvm2 a-- 57.62g 28.81g
/dev/sdb1 vg1 lvm2 a-- <2.73t 0
/dev/sdc1 vg1 lvm2 a-- <2.73t 0
```
 `pvdisplay` 就比較詳細:
```
sudo pvdisplay 
```
```result
--- Physical volume ---
PV Name /dev/sdb1
VG Name vg1
PV Size <2.73 TiB / not usable 3.44 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 715396
Free PE 0
Allocated PE 715396
PV UUID cWvfBE-Vbyp-l09E-QH0O-ZZoC-AdSG-t1J7TT
--- Physical volume ---
PV Name /dev/sdc1
VG Name vg1
PV Size <2.73 TiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 715396
Free PE 0
Allocated PE 715396
PV UUID eDdYr4-HSZC-wRBa-feGx-SHp1-Wfye-m0e1PY
--- Physical volume ---
PV Name /dev/sda3
VG Name ubuntu-vg
PV Size 57.62 GiB / not usable 2.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 14751
Free PE 7376
Allocated PE 7375
PV UUID zUlIaB-1Uof-3eF6-z5I7-OnqJ-UYDk-gThxJQ
```
# VG(Volume Group)
VG(Volume Group) 由 PV 組成你可以自由的排列組合。我們拿剛剛建立的3個PV把它組成一個VG。
## 建立 VG
```
vgcreate vg1 /dev/sdb /dev/sdc /dev/sdd
```
`vg1` 是你的VG 名字,可以自由更改。
後面的`/dev/sdb``/dev/sdc``/dev/sdd` 就是你剛剛建立的 PV。
## 查看 VG
使用 `vgdisplay` 和 `vgs` 來檢視你建立的 VG。
# LV(Logical Volume)
LV(Logical Volume) 可以對應到實際的硬碟,它才是可以被 mount 到 directory 的東西。LV 可以只使用 VG 某一部份的也就是說一個VG可以切出很多LV。
## 建立 LV
```
lvcreate --size 10G --name lv1 vg1
```
`--size 10G` 表示 LV 的空間是 10G
`--name lv1` 表示 LV的名字叫做 `lv1`
最後的 `vg1` 則是你要從那一個 VG 來建立這個 LV。
當然可以直接指定LV使用所有的空間
```
lvcreate -l +100%FREE --name lvm1 vg1
```
`-l``--size` 同義。
## 查看 LV
使用 `lvdisplay` 和 `lvs` 來檢視你建立的 LV。
# 格式化 LV
建立好LV之後就可以格式化它然後掛載它先用`lvdisplay`確認一下 LV的路徑
![[20240228_170525_WindowsTerminal_901x169.png]]
格式化:
```
mkfs -t ext4 /dev/vg1/lv1
```
# Mount LV
先建立一個目錄來掛載 LV:
```
mnkdir /myLVM
```
再把 LV 掛上去:
```
mount /dev/vg1/lv1 /myLVM
```
這樣就可以在 /myLVM 操作了。
## 開機自動掛載
要開機自動掛載的話需要知道你要掛載硬碟的UUID然後把它填到 `/etc/fstab` 就可以了。
`blkid` 來看 UUID:
![[20240228_170818_WindowsTerminal_1733x303.png]]
然後在 `/etc/fstab` 加入一行:
```
UUID=b4ccb44d-3ddf-458d-b247-a1a16be4f478 /lvm1 ext4 defaults,nofail 0 2
```
# LVM 增加空間
增加空間的大概步驟是這樣:
1. 電腦裝上新硬碟
2. 建立PV如[[lvm#建立PV]]
3.`vgextend` 把這個新的 PV 加入到既有的 VG
4.`lvextend` 來擴大容量
5.`resize2fs` 來擴大容量
## 用 `vgextend` 新增 PV
假設 vg1 是目前的 VG 名字,新增的 PV是 /dev/sdc
```
vgextend vg1 /dev/sdc
```
## 用 `lvextend` 來擴大容量
先用`lvdisplay`確認一下 LV的路徑假設是 `/dev/vg1/lv1`
```
lvextend -L +10G /dev/vg1/lv1 # 多 10G 空間
or
lvextend -l +40%FREE /dev/vg1/lv1 多 40% 空間
```
## 用 `resize2fs` 來擴大容量
```
resize2fs /dev/mapper/vg01-lv002
```
# 參考
- [What is LVM2 in Linux ?. LVM | by The_CodeConductor | Medium](https://medium.com/@The_CodeConductor/what-is-lvm2-in-linux-3d28b479e250)
- [建立LVM磁區 - HackMD](https://hackmd.io/@yzai/BJUIhnAb9)
- [LVM — pv, vg, lv. 动态的分配档案系统的空间,方便管理者随时调整空间,达到妥善使用硬件ㄒㄧㄠ | by Kiwi lee | Medium --- LVM — pv, vg, lv. 動態的分配檔案系統的空間,方便管理者隨時調整空間,達到妥善使用硬體ㄒㄧㄠ | by Kiwi lee | Medium](https://sean22492249.medium.com/lvm-pv-vg-lv-1777a84a3ce8)
- [天黑的時候,星星就會出現!: Linux LVM (建立、擴充、移除LVM磁區) 操作筆記](https://sc8log.blogspot.com/2017/03/linux-lvm-lvm.html)
- [Maxsolar's Linux Blog: LVM2學習筆記](https://maxubuntu.blogspot.com/2010/05/lvm2.html)