使用新硬盘给Linux文件系统扩容(LVM全流程操作)

0 背景

前两天刚装好的服务器(Ubuntu Server 20.04 LTS)突然就炸了,vscode连不上,只能命令行ssh,进去就提示空间不足。

$ df -h
Filesystem                         Size  Used Avail Use% Mounted on
...
/dev/mapper/ubuntu--vg-ubuntu--lv   98G   76G   18G  81% /
...

仔细一看,好家伙,我两块3.5T的硬盘,根目录实际就分了100G不到(装系统的时候一直下一步下一步...也没注意
无语,只好重新分配空间了

首先搜一下这个问题,发现这篇博客里说的跟我的情况很像。于是学习一下Linux LVM。

根据man lvm其它资料,可以知道LVM是涵盖Linux物理设备到文件系统之间的管理机制,自底向上抽象出了PV, VG, LV, FS层。根据图中的LVM层级关系,我们开始接下来的扩容。

Physical Volume (PV): physical hard disk, disk partition or software RAID partition formatted as LVM PV.
Volume Group (VG): is made from one or more physical volumes. A VG can can be extended by adding more PVs. A VG is like a virtual disk drive, from which one or more logical volumes are carved.
Logical Volume (LV): is similar to a partition in a non-LVM system. A LV is formatted with the desired file system (EXT3, XFS, JFS, etc), it is then available for mounting and data storage.

1 问题明确

首先明确目标:将一块新的硬盘和一块只用了100G的硬盘空间,扩容给/dev/mapper/ubuntu--vg-ubuntu--lv

使用fdisk和lsblk查看当前设备和分区的信息:

$ sudo lsblk
NAME                      MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
nvme1n1                   259:0    0   3.5T  0 disk 
nvme0n1                   259:1    0   3.5T  0 disk 
├─nvme0n1p1               259:2    0   1.1G  0 part /boot/efi
├─nvme0n1p2               259:3    0   1.5G  0 part /boot
└─nvme0n1p3               259:4    0   3.5T  0 part 
  └─ubuntu--vg-ubuntu--lv 253:0    0   100G  0 lvm  /

$ sudo fdisk -l | grep nvme
Disk /dev/nvme1n1: 3.5 TiB, 3840755982336 bytes, 7501476528 sectors
Disk /dev/nvme0n1: 3.5 TiB, 3840755982336 bytes, 7501476528 sectors
/dev/nvme0n1p1    2048    2203647    2201600  1.1G EFI System
/dev/nvme0n1p2 2203648    5349375    3145728  1.5G Linux filesystem
/dev/nvme0n1p3 5349376 7501473791 7496124416  3.5T Linux filesystem

由于我事先知道服务器上只有固态盘,插的nvme接口,不同于传统SATA接口设备会显示在/dev/sd*(sd代表SCSI disk,The device is usually /dev/sda, /dev/sdb or so. A device name refers to the entire disk. Old systems without libata (a library used inside the Linux kernel to support ATA host controllers and devices) make a difference between IDE and SCSI disks. In such cases the device name will be /dev/hd* (IDE) or /dev/sd* (SCSI). 可以在/proc/scsi/scsi查看scsi设备)
从命令输出可以看到我们有两块nvme的固态盘: nvme0和nvme1,大小都是3.5TB,nvme0有三个分区,p1 p2和p3,nvme1看起来没有分区。
至于nvme0n1p3各个数字的含义,0代表固态盘插在controller 0上(nvme controller?https://en.wikipedia.org/wiki/NVM_Express ),n1代表namespace1( https://nvmexpress.org/resources/nvm-express-technology-features/nvme-namespaces/ ),p3代表partition3

2 PV扩容

使用pvcreate创建一个新Physical Volume,以便LVM识别。pvcreate会将LVM disk label写入device,并初始化LVM metadata areas

$ sudo pvcreate /dev/nvme1n1
WARNING: gpt signature detected on /dev/nvme1n1 at offset XXX. Wipe it? [y/n]: y
  Wiping gpt signature on /dev/nvme1n1.
WARNING: PMBR signature detected on /dev/nvme1n1 at offset XXX. Wipe it? [y/n]: y
  Wiping PMBR signature on /dev/nvme1n1.
  Physical volume "/dev/nvme1n1" successfully created.

在无视掉WARNING的情况下,提示创建成功。
此时使用pvdisplay查看PV物理卷

$ sudo pvdisplay
  --- Physical volume ---
  PV Name               /dev/nvme0n1p3
  VG Name               ubuntu-vg
  PV Size               3.49 TiB / not usable 0   
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              915054
  Free PE               889454
  Allocated PE          25600
  PV UUID               VrE2Yr-ityA-Sy8z-fwMe-5Iup-M9qo-AXLDhA
   
  "/dev/nvme1n1" is a new physical volume of "3.49 TiB"
  --- NEW Physical volume ---
  PV Name               /dev/nvme1n1
  VG Name               
  PV Size               3.49 TiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               jfAhs6-zusn-Qrq0-ZicB-eMTD-e5mC-0Z7gsz

可以看到新的PV

3 VG扩容

使用vgextend,将新的PV加入VG

$ sudo vgextend ubuntu-vg /dev/nvme1n1
  Volume group "ubuntu-vg" successfully extended

此时使用vgdisplay查看VG信息

$ sudo vgdisplay
  --- Volume group ---
  VG Name               ubuntu-vg
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               6.98 TiB
  PE Size               4.00 MiB
  Total PE              1830761
  Alloc PE / Size       25600 / 100.00 GiB
  Free  PE / Size       1805161 / <6.89 TiB
  VG UUID               fOQred-Yai2-qWwL-fL1E-3uaN-ejrO-YUUDRc

可以看到之前没分完的3.5T连带新的3.5T空间,都体现在Free PE / Size项里,有6.89T的空间可用。

4 LV扩容

使用lvextend对LV进行扩容

$ sudo lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv
  Size of logical volume ubuntu-vg/ubuntu-lv changed from 100.00 GiB (25600 extents) to 6.98 TiB (1830761 extents).
  Logical volume ubuntu-vg/ubuntu-lv successfully resized.

使用lvdisplay查看LV信息,可以看到LV Size和VG Size已经相同,为6.98 T,VG的Free Size也降到了0。

$ sudo lvdisplay
  --- Logical volume ---
  LV Path                /dev/ubuntu-vg/ubuntu-lv
  LV Name                ubuntu-lv
  VG Name                ubuntu-vg
  LV UUID                xCuzUw-DIYE-bJDU-Gbp1-ibqO-K7cO-Kuftet
  LV Write Access        read/write
  LV Creation host, time ubuntu-server, 2022-05-18 09:14:14 +0000
  LV Status              available
  # open                 1
  LV Size                6.98 TiB
  Current LE             1830761
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

$ sudo vgdisplay
  --- Volume group ---
  VG Name               ubuntu-vg
  System ID             
  Format                lvm2
  Metadata Areas        2
  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                2
  Act PV                2
  VG Size               6.98 TiB
  PE Size               4.00 MiB
  Total PE              1830761
  Alloc PE / Size       1830761 / 6.98 TiB
  Free  PE / Size       0 / 0   
  VG UUID               fOQred-Yai2-qWwL-fL1E-3uaN-ejrO-YUUDRc

5 FS扩容

使用resize2fs对文件系统resize

$ sudo resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv
resize2fs 1.45.5 (07-Jan-2020)
Filesystem at /dev/mapper/ubuntu--vg-ubuntu--lv is mounted on /; on-line resizing required
old_desc_blocks = 13, new_desc_blocks = 894
The filesystem on /dev/mapper/ubuntu--vg-ubuntu--lv is now 1874699264 (4k) blocks long.

df查看,发现文件系统已经成功识别新的空间

$ df -h
Filesystem                         Size  Used Avail Use% Mounted on
...
/dev/mapper/ubuntu--vg-ubuntu--lv  6.9T   35G  6.6T   1% /
...

6 致谢及参考资料

至此扩容行动大功告成,感谢man手册,linux-training.be网站,askubuntu上的回答,特别是John W Kerns的博客,lvm的图示真的很直观。
http://linux-training.be/storage/ch12.html
https://askubuntu.com/questions/219881/how-can-i-create-one-logical-volume-over-two-disks-using-lvm
https://askubuntu.com/questions/7002/how-to-set-up-multiple-hard-drives-as-one-volume
https://packetpushers.net/ubuntu-extend-your-default-lvm-space/

posted @ 2022-05-25 21:33  HarryPotterIsDead!  阅读(1441)  评论(0编辑  收藏  举报