diff --git a/cdist/conf/type/__install_bootloader_grub/explorer/target_os b/cdist/conf/type/__install_bootloader_grub/explorer/target_os
new file mode 100755
index 00000000..f235710a
--- /dev/null
+++ b/cdist/conf/type/__install_bootloader_grub/explorer/target_os
@@ -0,0 +1,100 @@
+#!/bin/sh
+#
+# 2010-2011 Nico Schottelius (nico-cdist at schottelius.org)
+# 2014 Steven Armstrong (steven-cdist at armstrong.cc)
+#
+# This file is part of cdist.
+#
+# cdist is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# cdist is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with cdist. If not, see .
+#
+#
+# All os variables are lower case. Keep this file in alphabetical
+# order by os variable except in cases where order otherwise matters,
+# in which case keep the primary os and its derivatives together in
+# a block (see Debian and Redhat examples below).
+#
+
+chroot="$(cat "$__object/parameter/chroot")"
+
+if grep -q ^Amazon "$chroot/etc/system-release" 2>/dev/null; then
+ echo amazon
+ exit 0
+fi
+
+if [ -f "$chroot/etc/arch-release" ]; then
+ echo archlinux
+ exit 0
+fi
+
+if [ -f "$chroot/etc/cdist-preos" ]; then
+ echo cdist-preos
+ exit 0
+fi
+
+### Debian and derivatives
+if grep -q ^DISTRIB_ID=Ubuntu "$chroot/etc/lsb-release" 2>/dev/null; then
+ echo ubuntu
+ exit 0
+fi
+
+if [ -f "$chroot/etc/debian_version" ]; then
+ echo debian
+ exit 0
+fi
+###
+
+if [ -f "$chroot/etc/gentoo-release" ]; then
+ echo gentoo
+ exit 0
+fi
+
+if [ -f "$chroot/etc/openwrt_version" ]; then
+ echo openwrt
+ exit 0
+fi
+
+if [ -f "$chroot/etc/owl-release" ]; then
+ echo owl
+ exit 0
+fi
+
+### Redhat and derivatives
+if grep -q ^CentOS "$chroot/etc/redhat-release" 2>/dev/null; then
+ echo centos
+ exit 0
+fi
+
+if grep -q ^Fedora "$chroot/etc/redhat-release" 2>/dev/null; then
+ echo fedora
+ exit 0
+fi
+
+if [ -f "$chroot/etc/redhat-release" ]; then
+ echo redhat
+ exit 0
+fi
+###
+
+if [ -f "$chroot/etc/SuSE-release" ]; then
+ echo suse
+ exit 0
+fi
+
+if [ -f "$chroot/etc/slackware-version" ]; then
+ echo slackware
+ exit 0
+fi
+
+echo "Unknown OS" >&2
+exit 1
diff --git a/cdist/conf/type/__install_bootloader_grub/gencode-remote b/cdist/conf/type/__install_bootloader_grub/gencode-remote
index ed57331a..0db6dee6 100755
--- a/cdist/conf/type/__install_bootloader_grub/gencode-remote
+++ b/cdist/conf/type/__install_bootloader_grub/gencode-remote
@@ -1,6 +1,6 @@
#!/bin/sh
#
-# 2011 Steven Armstrong (steven-cdist at armstrong.cc)
+# 2011-2015 Steven Armstrong (steven-cdist at armstrong.cc)
#
# This file is part of cdist.
#
@@ -18,51 +18,74 @@
# along with cdist. If not, see .
#
-device="$(cat "$__object/parameter/device")"
+device="$(cat "$__object/parameter/device" 2>/dev/null || echo "/$__object_id")"
chroot="$(cat "$__object/parameter/chroot")"
+target_os=$(cat "$__object/explorer/target_os")
+
+mkdir "$__object/files"
+install_script="$__object/files/install_script"
+# Link file descriptor #6 with stdout
+exec 6>&1
+# Link stdout with $install_script
+exec > $install_script
+
+# Generate script to install bootloader on distro
+printf '#!/bin/sh -l\n'
+
+case "$target_os" in
+ ubuntu|debian)
+ if [ -s "$__global/explorer/efi" ]; then
+ # FIXME: untested. maybe also just run update-grub for EFI system?
+ printf 'grub-mkconfig --output=/boot/efi/EFI/%s/grub.cfg\n' "$target_os"
+ printf 'mkdir -p /boot/efi/EFI/BOOT\n'
+ printf 'cp /boot/efi/EFI/%s/grubx64.efi /boot/efi/EFI/BOOT/bootx64.efi' "$target_os"
+ else
+ printf 'grub-install "%s"\n' "$device"
+ printf 'update-grub\n'
+ fi
+ ;;
+ archlinux)
+ if [ -s "$__global/explorer/efi" ]; then
+ echo "EFI boot loader installation is on your operating system ($target_os) is currently not supported by this type (${__type##*/})." >&2
+ echo "Please contribute an implementation for it if you can." >&2
+ exit 1
+ else
+ printf 'grub-install "%s"\n' "$device"
+ # bugfix/workarround: rebuild initramfs
+ # FIXME: doesn't belong here
+ printf 'mkinitcpio -p linux\n'
+ printf 'grub-mkconfig -o /boot/grub/grub.cfg\n'
+ fi
+ ;;
+ centos)
+ if [ -s "$__global/explorer/efi" ]; then
+ printf 'grub2-mkconfig --output=/boot/efi/EFI/%s/grub.cfg\n' "$target_os"
+ printf 'mkdir -p /boot/efi/EFI/BOOT\n'
+ printf 'cp /boot/efi/EFI/%s/grubx64.efi /boot/efi/EFI/BOOT/bootx64.efi' "$target_os"
+ else
+ printf 'grub2-install "%s"\n' "$device"
+ printf 'grub2-mkconfig --output=/boot/grub2/grub.cfg\n'
+ fi
+ ;;
+ *)
+ echo "Your operating system ($target_os) is currently not supported by this type (${__type##*/})." >&2
+ echo "If you can, please contribute an implementation for it." >&2
+ exit 1
+ ;;
+esac
+# Restore stdout and close file descriptor #6.
+exec 1>&6 6>&-
+
cat << DONE
-os=\$(
-if grep -q ^DISTRIB_ID=Ubuntu ${chroot}/etc/lsb-release 2>/dev/null; then
- echo ubuntu
- exit 0
-fi
-
-if [ -f ${chroot}/etc/arch-release ]; then
- echo archlinux
- exit 0
-fi
-
-if [ -f ${chroot}/etc/debian_version ]; then
- echo debian
- exit 0
-fi
-)
-
# Ensure /tmp exists
[ -d "${chroot}/tmp" ] || mkdir -m 1777 "${chroot}/tmp"
# Generate script to run in chroot
-script=\$(mktemp "${chroot}/tmp/__install_bootloader_grub.XXXXXXXXXX")
-# Link file descriptor #6 with stdout
-exec 6>&1
-# Link stdout with \$script
-exec > \$script
-
-echo "#!/bin/sh -l"
-echo "grub-install $device"
-case \$os in
- archlinux)
- # bugfix/workarround: rebuild initramfs
- # FIXME: doesn't belong here
- echo "mkinitcpio -p linux"
- echo "grub-mkconfig -o /boot/grub/grub.cfg"
- ;;
- ubuntu|debian) echo "update-grub" ;;
-esac
-
-# Restore stdout and close file descriptor #6.
-exec 1>&6 6>&-
+script=\$(mktemp "${chroot}/tmp/${__type##*/}.XXXXXXXXXX")
+cat > \$script << script_DONE
+$(cat "$install_script")
+script_DONE
# Make script executable
chmod +x "\$script"
@@ -70,4 +93,5 @@ chmod +x "\$script"
# Run script in chroot
relative_script="\${script#$chroot}"
chroot "$chroot" "\$relative_script"
+rm -rf \$script
DONE
diff --git a/cdist/conf/type/__install_bootloader_grub/parameter/default/chroot b/cdist/conf/type/__install_bootloader_grub/parameter/default/chroot
new file mode 100644
index 00000000..ea8c4bf7
--- /dev/null
+++ b/cdist/conf/type/__install_bootloader_grub/parameter/default/chroot
@@ -0,0 +1 @@
+/target