LVM管理
一、部署lvm
物理卷(PV):把常规的块设备(硬盘,分区等可以读写数据的设备)通过pvcreate命令对其进行初始化,就成了物理卷
卷组(VG):把多个物理卷的容量组成一个逻辑整体,可以从里面灵活分配容量
逻辑卷(LV):从卷组中划分部分空间成为一个可以读写数据的逻辑单元。需要对其格式化然后挂载使用
创建lvm步骤:
a) 添加物理磁盘,创建物理卷
b) 创建卷组,将物理卷加入卷组
c) 在卷组中划分逻辑卷
d) 格式化逻辑卷
e) 挂载使用
//准备物理卷
[root@yy ~]# pvcreate /dev/sd{b,c}
Physical volume "/dev/sdb" successfully created.
Physical volume "/dev/sdc" successfully created.
[root@yy ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 cl lvm2 a-- <99.00g 0
/dev/sdb lvm2 --- 5.00g 5.00g
/dev/sdc lvm2 --- 5.00g 5.00g
//创建卷组
[root@yy ~]# vgcreate vgyy0 /dev/sd{b,c}
Volume group "vgyy0" successfully created
[root@yy ~]# vgs
VG #PV #LV #SN Attr VSize VFree
cl 1 3 0 wz--n- <99.00g 0
vgyy0 2 0 0 wz--n- 9.99g 9.99g
//创建逻辑卷
[root@yy ~]# lvcreate -n lvyy0 -L 6G vgyy0
Logical volume "lvyy0" created.
[root@yy ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
home cl -wi-ao---- 31.18g
root cl -wi-ao---- <63.88g
swap cl -wi-ao---- <3.94g
lvyy0 vgyy0 -wi-a----- 6.00g
//格式化逻辑卷
[root@yy ~]# mkfs.ext4 /dev/vgyy0/lvyy0
mke2fs 1.45.6 (20-Mar-2020)
Creating filesystem with 1572864 4k blocks and 393216 inodes
Filesystem UUID: e05f56fb-11e4-49e0-a2e9-fa3f45cd2840
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736
Allocating group tables: done
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done
//创建挂载点
[root@yy ~]# mkdir /mnt/yy0
//查看新建逻辑卷的uuid
[root@yy ~]# blkid /dev/vgyy0/lvyy0
/dev/vgyy0/lvyy0: UUID="e05f56fb-11e4-49e0-a2e9-fa3f45cd2840" BLOCK_SIZE="4096" TYPE="ext4"
//永久挂载
[root@yy ~]# vi /etc/fstab
UUID="e05f56fb-11e4-49e0-a2e9-fa3f45cd2840" /mnt/yy0 ext4 defaults 0 0
//重新加载挂载并查看
[root@yy ~]# mount -a
[root@yy ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 2.0G 0 2.0G 0% /dev
tmpfs tmpfs 2.0G 0 2.0G 0% /dev/shm
tmpfs tmpfs 2.0G 9.4M 2.0G 1% /run
tmpfs tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
/dev/mapper/cl-root xfs 69G 2.1G 67G 4% /
/dev/mapper/cl-home xfs 34G 268M 34G 1% /home
/dev/sda1 xfs 1.1G 224M 840M 22% /boot
tmpfs tmpfs 389M 0 389M 0% /run/user/0
/dev/mapper/vgyy0-lvyy0 ext4 6.3G 26M 6.0G 1% /mnt/yy0
2.扩展逻辑卷大小
扩展逻辑卷:ext格式、xfs格式都可以
裁剪逻辑卷:只有ext格式
[root@yy ~]# lvs | grep lvyy0
lvyy0 vgyy0 -wi-ao---- 6.00g
[root@yy ~]# lvextend -L 7G /dev/vgyy0/lvyy0 //扩展逻辑卷到7G,+7G是在原有的基础上加上7G
Size of logical volume vgyy0/lvyy0 changed from 6.00 GiB (1536 extents) to 7.00 GiB (1792 extents).
Logical volume vgyy0/lvyy0 successfully resized.
[root@yy ~]# resize2fs /dev/vgyy0/lvyy0 //ext格式扩展用resize2fs,xfs格式扩展用xfs_growfs
resize2fs 1.45.6 (20-Mar-2020)
Filesystem at /dev/vgyy0/lvyy0 is mounted on /mnt/yy0; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/vgyy0/lvyy0 is now 1835008 (4k) blocks long.
[root@yy ~]# lvs | grep lvyy0
lvyy0 vgyy0 -wi-ao---- 7.00g
三、裁剪逻辑卷
[root@yy ~]# umount /mnt/yy0 //取消挂载
[root@yy ~]# e2fsck -f /dev/vgyy0/lvyy0 //检查逻辑卷文件系统
e2fsck 1.45.6 (20-Mar-2020)
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/vgyy0/lvyy0: 11/458752 files (0.0% non-contiguous), 52095/1835008 blocks
[root@yy ~]# lvreduce -L 3G /dev/vgyy0/lvyy0 //裁剪逻辑卷到3G
WARNING: Reducing active logical volume to 3.00 GiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vgyy0/lvyy0? [y/n]: y
Size of logical volume vgyy0/lvyy0 changed from 7.00 GiB (1792 extents) to 3.00 GiB (768 extents).
Logical volume vgyy0/lvyy0 successfully resized.
[root@yy ~]# e2fsck -f /dev/vgyy0/lvyy0 //再次检查逻辑卷文件系统
e2fsck 1.45.6 (20-Mar-2020)
The filesystem size (according to the superblock) is 1835008 blocks
The physical size of the device is 786432 blocks
Either the superblock or the partition table is likely to be corrupt!
Abort<y>? yes
[root@yy ~]# mount -a //重新加载挂载
mount: /mnt/yy0: wrong fs type, bad option, bad superblock on /dev/mapper/vgyy0-lvyy0, missing codepage or helper program, or other error.
[root@yy ~]# lvs |grep lvyy0 //查看裁剪后逻辑卷大小
lvyy0 vgyy0 -wi-a----- 3.00g
四、删除逻辑卷LV,卷组VG、物理卷PV
//取消挂载
[root@yy ~]# umount /mnt/yy0/
umount: /mnt/yy0/: not mounted.
//删除逻辑卷
[root@yy ~]# lvremove /dev/vgyy0/lvyy0
Do you really want to remove active logical volume vgyy0/lvyy0? [y/n]: y
Logical volume "lvyy0" successfully removed.
[root@yy ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
home cl -wi-ao---- 31.18g
root cl -wi-ao---- <63.88g
swap cl -wi-ao---- <3.94g
//删除卷组
[root@yy ~]# vgremove vgyy0
Volume group "vgyy0" successfully removed
[root@yy ~]# vgs
VG #PV #LV #SN Attr VSize VFree
cl 1 3 0 wz--n- <99.00g 0
//删除物理卷
[root@yy ~]# pvremove /dev/sd{b,c}
Labels on physical volume "/dev/sdb" successfully wiped.
Labels on physical volume "/dev/sdc" successfully wiped.
[root@yy ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 cl lvm2 a-- <99.00g 0
五、数据迁移
[root@yy ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 cl lvm2 a-- <99.00g 0
/dev/sdb yyvg lvm2 a-- <5.00g <2.00g
/dev/sdc yyvg lvm2 a-- <5.00g <5.00g
[root@yy ~]# pvmove /dev/sdb
/dev/sdb: Moved: 2.73%
/dev/sdb: Moved: 100.00%
[root@yy ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 cl lvm2 a-- <99.00g 0
/dev/sdb yyvg lvm2 a-- <5.00g <5.00g
/dev/sdc yyvg lvm2 a-- <5.00g <2.00g
六、怎么从卷组中删除pv物理卷
[root@yy ~]# vgs
VG #PV #LV #SN Attr VSize VFree
cl 1 3 0 wz--n- <99.00g 0
yyvg 2 0 0 wz--n- 9.99g 9.99g
[root@yy ~]# vgreduce yyvg /dev/sdb
Removed "/dev/sdb" from volume group "yyvg"
[root@yy ~]# vgs
VG #PV #LV #SN Attr VSize VFree
cl 1 3 0 wz--n- <99.00g 0
yyvg 1 0 0 wz--n- <5.00g <5.00g
Linux计划任务管理课后作业
1.在linux系统中备份脚本backup.sh需要再每周1-5的每天下午1点和晚上8点执行,下列哪个cron命令可以完成(D)
a. 00 13,20 * 1-5 * backup.sh //每年的1~5月的每天的下午1点和晚上8点执行backup.sh
b. 0 13,20 1,5 * * backup.sh //每月的1号和5号的下午1点和晚上8点执行backup.sh
c. * 13,20 * * 1-5 backup.sh //每周一到周五的下午一点中的每一分钟和晚上八点的每一分钟执行backup.sh备份脚本
d. 00 13,20 * * 1-5 backup.sh //每周一至周五的下午1点和晚上8点执行backup.sh备份脚本
2.新建/scripts/httpd.sh文件,并让/scripts/httpd.sh脚本在每天的00:10分执行
[root@yy ~]# crontab -e
crontab: installing new crontab
[root@yy ~]# crontab -l
10 00 * * * /bin/bash /scripts/httpd.sh
3.新建/backup目录,每周一下午5:50将/backup目录下的所有文件打包成 backup.tar.gz
[root@yy ~]# mkdir /backup
[root@yy ~]# which tar
/usr/bin/tar
[root@yy ~]# crontab -e
crontab: installing new crontab
[root@yy ~]# crontab -l
50 17 * * 1 /usr/bin/tar -czf backup.tar.gz /backup
4.写一个定时任务,每天0点5分把/var/log/nginx下7天前的文件转移到/backup/2018_xx_xx的目录中
[root@yy ~]# which mv
alias mv='mv -i'
/usr/bin/mv
[root@yy ~]# crontab -e
crontab: installing new crontab
[root@yy ~]# crontab -l
5 0 * * * /usr/bin/find /var/log/nginx -name "*" -utime +7 -type f -exec /usr/bin/mv {} /backup/$(date +"%Y-%m-%d")/
5.系统脚本/scripts/which.sh,如何定时每隔7分钟执行一次。
[root@yy ~]# crontab -e
crontab: installing new crontab
[root@yy ~]# crontab -l
*/7 * * * * /bin/bash /scripts/which.sh
6.如何不小心删除了/var/spool/cron/root文件,该如何恢复。
[root@yy ~]# cat /var/log/cron | grep "CMD"
Jul 18 15:01:01 localhost CROND[2020]: (root) CMD (run-parts /etc/cron.hourly)
Jul 18 16:01:01 localhost CROND[2843]: (root) CMD (run-parts /etc/cron.hourly)
Jul 18 16:14:01 localhost CROND[2873]: (root) CMD (/bin/bash /scripts/which.sh)
Jul 18 16:14:01 localhost CROND[2871]: (root) CMDOUT (/bin/bash: /scripts/which.sh: No such file or directory)
Jul 18 16:21:01 localhost CROND[2882]: (root) CMD (/bin/bash /scripts/which.sh)
Jul 18 16:21:01 localhost CROND[2880]: (root) CMDOUT (/bin/bash: /scripts/which.sh: No such file or directory)
Jul 18 16:28:01 localhost CROND[2896]: (root) CMD (/bin/bash /scripts/which.sh)
Jul 18 16:28:01 localhost CROND[2894]: (root) CMDOUT (/bin/bash: /scripts/which.sh: No such file or directory)
/scripts/which.sh: No such file or directory)
Jul 18 16:28:01 localhost CROND[2896]: (root) CMD (/bin/bash /scripts/which.sh)
Jul 18 16:28:01 localhost CROND[2894]: (root) CMDOUT (/bin/bash: /scripts/which.sh: No such file or directory)