首页 > 服务器运维 > linux 系统运维

linux如何自动分区、格式化、挂载脚本实例应用

功能:自动检测是否有尚未分区的数据盘,格式化新的数据盘并自动挂载0wo香港云服务器_物理独立服务器_VPS虚拟主机免费备案、租用测评、优惠排名 - VPS巴士

解决了什么问题:一键式检测是否有尚未分区的数据盘,并能对其格式化和自动挂载,省去了复杂的命令和步骤0wo香港云服务器_物理独立服务器_VPS虚拟主机免费备案、租用测评、优惠排名 - VPS巴士

执行方法:以root身份执行命令0wo香港云服务器_物理独立服务器_VPS虚拟主机免费备案、租用测评、优惠排名 - VPS巴士

wgethttp://mirrors.linuxeye.com/scripts/auto_fdisk.shchmod+xauto_fdisk.sh./auto_fdisk.sh

结果:出现如下即自动分区、格式化、挂载成功:0wo香港云服务器_物理独立服务器_VPS虚拟主机免费备案、租用测评、优惠排名 - VPS巴士

linux如何自动分区、格式化、挂载脚本实例应用0wo香港云服务器_物理独立服务器_VPS虚拟主机免费备案、租用测评、优惠排名 - VPS巴士

脚本内容如下:0wo香港云服务器_物理独立服务器_VPS虚拟主机免费备案、租用测评、优惠排名 - VPS巴士

#!/bin/bash#Author:yeho<lj2007331ATgmail.com>#BLOG:https://blog.linuxeye.com##Notes:OneinStackforCentOS/RadHat5+Debian6+andubuntu12+##Projecthomepage:#http://oneinstack.com#https://github.com/lj2007331/oneinstackexportPATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/binclearprintf"########################################################################OneinStackforCentOS/RadHat5+Debian6+andUbuntu12+##Autofdisk##Formoreinformationpleasevisithttp://oneinstack.com########################################################################"echo=echoforcmdinecho/bin/echo;do$cmd>/dev/null2>&1||continueif!$cmd-e""|grep-qE'^-e';thenecho=$cmdbreakfidoneCSI=$($echo-e"33[")CEND="${CSI}0m"CDGREEN="${CSI}32m"CRED="${CSI}1;31m"CGREEN="${CSI}1;32m"CYELLOW="${CSI}1;33m"CBLUE="${CSI}1;34m"CMAGENTA="${CSI}1;35m"CCYAN="${CSI}1;36m"CSUCCESS="$CDGREEN"CFAILURE="$CRED"CQUESTION="$CMAGENTA"CWARNING="$CYELLOW"CMSG="$CCYAN"#Checkifuserisroot[$(id-u)!="0"]&&{echo"${CFAILURE}Error:Youmustberoottorunthisscript${CEND}";exit1;}MOUNT_DIR=/dataFSTAB_FILE=/etc/fstabcount=0TMP1=/tmp/.tmp1TMP2=/tmp/.tmp2>$TMP1>$TMP2#checklockfile,onetimeonlyletthescriptrunonetimeLOCKfile=/tmp/.$(basename$0)if[-f"$LOCKfile"];thenechoecho"${CWARNING}Thescriptisalreadyexist,pleasenexttimetorunthisscript${CEND}"echoexitelseechoecho"${CMSG}Step1.Nolockfile,begintocreatelockfileandcontinue${CEND}"echotouch$LOCKfilefi#checkdiskpartitioncheck_disk(){>$LOCKfileforiin`fdisk-l|grep"Disk"|grep"/dev"|awk'{print$2}'|awk-F:'{print$1}'|grep"vd"`doDEVICE_COUNT=$(fdisk-l$i|grep"$i"|awk'{print$2}'|awk-F:'{print$1}'|wc-l)NEW_MOUNT=$(df-h)if[$DEVICE_COUNT-lt2];thenif[-n"$(echo$NEW_MOUNT|grep-w"$i")"-o"$(grep-v'^#'$FSTAB_FILE|grep-v^$|awk'{print$1,$2,$3}'|grep-w"$i"|awk'{print$2}')"=='/'-o"$(grep-v'^#'$FSTAB_FILE|grep-v^$|awk'{print$1,$2,$3}'|grep-w"$i"|awk'{print$3}')"=='swap'];thenecho"${CWARNING}The$idiskismounted${CEND}"elseecho$i>>$LOCKfileecho"Youhaveafreedisk,Nowwillfdiskitandmountit"fifidoneDISK_LIST=$(cat$LOCKfile)if["X$DISK_LIST"=="X"];thenechoecho"${CWARNING}Nofreediskneedtobefdisk.Exitscript${CEND}"echorm-rf$LOCKfileexit0elseecho"${CMSG}Thissystemhavefreedisk:${CEND}"foriin`echo$DISK_LIST`doecho"$i"count=$((count+1))done[$count-gt1]&&{echo"${CWARNING}Thissystemhasatleasttwofreedisk,Youmustmanuallymountit${CEND}";exit0;}fi}#checkoscheck_os(){os_release=$(grep"AliyunLinuxrelease"/etc/issue2>/dev/null)os_release_2=$(grep"AliyunLinuxrelease"/etc/aliyun-release2>/dev/null)if["$os_release"]&&["$os_release_2"];thenifecho"$os_release"|grep"release5">/dev/null2>&1;thenos_release=aliyun5modify_envfifi}#installext4modify_env(){modprobeext4yum-yinstalle4fsprogs}#fdisk,formatingandcreatethefilesystemfdisk_fun(){fdisk-S56$1<<EOFnp1wqEOFsleep5mkfs.ext4${1}1}#makedirectorymake_dir(){echo"${CMSG}Step4.Begintomakedirectory${CEND}"[-d"$MOUNT_DIR"]&&mv${MOUNT_DIR}{,_bk}mkdir-p$MOUNT_DIRecho"$MOUNT_DIR">>$TMP1}#config/etc/fstabandmountdevicemain(){foriin`echo$DISK_LIST`doechoecho"${CMSG}Step3.Begintofdiskfreedisk${CEND}"[-n"`df-h|grep${i}1`"]&&{echo"${CFAILURE}The${i}1alreadymount${CEND}";echo;exit0;}fdisk_fun$i>/dev/null2>&1echoecho"${i}1">>$TMP2donemake_dir>$LOCKfilepaste$TMP2$TMP1>$LOCKfileechoecho"${CMSG}Step5.Begintowriteconfigurationto/etc/fstabandmountdevice${CEND}"whilereadabdo[-z"`grep^${a}$FSTAB_FILE`"-a-z"`grep${b}$FSTAB_FILE`"]&&echo"${a}$bext4defaults00">>$FSTAB_FILEdone<$LOCKfilemount-aecho}#startscriptecho"${CMSG}Step2.Begintocheckfreedisk${CEND}"#servicemysqldstop#mv/data/rootcheck_oscheck_diskmaindf-h#mv/root/data/*/data#servicemysqldstartrm-rf$LOCKfile$TMP1$TMP2
版权声明

本文内容由互联网用户自发贡献,版权归用户所有,本站不为本文内容承担相关法律责任。如有侵犯您的合法权利,请联系本站进行删除。

留言与评论(共有 条评论)

   
验证码:

标签列表

VPS巴士

统计代码 | 京ICP1234567-2号

Powered By VPS巴士

使用手机软件扫描微信二维码

关注我们可获取更多热点资讯