LVM管理与计划任务管理

本文介绍了Linux系统的LVM管理,包括部署LVM、卷组管理与逻辑卷管理,详细阐述了各步骤及注意事项。此外,还讲解了计划任务管理,提供了多个cron命令示例,用于设置不同频率和时间的脚本执行。
摘要由CSDN通过智能技术生成

LVM管理与计划任务管理

lvm管理

部署lvm

创建lvm步骤:
a) 添加物理磁盘,创建物理卷
b) 创建卷组,将物理卷加入卷组
c) 在卷组中划分逻辑卷
d) 格式化逻辑卷
e) 挂载使用

//1.准备物理磁盘
[root@centos8 ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0  100G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   99G  0 part 
  ├─cs_192-root 253:0    0 65.2G  0 lvm  /
  ├─cs_192-swap 253:1    0    2G  0 lvm  [SWAP]
  └─cs_192-home 253:2    0 31.8G  0 lvm  /home
sdb               8:16   0   20G  0 disk 
sdc               8:32   0   20G  0 disk 
sdd               8:48   0   20G  0 disk 
sr0              11:0    1 10.3G  0 rom
//1.将磁盘加入pv
[root@centos8 ~]# pvcreate /dev/sdb
  Physical volume "/dev/sdb" successfully created.
[root@centos8 ~]# pvs
  PV         VG     Fmt  Attr PSize   PFree 
  /dev/sda2  cs_192 lvm2 a--  <99.00g     0 
  /dev/sdb          lvm2 ---   20.00g 20.00g
//2.创建名为wjh的卷组
[root@centos8 ~]# vgcreate wjh /dev/sdb
  Volume group "wjh" successfully created
[root@centos8 ~]# vgs
  VG     #PV #LV #SN Attr   VSize   VFree  
  cs_192   1   3   0 wz--n- <99.00g      0 
  wjh      1   0   0 wz--n- <20.00g <20.00g

//3.创建逻辑卷, 分配名称, 以及大小, 指定卷组
[root@centos8 ~]# lvcreate -L 100M -n  lv1 wjh
  Logical volume "lv1" created.
[root@centos8 ~]# lvs                                                  
  lv1  wjh    -wi-a----- 100.00m                                                    
//4.格式化文件系统
[root@centos8 ~]# mkfs.ext4 /dev/wjh/lv1 
mke2fs 1.45.6 (20-Mar-2020)
Creating filesystem with 102400 1k blocks and 25688 inodes
Filesystem UUID: 0a9245f4-9965-480c-b536-8bffef3dd538
Superblock backups stored on blocks: 
	8193, 24577, 40961, 57345, 73729

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done 

//挂在并使用
[root@centos8 ~]# mkdir /lv1
[root@centos8 ~]# blkid /dev/wjh/lv1
/dev/wjh/lv1: UUID="0a9245f4-9965-480c-b536-8bffef3dd538" BLOCK_SIZE="1024" TYPE="ext4"
[root@centos8 ~]# vi /etc/fstab 
[root@centos8 ~]# cat /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Mon Jun 27 11:49:55 2022
#
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
/dev/mapper/cs_192-root /                       xfs     defaults        0 0
UUID=07e367cc-e7f5-49ae-ae77-1c310cbcb3c7 /boot                   xfs     defaults        0 0
/dev/mapper/cs_192-home /home                   xfs     defaults        0 0
/dev/mapper/cs_192-swap none                    swap    defaults        0 0
UUID="0a9245f4-9965-480c-b536-8bffef3dd538" /lv1 ext4 defaults 0 0

[root@centos8 ~]# df -Th
Filesystem              Type      Size  Used Avail Use% Mounted on
...
/dev/mapper/wjh-lv1     ext4       93M  1.6M   85M   2% /lv1

卷组管理

  • 扩展卷组,将新磁盘加入卷组
//1.新硬盘加入pv
[root@centos8 ~]# pvcreate /dev/sdc
  Physical volume "/dev/sdc" successfully created.
  
2.使用vgextend扩展
[root@centos8 ~]# vgextend wjh /dev/sdc
  Volume group "wjh" successfully extended
[root@centos8 ~]# vgs
  VG     #PV #LV #SN Attr   VSize   VFree 
  cs_192   1   3   0 wz--n- <99.00g     0 
  wjh      2   1   0 wz--n-  39.99g 39.89g
  
//缩减卷组,将指定磁盘从卷组中删除
[root@centos8 ~]# vgreduce wjh /dev/sdc
  Removed "/dev/sdc" from volume group "wjh"
[root@centos8 ~]# vgs
  VG     #PV #LV #SN Attr   VSize   VFree  
  cs_192   1   3   0 wz--n- <99.00g      0 
  wjh      1   1   0 wz--n- <20.00g <19.90g

//数据迁移卷组,同一卷组的磁盘才可以进行在线迁移
[root@centos8 ~]# pvs
  PV         VG     Fmt  Attr PSize   PFree  
  /dev/sda2  cs_192 lvm2 a--  <99.00g      0 
  /dev/sdb   wjh    lvm2 a--  <20.00g <19.90g
  /dev/sdc   wjh    lvm2 a--  <20.00g <20.00g
[root@centos8 ~]# pvmove /dev/sdb
  /dev/sdb: Moved: 100.00%
  
  //效果展示
[root@centos8 ~]# pvs
  PV         VG     Fmt  Attr PSize   PFree  
  /dev/sda2  cs_192 lvm2 a--  <99.00g      0 
  /dev/sdb   wjh    lvm2 a--  <20.00g <20.00g
  /dev/sdc   wjh    lvm2 a--  <20.00g <19.90g

逻辑卷管理

  • 逻辑卷扩展,逻辑卷的扩展取决于卷组中的容量,逻辑卷扩展的容量不能超过卷组的容量
1.扩展lv逻辑卷
[root@centos8 ~]# lvs
  LV   VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  home cs_192 -wi-ao----  31.81g                                                    
  root cs_192 -wi-ao---- <65.16g                                                    
  swap cs_192 -wi-ao----  <2.03g                                                    
  lv1  wjh    -wi-ao---- 100.00m
  
  //将逻辑卷提升到800M 
[root@centos8 ~]# lvextend -L 800M /dev/wjh/lv1 
  Size of logical volume wjh/lv1 changed from 100.00 MiB (25 extents) to 800.00 MiB (200 extents).
  Logical volume wjh/lv1 successfully resized.
[root@centos8 ~]# lvs
  LV   VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  home cs_192 -wi-ao----  31.81g                                                    
  root cs_192 -wi-ao---- <65.16g                                                    
  swap cs_192 -wi-ao----  <2.03g                                                    
  lv1  wjh    -wi-ao---- 800.00m                                                  
  //增加800M分配给逻辑卷
[root@centos8 ~]# lvextend -L +800M /dev/wjh/lv1 
  Size of logical volume wjh/lv1 changed from 800.00 MiB (200 extents) to 1.56 GiB (400 extents).
  Logical volume wjh/lv1 successfully resized.
[root@centos8 ~]# lvs
  LV   VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  home cs_192 -wi-ao----  31.81g                                                    
  root cs_192 -wi-ao---- <65.16g                                                    
  swap cs_192 -wi-ao----  <2.03g                                                    
  lv1  wjh    -wi-ao----   1.56g                                                    
2.扩展xfs文件系统
//xfs扩容
[root@haha ~]# xfs_growfs /dev/wjh/lv1
//ext扩容
[root@centos8 ~]# resize2fs /dev/wjh/lv1 
resize2fs 1.45.6 (20-Mar-2020)
Filesystem at /dev/wjh/lv1 is mounted on /lv1; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 13
The filesystem on /dev/wjh/lv1 is now 1638400 (1k) blocks long.

//对ext4文件系统的逻辑卷裁剪容量
首先自己创建一个逻辑卷作为裁剪的对象
[root@centos8 ~]# lvcreate -n lv2 -L 1G wjh
WARNING: ext4 signature detected on /dev/wjh/lv2 at offset 1080. Wipe it? [y/n]: y
  Wiping ext4 signature on /dev/wjh/lv2.
  Logical volume "lv2" created.
[root@centos8 ~]# lvs
  LV   VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  home cs_192 -wi-ao----  31.81g                                                    
  root cs_192 -wi-ao---- <65.16g                                                    
  swap cs_192 -wi-ao----  <2.03g                                                    
  lv1  wjh    -wi-ao----   1.56g                                                    
  lv2  wjh    -wi-a-----   1.00g                                                    
[root@centos8 ~]# mkfs.ext4 /dev/wjh/lv2
mke2fs 1.45.6 (20-Mar-2020)
Creating filesystem with 262144 4k blocks and 65536 inodes
Filesystem UUID: 7090a6b0-2235-4e1e-a555-0cc93ba3d37a
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

[root@centos8 ~]# mkdir /lv2
[root@centos8 ~]# blkid /dev/wjh/lv2
/dev/wjh/lv2: UUID="7090a6b0-2235-4e1e-a555-0cc93ba3d37a" BLOCK_SIZE="4096" TYPE="ext4"
[root@centos8 ~]# vi /etc/fstab 
[root@centos8 ~]# cat /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Mon Jun 27 11:49:55 2022
#
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
/dev/mapper/cs_192-root /                       xfs     defaults        0 0
UUID=07e367cc-e7f5-49ae-ae77-1c310cbcb3c7 /boot                   xfs     defaults        0 0
/dev/mapper/cs_192-home /home                   xfs     defaults        0 0
/dev/mapper/cs_192-swap none                    swap    defaults        0 0
UUID="0a9245f4-9965-480c-b536-8bffef3dd538" /lv1 ext4 defaults 0 0 
r 
UUID="7090a6b0-2235-4e1e-a555-0cc93ba3d37a" /lv2 ext4 defaults 0 0 
[root@centos8 ~]# mount -a
[root@centos8 ~]# df -hT 
Filesystem              Type      Size  Used Avail Use% Mounted on
devtmpfs                devtmpfs  874M     0  874M   0% /dev
tmpfs                   tmpfs     893M     0  893M   0% /dev/shm
tmpfs                   tmpfs     893M  8.7M  885M   1% /run
tmpfs                   tmpfs     893M     0  893M   0% /sys/fs/cgroup
/dev/mapper/cs_192-root xfs        66G  2.2G   64G   4% /
/dev/sda1               xfs      1014M  211M  804M  21% /boot
/dev/mapper/cs_192-home xfs        32G  260M   32G   1% /home
tmpfs                   tmpfs     179M     0  179M   0% /run/user/0
/dev/mapper/wjh-lv1     ext4      1.6G  2.7M  1.5G   1% /lv1
/dev/mapper/wjh-lv2     ext4      976M  2.6M  907M   1% /lv2

1、如果已经挂载,必须先卸载
root@centos8 ~]# umount /lv2

