Oct.28

Red Hat/CentOS hard disk partitioning,formatting & updating fstab with UUID

I came to a situation where I need to partitioning, formatting & updating fstab with UUID of 25 servers each having 26 disks in total. If i am doing it manually, it will take ages to complete.

Here is a script which has FOR loop, ARRAY, COUNTER…etc below are the major tasks

  1. read disk names
  2. create mount points
  3. creating the partition using fdisk (noninteractive way)
  4. formatting with XFS
  5. getting UUID and update in fstab
  6. setting block reservation to 0

 

#!/bin/bash
echo “________HDD__________”
IFS=$’\n’ read -d ” -ra DiskName < <(fdisk -l | grep 800 | egrep -v “GPT|WARNING” | awk ‘{print $2}’ | sed ‘s/://g’)
for i in “${!DiskName[@]}”;do

COUNTER=$(expr $COUNTER + 1)
mkdir -p /data/s$COUNTER
echo -e “o\nn\np\n1\n\n\nw” | fdisk ${DiskName[$i]}
mkfs.xfs $(echo “${DiskName[$i]}1”)
Uuid=$(blkid | grep $(echo “${DiskName[$i]}1”) | awk ‘{print $2}’ | sed ‘s/”//g’)
echo “$Uuid /data/s$COUNTER xfs defaults,noatime 1 2″ >> /etc/fstab
mount /data/s$COUNTER
xfs_io -x -c resblks /data/s$COUNTER

doneIFS=$’\n’ read -d ” -ra ForDiskName < <(fdisk -l | grep 4000 | egrep -v “GPT|WARNING” | awk ‘{print $2}’ | sed ‘s/://g’)
for i in “${!ForDiskName[@]}”;do

ForCOUNTER=$(expr $ForCOUNTER + 1)
mkdir -p /data/$ForCOUNTER
parted -s -a optimal ${ForDiskName[$i]}
mklabel gpt — mkpart primary 512s 100% sleep 2
mkfs.xfs -f $(echo “${ForDiskName[$i]}1”)
Uuid=$(blkid | grep $(echo “${ForDiskName[$i]}1”) | awk ‘{print $2}’ | sed ‘s/”//g’)
echo “$Uuid /data/$ForCOUNTER xfs defaults,noatime 1 2” >> /etc/fstab
mount /data/$ForCOUNTER
xfs_io -x -c resblks /data/$ForCOUNTER

done

NOTE: copy-paste may add some extra Characters, I Am Not an expert in scripting 🙂

Share this Story:
  • facebook
  • twitter
  • gplus

About bentech4u

Leave a comment