diff --git a/server/ltsp-update-kernels b/server/ltsp-update-kernels index 9253d321..e2220942 100755 --- a/server/ltsp-update-kernels +++ b/server/ltsp-update-kernels @@ -13,6 +13,7 @@ # Oliver Grawert # 2009, Warren Togami # 2012, Alkis Georgopoulos +# 2018, Andrey Kvaps # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License as @@ -48,6 +49,46 @@ Options: EOF } +# List kernel versions in a descending order, while also respecting the e.g. +# LIST_KERNELS="generic generic-pae *" +# order that the user may have put in $CHROOT/etc/ltsp/update-kernels.conf. +# Distros are required to set e.g. KERNEL_PREFIX="vmlinuz-*', KERNEL_SUFFIX="". +# The * goes where the version is expected to go. +kernel_split() { + local orig_flags kernels arch loop_kernels kernel + # Save values of flags, and restore them later. + orig_flags=$(set +o) + + # Disable glob expansion for this function + set -f + for kernel in $(find "$BASE/$name/boot/" -type f -name "$KERNEL_PREFIX$KERNEL_SUFFIX" -printf "%f\n"); do + # Validate the "arch" + if [ "${LIST_KERNELS}" = "ALL" ]; then + LIST_KERNELS="*" + fi + for arch in ${LIST_KERNELS:-*}; do + case "$kernel" in + $KERNEL_PREFIX$arch$KERNEL_SUFFIX) + echo "$kernel" + break 1 + ;; + esac + done + done | sed -n "$KERNEL_NAMES" | sort -k 4,4V -k 3,3rV + + eval "$orig_flags" +} + +kernel_versions(){ + for arch in ${LIST_KERNELS:-"ALL"} ; do + LIST_KERNELS="$arch" kernel_split | awk '{print $3$4}' + done +} + +kernel_variants(){ + kernel_split | awk '{print $4}' | sort -u +} + trap_cleanup() { # Stop trapping trap - 0 HUP INT QUIT KILL SEGV PIPE TERM @@ -130,6 +171,157 @@ update_kernels() { cleanup_kernels "$name" "$tftpboot" "$chroot" link_kernel_flavors "$tftpboot/$name" + + # Configure grub + if [ -d /usr/lib/grub ]; then + [ -d /usr/lib/grub/i386-pc ] || echo "Skipping Grub pc bootloader installation. Install the grub-pc-bin package if you need it." + [ -d /usr/lib/grub/i386-efi ] || echo "Skipping Grub i386-efi bootloader installation. Install the grub-efi-ia32-bin package if you need it." + [ -d /usr/lib/grub/x86_64-efi ] || echo "Skipping Grub x86_64-efi bootloader installation. Install the grub-efi-amd64-bin package if you need it." + grub-mknetdir --net-directory="$tftpdir" --subdir="$TFTP_BOOT_DIR/$name/grub" && + generate_grub_config "$tftpboot/$name" + else + echo "Skipping Grub configuration. Install the grub-common package if you need it." + fi +} + +generate_grub_config() { + local tftpname last_flavor file name version flavor initrd + + tftpname=$1 + if [ ! -d "$tftpname" ]; then + echo "Directory $tftpname does not exist" + return 1 + fi + + # Ensure default values for BOOT_METHODS, CMDLINE_LINUX_DEFAULTS, CMDLINE_NFS + # and CMDLINE_NBD. Distros *should* ship an /etc/ltsp/update-kernels.conf with + # appropriate values for their distro. + BOOT_METHODS=${BOOT_METHODS:-"NFS NBD AOE"} + CMDLINE_LINUX_DEFAULTS=${CMDLINE_LINUX_DEFAULTS:-"ro init=/sbin/init-ltsp"} + CMDLINE_NFS=${CMDLINE_NFS:-"root=/dev/nfs ip=dhcp"} + CMDLINE_NBD=${CMDLINE_NBD:-"root=/dev/nbd0"} + CMDLINE_AOE=${CMDLINE_AOE:-"root=/dev/etherd/e0.0"} + + # Set a default BOOTPROMPT_OPTS using the first defined in BOOT_METHODS + boot_method_default=$(echo $BOOT_METHODS | awk '{print $1}') + cmdline_method_default=$(eval echo '$CMDLINE_'$boot_method_default) + BOOTPROMPT_OPTS="$CMDLINE_LINUX_DEFAULTS $cmdline_method_default" + + # Those defaults should work on debian-based distros, but shouldn't hurt + # elsewhere because they wouldn't match actual files and they'd be ignored. + KERNEL_NAMES=${KERNEL_NAMES:-'s/\(vmlinu[xz]-\)\([^-]*-[^-]*-\)\(.*\)/& \1 \2 \3/p'} + INITRD_NAME=${INITRD_NAME:-'s/vmlinu[xz]/initrd.img/p'} + GRUBCFG="$tftpdir/$TFTP_BOOT_DIR/$name/grub" + + # Remove all autogenerated menus. + rm -f $GRUBCFG/ltsp* $GRUBCFG/grub.cfg $GRUBCFG/memtest.cfg + + cat > $GRUBCFG/ltsp.cfg < $GRUBCFG/ltsp-$method.cfg < $GRUBCFG/ltsp-versions-$method.cfg <> $GRUBCFG/ltsp-versions-$method.cfg <> $GRUBCFG/ltsp-versions-$method.cfg < $GRUBCFG/memtest.cfg <> $GRUBCFG/ltsp.cfg + fi + done + ln -sf "ltsp.cfg" "$GRUBCFG/grub.cfg" + + # link lts.conf + for dir in i386-pc x86_64-efi i386-efi; do + mkdir -p "$GRUBCFG/$dir" + ln -sf "../../lts.conf" "$GRUBCFG/$dir/lts.conf" + done + } # Create symlinks for each kernel flavor in the tftp dir.