合 使用脚本初始化并挂载Linux数据盘
Tags: LinuxOS脚本脚本分享挂载磁盘shell脚本初始化分区
使用脚本初始化Linux数据盘
使用方法
参考:https://support.huaweicloud.com/qs-evs/evs_01_0105.html
1 2 3 | wget https://ecs-instance-driver.obs.cn-north-1.myhuaweicloud.com/datadisk/LinuxVMDataDiskAutoInitialize.sh chmod +x LinuxVMDataDiskAutoInitialize.sh ./LinuxVMDataDiskAutoInitialize.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 | cat > LinuxVMDataDiskAutoInitialize.sh <<"EOF" #!/bin/bash # Author: Seaton Jiang <seaton@vtrois.com> # Maintainer:Sibiao Luo <sibiaoluo@huawei.com> # Github URL: https://github.com/Vtrois/SpacePack # License: Apache License 2.0 # Date: 2017-08-03 export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin clear echo -e "\n\033[36mStep 1: Initializing script and check root privilege\033[0m" if [ "$(id -u)" = "0" ];then echo -e "\033[33mIs running, please wait!\033[0m" yum -y install e4fsprogs > /dev/null 2>&1 echo -e "\033[32mSuccess, the script is ready to be installed!\033[0m" else echo -e "\033[31mError, this script must be run as root!\n\033[0m" exit 1 fi echo -e "\n\033[36mStep 2: Show all active disks:\033[0m" fdisk -l 2>/dev/null | grep -o "Disk /dev/.*d[a-z]" | grep -v "/dev/vda" echo -e -n "\n\033[36mStep 3: Please choose the disk(e.g.: /dev/vdb and q to quit):\033[0m" read Disk if [ $Disk == q ];then exit fi until fdisk -l 2>/dev/null | grep -o "Disk /dev/.*d[a-z]" | grep -v "/dev/vda" | grep "Disk $Disk" &>/dev/null;do echo -e -n "\033[31mOops, something went wrong, please try again (e.g.: /dev/vdb or q to quit):\033[0m" read Disk if [ $Disk == q ];then exit fi done while mount | grep "$Disk" > /dev/null 2>&1;do echo -e "\033[31m\nYour disk has been mounted:\033[0m" mount | grep "$Disk" echo -e -n "\033[31m\nForce uninstalling? [y/n]:\033[0m" read Umount until [ $Umount == y -o $Umount == n ];do echo -e -n "\033[31mOops, something went wrong, please try again [y/n]:\033[0m" read Umount done if [ $Umount == n ];then exit else echo -e "\033[33mIs running, please wait!\033[0m" for i in `mount | grep "$Disk" | awk '{print $3}'`;do fuser -km $i >/dev/null umount $i >/dev/null temp=`echo $Disk | sed 's;/;\\\/;g'` sed -i -e "/^$temp/d" /etc/fstab sleep 2 done echo -e "\033[32mSuccess, the disk is unloaded!\033[0m" fi echo -e -n "\n\033[36mReady to begin to format the disk? [y/n]:\033[0m" read Choice until [ $Choice == y -o $Choice == n ];do echo -e -n "\033[31mOops, something went wrong, please try again [y/n]:\033[0m" read Choice done if [ $Choice == n ];then exit else echo -e "\033[33mIs running, please wait!\033[0m" dd if=/dev/zero of=$Disk bs=512 count=1 &>/dev/null sleep 2 sync fi echo -e "\033[32mSuccess, the disk has been formatted!\033[0m" done echo -e "\n\033[36mStep 4: The disk is partitioning and formatting\033[0m" echo -e "\033[33mIs running, please wait!\033[0m" fdisk_mkfs() { fdisk -S 56 $1 << EOF n p 1 wq EOF sleep 2 mkfs.ext4 ${1}1 } fdisk_mkfs $Disk > /dev/null 2>&1 echo -e "\033[32mSuccess, the disk has been partitioned and formatted!\033[0m" echo -e "\n\033[36mStep 5: Make a directory and mount it\033[0m" echo -e -n "\033[33mPlease enter a location to mount (e.g.: /mnt/data):\033[0m" read Mount mkdir $Mount > /dev/null 2>&1 mount ${Disk}1 $Mount echo -e "\033[32mSuccess, the mount is completed!\033[0m" echo -e "\n\033[36mStep 6: Write configuration to /etc/fstab and mount device\033[0m" echo ${Disk}1 $Mount 'ext4 defaults 0 0' >> /etc/fstab echo -e "\033[32mSuccess, the /etc/fstab is Write!\033[0m" echo -e "\n\033[36mStep 7: Show information about the file system on which each FILE resides\033[0m" df -h sleep 2 echo -e "\n\033[36mStep 8: Show the write configuration to /etc/fstab\033[0m" cat /etc/fstab EOF |
详细说明
操作场景
本文以云服务器的操作系统为“CentOS 7.4 64位”为例,采用初始化数据盘脚本的方式为数据盘设置分区。
不同云服务器的操作系统的格式化操作可能不同,本文仅供参考,具体操作步骤和差异请参考对应的云服务器操作系统的产品文档。
须知:
首次使用磁盘时,如果您未参考本章节对磁盘执行初始化操作,主要包括创建分区和文件系统等操作,那么当后续扩容磁盘时,新增容量部分的磁盘可能无法正常使用。
前提条件
约束与限制
该初始化脚本仅适用于划分普通partition分区,做ext4的文件系统。不适用于划分LVM或VG分区。
初始化新挂载的磁盘
登录云服务器,执行以下命令获取自动初始化磁盘脚本。
1wget https://ecs-instance-driver.obs.cn-north-1.myhuaweicloud.com/datadisk/LinuxVMDataDiskAutoInitialize.sh说明:
若回显异常,请检查云服务器是否绑定弹性公网IP。除华北-北京一外的区域,绑定弹性公网IP后才能获取脚本。
修改自动初始化磁盘脚本权限
本人提供Oracle(OCP、OCM)、MySQL(OCP)、PostgreSQL(PGCA、PGCE、PGCM)等数据库的培训和考证业务,私聊QQ646634621或微信dbaup66,谢谢!