Extlinux fallback

If GRUB2 is not available in the system a fallback to install Extlinux
will happen. This patch also allows you to enforce the use of Extlinux
exporting the DIB_EXTLINUX variable.

Change-Id: I88fa792a0bd19cacca167134f2054b11c807a451
This commit is contained in:
Lucas Alvares Gomes 2013-08-27 11:00:03 +01:00
parent b01f412c1e
commit 5dd6c4a834
3 changed files with 151 additions and 91 deletions

View File

@ -49,6 +49,7 @@ package_map = {
'openssh-client': 'openssh-clients', 'openssh-client': 'openssh-clients',
'qpidd': 'qpid-cpp-server', 'qpidd': 'qpid-cpp-server',
'qpid-client': 'qpid-cpp-client', 'qpid-client': 'qpid-cpp-client',
'extlinux': 'syslinux-extlinux',
} }
for arg in sys.argv[1:]: for arg in sys.argv[1:]:

View File

@ -1,5 +1,7 @@
Sets up a partitioned disk (rather than building just one filesystem with no Sets up a partitioned disk (rather than building just one filesystem with no
partition table). partition table).
The disk will have grub[2]-install run on it, and that assumes a functional By default the disk will have grub[2]-install run on it, in case GRUB2
grub[2] setup. is not available in the system a fallback to Extlinux will happen. It's
also possible to enforce the use of Extlinux exporting a DIB_EXTLINUX
varible to the environment.

View File

@ -6,28 +6,77 @@
set -e set -e
set -x set -x
install-packages grub-pc
# 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 [ -z "$GRUBNAME" ]; then
echo "NO grub-install or grub2-install found"
exit 1
fi
# FIXME: # FIXME:
[ -n "$IMAGE_BLOCK_DEVICE" ] [ -n "$IMAGE_BLOCK_DEVICE" ]
PART_DEV=$IMAGE_BLOCK_DEVICE PART_DEV=$IMAGE_BLOCK_DEVICE
BOOT_DEV=$(echo $IMAGE_BLOCK_DEVICE | sed -e 's/p1//') BOOT_DEV=$(echo $IMAGE_BLOCK_DEVICE | sed -e 's#p1##' | sed -e 's#mapper/##')
GRUB_OPTS=${GRUB_OPTS:""}
# XXX: This is buggy: function install_extlinux {
# - --target=i386-pc is invalid for non-i386/amd64 architectures install-packages extlinux
# - and for UEFI too.
# GRUB_OPTS="$GRUB_OPTS --target=i386-pc" echo "Installing Extlinux..."
if [[ ! $GRUB_OPTS == *--target* ]]; then
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)
elif [ -f /etc/debian_version ]; then
kernel=$(ls -1rv /boot/vmlinuz*generic | head -1)
initrd=$(ls -1rv /boot/initrd*generic | head -1)
else
echo "Unable to find kernel and initram"
exit 1
fi
cat > /boot/syslinux/syslinux.cfg<<_EOF_
DEFAULT linux
LABEL linux
KERNEL $kernel
APPEND rw root=LABEL=cloudimg-rootfs
INITRD $initrd
_EOF_
}
function install_grub2 {
install-packages grub-pc
# 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..."
GRUB_OPTS=${GRUB_OPTS:""}
# 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"
if [[ ! $GRUB_OPTS == *--target* ]]; then
# /sys/ comes from the host machine. If the host machine is using EFI # /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 # but the image being built doesn't have EFI boot-images installed we
# should set the --target to use a BIOS-based boot-image. # should set the --target to use a BIOS-based boot-image.
@ -57,50 +106,58 @@ if [[ ! $GRUB_OPTS == *--target* ]]; then
esac esac
fi fi
fi fi
fi fi
$GRUBNAME --modules="biosdisk part_msdos" $GRUB_OPTS $BOOT_DEV $GRUBNAME --modules="biosdisk part_msdos" $GRUB_OPTS $BOOT_DEV
# This might be better factored out into a per-distro 'install-bootblock' # This might be better factored out into a per-distro 'install-bootblock'
# helper. # helper.
if [ -f "/boot/grub/grub.cfg" ] ; then if [ -f "/boot/grub/grub.cfg" ] ; then
GRUB_CFG=/boot/grub/grub.cfg GRUB_CFG=/boot/grub/grub.cfg
elif [ -f "/boot/grub2/grub.cfg" ] ; then elif [ -f "/boot/grub2/grub.cfg" ] ; then
GRUB_CFG=/boot/grub2/grub.cfg GRUB_CFG=/boot/grub2/grub.cfg
fi fi
# If GRUB configuration file does not exist, generate one # If GRUB configuration file does not exist, generate one
if [ ! $GRUB_CFG ]; then if [ ! $GRUB_CFG ]; then
if [ -d /boot/grub2 ]; then if [ -d /boot/grub2 ]; then
GRUB_CFG=/boot/grub2/grub.cfg GRUB_CFG=/boot/grub2/grub.cfg
elif [ -d /boot/grub ]; then elif [ -d /boot/grub ]; then
GRUB_CFG=/boot/grub/grub.cfg GRUB_CFG=/boot/grub/grub.cfg
fi fi
grub2-mkconfig -o $GRUB_CFG grub2-mkconfig -o $GRUB_CFG
fi; fi;
DIST=`lsb_release -is` DIST=`lsb_release -is`
[ -n "$DIST" ] [ -n "$DIST" ]
RELEASE=`lsb_release -cs` RELEASE=`lsb_release -cs`
[ -n "$RELEASE" ] [ -n "$RELEASE" ]
# grub-mkconfig generates a config with the device in it, # grub-mkconfig generates a config with the device in it,
# This shouldn't be needed, but old code has bugs # This shouldn't be needed, but old code has bugs
if [ $RELEASE = 'precise' ] ; then if [ $RELEASE = 'precise' ] ; then
# Replace the search attempt with a hardcoded root as the Ubuntu reference # Replace the search attempt with a hardcoded root as the Ubuntu reference
# images use. # images use.
sed -i "s%search --no.*%%" $GRUB_CFG sed -i "s%search --no.*%%" $GRUB_CFG
sed -i "s%set root=.*%set root=(hd0,1)%" $GRUB_CFG sed -i "s%set root=.*%set root=(hd0,1)%" $GRUB_CFG
fi fi
# force use of a LABEL: # force use of a LABEL:
# NOTE: Updating the grub config by hand once deployed should work, its just # NOTE: Updating the grub config by hand once deployed should work, its just
# prepping it in a different environment that needs fiddling. # prepping it in a different environment that needs fiddling.
sed -i "s%$PART_DEV%LABEL=cloudimg-rootfs%" $GRUB_CFG 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%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 sed -i "s%root=UUID=[A-Za-z0-9\-]*%root=LABEL=cloudimg-rootfs%" $GRUB_CFG
if [ $DIST = 'Fedora' ] ; then if [ $DIST = 'Fedora' ] ; then
# enable serial console # enable serial console
sed -i "s%LABEL=cloudimg-rootfs%LABEL=cloudimg-rootfs console=tty0 console=ttyS0,115200%" $GRUB_CFG sed -i "s%LABEL=cloudimg-rootfs%LABEL=cloudimg-rootfs console=tty0 console=ttyS0,115200%" $GRUB_CFG
if [ $(lsb_release -rs) = '19' ]; then if [ $(lsb_release -rs) = '19' ]; then
sed -i "s%UUID=[A-Za-z0-9\-]*%LABEL=cloudimg-rootfs%" /etc/fstab sed -i "s%UUID=[A-Za-z0-9\-]*%LABEL=cloudimg-rootfs%" /etc/fstab
fi fi
fi
}
DIB_EXTLINUX=${DIB_EXTLINUX:-0}
if [ "$DIB_EXTLINUX" != "0" ]; then
install_extlinux
else
install_grub2
fi fi