1 # Copyright 1999-2018 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
4 # XXX: we need to review menu.lst vs grub.conf handling. We've been converting
5 # all systems to grub.conf (and symlinking menu.lst to grub.conf), but
6 # we never updated any of the source code (it still all wants menu.lst),
7 # and there is no indication that upstream is making the transition.
9 # If you need to roll a new grub-static distfile, here is how.
10 # - Robin H. Johnson <robbat2@gentoo.org> - 29 Nov 2010
11 # FEATURES='-noauto -noinfo -nodoc -noman -splitdebug nostrip' \
12 # USE='static -ncurses -netboot -custom-cflags' \
13 # PORTAGE_COMPRESS=true GRUB_STATIC_PACKAGE_BUILDING=1 ebuild \
14 # grub-${PVR}.ebuild clean package && \
15 # qtbz2 -s -j ${PKGDIR}/${CAT}/${PF}.tbz2 && \
16 # mv ${PF}.tar.bz2 ${DISTDIR}/grub-static-${PVR}.tar.bz2
20 inherit eutils mount-boot toolchain-funcs linux-info flag-o-matic autotools pax-utils
22 PATCHVER="1.15" # Should match the revision ideally
23 DESCRIPTION="GNU GRUB Legacy boot loader"
24 HOMEPAGE="https://www.gnu.org/software/grub/"
25 SRC_URI="mirror://gentoo/${P}.tar.gz
26 mirror://gnu-alpha/${PN}/${P}.tar.gz
27 mirror://gentoo/splash.xpm.gz
28 mirror://gentoo/${P}-patches-${PATCHVER}.tar.bz2
29 https://dev.gentoo.org/~whissi/dist/${PN}/${P}-patches-${PATCHVER}.tar.bz2"
33 KEYWORDS="amd64 x86 ~x86-fbsd"
34 IUSE="custom-cflags ncurses netboot static"
36 LIB_DEPEND="ncurses? ( >=sys-libs/ncurses-5.9-r3:0[static-libs(+),abi_x86_32(-)] )"
37 RDEPEND="!static? ( ${LIB_DEPEND//[static-libs(+),/=[} )"
40 static? ( ${LIB_DEPEND} )"
41 RDEPEND+=" !sys-boot/grub:2[-multislot]"
45 amd64) CONFIG_CHECK='~IA32_EMULATION' check_extra_config ;;
50 # Grub will not handle a kernel larger than EXTENDED_MEMSIZE Mb as
51 # discovered in bug 160801. We can change this, however, using larger values
52 # for this variable means that Grub needs more memory to run and boot. For a
53 # kernel of size N, Grub needs (N+1)*2. Advanced users should set a custom
54 # value in make.conf, it is possible to make kernels ~16Mb in size, but it
55 # needs the kitchen sink built-in.
57 if [[ -z ${GRUB_MAX_KERNEL_SIZE} ]] ; then
59 amd64) GRUB_MAX_KERNEL_SIZE=9 ;;
60 x86) GRUB_MAX_KERNEL_SIZE=5 ;;
64 einfo "Grub will support the ${t} maximum kernel size of ${GRUB_MAX_KERNEL_SIZE} Mb (GRUB_MAX_KERNEL_SIZE)"
67 -e "/^#define.*EXTENDED_MEMSIZE/s,3,${GRUB_MAX_KERNEL_SIZE},g" \
68 "${S}"/grub/asmstub.c \
71 # gcc-6 and above doesnt have a '-nopie' option patched in, use upstream's -fno-pie
72 if [[ tc-is-gcc && $(gcc-major-version) -gt 5 ]]; then
73 sed -i 's/-nopie/-fno-pie/' \
74 "${WORKDIR}"/patch/860_all_grub-0.97-pie-r1.patch \
78 EPATCH_SUFFIX="patch" epatch "${WORKDIR}"/patch
80 epatch "${FILESDIR}"/grub-0.97-Add-esp-to-list-of-clobbered-registers.patch
81 epatch "${FILESDIR}"/grub-0.97-ncurses-pkgconfig.patch
85 rm -f "${S}"/aclocal.m4 # seems to keep bug 418287 away
90 filter-flags -fPIE #168834
92 use amd64 && multilib_toolchain_setup x86
94 unset BLOCK_SIZE #73499
96 ### i686-specific code in the boot loader is a bad idea; disabling to ensure
97 ### at least some compatibility if the hard drive is moved to an older or
98 ### incompatible system.
100 # grub-0.95 added -fno-stack-protector detection, to disable ssp for stage2,
101 # but the objcopy's (faulty) test fails if -fstack-protector is default.
102 # create a cache telling configure that objcopy is ok, and add -C to econf
103 # to make use of the cache.
105 # CFLAGS has to be undefined running econf, else -fno-stack-protector detection fails.
106 # STAGE2_CFLAGS is not allowed to be used on emake command-line, it overwrites
107 # -fno-stack-protector detected by configure, removed from netboot's emake.
108 use custom-cflags || unset CFLAGS
110 tc-ld-disable-gold #439082 #466536 #526348
112 export grub_cv_prog_objcopy_absolute=yes #79734
113 use static && append-ldflags -static
115 if use amd64 && use static ; then
116 if [[ -n ${GRUB_STATIC_PACKAGE_BUILDING} ]] ; then
117 eerror "You have set GRUB_STATIC_PACKAGE_BUILDING. This"
118 eerror "is specifically intended for building the tarballs for the"
119 eerror "grub-static package via USE='static -ncurses'."
120 eerror "All bets are now off."
124 # build the net-bootable grub first, but only if "netboot" is set
125 if use netboot ; then
126 mkdir -p "${WORKDIR}"/netboot
127 pushd "${WORKDIR}"/netboot >/dev/null
131 --datadir=/usr/lib/grub \
133 --disable-auto-linux-mem-opt \
135 --enable-{3c{5{03,07,09,29,95},90x},cs89x0,davicom,depca,eepro{,100}} \
136 --enable-{epic100,exos205,ni5210,lance,ne2100,ni{50,65}10,natsemi} \
137 --enable-{ne,ns8390,wd,otulip,rtl8139,sis900,sk-g16,smc9000,tiara} \
138 --enable-{tulip,via-rhine,w89c840}
142 # Now build the regular grub
143 # Note that FFS and UFS2 support are broken for now - stage1_5 files too big
146 --datadir=/usr/lib/grub \
148 --disable-auto-linux-mem-opt \
149 $(use_with ncurses curses)
151 # sanity check due to common failure
152 use ncurses && ! grep -qs "HAVE_LIBCURSES.*1" config.h && die "USE=ncurses but curses not found"
156 use netboot && emake -C "${WORKDIR}"/netboot w89c840_o_CFLAGS="-O"
161 # non-default block size also give false pass/fails.
167 local _stage2_file="${S}/stage2/stage2"
168 if [[ -f "${_stage2_file}" ]]; then
169 local _stage2_filesize=$(stat -c '%s' "${_stage2_file}" 2>/dev/null)
170 if [[ -z "${_stage2_filesize}" ]]; then
171 ewarn "Sanity check skipped -- 'stat' returned nothing"
172 elif [[ "${_stage2_filesize}" -gt 1000000 ]]; then
173 eerror "Sanity check failed: stage2 (${_stage2_file}) is larger than 1MB (${_stage2_filesize} bytes)!"
174 eerror "Please check your CFLAGS and/or file a bug report at https://bugs.gentoo.org."
175 die "stage2 sanity check failed"
177 einfo "stage2 sanity check passed"
180 ewarn "Sanity check skipped -- no stage2 found"
184 if use netboot ; then
185 exeinto /usr/lib/grub/${CHOST}
186 doexe "${WORKDIR}"/netboot/stage2/{nbgrub,pxegrub}
187 newexe "${WORKDIR}"/netboot/stage2/stage2 stage2.netboot
190 pax-mark -m "${D}"/sbin/grub #330745
192 newdoc docs/menu.lst grub.conf.sample
193 dodoc "${FILESDIR}"/grub.conf.gentoo
195 [[ -n ${GRUB_STATIC_PACKAGE_BUILDING} ]] && \
196 mv "${D}"/usr/share/doc/{${PF},grub-static-${PF/grub-}}
198 insinto /usr/share/grub
199 doins "${DISTDIR}"/splash.xpm.gz
204 local dir=${boot_dir}
207 [[ ! -L ${dir}/boot ]] && ln -s . "${dir}/boot"
209 if [[ ! -e ${dir} ]] ; then
210 mkdir "${dir}" || die
213 # change menu.lst to grub.conf
214 if [[ ! -e ${dir}/grub.conf ]] && [[ -e ${dir}/menu.lst ]] ; then
215 mv -f "${dir}"/menu.lst "${dir}"/grub.conf
216 ewarn "*** IMPORTANT NOTE: menu.lst has been renamed to grub.conf"
220 if [[ ! -e ${dir}/menu.lst ]]; then
221 einfo "Linking from new grub.conf name to menu.lst"
222 ln -snf grub.conf "${dir}"/menu.lst
225 if [[ -e ${dir}/stage2 ]] ; then
226 mv "${dir}"/stage2{,.old}
227 ewarn "*** IMPORTANT NOTE: you must run grub and install"
228 ewarn "the new version's stage1 to your MBR. Until you do,"
229 ewarn "stage1 and stage2 will still be the old version, but"
230 ewarn "later stages will be the new version, which could"
231 ewarn "cause problems such as an unbootable system."
233 ewarn "This means you must use either grub-install or perform"
234 ewarn "root/setup manually."
236 ewarn "For more help, see the wiki:"
237 ewarn "https://wiki.gentoo.org/wiki/GRUB"
241 einfo "Copying files from /lib/grub and /usr/share/grub to ${dir}"
243 "${ROOT}"/lib*/grub/*/* \
244 "${ROOT}"/usr/share/grub/* ; do
245 [[ -f ${x} ]] && cp -p "${x}" "${dir}"/
248 if [[ ! -e ${dir}/grub.conf ]] ; then
249 s="${ROOT}/usr/share/doc/${PF}/grub.conf.gentoo"
250 [[ -e "${s}" ]] && cat "${s}" >${dir}/grub.conf
251 [[ -e "${s}.gz" ]] && zcat "${s}.gz" >${dir}/grub.conf
252 [[ -e "${s}.bz2" ]] && bzcat "${s}.bz2" >${dir}/grub.conf
255 # Per bug 218599, we support grub.conf.install for users that want to run a
256 # specific set of Grub setup commands rather than the default ones.
257 grub_config=${dir}/grub.conf.install
258 [[ -e ${grub_config} ]] || grub_config=${dir}/grub.conf
259 if [[ -e ${grub_config} ]] ; then
261 -v '^[[:space:]]*(#|$|default|fallback|initrd|password|splashimage|timeout|title)' \
264 --device-map="${dir}"/device.map \
268 # the grub default commands silently piss themselves if
269 # the default file does not exist ahead of time
270 if [[ ! -e ${dir}/default ]] ; then
271 grub-set-default --root-directory="${boot_dir}" default
273 einfo "Grub has been installed to ${boot_dir} successfully."
277 mount-boot_mount_boot_partition
279 if [[ -n ${DONT_MOUNT_BOOT} ]]; then
280 elog "WARNING: you have DONT_MOUNT_BOOT in effect, so you must apply"
281 elog "the following instructions for your /boot!"
282 elog "Neglecting to do so may cause your system to fail to boot!"
285 setup_boot_dir "${ROOT}"/boot
286 # Trailing output because if this is run from pkg_postinst, it gets mixed into
290 elog "To interactively install grub files to another device such as a USB"
291 elog "stick, just run the following and specify the directory as prompted:"
292 elog " emerge --config =${PF}"
293 elog "Alternately, you can export GRUB_ALT_INSTALLDIR=/path/to/use to tell"
294 elog "grub where to install in a non-interactive way."
296 # needs to be after we call setup_boot_dir
297 mount-boot_pkg_postinst
302 if [ ! -d "${GRUB_ALT_INSTALLDIR}" ]; then
303 einfo "Enter the directory where you want to setup grub:"
306 dir="${GRUB_ALT_INSTALLDIR}"
308 setup_boot_dir "${dir}"