首页 > 系统相关 >vmware自动扩容所有磁盘脚本

vmware自动扩容所有磁盘脚本

时间:2024-01-03 19:33:50浏览次数:42  
标签:脚本 return NAME VG LV SWAP 磁盘 local vmware

从vmware中获取的自动扩容磁盘脚本

#!/bin/bash
#
# Copyright 2016-2020  VMware, Inc.  All rights reserved.
#
# autogrow    Grow all partitions to 100%

# /usr/lib/applmgmt/support/scripts/resize-root.py
. /usr/sbin/disk_utils.sh
lvm_autogrow
# Resize swap partition at boot
tlog() {
   echo `date` Disk Util: "$@"
}

# Following routine resizes all PVs in LVM called by resize_lvm_vg
resize_lvm_pv()
{
   # Add a force rescan of scsi bus to allow hotadd changed disk size
   # Note the following command is SuSE / SLES specific
   tlog "INFO: Scanning Hard disk sizes"
   rescan-scsi-bus.sh -w --forcerescan 2>&1
   # Now resize all LVM PVs
   local DEVLIST=`ls /sys/block/ | grep sd[a-z]`
   for d in $DEVLIST; do
      local DEV=/dev/${d}
      # check for partitions, filesystem, swap directly on the device and LVM.
      tlog "INFO: Resizing PV $DEV"
      if  file -s $DEV | grep -q "LVM2"; then
         pvresize $DEV 2>&1
      fi
   done
}

# Following routine resizes all VGs
resize_lvm_vg()
{
   local VGLIST=`lvdisplay | grep "LV Path"|awk '{print $3}'`
   # Call resize PVs (Doesn't hurt if called multiple times)
   resize_lvm_pv
   # Now resize all LVM LVs
   for v in $VGLIST; do
     tlog "INFO: LV Resizing $v"
     lvresize --resizefs -l +100%FREE "$v" 2>&1
   done
   return 0
}


#
# lists the next device that don't contain partitions, filesystems or LVM data.
#


get_next_uninitialized_device()
{
   DISKFILE=$1
   lockfile /tmp/disk.create.lockfile
   local __resultvar=$1
   local DEVLIST=`ls /sys/block/ | grep sd[a-z]`
   for d in $DEVLIST; do
      local DEV=/dev/${d}
      # check for partitions, filesystem, swap directly on the device and LVM.
      if ! file -s $DEV | grep -q "partition [[:digit:]]\+:\|filesystem data\|LVM2\|swap file\|GPT\|MBR"; then
         if ! grep -q $DEV $DISKFILE; then
            echo $DEV >> $DISKFILE
            echo $DEV
            rm -f /tmp/disk.create.lockfile
            return
         fi
      fi
   done
   rm -f /tmp/disk.create.lockfile
   echo ""
}


create_vg_lg()
{
   local VG_NAME="$1"
   local newdisk="$2"
   local LV_NAME="$3"
   local LV_FSTYPE="$4"
   local LV_MOUNTPOINT="$5"
   local LV_OPTS="$6"
   tlog "INFO: $VG_NAME on $newdisk setup started"
   set_vg "$VG_NAME" "$newdisk" || (tlog "LVM:VG $VG_NAME not created on $newdisk"; return 1)
   set_lv "$VG_NAME" "$LV_NAME" "$LV_FSTYPE" "$LV_MOUNTPOINT"\
                "$LV_OPTS" || (tlog "LVM:LV $VG_NAME not created"; return 1)
   tlog "INFO: $VG_NAME on $newdisk setup completed"
   return 0
}
set_vg()
{
  local VG_NAME="$1"
  local newdisk="$2"
  if [ "$newdisk" != "" ]; then
     vgcreate -s 8m -l 8 -f "$VG_NAME" "$newdisk" 2>&1 >/dev/null \
        || (tlog "ERROR:VG $VG_NAME could not be created"; return 1)
  else
     tlog "WARN No disk available for creating vg $VGNAME"
     return 1
  fi
  return 0
}

