2018/12/19

增大 Ubuntu 18.04 上 root lvm volume



昨天剛灌的機器今天磁碟就滿了, 怎麼可能?

一看, 原來當初給主磁碟 20G 空間 Ubuntu 18.04 只先配置 LV 4G 的空間, 難怪空間爆炸了

所以其實 PV 上空間還是夠的, 這時候就是把 LV 重新設定大小就好了

如果是用 lvextend 則會報錯:

```bash
$ sudo lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv
/etc/lvm/archive/.lvm_template_2310_1202205345: write error failed: No space left on device
```

正確步驟:

```bash
# 1. 用 lvresize
$ sudo lvresize -A n -l +100%FREE /dev/ubuntu-vg/ubuntu-lv
Size of logical volume ubuntu-vg/ubuntu-lv changed from 4.00 GiB (1024 extents) to <19.00 GiB (4863 extents)
WARNING: This metadata update is NOT backed up.
Logical volume ubuntu-vg/ubuntu-lv successfully resized.


# 2. resize ext4
$ sudo resize2fs /dev/ubuntu-vg/ubuntu-lv
resize2fs 1.44.1 (24-Mar-2018)
Filesystem at /dev/ubuntu-vg/ubuntu-lv is mounted on /; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 3
The filesystem on /dev/ubuntu-vg/ubuntu-lv is now 4979712 (4k) blocks long.

# 3. df 看一下
$ df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/ubuntu--vg-ubuntu--lv 19540624 4050844 14634724 22% /
```

2018/12/17

增大 EBS 磁碟容量後 LVM 要做的事情



前情提要:
1. 磁碟採用 NVME 介面(t3, m5, c5, r5)
2. 原先建置 LVM 時是用整顆磁碟, 所以裝置下只會有 /dev/nvme1n1 不會有 /dev/nvme1n1p1


增大前先看一下 lvm 配置

```sh
$ sudo pvdisplay
--- Physical volume ---
PV Name /dev/nvme1n1
VG Name VG1
PV Size 50.00 GiB / not usable 4.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 12799
Free PE 0
Allocated PE 12799

$ sudo lvdisplay
--- Logical volume ---
LV Path /dev/VG1/DATA
LV Name DATA
VG Name VG1
LV Write Access read/write
LV Creation host, time ip-172-31-37-10, 2018-10-08 09:05:34 +0000
LV Status available
# open 1
LV Size 50.00 GiB
Current LE 12799
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 252:0

$ sudo vgdisplay
--- Volume group ---
VG Name VG1
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 2
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 1
Max PV 0
Cur PV 1
Act PV 1
VG Size 50.00 GiB
PE Size 4.00 MiB
Total PE 12799
Alloc PE / Size 12799 / 50.00 GiB
Free PE / Size 0 / 0
```

在 ebs 頁面把該磁碟加大到100G後 直接跑 resize2fs 發現沒有做用

研究了一下發現需要先做 pvresize,做完後 pv 的確變成 100G 了

```sh
$ sudo pvresize /dev/nvme1n1
Physical volume "/dev/nvme1n1" changed
1 physical volume(s) resized / 0 physical volume(s) not resized

$ sudo pvdisplay
--- Physical volume ---
PV Name /dev/nvme1n1
VG Name VG1
PV Size 100.00 GiB / not usable 3.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 25599
Free PE 12800
Allocated PE 12799
```

看一下 lv,還是 50G 沒變

```sh
$ sudo lvdisplay
--- Logical volume ---
LV Path /dev/VG1/DATA
LV Name DATA
VG Name VG1
LV Write Access read/write
LV Creation host, time ip-172-31-37-10, 2018-10-08 09:05:34 +0000
LV Status available
# open 1
LV Size 50.00 GiB
Current LE 12799
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 252:0
```

lv 也要 resize一下,這邊把它稱大到所有的可用空間 +100%FREE

```sh
$ sudo lvresize -l +100%FREE /dev/mapper/VG1-DATA
Size of logical volume VG1/DATA changed from 50.00 GiB (12799 extents) to 100.00 GiB (25599 extents).
Logical volume DATA successfully resized.
```

lv resize 完後看一下,恩 變大了

```sh
$ sudo lvdisplay
--- Logical volume ---
LV Path /dev/VG1/DATA
LV Name DATA
VG Name VG1
LV Write Access read/write
LV Creation host, time ip-172-31-37-10, 2018-10-08 09:05:34 +0000
LV Status available
# open 1
LV Size 100.00 GiB
Current LE 25599
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 252:0

$ sudo vgdisplay
--- Volume group ---
VG Name VG1
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 4
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 1
Max PV 0
Cur PV 1
Act PV 1
VG Size 100.00 GiB
PE Size 4.00 MiB
Total PE 25599
Alloc PE / Size 25599 / 100.00 GiB
Free PE / Size 0 / 0
```

不過用 df 看 還是沒變大

```sh
$ df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/VG1-DATA 51470972 48766280 67076 100% /data
```

跑 resize2fs 後就正常了

```sh
$ sudo resize2fs /dev/mapper/VG1-DATA
resize2fs 1.42.13 (17-May-2015)
Filesystem at /dev/mapper/VG1-DATA is mounted on /data; on-line resizing required
old_desc_blocks = 4, new_desc_blocks = 7
The filesystem on /dev/mapper/VG1-DATA is now 26213376 (4k) blocks long.

$ df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/VG1-DATA 103077152 48774260 49568340 50% /data
```