[OpenWrt-Devel] [RFC 6/6] grub2: add preinit hook for bootloader upgrade

Tomasz Maciej Nowak tomek_n at o2.pl
Sun Jan 13 16:49:15 EST 2019


The current OpenWrt installations lack the necessary bits to upgrade
bootloader on sysupgrade, therefore add preinit hook which will perform
upgrade of bootloader on first boot.

Signed-off-by: Tomasz Maciej Nowak <tomek_n at o2.pl>
---
 package/boot/grub2/Makefile                   |  6 ++++++
 .../boot/grub2/files/81_upgrade_bootloader    | 20 +++++++++++++++++++
 2 files changed, 26 insertions(+)
 create mode 100644 package/boot/grub2/files/81_upgrade_bootloader

diff --git a/package/boot/grub2/Makefile b/package/boot/grub2/Makefile
index 4445a30ea8..a0ab36f328 100644
--- a/package/boot/grub2/Makefile
+++ b/package/boot/grub2/Makefile
@@ -143,6 +143,9 @@ define Package/grub2-generic/install
 		$(1)/usr/lib/grub/grub2/
 	printf 'BL=$(PKG_NAME)\nBL_VARIANT=generic\nBL_VERSION=$(PKG_VERSION)\nBL_RELEASE=$(PKG_RELEASE)\n' \
 		> $(1)/usr/lib/grub/grub2/bl-release
+
+	$(INSTALL_DIR) $(1)/lib/preinit
+	$(CP) ./files/81_upgrade_bootloader $(1)/lib/preinit/
 endef
 
 define Package/grub2-legacy/install
@@ -155,6 +158,9 @@ define Package/grub2-legacy/install
 		$(1)/usr/lib/grub/grub2/
 	printf 'BL=$(PKG_NAME)\nBL_VARIANT=legacy\nBL_VERSION=$(PKG_VERSION)\nBL_RELEASE=$(PKG_RELEASE)\n' \
 		> $(1)/usr/lib/grub/grub2/bl-release
+
+	$(INSTALL_DIR) $(1)/lib/preinit
+	$(CP) ./files/81_upgrade_bootloader $(1)/lib/preinit/
 endef
 
 $(eval $(call HostBuild))
diff --git a/package/boot/grub2/files/81_upgrade_bootloader b/package/boot/grub2/files/81_upgrade_bootloader
new file mode 100644
index 0000000000..c1f2325cc0
--- /dev/null
+++ b/package/boot/grub2/files/81_upgrade_bootloader
@@ -0,0 +1,20 @@
+#!/bin/sh
+
+upgrade_bootloader() {
+	local diskdev
+
+	. /lib/upgrade/common.sh
+
+	if [ ! -f /boot/bl-release ] && export_bootdevice && export_partdevice diskdev 0; then
+		echo "(hd0) /dev/$diskdev" > /tmp/device.map
+		echo "- bootloader upgrade -"
+		/usr/sbin/grub-bios-setup \
+			-m "/tmp/device.map" \
+			-d "/usr/lib/grub/grub2" \
+			-r "hd0,msdos1" \
+			"/dev/$diskdev" \
+		&& cp -f /usr/lib/grub/grub2/bl-release /boot/
+	fi
+}
+
+[ "$INITRAMFS" = "1" ] || boot_hook_add preinit_main upgrade_bootloader
-- 
2.20.1


_______________________________________________
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org
https://lists.openwrt.org/mailman/listinfo/openwrt-devel



More information about the openwrt-devel mailing list