Hetzner 使用 LVM 合并硬盘空间

date
Nov 25, 2021
slug
hetzner-使用-lvm-合并硬盘空间
status
Published
tags
兴趣
summary
由于原始硬盘容量无法满足业务需求,遂额外加购硬盘。起初使用的是 RAID0 方案合并硬盘空间,但是由于 RAID0 的特性限制,最终可用容量为最小硬盘容量乘以总硬盘数。如果硬盘大小不同则不可避免有大量的容量浪费。且 RAID0 本身有高风险,如果一枚硬盘物理损坏则全部数据丢失,无法找回。
type
Post
URL

背景

由于原始硬盘容量无法满足业务需求,遂额外加购硬盘。起初使用的是 RAID0 方案合并硬盘空间,但是由于 RAID0 的特性限制,最终可用容量为最小硬盘容量乘以总硬盘数。如果硬盘大小不同则不可避免有大量的容量浪费。且 RAID0 本身有高风险,如果一枚硬盘物理损坏则全部数据丢失,无法找回。
<!--more-->

开干

前往 Rescue System, 删除 RAID 并格式化盘符
mdadm --remove /dev/md0
mdadm --remove /dev/md1
mdadm --remove /dev/md2

mdadm --stop /dev/md0
mdadm --stop /dev/md1
mdadm --stop /dev/md2

mkfs.ext4 /dev/nvme0n1
mkfs.ext4 /dev/nvme1n1
mkfs.ext4 /dev/nvme2n1
通过 installimage 安装操作系统。在安装配置文件中修改和添加分区信息
...
SWRAID 0 ## 关闭 RAID
...

## 编写分区
PART  /boot  ext3  512M
PART  lvm    vg0   all

LV   vg0   swap   swap  swap  32G
LV   vg0   root   /     ext4  all
进入正常系统后,列出本机中的物理硬盘,记录硬盘名
fdisk -l

Disk /dev/nvme0n1: 1.86 TiB, 2048408248320 bytes, 4000797360 sectors
Disk model: SAMSUNG MZVL22T0HBLB-00B00
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xacf2949f

Device         Boot   Start        End    Sectors  Size Id Type
/dev/nvme0n1p1         2048    1050623    1048576  512M 83 Linux
/dev/nvme0n1p2      1050624 4000795311 3999744688  1.9T 8e Linux LVM

Disk /dev/nvme1n1: 476.94 GiB, 512110190592 bytes, 1000215216 sectors
Disk model: SAMSUNG MZVLB512HBJQ-00000
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 /dev/nvme2n1: 476.94 GiB, 512110190592 bytes, 1000215216 sectors
Disk model: SAMSUNG MZVLB512HBJQ-00000
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 /dev/mapper/vg0-swap: 32 GiB, 34359738368 bytes, 67108864 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 /dev/mapper/vg0-root: 1.83 TiB, 2013504995328 bytes, 3932626944 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
添加其他物理卷至 LVM
pvcreate /dev/nvme1n1 /dev/nvme2n1
将其他物理卷扩展至卷组 vg0
vgextend vg0 /dev/nvme1n1 /dev/nvme2n1
将剩余空间添加至逻辑卷并整理
lvextend -l +100%FREE /dev/mapper/vg0-root
resize2fs /dev/mapper/vg0-root
查询可用空间
df -hl

Filesystem            Size  Used Avail Use% Mounted on
tmpfs                 6.3G  940K  6.3G   1% /run
/dev/mapper/vg0-root  2.8T  1.9G  2.6T   1% /
tmpfs                  32G     0   32G   0% /dev/shm
tmpfs                 5.0M     0  5.0M   0% /run/lock
/dev/nvme0n1p1        488M   89M  374M  20% /boot
tmpfs                 6.3G     0  6.3G   0% /run/user/0

fdisk -l

Disk /dev/nvme0n1: 1.86 TiB, 2048408248320 bytes, 4000797360 sectors
Disk model: SAMSUNG MZVL22T0HBLB-00B00
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xacf2949f

Device         Boot   Start        End    Sectors  Size Id Type
/dev/nvme0n1p1         2048    1050623    1048576  512M 83 Linux
/dev/nvme0n1p2      1050624 4000795311 3999744688  1.9T 8e Linux LVM

Disk /dev/nvme1n1: 476.94 GiB, 512110190592 bytes, 1000215216 sectors
Disk model: SAMSUNG MZVLB512HBJQ-00000
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 /dev/nvme2n1: 476.94 GiB, 512110190592 bytes, 1000215216 sectors
Disk model: SAMSUNG MZVLB512HBJQ-00000
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 /dev/mapper/vg0-swap: 32 GiB, 34359738368 bytes, 67108864 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 /dev/mapper/vg0-root: 2.76 TiB, 3037720477696 bytes, 5933047808 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
Bingo! 🎉

最后吐槽一下,中文互联网信息质量是真的烂。在中文互联网搜索技术资料,基本除了漫天飞舞的垃圾广告,剩下就是你抄我我抄你的垃圾社区“博客”和采集站,真正有意义的内容少之又少。这是我第一次处理 LVM 的问题,折腾花了不少时间,最终还是在某个英文网站上找到了参考资料;当然也有可能是我的中文搜索水平太烂。总之,务必学好英文,远离垃圾中文互联网。
notion image

© Meow 2020 - 2022