Mint Serena 64 bit using the Mate desktop environment. Most of the following scripts will run on this machine, although some have only been tested with the Gnome desktop. Extra software requirements can be met by the Ubuntu and safe PPA repositories. Scripts may be re-written or added to from time to time, the date serves as the version number. Syntax highlighting on these pages is provided by GVim's "Convert to HTML" option.

Clear swap partition

# Filename: Clear-swap
# Author: robz
# Version: 280611
# One for the "nautilus-scripts" directory use to clear the swap partition.
# In this script I'm assuming you use a separate partition for your swap and
# that that swap uses a UUID to identify it, have a look in your /etc/fstab.
# It works by dismounting the swap file, writing zeros to the partition it
# occupied then remounting again with the original parameters.
# Enhanced dd "dcfldd" will be needed for this, "sudo apt-get install dcfldd"
# Using "dcfldd if=/dev/zero" will clear swap fairly rapidly, an option for the
# paranoid being "dcfldd if=/dev/urandom" :)

C2="\033[1m\E[32m" C6="\033[1m\E[36m" C7="\033[1m\E[37m"    # green cyan white.
TOTL=$(awk '/SwapTotal/{print $2/1024}' /proc/meminfo)      # Swap size MBs.

# Run a terminal and execute all in a root shell.
gnome-terminal --title "Clear Swap Partition" --geometry=62x11+0+24 -x\
    sudo bash -c "\
    ${LCTN=$(awk 'NR==2 {print $1}' /proc/swaps)};\
    ${UUID=$(awk -F'[= ]' '/^UUID.*swap/{print $2}' /etc/fstab)};\
    tput clear; tput civis;\
    if [[ -z $LCTN || -z $UUID ]]; then\
        echo -e '$C6''\n[Error] Can'\''t read location and/or UUID, exiting.';\
        read -t10; exit 1;\
    echo -ne '$C6''Write ''$C7''$TOTL''MiBs''$C6'' of zeros to ';\
    echo -e 'swap space on partition ''$C7''$LCTN''$C6\n';\
    swapoff -a;\
    trap 'killall dcfldd 2> /dev/null; mkswap -U $UUID $LCTN;\
    swapon -a; sleep 5; exit' INT TERM EXIT;\
    dcfldd if=/dev/zero of=$LCTN statusinterval=2 bs=10M conv=notrunc;\
    echo -ne '$C2''\nAll Done.....''$C6''\nRe-mounting swap on partition: ';\
    echo -e '$C7''$LCTN''$C6''\nusing UUID: ''$C7''$UUID\n''$C6';\
    read -t5"

# TODO this needs zero the free space code adding to it for an all singing all
# dancing version.

No comments: