云服务自动分区挂载数据盘和分区

来自linux中国网wiki
Evan讨论 | 贡献2021年12月8日 (三) 03:48的版本 →‎function 化
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳到导航 跳到搜索

Note

背景信息

数据盘的设备名默认由系统分配,命名规则如下所示:

   I/O优化实例的数据盘设备名为/dev/vd[b-z],例如/dev/vdb、/dev/vdc和/dev/vdd等。
   非I/O优化实例的数据盘设备名为/dev/xvd[b-z],例如/dev/xvdb、/dev/xvdc和/dev/xvdd等。

数据盘支持创建GPT分区和MBR分区,二者区别如下:

   GPT:能够识别大于2 TiB的分区,创建分区数量无限制。
   MBR:只能识别小于或等于2 TiB的分区,最多创建4个分区。


分区格式化大于2 TiB数据盘

分区格式化大于2 TiB数据盘 aliyun

分区格式化小于2 TiB数据盘

来自qq云脚本

function 化


#!/bin/bash
# 2019 建议尽量用xfs on aws ec2  Tue Jan 14 06:06:39 UTC 2020

function   vdbmk()
{
if grep -q /data /etc/fstab ;then
 uuid=notneed; echo /data already in fstab;
else uuid=`mkfs.xfs /dev/vdb > /dev/null 2>&1 && blkid /dev/vdb | awk '{print $2}'`;
fi

if [[ $uuid == UUID* ]]; then 
echo $uuid /data xfs  defaults 1 0 >> /etc/fstab;

  #check the  fstab file 2020
  if   grep  /data /etc/fstab | grep UUID ;then
  mount -a; 
  else  
    echo  "fstab files has err  exit "
  exit 1
   fi   

else echo mkfs failed; 
fi
}


function   xdvmk()
{
if grep -q /data /etc/fstab ;then
 uuid=notneed; echo /data already in fstab;
else uuid=`mkfs.xfs /dev/xvdb > /dev/null 2>&1 && blkid /dev/xvdb | awk '{print $2}'`;
fi

if [[ $uuid == UUID* ]]; then 
echo $uuid /data xfs  defaults 1 0 >> /etc/fstab;

  #check the  fstab file 2020
  if   grep  /data /etc/fstab | grep UUID ;then
  mount -a; 
  else  
    echo  "fstab files has err  exit "
  exit 1
   fi   

else echo mkfs failed; 
fi
}

function   nvmmk()
{
mkdir -p  /data
if grep -q /data /etc/fstab ;then
 uuid=notneed; echo /data already in fstab;
else uuid=`mkfs.xfs /dev/nvme1n1 > /dev/null 2>&1 && blkid /dev/nvme1n1 | awk '{print $2}'`;
fi

if [[ $uuid == UUID* ]]; then 
echo $uuid /data xfs  defaults 1 0 >> /etc/fstab;

  #check the  fstab file 2020
  if   grep  /data /etc/fstab | grep UUID ;then
  mount -a; 
  else  
    echo  "fstab files has err  exit "
  exit 1
   fi   

else echo mkfs failed; 
fi
}

mkdir -p  /data
   if  fdisk  -l   | grep  nvme1n1; then 
    nvmmk;
   elif fdisk  -l   | grep  vdb; then
    vdbmk;
   elif fdisk  -l   | grep  xvdb; then
    xdvmk;
   else 
   echo "not  xvdb or  nvm"; exit  1
   fi

rm $0

简洁版本

Wed Dec 08 2021
#想简洁就这个   qcloud 专用 
#!/bin/bash
#建议尽量用ext4 only aliyun 一般是ext4
mkdir -p  /data
if grep -q /data /etc/fstab ;then
 uuid=notneed; echo /data already in fstab;
else uuid=`mkfs.ext4 /dev/vdb > /dev/null 2>&1 && blkid /dev/vdb | awk '{print $2}'`;
fi

