Skip to content

Commit 2c4b423

Browse files
committed
UEFI support
1 parent ad50c3f commit 2c4b423

File tree

6 files changed

+95
-19
lines changed

6 files changed

+95
-19
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
/latest-iso-minimal-x86_64-linux
33
/latest-iso-graphical-x86_64-linux
44
/nixos-in-place-test
5+
*.iso

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,14 @@ old system and all your old files still exist and are setup to mount on
2929
`/old-root/nixos` and `/` is rebound before spinning up the system; everything
3030
else in `/old-root` is fair game to delete.
3131

32-
NixOS installs GRUB2 on top of your existing boot loader. If you'd like to boot
33-
into `/old-root`, you can; you just need to add the GRUB entry, from
32+
If your system boots using MBR, NixOS installs GRUB2 on top of your existing boot loader.
33+
If you'd like to boot into `/old-root`, you can; you just need to add the GRUB entry, from
3434
`/old-root/boot/grub`, manually in your Nix files.
3535

36+
If your system boots using UEFI, NixOS adds systemd-boot efi image to your
37+
efi system partition, so your bios should let you choose between your old efi
38+
image and systemd-boot.
39+
3640
## Platform-specifics
3741
### Digital Ocean
3842
For use on DO droplets, follow the normal steps for your platform (Debian has

install

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,46 @@ check_existence "grep"
1818
check_existence "unsquashfs"
1919
check_existence "mktemp"
2020
check_existence "id"
21+
check_existence "head"
22+
check_existence "tail"
23+
check_existence "fdisk"
24+
check_existence "cat"
2125
log_end "seems sane"
2226

2327
## Setup essential variables; allow overriding with input
2428
root_mount=$(mount | grep "/ " | sed 's/ /\n/' | head -n1)
2529
root_type=$(mount | grep -Eo "/ type \w+" | sed 's/ /\n/g' | tail -n1)
26-
if grep '/dev/nvme' <<< $root_mount; then
27-
grub_device=$(echo $root_mount | sed "s|p[0-9]\+$||");
28-
elif grep '/dev/sd' <<< $root_mount; then
29-
grub_device=$(echo $root_mount | sed "s|[0-9]\+||");
30-
elif grep '/dev/vd' <<< $root_mount; then
31-
grub_device=$(echo $root_mount | sed "s|[0-9]\+||");
30+
31+
expect_one_line() {
32+
ERROR_MESSAGE_ZERO=$1
33+
ERROR_MESSAGE_MULTIPLE=$2
34+
(set -eu;
35+
IFS='' read -r x || (echo "$ERROR_MESSAGE_ZERO" >& 2; exit 1)
36+
(! IFS='' read -r) || (echo "$ERROR_MESSAGE_MULTIPLE" >& 2; exit 1)
37+
printf "%s\n" "$x");
38+
}
39+
40+
if [ -d /sys/firmware/efi ]; then
41+
boot_type=uefi
42+
boot_device=$(
43+
set -euo pipefail
44+
fdisk -l -o 'Device,Type' |
45+
grep 'EFI System' |
46+
sed -r 's_([^ ]*) *EFI System$_\1_' |
47+
expect_one_line \
48+
"Fdisk found no EFI System Partitions: please specify with -g option" \
49+
"Found multiple EFI System Partitions: please specify with -g option")
3250
else
33-
echo "Unable to determine your grub boot device! Please specify with the -g option."
51+
boot_type=mbr
52+
if grep '/dev/nvme' <<< "$root_mount"; then
53+
boot_device=$(echo "$root_mount" | sed "s|p[0-9]\+$||");
54+
elif grep '/dev/sd' <<< "$root_mount"; then
55+
boot_device=$(echo "$root_mount" | sed "s|[0-9]\+||");
56+
elif grep '/dev/vd' <<< "$root_mount"; then
57+
boot_device=$(echo "$root_mount" | sed "s|[0-9]\+||");
58+
else
59+
echo "Unable to determine your grub boot device! Please specify with the -g option."
60+
fi
3461
fi
3562
working_directory=$(mktemp -d)
3663

@@ -48,7 +75,7 @@ digitalocean=false
4875
while getopts ":g:r:t:Gdw:h" opt; do
4976
case $opt in
5077
g)
51-
grub_device=$OPTARG
78+
boot_device=$OPTARG
5279
;;
5380
r)
5481
root_mount=$OPTARG
@@ -100,9 +127,22 @@ then
100127
fi
101128
fi
102129

