barjac

barjac

Jan 18th, 2010
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.43 KB | None | 0 0
  1. #!/bin/bash
  2. # swapfix.sh
  3.  
  4. # This tool checks that the swap file on a system is consistent with what is expected
  5. # by the current initrd, fstab and menu.lst.
  6. # It reports current problems and inconsistencies.
  7. # When run with the -fix command line argument it attempts to fix all
  8. # the problems it finds.
  9. # It will correct the UUID entry for swap in fstab.
  10. # It will correct the UUID entries for resume in menu.lst
  11. # It will then re-create initrds from the corrected fstab entry.
  12. # It assumes that UUIDs are in use thoughout and does not support other systems
  13. # such as labels or device IDs, so use with care!
  14. ###############################################################################
  15.  
  16. # Check that we are running with root privelage.
  17. if [ $UID -ne 0 ];then
  18.    echo "Sorry, you must run this as superuser."
  19.    echo "Use su"
  20.    exit 1
  21. fi
  22. # Warn when using -fix option.
  23. if [ "$1" == "-fix" ];then
  24.     echo "######  You are running this script with the -fix option !  #######"
  25.     echo "###### This will modify your system and could cause damage. #######"
  26.     echo "######          You use this at your own risk.              #######"
  27.     echo "###### The original fstab and menu.lst will be saved as     #######"
  28.     echo "######    /boot/grub/menu.lst_orig and /etc/fstab_orig.     #######"
  29.     echo "######       Are you sure you want to continue ?            #######"
  30.     echo "######      Type YES to continue. (Case sensitive).         #######"    
  31.     read confirm
  32.       if [ $confirm != "YES" ];then
  33.     exit 1
  34.       fi
  35. # Back up fstab and menu.lst unless already done.
  36.   if [ -a /etc/fstab_orig ]
  37.   then
  38.   echo "Note /etc/fstab_orig already exists"
  39.   else
  40.     cp /etc/fstab /etc/fstab_orig
  41.   fi
  42.   if [ -a /boot/grub/menu.lst_orig ]
  43.   then
  44.   echo "Note /boot/grub/menu.lst_orig already exists"
  45.   else
  46.     cp /boot/grub/menu.lst /boot/grub/menu.lst_orig
  47.   fi
  48. fi
  49.  
  50. #Check for multiple swap files.
  51. swaps=$(blkid |grep swap|wc -l)
  52. if [ $swaps -gt 1 ];then
  53.  
  54. # Check if swap has already been chosen from multiple swap partitions.
  55.   if [ -a /tmp/blkidswap.txt ]
  56.   then
  57.     blkidswap=$(cat /tmp/blkidswap.txt)
  58.   else
  59. # Ask user to choose a swap.
  60.     echo "You have more than one swap file on this system at present"
  61.     echo "Look at the following and select the one you want to use by"
  62.     echo "typing the first 6 or 7 characters of the UUID followed by ENTER"
  63.     echo "You will not be asked for this again on subsequent"
  64.     echo "runs of swapfix.sh unless /tmp/blkidswap.txt is deleted."
  65.     blkid |grep swap
  66.     echo "Please select "
  67.     read swap_partial
  68.     blkidswap=$(blkid |grep $swap_partial|cut -d'"' -f 2)
  69.     echo $blkidswap > /tmp/blkidswap.txt
  70.     echo "OK - Using "$blkidswap" as the swap."
  71.    fi
  72. else
  73.  
  74. # Get the single swap UUID from blkid
  75. blkidswap=$(blkid |grep swap|cut -d'"' -f 2)
  76.  
  77. fi
  78.  
  79. # Get swap UUID in fstab
  80. fstabswap=$(sed -e '/^#/d' /etc/fstab | grep swap | cut -d= -f2 | cut -d" " -f1)
  81. # Get swap UUID from menu.lst
  82. menuswap=$(sed -e '/^#/d' /boot/grub/menu.lst | grep -m1 resume | cut -d"=" -f6 | cut -d" " -f1)
  83. echo
  84. echo "Swap UUID reported by blkid   "$blkidswap
  85. echo "Swap UUID in the fstab entry  "$fstabswap
  86. echo "Swap UUID in menu.lst resumes "$menuswap
  87. echo
  88. # Check that fstab swap UUID is the same as that shown by blkid.
  89. if [ $blkidswap == $fstabswap ];then
  90.     echo "Swap UUID in fstab correctly refers to an existing swap partition."
  91. else
  92.     echo "Swap UUID in fstab does not exist on this system."
  93. # If "-fix" passed as parameter then modify fstab
  94.       if [ "$1" == "-fix" ];then
  95.       echo "Fixing fstab now..."
  96.       sed -i "s/$fstabswap/$blkidswap/" /etc/fstab
  97.       # Get swap UUID from fstab agian after fixing
  98.       fstabswap=$(sed -e '/^#/d' /etc/fstab | grep swap | cut -d= -f2 | cut -d" " -f1)
  99.       echo "Swap in fstab has been changed to "$fstabswap
  100.       else
  101.       echo "Running <swapchk.sh -fix> will replace fstab swap UUID with blkid swap UUID."
  102.       fi
  103. fi
  104.  
  105. if [ $blkidswap == $menuswap ];then
  106.     echo "Resume UUID in menu.lst correctly refers to an existing swap partition."
  107. else
  108.     echo "Resume UUID in menu.lst does not exist on this system."
  109. # If "-fix" passed as parameter then modify menu.lst
  110.       if [ "$1" == "-fix" ];then
  111.       echo "Fixing menu.lst now..."
  112.       sed -i "s/$menuswap/$blkidswap/" /boot/grub/menu.lst
  113. # Get swap UUID from menu.lst agian now it has been fixed.
  114.       menuswap=$(sed -e '/^#/d' /boot/grub/menu.lst | grep -m1 resume | cut -d"=" -f6 | cut -d" " -f1)
  115.       echo "Swap (resume=) in menu.lst has been changed to "$menuswap
  116.       else
  117.       echo "Running <swapchk.sh -fix> will replace all resume entries with the swap UUID reported by blkid."
  118.       fi
  119. fi
  120.  
  121. # if initrd contains the same UUID as blkid then $initrd holds UUID - else ""
  122. initrd=$(lsinitrd /boot/initrd.img | grep -m1 $blkidswap |cut -d" " -f3 | cut -d= -f2)
  123.  
  124. if [ "$initrd" == "$blkidswap" ];then    
  125.     echo "The entry for swap UUID in initrd corresponds correctly with the one reported by blkid."
  126.     if [ $initrd != $fstabswap ];then
  127.         echo "However this currently differs from the swap UUID shown in fstab."
  128.         echo "Running <swapchk.sh -fix> will correct fstab."
  129.     fi
  130. else
  131.     echo "There is no reference in initrd to the swap UUID reported by blkid."
  132.           if [ "$1" == "-fix" ];then
  133.       echo "Fixing initrds please wait..."
  134.       bootloader-config --action rebuild-initrds
  135.       else
  136.       echo "Running <swapchk.sh -fix> will rebuild initrds to correct this."
  137.       fi
  138. fi
  139.  
  140. echo "Done"
Add Comment
Please, Sign In to add comment