目录

Lvm管理

Lvm管理磁盘空间

逻辑卷管理LVM(Logical Volume Manager)是Linux系统的一种管理硬盘分区机制,具有动态管理硬盘的能力。本文介绍了如何通过LVM在多块云盘上创建一个逻辑卷,适用于Linux实例。

通过LVM创建逻辑卷

创建物理卷

查看磁盘信息

1
2
3
4
5
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
vda    253:0    0  100G  0 disk
└─vda1 253:1    0  100G  0 part /
vdb    253:16   0  200G  0 disk
vdc    253:32   0  200G  0 disk

安装lvm2

1
yum install -y lvm2

分区

  • mbr分区

    1、查看磁盘

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    
    fdisk -l
    
    Disk /dev/sdb: 2147 MB, 2147483648 bytes, 4194304 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/sda: 21.5 GB, 21474836480 bytes, 41943040 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 label type: dos
    Disk identifier: 0x000cd8ec
    
    Device Boot      Start         End      Blocks   Id  System
    /dev/sda1   *        2048    41943039    20970496   83  Linux
    
    Disk /dev/sdc: 536.9 GB, 536870912000 bytes, 1048576000 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
    

    2、分区

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    
    fdisk /dev/vdb
    
    Welcome to fdisk (util-linux 2.23.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
    Building a new DOS disklabel with disk identifier 0xe80f8142.
    
    Command (m for help): n
    Partition type:
      p   primary (0 primary, 0 extended, 4 free)
      e   extended
    Select (default p): p
    Partition number (1-4, default 1): 1
    First sector (2048-1048575999, default 2048): 
    Using default value 2048
    Last sector, +sectors or +size{K,M,G} (2048-1048575999, default 1048575999): 
    Using default value 1048575999
    Partition 1 of type Linux and of size 500 GiB is set
    
    Command (m for help): t
    Selected partition 1
    Hex code (type L to list all codes): 8e
    Changed type of partition 'Linux' to 'Linux LVM'
    
    Command (m for help): w
    The partition table has been altered!
    
    Calling ioctl() to re-read partition table.
    Syncing disks.
    
  • gpt分区

    1、分区

    1
    2
    3
    4
    5
    6
    7
    
    parted /dev/sdb
    mklabel gpt
    unit TB # 设置单位为TB
    mkpart primary 0 3  设置3TB的分区
    mkpart primary 0 1396MB 
    mkpart primary 0 -1
    toggle 1 lvm  # 打上lvm标签
    

创建物理卷pv

1
2
3
4
5
6
7
8
# 新建物理卷
pvcreate /dev/vdb /dev/vdc
  Physical volume "/dev/vdb" successfully created.
  Physical volume "/dev/vdc" successfully created.
# 扩容物理卷
pvresize /dev/vdb
Physical volume "/dev/vdb" changed
  1 physical volume(s) resized or updated / 0 physical volume(s) not resized

查看pv信息

1
2
3
4
5
6
7
8
lvmdiskscan
  /dev/vda1 [    <100.00 GiB]
  /dev/vdb  [     200.00 GiB] LVM physical volume
  /dev/vdc  [     200.00 GiB] LVM physical volume
  0 disks
  1 partition
  2 LVM physical volume whole disks
  0 LVM physical volumes

创建卷组

创建卷组VG

1
2
vgcreate mongo_rs /dev/vdb /dev/vdc
  Volume group "mongo_rs" successfully created

查看卷组信息

1
2
3
vgs
VG       #PV #LV #SN Attr   VSize   VFree
  mongo_rs   2   0   0 wz--n- 399.99g 399.99g

创建逻辑卷

创建逻辑卷LV

1
2
3
4
5
6
7
8
# 创建lv
lvcreate -L 350g -n mongo_rs_lv mongo_rs
Logical volume "mongo_rs_lv" created
# 扩容lv
lvextend -L +21.99G /dev/lvm_test/lvm_test_lv
  Rounding size to boundary between physical extents: 21.99 GiB.
  Size of logical volume lvm_test/lvm_test_lv changed from 18.00 GiB (4608 extents) to 39.99 GiB (10238 extents).
  Logical volume lvm_test/lvm_test_lv successfully resized. 

查看逻辑卷

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
lvdisplay

  --- Logical volume ---
  LV Path                /dev/mongo_rs/mongo_rs_lv
  LV Name                mongo_rs_lv
  VG Name                mongo_rs
  LV UUID                4VLpZ3-YTkN-cq65-nE03-O4GB-W4wK-eYLq0N
  LV Write Access        read/write
  LV Creation host, time mongdb-01, 2022-01-20 14:57:28 +0800
  LV Status              available
  # open                 0
  LV Size                350.00 GiB
  Current LE             89600
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           252:0

创建并挂载文件系统

创建ext4文件系统

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 格式化文件系统
mkfs.ext4 /dev/mongo_rs/mongo_rs_lv

mke2fs 1.42.9 (28-Dec-2013)
文件系统标签=
OS type: Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
22937600 inodes, 91750400 blocks
4587520 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=2239758336
2800 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
	32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
	4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968

Allocating group tables: 完成
正在写入inode表: 完成
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

# 更新文件系统
## ext4
resize2fs /dev/lvm_test/lvm_test_lv
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/lvm_test/lvm_test_lv is mounted on /data; on-line resizing required
old_desc_blocks = 3, new_desc_blocks = 5
The filesystem on /dev/lvm_test/lvm_test_lv is now 10483712 blocks long.
## xfs
xfs_growfs /dev/lvm_test/lvm_test_lv

创建挂载点

1
mkdir /data

挂载文件系统

1
mount /dev/mongo_rs/mongo_rs_lv /data

查看逻辑卷挂载信息

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
df -TPh

文件系统                         类型      容量  已用  可用 已用% 挂载点
/dev/vda1                        ext4       99G  1.9G   93G    2% /
devtmpfs                         devtmpfs  7.5G     0  7.5G    0% /dev
tmpfs                            tmpfs     7.6G     0  7.6G    0% /dev/shm
tmpfs                            tmpfs     7.6G  544K  7.6G    1% /run
tmpfs                            tmpfs     7.6G     0  7.6G    0% /sys/fs/cgroup
tmpfs                            tmpfs     1.6G     0  1.6G    0% /run/user/0
/dev/mapper/mongo_rs-mongo_rs_lv ext4      345G   69M  327G    1% /data

添加开机启动

  • 查看uuid
1
2
3
4
5
blkid
/dev/vda1: UUID="1114fe9e-2309-4580-b183-d778e6d97397" TYPE="ext4"
/dev/vdb: UUID="ev6YG0-PurR-p4hP-1mNM-ZDpr-36eE-zndFzA" TYPE="LVM2_member"
/dev/mapper/mongo_rs-mongo_rs_lv: UUID="0d567c75-6fbc-41e0-98ef-df78bcd74433" TYPE="ext4"
/dev/vdc: UUID="UjWQY7-oSHb-pcmS-wc39-M9uI-Zbqd-YEITMf" TYPE="LVM2_member"
  • 添加到开机启动
1
2
vim /etc/fstab
UUID=0d567c75-6fbc-41e0-98ef-df78bcd74433 /data ext4 defaults 0 0

通过LVM管理逻辑卷

LVM扩容

  • 扩容pv卷

    扩容pv

    1
    
    pvresize /dev/vdb
    

    扩容逻辑卷

    1
    
    lvextend -L +50G /dev/mongo_rs/mongo_rs_lv
    

    扩容文件系统

    • ext4文件系统
    1
    
    resize2fs /dev/mongo_rs/mongo_rs_lv 
    
    • xfs文件系统
    1
    
    xfs_growfs /dev/mongo_rs/mongo_rs_lv 
    

    查看文件系统

    1
    
    df -TPH
    
  • 新增pv卷 增加pv

1
pvcreate /dev/vdf

扩展vg

1
vgextend  mongo_rs /dev/vdf

扩容逻辑卷

1
lvextend -L +50G /dev/mongo_rs/mongo_rs_lv

扩容文件系统

  • ext4文件系统
1
resize2fs /dev/mongo_rs/mongo_rs_lv 
  • xfs文件系统
1
xfs_growfs /dev/mongo_rs/mongo_rs_lv 

查看文件系统

1
df -TPH

LVM缩容

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# 查看挂载
mount
# 卸载lv
umount /dev/centos-home
e2fsck -f /dev/centos-home
# 缩减逻辑边界
resize2fs /dev/centos-home  5G
# 缩减物理边界
lvchange  -a n /dev/centos-home
lvreduce -L 5G /dev/centos-home
lvchange  -a y /dev/centos-home
e2fsck  -f /dev/centos-home
# 挂载
mount /dev/centos-home /home