存储管理
一、创建LVM
[root@localhost ~]# lsblk 查看磁盘
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 19G 0 part
├─centos-root 253:0 0 17G 0 lvm /
└─centos-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 2G 0 disk
sdc 8:32 0 2G 0 disk
sdd 8:48 0 2G 0 disk
sde 8:64 0 2G 0 disk
sdf 8:80 0 2G 0 disk
sr0 11:0 1 4.2G 0 rom
[root@localhost ~]# pvcreate /dev/sdb 创建物理卷
Physical volume "/dev/sdb" successfully created.
您在 /var/spool/mail/root 中有新邮件
[root@localhost ~]# vgcreate vg1 /dev/sdb 创建卷组
Volume group "vg1" successfully created
[root@localhost ~]# lvcreate -l 100 -n lv1 vg1 创建逻辑卷
Logical volume "lv1" created.
[root@localhost ~]# mkfs.ext4 /dev/vg1/lv1
mke2fs 1.42.9 (28-Dec-2013)
文件系统标签=
OS type: Linux
块大小=1024 (log=0)
分块大小=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
102400 inodes, 409600 blocks
20480 blocks (5.00%) reserved for the super user
第一个数据块=1
Maximum filesystem blocks=34078720
50 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409
Allocating group tables: 完成
正在写入inode表: 完成
Creating journal (8192 blocks): 完成
Writing superblocks and filesystem accounting information: 完成
[root@localhost ~]# mkdir /mnt/lv1
方法1:[root@localhost ~]# mount /dev/vg1/lv1 /mnt/lv1 临时挂载
方法2:[root@localhost ~]# blkid /dev/sdb
/dev/sdb: UUID="2fMoPU-7knV-ThUO-meuw-gTLE-W1Kh-jSyzwq" TYPE="LVM2_member"
[root@localhost ~]# vim /etc/fstab
UUID=00d83adf-074f-4820-960f-dcfa0ea8d9d5 /mnt/lv1 ext4 defaults 0 0
[root@localhost ~]# df -hT 查看挂载点
文件系统 类型 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root xfs 17G 4.3G 13G 26% /
devtmpfs devtmpfs 473M 0 473M 0% /dev
tmpfs tmpfs 489M 0 489M 0% /dev/shm
tmpfs tmpfs 489M 7.2M 482M 2% /run
tmpfs tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/sda1 xfs 1014M 161M 854M 16% /boot
tmpfs tmpfs 98M 12K 98M 1% /run/user/42
tmpfs tmpfs 98M 0 98M 0% /run/user/0
/dev/mapper/vg1-lv1 ext4 380M 2.3M 354M 1% /mnt/lv1
二、创建快照
[root@localhost ~]# lvcreate -L 200M -s -n lv1-snap /dev/vg1/lv1 创建快照卷 s代表快照 n代表命名
Using default stripesize 64.00 KiB.
Logical volume "lv1-snap" created.
[root@localhost ~]# lvs 查看两个卷的情况
LV VG Attr LSize Pool Origin Data% Meta% Move Log
Cpy%Sync Convert
root centos -wi-ao---- <17.00g
swap centos -wi-ao---- 2.00g
lv1 vg1 owi-aos--- 400.00m
lv1-snap vg1 swi-a-s--- 200.00m lv1 0.01
[root@localhost ~]# mkdir /mnt/lv1-snap
[root@localhost ~]# mount -o ro /dev/vg1/lv1-snap /mnt/lv1-snap
[root@localhost ~]# df -hT
文件系统 类型 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root xfs 17G 4.3G 13G 26% /
devtmpfs devtmpfs 473M 0 473M 0% /dev
tmpfs tmpfs 489M 0 489M 0% /dev/shm
tmpfs tmpfs 489M 7.2M 481M 2% /run
tmpfs tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/sda1 xfs 1014M 161M 854M 16% /boot
tmpfs tmpfs 98M 12K 98M 1% /run/user/42
tmpfs tmpfs 98M 0 98M 0% /run/user/0
/dev/mapper/vg1-lv1 ext4 380M 2.3M 354M 1% /mnt/lv1
/dev/mapper/vg1-lv1--snap ext4 380M 2.3M 354M 1% /mnt/lv1-snap
[root@localhost ~]# ls /mnt/lv1 查看文件内容
lost+found
[root@localhost ~]# ls /mnt/lv1-snap
lost+found
>>取消挂载不成功的做法:
fuser -m /dev/sdb
/dev/sdb:3052c
kill -9 3052
umount /dev/sdb
TIM截图20180427195652.png
三、扩容VG
[root@localhost ~]# pvcreate /dev/sdc
Physical volume "/dev/sdc" successfully created.
[root@localhost ~]# vgextend vg1 /dev/sdc
Volume group "vg1" successfully extended
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz--n- <19.00g 0
vg1 2 1 0 wz--n- 3.99g 3.60g
四、缩减VG
>一般VG有数据的话要进行数据迁移
[root@localhost ~]# pvs 查看vg1是否有数据
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a-- <19.00g 0
/dev/sdb vg1 lvm2 a-- <2.00g <1.61g
/dev/sdc vg1 lvm2 a-- <2.00g <2.00g
[root@localhost ~]# pvmove /dev/sdb 移除数据
/dev/sdb: Moved: 9.00%
/dev/sdb: Moved: 100.00%
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a-- <19.00g 0
/dev/sdb vg1 lvm2 a-- <2.00g <2.00g
/dev/sdc vg1 lvm2 a-- <2.00g <1.61g
[root@localhost ~]# vgreduce vg1 /dev/sdb 缩减vg1
Removed "/dev/sdb" from volume group "vg1"
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a-- <19.00g 0
/dev/sdb lvm2 --- 2.00g 2.00g
/dev/sdc vg1 lvm2 a-- <2.00g <1.61g
五、减容LV
>>需要先将LV卸载umount 本例子减容至100M
[root@localhost ~]# umount /dev/vg1/lv1
[root@localhost ~]# df -hT
文件系统 类型 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root xfs 17G 4.3G 13G 26% /
devtmpfs devtmpfs 473M 0 473M 0% /dev
tmpfs tmpfs 489M 0 489M 0% /dev/shm
tmpfs tmpfs 489M 7.2M 481M 2% /run
tmpfs tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/sda1 xfs 1014M 161M 854M 16% /boot
tmpfs tmpfs 98M 32K 98M 1% /run/user/0
/dev/sr0 iso9660 4.3G 4.3G 0 100% /run/media/root/CentOS 7 x86_64
[root@localhost ~]# resize2fs -p /dev/vg1/lv1 100M
resize2fs 1.42.9 (28-Dec-2013)
请先运行 'e2fsck -f /dev/vg1/lv1'.
[root@localhost ~]# e2fsck -f /dev/vg1/lv1
e2fsck 1.42.9 (28-Dec-2013)
第一步: 检查inode,块,和大小
第二步: 检查目录结构
第3步: 检查目录连接性
Pass 4: Checking reference counts
第5步: 检查簇概要信息
/dev/vg1/lv1: 11/102400 files (0.0% non-contiguous), 23456/409600 blocks
[root@localhost ~]# resize2fs -p /dev/vg1/lv1 100M
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vg1/lv1 to 102400 (1k) blocks.
Begin pass 2 (max = 8192)
正在重定位块 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Begin pass 3 (max = 50)
正在扫描inode表 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
The filesystem on /dev/vg1/lv1 is now 102400 blocks long.
[root@localhost ~]# lvreduce -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 400.00 MiB (100 extents) to 100.00 MiB (25 extents).
Logical volume vg1/lv1 successfully resized.
[root@localhost ~]# mount /dev/vg1/lv1 /mnt/lv1
[root@localhost ~]# df -hT
文件系统 类型 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root xfs 17G 4.3G 13G 26% /
devtmpfs devtmpfs 473M 0 473M 0% /dev
tmpfs tmpfs 489M 0 489M 0% /dev/shm
tmpfs tmpfs 489M 7.2M 481M 2% /run
tmpfs tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/sda1 xfs 1014M 161M 854M 16% /boot
tmpfs tmpfs 98M 36K 98M 1% /run/user/0
/dev/sr0 iso9660 4.3G 4.3G 0 100% /run/media/root/CentOS 7 x86_64
/dev/mapper/vg1-lv1 ext4 89M 1.6M 81M 2% /mnt/lv1
六、交换分区swap
[root@localhost ~]# fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
Device does not contain a recognized partition table
使用磁盘标识符 0x9328132d 创建新的 DOS 磁盘标签。
命令(输入 m 获取帮助):n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p):
Using default response p
分区号 (1-4,默认 1):
起始 扇区 (2048-4194303,默认为 2048):
将使用默认值 2048
Last 扇区, +扇区 or +size{K,M,G} (2048-4194303,默认为 4194303):1G
值超出范围。
Last 扇区, +扇区 or +size{K,M,G} (2048-4194303,默认为 4194303):+1G
分区 1 已设置为 Linux 类型,大小设为 1 GiB
命令(输入 m 获取帮助):n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): p
分区号 (2-4,默认 2):
起始 扇区 (2099200-4194303,默认为 2099200):
将使用默认值 2099200
Last 扇区, +扇区 or +size{K,M,G} (2099200-4194303,默认为 4194303):+1G
值超出范围。
Last 扇区, +扇区 or +size{K,M,G} (2099200-4194303,默认为 4194303):
将使用默认值 4194303
分区 2 已设置为 Linux 类型,大小设为 1023 MiB
命令(输入 m 获取帮助): t
分区号 (1,2,默认 2):
Hex 代码(输入 L 列出所有代码):82
已将分区“Linux”的类型更改为“Linux swap / Solaris”
命令(输入 m 获取帮助):p
磁盘 /dev/sdb:2147 MB, 2147483648 字节,4194304 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x9328132d
设备 Boot Start End Blocks Id System
/dev/sdb1 2048 2099199 1048576 83 Linux
/dev/sdb2 2099200 4194303 1047552 82 Linux swap / Solaris
命令(输入 m 获取帮助):w
The partition table has been altered!
Calling ioctl() to re-read partition table.
正在同步磁盘。
[root@localhost ~]# partprobe /dev/sdb
[root@localhost ~]# mkswap /dev/sdb2
正在设置交换空间版本 1,大小 = 1047548 KiB
无标签,UUID=e87bc98d-cffc-4d58-807a-057b046346e5
[root@localhost ~]# blkid /dev/sdb2
/dev/sdb2: UUID="e87bc98d-cffc-4d58-807a-057b046346e5" TYPE="swap"
[root@localhost ~]# vim /etc/fstab
[root@localhost ~]# swapon -a
[root@localhost ~]# free -m
total used free shared buff/cache available
Mem: 976 515 168 2 292 278
Swap: 3070 125 2945
七、做raid5
[root@localhost ~]# mdadm -C /dev/md0 -l5 -n3 -x1 /dev/sd{d,e,f,c}
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.
[root@localhost ~]# mkfs.xfs /dev/md0
meta-data=/dev/md0 isize=512 agcount=8, agsize=130944 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=1047552, imaxpct=25
= sunit=128 swidth=256 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=8 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@localhost ~]# mkdir /mnt/raid5
[root@localhost ~]# mount /dev/md0 /mnt/raid5
[root@localhost ~]# cp -rf /etc /mnt/raid5/etc1
[root@localhost ~]# mdadm -D /dev/md0
/dev/md0:
Version : 1.2
Creation Time : Fri Apr 27 21:42:04 2018
Raid Level : raid5
Array Size : 4190208 (4.00 GiB 4.29 GB)
Used Dev Size : 2095104 (2046.00 MiB 2145.39 MB)
Raid Devices : 3
Total Devices : 4
Persistence : Superblock is persistent
Update Time : Fri Apr 27 21:45:51 2018
State : clean
Active Devices : 3
Working Devices : 4
Failed Devices : 0
Spare Devices : 1
Layout : left-symmetric
Chunk Size : 512K
Consistency Policy : resync
Name : localhost.localdomain:0 (local to host localhost.localdomain)
UUID : d9e40c5c:1486f942:2c73e9b2:b9d5bc80
Events : 18
Number Major Minor RaidDevice State
0 8 48 0 active sync /dev/sdd
1 8 64 1 active sync /dev/sde
4 8 80 2 active sync /dev/sdf
3 8 32 - spare /dev/sdc
[root@localhost ~]# mdadm -D -s /dev/md0
ARRAY /dev/md0 metadata=1.2 spares=1 name=localhost.localdomain:0 UUID=d9e40c5c:1486f942:2c73e9b2:b9d5bc80
[root@tianyun ~]# mdadm -D -s > /etc/mdadm.conf设置开机生效
在端口1:
[root@localhost ~]# mdadm /dev/md0 -f /dev/sde -r /dev/sde 模拟一块硬盘损坏,并移除
mdadm: set /dev/sde faulty in /dev/md0
mdadm: hot removed /dev/sde from /dev/md0
在端口2:
[root@localhost~]#watch -n 0.5 'mdadm -D /dev/md0|tail'
Every 0.5s: mdadm -D /dev/md0|tail Fri Apr 27
21:50:56 2018
Consistency Policy : resync
Name : localhost.localdomain:0 (local to host localhost.localdomain)
UUID : d9e40c5c:1486f942:2c73e9b2:b9d5bc80
Events : 38
Number Major Minor RaidDevice State
0 8 48 0 active sync /dev/sdd
3 8 32 1 active sync /dev/sdc
4 8 80 2 active sync /dev/sdf
卸载raid
umount /dev/md0
mdadm -S /dev/md0
rm -rfv /etc/mdadm.conf
TIM截图20180427220412.png