10_linux.in 9.14 KB
Newer Older
1 2
#! /bin/sh
set -e
3

4
# grub-mkconfig helper script.
5
# Copyright (C) 2006,2007,2008,2009,2010  Free Software Foundation, Inc.
6
#
7 8 9
# GRUB is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
10 11
# (at your option) any later version.
#
12 13 14 15
# GRUB is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
16 17
#
# You should have received a copy of the GNU General Public License
18
# along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
19

20 21 22 23
prefix="@prefix@"
exec_prefix="@exec_prefix@"
datarootdir="@datarootdir@"

24
. "$pkgdatadir/grub-mkconfig_lib"
25

Robert Millan's avatar
Robert Millan committed
26
export TEXTDOMAIN=@PACKAGE@
27
export TEXTDOMAINDIR="@localedir@"
Robert Millan's avatar
Robert Millan committed
28

29 30
CLASS="--class gnu-linux --class gnu --class os"

31
if [ "x${GRUB_DISTRIBUTOR}" = "x" ] ; then
32 33 34
  OS=GNU/Linux
else
  OS="${GRUB_DISTRIBUTOR} GNU/Linux"
35
  CLASS="--class $(echo ${GRUB_DISTRIBUTOR} | tr 'A-Z' 'a-z' | cut -d' ' -f1|LC_ALL=C sed 's,[^[:alnum:]_],_,g') ${CLASS}"
36 37
fi

38 39 40 41 42 43 44 45
# loop-AES arranges things so that /dev/loop/X can be our root device, but
# the initrds that Linux uses don't like that.
case ${GRUB_DEVICE} in
  /dev/loop/*|/dev/loop[0-9])
    GRUB_DEVICE=`losetup ${GRUB_DEVICE} | sed -e "s/^[^(]*(\([^)]\+\)).*/\1/"`
  ;;
esac

46 47
# btrfs may reside on multiple devices. We cannot pass them as value of root= parameter
# and mounting btrfs requires user space scanning, so force UUID in this case.
48
if [ "x${GRUB_DEVICE_UUID}" = "x" ] || [ "x${GRUB_DISABLE_LINUX_UUID}" = "xtrue" ] \
49
    || ! test -e "/dev/disk/by-uuid/${GRUB_DEVICE_UUID}" \
50
    || ( test -e "${GRUB_DEVICE}" && uses_abstraction "${GRUB_DEVICE}" lvm ); then
51
  LINUX_ROOT_DEVICE=${GRUB_DEVICE}
52 53 54 55
else
  LINUX_ROOT_DEVICE=UUID=${GRUB_DEVICE_UUID}
fi

56
case x"$GRUB_FS" in
57
    xbtrfs)
58
	rootsubvol="`make_system_path_relative_to_its_root /`"
59 60 61 62 63
	rootsubvol="${rootsubvol#/}"
	if [ "x${rootsubvol}" != x ]; then
	    GRUB_CMDLINE_LINUX="rootflags=subvol=${rootsubvol} ${GRUB_CMDLINE_LINUX}"
	fi;;
    xzfs)
64
	rpool=`${grub_probe} --device ${GRUB_DEVICE} --target=fs_label 2>/dev/null || true`
65
	bootfs="`make_system_path_relative_to_its_root / | sed -e "s,@$,,"`"
66
	LINUX_ROOT_DEVICE="ZFS=${rpool}${bootfs}"
67
	;;
68
esac
69

70 71
title_correction_code=

