准备磁盘分区
fdisk /dev/sdb n 创建三个新分区,分别1G t 改变分区类型为8e准备物理卷
pvcreate /dev/sdb1 pvcreate /dev/sdb2 pvcreate /dev/sdb3pvdisplay/pvs 列出当前的物理卷
pvremove /dev/sdb3 删除物理卷准备卷组
vgcreate vg1 /dev/sdb1 /dev/sdb2 /dev/sdb3 vgdisplay /vgs列出卷组 vgremove vg1 删除物理卷创建逻辑卷
lvcreate -L 200M -n lv1 vg1 lvdisplay 列出逻辑卷 格式化mkfs.xfs /dev/vg1/lv1,再挂载 lvremove /dev/vg1/lv1 删除逻辑卷扩容逻辑卷
lvresize -L 300M /dev/vg1/lv1 重新设置卷大小 e2fsck -f /dev/vg1/lv1 检查磁盘错误 (ext4执行) resize2fs /dev/vg1/lv1 更新逻辑卷信息(ext4执行) xfs_growfs /dev/vg1/lv1 xfs文件系统需要执行缩减逻辑卷(xfs不支持 )
先umount e2fsck -f /dev/vg1/lv1 检查磁盘错误(ext) resize2fs /dev/vg1/lv1 100M 更新逻辑卷信息(ext) lvresize -L 100M /dev/vg1/lv1 重新设置卷大小扩展卷组
fdisk /dev/sdb 新增/dev/sdb5(逻辑分区8e) 2G pvcreate /dev/sdb5 vgextend vg1 /dev/sdb5 lvresize -L 100M /dev/vg1/lv1 重新设置卷大小
Lvm
1.创建物理分区;
2.物理分区设置成物理卷; 3.物理卷设置成卷组; 4.卷组划分成逻辑卷, 5.然后再格式化,挂载先创建3个分区,在分区的时候按创建分区,按t设置分区类型8e 为lvm;
[root@linux-128 ~]# fdisk /dev/sdb命令(输入 m 获取帮助):nPartition type: p primary (0 primary, 0 extended, 4 free) e extendedSelect (default p): p分区号 (1-4,默认 1):起始 扇区 (2048-41943039,默认为 2048):将使用默认值 2048Last 扇区, +扇区 or +size{K,M,G} (2048-41943039,默认为 41943039):+1G分区 1 已设置为 Linux 类型,大小设为 1 GiB命令(输入 m 获取帮助):t已选择分区 1Hex 代码(输入 L 列出所有代码):8e已将分区“Linux”的类型更改为“Linux LVM”命令(输入 m 获取帮助):p磁盘 /dev/sdb:21.5 GB, 21474836480 字节,41943040 个扇区Units = 扇区 of 1 * 512 = 512 bytes扇区大小(逻辑/物理):512 字节 / 512 字节I/O 大小(最小/最佳):512 字节 / 512 字节磁盘标签类型:dos磁盘标识符:0xbc339889 设备 Boot Start End Blocks Id System/dev/sdb1 2048 2099199 1048576 8e Linux LVM/dev/sdb2 2099200 4196351 1048576 8e Linux LVM/dev/sdb3 4196352 6293503 1048576 8e Linux LVM
1.创建物理卷
需要安装命令:pvcreate
小知识:如果只知道命令,不知道这个命令是那个包安装的,可以使用命令:yum provides /*/[命令] 来查找命令是那个包安装的。
[root@linux-128 ~]# yum provides /*/pvcreate已加载插件:fastestmirrorbase | 3.6 kB 00:00:00extras | 3.4 kB 00:00:00updates | 3.4 kB 00:00:00(1/2): extras/7/x86_64/primary_db | 129 kB 00:00:00(2/2): updates/7/x86_64/primary_db | 3.6 MB 00:00:02Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.cn99.comextras/7/x86_64/filelists_db | 486 kB 00:00:00updates/7/x86_64/filelists_db | 2.1 MB 00:00:011:bash-completion-2.1-6.el7.noarch : Programmable completion for Bash源 :base匹配来源:文件名 :/usr/share/bash-completion/completions/pvcreate7:lvm2-2.02.171-8.el7.x86_64 : Userland logical volume management tools源 :base匹配来源:文件名 :/usr/sbin/pvcreate
lvm2-2.02.171-8.el7.x86_64 这个就是包名。
[root@linux-128 ~]# yum install -y lvm2
- 安装完使用命令:pvcreate 创建物理卷
[root@linux-128 ~]# pvcreate /dev/sdb1WARNING: ext4 signature detected on /dev/sdb1 at offset 1080. Wipe it? [y/n]: y Wiping ext4 signature on /dev/sdb1. Physical volume "/dev/sdb1" successfully created.[root@linux-128 ~]# pvcreate /dev/sdb2 Physical volume "/dev/sdb2" successfully created.[root@linux-128 ~]# pvcreate /dev/sdb3 Physical volume "/dev/sdb3" successfully created.
- 物理卷创建完后,使用命令:pvdisplay或者pvs来查看信息:
[root@linux-128 ~]# pvdisplay "/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 GqWi3Q-4XT9-1RyO-JE3C-Zber-oPw5-4zGyyp "/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 zxaUnf-kxNt-Udki-sYSk-B3so-8hi1-qTM9gB "/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 6TgcyT-DY1j-9PHt-upJH-nZql-V20D-Ttbm1w
[root@linux-128 ~]# 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
- pvremove /dev/sdb3 删除物理卷
2.创建卷组
- 命令:vgcreate来创建卷组,可以1或者多个物理卷来创建1个卷组;
- 格式:vgcreate [卷组名] dev/sdb1 多个物理卷用空格隔开;
root@linux-128 ~]# vgcreate vg1 /dev/sdb1 dev/sdb2 Device dev/sdb2 not found (or ignored by filtering).
- 用命令:vgdisplay 或者vgs来查看信息:
[root@linux-128 ~]# 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 FbIw1F-dYh6-LL3L-Ah8O-NSTQ-v5gN-G2Lpsy
[root@linux-128 ~]# vgs VG #PV #LV #SN Attr VSize VFree vg1 2 0 0 wz--n- 1.99g 1.99g
- vgremove vg1 删除物理卷
3.创建逻辑卷
- 命令:lvcreate
[root@linux-128 ~]# lvcreate -L 100M -n lv1 vg1 Logical volume "lv1" created.
- 命令:lvdisplay或者lvs来查看信息
[root@linux-128 ~]# lvdisplay --- Logical volume --- LV Path /dev/vg1/lv1 LV Name lv1 VG Name vg1 LV UUID TOmOhK-Hf1Y-aKkQ-nfP4-9tu3-cmUD-lbGjMz LV Write Access read/write LV Creation host, time linux-128, 2017-11-06 22:54:39 +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@linux-128 ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert lv1 vg1 -wi-a----- 100.00m
4.格式化成ext4
[root@linux-128 ~]# 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: 完成
5.挂载:mount
root@linux-128 ~]# mount /dev/vg1/lv1 /mnt[root@linux-128 ~]# df -h文件系统 容量 已用 可用 已用% 挂载点/dev/sda3 16G 1.3G 15G 8% /devtmpfs 907M 0 907M 0% /devtmpfs 916M 0 916M 0% /dev/shmtmpfs 916M 8.6M 908M 1% /runtmpfs 916M 0 916M 0% /sys/fs/cgroup/dev/sda1 197M 109M 88M 56% /boottmpfs 184M 0 184M 0% /run/user/0/dev/mapper/vg1-lv1 93M 1.6M 85M 2% /mnt
[root@linux-128 ~]# mkdir /mnt/1122[root@linux-128 ~]# touch /mnt/23.txt[root@linux-128 ~]# echo "112233" > 23.txt[root@linux-128 ~]# cat 23.txt112233
扩容逻辑卷
- Etx4系统类型扩容前要先卸载挂载点
[root@linux-128 ~]# umount /mnt
- 命令:lvresize -L 来扩容,重新设置卷大小
[root@linux-128 ~]# 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 -f /dev/vg1/lv1 检查磁盘错误 (ext4执行)
[root@linux-128 ~]# 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), 8897/102400 blocks
- resize2fs /dev/vg1/lv1 更新逻辑卷信息(ext4执行)
[root@linux-128 ~]# 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@linux-128 ~]# mount /dev/vg1/lv1 /mnt[root@linux-128 ~]# ls /mnt1122 23.txt lost+found[root@linux-128 ~]# cat 23.txt112233
扩容之前创建的文件还存在,说明扩容不影响之前的文件。
缩减逻辑卷
- 先卸载挂载点
[root@linux-128 ~]# umount /mnt
- e2fsck -f /dev/vg1/lv1 检查磁盘错误(ext)
[root@linux-128 ~]# 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), 11885/204800 blocks
- resize2fs /dev/vg1/lv1 100M 更新逻辑卷信息(ext)
[root@linux-128 ~]# 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.
- lvresize -L 100M /dev/vg1/lv1 重新设置卷大小
[root@linux-128 ~]# 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@linux-128 ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert lv1 vg1 -wi-a----- 100.00m
- 挂载
[root@linux-128 ~]# mount /dev/vg1/lv1 /mnt[root@linux-128 ~]# cat /mnt/23.txt[root@linux-128 ~]# cat 23.txt112233
并不会因缩容导致文件丢失
Xfs类型来扩容
- 先卸载挂载点
[root@linux-128 ~]# umount /mnt
- 设置文件类型
[root@linux-128 ~]# 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
[root@linux-128 ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert lv1 vg1 -wi-ao---- 100.00m
[root@linux-128 ~]# mount /dev/vg1/lv1 /mnt[root@linux-128 ~]# ls /mnt[root@linux-128 ~]# mkdir /mnt/123[root@linux-128 ~]# touch /mnt/12.txt[root@linux-128 ~]# echo '123123' > /mnt/12.txt[root@linux-128 ~]# cat /mnt/12.txt123123
- lvresize -L 300M /dev/vg1/lv1;扩容300M
[root@linux-128 ~]# 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.
[root@linux-128 ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert lv1 vg1 -wi-ao---- 300.00m
4.xfs_growfs /dev/vg1/lv1; xfs文件系统需要执行
[root@linux-128 ~]# 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@linux-128 ~]# df -h文件系统 容量 已用 可用 已用% 挂载点/dev/sda3 16G 1.3G 15G 8% /devtmpfs 907M 0 907M 0% /devtmpfs 916M 0 916M 0% /dev/shmtmpfs 916M 8.6M 908M 1% /runtmpfs 916M 0 916M 0% /sys/fs/cgroup/dev/sda1 197M 109M 88M 56% /boottmpfs 184M 0 184M 0% /run/user/0/dev/mapper/vg1-lv1 297M 5.5M 292M 2% /mnt[root@linux-128 ~]# cat /mnt/12.txt123123
卷组扩容
- 逻辑卷最大扩容到卷组的大小,当逻辑卷扩容到卷组大小的时候,就要想办法给卷组扩容,之前用/dev/sdb1和/dev/sdb2组成的卷组vg1,我们现在见/dev/sdb3加进来
[root@linux-128 ~]# vgextend vg1 /dev/sdb3 Volume group "vg1" successfully extended
[root@linux-128 ~]# vgs VG #PV #LV #SN Attr VSize VFree vg1 3 1 0 wz--n- <2.99g <2.70g
- lvresize -L 100M /dev/vg1/lv1 重新设置卷大小
[root@linux-128 ~]# lvresize -L 500M /dev/vg1/lv1 Size of logical volume vg1/lv1 changed from 300.00 MiB (75 extents) to 500.00 MiB (125 extents). Logical volume vg1/lv1 successfully resized.
[root@linux-128 ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert lv1 vg1 -wi-ao---- 500.00m
磁盘故障 多半是因为磁盘挂载出现问题
- 输入root账号
- vi /etc/fstab 删除最下面一行,保存退出 3 重启服务器