从零开始创建LVM分区方案

环境信息

  • 磁盘分区: /dev/nvme0n1p4
root@iStoreOS:~# lsblk 
NAME        MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
sda           8:0    0 238.5G  0 disk 
└─sda1        8:1    0 238.5G  0 part /mnt/usb2_2-1
nbd0         43:0    0     0B  0 disk 
nbd1         43:32   0     0B  0 disk 
nbd2         43:64   0     0B  0 disk 
nbd3         43:96   0     0B  0 disk 
nbd4         43:128  0     0B  0 disk 
nbd5         43:160  0     0B  0 disk 
nbd6         43:192  0     0B  0 disk 
nbd7         43:224  0     0B  0 disk 
nvme0n1     259:0    0 953.9G  0 disk 
├─nvme0n1p1 259:1    0   128M  0 part /boot
│                                     /boot
├─nvme0n1p2 259:2    0   256M  0 part /rom
├─nvme0n1p3 259:3    0     2G  0 part /overlay/upper/opt/docker
│                                     /overlay
└─nvme0n1p4 259:4    0 951.5G  0 part 

安装LVM2

root@iStoreOS:~# opkg install lvm2
Installing lvm2 (2.03.16-1) to root...
Downloading https://mirrors.tuna.tsinghua.edu.cn/openwrt/releases/22.03.5/packages/x86_64/packages/lvm2_2.03.16-1_x86_64.ipk
Installing libaio (0.3.112-2) to root...
Downloading https://mirrors.tuna.tsinghua.edu.cn/openwrt/releases/22.03.5/packages/x86_64/packages/libaio_0.3.112-2_x86_64.ipk
Configuring libaio.
Configuring lvm2.
File descriptor 3 (/tmp/lock/opkg.lock) leaked on lvm invocation. Parent PID 21055: /bin/sh
File descriptor 3 (/tmp/lock/opkg.lock) leaked on lvm invocation. Parent PID 21055: /bin/sh

磁盘分区

分区格式化

创建lvm分区,需要先对磁盘分区进行格式化操做

root@iStoreOS:~# mkfs.ext4 /dev/nvme0n1p4
mke2fs 1.46.5 (30-Dec-2021)
Found a gpt partition table in /dev/nvme0n1p4
Proceed anyway? (y,N) y
Discarding device blocks: done                            
Creating filesystem with 249428305 4k blocks and 62357504 inodes
Filesystem UUID: 8e4c37a3-4f78-4040-9e86-34baa2af5cb2
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
        4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968, 
        102400000, 214990848

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (262144 blocks): done
Writing superblocks and filesystem accounting information: done     

配置LVM

创建PV

pv是一个逻辑卷,相当于一个虚拟磁盘

root@iStoreOS:~# pvcreate lvm /dev/nvme0n1p4
  No device found for lvm.
  Physical volume "/dev/nvme0n1p4" successfully created.
root@iStoreOS:~# pvdisplay 
  "/dev/nvme0n1p4" is a new physical volume of "951.49 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/nvme0n1p4
  VG Name               
  PV Size               951.49 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               YM02sw-Nb7v-aE0y-fanC-YHJX-rEZd-ZPo8mv

/dev/nvme0n1p4 : 需要创建LVM分区的磁盘分区
lvm: 设置pv的名称

创建vg

VG 是由一个或多个物理卷(Physical Volumes,PV)组成的逻辑容器,用于存储和管理逻辑卷(Logical Volumes,LV),类似于云计算的资源池。

命令如下

vgcreate lvg /dev/nvme0n1p4
  • lvg:设置vg的名称
  • /dev/nvme0n1p4:需要添加的磁盘分区,该参数可以添加多个
root@iStoreOS:~# vgcreate lvg /dev/nvme0n1p4
  Volume group "lvg" successfully created
root@iStoreOS:~# vgdisplay 
  --- Volume group ---
  VG Name               lvg
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  1
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               951.49 GiB
  PE Size               4.00 MiB
  Total PE              243582
  Alloc PE / Size       0 / 0   
  Free  PE / Size       243582 / 951.49 GiB
  VG UUID               QYKacr-LDgd-clCw-0l3K-qHzo-nQkD-8jC2jz

创建LV

LV就是虚拟分区

创建第一个lv分区

root@iStoreOS:~# lvcreate -n wxy -L 780G lvg
  Logical volume "wxy" created.
root@iStoreOS:~# lsblk 
NAME        MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
nvme0n1     259:0    0 953.9G  0 disk 
├─nvme0n1p1 259:1    0   128M  0 part /boot
│                                     /boot
├─nvme0n1p2 259:2    0   256M  0 part /rom
├─nvme0n1p3 259:3    0     2G  0 part /overlay/upper/opt/docker
│                                     /overlay
└─nvme0n1p4 259:4    0 951.5G  0 part 
  └─lvg-wxy 253:0    0   780G  0 lvm  
  • wxy:创建的lv名称
  • lvglv所属的卷组
  • -L 780G:使用-L参数指定改lv的大小

