wiper.sh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #!/usr/bin/env bash
  2. #variables:
  3. random_pass=7
  4. zeroing_pass=1
  5. prereq_list="pv smartmontools"
  6. #check that a disk has been provided:
  7. if [ -z "$1" ]
  8. then
  9. echo "Usage: \"./wiper.sh diskname\", for example: \"./wiper.sh sdb\""
  10. exit
  11. fi
  12. #check the disk exists:
  13. if [ ! -e "/dev/$1" ]; then
  14. echo "/dev/$1 does not exists. exiting."
  15. exit
  16. fi
  17. #prerequisites:
  18. for prereq in $prereq_list; do
  19. REQUIRED_PKG="$prereq"
  20. PKG_OK=$(dpkg-query -W --showformat='${Status}\n' $REQUIRED_PKG|grep "install ok installed")
  21. #echo Checking for $REQUIRED_PKG: $PKG_OK
  22. if [ "" = "$PKG_OK" ]; then
  23. echo "$REQUIRED_PKG is not present. Setting up $REQUIRED_PKG."
  24. sudo apt-get --yes install $REQUIRED_PKG
  25. fi
  26. done
  27. #collect data about disk type:
  28. disk_type=$(smartctl -a /dev/$1 | grep -i "Rotation Rate:" | cut -d':' -f2 | tr -d " ")
  29. #store if the disk is an ssd:
  30. if [[ $disk_type == *"SolidStateDevice"* ]]; then
  31. disk_ssd=1
  32. # echo "disk is ssd"
  33. elif [[ $disk_type == *"rpm"* ]]; then
  34. disk_ssd=0
  35. # echo "disk is not ssd"
  36. else
  37. echo "disk type unknown, exiting"
  38. exit
  39. fi
  40. #check for disk errors, differntiating by device type since smart output is different between sata and sas drives:
  41. if smartctl -a /dev/$1 | grep -q "SATA"; then
  42. #echo "Type of disk: SATA"
  43. if smartctl -a /dev/$1 | grep -q "No Errors Logged"; then
  44. echo "SATA no errors, conitnuing"
  45. else
  46. sata_model=$(smartctl -a /dev/$1 | grep -i "Device Model:" | cut -d':' -f2 | tr -d " ")
  47. sata_serial=$(smartctl -a /dev/$1 | grep -i "Serial number:" | cut -d':' -f2 | tr -d " ")
  48. echo "!!! ERRORS !!!"
  49. echo "SATA errors, aborting!!!"
  50. echo "NO WIPING NEEDED, JUST DESTROY THE DISK MECHANICALLY"
  51. echo "!!! EXITING !!!"
  52. echo ""
  53. echo "Model: $sata_model"
  54. echo "Serial: $sata_serial"
  55. exit
  56. fi
  57. elif smartctl -a /dev/$1 | grep -q "SAS"; then
  58. #echo "Type of disk: SAS"
  59. sas_errors=$(smartctl -a /dev/$1 | grep "Elements in grown defect list" | cut -d':' -f2 | tr -d " ")
  60. if [ "$sas_errors" -gt 0 ]; then
  61. sas_vendor=$(smartctl -a /dev/$1 | grep -i "Vendor:" | cut -d':' -f2 | tr -d " ")
  62. sas_model=$(smartctl -a /dev/$1 | grep -i "Product:" | cut -d':' -f2 | tr -d " ")
  63. sas_serial=$(smartctl -a /dev/$1 | grep -i "Serial number:" | cut -d':' -f2 | tr -d " ")
  64. echo "!!! ERRORS !!!"
  65. echo "Elements in grown defect list: " $sas_errors
  66. echo "NO WIPING NEEDED, JUST DESTROY THE DISK MECHANICALLY"
  67. echo "!!! EXITING !!!"
  68. echo ""
  69. echo "Vendor: $sas_vendor"
  70. echo "Model: $sas_model"
  71. echo "Serial: $sas_serial"
  72. exit
  73. else
  74. echo "SAS no errors, continuing"
  75. fi
  76. else
  77. echo "the disk type is none of the expected ones, exiting"
  78. exit
  79. fi
  80. #warning if is an ssd
  81. if [ "$disk_ssd" -eq 1 ]; then
  82. echo "WARNING, DISK IS AN SSD, Remember that sectors are reallocated thus unwanted data might remain on the sectors"
  83. #TODO: ATA Secure erase? https://grok.lsu.edu/article.aspx?articleid=16716
  84. fi
  85. #calculate disk bytes:
  86. disk_blocks=$(cat /proc/partitions | grep -w $1 | tr -s ' ' | cut -d " " -f4);
  87. disk_bytes=$(( 1024*disk_blocks ))
  88. #wipe:
  89. #see: https://serverfault.com/questions/6440/is-there-an-alternative-to-dev-urandom
  90. echo "wiping $1"
  91. for r_pass in $(seq 1 $random_pass); do
  92. echo "random pass $r_pass of $random_pass :"
  93. openssl enc -pbkdf2 -pass pass:"$(dd if=/dev/urandom bs=128 count=1 2>/dev/null | base64)" -nosalt </dev/zero | pv --progress --eta --rate --bytes --size $disk_bytes | dd of=/dev/$1 bs=2M oflag=direct iflag=fullblock
  94. done
  95. for z_pass in $(seq 1 $zeroing_pass); do
  96. echo "zeroing pass $z_pass of $zeroing_pass :"
  97. dd if=/dev/zero | pv --progress --eta --rate --bytes --size $disk_bytes | dd of=/dev/$1 bs=2M oflag=direct iflag=fullblock
  98. done
  99. echo "!!! FINISHED wiping $1 !!!"