2020.11.09星期一  正式班D25

13.7 LVM

13.7.1 lvm简介

  • lvm介绍

  • 使用lvm的原因

    fdisk、gdisk工具对磁盘分区是把分区大小固定死的

  • lvm优缺点

13.7.2 lvm基本使用

  • 下载安装软件包

    [root@ccc ~]# yum install -y lvm2
    
  • 制作pv:可以对分区做,也可以对整块盘做

    # 1、制作
    [root@ccc ~]# pvcreate /dev/sdb1
    WARNING: xfs signature detected on /dev/sdb1 at offset 0. Wipe it? [y/n]: y
      Wiping xfs signature on /dev/sdb1.
      Physical volume "/dev/sdb1" successfully created.
    [root@ccc ~]# pvcreate /dev/sdb2
      Wiping xfs signature on /dev/sdb2.
      Physical volume "/dev/sdb2" successfully created.
    [root@ccc ~]# pvcreate /dev/sdb3
      Wiping swap signature on /dev/sdb3.
      Physical volume "/dev/sdb3" successfully created.
    
    # 2、查看
    [root@ccc ~]# 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
    [root@ccc ~]# pvscan
      PV /dev/sdb2                      lvm2 [1.00 GiB]
      PV /dev/sdb3                      lvm2 [1.00 GiB]
      PV /dev/sdb1                      lvm2 [1.00 GiB]
      Total: 3 [3.00 GiB] / in use: 0 [0   ] / in no VG: 3 [3.00 GiB]
    
  • 制作vg:将pv划入vg中

    # 制作一个vg1(包含/dev/sdb1和/dev/sdb2两个pv)
    [root@ccc ~]# vgcreate vg1 /dev/sdb1 /dev/sdb2
      Volume group "vg1" successfully created
    [root@ccc ~]# vgs
      VG  #PV #LV #SN Attr   VSize VFree
      vg1   2   0   0 wz--n- 1.99g 1.99g
    
    # 制作vg2(包含/dev/sdb3一个pv)
    [root@ccc ~]# vgcreate vg2 /dev/sdb3
      Volume group "vg2" successfully created
    [root@ccc ~]# vgs
      VG  #PV #LV #SN Attr   VSize    VFree
      vg1   2   0   0 wz--n-    1.99g    1.99g
      vg2   1   0   0 wz--n- 1020.00m 1020.00m
    
  • 创建逻辑卷lvm

    选项

    -L # 逻辑卷大小

    -n # 逻辑卷名字

    # 从vg1中分出来逻辑卷lv1_from_vg1、lv2_from_vg1
    [root@ccc ~]# lvcreate -L 100M -n lv1_from_vg1 vg1
      Logical volume "lv1_from_vg1" created.
    [root@ccc ~]# lvcreate -L 200M -n lv2_from_vg1 vg1
      Logical volume "lv2_from_vg1" created.
    
    # 从vg2中分出来逻辑卷lv1_from_vg2、lv2_from_vg2
    [root@ccc ~]# lvcreate -L 300M -n lv1_from_vg2 vg2
      Logical volume "lv1_from_vg2" created.
    [root@ccc ~]# lvcreate -L 400M -n lv2_from_vg2 vg2
      Logical volume "lv2_from_vg2" created.
    
    # 查看
    [root@ccc ~]# lvs
      LV           VG  Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
      lv1_from_vg1 vg1 -wi-a----- 100.00m
      lv2_from_vg1 vg1 -wi-a----- 200.00m
      lv1_from_vg2 vg2 -wi-a----- 300.00m
      lv2_from_vg2 vg2 -wi-a----- 400.00m
    
  • 格式与挂载

    [root@ccc ~]# mkfs.xfs /dev/vg1/lv1_from_vg1
    [root@ccc ~]# mkfs.xfs /dev/vg1/lv2_from_vg1
    [root@ccc ~]# mkfs.xfs /dev/vg2/lv1_from_vg2
    [root@ccc ~]# mkfs.xfs /dev/vg2/lv2_from_vg2
    
    [root@ccc ~]# mount /dev/vg1/lv1_from_vg1 /test1/
    [root@ccc ~]# mount /dev/vg1/lv2_from_vg1 /test2/
    [root@ccc ~]# mount /dev/vg2/lv1_from_vg2 /test3/
    [root@ccc ~]# mount /dev/vg2/lv2_from_vg2 /test4/
    
    # 查看
    [root@ccc ~]# df
    文件系统                       1K-块    已用    可用 已用% 挂载点
    ...
    /dev/mapper/vg1-lv1_from_vg1   98980    5344   93636    6% /test1
    /dev/mapper/vg1-lv2_from_vg1  201380   10464  190916    6% /test2
    /dev/mapper/vg2-lv1_from_vg2  303780   15584  288196    6% /test3
    /dev/mapper/vg2-lv2_from_vg2  406180   20704  385476    6% /test4
    