创建第二个分区

root@iStoreOS:~# lvcreate -n user -L 70G lvg
  Logical volume "user" created.

创建第三个分区

root@iStoreOS:~# lvcreate -n docker -l +100%FREE lvg
  Logical volume "docker" created.
root@iStoreOS:~# 
  • -l +100%FREE:将剩余空间全部添加给名称dockerlv

格式化lv

首先查看当前分区列表

root@iStoreOS:~# lsblk 
NAME           MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
sda              8:0    0 238.5G  0 disk 
└─sda1           8:1    0 238.5G  0 part /mnt/usb2_2-1
nbd0            43:0    0     0B  0 disk 
nbd1            43:32   0     0B  0 disk 
nbd2            43:64   0     0B  0 disk 
nbd3            43:96   0     0B  0 disk 
nbd4            43:128  0     0B  0 disk 
nbd5            43:160  0     0B  0 disk 
nbd6            43:192  0     0B  0 disk 
nbd7            43:224  0     0B  0 disk 
nvme0n1        259:0    0 953.9G  0 disk 
├─nvme0n1p1    259:1    0   128M  0 part /boot
│                                        /boot
├─nvme0n1p2    259:2    0   256M  0 part /rom
├─nvme0n1p3    259:3    0     2G  0 part /overlay/upper/opt/docker
│                                        /overlay
└─nvme0n1p4    259:4    0 951.5G  0 part 
  ├─lvg-wxy    253:0    0   780G  0 lvm  
  ├─lvg-user   253:1    0    70G  0 lvm  
  └─lvg-docker 253:2    0 101.5G  0 lvm  

格式化docker

root@iStoreOS:~# mkfs.ext4 /dev/lvg/docker 
mke2fs 1.46.5 (30-Dec-2021)
Discarding device blocks: done                            
Creating filesystem with 26605568 4k blocks and 6651904 inodes
Filesystem UUID: 07081ca1-5a03-426b-8274-4c7ad790230c
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
        4096000, 7962624, 11239424, 20480000, 23887872

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (131072 blocks): done
Writing superblocks and filesystem accounting information: done   

格式化wxy

root@iStoreOS:~# mkfs.ext4 /dev/lvg/wxy 
mke2fs 1.46.5 (30-Dec-2021)
Discarding device blocks: done                            
Creating filesystem with 204472320 4k blocks and 51118080 inodes
Filesystem UUID: af252620-3010-49f1-bc02-474b208eba90
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
        4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968, 
        102400000

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (262144 blocks): done
Writing superblocks and filesystem accounting information: done     

格式化user

root@iStoreOS:~# mkfs.ext4 /dev/lvg/user 
mke2fs 1.46.5 (30-Dec-2021)
Discarding device blocks: done                            
Creating filesystem with 18350080 4k blocks and 4587520 inodes
Filesystem UUID: 3f6044e9-939d-43cb-b618-d7a437361597
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
        4096000, 7962624, 11239424

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (131072 blocks): done
Writing superblocks and filesystem accounting information: done   

root@iStoreOS:~# 

挂载lv

root@iStoreOS:~# mkdir /wxy
root@iStoreOS:~# mount /dev/lvg/wxy /wxy/
root@iStoreOS:~# touch /wxy/1
root@iStoreOS:~# df -h
Filesystem                Size      Used Available Use% Mounted on
/dev/root               154.3M    154.3M         0 100% /rom
tmpfs                     7.7G      4.3M      7.7G   0% /tmp
/dev/nvme0n1p3            1.9G      3.7M      1.9G   0% /overlay
overlayfs:/overlay        1.9G      3.7M      1.9G   0% /
tmpfs                     4.0M         0      4.0M   0% /mnt
/dev/nvme0n1p1          127.7M      5.5M    122.2M   4% /boot
/dev/nvme0n1p1          127.7M      5.5M    122.2M   4% /boot
tmpfs                   512.0K         0    512.0K   0% /dev
cgroup                    7.7G         0      7.7G   0% /sys/fs/cgroup
/dev/sda1               233.7G     70.0G    151.7G  32% /mnt/usb2_2-1
tmpfs                   512.0K         0    512.0K   0% /sys/module/md_mod/parameters/new_array
/dev/nvme0n1p3            1.9G      3.7M      1.9G   0% /overlay/upper/opt/docker
/dev/mapper/lvg-wxy     766.7G     28.0K    727.7G   0% /wxy
root@iStoreOS:~# 

仅演示wxy的挂载,那么到这里就结束了,如果后期需要调整lv分区的大小,可以参考下面的文章

Linux使用LVM调整根分区(扩容)

你可能感兴趣的:(服务器,运维,linux)