Skip to content

Commit

Permalink
Handle /dev/mmcblk0p1-style names; deal with ro efivarfs
Browse files Browse the repository at this point in the history
Bump version to 1.0.31
  • Loading branch information
sakaki- committed Oct 3, 2018
1 parent 1a2b10b commit ede8b9f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 12 deletions.
51 changes: 42 additions & 9 deletions buildkernel
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ shopt -s nullglob
# ********************** variables *********************
PROGNAME="$(basename "${0}")"
CONFFILE="/etc/${PROGNAME}.conf"
VERSION="1.0.30"
VERSION="1.0.31"
ETCPROFILE="/etc/profile"
DEFAULTEFIBOOTFILE="bootx64.efi"
EFIBOOTFILE="${DEFAULTEFIBOOTFILE}"
Expand Down Expand Up @@ -165,6 +165,11 @@ done

# running under EFI?
declare -i USINGEFI=0
# efivarfs mounted ro on entry? (OpenRC does, systemd doesn't)
declare -i ONENTRYROEFIVARFS=0
# and subsequently remounted rw?
declare -i EFIVARFSREMOUNTEDRW=0

# has support for kernel modules
declare -i USINGMODULES=1

Expand All @@ -184,6 +189,7 @@ cleanup_and_exit_with_code() {
set +e
trap - EXIT
umount_all_remembered_mountpoints
restore_efivarfs_mount_state
exit $1
}
fn_exists() {
Expand Down Expand Up @@ -354,6 +360,21 @@ umount_all_remembered_mountpoints() {
umount_and_forget "${M}"
done
}
ensure_efivarfs_rw_if_present() {
if ((USINGEFI==1 && ONENTRYROEFIVARFS==1)); then
warning "Temporarily remounting efivarfs read-write"
mount -o remount,rw "/sys/firmware/efi/efivars"
EFIVARFSREMOUNTEDRW=1
fi
}
restore_efivarfs_mount_state() {
if ((USINGEFI==1 && ONENTRYROEFIVARFS==1 && EFIVARFSREMOUNTEDRW==1)); then
warning "Remounting efivarfs read-only"
if mount -o remount,ro "/sys/firmware/efi/efivars"; then
EFIVARFSREMOUNTEDRW=0
fi
fi
}
check_is_luks_volume() {
cryptsetup isLuks "${1}" || die "Path '${1}' is not a LUKS volume"
}
Expand Down Expand Up @@ -456,9 +477,10 @@ setup_final_variables() {
# has been set explicitly in buildkernel.conf
KEYFILEPARTUUID="${KEYFILEPARTUUID:-${EFIPARTUUID}}"
KEYFILEPATHMAP="${PARTUUIDDEVDIR}/${KEYFILEPARTUUID}"
# get the real root filesystem type
# get the real root filesystem type if not specified
# falling back to ext4 if the findmnt-based lookup fails
if [[ ! -v CMDLINE_ROOTFSTYPE ]]; then
CMDLINE_ROOTFSTYPE="$(/bin/findmnt -n -o FSTYPE -S ${CMDLINE_REAL_ROOT})"
CMDLINE_ROOTFSTYPE="$(/bin/findmnt -n -o FSTYPE -S ${CMDLINE_REAL_ROOT} 2>/dev/null || echo ext4)"
fi
# we use path syntax rather than "=PARTUUID=" syntax, as more reliable
KERNEL_CMD_LINE="root=${CMDLINE_ROOT} crypt_root=${CRYPTPATHMAP} dolvm "
Expand Down Expand Up @@ -490,6 +512,12 @@ setup_final_variables() {
check_if_booted_under_efi() {
if [ -d "/sys/firmware/efi" ]; then
USINGEFI=1
# efivarfs mounted ro?
if findmnt "/sys/firmware/efi/efivars" --options "ro" &>/dev/null; then
ONENTRYROEFIVARFS=1
else
ONENTRYROEFIVARFS=0
fi
else
USINGEFI=0
fi
Expand Down Expand Up @@ -548,9 +576,10 @@ find_all_luks_partitions() {
local NEXTUUID="${NEXTPART##*/}"
local NEXTPARTNAME="$(readlink --canonicalize "${NEXTPART}")" # e.g. /dev/sda3
# nvme devices have paths of form e.g. /dev/nvme0n1p1
# mmc devices have paths of form e.g. /dev/mmcblk0p1
# standard drives have form e.g. /dev/sda3
if [[ ${NEXTPARTNAME} =~ ^/dev/nvme.*$ ]]; then
local NEXTDEVNAME="${NEXTPARTNAME%%p[[:digit:]]*}" # e.g. /dev/nvme0n1
if [[ ${NEXTPARTNAME} =~ ^/dev/.*[[:digit:]]p[[:digit:]]+$ ]]; then
local NEXTDEVNAME="${NEXTPARTNAME%%p[[:digit:]]*}" # e.g. /dev/nvme0n1, /dev/mmcblk0
else
local NEXTDEVNAME="${NEXTPARTNAME%%[[:digit:]]*}" # e.g. /dev/sda
fi
Expand Down Expand Up @@ -582,9 +611,10 @@ find_all_efi_system_partitions() {
local NEXTUUID="${NEXTPART##*/}"
local NEXTPARTNAME="$(readlink --canonicalize "${NEXTPART}")" # e.g. /dev/sda3
# nvme devices have paths of form e.g. /dev/nvme0n1p1
# mmc devices have paths of form e.g. /dev/mmcblk0p1
# standard drives have form e.g. /dev/sda3
if [[ ${NEXTPARTNAME} =~ ^/dev/nvme.*$ ]]; then
local NEXTDEVNAME="${NEXTPARTNAME%%p[[:digit:]]*}" # e.g. /dev/nvme0n1
if [[ ${NEXTPARTNAME} =~ ^/dev/.*[[:digit:]]p[[:digit:]]+$ ]]; then
local NEXTDEVNAME="${NEXTPARTNAME%%p[[:digit:]]*}" # e.g. /dev/nvme0n1, /dev/mmcblk0
else
local NEXTDEVNAME="${NEXTPARTNAME%%[[:digit:]]*}" # e.g. /dev/sda
fi
Expand Down Expand Up @@ -1457,9 +1487,10 @@ conform_efi_boot_order_if_possible() {
local PARTNAME="$(readlink --canonicalize "${EFIPARTPATH}")" # e.g. /dev/sda3
local DEVNAME
# nvme devices have paths of form e.g. /dev/nvme0n1p1
# mmc devices have paths of form e.g. /dev/mmcblk0p1
# standard drives have form e.g. /dev/sda3
if [[ ${PARTNAME} =~ ^/dev/nvme.*$ ]]; then
DEVNAME="${PARTNAME%%p[[:digit:]]*}" # e.g. /dev/nvme0n1
if [[ ${PARTNAME} =~ ^/dev/.*[[:digit:]]p[[:digit:]]+$ ]]; then
DEVNAME="${PARTNAME%%p[[:digit:]]*}" # e.g. /dev/nvme0n1, /dev/mmcblk0
else
DEVNAME="${PARTNAME%%[[:digit:]]*}" # e.g. /dev/sda
fi
Expand Down Expand Up @@ -1489,6 +1520,7 @@ conform_efi_boot_order_if_possible() {
fi
fi
if ((NEWBOOTENTRYNEEDED==1)); then
ensure_efivarfs_rw_if_present
# remove any existing bootnums with the same name first
declare -i I=0
for I in "${!EBOOTNAMES[@]}"; do
Expand All @@ -1499,6 +1531,7 @@ conform_efi_boot_order_if_possible() {
done
show "Adding ('${BOOTLABEL}') to top of the EFI boot list"
efibootmgr --create --disk "${DEVNAME}" --part "${PARTNUM}" --loader "${SMBOOTPATH}" --label "${BOOTLABEL}"
restore_efivarfs_mount_state
else
show "Appropriate entry already exists at the top of EFI boot list"
fi
Expand Down
9 changes: 7 additions & 2 deletions buildkernel.8
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.TH BUILDKERNEL 8 "Version 1.0.30: May 2018"
.TH BUILDKERNEL 8 "Version 1.0.31: October 2018"
.SH NAME
buildkernel \- build secure boot kernel, save to EFI system partition
.SH SYNOPSIS
Expand Down Expand Up @@ -83,7 +83,12 @@ backs up the old kernel and config on the EFI system partition, if any are prese
.IP \(bu 2
copies the newly built kernel (which is configured so as to be an EFI executable), into \fI/boot/efi/EFI/Boot/bootx64.efi\fR (the magic location expected by most UEFI BIOSes; you can override this \(em see \fBgenkenrnel.conf\fR(5)); and also copies the config to the same directory;
.IP \(bu 2
ensures that an EFI boot entry for the new kernel exists, and that it is placed at the top of the EFI boot order (N.B., it is only possible to do this if the system is currently booted under EFI);
ensures that an EFI boot entry for the new kernel exists, and that it
is placed at the top of the EFI boot order (N.B., it is only possible
to do this if the system is currently booted under EFI); note that
\fBbuildkernel\fR will temporarily make the special
\fI/sys/firmware/efi/efivars\fR filesystem read-write, if required for
these modifications to be made;
.IP \(bu 2
performs a filesystem sync and then unmounts the EFI system partition (if you so specify, see the \fB--unmount-at-end\fR option text).
.RE
Expand Down
11 changes: 10 additions & 1 deletion buildkernel.conf.5
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.TH BUILDKERNEL 5 "Version 1.0.30: May 2018"
.TH BUILDKERNEL 5 "Version 1.0.31: October 2018"
.SH NAME
buildkernel.conf \- a configuration file for \fBbuildkernel\fR(8)
.SH SYNOPSIS
Expand Down Expand Up @@ -184,6 +184,15 @@ intruct the kernel to XZ compress its integral initrams (the default
behaviour prior to version 1.0.30); doing so may cause boot
issues on certain systems with modest RAM.

Most users will not need to override the default.
.br
.TP
.BR CMDLINE_ROOTFSTYPE
If you wish to explicitly specify your root filesystem's type, do so
via this variable. Otherwise, \fBbuildkernel\fR will attempt to
automatically detect the filesystem type of \fBCMDLINE_REAL_ROOT\fR
(falling back to \fBext4\fR, in case of error).

Most users will not need to override the default.

.RE
Expand Down

0 comments on commit ede8b9f

Please sign in to comment.