13.7.3 在线动态扩容

  • 命令

    lvextend -L [+]MGT /dev/VG_NAME/VL_NAME
    # -L 100M与-L +100M不是一个意思,+100M代表在原有基础上扩容
    
  • 示例

    [root@ccc ~]# lvextend -L +100M /dev/vg1/lv2_from_vg1
      Size of logical volume vg1/lv2_from_vg1 changed from 200.00 MiB (50 extents) to 300.00 MiB (75 extents).
      Logical volume vg1/lv2_from_vg1 successfully resized.
    [root@ccc ~]# lvs
      LV           VG  Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
      lv1_from_vg1 vg1 -wi-ao---- 100.00m
      lv2_from_vg1 vg1 -wi-ao---- 300.00m
      lv1_from_vg2 vg2 -wi-ao---- 300.00m
      lv2_from_vg2 vg2 -wi-ao---- 400.00m
    [root@ccc ~]# xfs_growfs /dev/vg1/lv1_from_vg1   # 扩展逻辑卷后需更新xfs文件系统
    

13.7.4 在线动态缩容与删除

  • !!!!!!!!!!!!!!!不要缩容!!!!!!!!!!!!!

  • xfs不支持缩容

    lvreduce -L [-]MGT /dev/VG_NAME/LV_NAME  # 缩减逻辑卷
    
  • 删除

    # 删除lv之前需先卸载挂载点
    [root@ccc ~]# umount /test1
    [root@ccc ~]# lvremove /dev/vg1/lv1_from_vg1
    Do you really want to remove active logical volume vg1/lv1_from_vg1? [y/n]: y
      Logical volume "lv1_from_vg1" successfully removed
    [root@ccc ~]# umount /test2
    [root@ccc ~]# lvremove /dev/vg1/lv2_from_vg1 -y
      Logical volume "lv2_from_vg1" successfully removed
    
    # 删除vg
    [root@ccc ~]# vgremove vg1
      Volume group "vg1" successfully removed
    
    # 删pv:只能删掉不属于任何vg的pv
    [root@ccc ~]# pvremove /dev/sdb1
      Labels on physical volume "/dev/sdb1" successfully wiped.
    