if [[ $uuid == UUID* ]]; then 
echo $uuid /data ext4 noatime,acl,user_xattr 1 0 >> /etc/fstab;
 mount -a; 
else echo mkfs failed; 
fi
rm $0


过程 
java2:~# bash  -x 1
+ mkdir -p /data
+ grep -q /data /etc/fstab
++ mkfs.ext4 /dev/vdb
++ blkid /dev/vdb
++ awk '{print $2}'
+ uuid='UUID="717368e4-f101-46b1-91ea-ef1812342501"'
+ [[ UUID="717368e4-f101-46b1-91ea-ef1812342501" == UUID* ]]
+ echo 'UUID="717368e4-f101-46b1-91ea-ef1812342501"' /data ext4 noatime,acl,user_xattr 1 0
+ mount -a
+ rm 1









以前的老的版本



#!/bin/bash
# 2019 建议尽量用xfs on aws ec2  Tue Jan 14 06:06:39 UTC 2020
mkdir -p  /data
if grep -q /data /etc/fstab ;then
 uuid=notneed; echo /data already in fstab;
    if  fdisk  -l   | grep  nvme1n1; then 
   
       uuid=`mkfs.xfs /dev/nvme1n1 > /dev/null 2>&1 && blkid /dev/nvme1n1 | awk '{print $2}'`;
   elif fdisk  -l   | grep  xvdb; then

       uuid=`mkfs.xfs /dev/xvdb > /dev/null 2>&1 && blkid /dev/xvdb | awk '{print $2}'`;
else 
   echo "not  xvdb or  nvm"; exit  1
fi

if [[ $uuid == UUID* ]]; then 
echo $uuid /data xfs  defaults 1 0 >> /etc/fstab;

  #check the  fstab file 2020
  if   grep  /data /etc/fstab | grep UUID ;then
  mount -a; 
  else  
    echo  "fstab files has err  exit "
  exit 1
   fi   

else echo mkfs failed; 
fi










#!/bin/bash
# 2019 建议尽量用xfs on aws ec2  Sun Sep 29 16:09:40 CST 2019  如果只用了xvdb 数据盘
mkdir -p  /data
if grep -q /data /etc/fstab ;then
 uuid=notneed; echo /data already in fstab;
else uuid=`mkfs.xfs /dev/xvdb > /dev/null 2>&1 && blkid /dev/xvdb | awk '{print $2}'`;
fi

if [[ $uuid == UUID* ]]; then 
echo $uuid /data xfs  defaults 1 0 >> /etc/fstab;

  #check the  fstab file 2020
  if   grep  /data /etc/fstab | grep UUID ;then
  mount -a; 
  else  
    echo  "fstab files has err  exit "
  exit 1
   fi   

else echo mkfs failed; 
fi
rm $0







#note  原版的 fstab xfs 
UUID="f78f0ed2-2d18-49a1-b5b9-33a39f77315b"  /data2                   xfs     defaults        0 0






[root@VM_135_85_centos ~]# mkfs.ext3 /dev/vdb > /dev/null 2>&1 && blkid /dev/vdb

/dev/vdb: UUID="aee97e63-0081-4088-85c4-c6ae9a7ab253" SEC_TYPE="ext2" TYPE="ext3"

阿里云脚本

#/bin/bash
#########################################
#Function:    auto fdisk
#Usage:       bash auto_fdisk.sh
#Author:      Customer service department
#Company:     Alibaba Cloud Computing
#Version:     4.0
#########################################

count=0
tmp1=/tmp/.tmp1
tmp2=/tmp/.tmp2
>$tmp1
>$tmp2
fstab_file=/etc/fstab

#check lock file ,one time only let the script run one time 
LOCKfile=/tmp/.$(basename $0)
if [ -f "$LOCKfile" ]
then
  echo -e "\033[1;40;31mThe script is already exist,please next time to run this script.\033[0m"
  exit
else
  echo -e "\033[40;32mStep 1.No lock file,begin to create lock file and continue.\033[40;37m"
  touch $LOCKfile
fi

#check user
if [ $(id -u) != "0" ]
then
  echo -e "\033[1;40;31mError: You must be root to run this script, please use root to install this script.\033[0m"
  rm -rf $LOCKfile
  exit 1
fi

#check disk partition
check_disk()
{
  >$LOCKfile
  device_list=$(fdisk -l|grep "Disk"|grep "/dev"|awk '{print $2}'|awk -F: '{print $1}'|grep "vd")
  for i in `echo $device_list`
  do
    device_count=$(fdisk -l $i|grep "$i"|awk '{print $2}'|awk -F: '{print $1}'|wc -l)
    echo 
    if [ $device_count -lt 2 ]
    then
      now_mount=$(df -h)
      if echo $now_mount|grep -w "$i" >/dev/null 2>&1
      then
        echo -e "\033[40;32mThe $i disk is mounted.\033[40;37m"
      else
        echo $i >>$LOCKfile
        echo "You have a free disk,Now will fdisk it and mount it."
      fi
    fi
  done
  disk_list=$(cat $LOCKfile)
  if [ "X$disk_list" == "X" ]
  then
    echo -e "\033[1;40;31mNo free disk need to be fdisk.Exit script.\033[0m"
    rm -rf $LOCKfile
    exit 0
  else
    echo -e "\033[40;32mThis system have free disk :\033[40;37m"
    for i in `echo $disk_list`
    do
      echo "$i"
      count=$((count+1))
    done
  fi
}

#check os
check_os()
{
  os_release=$(grep "Aliyun Linux release" /etc/issue 2>/dev/null)
  os_release_2=$(grep "Aliyun Linux release" /etc/aliyun-release 2>/dev/null)
  if [ "$os_release" ] && [ "$os_release_2" ]
  then
    if echo "$os_release"|grep "release 5" >/dev/null 2>&1
    then
      os_release=aliyun5
      modify_env
     fi
  fi
}

#install ext4
modify_env()
{
  modprobe ext4
  yum install e4fsprogs -y
}

#fdisk ,formating and create the file system
fdisk_fun()
{
fdisk -S 56 $1 << EOF
n
p
1


wq
EOF

sleep 5
mkfs.ext4 ${1}1
}

#make directory
make_dir()
{
  echo -e "\033[40;32mStep 4.Begin to make directory\033[40;37m"
  now_dir_count=$(ls /|grep "alidata*"|awk -F "data" '{print $2}'|sort -n|tail -1)
  if [ "X$now_dir_count" ==  "X" ]
  then
    for j in `seq $count`
    do
      echo "/alidata$j" >>$tmp1
      mkdir /alidata$j
    done
  else
    for j in `seq $count`
    do
      k=$((now_dir_count+j))
      echo "/alidata$k" >>$tmp1
      mkdir /alidata$k
    done
  fi
 }

#config /etc/fstab and mount device
main()
{
  for i in `echo $disk_list`
  do
    echo -e "\033[40;32mStep 3.Begin to fdisk free disk.\033[40;37m"
    fdisk_fun $i
    echo "${i}1" >>$tmp2
  done
  make_dir
  >$LOCKfile
  paste $tmp2 $tmp1 >$LOCKfile
  echo -e "\033[40;32mStep 5.Begin to write configuration to /etc/fstab and mount device.\033[40;37m"
  while read a b
  do
    if grep -v ^# $fstab_file|grep ${a} >/dev/null
    then
      sed -i "s=${a}*=#&=" $fstab_file
    fi
    echo "${a}             $b                 ext4    defaults        0 0" >>$fstab_file
  done <$LOCKfile
  mount -a
}

#=========start script===========
echo -e "\033[40;32mStep 2.Begin to check free disk.\033[40;37m"
check_os
check_disk
main
df -h
rm -rf $LOCKfile $tmp1 $tmp2

参考

https://www.qcloud.com/document/product/362/6735


如果是阿里 格式化和挂载数据盘



linux fdisk 分区、格式化、挂载