set_lv()
{
      local VG_NAME="$1"
      local LV_NAME="$2"
      local LV_FSTYPE="$3"
      local LV_MOUNTPOINT="$4"
      local LV_OPTS="$5"
      lvcreate -l "100%FREE" -n "$LV_NAME" "$VG_NAME" 2>&1 > /dev/null
      if [ $? -ne 0 ]; then
         tlog "ERROR: Cannot create LV $LV_NAME."
         return 1
      fi

      if [ "${LV_FSTYPE}" == "swap" ]
      then
         # We do mkswap in recreate_swap function
         # grep for whole word here to be more accurate
         if ! (grep -qw "${VG_NAME}/${LV_NAME}" /etc/fstab); then
           echo "/dev/${VG_NAME}/${LV_NAME} none ${LV_FSTYPE} sw,nosuid,nodev 0 0" >> /etc/fstab
         fi
         return 0
      fi
      FSOPT=""
      if [ "${LV_FSTYPE}" == "ext3" ]
      then
          FSOPT="-q"
      fi

      if ! mkfs."$LV_FSTYPE" $FSOPT "/dev/$VG_NAME/$LV_NAME" 2>&1 > /dev/null; then
         tlog "ERROR: Cannot create a filesystem on /dev/$VG_NAME/$LV_NAME."
         return 1
      fi
      return 0
}

# The following should be called early in boot cycle to resize swap
# It is not expected that lvm autogrow may be executed everytime so
# it does resize swap logical volume (firstboot)
recreate_swap()
{
   local DEV_SWAP=/dev/sdc
   local VG_SWAP=swap_vg
   local LV_SWAP=swap1
   local LV_FSTYPE=swap
   local LV_SWAP_MAPPER=/dev/mapper/${VG_SWAP}-${LV_SWAP}
   # The disk might be blank disk might need to recreate swap_vg
   # exit if it fails with return value 1
   tlog "INFO: Recreating swap"
   vgexists=`pvs 2>/dev/null | awk "/[[:space:]]${VG_SWAP}[[:space:]]/{print \$1}"`
   if [ "$vgexists" == "" ]; then
      set_vg "${VG_SWAP}" "$DEV_SWAP" || (tlog "SWAP:VG $VG_SWAP not created on $DEV_SWAP"; return 1)
      set_lv "${VG_SWAP}" "${LV_SWAP}" "${LV_FSTYPE}" || (tlog "SWAP:LV $LV_SWAP not created"; return 1)
   fi
   # Also we need to make sure that the swap partition is resized
   # If there is no change it errors out keeping the old swapon
   pvresize "$DEV_SWAP"  || (tlog " pvresize swap failed"; return 1)
   lvresize -l +100%FREE "/dev/${VG_SWAP}/${LV_SWAP}" 2>&1 > /dev/null \
         || (tlog " Swap resize not needed"; return 0)
   # Now swapoff and swapon the device to utilize full size
   # Best done after reboot - early in boot cycle
   # otherwise swapoff may fail
   if (/sbin/swapon -s | grep -q "${LV_SWAP_MAPPER}"); then
      /sbin/swapoff "${LV_SWAP_MAPPER}" || (tlog "SWAP:swapoff failed"; return 1)
   fi
   /sbin/mkswap -f "${LV_SWAP_MAPPER}" || (tlog "SWAP:mkswap failed"; return 1)
   /sbin/swapon "${LV_SWAP_MAPPER}" || (tlog "SWAP:swapon failed"; return 1)
   tlog "INFO: Swap resizing done"
   return 0
}

#
# lists the PVs for a given VG.
# used during firstboot. it's expected that the PVs are at the disk level, not partitions.
#
get_devices_for_vg()
{
   local VG_NAME=$1
   pvs | awk "/[[:space:]]${VG_NAME}[[:space:]]/{print \$1}"
}


#
# guesses the name of the lv that resides on device.
#
get_lv_for_device()
{
   local DEV=$1

   local VG_NAME=`pvs | grep "$DEV" | head -1 | awk '{print $2}'`

   # the LV name should be the VG without _vg suffix
   echo ${VG_NAME/%_vg/}
}


