lvm 磁盘管理

逻辑卷磁盘管理

lvm是一个工具,可以将多个磁盘或多个分区,整合成一个文件系统分区,比如你有4个硬盘,每个硬盘是1T的容量,通过lvm ,就可以制作成一个4T的磁盘挂载到/mnt 目录提供存储服务。

查看所有分区信息

[root@localhost ~]# fdisk -l

Disk /dev/vda: 42.9 GB, 42949672960 bytes, 83886080 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: 0x0008d73a

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048    83884031    41940992   83  Linux
[root@localhost ~]#

安装lvm2

yum install lvm2 -y

lvm2的描述信息

[root@localhost ~]# yum info lvm2
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * epel: hkg.mirror.rackspace.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Installed Packages
Name        : lvm2
Arch        : x86_64
Epoch       : 7
Version     : 2.02.180
Release     : 10.el7_6.7
Size        : 3.0 M
Repo        : installed
From repo   : updates
Summary     : Userland logical volume management tools
URL         : http://sources.redhat.com/lvm2
License     : GPLv2
Description : LVM2 includes all of the support for handling read/write operations on
            : physical volumes (hard disks, RAID-Systems, magneto optical, etc.,
            : multiple devices (MD), see mdadm(8) or even loop devices, see
            : losetup(8)), creating volume groups (kind of virtual disks) from one
            : or more physical volumes and creating one or more logical volumes
            : (kind of logical partitions) in volume groups.

Available Packages
Name        : lvm2
Arch        : x86_64
Epoch       : 7
Version     : 2.02.180
Release     : 10.el7_6.8
Size        : 1.3 M
Repo        : updates/7/x86_64
Summary     : Userland logical volume management tools
URL         : http://sources.redhat.com/lvm2
License     : GPLv2
Description : LVM2 includes all of the support for handling read/write operations on
            : physical volumes (hard disks, RAID-Systems, magneto optical, etc.,
            : multiple devices (MD), see mdadm(8) or even loop devices, see
            : losetup(8)), creating volume groups (kind of virtual disks) from one
            : or more physical volumes and creating one or more logical volumes
            : (kind of logical partitions) in volume groups.

[root@localhost ~]#

删除旧的磁盘分区

[root@localhost ~]# fdisk /dev/sdb 
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): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

Command (m for help): p

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 label type: dos
Disk identifier: 0x57181a0a

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4194303     2096128   83  Linux

Command (m for help): d
Selected partition 1
Partition 1 is deleted

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

Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# 

创建磁盘分区1

其中8e指定分区类型为lvm

[root@localhost ~]# fdisk /dev/sdb
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): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): 
Using default response p
Partition number (1-4, default 1): 
First sector (2048-4194303, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-4194303, default 4194303): +1G
Partition 1 of type Linux and of size 1 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.
[root@localhost ~]#

创建磁盘分区2

[root@localhost ~]# fdisk /dev/sdc 
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 0x6115b360.

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): 
Using default response p
Partition number (1-4, default 1): 
First sector (2048-4194303, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-4194303, default 4194303): +2G
Value out of range.
Last sector, +sectors or +size{K,M,G} (2048-4194303, default 4194303): +1.8G
Unsupported suffix: '.8G'.
Supported: 10^N: KB (KiloByte), MB (MegaByte), GB (GigaByte)
            2^N: K  (KibiByte), M  (MebiByte), G  (GibiByte)
Last sector, +sectors or +size{K,M,G} (2048-4194303, default 4194303): +1G
Partition 1 of type Linux and of size 1 GiB is set

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): l

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

Command (m for help): p

Disk /dev/sdc: 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 label type: dos
Disk identifier: 0x6115b360

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1            2048     2099199     1048576   8e  Linux LVM

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

Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# 

创建磁盘分区3

[root@localhost ~]# fdisk /dev/sdd 
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 0x2a7fe36f.

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): 
Using default response p
Partition number (1-4, default 1): 
First sector (2048-2097151, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-2097151, default 2097151): 800M
Value out of range.
Last sector, +sectors or +size{K,M,G} (2048-2097151, default 2097151): +800M
Partition 1 of type Linux and of size 800 MiB 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): p

Disk /dev/sdd: 1073 MB, 1073741824 bytes, 2097152 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: 0x2a7fe36f

   Device Boot      Start         End      Blocks   Id  System