72 73
linux_entry ()
{
Robert Millan's avatar
Robert Millan committed
74 75
  os="$1"
  version="$2"
76
  type="$3"
Robert Millan's avatar
Robert Millan committed
77
  args="$4"
78

79 80 81
  if [ -z "$boot_device_id" ]; then
      boot_device_id="$(grub_get_device_id "${GRUB_DEVICE}")"
  fi
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
  if [ x$type != xsimple ] ; then
      case $type in
	  recovery)
	      title="$(gettext_printf "%s, with Linux %s (recovery mode)" "${os}" "${version}")" ;;
	  *)
	      title="$(gettext_printf "%s, with Linux %s" "${os}" "${version}")" ;;
      esac
      if [ x"$title" = x"$GRUB_ACTUAL_DEFAULT" ] || [ x"Previous Linux versions>$title" = x"$GRUB_ACTUAL_DEFAULT" ]; then
	  replacement_title="$(echo "Advanced options for ${OS}" | sed 's,>,>>,g')>$(echo "$title" | sed 's,>,>>,g')"
	  quoted="$(echo "$GRUB_ACTUAL_DEFAULT" | grub_quote)"
	  title_correction_code="${title_correction_code}if [ \"x\$default\" = '$quoted' ]; then default='$(echo "$replacement_title" | grub_quote)'; fi;"
	  grub_warn "$(gettext_printf "Please don't use old title \`%s' for GRUB_DEFAULT, use \`%s' (for versions before 2.00) or \`%s' (for 2.00 or later)" "$GRUB_ACTUAL_DEFAULT" "$replacement_title" "gnulinux-advanced-$boot_device_id>gnulinux-$version-$type-$boot_device_id")"
      fi
      echo "menuentry '$(echo "$title" | grub_quote)' ${CLASS} \$menuentry_id_option 'gnulinux-$version-$type-$boot_device_id' {" | sed "s/^/$submenu_indentation/"
  else
      echo "menuentry '$(echo "$os" | grub_quote)' ${CLASS} \$menuentry_id_option 'gnulinux-simple-$boot_device_id' {" | sed "s/^/$submenu_indentation/"
  fi      
  if [ x$type != xrecovery ] ; then
100
      save_default_entry | grub_add_tab
101
  fi
102 103 104

  # Use ELILO's generic "efifb" when it's known to be available.
  # FIXME: We need an interface to select vesafb in case efifb can't be used.
105
  if [ "x$GRUB_GFXPAYLOAD_LINUX" = x ]; then
106
      echo "	load_video" | sed "s/^/$submenu_indentation/"
107 108
      if grep -qx "CONFIG_FB_EFI=y" "${config}" 2> /dev/null \
	  && grep -qx "CONFIG_VT_HW_CONSOLE_BINDING=y" "${config}" 2> /dev/null; then
109
	  echo "	set gfxpayload=keep" | sed "s/^/$submenu_indentation/"
110 111
      fi
  else
112
      if [ "x$GRUB_GFXPAYLOAD_LINUX" != xtext ]; then
113
	  echo "	load_video" | sed "s/^/$submenu_indentation/"
114
      fi
115
      echo "	set gfxpayload=$GRUB_GFXPAYLOAD_LINUX" | sed "s/^/$submenu_indentation/"
116 117
  fi

118
  echo "	insmod gzio" | sed "s/^/$submenu_indentation/"
119

120
  if [ x$dirname = x/ ]; then
121
    if [ -z "${prepare_root_cache}" ]; then
122
      prepare_root_cache="$(prepare_grub_to_access_device ${GRUB_DEVICE} | grub_add_tab)"
123
    fi
124
    printf '%s\n' "${prepare_root_cache}" | sed "s/^/$submenu_indentation/"
125 126
  else
    if [ -z "${prepare_boot_cache}" ]; then
127
      prepare_boot_cache="$(prepare_grub_to_access_device ${GRUB_DEVICE_BOOT} | grub_add_tab)"
128
    fi
129
    printf '%s\n' "${prepare_boot_cache}" | sed "s/^/$submenu_indentation/"
130
  fi
131
  message="$(gettext_printf "Loading Linux %s ..." ${version})"
132
  sed "s/^/$submenu_indentation/" << EOF
133
	echo	'$(echo "$message" | grub_quote)'
Robert Millan's avatar
Robert Millan committed
134
	linux	${rel_dirname}/${basename} root=${linux_root_device_thisversion} ro ${args}
135 136
EOF
  if test -n "${initrd}" ; then
137
    # TRANSLATORS: ramdisk isn't identifier. Should be translated.
138
    message="$(gettext_printf "Loading initial ramdisk ...")"
139
    sed "s/^/$submenu_indentation/" << EOF
140
	echo	'$(echo "$message" | grub_quote)'
141 142 143
	initrd	${rel_dirname}/${initrd}
EOF
  fi
144
  sed "s/^/$submenu_indentation/" << EOF
145 146 147 148
}
EOF
}

149 150 151
machine=`uname -m`
case "x$machine" in
    xi?86 | xx86_64)
152 153 154 155
	list=
	for i in /boot/vmlinuz-* /vmlinuz-* /boot/kernel-* ; do
	    if grub_file_is_not_garbage "$i" ; then list="$list $i" ; fi
	done ;;
156
    *) 
157 158 159 160
	list=
	for i in /boot/vmlinuz-* /boot/vmlinux-* /vmlinuz-* /vmlinux-* /boot/kernel-* ; do
                  if grub_file_is_not_garbage "$i" ; then list="$list $i" ; fi
	done ;;
161 162 163 164
esac

case "$machine" in
    i?86) GENKERNEL_ARCH="x86" ;;
165 166 167
    mips|mips64) GENKERNEL_ARCH="mips" ;;
    mipsel|mips64el) GENKERNEL_ARCH="mipsel" ;;
    arm*) GENKERNEL_ARCH="arm" ;;
168
    *) GENKERNEL_ARCH="$machine" ;;
169 170
esac

171
prepare_boot_cache=
172
prepare_root_cache=
173
boot_device_id=
174 175 176 177 178
title_correction_code=

# Extra indentation to add to menu entries in a submenu. We're not in a submenu
# yet, so it's empty. In a submenu it will be equal to '\t' (one tab).
submenu_indentation=""
179

180
is_top_level=true
181
while [ "x$list" != "x" ] ; do
182
  linux=`version_find_latest $list`
183
  gettext_printf "Found linux image: %s\n" "$linux" >&2
184 185
  basename=`basename $linux`
  dirname=`dirname $linux`
186
  rel_dirname=`make_system_path_relative_to_its_root $dirname`
187
  version=`echo $basename | sed -e "s,^[^0-9]*-,,g"`
188
  alt_version=`echo $version | sed -e "s,\.old$,,g"`
189
  linux_root_device_thisversion="${LINUX_ROOT_DEVICE}"
190 191

  initrd=
192
  for i in "initrd.img-${version}" "initrd-${version}.img" "initrd-${version}.gz" \
193 194
	   "initrd-${version}" "initramfs-${version}.img" \
	   "initrd.img-${alt_version}" "initrd-${alt_version}.img" \
195 196
	   "initrd-${alt_version}" "initramfs-${alt_version}.img" \
	   "initramfs-genkernel-${version}" \
197 198 199
	   "initramfs-genkernel-${alt_version}" \
	   "initramfs-genkernel-${GENKERNEL_ARCH}-${version}" \
	   "initramfs-genkernel-${GENKERNEL_ARCH}-${alt_version}"; do
200 201 202 203 204
    if test -e "${dirname}/${i}" ; then
      initrd="$i"
      break
    fi
  done
205

206
  config=
207
  for i in "${dirname}/config-${version}" "${dirname}/config-${alt_version}" "/etc/kernels/kernel-config-${version}" ; do
208 209
    if test -e "${i}" ; then
      config="${i}"
210 211 212 213
      break
    fi
  done

214 215 216 217 218
  initramfs=
  if test -n "${config}" ; then
      initramfs=`grep CONFIG_INITRAMFS_SOURCE= "${config}" | cut -f2 -d= | tr -d \"`
  fi

