lvm
操作流程:
磁盘分区-->创建物理卷-->划分为卷组-->划分成逻辑卷-->格式化、挂载-->扩容。磁盘分区
注: 创建分区时需要更改其文件类型为lvm(代码8e)
分区 3 已设置为 Linux 类型,大小设为 1 GiB命令(输入 m 获取帮助):t分区号 (1-3,默认 3):Hex 代码(输入 L 列出所有代码):8e已将分区“Linux”的类型更改为“Linux LVM”
创建物理卷
#### pvcreate命令
pvcreate=physical volume create
[root@cham2 ~]# pvcreate /dev/sdb1-bash: pvcreate: 未找到命令
命令不存在,需要安装该命令包!
[root@cham2 ~]# yum install -y lvm已加载插件:fastestmirrorRepodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fastbase | 3.6 kB 00:00:00 extras | 3.4 kB 00:00:00 updates | 3.4 kB 00:00:00 (1/2): extras/7/x86_64/primary_db | 168 kB 00:00:00 (2/2): updates/7/x86_64/primary_db | 5.7 MB 00:00:04 Determining fastest mirrors * base: mirrors.btte.net * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com没有可用软件包 lvm。错误:无须任何处理
即,‘lvm’不是该命令所在包的正确名称,此时不知道正确的包的名称,需要进行查找,进行如下操作:
[root@cham2 ~]# yum provides "/*/pvcreate" 已加载插件:fastestmirrorLoading mirror speeds from cached hostfile * base: mirrors.btte.net * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com 7:lvm2-2.02.166-1.el7_3.4.x86_64 : Userland logical volume management tools源 :updates匹配来源:文件名 :/usr/sbin/pvcreate即,该包的名称应该是“lvm2”!!![root@cham2 ~]# yum install -y lvm2
yum provides命令: 通过模块查找包
安装完成后继续创建物理卷:
[root@cham2 ~]# pvcreate /dev/sdb1 Physical volume "/dev/sdb1" successfully created.[root@cham2 ~]# pvcreate /dev/sdb2 Physical volume "/dev/sdb2" successfully created.[root@cham2 ~]# pvcreate /dev/sdb3 Physical volume "/dev/sdb3" successfully created.
注: 一般情况在创建完分区后系统会自动生成相应的分区文件“/dev/sdbn”,但是当系统未自动生成时无法完成物理卷创建任务,需要执行命令 “partprobe” 生成相应文件再执行pvcreate命令。
[root@cham2 ~]# partprobe[root@cham2 ~]# ls /dev/sdb1/dev/sdb1
pvdisplay命令&pvs命令
物理卷创建完成后可以使用命令pvdisplay或pvs进行查看:
[root@cham2 ~]# pvdisplay "/dev/sdb3" is a new physical volume of "1.00 GiB" --- NEW Physical volume --- PV Name /dev/sdb3 VG Name PV Size 1.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID GSPdKe-f0UH-7N3v-jEJc-qKtX-spJV-GETeXk "/dev/sdb2" is a new physical volume of "1.00 GiB" --- NEW Physical volume --- PV Name /dev/sdb2 VG Name PV Size 1.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID 079lZ8-2cLK-XQ7c-oBN4-xe40-i2qT-GeNK8Z "/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 d8kpdO-I4nV-U9Rd-ZJ6l-Fn29-69TA-EpPC8I
[root@cham2 ~]# pvs PV VG Fmt Attr PSize PFree /dev/sdb1 lvm2 --- 1.00g 1.00g /dev/sdb2 lvm2 --- 1.00g 1.00g /dev/sdb3 lvm2 --- 1.00g 1.00g
创建物理卷组
vgcreate命令
vgcreate=volume group create
语法: vgcreate [组名] [参数] 参数指分区
创建卷组:
[root@cham2 ~]# vgcreate vg1 /dev/sdb1 /dev/sdb2 Volume group "vg1" successfully created
创建完成后同样可以使用vgdisplay命令和vgs命令进行查看:
[root@cham2 ~]# vgdisplay --- Volume group --- VG Name vg1 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 1.99 GiB PE Size 4.00 MiB Total PE 510 Alloc PE / Size 0 / 0 Free PE / Size 510 / 1.99 GiB VG UUID vMT1UN-VOkT-C0sh-kSBQ-6MFC-j1H2-LpS0nm [root@cham2 ~]# vgs VG #PV #LV #SN Attr VSize VFree vg1 2 0 0 wz--n- 1.99g 1.99g
vgremove命令
vgremove=volume group remove 删除卷组
创建逻辑卷
lvcreate命令
lvcreate=logical volume create 创建逻辑卷
语法: lvcreate [options] [参数] 此处参数指物理卷组
选项: -L:指定逻辑卷的大小,单位为“kKmMgGtT”字节 -n:指定逻辑卷的名称。[root@cham2 ~]# lvcreate -L 100M -n lv1 vg1 Logical volume "lv1" created.
创建完成后同样可以使用lvdisplay命令和lvs命令进行查看:
[root@cham2 ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert lv1 vg1 -wi-a----- 100.00m [root@cham2 ~]# lvdisplay --- Logical volume --- LV Path /dev/vg1/lv1 LV Name lv1 VG Name vg1 LV UUID hqONFQ-UylG-zTA1-Ewzr-1Iag-qpMl-pymiip LV Write Access read/write LV Creation host, time cham2, 2017-11-06 14:00:27 +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 8192 Block device 253:0
ext4格式
格式化&挂载
格式化成ext4格式
[root@cham2 ~]# mkfs.ext4 /dev/vg1/lv1mke2fs 1.42.9 (28-Dec-2013)文件系统标签=OS type: Linux块大小=1024 (log=0)分块大小=1024 (log=0)Stride=0 blocks, Stripe width=0 blocks25688 inodes, 102400 blocks5120 blocks (5.00%) reserved for the super user第一个数据块=1Maximum filesystem blocks=3368550413 block groups8192 blocks per group, 8192 fragments per group1976 inodes per groupSuperblock backups stored on blocks: 8193, 24577, 40961, 57345, 73729Allocating group tables: 完成 正在写入inode表: 完成 Creating journal (4096 blocks): 完成Writing superblocks and filesystem accounting information: 完成
格式化完成后开始挂载:
[root@cham2 ~]# mount /dev/vg1/lv1 /mnt/[root@cham2 ~]# df -h文件系统 容量 已用 可用 已用% 挂载点/dev/sda3 17G 1.2G 15G 8% /devtmpfs 483M 0 483M 0% /devtmpfs 493M 0 493M 0% /dev/shmtmpfs 493M 6.8M 486M 2% /runtmpfs 493M 0 493M 0% /sys/fs/cgroup/dev/sda1 187M 109M 79M 59% /boottmpfs 99M 0 99M 0% /run/user/0/dev/mapper/vg1-lv1 93M 1.6M 85M 2% /mnt[root@cham2 ~]# ls -l /dev/vg1/lv1lrwxrwxrwx 1 root root 7 11月 6 14:01 /dev/vg1/lv1 -> ../dm-0[root@cham2 ~]# ls -l /dev/mapper/vg1-lv1lrwxrwxrwx 1 root root 7 11月 6 14:01 /dev/mapper/vg1-lv1 -> ../dm-0
扩容
流程:更改逻辑卷信息(lvresize)-->检测磁盘错误(e2fsck -f)-->更新逻辑卷信息(resize2fs)
先在/mnt/目录下写入一些东西,备用:
[root@cham2 ~]# mkdir /mnt/1212[root@cham2 ~]# touch /mnt/23.txt[root@cham2 ~]# echo "111111111111111" > /mnt/23.txt
开始扩容:
lvresize命令
lvresize命令(=logical volume resize)用于调整LVM逻辑卷的空间大小,可以增大空间和缩小空间。使用lvresize命令调整逻辑卷空间大小和缩小空间时需要谨慎,因为它有可能导致数据丢失。
注: 正常情况下该命令应该在卸载后再执行。
语法: lvresize [optiones] [参数] 参数指逻辑卷选项: -L:指定大小[root@cham2 ~]# lvresize -L 200M /dev/vg1/lv1 Size of logical volume vg1/lv1 changed from 100.00 MiB (25 extents) to 200.00 MiB (50 extents). Logical volume vg1/lv1 successfully resized.
e2fsck命令
检测磁盘错误(ext4执行)
[root@cham2 ~]# e2fsck -f /dev/vg1/lv1e2fsck 1.42.9 (28-Dec-2013)第一步: 检查inode,块,和大小第二步: 检查目录结构第3步: 检查目录连接性Pass 4: Checking reference counts第5步: 检查簇概要信息/dev/vg1/lv1: 13/25688 files (7.7% non-contiguous), 8898/102400 blocks
resize2fs命令
更新逻辑卷信息(ext4执行)
[root@cham2 ~]# resize2fs /dev/vg1/lv1resize2fs 1.42.9 (28-Dec-2013)Resizing the filesystem on /dev/vg1/lv1 to 204800 (1k) blocks.The filesystem on /dev/vg1/lv1 is now 204800 blocks long.
更新逻辑卷信息后新分区在挂载的时候才能被识别新的大小。
挂载:[root@cham2 ~]# !mountmount /dev/vg1/lv1 /mnt/[root@cham2 ~]# df -h文件系统 容量 已用 可用 已用% 挂载点/dev/sda3 17G 1.2G 15G 8% /devtmpfs 483M 0 483M 0% /devtmpfs 493M 0 493M 0% /dev/shmtmpfs 493M 6.8M 486M 2% /runtmpfs 493M 0 493M 0% /sys/fs/cgroup/dev/sda1 187M 109M 79M 59% /boottmpfs 99M 0 99M 0% /run/user/0/dev/mapper/vg1-lv1 190M 1.6M 175M 1% /mnt
系统扩容不会使文件丢失:
[root@cham2 ~]# ls /mnt/1212 23.txt lost+found[root@cham2 ~]# cat /mnt/23.txt111111111111111
缩容(xfs格式不支持)
流程:卸载(umount)-->检测(e2fsck -f)-->更新逻辑卷信息(大小)(resize2fs)-->重置逻辑卷大小(lvresize)
[root@cham2 ~]# !umountumount /mnt/[root@cham2 ~]# e2fsck -f /dev/vg1/lv1e2fsck 1.42.9 (28-Dec-2013)第一步: 检查inode,块,和大小第二步: 检查目录结构第3步: 检查目录连接性Pass 4: Checking reference counts第5步: 检查簇概要信息/dev/vg1/lv1: 13/49400 files (7.7% non-contiguous), 11886/204800 blocks[root@cham2 ~]# resize2fs /dev/vg1/lv1 100Mresize2fs 1.42.9 (28-Dec-2013)Resizing the filesystem on /dev/vg1/lv1 to 102400 (1k) blocks.The filesystem on /dev/vg1/lv1 is now 102400 blocks long.[root@cham2 ~]# lvresize -L 100M /dev/vg1/lv1 WARNING: Reducing active logical volume to 100.00 MiB. THIS MAY DESTROY YOUR DATA (filesystem etc.)Do you really want to reduce vg1/lv1? [y/n]: y Size of logical volume vg1/lv1 changed from 200.00 MiB (50 extents) to 100.00 MiB (25 extents). Logical volume vg1/lv1 successfully resized.[root@cham2 ~]# lvdisplay --- Logical volume --- LV Path /dev/vg1/lv1 LV Name lv1 VG Name vg1 LV UUID hqONFQ-UylG-zTA1-Ewzr-1Iag-qpMl-pymiip LV Write Access read/write LV Creation host, time cham2, 2017-11-06 14:00:27 +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 8192 Block device 253:0 [root@cham2 ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert lv1 vg1 -wi-a----- 100.00m [root@cham2 ~]# !mountmount /dev/vg1/lv1 /mnt/[root@cham2 ~]# ls /mnt/1212 23.txt lost+found即,合理缩容也不会使系统内文件丢失
xfs格式
格式化&挂载
格式化成xfs格式
[root@cham2 ~]# mkfs.xfs -f /dev/vg1/lv1meta-data=/dev/vg1/lv1 isize=512 agcount=4, agsize=6400 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0, sparse=0data = bsize=4096 blocks=25600, imaxpct=25 = sunit=0 swidth=0 blksnaming =version 2 bsize=4096 ascii-ci=0 ftype=1log =internal log bsize=4096 blocks=855, version=2 = sectsz=512 sunit=0 blks, lazy-count=1realtime =none extsz=4096 blocks=0, rtextents=0
扩容
流程:更改逻辑卷信息(lvresize)-->更新逻辑卷信息(xfs_growfs)
更改逻辑卷信息(lvresize)
[root@cham2 ~]# lvresize -L 300M /dev/vg1/lv1 Size of logical volume vg1/lv1 changed from 100.00 MiB (25 extents) to 300.00 MiB (75 extents). Logical volume vg1/lv1 successfully resized.
xfs_growfs命令(该命令必须在挂载状态执行)
[root@cham2 ~]# xfs_growfs /dev/vg1/lv1meta-data=/dev/mapper/vg1-lv1 isize=512 agcount=4, agsize=6400 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0 spinodes=0data = bsize=4096 blocks=25600, imaxpct=25 = sunit=0 swidth=0 blksnaming =version 2 bsize=4096 ascii-ci=0 ftype=1log =internal bsize=4096 blocks=855, version=2 = sectsz=512 sunit=0 blks, lazy-count=1realtime =none extsz=4096 blocks=0, rtextents=0data blocks changed from 25600 to 76800[root@cham2 ~]# df -h文件系统 容量 已用 可用 已用% 挂载点/dev/sda3 17G 1.2G 15G 8% /devtmpfs 483M 0 483M 0% /devtmpfs 493M 0 493M 0% /dev/shmtmpfs 493M 6.8M 486M 2% /runtmpfs 493M 0 493M 0% /sys/fs/cgroup/dev/sda1 187M 109M 79M 59% /boottmpfs 99M 0 99M 0% /run/user/0/dev/mapper/vg1-lv1 297M 5.5M 292M 2% /mnt
扩容卷组
eg:将/dev/sdb3增加到vg1
vgextend命令
语法: vgextend [卷组名] [物理卷]
[root@cham2 ~]# vgextend vg1 /dev/sdb3 Volume group "vg1" successfully extended[root@cham2 ~]# vgdisplay --- Volume group --- VG Name vg1 System ID Format lvm2 Metadata Areas 3 Metadata Sequence No 6 VG Access read/write VG Status resizable MAX LV 0 Cur LV 1 Open LV 1 Max PV 0 Cur PV 3 Act PV 3 VG Size <2.99 GiB PE Size 4.00 MiB Total PE 765 Alloc PE / Size 75 / 300.00 MiB Free PE / Size 690 / <2.70 GiB VG UUID vMT1UN-VOkT-C0sh-kSBQ-6MFC-j1H2-LpS0nm [root@cham2 ~]# vgs VG #PV #LV #SN Attr VSize VFree vg1 3 1 0 wz--n- <2.99g <2.70g
磁盘故障小案例
- 这是因为添加的另外一块磁盘不存在了,但是配置文件/etc/fstab中又有挂载的配置,挂载出问题就导致这样的故障。
[root@localhost ~]# vi /etc/fstab
- 光标移动到最后的挂载信息位置,键盘按dd删除光标所在行,按ESC输入wq回车退出,然后重启主机。