2、裁剪容量,必须是先检测文件系统
[root@centos8 ~]# e2fsck -f /dev/wjh/lv2 
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/wjh/lv2: 11/260096 files (0.0% non-contiguous), 43846/1048576 blocks
[root@centos8 ~]# resize2fs /dev/wjh/lv2 555M
resize2fs 1.45.6 (20-Mar-2020)
Resizing the filesystem on /dev/wjh/lv2 to 568320 (1k) blocks.
The filesystem on /dev/wjh/lv2 is now 568320 (1k) blocks long.

3、调整完毕后采取裁剪逻辑卷容量
[root@centos8 ~]# lvreduce -L 555M /dev/wjh/lv2 
  Rounding size to boundary between physical extents: 556.00 MiB.
  WARNING: Reducing active logical volume to 556.00 MiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce wjh/lv2? [y/n]: y
  Size of logical volume wjh/lv2 changed from 1.00 GiB (256 extents) to 556.00 MiB (139 extents).
  Logical volume wjh/lv2 successfully resized.  

4、强烈建议裁剪后,再次检测文件系统
[root@centos8 ~]# e2fsck -f /dev/wjh/lv2 
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/wjh/lv2: 11/142240 files (0.0% non-contiguous), 28476/568320 blocks

5、挂载测试
如果能够挂载,一般说明裁剪成功,文件系统没有损坏
[root@centos8 ~]# mount -a
[root@centos8 ~]# df -hT 
Filesystem              Type      Size  Used Avail Use% Mounted on
devtmpfs                devtmpfs  874M     0  874M   0% /dev
tmpfs                   tmpfs     893M     0  893M   0% /dev/shm
tmpfs                   tmpfs     893M  8.7M  885M   1% /run
tmpfs                   tmpfs     893M     0  893M   0% /sys/fs/cgroup
/dev/mapper/cs_192-root xfs        66G  2.1G   64G   4% /
/dev/sda1               xfs      1014M  211M  804M  21% /boot
/dev/mapper/cs_192-home xfs        32G  260M   32G   1% /home
tmpfs                   tmpfs     179M     0  179M   0% /run/user/0
/dev/mapper/wjh-lv2     ext4      530M  2.3M  499M   1% /lv2



删除逻辑卷lv0、删除卷组vg0、删除物理卷

缩减逻辑卷注意事项:

  1. 不能在线缩减,得先卸载;
  2. 确保缩减后的空间大小依然能存储原有的所有数据;
  3. 在缩减之前应该先强行检查文件,以确保文件系统处于一致性状态。使用命令e2fsck -f /PATH/TO/LV
root@centos8 ~]# umount /lv2
[root@centos8 ~]# lvremove /dev/wjh/lv2
Do you really want to remove active logical volume wjh/lv2? [y/n]: y
  Logical volume "lv2" successfully removed.
[root@centos8 ~]# vgremove wjh
  Volume group "wjh" successfully removed
[root@centos8 ~]# pvremove  /dev/sd{b,c,d}
[root@centos8 ~]# lvs

  LV   VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  home cs_192 -wi-ao----  31.81g                                                    
  root cs_192 -wi-ao---- <65.16g                                                    
  swap cs_192 -wi-ao----  <2.03g                                                    
[root@centos8 ~]# vgs
  VG     #PV #LV #SN Attr   VSize   VFree
  cs_192   1   3   0 wz--n- <99.00g    0
计划任务管理

1.在linux系统中备份脚本backup.sh需要再每周1-5的每天下午1点和晚上8点执行,下列哪个cron命令可以完成(D)

a. 00 13,20 * 1-5 * backup.sh //每年1-5月下午1点和8点执行
b. 0 13,20 1,5 * * backup.sh //每月的1和5号下午1点和8点执行
c. * 13,20 * * 1-5 backup.sh //每周的1到星期五下午1点和8点每分钟执行
d. 00 13,20 * * 1-5 backup.sh //每周1-5的每天下午1点和晚上8点

说明以上答案每一行是什么含义

2.新建/scripts/httpd.sh文件,并让/scripts/httpd.sh脚本在每天的00:10分执行

[root@centos8 ~]# mkdir -p /sripts/httpd.sh
[root@centos8 ~]# crontab -e
crontab: installing new crontab
[root@centos8 ~]# crontab -l
10 00 * * * /bin/bash -x /scripts/httpd.sh &> /dev/null

3.新建/backup目录,每周一下午5:50将/backup目录下的所有文件打包成
backup.tar.gz

[root@centos8 ~]# mkdir backup
[root@centos8 ~]# crontab -e
crontab: installing new crontab
[root@centos8 ~]# crontab -l
##scripts/httpd.sh脚本在每天的00:10分执行
10 00 * * * /bin/bash -x /scripts/httpd.sh &> /dev/null

## 每周一下午5:50将/backup目录下的所有文件打包成 backup.tar.gz

50 5 * * 1 /usr/bin/tar -zcf backup.tar.gz /root/backup

4.写一个定时任务,每天0点5分把/var/log/nginx下7天前的文件转移
到/backup/2018_xx_xx的目录中

[root@centos8 ~]# crontab -e
crontab: installing new crontab
[root@centos8 ~]# crontab -l
##scripts/httpd.sh脚本在每天的00:10分执行
10 00 * * * /bin/bash -x /scripts/httpd.sh &> /dev/null

## 每周一下午5:50将/backup目录下的所有文件打包成 backup.tar.gz

50 5 * * 1 /usr/bin/tar -zcf backup.tar.gz /root/backup

##每天0点5分把/var/log/nginx下7天前的文件转移到/backup/2018_xx_xx的目录中
05 00 * * * /usr/bin/find /var/log/nginx -name "*" -mtime +7 -type f -exec mv {} /backup/$(date +%Y-%m-%d)/ \;

5.系统脚本/scripts/which.sh,如何定时每隔7分钟执行一次?

[root@centos8 ~]# crontab -e
crontab: installing new crontab
[root@centos8 ~]# crontab -l
##scripts/httpd.sh脚本在每天的00:10分执行
10 00 * * * /bin/bash -x /scripts/httpd.sh &> /dev/null

## 每周一下午5:50将/backup目录下的所有文件打包成 backup.tar.gz

50 5 * * 1 /usr/bin/tar -zcf backup.tar.gz /root/backup

##每天0点5分把/var/log/nginx下7天前的文件转移到/backup/2018_xx_xx的目录中
05 00 * * * /usr/bin/find /var/log/nginx -name "*" -mtime +7 -type f -exec mv {} /backup/$(date +%Y-%m-%d)/ \;

##系统脚本/scripts/which.sh,如何定时每隔7分钟执行一次
*/7 * * * * /bin/bash -x /scripts/which.sh


6.如何不小心删除了/var/spool/cron/root文件,该如何恢复。

[root@centos8 ~]# cat /var/log/cron* | grep CMD |head
Jul 18 16:01:01 centos8 CROND[3585]: (root) CMD (run-parts /etc/cron.hourly)
Jul 18 17:01:01 centos8 CROND[3912]: (root) CMD (run-parts /etc/cron.hourly)
Jul 18 18:01:01 centos8 CROND[3960]: (root) CMD (run-parts /etc/cron.hourly)
Jun 28 10:01:01 centos8 CROND[1752]: (root) CMD (run-parts /etc/cron.hourly)
Jun 28 11:01:01 centos8 CROND[1829]: (root) CMD (run-parts /etc/cron.hourly)
Jun 28 14:01:01 centos8 CROND[1690]: (root) CMD (run-parts /etc/cron.hourly)
Jul 18 15:01:01 centos8 CROND[2160]: (root) CMD (run-parts /etc/cron.hourly)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值