Skip to content

Instantly share code, notes, and snippets.

@lonehack
Created May 25, 2017 03:58
Show Gist options
  • Save lonehack/6aec7415ecb4060512fd739cc405bc9a to your computer and use it in GitHub Desktop.
Save lonehack/6aec7415ecb4060512fd739cc405bc9a to your computer and use it in GitHub Desktop.
Shell script for disk repair
#!/bin/bash
YELLOW="\033[1;33m"
RED="\033[1;31m"
WHITE="\033[1;37m"
ENDCOLOR="\033[0m"
# Root Check
if [ $USER = root ]; then
echo -e $RED"Error: must be not root"$ENDCOLOR
echo "command : $0"
echo -e $YELLOW"Exiting..."$ENDCOLOR
exit 0
fi
# Listing Partition
echo -e $YELLOW"Listing partitions.."$ENDCOLOR
df -T
# Partition Repair
TRY=3
while [ $TRY -gt 0 ] ; do
# Entering partition parameter
echo -e $WHITE"Type partition (usually /dev/sdXX), then press [ENTER] :"$ENDCOLOR
read part
# Check partition existance
if df -T|grep $part; then
echo -e $YELLOW"Repairing $part partition..."$ENDCOLOR
TYPE=$(sudo blkid -o value -s TYPE $part)
MOUNT=$(df "$part" | tail -1 | awk '{ print $6 }')
# Unmount partition
echo -e $YELLOW"Unmounting $part partition..."$ENDCOLOR
if [ -n $(which udiskctl) ] ; then
echo "automount - OK"
udisksctl unmount -b $part
else
echo "Can't automount - using umount..."
umount $MOUNT
fi
# Repair partition
if [ $TYPE = "ntfs" ] ; then
echo -e $YELLOW"Repairing NTFS partition..."$ENDCOLOR
sudo ntfsfix -db $part
elif [ $TYPE = "vfat" ] ; then
echo -e $YELLOW"Repair FAT partition..."$ENDCOLOR
sudo dosfsck -w -r -l -a -v -t $part
elif [ $TYPE = "ext4" ] || [ $TYPE = "ext3" ] || [ $TYPE = "ext2" ] ; then
echo -e $YELLOW"Repair Linux partition..."$ENDCOLOR
sudo e2fsck -cfpv $part
else
echo -e $RED"Error : Partition format not supported!"$ENDCOLOR
fi
# Re-mount partition
echo -e $YELLOW"Re-Mounting $part partition..."$ENDCOLOR
if [ -n $(which udiskctl) ] ; then
udisksctl mount -b $part
echo "Re-mount success!"
else
echo "Failed to re-mount"
fi
echo -e $YELLOW"Partition repaired!"$ENDCOLOR
exit 0
else
echo -e $RED"Error : Partition $ENDCOLOR$YELLOW$part$ENDCOLOR$RED not found/mounted!"$ENDCOLOR
echo "Mount the partition first then type again"
TRY=`expr $TRY - 1`
fi
done
echo -e $RED"Too much error! Exiting program..."$ENDCOLOR
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment