Linux中LVM如何格扩容硬盘

Posted by     "lxg" on Saturday, July 13, 2024

现在要求将新添加的硬盘/dev/sdb扩容到 / 目录。

第零步、磁盘基本信息检查

查看系统可用块设备以及依赖关系

root@dev:~# lsblk
NAME                      MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
loop0                       7:0    0  63.9M  1 loop /snap/core20/2182
loop1                       7:1    0 111.9M  1 loop /snap/lxd/24322
loop2                       7:2    0  63.9M  1 loop /snap/core20/2318
loop3                       7:3    0    87M  1 loop /snap/lxd/28373
loop4                       7:4    0  40.9M  1 loop /snap/snapd/20290
loop5                       7:5    0  38.8M  1 loop /snap/snapd/21759
sda                         8:0    0    20G  0 disk
├─sda1                      8:1    0     1M  0 part
├─sda2                      8:2    0   1.8G  0 part /boot
└─sda3                      8:3    0  18.2G  0 part
  └─ubuntu--vg-ubuntu--lv 253:0    0    10G  0 lvm  /
sdb                         8:16   0    50G  0 disk
sdc                         8:32   0   200G  0 disk
└─sdc1                      8:33   0   200G  0 part
  └─vg0-lv0               253:1    0   199G  0 lvm
sr0                        11:0    1  1024M  0 rom

查看当前物理卷

root@dev:~# pvs
  PV         VG        Fmt  Attr PSize    PFree
  /dev/sda3  ubuntu-vg lvm2 a--    18.22g    8.22g
  /dev/sdc1  vg0       lvm2 a--  <200.00g 1020.00m

查看系统初始硬盘

root@dev:~# df -hT
Filesystem                        Type     Size  Used Avail Use% Mounted on
tmpfs                             tmpfs    791M  1.7M  789M   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv ext4     9.8G  7.2G  2.2G  77% /
tmpfs                             tmpfs    3.9G     0  3.9G   0% /dev/shm
tmpfs                             tmpfs    5.0M     0  5.0M   0% /run/lock
/dev/sda2                         ext4     1.8G  271M  1.4G  17% /boot
overlay                           overlay  9.8G  7.2G  2.2G  77% /var/lib/docker/overlay2/0556338779e3e80141e0e83393d93199a82ebc168f8314da822f25dfa4e06e3f/merged
tmpfs                             tmpfs    791M  4.0K  791M   1% /run/user/1000

第一步、创建新分区

root@dev:~# fdisk /dev/sdb

Welcome to fdisk (util-linux 2.37.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x4b7825d4.

Command (m for help): n  //添加新分区
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p  // 回车,默认p,主分区
Partition number (1-4, default 1):  // 回车,默认1
First sector (2048-104857599, default 2048): // 回车,默认起始位置
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-104857599, default 104857599): // 回车,默认最后位置

Created a new partition 1 of type 'Linux' and of size 50 GiB.

Command (m for help): t //更改分区类型
Selected partition 1
Hex code or alias (type L to list all): L //列出分区类型

