0%

服务器磁盘挂载与扩容

云服务器磁盘挂载与扩容

磁盘挂载

  1. 上传脚本至服务器,命令行运行bash -x init.disk.sh执行脚本,等待脚本运行结束。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    #!/usr/bin/env bash
    ### 自动挂载磁盘
    ### 适用于阿里云服务器自动挂载磁盘

    ### 功能
    # 适用于阿里云服务器第一次
    # 将挂载的磁盘,每块依次挂载到/data/ /data1/ /data2目录
    # 无需传参,自动使用磁盘所有存储空间

    #信号非0退出脚本
    #set -e

    #去掉rc.local
    grep init_disk.sh /etc/rc.local >/dev/null && sed -i 's/^bash \/usr\/local\/scripts\/init_disk.sh/# &/g' /etc/rc.local

    #清理脚本
    trap "rm -rf ${BASH_SOURCE}" EXIT

    # ctrl+c
    trap "" SIGINT SIGQUIT

    ### 先检查磁盘信息
    __check() {
    #获取磁盘信息
    _dev=`fdisk -l 2> /dev/null |grep /dev/...1 |head -n 1 |cut -c 1-7`
    _disk_all=(
    `fdisk -l 2>/dev/null |grep -v "doesn't contain" |grep -o "${_dev}." |uniq -c |grep -oP "(?<=1 )${_dev}."`
    )

    #检查磁盘(没有可操作的磁盘则退出脚本)
    echo ${_disk_all[@]} |grep -E "/dev/[a-z]{3}" || { echo "No new disks were found" && exit 1; }
    }

    ### 前期准备
    __ready() {

    # 根据主机名修改hosts
    _hostname=`hostname`
    grep ${_hostname} /etc/hosts || sed -i "s/.*127.0.0.1.*/127.0.0.1 ${_hostname} localhost/g" /etc/hosts

    # 判断系统
    which apt-get && system=ubuntu
    which yum && system=centos

    # 安装软件包
    case ${system} in
    ubuntu)
    apt-get update
    apt-get -y install lvm2 || echo "lvm2 Already installed"
    ;;
    centos)
    yum -y install lvm2 || echo "lvm2 Already installed"
    ;;
    *)
    echo "Unidentified _system environment variable"
    esac

    }

    #执行主程序
    __main() {

    #磁盘分区
    fdisk ${_disk} <<-EOF
    n
    p
    1


    t
    8e
    wq
    EOF

    #创建lvm
    sync ;sleep 2
    pvcreate ${_disk}1
    vgcreate welab_vg${_num} ${_disk}1
    lvcreate -l 100%VG -n welab_vol${_num} welab_vg${_num}
    mkfs -t ext4 /dev/welab_vg${_num}/welab_vol${_num}
    [ -d /data${_num} ] || mkdir -p /data${_num}

    # parted /dev/vdc
    # mklabel gpt
    # mkpart primary 1024K 3220G
    # Ignore
    # toggle 1 lvm
    # q

    # pvcreate /dev/vdc1
    # vgcreate welab_vg1 /dev/vdc1
    # lvcreate -L 2998G -n welab_vol1 welab_vg1
    # mkfs -t ext4 /dev/welab_vg1/welab_vol1
    # mkdir /data1
    # echo "UUID=`blkid /dev/welab_vg1/welab_vol1 |awk -F'"' '{print $2}'` /data1 ext4 defaults 0 0" >> /etc/fstab
    # mount -a

    # pvcreate /dev/vd[cdef]1
    # vgcreate -A y -s 128M welab_vg1 /dev/vd[cdef]1
    # lvcreate -A y -i 4 -I 8 -l 100%VG -n welab_vol1 welab_vg1
    # mkfs.ext4 /dev/welab_vg1/welab_vol1 -m 0 -O extent,uninit_bg -E lazy_itable_init=1,stride=2,stripe_width=32 -b 4096 -T largefile -L welab_vol1
    # echo "UUID=`blkid /dev/welab_vg1/welab_vol1 |awk -F'"' '{print $2}'` /data1 ext4 defaults,noatime,nodiratime,nodelalloc,barrier=0,data=writeback 0 0" >> /etc/fstab

    #去掉遗留的data挂载
    #sed -ri '/\/data[0-9]?/d' /etc/fstab

    #挂载磁盘
    [ -b /dev/welab_vg${_num}/welab_vol${_num} ] && \
    echo "UUID=`blkid /dev/welab_vg${_num}/welab_vol${_num} |awk -F'"' '{print $2}'` /data${_num} ext4 defaults 0 0" >> /etc/fstab
    sleep 3
    mount -a
    }

    __do_main() {
    _data_name=`df -h |grep -oP "data(\d*)?"`
    _data_num=`echo ${_data_name} |sed 's/data//g'`
    [ x${_data_num} == x'' ] && _num=-1 || _num=$[ ${_data_num}+1 ]
    [ x${_data_name} == x'data' ] && _num=0

    for _disk in ${_disk_all[@]} ; do
    _size=$[ `fdisk -l 2>/dev/null |grep ${_disk} |awk '{print $5}'`/1024/1024/1024-1 ]

    if [ ${_size} -gt 20 ] ;then
    _num=$[${_num}+1]
    [ ${_num} -eq 0 ] && unset _num
    __main
    else
    echo "Disk ${_disk} capacity is less than 20G"
    fi
    done
    }

    __check
    __ready
    __do_main

磁盘扩容

  1. 格式化分区

    1
    2
    3
    4
    5
    6
    7
    8
    9
    fdisk /dev/vdc <<-EOF
    n
    p
    1

    t
    8e
    wq
    EOF
  2. 先卸载需要扩容的磁盘

    1
    umount /data
  3. 增加磁盘,创建pv

    1
    pvcreate /dev/vdc1
  4. 扩容vg

    1
    vgextend welab_vg /dev/vdc1
  5. 扩容lv

    1
    lvextend -l 100%VG /dev/welab_vg/welab_vol
  6. 检查文件系统

    1
    e2fsck -f /dev/welab_vg/welab_vol
  7. 扩容文件系统

    1
    resize2fs /dev/welab_vg/welab_vol
  8. 添加fatab,重新挂载

    1
    mount -a
  9. 建议卸载磁盘,不能卸载的情况下可以最后执行

    1
    mount -o remount,rw /data