linux磁盘篇——lvm
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux磁盘篇——lvm相关的知识,希望对你有一定的参考价值。
LVM解决了分区大小分配磁盘扩展问题。
例如:
在创建的系统的时候讲/date 分配挂载在/目录下,后期由于业务需求需要独立出来。重新挂载一个新的分区,期中之前的数据将无法使用,虽然可以使用cp等命令将数据迁移。但是若果新挂载的这个硬盘容量也满了了?挂载一个新的更大容量的硬盘重新挂载?若是长期使用这种办法也是也可以,但是资源浪费严重。
LVM就是打破物理存储限制、可以随意增加容量、缩减容量。
1)LVM原理图
2)LVM相关名词解释
01物理存储介质:例如磁盘等
02物理卷PV:磁盘分区、raid等
03卷组VG:PV容量组件的池(pv的集合等)、类似于一块待分区的“磁盘”
04逻辑卷LV:VG的分区,再次上可以建立文件系统挂载等使用。类似于磁盘的分区可以被格式化、挂载使用
05逻辑块LE:逻辑卷上最小的容量单位,默认是4MB
演示环境:
VMware workstation 12.0.1 build-3160714
centos 6.9
挂载了两块新的虚拟磁盘容量分别为1GB 2GB
[[email protected] ~]# fdisk -l |grep "/dev/sd[abc]"
Disk /dev/sda: 21.5 GB, 21474836480 bytes
/dev/sda1 * 1 26 204800 83 Linux
/dev/sda2 26 157 1048576 82 Linux swap / Solaris
/dev/sda3 157 2611 19717120 83 Linux
Disk /dev/sdb: 1073 MB, 1073741824 bytes
Disk /dev/sdc: 2147 MB, 2147483648 bytes
[[email protected] ~]#
LVM常用命令
阶段 | 显示 | 创建 | 删除 | 增加 | 缩小 |
PV | pvdisplay | pvcreat | pvremove | ||
VG | vgdisplay | vgcreat | vgremove | vgextend | vgreduce |
LV | lvdisplay | lvcreat | lvremove | lvextend | lvreduce |
3)创建LVM过程
01、使用fdisk初始化磁盘-选择磁盘格式为8e
创建流程:新建主分区-编号为1-容量所有-选择格式为LVM-保存
01-1、初始化/dev/sdb
[[email protected] ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x9396a336.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-130, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-130, default 130):
Using default value 130
Command (m for help): p
Disk /dev/sdb: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x9396a336
Device Boot Start End Blocks Id System
/dev/sdb1 1 130 1044193+ 83 Linux
Command (m for help): t ###选择格式
Selected partition 1
Hex code (type L to list codes): 8e ##格式设置为8e
Changed system type of partition 1 to 8e (Linux LVM)
Command (m for help): p
Disk /dev/sdb: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x9396a336
Device Boot Start End Blocks Id System
/dev/sdb1 1 130 1044193+ 8e Linux LVM
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[[email protected] ~]#
01-2、初始化/dev/sdc
步骤如上
[[email protected] ~]# fdisk -l |grep "/dev/sd[abc]"
Disk /dev/sda: 21.5 GB, 21474836480 bytes
/dev/sda1 * 1 26 204800 83 Linux
/dev/sda2 26 157 1048576 82 Linux swap / Solaris
/dev/sda3 157 2611 19717120 83 Linux
Disk /dev/sdb: 1073 MB, 1073741824 bytes
/dev/sdb1 1 130 1044193+ 8e Linux LVM
Disk /dev/sdc: 2147 MB, 2147483648 bytes
/dev/sdc1 1 261 2096451 8e Linux LVM
[[email protected] ~]#
02、创建PV
#创建pv
[[email protected] ~]# pvcreate /dev/sd[bc]1
Physical volume "/dev/sdb1" successfully created
Physical volume "/dev/sdc1" successfully created
[[email protected] ~]#
#查看pv
[[email protected] ~]# pvdisplay
"/dev/sdb1" is a new physical volume of "1019.72 MiB"
--- NEW Physical volume ---
PV Name /dev/sdb1
VG Name
PV Size 1019.72 MiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID GVfctN-vmJQ-3A6M-ogcd-fVbi-udQD-RRu03q
"/dev/sdc1" is a new physical volume of "2.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdc1
VG Name
PV Size 2.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID 74mwlF-SBuT-U19r-JQZk-1BOj-NdMd-cdXIFg
[[email protected] ~]#
03、创建VG
[[email protected] ~]# vgcreate disk01 /dev/sd[bc]1 ###创建VG命名为disk01
Volume group "disk01" successfully created
[[email protected] ~]# vgdisplay
--- Volume group ---
VG Name disk01 #VG名称
System ID
Format lvm2
Metadata Areas 2
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 2
Act PV 2
VG Size 2.99 GiB ##VG总容量
PE Size 4.00 MiB ##LE块的大小
Total PE 765 ##LE块的数量
Alloc PE / Size 0 / 0
Free PE / Size 765 / 2.99 GiB
VG UUID fj09w3-RSVt-oRxH-nETB-SsC3-C8i9-CxLA5j
[[email protected] ~]#
04、从VG中划分LV
lvcreate 命令常用参数:
-L 设置指定大小
-n 设置指定名称
[[email protected] ~]# lvcreate -L 100M -n LVM_date01 disk01
Logical volume "LVM_date01" created.
[[email protected] ~]# lvdisplay
--- Logical volume ---
LV Path /dev/disk01/LVM_date01
LV Name LVM_date01 #LV名称
VG Name disk01 #所属VG名称
LV UUID g3BkWT-F79P-txPb-VvFv-FIsF-Ox1t-ZoTpye
LV Write Access read/write
LV Creation host, time linux-edu, 2017-11-25 20:05:04 +0800
LV Status available
# open 0
LV Size 100.00 MiB ##LV的总容量
Current LE 25 ##LE的个数
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
[[email protected] ~]#
05、格式化、挂载分区
[[email protected] ~]# mkfs.ext4 /dev/disk01/LVM_date01
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
......
[[email protected] /]# mount /dev/disk01/LVM_date01 /data/
[[email protected] /]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 19G 1.5G 17G 9% /
tmpfs 491M 0 491M 0% /dev/shm
/dev/sda1 190M 35M 146M 19% /boot
/dev/mapper/disk01-LVM_date01
93M 1.6M 87M 2% /data
[[email protected] /]#
至此LVM分区创建完毕
4)扩容LVM
01、首先逻辑扩展(扩展LV容量)
使用的命令:
lvextend 扩展LV容量
常用参数: -L 500M #将容量扩展到500M
-L +500M #在原有的基础上加上500M
[[email protected] /]# lvextend -L +200M /dev/disk01/LVM_date01
Size of logical volume disk01/LVM_date01 changed from 100.00 MiB (25 extents) to 300.00 MiB (75 extents).
Logical volume LVM_date01 successfully resized.
[[email protected] /]# lvdisplay
--- Logical volume ---
LV Path /dev/disk01/LVM_date01
LV Name LVM_date01
VG Name disk01
LV UUID g3BkWT-F79P-txPb-VvFv-FIsF-Ox1t-ZoTpye
LV Write Access read/write
LV Creation host, time linux-edu, 2017-11-25 20:05:04 +0800
LV Status available
# open 1
LV Size 300.00 MiB
Current LE 75
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
[[email protected] /]#
02、其次文件系统的扩展
使用的命令是:resize2fs
[[email protected] /]# resize2fs -p /dev/disk01/LVM_date01
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/disk01/LVM_date01 is mounted on /data; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 2
Performing an on-line resize of /dev/disk01/LVM_date01 to 307200 (1k) blocks.
The filesystem on /dev/disk01/LVM_date01 is now 307200 blocks long.
[[email protected] /]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 19G 1.5G 17G 9% /
tmpfs 491M 0 491M 0% /dev/shm
/dev/sda1 190M 35M 146M 19% /boot
/dev/mapper/disk01-LVM_date01
287M 2.1M 270M 1% /data
[[email protected] /]#
至此扩容成功
5)缩小LVM
缩减容量是一件危险的操作;缩减必须在离线状态下执行;并且必须先强制检查文件系统错误,防止缩减过程损坏数据;
01、卸载、检查
[[email protected] /]# umount /dev/disk01/LVM_date01
[[email protected] /]# e2fsck -f /dev/disk01/LVM_date01
e2fsck 1.41.12 (17-May-2010)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/disk01/LVM_date01: 11/75088 files (9.1% non-contiguous), 15637/307200 blocks
[[email protected] /]#
02、缩减文件系统的容量
[[email protected] /]# resize2fs /dev/disk01/LVM_date01 100M
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/disk01/LVM_date01 to 102400 (1k) blocks.
The filesystem on /dev/disk01/LVM_date01 is now 102400 blocks long.
[[email protected] /]#
03、在缩减LV的容量
[[email protected] /]# lvreduce -L 100M /dev/disk01/LVM_date01
WARNING: Reducing active logical volume to 100.00 MiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce disk01/LVM_date01? [y/n]: y
Size of logical volume disk01/LVM_date01 changed from 300.00 MiB (75 extents) to 100.00 MiB (25 extents).
Logical volume LVM_date01 successfully resized.
[[email protected] /]# lvdisplay
--- Logical volume ---
LV Path /dev/disk01/LVM_date01
LV Name LVM_date01
VG Name disk01
LV UUID g3BkWT-F79P-txPb-VvFv-FIsF-Ox1t-ZoTpye
LV Write Access read/write
LV Creation host, time linux-edu, 2017-11-25 20:05:04 +0800
LV Status available
# open 0
LV Size 100.00 MiB
Current LE 25
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
[[email protected] /]#
03、重新挂载
[[email protected] /]# mount /dev/disk01/LVM_date01 /data/
[[email protected] /]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 19G 1.5G 17G 9% /
tmpfs 491M 0 491M 0% /dev/shm
/dev/sda1 190M 35M 146M 19% /boot
/dev/mapper/disk01-LVM_date01
93M 1.6M 87M 2% /data
[[email protected] /]#
本文借鉴前辈、重新整理。
以上是关于linux磁盘篇——lvm的主要内容,如果未能解决你的问题,请参考以下文章