/dev/sdd1            2048     1640447      819200   8e  Linux LVM

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

Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]#

创建物理卷

[root@localhost ~]# pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created.
[root@localhost ~]# pvcreate /dev/sdc1
  Physical volume "/dev/sdc1" successfully created.
[root@localhost ~]# pvcreate /dev/sdd1
  Physical volume "/dev/sdd1" successfully created.

查看当前物理卷

[root@localhost ~]# pvdisplay 
  "/dev/sdc1" is a new physical volume of "1.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdc1
  VG Name               
  PV Size               1.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               icFgbd-fF7D-xf31-zx0x-H4Bc-EFOX-N1D5BO
   
  "/dev/sdd1" is a new physical volume of "800.00 MiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdd1
  VG Name               
  PV Size               800.00 MiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               9n3Lmq-sjor-b8hC-fcXh-QhaZ-dSYe-dMJ7sx
   
  "/dev/sdb1" is a new physical volume of "1.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb1
  VG Name               
  PV Size               1.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               CWTPxW-PbON-udSB-Sa17-drs6-FvSt-uCtDDA
   
[root@localhost ~]#

列出已存在物理卷

[root@localhost ~]# pvs
  PV         VG Fmt  Attr PSize   PFree  
  /dev/sdb1     lvm2 ---    1.00g   1.00g
  /dev/sdc1     lvm2 ---    1.00g   1.00g
  /dev/sdd1     lvm2 ---  800.00m 800.00m
[root@localhost ~]# 

pvremove /dev/sdb3 删除/恢复物理卷(如果需要缩容)

[root@localhost ~]# pvremove /dev/sdb1
  Labels on physical volume "/dev/sdb1" successfully wiped.
[root@localhost ~]# pvs
  PV         VG Fmt  Attr PSize   PFree  
  /dev/sdc1     lvm2 ---    1.00g   1.00g
  /dev/sdd1     lvm2 ---  800.00m 800.00m
[root@localhost ~]# pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created.
[root@localhost ~]# pvs
  PV         VG Fmt  Attr PSize   PFree  
  /dev/sdb1     lvm2 ---    1.00g   1.00g
  /dev/sdc1     lvm2 ---    1.00g   1.00g
  /dev/sdd1     lvm2 ---  800.00m 800.00m
[root@localhost ~]# 

vgcreate创建卷组:

[root@localhost ~]# vgcreate vg1 /dev/sdb1 /dev/sdc1 /dev/sdd1
  Volume group "vg1" successfully created
[root@localhost ~]#

vgdisplay和vgs查看卷组:

[root@localhost ~]# vgdisplay 
  --- Volume group ---
  VG Name               vg1
  System ID             
  Format                lvm2
  Metadata Areas        3
  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                3
  Act PV                3
  VG Size               <2.77 GiB
  PE Size               4.00 MiB
  Total PE              709
  Alloc PE / Size       0 / 0   
  Free  PE / Size       709 / <2.77 GiB
  VG UUID               4niFDx-ie5Y-oxf6-8T86-O96e-6QnV-P4hiQE
   
[root@localhost ~]#

lvcreate创建逻辑卷:

[root@localhost ~]# lvcreate -L 100M -n lv1 vg1
  Logical volume "lv1" created.
[root@localhost ~]#

逻辑卷设备文件

[root@localhost ~]# ls /dev/vg1/lv1 
/dev/vg1/lv1
[root@localhost ~]# ls /dev/vg1/lv1 -l
lrwxrwxrwx. 1 root root 7 Aug 11 20:41 /dev/vg1/lv1 -> ../dm-0
[root@localhost ~]#

格式化逻辑卷lv1为ext4文件系统:

[root@localhost ~]# mkfs.ext
mkfs.ext2  mkfs.ext3  mkfs.ext4  
[root@localhost ~]# mkfs.ext4 /dev/vg1/lv1 
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
25688 inodes, 102400 blocks
5120 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=33685504
13 block groups
8192 blocks per group, 8192 fragments per group
1976 inodes per group
Superblock backups stored on blocks: 
	8193, 24577, 40961, 57345, 73729

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

[root@localhost ~]

挂载使用逻辑卷:

[root@localhost ~]# mount /dev/vg1/lv1 /mnt/
[root@localhost ~]# df -h
Filesystem           Size  Used Avail Use% Mounted on
/dev/sda3             38G  7.8G   30G  21% /
devtmpfs             904M     0  904M   0% /dev
tmpfs                914M     0  914M   0% /dev/shm
tmpfs                914M  9.0M  905M   1% /run
tmpfs                914M     0  914M   0% /sys/fs/cgroup
/dev/sda1            297M  115M  183M  39% /boot
tmpfs                183M     0  183M   0% /run/user/0
/dev/mapper/vg1-lv1   93M  1.6M   85M   2% /mnt
[root@localhost ~]# umount /mnt/

扩容逻辑卷

  • lvresize -L 300M /dev/vg1/lv1 重新设置卷大小
  • e2fsck -f /dev/vg1/lv1 检查磁盘错误(ext4执行)
  • resize2fs /dev/vg1/lv1 更新逻辑卷信息(ext4执行)
  • xfs_growfs /dev/vg1/lv1 xfs 文件系统需要执行

缩减逻辑卷(xfs不支持)

  • 先umounty已经挂载的逻辑卷
  • e2fsck -f /dev/vg1/lv1 检查磁盘错误(ext)
  • resize2fs /dev/vg1/lv1 100M 更新逻辑卷信息(ext)
  • lvresize -L 100M /dev/vg1/lv1 重新设置卷大小

直接删除逻辑卷

[root@localhost ~]# lvremove /dev/vg1/lv1 
Do you really want to remove active logical volume vg1/lv1? [y/n]: y
  Logical volume "lv1" successfully removed
[root@localhost ~]#

查看逻辑卷总的容量大小。创建最大容量的逻辑卷

[root@localhost ~]# vgdisplay 
  --- Volume group ---
  VG Name               vg1
  System ID             
  Format                lvm2
  Metadata Areas        3
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                3
  Act PV                3
  VG Size               <2.77 GiB
  PE Size               4.00 MiB
  Total PE              709
  Alloc PE / Size       0 / 0   
  Free  PE / Size       709 / <2.77 GiB
  VG UUID               4niFDx-ie5Y-oxf6-8T86-O96e-6QnV-P4hiQE
   
[root@localhost ~]# lvcreate -L 2700M -n lv1 vg1
WARNING: ext4 signature detected on /dev/vg1/lv1 at offset 1080. Wipe it? [y/n]: y
  Wiping ext4 signature on /dev/vg1/lv1.
  Logical volume "lv1" created.
[root@localhost ~]#

格式化本次创建的逻辑卷lv1为ext4文件系统,并挂载使用逻辑卷:

[root@localhost ~]# mkfs.ext4 /dev/vg1/lv1 
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
172832 inodes, 691200 blocks
34560 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=708837376
22 block groups
32768 blocks per group, 32768 fragments per group
7856 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912

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

[root@localhost ~]# mount /dev/vg
vg1/         vga_arbiter  
[root@localhost ~]# mount /dev/vg1/lv1 /mnt/
[root@localhost ~]# df -h
Filesystem           Size  Used Avail Use% Mounted on
/dev/sda3             38G  7.8G   30G  21% /
devtmpfs             904M     0  904M   0% /dev
tmpfs                914M     0  914M   0% /dev/shm
tmpfs                914M  9.0M  905M   1% /run
tmpfs                914M     0  914M   0% /sys/fs/cgroup
/dev/sda1            297M  115M  183M  39% /boot
tmpfs                183M     0  183M   0% /run/user/0
/dev/mapper/vg1-lv1  2.6G  8.0M  2.4G   1% /mnt
[root@localhost ~]#

设置lvm开机自动挂载

#
# /etc/fstab
# Created by anaconda on Mon May 27 10:32:20 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=c9305e2c-0722-43a6-a536-c94fedbe7d48 /                       xfs     defaults        0 0 
UUID=7cb44bde-e4c2-4639-9b63-4d2373ed1bb2 /boot                   xfs     defaults        0 0 
UUID=ac576e44-e341-4a18-8a3b-1521ad380bfd swap                    swap    defaults        0 0 
/dev/mapper/vg1-lv1                     /mnt                    ext4    defaults        0 0 

email:[email protected] 画笔

你可能感兴趣的:(lvm 磁盘管理)