Advertisement
Guest User

clear-swap-corrupt.sh

a guest
Dec 22nd, 2015
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.84 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. ###########################
  4. # Clear Swap Application  #
  5. # Patrick L. Francis      #
  6. # Last Updated 1/30/2011  #
  7. ###########################
  8. #
  9. #############################################################################
  10. # Modified by IRQ42                                                         #
  11. # Added feature to corrupt remnant data on swap partitions/files before     #
  12. # turning swap back on as well as bug fixes.    Work in progress ...        #
  13. # Dec. 2015                                                                 #
  14. #############################################################################
  15.  
  16.  
  17.  
  18. ## make sure user is root before running app
  19.  
  20. if [[ $EUID -ne 0 ]]; then
  21.     clear
  22.     echo
  23.     echo
  24.     echo -e "..............................................."
  25.     echo -e ".. You must be \033[1mroot\033[0m to do this .."
  26.     echo -e "..............................................."
  27.     echo
  28.     echo
  29.     echo
  30.     sleep 5 # why waste 5 seconds of my time?
  31.     clear
  32.     exit 1
  33. fi
  34.  
  35.  
  36.  
  37. # Wipe out remnant data
  38. corrupt_data() {
  39.     for f in $1
  40.     do
  41.     # regex accepts UUID versions 1 through 5
  42.     regex_uuid='[a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12}'
  43.  
  44.         echo "..............................................."
  45.         echo "..Corrupting remnant swap data on $f ..        "
  46.         echo "..Do not interrupt until complete!!............"
  47.         echo "..............................................."
  48.  
  49.         # see if we are dealing with a block device
  50.         if [[ $(stat $f | egrep -c 'block') -eq 1 ]]
  51.         then
  52.             # get UUID of swap fs .. swap must be on before doing so
  53.             uuid=$(blkid -c /dev/null -o list | grep "$f" | egrep -o $regex_uuid)
  54.  
  55.             # get size of block device
  56.             fsize=$(blockdev --getsize64 $f)
  57.  
  58.             # swapoff before writing
  59.             echo -e "swapoff UUID: $uuid\t$f"
  60.             swapoff -U $uuid
  61.             sync
  62.  
  63.             # write FSIZE bytes of garbage to device
  64.             head -c $fsize $garbage_stream > $f
  65.             sync
  66.  
  67.             # mkswap and restore original UUID
  68.             mkswap --uuid $uuid $f
  69.  
  70.         # have to swapon devices in loop for some reason
  71.         echo -e "swapon UUID: $uuid\t$f"
  72.             swapon -U $uuid
  73.  
  74.         # see if we are dealing with a regular file
  75.         elif [[ $(stat $f | egrep -c 'regular') -eq 1 ]]
  76.         then
  77.             # get size of regular file
  78.             fsize=$(stat $f | grep 'Size:' | awk '{print $2}')
  79.  
  80.             # swapoff before writing
  81.             echo -e "swapoff swap file: $f"
  82.             swapoff $f
  83.             sync
  84.  
  85.             #write FSIZE bytes of garbage to file
  86.             head -c $fsize $garbage_stream> $f
  87.             sync
  88.  
  89.             # turn swap file back on
  90.             echo -e "swapon swap file $f"
  91.             swapon $f
  92.  
  93.         # anything else we print an error message
  94.         else
  95.             echo "E: $f is unrecognized file type, can't write (not safe)"
  96.         fi
  97.  
  98.     done
  99. }
  100.  
  101. do_corrupt=1
  102.  
  103. # check for too many arguments
  104. if [[ $# -gt 1 ]]
  105. then
  106.     echo "$0: too many arguments. $0 -h for info"
  107.  
  108.     exit 1
  109. # check for empty first arg
  110. elif [[ $# -eq 0 || -z $1 ]] # || $(egrep -c '\s' $1) -gt 0 ]]
  111. then
  112.     do_corrupt=0
  113.  
  114.     ## do to bugs, this option is not currently supported
  115.     echo "Sorry, this option is not yet supported"
  116.     echo -e "Try \x1b[35m$0 /dev/zero\x1b[0m for speed"
  117.  
  118.     exit 0
  119. # show help
  120. elif [[ $(echo $1 | egrep -c '\s?-h$') -eq 1 ]]
  121. then
  122.     echo "SYNOPSIS:"
  123.     echo "$0 [specialfile]"
  124.     echo
  125.     echo "Specify a character device to read data from to corrupt remnant data."
  126.     echo "e.g. $0 /dev/urandom"
  127.     echo
  128.     echo "If no character device to read from is specified, swap will be cleared but"
  129.     echo "media will not be overwritten."
  130.  
  131.     exit 0
  132. elif [[ $(stat $1 | egrep -c 'character special file') -ne 1 ]]
  133. then
  134.     echo "E: $1 is not a character special file. Quitting!"
  135.  
  136.     exit 1
  137. fi
  138.  
  139. garbage_stream=$1
  140.  
  141. echo -e "..............................................."
  142. echo -e ".. How do things look before we do anything? .."
  143. echo -e "..............................................."
  144. free
  145. echo -e "........................................."
  146. echo -e ".. Let's sync up before we get started .."
  147. echo -e "........................................."
  148. echo -e "."
  149. sync
  150. echo -e "."
  151. echo -e "......................................................"
  152. echo -e ".. Let's dump our drop_caches and see how that does .."
  153. echo -e "......................................................"
  154. echo -e "."
  155. echo 3 > /proc/sys/vm/drop_caches
  156. #echo 0 > /proc/sys/vm/drop_caches ## not needed due to above, causes issues.
  157. free
  158. echo -e "."
  159. echo -e "...................................................."
  160. echo -e "..Let's compare free physical memory to swap size .."
  161. echo -e "...................................................."
  162.  
  163. ## How much free memory do we have?
  164.  
  165. physical=`free|grep Mem:|awk '{print $4}'`
  166.  
  167. ## How large is swap?
  168.  
  169. swap=`free|grep Swap:|awk '{print $3}'`
  170.  
  171. ## Is the amount of free physical memory less than size of swap
  172. ## If so, push it to phyiscal memory... if it is not, then exit.
  173.  
  174. ## I avoid any type of math in bash and use awk for comparison operators
  175.  
  176. compare=`echo "$physical $swap good bad" | awk '{if ($1 > $2) print $3; else print $4}'`
  177.  
  178. if [ "$compare" == "good" ]; then
  179.  
  180.     # get path(s) for swaps
  181.     swaps=$(cat /proc/swaps | egrep '\/' | awk '{print $1}')
  182.  
  183.     echo -e "."
  184.     echo -e "..................................................."
  185.     echo -e ".. sufficient physical memory, clearing swap now .."
  186.     echo -e "..................................................."
  187.     ## turn swaps off, then back on
  188.     echo -e "."
  189.     echo -e "......................"
  190.     echo -e ".. turning swaps off .."
  191.     echo -e "......................"
  192.     echo -e "."
  193.    
  194.  
  195.     # after swapoff, corrupt remnant data on disk
  196.     if [[ $do_corrupt -eq 1 ]]
  197.     then
  198.         corrupt_data $swaps
  199.     else
  200.         swapoff -a # this needs to be fixed proper .. wtf were you thinking when you wrote this?
  201.     fi
  202.  
  203.     echo -e "."
  204.     echo -e "....................."
  205.     echo -e ".. turning swap on .."
  206.     echo -e "....................."
  207.     echo -e "."
  208.     swapon -a  # this doesn't work in all cases, you need to do them explicitly!!
  209.     echo -e "."
  210.     echo -e "..................................................."
  211.     echo -e ".. swap has been freed, here is how it looks now .."
  212.     echo -e "..................................................."
  213.     echo
  214.     echo
  215.     free
  216. else
  217.     echo
  218.     echo
  219.     echo -e ".............................................."
  220.     echo -e ".. kill the app, not enough physical memory .."
  221.     echo -e ".............................................."
  222.     echo
  223.     echo
  224.     exit 1
  225. fi
  226.  
  227. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement