Skip to content

Instantly share code, notes, and snippets.

@raelga
Last active December 19, 2015 18:39
Show Gist options
  • Save raelga/6000743 to your computer and use it in GitHub Desktop.
Save raelga/6000743 to your computer and use it in GitHub Desktop.
Divide and look for badblocks
#!/bin/bash
if [ -z $1 ]
then
echo "Usage: $0 sdx"
exit
elif [ `echo $1 | grep -c '/dev/'` -eq 0 ]
then
dev_path="/dev/$1"
dev_name="$1"
else
dev_path="$1"
dev_name=`echo $1 | sed 's@/dev/@@'`
fi
if [ ! -e $dev_path ]
then
echo "Disck $dev_path not exists."
exit
elif [ `mount | grep -c $dev_path` -gt 0 ]
then
echo "Disk $dev_path is mounted."
exit
fi
echo "The selected disck is $dev_path."
read -n 1 -p "?? Continue? (Y/N)? " answer > /dev/null
case "$answer" in
[Nn]* ) echo "Nothing to do here..."; exit;;
* ) echo "Yai!";;
esac
size="$(echo `fdisk -l ${dev_path}| sed -n 's/Disk \/dev.* \([0-9]*\) bytes/\1/p' 2>/dev/null` / 1024^3 | bc)"
parted -s ${dev_path} mktable gpt;
for i in `seq 0 100 ${size}`;
do
dev_part_name="${dev_name}`expr $i / 100 + 1`";
dev_part_path="/dev/${dev_part_name}"
dev_part_badb="/root/${dev_part_name}.bb"
echo "#### ${dev_part_name} ($i `expr $i + 100`) # ${dev_part_badb}";
parted -s ${dev_path} mkpart BB-$i ${i}GB `expr $i + 100`GB;
badblocks -b 4096 -c 512 -p 3 -s -v -w -o ${dev_part_badb} ${dev_part_path};
mkfs.ext4 -j -m 0 -O dir_index,extent,has_journal,large_file,sparse_super -b 4096 -L badblocks-$i -l ${dev_part_badb} ${dev_part_path};
done;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment