Linux系统下创建LV(逻辑卷)并挂载

发布时间 2023-09-27 18:24:30作者: Justtosee

1、查看磁盘情况

[root@ecs-34a8-0913916 ~]# fdisk -l

Disk /dev/vda: 107.4 GB, 107374182400 bytes, 209715200 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: 0x000bcc64

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1            2048     8390655     4194304   82  Linux swap / Solaris
/dev/vda2   *     8390656   209715166   100662255+  83  Linux

Disk /dev/vdb: 1073.7 GB, 1073741824000 bytes, 2097152000 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: 0x1f1ab182

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048  2097151999  1048574976   83  Linux

可以看到有个/dev/vdb1的磁盘分区待使用。

如果没有/dev/vdb1的分区的话,需要对磁盘/dev/vdb先进行分区。

2、系统标识符

[root@ecs-34a8-0913916 ~]# 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.


Command (m for help): p

Disk /dev/vdb: 1073.7 GB, 1073741824000 bytes, 2097152000 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: 0x1f1ab182

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048  2097151999  1048574976   83  Linux

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): p

Disk /dev/vdb: 1073.7 GB, 1073741824000 bytes, 2097152000 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: 0x1f1ab182

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048  2097151999  1048574976   8e  Linux LVM

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

输入 改变分区1的属性

输入 查看有个属性对应的命令

输入 8e 改变分区1为 Linux LVM格式

输入 p 打印分区情况,发现建立的分区 /dev/vdb1 为 Linux LVM 格式

输入w保存退出(q为不保存修改退出)

3、创建PV

[root@ecs-34a8-0913991 ~]# pvcreate /dev/vdb1
WARNING: xfs signature detected on /dev/vdb1 at offset 0. Wipe it? [y/n]: y
  Wiping xfs signature on /dev/vdb1.
  Physical volume "/dev/vdb1" successfully created.
[root@ecs-34a8-0913991 ~]# 
[root@ecs-34a8-0913991 ~]# vgcreate vg_vdb /dev/vdb1
  Volume group "vg_vdb" successfully created
[root@ecs-34a8-0913991 ~]# vgdisplay 
  --- Volume group ---
  VG Name               vg_vdb
  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               <1000.00 GiB
  PE Size               4.00 MiB
  Total PE              255999
  Alloc PE / Size       0 / 0   
  Free  PE / Size       255999 / <1000.00 GiB
  VG UUID               rnjRTf-Nwaz-vB0F-a2Yv-WK1b-QNwb-WswEf6

创建PV:pvcreate /dev/vdb1

查看PV:pvdisplay

4、创建 VG

[root@ecs-34a8-0913991 ~]# vgcreate vg_vdb /dev/vdb1
  Volume group "vg_vdb" successfully created
[root@ecs-34a8-0913991 ~]# vgdisplay 
  --- Volume group ---
  VG Name               vg_vdb
  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               <1000.00 GiB
  PE Size               4.00 MiB
  Total PE              255999
  Alloc PE / Size       0 / 0   
  Free  PE / Size       255999 / <1000.00 GiB
  VG UUID               rnjRTf-Nwaz-vB0F-a2Yv-WK1b-QNwb-WswEf6
   

创建VG:vgcreate vg_vdb /dev/vdb1

查看VG:vgdisplay

这样我们就创建了一个 1000G(255999 个PE,要记住这个数字)的VG(名字为vg_vdb

 

5、创建LV

[root@ecs-34a8-0913991 ~]# lvcreate -l 255999 -n lv_vdb vg_vdb
  Logical volume "lv_vdb" created.

创建LV:lvcreate -l 255999 -n lv_vdb vg_vdb (255999是VG中PE的个数)

查看LV:lvdisplay

[root@ecs-34a8-0913991 ~]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/vg_vdb/lv_vdb
  LV Name                lv_vdb
  VG Name                vg_vdb
  LV UUID                HNgtI1-OSYg-GRmk-xBMB-VwxR-CdJv-pWyBMq
  LV Write Access        read/write
  LV Creation host, time ecs-34a8-0913991, 2023-09-27 18:07:35 +0800
  LV Status              available
  # open                 1
  LV Size                <1000.00 GiB
  Current LE             255999
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           252:0
   

6、格式化刚刚创建的LV

# mkfs -t ext4 /dev/vg_vdb/lv_vdb
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
65536000 inodes, 262142976 blocks
13107148 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2409627648
8000 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, 
        102400000, 214990848

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

7、挂载磁盘

[root@ecs-34a8-0913991 ~]# mkdir /data01
[root@ecs-34a8-0913991 ~]# mount /dev/vg_vdb/lv_vdb /data01

8、加入开机自启/etc/fstab