219
  if test -n "${initrd}" ; then
220
    gettext_printf "Found initrd image: %s\n" "${dirname}/${initrd}" >&2
221
  elif test -z "${initramfs}" ; then
222
    # "UUID=" and "ZFS=" magic is parsed by initrd or initramfs.  Since there's
223
    # no initrd or builtin initramfs, it can't work here.
224
    linux_root_device_thisversion=${GRUB_DEVICE}
225 226
  fi

227
  if [ "x$is_top_level" = xtrue ] && [ "x${GRUB_DISABLE_SUBMENU}" != xy ]; then
228 229 230
    linux_entry "${OS}" "${version}" simple \
    "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT}"

231
    submenu_indentation="$grub_tab"
232 233 234 235 236 237
    
    if [ -z "$boot_device_id" ]; then
	boot_device_id="$(grub_get_device_id "${GRUB_DEVICE}")"
    fi
    # TRANSLATORS: %s is replaced with an OS name
    echo "submenu '$(gettext_printf "Advanced options for %s" "${OS}" | grub_quote)' \$menuentry_id_option 'gnulinux-advanced-$boot_device_id' {"
238
    is_top_level=false
239 240 241 242
  fi

  linux_entry "${OS}" "${version}" advanced \
              "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT}"
243
  if [ "x${GRUB_DISABLE_RECOVERY}" != "xtrue" ]; then
244 245
    linux_entry "${OS}" "${version}" recovery \
                "single ${GRUB_CMDLINE_LINUX}"
246
  fi
247

248
  list=`echo $list | tr ' ' '\n' | fgrep -vx "$linux" | tr '\n' ' '`
249
done
250 251 252

# If at least one kernel was found, then we need to
# add a closing '}' for the submenu command.
253
if [ x"$is_top_level" != xtrue ]; then
254 255 256 257
  echo '}'
fi

echo "$title_correction_code"