#
# creates several per-disk VGs, based on configuration file,
# and one LV in each VG.
# the disks come pre-initialized with a VG, used only as a tag
# to distinguish between them.
#
# New setup is as follows
# CloudVM has few disks populated
# 1. sda- system/root,
# 2. sdb - CloudComponents/RPM packages,
# 3. sdc - swap disk
# All other disks are assumed to be blank (either a blank vmdk or a
# dynamic disk). File systems are assigned to disks in the order they appear
# in cloudvm_disk.cfg.  Because cloudvm_disk.cfg is used both to created the
# disks in the OVF and to assign them (here), this mapping is not fragile.
#
setup_storage()
{
   local DISK_CFG=$1
   local failed=0

   if ! grep -q "^LV_CFG=" "$DISK_CFG"; then
      tlog "ERROR: LV_CFG not found."
      return 1
   fi

   if vgdisplay 2>&1 | grep -q "VG Name[[:space:]]*$VG_NAME\$"; then
      tlog "WARNING: Volume group $VG_NAME already exists."
      return 0
   fi

   echo >> /etc/fstab
   DISKFILE=$(mktemp '/tmp/XXXXdisk.reserved') ||\
   { echo "Failed to create XXXXdisk.reserved"; exit 1; }
   grep "^LV_CFG=" "$DISK_CFG" |  ( while read line; do
      LV_CFG=$(echo $line | cut -f2 -d=)
      set -- $LV_CFG
      # see http://tldp.org/LDP/abs/html/gotchas.html#BADREAD0
      LV_NAME=$1; LV_MOUNTPOINT=$2; LV_FSTYPE=$3; LV_OPTS=$5
      VG_NAME=${LV_NAME}_vg

      if [[ "$LV_NAME" == "lvm_snapshot" ]]; then
         echo "Switching VG format for $LV_NAME"
         snapshot_lvname=lv_${LV_NAME}
         snapshot_vgname=vg_${LV_NAME}
         LV_NAME=$snapshot_lvname
         VG_NAME=$snapshot_vgname
      fi
      echo "Creating disk with VG $VG_NAME, LV $LV_NAME"
      local newdisk=`get_next_uninitialized_device $DISKFILE`
      # create_vg_lg used to executed in parallel, refer to PR 1585511 & 1608127
      create_vg_lg "$VG_NAME" "$newdisk" "$LV_NAME" "$LV_FSTYPE" \
                "$LV_MOUNTPOINT" "$LV_OPTS"
      if ! mkdir -p "$LV_MOUNTPOINT" 2>&1 > /dev/null; then
         tlog "ERROR: Cannot create mountpoint $LV_MOUNTPOINT."
         failed=1
      elif ! (grep -qw "${VG_NAME}/${LV_NAME}" /etc/fstab); then
        echo "/dev/$VG_NAME/$LV_NAME $LV_MOUNTPOINT $LV_FSTYPE $LV_OPTS 0 2" >> /etc/fstab
      fi
    done
    rm -f $DISKFILE
    if [ "$failed" -eq "1" ]; then
      return 1
    fi
   )
   tlog "INFO: All filesystems created. Mounting all"
   if ! mount -a; then
      tlog "ERROR: Cannot mount all filesystems."
      return 1
   fi
}


#
# expands LVs into new disk free space.
#
lvm_autogrow()
{
   # we don't handle brand new disks
   # The user has to manually do vgcreate
   # or vgextend to add it to existing VG

   # For existing VGs just call resize_lvm_vg
   resize_lvm_vg
   return $?
}

标签:脚本,return,NAME,VG,LV,SWAP,磁盘,local,vmware
From: https://blog.51cto.com/xiaoyuanzheng/9087922

