scanall.sh 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #!/bin/bash
  2. # scanall.sh
  3. # Scans for bad sectors on all devices on system, lists bad sectors on a file
  4. # and runs fsck for bad sectors when found.
  5. # CAUTION: It is not well-tested, so please use carefully. Please report bugs if
  6. # found.
  7. # Download, run `chmod +x /path/to/scanall.sh`, run `sudo ./scanall.sh`
  8. if [ "$(id -u)" -ne 0 ]; then echo 'Please run as root.' >&2; exit 1; fi
  9. device_lines=$(lsblk -o path,uuid,mountpoint -nl)
  10. badsectorfile='/tmp/badsectors.txt'
  11. echo "$device_lines" | while read line ; do
  12. # Find scannable FSs
  13. # Lines that have a UUID and not mounted
  14. device=$(echo "$line" | awk '{print $1}')
  15. uuid=$(echo "$line" | awk '{print $2}')
  16. mountpoint=$(echo "$line" | awk '{print $3}')
  17. if [ ! -z "$uuid" ] && [ -z "$mountpoint" ]; then
  18. echo "=== Scanning $device for bad sectors..."
  19. sudo badblocks -v "$device" > "$badsectorfile"
  20. if [ -s "$badsectorfile" ]; then
  21. echo "--- $device has bad sectors, running fsck..."
  22. sudo fsck -l "$badsectorfile" "$device"
  23. rm "$badsectorfile"
  24. else
  25. echo "--- $device does not have bad sectors, skipping fsck..."
  26. fi
  27. else
  28. if [ ! -z "$mountpoint" ]; then
  29. echo "=== Skipping $device because it's already mounted"
  30. else
  31. echo "=== Skipping $device"
  32. fi
  33. fi
  34. done
  35. rm "$badsectorfile"