130+
boot_description=$(
131+
case $boot_type in
132+
uefi)
133+
echo "uefi boot with esp at $boot_device"
134+
;;
135+
mbr)
136+
echo "mbr at $boot_device"
137+
;;
138+
*)
139+
echo "bad boot type" >&2
140+
exit 1
141+
;;
142+
esac)
103143
## Give one last chance to back out
104144
log "NixOS installer (nixos-in-place)"
105-
log " GRUB => $grub_device"
145+
log " Boot => $boot_description"
106146
log " Root => $root_mount ($root_type)"
107147
log " ISO => $iso"
108148
log " Digital Ocean => $digitalocean"
@@ -134,7 +174,7 @@ pushd "$working_directory"
134174
135175
## Setup the chroot environment before install
136176
log "Embarking stage1!"
137-
"$here/stage1" "$here" "$root_mount" "$root_type" "$grub_device" "$digitalocean"
177+
"$here/stage1" "$here" "$root_mount" "$root_type" "$boot_type" "$boot_device" "$digitalocean"
138178
139179
## Minimize residual space usage
140180
# /var/empty is immutable https://github.com/NixOS/nixpkgs/pull/18365

stage1

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ set -eu
55
here=$1
66
root_mount=$2
77
root_type=$3
8-
grub_device=$4
9-
digitalocean=$5
8+
boot_type=$4
9+
boot_device=$5
10+
digitalocean=$6
1011

1112
## Bring in some helper functions
1213
. "$here/util"
@@ -28,7 +29,7 @@ log_end "$BASH"
2829

2930
## Don't run systemd on chroot start, run our stage2 script
3031
log "Patching init"
31-
sed -i "s,exec systemd,exec /$BASH -i /nixos-in-place/stage2 $root_mount $root_type $grub_device $digitalocean," "$INIT"
32+
sed -i "s,exec systemd,exec /$BASH -i /nixos-in-place/stage2 $root_mount $root_type $boot_type $boot_device $digitalocean," "$INIT"
3233
sed -i "s,starting systemd,starting /nixos-in-place/stage2," "$INIT"
3334

3435
## Don't try to remount / since we didn't do a proper phase 1
@@ -43,12 +44,27 @@ mount --bind /nixos nixos
4344
mkdir -p nixos-in-place
4445
mount --bind "$here" nixos-in-place
4546

47+
case "$boot_type" in
48+
mbr)
49+
true
50+
;;
51+
uefi)
52+
(ls -1 /sys/firmware/efi/efivars | grep -q .) || mount -t efivarfs efivarfs /sys/firmware/efi/efivars
53+
mkdir -p nixos/boot
54+
mount "$boot_device" nixos/boot
55+
;;
56+
*)
57+
echo "bad boot type" >&2
58+
exit 1
59+
esac
60+
4661
## Imbue the modified live CD environment so we can install
4762
log "Embarking stage2!"
4863
chroot . "/$INIT"
4964

5065
## Various platforms use different flags; just try what we can to clean up
5166
log "Cleaning up mounts"
67+
umount -f nixos/boot >/dev/null 2>&1 || true
5268
umount -fR nixos >/dev/null 2>&1 || true
5369
umount -f nixos >/dev/null 2>&1 || true
5470
umount -f nixos-in-place >/dev/null 2>&1 || true

stage2

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ set -eu
44

55
root_mount=$1
66
root_type=$2
7-
grub_device=$3
8-
digitalocean=$4
7+
boot_type=$3
8+
boot_device=$4
9+
digitalocean=$5
910
digitalocean_configs=""
1011

1112
if [ "$digitalocean" = "true" ];
@@ -47,12 +48,26 @@ nix-env -i pcre
4748
## Generate a base config
4849
nixos-generate-config --root /nixos
4950

51+
grub_device_config=$(
52+
case "$boot_type" in
53+
mbr)
54+
echo "boot.loader.grub.device = \"$boot_device\";\n"
55+
;;
56+
uefi)
57+
true
58+
;;
59+
*)
60+
echo "bad boot type" >&2
61+
exit 1
62+
;;
63+
esac)
64+
5065
cat <<EOF > /nixos/etc/nixos/nixos-in-place.nix
5166
{ config, pkgs, ... }:
5267
{
5368
## Everything below is generated from nixos-in-place; modify with caution!
5469
boot.kernelParams = ["boot.shell_on_fail"];
55-
boot.loader.grub.device = "$grub_device";
70+
$grub_device_config
5671
boot.loader.grub.storePath = "/nixos/nix/store";
5772
boot.initrd.supportedFilesystems = [ "$root_type" ];
5873
boot.initrd.postDeviceCommands = ''

util

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ show_help()
2020
log "usage: $0 [OPTION...]"
2121
log
2222
log " -h Show this help message"
23-
log " -g <dev> Set GRUB target device (default: ${grub_device:-undefined})"
23+
log " -g <dev> Set boot device (a device with MBR or EFI System Partition) (default: ${boot_device:-undefined})"
2424
log " -r <dev> Set root filesystem target device (default: ${root_mount:-undefined})"
2525
log " -t <type> Set root filesystem type (default: ${root_type:-undefined})"
2626
log " -G Use the graphical NixOS ISO (default: ${iso:-undefined})"

0 commit comments

Comments
 (0)