相关文章

  • VMware NSX-T Data Center 3.2.3.1 - 数据中心网络全栈虚拟化
    VMwareNSX-TDataCenter3.2.3.1-数据中心网络全栈虚拟化作者主页:sysin.orgVMwareNSX3.2.3.1|27JULY2023|Build22104592NSX-T概述VMwareNSX-T™DataCenter提供了一个敏捷式软件定义基础架构,用来构建云原生应用程序环境。NSX-TDataCenter专注于为具有异构端点......
  • VMware Horizon 8 2306 - 虚拟桌面基础架构 (VDI) 和应用软件
    VMwareHorizon82306-虚拟桌面基础架构(VDI)和应用软件作者主页:sysin.orgVersion2306DocumentationReleaseNotesReleaseDate2023-07-06虚拟桌面基础架构(VDI)和应用软件VMwareHorizon通过高效安全地将虚拟桌面和应用从本地部署环境交付到云端,提升数字化工作空间体验......
  • VMware ESXi 8.0U1c macOS Unlocker & OEM BIOS (标准版和厂商定制版)
    VMwareESXi8.0U1cmacOSUnlocker&OEMBIOS(标准版和厂商定制版)ESXi8.0U1c标准版,DellHPE联想浪潮定制版作者主页:sysin.org2023-07-27,VMwarevSphere8.0U1c发布。详见:VMwarevSphere8Update1新增功能VMwarevSphere是VMware的虚拟化平台,可将数据中心转换为包......
  • VMware vCenter Server 8.0U1c 发布 - 集中式管理 vSphere 环境
    VMwarevCenterServer8.0U1c发布-集中式管理vSphere环境作者主页:sysin.org2023-07-27,VMwarevSphere8.0U1c发布。详见:VMwarevSphere8Update1新增功能VMwarevCenterServer是一款高级服务器管理软件,提供了一个集中式平台来控制vSphere环境,以实现跨混合云的可见......
  • VMware vRealize Automation 8.3 发布 - 现代基础架构自动化
    vRealizeAutomation8.3|17March2021vRAEasyInstaller(ISO)build17556762vRAproduct(appliance)build17551690概述VMwarevRealizeAutomation是一个支持自助式多云环境的现代基础架构自动化平台。借助vRealizeAutomation,客户可以通过自助式自动化提高敏捷性、工......
  • VMware vRealize Log Insight 8.3 发布 - 智能日志记录和分析
    VMwarevRealizeLogInsight8.3发布-智能日志记录和分析VMwarevRealizeLogInsight8.3概述vRealizeLoglnsight提供了高度可扩展的异构日志管理功能,它具有多个可在其中执行操作的直观仪表盘、完善的分析功能和范围更广的第三方延展性。它还能够跨物理、虚拟和云计算环境......
  • VMware vRealize Suite 8.3 发布 - 多云环境的云计算管理解决方案
    概述VMwarevRealizeSuite是一种多云环境的云计算管理解决方案,为IT组织提供了一个基于DevOps和ML原则的基础架构自动化、一致运维和监管的现代平台。vRealizeSuite如何帮助管理多云环境?应用运维使开发人员能够实时地快速发布基于微服务、分布极广的云计算应用,对其执行故......
  • VMware Cloud Foundation 4.2 发布 - 领先的混合云平台
    VMwareCloudFoundation4.2|09FEB2021|Build17559673VMwareCloudFoundation4.1|06OCT2020|Build16961769VMwareCloudFoundation4.0|14APR2020|Build16008466借助适用于现代应用的混合云平台,推动实现数字化转型什么是VMwareCloudFoundation?VMwareC......
  • VMware vRealize Suite 8.4 发布 - 多云环境的云计算管理解决方案
    VMwarevRealizeSuite8.4.0,ReleaseDate:2021-04-15概述VMwarevRealizeSuite是一种多云环境的云计算管理解决方案,为IT组织提供了一个基于DevOps和ML原则的基础架构自动化、一致运维和监管的现代平台。vRealizeSuite如何帮助管理多云环境?应用运维使开发人员能够实时......
  • VMware vRealize Suite Lifecycle Manager 8.4 发布 - vRealize Suite 生命周期和内容
    使用vRealizeSuiteLifecycleManager管理vRealizeSuitevRealizeSuiteLifecycleManager针对vRealizeSuite产品提供完整的生命周期和内容管理功能。它通过自动执行部署、升级和配置来帮助客户缩短价值实现时间,同时将DevOps原则应用到vRealizeSuite内容管理中。vRea......