00 Empty            24 NEC DOS          81 Minix / old Lin  bf Solaris
01 FAT12            27 Hidden NTFS Win  82 Linux swap / So  c1 DRDOS/sec (FAT-
02 XENIX root       39 Plan 9           83 Linux            c4 DRDOS/sec (FAT-
03 XENIX usr        3c PartitionMagic   84 OS/2 hidden or   c6 DRDOS/sec (FAT-
04 FAT16 <32M       40 Venix 80286      85 Linux extended   c7 Syrinx
05 Extended         41 PPC PReP Boot    86 NTFS volume set  da Non-FS data
06 FAT16            42 SFS              87 NTFS volume set  db CP/M / CTOS / .
07 HPFS/NTFS/exFAT  4d QNX4.x           88 Linux plaintext  de Dell Utility
08 AIX              4e QNX4.x 2nd part  8e Linux LVM        df BootIt
09 AIX bootable     4f QNX4.x 3rd part  93 Amoeba           e1 DOS access
0a OS/2 Boot Manag  50 OnTrack DM       94 Amoeba BBT       e3 DOS R/O
0b W95 FAT32        51 OnTrack DM6 Aux  9f BSD/OS           e4 SpeedStor
0c W95 FAT32 (LBA)  52 CP/M             a0 IBM Thinkpad hi  ea Linux extended
0e W95 FAT16 (LBA)  53 OnTrack DM6 Aux  a5 FreeBSD          eb BeOS fs
0f W95 Ext'd (LBA)  54 OnTrackDM6       a6 OpenBSD          ee GPT
10 OPUS             55 EZ-Drive         a7 NeXTSTEP         ef EFI (FAT-12/16/
11 Hidden FAT12     56 Golden Bow       a8 Darwin UFS       f0 Linux/PA-RISC b
12 Compaq diagnost  5c Priam Edisk      a9 NetBSD           f1 SpeedStor
14 Hidden FAT16 <3  61 SpeedStor        ab Darwin boot      f4 SpeedStor
16 Hidden FAT16     63 GNU HURD or Sys  af HFS / HFS+       f2 DOS secondary
17 Hidden HPFS/NTF  64 Novell Netware   b7 BSDI fs          fb VMware VMFS
18 AST SmartSleep   65 Novell Netware   b8 BSDI swap        fc VMware VMKCORE
1b Hidden W95 FAT3  70 DiskSecure Mult  bb Boot Wizard hid  fd Linux raid auto
1c Hidden W95 FAT3  75 PC/IX            bc Acronis FAT32 L  fe LANstep
1e Hidden W95 FAT1  80 Old Minix        be Solaris boot     ff BBT

Aliases:
   linux          - 83
   swap           - 82
   extended       - 05
   uefi           - EF
   raid           - FD
   lvm            - 8E
   linuxex        - 85
Hex code or alias (type L to list all): 8e  //lvm分区
Changed type of partition 'Linux' to 'Linux LVM'.

Command (m for help): p //打印,Blocks为分区大小 单位为kb
Disk /dev/sdb: 50 GiB, 53687091200 bytes, 104857600 sectors
Disk model: VMware Virtual S
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: 0x4b7825d4

Device     Boot Start       End   Sectors Size Id Type
/dev/sdb1        2048 104857599 104855552  50G 8e Linux LVM

Command (m for help): w //保存
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

至此已经创建分区,通过lsblk查看已经存在sdb1分区

root@dev:~# lsblk
NAME                      MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
loop0                       7:0    0  63.9M  1 loop /snap/core20/2182
loop1                       7:1    0 111.9M  1 loop /snap/lxd/24322
loop2                       7:2    0  63.9M  1 loop /snap/core20/2318
loop3                       7:3    0    87M  1 loop /snap/lxd/28373
loop4                       7:4    0  40.9M  1 loop /snap/snapd/20290
loop5                       7:5    0  38.8M  1 loop /snap/snapd/21759
sda                         8:0    0    20G  0 disk
├─sda1                      8:1    0     1M  0 part
├─sda2                      8:2    0   1.8G  0 part /boot
└─sda3                      8:3    0  18.2G  0 part
  └─ubuntu--vg-ubuntu--lv 253:0    0    10G  0 lvm  /
sdb                         8:16   0    50G  0 disk
└─sdb1                      8:17   0    50G  0 part
sdc                         8:32   0   200G  0 disk
└─sdc1                      8:33   0   200G  0 part
  └─vg0-lv0               253:1    0   199G  0 lvm
sr0                        11:0    1  1024M  0 rom

第二步、将物理硬盘格式化成PV(物理卷)

root@dev:~# pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created.

第三步、将物理卷pv并到vg(卷组)

首先使用vgdisplay 命令查看卷组

root@dev:~# vgdisplay
  --- Volume group ---
  VG Name               vg0
  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               0
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <200.00 GiB
  PE Size               4.00 MiB
  Total PE              51199
  Alloc PE / Size       50944 / 199.00 GiB
  Free  PE / Size       255 / 1020.00 MiB
  VG UUID               rILfqs-9cHH-9q2s-7FAt-Oit2-1qF0-WKxnv0

  --- Volume group ---
  VG Name               ubuntu-vg
  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               18.22 GiB
  PE Size               4.00 MiB
  Total PE              4665
  Alloc PE / Size       2560 / 10.00 GiB
  Free  PE / Size       2105 / 8.22 GiB
  VG UUID               K05qLs-p5R7-rwER-nh3P-qZHD-DQpF-C8x3UB

ubuntu-vg 是已经存在的卷组,记住这个名字将pv并到ubuntu-vg

root@dev:~# vgextend ubuntu-vg /dev/sdb1
  Volume group "ubuntu-vg" successfully extended

再次查看vgdisplay 发现SIZE已经变化

root@dev:~# vgdisplay
  --- Volume group ---
  VG Name               vg0
  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               0
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <200.00 GiB
  PE Size               4.00 MiB
  Total PE              51199
  Alloc PE / Size       50944 / 199.00 GiB
  Free  PE / Size       255 / 1020.00 MiB
  VG UUID               rILfqs-9cHH-9q2s-7FAt-Oit2-1qF0-WKxnv0

  --- 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               <68.22 GiB
  PE Size               4.00 MiB
  Total PE              17464
  Alloc PE / Size       2560 / 10.00 GiB
  Free  PE / Size       14904 / <58.22 GiB
  VG UUID               K05qLs-p5R7-rwER-nh3P-qZHD-DQpF-C8x3UB

第四步、扩充逻辑卷

查看当前的逻辑卷

root@dev:~# lvdisplay
  --- Logical volume ---
  LV Path                /dev/vg0/lv0
  LV Name                lv0
  VG Name                vg0
  LV UUID                4xDsGC-zBZk-S7v9-DOEo-Zxe0-mVU2-lWeFMR
  LV Write Access        read/write
  LV Creation host, time dev, 2024-06-02 16:02:44 +0000
  LV Status              available
  # open                 0
  LV Size                199.00 GiB
  Current LE             50944
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1

  --- Logical volume ---
  LV Path                /dev/ubuntu-vg/ubuntu-lv
  LV Name                ubuntu-lv
  VG Name                ubuntu-vg
  LV UUID                1yy4d9-aSpX-pZ6A-PRvE-embQ-aHq1-D2TDSY
  LV Write Access        read/write
  LV Creation host, time ubuntu-server, 2023-02-04 08:21:52 +0000
  LV Status              available
  # open                 1
  LV Size                10.00 GiB
  Current LE             2560
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

逻辑卷/dev/ubuntu-vg/ubuntu-lv是我们要扩充的

root@dev:~# lvextend /dev/ubuntu-vg/ubuntu-lv /dev/sdb1
  Size of logical volume ubuntu-vg/ubuntu-lv changed from 10.00 GiB (2560 extents) to <60.00 GiB (15359 extents).
  Logical volume ubuntu-vg/ubuntu-lv successfully resized.

第五步、写入文件系统,使扩充生效

root@dev:~# resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv
resize2fs 1.46.5 (30-Dec-2021)
Filesystem at /dev/mapper/ubuntu--vg-ubuntu--lv is mounted on /; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 8
The filesystem on /dev/mapper/ubuntu--vg-ubuntu--lv is now 15727616 (4k) blocks long.

如果你使用的xfs文件系统,则需要使用xfs_growfs /dev/mapper/ubuntu--vg-ubuntu--lv命令

第六步、验证

root@dev:~# df -hT
Filesystem                        Type     Size  Used Avail Use% Mounted on
tmpfs                             tmpfs    791M  1.7M  789M   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv ext4      59G  7.2G   50G  13% /
tmpfs                             tmpfs    3.9G     0  3.9G   0% /dev/shm
tmpfs                             tmpfs    5.0M     0  5.0M   0% /run/lock
/dev/sda2                         ext4     1.8G  271M  1.4G  17% /boot
overlay                           overlay   59G  7.2G   50G  13% /var/lib/docker/overlay2/0556338779e3e80141e0e83393d93199a82ebc168f8314da822f25dfa4e06e3f/merged
tmpfs                             tmpfs    791M  4.0K  791M   1% /run/user/1000
root@dev:~#
root@dev:~# lsblk
NAME                      MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
loop0                       7:0    0  63.9M  1 loop /snap/core20/2182
loop1                       7:1    0 111.9M  1 loop /snap/lxd/24322
loop2                       7:2    0  63.9M  1 loop /snap/core20/2318
loop3                       7:3    0    87M  1 loop /snap/lxd/28373
loop4                       7:4    0  40.9M  1 loop /snap/snapd/20290
loop5                       7:5    0  38.8M  1 loop /snap/snapd/21759
sda                         8:0    0    20G  0 disk
├─sda1                      8:1    0     1M  0 part
├─sda2                      8:2    0   1.8G  0 part /boot
└─sda3                      8:3    0  18.2G  0 part
  └─ubuntu--vg-ubuntu--lv 253:0    0    60G  0 lvm  /
sdb                         8:16   0    50G  0 disk
└─sdb1                      8:17   0    50G  0 part
  └─ubuntu--vg-ubuntu--lv 253:0    0    60G  0 lvm  /
sdc                         8:32   0   200G  0 disk
└─sdc1                      8:33   0   200G  0 part
  └─vg0-lv0               253:1    0   199G  0 lvm
sr0                        11:0    1  1024M  0 rom

再来复习下 lvm 中的关系

「真诚赞赏,手留余香」

Little Star Blog

真诚赞赏,手留余香

使用微信扫描二维码完成支付