Cleandisk : a utility to wipe unused disk space

From: Robert Binkley (leebinkley_at_YAHOO.COM)
Date: 09/14/05

  • Next message: Andrew J. Forgue: "Re: Problem witj LPP file"
    Date:         Wed, 14 Sep 2005 12:59:44 -0700
    To: aix-l@Princeton.EDU
    
    

    ##############################################
    Havent used but located script to wipedisk
    ################################################
    #!/bin/bash
    #
    # Cleandisk : a utility to wipe unused disk space
    #
    # Version 0.1
    # Author : Iain Roberts
    # Date : 25th October 2003
    #

    # This is a little shell script to wipe disk space
    which is unused but may
    # still have scraps of deleted files. It is designed
    to be run on a running
    # system. It does a number of overwrites specified
    when the script is run.
    # Each overwrite uses dd and writes first from
    /dev/zero and then from
    # /dev/urandom.

    # The problem with this approach in a shell script is
    that I can't see how to
    # overwrite all unused disk space without risking
    filling up the whole
    # partition and so potentially causing problems on the
    system. The way I've
    # done it is to write the random/zero data to files.
    As the available space
    # gets smaller, the files get smaller to fill up more
    of it. The script
    # won't let the space run out completely, though,
    which means that there
    # will always be a small amount of disk space which
    never gets overwritten.

    # Oh - and it's damn slow too.

    # This script is provided with absolutely no warranty,
    nor any guarantee that
    # it won't wreck your system. Use it with care.

    #------------#
    # Variables #
    #------------#

    FS=$1
    typeset -i OVERWRITES=$2
    typeset -i CHUNKS=0
    typeset -i CSIZE=0

    #------------#
    # Functions #
    #------------#

    cleanup() {
           echo "Cleaning up files created by cleandisk."
           echo "Now deleting files $FS/cleandisk/*"
           /bin/rm -r $FS/cleandisk
    }

    msgcat() {
    MSG[0]="Cleandisk completed. Only $FSFREE KB free
    space was left in $FS"
    MSG[1]="$FS/cleandisk already exists...exiting"
    MSG[2]="You must specify a number of overwrites. I
    recommend between 1 and 10"
    MSG[3]="dd if=/dev/zero has failed. Please
    investigate."
    MSG[4]="dd if=/dev/urandom has failed. Please
    investigate."
    MSG[5]="$FS is not a partition mount point."
    MSG[6]="Usage : cleandisk <mount_point>
    <overwrite_count>. e.g. cleandisk /home 3"
    MSG[7]="$FS/cleandisk exists. Please delete and try
    again."

    echo "${MSG[$1]}"
    exit $1
    }

    #-----------#
    # Main body #
    #-----------#

    [[ $OVERWRITES -lt 1 ]] && msgcat 2

    if df $FS|awk '{print $6}' |grep -q "^${FS}$"
      then
        typeset -i FSFREE=$(df -k $FS|grep " $FS$"|awk
    '{print $4}')

        # You might have a directory called cleandisk with
    important files in
        # so, to be safe, I'll exit if you've got a
    cleandisk directory.
        # Just manually delete the directory and re-run
    script.
        if [ -e $FS/cleandisk ]
          then
            msgcat 7
          else
            echo "Creating directory $FS/cleandisk"
            mkdir $FS/cleandisk
        fi

        # This while loop runs once for each chunk
        while [ $FSFREE -gt 2048 ]
          do
            echo " $FSFREE KB left in $FS to wipe clean"

            # Figure out best chunk size (1MB/10MB/100MB)
            # Always leave > 2xchunk size free (e.g. if
    chunk size is 10MB, at least
            # 20MB must exist to be wiped, so after wipe
    at least 10MB will be free
            # This helps protect against filesystems filling up
    from other
            # processes

            if [ $FSFREE -gt 204800 ]
              then
                      CSIZE=10240 # 100MB
             elif [ $FSFREE -gt 20480 ]
              then
                      CSIZE=1024 # 10MB
             elif [ $FSFREE -gt 2048 ]
              then
                      CSIZE=102 # 1MB(ish)
             else
                     cleanup
                     msgcat 0
             fi

            typeset -i MB=CSIZE/102
            typeset -i COUNT=0

            echo " Now writing a chunk of $MB MB (may
    take some time)"
            printf " Overwrite count --> "

            # This while loop runs once for each overwrite per
    chunk
            while [ $COUNT -lt $OVERWRITES ]
              do
               ((COUNT+=1))
                dd if=/dev/zero of=$FS/cleandisk/$CHUNKS bs=10240
    count=$CSIZE >/dev/null 2>&1|| msgcat 3
                dd if=/dev/urandom of=$FS/cleandisk/$CHUNKS
    bs=10240 count=$CSIZE >/dev/null 2>&1|| msgcat 4
               printf "$COUNT"
              done
            # End of loop running once per overwrite per
    chunk

            FSFREE=$(df -k $FS|grep " $FS$"|awk '{print
    $4}')
            ((CHUNKS+=1))
            echo -e "\n$CHUNKS chunk(s) have now been written."
           done
           # End of loop running once per chunk

        else
         msgcat 5
    fi
               
    cleanup
    msgcat 0

    Robert Lee Binkley leebinkley@yahoo.com
     IBM AIX Specialist Certified
     HP-UX System Administration Certified
    Work It's Nice To Be Important But It's More Important To Be Nice


  • Next message: Andrew J. Forgue: "Re: Problem witj LPP file"

    Relevant Pages