2012-11-09 11:04:13 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2013-02-13 22:16:00 +00:00
|
|
|
# Configure grub. Note that the various conditionals here are to handle
|
|
|
|
# different distributions gracefully.
|
2012-11-09 11:04:13 +00:00
|
|
|
|
|
|
|
set -e
|
2013-04-02 00:25:25 +00:00
|
|
|
set -x
|
2012-11-09 11:04:13 +00:00
|
|
|
|
2013-04-02 00:25:25 +00:00
|
|
|
# FIXME:
|
|
|
|
[ -n "$IMAGE_BLOCK_DEVICE" ]
|
|
|
|
PART_DEV=$IMAGE_BLOCK_DEVICE
|
2013-08-27 10:00:03 +00:00
|
|
|
BOOT_DEV=$(echo $IMAGE_BLOCK_DEVICE | sed -e 's#p1##' | sed -e 's#mapper/##')
|
|
|
|
|
|
|
|
function install_extlinux {
|
|
|
|
install-packages extlinux
|
|
|
|
|
|
|
|
echo "Installing Extlinux..."
|
|
|
|
|
|
|
|
MBR=/usr/share/syslinux/mbr.bin
|
|
|
|
if [ ! -f $MBR ]; then
|
|
|
|
MBR=/usr/lib/syslinux/mbr.bin
|
|
|
|
if [ ! -f $MBR ]; then
|
|
|
|
echo "mbr.bin (from SYSLINUX) not found."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
dd if=$MBR of=$BOOT_DEV
|
|
|
|
|
|
|
|
mkdir -p /boot/syslinux
|
|
|
|
extlinux --install /boot/syslinux
|
|
|
|
|
|
|
|
if [ -f /etc/redhat-release ]; then
|
|
|
|
kernel=$(ls -1rv /boot/vmlinuz* | head -1)
|
|
|
|
initrd=$(ls -1rv /boot/initramfs* | head -1)
|
2013-10-11 12:39:16 +00:00
|
|
|
elif [ -f /etc/SuSE-release ]; then
|
|
|
|
kernel=$(ls -1rv /boot/vmlinuz* | head -1)
|
|
|
|
initrd=$(ls -1rv /boot/initrd* | head -1)
|
2013-08-27 10:00:03 +00:00
|
|
|
elif [ -f /etc/debian_version ]; then
|
|
|
|
kernel=$(ls -1rv /boot/vmlinuz*generic | head -1)
|
|
|
|
initrd=$(ls -1rv /boot/initrd*generic | head -1)
|
2014-02-28 14:56:41 +00:00
|
|
|
|
|
|
|
# in case files with "generic" suffix were not found, fall back to default
|
|
|
|
kernel=${kernel:-$(ls -1rv /boot/vmlinuz* | head -1)}
|
|
|
|
initrd=${initrd:-$(ls -1rv /boot/initrd* | head -1)}
|
2013-08-27 10:00:03 +00:00
|
|
|
else
|
|
|
|
echo "Unable to find kernel and initram"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
cat > /boot/syslinux/syslinux.cfg<<_EOF_
|
|
|
|
DEFAULT linux
|
|
|
|
|
|
|
|
LABEL linux
|
|
|
|
KERNEL $kernel
|
2014-03-25 13:24:43 +00:00
|
|
|
APPEND ro root=LABEL=cloudimg-rootfs console=tty0 console=ttyS0,115200
|
2013-08-27 10:00:03 +00:00
|
|
|
INITRD $initrd
|
|
|
|
_EOF_
|
|
|
|
}
|
|
|
|
|
|
|
|
function install_grub2 {
|
|
|
|
|
2013-09-14 18:06:08 +00:00
|
|
|
# Check for offline installation of grub
|
|
|
|
if [ -f "/tmp/grub/install" ] ; then
|
|
|
|
source /tmp/grub/install
|
|
|
|
else
|
|
|
|
install-packages grub-pc
|
|
|
|
fi
|
2013-08-27 10:00:03 +00:00
|
|
|
|
|
|
|
# XXX: grub-probe on the nbd0/loop0 device returns nothing - workaround, manually
|
|
|
|
# specify modules. https://bugs.launchpad.net/ubuntu/+source/grub2/+bug/1073731
|
|
|
|
GRUBNAME=`which grub-install` || echo "trying grub2-install"
|
|
|
|
if [ -z "$GRUBNAME" ]; then
|
|
|
|
GRUBNAME="bash -x `which grub2-install`"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# If no GRUB2 is found, fallback to extlinux
|
|
|
|
if [ -z "$GRUBNAME" ] || [ $($GRUBNAME --version | grep "0.97" | wc -l) -ne 0 ]; then
|
|
|
|
echo "No GRUB2 found. Fallback to Extlinux..."
|
|
|
|
install_extlinux
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "Installing GRUB2..."
|
|
|
|
|
2014-02-27 21:53:07 +00:00
|
|
|
GRUB_OPTS=${GRUB_OPTS:-""}
|
2013-08-27 10:00:03 +00:00
|
|
|
# XXX: This is buggy:
|
|
|
|
# - --target=i386-pc is invalid for non-i386/amd64 architectures
|
|
|
|
# - and for UEFI too.
|
|
|
|
# GRUB_OPTS="$GRUB_OPTS --target=i386-pc"
|
2014-03-27 16:57:18 +00:00
|
|
|
if [[ ! $GRUB_OPTS == *--target* ]] && [[ $($GRUBNAME --version) =~ ' 2.' ]]; then
|
2013-08-27 10:00:03 +00:00
|
|
|
# /sys/ comes from the host machine. If the host machine is using EFI
|
|
|
|
# but the image being built doesn't have EFI boot-images installed we
|
|
|
|
# should set the --target to use a BIOS-based boot-image.
|
|
|
|
#
|
|
|
|
# * --target tells grub what's the target platform
|
|
|
|
# * the boot images are placed in /usr/lib/grub/<cpu>-<platform>
|
|
|
|
# * i386-pc is used for BIOS-based machines
|
|
|
|
# http://www.gnu.org/software/grub/manual/grub.html#Installation
|
|
|
|
#
|
|
|
|
if [ -d /sys/firmware/efi ]; then
|
|
|
|
if [ ! -d /usr/lib/grub/*-efi ]; then
|
|
|
|
case $ARCH in
|
|
|
|
"x86_64"|"amd64")
|
|
|
|
GRUB_OPTS="$GRUB_OPTS --target=i386-pc"
|
|
|
|
;;
|
|
|
|
"i386")
|
|
|
|
target=i386-pc
|
|
|
|
if [ -e /proc/device-tree ]; then
|
|
|
|
for x in /proc/device-tree/*; do
|
|
|
|
if [ -e "$x" ]; then
|
|
|
|
target="i386-ieee1275"
|
|
|
|
fi
|
|
|
|
done
|
2013-07-12 16:50:39 +00:00
|
|
|
fi
|
2013-08-27 10:00:03 +00:00
|
|
|
GRUB_OPTS="$GRUB_OPTS --target=$target"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
fi
|
|
|
|
fi
|
2013-07-12 16:50:39 +00:00
|
|
|
fi
|
|
|
|
|
2013-08-27 10:00:03 +00:00
|
|
|
$GRUBNAME --modules="biosdisk part_msdos" $GRUB_OPTS $BOOT_DEV
|
2013-07-12 16:50:39 +00:00
|
|
|
|
2013-08-27 10:00:03 +00:00
|
|
|
# This might be better factored out into a per-distro 'install-bootblock'
|
|
|
|
# helper.
|
2013-12-12 11:55:39 +00:00
|
|
|
if [ -d /boot/grub2 ]; then
|
2013-08-27 10:00:03 +00:00
|
|
|
GRUB_CFG=/boot/grub2/grub.cfg
|
2013-12-12 11:55:39 +00:00
|
|
|
elif [ -d /boot/grub ]; then
|
|
|
|
GRUB_CFG=/boot/grub/grub.cfg
|
2013-08-27 10:00:03 +00:00
|
|
|
fi
|
2013-07-12 16:15:08 +00:00
|
|
|
|
2013-08-27 10:00:03 +00:00
|
|
|
DIST=`lsb_release -is`
|
|
|
|
[ -n "$DIST" ]
|
2013-12-12 11:55:39 +00:00
|
|
|
echo 'GRUB_TERMINAL=console' >>/etc/default/grub
|
|
|
|
echo 'GRUB_GFXPAYLOAD_LINUX=text' >>/etc/default/grub
|
|
|
|
GRUB_MKCONFIG="grub2-mkconfig -o $GRUB_CFG"
|
|
|
|
case $DIST in
|
|
|
|
'Ubuntu'|'Debian')
|
|
|
|
sed -i -e 's/\(^GRUB_CMDLINE_LINUX.*\)"$/\1 nofb nomodeset vga=normal"/' /etc/default/grub
|
|
|
|
GRUB_MKCONFIG=update-grub
|
|
|
|
;;
|
|
|
|
'Fedora')
|
|
|
|
echo 'GRUB_CMDLINE_LINUX="nofb nomodeset vga=normal"' >>/etc/default/grub
|
|
|
|
;;
|
|
|
|
'openSUSE project')
|
|
|
|
sed -i -e 's/\(^GRUB_CMDLINE_LINUX.*\)"$/\1 nofb nomodeset vga=normal"/' /etc/default/grub
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
$GRUB_MKCONFIG
|
|
|
|
|
2013-08-27 10:00:03 +00:00
|
|
|
RELEASE=`lsb_release -cs`
|
|
|
|
[ -n "$RELEASE" ]
|
|
|
|
# grub-mkconfig generates a config with the device in it,
|
|
|
|
# This shouldn't be needed, but old code has bugs
|
2014-03-27 16:57:18 +00:00
|
|
|
if [ $RELEASE = 'precise' ] || [ $RELEASE = 'wheezy' ]; then
|
2013-08-27 10:00:03 +00:00
|
|
|
sed -i "s%search --no.*%%" $GRUB_CFG
|
|
|
|
sed -i "s%set root=.*%set root=(hd0,1)%" $GRUB_CFG
|
|
|
|
fi
|
|
|
|
# force use of a LABEL:
|
|
|
|
# NOTE: Updating the grub config by hand once deployed should work, its just
|
|
|
|
# prepping it in a different environment that needs fiddling.
|
|
|
|
sed -i "s%$PART_DEV%LABEL=cloudimg-rootfs%" $GRUB_CFG
|
|
|
|
sed -i "s%search --no-floppy --fs-uuid --set=root .*$%search --no-floppy --set=root --label cloudimg-rootfs%" $GRUB_CFG
|
|
|
|
sed -i "s%root=UUID=[A-Za-z0-9\-]*%root=LABEL=cloudimg-rootfs%" $GRUB_CFG
|
2013-10-11 12:39:16 +00:00
|
|
|
if [ "$DIST" = 'Fedora' ] ; then
|
2013-08-27 10:00:03 +00:00
|
|
|
# enable serial console
|
|
|
|
sed -i "s%LABEL=cloudimg-rootfs%LABEL=cloudimg-rootfs console=tty0 console=ttyS0,115200%" $GRUB_CFG
|
|
|
|
if [ $(lsb_release -rs) = '19' ]; then
|
|
|
|
sed -i "s%UUID=[A-Za-z0-9\-]*%LABEL=cloudimg-rootfs%" /etc/fstab
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
DIB_EXTLINUX=${DIB_EXTLINUX:-0}
|
|
|
|
if [ "$DIB_EXTLINUX" != "0" ]; then
|
|
|
|
install_extlinux
|
|
|
|
else
|
|
|
|
install_grub2
|
2013-05-10 02:59:35 +00:00
|
|
|
fi
|