Категории

пятница, 5 сентября 2014 г.

Скрипт для исправления проблем в Ubuntu GRUB в 1 клик.

Скрипт для исправления проблем в GRUB в 1 клик. Возможно применение в автоматизированных скриптах.
#!/bin/sh

load_default_grub() {
 . /etc/default/grub
}

get_yes_no() {
 while [ true ]; do
  echo -n "$1 (Y/N) ? "
  read a
  echo
  if [ $? != 0 ]; then
   a='No'; return;
  fi
  case $a in
   [Yy][Ee][Ss]|[Yy]) a='Yes'; return;;
   [Nn][Oo]|[Nn]) a='No'; return;;
   *) ;;
  esac
 done
}

# Load config and uncomment variable.
load_default_grub
if (grep -qwai '^#GRUB_DISABLE_RECOVERY=' /etc/default/grub >/dev/null); then
 echo 'INFO: Uncomment the GRUB_DISABLE_RECOVERY variable.'
 sed -i 's/^#GRUB_DISABLE_RECOVERY=\(.*\)/GRUB_DISABLE_RECOVERY=\1/' /etc/default/grub
fi
# Load config again, check value and set if need.
load_default_grub
if [ "$GRUB_DISABLE_RECOVERY" != 'true' ]; then
 if [ -z "$GRUB_DISABLE_RECOVERY" ]; then
  echo 'INFO: Add GRUB_DISABLE_RECOVERY="true"'
  echo 'GRUB_DISABLE_RECOVERY="true"' >> /etc/default/grub
 else
  echo 'INFO: Set GRUB_DISABLE_RECOVERY to "true"'
  sed -i 's/^GRUB_DISABLE_RECOVERY=.*/GRUB_DISABLE_RECOVERY="true"/' /etc/default/grub
 fi
fi

# Delete deprecated options GRUB_HIDDEN_TIMEOUT*.
if (grep -qwai '^GRUB_HIDDEN_TIMEOUT.*' /etc/default/grub >/dev/null); then
 echo 'INFO: Delete deprecated options GRUB_HIDDEN_TIMEOUT*'
 sed -i '/^GRUB_HIDDEN_TIMEOUT.*/d' /etc/default/grub
fi

# Add option GRUB_RECORDFAIL_TIMEOUT=15 for automatic boot on fail.
if ! (grep -qwai '^GRUB_RECORDFAIL_TIMEOUT=.*' /etc/default/grub >/dev/null); then
 echo 'INFO: Add option GRUB_RECORDFAIL_TIMEOUT=15 for automatic boot on fail.'
 echo 'GRUB_RECORDFAIL_TIMEOUT=15' >> /etc/default/grub
fi


#GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"
if (grep -qwai '^GRUB_CMDLINE_LINUX_DEFAULT=.*' /etc/default/grub >/dev/null); then
 # Load config again, check value and set if need.
 load_default_grub
 for i in $GRUB_CMDLINE_LINUX_DEFAULT; do
  case "$i" in
   'splash') i='nosplash' ;;
   'quiet') i='noquiet' ;;
  esac
  if [ -n "$GRUB_CMDLINE_LINUX_DEFAULT_OUT" -a -n "$i" ]; then
   GRUB_CMDLINE_LINUX_DEFAULT_OUT="$GRUB_CMDLINE_LINUX_DEFAULT_OUT $i"
  else
   GRUB_CMDLINE_LINUX_DEFAULT_OUT="$i"
  fi
 done
 sed -i -e "s/^GRUB_CMDLINE_LINUX_DEFAULT=.*/GRUB_CMDLINE_LINUX_DEFAULT=\"${GRUB_CMDLINE_LINUX_DEFAULT_OUT}\"/" /etc/default/grub
fi

#Finnaly re-generate grub.cfg.
get_yes_no "Do you want to run update-grub"
[ $a = 'Yes' ] && update-grub