shell之磁盘初始化脚本

 公司经常进行系统安装,然后公司服务器一般都是挂了很多块磁盘,要是一台台的手动去做磁盘初始化非常麻烦,这里就写成了脚本去做磁盘的初始化.也可以通过ansible-playbook做磁盘初始化.脚本内容如下,写的比较简单.
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin
export PATH
s=`ls  /dev/sd*|egrep -v "sda"|wc -l`
i=1
[ ! -f /sbin/parted ] && sudo apt-get install -y parted
while [ i -les ]
do
j=`echo i|awk '{printf "%c",97+i}'` 
parted /dev/sdj <<EOF

mklabel gpt

ignore

mkpart primary 0 -1

yes

ignore

quit

EOF

a="?"
if [ a -eq 0 ];then
mkfs.ext4  /dev/sd{j}1 

mkdir -p /data/dev/sd{j}1

mount="/dev/sd{j}1 /data/dev/sd{j}1 ext4 errors=remount-ro 0 0"

echomount >>/etc/fstab               
fi
i=((i+1))

done

echo "/n/n*****Formating and Mounting have finished****/n/n"

mount -a

ansible方式磁盘初始化
task文件:
  - name: create mount dir
    file: path="{{ item.value }}" state=directory recurse=yes 
    with_dict: "{{ disks }}"
  - name: format datanode disks
    filesystem: fstype=ext4 dev="{{ item.key }}" force=true
    with_dict: "{{ disks }}"

  - name: mount datanode disks
    mount:   path="{{ item.value }}" src="{{ item.key }}"     fstype=ext4 opts="errors=remount-ro" state=mounted
    with_dict: "{{ disks }}"

var文件
  disks:
  /dev/sdc: /data/dev/sdc1
  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • 我的微信公众号
  • 我的微信公众号扫一扫
  • weinxin
avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: