Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #
- ###########################
- # Clear Swap Application #
- # Patrick L. Francis #
- # Last Updated 1/30/2011 #
- ###########################
- #
- #############################################################################
- # Modified by IRQ42 #
- # Added feature to corrupt remnant data on swap partitions/files before #
- # turning swap back on as well as bug fixes. Work in progress ... #
- # Dec. 2015 #
- #############################################################################
- ## make sure user is root before running app
- if [[ $EUID -ne 0 ]]; then
- clear
- echo
- echo
- echo -e "..............................................."
- echo -e ".. You must be \033[1mroot\033[0m to do this .."
- echo -e "..............................................."
- echo
- echo
- echo
- sleep 5 # why waste 5 seconds of my time?
- clear
- exit 1
- fi
- # Wipe out remnant data
- corrupt_data() {
- for f in $1
- do
- # regex accepts UUID versions 1 through 5
- 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}'
- echo "..............................................."
- echo "..Corrupting remnant swap data on $f .. "
- echo "..Do not interrupt until complete!!............"
- echo "..............................................."
- # see if we are dealing with a block device
- if [[ $(stat $f | egrep -c 'block') -eq 1 ]]
- then
- # get UUID of swap fs .. swap must be on before doing so
- uuid=$(blkid -c /dev/null -o list | grep "$f" | egrep -o $regex_uuid)
- # get size of block device
- fsize=$(blockdev --getsize64 $f)
- # swapoff before writing
- echo -e "swapoff UUID: $uuid\t$f"
- swapoff -U $uuid
- sync
- # write FSIZE bytes of garbage to device
- head -c $fsize $garbage_stream > $f
- sync
- # mkswap and restore original UUID
- mkswap --uuid $uuid $f
- # have to swapon devices in loop for some reason
- echo -e "swapon UUID: $uuid\t$f"
- swapon -U $uuid
- # see if we are dealing with a regular file
- elif [[ $(stat $f | egrep -c 'regular') -eq 1 ]]
- then
- # get size of regular file
- fsize=$(stat $f | grep 'Size:' | awk '{print $2}')
- # swapoff before writing
- echo -e "swapoff swap file: $f"
- swapoff $f
- sync
- #write FSIZE bytes of garbage to file
- head -c $fsize $garbage_stream> $f
- sync
- # turn swap file back on
- echo -e "swapon swap file $f"
- swapon $f
- # anything else we print an error message
- else
- echo "E: $f is unrecognized file type, can't write (not safe)"
- fi
- done
- }
- do_corrupt=1
- # check for too many arguments
- if [[ $# -gt 1 ]]
- then
- echo "$0: too many arguments. $0 -h for info"
- exit 1
- # check for empty first arg
- elif [[ $# -eq 0 || -z $1 ]] # || $(egrep -c '\s' $1) -gt 0 ]]
- then
- do_corrupt=0
- ## do to bugs, this option is not currently supported
- echo "Sorry, this option is not yet supported"
- echo -e "Try \x1b[35m$0 /dev/zero\x1b[0m for speed"
- exit 0
- # show help
- elif [[ $(echo $1 | egrep -c '\s?-h$') -eq 1 ]]
- then
- echo "SYNOPSIS:"
- echo "$0 [specialfile]"
- echo
- echo "Specify a character device to read data from to corrupt remnant data."
- echo "e.g. $0 /dev/urandom"
- echo
- echo "If no character device to read from is specified, swap will be cleared but"
- echo "media will not be overwritten."
- exit 0
- elif [[ $(stat $1 | egrep -c 'character special file') -ne 1 ]]
- then
- echo "E: $1 is not a character special file. Quitting!"
- exit 1
- fi
- garbage_stream=$1
- echo -e "..............................................."
- echo -e ".. How do things look before we do anything? .."
- echo -e "..............................................."
- free
- echo -e "........................................."
- echo -e ".. Let's sync up before we get started .."
- echo -e "........................................."
- echo -e "."
- sync
- echo -e "."
- echo -e "......................................................"
- echo -e ".. Let's dump our drop_caches and see how that does .."
- echo -e "......................................................"
- echo -e "."
- echo 3 > /proc/sys/vm/drop_caches
- #echo 0 > /proc/sys/vm/drop_caches ## not needed due to above, causes issues.
- free
- echo -e "."
- echo -e "...................................................."
- echo -e "..Let's compare free physical memory to swap size .."
- echo -e "...................................................."
- ## How much free memory do we have?
- physical=`free|grep Mem:|awk '{print $4}'`
- ## How large is swap?
- swap=`free|grep Swap:|awk '{print $3}'`
- ## Is the amount of free physical memory less than size of swap
- ## If so, push it to phyiscal memory... if it is not, then exit.
- ## I avoid any type of math in bash and use awk for comparison operators
- compare=`echo "$physical $swap good bad" | awk '{if ($1 > $2) print $3; else print $4}'`
- if [ "$compare" == "good" ]; then
- # get path(s) for swaps
- swaps=$(cat /proc/swaps | egrep '\/' | awk '{print $1}')
- echo -e "."
- echo -e "..................................................."
- echo -e ".. sufficient physical memory, clearing swap now .."
- echo -e "..................................................."
- ## turn swaps off, then back on
- echo -e "."
- echo -e "......................"
- echo -e ".. turning swaps off .."
- echo -e "......................"
- echo -e "."
- # after swapoff, corrupt remnant data on disk
- if [[ $do_corrupt -eq 1 ]]
- then
- corrupt_data $swaps
- else
- swapoff -a # this needs to be fixed proper .. wtf were you thinking when you wrote this?
- fi
- echo -e "."
- echo -e "....................."
- echo -e ".. turning swap on .."
- echo -e "....................."
- echo -e "."
- swapon -a # this doesn't work in all cases, you need to do them explicitly!!
- echo -e "."
- echo -e "..................................................."
- echo -e ".. swap has been freed, here is how it looks now .."
- echo -e "..................................................."
- echo
- echo
- free
- else
- echo
- echo
- echo -e ".............................................."
- echo -e ".. kill the app, not enough physical memory .."
- echo -e ".............................................."
- echo
- echo
- exit 1
- fi
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement