Files
Obsidian-Main/21.01. Linux/lvm.md

187 lines
5.3 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 lv1 vg1
```
`-l``--size` 同義。
### 建立 raid 1
使用 `--type raid1` 可以建立 raid 1 的 LVM
```
lvcreate --type raid1 -l +100%FREE --name lv1 vg1
```
## 查看 LV
使用 `lvdisplay` 和 `lvs` 來檢視你建立的 LV。
# 格式化 LV
建立好LV之後就可以格式化它然後掛載它先用`lvdisplay`確認一下 LV的路徑
![[20240228_170525_WindowsTerminal_901x169.png]]
格式化:
```
mkfs -t ext4 /dev/vg1/lv1
```
# Mount LV
先建立一個目錄來掛載 LV:
```
mkdir /lvm1
```
再把 LV 掛上去:
```
mount /dev/vg1/lv1 /lvm1
```
這樣就可以在 /myLVM 操作了。
## 開機自動掛載
![[開機自動掛載硬碟]]
# 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/vg1/lv1
```
# LVM 換電腦
可能你重灌,或是把硬碟從這一台電腦換到另一台電腦,這都需要重新 "active" 原本的 LVM。
先用 `lvscan` 看看是否有找到原本的 LVM
![[20240311_194022_WindowsTerminal_925x99.png]]
如果有的話,紅框位置就是 LVM 的路徑,之後只要啟用它就可以了:
```
sudo lvchange -a y /dev/vg1/lv1
```
之後再[[硬碟操作#掛載硬碟|mount]]它就好了。
# 參考
- [What is LVM2 in Linux ?. LVM](https://medium.com/@The_CodeConductor/what-is-lvm2-in-linux-3d28b479e250)
- [建立LVM磁區 - HackMD](https://hackmd.io/@yzai/BJUIhnAb9)
- [LVM — pv, vg, lv](https://sean22492249.medium.com/lvm-pv-vg-lv-1777a84a3ce8)
- [Linux LVM (建立、擴充、移除LVM磁區) 操作筆記](https://sc8log.blogspot.com/2017/03/linux-lvm-lvm.html)
- [LVM2學習筆記](https://maxubuntu.blogspot.com/2010/05/lvm2.html)
- [技术|如何在 Linux 中创建/配置 LVM逻辑卷管理](https://linux.cn/article-12670-1.html)
- [系统运维|如何在 Linux 中扩展/增加 LVM 大小(逻辑卷调整)](https://linux.cn/article-12673-1.html)
- [系统运维|如何在 Linux 中减少/缩小 LVM 大小(逻辑卷调整)](https://linux.cn/article-12740-1.html)
- [使用 lvm 內建的 raid 1 功能達成硬碟故障時的高可用性](https://gholk.github.io/linux-lvm-raid-1-builtin-boot.html)