RHEL7磁盘分区挂载和格式化
安装大数据平台,每台机器需要挂载10个磁盘,用JBOD模式,操作系统为RHEL7.2。
写了两个脚本,format_disk.sh和mount_disk.sh实现磁盘自动分区格式化以及挂载,修改fstab。
format_disk.sh
#!/bin/bash disks=(sdb sdc sdd sde sdf sdg sdh sdi sdj sdk) for ((i=0;i<${#disks[*]};i++)) do mounted=`mount|grep "/mnt/${disks[i]}"` if [ "$mounted" ] ; then echo "umount /mnt/${disks[i]}" umount /mnt/${disks[i]} fi echo "make partition /dev/${disks[i]}" parted -s /dev/${disks[i]} mklabel gpt parted -s /dev/${disks[i]} mkpart primary xfs 0% 100% echo "format disk /dev/${disks[i]}" mkfs.xfs -f -L ${disks[i]} /dev/${disks[i]} done echo -n "reboot?" read ANS case $ANS in y|Y|yes|Yes) echo "run mount_disk.sh after rebooting" reboot ;; n|N|no|No) exit 0 ;; esac
mount_disk.sh
#!/bin/bash rm -f fstab cp /etc/fstab . -f cat fstab >fstab.new sed -e '/\/mnt\/sd/d' fstab > fstab.new cat fstab.new disks=(sdb sdc sdd sde sdf sdg sdh sdi sdj sdk) for ((i=0;i<${#disks[*]};i++)) do mounted=`mount|grep "/mnt/${disks[i]}"` if [ "$mounted" ] ; then echo "umount /mnt/${disks[i]}" umount /mnt/${disks[i]} fi if [ ! -d "/mnt/${disks[i]}" ]; then mkdir /mnt/${disks[i]} fi UUID=`lsblk -f /dev/${disks[i]}|grep "^${disks[i]} " |awk '{print $4}'` if [ "$UUID" ]; then echo "write partition to fstab.new" echo "UUID=${UUID} /mnt/${disks[i]} xfs defaults 0 0" >> fstab.new fi done cat fstab.new echo -n "use this for /etc/fstab?" read ANS case $ANS in y|Y|yes|Yes) cp fstab.new /etc/fstab -f mount -a mount ;; n|N|no|No) exit 0 ;; esac