Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # swapfix.sh
- # This tool checks that the swap file on a system is consistent with what is expected
- # by the current initrd, fstab and menu.lst.
- # It reports current problems and inconsistencies.
- # When run with the -fix command line argument it attempts to fix all
- # the problems it finds.
- # It will correct the UUID entry for swap in fstab.
- # It will correct the UUID entries for resume in menu.lst
- # It will then re-create initrds from the corrected fstab entry.
- # It assumes that UUIDs are in use thoughout and does not support other systems
- # such as labels or device IDs, so use with care!
- ###############################################################################
- # Check that we are running with root privelage.
- if [ $UID -ne 0 ];then
- echo "Sorry, you must run this as superuser."
- echo "Use su"
- exit 1
- fi
- # Warn when using -fix option.
- if [ "$1" == "-fix" ];then
- echo "###### You are running this script with the -fix option ! #######"
- echo "###### This will modify your system and could cause damage. #######"
- echo "###### You use this at your own risk. #######"
- echo "###### The original fstab and menu.lst will be saved as #######"
- echo "###### /boot/grub/menu.lst_orig and /etc/fstab_orig. #######"
- echo "###### Are you sure you want to continue ? #######"
- echo "###### Type YES to continue. (Case sensitive). #######"
- read confirm
- if [ $confirm != "YES" ];then
- exit 1
- fi
- # Back up fstab and menu.lst unless already done.
- if [ -a /etc/fstab_orig ]
- then
- echo "Note /etc/fstab_orig already exists"
- else
- cp /etc/fstab /etc/fstab_orig
- fi
- if [ -a /boot/grub/menu.lst_orig ]
- then
- echo "Note /boot/grub/menu.lst_orig already exists"
- else
- cp /boot/grub/menu.lst /boot/grub/menu.lst_orig
- fi
- fi
- #Check for multiple swap files.
- swaps=$(blkid |grep swap|wc -l)
- if [ $swaps -gt 1 ];then
- # Check if swap has already been chosen from multiple swap partitions.
- if [ -a /tmp/blkidswap.txt ]
- then
- blkidswap=$(cat /tmp/blkidswap.txt)
- else
- # Ask user to choose a swap.
- echo "You have more than one swap file on this system at present"
- echo "Look at the following and select the one you want to use by"
- echo "typing the first 6 or 7 characters of the UUID followed by ENTER"
- echo "You will not be asked for this again on subsequent"
- echo "runs of swapfix.sh unless /tmp/blkidswap.txt is deleted."
- blkid |grep swap
- echo "Please select "
- read swap_partial
- blkidswap=$(blkid |grep $swap_partial|cut -d'"' -f 2)
- echo $blkidswap > /tmp/blkidswap.txt
- echo "OK - Using "$blkidswap" as the swap."
- fi
- else
- # Get the single swap UUID from blkid
- blkidswap=$(blkid |grep swap|cut -d'"' -f 2)
- fi
- # Get swap UUID in fstab
- fstabswap=$(sed -e '/^#/d' /etc/fstab | grep swap | cut -d= -f2 | cut -d" " -f1)
- # Get swap UUID from menu.lst
- menuswap=$(sed -e '/^#/d' /boot/grub/menu.lst | grep -m1 resume | cut -d"=" -f6 | cut -d" " -f1)
- echo
- echo "Swap UUID reported by blkid "$blkidswap
- echo "Swap UUID in the fstab entry "$fstabswap
- echo "Swap UUID in menu.lst resumes "$menuswap
- echo
- # Check that fstab swap UUID is the same as that shown by blkid.
- if [ $blkidswap == $fstabswap ];then
- echo "Swap UUID in fstab correctly refers to an existing swap partition."
- else
- echo "Swap UUID in fstab does not exist on this system."
- # If "-fix" passed as parameter then modify fstab
- if [ "$1" == "-fix" ];then
- echo "Fixing fstab now..."
- sed -i "s/$fstabswap/$blkidswap/" /etc/fstab
- # Get swap UUID from fstab agian after fixing
- fstabswap=$(sed -e '/^#/d' /etc/fstab | grep swap | cut -d= -f2 | cut -d" " -f1)
- echo "Swap in fstab has been changed to "$fstabswap
- else
- echo "Running <swapchk.sh -fix> will replace fstab swap UUID with blkid swap UUID."
- fi
- fi
- if [ $blkidswap == $menuswap ];then
- echo "Resume UUID in menu.lst correctly refers to an existing swap partition."
- else
- echo "Resume UUID in menu.lst does not exist on this system."
- # If "-fix" passed as parameter then modify menu.lst
- if [ "$1" == "-fix" ];then
- echo "Fixing menu.lst now..."
- sed -i "s/$menuswap/$blkidswap/" /boot/grub/menu.lst
- # Get swap UUID from menu.lst agian now it has been fixed.
- menuswap=$(sed -e '/^#/d' /boot/grub/menu.lst | grep -m1 resume | cut -d"=" -f6 | cut -d" " -f1)
- echo "Swap (resume=) in menu.lst has been changed to "$menuswap
- else
- echo "Running <swapchk.sh -fix> will replace all resume entries with the swap UUID reported by blkid."
- fi
- fi
- # if initrd contains the same UUID as blkid then $initrd holds UUID - else ""
- initrd=$(lsinitrd /boot/initrd.img | grep -m1 $blkidswap |cut -d" " -f3 | cut -d= -f2)
- if [ "$initrd" == "$blkidswap" ];then
- echo "The entry for swap UUID in initrd corresponds correctly with the one reported by blkid."
- if [ $initrd != $fstabswap ];then
- echo "However this currently differs from the swap UUID shown in fstab."
- echo "Running <swapchk.sh -fix> will correct fstab."
- fi
- else
- echo "There is no reference in initrd to the swap UUID reported by blkid."
- if [ "$1" == "-fix" ];then
- echo "Fixing initrds please wait..."
- bootloader-config --action rebuild-initrds
- else
- echo "Running <swapchk.sh -fix> will rebuild initrds to correct this."
- fi
- fi
- echo "Done"
Add Comment
Please, Sign In to add comment