13.7.5 快照

  • lvm的快照功能采用写时复制技术(Copy-On-Write,COW)

  • 创建快照时不用停止服务即可对数据进行备份

  • 快照本质是特殊的lv,创建快照后会将老数据赋给快照空间

    # 准备初始数据
    [root@ccc ~]# df
    文件系统                       1K-块    已用    可用 已用% 挂载点
    ...
    /dev/mapper/vg1-lv1_from_vg1   98980    5344   93636    6% /test1
    [root@ccc ~]# echo 111 > /test1/1.txt
    
    # 查看vg1容量是否充足
    [root@ccc ~]# vgs
      VG  #PV #LV #SN Attr   VSize   VFree
      vg1   1   1   0 wz--n- <10.00g <9.90g
    
    # 在vg1卷里创建一个lv1_from_vg1的快照
    [root@ccc ~]# lvcreate -L 1G -s -n lv1_from_vg1_snap /dev/vg1/lv1_from_vg1
      Reducing COW size 1.00 GiB down to maximum usable size 104.00 MiB.
      Logical volume "lv1_from_vg1_snap" created.
    
    # 查看
    [root@ccc ~]# lvs
      LV                VG  Attr       LSize   Pool Origin       Data%  Meta%  Move Log Cpy%Sync Convert
      lv1_from_vg1      vg1 owi-aos--- 100.00m
      lv1_from_vg1_snap vg1 swi-a-s--- 104.00m      lv1_from_vg1 0.01
    # 修改文件/test/1.txt
    [root@ccc ~]# echo 11111111 >> /test1/1.txt
    [root@ccc ~]# cat /test1/1.txt
    111
    11111111
    
    # 恢复数据
    # 挂载快照,和原来lvm使用同一个UUID,不被xfs允许,需加选项-o nouuid
    [root@ccc ~]# mount -o nouuid /dev/vg1/lv1_from_vg1_snap /opt/
    [root@ccc ~]# cat /opt/1.txt
    111
    [root@ccc ~]# cp /opt/1.txt /test1/1.txt
    cp:是否覆盖"/test1/1.txt"? y
    [root@ccc ~]# cat /test1/1.txt
    111
    
  • 要恢复的文件个数过多,直接合并

    [root@ccc ~]# mount /dev/vg1/lv1_freom_vg1 /test1
    [root@ccc ~]# echo ccc > /test1/2.txt
    [root@ccc ~]# cat /test1/2.txt
    ccc
    [root@ccc ~]# lvcreate -L 1G -n lv1_from_vg1_snap /dev/vg1/lv1_freom_vg1
      Volume group name expected (no slash)
      Run `lvcreate --help' for more information.
    [root@ccc ~]# lvcreate -L 1G -s -n lv1_from_vg1_snap /dev/vg1/lv1_freom_vg1
      Reducing COW size 1.00 GiB down to maximum usable size 104.00 MiB.
      Logical volume "lv1_from_vg1_snap" created.
    [root@ccc ~]# echo 1111 >> /test1/2.txt
    [root@ccc ~]# echo 1111 >> /test1/2.txt
    [root@ccc ~]# echo 1111 >> /test1/2.txt
    [root@ccc ~]# echo 1111 >> /test1/2.txt
    [root@ccc ~]# cat /test1/2.txt
    ccc
    1111
    1111
    1111
    1111
    [root@ccc ~]# mount -o nouuid /dev/vg1/lv1_from_vg1_snap /opt/
    [root@ccc ~]# cat /opt/2.txt
    ccc
    
    # 先卸载数据源与快照,再进行合并,快照是一次性的会自动删除
    [root@ccc ~]# umount /opt/
    [root@ccc ~]# umount /test1/
    [root@ccc ~]# lvconvert --mergesnapshot /dev/vg1/lv1_from_vg1_snap
      Merging of volume vg1/lv1_from_vg1_snap started.
      vg1/lv1_freom_vg1: Merged: 100.00%
    [root@ccc ~]# mount /dev/vg1/lv1_freom_vg1 /test1
    [root@ccc ~]# cat /test1/2.txt
    ccc
    

14 网络管理

14.1 网卡名字

  • 配置回归传统命名方式

    # 修改网卡配置文件名称
    [root@ccc ~]# cd /etc/sysconfig/network-scripts/
    [root@ccc network-scripts]# mv ifcfg-ens33 ifcfg-eth0
    
    # 修改网卡配置文件设备名称
    [root@ccc network-scripts]# sed -i "s@ens33@eth0@g" ifcfg-eth0
    
    # GRUB添加kernel参数
    [root@ccc network-scripts]# vim /etc/sysconfig/grub
    [root@ccc network-scripts]# tail -2 /etc/sysconfig/grub | head -1
    GRUB_CMDLINE_LINUX="spectre_v2=retpoline rhgb quiet 'net.ifnames=0 biosdevname=0'"
    
    # 加载到引导分区
    [root@ccc network-scripts]# grub2-mkconfig -o /boot/grub2/grub.cfg
    Generating grub configuration file ...
    Found linux image: /boot/vmlinuz-3.10.0-1127.el7.x86_64
    Found initrd image: /boot/initramfs-3.10.0-1127.el7.x86_64.img
    Found linux image: /boot/vmlinuz-0-rescue-d756bc9b1bf84350b002ec5d9e37360d
    Found initrd image: /boot/initramfs-0-rescue-d756bc9b1bf84350b002ec5d9e37360d.img
    done
    
    # 重启系统以生效
    [root@ccc ~]# reboot
    

14.2 基本网络配置

14.2.1 查看网卡信息

  • 查看网卡信息

    # 查看当前系统所连接的所有网卡
    [root@ccc ~]# lspci | grep -i eth
    02:01.0 Ethernet controller: Intel Corporation 82545EM Gigabit Ethernet Controller (Copper) (rev 01)
    
    # 确认网线已经连接好
    [root@ccc ~]# ethtool eth0
    Settings for eth0:
    	Supported ports: [ TP ]
    	Supported link modes:   10baseT/Half 10baseT/Full
    	                        100baseT/Half 100baseT/Full
    	                        1000baseT/Full
    	Supported pause frame use: No
    	Supports auto-negotiation: Yes
    	Supported FEC modes: Not reported
    	Advertised link modes:  10baseT/Half 10baseT/Full
    	                        100baseT/Half 100baseT/Full
    	                        1000baseT/Full
    	Advertised pause frame use: No
    	Advertised auto-negotiation: Yes
    	Advertised FEC modes: Not reported
    	Speed: 1000Mb/s
    	Duplex: Full
    	Port: Twisted Pair
    	PHYAD: 0
    	Transceiver: internal
    	Auto-negotiation: on
    	MDI-X: off (auto)
    	Supports Wake-on: d
    	Wake-on: d
    	Current message level: 0x00000007 (7)
    			       drv probe link
    	Link detected: yes
    [root@ccc ~]# mii-tool eth0
    eth0: negotiated 1000baseT-FD flow-control, link ok
    # link ok指网卡能够被识别,并且接了有效网线
    

14.2.2 ifconfig命令

  • ifconfig命令

    ifconfig -a  									   # 查看所有网卡信息
    ifconfig eth0									   # 查看单个的网卡信息
    ifconfig eth0 192.168.1.122 netmask 255.255.255.0  # 设定临时IP和掩码(重启失效)
    ifconfig eth0 192.168.1.122/24 				  	   # 临时设定IP地址
    ifconfig eth0:1 192.168.0.2 netmask 255.255.255.0  # 配置子接口
    ifconfig eth0:0 down						  	   # 删除
    ifconfig eth0:1 del 192.168.0.2					   # 删除,不加掩码
    
    开启与关闭
    ifconfig eth0 down|up  							   # 不加载网卡配置文件
    ifdown eth0 |ifup eth0  						   # 加载网卡配置文件
    

14.2.3 ping命令

  • ping

    ping 目标IP地址  # ctrl+c结束,测试两台主机网络是否相通
    ping -c 次数 目标IP地址
    
    # 在自己机器上执行,禁止别人ping自己
    [root@ccc ~]# echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all
    

14.2.4 NetworkManager服务

  • 作用:是redhat6自带的检测网络、自动连接网络的图形化工具。

  • NetworkManager服务会干扰网络配置,如DNS经常会被刷掉,因此常关闭。

  • 网卡相关的配置文件/etc/sysconfig/network-scripts/ifcfg-ethX接口的网络配置文件

    # 打开网卡配置文件,完成静态IP配置
    # 修改后重启网络服务systemctl restart network
      1 TYPE=Ethernet
      2 PROXY_METHOD=none
      3 BROWSER_ONLY=no
      4 BOOTPROTO=none									# dhcp动态获取IP
    													# none根据其他选项决定动态或静态
    													# static手动指定IP
      5 DEFROUTE=yes
      6 IPV4_FAILURE_FATAL=no
      7 NAME=eth0
      8 UUID=39e06fd4-3081-4cfe-8937-65fd357b6727
      9 DEVICE=eth0										# 网卡名字
     10 ONBOOT=yes										# yes激活状态no禁用
     11 IPADDR=192.168.29.55							# IP地址
     12 NETMASK=255.255.255.0							# 子网掩码
     13 DNS1=8.8.8.8									# DNS服务器
     14 PREFIX=24
     15 GATEWAY=192.168.29.1							# 默认网关
    

14.2.5 DNS配置文件

  • DNS解析文件/etc/resolv.conf

    [root@ccc ~]# cat /etc/resolv.conf			# 设置DNS指向,最多三个
    # Generated by NetworkManager
    nameserver 8.8.8.8							# 对应网卡配置文件中的配置项DNS
    
  • 本地名称解析文件(优先于DNS)/etc/hosts

    dns优先级
    浏览器dns缓存-->本地系统DNS缓存-->本地计算机HOSTS文件-->ISP DNS缓存-->递归/迭代查询
    

14.2.6 永久设置主机名

  • hostnamectl

  • /etc/hostname

    [root@ccc ~]# hostnamectl set-hostname zzz
    [root@ccc ~]# hostname
    zzz
    [root@ccc ~]# vim /etc/hostname
    

14.2.7 端口与服务的对应关系

  • 对应关系

    [root@ccc ~]# grep '^ftp\ | ^ssh' /etc/services
    
11-10 04:33