Advertisement
rs232

usbfix

Oct 3rd, 2023 (edited)
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. #!/bin/sh -x
  2. # usbfix v0.4 rs232
  3. PID=$$
  4. export PATH=/bin:/usr/bin:/sbin:/usr/sbin:/home/root:
  5. alias logi='logger -p INFO -t usbfix[$PID]'
  6. [ ! $1 ] && days=30 || days="$1"
  7. secs=$((((days*24)*60)*60))
  8. now=$(date +%s)
  9. chk=$(nvram get usb_chk)
  10. [ $(echo "$chk" | wc -c ) -lt 2 ] && chk=0
  11. [ "$chk" -lt $((now-secs)) ] && {
  12. blkid | grep -Eo 'sd[a-d][0-9]' | while read device; do
  13. fs=$(mount | grep "^\/dev\/$device\ on\ \/tmp\/" | awk '{print $5}')
  14. freeme() {
  15. for process in $(lsof | grep '\/tmp\/mnt\/' | awk '{print $1}'); do
  16. id=$(ps | grep $process | grep -v 'grep' | awk '{print $1}')
  17. prog=$(ps | grep $process | grep -v grep | awk '{print $5" "$6" "$7" "$8" "$9" "$10" "$11" "$12" "$13" "$14" "$15}')
  18. kill "${id}";
  19. echo "${prog}" >> /tmp/prog
  20. done
  21. sleep 1
  22. umount -f /dev/$device
  23. sleep 1
  24. }
  25. rerun() {
  26. chmod +x /tmp/prog
  27. /tmp/prog
  28. rm /tmp/prog
  29. }
  30. echo "${fs}" | grep -q 'ntfs\|fuseblk' && {
  31. logi "Reparing /dev/$device with filesystem $fs"
  32. freeme
  33. /usr/sbin/ntfsck /dev/"$device"
  34. rerun
  35. }
  36. echo "${fs}" | grep -qi 'fat' && {
  37. logi "Reparing /dev/$device with filesystem $fs"
  38. # If you have FAT and know the repair command for it please share
  39. }
  40. echo "${fs}" | grep -Eq "ext2\|ext3\|ext4" && {
  41. logi "Reparing /dev/$device with filesystem $fs"
  42. freeme
  43. /usr/sbin/e2fsck -p -y -c /dev/"$device"
  44. rerun
  45. }
  46. echo "${fs}" | grep -q 'hpfs\|hfp\|hfs' && {
  47. logi "Reparing /dev/$device with filesystem $fs"
  48. freeme
  49. # Command needs to be checked!
  50. /usr/sbin/fsck.hfsplus /dev/"$device"
  51. rerun
  52. }
  53. echo "${fs}" | grep -qi "zfs" && {
  54. logi "Reparing /dev/$device with filesystem $fs"
  55. freeme
  56. [ $(zpool list | grep -v "NAME\|no\ pools\ available" | wc -l) -ne 0 ] && {
  57. zpool list | grep -v "NAME\|no\ pools\ available" | while read line; do
  58. pname=$(echo $line | awk '{print $1}')
  59. zpool scrub $pname
  60. done
  61. }
  62. rerun
  63. # If you have zfs and know the repair command for it please share
  64. }
  65. done
  66. nvram set usb_chk=$now
  67. nvram commit
  68. sleep 1
  69. # reboot
  70. # should be reboot in production to allow volumes to mount properly
  71. } || {
  72. logi "usbfix skipped ($days days have not gone past yet)"
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement