磁盘管理,这是运维的基础了,今天有人问到这个,那就说一下(因为给钱了),之前写过一个批量格式化磁盘的脚本:https://www.cnblogs.com/daxionglaiba/p/7512054.html 密码(123456)。这里不做详解。只是简单介绍一下我自己的认识,和大家分享。

常用命令

fdisk 、parted 、df 、du 、mount/umount 、mkfs 等等 

不常用命令

fsck 、blkid、quota 、quotacheck 等等

脚本需求

这里也没有什么可以说的。写一下LVM吧。这里也是已脚本的方式(因为给钱了,第一次写这样的脚本,个人感觉比较傻。)。具体需求如下。

脚本内容

#!/bin/bash
#  -------------+---------------------------------
# * Filename    :       disk_manager.sh
# * Date        :       2020-08-24 
# * Author      :       wangjianxiong           
# * Description :                       
#  -------------+---------------------------------

MYPATH=$(dirname $0)

disk_list()
{
    fdisk -l | grep ^"Disk /dev" | grep -v mapper
}

umount_disk()
{
    fdisk -l | grep ^"Disk /dev" | grep -v mapper |  awk '{print $2}' | tr -d [:] | sort -k2n | awk '{if ($0!=line) print;line=$0}' > $MYPATH/../conf/all_list
    df -h | awk '{print $1}' | grep "/dev/" | grep -v mapper | tr -d [0-9] | sort -k2n | awk '{if ($0!=line) print;line=$0}' > $MYPATH/../conf/mount_list
    pvdisplay | awk '/PV Name/ {print $3}' | tr -d "0-9" > $MYPATH/../conf/vg_disk
    
    cat $MYPATH/../conf/mount_list $MYPATH/../conf/vg_disk | sort | uniq > $MYPATH/../conf/undisk_list

    for i in `cat $MYPATH/../conf/undisk_list`
    do
        sed -i 's#'$i'#EXCLUSIVE#;/EXCLUSIVE/d' $MYPATH/../conf/all_list
    done
}

df_disk()
{
    df -h | grep -v tmpfs
}

kr_vg(){
echo "卷组列表:"
vgdisplay | awk '/VG Name/ {print $3}'
read -p "请输入想要扩容的 [ vg ] : " vgname
echo "未挂载磁盘列表:"
umount_disk
read -p "请输入[ 磁盘 | 分区 ] : " dev_name
fdisk $dev_name<<EOF
n
p
1


t
8e
w
EOF
PV_NAME=`fdisk -l $dev_name | grep ^$dev_name | awk '{print $1}'`
VG_NAME=`vgdisplay | awk '/VG Name/ {print $3}'`
LVS_NAME=`lvdisplay | awk '/LV Name/ {print $3}'`
pvcreate $PV_NAME
vgextend $vgname $PV_NAME
}

kr_lv()
{
echo "逻辑卷列表: "
lvdisplay | awk '/LV Path/ {print $3}'
read -p "请选择需要扩容的逻辑卷 [ LV ]: " kr_lv

echo "最多扩容: "
vgs | awk '{print $NF}'
read -p "请输入需要给逻辑卷扩容多少G :" num_lv

lvextend -L +${num_lv}G $kr_lv
resize2fs $kr_lv
}

lvm_manager()
{
while true
do
    cat $MYPATH/../file/lvm_info
    read -p "请输入需要的操作的序号: " n
    echo -e
    case $n in
       1) vgdisplay
          echo "==========================================="
          vgs
          echo "==========================================="
          ;;
       2) pvdisplay
          echo "=============================================="
          pvs
          echo "=============================================="
          ;;
       3) lvdisplay
          echo "===================================================================================="
          lvs
          echo "===================================================================================="
          ;;
       4) kr_vg
          ;;
       5) kr_lv
          ;;
       6) create_vg
          ;;
       7) break
          ;;
       *) echo "error,again"
          ;;
    esac
done
}

while true
do
    cat $MYPATH/../file/main_menu
    read -p "请输入需要的操作的序号: " n
    echo -e
    case $n in
       1) disk_list
          ;;
       2) umount_disk
          ;;
       3) df_disk
          ;;
       4) lvm_manager
          ;;
       5) exit
          ;;
       *) echo "error,again"
          ;;
    esac
done
View Code